GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(codex): fail over to another account on a pre-stream upstream 5xx · lidge-jun/opencodex@df6ef78

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/server/responses
    • core.ts
  • tests/codex-integration
    • codex-quota-rejection.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/server/responses/core.ts‎

Copy file name to clipboardExpand all lines: src/server/responses/core.ts
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ import {
212212
fetchWithResetRetry,
213213
fetchWithTransientRetry,
214214
isNonReplayableResponse,
215+
isTransientUpstreamStatus,
215216
prepareSameTarget429Wait,
216217
} from "../../lib/upstream-retry";
217218
import {
@@ -1146,6 +1147,28 @@ export async function shouldRetryCodexPoolAccountQuota(
11461147
}
11471148
}
11481149

1150+
/**
1151+
* A pre-stream upstream 5xx another Codex account may still be able to serve.
1152+
*
1153+
* `server_is_overloaded` is the shape this exists for. The ChatGPT backend refuses in a few
1154+
* hundred milliseconds, the body carries no quota evidence, and nothing in that exchange is
1155+
* account health — so the pool keeps choosing the same account and every request fails on it
1156+
* while the other accounts sit idle. That is what an operator sees as the pool refusing to move.
1157+
*
1158+
* The status stays exactly as upstream sent it. `classifyCodexUpstreamOutcome` maps 5xx to the
1159+
* transient class, so the account earns an ordinary failure streak and `upstreamFailoverThreshold`
1160+
* decides when it is soft-avoided, rather than a quota cooldown it never earned.
1161+
*
1162+
* Deliberately narrow. {@link isNonReplayableResponse} still refuses: a post-send WebSocket
1163+
* gateway status means the body already reached the origin, so sending it from a second account
1164+
* could duplicate a turn the origin may still be running. A 5xx whose body confirms quota is not
1165+
* routed here either — {@link shouldRetryCodexPoolAccountQuota} classifies that one first and
1166+
* carries the cooldown with it.
1167+
*/
1168+
export function shouldRetryCodexPoolAccountTransient(response: Response): boolean {
1169+
return !isNonReplayableResponse(response) && isTransientUpstreamStatus(response.status);
1170+
}
1171+
11491172
interface CodexPoolAccountRetryArgs {
11501173
/** Sanitized caller input, before any selected Pool credential was materialized. */
11511174
callerAuthHeaders: Headers;
@@ -1320,6 +1343,21 @@ async function retryCodexPoolOnAlternateAccount(
13201343
const inboundWire = options.inboundWire ?? "responses";
13211344
const entitlementResolver = options.resolveCodexModelEntitlements ?? resolveCodexModelEntitlements;
13221345
let retryAuthCtx: CodexAuthContext | undefined;
1346+
// A transient 5xx must record even when this request cannot move: the ordinary terminal
1347+
// recorder only fires for an OK event-stream body, so a pre-stream refusal would otherwise
1348+
// leave the account looking healthy no matter how many times it refused, and the pool would
1349+
// keep handing it the next request.
1350+
const recordUnmovedTransientOutcome = (): void => {
1351+
if (!isTransientUpstreamStatus(outcomeStatus)) return;
1352+
recordCodexUpstreamOutcome(config, firstAuthCtx.accountId, outcomeStatus, {
1353+
threadId: firstAuthCtx.affinityKey,
1354+
fixedAccount: firstAuthCtx.fixedAccount,
1355+
modelId: route.modelId,
1356+
probeLeaseId: codexProbeLeaseId(firstAuthCtx),
1357+
probeQuotaScope: codexProbeQuotaScope(firstAuthCtx),
1358+
writerGeneration: firstAuthCtx.writerGeneration,
1359+
});
1360+
};
13231361
if (outcomeStatus === 400 && ACCOUNT_GATED_NATIVE_OPENAI_MODELS.has(route.modelId)) {
13241362
invalidateCodexModelEntitlementsForAccount(firstAuthCtx.accountId);
13251363
let refreshed;
@@ -1344,6 +1382,7 @@ async function retryCodexPoolOnAlternateAccount(
13441382
// Exact account selectors may retry the same confirmed account above, but must never resolve
13451383
// an alternate. Quota failures and a refreshed entitlement miss remain terminal.
13461384
if (!retryAuthCtx && (firstAuthCtx.fixedAccount || args.sameAccountOnly === true)) {
1385+
recordUnmovedTransientOutcome();
13471386
return { kind: "no-alternate" };
13481387
}
13491388
try {
@@ -1394,6 +1433,7 @@ async function retryCodexPoolOnAlternateAccount(
13941433
writerGeneration: firstAuthCtx.writerGeneration,
13951434
});
13961435
}
1436+
recordUnmovedTransientOutcome();
13971437
return { kind: "no-alternate" };
13981438
}
13991439

@@ -5669,6 +5709,10 @@ async function handleResponsesInner(
56695709
// ChatGPT sometimes wraps quota exhaustion in a generic 5xx. Normalize only
56705710
// body-confirmed cases to quota evidence so cooldown and rotation both apply.
56715711
poolRetryOutcome = upstreamResponse.status >= 500 ? 429 : upstreamResponse.status;
5712+
} else if (!authCtx.fixedAccount && shouldRetryCodexPoolAccountTransient(upstreamResponse)) {
5713+
// A plain transient 5xx the same-account retry layer could not absorb. Keep the real
5714+
// status so it records as transient rather than quota.
5715+
poolRetryOutcome = upstreamResponse.status;
56725716
}
56735717

56745718
if (poolRetryOutcome !== undefined) {
Collapse file

‎tests/codex-integration/codex-quota-rejection.test.ts‎

Copy file name to clipboardExpand all lines: tests/codex-integration/codex-quota-rejection.test.ts
+38-1Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { describe, expect, test } from "bun:test";
22
import { classifyCodexPreStreamRejection } from "../../src/codex/quota-rejection";
33
import { BOUNDED_BODY_MAX_BYTES } from "../../src/lib/bounded-body";
4-
import { consumeComboFailure, shouldRetryCodexPoolAccountQuota } from "../../src/server/responses/core";
4+
import {
5+
consumeComboFailure,
6+
shouldRetryCodexPoolAccountQuota,
7+
shouldRetryCodexPoolAccountTransient,
8+
} from "../../src/server/responses/core";
9+
import { markResponseNonReplayable } from "../../src/lib/upstream-retry";
510

611
function jsonRejection(status: number, error: Record<string, unknown>): Response {
712
return Response.json({ error }, { status });
@@ -91,6 +96,38 @@ describe("Codex pre-stream quota rejection classification", () => {
9196
await expect(shouldRetryCodexPoolAccountQuota(response)).resolves.toBe(false);
9297
});
9398

99+
test.each([
100+
[500, true],
101+
[502, true],
102+
[503, true],
103+
[504, true],
104+
[520, true],
105+
[507, false],
106+
[429, false],
107+
[400, false],
108+
[200, false],
109+
])("selects transient pool-account retries by HTTP %i", (status, expected) => {
110+
expect(shouldRetryCodexPoolAccountTransient(new Response(null, { status }))).toBe(expected);
111+
});
112+
113+
test("a server_is_overloaded 503 moves to another account even though it carries no quota evidence", () => {
114+
// The shape that wedged a live pool: the backend refuses in under a second, the body says
115+
// nothing about quota, and the account keeps winning selection because nothing recorded a
116+
// failure against it.
117+
const response = Response.json({
118+
error: { type: "server_error", code: "server_is_overloaded", message: "server is overloaded" },
119+
}, { status: 503 });
120+
expect(shouldRetryCodexPoolAccountTransient(response)).toBe(true);
121+
});
122+
123+
test("a non-replayable gateway status is never sent from a second account", () => {
124+
// The body already reached the origin, so a second send could duplicate a turn it may
125+
// still be running. This is the one 5xx that stays put.
126+
const response = new Response(null, { status: 502 });
127+
markResponseNonReplayable(response);
128+
expect(shouldRetryCodexPoolAccountTransient(response)).toBe(false);
129+
});
130+
94131
test.each([
95132
["error.message", { error: { message: "The usage limit has been reached" } }],
96133
["last_error.message", { last_error: { message: "The usage limit has been reached" } }],

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.