Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a used-up Codex sign-in, the one account, is heard on its relayed turns and served without a restart
yetone34b2dd9b7038
diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.mdindex 41fd0811a..3617164ed 100644--- a/docs/subsystems/agent-wiring.md+++ b/docs/subsystems/agent-wiring.md@@ -83,7 +83,7 @@ something else shows up as drift.- JSON/JSONC config edits and their previews reject malformed input rather than editing a partially parsed document. Object edits and VS Code's model-group array edits leave the original file untouched on a parse error; valid comments and trailing commas remain supported (`internal/edit`). An edit's result is checked as the apps read JSONC (`edit.ValidJSONC`: jsonc-parser with `allowTrailingComma`, as OpenCode, VS Code and Zed parse; a trailing comma after a value is fine, one with nothing before it, `{,}` or `},,`, is not), and an edit that would turn a file the app reads into one it can't is refused, nothing written (`checkJSONC`). A delete takes the last member's own trailing comma with it, an insert into an object of comments only adds no comma, and a lone comma an older magpie left (`"mcp": {,}` after the hub was toggled for OpenCode, chenxiang0010 on Discord) is dropped by the next edit of that file (`dropLoneCommas`).- A config that starts with a UTF-8 byte order mark (a Windows `settings.json` or `config.toml` saved by PowerShell, Notepad or a channel switcher, #1534) is read without it (`edit.Read`), and a write keeps it (`keepBOM` in `WriteAtomic`): the user's file keeps the encoding it was saved in. That covers every format `internal/edit` writes (JSON/JSONC and its arrays, TOML, YAML, env); Codex reads a TOML file through a BOM too, so magpie no longer refuses one. A file read some other way trims it with `edit.TrimBOM` (the Claude Code and CC Switch imports). A line an edit takes out of a CRLF file goes with its `\r\n`.- Codex's model picker tags each of its own models by where a pick on the Agents page sends it: via magpie while routed, joined, or failing over, and direct while it is only beside its sign-in on one of magpie's models (`beside`, not joined, failover off), where such a pick takes magpie out (#940) and the GUI asks first (`TestCodexBesideOwnModelsSayDirect`).-- Codex signed in to ChatGPT stays beside its sign-in (`openai_base_url`) unless the user picks magpie API. magpie becomes its provider only while the Codex app holds the active account, which it sends nothing for: `/wham/usage` says it isn't allowed, and it has no credits and isn't a workspace still within its overage (`codexHeld`, read by `provider.CodexUsedUp`). Where the app goes by what usage can't show (an experiment's gate, a reserve, a Team plan's overage under the reserve experiment), it counts as held: a wrong move costs ChatGPT extras, a missed one every turn. A saved reading kept through a failed read stops saying held once its used-up window has started again; a hold with no window used up stays till a fresh read. The app then loses its ChatGPT state (durable threads, remote control), so a window at 100% alone, which credits get past, doesn't move it (`codex.out` marks the move, and Sync undoes it once the account has room). The same holds on one of Codex's own models joined beside the sign-in (MasterYi on Discord: the Codex app greyed Send for every model on a used-up account): Sync makes magpie the provider on that model as magpie serves it (`codexOwnViaMagpie`, `codex/<model>`), keeps the model picked in `codex.out_own`, and once the account has room puts Codex back joined on it; a model the user picked meanwhile stays theirs. With no ChatGPT account in magpie to serve it, Codex is left as it is (`TestCodexOwnModelGoesThroughMagpieWhileUsedUp`, `TestCodexOwnModelKeptOnChatGPT`). The account is read again at once when a reply says it may be out (a `usage_limit_reached` rest, or an `x-codex-*` window heard at 100%; `provider.CodexMayBeUsedUp`, at most every 20s, the active account only), not only on the five-minute look. The Codex app greys Send for every model, magpie's too, while its `authMethod` is `chatgpt` and its rate limit says not allowed (its composer's gate in ChatGPT.app 26.1013), and reads `authMethod` from its app-server's `account/read` only as the app-server starts; with magpie the provider (`requires_openai_auth` false) that answers no account, so the gate lifts. So the gateway process restarts the app's own app-server each time magpie becomes Codex's provider for the used-up account (`Agent.AppOut`) and again when Codex is put back on the account after its reset, and likewise whenever what the app-server reads as it starts and magpie writes changes (`model_provider`, `openai_base_url`, `model_catalog_json`, the magpie table's `base_url`, and a hash of what the catalog file holds, `codexCatalogTag` in [`codex_app_route.go`](../../internal/agent/codex_app_route.go), read again only when its size or time changes; `codexAppRoute`, through the `provider.CodexAppRoute` hook): a Disconnect, a Connect, failover written or taken out, a provider or group renamed or a model added. Without the catalog in it the app kept the names and models it started with until it was restarted (lucis0116 on X). Before this an app-server started while Codex was connected kept sending to the gateway after Disconnect until the app was restarted (JiangLiuer222 on X). At the watcher's first look only a used-up account's app-servers are ended, since what the others read before magpie started isn't known. It runs on every system (`KeepCodexAppOnItsProvider`, every 3s; Ethereal499 and Isdavexxx on X, MasterYi on Discord, #540: the earlier once-only reopen ran on macOS from the GUI process only, so Windows, Linux, a GUI-less magpie and the switch back stayed greyed). It ends only app-servers on magpie's Codex home (`codexAppHomeIs`: the process's `CODEX_HOME`, else `HOME/.codex`, read by `proc.EnvOf`; Windows can't tell and counts every one), only once no Codex request is in flight through the gateway nor ended in the last 10s (`Server.CodexBusy`) and no session under the Codex home has a turn started and not ended (`codexTurnOpen`), and with a clean exit (`proc.End`: SIGTERM, `TerminateProcess` with code 0), since the app shows any other exit as a crash. The app starts another app-server at once and keeps its window, threads and drafts (tried in the real app in a sandbox `CODEX_HOME`, both ways). An app-server that outlives three ends, or may not be ended, leaves the row saying to quit and reopen the app (`CodexAppStuck`). Until the reset the app's ChatGPT side is off (cloud tasks, its usage meter, Log out); the Agents row says so with the reset's time (`appOut` in `/api/state`, `CodexOutUntil`), and it all comes back at the reset. While the active account is held or a window is at 90% or more, its usage is read every minute rather than every five (`codexLookEvery`). `TestCodexAppServerRestartedEachWay`, `TestCodexAppServerRestartedOnDisconnect`, `TestCodexAppRouteFollowsConnectAndDisconnect`, `TestCodexAppRouteFollowsTheCatalog`, `TestCodexAppServerUnknown`, `TestCodexAppServerStuck`, `TestCodexAppServersOnHome`, `TestCodexTurnOpen`, `TestCodexBusy`, `codex-app-out.test.cjs`.+- Codex signed in to ChatGPT stays beside its sign-in (`openai_base_url`) unless the user picks magpie API. magpie becomes its provider only while the Codex app holds the active account, which it sends nothing for: `/wham/usage` says it isn't allowed, and it has no credits and isn't a workspace still within its overage (`codexHeld`, read by `provider.CodexUsedUp`). Where the app goes by what usage can't show (an experiment's gate, a reserve, a Team plan's overage under the reserve experiment), it counts as held: a wrong move costs ChatGPT extras, a missed one every turn. A saved reading kept through a failed read stops saying held once its used-up window has started again; a hold with no window used up stays till a fresh read. The app then loses its ChatGPT state (durable threads, remote control), so a window at 100% alone, which credits get past, doesn't move it (`codex.out` marks the move, and Sync undoes it once the account has room). The same holds on one of Codex's own models joined beside the sign-in (MasterYi on Discord: the Codex app greyed Send for every model on a used-up account): Sync makes magpie the provider on that model as magpie serves it (`codexOwnViaMagpie`, `codex/<model>`), keeps the model picked in `codex.out_own`, and once the account has room puts Codex back joined on it; a model the user picked meanwhile stays theirs. With no ChatGPT account in magpie to serve it, Codex is left as it is (`TestCodexOwnModelGoesThroughMagpieWhileUsedUp`, `TestCodexOwnModelKeptOnChatGPT`). The account is read again at once when a reply says it may be out (a `usage_limit_reached` rest, or an `x-codex-*` window heard at 100%; `provider.CodexMayBeUsedUp`, at most every 20s, the active account only), not only on the five-minute look. A turn relayed to Codex's own sign-in, the one account on (`codexUpstreamOn` with no account, the gateway's `codexRelayedAccount`), is heard the same way: its `x-codex-*` windows and `codex.rate_limits` event kept as the sign-in's usage (`heardCodexLimits`), and a `usage_limit_reached` refusal reading it again. Before, only turns served through magpie's accounts were, so with one account magpie learned the account was out only at the next look, minutes on, and the Codex app kept Send greyed until it was restarted (unsplashz on Discord; `TestCodexUsedUpRelayedAsksAtOnce`, `TestCodexRelayedWindowsHeard`). A Codex started before magpie became its provider keeps sending its own model's turns to its built-in provider; while the sign-in is known out (`provider.CodexUsedUpKnown`) and the request signs as it, those go through routing as `codex/<model>` (`codexAccounts`), as a restarted Codex's do (`TestCodexHeldSignInServedWithoutRestart`). The Codex app greys Send for every model, magpie's too, while its `authMethod` is `chatgpt` and its rate limit says not allowed (its composer's gate in ChatGPT.app 26.1013), and reads `authMethod` from its app-server's `account/read` only as the app-server starts; with magpie the provider (`requires_openai_auth` false) that answers no account, so the gate lifts. So the gateway process restarts the app's own app-server each time magpie becomes Codex's provider for the used-up account (`Agent.AppOut`) and again when Codex is put back on the account after its reset, and likewise whenever what the app-server reads as it starts and magpie writes changes (`model_provider`, `openai_base_url`, `model_catalog_json`, the magpie table's `base_url`, and a hash of what the catalog file holds, `codexCatalogTag` in [`codex_app_route.go`](../../internal/agent/codex_app_route.go), read again only when its size or time changes; `codexAppRoute`, through the `provider.CodexAppRoute` hook): a Disconnect, a Connect, failover written or taken out, a provider or group renamed or a model added. Without the catalog in it the app kept the names and models it started with until it was restarted (lucis0116 on X). Before this an app-server started while Codex was connected kept sending to the gateway after Disconnect until the app was restarted (JiangLiuer222 on X). At the watcher's first look only a used-up account's app-servers are ended, since what the others read before magpie started isn't known. It runs on every system (`KeepCodexAppOnItsProvider`, every 3s; Ethereal499 and Isdavexxx on X, MasterYi on Discord, #540: the earlier once-only reopen ran on macOS from the GUI process only, so Windows, Linux, a GUI-less magpie and the switch back stayed greyed). It ends only app-servers on magpie's Codex home (`codexAppHomeIs`: the process's `CODEX_HOME`, else `HOME/.codex`, read by `proc.EnvOf`; Windows can't tell and counts every one), only once no Codex request is in flight through the gateway nor ended in the last 10s (`Server.CodexBusy`) and no session under the Codex home has a turn started and not ended (`codexTurnOpen`), and with a clean exit (`proc.End`: SIGTERM, `TerminateProcess` with code 0), since the app shows any other exit as a crash. The app starts another app-server at once and keeps its window, threads and drafts (tried in the real app in a sandbox `CODEX_HOME`, both ways). An app-server that outlives three ends, or may not be ended, leaves the row saying to quit and reopen the app (`CodexAppStuck`). Until the reset the app's ChatGPT side is off (cloud tasks, its usage meter, Log out); the Agents row says so with the reset's time (`appOut` in `/api/state`, `CodexOutUntil`), and it all comes back at the reset. While the active account is held or a window is at 90% or more, its usage is read every minute rather than every five (`codexLookEvery`). `TestCodexAppServerRestartedEachWay`, `TestCodexAppServerRestartedOnDisconnect`, `TestCodexAppRouteFollowsConnectAndDisconnect`, `TestCodexAppRouteFollowsTheCatalog`, `TestCodexAppServerUnknown`, `TestCodexAppServerStuck`, `TestCodexAppServersOnHome`, `TestCodexTurnOpen`, `TestCodexBusy`, `codex-app-out.test.cjs`.- Moving a field from one of magpie's models to another (the agent's own picker) is not drift. Moving it off magpie is.- Claude Desktop applying a configLibrary profile other than magpie's is not drift when that profile is a gateway profile for this magpie's gateway (`desktopAppliedGateway` in [`claudedesktop.go`](../../internal/agent/claudedesktop.go)): Desktop's "Import configuration" adds a file exported on another computer, magpie's profile there with the user's connectors (`managedMcpServers`), as a new entry under a random id (aa86222 on Discord). magpie keeps it applied on Reconnect and through Sync, and writes in it only `inferenceGatewayApiKey` (with a missing auth scheme) when the gateway would refuse its key (`desktopKeyApplied`; magpie's own keys count as taken, `desktopKeyTaken`). A profile for another gateway, CC Switch's among them, is still drift, and Reconnect applies magpie's. The first switch-on still starts magpie's profile from an applied one (#2035). `TestClaudeDesktopImportedProfileIsOnMagpie`.- A field that follows another while empty (`Follows`: Claude Code's tiers and subagents on its main model) is read by `Drift` as on that field's model. The main model moved, in Claude Code's own `/model`, onto the model magpie had set a tier to is not drift (#1050). The tier keeps that model as its own through the move and the next one (`tier_own` in the stash), whatever it would follow on the new main model: fable, which follows no model that is no Fable, and haiku, which follows a Claude model on its Haiku, read as following and lost their model on the next move (#2257).diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex ddd4e0a14..823652fc7 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -206,7 +206,7 @@ recorded as a prediction failure (`noPrediction`).- In turn (`rotate`), a conversation nobody has answered yet goes to the key, account or model the fewest other conversations of the provider's model or the group answered in the last `heldFor` (30 minutes) are on (`leastHeld`, #946). Ties, resting ones and a group with a group in it keep routing's order. With session affinity, two sessions of one group so stay on different models.- A ChatGPT account that holds credits answers past its windows on them; the vendor spends them. That is the default. Set not to spend them (Usage card's *Use credits*, `magpie quota credits <account> off`), the account is held at 100% of a window till it renews, by magpie's latest reading of its windows (`provider.Allowances`, refreshed about every minute), so the other accounts, groups and fallbacks take the request. A cap set on the account (1–99%) takes precedence. Unlike a cap, the hold is at the vendor's own 100%, so an account allowed to spend its resets spends one there, both when every candidate is held and after the last one is refused. A reset made forgets the account's cached allowance (`renewedNow`), so neither hold outlives the windows it started again. One let spend its credits, and holding some by its last `/wham/usage` (`credits.has_credits` with a balance over 0, or `unlimited`; `QuotaWindow.Credits`, `Limit.Credits`, `Allowance.OnCredits`), is not out when a window fills (豆子 and Zhenzhen on Discord): ChatGPT may answer the turn that uses the window up 429 `usage_limit_reached` before it spends the credits, and the next turn on them. So that 429 is asked again at once, once a request, on the same account, before a reset is looked at, on the routed path (`creditsAgain`, `creditsAsked`, `Try.Credits`) and on Codex's own sign-in's relay (`relayOnCredits`), rather than ended for the agent, which ended a running session while a new one worked. Refused again, it rests `fallbackCooldown` (`Rest.By` `credits`) and is read again, not until its window renews, and its full windows rest it nowhere else (`full` is zero for it, `spendsCredits`). Smart, Pace and Least used put such an account first of the spent tier (`routes.Seat.Credits`, `spentBefore`): after those with allowance left, ahead of those used up with nothing to go on with (`Weighed.Credits`, told on the Routing page). Switching to credits early, at a few percent left, was weighed and not done: the vendor spends credits only once the allowance is used up, so it would only waste what is left. `TestCodexCreditsAskedAgainAtTheEdge`, `TestCodexCreditsNoLongRest`, `TestCodexCreditsFirstOfTheSpent`, `TestCreditsFirstOfTheSpent`, `routing-codex-credits-again.test.cjs`.- Different failures are classified separately: out of credit, out of quota, rate limit and other errors rest for different lengths. A fix must not merge them into one bucket (see #873).-- One of Codex's own models on the backend path with no routing to do (`codexAccounts` false: one ChatGPT account, no usage cap, no key holds, nothing pinned) is relayed as Codex sent it, on Codex's own sign-in, its bytes and headers unchanged and its reply streamed as it comes (`codexUpstreamOn` with no account): routing was built for more than one account, and that path also keeps the turn's native compaction with the backend. Before, nothing was asked again on it, so an overload went straight to Codex, which doesn't retry the backend's `server_is_overloaded` itself (夜松云 on Discord). Now a transient overload (`codexBusy`: what `busy` calls busy, a 529, a 503 saying overloaded, the backend's in-stream `response.failed` with `server_is_overloaded` (#1418); never a 429, a usage limit, an edge's drop of the connection (`edgeSaidDrop`) or a reset (`codexReset`)) is asked again of the same account, the same body and headers, up to `lastRetries` times after `retryPause<<n`, as the accounts loop asks its last account, and only before any of the reply has reached Codex. A stream is held for it through a `holdWriter` with its reasoning let through (`thinkingShown`), kept alive past `keepHeldAfter` across the tries (one `keptAlive`), so Codex sees the stream's headers and comments, never a failed try's frames. Nothing rests for an overload. Once the retries are spent, a turn whose codex subscription has fallbacks (`codexFallsBack`; never a compaction) goes on to them (`codexFallBack`: the turn served as `codex/<model>` with the account left out of the candidates, `fallbacksOnly`, and the same keepalive carried in `handedOnKey`); without fallbacks Codex gets the last try's failure as the backend said it. Each try is a `HandedOn` usage record and a try in the trace. A success is relayed as before, with nothing held when no retry or fallback is left (`TestCodexOneAccountOverloadAskedAgain`, `…StreamOverloadAskedAgain`, `…OverloadBounded`, `…OverloadFallsBack`, `…OverloadKeptAlive`, `…RefusalNotAskedAgain` in [`codex_one_account_overload_test.go`](../../internal/gateway/codex_one_account_overload_test.go)).+- One of Codex's own models on the backend path with no routing to do (`codexAccounts` false: one ChatGPT account, no usage cap, no key holds, nothing pinned, and the sign-in not known to be out of its allowance, `provider.CodexUsedUpKnown`, which routes the turn as `codex/<model>` as a restarted Codex's goes) is relayed as Codex sent it, on Codex's own sign-in, its bytes and headers unchanged and its reply streamed as it comes (`codexUpstreamOn` with no account): routing was built for more than one account, and that path also keeps the turn's native compaction with the backend. Before, nothing was asked again on it, so an overload went straight to Codex, which doesn't retry the backend's `server_is_overloaded` itself (夜松云 on Discord). Now a transient overload (`codexBusy`: what `busy` calls busy, a 529, a 503 saying overloaded, the backend's in-stream `response.failed` with `server_is_overloaded` (#1418); never a 429, a usage limit, an edge's drop of the connection (`edgeSaidDrop`) or a reset (`codexReset`)) is asked again of the same account, the same body and headers, up to `lastRetries` times after `retryPause<<n`, as the accounts loop asks its last account, and only before any of the reply has reached Codex. A stream is held for it through a `holdWriter` with its reasoning let through (`thinkingShown`), kept alive past `keepHeldAfter` across the tries (one `keptAlive`), so Codex sees the stream's headers and comments, never a failed try's frames. Nothing rests for an overload. Once the retries are spent, a turn whose codex subscription has fallbacks (`codexFallsBack`; never a compaction) goes on to them (`codexFallBack`: the turn served as `codex/<model>` with the account left out of the candidates, `fallbacksOnly`, and the same keepalive carried in `handedOnKey`); without fallbacks Codex gets the last try's failure as the backend said it. Each try is a `HandedOn` usage record and a try in the trace. The reply's `x-codex-*` windows are kept as the sign-in's usage and a `usage_limit_reached` refusal has it read again at once (`heardCodexLimits`, `codexMayBeUsedUp`), as on the accounts path. A success is relayed as before, with nothing held when no retry or fallback is left (`TestCodexOneAccountOverloadAskedAgain`, `…StreamOverloadAskedAgain`, `…OverloadBounded`, `…OverloadFallsBack`, `…OverloadKeptAlive`, `…RefusalNotAskedAgain` in [`codex_one_account_overload_test.go`](../../internal/gateway/codex_one_account_overload_test.go)).- A subscription account whose sign-in its vendor revoked (`provider.SignInRevoked`: 401 `token_revoked` and the like, #1793) fails as `failAuth`: the next member is asked at once, nothing rests, and the account is marked (`noteSignIn` in [`signin_revoked.go`](../../internal/gateway/signin_revoked.go)). Its later requests aren't sent while it has that credential: `forwardOnce` answers them with a 401 of magpie's own (`revokedReply`), so they too pass on. Compaction on an account of magpie's (`compactOn`) picks a marked one last. A key's provider answering the same words rests as before.- A ChatGPT 401 that says the token sent has expired and asks for a refresh (`provider.SignInExpired`, #2973) while its `exp` is ahead renews the account's sign-in once (`provider.NoteExpiredSignIn` from `noteSignIn`), keyed on the token sent, so the next request goes with a fresh one; nothing is marked, and the request fails over as any 401 does ([Providers and accounts](providers-accounts.md), A token called expired).- A sub2api key out of its own limit answers 429 `api key 7天限额已用完` (or `日限额`, `5小时限额`) as `rate_limit_exceeded`. It is out of quota (`plannedWords`), not rate limited: it rests till the window it filled renews when its windows as last read say so, else `quotaRest`, and its windows are read again (`staleKeyAllowance`). A reading that finds that window full no more (the limit raised in the relay's panel, or the key's usage reset) lifts the rest (`renewed`, told through `provider.OnRenewed`), as a Codex reset spent does an account's; the key's other rests stay. While it rests out of its windows, they are read again each time it is planned (`restingOf`, once a minute at most), so this holds where nothing weighs it by them: an ordered group. The relay out of accounts for everyone (503 `No available accounts`, or a later version's 429 `All available accounts are currently rate-limited`) says nothing of the key's windows: it backs off as any failure or rate limit does, never to the window. A key's windows count only its own spend, so other users draining the relay's accounts show up as those errors, not in the windows.diff --git a/internal/gateway/codex_app_out_test.go b/internal/gateway/codex_app_out_test.goindex 4c014ae26..b97474b70 100644--- a/internal/gateway/codex_app_out_test.go+++ b/internal/gateway/codex_app_out_test.go@@ -1,9 +1,14 @@package gatewayimport (+ "io"+ "net/http"+ "net/http/httptest""strings""sync""testing"++ "github.com/yetone/magpie/internal/provider")// A turn refused with usage_limit_reached on the account Codex is signed@@ -31,3 +36,127 @@ func TestCodexUsedUpReplyAsksAtOnce(t *testing.T) {t.Fatalf("asked %v, want [email protected]", asked)}}++// The same refusal with Codex's sign-in the only account: the turn is+// relayed to ChatGPT as Codex sent it, and still has the account read+// again at once. Only the next look, minutes on, made magpie Codex's+// provider before, and the Codex app kept Send greyed until it was+// restarted (unsplashz on Discord). A request signed by a ChatGPT account+// magpie doesn't have (a Codex in WSL) asks nothing.+func TestCodexUsedUpRelayedAsksAtOnce(t *testing.T) {+ codexSignedIn(t)+ var tried, models []string+ usedUp(t, &tried, &models)+ var mu sync.Mutex+ var asked []string+ old := codexMayBeUsedUp+ codexMayBeUsedUp = func(user string) { mu.Lock(); asked = append(asked, user); mu.Unlock() }+ t.Cleanup(func() { codexMayBeUsedUp = old })++ code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)+ if code != 429 || !strings.Contains(body, "usage_limit_reached") || strings.Join(tried, ",") != "acct-1" {+ t.Fatalf("%d %s tried %v", code, body, tried)+ }+ mu.Lock()+ got := strings.Join(asked, ",")+ asked = nil+ mu.Unlock()+ if got != "[email protected]" {+ t.Fatalf("asked %q, want [email protected]", got)+ }++ rec := httptest.NewRecorder()+ req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"gpt-5.5","stream":true,"input":"ping"}`))+ req.Header.Set("Authorization", "Bearer chatgpt-token")+ req.Header.Set("chatgpt-account-id", "acct-1-elsewhere")+ New().Handler().ServeHTTP(rec, req)+ mu.Lock()+ defer mu.Unlock()+ if len(asked) != 0 {+ t.Fatalf("another sign-in's refusal asked for %v", asked)+ }+}++// A relayed turn that answers with the account's window heard full keeps+// it as the sign-in's usage, as a turn through magpie's accounts does+// (#1295), which reads the account again at once (NoteCodexLimits).+func TestCodexRelayedWindowsHeard(t *testing.T) {+ codexSignedIn(t)+ up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ io.ReadAll(r.Body)+ w.Header().Set("x-codex-primary-used-percent", "100")+ w.Header().Set("x-codex-primary-window-minutes", "300")+ io.WriteString(w, sse(+ `data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`,+ `data: {"type":"response.output_text.delta","delta":"pong"}`,+ `data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`))+ }))+ t.Cleanup(up.Close)+ was := provider.CodexBase+ provider.CodexBase = up.URL + "/backend-api/codex"+ t.Cleanup(func() { provider.CodexBase = was })+ var mu sync.Mutex+ var heard []string+ old := noteCodexLimits+ noteCodexLimits = func(agent, user string, ls []provider.CodexLimit) {+ mu.Lock()+ defer mu.Unlock()+ for _, l := range ls {+ if l.Used >= 100 {+ heard = append(heard, agent+"/"+user)+ }+ }+ }+ t.Cleanup(func() { noteCodexLimits = old })++ code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)+ if code != 200 || !strings.Contains(body, "pong") {+ t.Fatalf("status %d: %s", code, body)+ }+ mu.Lock()+ defer mu.Unlock()+ if strings.Join(heard, ",") != "codex/[email protected]" {+ t.Fatalf("heard %v, want codex/[email protected]", heard)+ }+}++// Codex's sign-in, the one account, known to be out: a Codex started+// before magpie became its provider (codex.out) still sends its own+// model's turns to its built-in provider. They go through magpie's+// accounts as codex/<model>, as a restarted Codex's do, rather than+// relayed to the same refusal until a restart (unsplashz on Discord).+// Not known out, or signed by an account magpie doesn't have, they go+// as they came.+func TestCodexHeldSignInServedWithoutRestart(t *testing.T) {+ codexSignedIn(t)+ held := false+ old := codexHeldKnown+ codexHeldKnown = func() bool { return held }+ t.Cleanup(func() { codexHeldKnown = old })+ ask := func(acct string) (string, bool) {+ r := httptest.NewRequest("POST", CodexPath+"/responses", nil)+ r.Header.Set("Authorization", "Bearer chatgpt-token")+ r.Header.Set("chatgpt-account-id", acct)+ return codexAccounts(r, "gpt-5.5")+ }+ if id, ok := ask("acct-1"); ok {+ t.Fatalf("with room, routed as %s", id)+ }+ held = true+ if id, ok := ask("acct-1"); !ok || id != "codex/gpt-5.5" {+ t.Fatalf("held: %q %v, want codex/gpt-5.5", id, ok)+ }+ if id, ok := ask("acct-elsewhere"); ok {+ t.Fatalf("another sign-in routed as %s", id)+ }++ // end to end: the turn goes through routing, which answers in its own+ // words (the vendor named) where the relay hands ChatGPT's on as they+ // came+ var tried, models []string+ usedUp(t, &tried, &models)+ code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)+ if code != 429 || strings.Join(tried, ",") != "acct-1" || !strings.Contains(body, "Codex: The usage limit") {+ t.Fatalf("not served through routing: %d %s tried %v", code, body, tried)+ }+}diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.goindex 698279585..2d66ae051 100644--- a/internal/gateway/codex_backend.go+++ b/internal/gateway/codex_backend.go@@ -740,6 +740,9 @@ func compactOn(r *http.Request, model string) *provider.Provider {// A gateway key held to some models or accounts (#882, #905) is served// through it always, alone no less: the relay would spend Codex's own// sign-in with nothing of the key asked, its accounts and its models both.+// codexHeldKnown is provider.CodexUsedUpKnown, which tests set.+var codexHeldKnown = provider.CodexUsedUpKnown+func codexAccounts(r *http.Request, model string) (string, bool) {h := r.Headerif model == "" || strings.Contains(model, "/") || apiKey(h) {@@ -767,6 +770,19 @@ func codexAccounts(r *http.Request, model string) (string, bool) {if keyHeld || accHeld {return id, true}+ // Codex's sign-in known to be out (of its allowance, no credits): a+ // Codex started before magpie took its turns over (codex.out) still+ // sends them to its built-in provider, and relayed as they came each+ // one met the same refusal until Codex was restarted, which then sent+ // them to magpie as codex/<model> (unsplashz on Discord). Served as+ // that restarted Codex's are, they go where routing sends them, with+ // no restart. A request signed by an account magpie doesn't have (a+ // Codex in WSL) goes as it came+ if ok && p.Account != nil && p.Account.Agent == "codex" && codexHeldKnown() {+ if acct := h.Get("chatgpt-account-id"); acct == "" || acct == provider.CodexSignedInAccount() {+ return id, true+ }+ }if !ok || p.Account == nil || p.Account.Agent != "codex" || len(p.AlsoOn()) == 0 && !pinned {return "", false}@@ -1084,6 +1100,7 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st}var sniff *usageSniffervar refusal []byte // what OpenAI said, of a request it turned away+ relayedAcct := "" // magpie's ChatGPT account the turn went out on, if anyif rest == "/responses" && base != codexAPIBase {// the account goes on its credits, its week used up: a reset it// spends by itself when the week runs out goes first@@ -1231,6 +1248,17 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest stbody = b}if rest == "/responses" {+ if acct, ok := codexRelayedAccount(base, r.Header, on); ok {+ // the windows the reply tells of the account it went out on+ // are kept as its usage, and a full one has it read again at+ // once, as a turn served through magpie's accounts has: with+ // Codex's sign-in the only account, every turn comes this way,+ // and the Codex app was moved off a used-up account only at+ // the next look, minutes on, Send greyed until it was+ // restarted (unsplashz on Discord)+ heardCodexLimits(acct, res)+ relayedAcct = acct.Account.User+ }// a web page or nothing at all, served 200, is the 502 it stands// for (#1012)res = notAnAPIReply(res, "OpenAI: ")@@ -1390,6 +1418,12 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st// the Routing view show why, not just thatcall.Error = provider.APIError(refusal, res.Status)errType = provider.ErrorType(refusal)+ if relayedAcct != "" && failure(res.StatusCode, refusal) == failQuota {+ // the account it went out on refused the turn as out of its+ // allowance: read again now, so magpie takes the turns over+ // (codex.out) and the Codex app is told without a restart+ codexMayBeUsedUp(relayedAcct)+ }}end(call.Status, call.Error, uu.Input+uu.Output+uu.CacheRead+uu.CacheWrite, uu.Output)call.Route = tr.ID@@ -1497,23 +1531,35 @@ func fallbacksOnly(cands []candidate, pl planned, id, model string) ([]candidate// stays openai too, rather than a codex row that names the wrong account.// Rows already written are not rewritten.func codexServed(base string, h http.Header, on *provider.Provider) (id, account string) {- if base != provider.CodexBase || apiKey(h) {+ p, ok := codexRelayedAccount(base, h, on)+ if !ok {return "openai", ""}+ return p.ID, p.Account.User+}++// codexRelayedAccount is the ChatGPT account of magpie's that a relay+// through codexUpstreamOn went out on: the one it was signed with (on),+// or Codex's own sign-in when the request names it; false for a call to+// OpenAI's API, or one signed by an account magpie doesn't have.+func codexRelayedAccount(base string, h http.Header, on *provider.Provider) (provider.Provider, bool) {+ if base != provider.CodexBase || apiKey(h) {+ return provider.Provider{}, false+ }if on != nil && on.Account != nil && on.Account.User != "" {- return on.ID, on.Account.User+ return *on, true}// the token went out as Codex sent it: it is the sign-in magpie reads// only when it names the same ChatGPT account. A Codex in WSL, or// under another CODEX_HOME, signs with its own, which isn't named hereif acct := h.Get("chatgpt-account-id"); acct != "" && acct != provider.CodexSignedInAccount() {- return "openai", ""+ return provider.Provider{}, false}p, err := provider.Find("codex")if err != nil || p.Account == nil || p.Account.User == "" {- return "openai", ""+ return provider.Provider{}, false}- return p.ID, p.Account.User+ return *p, true}// unreadableItem is the item OpenAI's refusal names: sealed content it