Files
opencode2api/internal/identity/request.go
T
Empty 7f68c51d1d fix: mint free-tier session IDs with the client's descending Identifier encoding (#70)
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
2026-10-13 15:01:29 +08:00

159 lines
5.5 KiB
Go

// Package identity derives stable session identities and unique request IDs.
package identity
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"net/http"
"regexp"
"opencode2api/internal/jsonutil"
)
type RequestIDs struct {
Session string
Request string
Project string
ParentSession string
}
func DeriveRequestIDs(r *http.Request, body map[string]any) RequestIDs {
signal := jsonutil.FirstString(
r.Header.Get("x-opencode-session"),
r.Header.Get("x-session-affinity"),
r.Header.Get("X-Session-Id"),
r.Header.Get("x-session-id"),
r.Header.Get("conversation-id"),
jsonutil.StringAt(body, "conversation_id"),
jsonutil.StringAt(body, "metadata", "session_id"),
)
if signal == "" {
// Using the first user turn keeps a multi-turn conversation stable as its
// history grows while separating conversations with different beginnings.
signal = conversationSeed(body)
}
if signal == "" {
signal = jsonutil.StringAt(body, "previous_response_id")
}
if signal == "" || signal == `{}` {
signal = RandomID("fallback", 16)
}
session := CanonicalSessionID(signal)
projectSignal := jsonutil.FirstString(r.Header.Get("x-opencode-project"), jsonutil.StringAt(body, "metadata", "project_id"))
if projectSignal == "" {
projectSignal = "opencode2api:default-project"
}
parentSession := jsonutil.FirstString(
r.Header.Get("x-parent-session-id"),
jsonutil.StringAt(body, "metadata", "parent_session_id"),
)
return RequestIDs{
Session: session,
Request: RandomID("req", 16),
Project: StableID("prj", projectSignal),
ParentSession: parentSession,
}
}
func conversationSeed(body map[string]any) string {
if input, ok := body["input"].(string); ok && input != "" {
return input
}
for _, field := range []string{"messages", "input"} {
for _, raw := range jsonutil.SliceAt(body, field) {
item, ok := raw.(map[string]any)
if !ok || jsonutil.StringAt(item, "role") != "user" {
continue
}
encoded, _ := json.Marshal(item["content"])
if len(encoded) > 0 && string(encoded) != "null" {
return string(encoded)
}
}
}
return ""
}
func StableID(prefix, value string) string {
sum := sha256.Sum256([]byte(prefix + "\x00" + value))
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 rejects any other session shape with
// 403 FreeTierError.
var canonicalSessionPattern = regexp.MustCompile(`^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$`)
// descendingTimePartPattern matches the time part the official client mints
// with Identifier.descending(): the 48-bit value ~(ts_ms<<12|counter) in hex.
// Bitwise NOT sets the low 12 bits, so a genuine time part ends in
// 0xFFF - counter. Probing the live endpoint shows the free tier accepts
// counters 1-63 (low 12 bits 0xFC0-0xFFF) and rejects everything beyond with
// 403 FreeTierError even when the ses_ shape is right, so the pattern accepts
// exactly that window.
var descendingTimePartPattern = regexp.MustCompile(`^[0-9a-f]{9}f[c-f][0-9a-f]$`)
const base62Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
// CanonicalSessionID returns signal unchanged when it already carries an
// official OpenCode session minted by the client's Identifier.descending()
// (preserving upstream prompt-cache affinity). Any other downstream identity
// (UUIDs, foreign client sessions, legacy gateway sessions, conversation
// seeds) is deterministically re-encoded into that exact layout — inverted
// 48-bit millisecond timestamp with counter 1, so the low 12 hex digits are
// ffe — while keeping the same stable session per conversation. A re-encoded
// session passes the free tier's encoding check where the raw hash shape was
// rejected with 403 FreeTierError.
func CanonicalSessionID(signal string) string {
if canonicalSessionPattern.MatchString(signal) {
if descendingTimePartPattern.MatchString(signal[4:16]) {
return signal
}
// Right shape but a foreign encoding (raw hash or ascending ID):
// fall through and re-encode deterministically.
}
sum := sha256.Sum256([]byte("ses\x00" + signal))
timePart := descendingTimePart(sum[:6])
randomPart := base62Fixed(new(big.Int).SetBytes(sum[6:16]), 14)
return "ses_" + timePart + randomPart
}
// descendingTimePart renders six hash bytes as the official client's
// Identifier.descending() layout: a 36-bit timestamp shifted into the top of
// a 48-bit word, ORed with counter 1, then bitwise-NOT-ed so the low 12 hex
// digits read ffe.
func descendingTimePart(hash []byte) string {
current := (uint64(hash[0])<<28 | uint64(hash[1])<<20 | uint64(hash[2])<<12 |
uint64(hash[3])<<4 | uint64(hash[4])>>4) << 12
current |= 1
inverted := ^current & 0xFFFFFFFFFFFF
return hex.EncodeToString([]byte{
byte(inverted >> 40), byte(inverted >> 32), byte(inverted >> 24),
byte(inverted >> 16), byte(inverted >> 8), byte(inverted),
})
}
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 {
panic(fmt.Sprintf("crypto/rand failed: %v", err))
}
return prefix + "_" + hex.EncodeToString(buf)
}