@@ -212,6 +212,7 @@ import { |
212 | 212 | fetchWithResetRetry, |
213 | 213 | fetchWithTransientRetry, |
214 | 214 | isNonReplayableResponse, |
| 215 | + isTransientUpstreamStatus, |
215 | 216 | prepareSameTarget429Wait, |
216 | 217 | } from "../../lib/upstream-retry"; |
217 | 218 | import { |
@@ -1146,6 +1147,28 @@ export async function shouldRetryCodexPoolAccountQuota( |
1146 | 1147 | } |
1147 | 1148 | } |
1148 | 1149 |
|
| 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 | + |
1149 | 1172 | interface CodexPoolAccountRetryArgs { |
1150 | 1173 | /** Sanitized caller input, before any selected Pool credential was materialized. */ |
1151 | 1174 | callerAuthHeaders: Headers; |
@@ -1320,6 +1343,21 @@ async function retryCodexPoolOnAlternateAccount( |
1320 | 1343 | const inboundWire = options.inboundWire ?? "responses"; |
1321 | 1344 | const entitlementResolver = options.resolveCodexModelEntitlements ?? resolveCodexModelEntitlements; |
1322 | 1345 | 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 | + }; |
1323 | 1361 | if (outcomeStatus === 400 && ACCOUNT_GATED_NATIVE_OPENAI_MODELS.has(route.modelId)) { |
1324 | 1362 | invalidateCodexModelEntitlementsForAccount(firstAuthCtx.accountId); |
1325 | 1363 | let refreshed; |
@@ -1344,6 +1382,7 @@ async function retryCodexPoolOnAlternateAccount( |
1344 | 1382 | // Exact account selectors may retry the same confirmed account above, but must never resolve |
1345 | 1383 | // an alternate. Quota failures and a refreshed entitlement miss remain terminal. |
1346 | 1384 | if (!retryAuthCtx && (firstAuthCtx.fixedAccount || args.sameAccountOnly === true)) { |
| 1385 | + recordUnmovedTransientOutcome(); |
1347 | 1386 | return { kind: "no-alternate" }; |
1348 | 1387 | } |
1349 | 1388 | try { |
@@ -1394,6 +1433,7 @@ async function retryCodexPoolOnAlternateAccount( |
1394 | 1433 | writerGeneration: firstAuthCtx.writerGeneration, |
1395 | 1434 | }); |
1396 | 1435 | } |
| 1436 | + recordUnmovedTransientOutcome(); |
1397 | 1437 | return { kind: "no-alternate" }; |
1398 | 1438 | } |
1399 | 1439 |
|
@@ -5669,6 +5709,10 @@ async function handleResponsesInner( |
5669 | 5709 | // ChatGPT sometimes wraps quota exhaustion in a generic 5xx. Normalize only |
5670 | 5710 | // body-confirmed cases to quota evidence so cooldown and rotation both apply. |
5671 | 5711 | 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; |
5672 | 5716 | } |
5673 | 5717 |
|
5674 | 5718 | if (poolRetryOutcome !== undefined) { |
|