gui: a provider row's models and agents are made once at their size

providerInfoWith grew a row's model list by append for the decision
models the vendor list lacks and for the picks it lacked, then grew it
again with slices.Insert to put those picks in front, and grew Agents
one append at a time. The rows are now counted first and each list is
made once at its size, in the same order as before.

Real accounts (sandbox copy of the owner's HOME, network blocked),
40 polls of /api/state, /api/providers and drift, pprof alloc_space
between the two heap snapshots, v0.1.2484 (mhost) vs this (mrows):
  providerInfoWith flat 50.7 MB -> 38.5 MB, cum 164.9 MB -> 138.8 MB
  providersState cum 273.2 MB -> 270.5 MB (the total is noisy +-25 MB
  from the state encoder's pooled buffers: 621 vs 637 MB)
  /api/state and drift bodies byte-identical; /api/providers equal
  but for `fetched` and one login's `lapsed`, set by a refused refresh
  in the blocked sandbox when a token came due during one of the runs
  (logins_on.go), a path this change doesn't touch.

TestProviderRowListsMadeAtTheirSize: 107 rows from 100 listed models,
7 picks the list lacks plus one it has, 3 agents; picks in front in
their order, cap == len for Models and Agents. On the old
providers.go (at HEAD) it fails: "models made for 219, 107 rows".
Forcing growth by hand in the new code fails it the same way.

Run on mote at this tree: gofmt, go vet, GOOS=windows go vet ok; the
new test and the model-order tests 3/3 ok; go test ./internal/gui:
only the known reds TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners (#3949) failed.
Not run: the full ./... suite.
This commit is contained in:
yetone
2026-11-01 06:01:05 +08:00
parent 5bfa9f0762
commit 616405061a
2 changed files with 54 additions and 10 deletions
@@ -1,10 +1,12 @@
package gui
import (
"fmt"
"os"
"slices"
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
@@ -35,3 +37,34 @@ func TestUnlistedPicksShownFirst(t *testing.T) {
t.Fatalf("%d exposed, want 4", info.Exposed)
}
}
// A row's models and agents are each made once, at their size: grown by
// append for the picks the list lacks and again to put them in front, it
// was 8 MB of what twenty polls of the Providers page allocated with real
// accounts.
func TestProviderRowListsMadeAtTheirSize(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
p := provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.example/v1"}
var avail, exposed []catalog.Model
for i := range 100 {
avail = append(avail, catalog.Model{ID: fmt.Sprintf("m-%d", i)})
}
for i := range 7 {
exposed = append(exposed, catalog.Model{ID: fmt.Sprintf("pick-%d", i)})
}
exposed = append(exposed, avail[3])
agents := []agentUse{{Agent: &agent.Agent{ID: "a"}}, {Agent: &agent.Agent{ID: "b"}}, {Agent: &agent.Agent{ID: "c"}}}
info := providerInfoWith(p, provider.Listing{Available: avail, Exposed: exposed}, agents, nil)
if len(info.Models) != 107 || info.Models[0].ID != "pick-6" || info.Models[7].ID != "m-0" {
t.Fatalf("%d models, first %q, eighth %q", len(info.Models), info.Models[0].ID, info.Models[7].ID)
}
if cap(info.Models) != len(info.Models) {
t.Fatalf("models made for %d, %d rows: grown on the way", cap(info.Models), len(info.Models))
}
if len(info.Agents) != 3 || cap(info.Agents) != 3 {
t.Fatalf("agents made for %d, %d rows", cap(info.Agents), len(info.Agents))
}
}
+21 -10
View File
@@ -667,7 +667,7 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
Chat: p.Chat, Responses: p.Responses, Anthropic: p.Anthropic, Gemini: p.Gemini, Decide: p.Decide, BaseAPI: p.BaseAPI, ModelTest: p.ModelTest(), TestsAs: p.TestClients(), DecideTest: p.AsksDecideModels(), MarksKinds: p.MarksKinds(),
Catalog: p.Catalog, Website: p.Website, KeysURL: p.KeysURL,
Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, AccountNames: p.AccountNames, Headers: p.Headers, ExtraBody: p.ExtraBody, ReplyHeaders: p.ReplyHeaders, Searches: p.Searches, Cline: p.ClinePinnable(), PinUpstream: p.PinUpstream, Unredacted: p.Unredacted, CountFree: p.CountFree, InsecureTLS: p.InsecureTLS, TakesInsecureTLS: p.TakesInsecureTLS(), LimitRestOff: p.LimitRestOff, NativeCompaction: p.NativeCompaction, CarriesCompaction: p.CarriesCompaction(), PassUserAgent: p.PassUserAgent, OwnClient: p.OwnClient(), AuthHeader: p.AuthHeader, BothAuth: p.BothAuth(), BalanceURL: p.BalanceURL, BalancePath: p.BalancePath, BalanceMethod: p.BalanceMethod, ModelsURL: p.ModelsURL,
Ready: p.Ready(), Chosen: p.PicksOf(avail), Models: make([]modelJSON, 0, len(avail)), Agents: []providerAgent{},
Ready: p.Ready(), Chosen: p.PicksOf(avail), Agents: make([]providerAgent, 0, len(agents)),
Fallback: p.Fallback, Routing: p.Routing, Sink: p.Sink, Affinity: p.Affinity, KeepLogin: p.KeepLogin, KeepLoginAs: p.KeepLoginAs, SwitchOnly: p.SwitchOnly, CapStop: p.CapStop, CapCareful: p.CapCareful, Unlisted: p.Unlisted, NoModels: p.ServesNone(), BestInFamily: p.BestInFamily && len(p.Models) == 0 && !p.NoModels, Off: p.Off, Contexts: p.Contexts,
MaxConcurrency: p.MaxConcurrency, PluginConcurrency: p.PluginConcurrency(), PriceRate: p.PriceRate,
RateWindows: p.RateWindows, RateNow: rateNowOf(p, time.Now()),
@@ -805,7 +805,6 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
for _, m := range ls.Newest {
newest[m.ID] = true
}
seen := map[string]bool{}
names, kept := p.ModelNames(), p.ModelEfforts()
// only read, so the settings every look shares: a copy of its own for
// each provider was 16 MB of what twenty window polls allocated
@@ -890,9 +889,13 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
}
return j
}
// the rows counted before any is made, so the list is made once at its
// size: grown by append for the decision models and the picks it
// lacked, and again to put those picks in front, it was 8 MB of what
// twenty polls of the Providers page allocated with real accounts
seen := make(map[string]bool, len(avail))
for _, m := range avail {
seen[m.ID] = true
out.Models = append(out.Models, named(m, exposed[m.ID]))
}
// Keep an explicit empty list for remote magpie: the editor must not
// guess that a Jev Router chat model answers System One.
@@ -900,20 +903,20 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
if p.IsRemoteMagpie() {
deciders = []string{}
}
var unlisted []catalog.Model // decision models the vendor list lacks
for _, m := range p.DecisionModels() {
deciders = append(deciders, m.ID)
if !seen[m.ID] {
seen[m.ID] = true
out.Models = append(out.Models, named(m, exposed[m.ID]))
unlisted = append(unlisted, m)
}
}
// picks the vendor list does not know go first, so they are visible,
// the last one picked foremost; put in front all at once: a fresh list
// for each was 13 MB of what twenty window polls allocated
var front []modelJSON
// the last one picked foremost
var front []catalog.Model
for _, m := range exposedList {
if !seen[m.ID] {
front = append(front, named(m, true))
front = append(front, m)
// one picked by hand that is a decision model all the same
// (OpenRouter's inception/mercury-decide:free, coeo91 on
// Discord) is tested and marked as one in the editor
@@ -922,8 +925,16 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
}
}
}
slices.Reverse(front)
out.Models = slices.Insert(out.Models, 0, front...)
out.Models = make([]modelJSON, 0, len(front)+len(avail)+len(unlisted))
for i := len(front) - 1; i >= 0; i-- {
out.Models = append(out.Models, named(front[i], true))
}
for _, m := range avail {
out.Models = append(out.Models, named(m, exposed[m.ID]))
}
for _, m := range unlisted {
out.Models = append(out.Models, named(m, exposed[m.ID]))
}
if deciders != nil {
out.Deciders = &deciders
}