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.
This commit is contained in:
yetone
2026-11-11 14:43:25 +08:00
parent 1ccacad632
commit 0c44b9ece4
3 changed files with 45 additions and 1 deletions
+15 -1
View File
@@ -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
+26
View File
@@ -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)
}
}
+4
View File
@@ -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.