agent: a provider's options an agent reaches on its own are kept

Goose's model field asked ownOptions each poll, for the picker and again
for Drift, and each provider's models were made into options anew
(catalog.Provider, options, group, its icon): ownOptionsFrom was 21.2 MB
of forty polls with real accounts. For agents without a registry of
their own (native == nil), each provider's options are now kept in
ownBuilt per provider.StampNow (a minute at most) and copied into the
caller's list; Pi's and omp's, which read their own registries, are
built as before (ownProvider is the old loop body).

TestOwnOptionsKept: a catalog of 200 OpenAI models; asking again takes a
fifth or less of the allocations building does, a caller's write doesn't
reach what is kept, and a model the catalog lists since is offered.

Run on mote at c1457f2ee plus this commit and the next one (Goose's own
options kept), together:
- gofmt; go vet darwin, windows, linux: ok
- TestClaudeViaMagpieKept and TestOwnOptionsKept alone 3/3 ok
- go test ./internal/agent -timeout 45m: ok (267.7s)
- real accounts, sandbox HOME, network blocked, 40 polls, both commits
  against c1457f2ee: state and drift answers byte for byte the same;
  footprint peak 134 MB, RSS at end 177 MB
- broken by hand (ownOptionsFrom builds each time): red, "took 211
  allocations, building them 202"
- ownOptionsFrom 21.2 -> 7.0 MB, gooseIn.func10 28.9 -> 16.0 MB; Picks
  134 -> 118 MB with the commit before

Not run: the GUI suite (no GUI file changed), Windows and Linux test runs.

Picked onto ef63ef4a2 (with ef63ef4a2, Codex's subagent models kept, and gateway, redact and GUI commits; none in internal/agent); not run again there.
This commit is contained in:
yetone
2026-11-01 06:53:25 +08:00
parent 5e0d4e1282
commit 68ebd1ec4a
2 changed files with 97 additions and 23 deletions
+38 -23
View File
@@ -446,34 +446,49 @@ func ownOptionsFrom(native func(p string) []catalog.Model, authFile string, cur
sort.Strings(providers)
var out []Option
for _, p := range providers {
name := catalog.ProviderName(p)
if name == "" {
name = nativeProviderNames[p]
if native == nil {
out = append(out, ownBuilt.shared(p, func() []Option { return ownProvider(nil, p) })...)
} else {
out = append(out, ownProvider(native, p)...)
}
if name == "" {
name = p
}
var ms []catalog.Model
if native != nil {
ms = native(p)
}
if len(ms) == 0 {
ms = catalog.Provider(p)
}
if len(ms) == 0 {
ms = codexServed(p)
}
opts := group(name, options(ms, p+"/"))
if ic := providerIcon(p); ic != "" {
for i := range opts {
opts[i].GroupIcon = ic
}
}
out = append(out, opts...)
}
return out
}
// ownBuilt is ownProvider's options built last per provider, for agents
// without a registry of their own: Goose's model field asked them each
// poll, for the picker and again for Drift, each provider's models made
// into options anew, 20 MB of forty polls with real accounts.
var ownBuilt stampedLists[Option]
// ownProvider is the options of one provider an agent reaches on its own.
func ownProvider(native func(p string) []catalog.Model, p string) []Option {
name := catalog.ProviderName(p)
if name == "" {
name = nativeProviderNames[p]
}
if name == "" {
name = p
}
var ms []catalog.Model
if native != nil {
ms = native(p)
}
if len(ms) == 0 {
ms = catalog.Provider(p)
}
if len(ms) == 0 {
ms = codexServed(p)
}
opts := group(name, options(ms, p+"/"))
if ic := providerIcon(p); ic != "" {
for i := range opts {
opts[i].GroupIcon = ic
}
}
return opts
}
// ---- agents ----------------------------------------------------------------
// magpieProviderJSON is the provider block agents with JSON configs get.
+59
View File
@@ -0,0 +1,59 @@
package agent
import (
"fmt"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// A provider's options an agent reaches on its own are kept while
// nothing changed: Goose's model field asked them each poll, for the
// picker and again for Drift, each provider's models made into options
// anew (20 MB of forty polls with real accounts). Each caller still has
// a list of its own, and a catalog read since is offered.
func TestOwnOptionsKept(t *testing.T) {
syncHome(t)
writeCatalog := func(extra string) {
var ms []string
for i := range 200 {
ms = append(ms, fmt.Sprintf(`"m%d": {"id":"m%d","name":"M%d"}`, i, i, i))
}
if extra != "" {
ms = append(ms, fmt.Sprintf(`"%s": {"id":"%s","name":"%s"}`, extra, extra, extra))
}
if err := os.WriteFile(catalog.CachePath(), []byte(`{"openai": {"name":"OpenAI","models": {`+strings.Join(ms, ",")+`}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
}
writeCatalog("")
values := func() map[string]bool {
out := map[string]bool{}
for _, o := range ownOptions("", "", "openai") {
out[o.Value] = true
}
return out
}
if got := values(); !got["openai/m7"] || len(got) < 200 {
t.Fatalf("openai's models not offered: %d", len(got))
}
built := testing.AllocsPerRun(5, func() { ownProvider(nil, "openai") })
kept := testing.AllocsPerRun(20, func() { ownOptions("", "", "openai") })
if kept*5 > built {
t.Fatalf("a provider's options already built took %.0f allocations, building them %.0f", kept, built)
}
mine := ownOptions("", "", "openai")
for i := range mine {
mine[i].Value = "x"
}
if got := values(); !got["openai/m7"] {
t.Fatal("a caller's write reached the options kept")
}
writeCatalog("fresh")
if !values()["openai/fresh"] {
t.Fatal("a model the catalog lists since is not offered")
}
}