From fdabddeb410310e3346debaaf17b57d01d9e888f Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 9 Nov 2026 22:26:44 +0800 Subject: [PATCH] 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). --- internal/catalog/live.go | 35 ++++++++++++- internal/catalog/live_same_test.go | 82 ++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 internal/catalog/live_same_test.go diff --git a/internal/catalog/live.go b/internal/catalog/live.go index 6e582f8be..0ddc1be9e 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -1,6 +1,7 @@ package catalog import ( + "bytes" "context" "encoding/json" "errors" @@ -313,15 +314,45 @@ func saveLive(provider, base, asked string, models []Model, sides map[string][]M return err } now := time.Now() - b, _ := json.MarshalIndent(liveFile{Fetched: now, Base: base, Models: models, Sides: sides, Kinded: true, - Since: liveSince(provider, models, now), Asked: asked}, "", " ") + f := liveFile{Fetched: now, Base: base, Models: models, Sides: sides, Kinded: true, + Since: liveSince(provider, models, now), Asked: asked} + same := sameLive(p, f) + b, _ := json.MarshalIndent(f, "", " ") if err := os.WriteFile(p, b, 0o644); err != nil { return err } + if same { + // the list as it was, fetched again (an account's each 45 + // minutes, KeepAccountsListed): only when it was fetched is other. + // Its next read looks at the file, and nothing else is told: + // Touched had every agent's lists built and compared again, and + // Forget moved the stamp dsh's rounds build magpie's models by, + // ~40 MB of allocations a time in a magpie at rest. + filememo.Look(p) + return nil + } Touched() return nil } +// sameLive is whether the list saved at p is f but for when it was +// fetched: the bytes on the disk are f's, written at the time they say. +func sameLive(p string, f liveFile) bool { + old, err := os.ReadFile(p) + if err != nil { + return false + } + var at struct { + Fetched time.Time `json:"fetched"` + } + if json.Unmarshal(old, &at) != nil { + return false + } + f.Fetched = at.Fetched + b, err := json.MarshalIndent(f, "", " ") + return err == nil && bytes.Equal(b, old) +} + // liveSince is Since for models about to be saved for provider at now: // what the list saved before said of each model it had too, now for one it // didn't. A list saved before Since was kept had each of its models since diff --git a/internal/catalog/live_same_test.go b/internal/catalog/live_same_test.go new file mode 100644 index 000000000..ca044653b --- /dev/null +++ b/internal/catalog/live_same_test.go @@ -0,0 +1,82 @@ +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) + } +}