Commit Graph
2 Commits
Author SHA1 Message Date
yetone affa979d56 gateway: Codex's one account used up goes on to the Codex subscription's fallbacks
adorable_piglet on Discord: once the ChatGPT (Plus) account's quota ran
out, the Codex app couldn't send at all, and magpie never moved the turn
to the fallback they had set (their own API key, another provider).

Cause: with one ChatGPT account and nothing to route, a turn on Codex's
own sign-in is relayed as it came (codexUpstreamOn). The relay handed an
overload on to the subscription's fallbacks (codexFallBack), but a
quota, credit or rate refusal went to Codex as the backend said it, the
429 usage_limit_reached with its x-codex headers. Codex then stops.

Change: codexAccountOut names the refusals that are the account's, not
the request's: failure() quota, credit or rate, as a status or as a
stream's failure before any reply (response.failed, an error event, a
stream without Content-Type), and a 403 usage_not_included. Never a 401:
Codex renews its own sign-in on it. When the subscription has fallbacks
(codexFallsBack, never a compaction), such a refusal now goes on to them
once the credits retry and the automatic reset have been looked at, as
an overload does; the trace keeps the try's real failure kind. With no
fallback set, Codex still gets the backend's refusal.

What Codex's own client does: it greys Send from its own /wham usage
read. magpie already gets past that once the usage reading says used up
(codex.out with the app-server restart, #540, 9f641daea); this change is
the turn itself.

docs/subsystems/gateway-routing.md says it.

Run (mote, at this tree):
- go test -tags nogui ./internal/gateway -run 'TestCodexOneAccount|TestCodexOwnModel|TestCodexSpent': ok.
- New TestCodexOneAccountSpentFallsBack: all 7 codexSpent shapes red on
  the old code (Codex got the 429/403 or the failed stream), green now.
  TestCodexOneAccountSpentNoFallbackRelayed and
  TestCodexOneAccountSignInRefusedNotFallenBack hold the no-fallback and
  401 cases.
- GOOS=linux and GOOS=windows go vet ./internal/gateway: ok. gofmt clean.
- The whole internal/gateway package (-timeout 45m) was still running at
  push; the result is reported separately.

Broken by hand: codexAccountOut returning false: the 7 subtests red again.

Not tried: a real used-up ChatGPT account in the real Codex app.
2026-11-03 12:41:12 +08:00
yetone 5f73537f5b gateway: Codex's own turn on one ChatGPT account is asked again on an overload, then its fallbacks (夜松云 on Discord)
Codex signed in to ChatGPT with magpie as openai_base_url, one account,
no usage cap: Codex's own gpt-* turns go through codexUpstreamOn with no
account, relayed as Codex sent them. That path was pass-through since
0f0b8fc87 because routing was only built for more than one account, and
the relay keeps Codex's own token, request bytes and headers, streams at
once, and keeps native compaction with the backend (#2582). It also
meant no retry and no fallback: a 529, or the backend's in-stream
response.failed server_is_overloaded (#1418), went straight to Codex,
which doesn't retry ServerOverloaded itself.

Now, on that path only (POST /responses to the ChatGPT backend, no
account picked):
- a transient overload (codexBusy: busy() minus 429, edgeSaidDrop and
  codexReset) is asked again of the same account, same body and
  headers, up to lastRetries times after retryPause<<n, as the accounts
  loop asks its last account; only before any reply byte reached Codex;
- a stream is held through a holdWriter with reasoning let through
  (thinkingShown) and one keptAlive across tries, so Codex gets the
  stream's keepalives, never a failed try's frames;
- once retries are spent, a codex subscription with fallbacks
  (codexFallsBack, never a compaction) goes on to them: codexFallBack
  serves codex/<model> with this account left out (fallbacksOnly via
  handedOnKey) and the same keepalive; without fallbacks Codex gets the
  last try's failure as before;
- nothing rests; each try is a HandedOn usage record and a trace try.
A success is relayed as before; nothing is held when no retry or
fallback is left. Prompts, base_instructions and Codex's bytes/headers
are untouched.

Docs: docs/subsystems/gateway-routing.md. No README/site page said one
account is relayed unchanged (grepped en/zh/ja).

Tests: codex_one_account_overload_test.go (529 then 200; #1418 stream
then answer; bounded 1+lastRetries for status and stream; falls back
for status and stream; 400 asked once; kept alive with and without a
fallback). Each overload test fails on ef655acb4 (one request, the
overload relayed); the refusal test passes there, as it should. Broke
by hand: without the fallbacksOnly hook the FallsBack tests go red
("ChatGPT asked 4 times").

Run (sandbox HOME, at ef655acb4 + this change):
- go test -run 'Codex|Overload|Fallback|Patience|Hold' ./internal/gateway: ok
- go test -race -count=5 -cpu 1,4 on the new tests,
  TestCodexOwnModel*, TestCodexResetJudgedByTheBytesSent: ok
- gofmt -l: clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: ok
- set -o pipefail; go test -timeout 45m ./...: 61 packages ok, internal/gateway
  included (2633s); load average ~200-250. Three reds, each already
  filed, none in code this touches:
  internal/gui TestSetAnswersWhileDevinAndProvidersHang (#1375, passes
  -count=3 alone), internal/library
  TestRTKCheckLatestDoesNotWaitForThePackageManager (#2848, passes
  -count=3 alone), internal/agent past 45m (#2723; alone with
  -timeout 90m: ok, 3226s)

- rebased on fb101b0d5: gofmt, the three vets of internal/gateway and
  go test -run 'Codex|Overload|Fallback|Patience|Hold' ./internal/gateway: ok

- rebased again on 16327872c (internal/provider only): go vet
  ./internal/gateway ./internal/provider: ok
2026-10-20 03:04:43 +08:00