From 4a7d1c63ac540158f3ed635ee5eedaa2d838f815 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 00:28:03 +0800 Subject: [PATCH] provider: Resolve keeps the listed-by map and finds a group without copying the catalog A live magpie's heap profile (v0.1.2404, 53 minutes) allocated 1.3 GB in isMagpie -> provider.Resolve, from the window's drift poll asking every 15s for each model value an agent's settings name: - a bare model no entry has ("gpt-5.5") built listedBy, every provider's whole model list, again on each request: heldOf only kept it for one second inside a Hold (835 MB); - a group ref copied the whole Catalog() to find one group (425 MB). listedBy now goes through keptOf, the Stamp-keyed store the catalog already uses, so it is built again only when providers.json, a group or a setting changes. A group ref is looked up in the groups' own entries first, and the full catalog stays the fallback. TestResolveKeptAcrossRequests: three providers with 40 models each and a group, each request in its own Hold. "group/g", "nope" and "m-b2" take 44, 43 and 28 allocations once built (bar 60), they resolve right, and a provider saved since is resolved by the next request. Broken by hand: heldOf("listed", listedBy) put back, red; the Catalog() copy first put back, red. Run at this tree on mote: gofmt -l clean; go vet ./internal/provider for darwin, linux and windows ok; go test -count=1 -timeout 45m ./internal/provider ok (126.0s), ./internal/agent ok (199.3s), ./internal/gateway ok (595.0s). --- internal/provider/models.go | 12 +++++- internal/provider/resolve_kept_test.go | 59 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 internal/provider/resolve_kept_test.go diff --git a/internal/provider/models.go b/internal/provider/models.go index d77af0d1f..848043ea5 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -2998,6 +2998,13 @@ func Resolve(id string) (Provider, string, bool) { // asking, but a value in its settings still has it id = strings.TrimSuffix(strings.TrimSpace(id), "[1m]") if IsGroupRef(id) { + // the groups' entries first: Catalog copies every entry, and was + // built for each group an agent's settings name (the window's drift + // poll, every 15s) + entries := providerEntries() + if e, ok := entryIn(groupEntries(entries), id); ok { + return e.Provider, e.Model, true + } if e, ok := entryIn(Catalog(), id); ok { return e.Provider, e.Model, true } @@ -3028,7 +3035,10 @@ func resolveIn(entries []Entry, id string) (Provider, string, bool) { } // not exposed, but some provider lists it var found []Provider - for _, p := range heldOf("listed", listedBy)[id] { + // kept across requests, as the catalog is: built again for each model + // value no entry has (an agent's bare "gpt-5.5"), every provider's whole + // list was 835 MB of a live magpie's 33 GB in 53 minutes + for _, p := range heldOf("listed", func() map[string][]Provider { return keptOf("listed", listedBy) })[id] { if !p.DecidesModel(id) { found = append(found, p) } diff --git a/internal/provider/resolve_kept_test.go b/internal/provider/resolve_kept_test.go new file mode 100644 index 000000000..ecb7ef420 --- /dev/null +++ b/internal/provider/resolve_kept_test.go @@ -0,0 +1,59 @@ +package provider + +import ( + "path/filepath" + "testing" + "time" +) + +// A model value no entry has (an agent's bare "gpt-5.5" in its config) +// and a group's ref are resolved, request after request, from what the +// first request built while nothing moved: the window's drift poll asked +// for each every 15s, and each built every provider's whole list again, +// or copied the whole catalog to find one group (1.3 GB of a live +// magpie's 33 GB in 53 minutes). A provider saved since is seen at once. +func TestResolveKeptAcrossRequests(t *testing.T) { + isolate(t) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + defer func(d, k time.Duration) { heldFor, keptFor = d, k }(heldFor, keptFor) + heldFor = 0 // each request after the last one's hold has gone + keptFor = time.Minute // a loaded machine's pause isn't the bound + var models []string + for _, m := range []string{"a", "b", "c", "d", "e", "f", "g", "h"} { + for _, n := range []string{"1", "2", "3", "4", "5"} { + models = append(models, "m-"+m+n) + } + } + for _, id := range []string{"r1", "r2", "r3"} { + if err := Save(Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil { + t.Fatal(err) + } + } + if err := SaveGroup(Group{ID: "g", Name: "g", Members: []string{"r1/m-a1", "r2/m-a1"}}); err != nil { + t.Fatal(err) + } + request := func(id string) bool { + defer Hold()() + _, _, ok := Resolve(id) + return ok + } + for _, id := range []string{"group/g", "nope", "m-b2"} { + request(id) + if n := testing.AllocsPerRun(20, func() { request(id) }); n > 60 { + t.Errorf("%s resolved again with nothing changed: %.0f allocations", id, n) + } + } + if !request("group/g") || request("nope") || !request("m-b2") { + t.Fatal("resolved wrong") + } + if err := Save(Provider{ID: "r4", Name: "r4", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m-new"}}); err != nil { + t.Fatal(err) + } + if !request("m-new") { + t.Fatal("a model saved since isn't resolved by the next request") + } +}