Files
magpie/internal/gateway/opencode_client_test.go
T
yetone f0f63bf6b8 provider: Zen's free models answer again: asked at opencode.ai/inference/v1, with stand-in tools that take a parameter, and without x-session-affinity (c9cu on Discord)
c9cu: "opencode muse 1.3 又不行了,magpie v0.1.1239". Zen's free tier moved and
tightened in mid-October 2026, found against the real upstream on 10-15:

- free models answer only at https://opencode.ai/inference/v1 (models.dev's
  opencode api is that URL now); OpenCodeFreeURL maps /zen/... there for a
  free model, never /zen/go or another host, in forwardOnce and the probe;
- the bash and read tools offered must each have at least one parameter:
  the stand-ins (zenfree.go, the probe) now carry OpenCode's own
  (OpenCodeToolSchemas);
- a request carrying x-session-affinity is answered 403 FreeTierError.
  OpenCode 1.19.0, captured through a forwarding proxy, sends Zen neither
  x-session-affinity nor X-Session-Id, so OpenCodeClient takes both out;
  the agent's own session id still names x-opencode-session.
  Bisected header by header: X-Session-Id, Accept and the UA version
  don't matter; x-session-affinity alone turns a 200 into a 403.

Live against the real Zen with the public key (a test file since removed):
muse via /v1/responses streamed (xhigh) and not, big-pickle on chat
streamed, mimo-v2.6-flash-free on chat: all 200; muse via /v1/messages 200.
The same code with x-session-affinity left in: 403 on every case.

Run: gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows
-tags nogui on internal/provider and internal/gateway; the full
go test -tags nogui -count=1 ./... in a sandbox HOME, whose one red was
gateway's TestOpenCodeClientHeaders still expecting the two headers
(updated here), then internal/gateway alone again: ok (678s).
New tests TestZenFreeAtInferenceURL, TestOpenCodeFreeURL and the probe's
non-empty properties check fail on the old code.

The community plugin got the same fix in magpie-community/plugins 5fc0a75
(zen-free 0.1.7); its npm publish is blocked on the repo's publisher setup.
2026-10-15 02:25:17 +08:00

113 lines
4.8 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// OpenCode Zen's free models answer 403 "OpenCode's free tier can only be
// used from within OpenCode" unless the request says it is OpenCode's: its
// User-Agent (1.18.0 or newer) and a session id of its form, as OpenCode
// sends them to a provider whose id starts with "opencode". Requests to
// OpenCode's gateway carry them, those to anyone else don't, and the body
// goes as the caller sent it either way.
func TestOpenCodeClientHeaders(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, p := range []provider.Provider{
{ID: "zen", Name: "Zen", Key: "public", Chat: "https://opencode.ai/zen/v1", Models: []string{"kimi-k2.6"}},
{ID: "other", Name: "Other", Key: "k", Chat: "https://other.test/v1", Models: []string{"kimi-k2.6"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
type seen struct {
h http.Header
body string
}
got := map[string][]seen{}
s := New()
s.client = &http.Client{Transport: countTransport(func(r *http.Request) (*http.Response, error) {
b, _ := io.ReadAll(r.Body)
r.Body.Close()
got[r.URL.Host] = append(got[r.URL.Host], seen{r.Header.Clone(), string(b)})
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/json"}},
Body: io.NopCloser(strings.NewReader(`{"id":"c","object":"chat.completion","model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"pong"},"finish_reason":"stop"}]}`))}, nil
})}
send := func(model, affinity string) {
t.Helper()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"`+model+`","messages":[{"role":"user","content":"Say pong"}],`+
`"tools":[{"type":"function","function":{"name":"bash","parameters":{"type":"object"}}},{"type":"function","function":{"name":"read","parameters":{"type":"object"}}}]}`))
req.Header.Set("User-Agent", "some-agent/1.0")
if affinity != "" {
req.Header.Set("x-session-affinity", affinity)
}
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%s: %d %s", model, rec.Code, rec.Body)
}
}
send("zen/kimi-k2.6", "")
send("zen/kimi-k2.6", "")
send("zen/kimi-k2.6", "ses_ecc5d8f28ffe9Cy14V8Em7lI2u") // the agent's own session id names the session, and the header itself stays behind
send("other/kimi-k2.6", "")
zen, other := got["opencode.ai"], got["other.test"]
if len(zen) != 3 || len(other) != 1 {
t.Fatalf("requests: %v", got)
}
id := regexp.MustCompile(`^(ses|msg)_[0-9a-f]{12}[0-9A-Za-z]{14}$`)
for i, r := range zen {
if ua := r.h.Get("User-Agent"); ua != provider.OpenCodeUserAgent {
t.Errorf("zen %d: User-Agent %q", i, ua)
}
if !id.MatchString(r.h.Get("x-opencode-session")) || !id.MatchString(r.h.Get("x-opencode-request")) ||
!strings.HasPrefix(r.h.Get("x-opencode-session"), "ses_") || !strings.HasPrefix(r.h.Get("x-opencode-request"), "msg_") {
t.Errorf("zen %d: session %q request %q", i, r.h.Get("x-opencode-session"), r.h.Get("x-opencode-request"))
}
if v := r.h.Get("x-opencode-session-id"); v != r.h.Get("x-opencode-session") {
t.Errorf("zen %d: x-opencode-session-id %q, x-opencode-session %q", i, v, r.h.Get("x-opencode-session"))
}
// OpenCode 1.19.0 sends Zen neither, and Zen's free tier answers 403
// FreeTierError to a request carrying x-session-affinity
for _, h := range []string{"x-session-affinity", "X-Session-Id"} {
if v := r.h.Get(h); v != "" {
t.Errorf("zen %d: %s %q went to Zen", i, h, v)
}
}
if r.h.Get("x-opencode-client") != "cli" || r.h.Get("x-opencode-project") != "global" {
t.Errorf("zen %d: client %q project %q", i, r.h.Get("x-opencode-client"), r.h.Get("x-opencode-project"))
}
if r.h.Get("Authorization") != "Bearer public" {
t.Errorf("zen %d: Authorization %q", i, r.h.Get("Authorization"))
}
if r.body != other[0].body {
t.Errorf("zen %d: body changed:\n%s\nvs\n%s", i, r.body, other[0].body)
}
}
// one conversation, one session; each request its own id
if zen[0].h.Get("x-opencode-session") != zen[1].h.Get("x-opencode-session") || zen[0].h.Get("x-opencode-request") == zen[1].h.Get("x-opencode-request") {
t.Errorf("session %q %q, request %q %q", zen[0].h.Get("x-opencode-session"), zen[1].h.Get("x-opencode-session"),
zen[0].h.Get("x-opencode-request"), zen[1].h.Get("x-opencode-request"))
}
if s := zen[2].h.Get("x-opencode-session"); s != "ses_ecc5d8f28ffe9Cy14V8Em7lI2u" {
t.Errorf("OpenCode's own session became %q", s)
}
for k := range other[0].h {
if strings.HasPrefix(strings.ToLower(k), "x-opencode-") {
t.Errorf("other provider got %s", k)
}
}
if ua := other[0].h.Get("User-Agent"); strings.HasPrefix(ua, "opencode/") {
t.Errorf("other provider got User-Agent %q", ua)
}
}