provider: IsOpenCode reads the host without parsing a URL

The Providers page asks IsOpenCode for each model of each provider, and
it parsed "//"+host with url.Parse each time: url.parse was 20 MB of what
20 warm rounds of drift, state and providers allocated with the owner's
real accounts, OpenCodeURL's share 7.5 MB.

openCodeAt takes the host as Host gives it: whatever is before the last
'@' aside, an IPv6 literal never OpenCode's, the port aside, then the
same openCodeHost check OpenCodeURL ends in.

Run on mote (mote-run, Go 1.26.8):
- gofmt clean; go vet ok, also GOOS=windows and GOOS=linux.
- TestOpenCodeAtReadsTheHostAsAURLWould alone: ok. It checks openCodeAt
  against OpenCodeURL for 18 hosts (user info, ports, IPv6, a trailing
  dot, look-alikes such as opencode.ai@evil.com and opencode.ai.evil.com)
  and that a look allocates nothing.
- Broken by hand (openCodeAt answering through OpenCodeURL): red, "a look
  allocated 3 times, want 0".
- go test -timeout 45m ./internal/provider ./internal/gateway: ok
  (156s, 681s), at 606743831; this head is it rebased on main.

Real use: the owner's real-account sandbox, offline (proxy to :9), 20
warm rounds of /api/drift, /api/state and /api/providers, at this head:
- net/url.parse 20.5 MB -> 5 MB (what is left is openCodeGateway and
  others, not IsOpenCode); all allocated 371 MB -> 327 MB.
- /api/state and /api/drift bytes identical to 6a7a690be's (cmp);
  /api/providers differs only in each provider's "fetched" time.
This commit is contained in:
yetone
2026-11-01 05:42:21 +08:00
parent 5a56bc7d73
commit 3a3bdb9f23
2 changed files with 39 additions and 2 deletions
+22
View File
@@ -0,0 +1,22 @@
package provider
import "testing"
// IsOpenCode reads its host as OpenCodeURL reads a URL, with no URL
// parsed for it: it is asked for every model of the providers' page.
func TestOpenCodeAtReadsTheHostAsAURLWould(t *testing.T) {
for _, h := range []string{
"opencode.ai", "OpenCode.AI", "opencode.ai.", "opencode.ai:443",
"zen.opencode.ai", "api.opencode.ai:8443", "u:p@opencode.ai",
"u@zen.opencode.ai:1", "notopencode.ai", "opencode.ai.evil.com",
"evil.com", "evil.com@", "opencode.ai@evil.com", "[::1]:443",
"[::1]", "127.0.0.1:3425", "", "localhost",
} {
if got, want := openCodeAt(h), OpenCodeURL("//"+h); got != want {
t.Errorf("%q: %v, a URL says %v", h, got, want)
}
}
if n := testing.AllocsPerRun(100, func() { openCodeAt("u@zen.opencode.ai:8443") }); n != 0 {
t.Fatalf("a look allocated %v times, want 0", n)
}
}
+17 -2
View File
@@ -1878,8 +1878,23 @@ func (p Provider) Where() string {
// IsOpenCode reports whether the provider is OpenCode's gateway (Zen or Go),
// which routes and caches by conversation and turns away requests that do
// not name one in x-opencode-session.
func (p Provider) IsOpenCode() bool {
return OpenCodeURL("//" + p.Host()) // its port, if it names one, aside
func (p Provider) IsOpenCode() bool { return openCodeAt(p.Host()) }
// openCodeAt is OpenCodeURL for a host as Host gives it, its port and
// any user aside, read without parsing a URL: the providers' page asks
// IsOpenCode for each model, and url.Parse there was 7 MB of what twenty
// window polls allocated.
func openCodeAt(h string) bool {
if i := strings.LastIndexByte(h, '@'); i >= 0 {
h = h[i+1:]
}
if strings.HasPrefix(h, "[") {
return false // an IPv6 address
}
if i := strings.IndexByte(h, ':'); i >= 0 {
h = h[:i]
}
return openCodeHost(h)
}
// IsBedrock reports whether the provider is Amazon Bedrock: its runtime