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.