mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
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:
1 parent
4a4db10387
commit
6bb3487f7f
4 files changed
+93
-1
No files matched your search
@@ -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))
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user