116 lines
2.5 KiB
Go
116 lines
2.5 KiB
Go
// cache.go — Tool result caching with file staleness detection.
|
|
//
|
|
// Caches tool results keyed by (tool name, arguments). File-based tools
|
|
// track mtime and size; cache entries are invalidated when the underlying
|
|
// file changes. Non-file results never expire within a session.
|
|
|
|
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
// cachedResult stores a tool result with file metadata for staleness detection.
|
|
type cachedResult struct {
|
|
Result string
|
|
ModTime int64 // unix nanoseconds of the file at cache time; 0 if no file
|
|
Size int64 // file size at cache time; -1 if no file
|
|
}
|
|
|
|
const (
|
|
resultCacheMaxEntries = 256
|
|
resultCacheMaxBytes = 32 * 1024 * 1024
|
|
)
|
|
|
|
type resultCache struct {
|
|
mu sync.Mutex
|
|
items map[string]cachedResult
|
|
bytes int
|
|
}
|
|
|
|
func (c *resultCache) Load(key string) (cachedResult, bool) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
v, ok := c.items[key]
|
|
return v, ok
|
|
}
|
|
|
|
func (c *resultCache) Delete(key string) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if v, ok := c.items[key]; ok {
|
|
c.bytes -= len(v.Result)
|
|
delete(c.items, key)
|
|
}
|
|
}
|
|
|
|
func (c *resultCache) Store(key string, value cachedResult) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if c.items == nil {
|
|
c.items = make(map[string]cachedResult)
|
|
}
|
|
if old, ok := c.items[key]; ok {
|
|
c.bytes -= len(old.Result)
|
|
}
|
|
if len(value.Result) > resultCacheMaxBytes {
|
|
return
|
|
}
|
|
c.items[key] = value
|
|
c.bytes += len(value.Result)
|
|
for len(c.items) > resultCacheMaxEntries || c.bytes > resultCacheMaxBytes {
|
|
for k, v := range c.items {
|
|
delete(c.items, k)
|
|
c.bytes -= len(v.Result)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// extractFilePath attempts to pull a "path" field from JSON tool args.
|
|
func extractFilePath(args json.RawMessage) string {
|
|
var m map[string]json.RawMessage
|
|
if json.Unmarshal(args, &m) != nil {
|
|
return ""
|
|
}
|
|
raw, ok := m["path"]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
var p string
|
|
if json.Unmarshal(raw, &p) != nil {
|
|
return ""
|
|
}
|
|
return p
|
|
}
|
|
|
|
// cacheValid checks if a cached result is still valid by stat-ing the file.
|
|
func cacheValid(entry cachedResult, args json.RawMessage) bool {
|
|
if entry.Size < 0 {
|
|
return true
|
|
}
|
|
path := extractFilePath(args)
|
|
if path == "" {
|
|
return true
|
|
}
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return info.ModTime().UnixNano() == entry.ModTime && info.Size() == entry.Size
|
|
}
|
|
|
|
// fileStat returns (modtime nanoseconds, size) for a path, or (0, -1) if unavailable.
|
|
func fileStat(path string) (int64, int64) {
|
|
if path == "" {
|
|
return 0, -1
|
|
}
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return 0, -1
|
|
}
|
|
return info.ModTime().UnixNano(), info.Size()
|
|
}
|