Skip to content

Commit 1daf4f6

Browse files
committed
fix(responses): restore non-replayable replay-refusal guards dropped in rebase
The quota-family rebase lost upstream isNonReplayableResponse guards and wire-scoped enforceDeclaredToolNames in the 429 recovery arms (CodeRabbit Major on PR #4676). A replay-refused 429 must never enter same-target retry, key rotation, or OAuth failover. No behavior change besides the restored guards.
1 parent 00dc6a3 commit 1daf4f6

4 files changed

Lines changed: 81 additions & 45 deletions

File tree

‎src/server/responses/adapter-continuation.ts‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
fetchWithTransientRetry,
2626
fetchWithResetRetry,
2727
applyUpstreamRecoveryInit,
28+
isNonReplayableResponse,
2829
prepareSameTarget429Wait,
2930
} from "../../lib/upstream-retry";
3031
import { redactSecretString } from "../../lib/redact";
@@ -262,9 +263,12 @@ export function createAdapterContinuations(
262263
// Same-target 429 wait-and-retry (opt-in `retryOn429`) before key/account failover:
263264
// a primary-key rate-limit blip replays on the SAME key, matching the main recovery
264265
// loop; only after the attempts are exhausted does the continuation fail over.
265-
while (
266-
response.status === 429
267-
&& rateLimitPolicy !== null
266+
while (
267+
response.status === 429
268+
// A synthesized replay refusal is not a rate limit; replaying the continuation on
269+
// it would re-send a turn whose first send may already have been processed.
270+
&& !isNonReplayableResponse(response)
271+
&& rateLimitPolicy !== null
268272
&& adapterExchange.rateLimitRetries < rateLimitPolicy.attempts
269273
// The main recovery loop and the passthrough ladder both consult the shared remainder
270274
// here; this loop did not, so a request whose budget was already spent could still
@@ -311,7 +315,7 @@ export function createAdapterContinuations(
311315
}
312316
}
313317

314-
if (response.status === 429 && hasKeyPoolFailover(route.provider)) {
318+
if (response.status === 429 && !isNonReplayableResponse(response) && hasKeyPoolFailover(route.provider)) {
315319
const rotated = rotateProviderTransportOn429(config, route.providerName, route.provider, {
316320
retryAfter: response.headers.get("retry-after"),
317321
now: Date.now(),
@@ -344,10 +348,11 @@ export function createAdapterContinuations(
344348
continue;
345349
}
346350
}
347-
if (
348-
response.status === 429
349-
&& transportState.anthropicPoolAccountId
350-
&& transportState.anthropicPoolFailovers < ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST
351+
if (
352+
response.status === 429
353+
&& transportState.anthropicPoolAccountId
354+
&& !isNonReplayableResponse(response)
355+
&& transportState.anthropicPoolFailovers < ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST
351356
) {
352357
const nextAccountId = rotateAnthropicAccountOn429(
353358
config,
@@ -385,10 +390,11 @@ export function createAdapterContinuations(
385390
// 429 stayed terminal even with failover fully active -- the same class of divergence the
386391
// two sidecars already produced once. Request-local state is shared with the other arms so
387392
// the per-request bound cannot be silently re-armed by reaching a different loop.
388-
if (
389-
response.status === 429
390-
&& transportState.genericFailoverAccountId
391-
&& transportState.genericFailovers < GENERIC_OAUTH_MAX_FAILOVERS_PER_REQUEST
393+
if (
394+
response.status === 429
395+
&& transportState.genericFailoverAccountId
396+
&& !isNonReplayableResponse(response)
397+
&& transportState.genericFailovers < GENERIC_OAUTH_MAX_FAILOVERS_PER_REQUEST
392398
&& isGenericOAuthFailoverEnabled(config, route.providerName)
393399
) {
394400
// Intersection with the shared request budget. The continuation loop re-sends the

‎src/server/responses/adapter-dispatch.ts‎

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isNonReplayableResponse } from "../../lib/upstream-retry";
12
import type { ResponsesRequestContext, ResponsesAdmissionState } from "./core-options";
23
import type { PreparedResponsesRequest } from "./request-prepare";
34
import type { ResponsesTransport } from "./request-transport";
@@ -524,11 +525,17 @@ export async function prepareAdapterExchange(
524525
return { failed: formatErrorResponse(502, "upstream_error", msg) };
525526
}
526527
};
527-
// Keep recovery kinds in sync with the native Responses `passthroughRecovery:` loop above.
528-
recovery: for (;;) {
529-
if (
530-
upstreamResponse.status === 401
531-
&& isOAuth401ReplayProvider
528+
// Keep recovery kinds in sync with the native Responses `passthroughRecovery:` loop above.
529+
recovery: for (;;) {
530+
// Preserve the terminal verdict through adapter and combo error formatting.
531+
// This also covers a reset reached by a 401/429/413 recovery refetch.
532+
if (isNonReplayableResponse(upstreamResponse)) {
533+
cleanupUpstreamAbort();
534+
return upstreamResponse;
535+
}
536+
if (
537+
upstreamResponse.status === 401
538+
&& isOAuth401ReplayProvider
532539
&& transportState.sentOAuthSnapshot
533540
&& !oauth401ReplayAttempted
534541
&& !sendBudgetExhausted()
@@ -611,10 +618,15 @@ export async function prepareAdapterExchange(
611618
provider: route.provider,
612619
adapterName: transportState.activeAdapter.name,
613620
});
614-
const result = await rebuildAndRefetch("key-401");
615-
if ("failed" in result) return result.failed;
616-
upstreamResponse = result;
617-
}
621+
const result = await rebuildAndRefetch("key-401");
622+
if ("failed" in result) return result.failed;
623+
upstreamResponse = result;
624+
// A recovery refetch can itself die on an ambiguous pre-header reset, and the refusal
625+
// that answers it is a 429. Every arm below keys on 429, so letting it fall through
626+
// hands the marked refusal to the next waiting arm and replays the send it exists to
627+
// stop. Re-enter the loop guard instead, which returns it unchanged.
628+
if (isNonReplayableResponse(upstreamResponse)) continue recovery;
629+
}
618630

619631
// Same-target 429 wait-and-retry (opt-in `retryOn429`, issue #487). Codex never retries
620632
// 429 itself (it retries 5xx only), and single-key pools cannot use the failover below,
@@ -651,10 +663,13 @@ export async function prepareAdapterExchange(
651663
upstream.abort();
652664
return clientCancelledResponse();
653665
}
654-
const result = await rebuildAndRefetch("rate-limit-429");
655-
if ("failed" in result) return result.failed;
656-
upstreamResponse = result;
657-
}
666+
const result = await rebuildAndRefetch("rate-limit-429");
667+
if ("failed" in result) return result.failed;
668+
upstreamResponse = result;
669+
// The refusal is a 429 too: without this the while condition is still true and the
670+
// next configured attempt replays it on the same target.
671+
if (isNonReplayableResponse(upstreamResponse)) continue recovery;
672+
}
658673

659674
// Multi-key 429 failover: rotate to the next pool key (cooldown-aware) and retry the
660675
// SAME request once per remaining key. OAuth/forward providers and single-key pools
@@ -682,10 +697,13 @@ export async function prepareAdapterExchange(
682697
provider: route.provider,
683698
adapterName: transportState.activeAdapter.name,
684699
});
685-
const result = await rebuildAndRefetch("key-429");
686-
if ("failed" in result) return result.failed;
687-
upstreamResponse = result;
688-
}
700+
const result = await rebuildAndRefetch("key-429");
701+
if ("failed" in result) return result.failed;
702+
upstreamResponse = result;
703+
// Rotating on the refusal would also write a cooldown against a key that rate-limited
704+
// nothing, which outlives the request.
705+
if (isNonReplayableResponse(upstreamResponse)) continue recovery;
706+
}
689707

690708
// Opt-in Anthropic OAuth account pool (#294): cool the failed account and retry
691709
// with another eligible OAuth account (bounded per request). Disabled by default.
@@ -718,10 +736,11 @@ export async function prepareAdapterExchange(
718736
);
719737
sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel);
720738
recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name);
721-
const result = await rebuildAndRefetch("anthropic-oauth-429");
722-
if ("failed" in result) return result.failed;
723-
upstreamResponse = result;
724-
} catch {
739+
const result = await rebuildAndRefetch("anthropic-oauth-429");
740+
if ("failed" in result) return result.failed;
741+
upstreamResponse = result;
742+
if (isNonReplayableResponse(upstreamResponse)) continue recovery;
743+
} catch {
725744
break;
726745
}
727746
}
@@ -815,9 +834,12 @@ export async function prepareAdapterExchange(
815834
hop.permit?.release();
816835
return result.failed;
817836
}
818-
upstreamResponse = result;
819-
} catch {
820-
// A throw before the send — snapshot fetch, credential application, adapter
837+
upstreamResponse = result;
838+
// The hop's permit is already settled by the dispatch boundary above; continuing
839+
// only skips the remaining arms, it does not abandon a reservation.
840+
if (isNonReplayableResponse(upstreamResponse)) continue recovery;
841+
} catch {
842+
// A throw before the send — snapshot fetch, credential application, adapter
821843
// resolution — must hand the reservation back. Without this the ladder charges the
822844
// request for a send it never made, and a later recovery in the same request is
823845
// refused on an allowance nothing spent. release() is idempotent and a no-op once

‎src/server/responses/passthrough-dispatch.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ import { streamingContextOverflowResponse } from "./context-overflow";
101101
import {
102102
SendBudgetExhaustedError,
103103
fetchWithTransientRetry,
104-
applyUpstreamRecoveryInit,
105-
TRANSIENT_RETRY_MAX_ATTEMPTS,
106-
prepareSameTarget429Wait,
104+
applyUpstreamRecoveryInit,
105+
TRANSIENT_RETRY_MAX_ATTEMPTS,
106+
isNonReplayableResponse,
107+
prepareSameTarget429Wait,
107108
sleepWithAbort,
108109
} from "../../lib/upstream-retry";
109110
import { mapCodexAuthContextErrorToResponse } from "./codex-auth-error";
@@ -1137,9 +1138,13 @@ export async function preparePassthroughExchange(
11371138

11381139
// Native Responses returns before the generic adapter's OAuth rotation loop. Keep
11391140
// the same quorum, cooldown and request budget here, before any client bytes flow.
1140-
if (
1141-
upstreamResponse.status === 429
1142-
&& transportState.genericFailoverAccountId
1141+
if (
1142+
upstreamResponse.status === 429
1143+
// Not a provider rate limit when this proxy synthesized it for a refused reset
1144+
// replay; rotating accounts on it would re-send an inference that may already
1145+
// have run and would cool down an account that refused nothing.
1146+
&& !isNonReplayableResponse(upstreamResponse)
1147+
&& transportState.genericFailoverAccountId
11431148
&& transportState.genericFailovers < GENERIC_OAUTH_MAX_FAILOVERS_PER_REQUEST
11441149
&& isGenericOAuthFailoverEnabled(config, route.providerName)
11451150
) {
@@ -1193,9 +1198,10 @@ export async function preparePassthroughExchange(
11931198
// immediately with no same-key replay. Pre-stream only — nothing has been relayed yet, so
11941199
// the replay is lossless (same invariant as the recovery loop). Forward/OAuth providers
11951200
// keep their pool logic below (rateLimitRetryPolicyFor returns null for them).
1196-
while (
1197-
upstreamResponse.status === 429
1198-
&& rateLimitPolicy !== null
1201+
while (
1202+
upstreamResponse.status === 429
1203+
&& !isNonReplayableResponse(upstreamResponse)
1204+
&& rateLimitPolicy !== null
11991205
&& rateLimitRetries < rateLimitPolicy.attempts
12001206
// Checked here rather than inside the helper: prepareSameTarget429Wait releases the 429
12011207
// body, so a refusal discovered after the wait can no longer return the real rate-limit

‎src/server/responses/run-turn-execution.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ export async function executeResponsesRunTurn(
376376
stallTimeoutSec: config.stallTimeoutSec,
377377
hideThinkingSummary: parsed.options.hideThinkingSummary,
378378
declaredToolNames,
379+
enforceDeclaredToolNames: inboundWire !== "chat" && inboundWire !== "anthropic",
379380
toolParameterSchemas,
380381
...(options.onFirstOutput ? { onFirstOutput: options.onFirstOutput } : {}),
381382
...(routedCompaction ? { compaction: true } : {}),
@@ -446,6 +447,7 @@ export async function executeResponsesRunTurn(
446447
hideThinkingSummary: parsed.options.hideThinkingSummary,
447448
toolNsMap,
448449
declaredToolNames,
450+
enforceDeclaredToolNames: inboundWire !== "chat" && inboundWire !== "anthropic",
449451
toolParameterSchemas,
450452
freeformToolNames,
451453
toolSearchToolNames,

0 commit comments

Comments
 (0)