changed session id algo

This commit is contained in:
Luxferre
2026-09-18 19:44:59 +03:00
parent 0cc2987dcb
commit d5e69c4a0a
2 changed files with 2519 additions and 2130 deletions
+532 -181
View File
File diff suppressed because it is too large Load Diff
+47 -9
View File
@@ -1179,11 +1179,11 @@ func TestLLMNonStreamingAndHeaders(t *testing.T) {
} }
// Non-OpenCode endpoints keep the legacy session-affinity headers and must // Non-OpenCode endpoints keep the legacy session-affinity headers and must
// not receive any x-opencode-* headers. // not receive any x-opencode-* headers.
if gotLegacy != opencodeSessionID() { if !isOpencodeSessionID(gotLegacy) {
t.Errorf("X-Session-Id = %q, want %q", gotLegacy, opencodeSessionID()) t.Errorf("X-Session-Id = %q, want a ses_* id", gotLegacy)
} }
if gotAffinity != opencodeSessionID() { if gotAffinity != gotLegacy {
t.Errorf("x-session-affinity = %q, want %q", gotAffinity, opencodeSessionID()) t.Errorf("x-session-affinity = %q, want %q", gotAffinity, gotLegacy)
} }
if gotOCClient != "" || gotOCReq != "" { if gotOCClient != "" || gotOCReq != "" {
t.Errorf("unexpected x-opencode headers: client=%q request=%q", gotOCClient, gotOCReq) t.Errorf("unexpected x-opencode headers: client=%q request=%q", gotOCClient, gotOCReq)
@@ -1257,8 +1257,8 @@ func TestApplyLLMHeadersOpencode(t *testing.T) {
if got := req.Header.Get("x-opencode-project"); got != "global" { if got := req.Header.Get("x-opencode-project"); got != "global" {
t.Errorf("x-opencode-project = %q, want global", got) t.Errorf("x-opencode-project = %q, want global", got)
} }
if got := req.Header.Get("x-opencode-session"); got != opencodeSessionID() { if got := req.Header.Get("x-opencode-session"); !isOpencodeSessionID(got) {
t.Errorf("x-opencode-session = %q, want %q", got, opencodeSessionID()) t.Errorf("x-opencode-session = %q, want a ses_* id", got)
} }
if got := req.Header.Get("x-opencode-request"); got != "msg_test" { if got := req.Header.Get("x-opencode-request"); got != "msg_test" {
t.Errorf("x-opencode-request = %q, want msg_test", got) t.Errorf("x-opencode-request = %q, want msg_test", got)
@@ -1302,6 +1302,29 @@ func TestApplyLLMHeadersOpencodeAuth(t *testing.T) {
} }
} }
// isOpencodeSessionID reports whether sid matches the Zen session id shape
// emitted by opencodeSessionID: "ses_" + 12 lowercase hex chars (ending in
// "ffe") + 14 alphanumeric characters.
func isOpencodeSessionID(sid string) bool {
if !strings.HasPrefix(sid, "ses_") || len(sid) != 30 {
return false
}
for _, c := range sid[4:16] {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
return false
}
}
if sid[13:16] != "ffe" {
return false
}
for _, c := range sid[16:] {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
return false
}
}
return true
}
func TestOpencodeSessionID(t *testing.T) { func TestOpencodeSessionID(t *testing.T) {
sid := opencodeSessionID() sid := opencodeSessionID()
if !strings.HasPrefix(sid, "ses_") { if !strings.HasPrefix(sid, "ses_") {
@@ -1310,11 +1333,26 @@ func TestOpencodeSessionID(t *testing.T) {
if len(sid) != 30 { if len(sid) != 30 {
t.Fatalf("session id length = %d, want 30: %q", len(sid), sid) t.Fatalf("session id length = %d, want 30: %q", len(sid), sid)
} }
for _, c := range sid[4:] { // The 12 characters after the "ses_" prefix are lowercase hex (the real
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { // OpenCode ids carry a millisecond timestamp there, ending in "ffe").
t.Fatalf("invalid character %c in session id %q", c, sid) hexPart := sid[4:16]
for _, c := range hexPart {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
t.Fatalf("non-hex character %c in session id prefix %q", c, sid)
} }
} }
if hexPart[9:] != "ffe" {
t.Fatalf("session id hex marker = %q, want suffix ffe: %q", hexPart[9:], sid)
}
// The remaining 14 characters are alphanumeric.
for _, c := range sid[16:] {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
t.Fatalf("invalid character %c in session id tail %q", c, sid)
}
}
if opencodeSessionID() == sid {
t.Fatalf("two consecutive session ids are identical: %q", sid)
}
} }
func TestLLMNoAuthHeaderWhenNoKey(t *testing.T) { func TestLLMNoAuthHeaderWhenNoKey(t *testing.T) {