mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 16:04:01 +00:00
OpenCode's free tier started rejecting gateway requests with 403
FreeTierError ('free tier can only be used from within OpenCode') around
2026-10-11, on every lane (anonymous and Zen keys), while the official
client kept working.
Root cause, isolated against the live endpoint with 30+ controlled probes:
on top of the known agent-shape requirements (stream + core tools present,
canonical ses_ layout), the free tier now validates HOW the session ID's
12-hex time part is encoded. The official client mints session IDs with
Identifier.descending(): the 48-bit value ~(ts_ms << 12 | counter) in hex,
counter 1-48. Probing shows requests whose time part decodes to that layout
pass (timestamps themselves are barely checked; counter 100 or 4095 fails),
and any foreign encoding - the raw sha256 time part this gateway emitted,
or an ascending Identifier - fails with 403.
Fix CanonicalSessionID:
- Keep IDs whose time part already decodes to the client's descending
layout (genuine opencode sessions keep their upstream prompt-cache
affinity).
- Re-encode everything else deterministically: the hash timestamp moves to
the top 36 bits, counter 1 is ORed in, and the word is bitwise-NOT-ed, so
the low 12 hex digits read ffe exactly like the client. Same input still
maps to the same stable session per conversation; only the wire format
changed.
Verified end to end against opencode.ai/zen with an anonymous-lane gateway
build: chat completions (streaming and plain) return 200 across free
models, multi-turn conversations stay on one stable session, and
go build / go vet / go test pass.
Fixes #68
70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package identity
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
func TestCanonicalSessionIDKeepsGenuineDescendingIDs(t *testing.T) {
|
|
// Time parts the official client mints via Identifier.descending() with
|
|
// counters 1, 2, 16, 48: low 12 hex digits 0xFFF - counter.
|
|
genuine := []string{
|
|
"ses_ecc5beb7cffe2U0aluUnb5nnpZ",
|
|
"ses_ecc441adcffep73ndhq1lBqQim",
|
|
"ses_f02a5a612ffepq6RK15QX04Se0",
|
|
"ses_4892557ffffeDiICzp8gKhMR0u",
|
|
"ses_ecc1f76c9fef3zWGdD1GSCEwPf",
|
|
"ses_ecc1f76c9fcf3zWGdD1GSCEwPf",
|
|
"ses_ecc1f76c9fce3zWGdD1GSCEwPf",
|
|
}
|
|
for _, id := range genuine {
|
|
if got := CanonicalSessionID(id); got != id {
|
|
t.Fatalf("CanonicalSessionID(%q) = %q, want unchanged", id, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCanonicalSessionIDReencodesForeignEncodings(t *testing.T) {
|
|
// Right shape, wrong encoding: ascending ID (001 suffix), counter 100
|
|
// (9b1 suffix), and the raw-hash layout the gateway used to emit.
|
|
foreign := []string{
|
|
"ses_133a414840012U0aluUnb5nnpZ",
|
|
"ses_ecc1f76c9f9b3zWGdD1GSCEwPf",
|
|
"ses_088a704c30424N5kWDK0snKFbd",
|
|
}
|
|
pattern := regexp.MustCompile(`^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$`)
|
|
for _, id := range foreign {
|
|
got := CanonicalSessionID(id)
|
|
if !pattern.MatchString(got) {
|
|
t.Fatalf("CanonicalSessionID(%q) = %q, not canonical shape", id, got)
|
|
}
|
|
if !descendingTimePartPattern.MatchString(got[4:16]) {
|
|
t.Fatalf("CanonicalSessionID(%q) = %q, time part not descending-encoded", id, got)
|
|
}
|
|
if got == id {
|
|
t.Fatalf("CanonicalSessionID(%q) kept a foreign encoding", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCanonicalSessionIDDeterministic(t *testing.T) {
|
|
a := CanonicalSessionID("uuid-4f1d2a90-1234-abcd-5678-9abcdef01234")
|
|
b := CanonicalSessionID("uuid-4f1d2a90-1234-abcd-5678-9abcdef01234")
|
|
if a != b {
|
|
t.Fatalf("not deterministic: %q vs %q", a, b)
|
|
}
|
|
if !descendingTimePartPattern.MatchString(a[4:16]) {
|
|
t.Fatalf("re-encoded id %q lacks a descending time part", a)
|
|
}
|
|
}
|
|
|
|
func TestDescendingTimePartLayout(t *testing.T) {
|
|
// hash[0:5] = 00 00 00 00 01 -> 36-bit stamp 0x00000001 -> low 12 bits
|
|
// 0xFFF ^ 0x001 ... counter 1 ORed before the NOT: current = 0x1001,
|
|
// inverted low 12 bits = 0xFFE.
|
|
got := descendingTimePart([]byte{0, 0, 0, 0, 1, 0xFF})
|
|
if got[9:] != "ffe" {
|
|
t.Fatalf("descendingTimePart = %q, want suffix ffe", got)
|
|
}
|
|
}
|