diff --git a/internal/cache/signature_cache.go b/internal/cache/signature_cache.go index 12f19cf0..c1326629 100644 --- a/internal/cache/signature_cache.go +++ b/internal/cache/signature_cache.go @@ -3,6 +3,7 @@ package cache import ( "crypto/sha256" "encoding/hex" + "sort" "sync" "time" ) @@ -89,20 +90,17 @@ func CacheSignature(sessionID, text, signature string) { ts time.Time }{key, entry.Timestamp}) } - // Simple approach: remove first quarter of entries + // Sort by timestamp (oldest first) using sort.Slice + sort.Slice(oldest, func(i, j int) bool { + return oldest[i].ts.Before(oldest[j].ts) + }) + toRemove := len(oldest) / 4 if toRemove < 1 { toRemove = 1 } - // Sort by timestamp (oldest first) - simple bubble for small N + for i := 0; i < toRemove; i++ { - minIdx := i - for j := i + 1; j < len(oldest); j++ { - if oldest[j].ts.Before(oldest[minIdx].ts) { - minIdx = j - } - } - oldest[i], oldest[minIdx] = oldest[minIdx], oldest[i] delete(sc.entries, oldest[i].key) } } diff --git a/internal/translator/antigravity/claude/antigravity_claude_response.go b/internal/translator/antigravity/claude/antigravity_claude_response.go index 939551ba..8f47b9bf 100644 --- a/internal/translator/antigravity/claude/antigravity_claude_response.go +++ b/internal/translator/antigravity/claude/antigravity_claude_response.go @@ -9,8 +9,6 @@ package claude import ( "bytes" "context" - "crypto/sha256" - "encoding/hex" "fmt" "strings" "sync/atomic" @@ -46,27 +44,6 @@ type Params struct { CurrentThinkingText strings.Builder // Accumulates thinking text for signature caching } -// deriveSessionIDFromRequest generates a stable session ID from the request JSON. -func deriveSessionIDFromRequest(rawJSON []byte) string { - messages := gjson.GetBytes(rawJSON, "messages") - if !messages.IsArray() { - return "" - } - for _, msg := range messages.Array() { - if msg.Get("role").String() == "user" { - content := msg.Get("content").String() - if content == "" { - content = msg.Get("content.0.text").String() - } - if content != "" { - h := sha256.Sum256([]byte(content)) - return hex.EncodeToString(h[:16]) - } - } - } - return "" -} - // toolUseIDCounter provides a process-wide unique counter for tool use identifiers. var toolUseIDCounter uint64 @@ -92,7 +69,7 @@ func ConvertAntigravityResponseToClaude(_ context.Context, _ string, originalReq HasFirstResponse: false, ResponseType: 0, ResponseIndex: 0, - SessionID: deriveSessionIDFromRequest(originalRequestRawJSON), + SessionID: deriveSessionID(originalRequestRawJSON), } }