From 3a3bdb9f2316a460a524b4ce313d7a5d1342539f Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 05:40:28 +0800 Subject: [PATCH] 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. --- internal/provider/opencode_at_test.go | 22 ++++++++++++++++++++++ internal/provider/provider.go | 19 +++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 internal/provider/opencode_at_test.go diff --git a/internal/provider/opencode_at_test.go b/internal/provider/opencode_at_test.go new file mode 100644 index 000000000..aba0aa1fe --- /dev/null +++ b/internal/provider/opencode_at_test.go @@ -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) + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index a65e0bc1c..ebb06da04 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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