Files
magpie/internal/usage/codex_used_test.go
T
yetone 28f3c4d7fd codex: the Codex app's first 100 models hold the ones used, hidden ones last
@ZhenLuoTTAWDTT on X: with Codex on magpie the model list stops short,
and the models past it can't be chosen.

Cause, from the clients' own code: the Codex app (ChatGPT.app
26.1013.40934, app-initial-90eb42395b7d.js) and the VS Code extension
(openai.chatgpt 26.51021.22027, model-queries-667cf4bd657d.js) both ask
their app-server for model/list with includeHidden, cursor null and
limit 100 (e?.limit??100) and never ask for nextCursor. codex-rs's
list_models pages the list sorted by priority, hidden entries counted,
so anything past the 100th is in no picker there. The CLI's /model
(tui model_popups.rs) lists every preset in a scrolling popup and has no
cap. magpie truncated nothing; its models are numbered after the
account's own, so a user with many models lost everything past ~100 in
the app, while hidden entries (codex-auto-review and the like) took
slots at the top.

Fix: codexcat.Fit ranks the list last of all, on every route magpie
hands Codex a list (/backend-api/codex/models after the user's own
order, /v1/codex/models before its 1 MiB cut, and the
model_catalog_json file via CatalogUsed): hidden entries go after every
listed one, and with more than 100 listed, the models Codex used in the
last 30 days (usage.CodexUsed, most recent first, at most 100; on
/v1/codex/models only the asking key's, CodexUsedBy) are brought into
the first 100 in the order they stood. The rest keep their order. The
list's ETag and X-Models-Etag carry a tag of the used set
(codexcat.UsedTag), so Codex asks again when it uses a model it hadn't;
CodexUsed is read at most once a minute, or at once when Append writes a
Codex call to a model it doesn't hold. Documented in
docs/subsystems/gateway-routing.md.

Tests (each red on the old code, broken by hand: Fit a no-op, the gateway
calls and the used tag removed, Append's codexUsedNew removed):
TestFitKeepsUsedModelsWhereTheCodexAppReads, TestFitLeavesAShortListsOrder,
TestCatalogUsedFits, TestUsedTagIsTheSet (codexcat), TestCodexUsed
(usage), TestCodexModelListKeepsUsedModelsInTheAppsHundred and
TestCodexCatalogKeepsTheKeysUsedModelsFirst (gateway).

Real run: codex-cli 0.162.1's app-server in a sandbox CODEX_HOME (never
~/.codex) with a model_catalog_json of 3 hidden entries at priority 1-3
and 150 listed models, driven over stdio with the app's own request
(model/list includeHidden, limit 100). Before: page 1 held 100 of 153,
3 of them hidden, the last many/model-096, nextCursor "100". The same
catalog put through Fit with many/model-140 used: page 1 has no hidden
entry and ends with many/model-140. The TUI's /model scrolled to all
150 (tmux).

Run with this change on e4ca47b0f: gofmt -l clean; go vet
-tags nogui with GOOS=linux and GOOS=windows on codexcat, usage, gateway
and agent clean. go test -count=1 -timeout 45m: codexcat ok, usage ok;
-run 'Codex|Fit|Used|ModelList|Catalog' on gateway, usage and agent ok
(122s, 11s, 80s). The whole gateway and agent packages ran too, but the
machine's disk was full under other sessions' runs (209M to 2.5G free),
and 959 lines of that log are "no space left on device". Of the reds,
three are not that: TestFolderVarsAreListed is #3884 (CARGO_HOME, red on
main since e4552cca9); TestHistoryKeepsDaysAndDropsOld (gateway) and
TestSuffixModesReachAgentFiles (agent) failed beside half-written
settings files and pass alone 3 of 3 at this commit. Not run: the full
./... suite (no room on the disk); the Codex app and VS Code extension
themselves against a magpie with this change (their request was read
from their bundles and replayed against a real app-server).

Rebased on ca8a6f346 (agent rename and Codex app route, claude_runs in
gateway) and run again: GOOS=windows go vet on agent
and gateway clean; -run 'Codex|Fit|Used|ModelList|Catalog' ok on
codexcat, usage and gateway (75s), and with |Rename ok on agent (68s).
Then rebased on 94c2aaecb (usage rename) and usage run whole again: ok
(88s).
2026-10-27 18:37:16 +08:00

36 lines
1.7 KiB
Go

package usage
import (
"slices"
"testing"
"time"
)
// CodexUsed is the models Codex asked for, the latest first, its own calls
// alone; a model Codex uses for the first time is in it at once, though
// the list is otherwise read again a minute on (codexcat.Fit puts these in
// the first 100 the Codex app reads).
func TestCodexUsed(t *testing.T) {
pageHome(t)
now := time.Now()
Append(Record{Time: now.Add(-40 * 24 * time.Hour), Agent: "codex", Provider: "p", Requested: "p/too-old", Status: 200})
Append(Record{Time: now.Add(-3 * time.Hour), Agent: "codex", Provider: "p", Requested: "p/a", Status: 200})
Append(Record{Time: now.Add(-2 * time.Hour), Agent: "codex", Provider: "p", Requested: "p/b", Status: 500})
Append(Record{Time: now.Add(-90 * time.Minute), Agent: "claude", Provider: "p", Requested: "p/claude-only", Status: 200})
Append(Record{Time: now.Add(-80 * time.Minute), Agent: "codex", Provider: "p", Requested: "p/keyed", CallerKeyID: "k1", Status: 200})
Append(Record{Time: now.Add(-time.Hour), Agent: "codex", Provider: "p", Requested: "p/a", Status: 200})
if got, want := CodexUsed(), []string{"p/a", "p/keyed", "p/b"}; !slices.Equal(got, want) {
t.Fatalf("CodexUsed %v, want %v", got, want)
}
if got := CodexUsedBy("k1"); !slices.Equal(got, []string{"p/keyed"}) {
t.Errorf("CodexUsedBy(k1) %v", got)
}
if got := CodexUsedBy(""); !slices.Equal(got, []string{"p/a", "p/b"}) {
t.Errorf("CodexUsedBy(\"\") %v", got)
}
Append(Record{Time: now.Add(-time.Minute), Agent: "codex", Provider: "p", Requested: "p/new", Status: 200})
if got := CodexUsed(); !slices.Contains(got, "p/new") {
t.Errorf("a model used for the first time is not in %v", got)
}
}