laomao04_65777 on Discord: Zen's free models answered 429 "Free usage limit reached. Free models work in OpenCode" to the built-in OpenCode Zen provider, while real OpenCode 2 on the same machine was served. magpie sent OpenCode 1.x's shape. It now sends what OpenCode 2.2.5 sent in a captured request (internal/provider/testdata/opencode-2.2.5-chat.json, from a recording proxy), as the zen-free plugin does since plugins d62193c and 8501988: - User-Agent opencode/latest/<v>/cli. <v> is the version OpenCode's updater (opencode.ai/update/api/latest/cli/npm) names, never below 2.2.5 and never anything but a plain x.y.z. It is read in the background at most every 6h, with a 3s timeout and no redirects. - x-opencode-project is OpenCode's project id for the folder the agent says it works in: sha1 of git-remote:host/path (origin, else the first remote; worktree .git files followed), else of directory:<root or folder>. It is no longer "global". - b3 and traceparent: one trace a conversation, a new span a request. - The session id goes in x-opencode-session, x-opencode-session-id, x-session-affinity and x-session-id. Accept is */*, and x-opencode-request is gone. - On a free request the agent's own headers (Claude Code's x-stainless-*, Codex's originator) are dropped. Only Content-Type and Anthropic-Version and Anthropic-Beta are kept. - The core tools are edit, glob, grep, read and shell with 2.2.5's schemas, strict false on Chat. Claude Code's Read, Edit, Glob and Grep are offered under those names. Its Bash is no longer bash: shell is a stub. - OpenCode's <env> block, word for word, for the agent's folder, is added to the system prompt unless it has one already (OpenCode's own). - Paths are the v2 catalog's: /inference/openai/v1 for Chat and Responses, and /inference/anthropic/v1 for Messages. The models list is unchanged. Every route takes this: chat, responses and messages through forwardOnce, the Test button's probe (tiny/zenFreeProbe), and the models list (listHeadersAt: UA and x-opencode-client). Sibling: paid Zen and OpenCode Go share OpenCodeClient, so they get 2.2.5's headers. Their /zen/v1 and /zen/go/v1 paths are left as they are: #4740. Run on mote (mote-run) at this tree: - gofmt -l . is clean. - GOOS=darwin and windows go vet ./... pass. GOOS=linux go vet passes for internal/provider and internal/gateway. The whole-tree Linux vet stops in Wails' cgo when run from macOS, and in WSL for want of gtk4, before it reaches this change. - go test ./internal/provider ./internal/gateway -count=1 -timeout 45m: ok provider (154s) and ok gateway (715s) on base788057340. After the rebase ontoa5a9cdf7b, which touched gateway.go and ir.go, the same tree passed again: ok provider (138s) and ok gateway (816s), with GOOS darwin, linux and windows go vet of both packages. - Rebased again onto4c815ac2a, which touched search.go and provider.go. At that head gofmt and the three-OS vet of both packages are clean, and -run 'OpenCode|Zen|Search|Searcher' passes in both packages. The full suites were not rerun after this last rebase. Broken by hand, each turning a test red (provider and gateway, -run 'OpenCode|Zen', on mote): - version 1.19.0: TestOpenCodeClientIs225 and TestOpenCodeVersionFromUpdater. - the old /inference/v1 path: TestOpenCodeFreeURL, TestZenFreeAsOpenCode225, TestZenFreeAtInferenceURL and TestZenPublicTestAsksFreeModels. - caller headers kept: TestZenFreeAsOpenCode225. - no env block: TestZenFreeAsOpenCode225. - no strict: TestZenFreeAtInferenceURL. - project "global": TestOpenCodeClientIs225, TestOpenCodeClientHeaders and TestZenFreeAsOpenCode225. - the same span on every request: TestOpenCodeClientIs225 and TestOpenCodeClientHeaders. - no 2.2.5 floor: TestOpenCodeVersionFromUpdater. - a changed read schema: TestOpenCodeToolsAre225s. Not tried: - A real 200 from a free model: this egress IP's free allowance is spent. The captured 2.2.5 request itself got 429. - Header order and case: Go's HTTP/1 writes headers sorted, and HTTP/2 lowercases them, so OpenCode's order cannot be copied. - Windows' project id against real OpenCode on Windows.
101 lines
3.8 KiB
Go
101 lines
3.8 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{"X-Session-Affinity": {"from-the-agent"}}
|
|
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)
|
|
}
|
|
// OpenCode 2.2.5 sends the session in x-session-affinity and
|
|
// x-session-id too, and no x-opencode-request (#3491)
|
|
if h.Get("x-session-affinity") != id || h.Get("X-Session-Id") != id || h.Get("x-opencode-request") != "" {
|
|
t.Fatalf("headers %v", h)
|
|
}
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
}
|