From 8185202382393628b64a347d0b88a791414ab01f Mon Sep 17 00:00:00 2001 From: 58 Date: Thu, 17 Sep 2026 14:07:36 +0800 Subject: [PATCH] fix: use canonical OpenCode session IDs for Zen free tier Upstream began rejecting anonymous free-tier requests whose x-opencode-session does not match ses_[0-9a-f]{12}[0-9A-Za-z]{14} with 403 FreeTierError. Map every downstream identity into that shape deterministically (preserving native sessions), and bump the impersonated client version to 1.18.31. --- internal/httpx/client.go | 2 +- internal/identity/request.go | 38 +++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/internal/httpx/client.go b/internal/httpx/client.go index 7cf868c..34fb9f2 100644 --- a/internal/httpx/client.go +++ b/internal/httpx/client.go @@ -6,5 +6,5 @@ import ( ) func UserAgent() string { - return fmt.Sprintf("opencode/1.18.21 (%s %s; %s)", runtime.GOOS, runtime.GOARCH, runtime.Version()) + return fmt.Sprintf("opencode/1.18.31 (%s %s; %s)", runtime.GOOS, runtime.GOARCH, runtime.Version()) } diff --git a/internal/identity/request.go b/internal/identity/request.go index 19d2ed3..316d2d1 100644 --- a/internal/identity/request.go +++ b/internal/identity/request.go @@ -7,7 +7,9 @@ import ( "encoding/hex" "encoding/json" "fmt" + "math/big" "net/http" + "regexp" "opencode2api/internal/jsonutil" ) @@ -40,7 +42,7 @@ func DeriveRequestIDs(r *http.Request, body map[string]any) RequestIDs { if signal == "" || signal == `{}` { signal = RandomID("fallback", 16) } - session := StableID("ses", signal) + session := CanonicalSessionID(signal) projectSignal := jsonutil.FirstString(r.Header.Get("x-opencode-project"), jsonutil.StringAt(body, "metadata", "project_id")) if projectSignal == "" { projectSignal = "opencode2api:default-project" @@ -81,6 +83,40 @@ func StableID(prefix, value string) string { return prefix + "_" + hex.EncodeToString(sum[:12]) } +// canonicalSessionPattern matches OpenCode's canonical session format: +// "ses_" + 12 lowercase hex timestamp characters + 14 Base62 characters. +// Since 2026-09-16 the Zen free tier (Authorization: Bearer public) rejects +// any other session shape with 403 FreeTierError. +var canonicalSessionPattern = regexp.MustCompile(`^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$`) + +const base62Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + +// CanonicalSessionID returns signal unchanged when it already carries an +// official OpenCode session (preserving upstream prompt-cache affinity). +// Any other downstream identity (UUIDs, foreign client sessions, legacy +// gateway sessions, conversation seeds) is deterministically hashed into the +// canonical shape so the same conversation keeps a stable session. +func CanonicalSessionID(signal string) string { + if canonicalSessionPattern.MatchString(signal) { + return signal + } + sum := sha256.Sum256([]byte("ses\x00" + signal)) + timePart := hex.EncodeToString(sum[:6]) + randomPart := base62Fixed(new(big.Int).SetBytes(sum[6:16]), 14) + return "ses_" + timePart + randomPart +} + +func base62Fixed(n *big.Int, width int) string { + base := big.NewInt(62) + out := make([]byte, width) + remainder := new(big.Int) + for i := width - 1; i >= 0; i-- { + n.DivMod(n, base, remainder) + out[i] = base62Alphabet[remainder.Int64()] + } + return string(out) +} + func RandomID(prefix string, size int) string { buf := make([]byte, size) if _, err := rand.Read(buf); err != nil {