diff --git a/internal/gui/agent_models.go b/internal/gui/agent_models.go index bd6937a55..8058461b9 100644 --- a/internal/gui/agent_models.go +++ b/internal/gui/agent_models.go @@ -6,6 +6,8 @@ import ( "net/http" "slices" "strings" + "sync" + "time" "github.com/yetone/magpie/internal/agent" "github.com/yetone/magpie/internal/provider" @@ -106,7 +108,49 @@ func agentModelCount(a *agent.Agent, fields []fieldJSON) *modelCountJSON { return nil } +// modelCounts keeps each agent's count while provider.StampNow stays the +// same, for a minute at most, as the agents' model lists are kept +// (agent's magpieModelsFor): every poll of the Agents page counted each +// agent's list anew, ListedFor built whole for a number (0.27 GB of the +// 2.6 GB a real config allocated over 20 polls). +var modelCounts struct { + sync.Mutex + m map[string]keptCount +} + +type keptCount struct { + c modelCountJSON + stamp provider.Stamp + at time.Time +} + +// modelCountFor is how long a count is kept while the stamp says nothing +// changed: what it can't see (another app rewriting an account's file, a +// model retired by its date) reaches the count within it. +const modelCountFor = time.Minute + func modelCount(id string) *modelCountJSON { + modelCounts.Lock() + k, ok := modelCounts.m[id] + modelCounts.Unlock() + // the stamp is read before the count is made: a change while it is + // moves it past the one kept, and the next call counts again + stamp := provider.StampNow() + if !ok || k.stamp != stamp || time.Since(k.at) >= modelCountFor { + k = keptCount{countModels(id), stamp, time.Now()} + modelCounts.Lock() + if modelCounts.m == nil { + modelCounts.m = map[string]keptCount{} + } + modelCounts.m[id] = k + modelCounts.Unlock() + } + c := k.c + c.By, c.Names = slices.Clone(c.By), slices.Clone(c.Names) + return &c +} + +func countModels(id string) modelCountJSON { listed, _ := provider.ListedFor(id) off := provider.ModelOff(id) c := &modelCountJSON{Listed: len(listed)} @@ -131,7 +175,7 @@ func modelCount(id string) *modelCountJSON { } c.By[i].N++ } - return c + return *c } // usedBy reports whether one of the agent's values is the entry: its id, diff --git a/internal/gui/model_count_kept_test.go b/internal/gui/model_count_kept_test.go new file mode 100644 index 000000000..b23884cfc --- /dev/null +++ b/internal/gui/model_count_kept_test.go @@ -0,0 +1,75 @@ +package gui + +import ( + "fmt" + "os" + "runtime" + "slices" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" +) + +// An agent's model count is kept while nothing it is counted from changed: +// every poll of the Agents page counted each agent's list anew, its whole +// ListedFor built for a number. A provider or a model hidden since is +// counted at once. +func TestModelCountKeptWhileNothingChanged(t *testing.T) { + sandboxHome(t) + var models []string + for i := range 300 { + models = append(models, fmt.Sprintf("m-%d", i)) + } + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil { + t.Fatal(err) + } + // written long enough ago that its time tells a later write apart + old := time.Now().Add(-time.Hour) + if err := os.Chtimes(provider.Path(), old, old); err != nil { + t.Fatal(err) + } + c := modelCount("codex") + if c.Listed < 300 || c.Shown != c.Listed { + t.Fatalf("relay's models not counted: %+v", c) + } + bytesPer := func(f func()) uint64 { + f() + var a, b runtime.MemStats + runtime.ReadMemStats(&a) + for range 20 { + f() + } + runtime.ReadMemStats(&b) + return (b.TotalAlloc - a.TotalAlloc) / 20 + } + built := bytesPer(func() { countModels("codex") }) + kept := bytesPer(func() { modelCount("codex") }) + if kept*20 > built { + t.Fatalf("a count already made took %d bytes, counting %d", kept, built) + } + + // each caller's copy is its own + mine := modelCount("codex") + mine.Names[0] = "mine" + mine.By[0].N = -1 + if c := modelCount("codex"); c.Names[0] == "mine" || c.By[0].N < 0 { + t.Fatalf("a caller's write reached the count kept: %+v", c) + } + + if err := provider.Save(provider.Provider{ID: "nb", Name: "NB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x"}}); err != nil { + t.Fatal(err) + } + if got := modelCount("codex"); got.Listed != c.Listed+1 { + t.Fatalf("a provider saved since is not counted: %d, was %d", got.Listed, c.Listed) + } + s := settings.Load() + s.HiddenModels = map[string][]string{"codex": {"relay/m-1"}} + if err := settings.Save(s); err != nil { + t.Fatal(err) + } + if got := modelCount("codex"); got.Shown != got.Listed-1 || slices.Contains(got.Names, "m-1") { + t.Fatalf("a model hidden since is still counted: %+v", got) + } +}