Files
CLIProxyAPI/internal/runtime/executor/helps/antigravity_aicode_cache.go
Luis Pater b90c5a587a feat(antigravity): align Hub 2.35.2 / aicode v1alpha protocol, control plane, and cross-tenant isolation
- Cross-tenant session/LCP affinity across model switches, cascade/parent subagent alias extraction and RESP forwarding, automatic subagent custom_agent requestType promotion, credential-scoped reasoning replay isolation, foreign thoughtSignature scrubbing on *ojson.Doc, 14-key payload order, and 270s prompt-cache keepalive guards.
- Shared Google RPC array and HTTP 200 in-band error unwrapping/classification, pre-first-byte same-auth micro-retries for 14 TPU transient errors, expanded quota/billing/policy classification with strict HTTP 403/gRPC 7 TOS validation, and read-only SafetySignals logging.
- 32-leg continuationToken stream resumption, dynamic deprecatedModelIds rerouting, Orchestrator model synthesis, ModelDetails capability persistence, authenticated rewriteUri grounding redirects, FULL_ELIGIBILITY_CHECK, ClaimAICodeBankedQuotaReset, and validated no-redirect dynamic endpointUrl onboarding.
- PKCE S256 OAuth login, downscoped auth/aicode token minting with bounded cache, mutually verified Home RESP aicode-token handoff, grant/digest refresh continuity verification, flat v1alpha *ojson.Doc codec with GOOGLE_ONE_AI fallback experience, businessaicode parent support, and unbroken FinalizePayloadDoc semantic barrier across v1alpha, v1internal, 403 fallback, continuation, and countTokens.
- Full v1alpha control-plane integration (fetchConfig with 8-group proto agySurfaceConfig, typed Mendel flags with conflict rejection, fetchLicenses, prod/preprod consistency, and 2 MiB-guarded KV snapshot publication), auto-mode Mendel routing, real-time v1alpha:fetchQuotaStatus credits refresh with read-only dark-launch shadow comparison and exact int64 comparison, and unified pure-Go Hub 2.35.2 UA + HTTP/1.1-only no-ALPN transport.
2026-11-29 10:40:46 +08:00

109 lines
2.2 KiB
Go

package helps
import (
"container/list"
"sync"
)
const defaultBoundedLRUCapacity = 1024
// BoundedLRU is a thread-safe bounded least-recently-used key/value cache.
type BoundedLRU[K comparable, V any] struct {
mu sync.Mutex
capacity int
order *list.List
items map[K]*list.Element
}
type boundedLRUEntry[K comparable, V any] struct {
key K
value V
}
// NewBoundedLRU returns a BoundedLRU holding at most capacity entries.
func NewBoundedLRU[K comparable, V any](capacity int) *BoundedLRU[K, V] {
if capacity <= 0 {
capacity = defaultBoundedLRUCapacity
}
return &BoundedLRU[K, V]{
capacity: capacity,
order: list.New(),
items: make(map[K]*list.Element, capacity),
}
}
// Get retrieves the value stored under key and refreshes its recency.
func (c *BoundedLRU[K, V]) Get(key K) (V, bool) {
var zero V
if c == nil {
return zero, false
}
c.mu.Lock()
defer c.mu.Unlock()
elem, ok := c.items[key]
if !ok {
return zero, false
}
c.order.MoveToFront(elem)
return elem.Value.(*boundedLRUEntry[K, V]).value, true
}
// Set inserts or updates key with value, evicting the least recently used entry
// when the cache exceeds capacity.
func (c *BoundedLRU[K, V]) Set(key K, value V) {
if c == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
if elem, ok := c.items[key]; ok {
elem.Value.(*boundedLRUEntry[K, V]).value = value
c.order.MoveToFront(elem)
return
}
elem := c.order.PushFront(&boundedLRUEntry[K, V]{key: key, value: value})
c.items[key] = elem
for c.order.Len() > c.capacity {
oldest := c.order.Back()
if oldest == nil {
break
}
c.order.Remove(oldest)
delete(c.items, oldest.Value.(*boundedLRUEntry[K, V]).key)
}
}
// Delete removes key from the cache if present.
func (c *BoundedLRU[K, V]) Delete(key K) {
if c == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
if elem, ok := c.items[key]; ok {
c.order.Remove(elem)
delete(c.items, key)
}
}
// Purge removes all entries from the cache.
func (c *BoundedLRU[K, V]) Purge() {
if c == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
c.order.Init()
clear(c.items)
}
// Len returns the current number of cached entries.
func (c *BoundedLRU[K, V]) Len() int {
if c == nil {
return 0
}
c.mu.Lock()
defer c.mu.Unlock()
return len(c.items)
}