diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 7b5575249..505d9990c 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -627,9 +627,11 @@ func Detected() []*Agent { } // Find resolves a user-typed name (id, alias, or unique prefix). -func Find(q string) (*Agent, error) { +func Find(q string) (*Agent, error) { return FindIn(All(), q) } + +// FindIn is Find among all, a list of All() the caller has built already. +func FindIn(all []*Agent, q string) (*Agent, error) { q = strings.ToLower(strings.TrimSpace(q)) - all := All() var prefix []*Agent for _, a := range all { if strings.EqualFold(a.ID, q) { // codex@wsl:Ubuntu diff --git a/internal/gui/account_label_agents_test.go b/internal/gui/account_label_agents_test.go new file mode 100644 index 000000000..ccda3cb7b --- /dev/null +++ b/internal/gui/account_label_agents_test.go @@ -0,0 +1,46 @@ +package gui + +import ( + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/agent" + "github.com/yetone/magpie/internal/filememo" + "github.com/yetone/magpie/internal/provider" +) + +// Each account's row on the Providers page is named for its agent, and +// agent.Find built every agent magpie knows (about a hundred, each with +// its files looked at) again for each row: with the owner's config 58 MB +// of the 1.4 GB a magpie allocated over 20 window polls. A request builds +// them once for all its rows. +func TestAccountLabelsBuildTheAgentsOnce(t *testing.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")) + codex, err := agent.Find("codex") + if err != nil { + t.Fatal(err) + } + p := provider.Provider{ID: "codex-a", Name: "Codex A", Account: &provider.Account{Agent: "codex", User: "a"}} + if name, icon := accountLabel(p); name != codex.Name || icon != codex.Icon { + t.Fatalf("not held: %q %q, want %q %q", name, icon, codex.Name, codex.Icon) + } + all := testing.AllocsPerRun(3, func() { agent.All() }) + + // as a GET /api/* is served (held) + defer provider.Hold()() + defer filememo.Hold()() + accountLabel(p) // the request's first row builds them + row := testing.AllocsPerRun(50, func() { + if name, icon := accountLabel(p); name != codex.Name || icon != codex.Icon { + t.Errorf("held: %q %q, want %q %q", name, icon, codex.Name, codex.Icon) + } + }) + t.Logf("a row: %.0f allocations; building the agents: %.0f", row, all) + if row > all/20 { + t.Errorf("a row took %.0f allocations against %.0f to build every agent: the agents were built for it", row, all) + } +} diff --git a/internal/gui/providers.go b/internal/gui/providers.go index e3d063c04..9afb89295 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -369,6 +369,14 @@ type accountJSON struct { WSL string `json:"wsl,omitempty"` } +// agentNamed is agent.Find among the agents built once for the request +// (provider.Held). Each account's row named its agent, and Find built all +// of them again for it: with the owner's config 58 MB of the 1.4 GB a +// magpie allocated over 20 window polls. +func agentNamed(q string) (*agent.Agent, error) { + return agent.FindIn(provider.Held("agent.All", agent.All), q) +} + // accountLabel is the name and logo an account is shown with: its agent's, // the subscription's for one no agent magpie configures, and a plugin's // provider's for a plugin's sign-in, whose agent is "plugin" (#694 listed a @@ -388,7 +396,7 @@ func accountLabel(p provider.Provider) (name, icon string) { } else if a.Agent == provider.ChatGPTAPIID { // a ChatGPT plan through OpenAI's API, not Codex's backend name, icon = "ChatGPT API", "openai" - } else if ag, err := agent.Find(a.Agent); err == nil { + } else if ag, err := agentNamed(a.Agent); err == nil { name, icon = ag.Name, ag.Icon } else if a.Agent == "cursor" { // a Cursor subscription is served by the gateway, not an agent magpie configures @@ -992,7 +1000,7 @@ func providersState() providersJSON { // a removed account as its row was named: a plugin's by the // provider it signs in to, not its agent "plugin" (#694) e.Name, e.Icon = accountLabel(p) - } else if a, err := agent.Find(x.Agent); err == nil { + } else if a, err := agentNamed(x.Agent); err == nil { e.Name, e.Icon = a.Name, a.Icon } s.Excluded = append(s.Excluded, e) diff --git a/internal/provider/hold.go b/internal/provider/hold.go index 1076d7a4b..543b818b6 100644 --- a/internal/provider/hold.go +++ b/internal/provider/hold.go @@ -179,6 +179,10 @@ func StampNow() Stamp { roots: [3]rootStamp{stampOf(Path()), stampOf(settings.Path()), stampOf(catalog.CachePath())}} } +// Held is what build gives, built once while a request holds it (Hold), +// for what each row of a page looks up: shared by them, so read only. +func Held[T any](key string, build func() T) T { return heldOf(key, build) } + // heldOf is what build gives, built once while a request holds it. func heldOf[T any](key string, build func() T) T { held.Lock()