Files
magpie/internal/agent/described_once_test.go
T
yetone 78cbb711d9 agent, codexcat: a poll asks Vision once per stamp and reads Codex's cache levels once per change
A heap profile of the live magpie (v0.1.2646, 37 minutes, the owner's
real accounts) had buildModels allocate 1288MB. 553MB of it was the
gateway's seer, asked once per list build (provider.Described), and
238MB was codexEfforts -> TakesEffort -> CacheEntries, which decodes the
whole Codex models_cache.json for each model asked.

- buildModels asks describedNow(), kept per provider.StampNow() in a
  stampedLists like the lists themselves, so every agent's list of one
  stamp shares one answer, and a change (a provider saved) asks again.
- TakesEffort reads a slug's default level from cacheLevels(), a
  filememo.Read of the cache file by its stat: the decode keeps only
  each model's level, and runs again when the file changes. magpie's own
  added entries are skipped as CacheEntries skips them.

Run on mote (macOS 27.2):
- TestListsAskVisionOnce (new): pi, claude, codex, opencode, droid and
  dsh lists ask Described once; after a provider save, once more, and a
  "no" reaches the new list. With buildModels asking Described itself
  (put back by hand): red. Restored: ok.
- TestTakesEffortReadsTheCacheOnChange (new): a 300-model cache, levels
  for a slug and codex/<slug>, the default for magpie/x and an unknown
  one, 20 allocs or fewer a look, and the new level after a rewrite a
  minute later. With TakesEffort on CacheEntries again (by hand): red.
  Restored: ok.
- go test ./internal/agent ./internal/codexcat at this tree: ok (162.5s).
  ./internal/gateway and ./internal/gui at the tree before the test's
  last fix (only the new test changed since): ok but
  TestCodexPoolSaysTheClientsVersion (#4807, red in a full gateway run
  on mote, untouched here) and TestMainWindowCorners/macOS_26_SDK
  (#3949, red on macOS 27).
- gofmt -l clean; go vet, GOOS=windows and GOOS=linux go vet of both
  packages: ok.

Not tried: a heap profile of a live build with this change.
2026-11-02 06:59:17 +08:00

50 lines
1.6 KiB
Go

package agent
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Every agent's list asks whether magpie describes images to a model that
// can't see them; the gateway answers by ranking every provider's models
// that see, so it is asked once for all the lists built while nothing
// changed, and again once something did.
func TestListsAskVisionOnce(t *testing.T) {
syncHome(t)
var asked int
was := provider.Described
provider.Described = func() bool { asked++; return true }
t.Cleanup(func() { provider.Described = was })
if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5"}}); err != nil {
t.Fatal(err)
}
agents := []string{"pi", "claude", "codex", "opencode", "droid", "dsh"}
for _, a := range agents {
if ms := sharedModels(a); len(ms) == 0 || !ms[len(ms)-1].Images {
t.Fatalf("%s: %+v, want its models taking images", a, ms)
}
}
if n := asked; n != 1 {
t.Errorf("%d agents' lists asked Vision %d times, want once", len(agents), n)
}
provider.Described = func() bool { asked++; return false }
if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5", "gpt-5.4"}}); err != nil {
t.Fatal(err)
}
ms, ids := sharedModels("pi"), ""
for _, m := range ms {
ids += " " + m.ID
if m.Images {
t.Errorf("after a change: %s takes images, want none to", m.ID)
}
}
if !strings.Contains(ids, "oa/gpt-5.4") {
t.Errorf("after a change:%s, want oa/gpt-5.4 among them", ids)
}
if asked != 2 {
t.Errorf("Vision asked %d times in all, want twice", asked)
}
}