From 0c44b9ece4fdfebfc9a2f5996b39fe6a837e5d7d Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 11 Nov 2026 13:14:37 +0800 Subject: [PATCH] agent: a dsh round builds the provider catalog once keepDshWired's round ran outside any provider.Hold, so every PublicRef, RenamedRef and Exposed it asked built every provider's models again. With a real account, 158 MB of what magpie allocated in its first minute was this round, 72.55 MB of it dshStartOnItsRoute's four refs, all worked out before the first was looked up. The round now holds the catalog for its whole run, and the start's refs are worked out only when the one before isn't listed. provider.EntriesBuilt exposes the build count for the test. Test: TestDshRoundBuildsTheCatalogOnce gives the start an auto ref no model has and counts catalog builds in a writing round. On the old dsh.go it is red with 3 builds; with the change, 1. --- internal/agent/dsh.go | 16 +++++++++++++++- internal/agent/dsh_rounds_test.go | 26 ++++++++++++++++++++++++++ internal/provider/hold.go | 4 ++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/internal/agent/dsh.go b/internal/agent/dsh.go index f29c14b2f..0f851bd30 100644 --- a/internal/agent/dsh.go +++ b/internal/agent/dsh.go @@ -2148,7 +2148,16 @@ func dshStartOnItsRoute(items []dshItem, models []catalog.Model) []dshItem { continue } want, id := "", c["model"] - for _, ref := range []string{id, provider.PublicRef(id), provider.RenamedRef(id), provider.PublicRef(provider.RenamedRef(id))} { + // each ref is worked out only when the one before it isn't listed: + // PublicRef and RenamedRef of an auto ref look at the whole catalog + refs := []func(string) string{ + func(id string) string { return id }, + provider.PublicRef, + provider.RenamedRef, + func(id string) string { return provider.PublicRef(provider.RenamedRef(id)) }, + } + for _, of := range refs { + ref := of(c["model"]) for _, m := range models { if m.ID == ref { want, id = dshRouteOf(m), ref @@ -2673,6 +2682,11 @@ func (r *dshRounds) once() string { r.done = false return "" } + // one catalog for the whole round: outside a Hold, each PublicRef, + // RenamedRef and Exposed the round asks built every provider's models + // again, 158 MB of what a real account's magpie allocated in its first + // minute + defer provider.Hold()() key := dshRoundKey{files: dshFilesSum(dir, files), gw: gateway.URL(), models: r.last.models} // the stamp is read before the models are built: a change while they // are moves it past the one kept, and the next round builds them again diff --git a/internal/agent/dsh_rounds_test.go b/internal/agent/dsh_rounds_test.go index 7852fe410..4c5dbc3f9 100644 --- a/internal/agent/dsh_rounds_test.go +++ b/internal/agent/dsh_rounds_test.go @@ -127,3 +127,29 @@ func TestDshRoundAfterAChangeWritesAgain(t *testing.T) { } }) } + +// A round that writes builds the catalog once, though it asks for public +// and renamed refs of an auto start and for every route: outside a Hold +// each of them built every provider's models again (158 MB in a real +// account's first minute). +func TestDshRoundBuildsTheCatalogOnce(t *testing.T) { + r, web := dshSettled(t) + b, _ := os.ReadFile(web) + start := strings.NewReplacer("model: deepseek/flash\n", "model: auto/nothing-listed\n", `model: "deepseek/flash"`, "model: auto/nothing-listed").Replace(string(b)) + if !strings.Contains(start, "auto/nothing-listed") { + t.Fatalf("no start in the patch list:\n%s", b) + } + os.WriteFile(web, []byte(start), 0o644) + if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash", "max"}}); err != nil { + t.Fatal(err) + } + before := provider.EntriesBuilt() + r.once() + if !dshListed(web, "deepseek/max") { + t.Fatal("the round wrote no route") + } + t.Logf("%d catalog builds", provider.EntriesBuilt()-before) + if n := provider.EntriesBuilt() - before; n > 1 { + t.Fatalf("a round built the catalog %d times", n) + } +} diff --git a/internal/provider/hold.go b/internal/provider/hold.go index 5ee3b5612..1cd9f1a7d 100644 --- a/internal/provider/hold.go +++ b/internal/provider/hold.go @@ -337,6 +337,10 @@ func heldEntries(build func() []Entry) []Entry { // that counts them. var entriesBuilt atomic.Int64 +// EntriesBuilt is how many times the catalog was built from every +// provider's files, for another package's test that counts them. +func EntriesBuilt() int64 { return entriesBuilt.Load() } + // keptFor is how long what kept has is used while the Stamp stays the // same: an account another program signed in to or out of, or a model // retired by its date, moves no stamp, and is seen this long after.