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).
52 lines
1.8 KiB
Go
52 lines
1.8 KiB
Go
package usage
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// A Claude account's model list fetches models.dev again every 45 minutes
|
|
// (catalog.Asked), most often to the same bytes. Keyed on models.json's
|
|
// mtime, each fetch dropped the request index and the file of it on disk,
|
|
// and the next usage query read every session file again: ~15 s of CPU
|
|
// over a long history. Only a catalog that changed may drop them.
|
|
func TestRequestStateKeptAcrossACatalogFetchedAgain(t *testing.T) {
|
|
pageHome(t)
|
|
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
|
|
p := catalog.CachePath()
|
|
write := func(b string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(b), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
const one = `{"openai":{"id":"openai","models":{"gpt-6.1-sol":{"id":"gpt-6.1-sol","cost":{"input":2,"output":10}}}}}`
|
|
write(one)
|
|
old := time.Now().Add(-time.Hour)
|
|
if err := os.Chtimes(p, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, price := readRequestState(now).meta, usagePriceCacheKey(now)
|
|
write(one)
|
|
if st, err := os.Stat(p); err != nil || !st.ModTime().After(old.Add(30*time.Minute)) {
|
|
t.Fatalf("not written again: %v %v", st, err)
|
|
}
|
|
if got := readRequestState(now).meta; got != before {
|
|
t.Error("the same catalog fetched again changed the request index's meta")
|
|
}
|
|
if got := usagePriceCacheKey(now); got != price {
|
|
t.Error("the same catalog fetched again changed the price key")
|
|
}
|
|
write(`{"openai":{"id":"openai","models":{"gpt-6.1-sol":{"id":"gpt-6.1-sol","cost":{"input":3,"output":10}}}}}`)
|
|
if readRequestState(now).meta == before || usagePriceCacheKey(now) == price {
|
|
t.Error("a catalog that changed kept the request meta or the price key")
|
|
}
|
|
}
|