gateway: a used-up Codex sign-in, the one account, is heard on its relayed turns and served without a restart

unsplashz on Discord: Codex beside its ChatGPT sign-in, one Codex
account in magpie. When the account ran out, Codex couldn't send until it
was restarted, after which its turns went through magpie.

Cause. With Codex's sign-in the only account on, an own-model turn is
relayed to ChatGPT as Codex sent it (codexUpstreamOn, no account). That
path never kept the reply's x-codex-* / codex.rate_limits windows
(heardCodexLimits) and never asked for the account to be read again on a
usage_limit_reached refusal (codexMayBeUsedUp). Only turns served through
magpie's accounts did. So magpie learned the account was out at the next
look, one to five minutes on, and until then wrote no codex.out and left
the Codex app's app-server alone, its Send greyed. A Codex started
before codex.out also kept sending own-model turns to its built-in
provider, each relayed to the same refusal, until it was restarted.

Fix.
- codexUpstreamOn: for a /responses relay on magpie's ChatGPT account
  (codexRelayedAccount: the account signed with, or Codex's sign-in when
  the request's chatgpt-account-id names it), the reply's windows are
  heard, and a quota refusal calls codexMayBeUsedUp. codexServed now uses
  the same lookup.
- codexAccounts: while the sign-in is known out (CodexUsedUpKnown, the
  cached reading only) and the request signs as it, an own-model turn
  goes through routing as codex/<model>, as a restarted Codex's does.
  Another sign-in (a Codex in WSL) still goes as it came.

Run:
- go test ./internal/gateway -run 'TestCodexUsedUp|TestCodexRelayedWindowsHeard|TestCodexHeldSignInServedWithoutRestart|TestCodexOwnModel|TestCodexPaused|OneAccount' (mote): ok.
- GOOS=darwin/linux/windows go vet ./internal/gateway (mote): clean. gofmt -l: clean.
- go test ./... -timeout 45m -count=1 at 25fbbd03e on the Linux VM (wsl-run,
  mote had another session's full suite): 73 ok, internal/gateway ok
  (738s). Reds: ., internal/cli and internal/gui don't build there (no
  gtk4/webkitgtk-6.0 dev packages); internal/agent TestInstalledVersion,
  TestGrokInstalled, TestOmpDetectedWithPiDir, TestZedAppPath, all #4033
  (and #3548). The first three are red the same on origin/main e61e10535
  there; TestZedAppPath passes 3 of 3 alone on this branch.
- go test . ./internal/cli ./internal/gui ./internal/agent -timeout 45m at
  25fbbd03e on mote (load ~300, other sessions' suites): ., cli, agent ok.
  internal/gui: 12 real-window AppKit tests killed at their deadlines
  (#2645, #4125). Alone, TestGatewayHeldByAContainer and
  TestGatewayFixQuitsOnlyAMagpie pass; the other 10 (TestDockJoinLeadsCommandTab,
  TestPanelCloseHidesIt, TestPanelJoinsFullScreenSpaces,
  TestPluginPageLoadsItsFilesInTheWindow, TestDockReopenKeepsTheWindowsSpace,
  TestMainWindowCorners, TestMainWindowRightToLeft,
  TestHandoverLeavesTheMenuBarIconForTheNextMagpie,
  TestTrayCellClickReleasedPanel, TestCoveredWindowStaysUp) are red the
  same on origin/main e61e10535 there. Nothing in gui is touched.
- Rebased on origin/main (12 commits, none in codex_backend.go):
  go test ./internal/gateway -run TestCodex at the pushed head on the
  Linux VM (mote stopped answering ssh): ok.

Broken by hand:
- codexMayBeUsedUp call removed: TestCodexUsedUpRelayedAsksAtOnce red
  (asked "", want me@example.com).
- heardCodexLimits call removed: TestCodexRelayedWindowsHeard red (heard []).
- held branch off in codexAccounts: TestCodexHeldSignInServedWithoutRestart
  red (held: "" false). Account-id guard removed: red (another sign-in
  routed as codex/gpt-5.5).

Not tried:
- A real used-up ChatGPT account, or the real Codex app. The real codex
  0.162.1 CLI in a sandbox CODEX_HOME against a fake backend never got
  past its workspace routing discovery (/wham/accounts/check), so no turn
  reached the fake.
- With one account and no fallbacks, the turn served through routing
  still ends in the account's 429 (worded by routing). It goes on only
  where routing has somewhere to send it, as for a restarted Codex.
This commit is contained in:
yetone
2026-10-29 15:14:02 +08:00
parent be8b0f3816
commit 34b2dd9b70
4 changed files with 182 additions and 7 deletions
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -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.
+129
View File
@@ -1,9 +1,14 @@
package gateway
import (
"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 me@example.com", 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 != "me@example.com" {
t.Fatalf("asked %q, want me@example.com", 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/me@example.com" {
t.Fatalf("heard %v, want codex/me@example.com", 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)
}
}
+51 -5
View File
@@ -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.Header
if 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 *usageSniffer
var refusal []byte // what OpenAI said, of a request it turned away
relayedAcct := "" // magpie's ChatGPT account the turn went out on, if any
if 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 st
body = 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 that
call.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 here
if 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