mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-12-03 07:32:47 +08:00
fix(antigravity): use signed int64 session IDs matching Hub format
- Generate random and deterministic session IDs as signed 64-bit decimal values, using FNV-1a for stable IDs. - Preserve negative signs in reasoning replay keys and test positive/negative ID coverage. Closes: #7857
This commit is contained in:
@@ -3,8 +3,10 @@ package executor
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -12,8 +14,8 @@ import (
|
||||
|
||||
"github.com/MoeForge/moego/json"
|
||||
"github.com/MoeForge/moego/ojson"
|
||||
"github.com/MoeForge/moego/regexp"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
|
||||
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
cliproxysession "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/session"
|
||||
@@ -740,10 +742,18 @@ func assertSchemaSanitizedAndPropertyPreserved(t *testing.T, params map[string]a
|
||||
}
|
||||
}
|
||||
|
||||
func assertSignedInt64RequestSessionID(t *testing.T, sessionID string) int64 {
|
||||
t.Helper()
|
||||
parsed, errParse := strconv.ParseInt(sessionID, 10, 64)
|
||||
if errParse != nil || strconv.FormatInt(parsed, 10) != sessionID {
|
||||
t.Fatalf("request.sessionId = %q, want canonical signed int64 decimal (err=%v)", sessionID, errParse)
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func TestAntigravityExecutorSynthesizedSessionIDScopedPerCredential(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
negativeDecimal := regexp.MustCompile(`^-[0-9]+$`)
|
||||
executor := NewAntigravityExecutor(&config.Config{})
|
||||
|
||||
captureSessionID := func(t *testing.T, authID, kind, model, userText string, metadata map[string]any) string {
|
||||
@@ -790,9 +800,7 @@ func TestAntigravityExecutorSynthesizedSessionIDScopedPerCredential(t *testing.T
|
||||
t.Fatalf("expected upstream request body to be captured for auth=%s kind=%s model=%s", authID, kind, model)
|
||||
}
|
||||
sessionID := ojson.GetBytes(capturedBody, "request.sessionId").String()
|
||||
if !negativeDecimal.MatchString(sessionID) {
|
||||
t.Fatalf("request.sessionId = %q, want negative int63 decimal", sessionID)
|
||||
}
|
||||
assertSignedInt64RequestSessionID(t, sessionID)
|
||||
return sessionID
|
||||
}
|
||||
|
||||
@@ -849,7 +857,6 @@ func TestAntigravityExecutorSynthesizedSessionIDScopedPerCredential(t *testing.T
|
||||
func TestAntigravityExecutorIncomingSessionIDScopedPerCredentialAndSubagent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
negativeDecimal := regexp.MustCompile(`^-[0-9]+$`)
|
||||
executor := NewAntigravityExecutor(&config.Config{})
|
||||
|
||||
captureSessionID := func(t *testing.T, authID, kind, model string, sourceFormat sdktranslator.Format, payload []byte, enrich bool, metadata map[string]any) string {
|
||||
@@ -899,9 +906,7 @@ func TestAntigravityExecutorIncomingSessionIDScopedPerCredentialAndSubagent(t *t
|
||||
t.Fatalf("expected upstream request body to be captured for auth=%s kind=%s model=%s", authID, kind, model)
|
||||
}
|
||||
sessionID := ojson.GetBytes(capturedBody, "request.sessionId").String()
|
||||
if !negativeDecimal.MatchString(sessionID) {
|
||||
t.Fatalf("request.sessionId = %q, want negative int63 decimal", sessionID)
|
||||
}
|
||||
assertSignedInt64RequestSessionID(t, sessionID)
|
||||
return sessionID
|
||||
}
|
||||
|
||||
@@ -1798,3 +1803,64 @@ func TestAntigravityRequests_StripUserProjectHeaderOnCrossOriginRedirect(t *test
|
||||
assertRedirectHops(t, hops)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAntigravitySessionIDDistributionIncludesPositiveAndNegative(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("generateSessionID emits signed int64 values with both signs", func(t *testing.T) {
|
||||
var pos, neg int
|
||||
for i := 0; i < 128; i++ {
|
||||
val := assertSignedInt64RequestSessionID(t, generateSessionID())
|
||||
if val > 0 {
|
||||
pos++
|
||||
} else if val < 0 {
|
||||
neg++
|
||||
}
|
||||
}
|
||||
if pos == 0 || neg == 0 {
|
||||
t.Fatalf("generateSessionID produced skewed signs: pos=%d neg=%d", pos, neg)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("generateStableSessionIDFromContentsForAuth matches FNV-1a signed int64 with both signs", func(t *testing.T) {
|
||||
var unauthPos, unauthNeg int
|
||||
var authPos, authNeg int
|
||||
for i := 0; i < 64; i++ {
|
||||
unauthPrompt := fmt.Sprintf("conversation-%d: explain topic", i)
|
||||
unauthDoc := ojson.NewDoc([]byte(fmt.Sprintf(`{"request":{"contents":[{"role":"user","parts":[{"text":%q}]}]}}`, unauthPrompt)))
|
||||
|
||||
unauthID := generateStableSessionIDDocForAuth(unauthDoc, "")
|
||||
wantUnauth := helps.HashAntigravitySessionID([]byte(unauthPrompt))
|
||||
if unauthID != wantUnauth {
|
||||
t.Fatalf("unauthenticated stable sessionId = %q, want %q", unauthID, wantUnauth)
|
||||
}
|
||||
unauthVal := assertSignedInt64RequestSessionID(t, unauthID)
|
||||
if unauthVal > 0 {
|
||||
unauthPos++
|
||||
} else if unauthVal < 0 {
|
||||
unauthNeg++
|
||||
}
|
||||
|
||||
// Sequential trailing-digit prompts still avalanche across both signs when auth-scoped.
|
||||
authPrompt := fmt.Sprintf("user prompt %d", i)
|
||||
authDoc := ojson.NewDoc([]byte(fmt.Sprintf(`{"request":{"contents":[{"role":"user","parts":[{"text":%q}]}]}}`, authPrompt)))
|
||||
authID := generateStableSessionIDDocForAuth(authDoc, "auth-a")
|
||||
wantAuth := helps.HashAntigravitySessionID([]byte(authPrompt + "\x00auth-a\x00cli-proxy-api:antigravity:content-session"))
|
||||
if authID != wantAuth {
|
||||
t.Fatalf("auth-scoped stable sessionId = %q, want %q", authID, wantAuth)
|
||||
}
|
||||
authVal := assertSignedInt64RequestSessionID(t, authID)
|
||||
if authVal > 0 {
|
||||
authPos++
|
||||
} else if authVal < 0 {
|
||||
authNeg++
|
||||
}
|
||||
}
|
||||
if unauthPos == 0 || unauthNeg == 0 {
|
||||
t.Fatalf("unauthenticated generateStableSessionIDDocForAuth produced skewed signs: pos=%d neg=%d", unauthPos, unauthNeg)
|
||||
}
|
||||
if authPos == 0 || authNeg == 0 {
|
||||
t.Fatalf("auth-scoped generateStableSessionIDDocForAuth produced skewed signs: pos=%d neg=%d", authPos, authNeg)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package executor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -860,9 +858,9 @@ func generateImageGenRequestID() string {
|
||||
|
||||
func generateSessionID() string {
|
||||
randSourceMutex.Lock()
|
||||
n := randSource.Int63n(9_000_000_000_000_000_000)
|
||||
n := int64(randSource.Uint64())
|
||||
randSourceMutex.Unlock()
|
||||
return "-" + strconv.FormatInt(n, 10)
|
||||
return strconv.FormatInt(n, 10)
|
||||
}
|
||||
|
||||
func generateStableSessionID(payload []byte) string {
|
||||
@@ -918,13 +916,13 @@ func generateStableSessionIDFromContentsForAuth(contents ojson.Result, authID st
|
||||
}
|
||||
var hashInput []byte
|
||||
if authID != "" {
|
||||
hashInput = []byte("cli-proxy-api:antigravity:content-session\x00" + authID + "\x00" + text)
|
||||
// Place variable text and authID before the fixed domain suffix so trailing-byte
|
||||
// changes avalanche into the sign bit of FNV-1a.
|
||||
hashInput = []byte(text + "\x00" + authID + "\x00cli-proxy-api:antigravity:content-session")
|
||||
} else {
|
||||
hashInput = []byte(text)
|
||||
}
|
||||
hash := sha256.Sum256(hashInput)
|
||||
value := int64(binary.BigEndian.Uint64(hash[:8])) & 0x7FFFFFFFFFFFFFFF
|
||||
stableID = "-" + strconv.FormatInt(value, 10)
|
||||
stableID = helps.HashAntigravitySessionID(hashInput)
|
||||
return false
|
||||
})
|
||||
if stableID != "" {
|
||||
|
||||
@@ -122,12 +122,7 @@ func antigravityRequestIsSymmetricForReasoningReplay(opts cliproxyexecutor.Optio
|
||||
func antigravityReasoningReplayScopeFromPayload(modelName string, payload []byte) antigravityReasoningReplayScope {
|
||||
sessionID := antigravityReplaySessionIDFromPayload(payload)
|
||||
if sessionID == "" {
|
||||
if stable := strings.TrimSpace(generateStableSessionID(payload)); stable != "" {
|
||||
sessionID = strings.TrimPrefix(stable, "-")
|
||||
if sessionID == "" {
|
||||
sessionID = stable
|
||||
}
|
||||
}
|
||||
sessionID = strings.TrimSpace(generateStableSessionID(payload))
|
||||
}
|
||||
if sessionID == "" {
|
||||
return antigravityReasoningReplayScope{}
|
||||
@@ -141,12 +136,7 @@ func antigravityReasoningReplayScopeFromPayload(modelName string, payload []byte
|
||||
func antigravityReasoningReplayScopeFromDoc(modelName string, doc *ojson.Doc) antigravityReasoningReplayScope {
|
||||
sessionID := antigravityReplaySessionIDFromDoc(doc)
|
||||
if sessionID == "" {
|
||||
if stable := strings.TrimSpace(generateStableSessionIDDoc(doc)); stable != "" {
|
||||
sessionID = strings.TrimPrefix(stable, "-")
|
||||
if sessionID == "" {
|
||||
sessionID = stable
|
||||
}
|
||||
}
|
||||
sessionID = strings.TrimSpace(generateStableSessionIDDoc(doc))
|
||||
}
|
||||
if sessionID == "" {
|
||||
return antigravityReasoningReplayScope{}
|
||||
|
||||
@@ -1367,8 +1367,28 @@ func TestAntigravityReasoningReplayScopeUsesStableSessionWithoutSessionId(t *tes
|
||||
if !scope.valid() {
|
||||
t.Fatal("scope should be valid from stable session hash")
|
||||
}
|
||||
if !strings.HasPrefix(scope.sessionKey, "session:") {
|
||||
t.Fatalf("sessionKey = %q", scope.sessionKey)
|
||||
wantKey := "session:" + generateStableSessionID(payload)
|
||||
if scope.sessionKey != wantKey {
|
||||
t.Fatalf("sessionKey = %q, want %q", scope.sessionKey, wantKey)
|
||||
}
|
||||
docScope := antigravityReasoningReplayScopeFromDoc("gemini-3-flash-agent", ojson.NewDoc(payload))
|
||||
if docScope.sessionKey != wantKey {
|
||||
t.Fatalf("doc sessionKey = %q, want %q", docScope.sessionKey, wantKey)
|
||||
}
|
||||
|
||||
// Verify negative signed FNV-1a session IDs keep their leading minus sign in replay keys.
|
||||
negPayload := []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"a"}]}]}}`)
|
||||
negStable := generateStableSessionID(negPayload)
|
||||
if !strings.HasPrefix(negStable, "-") {
|
||||
t.Fatalf("expected negative signed FNV-1a ID for test fixture, got %q", negStable)
|
||||
}
|
||||
negScope := antigravityReasoningReplayScopeFromPayload("gemini-3-flash-agent", negPayload)
|
||||
if negScope.sessionKey != "session:"+negStable {
|
||||
t.Fatalf("negative payload sessionKey = %q, want %q", negScope.sessionKey, "session:"+negStable)
|
||||
}
|
||||
negDocScope := antigravityReasoningReplayScopeFromDoc("gemini-3-flash-agent", ojson.NewDoc(negPayload))
|
||||
if negDocScope.sessionKey != "session:"+negStable {
|
||||
t.Fatalf("negative doc sessionKey = %q, want %q", negDocScope.sessionKey, "session:"+negStable)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package helps
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"hash/fnv"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -46,29 +45,39 @@ func stableProviderSessionUUID(provider string, kind string, identityValue strin
|
||||
return uuid.NewSHA1(uuid.NameSpaceOID, []byte(identity)).String()
|
||||
}
|
||||
|
||||
// DerivedAntigravitySessionID maps a derived session identity to Antigravity's negative decimal format.
|
||||
// HashAntigravitySessionID hashes input with 64-bit FNV-1a and formats the
|
||||
// signed int64 value as a base-10 decimal string, matching Antigravity Hub's
|
||||
// request.sessionId format (~50% positive, ~50% negative).
|
||||
func HashAntigravitySessionID(input []byte) string {
|
||||
h := fnv.New64a()
|
||||
_, _ = h.Write(input)
|
||||
return strconv.FormatInt(int64(h.Sum64()), 10)
|
||||
}
|
||||
|
||||
// DerivedAntigravitySessionID maps a derived session identity to Antigravity's
|
||||
// signed 64-bit decimal sessionId format.
|
||||
func DerivedAntigravitySessionID(metadataSets ...map[string]any) string {
|
||||
derivedID := DerivedSessionID(metadataSets...)
|
||||
if derivedID == "" {
|
||||
return ""
|
||||
}
|
||||
sum := sha256.Sum256([]byte("cli-proxy-api:antigravity:derived-session\x00" + derivedID))
|
||||
value := int64(binary.BigEndian.Uint64(sum[:8])) & 0x7FFFFFFFFFFFFFFF
|
||||
return "-" + strconv.FormatInt(value, 10)
|
||||
// Place the variable identity before the fixed domain suffix so trailing-byte
|
||||
// variations (e.g. sequential session or subagent IDs) still avalanche into the sign bit of FNV-1a.
|
||||
return HashAntigravitySessionID([]byte(derivedID + "\x00cli-proxy-api:antigravity:derived-session"))
|
||||
}
|
||||
|
||||
// AntigravitySessionIDForAuth deterministically maps the request's canonical
|
||||
// (or execution/derived) session identity scoped to a specific credential into
|
||||
// Antigravity's negative int63 decimal format ("-<int63>").
|
||||
// Antigravity's signed 64-bit decimal sessionId format.
|
||||
func AntigravitySessionIDForAuth(authID string, metadataSets ...map[string]any) string {
|
||||
kind, identityValue := antigravitySessionIdentity(metadataSets...)
|
||||
return formatAntigravitySessionIDForAuth(kind, authID, identityValue)
|
||||
}
|
||||
|
||||
// ScopeAntigravitySessionIDForAuth deterministically scopes an explicit
|
||||
// incoming request.sessionId to a specific credential in Antigravity's
|
||||
// negative int63 decimal format ("-<int63>"). When authID is empty, the
|
||||
// trimmed incoming sessionId is returned unchanged.
|
||||
// incoming request.sessionId to a specific credential in Antigravity's signed
|
||||
// 64-bit decimal sessionId format. When authID is empty, the trimmed incoming
|
||||
// sessionId is returned unchanged.
|
||||
func ScopeAntigravitySessionIDForAuth(authID, sessionID string) string {
|
||||
sessionID = strings.TrimSpace(sessionID)
|
||||
if sessionID == "" {
|
||||
@@ -87,10 +96,10 @@ func formatAntigravitySessionIDForAuth(kind, authID, identityValue string) strin
|
||||
return ""
|
||||
}
|
||||
authID = strings.TrimSpace(authID)
|
||||
input := strings.Join([]string{"cli-proxy-api", "antigravity", kind, authID, identityValue}, "\x00")
|
||||
sum := sha256.Sum256([]byte(input))
|
||||
value := int64(binary.BigEndian.Uint64(sum[:8])) & 0x7FFFFFFFFFFFFFFF
|
||||
return "-" + strconv.FormatInt(value, 10)
|
||||
// Place variable identity and authID before the fixed domain suffix so
|
||||
// sequential trailing bytes avalanche cleanly into the high sign bit of FNV-1a.
|
||||
input := strings.Join([]string{identityValue, authID, kind, "antigravity", "cli-proxy-api"}, "\x00")
|
||||
return HashAntigravitySessionID([]byte(input))
|
||||
}
|
||||
|
||||
func antigravitySessionIdentity(metadataSets ...map[string]any) (string, string) {
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
package helps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/MoeForge/moego/regexp"
|
||||
"github.com/google/uuid"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
)
|
||||
|
||||
func assertSignedInt64SessionID(t *testing.T, label, value string) int64 {
|
||||
t.Helper()
|
||||
parsed, errParse := strconv.ParseInt(value, 10, 64)
|
||||
if errParse != nil || strconv.FormatInt(parsed, 10) != value {
|
||||
t.Fatalf("%s = %q, want canonical signed int64 decimal (err=%v)", label, value, errParse)
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func TestDerivedSessionProviderMappings(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -28,9 +38,7 @@ func TestDerivedSessionProviderMappings(t *testing.T) {
|
||||
}
|
||||
|
||||
antigravityID := DerivedAntigravitySessionID(metadata)
|
||||
if matched := regexp.MustCompile(`^-[0-9]+$`).MatchString(antigravityID); !matched {
|
||||
t.Fatalf("Antigravity mapping = %q, want negative decimal", antigravityID)
|
||||
}
|
||||
assertSignedInt64SessionID(t, "Antigravity mapping", antigravityID)
|
||||
if repeated := DerivedAntigravitySessionID(metadata); repeated != antigravityID {
|
||||
t.Fatalf("Antigravity mapping is not stable: first=%q repeated=%q", antigravityID, repeated)
|
||||
}
|
||||
@@ -74,25 +82,19 @@ func TestDerivedSessionProviderMappingsRequireIdentity(t *testing.T) {
|
||||
func TestAntigravitySessionIDForAuth(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
negativeDecimal := regexp.MustCompile(`^-[0-9]+$`)
|
||||
|
||||
canonicalMeta := map[string]any{
|
||||
cliproxyexecutor.CanonicalSessionIDMetadataKey: "claude:sess-7706",
|
||||
cliproxyexecutor.ExecutionSessionMetadataKey: "exec-1",
|
||||
cliproxyexecutor.DerivedSessionIDMetadataKey: "ctx:v1:derived-1",
|
||||
}
|
||||
authAFirst := AntigravitySessionIDForAuth("auth-a", canonicalMeta)
|
||||
if !negativeDecimal.MatchString(authAFirst) {
|
||||
t.Fatalf("AntigravitySessionIDForAuth(auth-a) = %q, want negative decimal", authAFirst)
|
||||
}
|
||||
assertSignedInt64SessionID(t, "AntigravitySessionIDForAuth(auth-a)", authAFirst)
|
||||
if repeated := AntigravitySessionIDForAuth(" auth-a ", canonicalMeta); repeated != authAFirst {
|
||||
t.Fatalf("AntigravitySessionIDForAuth not stable across whitespace-trimmed authID: first=%q repeated=%q", authAFirst, repeated)
|
||||
}
|
||||
|
||||
authB := AntigravitySessionIDForAuth("auth-b", canonicalMeta)
|
||||
if !negativeDecimal.MatchString(authB) {
|
||||
t.Fatalf("AntigravitySessionIDForAuth(auth-b) = %q, want negative decimal", authB)
|
||||
}
|
||||
assertSignedInt64SessionID(t, "AntigravitySessionIDForAuth(auth-b)", authB)
|
||||
if authB == authAFirst {
|
||||
t.Fatalf("different credentials produced identical Antigravity session ID: %q", authB)
|
||||
}
|
||||
@@ -122,8 +124,6 @@ func TestAntigravitySessionIDForAuth(t *testing.T) {
|
||||
func TestScopeAntigravitySessionIDForAuth(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
negativeDecimal := regexp.MustCompile(`^-[0-9]+$`)
|
||||
|
||||
if got := ScopeAntigravitySessionIDForAuth("auth-a", " "); got != "" {
|
||||
t.Fatalf("ScopeAntigravitySessionIDForAuth(empty) = %q, want empty", got)
|
||||
}
|
||||
@@ -132,15 +132,81 @@ func TestScopeAntigravitySessionIDForAuth(t *testing.T) {
|
||||
}
|
||||
|
||||
authA := ScopeAntigravitySessionIDForAuth("auth-a", "-987654321")
|
||||
if !negativeDecimal.MatchString(authA) || authA == "-987654321" {
|
||||
t.Fatalf("ScopeAntigravitySessionIDForAuth(auth-a) = %q, want scoped negative decimal", authA)
|
||||
assertSignedInt64SessionID(t, "ScopeAntigravitySessionIDForAuth(auth-a)", authA)
|
||||
if authA == "-987654321" {
|
||||
t.Fatalf("ScopeAntigravitySessionIDForAuth(auth-a) = %q, want scoped value distinct from raw input", authA)
|
||||
}
|
||||
if repeated := ScopeAntigravitySessionIDForAuth(" auth-a ", " -987654321 "); repeated != authA {
|
||||
t.Fatalf("ScopeAntigravitySessionIDForAuth not stable across whitespace: first=%q repeated=%q", authA, repeated)
|
||||
}
|
||||
|
||||
authB := ScopeAntigravitySessionIDForAuth("auth-b", "-987654321")
|
||||
if !negativeDecimal.MatchString(authB) || authB == "-987654321" || authB == authA {
|
||||
t.Fatalf("ScopeAntigravitySessionIDForAuth(auth-b) = %q, want distinct scoped negative decimal (authA=%q)", authB, authA)
|
||||
assertSignedInt64SessionID(t, "ScopeAntigravitySessionIDForAuth(auth-b)", authB)
|
||||
if authB == "-987654321" || authB == authA {
|
||||
t.Fatalf("ScopeAntigravitySessionIDForAuth(auth-b) = %q, want distinct scoped signed int64 decimal (authA=%q)", authB, authA)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashAntigravitySessionIDMatchesFNV1a64AndIncludesBothSigns(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
referenceFNV1a64 := func(input []byte) string {
|
||||
var h uint64 = 0xcbf29ce484222325
|
||||
for _, b := range input {
|
||||
h = (h ^ uint64(b)) * 0x100000001b3
|
||||
}
|
||||
return strconv.FormatInt(int64(h), 10)
|
||||
}
|
||||
|
||||
samples := []string{"", "a", "hello", "session-1", "session-2", "cli-proxy-api\x00antigravity\x00canonical-session\x00auth-a\x00sess-1"}
|
||||
for _, sample := range samples {
|
||||
got := HashAntigravitySessionID([]byte(sample))
|
||||
want := referenceFNV1a64([]byte(sample))
|
||||
if got != want {
|
||||
t.Fatalf("HashAntigravitySessionID(%q) = %q, want %q", sample, got, want)
|
||||
}
|
||||
assertSignedInt64SessionID(t, fmt.Sprintf("HashAntigravitySessionID(%q)", sample), got)
|
||||
}
|
||||
|
||||
var derivedPos, derivedNeg int
|
||||
var authPos, authNeg int
|
||||
var scopePos, scopeNeg int
|
||||
for i := 0; i < 64; i++ {
|
||||
sid := fmt.Sprintf("session-%d", i)
|
||||
|
||||
derivedVal := assertSignedInt64SessionID(t, "DerivedAntigravitySessionID", DerivedAntigravitySessionID(map[string]any{
|
||||
cliproxyexecutor.DerivedSessionIDMetadataKey: "ctx:v1:" + sid,
|
||||
}))
|
||||
if derivedVal > 0 {
|
||||
derivedPos++
|
||||
} else if derivedVal < 0 {
|
||||
derivedNeg++
|
||||
}
|
||||
|
||||
authVal := assertSignedInt64SessionID(t, "AntigravitySessionIDForAuth", AntigravitySessionIDForAuth("auth-a", map[string]any{
|
||||
cliproxyexecutor.CanonicalSessionIDMetadataKey: "claude:" + sid,
|
||||
}))
|
||||
if authVal > 0 {
|
||||
authPos++
|
||||
} else if authVal < 0 {
|
||||
authNeg++
|
||||
}
|
||||
|
||||
scopeVal := assertSignedInt64SessionID(t, "ScopeAntigravitySessionIDForAuth", ScopeAntigravitySessionIDForAuth("auth-a", sid))
|
||||
if scopeVal > 0 {
|
||||
scopePos++
|
||||
} else if scopeVal < 0 {
|
||||
scopeNeg++
|
||||
}
|
||||
}
|
||||
|
||||
if derivedPos == 0 || derivedNeg == 0 {
|
||||
t.Fatalf("DerivedAntigravitySessionID produced skewed signs: pos=%d neg=%d", derivedPos, derivedNeg)
|
||||
}
|
||||
if authPos == 0 || authNeg == 0 {
|
||||
t.Fatalf("AntigravitySessionIDForAuth produced skewed signs: pos=%d neg=%d", authPos, authNeg)
|
||||
}
|
||||
if scopePos == 0 || scopeNeg == 0 {
|
||||
t.Fatalf("ScopeAntigravitySessionIDForAuth produced skewed signs: pos=%d neg=%d", scopePos, scopeNeg)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user