Files
opencode2api/internal/gateway/probe_test.go
T
CheerC 6bb3487f7f 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.
2026-10-29 21:40:31 +08:00

38 lines
1.3 KiB
Go

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)
}
}
}