Commit Graph
3 Commits
Author SHA1 Message Date
yetone 479de89001 gateway: Codex's model list is asked of the backend once for every app-server, and kept, so it comes within Codex's 5 s (#3734)
#3734: with ~20 Codex app-servers starting together, Codex logged
"failed to refresh available models: request timed out" 1,906 times a
day, fell back to its default metadata for magpie's groups (no
supports_search_tool), and inlined ~1,075 Apps tools: ~500K input tokens
a turn.

Each /backend-api/codex/models asked the ChatGPT backend on its own, and
gave its ask up at codexModelsWait (3 s). Measured with a minimal
gateway (gateway.New().Handler() alone, sandbox HOME, no providers), 20
lists at once with a real ChatGPT sign-in's headers, as app-servers ask:
- 4a33cf0a9: mean 2.24 s, max 3.01 s; the one at 3.01 s was handed an
  empty list (16 bytes), the backend asked 20 times. Again right after:
  mean 1.04 s, max 1.72 s, 20 asks again.
- this change: mean 1.72 s, max 1.74 s, all 20 the full list (568,401
  bytes) from one ask; again right after: max 0.01 s.

Now the backend's list is kept for each sign-in (account id, the
backend, Codex's version): within codexListFor (a minute) it is handed
out again; past it, handed out at once while one ask goes; with none,
the callers wait on the one ask for up to codexModelsWait as before,
and the ask goes on, up to 30 s, for the callers after, where a backend
slower than the wait had never got a list through. A 401 or 403 is
still Codex's to see, and no list from before is handed out in its
place. Each caller reads its own copy, since the windows, picks, V1
stamp and auto-review are written into the entries.

What changes for users: the account's own models (not magpie's, whose
part is built per call) are up to a minute behind the backend.

Not changed: Codex's own 300 s cache and the build of magpie's part
under provider.Hold (#746); no model_catalog_json floor is written.

Tests (codex_models_shared_test.go):
- 20 lists at once, the backend held: each answered with magpie's
  models and Codex's last list, the backend asked once; let go, its
  list reaches the next caller; fresh, five more ask it no more. On
  4a33cf0a9: asked 20 times, then 5 times more.
- a backend always 600 ms against a 200 ms wait gets its list through:
  red on 4a33cf0a9 (none in 10 s), green here.
- stale: three answers at once while the backend holds its second ask.
- a refused sign-in after a list: the 401 is passed on.
TestCodexModelListOnlyPicked and TestCodexNativeModelsTakeSetWindows
change the backend's list between calls, so they forget the kept one
there (forgetCodexLists, which also waits for every ask a test began).

Run at this head (cm3734 worktree, base 834935149):
- go test -race -count=10 on the five list tests (the four new, BackendSlow): ok.
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway/: ok;
  gofmt -l clean.
- go test -timeout 45m ./internal/gateway/: ok (429s), run before
  TestCodexModelListBackendAlwaysSlow was added; that test then run
  alone and in the -race run above: ok.
- Rebased onto 80857a66c (13cca8109 touched codexcat and Codex's
  models since): go test -run 'Codex|Fast' ./internal/gateway/: ok (42s).
2026-10-26 23:41:35 +08:00
yetone 9f7b6bd86c codex: a cached entry keeps the cache's own max when magpie's list has none, and Codex's models_cache.json is read under CODEX_HOME everywhere (follow-up to #674, from reviewing #679) — restoring a cached native entry (backend away) set max_context_window to max(MaxContext, Context) from magpie's live list, so a list saved without the max (before magpie kept it) wrote the window over the cache's own (872000 became 272000); now a list with no max leaves the cache's, and codexcat.Window still raises it to a window past it. codexcat.CacheEntries and catalog.Codex read ~/.codex/models_cache.json even with CODEX_HOME set, while codexVersion and codexInstructions followed it; all four now take catalog.CodexModelsCache() (CODEX_HOME, else ~/.codex), and provider's codexCLIHome and the copy in codexInstructions are gone. TestCodexReadsCacheUnderCodexHome, TestCodexCachedListUnderCodexHomeKeepsItsMax and TestCodexCatalogEntryFromCodexHome fail on the old code (the ~/.codex cache read, none listed from CODEX_HOME); TestCodexNativeModelsTakeSetWindows now wants a cached gpt-5.5 with no live max to keep the cache's 400000 (old code: 272000). go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass but TestProvidersAnswerWhileListsComeIn, a timing test that passes alone. 2026-10-03 21:39:13 +08:00
yetone 64665bb249 codex: a window set on a ChatGPT account's model reaches Codex's own model list (#674)
With Codex signed in to ChatGPT through magpie (openai_base_url at
/backend-api/codex), Codex reads the backend's model entries from magpie's
/backend-api/codex/models, which passed them on as the backend gave them:
a window the user set on the codex provider (magpie model context
codex/gpt-6.1-sol 600k, or "*" for all) was in /v1/models but Codex still
got context_window 272000. The model_catalog_json path did the same: an
own model's entry, taken from Codex's models_cache.json, kept the cache's
window instead of the one magpie resolved for it.

Now each native entry takes the window the user set (Provider.ContextOf,
as /v1/models says it) — context_window, and max_context_window raised to
it where the window is past it, so the entry never says less than its
window — and nothing else in it changes; a model with none set keeps the
backend's. The ETag carries the windows set, so setting or taking one away
has Codex ask again. A list made from Codex's cache (the backend not
answering), which keeps what magpie last handed Codex, takes the account's
own list's window back for a model with none set, so a window taken away
doesn't stay. The model_catalog_json entry takes the window magpie
resolved. base_instructions and the rest of each entry are untouched.

TestCodexNativeModelsTakeSetWindows (set windows on, other models as the
backend says, max raised, ETag changing on reset, the cache restored) and
TestCodexCatalogOwnEntryTakesResolvedWindow fail on the old code and pass.
Against the owner's real ChatGPT account in a sandbox gateway under a temp
HOME (codex provider only, gpt-6-sol 600000 and gpt-5.5 400000 set):
/backend-api/codex/models gave gpt-6-sol 600000/872000, gpt-5.5
400000/400000 (max raised from 272000), the other five 272000/872000 as
the backend lists them, all 52 fields and prompts kept; /v1/models agrees
for the two set; no token rotated. go vet, builds (darwin, GOOS=linux,
GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME
pass. A Codex session's usable window wasn't checked.
2026-10-03 20:45:58 +08:00