Files
magpie/internal/usage/catalog_key_test.go
T
yetone b3395ffdaf usage: a models.dev change no call of the user's names keeps the request index
The request index's meta named the catalog by models.json's bytes
(catalog.CacheKey, v0.1.4037), so every real change models.dev made, most
of them to aggregators' lists (OpenRouter, NanoGPT, Kilo, Vercel) no call
names, dropped every chunk and read every session file again: ~15 s of
CPU on a long history, several times a day.

The meta now names catalogBase: the catalog the rows were made under,
kept while each question the rows asked of the catalog is answered the
same. The questions are Pricer's base price (basePriceIn, taken out of
Pricer whole) and ListedAs's listed name; each is noted with its answer
the first time it is asked (noteAsk). A new catalog file, or a catalog
read again (catalog.Resets: Sync writes the file before it resets), asks
them all again; one answered otherwise names the index anew, as any
change did before. A question answered otherwise than first, or more
than 50k of them, does the same. The asks are kept in
catalog-asks-v1.json, written before any file of rows under the meta
(toDisk, heat, page, effort, digests), so a restart holds the rows on
disk to them too. The price cache key (usagePriceCacheKey) is unchanged.

Run on mica at this tree: go test ./internal/usage ./internal/catalog
./internal/gateway ok (gateway 1038s); go vet, GOOS=linux and
GOOS=windows go vet of usage and catalog clean; gofmt clean. Broken by
hand, each red in TestRequestStateKeptAcrossACatalogChangeNoRowAsks:
answersSame always true, the meta back on catalog.CacheKey, and the
catalog.Resets check dropped (a check between Sync's write and its reset).
catalog_asks.go joins the row code's go:embed, so the index is built
once after this update.
Not tried yet: a real models.dev change on a real account (next, on mote).
2026-11-13 19:42:11 +08:00

136 lines
4.3 KiB
Go

package usage
import (
"fmt"
"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 usagePriceCacheKey(now) == price {
t.Error("a catalog that changed kept the price key")
}
}
// models.dev changes several times a day, mostly in aggregators' lists no
// call of the user's names. The request index is built again only when a
// price or a listed name its rows hold is answered otherwise, across a
// restart too.
func TestRequestStateKeptAcrossACatalogChangeNoRowAsks(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
p := catalog.CachePath()
at := time.Now().Add(-2 * time.Hour)
reset := true
write := func(sol, kilo int) {
t.Helper()
b := fmt.Sprintf(`{"openai":{"id":"openai","models":{"gpt-6.1-sol":{"id":"gpt-6.1-sol","cost":{"input":%d,"output":10}}}},`+
`"kilo":{"id":"kilo","models":{"x/y":{"id":"x/y","cost":{"input":%d,"output":1}}}}}`, sol, kilo)
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)
}
at = at.Add(time.Minute) // each write a time of its own
if err := os.Chtimes(p, at, at); err != nil {
t.Fatal(err)
}
if reset {
catalog.Reset() // as Sync does once the file changed
}
}
// a session file's call priced at the catalog's word (#4278)
row := Record{Provider: UnknownProvider, SessionProvider: "openai", Model: "gpt-6.1-sol", Input: 1}
priced := func(want float64) {
t.Helper()
pr := Pricer()(row)
if pr == nil || pr.Input != want {
t.Fatalf("priced %+v, want input %v", pr, want)
}
}
// rows reach a file only after their asks did (saveAsks)
restart := func() {
saveAsks()
asks.Lock()
asks.path = ""
asks.Unlock()
}
write(2, 1)
meta := readRequestState(now).meta
priced(2)
write(2, 5)
if readRequestState(now).meta != meta {
t.Fatal("a change to a model no row names built the index again")
}
write(3, 5)
again := readRequestState(now).meta
if again == meta {
t.Fatal("a new price of a model a row names kept the index")
}
priced(3)
restart()
write(3, 7)
if readRequestState(now).meta != again {
t.Fatal("after a restart, a change no row asks of built the index again")
}
restart()
write(4, 7)
fourth := readRequestState(now).meta
if fourth == again {
t.Fatal("after a restart, a new price a row holds kept the index")
}
// Sync writes the file before it resets the catalog: a check between
// the two asks the catalog the file no longer holds, and the reset
// asks again
priced(4)
reset = false
write(5, 7)
if readRequestState(now).meta != fourth {
t.Fatal("the catalog still loaded answered otherwise")
}
catalog.Reset()
if readRequestState(now).meta == fourth {
t.Fatal("a check before the catalog was read again kept the index past it")
}
}