provider: CatalogFor is kept across polls until something it reads changes
Each /api/state poll and each Codex subagent list rebuilt an agent's catalog from scratch. In a real-account heap profile (m2463, 20 polls) CatalogFor and catalogKeeping were 69 + 36 MB of what was allocated, and CodexSubagentModels' 39 MB came through the same call. CatalogFor now goes through keptOf under "catalog-for\x00"+agent, so it is rebuilt only when the stamp keptOf already watches changes. Callers get their own clones of the shown and hidden lists, so a caller that writes into them never reaches the kept copy or another caller. Run on mote (mote-run, Go 1.26.8): - gofmt clean; go vet ok, also GOOS=windows and GOOS=linux. - TestCatalogForKeptAcrossPolls alone: ok. - Broken by hand (keptOf bypassed): red, "20 polls with nothing changed built the catalog 20 times, want 0". - go test -timeout 45m ./internal/agent ./internal/gateway: ok. ./internal/provider: ok except this test's first version, which wrote s[0] before reading its ids; fixed, and it passes alone.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// An agent's catalog is kept with the catalog across a window's polls
|
||||
// while nothing it is built from moved, not built again from every
|
||||
// provider's models on each; a provider saved, or a model taken out of
|
||||
// the agent's list, has it built again. A caller's change to its lists
|
||||
// never reaches the next caller's.
|
||||
func TestCatalogForKeptAcrossPolls(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 poll after the last one's hold has gone
|
||||
keptFor = time.Minute // a loaded machine's pause isn't the bound
|
||||
if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1", "m2"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids := func(es []Entry) []string {
|
||||
var out []string
|
||||
for _, e := range es {
|
||||
if e.Provider.ID == "relay" {
|
||||
out = append(out, e.ID)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
every := func(es []Entry) []string {
|
||||
var out []string
|
||||
for _, e := range es {
|
||||
out = append(out, e.ID)
|
||||
}
|
||||
return out
|
||||
}
|
||||
poll := func() (shown, hidden []string) {
|
||||
defer Hold()()
|
||||
s, h := CatalogFor("opencode")
|
||||
fs, fh := catalogFor("opencode")
|
||||
if !slices.Equal(every(s), every(fs)) || !slices.Equal(every(h), every(fh)) {
|
||||
t.Fatalf("kept %v/%v, built afresh %v/%v", every(s), every(h), every(fs), every(fh))
|
||||
}
|
||||
shown, hidden = ids(s), ids(h)
|
||||
if len(s) > 0 {
|
||||
s[0].ID = "written" // the caller's own
|
||||
}
|
||||
return shown, hidden
|
||||
}
|
||||
if s, _ := poll(); !slices.Equal(s, []string{"relay/m1", "relay/m2"}) {
|
||||
t.Fatalf("shown %v", s)
|
||||
}
|
||||
before := catalogsBuilt.Load()
|
||||
for range 20 {
|
||||
poll()
|
||||
}
|
||||
if n := catalogsBuilt.Load() - before; n != 0 {
|
||||
t.Fatalf("20 polls with nothing changed built the catalog %d times, want 0", n)
|
||||
}
|
||||
if err := SetHiddenModels("opencode", []string{"relay/m1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s, h := poll(); !slices.Equal(s, []string{"relay/m2"}) || !slices.Equal(h, []string{"relay/m1"}) {
|
||||
t.Fatalf("after hiding m1: shown %v, hidden %v", s, h)
|
||||
}
|
||||
if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m3"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s, _ := poll(); !slices.Equal(s, []string{"relay/m3"}) {
|
||||
t.Fatalf("after a save the poll shows %v, want [relay/m3]", s)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
@@ -64,7 +65,26 @@ func described() bool { return Described != nil && Described() }
|
||||
// (none when its lists aren't narrowed): its visibility's, less the models
|
||||
// taken out of its lists one by one (HiddenModels), or, when it is shown
|
||||
// only the models picked for it, those alone (PickedModels).
|
||||
//
|
||||
// It is kept across requests with the catalog (keptOf): a window poll
|
||||
// asks it for every agent, and building each again from the whole
|
||||
// catalog was ~140 MB of what twenty polls allocated. Each caller gets
|
||||
// its own copy of the lists.
|
||||
func CatalogFor(agent string) (shown, hidden []Entry) {
|
||||
c := keptOf("catalog-for\x00"+agent, func() catalogLists {
|
||||
catalogsBuilt.Add(1)
|
||||
s, h := catalogFor(agent)
|
||||
return catalogLists{slices.Clip(s), slices.Clip(h)}
|
||||
})
|
||||
return slices.Clone(c.shown), slices.Clone(c.hidden)
|
||||
}
|
||||
|
||||
type catalogLists struct{ shown, hidden []Entry }
|
||||
|
||||
// catalogsBuilt counts the lists CatalogFor built, for a test.
|
||||
var catalogsBuilt atomic.Int64
|
||||
|
||||
func catalogFor(agent string) (shown, hidden []Entry) {
|
||||
listed, hidden := ListedFor(agent)
|
||||
off := ModelOff(agent)
|
||||
for _, e := range listed {
|
||||
|
||||
Reference in New Issue
Block a user