Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

agent: a Codex sign-in OpenAI refused to refresh for good counts as none (#4308)

yetonefb2e11e288fa
diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md
index 1d31b8de7..bc578cfe1 100644
--- a/docs/subsystems/agent-wiring.md
+++ b/docs/subsystems/agent-wiring.md
@@ -122,7 +122,7 @@ something else shows up as drift.
- `proxied` (Muma102kMichael on X: every project and every new chat 502 after magpie took Codex over) is this machine's Codex sending its requests for the gateway through a proxy that doesn't pass them on, while the gateway answers here ([`codex_proxy.go`](../../internal/agent/codex_proxy.go)). Codex's reqwest takes `HTTP_PROXY`, else the system's proxy for `http://` (macOS' HTTP entry alone, never its HTTPS or SOCKS one; Windows' `ProxyServer` only when it is one address for every protocol), else `ALL_PROXY`, and lets only an exact IP or a CIDR in `NO_PROXY` (on Windows, with no `NO_PROXY`, the bypass list with `*.` taken out) keep 127.0.0.1 off it: `127.*` and `localhost` don't, and macOS' exceptions aren't read. With `[features] respect_system_proxy` it asks the system alone, matching its list itself, wildcards and `<local>` included, and goes direct when no proxy is set there; a macOS SOCKS entry alone it can't use, and takes `HTTP_PROXY`/`ALL_PROXY` then (codex-rs `outbound_proxy`; tried with codex-cli 0.162.1). Its reqwest has no SOCKS, so a `socks5://` proxy is spoken to as an HTTP one, and the probe does the same. The environment is Codex's: the login shell's `*_PROXY` (`proc.LoginEnv`, which a terminal's Codex and the ChatGPT app's both have; only exported ones, as the probe asks a child `/bin/sh` for them, so fish's unexported universal `http_proxy` doesn't count, #2627) over magpie's own, which a magpie opened from the Dock lacks (liu_dyj on X: a `NO_PROXY=127.0.0.1` in the profile, or a macOS proxy with no HTTP entry, warned while Codex went direct). `codexProxyFor` works out that proxy the same way, and a `GET /` through it must come back as magpie's (`proxyProbe`), tried as `reachable` is. Its pill says how to fix it, as `unreachable`'s does, naming the proxy and the variable it was read from, `http_proxy` or `HTTP_PROXY` as it is (`Addr`, `Detail`), through `netproxy.Redact`, without its user and password (#1886), while the probe goes through it whole, and the dialog has Disconnect from magpie. Account failover, which magpie writes uninvited, waits for that probe (`codexProxyOK`) and isn't written while the proxy doesn't pass Codex on, and one it wrote goes again. Tests stub the env, the system proxy and `proxyProbe` in `TestMain`.
- Listing never starts a stopped WSL distro. A stopped distro's agent shows what was last seen there (`asleep`, `wsl.json`); setting one of its fields writes its files, which starts the distro. The Library keeps what it last saw of each agent's places there in the same memory (`WSLKeep`, `WSLKept`; see [library](library.md)), and lists a stopped distro's agents from it.
- Codex's sign-in (`login`) is ChatGPT (`""`, the default), Always ChatGPT (`chatgpt`) or magpie API (`api`). With ChatGPT, magpie's models join Codex's own beside the sign-in (`openai_base_url`), and while the Codex app holds the ChatGPT account Codex is signed in to (`codexUsedUp`, `provider.CodexUsedUp`: OpenAI no longer allows the account and it has no credits left, a workspace still within its overage excepted), magpie is made Codex's provider (`codex.out`): the Codex app sends nothing at all on an account it holds, a magpie model's turn included (#540), and as a provider of its own magpie is past that. A window at 100% with credits left doesn't count, and Codex stays signed in. Sync puts it back beside the sign-in once the account has room. Always ChatGPT never does that: Codex stays in its ChatGPT state, and the Codex app may send nothing till the account has room, while Codex CLI's turns still reach magpie by the base URL, and its routing and other accounts serve them. Codex's app-server answers `account/read` and `getAuthStatus` with no account and no token for a provider without `requires_openai_auth`, and the Codex app then switches to an identity of no account: its ChatGPT mode's chats and projects ("Work") are gone, with its Chrome control (#1824) and cloud tasks, while magpie's models still answer (Qyburn3 on X). So `[model_providers.magpie]` says `requires_openai_auth = true` while Codex has a ChatGPT sign-in with room (`openaiAuth`: `codexChatGPTKnown` and not `codexUsedUp`), magpie API included; Codex still sends magpie's key (`experimental_bearer_token`) and no ChatGPT-Account-ID, with the same tools (tried with ChatGPT.app 26.1021 and its codex 0.164.0-alpha.11 against a capturing server). Not for an API key alone (Codex drops its image tool for a provider requiring OpenAI auth, `uses_openai_actor_authorization`), no sign-in (the app would ask for one), or a sign-in the Codex app holds: the app would read its rate limit again and grey Send for magpie's models too, which `codex.out` exists to get past. Sync writes the key in or takes it out as the sign-in or the allowance changes, an older magpie's table included (`authStale`), and the key is part of the app-server's route (`codexAppRoute`), so the Codex app is restarted onto its account (`TestCodexMagpieProviderKeepsChatGPTAccount`, `TestCodexAppRouteFollowsOpenAIAuth`). While magpie is Codex's provider for `codex.out` the app has no account till the reset, so the Agents row opened over that takeover names browser use among what is off and offers Keep on ChatGPT right there (`keepChatGPT`, posting `login` = `chatgpt`); picked on one of Codex's own models taken over (`codex/<model>`), `set` puts Codex back on its sign-in at once, `model_provider` and `model_catalog_json` gone, where it had left magpie the provider (eddie.zhan on Discord, `TestCodexOwnModelOutThenKeptOnChatGPT`). While magpie is Codex's provider (magpie API, or `codex.out`), `Joined` reports false although the `codex.joined` mark stays for Sync's way back: the Agents row said Codex's own models stay in `/model` beside magpie's, the ChatGPT state the Codex app blocks sending in, and the app only leaves that state once it is restarted (#2267, `TestCodexProviderIsNotJoined`). A Codex that reaches the gateway from another machine (a WSL distro under NAT, or `MAGPIE_ADDR` pinned beyond loopback, `onAnotherMachine(at.gw())`) is wired as one with no ChatGPT sign-in (`chatGPT()` is false there): never joined or beside, since beside its sign-in it would send its ChatGPT token to `/backend-api/codex`, and from there the gateway takes a named key alone, so every request was a 401 (#2145, `TestCodexBeyondLoopbackNotJoined`). magpie is its provider and it sends `gwKey()`, the LAN sharing key; one of its own models picked goes straight to OpenAI on its sign-in.
-- Whether Codex is signed in to ChatGPT is read from its auth.json or keychain item (`codexChatGPTKnown`, `provider.CodexSignInKnown`). Codex rewrites auth.json in place when it refreshes its tokens (codex-rs `FileAuthStorage::save` opens it with truncate), so a read in that moment finds it empty or cut off, and a keychain read can fail for a moment. Such a read is unknown, and Codex is taken to be as magpie last wired it (joined, beside, or `codex.out`); only a sign-in that isn't there (no auth.json, no item) or holds no ChatGPT token is "not signed in". Taking an unknown read for "not signed in" made Sync or a pick move Codex from beside its sign-in to magpie as its provider, with no `codex.out` to bring it back, and the Codex app showed "magpie" for its account and magpie's catalog for its models, on and off (lucy on Discord, v0.1.1394; `TestCodexHalfWrittenAuthKeepsSignIn`, `TestCodexHalfWrittenAuthKeepsOut`, `TestCodexSignInKnown`). A top-level `forced_login_method = "api"` in Codex's config.toml (another tool or guide writes it; magpie never does) counts as no ChatGPT sign-in: Codex logs such a sign-in out as the TUI starts and its app-server reads no account (codex-rs `enforce_login_restrictions`, `load_auth`), so wired beside it the Codex app asked for a sign-in and offered an API key alone (Tiger on Discord). magpie is then the provider without `requires_openai_auth`, and Codex needs no sign-in. The line is the user's and stays; taking it out brings the ChatGPT account back to the Codex app (`requires_openai_auth`) (`TestCodexForcedAPILoginIsNoChatGPT`).
+- Whether Codex is signed in to ChatGPT is read from its auth.json or keychain item (`codexChatGPTKnown`, `provider.CodexSignInKnown`). Codex rewrites auth.json in place when it refreshes its tokens (codex-rs `FileAuthStorage::save` opens it with truncate), so a read in that moment finds it empty or cut off, and a keychain read can fail for a moment. Such a read is unknown, and Codex is taken to be as magpie last wired it (joined, beside, or `codex.out`); only a sign-in that isn't there (no auth.json, no item) or holds no ChatGPT token is "not signed in". Taking an unknown read for "not signed in" made Sync or a pick move Codex from beside its sign-in to magpie as its provider, with no `codex.out` to bring it back, and the Codex app showed "magpie" for its account and magpie's catalog for its models, on and off (lucy on Discord, v0.1.1394; `TestCodexHalfWrittenAuthKeepsSignIn`, `TestCodexHalfWrittenAuthKeepsOut`, `TestCodexSignInKnown`). A top-level `forced_login_method = "api"` in Codex's config.toml (another tool or guide writes it; magpie never does) counts as no ChatGPT sign-in: Codex logs such a sign-in out as the TUI starts and its app-server reads no account (codex-rs `enforce_login_restrictions`, `load_auth`), so wired beside it the Codex app asked for a sign-in and offered an API key alone (Tiger on Discord). magpie is then the provider without `requires_openai_auth`, and Codex needs no sign-in. The line is the user's and stays; taking it out brings the ChatGPT account back to the Codex app (`requires_openai_auth`) (`TestCodexForcedAPILoginIsNoChatGPT`). So does a sign-in OpenAI refused to refresh for good (jjt on Discord, #4308): Codex keeps its auth.json as it was and the refusal in memory alone, and wired beside it every turn, magpie's models too, failed at Codex's workspace routing discovery (`/wham/accounts/check`, 401). magpie refreshes Codex's sign-in itself (`codexTokenRenewing`, on its usage read), and a refusal Codex takes as permanent (codex-rs `request_chatgpt_token_refresh`: `refresh_token_expired`, `_reused` or `_invalidated`, a 400 `invalid_grant`, or a 401 carrying an error code; `codexRefreshGone`) is kept for that Codex home as the refused refresh token's hash (`codex-signin-gone.json`, only while the store still holds that token). `codexChatGPTKnown` reads that sign-in as none (`provider.CodexSignInGone`), and magpie is the provider without `requires_openai_auth`; the user's auth.json stays. A 5xx, a network error, a 401 with no code, or a token Codex replaced meanwhile marks nothing, and a record that can't be read is unknown. Signed in again, the refresh token is another one and counts at once; `KeepOnAnAccountWithRoom`'s loop (`noteCodexGoneNow`) has Sync write `requires_openai_auth` back within minutes (`TestCodexGoneSignInIsNoChatGPT`, `TestCodexSignInGoneWhenRefusedForGood`). In left-alone mode magpie doesn't refresh Codex's sign-in, so nothing is recorded there.
- Codex signed in to ChatGPT and not connected still gets `openai_base_url` while more of its ChatGPT accounts are on in magpie (`failover`, stash `codex.failover`; with a CC Switch mirror as its provider, the mirror's `base_url` and `codex.mirror_failover`), so a turn its own account has no allowance for goes on to the next — never for a Codex that reaches the gateway from another machine, where that path takes a named key alone and the failed-over turn came back a 401 (#2145, `TestCodexFailoverBeyondLoopbackNotWritten`); Sync takes one it wrote before off again. That Codex is `FailingOver`: not `Wired`, yet its requests go through magpie. The Agents row says so and what turns it off (`FailoverSaid` in the CLI and TUI, `failover` in `/api/state`, a button to Providers › Codex in the GUI), and the gateway hands it only its own models from `/backend-api/codex/models` (`CodexOwnOnly`, set on `gateway.CodexOwnOnly` by main; its ETag carries `provider.CodexOwnListTag`, and Sync ages a `models_cache.json` without it). A connected WSL Codex shares the gateway path, so while one is connected every Codex gets magpie's models (#1385). A base URL the user wrote by hand (#856) isn't failover's and stays a plain not-connected row. A Codex the user disconnected gets no failover till they connect it again. The app serving the gateway takes failover out as it quits (`EndFailover`, `EndCodexFailover` in `OnShutdown`), so a Codex left on its own goes straight to OpenAI rather than retrying a gateway that's gone (Leon920717 on X); the next start's Sync writes it again. Codex's app-server read the old base URL as it started and keeps it, and `KeepCodexDaemonCurrent` ends with magpie, so `EndCodexFailover` restarts it there when no codex session is on it (`codexDaemonCheck`); with one on it, it stays on the gone gateway until the user restarts it (thisisvainxx on X: "several restarts"). So does every thread already open, in the Codex app above all: each turn went to the closed port, and a system proxy in between answered `503 Service Unavailable: Unknown error, url: http://127.0.0.1:3425/backend-api/codex/responses` until magpie was opened again (idkatbro on X; without a proxy Codex says "Connection refused" and reconnects). Masking plays no part: it runs only in the gateway, and its own 503 says why. When `EndCodexFailover` took failover out, the gateway's address is this computer's and Codex runs (`provider.CodexPIDs`: the app's app-server, the daemon, the CLI's sessions, an editor's codex), `OnShutdown` starts `magpie codex-relay` apart from itself (`agent.RelayOpenCodex`, `codexrelay.Start`; Setsid, or a detached process on Windows). It waits up to 60 s for the gateway to let go of the port, then serves only `/backend-api/codex/*` from Codex on this computer (a loopback Host, no Origin) and passes each request to the ChatGPT backend as the gateway passes a turn of Codex's own: the same item-id, compaction and Claude-seal rewrites, zstd and proxy, signed by the caller's own Authorization (`gateway.CodexRelay`). A magpie model, a turn on one of magpie's accounts, and a turn masking would have masked are refused with a readable 400 instead (Codex retries a 5xx five times over and shows a 4xx at once); anything else says magpie isn't running, never as magpie (`ServedBy`). It ends once every Codex process it was left for has and nothing is in flight, and the next magpie to serve the gateway asks it to leave (`codexrelay.Leave` in `ListenAndServe` and `serveGatewayLocked`; a POST with the token from `codex-relay.json`, 0600 in magpie's folder): it closes its listener at once and finishes the turns it streams, up to 30 minutes. `TestCodexRelay*`, `TestLeaveFreesPortAndFinishesStream`, `TestEndsWhenCodexGone`, `TestLeaveNeedsToken`. Not covered: a connected Codex (magpie's models, left pointed at magpie by design), a Codex in a WSL distro, and a magpie that ends without quitting. Every restart magpie makes runs `codex app-server daemon restart` as its own responsible process on macOS (`proc.CombinedOutputOwn`, a disclaimed `posix_spawn`): the daemon works in the folder of each codex attached to it, and as magpie's child the first codex in ~/Desktop had macOS ask for the Desktop folder in magpie's name (reformed9448 on Discord; `TestRestartedCodexDaemonIsNotMagpies`). A magpie built without cgo (the CLI) still restarts it as its own child. A connected Codex keeps magpie as it quits: its model is one of magpie's, and Disconnect is what puts its own config back. A magpie that ends without quitting (killed, a crash, `magpie serve`) leaves failover in place.
- A Codex thread keeps the provider it was started on. While a magpie model is on, CC Switch's provider tables go through magpie too (`takeTables`), and get their own base URL back when Codex steps off magpie (`giveTables`, stash `codex.tables`). CC Switch's OpenAI mirror (a table of another id, `name = "OpenAI"`, `requires_openai_auth`, no `base_url`) isn't reached by `openai_base_url`, which takes only the built-in provider to magpie: a thread started on it and reopened went straight to ChatGPT, which refused the magpie model picked in it, and magpie never saw the request (#1796). While Codex goes to magpie by the base URL, `takeMirrors` gives each such mirror magpie's ChatGPT backend as its `base_url` (stash `codex.mirrors`), and `giveMirrors` takes it off again; a mirror a profile names, or one whose `base_url` was changed since, stays as it is. Moving the threads onto another provider is the Sessions page's Move (#887).
- An agent's config is written in the form the installed version reads, taken from what that version declares, not the latest one. VSCodium has no Copilot Chat of its own, and the Marketplace's last one (0.48.1) has no Custom Endpoint provider, only OpenAI Compatible (`customoai`, #1014). magpie reads the provider list in the package.json of the `github.copilot-chat` installed under `~/.vscode-oss/extensions` (`vscodeChatOf`). It writes its group for `customendpoint` when that is declared, for `customoai` when only that is, with each model's window as `maxInputTokens` + `maxOutputTokens` and the token as `x-api-key` (that version drops an Authorization in `requestHeaders`). A missing or unreadable extension keeps `customendpoint`. Sync rewrites a group written for the other provider, and Check reports it, as it does a Copilot Chat declaring neither. That Copilot Chat registers its providers only while signed in to GitHub with a personal Copilot plan, which the Notice says.
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index f2bcfd5ce..0f2c38406 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -234,7 +234,7 @@ recorded as a prediction failure (`noPrediction`).
- A relayed Chat stream's tool call fragment with no `index` (none, or null) is given its call's (`streamCallIndexes` in [`chattidy.go`](../../internal/gateway/chattidy.go), #3386). Gemini's OpenAI-compatible API sends its calls with none, where the OpenAI contract has one on every fragment: the AI SDK's OpenAI provider (Cursor's Private Inference) turned the chunk away ("expected number, received undefined" at `tool_calls[0].index`), and OpenAI's node SDK's stream helper kept no call at all. A fragment with an id other than the open call's, or after the first in its delta, is the next call; any other goes on with the open one. So two calls in chunks of their own are 0 and 1, never both 0, which that helper merges into one call. A fragment with an index keeps it. `TestChatTidyGivesCallsAnIndex` in [`chattidy_test.go`](../../internal/gateway/chattidy_test.go), `TestGeminiCompatRelayedCallsHaveAnIndex` in [`gemini_compat_cut_test.go`](../../internal/gateway/gemini_compat_cut_test.go).
- A translated Chat stream's tool calls are read by `index`, not as one after another (`chatDecoder.toolCall` in [`chat.go`](../../internal/gateway/chat.go)). An upstream may send several calls' fragments by turns (index 0, 1, 0, 1…), the first of each with its id and name; read as a new call at each change of index, two calls reached Codex as six `function_call` items, the first two with `{}` and four with no name (mysterious_dove on Discord). The open call streams as it comes; a call that starts while the open one's arguments aren't yet whole JSON is held by its index and given whole, in index order, at `finish_reason` or `[DONE]`. Every translation from Chat (Responses, Messages, Gemini, a whole reply) reads the stream through it. `TestInterleavedToolCallsToResponses`, `TestInterleavedToolCallsToAnthropic`, `TestInterleavedToolCallsWhole`, `TestToolCallsNamedFirstThenArgued` in [`translate_stream_test.go`](../../internal/gateway/translate_stream_test.go).
- A relayed Chat reply whose choice made calls and ended `stop` ends `tool_calls`, streamed (`chatTidy`) and whole (`chatWhole`), as the translations' collector already ends it (`collector.finish`): told it stopped, a client that reads `finish_reason` ends its turn with the calls not run (喜欢一个人 on Discord, a relay in front of deepseek-v4.1-flash). Any other reason stays as it came. `TestChatCallsEndToolCalls` in [`chattidy_finish_test.go`](../../internal/gateway/chattidy_finish_test.go).
-- Codex's model list from the backend path (`codexModels`) is the ChatGPT backend's for its sign-in and then magpie's (`provider.CodexListed`), its ETag and every reply's `X-Models-Etag` tagged with `provider.CodexListTag` so a change has Codex ask again. A Codex there only for account failover, not connected (`CodexOwnOnly`, `agent.CodexOwnOnly`; #1385), gets the backend's alone, tagged with `provider.CodexOwnListTag`; neither tag contains the other, so connecting or disconnecting has Codex fetch the list again. The user's picks on the codex provider (`provider.CodexNativePicked`) narrow the backend's listed models. They keep its hidden ones (`visibility: "hide"`), which no picker shows. One of those is `codex-auto-review`: when the list lacks it and no reviewer is set, Codex's auto-review runs on the conversation's model at low effort (#1460, `TestCodexModelListPicksKeepHidden`). The account's own models stay in it when they are kept out of other agents' lists for routing groups, by the codex provider "Only through routing groups" (`Provider.Unlisted`) or a group that unlists its members (`Group.Unlists`): Codex's own catalog (`catalogOf("codex")`, behind `ListedFor`/`CatalogFor`) keeps `CodexOwn` entries, so the Agents page shows its Codex section, its picker offers them as magpie's, and the desk file and `spawnChoices` list them. #1602 dropped them here, and the picker then still offered them through the own-models switch while the Codex app had none (士奇 on Discord, 10-28). They are hidden from Codex through Codex's list (`provider.CodexNativeHidden`, `TestCodexModelListKeepsOwnForGroups`, `TestCodexOwnModelsListedOnlyThroughGroups`).
+- Codex's model list from the backend path (`codexModels`) is the ChatGPT backend's for its sign-in and then magpie's (`provider.CodexListed`), its ETag and every reply's `X-Models-Etag` tagged with `provider.CodexListTag` so a change has Codex ask again. A Codex there only for account failover, not connected (`CodexOwnOnly`, `agent.CodexOwnOnly`; #1385), gets the backend's alone, tagged with `provider.CodexOwnListTag`; neither tag contains the other, so connecting or disconnecting has Codex fetch the list again. The user's picks on the codex provider (`provider.CodexNativePicked`) narrow the backend's listed models. They keep its hidden ones (`visibility: "hide"`), which no picker shows. One of those is `codex-auto-review`: when the list lacks it and no reviewer is set, Codex's auto-review runs on the conversation's model at low effort (#1460, `TestCodexModelListPicksKeepHidden`). The account's own models stay in it when they are kept out of other agents' lists for routing groups, by the codex provider "Only through routing groups" (`Provider.Unlisted`) or a group that unlists its members (`Group.Unlists`): Codex's own catalog (`catalogOf("codex")`, behind `ListedFor`/`CatalogFor`) keeps `CodexOwn` entries, so the Agents page shows its Codex section, its picker offers them as magpie's, and the desk file and `spawnChoices` list them. #1602 dropped them here, and the picker then still offered them through the own-models switch while the Codex app had none (士奇 on Discord, 10-28). They are hidden from Codex through Codex's list (`provider.CodexNativeHidden`, `TestCodexModelListKeepsOwnForGroups`, `TestCodexOwnModelsListedOnlyThroughGroups`). A backend answer that says the sign-in itself is revoked (`provider.SignInRevoked`, a 401 `token_revoked`; jjt on Discord, #4308) isn't passed through: the list is Codex's cached one (`codexcat.CacheEntries`) and magpie's, so the picker keeps magpie's models; any other refusal, a 401 `token_expired` among them, still is. The desk file does the same while the live list says revoked (`provider.CodexListRevoked`, `codexDesk.revoked`) (`TestCodexModelListRevokedSignIn`, `TestCodexDesktopCatalogRevokedSignIn`).
- The Codex app and its VS Code extension read only the first 100 of that list by priority, hidden entries counted: each asks its app-server for `model/list` with `limit: 100` and never for the next page (ChatGPT.app 26.1013, openai.chatgpt 26.51021; the CLI's `/model` lists every one). So `codexcat.FitOwn` ranks the list last of all: hidden entries after every listed one, and, with more than 100 listed (`codexcat.AppList`), Codex's own listed models (the backend's entries in `codexMerged`; a ChatGPT account's `codex/…` ones, `codexcat.ChatGPTOwn`, on the other two routes) and then the models Codex used in the last 30 days (`usage.CodexUsed`, most recent first, at most 100) brought into the first 100 in the order they stood, the rest kept in theirs. The user's own order (`codexcat.Order`, #855) goes first, so an order that names only magpie's models (set while Codex's list had no Codex section, or before OpenAI added a model) no longer puts every GPT model past the 100th (maiya on Discord); the user takes one out of the app by hiding it in Codex's list. The same holds for `/v1/codex/models` (with the models that key's Codex used alone, `usage.CodexUsedBy`, before the 1 MiB cut) and the `model_catalog_json` magpie writes (`codexcat.CatalogUsed`). `codexListTag` carries a tag of the used set (`codexcat.UsedTag`), so the ETag and `X-Models-Etag` change when Codex uses a model it hadn't; `usage.CodexUsed` is read again a minute on, or at once when `Append` writes a Codex call to a model it doesn't hold (`codexUsedNew`). `TestCodexModelListKeepsUsedModelsInTheAppsHundred`, `TestCodexCatalogKeepsTheKeysUsedModelsFirst`, `TestFitKeepsUsedModelsWhereTheCodexAppReads`, `TestCodexUsed`, `TestCodexModelListKeepsOwnModelsInTheAppsHundred`, `TestFitOwnKeepsCodexsOwnInTheAppsHundred` and `TestCatalogUsedKeepsChatGPTOwn` cover it (@ZhenLuoTTAWDTT on X).
- Reasoning state goes through magpie's translation as the vendor wrote it when the agent and the upstream speak the same API (#1445, XYenon): a non-streaming Messages request to a stream-only account, web search on the agent's own API, a plugin, failover. The IR carries it sealed (`Part.Sealed`, `SealedBy`; events `KThinkStart`, `KSealed`, `KTextSig` in [`ir.go`](../../internal/gateway/ir.go)), so each thinking block or reasoning item stays apart, whole or streamed, and a seal is written only for its own API (`sealedBy`). Anthropic's `redacted_thinking` goes back raw (to DeepSeek as a thinking block, below), and a thinking block signed with no text keeps its signature (`aBlock` still writes `"thinking": ""`, #1447), on Claude's subscription too. A Responses reasoning item keeps its id, summary and `encrypted_content`, and goes back to a Responses upstream as it came when it has no reasoning text (`sealedReasoning`; text, another vendor's, is still left out for OpenAI, #1411). Gemini's `thoughtSignature` stays on the call it signed and on text; a later call of a signed step goes without one, and only an unsigned step or Claude on Antigravity gets `skip_thought_signature_validator`. Another API's agent gets the call's signature in its id, as on chat (#687), with its length in the mark (`__ts<n>__`), so an id a client shortened carries no signature and the call goes as unsigned (#1536); it gets no one else's seal. The signature itself is kept in magpie's folder, `gemini-signatures/<key>`, named by its hash (`keepSignature` in [`gemini_sigstore.go`](../../internal/gateway/gemini_sigstore.go)), and the id carries only the key (`__tsk16__<key>`), so an id stays under 64 characters however long the step's signature (#2411: LobeHub's PostgreSQL index turned away a 3KB id). A key magpie no longer has, or whose file doesn't hash to it, reads as no signature; one not read for 90 days is removed. Where the folder can't be written, the id carries the signature as before. Across APIs, only Claude's reasoning is carried to a Responses client, below.
- Claude's reasoning goes to a Responses client (Codex) in an envelope and comes back as Claude wrote it (mysterious_dove_96933 on Discord). Anthropic wants a tool loop's next request to have the turn's thinking blocks, signed, and its `redacted_thinking` whole, or answers 400; Codex with `store: false` keeps only a reply's items, and of a reasoning item only `encrypted_content` is opaque. So the Anthropic decoders name a signature as Claude's (`KSig` with `sealAnthropic`, `Part.SignedBy`), and the Responses encoder and `renderResponses` give each thinking block a reasoning item whose `encrypted_content` is `magpie-claude1:` and the block's JSON in base64 (`claudeThinking`, [`internal/claudeseal`](../../internal/claudeseal/claudeseal.go)): its summary with it, an empty summary for a block signed with no text, and a `redacted_thinking` block as an item of its own. `parseResponses` reads such an item back into the block (`claudePart`), in order, so `anthropicBody` sends it as it was; an envelope that can't be read is its summary, as before. A Gemini thought's signature isn't named, and isn't sealed. The envelope goes to no Responses API: `passthrough` and `forwardOnce` take it out (`withoutClaudeSeals`: the item keeps its summary, one with none goes), `codexInputAs` leaves such an item out for Codex's own models as it does one with nothing sealed, and `codexUpstreamOn` takes it out before the ChatGPT backend; OpenAI's own `encrypted_content` is untouched. `redact.MaskJSON` keeps it whole (a reasoning item's seal), so the placeholders Claude was sent stay in what its signature covers. Claude's subscription names its signatures too, so Codex gets the same items, though its tool loop goes on inside Claude Code's own run, which keeps the thinking itself. `TestCodexToolLoopGivesClaudeItsThinkingBack`, `TestClaudeBlocksNeverGoToAResponsesAPI`, `TestGeminiSignatureIsNotSealedAsClaudes`, `TestMaskJSONKeepsClaudeBlocks`.
diff --git a/internal/agent/codex.go b/internal/agent/codex.go
index 537ba6465..f9cca0cf4 100644
--- a/internal/agent/codex.go
+++ b/internal/agent/codex.go
@@ -2468,7 +2468,18 @@ func codexChatGPTKnown(dir string) (signedIn, known bool) {
// valid JSON that isn't an object: not a sign-in Codex wrote
return false, false
}
- return a.Tokens != nil && a.Tokens.Access != "", true
+ if a.Tokens == nil || a.Tokens.Access == "" {
+ return false, true
+ }
+ // a sign-in OpenAI refused to refresh for good (revoked, or failed for
+ // good, #4308): Codex keeps its auth.json, but beside it every turn
+ // fails at Codex's workspace routing discovery, magpie's models too.
+ // None to magpie, as forced api above, until it is signed in again
+ // (another refresh token). A record that can't be read says nothing.
+ if gone, known := provider.CodexSignInGone(dir, b); !known || gone {
+ return false, known
+ }
+ return true, true
}
// codexReached reads Codex's newest model request since a time — the
diff --git a/internal/agent/codex_signin_gone_test.go b/internal/agent/codex_signin_gone_test.go
new file mode 100644
index 000000000..c730cf507
--- /dev/null
+++ b/internal/agent/codex_signin_gone_test.go
@@ -0,0 +1,61 @@
+package agent
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// jjt on Discord (#4308): Codex's ChatGPT sign-in revoked, its auth.json
+// left as it was. Wired beside it, every turn failed at Codex's workspace
+// routing discovery (401), magpie's models too, until the user signed in
+// again. A sign-in OpenAI refused to refresh for good is none to magpie:
+// magpie is Codex's provider, without requires_openai_auth, and Codex needs
+// no sign-in for magpie's models. Signed in again (another refresh token),
+// the ChatGPT account is back with nothing for the user to do. A record
+// magpie can't read says nothing either way.
+func TestCodexGoneSignInIsNoChatGPT(t *testing.T) {
+ home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x","refresh_token":"rt-dead"}}`, "")
+ dir := filepath.Join(home, ".codex")
+ if !codexChatGPT(dir) {
+ t.Fatal("a sign-in nothing was refused for is one")
+ }
+ p := provider.NoteCodexSignInGoneForTest(dir, "rt-dead")
+ cx := codex(home)
+ if err := cx.Fields[0].Set("fake/m1"); err != nil {
+ t.Fatal(err)
+ }
+ cfg := read()
+ if !strings.Contains(cfg, `model_provider = "magpie"`) {
+ t.Fatalf("revoked sign-in: magpie its provider, not beside it:\n%s", cfg)
+ }
+ if strings.Contains(cfg, "requires_openai_auth") {
+ t.Fatalf("revoked sign-in: no requires_openai_auth, or Codex asks the dead sign-in first:\n%s", cfg)
+ }
+ if c := cx.Check(); c != "" {
+ t.Fatal(c)
+ }
+ if _, err := os.Stat(filepath.Join(dir, "auth.json")); err != nil {
+ t.Fatalf("the user's auth.json kept: %v", err)
+ }
+
+ // a record that doesn't parse: unknown, not signed out
+ saved, _ := os.ReadFile(p)
+ os.WriteFile(p, []byte("{cut off"), 0o600)
+ if _, known := codexChatGPTKnown(dir); known {
+ t.Fatal("an unreadable record taken as known")
+ }
+ os.WriteFile(p, saved, 0o600)
+
+ // signed in again: the ChatGPT account back in the app
+ os.WriteFile(filepath.Join(dir, "auth.json"), []byte(`{"tokens":{"access_token":"y","id_token":"y.e30.y","refresh_token":"rt-new"}}`), 0o600)
+ if err := cx.Sync(); err != nil {
+ t.Fatal(err)
+ }
+ if cfg := read(); !strings.Contains(cfg, "requires_openai_auth = true") {
+ t.Fatalf("signed in again: the ChatGPT account back (requires_openai_auth):\n%s", cfg)
+ }
+}
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index e5b5bcd5a..123036d94 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -1971,6 +1971,21 @@ func (s *Server) askCodexList(rctx context.Context, h http.Header, query string,
func (s *Server) codexModels(w http.ResponseWriter, r *http.Request) {
var own []any
body, etag, deny, denyBody := s.codexBackendList(r)
+ // A sign-in the backend calls revoked (401 token_revoked, "Encountered
+ // invalidated oauth token", provider.SignInRevoked) is answered as a
+ // backend that didn't answer: Codex's last list of its own, magpie's
+ // models after it. Passed through, Codex tried one refresh, which
+ // fails for good for such a sign-in (openai/codex login/src/auth/
+ // manager.rs: refresh_token_expired, _reused, _invalidated), and kept
+ // its bundled list, so magpie's models were gone from the picker until
+ // the user signed in again (jjt on Discord, #4308). Nothing is kept of
+ // it: the next ask goes to the backend again, so a sign-in made again
+ // has its list at once, and Codex still learns of the dead sign-in on
+ // a turn of its own models, whose 401 goes to it as it came. Any other
+ // 401 or 403 ("token expired", for a refresh to mend) is Codex's to see.
+ if deny != nil && provider.SignInRevoked(deny.StatusCode, denyBody) {
+ deny, body, etag = nil, nil, ""
+ }
if deny != nil {
// a sign-in to renew is Codex's to see
w.Header().Set("Content-Type", deny.Header.Get("Content-Type"))
diff --git a/internal/gateway/codex_desktop.go b/internal/gateway/codex_desktop.go
index 160408139..ed5fa9f13 100644
--- a/internal/gateway/codex_desktop.go
+++ b/internal/gateway/codex_desktop.go
@@ -8,6 +8,7 @@ import (
"sync"
"time"
+ "github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/provider"
)
@@ -41,7 +42,10 @@ var codexDesk struct {
body []byte
got, tried time.Time
asking bool
- done chan struct{} // closed as the ask under way ends
+ // revoked: the last ask had the backend call the sign-in revoked
+ // (provider.CodexListRevoked)
+ revoked bool
+ done chan struct{} // closed as the ask under way ends
}
// CodexDesktopCatalog is the list a Codex beside its ChatGPT sign-in is
@@ -52,9 +56,15 @@ var codexDesk struct {
// gave. That is unknown, never an empty list, and the file as it was
// stands. The backend is asked again in the background when its list is
// old, CodexDesktopChanged told when it changed.
+//
+// A sign-in the backend calls revoked, with no list of its from before,
+// is handed Codex's last list of its own and magpie's models, as
+// codexModels answers Codex then (#4308): the backend has said all it
+// will for that sign-in, and without it the app's picker had none of
+// magpie's models until the user signed in again.
func CodexDesktopCatalog() (b []byte, ok bool) {
codexDesk.Lock()
- body := codexDesk.body
+ body, revoked := codexDesk.body, codexDesk.revoked
stale := body == nil || time.Since(codexDesk.got) >= codexDeskFor
if stale && !codexDesk.asking && time.Since(codexDesk.tried) >= codexDeskRetry {
codexDesk.asking, codexDesk.tried = true, time.Now()
@@ -62,6 +72,14 @@ func CodexDesktopCatalog() (b []byte, ok bool) {
go codexDeskRefresh(codexDesk.done)
}
codexDesk.Unlock()
+ if body == nil && revoked {
+ var own []any
+ for _, e := range codexcat.CacheEntries() {
+ own = append(own, e)
+ }
+ b, err := json.MarshalIndent(map[string]any{"models": codexMerged(own, true)}, "", " ")
+ return b, err == nil
+ }
if body == nil {
return nil, false
}
@@ -86,8 +104,13 @@ func codexDeskRefresh(done chan struct{}) {
codexDesk.Lock()
changed := false
if err == nil {
- changed = !bytes.Equal(b, codexDesk.body)
- codexDesk.body, codexDesk.got = b, time.Now()
+ changed = !bytes.Equal(b, codexDesk.body) || codexDesk.revoked
+ codexDesk.body, codexDesk.got, codexDesk.revoked = b, time.Now(), false
+ } else if revoked := provider.CodexListRevoked(err); revoked != codexDesk.revoked {
+ // told only where it changes what is handed out: with no list
+ // from before (CodexDesktopCatalog)
+ changed = codexDesk.body == nil
+ codexDesk.revoked = revoked
}
codexDesk.asking = false
codexDesk.Unlock()
diff --git a/internal/gateway/codex_desktop_test.go b/internal/gateway/codex_desktop_test.go
index 990f0a9fd..5189c5e96 100644
--- a/internal/gateway/codex_desktop_test.go
+++ b/internal/gateway/codex_desktop_test.go
@@ -24,7 +24,7 @@ func codexDeskReset(t *testing.T) {
<-done
}
codexDesk.Lock()
- codexDesk.body, codexDesk.got, codexDesk.tried, codexDesk.asking, codexDesk.done = nil, time.Time{}, time.Time{}, false, nil
+ codexDesk.body, codexDesk.got, codexDesk.tried, codexDesk.asking, codexDesk.done, codexDesk.revoked = nil, time.Time{}, time.Time{}, false, nil, false
codexDesk.Unlock()
}
reset()
diff --git a/internal/gateway/codex_revoked_list_test.go b/internal/gateway/codex_revoked_list_test.go
new file mode 100644
index 000000000..6fabb1891
--- /dev/null
+++ b/internal/gateway/codex_revoked_list_test.go
@@ -0,0 +1,151 @@
+package gateway
+
+import (
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// revokedBody is what the ChatGPT backend answered a request on a revoked
+// sign-in with (ysicing, #1793).
+const revokedBody = `{"error":{"message":"Encountered invalidated oauth token for user, failing request","type":null,"code":"token_revoked","param":null},"status":401}`
+
+// A Codex whose ChatGPT sign-in was revoked asks for its model list beside
+// it (jjt on Discord, #4308). The backend's 401 went to Codex, whose one
+// refresh fails for good for such a sign-in, and Codex kept its bundled
+// list: none of magpie's models until the user signed in again. Now the
+// list is Codex's last of its own and magpie's. A 401 that says the token
+// expired is still Codex's to refresh, and a sign-in made again has the
+// backend's list on the next ask: nothing of the revoked one is kept.
+func TestCodexModelListRevokedSignIn(t *testing.T) {
+ setup(t, provider.Chat, &fake{t: t})
+ forgetCodexLists(t)
+ var say atomic.Value
+ say.Store("revoked")
+ chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ switch say.Load() {
+ case "revoked":
+ w.WriteHeader(401)
+ io.WriteString(w, revokedBody)
+ case "expired":
+ w.WriteHeader(401)
+ io.WriteString(w, `{"error":{"message":"Provided authentication token is expired. Please try refreshing it.","code":"token_expired"},"status":401}`)
+ default:
+ io.WriteString(w, `{"models":[{"slug":"gpt-new","priority":1}]}`)
+ }
+ })
+ home, _ := os.UserHomeDir()
+ os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
+ os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
+ []byte(`{"etag":"\"v1\"","models":[{"slug":"gpt-5.5","priority":1,"base_instructions":"x"}]}`), 0o644)
+ was := codexListFor
+ codexListFor = time.Nanosecond
+ t.Cleanup(func() { codexListFor = was })
+ h := New().Handler()
+
+ if code, has := codexListAsk(h); code != 200 || !has["fake/m1"] || !has["gpt-5.5"] {
+ t.Fatalf("revoked sign-in: %d %v, want 200 with magpie's models and Codex's last list", code, has)
+ }
+ codexLists.asking.Wait()
+
+ say.Store("expired")
+ if code, _ := codexListAsk(h); code != 401 {
+ t.Fatalf("expired token: %d, want the backend's 401 for Codex to refresh", code)
+ }
+ codexLists.asking.Wait()
+
+ say.Store("ok")
+ if code, has := codexListAsk(h); code != 200 || !has["gpt-new"] || !has["fake/m1"] {
+ t.Fatalf("signed in again: %d %v, want the backend's list with magpie's", code, has)
+ }
+}
+
+// The Codex app's file list (CodexDesktopCatalog) for a sign-in the
+// backend calls revoked, with no list of the backend's from before: Codex's
+// last list of its own and magpie's models, told as a change for the file
+// to be written. A network error after it says nothing either way, and a
+// sign-in made again brings the backend's list back.
+func TestCodexDesktopCatalogRevokedSignIn(t *testing.T) {
+ codexSignedIn(t)
+ codexDeskReset(t)
+ if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Anthropic: "http://127.0.0.1:1",
+ Models: []string{"claude-opus-5.5"}}); err != nil {
+ t.Fatal(err)
+ }
+ home, _ := os.UserHomeDir()
+ os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
+ []byte(`{"etag":"\"v1\"","models":[{"slug":"gpt-5.5","priority":1,"base_instructions":"x"}]}`), 0o644)
+ var say atomic.Value
+ say.Store("revoked")
+ up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch say.Load() {
+ case "revoked":
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(401)
+ io.WriteString(w, revokedBody)
+ case "down":
+ http.Error(w, "down", http.StatusBadGateway)
+ default:
+ io.WriteString(w, `{"models":[{"slug":"gpt-new","priority":1,"visibility":"list"}]}`)
+ }
+ }))
+ defer up.Close()
+ was := provider.CodexBase
+ provider.CodexBase = up.URL + "/backend-api/codex"
+ defer func() { provider.CodexBase = was }()
+ var changed atomic.Int32
+ CodexDesktopChanged = func() { changed.Add(1) }
+ slugs := func(b []byte) map[string]bool {
+ var got struct {
+ Models []map[string]any `json:"models"`
+ }
+ json.Unmarshal(b, &got)
+ out := map[string]bool{}
+ for _, m := range got.Models {
+ s, _ := m["slug"].(string)
+ out[s] = true
+ }
+ return out
+ }
+ again := func() {
+ codexDesk.Lock()
+ codexDesk.got, codexDesk.tried = time.Time{}, time.Time{}
+ codexDesk.Unlock()
+ CodexDesktopCatalog()
+ codexDeskAsked(t)
+ }
+
+ if _, ok := CodexDesktopCatalog(); ok {
+ t.Fatal("known before the backend answered")
+ }
+ codexDeskAsked(t)
+ if changed.Load() != 1 {
+ t.Fatalf("revoked told %d times, want once", changed.Load())
+ }
+ b, ok := CodexDesktopCatalog()
+ if s := slugs(b); !ok || !s["relay/claude-opus-5.5"] || !s["gpt-5.5"] {
+ t.Fatalf("revoked sign-in: %v %s", ok, b)
+ }
+
+ say.Store("down")
+ again()
+ if _, ok := CodexDesktopCatalog(); ok {
+ t.Fatal("a 502 after the revoked sign-in taken as an answer")
+ }
+
+ say.Store("ok")
+ again()
+ b, ok = CodexDesktopCatalog()
+ if s := slugs(b); !ok || !s["gpt-new"] || !s["relay/claude-opus-5.5"] || s["gpt-5.5"] {
+ t.Fatalf("signed in again: %v %s", ok, b)
+ }
+}
diff --git a/internal/provider/account.go b/internal/provider/account.go
index f26b9a44e..b42d1a358 100644
--- a/internal/provider/account.go
+++ b/internal/provider/account.go
@@ -1129,8 +1129,14 @@ func codexTokenRenewing(ctx context.Context, dir, stale string) (tok, accountID
accountID = codexAccountID(a)
tok, err = codexRefresh(ctx, raw)
if err != nil {
+ // OpenAI refused this sign-in for good: Codex is signed out to
+ // magpie's wiring until it is signed in again (#4308)
+ if errors.As(err, new(codexRefreshGoneError)) {
+ noteCodexSignInGone(dir, a.Tokens.RefreshToken)
+ }
return "", "", err
}
+ forgetCodexSignInGone(dir)
// keep every other field as Codex wrote it. auth.json is the user's
// own sign-in, which Codex CLI and the Codex app read: written beside
// it, flushed and renamed in, never truncated in place, so a machine
@@ -1197,7 +1203,12 @@ func codexRefresh(ctx context.Context, raw map[string]any) (string, error) {
RefreshToken string `json:"refresh_token"`
}
if res.StatusCode != 200 || json.Unmarshal(b, &fresh) != nil || fresh.AccessToken == "" {
- return "", refreshFailed(res.StatusCode, "Codex", "Codex is signed out (token refresh failed); run codex login")
+ err := refreshFailed(res.StatusCode, "Codex", "Codex is signed out (token refresh failed); run codex login")
+ if res.StatusCode != 200 && codexRefreshGone(res.StatusCode, b) {
+ // refused for good, by Codex's own rule (#4308)
+ err = codexRefreshGoneError{err}
+ }
+ return "", err
}
toks["access_token"] = fresh.AccessToken
if fresh.IDToken != "" {
diff --git a/internal/provider/codex_models.go b/internal/provider/codex_models.go
index c02b20e88..14384ae71 100644
--- a/internal/provider/codex_models.go
+++ b/internal/provider/codex_models.go
@@ -67,6 +67,8 @@ func codexModels(ctx context.Context, sign func(context.Context, *http.Request,
// the models a flag of OpenAI's names unless Codex reads its list from a
// file. An error when Codex is not signed in to ChatGPT, the backend
// doesn't answer 200, or its answer lists no model: never an empty list.
+// CodexListRevoked tells one that is the backend calling the sign-in
+// revoked.
func CodexLiveList(ctx context.Context) ([]byte, error) {
home, _ := os.UserHomeDir()
p, ok := codexAccount(home)
@@ -92,7 +94,8 @@ func CodexLiveList(ctx context.Context) ([]byte, error) {
return nil, err
}
if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("ChatGPT models: %s", resp.Status)
+ // what it said, for CodexListRevoked
+ return nil, fmt.Errorf("ChatGPT models: %w", &accountStatusError{status: resp.StatusCode, body: b[:min(len(b), 64<<10)]})
}
var list struct {
Models []json.RawMessage `json:"models"`
@@ -776,3 +779,8 @@ func CodexTiers(m *catalog.Model, e Entry, members func(id string) []Member) {
// codexFastTier is Fast's service tier as Codex sends it.
const codexFastTier = "priority"
+
+// CodexListRevoked says err, from CodexLiveList, is the backend calling
+// Codex's sign-in revoked (SignInRevoked), not a refusal a refresh mends,
+// a 5xx or a network error.
+func CodexListRevoked(err error) bool { return codexRevoked(err) }
diff --git a/internal/provider/codex_signin_gone.go b/internal/provider/codex_signin_gone.go
new file mode 100644
index 000000000..5221c5f1b
--- /dev/null
+++ b/internal/provider/codex_signin_gone.go
@@ -0,0 +1,221 @@
+package provider
+
+// A ChatGPT sign-in OpenAI has refused to refresh for good (jjt on
+// Discord, #4308: revoked, or failed for good). Codex keeps such a
+// sign-in's auth.json as it was and remembers the refusal in memory alone
+// (codex-rs login/src/auth/manager.rs permanent_refresh_failure), so
+// magpie read the file as a sign-in and wired Codex beside it, on its
+// built-in provider. Every turn there, magpie's models too, then failed at
+// Codex's workspace routing discovery (401), and Codex's picker had none of
+// magpie's models. magpie refreshes the sign-in itself (codexTokenRenewing,
+// on a usage read), so it hears OpenAI's refusal too: kept here for the
+// refresh token refused, which the agent package reads as no sign-in
+// (CodexSignInGone). A sign-in made again has another refresh token, which
+// nothing here names: it counts again with no step of the user's.
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "errors"
+ "io/fs"
+ "log"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+ "sync/atomic"
+
+ "github.com/yetone/magpie/internal/catalog"
+ "github.com/yetone/magpie/internal/lastgood"
+)
+
+// codexGoneMu guards the record's file.
+var codexGoneMu sync.Mutex
+
+// codexGonePath is the record: a Codex home's folder to the hash of the
+// refresh token OpenAI refused for good there.
+func codexGonePath() string { return filepath.Join(filepath.Dir(Path()), "codex-signin-gone.json") }
+
+// codexGoneHash names a refresh token in the record without keeping it.
+func codexGoneHash(refresh string) string {
+ h := sha256.Sum256([]byte(refresh))
+ return hex.EncodeToString(h[:])
+}
+
+// readCodexGone reads the record. None yet is empty; a file that can't be
+// read or doesn't parse is an error: what it says is unknown.
+func readCodexGone() (map[string]string, error) {
+ m := map[string]string{}
+ b, err := os.ReadFile(codexGonePath())
+ if errors.Is(err, fs.ErrNotExist) {
+ return m, nil
+ }
+ if err != nil {
+ return nil, err
+ }
+ if err := json.Unmarshal(b, &m); err != nil {
+ return nil, err
+ }
+ return m, nil
+}
+
+// codexRefreshCode is the code a token endpoint's refusal carries, read as
+// Codex reads it (codex-rs login/src/oauth/error.rs TokenErrorDetail::parse):
+// "error" as a string, then error.code, then a top-level code.
+func codexRefreshCode(b []byte) string {
+ var v map[string]json.RawMessage
+ if json.Unmarshal(b, &v) != nil {
+ return ""
+ }
+ text := func(r json.RawMessage) string {
+ var s string
+ if json.Unmarshal(r, &s) == nil && strings.TrimSpace(s) != "" {
+ return s
+ }
+ return ""
+ }
+ if s := text(v["error"]); s != "" {
+ return s
+ }
+ var e map[string]json.RawMessage
+ if json.Unmarshal(v["error"], &e) == nil {
+ if s := text(e["code"]); s != "" {
+ return s
+ }
+ }
+ return text(v["code"])
+}
+
+// codexRefreshGone says a refresh refusal is for good, by Codex's own rule
+// (codex-rs login/src/auth/manager.rs request_chatgpt_token_refresh):
+// refresh_token_expired, refresh_token_reused or refresh_token_invalidated,
+// a 400 invalid_grant, or a 401. A 401 counts only with the endpoint's
+// error code in it: one with no code (a proxy's page) says nothing of the
+// sign-in. A 5xx, a 403, a 429 or any other 400 is a hiccup, as Codex
+// takes it.
+func codexRefreshGone(status int, body []byte) bool {
+ code := strings.ToLower(codexRefreshCode(body))
+ switch {
+ case code == "refresh_token_expired", code == "refresh_token_reused", code == "refresh_token_invalidated":
+ return true
+ case status == http.StatusBadRequest && code == "invalid_grant":
+ return true
+ }
+ return status == http.StatusUnauthorized && code != ""
+}
+
+// codexRefreshGoneError is a refresh OpenAI refused for good
+// (codexRefreshGone); the refusal it wraps is told as before.
+type codexRefreshGoneError struct{ error }
+
+func (e codexRefreshGoneError) Unwrap() error { return e.error }
+
+// noteCodexSignInGone keeps refresh, the refresh token OpenAI just refused
+// for good, as dead for the Codex home dir: only while the store still
+// holds it. One Codex refreshed meanwhile (its own refresh spent the token
+// magpie sent: refresh_token_reused), or a sign-in made since, is another
+// token and isn't marked.
+func noteCodexSignInGone(dir, refresh string) {
+ if refresh == "" {
+ return
+ }
+ b, _, err := readCodexLogin(dir, true)
+ var a codexAuth
+ if err != nil || json.Unmarshal(b, &a) != nil || a.Tokens.RefreshToken != refresh {
+ return
+ }
+ setCodexGone(dir, codexGoneHash(refresh))
+}
+
+// forgetCodexSignInGone takes dir out of the record, its sign-in refreshed.
+func forgetCodexSignInGone(dir string) { setCodexGone(dir, "") }
+
+// setCodexGone records hash for dir ("" forgets it), the agents' files told
+// when it changed.
+func setCodexGone(dir, hash string) {
+ codexGoneMu.Lock()
+ defer codexGoneMu.Unlock()
+ k := filepath.Clean(dir)
+ m, err := readCodexGone()
+ if err != nil {
+ if hash == "" {
+ return
+ }
+ m = map[string]string{}
+ }
+ if m[k] == hash {
+ return
+ }
+ if hash == "" {
+ delete(m, k)
+ } else {
+ m[k] = hash
+ }
+ out, err := json.MarshalIndent(m, "", " ")
+ if err == nil {
+ // a file that doesn't parse is kept aside before it is written over
+ err = lastgood.KeepUnparsed(codexGonePath(), lastgood.Into[map[string]string])
+ }
+ if err == nil {
+ err = writePrivate(codexGonePath(), append(out, '\n'))
+ }
+ if err != nil {
+ log.Printf("codex: keeping that the sign-in in %s is gone: %v", dir, err)
+ return
+ }
+ if hash != "" {
+ log.Printf("codex: OpenAI refused the sign-in in %s for good; magpie takes Codex as signed out until it is signed in again", dir)
+ }
+ if k == filepath.Clean(codexHome()) {
+ codexWasGone.Store(hash != "")
+ }
+ catalog.Touched()
+}
+
+// CodexSignInGone says whether auth, the sign-in read from the Codex home
+// dir, is one OpenAI refused to refresh for good (noteCodexSignInGone).
+// known false: the record couldn't be read, which says nothing either way.
+func CodexSignInGone(dir string, auth []byte) (gone, known bool) {
+ var a codexAuth
+ if json.Unmarshal(auth, &a) != nil || a.Tokens.RefreshToken == "" {
+ return false, true
+ }
+ m, err := readCodexGone()
+ if err != nil {
+ return false, false
+ }
+ return m[filepath.Clean(dir)] == codexGoneHash(a.Tokens.RefreshToken), true
+}
+
+// codexWasGone is whether Codex's own sign-in was gone at the last look.
+var codexWasGone atomic.Bool
+
+// noteCodexGoneNow tells the agents' files when Codex's own sign-in stops
+// being gone: signed in again, in Codex or anywhere else that writes its
+// auth.json, which magpie hears of from no refresh of its own. Run from
+// KeepOnAnAccountWithRoom's loop. A read that says nothing changes nothing.
+func noteCodexGoneNow() {
+ dir := codexHome()
+ b, ok, known := CodexSignInKnown(dir)
+ if !known {
+ return
+ }
+ gone := false
+ if ok {
+ if gone, known = CodexSignInGone(dir, b); !known {
+ return
+ }
+ }
+ if codexWasGone.Swap(gone) != gone {
+ catalog.Touched()
+ }
+}
+
+// NoteCodexSignInGoneForTest records refresh as refused for good in dir,
+// as a refresh OpenAI refused would, and says where the record is.
+func NoteCodexSignInGoneForTest(dir, refresh string) (record string) {
+ setCodexGone(dir, codexGoneHash(refresh))
+ return codexGonePath()
+}
diff --git a/internal/provider/codex_signin_gone_test.go b/internal/provider/codex_signin_gone_test.go
new file mode 100644
index 000000000..68e3e8b4f
--- /dev/null
+++ b/internal/provider/codex_signin_gone_test.go
@@ -0,0 +1,162 @@
+package provider
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+// Codex's rule for a refresh refused for good, as codex-rs
+// login/src/auth/manager.rs has it, and the code read where Codex reads
+// it. A 401 with no code (a proxy's page), a 403, a 5xx, a rate limit and
+// any other 400 say nothing of the sign-in.
+func TestCodexRefreshGoneRule(t *testing.T) {
+ for _, c := range []struct {
+ status int
+ body string
+ want bool
+ }{
+ {401, `{"error":{"message":"Your refresh token has been invalidated.","code":"refresh_token_invalidated"}}`, true},
+ {400, `{"error":{"code":"refresh_token_expired"}}`, true},
+ {400, `{"error":"refresh_token_reused"}`, true},
+ {403, `{"code":"REFRESH_TOKEN_INVALIDATED"}`, true},
+ {400, `{"error":"invalid_grant","error_description":"Refresh token has been revoked"}`, true},
+ {401, `{"error":"unauthorized_client"}`, true},
+ {401, `<html>401 Authorization Required</html>`, false},
+ {401, ``, false},
+ {400, `{"error":"invalid_request"}`, false},
+ {403, `{"error":"invalid_grant"}`, false},
+ {500, `{"error":"server_error"}`, false},
+ {502, `upstream connect error`, false},
+ {429, `{"error":{"code":"rate_limit_exceeded"}}`, false},
+ } {
+ if got := codexRefreshGone(c.status, []byte(c.body)); got != c.want {
+ t.Errorf("%d %s: %v, want %v", c.status, c.body, got, c.want)
+ }
+ }
+}
+
+// Codex's own sign-in, revoked (jjt on Discord, #4308): magpie's usage read
+// is turned away 401 token_revoked, its refresh is refused for good, and
+// the sign-in is gone to magpie (CodexSignInGone), kept across a restart.
+// A refresh that fails otherwise marks nothing, nor does a refusal of a
+// token Codex has replaced meanwhile. A sign-in made again is not gone,
+// with no step of the user's. A record that can't be read says nothing.
+func TestCodexSignInGoneWhenRefusedForGood(t *testing.T) {
+ home := signIn(t)
+ resetRevoked() // marks are read from this HOME's logins.json
+ t.Cleanup(resetRevoked)
+ rememberLogins(true) // this HOME's sign-ins, not an earlier test's
+ loginUsageCache.Lock()
+ loginUsageCache.m = nil
+ loginUsageCache.Unlock()
+ t.Cleanup(func() { codexWasGone.Store(false) })
+ dir := filepath.Join(home, ".codex")
+ auth := func() []byte {
+ b, err := os.ReadFile(filepath.Join(dir, "auth.json"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ return b
+ }
+ gone := func() (bool, bool) { return CodexSignInGone(dir, auth()) }
+
+ var answer atomic.Value // the token endpoint's: "gone", "down", "rotated" or "ok"
+ answer.Store("down")
+ usage := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(401)
+ w.Write([]byte(`{"error":{"message":"Encountered invalidated oauth token for user, failing request","type":null,"code":"token_revoked","param":null},"status":401}`))
+ }))
+ defer usage.Close()
+ refresh := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch answer.Load() {
+ case "down":
+ w.WriteHeader(502)
+ w.Write([]byte(`upstream connect error`))
+ case "rotated":
+ // Codex refreshed first: its new tokens are in auth.json by
+ // the time magpie's spent one is refused
+ writeFile(t, filepath.Join(home, ".codex", "auth.json"), map[string]any{
+ "auth_mode": "chatgpt",
+ "tokens": map[string]any{
+ "id_token": fakeJWT(map[string]any{"email": "[email protected]", "https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "pro", "chatgpt_account_id": "acct-1"}}),
+ "access_token": fakeJWT(map[string]any{"exp": float64(time.Now().Add(time.Hour).Unix()), "n": "codex"}),
+ "refresh_token": "r-codex", "account_id": "acct-1",
+ },
+ })
+ w.WriteHeader(401)
+ w.Write([]byte(`{"error":{"code":"refresh_token_reused"}}`))
+ default:
+ w.WriteHeader(401)
+ w.Write([]byte(`{"error":{"message":"Your refresh token has been invalidated.","code":"refresh_token_invalidated"}}`))
+ }
+ }))
+ defer refresh.Close()
+ oldBase, oldURL := CodexBase, codexTokenURL
+ CodexBase, codexTokenURL = usage.URL+"/backend-api/codex", refresh.URL
+ t.Cleanup(func() { CodexBase, codexTokenURL = oldBase, oldURL })
+ read := func() {
+ t.Helper()
+ loginUsageCache.Lock()
+ loginUsageCache.m = nil
+ loginUsageCache.Unlock()
+ LoginUsage(context.Background(), "codex")
+ }
+
+ if g, known := gone(); g || !known {
+ t.Fatalf("before anything was refused: gone %v known %v", g, known)
+ }
+ read() // the token endpoint down: unknown
+ if g, _ := gone(); g {
+ t.Fatal("a 502 from the token endpoint taken as the sign-in gone")
+ }
+
+ answer.Store("rotated")
+ read()
+ if g, _ := gone(); g {
+ t.Fatal("a token Codex replaced meanwhile marked gone")
+ }
+
+ answer.Store("gone")
+ read()
+ if g, known := gone(); !g || !known {
+ t.Fatalf("refused for good: gone %v known %v", g, known)
+ }
+ noteCodexGoneNow()
+ if !codexWasGone.Load() {
+ t.Fatal("the loop's look doesn't see it gone")
+ }
+ // the record names no token
+ if b, _ := os.ReadFile(codexGonePath()); json.Valid(b) && len(b) > 0 {
+ var m map[string]string
+ json.Unmarshal(b, &m)
+ if m[filepath.Clean(dir)] != codexGoneHash("r-codex") {
+ t.Fatalf("record %s", b)
+ }
+ }
+
+ // unreadable: unknown, never gone
+ p := codexGonePath()
+ saved, _ := os.ReadFile(p)
+ os.WriteFile(p, []byte("{cut off"), 0o600)
+ if g, known := gone(); g || known {
+ t.Fatalf("unreadable record: gone %v known %v, want unknown", g, known)
+ }
+ os.WriteFile(p, saved, 0o600)
+
+ // signed in again: another refresh token, not gone, and the loop says so
+ codexSignIn(t, home, "[email protected]", "r-new")
+ if g, known := gone(); g || !known {
+ t.Fatalf("signed in again: gone %v known %v", g, known)
+ }
+ noteCodexGoneNow()
+ if codexWasGone.Load() {
+ t.Fatal("the loop's look still sees it gone after a sign-in")
+ }
+}
diff --git a/internal/provider/codex_switch.go b/internal/provider/codex_switch.go
index 0305a6a53..9335f4847 100644
--- a/internal/provider/codex_switch.go
+++ b/internal/provider/codex_switch.go
@@ -425,6 +425,8 @@ func KeepOnAnAccountWithRoom(ctx context.Context) {
c, cancel := context.WithTimeout(ctx, time.Minute)
noteCodexUsedUp(CodexUsedUp(c))
cancel()
+ // Codex's own sign-in refused for good, or signed in again since
+ noteCodexGoneNow()
t.Reset(codexLookEvery(knownLoginUsage("codex")))
}
}