이대희 commited on
Commit
44dfd4f
·
1 Parent(s): 96e51bf

refactor: Improve cache eviction ordering and clean up session ID usage

Browse files

Improve the cache eviction routine to sort entries by timestamp using the standard library sort routine (stable, clearer and faster than the prior manual selection/bubble logic), and remove a redundant request-derived session ID helper in favor of the centralized session ID function. Also drop now-unused crypto/encoding imports.

This yields clearer, more maintainable eviction logic and removes duplicated/unused code and imports to reduce surface area and potential inconsistencies.

internal/cache/signature_cache.go CHANGED
@@ -3,6 +3,7 @@ package cache
3
  import (
4
  "crypto/sha256"
5
  "encoding/hex"
 
6
  "sync"
7
  "time"
8
  )
@@ -89,20 +90,17 @@ func CacheSignature(sessionID, text, signature string) {
89
  ts time.Time
90
  }{key, entry.Timestamp})
91
  }
92
- // Simple approach: remove first quarter of entries
 
 
 
 
93
  toRemove := len(oldest) / 4
94
  if toRemove < 1 {
95
  toRemove = 1
96
  }
97
- // Sort by timestamp (oldest first) - simple bubble for small N
98
  for i := 0; i < toRemove; i++ {
99
- minIdx := i
100
- for j := i + 1; j < len(oldest); j++ {
101
- if oldest[j].ts.Before(oldest[minIdx].ts) {
102
- minIdx = j
103
- }
104
- }
105
- oldest[i], oldest[minIdx] = oldest[minIdx], oldest[i]
106
  delete(sc.entries, oldest[i].key)
107
  }
108
  }
 
3
  import (
4
  "crypto/sha256"
5
  "encoding/hex"
6
+ "sort"
7
  "sync"
8
  "time"
9
  )
 
90
  ts time.Time
91
  }{key, entry.Timestamp})
92
  }
93
+ // Sort by timestamp (oldest first) using sort.Slice
94
+ sort.Slice(oldest, func(i, j int) bool {
95
+ return oldest[i].ts.Before(oldest[j].ts)
96
+ })
97
+
98
  toRemove := len(oldest) / 4
99
  if toRemove < 1 {
100
  toRemove = 1
101
  }
102
+
103
  for i := 0; i < toRemove; i++ {
 
 
 
 
 
 
 
104
  delete(sc.entries, oldest[i].key)
105
  }
106
  }
internal/translator/antigravity/claude/antigravity_claude_response.go CHANGED
@@ -9,8 +9,6 @@ package claude
9
  import (
10
  "bytes"
11
  "context"
12
- "crypto/sha256"
13
- "encoding/hex"
14
  "fmt"
15
  "strings"
16
  "sync/atomic"
@@ -46,27 +44,6 @@ type Params struct {
46
  CurrentThinkingText strings.Builder // Accumulates thinking text for signature caching
47
  }
48
 
49
- // deriveSessionIDFromRequest generates a stable session ID from the request JSON.
50
- func deriveSessionIDFromRequest(rawJSON []byte) string {
51
- messages := gjson.GetBytes(rawJSON, "messages")
52
- if !messages.IsArray() {
53
- return ""
54
- }
55
- for _, msg := range messages.Array() {
56
- if msg.Get("role").String() == "user" {
57
- content := msg.Get("content").String()
58
- if content == "" {
59
- content = msg.Get("content.0.text").String()
60
- }
61
- if content != "" {
62
- h := sha256.Sum256([]byte(content))
63
- return hex.EncodeToString(h[:16])
64
- }
65
- }
66
- }
67
- return ""
68
- }
69
-
70
  // toolUseIDCounter provides a process-wide unique counter for tool use identifiers.
71
  var toolUseIDCounter uint64
72
 
@@ -92,7 +69,7 @@ func ConvertAntigravityResponseToClaude(_ context.Context, _ string, originalReq
92
  HasFirstResponse: false,
93
  ResponseType: 0,
94
  ResponseIndex: 0,
95
- SessionID: deriveSessionIDFromRequest(originalRequestRawJSON),
96
  }
97
  }
98
 
 
9
  import (
10
  "bytes"
11
  "context"
 
 
12
  "fmt"
13
  "strings"
14
  "sync/atomic"
 
44
  CurrentThinkingText strings.Builder // Accumulates thinking text for signature caching
45
  }
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  // toolUseIDCounter provides a process-wide unique counter for tool use identifiers.
48
  var toolUseIDCounter uint64
49
 
 
69
  HasFirstResponse: false,
70
  ResponseType: 0,
71
  ResponseIndex: 0,
72
+ SessionID: deriveSessionID(originalRequestRawJSON),
73
  }
74
  }
75