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.
60 lines
2.3 KiB
Go
60 lines
2.3 KiB
Go
package codexcat
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// TakesEffort is asked per model each window poll; it reads Codex's
|
|
// models_cache.json again only when the file changes, and still says what
|
|
// the cache says: its level for Codex's own and a ChatGPT account's model,
|
|
// none for one magpie added, the levels' default for one not in it.
|
|
func TestTakesEffortReadsTheCacheOnChange(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("CODEX_HOME", "")
|
|
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
|
|
path := filepath.Join(home, ".codex", "models_cache.json")
|
|
write := func(level string, at time.Time) {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, `{"etag":"e","models":[{"slug":"gpt-6.1-sol","default_reasoning_level":%q},{"slug":"magpie/x","description":"X via magpie","default_reasoning_level":"high"}`, level)
|
|
for i := range 300 {
|
|
fmt.Fprintf(&b, `,{"slug":"m-%d","description":"%s","default_reasoning_level":"medium"}`, i, strings.Repeat("words ", 40))
|
|
}
|
|
b.WriteString("]}")
|
|
if err := os.WriteFile(path, []byte(b.String()), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Chtimes(path, at, at)
|
|
}
|
|
write("low", time.Now().Add(-time.Hour))
|
|
efforts := []string{"low", "medium", "high"}
|
|
ms := []catalog.Model{{ID: "gpt-6.1-sol", Efforts: efforts}, {ID: "codex/gpt-6.1-sol", Efforts: efforts}, {ID: "magpie/x", Efforts: efforts}, {ID: "other", Efforts: efforts}}
|
|
if got := TakesEffort(ms, "gpt-6.1-sol"); got != "low" {
|
|
t.Errorf("Codex's own: %q, want low", got)
|
|
}
|
|
if got := TakesEffort(ms, "codex/gpt-6.1-sol"); got != "low" {
|
|
t.Errorf("a ChatGPT account's: %q, want low", got)
|
|
}
|
|
if got, want := TakesEffort(ms, "magpie/x"), DefaultEffort(efforts); got != want {
|
|
t.Errorf("one magpie added: %q, want the default %q", got, want)
|
|
}
|
|
if got, want := TakesEffort(ms, "other"), DefaultEffort(efforts); got != want {
|
|
t.Errorf("not in the cache: %q, want %q", got, want)
|
|
}
|
|
if n := testing.AllocsPerRun(20, func() { TakesEffort(ms, "gpt-6.1-sol") }); n > 20 {
|
|
t.Errorf("a look at an unchanged cache: %.0f allocations, want a few", n)
|
|
}
|
|
write("xhigh", time.Now().Add(time.Minute))
|
|
if got := TakesEffort(ms, "gpt-6.1-sol"); got != "xhigh" {
|
|
t.Errorf("after Codex wrote the cache: %q, want xhigh", got)
|
|
}
|
|
}
|