Files
magpie/internal/agent/codex_own_test.go
T
yetone fb4632ee0c agent, provider: Codex's polls and the usage quotas read what is kept
Heap profile diff of the live magpie (v0.1.2906, /v1/magpie/heap, two
idle minutes with the window open: 269 MB allocated):
- ownCodex found the codex provider through provider.All, a copy of every
  provider, on each look (~9 MB). Its picks are now kept per stamp
  (codexPicks, stampedLists), read only.
- Codex's model and effort fields copied magpie's whole model list on
  each Options call, every poll of the effort field (~11 MB). They read
  sharedModels, the list kept, which they only read.
- visibleQuotas built every provider's offered models to narrow Gemini's
  and Antigravity's quotas (~20 MB). exposedIDs builds the providers
  asked only.

Run (mote, at this tree):
- gofmt -l internal, go vet ./..., GOOS=windows and GOOS=linux go vet
  ./internal/agent/ ./internal/provider/: clean
- go test -count=1 -timeout 45m ./... (all three commits' tree): 85
  packages ok, internal/gateway 748s, internal/provider 193s. Two red:
  internal/gui's TestMainWindowCorners/macOS_26_SDK, mote's macOS 27
  (#3949, filed before, untouched here), and internal/agent's
  TestCodexEffortOptionsReadTheKeptList, whose bar counted the
  environment's copy (see the effort test's comment), fixed in its test.
- then go test -count=1 -timeout 45m ./internal/agent/ with that fix:
  ok (200.1s)

Broken by hand:
- codexPicks through provider.Find again: 285 allocations, red
  (TestCodexOwnModelsFollowPicks, bar 20).
- the effort field on magpieModels again: 199896 bytes a look past
  its StampNow's 1712, red (TestCodexEffortOptionsReadTheKeptList, bar
  20000). The test takes the look's StampNow off: os.Environ copies the
  environment, and each t.Setenv before it in the package made that
  longer (an Unsetenv leaves its slot), ~56 KB a look in the whole
  package; run alone it is 1.7 KB.
- exposedIDs without the ids check: "other" built, red
  (TestExposedIDsBuildsOnlyTheProvidersAsked).

Not tried: the numbers on the live app after this build; that profile
is taken once it is released.
2026-11-06 12:48:51 +08:00

101 lines
3.4 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Codex's own models follow the ones ticked on its ChatGPT subscription.
func TestCodexOwnModelsFollowPicks(t *testing.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"))
t.Setenv("CODEX_HOME", "")
dir := filepath.Join(home, ".codex")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "models_cache.json"), []byte(`{"models":[
{"slug":"gpt-a","display_name":"A","priority":1},
{"slug":"gpt-b","display_name":"B","priority":2},
{"slug":"gpt-c","display_name":"C","priority":3}]}`), 0o644)
os.WriteFile(filepath.Join(dir, "auth.json"), []byte(`{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`), 0o600)
own := func() []string {
var out []string
for _, o := range codex(home).Fields[0].Options(nil) {
if o.Group == "OpenAI" {
out = append(out, o.Value)
}
}
return out
}
if got := own(); len(got) != 3 {
t.Fatalf("nothing ticked: %v", got)
}
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-c", "gpt-a"}}); err != nil {
t.Fatal(err)
}
if got := own(); len(got) != 2 || got[0] != "gpt-a" || got[1] != "gpt-c" {
t.Fatalf("ticked a and c: %v", got)
}
for _, o := range viaMagpie("codex", "") {
if o.Ref == "" || o.Ref != o.Value {
t.Fatalf("magpie option without its catalog ref: %+v", o)
}
}
// the ticks are read once while nothing changed, not by building every
// provider again on each look (each poll of the Agents page)
if n := leastAllocs(20, func() { codexPicks() }); n > 20 {
t.Fatalf("a look at Codex's ticks made %v allocations, want at most 20", n)
}
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-b"}}); err != nil {
t.Fatal(err)
}
if got := own(); len(got) != 1 || got[0] != "gpt-b" {
t.Fatalf("ticked b after a and c: %v", got)
}
}
// The codex provider switched off narrows nothing: Codex's own models are
// all there, its picks kept for when it is switched on again.
func TestCodexOwnModelsNotNarrowedWhileOff(t *testing.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"))
t.Setenv("CODEX_HOME", "")
dir := filepath.Join(home, ".codex")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "models_cache.json"), []byte(`{"models":[
{"slug":"gpt-a","display_name":"A","priority":1},
{"slug":"gpt-b","display_name":"B","priority":2}]}`), 0o644)
os.WriteFile(filepath.Join(dir, "auth.json"), []byte(`{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`), 0o600)
own := func() []string {
var out []string
for _, o := range codex(home).Fields[0].Options(nil) {
if o.Group == "OpenAI" {
out = append(out, o.Value)
}
}
return out
}
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-b"}, Off: true}); err != nil {
t.Fatal(err)
}
if got := own(); len(got) != 2 {
t.Errorf("switched off: %v (want both, its picks narrow nothing)", got)
}
if err := provider.SetOff("codex", false); err != nil {
t.Fatal(err)
}
if got := own(); len(got) != 1 || got[0] != "gpt-b" {
t.Errorf("switched on: %v (want just gpt-b)", got)
}
}