|
| 1 | +import { isNonReplayableResponse } from "../../lib/upstream-retry"; |
1 | 2 | import type { ResponsesRequestContext, ResponsesAdmissionState } from "./core-options"; |
2 | 3 | import type { PreparedResponsesRequest } from "./request-prepare"; |
3 | 4 | import type { ResponsesTransport } from "./request-transport"; |
@@ -524,11 +525,17 @@ export async function prepareAdapterExchange( |
524 | 525 | return { failed: formatErrorResponse(502, "upstream_error", msg) }; |
525 | 526 | } |
526 | 527 | }; |
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 |
532 | 539 | && transportState.sentOAuthSnapshot |
533 | 540 | && !oauth401ReplayAttempted |
534 | 541 | && !sendBudgetExhausted() |
@@ -611,10 +618,15 @@ export async function prepareAdapterExchange( |
611 | 618 | provider: route.provider, |
612 | 619 | adapterName: transportState.activeAdapter.name, |
613 | 620 | }); |
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 | + } |
618 | 630 |
|
619 | 631 | // Same-target 429 wait-and-retry (opt-in `retryOn429`, issue #487). Codex never retries |
620 | 632 | // 429 itself (it retries 5xx only), and single-key pools cannot use the failover below, |
@@ -651,10 +663,13 @@ export async function prepareAdapterExchange( |
651 | 663 | upstream.abort(); |
652 | 664 | return clientCancelledResponse(); |
653 | 665 | } |
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 | + } |
658 | 673 |
|
659 | 674 | // Multi-key 429 failover: rotate to the next pool key (cooldown-aware) and retry the |
660 | 675 | // SAME request once per remaining key. OAuth/forward providers and single-key pools |
@@ -682,10 +697,13 @@ export async function prepareAdapterExchange( |
682 | 697 | provider: route.provider, |
683 | 698 | adapterName: transportState.activeAdapter.name, |
684 | 699 | }); |
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 | + } |
689 | 707 |
|
690 | 708 | // Opt-in Anthropic OAuth account pool (#294): cool the failed account and retry |
691 | 709 | // with another eligible OAuth account (bounded per request). Disabled by default. |
@@ -718,10 +736,11 @@ export async function prepareAdapterExchange( |
718 | 736 | ); |
719 | 737 | sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel); |
720 | 738 | 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 { |
725 | 744 | break; |
726 | 745 | } |
727 | 746 | } |
@@ -815,9 +834,12 @@ export async function prepareAdapterExchange( |
815 | 834 | hop.permit?.release(); |
816 | 835 | return result.failed; |
817 | 836 | } |
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 |
821 | 843 | // resolution — must hand the reservation back. Without this the ladder charges the |
822 | 844 | // request for a send it never made, and a later recovery in the same request is |
823 | 845 | // refused on an allowance nothing spent. release() is idempotent and a no-op once |
|
0 commit comments