Files
magpie/internal/gateway/opencode_go_free_test.go
T
yetone 02727188a4 gateway: a Zen free model asked on OpenCode Go is asked at Zen's free tier, else the refusal says where it is served (51bit.eth)
51bit.eth (Discord #general) ran Claude Code through magpie with
OpenCode's free models on a provider named OpenCode Go
(opencode.ai/zen/go). Every request failed "400 OpenCode Go: Upstream
request failed: Model is unavailable.", and a header of their own
(x-opencode-session) didn't help. The same models worked in OpenCode.

Cause: OpenCode's two gateways serve different free models. Zen's free
tier (/zen, the public key) serves the -free models and big-pickle; Go
(/zen/go, a Go key) serves only beam-free and iceman-contributor-free
(its /models on 2026-11-02). Go answers any model it doesn't serve 400
"Model is unavailable." before it reads the key. A user ends up with a
Zen free model under Go by typing it, or because a Go key /models
refuses makes the provider list models.dev's opencode-go catalog,
which names free models Go doesn't serve (#4900, not changed here).

What changed:
- provider.RetryOpenCodeFreeOnZen, called in forwardOnce for OpenCode
  URLs beside RetryZhipuCoding: a 400 "Model is unavailable" from Go
  for a model OpenCodeFree calls free is asked once more at Zen's free
  tier on the same API (openCodeGoToZenFree: /zen/go/v1/... and
  /inference/go/... to OpenCodeFreeURL's /inference/openai|anthropic),
  with the public key in Authorization (and x-api-key on Anthropic),
  never the user's Go key. Zen's 2xx is returned as is.
- Refused at Zen too, Go's 400 keeps its words and adds what Zen said
  and where the model is served: under the OpenCode Zen provider,
  which needs no key. A model that isn't free isn't asked at Zen; its
  400 says to pick one the OpenCode Go provider lists.
- docs/subsystems/providers-accounts.md: a row beside the GLM Coding
  Plan one.

Run (mote, through mote-run):
- go test ./internal/provider -run 'OpenCode|Zen' and
  ./internal/gateway -run 'OpenCodeGo|ZenFree': ok, at the pushed head
  after the rebase too.
- gofmt -l: clean. GOOS=linux and GOOS=windows go vet ./internal/provider
  ./internal/gateway: ok.
- go test ./internal/provider -count=1 -timeout 45m: ok (113.9s).
  go test ./internal/gateway -count=1 -timeout 45m: one red,
  TestCodexPoolSaysTheClientsVersion, which is #4807 (open, red in full
  runs on mote; this change touches no Codex path). Output added there.
- Real: a built magpie serve on mote, sandbox HOME, port 3591, an
  opencode-go provider with a made-up key, Claude Code-shaped
  /v1/messages:
  - deepseek-v4.1-flash-free (Zen's): Go 400, then asked at
    /inference/openai/v1/chat/completions with the public key; Zen
    answered 429 FreeUsageLimitError (#3491), and the message names
    both and the OpenCode Zen provider.
  - cat-jam-free (in the catalog, served by neither): Go 400, Zen 400,
    message says where.
  - glm-5.3 (Go's, paid): "Invalid credential" as before, Zen not asked.

Broken by hand:
- Retry call removed from forwardOnce: TestOpenCodeGoFreeModelAskedAtZen
  and TestOpenCodeGoUnavailableSaysWhere red (old message, one request).
- Key swap removed: TestOpenCodeGoFreeModelAskedAtZen red, Zen asked
  with "Bearer sk-go-users-own".

Not tried:
- A working Go key (the owner's is expired), so beam-free on Go
  end to end is only the fake.
- A Zen free reply end to end: from mote Zen's free tier answers 429
  FreeUsageLimitError to magpie with the built-in OpenCode Zen
  provider too (#3491).
- Gemini protocol: not asked at Zen (OpenCode has no Gemini free URL).
2026-11-02 17:08:45 +08:00

145 lines
6.0 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// goUnavailable is what OpenCode Go answered on 2026-11-02 to a model it
// doesn't serve (deepseek-v4.1-flash-free, Zen's), the same on its chat
// and Messages APIs, before it looked at the key.
const goUnavailable = `{"type":"error","error":{"type":"api_error","message":"Upstream request failed: Model is unavailable."}}`
type goAsk struct{ url, auth, apiKey string }
// streamFor is a stream saying text in the shape of the API path is on.
func streamFor(path, text string) string {
if strings.HasSuffix(path, "/chat/completions") {
return chatSSE(text, "", "")
}
return anthropicSSE(text)
}
// goFreeServer is a gateway whose provider go is OpenCode Go as the preset
// adds it, with a Go key, listing the models given; Go answers
// goUnavailable to every model but those in serves, and Zen's free tier
// answers zen (status, then body) to everything, a stream saying "from
// zen" when the status is 200.
func goFreeServer(t *testing.T, models, serves []string, zenStatus int, zenRefusal string) (*Server, func() []goAsk) {
t.Helper()
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "go", Name: "OpenCode Go", Preset: "opencode-go", Key: "sk-go-users-own",
Chat: "https://opencode.ai/zen/go/v1", Responses: "https://opencode.ai/zen/go/v1", Anthropic: "https://opencode.ai/zen/go",
Models: models}); err != nil {
t.Fatal(err)
}
var mu sync.Mutex
var asked []goAsk
s := New()
s.client = &http.Client{Transport: countTransport(func(r *http.Request) (*http.Response, error) {
b, _ := io.ReadAll(r.Body)
r.Body.Close()
mu.Lock()
asked = append(asked, goAsk{r.URL.Host + r.URL.Path, r.Header.Get("Authorization"), r.Header.Get("x-api-key")})
mu.Unlock()
model := ""
for _, m := range models {
if strings.Contains(string(b), `"model":"`+m+`"`) {
model = m
}
}
if strings.HasPrefix(r.URL.Path, "/zen/go/") {
for _, m := range serves {
if m == model {
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"text/event-stream"}},
Body: io.NopCloser(strings.NewReader(streamFor(r.URL.Path, "from go")))}, nil
}
}
return &http.Response{StatusCode: 400, Header: http.Header{"Content-Type": {"application/json"}},
Body: io.NopCloser(strings.NewReader(goUnavailable))}, nil
}
if zenStatus < 300 {
return &http.Response{StatusCode: zenStatus, Header: http.Header{"Content-Type": {"text/event-stream"}},
Body: io.NopCloser(strings.NewReader(streamFor(r.URL.Path, "from zen")))}, nil
}
return &http.Response{StatusCode: zenStatus, Header: http.Header{"Content-Type": {"application/json"}},
Body: io.NopCloser(strings.NewReader(zenRefusal))}, nil
})}
return s, func() []goAsk { mu.Lock(); defer mu.Unlock(); return append([]goAsk(nil), asked...) }
}
func goClaudeAsk(s *Server, model string) *httptest.ResponseRecorder {
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(
`{"model":"`+model+`","max_tokens":64,"stream":true,"messages":[{"role":"user","content":"hi"}],`+claudeTools+`}`))
req.Header.Set("anthropic-version", "2023-06-01")
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, req)
return rec
}
// 51bit.eth's case: Claude Code asking one of Zen's free models on an
// OpenCode Go provider. Go answers "Model is unavailable"; the request is
// asked again at Zen's free tier, where OpenCode asks it, with the public
// key and never the user's Go key, and Claude Code gets Zen's reply.
func TestOpenCodeGoFreeModelAskedAtZen(t *testing.T) {
s, asked := goFreeServer(t, []string{"deepseek-v4.1-flash-free"}, nil, 200, "")
rec := goClaudeAsk(s, "go/deepseek-v4.1-flash-free")
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "from zen") {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
a := asked()
// on the API Go was asked on, at Zen's free tier
want := map[string]string{"opencode.ai/zen/go/v1/messages": "opencode.ai/inference/anthropic/v1/messages",
"opencode.ai/zen/go/v1/chat/completions": "opencode.ai/inference/openai/v1/chat/completions"}
if len(a) != 2 || want[a[0].url] == "" || a[1].url != want[a[0].url] {
t.Fatalf("asked %+v", a)
}
z := a[1]
if z.auth != "Bearer public" || z.apiKey != "" && z.apiKey != "public" || strings.Contains(z.auth+z.apiKey, "sk-go") {
t.Fatalf("Zen asked with %+v", z)
}
}
// A free model Go serves itself stays on Go, asked once, with the Go key.
func TestOpenCodeGoOwnFreeModelStaysOnGo(t *testing.T) {
s, asked := goFreeServer(t, []string{"beam-free"}, []string{"beam-free"}, 500, `{"error":{"message":"zen asked"}}`)
rec := goClaudeAsk(s, "go/beam-free")
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "from go") {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
if a := asked(); len(a) != 1 || !strings.HasPrefix(a[0].url, "opencode.ai/zen/go/") {
t.Fatalf("asked %+v", a)
}
}
// Refused at Zen too, the refusal says what both said and where the model
// is served; a paid model Go doesn't serve isn't asked at Zen at all, and
// its refusal says to pick one of Go's.
func TestOpenCodeGoUnavailableSaysWhere(t *testing.T) {
s, asked := goFreeServer(t, []string{"cat-jam-free", "claude-opus-5"}, nil, 400,
`{"error":{"type":"server_error","message":"Upstream request failed: Model is unavailable."}}`)
rec := goClaudeAsk(s, "go/cat-jam-free")
body := rec.Body.String()
if rec.Code < 400 || !strings.Contains(body, "OpenCode Go doesn't serve cat-jam-free") || !strings.Contains(body, "OpenCode Zen provider, which needs no key") {
t.Fatalf("%d %s", rec.Code, body)
}
n := len(asked())
rec = goClaudeAsk(s, "go/claude-opus-5")
body = rec.Body.String()
if rec.Code < 400 || !strings.Contains(body, "OpenCode Go doesn't serve claude-opus-5: pick a model the OpenCode Go provider lists") {
t.Fatalf("%d %s", rec.Code, body)
}
for _, a := range asked()[n:] {
if !strings.HasPrefix(a.url, "opencode.ai/zen/go/") {
t.Fatalf("a paid model asked at %s", a.url)
}
}
}