fix(probe): give probes and the Playground bench their own identity (#162)

The hourly free-model probe presented one pinned identity: project
"opencode2api:default-project" together with a session derived from the
model name. The free tier meters per project id and egress, so every
deployment running the same probe shared one bucket; once a popular exit
had charged that bucket, every probe answered 429 FreeUsageLimitError and
the model was cached as unavailable for an hour while live traffic on the
same process and egress succeeded.

The Playground bench pinned a session indirectly: it sent no session
signal, so DeriveRequestIDs fell back to conversationSeed — the system
prompt plus the first user turn — and every click replaying the same
prompt produced the same session.

Neither path is a conversation, so neither should present one. Each now
mints fresh identity per attempt: a canonical session and a random project
id for the probe, a canonical session per Playground click. Canonical ids
are forwarded verbatim, so the scope mixin does not apply and the shape the
free tier accepts is what goes out.

Verified end to end with the admin UI enabled on a temporary instance.
Three Playground clicks before the change logged the same upstream session
three times (ses_ea4eb1680ffe0B8cZBh0nKJ03A); two clicks after logged
ses_eb2c888f1ffe4Y0DodMQdwvu6c and ses_ed8748efdffe2ez6IH352OdMpV, both
200. Probes on this build measured 8/8 probe_succeeded with a fresh
availability cache — they already succeeded on this exit before the change,
so this is hardening for the charged-bucket case in the report, not a
reproduction of it.
This commit is contained in:
CheerC authored and GitHub committed 2026-10-29 21:40:31 +08:00
1 parent 4a4db10387
commit 6bb3487f7f
4 files changed
+93 -1

No files matched your search

+12
View File
@@ -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))
}
+28
View File
@@ -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)
}
}
}
+16 -1
View File
@@ -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
+37
View File
@@ -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)
}
}
}