mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
CanonicalSessionID re-encodes a conversation signal from the server key and the seed alone, so the upstream `x-opencode-session` was identical in every process on the same UTC day. The free tier keeps charging that session, so after a 429 a gateway restart no longer changed anything upstream: the same `ses_…` walked into the new exit's bucket and 429'd again. Mix a 16-byte nonce, minted once per process, into the hash. A running gateway still derives one stable session per conversation, while a restart re-mints the ids and starts a fresh upstream identity. Official client sessions are still forwarded verbatim. Refs #159, #158
34 lines
1.2 KiB
Go
34 lines
1.2 KiB
Go
package identity
|
|
|
|
import "testing"
|
|
|
|
// TestCanonicalSessionIDRestartSalt verifies that a synthesized session is
|
|
// stable within one process but changes when the process salt changes, which is
|
|
// what a daemon restart does.
|
|
func TestCanonicalSessionIDRestartSalt(t *testing.T) {
|
|
original := bootNonce
|
|
t.Cleanup(func() { bootNonce = original })
|
|
|
|
first := CanonicalSessionID("conversation seed")
|
|
if again := CanonicalSessionID("conversation seed"); again != first {
|
|
t.Fatalf("session id changed within one process: %q then %q", first, again)
|
|
}
|
|
if !isOfficialSessionID(first) {
|
|
t.Fatalf("session id %q is not accepted by the free tier pattern", first)
|
|
}
|
|
|
|
bootNonce = []byte("different boot")
|
|
if restarted := CanonicalSessionID("conversation seed"); restarted == first {
|
|
t.Fatalf("session id survived a restart: %q", restarted)
|
|
}
|
|
}
|
|
|
|
// TestCanonicalSessionIDKeepsOfficialID verifies that a client-minted official
|
|
// session is never re-salted, since its affinity belongs to the client.
|
|
func TestCanonicalSessionIDKeepsOfficialID(t *testing.T) {
|
|
official := "ses_012345678fc0ABCDEFGHIJKLMN"
|
|
if got := CanonicalSessionID(official); got != official {
|
|
t.Fatalf("official session rewritten: %q, want %q", got, official)
|
|
}
|
|
}
|