Files
magpie/internal/catalog/live_same_test.go
T
yetone fdabddeb41 catalog: a list fetched again as it was tells no agent
The magpie serving the gateway fetches each signed-in account's list
again once it is 45 minutes old (KeepAccountsListed), and saveLive
called Touched after every save: Changed ran SyncCatalog, which built
and compared every agent's model lists, and Forget moved the stamp
dsh's rounds build magpie's models by, so they built them again too.
For a list that came back the same, all of it was for nothing.

Now saveLive compares what it is about to write with the file there,
but for the fetch time. The same list is written (the new time keeps
the next relist 45 minutes away) and its next read looks at the file
(filememo.Look, also under a request's hold); nothing else is told. A
list that differs in a model, a part (Sides) or what it was asked as
is told as before.

Measured on mote with a copy of the owner's real config and accounts,
network blocked, the claude account's list set 2h old so the relist
falls in the window (acidle.sh, alloc_space from 60s to 180s idle):
  v0.1.3416 (t5): 42.8 MB, SyncCatalog 27.7 MB, buildModels 8.9 MB
  first cut, Forget kept (t6): 28.8 MB, dsh rounds' buildModels 12.1 MB
  this (t7): 14.1 MB (4.7 MB of it the heap profile's own write),
    neither SyncCatalog nor buildModels in the profile; the claude
    list's fetched moved to the relist's time (06:18:52)
Without a relist due, idle was 7.8 MB before and after.

Test: TestLiveSavedAgainSameTellsNoAgent. Broken by hand: always
Touched (red: "changed 2, forgot 2; want 1, 1"), and no
filememo.Look (red: the held read keeps the old fetch time).

Run at this head on mote: go test -count=1 ./internal/catalog/
./internal/provider/ ./internal/agent/ ./internal/filememo/ ok; go vet
of internal/catalog native, GOOS=linux and GOOS=windows ok; gofmt -l
clean.

Not tried: an account whose list really changed in a running magpie
(the test covers a model, a window, a part and the asked version).
2026-11-09 22:26:48 +08:00

83 lines
2.8 KiB
Go

package catalog
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
)
// A list fetched again as it was (an account's, each 45 minutes) moves only
// when it was fetched: the agents aren't told (Changed), as each time they
// were every agent's lists were built and compared again for nothing. A
// list that differs in anything else — a model, a part, what it was asked
// as — tells them. The held catalog isn't dropped either (Forget moves the
// stamp dsh's rounds build magpie's models again by); the file's next
// read, under a request's hold too, is the new one.
func TestLiveSavedAgainSameTellsNoAgent(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
changed, forgot := 0, 0
oldC, oldF := Changed, Forget
Changed, Forget = func() { changed++ }, func() { forgot++ }
defer func() { Changed, Forget = oldC, oldF }()
a := []Model{{ID: "gpt-a", Context: 200000}, {ID: "gpt-b"}}
save := func(ms []Model, asked string) {
t.Helper()
if err := SaveLiveAsked("acct", "https://a/v1", asked, ms); err != nil {
t.Fatal(err)
}
}
save(a, "1.0")
if changed != 1 || forgot != 1 {
t.Fatalf("first save: changed %d, forgot %d", changed, forgot)
}
// read as a list saved a while ago is: taken as unchanged under the
// hold, until it is looked at again
old := time.Now().Add(-time.Hour)
if err := os.Chtimes(LivePath("acct"), old, old); err != nil {
t.Fatal(err)
}
release := filememo.Hold()
defer release()
first, _ := readLive("acct")
time.Sleep(2 * time.Millisecond)
save([]Model{{ID: "gpt-a", Context: 200000}, {ID: "gpt-b"}}, "1.0")
if changed != 1 || forgot != 1 {
t.Errorf("the same list again: changed %d, forgot %d; want 1, 1", changed, forgot)
}
if again, _ := readLive("acct"); !again.Fetched.After(first.Fetched) {
t.Errorf("fetched %v, not after %v", again.Fetched, first.Fetched)
}
for _, c := range []struct {
name string
ms []Model
asked string
}{
{"a model's window", []Model{{ID: "gpt-a", Context: 400000}, {ID: "gpt-b"}}, "1.0"},
{"a model more", []Model{{ID: "gpt-a", Context: 400000}, {ID: "gpt-b"}, {ID: "gpt-c"}}, "1.0"},
{"asked as another", []Model{{ID: "gpt-a", Context: 400000}, {ID: "gpt-b"}, {ID: "gpt-c"}}, "2.0"},
} {
before := changed
save(c.ms, c.asked)
if changed != before+1 {
t.Errorf("%s: agents not told", c.name)
}
}
before := changed
if err := SaveLiveSides("acct", "https://a/v1", a, map[string][]Model{"https://a/v1": a[:1]}); err != nil {
t.Fatal(err)
}
if err := SaveLiveSides("acct", "https://a/v1", a, map[string][]Model{"https://a/v1": a}); err != nil {
t.Fatal(err)
}
if changed != before+2 {
t.Errorf("a part changed: told %d times, want 2", changed-before)
}
}