From 2e9df9b1be6efd8f42b9b222377bb573af62e6ca Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 01:57:32 +0800 Subject: [PATCH] agent: which of Codex's models magpie serves is kept while the catalog stays codexInMagpie marks Codex's own models that magpie serves on the user's ChatGPT account. Codex's options are asked per field of its Drift on every drift poll, and each time it built Codex's whole catalog (provider.CatalogFor) for these few names: 0.2 GB of the 2.6 GB a real config allocated over 20 polls. The names are now kept in a stampedLists, as magpieModels and viaMagpie are: built again when provider.StampNow moves, or a minute after they were. TestCodexServedKeptWhileCatalogStays: a signed-in account beside a 300-model relay; gpt-5.5 is marked served and the relay's model is not, a kept answer takes under a twentieth of a build's bytes, and the account's models picked since are followed at once. Red with the set found each time ("a set already found took 9948425 bytes, finding it 9954954"). Run on mote at this tree (0fe9f97c1 + this commit): go vet ./... for darwin, windows and linux (internal/gui skipped on linux, wails cgo), gofmt -l clean, go test -timeout 45m ./internal/agent (ok 402s) and ./internal/gateway (ok 963s). --- internal/agent/codex.go | 26 ++++++-- internal/agent/codex_served_kept_test.go | 77 ++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 internal/agent/codex_served_kept_test.go diff --git a/internal/agent/codex.go b/internal/agent/codex.go index e83a44820..e6b6c45a0 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -2130,12 +2130,9 @@ func ownCodex() []catalog.Model { // group gave the picker a catalog entry, and Codex's picker was the group // alone (shiqi_40904 on Discord). func codexInMagpie(own []Option) []Option { - shown, _ := provider.CatalogFor("codex") on := map[string]bool{} - for _, e := range shown { - if provider.CodexOwn(e) { - on[e.Model] = true - } + for _, m := range servedOnCodex.get("", codexServedModels) { + on[m] = true } for i := range own { own[i].Served = on[own[i].Value] @@ -2143,6 +2140,25 @@ func codexInMagpie(own []Option) []Option { return own } +// servedOnCodex is codexServedModels kept as magpie's lists are +// (stampedLists): Codex's options, built per field of its Drift on every +// drift poll, built Codex's whole catalog each time for these few names +// (0.2 GB of the 2.6 GB a real config allocated over 20 polls). +var servedOnCodex stampedLists[string] + +// codexServedModels is the models magpie's list shows Codex on a ChatGPT +// account of the user's. +func codexServedModels() []string { + shown, _ := provider.CatalogFor("codex") + var out []string + for _, e := range shown { + if provider.CodexOwn(e) { + out = append(out, e.Model) + } + } + return out +} + // codexViaMagpieFor is viaMagpieFor("codex") keeping the models magpie // serves on a ChatGPT account that own, Codex's own list, leaves out: one // Codex hides from its picker (gpt-6.1-terra) but the user ticked on the diff --git a/internal/agent/codex_served_kept_test.go b/internal/agent/codex_served_kept_test.go new file mode 100644 index 000000000..ef799648e --- /dev/null +++ b/internal/agent/codex_served_kept_test.go @@ -0,0 +1,77 @@ +package agent + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "os" + "path/filepath" + "runtime" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +// Which of Codex's own models magpie serves on the user's ChatGPT account +// is kept while nothing the catalog is built from changed: Codex's +// options, asked per field of its Drift on every drift poll, built +// Codex's whole catalog each time for these few names. A provider saved +// since is seen at once. +func TestCodexServedKeptWhileCatalogStays(t *testing.T) { + claims := func(m map[string]any) string { + b, _ := json.Marshal(m) + return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s" + } + me, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{ + "id_token": claims(map[string]any{"email": "me@example.com"}), + "access_token": claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}), + "refresh_token": "r-1", "account_id": "acct-1"}}) + home, _ := codexHome(t, string(me), "model = \"gpt-5.5\"\n") + os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), + []byte(`{"models":[{"slug":"gpt-5.5","display_name":"GPT-5.5","priority":1}, + {"slug":"gpt-5.4","display_name":"GPT-5.4","priority":2}]}`), 0o644) + 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) + os.Chtimes(provider.Path(), old, old) + + served := func(model string) bool { + return codexInMagpie([]Option{{Value: model}})[0].Served + } + if !served("gpt-5.5") { + t.Fatalf("gpt-5.5 on the user's account not marked served: %v", codexServedModels()) + } + if served("m-1") { + t.Fatal("a relay's model marked as served on the account") + } + 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() { codexServedModels() }) + kept := bytesPer(func() { served("gpt-5.5") }) + if kept*20 > built { + t.Fatalf("a set already found took %d bytes, finding it %d", kept, built) + } + + // the account's models picked since are seen at once + if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.4"}}); err != nil { + t.Fatal(err) + } + if served("gpt-5.5") || !served("gpt-5.4") { + t.Fatalf("the account's picks since not followed: %v", codexServedModels()) + } +}