Files
magpie/internal/usage/live_list_key_test.go
T
yetone 7e5cebb86c usage: a model list fetched again unchanged keeps the usage index
Each account's model list is fetched again on every start and every 45
minutes, and saveLive writes it again with only its "fetched" time moved.
Usage keyed the request index (and its file on disk, requests-v1.bin) and
the priced summaries on that file's mtime (statKey(catalog.LivePath)), so
each such save dropped every chunk: every start, and every refresh while
running, read the whole session history again, decoded every call shard
and rebuilt every row.

catalog.LiveKey names a saved list by a hash of its bytes without the
fetch time, made once per parse of the file (readLive's memo). A list
saved before Since was kept is dated by its fetch (LiveSince), so its
fetch time stays in its key. usage's liveListKey uses it for every
provider and Antigravity in readRequestState's meta and in
usagePriceCacheKey (the same mtime key, the sibling), and falls back to
the stat when the list can't be read.

Found by dumping both disk headers on mote: same binary, same sources,
and only the providers' hash differed, run to run.

Measured on mote, a copy of the owner's real accounts, GUI open, network
blocked, the Claude list rewritten as a start's fetch does. Restart of
the same build, t10 (main) against t11 (this):
  CPU in the first ~3 minutes   11.6s -> 2.5s
  allocated                     2.74GB -> 0.81GB
  sessions.loadCalls            771MB -> gone from the top
  footprint                     174MB -> 175MB
The first start after an upgrade that changes usage's code still builds
everything once (requestDiskBinary), as before.

Tests: TestRequestStateKeptAcrossAListFetchedAgain saves the same list
again an hour later and needs readRequestState's meta and
usagePriceCacheKey unchanged, and both changed for another list; on the
old usage code it is red on both. TestLiveKeyOnlyMovesWithTheList and
TestLiveKeyOfAnOldListKeepsItsFetch cover the key itself.

Run on mote at this tree: go test ./internal/catalog ./internal/usage
./internal/provider -count=1 ok; go vet ./... ok; GOOS darwin/linux/
windows go vet ./internal/catalog ./internal/usage ok (./... cross-OS
stops at wails' cgo files, as on main); gofmt clean.
Not run: the full ./... suite before the push (running after), GUI.
2026-11-10 00:36:16 +08:00

46 lines
1.5 KiB
Go

package usage
import (
"os"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// A provider's list is saved again on every start and each 45 minutes,
// changed only in when it was fetched. Keyed on the file's mtime, each save
// dropped the request index's chunks (and the file of them on disk) and
// the priced summaries, so every start read the whole session history
// again. Only a list that changed may drop them.
func TestRequestStateKeptAcrossAListFetchedAgain(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
save := func(ctx int) {
t.Helper()
if err := catalog.SaveLive("antigravity", "https://a/v1", []catalog.Model{{ID: "m", Context: ctx}}); err != nil {
t.Fatal(err)
}
}
save(1000)
before, price := readRequestState(now).meta, usagePriceCacheKey(now)
old := time.Now().Add(-time.Hour)
if err := os.Chtimes(catalog.LivePath("antigravity"), old, old); err != nil {
t.Fatal(err)
}
save(1000)
if st, err := os.Stat(catalog.LivePath("antigravity")); err != nil || !st.ModTime().After(old.Add(30*time.Minute)) {
t.Fatalf("not saved again: %v %v", st, err)
}
if got := readRequestState(now).meta; got != before {
t.Error("the same list fetched again changed the request index's meta")
}
if got := usagePriceCacheKey(now); got != price {
t.Error("the same list fetched again changed the price key")
}
save(2000)
if readRequestState(now).meta == before || usagePriceCacheKey(now) == price {
t.Error("a list that changed kept the request meta or the price key")
}
}