c9cu (v0.1.1222, Pi, a group routed to OpenCode Zen's muse-spark-1.3-contributor-free on /v1/responses) got 403 FreeTierError "OpenCode's free tier can only be used from within OpenCode" on a setup that worked the day before. Zen now reads x-opencode-session as OpenCode's identifier.ts lays an id out (12 hex of ~(Date.now()*0x1000 + counter), then 14 base62) and refuses one whose counter isn't small. magpie's built-in sent a sha256 of the conversation cut to that shape, so its counter was anything. The zen-free plugin hit the same (#100 on magpie-community/plugins, 0.1.5); the built-in had the same code. Header diff against the real upstream, same request with curl: - magpie's ses_e9283da055b15JwOAra76bwUx4 (hash): 403 on /v1/responses and /v1/chat/completions. - ses_ecc5d8f28ffe9Cy14V8Em7lI2u from a real OpenCode 1.19.0 run, the old opencode/1.18.34 UA unchanged: 200 on /v1/responses. So it is the session, not the UA or the endpoint; muse-spark is still served on Responses (Chat answers 400 ModelProtocolUnsupported, as before). Now (opencode_client.go): - openCodeSession makes a real descending id from the current time and a per-millisecond counter, kept per conversation (the last 1000) so a conversation's turns share one session; one OpenCode could have made is passed on unchanged, a hash-shaped one is not. - x-opencode-request is an ascending msg_ id the same way. - the session also goes in x-opencode-session-id, x-session-affinity and X-Session-Id, as OpenCode's packages/core/src/session/ model-request.ts sends it, and the User-Agent is OpenCode 1.19.0's (opencode/1.19.0 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14, the latest on npm, as the plugin captured it). - Kilo's task/session id goes through the same openCodeSession (the Kilo CLI is OpenCode's fork and makes ids the same way). Run: - Through an in-process gateway under a sandbox HOME against the real opencode.ai/zen with the public key: muse-spark-1.3-contributor-free on /v1/responses with reasoning xhigh, streamed and not, big-pickle streamed on Chat, mimo-v2.6-flash-free on Chat: all 200 "pong". The same file on origin/main's code: all 403 FreeTierError, the reporter's message word for word. - TestOpenCodeSessionIsOpenCodes, TestOpenCodeSessionsKeptBounded and TestOpenCodeClientHeaders (now with a real 1.19.0 session) fail with the hash put back by hand. - gofmt -l, go vet (darwin, GOOS=linux, GOOS=windows -tags nogui) on internal/provider and internal/gateway. - go test -tags nogui -count=1 ./... under a temp HOME on baseeba718736(load average ~170): every package ok but provider (TestPluginModelsSayExpired, TempDir cleanup race, added to #1543), gateway and agent (10m timeout in TestNotAnAPIReplyFailsOver and TestOmpLevelSuffix). Rerun with -timeout 40m: provider, gateway and agent ok. - Rebased onto488569227(kilo.go's model paging, no overlap) and not rebuilt there: the disk was under 10G free. - Kilo Gateway, anonymous, nvidia/nemotron-3.5-lightning:free through the same in-process gateway: 200 twice.
108 lines
4.6 KiB
Go
108 lines
4.6 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") // OpenCode 1.19.0's own goes as it is
|
|
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"))
|
|
}
|
|
for _, h := range []string{"x-opencode-session-id", "x-session-affinity", "X-Session-Id"} {
|
|
if v := r.h.Get(h); v != r.h.Get("x-opencode-session") {
|
|
t.Errorf("zen %d: %s %q, x-opencode-session %q", i, h, v, r.h.Get("x-opencode-session"))
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
}
|