Files
magpie/internal/provider/opencode_client_test.go
T
yetone 8e6354714f provider: OpenCode Zen's free models answer again, with a session id OpenCode itself could have made (c9cu on Discord)
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 base
  eba718736 (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 onto 488569227 (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.
2026-10-13 14:03:58 +08:00

99 lines
3.6 KiB
Go

package provider
import (
"net/http"
"strconv"
"testing"
"time"
)
// openCodeDecode reads an id as OpenCode's identifier.ts lays it out: the
// time in milliseconds (kept to 36 bits) and the count of ids made in it.
func openCodeDecode(t *testing.T, id string, descending bool) (ms, counter uint64) {
t.Helper()
v, err := strconv.ParseUint(id[4:16], 16, 64)
if err != nil {
t.Fatalf("%q: %v", id, err)
}
if descending {
v = ^v & 0xffffffffffff
}
return v / 0x1000, v % 0x1000
}
// Zen's free tier answers 403 FreeTierError ("can only be used from within
// OpenCode") to a session id OpenCode couldn't have made (c9cu on Discord:
// muse-spark-1.3-contributor-free from Pi worked one day and not the
// next). Checked against opencode.ai with the same request:
// ses_e9283da055b15JwOAra76bwUx4, a sha256 cut to the shape that magpie
// sent, got 403; ses_ecc5d8f28ffe9Cy14V8Em7lI2u, from a real OpenCode
// 1.19.0 run, got 200. The session made is OpenCode's: descending, the
// time it was made, a small count.
func TestOpenCodeSessionIsOpenCodes(t *testing.T) {
before := uint64(time.Now().UnixMilli()) % (1 << 36)
id := openCodeSession("conversation-a")
after := uint64(time.Now().UnixMilli()) % (1 << 36)
if !openCodeIDRe.MatchString(id) || id[:4] != "ses_" || !openCodeRealSession(id) {
t.Fatalf("session %q", id)
}
if ms, n := openCodeDecode(t, id, true); ms < before || ms > after || n < 1 || n >= 16 {
t.Fatalf("session %q: time %d (want %d..%d), count %d", id, ms, before, after, n)
}
// one conversation keeps one session, another gets its own
if again, other := openCodeSession("conversation-a"), openCodeSession("conversation-b"); again != id || other == id {
t.Fatalf("sessions %q %q %q", id, again, other)
}
if a, b := openCodeSession(""), openCodeSession(""); a == b || !openCodeRealSession(a) {
t.Fatalf("no conversation: %q %q", a, b)
}
// OpenCode's own goes as it is; a hash cut to its shape doesn't
const real, hashed = "ses_ecc5d8f28ffe9Cy14V8Em7lI2u", "ses_e9283da055b15JwOAra76bwUx4"
if got := openCodeSession(real); got != real {
t.Fatalf("OpenCode's own session became %q", got)
}
if openCodeRealSession(hashed) {
t.Fatal("a hash passes for OpenCode's session")
}
if got := openCodeSession(hashed); got == hashed || !openCodeRealSession(got) {
t.Fatalf("a hashed session went on as %q", got)
}
h := http.Header{}
OpenCodeClient(h, "conversation-a")
if h.Get("x-opencode-session") != id || h.Get("x-opencode-session-id") != id || h.Get("User-Agent") != OpenCodeUserAgent {
t.Fatalf("headers %v", h)
}
if req := h.Get("x-opencode-request"); req[:4] != "msg_" || !openCodeIDRe.MatchString(req) {
t.Fatalf("request %q", req)
}
}
// Ids made in one millisecond count up, as OpenCode's do; a request's is
// ascending.
func TestOpenCodeIDsCountUp(t *testing.T) {
now := time.UnixMilli(1_790_000_000_000)
first, second := openCodeNewID("msg", false, now), openCodeNewID("msg", false, now)
if ms, n := openCodeDecode(t, first, false); ms != 1_790_000_000_000%(1<<36) || n != 1 {
t.Fatalf("%q: time %d count %d", first, ms, n)
}
if _, n := openCodeDecode(t, second, false); n != 2 || second <= first {
t.Fatalf("%q after %q: count %d", second, first, n)
}
}
// Only the last openCodeKeep conversations' sessions are kept.
func TestOpenCodeSessionsKeptBounded(t *testing.T) {
first := openCodeSession("bounded-0")
for i := 1; i <= openCodeKeep; i++ {
openCodeSession("bounded-" + strconv.Itoa(i))
}
openCodeIDs.Lock()
n := openCodeIDs.order.Len()
openCodeIDs.Unlock()
if n > openCodeKeep {
t.Fatalf("%d kept", n)
}
if openCodeSession("bounded-0") == first {
t.Fatal("the oldest conversation's session was still kept")
}
}