diff --git a/internal/admin/debug.go b/internal/admin/debug.go index aca5523..57609e2 100644 --- a/internal/admin/debug.go +++ b/internal/admin/debug.go @@ -10,6 +10,7 @@ import ( "opencode2api/internal/config" "opencode2api/internal/gateway" "opencode2api/internal/httpx" + "opencode2api/internal/identity" "opencode2api/internal/jsonutil" modelcatalog "opencode2api/internal/models" "opencode2api/internal/protocol" @@ -144,6 +145,11 @@ func (a *Server) handleDebugInference(w http.ResponseWriter, r *http.Request) { } request.Header.Set("Content-Type", "application/json") request.Header.Set("Accept", "application/json") + // A Playground request is not a conversation: it replays the same prompt on + // every click, so a session derived from the prompt stays one bucket until + // it is charged and every later click answers 429 while live traffic + // succeeds. A fresh session per click keeps the bench on its own bucket. + request.Header.Set("x-opencode-session", benchSession()) recorder := debugResponseRecorder() started := time.Now() a.manager.Handler().ServeHTTP(recorder, request) @@ -301,3 +307,9 @@ func sensitiveDebugKey(key string) bool { } return false } + +// benchSession returns a fresh upstream session for one Playground request. It +// is canonical so the gateway forwards it verbatim instead of re-encoding it. +func benchSession() string { + return identity.CanonicalSessionID(identity.RandomID("playground", 16)) +} diff --git a/internal/admin/debug_test.go b/internal/admin/debug_test.go new file mode 100644 index 0000000..93e11dd --- /dev/null +++ b/internal/admin/debug_test.go @@ -0,0 +1,28 @@ +package admin + +import ( + "testing" + + "opencode2api/internal/identity" +) + +// TestBenchSessionIsFresh pins the session the Playground bench sends upstream. +// The bench replays one prompt per click, so a session derived from the prompt +// would reuse a single bucket until it is charged and every later click would +// answer 429 while live traffic succeeded. +func TestBenchSessionIsFresh(t *testing.T) { + first := benchSession() + second := benchSession() + + if first == "" { + t.Fatal("empty session") + } + if first == second { + t.Errorf("two bench requests reused session %q", first) + } + for _, session := range []string{first, second} { + if reencoded := identity.CanonicalSessionID(session); reencoded != session { + t.Errorf("bench session %q is not canonical (re-encoded to %q)", session, reencoded) + } + } +} diff --git a/internal/gateway/probe.go b/internal/gateway/probe.go index d75eef0..48f0cdf 100644 --- a/internal/gateway/probe.go +++ b/internal/gateway/probe.go @@ -88,6 +88,21 @@ func probePayload(model string, protocol wire.Protocol, effort string, v1 bool) // effort it accepts on this protocol. The reason is model_unavailable only // when every lane reported it; any other failure keeps the result // inconclusive. +// probeRequestIDs returns the upstream identity of one probe. A probe is not a +// conversation, so it must not present a pinned one: a fixed project id and a +// session derived from the model name are one bucket shared with every +// deployment that runs the same probe, and once that bucket is charged on a +// popular exit the probe answers 429 while live traffic on the same process and +// egress succeeds. Fresh identity per probe keeps each check on its own bucket; +// the availability cache already holds the answer for the next interval, so +// nothing is gained by reusing one. +func probeRequestIDs() identity.RequestIDs { + return identity.RequestIDs{ + Session: identity.CanonicalSessionID(identity.RandomID("availability", 16)), + Project: identity.RandomProjectID(), + } +} + func (g *Gateway) probeFreeModel(ctx context.Context, model string) probeResult { if !g.catalog.IsFreeModel(model) { return probeResult{reason: "not_free"} @@ -97,7 +112,7 @@ func (g *Gateway) probeFreeModel(ctx context.Context, model string) probeResult if !d.AvailableZen || !g.catalog.Supported(model) { return probeResult{reason: "unsupported_probe"} } - ids := identity.RequestIDs{Session: identity.CanonicalSessionID("availability:" + model), Project: "opencode2api:default-project"} + ids := probeRequestIDs() type lane struct { key, name, baseURL string client *http.Client diff --git a/internal/gateway/probe_test.go b/internal/gateway/probe_test.go new file mode 100644 index 0000000..559c3c3 --- /dev/null +++ b/internal/gateway/probe_test.go @@ -0,0 +1,37 @@ +package gateway + +import ( + "testing" + + "opencode2api/internal/identity" +) + +// TestProbeRequestIDsAreFresh pins the identity a probe presents upstream. A +// pinned project or session is one quota bucket shared with every deployment +// that runs the same probe, so a charged bucket on a popular exit made every +// probe 429 while live traffic on the same process and egress succeeded. +func TestProbeRequestIDsAreFresh(t *testing.T) { + first := probeRequestIDs() + second := probeRequestIDs() + + if first.Session == second.Session { + t.Errorf("two probes reused session %q", first.Session) + } + if first.Project == second.Project { + t.Errorf("two probes reused project %q", first.Project) + } + for _, ids := range []identity.RequestIDs{first, second} { + if ids.Session == "" || ids.Project == "" { + t.Fatalf("incomplete identity: %+v", ids) + } + // CanonicalSessionID leaves an official session untouched, so an + // idempotent round trip means the probe already sends the shape the + // free tier accepts instead of relying on the gateway to re-encode it. + if reencoded := identity.CanonicalSessionID(ids.Session); reencoded != ids.Session { + t.Errorf("probe session %q is not canonical (re-encoded to %q)", ids.Session, reencoded) + } + if len(ids.Project) != 40 { + t.Errorf("probe project %q is not a 40-character id", ids.Project) + } + } +}