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).
98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
package catalog
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A Claude account's list fetches the catalog again every 45 minutes
|
|
// (Asked), most often to the same bytes. Each fetch forgot the parsed
|
|
// catalog, told every agent to rewrite its list (Changed) and, as usage
|
|
// keyed its index on the file's mtime, had the next usage query read the
|
|
// whole session history again. The same bytes are only dated now; bytes
|
|
// that changed, in either file, are kept and told as before.
|
|
func TestSameCatalogFetchedAgainChangesNothing(t *testing.T) {
|
|
freshCache(t)
|
|
var mu sync.Mutex
|
|
api := `{"openai":{"id":"openai","models":{"gpt-6.1-sol":{"id":"gpt-6.1-sol","cost":{"input":2,"output":10}}}}}`
|
|
labs := `{"openai/gpt-6.1-sol":{"id":"gpt-6.1-sol","limit":{"context":400000}}}`
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
switch r.URL.Path {
|
|
case "/api.json":
|
|
w.Write([]byte(api))
|
|
case "/models.json":
|
|
w.Write([]byte(labs))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
pointAt(t, srv.URL+"/api.json")
|
|
told := 0
|
|
was := Changed
|
|
Changed = func() { told++ }
|
|
t.Cleanup(func() { Changed = was; labsTried.Store(false) })
|
|
|
|
again := func() {
|
|
t.Helper()
|
|
if err := Sync(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
again()
|
|
if told != 1 {
|
|
t.Fatalf("a first fetch told the agents %d times", told)
|
|
}
|
|
key := CacheKey()
|
|
old := time.Now().Add(-48 * time.Hour)
|
|
if err := os.Chtimes(CachePath(), old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !Stale() {
|
|
t.Fatal("a catalog two days old isn't stale")
|
|
}
|
|
again()
|
|
if told != 1 {
|
|
t.Error("the same catalog fetched again told the agents")
|
|
}
|
|
if got := CacheKey(); got != key {
|
|
t.Error("the same catalog fetched again changed its key")
|
|
}
|
|
if Stale() {
|
|
t.Error("the same catalog fetched again stayed stale: its time wasn't now")
|
|
}
|
|
|
|
mu.Lock()
|
|
labs = `{"openai/gpt-6.1-sol":{"id":"gpt-6.1-sol","limit":{"context":800000}}}`
|
|
mu.Unlock()
|
|
again()
|
|
if told != 2 {
|
|
t.Error("a lab catalog that changed wasn't told")
|
|
}
|
|
if got := CacheKey(); got == key {
|
|
t.Error("a lab catalog that changed kept the key")
|
|
}
|
|
key = CacheKey()
|
|
|
|
mu.Lock()
|
|
api = `{"openai":{"id":"openai","models":{"gpt-6.1-sol":{"id":"gpt-6.1-sol","cost":{"input":3,"output":10}}}}}`
|
|
mu.Unlock()
|
|
again()
|
|
if told != 3 {
|
|
t.Error("a catalog that changed wasn't told")
|
|
}
|
|
if got := CacheKey(); got == key {
|
|
t.Error("a catalog that changed kept the key")
|
|
}
|
|
if pr, ok := PriceOf("openai", "gpt-6.1-sol"); !ok || pr.Input != 3 {
|
|
t.Errorf("a catalog that changed wasn't read again: %+v %v", pr, ok)
|
|
}
|
|
}
|