A Claude account's model list fetches models.dev again every 45 minutes (catalog.Asked), most often to the same bytes. Each fetch rewrote models.json, so its mtime changed, and usage keyed its request index, the index's file on disk (requests-v1.bin, heat, page, effort) and its priced summaries on that mtime: the next usage query dropped every chunk and read every session file again. On the acctp history (15.7k session files) a start that rebuilds takes ~15 s of CPU in its first 30 s, against ~2 s for one that keeps the index. Each fetch also forgot the parsed catalog and told every agent to rewrite its list (catalog.Changed), for nothing changed. - catalog.CacheKey names models.json and labs.json by a hash of their bytes, kept by their stat so a key costs a stat until a file changes. Usage's request meta and price key use it in place of the mtime, as liveListKey already did for the providers' lists. - Sync writes models.json and labs.json only when their bytes changed; the same bytes are dated now (Stale, Missing and Asked read the time), and Reset and Touched run only when either file changed. Tests: TestSameCatalogFetchedAgainChangesNothing (catalog) and TestRequestStateKeptAcrossACatalogFetchedAgain (usage). Broken by hand: with keepFetched always writing, the catalog test is red (told the agents on the same bytes); with usage keyed on statKey again, the usage test is red (meta and price key changed).
73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package catalog
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// CacheKey names the catalog kept on disk, models.dev's provider catalog
|
|
// and the lab catalog beside it, by what the files hold, not by when they
|
|
// were written. A Claude account's model list fetches the catalog again
|
|
// every 45 minutes (Asked), and usage keyed its whole index on the file's
|
|
// mtime: each fetch, the same bytes, had the next usage query read every
|
|
// session file again, ~15 s of CPU over a long history.
|
|
func CacheKey() string { return fileKey(CachePath()) + "|" + fileKey(LabsPath()) }
|
|
|
|
// fileKeys holds each file's last key by the stat it was hashed at, so a
|
|
// key costs a stat until the file changes.
|
|
var fileKeys = struct {
|
|
sync.Mutex
|
|
m map[string]keyedFile
|
|
}{m: map[string]keyedFile{}}
|
|
|
|
type keyedFile struct {
|
|
size int64
|
|
mod time.Time
|
|
key string
|
|
}
|
|
|
|
// fileKey is a hash of path's bytes, "missing" when there is no file, and
|
|
// its stat when it can't be read: an unknown read is a change each time
|
|
// the file is written, as it was before.
|
|
func fileKey(path string) string {
|
|
st, err := os.Stat(path)
|
|
if err != nil {
|
|
return "missing"
|
|
}
|
|
fileKeys.Lock()
|
|
k, ok := fileKeys.m[path]
|
|
fileKeys.Unlock()
|
|
if ok && k.size == st.Size() && k.mod.Equal(st.ModTime()) {
|
|
return k.key
|
|
}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return fmt.Sprintf("stat:%d:%d", st.Size(), st.ModTime().UnixNano())
|
|
}
|
|
sum := sha256.Sum256(b)
|
|
key := hex.EncodeToString(sum[:])
|
|
fileKeys.Lock()
|
|
fileKeys.m[path] = keyedFile{size: st.Size(), mod: st.ModTime(), key: key}
|
|
fileKeys.Unlock()
|
|
return key
|
|
}
|
|
|
|
// keepFetched writes b to p, unless p holds b already: then only p's time
|
|
// is now, which Stale, Missing and Asked read as the fetch. It reports
|
|
// whether p's bytes changed.
|
|
func keepFetched(p string, b []byte) (bool, error) {
|
|
if old, err := os.ReadFile(p); err == nil && bytes.Equal(old, b) {
|
|
now := time.Now()
|
|
if err := os.Chtimes(p, now, now); err != nil {
|
|
return false, err
|
|
}
|
|
return false, nil
|
|
}
|
|
return true, writeWhole(p, b, time.Time{})
|
|
}
|