import { recordUpstreamProviderErrorDetail } from "../request-log-failure-detail"; import { providerAffinityGenerationIsCurrent } from "../../providers/api-key-selection"; import { clientFacingModelText } from "../../lib/error-destination"; import { responsesSpendTarget } from "./request-spend-target"; import { projectLocalModelRefusalLog } from "../request-log-failure-detail"; import { markProxyError, carryProxyError, isProxyError } from "../../lib/client-error-body"; import { codexAdapterBuildRefusal } from "./passthrough-build-error"; import { createAutoPauseRefreshErrorMapper, recoverAutoPausedAccount } from "./account-auto-pause-recovery"; import { wasAccountAutoPaused, wasPermanentAccountRefusal } from "../../oauth/account-auto-pause"; import { bindAnthropicRejectionResponse } from "../../adapters/anthropic-rejection-memo"; import { deferPhysicalSendReporter } from "./deferred-send-reporter"; import { usesApiKeyAccount } from "../../providers/label"; import { spendLedgerStorageErrorResponse } from "./spend-storage-error"; import { classifyAnthropic429 } from "../../oauth/anthropic-rate-limit-policy"; import { rotateAnthropicAccountOnResponseForInstance } from "../../oauth/anthropic-account-refusal"; import { authorizeResendForRecovery } from "../../lib/request-resend-gate"; import { ambiguousResendAllowanceFor, selfContainedResponsesBody } from "./reset-replay"; import { transientSendCapFor } from "./request-send-budget"; import { isNonReplayableResponse, isSameTargetRetryHold } from "../../lib/upstream-retry"; import { sanitizeNonReplayableUpstreamError } from "./non-replayable-error"; import { isLocalUpstream } from "../../lib/local-upstream"; import { resolveConnectTimeoutMs, resolveStallTimeoutMs } from "../../stall-timeout"; import type { ResponsesRequestContext, ResponsesAdmissionState } from "./core-options"; import type { PreparedResponsesRequest } from "./request-prepare"; import type { ResponsesTransport } from "./request-transport"; import type { ResponsesEffects } from "./response-effects"; import type { ResponsesSendBudget } from "./request-send-budget"; import { linkAbortSignal } from "./core-lifetime"; import type { AdapterRequest } from "../../adapters/base"; import type { AdapterEvent } from "../../types"; import { bridgeToResponsesSSE, buildResponseJSON, formatErrorResponse, formatUpstreamFailureResponse } from "../../bridge"; import { trackStreamLifetime } from "../lifecycle"; import { recordAdapterReasoning, recordAdapterTier, sealRequestAttemptIdentity, recordAttemptCredentialSource, } from "../request-log"; import { anthropicPoolCooldownResponse, clientCancelledResponse, readDisplaySafeErrorText } from "./core-errors"; import { isTranslatorBudgetExceededError } from "../../lib/translator-budget"; import { redactSecretString } from "../../lib/redact"; import { rewriteUpstreamPolicyRefusal } from "./policy-refusal"; import { carryPolicyCandidateRefusal } from "./policy-request-scope"; import { withProviderRequestSlot } from "../../providers/request-pacing"; import { providerFetch, fetchWithHeaderTimeout, safeHostLabel } from "./fetch-helpers"; import { isAnthropicBillingKeyPool, rotateAnthropicBillingKeyOnResponse } from "../../providers/anthropic-billing-recovery"; import { transientRetryPolicyFor, resetReplayPolicyFor, rateLimitRetryPolicyFor, hasKeyPoolFailover, rotateProviderTransportOn401, rateLimitRetryDelayMs, readQuotaResetAt, rotateProviderTransportOn429, } from "../../providers/key-failover"; import { fetchWithTransientRetry, fetchWithResetRetry, applyUpstreamRecoveryInit, SendBudgetExhaustedError, errorDestinationLabel, UpstreamRetryEvidenceError, prepareSameTarget429Wait, sleepWithAbort, } from "../../lib/upstream-retry"; import { describeUpstreamConnectFailure } from "./upstream-error"; import type { OpaqueBlobRecoveryGuard } from "./core-opaque-recovery"; import type { AttemptRecoveryKind } from "../../usage/log"; import type { OAuthAccessSnapshot } from "../../oauth"; import { publicOAuthErrorPresentation, OAuthAccountPausedError, OAuthLoginRequiredError, OAuthTokenRefreshBusyError, publicOAuthAuthenticationErrorMessage } from "../../oauth"; import { getAccountSet, markAccountNeedsReauthIfGeneration } from "../../oauth/store"; import { tryKiroAlternateAfterTerminalRefresh } from "../../oauth/kiro-terminal-failover"; import { classifyKiroRefusal } from "../../adapters/kiro-refusal"; import { classifyAntigravityRefusal } from "../../adapters/antigravity-refusal"; import { normalizeFinalKiroHttpError } from "../../adapters/kiro-retry"; import { noteKiroMonthlyRefusal } from "../../providers/kiro-usage"; import { persistKiroAccountState } from "../../providers/kiro-account-state-disk"; import { isXaiResponsesDestination, resolveProviderTransport } from "../../providers/xai-transport"; import { resolveCopilotApiBaseUrl } from "../../oauth/github-copilot"; import { resolveWireProtocolOverride } from "../adapter-resolve"; import { bindRouteReasoningReplayScope } from "./core-replay"; import { AnthropicAccountCooldownError, ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST, anthropicRoutingFor, formatAnthropicProviderForLog, } from "../../oauth/anthropic-routing"; import { isGenericOAuthFailoverEnabled, rotateGenericOAuthAccountOn429, rotateGenericOAuthAccountOnRefusal, quarantineKiroSuspendedAccount, failoverAccountSnapshot, rotateAntigravityAccountOnAuthRefusal, rotateAntigravityAccountOnUpstreamFault, } from "../../oauth/generic-account-failover"; import { attemptOpaqueBlobRecovery, consoleGoUploadRejectionBody, CONSOLE_GO_UPLOAD_RETRY_DELAY_MS, refusedReasoningEffortDowngrade, anthropicFastRefused, } from "./core-opaque-recovery"; import { createAnthropicThinkingRecovery } from "./anthropic-thinking-recovery"; import { shouldAttemptImageTierRetry } from "../image-retry"; import { isTransientConsoleGoUploadRejection } from "../../providers/opencode-zen-rate-limit"; import { formatAdapterUpstreamFailure } from "./adapter-failure-message"; import { consumeComboFailure } from "./core-combo-failure"; import { streamingContextOverflowResponse, jsonContextOverflowResponse, lateAttributionLimitResponse } from "./context-overflow"; import { OllamaLateAttributionBudgetError } from "../../adapters/ollama-native"; import { isFixedCodexAccount } from "./core-codex-account"; import { recordSubagentQuotaFailureForThreadSpawn } from "../../codex/subagent-model-fallback"; import { CYBER_POLICY_ERROR_CODE, } from "../../lib/errors"; import { resolveClientRetryAfter } from "../../lib/retry-after"; import { cancelBodyOnAbort } from "../../lib/abort"; import { createCodexAuthDispatchGuard, releaseCodexAuthContextProbeLease, unwrapUpstreamRetryEvidenceError } from "../../codex/auth-context"; import { isCanonicalOpenAiForwardProvider } from "../../providers/openai-tiers"; import { mapCodexAuthContextErrorToResponse } from "./codex-auth-error"; import { chargeWorkflowSends } from "../../lib/workflow-budget"; import { hasWorkflowSendObserver } from "../../lib/request-execution-budget"; import { isAntigravityAccessDeniedRefusal, isAntigravityValidationRefusal } from "./antigravity-validation-refusal"; import { createAntigravityModelDenialRecovery, recoverAntigravityAccount } from "./antigravity-model-denial-recovery"; import { adapterSendBudgetRefusal } from "../workflow-refusal"; import { AdmissionModelDeniedError, admissionModelDeniedResponse, projectAdmissionModelDeniedLog } from "../admission-model-scope"; /** One responsibility of the Responses request pipeline; state owners are explicit. */ export async function prepareAdapterExchange( requestContext: Pick, admissionState: ResponsesAdmissionState, requestState: Pick< PreparedResponsesRequest, | "parsed" | "toolBridgeMaps" | "translatorBudget" | "completedToolBlocks" | "selectedForwardHeaders" | "route" | "inboundWire" | "clientRequestedStream" | "subagentQuotaFailureModel" | "subagentFallbackAccountId" >, transportState: Pick< ResponsesTransport, | "activeAdapter" | "adapter" | "sameTargetRequest" | "sameTargetParsed" | "sameTargetToken" | "transportToken" | "oauthDispatch" | "recoverAutoPausedSelection" | "imageTierBias" | "oauth401ReplayAttempted" | "isOAuth401ReplayProvider" | "sentOAuthSnapshot" | "refreshResolvedOAuthSelection" | "replayOAuthCredentialSnapshot" | "invalidateSameTargetRequest" | "resolveSelectionAdapter" | "resolveCopilotSelection" | "copilotRefusalResponse" | "anthropicInstance" | "currentAnthropicRouteDecision" | "anthropicRouteDecision" | "anthropicPoolAccountId" | "anthropicPoolFailovers" | "anthropicSessionKey" | "commitResolvedOAuthSelection" | "genericFailoverAccountId" | "genericFailovers" | "genericFailoverLimit" | "applyFailoverSnapshot" | "noteRoutedAttemptSend" | "attemptSendDeferred" | "deferPhysicalAttribution" >, responseEffects: Pick, sendBudgetState: Pick< ResponsesSendBudget, | "adapterSendBudget" | "adapterDispatchBudget" | "noteAdapterPhysicalSend" | "noteAdapterRecoveryWithheld" | "remainingTransientSendBudget" | "adapterSendBudget" | "transientSendReporter" | "recoverySendAllowance" | "recoveryClassFor" | "sendBudgetExhausted" | "reserveCredentialHop" | "pendingHopPermit" | "workflowRootId" | "claimAmbiguousResend" | "sendsUsed" | "targetSendsUsed" | "initialSendAllowance" | "noteInitialDispatch" >, ) { const { options, config, logCtx, req } = requestContext; const { oauthDispatch, isOAuth401ReplayProvider, refreshResolvedOAuthSelection, invalidateSameTargetRequest, resolveSelectionAdapter, resolveCopilotSelection, copilotRefusalResponse, anthropicInstance, currentAnthropicRouteDecision, anthropicSessionKey, commitResolvedOAuthSelection, applyFailoverSnapshot, } = transportState; const { parsed, toolBridgeMaps, translatorBudget, route, inboundWire, clientRequestedStream, subagentQuotaFailureModel, } = requestState; const { cancelResponseCompletion, notifyResponseComplete, refreshRequestToolAliases } = responseEffects; const { adapterSendBudget, adapterDispatchBudget, noteAdapterPhysicalSend, noteAdapterRecoveryWithheld, remainingTransientSendBudget, transientSendReporter, recoverySendAllowance, recoveryClassFor, sendBudgetExhausted, reserveCredentialHop, claimAmbiguousResend, } = sendBudgetState; let selfContainedJudgment: boolean | undefined; // On the Antigravity direct lane the raw body carries only settings; judge the body the bridge // would have sent, built only if an ambiguous reset actually asks. const directRequest = options.directRequest; const requestIsSelfContained = (): boolean => selfContainedJudgment ??= selfContainedResponsesBody(directRequest ? { ...(parsed._rawBody as Record), ...directRequest.bridgeConversation() } : parsed._rawBody); const claimPreHeaderResend = (): boolean => authorizeResendForRecovery( "pre-header", "connection-reset", ambiguousResendAllowanceFor(route.provider, requestIsSelfContained, claimAmbiguousResend), ).allowed; const upstream = new AbortController(); const cleanupUpstreamAbort = linkAbortSignal(upstream, options.abortSignal); /** Contain replacement negotiation before adapter admission and release its upstream abort link. */ const resolveRotatedCopilotSelection = async (provider = route.provider): Promise => { try { await resolveCopilotSelection(parsed, false, provider); } catch (error) { cleanupUpstreamAbort(); upstream.abort(); if ((options.abortSignal ?? req.signal).aborted) return clientCancelledResponse(); return copilotRefusalResponse(error) ?? formatErrorResponse(502, "upstream_error", "GitHub Copilot Auto negotiation failed."); } }; // Where this exchange dials. Local upstreams (loopback / private / `.local` / `.lan`) are // operator-trusted and often CPU-bound, so an unset stall budget resolves to disabled for them. const localUpstream = isLocalUpstream(route.provider.baseUrl); // Header arrival and body silence are separate clocks (structure/transports/timeouts.md). const connectMs = resolveConnectTimeoutMs(config.connectTimeoutMs, { localUpstream }); const sendConnectMs = () => route.provider.authMode === "forward" ? resolveConnectTimeoutMs(config.connectTimeoutMs, { localUpstream: isLocalUpstream(route.provider.baseUrl) }) : 0; transportState.activeAdapter = transportState.adapter; const antigravityPoolActivated = route.providerName === "google-antigravity" && isGenericOAuthFailoverEnabled(config, route.providerName); const antigravityDenial = createAntigravityModelDenialRecovery(); // One immutable, body-safe outbound request per same-target sequence (URL, serialized body, // auth headers, generated compat headers). Same-target 429 replays reuse it verbatim; the // builder runs again only after a key/account/adapter rotation, an oauth refresh, or an // image-tier bias change (transportToken bump). `body` is always a serialized string, so // reuse is safe, and releaseBodyObservation is idempotent per build. let initialRequest: AdapterRequest | undefined; let inputTokenEstimate: number | undefined; // An adapter may know the turn needs no inference at all — Kiro's replayed history ending in a // delivered final answer. Answer it locally: no build (so no token estimate), no send (so // sendCount stays 0), and crucially no empty-completion guard, which treats an outputless // terminal as a failed turn and re-invokes the identical request. Routing this through the // ordinary event path would therefore reinstate the loop it exists to end. const localTerminal = transportState.activeAdapter.localTerminal?.(parsed); if (localTerminal) { logCtx.localTerminalReason = localTerminal.reason; // Mark the physical attempt too, not just the parent row. `finishRequestAttempt` finalizes the // attempt through the same estimated-provider path, so without this the row reads exact while // its own attempt still claims an estimate — the detailed accounting a maintainer actually // reads for a zero-send turn. if (logCtx.activeAttempt) logCtx.activeAttempt.locallyAnswered = true; cleanupUpstreamAbort(); upstream.abort(); const terminalEvents: AdapterEvent[] = [{ type: "done", endTurn: true, usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }, }]; if (parsed.stream) { const localSse = bridgeToResponsesSSE( (async function* () { yield* terminalEvents; })(), parsed._responseModelId ?? parsed.modelId, toolBridgeMaps.toolNsMap, toolBridgeMaps.freeformToolNames, toolBridgeMaps.toolSearchToolNames, cancelResponseCompletion, 2_000, { translatorBudget, onCompletedResponse: notifyResponseComplete, ...(options.forceEmptyResponseId ? { responseId: "" } : {}), ...(options.onFirstOutput ? { onFirstOutput: options.onFirstOutput } : {}), }, ); // Same lifetime tracking as every other streaming return in this function: the turn // admission lease is released when the body finishes or the client disconnects. Returning // the raw stream would hold a lease for a turn that already has all of its output. const localTurnAc = new AbortController(); return new Response( trackStreamLifetime(localSse, localTurnAc, undefined, options.turnAdmissionLease), { headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "Connection": "keep-alive", "X-Accel-Buffering": "no", }, }, ); } const json = buildResponseJSON(terminalEvents, parsed._responseModelId ?? parsed.modelId, { translatorBudget }); notifyResponseComplete(json); return new Response(JSON.stringify(json), { headers: { "Content-Type": "application/json" } }); } try { initialRequest = await transportState.activeAdapter.buildRequest(parsed, { headers: requestState.selectedForwardHeaders, completedToolBlocks: requestState.completedToolBlocks, providerName: route.providerName, translatorBudget, abortSignal: upstream.signal, }); refreshRequestToolAliases(initialRequest); recordAdapterReasoning(logCtx, initialRequest); recordAdapterTier(logCtx, initialRequest); inputTokenEstimate = typeof initialRequest.usageLog?.inputTokens === "number" ? initialRequest.usageLog.inputTokens : undefined; if (inputTokenEstimate !== undefined) logCtx.usageLogInputTokens = inputTokenEstimate; } catch (err) { // A throwing buildRequest never returned a request; if a post-build step threw, release // the serialized-body observation (idempotent) so the translator budget is not leaked. // The build runs after linkAbortSignal, so a failure must also tear the link down and // abort the upstream controller instead of escaping handleResponses unmapped. initialRequest?.releaseBodyObservation?.(); cleanupUpstreamAbort(); upstream.abort(); if (options.abortSignal?.aborted) return clientCancelledResponse(); if (err instanceof OllamaLateAttributionBudgetError) return lateAttributionLimitResponse(); // An adapter can also throw the shared translator budget while sizing generated // request content (e.g. Ollama late-tool attribution); keep its established 413. if (isTranslatorBudgetExceededError(err)) { return formatErrorResponse(413, "request_too_large", "request translation buffer exceeded the safe limit", { code: "translation_buffer_limit", }); } const authRefusal = codexAdapterBuildRefusal(err, admissionState.authCtx, route.codexAccountNamespace); if (authRefusal) return authRefusal; const msg = err instanceof Error ? err.message : String(err); return formatErrorResponse(400, "invalid_request_error", redactSecretString(msg)); } // The catch path above always returns, so the request is definitely assigned here. // Capture it in a const so the fetch callbacks read a narrowed, immutable value // (TypeScript drops narrowing for a `let` captured by a nested function). const builtInitialRequest = initialRequest; transportState.sameTargetRequest = builtInitialRequest; transportState.sameTargetParsed = parsed; transportState.sameTargetToken = transportState.transportToken; /** * Invalidate the same-target request cache. Every credential/adapter/parsed mutation MUST * go through here: the cache keys on `parsed` REFERENCE identity, so an in-place mutation * is invisible to it and a missed bump would replay a request built with a stale key. */ const initialRecovery = route.providerName === "github-copilot" ? sendBudgetState.pendingHopPermit?.sendClass === "auth-recovery" ? "oauth-account-429" : transportState.oauth401ReplayAttempted ? "oauth-401" : undefined : undefined; let upstreamResponse: Response; // Preserve bounded terminal bytes without retaining the refused send's pacing lease. // Reading the owned body to EOF (or cancelling on its bound) frees capacity BEFORE // replacement admission; keeping that live body would wait on its own concurrency slot. const retainFailureResponse = async (): Promise => { const original = upstreamResponse; const text = await readDisplaySafeErrorText(original, upstream.signal, ""); const headers = new Headers(original.headers); headers.delete("content-length"); headers.delete("content-encoding"); upstreamResponse = carryProxyError(original, new Response(text, { status: original.status, statusText: original.statusText, headers })); return upstreamResponse; }; const autoPauseEnabled = config.providers[route.providerName]?.accountAutoPause?.enabled === true; const billingPoolRequest = isAnthropicBillingKeyPool(route.provider); let billingRecoveryActive = false; const genericRefetchCountsSends = () => !!sendBudgetState.workflowRootId || options.comboAttempt === true || sendBudgetState.adapterSendBudget?.spendEnforced === true || transientRetryPolicyFor(route.provider) !== null || resetReplayPolicyFor(route.provider) !== null || billingPoolRequest || billingRecoveryActive || autoPauseEnabled; let handoffPermit: ResponsesSendBudget["pendingHopPermit"]; try { if (transportState.activeAdapter.fetchResponse) { transportState.noteRoutedAttemptSend(inputTokenEstimate, initialRecovery); const producer = adapterDispatchBudget?.beginSpendProducer?.(); try { upstreamResponse = await withProviderRequestSlot(route.providerName, route.provider, route.modelId, upstream.signal, pacingSlot => transportState.activeAdapter.fetchResponse!(builtInitialRequest, { kiroPreferAccountFailover: route.providerName === "kiro" && isGenericOAuthFailoverEnabled(config, "kiro"), abortSignal: upstream.signal, timeoutMs: sendConnectMs(), sendBudget: adapterDispatchBudget, onPhysicalSend: send => { antigravityDenial.beforeSend(); noteAdapterPhysicalSend(inputTokenEstimate, send); }, onRecoveryWithheld: noteAdapterRecoveryWithheld, stream: parsed.stream, executor: providerFetch(route.provider, options.codexWsRuntimeIdentity, { pacingSlotAcquired: true, pacingSlot, dispatchOverride: oauthDispatch(builtInitialRequest), providerName: route.providerName, modelId: route.modelId, beforeDispatch: isCanonicalOpenAiForwardProvider(route.provider) ? createCodexAuthDispatchGuard(admissionState.authCtx, options.codexAuthPolicy ?? config, route.modelId, options.admission, options.visionDescribeTerminal === true) : undefined, }), })); } finally { producer?.close(); } } else { // #1851 scope guard: transient-5xx retry on this generic adapter path is opt-in for // direct Google AI Studio only (Vertex/Antigravity use fetchResponse above). Other // adapters keep reset-only retry so combo failover still hops on the first 5xx // instead of burning ~1.2s of same-target retries per hop. // #2643: an opted-in key-auth openai-chat provider also gets transient-5xx retry. The // legacy direct-Google exception is preserved exactly; every other adapter still keeps // reset-only semantics so combo failover hops on the first 5xx. const transientPolicy = transientRetryPolicyFor(route.provider); const resetPolicy = resetReplayPolicyFor(route.provider); // Rebook a native Copilot hop against the negotiated destination before dispatch. // Raw externally counted permits settle through the retry helper's send reporter. if (route.providerName === "github-copilot" && adapterSendBudget && sendBudgetState.pendingHopPermit && sendBudgetState.pendingHopPermit.sendClass === "auth-recovery") { const original = sendBudgetState.pendingHopPermit; sendBudgetState.pendingHopPermit = undefined; original.release(); const decision = adapterSendBudget.reserveDispatch({ sendClass: original.sendClass, targetKey: `${route.providerName}|${builtInitialRequest.url}|${route.modelId}`, countedExternally: true, }); if (!decision.allowed) throw new SendBudgetExhaustedError(errorDestinationLabel(builtInitialRequest.url)); handoffPermit = decision.permit; } const compactPrepaid = handoffPermit ?? (options.compactionRecoveryAttempted ? sendBudgetState.pendingHopPermit : undefined); if (compactPrepaid) sendBudgetState.pendingHopPermit = undefined; let compactPrepaidUsed = false; // A combo-owned or compaction-prepaid send is settled by the physical-dispatch receipt // when spend enforcement is off. Enforced spend and direct requests keep the shared // physical-send reporter, which already owns the permit lifecycle there. // Stable per request: a Combo or compaction permit comes from `reserveDispatch`, which starts // (and freezes) the spend policy first, so `spendEnforced` cannot change before dispatch. const receiptMode = !sendBudgetState.adapterSendBudget?.spendEnforced && Boolean(options.comboInitialSend || compactPrepaid); // Combo and emergency compaction admission already book this target's first send. // Its configured initial ceiling is target-local; the shared remainder below still // accounts for earlier targets without deducting their sends from this target twice. const initialSendCap = transientPolicy || resetPolicy || (options.comboAttempt && route.provider.adapter === "google") ? transientSendCapFor(transientPolicy?.attempts, (options.comboAttempt || compactPrepaid) ? 0 : sendBudgetState.sendsUsed) : 1; const fetchWithRetryPolicy = (route.provider.adapter === "google" || transientPolicy) ? fetchWithTransientRetry : fetchWithResetRetry; const initialReporter = transientSendReporter(compactPrepaid ?? options.comboDispatchPermit); const deferredInitial = (billingPoolRequest || route.providerName === "github-copilot" && route.provider.authMode !== "forward") && !receiptMode ? deferPhysicalSendReporter(initialReporter) : undefined; try { upstreamResponse = await fetchWithRetryPolicy( recovery => { // Unconfigured generic sends may omit the counting reporter. Capture policy // independently so a later recovery still belongs to this request's start. sendBudgetState.adapterSendBudget?.startRequest?.(responsesSpendTarget(logCtx, route.providerName)); // Receipt mode books the physical send at executor admission instead of here. if (!deferredInitial && !receiptMode && compactPrepaid && !compactPrepaidUsed) { if (!compactPrepaid.use()) throw new SendBudgetExhaustedError(errorDestinationLabel(builtInitialRequest.url)); compactPrepaidUsed = true; } // Key transport keeps this metadata pending until it binds the actual selection. transportState.noteRoutedAttemptSend(inputTokenEstimate, recovery ?? initialRecovery); return fetchWithHeaderTimeout(builtInitialRequest.url, applyUpstreamRecoveryInit({ method: builtInitialRequest.method, headers: builtInitialRequest.headers, body: builtInitialRequest.body, }, recovery), upstream.signal, sendConnectMs(), parsed.stream, providerFetch(route.provider, options.codexWsRuntimeIdentity, { dispatchOverride: oauthDispatch(builtInitialRequest), providerName: route.providerName, modelId: route.modelId, onPhysicalDispatch: () => { if (deferredInitial) { if (!deferredInitial.beforeDispatch()) throw new SendBudgetExhaustedError(errorDestinationLabel(builtInitialRequest.url)); if (compactPrepaid && !compactPrepaidUsed) { if (!compactPrepaid.use()) throw new SendBudgetExhaustedError(errorDestinationLabel(builtInitialRequest.url)); compactPrepaidUsed = true; } } // Preserve the unconfigured direct-adapter accounting contract; Combo admission // and explicit retry/compaction budgets require a receipt for their booked send. if (receiptMode) { sendBudgetState.noteInitialDispatch(compactPrepaid && !compactPrepaidUsed ? compactPrepaid : undefined); compactPrepaidUsed = true; } }, beforeDispatch: isCanonicalOpenAiForwardProvider(route.provider) ? createCodexAuthDispatchGuard(admissionState.authCtx, options.codexAuthPolicy ?? config, route.modelId, options.admission, options.visionDescribeTerminal === true) : undefined, })); }, { abortSignal: upstream.signal, label: safeHostLabel(builtInitialRequest.url), destination: errorDestinationLabel(builtInitialRequest.url), claimAmbiguousResend: claimPreHeaderResend, ...(sendBudgetState.workflowRootId || sendBudgetState.adapterSendBudget?.spendEnforced || options.comboDispatchPermit || transientPolicy || resetPolicy || compactPrepaid || options.comboAttempt || billingPoolRequest || autoPauseEnabled ? { // A pending compaction permit already booked this leg's first physical send. ...(transientPolicy || resetPolicy || compactPrepaid || options.comboAttempt || autoPauseEnabled ? { attempts: Math.min(initialSendCap, sendBudgetState.initialSendAllowance(initialSendCap) + (compactPrepaid ? 1 : 0)), } : {}), // Reporter path: direct requests and enforced spend. Receipt-mode sends are // settled by `onPhysicalDispatch`, so a reporter here would count them twice. ...(receiptMode ? {} : { onSendsConsumed: deferredInitial?.report ?? initialReporter }), } : {}), }, ); } finally { compactPrepaid?.release(); } } } catch (err) { cleanupUpstreamAbort(); upstream.abort(); if (options.abortSignal?.aborted) return clientCancelledResponse(); const storageRefusal = spendLedgerStorageErrorResponse(err, logCtx); if (storageRefusal) { releaseCodexAuthContextProbeLease(admissionState.authCtx); return storageRefusal; } const refusal = err instanceof UpstreamRetryEvidenceError ? err.cause : err; if (refusal instanceof AdmissionModelDeniedError) { releaseCodexAuthContextProbeLease(admissionState.authCtx); return admissionModelDeniedResponse(projectAdmissionModelDeniedLog(refusal, logCtx)); } const codexRefusal = mapCodexAuthContextErrorToResponse(unwrapUpstreamRetryEvidenceError(err), { now: Date.now(), accountSelector: route.codexAccountNamespace, }); if (codexRefusal) { releaseCodexAuthContextProbeLease(admissionState.authCtx); return codexRefusal; } // A pause committed during pacing is local admission policy, not a failed upstream. if (refusal instanceof OAuthAccountPausedError) { return markProxyError(formatErrorResponse(403, "permission_error", publicOAuthAuthenticationErrorMessage(refusal)), publicOAuthErrorPresentation(refusal)); } if (refusal instanceof OAuthLoginRequiredError) { return markProxyError(formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(refusal)), publicOAuthErrorPresentation(refusal)); } if (refusal instanceof AnthropicAccountCooldownError) { return anthropicPoolCooldownResponse(refusal, { comboAttempt: options.comboAttempt, logCtx }); } // A budget refusal is a decision this process made, not an upstream fault. Reporting it as // 502 does more than mislabel it: the Codex client retries 5xx and does not retry a 429, so // blaming the provider makes the caller send the whole turn again -- the amplification this // budget exists to stop. The passthrough path has answered 429 here since #4546. if (err instanceof SendBudgetExhaustedError) { return adapterSendBudgetRefusal(logCtx, sendBudgetState.workflowRootId, err.message); } const msg = describeUpstreamConnectFailure(err, connectMs); return formatErrorResponse(502, "upstream_error", msg); } finally { handoffPermit?.release(); builtInitialRequest.releaseBodyObservation?.(); } // Same-target 429 retry budget is per REQUEST: it lives OUTSIDE the recovery loop (so a 413/401 // replay that comes back 429 cannot silently re-arm a fresh budget) and is SHARED with the // terminal-guard continuation below, so the main loop + one continuation can never exceed // `attempts` same-key replays in total (bounded per request). const rateLimitPolicy = rateLimitRetryPolicyFor(route.provider); let rateLimitRetries = 0; // Shared with the terminal-guard continuation below: an image-tier reduction that let the // main request clear a 413 must not be forgotten on the very next continuation build. // Recovery helpers also serve initial HTTP-200 adapter errors during delivery. // Recovery loop: multi-key 429 failover + at most ONE opaque-state rebuild and ONE // anthropic 413 tightened retry // (devlog/260714_image_normalization_pipeline/030). One mutable activeAdapter serves // both paths so a 429→413 sequence never rebuilds against a stale pre-rotation // adapter, and imageTierBias — once armed — rides EVERY subsequent rebuild so a // 413→429 rotation cannot silently undo the tightening. let imageRetryAttempted = false; const opaqueBlobRecoveryGuard: OpaqueBlobRecoveryGuard = { attempted: false }; const thinkingSignatureRecovery = createAnthropicThinkingRecovery( parsed, upstream.signal, opaqueBlobRecoveryGuard, () => !sendBudgetExhausted(), logCtx, ); // Console Go answers a transient 400 "Invalid upload request." for bodies it accepts // moments later; at most one byte-identical replay is allowed per request. const consoleGoUploadRetryGuard: { attempted: boolean } = { attempted: false }; let oauth401ReplayAttempted = transportState.oauth401ReplayAttempted; let antigravityAuthRotationAttempted = false; let antigravityFaultRotationAttempted = false; // Direct requests get one reasoning-effort downgrade. Combo candidates may walk down the // published ladder one refused rung at a time; the request send budget bounds those retries. const reasoningEffortDowngradeGuard: { attempted: boolean } = { attempted: false }; // At most one Anthropic fast-mode downgrade per request, for the same reason. const anthropicFastDowngradeGuard: { attempted: boolean } = { attempted: false }; /** * Rebuild the request from the current parsed input (and any image-tier bias) and refetch * it once, tagging the attempt with the given recovery kind. Rebuilds are deterministic * for the same parsed request, so same-target replays stay byte-identical. */ const rebuildAndRefetch = async ( recovery: AttemptRecoveryKind, /** * Called at the dispatch boundary — after the request is rebuilt and shaped, immediately * before the send. A caller holding a reserved hop confirms it here rather than before the * rebuild, because a build failure returns `{ failed }` without ever reaching the wire and * a permit confirmed earlier would keep the charge for a send that never happened. */ onDispatch?: () => void, preserveFailureResponse?: Response, ): Promise => { let replacementAdmitted = false, dispatchConfirmed = false; // Only budgets without a physical observer need this legacy workflow report. let fastDowngradeWorkflowCharged = false; const chargeFastDowngradeWorkflowSend = (): void => { if (recovery !== "anthropic-fast-downgrade" || fastDowngradeWorkflowCharged) return; fastDowngradeWorkflowCharged = true; if (!sendBudgetState.adapterSendBudget || !hasWorkflowSendObserver(sendBudgetState.adapterSendBudget)) chargeWorkflowSends(sendBudgetState.workflowRootId, 1); }; let retryRequest: AdapterRequest; if (transportState.sameTargetRequest !== undefined && transportState.sameTargetParsed === parsed && transportState.sameTargetToken === transportState.transportToken) { // Same target (key/adapter/parsed/tier unchanged): replay the exact cached request. retryRequest = transportState.sameTargetRequest; } else { try { retryRequest = await transportState.activeAdapter.buildRequest(parsed, { headers: requestState.selectedForwardHeaders, completedToolBlocks: requestState.completedToolBlocks, providerName: route.providerName, translatorBudget, abortSignal: upstream.signal, ...(transportState.imageTierBias > 0 ? { imageTierBias: transportState.imageTierBias } : {}), }); recordAdapterReasoning(logCtx, retryRequest); recordAdapterTier(logCtx, retryRequest); } catch (err) { const authRefusal = options.abortSignal?.aborted ? undefined : codexAdapterBuildRefusal(err, admissionState.authCtx, route.codexAccountNamespace, () => { cleanupUpstreamAbort(); upstream.abort(); }); if (authRefusal) return { failed: authRefusal }; // A rotated credential that cannot be shaped keeps the refusal it was meant to // replace. The image-tier and reasoning-effort rebuilds are different: the request itself // failed to shape, and their established build-error contracts stay authoritative. if (preserveFailureResponse && recovery !== "image-413" && recovery !== "reasoning-effort-downgrade" && !options.abortSignal?.aborted) { return { failed: preserveFailureResponse }; } // A rotated/rebuilt adapter build failure is a request-shaping error, not an // upstream connect failure: tear the abort link down and map it as 400. cleanupUpstreamAbort(); upstream.abort(); if (options.abortSignal?.aborted) return { failed: clientCancelledResponse() }; if (err instanceof OllamaLateAttributionBudgetError) { return { failed: lateAttributionLimitResponse() }; } if (isTranslatorBudgetExceededError(err)) { return { failed: formatErrorResponse(413, "request_too_large", "request translation buffer exceeded the safe limit", { code: "translation_buffer_limit", }) }; } const msg = err instanceof Error ? err.message : String(err); return { failed: formatErrorResponse(400, "invalid_request_error", redactSecretString(msg)) }; } transportState.sameTargetRequest = retryRequest; transportState.sameTargetParsed = parsed; transportState.sameTargetToken = transportState.transportToken; } refreshRequestToolAliases(retryRequest); const retryEstimate = typeof retryRequest.usageLog?.inputTokens === "number" ? retryRequest.usageLog.inputTokens : undefined; if (retryEstimate !== undefined) logCtx.usageLogInputTokens = retryEstimate; if (!transportState.deferPhysicalAttribution) { logCtx.providerAdapter = transportState.activeAdapter.name; sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel); } recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name); try { try { if (transportState.activeAdapter.fetchResponse) { transportState.noteRoutedAttemptSend(retryEstimate, recovery); const producer = adapterDispatchBudget?.beginSpendProducer?.(); try { return await withProviderRequestSlot(route.providerName, route.provider, route.modelId, upstream.signal, pacingSlot => { // The dispatch boundary is HERE, not before the pacing wait: that wait can reject for // an abort, a saturated queue, an expired slot or a removed provider, and none of // those reach the wire. Confirming earlier would hold the charge for a send that the // pacer refused. if (!dispatchConfirmed) { onDispatch?.(); dispatchConfirmed = true; } return transportState.activeAdapter.fetchResponse!(retryRequest, { kiroPreferAccountFailover: route.providerName === "kiro" && isGenericOAuthFailoverEnabled(config, "kiro"), abortSignal: upstream.signal, timeoutMs: sendConnectMs(), sendBudget: adapterDispatchBudget, onPhysicalSend: send => { if (preserveFailureResponse) replacementAdmitted = true; antigravityDenial.beforeSend(); noteAdapterPhysicalSend(retryEstimate, send); chargeFastDowngradeWorkflowSend(); }, onRecoveryWithheld: noteAdapterRecoveryWithheld, stream: parsed.stream, executor: providerFetch(route.provider, options.codexWsRuntimeIdentity, { pacingSlotAcquired: true, pacingSlot, dispatchOverride: oauthDispatch(retryRequest), providerName: route.providerName, modelId: route.modelId, beforeDispatch: isCanonicalOpenAiForwardProvider(route.provider) ? createCodexAuthDispatchGuard(admissionState.authCtx, options.codexAuthPolicy ?? config, route.modelId, options.admission, options.visionDescribeTerminal === true) : undefined, }), }); }); } finally { producer?.close(); } } // #2643 review: this leg used to call fetchWithHeaderTimeout directly, so an // opted-in provider's transient-5xx policy applied to the initial send and to // native chat but was silently bypassed here — a 429 that recovered into a // retryable 503 got no retry on the Responses path. Route it through the same // selection, and pass what is LEFT of the request-scoped budget rather than a // fresh one, so a recovery loop cannot multiply total upstream sends. const refetchTransientPolicy = transientRetryPolicyFor(route.provider); const refetchWithPolicy = (route.provider.adapter === "google" || refetchTransientPolicy) ? fetchWithTransientRetry : fetchWithResetRetry; const helperCountsSends = genericRefetchCountsSends(); const prepaid = sendBudgetState.pendingHopPermit; const configuredTotal = refetchTransientPolicy?.attempts; const refetchCap = transientSendCapFor(configuredTotal, sendBudgetState.targetSendsUsed); // An exact total includes a booked hop, even when it consumed the last base slot. // Keep that funded send while forbidding the final reserve from widening the total. const prepaidLastSlot = helperCountsSends && configuredTotal !== undefined && prepaid && refetchCap > 0 && remainingTransientSendBudget(refetchCap) === 0; if (prepaidLastSlot) sendBudgetState.pendingHopPermit = undefined; const refetchAllowance = helperCountsSends ? prepaidLastSlot ? { attempts: 1, permit: prepaid } : recoverySendAllowance( refetchCap, recoveryClassFor(recovery), `${route.providerName}|${route.modelId}|${recovery}`, { allowFinalRecoveryReserve: configuredTotal === undefined }, ) : undefined; let refetchPermitUsed = false; const refetchReporter = refetchAllowance ? transientSendReporter(refetchAllowance.permit) : undefined; const deferredRefetch = (billingPoolRequest || options.comboAttempt || route.providerName === "github-copilot" && route.provider.authMode !== "forward") && refetchReporter ? deferPhysicalSendReporter(refetchReporter) : undefined; const pendingKeyMetadata = deferredRefetch && transportState.attemptSendDeferred(); try { return await refetchWithPolicy( recoveryKind => { sendBudgetState.adapterSendBudget?.startRequest?.(responsesSpendTarget(logCtx, route.providerName)); // Prepare key metadata before the guarded transport commits its attempt; // counters and permits still wait for the physical executor after pacing. if (pendingKeyMetadata) transportState.noteRoutedAttemptSend(retryEstimate, recoveryKind ?? recovery); const confirmDispatch = () => { if (refetchAllowance?.permit && !refetchPermitUsed && (!onDispatch || refetchAllowance.permit !== prepaid)) { if (!refetchAllowance.permit.use()) throw new SendBudgetExhaustedError(errorDestinationLabel(retryRequest.url)); refetchPermitUsed = true; } if (!pendingKeyMetadata) transportState.noteRoutedAttemptSend(retryEstimate, recoveryKind ?? recovery); // Counted Combo/pool sends confirm at the physical executor; legacy // helper paths retain their existing thunk admission boundary. if (!dispatchConfirmed) { onDispatch?.(); dispatchConfirmed = true; } if (preserveFailureResponse) replacementAdmitted = true; if (!refetchAllowance) chargeFastDowngradeWorkflowSend(); }; if (!deferredRefetch) confirmDispatch(); return fetchWithHeaderTimeout(retryRequest.url, applyUpstreamRecoveryInit({ method: retryRequest.method, headers: retryRequest.headers, body: retryRequest.body, }, recoveryKind), upstream.signal, sendConnectMs(), parsed.stream, providerFetch(route.provider, options.codexWsRuntimeIdentity, { dispatchOverride: oauthDispatch(retryRequest), onPhysicalDispatch: deferredRefetch ? () => { if (!deferredRefetch.beforeDispatch()) throw new SendBudgetExhaustedError(errorDestinationLabel(retryRequest.url)); confirmDispatch(); } : undefined, providerName: route.providerName, modelId: route.modelId, beforeDispatch: isCanonicalOpenAiForwardProvider(route.provider) ? createCodexAuthDispatchGuard(admissionState.authCtx, options.codexAuthPolicy ?? config, route.modelId, options.admission, options.visionDescribeTerminal === true) : undefined, })); }, { abortSignal: upstream.signal, label: safeHostLabel(retryRequest.url), destination: errorDestinationLabel(retryRequest.url), claimAmbiguousResend: claimPreHeaderResend, ...(refetchAllowance ? { attempts: refetchAllowance.attempts, onSendsConsumed: deferredRefetch?.report ?? refetchReporter, } : {}), }, ); } finally { // Refund only a reservation whose send never reached its admission boundary. // A used or externally settled permit ignores this. refetchAllowance?.permit?.release(); } } finally { retryRequest.releaseBodyObservation?.(); } } catch (err) { const storageRefusal = !options.abortSignal?.aborted && spendLedgerStorageErrorResponse(err, logCtx); if (storageRefusal) { cleanupUpstreamAbort(); upstream.abort(); releaseCodexAuthContextProbeLease(admissionState.authCtx); return { failed: storageRefusal }; } const scopeRefusal = unwrapUpstreamRetryEvidenceError(err); if (!options.abortSignal?.aborted && scopeRefusal instanceof AdmissionModelDeniedError) { cleanupUpstreamAbort(); upstream.abort(); releaseCodexAuthContextProbeLease(admissionState.authCtx); return { failed: admissionModelDeniedResponse(projectAdmissionModelDeniedLog(scopeRefusal, logCtx)) }; } const codexRefusal = !options.abortSignal?.aborted && mapCodexAuthContextErrorToResponse(unwrapUpstreamRetryEvidenceError(err), { now: Date.now(), accountSelector: route.codexAccountNamespace, }); if (codexRefusal) { cleanupUpstreamAbort(); upstream.abort(); releaseCodexAuthContextProbeLease(admissionState.authCtx); return { failed: codexRefusal }; } if (preserveFailureResponse && (recovery !== "reasoning-effort-downgrade" || err instanceof SendBudgetExhaustedError) && !replacementAdmitted && !options.abortSignal?.aborted) return { failed: preserveFailureResponse }; cleanupUpstreamAbort(); upstream.abort(); if (options.abortSignal?.aborted) { return { failed: clientCancelledResponse() }; } const refusal = err instanceof UpstreamRetryEvidenceError ? err.cause : err; if (refusal instanceof OAuthAccountPausedError) { return { failed: markProxyError(formatErrorResponse(403, "permission_error", publicOAuthAuthenticationErrorMessage(refusal)), publicOAuthErrorPresentation(refusal)) }; } if (refusal instanceof OAuthLoginRequiredError) { return { failed: markProxyError(formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(refusal)), publicOAuthErrorPresentation(refusal)) }; } if (refusal instanceof AnthropicAccountCooldownError) { return { failed: anthropicPoolCooldownResponse(refusal, { comboAttempt: options.comboAttempt, logCtx }) }; } // Same rule on the recovery leg: the ladder refused to send again, so the answer names // this proxy rather than the provider it never reached. if (err instanceof SendBudgetExhaustedError) { return { failed: adapterSendBudgetRefusal(logCtx, sendBudgetState.workflowRootId, err.message) }; } const msg = describeUpstreamConnectFailure(err, connectMs); return { failed: formatErrorResponse(502, "upstream_error", msg) }; } }; const refetchThinkingSignature = () => { invalidateSameTargetRequest(); return rebuildAndRefetch("opaque-blob-rejection"); }; if (!upstreamResponse.ok) { const rotateAntigravityAuth = async ( failedResponse: Response, recovery: AttemptRecoveryKind, // An account id (model-404 recovery already chose it) or a selector; the 403 refusal default. pick: string | ((sent: { accountId: string; generation: string }) => string | null) = sent => rotateAntigravityAccountOnAuthRefusal(antigravityPoolActivated, sent.accountId, sent.generation, route.modelId), ): Promise => { const sent = transportState.replayOAuthCredentialSnapshot ?? transportState.sentOAuthSnapshot; if (!antigravityPoolActivated || !sent || transportState.genericFailovers >= transportState.genericFailoverLimit) return null; const nextId = typeof pick === "string" ? pick : pick(sent); if (!nextId) return null; const adapterOwnsDispatch = transportState.activeAdapter.fetchResponse !== undefined; return recoverAntigravityAccount({ nextId, failedResponse, adapterOwnsDispatch, reserve: () => reserveCredentialHop("auth-recovery", `${route.providerName}|${route.modelId}|antigravity-auth`, !adapterOwnsDispatch && genericRefetchCountsSends()), applyFailoverSnapshot, setPermit: permit => { sendBudgetState.pendingHopPermit = permit; }, complete: () => { transportState.genericFailovers += 1; }, refetch: onDispatch => rebuildAndRefetch(recovery, onDispatch, failedResponse), prepare: () => { invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name, oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot, }); sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel); recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name); }, }); }; // Keep recovery kinds in sync with the native Responses `passthroughRecovery:` loop above. recovery: for (;;) { const modelRotation = await antigravityDenial.recover(upstreamResponse, transportState.replayOAuthCredentialSnapshot ?? transportState.sentOAuthSnapshot, { config, model: route.modelId, enabled: antigravityPoolActivated && transportState.activeAdapter.name === "google", remaining: transportState.genericFailovers < transportState.genericFailoverLimit, signal: options.abortSignal, rotate: id => rotateAntigravityAuth(upstreamResponse, "oauth-account-403", id), }); if (modelRotation) { upstreamResponse = modelRotation; continue recovery; } if (wasAccountAutoPaused(upstreamResponse) && route.provider.authMode !== "forward") { const recovered = await recoverAutoPausedAccount({ requestKey: transportState, reserve: reserveCredentialHop, remaining: transientSendCapFor(transientRetryPolicyFor(route.provider)?.attempts, sendBudgetState.targetSendsUsed), mapRefreshError: createAutoPauseRefreshErrorMapper(upstreamResponse, upstream, cleanupUpstreamAbort, copilotRefusalResponse, () => (options.abortSignal ?? req.signal).aborted, clientCancelledResponse), refresh: () => transportState.recoverAutoPausedSelection(parsed), refetch: () => rebuildAndRefetch("oauth-account-429"), setPermit: permit => { sendBudgetState.pendingHopPermit = permit; } }); if (!recovered) break; if ("failed" in recovered) return recovered.failed; upstreamResponse = recovered; continue recovery; } // Preserve the terminal verdict through adapter and combo error formatting. // This also covers a reset reached by a 401/429/413 recovery refetch. if (isNonReplayableResponse(upstreamResponse)) { try { return await sanitizeNonReplayableUpstreamError(upstreamResponse, upstream.signal); } finally { cleanupUpstreamAbort(); } } bindAnthropicRejectionResponse(transportState.sameTargetRequest, upstreamResponse); if (await thinkingSignatureRecovery.prepareHttp( upstreamResponse, transportState.activeAdapter.name, )) { try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* closed */ } invalidateSameTargetRequest(); const result = await rebuildAndRefetch("opaque-blob-rejection"); if ("failed" in result) return result.failed; upstreamResponse = result; continue recovery; } if ( upstreamResponse.status === 401 && !wasPermanentAccountRefusal(upstreamResponse) && isOAuth401ReplayProvider && transportState.sentOAuthSnapshot && !oauth401ReplayAttempted && !sendBudgetExhausted() ) { oauth401ReplayAttempted = transportState.oauth401ReplayAttempted = true; let refreshed: OAuthAccessSnapshot; try { refreshed = await refreshResolvedOAuthSelection(transportState.sentOAuthSnapshot); } catch (err) { const cancelled = options.abortSignal?.aborted || req.signal.aborted; if (cancelled || err instanceof OAuthTokenRefreshBusyError) { cleanupUpstreamAbort(); upstream.abort(); try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already closed */ } releaseCodexAuthContextProbeLease(admissionState.authCtx); return cancelled ? clientCancelledResponse() : markProxyError(formatErrorResponse(503, "server_error", publicOAuthAuthenticationErrorMessage(err), { retryAfter: "1" }), publicOAuthErrorPresentation(err)); } const failed = transportState.sentOAuthSnapshot; if (route.providerName === "google-antigravity" && !antigravityAuthRotationAttempted && err instanceof OAuthLoginRequiredError && failed && getAccountSet(route.providerName)?.accounts.some(row => row.id === failed.accountId && row.needsReauth === true)) { antigravityAuthRotationAttempted = true; const rotated = await rotateAntigravityAuth(upstreamResponse, "oauth-401"); if (rotated) { upstreamResponse = rotated; continue recovery; } } if (route.providerName === "kiro" && err instanceof OAuthLoginRequiredError && failed && transportState.genericFailovers < transportState.genericFailoverLimit) { const alternate = await tryKiroAlternateAfterTerminalRefresh(config, failed.accountId, failed.generation); if (alternate) { const adapterOwnsDispatch = transportState.activeAdapter.fetchResponse !== undefined; const hop = reserveCredentialHop("auth-recovery", `${route.providerName}|${route.modelId}|terminal-refresh-account`, !adapterOwnsDispatch && genericRefetchCountsSends()); if (hop.allowed) { try { const admitted = await applyFailoverSnapshot(alternate); if (admitted?.accountId === alternate.accountId) { transportState.genericFailovers += 1; invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name, oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot, }); sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel); recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name); sendBudgetState.pendingHopPermit = hop.permit; const result = await rebuildAndRefetch("oauth-account-429", () => { if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError(); }); if ("failed" in result) return result.failed; upstreamResponse = result; continue recovery; } } catch { /* Keep the original public authentication error. */ } finally { sendBudgetState.pendingHopPermit = undefined; hop.permit?.release(); } } } } cleanupUpstreamAbort(); if (err instanceof OAuthAccountPausedError) { return markProxyError(formatErrorResponse(403, "permission_error", publicOAuthAuthenticationErrorMessage(err)), publicOAuthErrorPresentation(err)); } return markProxyError(formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(err)), publicOAuthErrorPresentation(err)); } if (route.provider.googleMode === "cloud-code-assist" && !refreshed.projectId) { cleanupUpstreamAbort(); return markProxyError(formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(new Error("Cloud Code Assist project is required")))); } if (route.providerName === "kiro") { parsed._kiroAuthContext = { ...(refreshed.kiro ?? {}) }; } let refreshedProvider = resolveProviderTransport( route.providerName, { ...route.provider, apiKey: refreshed.accessToken, ...(refreshed.projectId ? { project: refreshed.projectId } : {}), }, parsed.options.promptCacheKey, route.providerName === "github-copilot" ? resolveCopilotApiBaseUrl(refreshed.apiBaseUrl) : undefined, ); const copilotRefusal = await resolveRotatedCopilotSelection(refreshedProvider); if (copilotRefusal) return copilotRefusal; refreshedProvider = route.provider; if (route.providerName !== "github-copilot") { transportState.sentOAuthSnapshot = refreshed; transportState.replayOAuthCredentialSnapshot = { accountId: refreshed.accountId, generation: refreshed.generation }; } invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, refreshedProvider, inboundWire, route.staticPolicy), config.cacheRetention, ); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: refreshedProvider, adapterName: transportState.activeAdapter.name, oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot, }); const result = await rebuildAndRefetch("oauth-401"); if ("failed" in result) return result.failed; upstreamResponse = result; continue recovery; } if (route.providerName === "google-antigravity" && upstreamResponse.status === 401 && !wasPermanentAccountRefusal(upstreamResponse) && oauth401ReplayAttempted && !antigravityAuthRotationAttempted && !isNonReplayableResponse(upstreamResponse)) { antigravityAuthRotationAttempted = true; const rotated = await rotateAntigravityAuth(upstreamResponse, "oauth-401"); if (rotated) { upstreamResponse = rotated; continue recovery; } } if (route.providerName === "google-antigravity" && upstreamResponse.status === 403 && transportState.activeAdapter.name === "google" && antigravityPoolActivated && !isNonReplayableResponse(upstreamResponse) && transportState.genericFailovers < transportState.genericFailoverLimit && (!antigravityAuthRotationAttempted && await isAntigravityValidationRefusal(upstreamResponse, options.abortSignal) // A plain access-denied 403 keeps rotating while siblings remain: each refused account is // cooled before the hop, so the ring advances and `genericFailoverLimit` bounds the sends. || await isAntigravityAccessDeniedRefusal(upstreamResponse, options.abortSignal))) { antigravityAuthRotationAttempted = true; const rotated = await rotateAntigravityAuth(upstreamResponse, "oauth-account-403"); if (rotated) { upstreamResponse = rotated; continue recovery; } } // A 502/503/504 that outlived the same-account ladder is replayed once on a live sibling: the // failing account is cooled briefly (not marked), and one extra ladder bounds the load a // pool-wide outage can add to a request. if (route.providerName === "google-antigravity" && [502, 503, 504].includes(upstreamResponse.status) && transportState.activeAdapter.name === "google" && antigravityPoolActivated && !antigravityFaultRotationAttempted && !isNonReplayableResponse(upstreamResponse) && transportState.genericFailovers < transportState.genericFailoverLimit) { antigravityFaultRotationAttempted = true; const rotated = await rotateAntigravityAuth(upstreamResponse, "transient-5xx", sent => rotateAntigravityAccountOnUpstreamFault(antigravityPoolActivated, sent.accountId, sent.generation, route.modelId)); if (rotated) { upstreamResponse = rotated; continue recovery; } } // Static API-key pools can recover a credential-scoped 401 without abandoning the // provider: one revoked or mistyped key says nothing about its siblings. OAuth providers // refresh above and never enter here — `hasKeyPoolFailover` rejects oauth/forward modes. // Runs after the OAuth replay so a refreshable token is never treated as a dead key. while (upstreamResponse.status === 401 && hasKeyPoolFailover(route.provider)) { const rotated = rotateProviderTransportOn401(config, route.providerName, route.provider, { now: Date.now(), attemptedKey: route.provider.apiKey, promptCacheKey: parsed.options.promptCacheKey, }); if (!rotated) break; if (billingRecoveryActive && (options.abortSignal?.aborted || upstream.signal.aborted)) { cleanupUpstreamAbort(); upstream.abort(); return clientCancelledResponse(); } const billingHop = (billingPoolRequest || billingRecoveryActive) ? reserveCredentialHop("auth-recovery", `${route.providerName}|${route.modelId}|key-401`, transportState.activeAdapter.fetchResponse === undefined) : undefined; if (billingHop && !billingHop.allowed) break; const failedResponse = billingPoolRequest || billingRecoveryActive ? await retainFailureResponse() : undefined; try { // Release the failed response's socket before retrying; unread bodies otherwise linger // until runtime cleanup (one per rotated key). if (!failedResponse) try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } route.provider = rotated; const selectionFailure = await resolveRotatedCopilotSelection(); if (selectionFailure) return selectionFailure; invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention, ); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name, }); if (billingHop) sendBudgetState.pendingHopPermit = billingHop.permit; const result = await rebuildAndRefetch("key-401", billingHop ? () => { if (billingHop.permit?.use() === false) throw new SendBudgetExhaustedError(); } : undefined, failedResponse); if ("failed" in result) { if (result.failed !== failedResponse) return result.failed; break; } if (failedResponse) { try { void failedResponse.body?.cancel().catch(() => {}); } catch { /* closed */ } } upstreamResponse = result; // A recovery refetch can itself die on an ambiguous pre-header reset, and the refusal // that answers it is a 429. Every arm below keys on 429, so letting it fall through // hands the marked refusal to the next waiting arm and replays the send it exists to // stop. Re-enter the loop guard instead, which returns it unchanged. if (isNonReplayableResponse(upstreamResponse)) continue recovery; } finally { if (billingHop) { sendBudgetState.pendingHopPermit = undefined; billingHop.permit?.release(); } } } while ((upstreamResponse.status === 400 || upstreamResponse.status === 402) && hasKeyPoolFailover(route.provider)) { const rotated = await rotateAnthropicBillingKeyOnResponse(upstreamResponse, config, route.providerName, route.provider, { signal: upstream.signal, promptCacheKey: parsed.options.promptCacheKey }); if (upstream.signal.aborted || options.abortSignal?.aborted) { cleanupUpstreamAbort(); upstream.abort(); return clientCancelledResponse(); } if (!rotated) break; billingRecoveryActive = true; const hop = reserveCredentialHop("auth-recovery", `${route.providerName}|${route.modelId}|key-401`, transportState.activeAdapter.fetchResponse === undefined); if (!hop.allowed) break; const failedResponse = await retainFailureResponse(); try { route.provider = rotated; invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name }); sendBudgetState.pendingHopPermit = hop.permit; const result = await rebuildAndRefetch("key-401", () => { if (hop.permit?.use() === false) throw new SendBudgetExhaustedError(); }, failedResponse); if ("failed" in result) { if (result.failed !== failedResponse) return result.failed; break; } try { void failedResponse.body?.cancel().catch(() => {}); } catch { /* closed */ } upstreamResponse = result; continue recovery; } finally { sendBudgetState.pendingHopPermit = undefined; hop.permit?.release(); } } // Anthropic fast mode refused (no usage credits, organization not enabled, model outside // the lane, fast pool empty): resend once at standard speed, as Claude Code does. This // sits before every 429 arm because the refusal says nothing about the account's standard // lane; waiting, cooling, or rotating on it would punish a healthy credential. The // resend is reserved and confirmed exactly like the generic OAuth hop below, and the // decision lives on this request, so every later rebuild of it stays standard. if (await anthropicFastRefused( upstreamResponse, transportState.sameTargetRequest, transportState.activeAdapter.name, anthropicFastDowngradeGuard.attempted, upstream.signal, )) { const adapterOwnsDispatch = transportState.activeAdapter.fetchResponse !== undefined; const hop = reserveCredentialHop( "repair", `${route.providerName}|${route.modelId}|anthropic-fast-downgrade`, !adapterOwnsDispatch && genericRefetchCountsSends(), ); if (hop.allowed) { anthropicFastDowngradeGuard.attempted = true; parsed.options.tierDecision = { kind: "drop" }; parsed.options.serviceTier = undefined; if (parsed.options.tierObservation) { parsed.options.tierObservation = { ...parsed.options.tierObservation, upstreamDeclinedFast: true }; } invalidateSameTargetRequest(); try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } sendBudgetState.pendingHopPermit = hop.permit; let result: Response | { failed: Response }; try { result = await rebuildAndRefetch("anthropic-fast-downgrade", () => { if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError(); }); } finally { sendBudgetState.pendingHopPermit = undefined; } if ("failed" in result) { hop.permit?.release(); return result.failed; } upstreamResponse = result; continue recovery; } } // Same-target 429 wait-and-retry (opt-in `retryOn429`, issue #487). Codex never retries // 429 itself (it retries 5xx only), and single-key pools cannot use the failover below, // so wait (Retry-After or the fixed interval) and replay the IDENTICAL request on the // same key first. Pre-stream only: a 429 arrives before any bytes are relayed, so the // replay is lossless. Runs before key failover so "primary-first" setups keep the same // key on rate-limit blips; only after the attempts are exhausted does failover run. while ( upstreamResponse.status === 429 && !isSameTargetRetryHold(upstreamResponse) && rateLimitPolicy !== null && providerAffinityGenerationIsCurrent(config, route.providerName, route.provider) && rateLimitRetries < rateLimitPolicy.attempts && !sendBudgetExhausted() ) { rateLimitRetries += 1; // Release unread body + deliberate wait via the shared same-target helper. const retryAfterHeader = upstreamResponse.headers.get("retry-after"); try { for await (const _ of prepareSameTarget429Wait({ body: upstreamResponse.body, signal: options.abortSignal, delayMs: rateLimitRetryDelayMs(rateLimitPolicy, retryAfterHeader, Date.now()), })) { // pre-stream: no stall watchdog to feed } } catch { cleanupUpstreamAbort(); upstream.abort(); return clientCancelledResponse(); } // Client cancellation wins over any stale timer edge: re-check before dispatching the // replay so an adapter never starts work for a request the client already abandoned. if (options.abortSignal?.aborted || upstream.signal.aborted) { cleanupUpstreamAbort(); upstream.abort(); return clientCancelledResponse(); } const result = await rebuildAndRefetch("rate-limit-429"); if ("failed" in result) return result.failed; upstreamResponse = result; // The refusal is a 429 too: without this the while condition is still true and the // next configured attempt replays it on the same target. if (isNonReplayableResponse(upstreamResponse) || (isAnthropicBillingKeyPool(route.provider) && [400, 402].includes(upstreamResponse.status))) continue recovery; } // Multi-key 429 failover: rotate to the next pool key (cooldown-aware) and retry the // SAME request once per remaining key. OAuth/forward providers and single-key pools // return null immediately, so this stays a no-op for them (src/providers/key-failover.ts). while (upstreamResponse.status === 429 && hasKeyPoolFailover(route.provider)) { // A quota exhaustion is dated in the BODY, not in `Retry-After` — OpenRouter // sends no header for it (#4024). Read a bounded prefix before the socket is // released below; a failed or slow read just leaves the header path in charge. // Peeks a bounded prefix and hands back a Response still carrying the whole // body, so the cancel below still releases the socket. let peeked: Awaited>; try { peeked = await readQuotaResetAt(upstreamResponse, { signal: options.abortSignal }); } catch { cleanupUpstreamAbort(); upstream.abort(); return clientCancelledResponse(); } if (options.abortSignal?.aborted) { cleanupUpstreamAbort(); upstream.abort(); return clientCancelledResponse(); } upstreamResponse = peeked.response; const quotaResetAt = peeked.at; const rotated = rotateProviderTransportOn429(config, route.providerName, route.provider, { retryAfter: upstreamResponse.headers.get("retry-after"), now: Date.now(), attemptedKey: route.provider.apiKey, promptCacheKey: parsed.options.promptCacheKey, quotaResetAt, }); if (!rotated) break; if (billingRecoveryActive && (options.abortSignal?.aborted || upstream.signal.aborted)) { cleanupUpstreamAbort(); upstream.abort(); return clientCancelledResponse(); } const billingHop = (billingPoolRequest || billingRecoveryActive) ? reserveCredentialHop("auth-recovery", `${route.providerName}|${route.modelId}|key-429`, transportState.activeAdapter.fetchResponse === undefined) : undefined; if (billingHop && !billingHop.allowed) break; const failedResponse = billingPoolRequest || billingRecoveryActive ? await retainFailureResponse() : undefined; try { // Release the failed response's socket before retrying; unread bodies otherwise linger // until runtime cleanup (one per rotated key under a rate-limit storm). if (!failedResponse) try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } route.provider = rotated; const selectionFailure = await resolveRotatedCopilotSelection(); if (selectionFailure) return selectionFailure; invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention, ); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name, }); if (billingHop) sendBudgetState.pendingHopPermit = billingHop.permit; const result = await rebuildAndRefetch("key-429", billingHop ? () => { if (billingHop.permit?.use() === false) throw new SendBudgetExhaustedError(); } : undefined, failedResponse); if ("failed" in result) { if (result.failed !== failedResponse) return result.failed; break; } if (failedResponse) { try { void failedResponse.body?.cancel().catch(() => {}); } catch { /* closed */ } } upstreamResponse = result; // Rotating on the refusal would also write a cooldown against a key that rate-limited // nothing, which outlives the request. if (isNonReplayableResponse(upstreamResponse) || (billingRecoveryActive && upstreamResponse.status === 401) || (route.provider.adapter === "anthropic" && [400, 402].includes(upstreamResponse.status))) continue recovery; } finally { if (billingHop) { sendBudgetState.pendingHopPermit = undefined; billingHop.permit?.release(); } } } // Anthropic OAuth: recover a rate limit or proven account entitlement refusal // before output, within the shared request and account rotation limits. while ( (upstreamResponse.status === 429 || upstreamResponse.status === 403 || upstreamResponse.status === 401) && anthropicInstance && transportState.anthropicPoolAccountId ) { // Same admission as the other rotation ladders: the roster cap cannot see the shared // send budget. A canRetry term, not a break -- the call also records the refusal. const adapterOwnsDispatch = transportState.activeAdapter.fetchResponse !== undefined; const hop = reserveCredentialHop( "auth-recovery", `${route.providerName}|${route.modelId}|anthropic-pool-rotation`, !adapterOwnsDispatch && genericRefetchCountsSends(), ); let nextAccountId: string | null; try { nextAccountId = await rotateAnthropicAccountOnResponseForInstance(anthropicInstance, upstreamResponse, { config, accountId: transportState.anthropicPoolAccountId, sessionKey: anthropicSessionKey, model: route.modelId, requestKey: transportState, decision: transportState.anthropicRouteDecision, currentDecision: currentAnthropicRouteDecision, signal: upstream.signal, // A denied reservation must not rotate on base headroom its bounds cannot see. canRetry: hop.allowed && (hop.permit !== undefined || !sendBudgetExhausted()) && transportState.anthropicPoolFailovers < ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST, }); } catch (rotationError) { hop.permit?.release(); throw rotationError; } if (!nextAccountId) { hop.permit?.release(); break; } try { const admitted = await commitResolvedOAuthSelection(await anthropicRoutingFor(anthropicInstance).getAnthropicPoolAccessSnapshot(nextAccountId)); if (!admitted) throw new Error("OAuth selection changed during recovery"); try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } transportState.anthropicPoolAccountId = admitted.accountId; transportState.anthropicPoolFailovers += 1; route.provider = { ...route.provider, apiKey: admitted.accessToken }; invalidateSameTargetRequest(); logCtx.provider = formatAnthropicProviderForLog(anthropicInstance, admitted.accountId, config); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention, ); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name, oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot, }); sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel); recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name); // The replay IS this hop's send; the dispatching layer settles the reservation. sendBudgetState.pendingHopPermit = hop.permit; let result: Response | { failed: Response }; try { result = await rebuildAndRefetch(upstreamResponse.status === 401 ? "oauth-401" : upstreamResponse.status === 403 ? "oauth-account-403" : "anthropic-oauth-429", () => { if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError(); }); } finally { sendBudgetState.pendingHopPermit = undefined; } if ("failed" in result) { // A no-op once the boundary was reached; a refund when the rebuild died first. hop.permit?.release(); return result.failed; } upstreamResponse = result; if (isNonReplayableResponse(upstreamResponse)) continue recovery; } catch { // release() refunds a pre-send hop and is a no-op once the send settled it. hop.permit?.release(); break; } } // Generic OAuth account failover (#2568) rotates reactively after a refusal when // two accounts are stored. Kiro additionally classifies bounded 400/403 refusals; // all other providers retain the original 429 loop below. if (route.providerName === "kiro") { while ( (upstreamResponse.status === 429 || upstreamResponse.status === 400 || upstreamResponse.status === 403) && transportState.genericFailoverAccountId ) { const refusal = classifyKiroRefusal(upstreamResponse.status, await readDisplaySafeErrorText(upstreamResponse.clone(), upstream.signal, "")); if (refusal.kind === "other") break; const sent = transportState.sentOAuthSnapshot; const monthlyCooldownMs = refusal.kind === "monthly_quota" && sent ? noteKiroMonthlyRefusal(sent.accountId, sent.generation, Date.now()) : undefined; if (monthlyCooldownMs !== undefined) persistKiroAccountState(); if (refusal.kind === "suspended") quarantineKiroSuspendedAccount(transportState.genericFailoverAccountId, sent?.generation); if (transportState.genericFailovers >= transportState.genericFailoverLimit || !isGenericOAuthFailoverEnabled(config, "kiro")) break; // Intersection with the shared request budget. This arm re-sends through // rebuildAndRefetch, so the roster cap alone would let one request walk the roster on // an allowance the rest of the request cannot see. A refusal ends the ladder with the // original HTTP response already in hand. // // Who settles this reservation depends on who dispatches the replay (#4709). An // adapter that owns its ladder -- Kiro's reset loop, Cursor's transport loop -- // reserves once per physical send and would charge the same replay again; the helper // path reports it again through `onSendsConsumed`. Both turned one physical send into // two charges, and once the allowance was spent, into a synthetic error in place of // the refusal this hop was recovering from. The wire protocol is resolved from the // provider and model, not from the account, so an account rotation cannot move the // replay between these two shapes. const adapterOwnsDispatch = transportState.activeAdapter.fetchResponse !== undefined; const hop = reserveCredentialHop( "auth-recovery", `${route.providerName}|${route.modelId}|adapter-recovery-oauth-429`, // Counted generic helpers report physical sends; an adapter-owned // ladder settles its own booking. !adapterOwnsDispatch && genericRefetchCountsSends(), ); if (!hop.allowed) break; const nextAccountId = rotateGenericOAuthAccountOnRefusal( config, route.providerName, transportState.genericFailoverAccountId, refusal.kind, upstreamResponse.headers.get("retry-after"), Date.now(), route.modelId, monthlyCooldownMs, ); if (!nextAccountId) { hop.permit?.release(); break; } try { // The FULL snapshot, not just the bearer: Antigravity pairs an account-matched // projectId with its token and Kiro carries routing metadata, so a token-only swap // would mix one account's credential with another's routing data. const snapshot = await failoverAccountSnapshot(route.providerName, nextAccountId); if (!await applyFailoverSnapshot(snapshot)) { hop.permit?.release(); break; } transportState.genericFailovers += 1; invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention, ); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name, oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot, }); if (!transportState.deferPhysicalAttribution) sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel); recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name); // The replay IS this hop's send, so hand the reservation down and let the layer that // dispatches settle it: `adapterDispatchBudget` spends it on the adapter's first // reservation, and the retry helper's reporter settles the external booking. sendBudgetState.pendingHopPermit = hop.permit; let result: Response | { failed: Response }; try { // Confirm at the dispatch boundary, not here: a rebuild can fail while shaping the // request and return `{ failed }` without reaching the wire, and a permit confirmed // before that would hold the charge for a send that never happened. An // adapter-owned ladder is the exception -- its own reservation is the confirmation, // and settling here first would hand it a dead permit, which it reads as an // exhausted request and stops sending on. result = await rebuildAndRefetch("oauth-account-429", () => { if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError(); }, upstreamResponse); } finally { sendBudgetState.pendingHopPermit = undefined; } if ("failed" in result) { // A no-op if the boundary was reached; a refund if the rebuild died before it. hop.permit?.release(); if (result.failed !== upstreamResponse) return result.failed; break; } try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } upstreamResponse = result; // The hop's permit is already settled by the dispatch boundary above; continuing // only skips the remaining arms, it does not abandon a reservation. if (isNonReplayableResponse(upstreamResponse)) continue recovery; } catch { // A throw before the send — snapshot fetch, credential application, adapter // resolution — must hand the reservation back. Without this the ladder charges the // request for a send it never made, and a later recovery in the same request is // refused on an allowance nothing spent. release() is idempotent and a no-op once // used, so a throw from the rebuild keeps its charge. hop.permit?.release(); break; } } } else { while ( upstreamResponse.status === 429 && transportState.genericFailoverAccountId && transportState.genericFailovers < transportState.genericFailoverLimit && isGenericOAuthFailoverEnabled(config, route.providerName) ) { // Intersection with the shared request budget. This arm re-sends through // rebuildAndRefetch, so the roster cap alone would let one request walk the roster on // an allowance the rest of the request cannot see. A refusal ends the ladder with the // real 429 already in hand, which is the decided exhaustion contract. // // Who settles this reservation depends on who dispatches the replay (#4709). An // adapter that owns its ladder -- Kiro's reset loop, Cursor's transport loop -- // reserves once per physical send and would charge the same replay again; the helper // path reports it again through `onSendsConsumed`. Both turned one physical send into // two charges, and once the allowance was spent, into a synthetic error in place of // the 429 this hop was recovering from. The wire protocol is resolved from the // provider and model, not from the account, so an account rotation cannot move the // replay between these two shapes. const adapterOwnsDispatch = transportState.activeAdapter.fetchResponse !== undefined; const hop = reserveCredentialHop( "auth-recovery", `${route.providerName}|${route.modelId}|adapter-recovery-oauth-429`, // Counted generic helpers report physical sends; an adapter-owned // ladder settles its own booking. !adapterOwnsDispatch && genericRefetchCountsSends(), ); if (!hop.allowed) break; const nextAccountId = rotateGenericOAuthAccountOn429( config, route.providerName, transportState.genericFailoverAccountId, upstreamResponse.headers.get("retry-after"), Date.now(), route.modelId, ); if (!nextAccountId) { hop.permit?.release(); break; } try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } try { // The FULL snapshot, not just the bearer: Antigravity pairs an account-matched // projectId with its token and Kiro carries routing metadata, so a token-only swap // would mix one account's credential with another's routing data. const snapshot = await failoverAccountSnapshot(route.providerName, nextAccountId); transportState.genericFailovers += 1; if (!await applyFailoverSnapshot(snapshot)) { hop.permit?.release(); break; } invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention, ); // Same contract as the Kiro refusal arm: the replay must carry the replacement // account's continuation owner and reasoning scope, never the account that just 429'd. bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name, oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot, }); if (!transportState.deferPhysicalAttribution) sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel); recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name); // The replay IS this hop's send, so hand the reservation down and let the layer that // dispatches settle it: `adapterDispatchBudget` spends it on the adapter's first // reservation, and the retry helper's reporter settles the external booking. sendBudgetState.pendingHopPermit = hop.permit; let result: Response | { failed: Response }; try { // Confirm at the dispatch boundary, not here: a rebuild can fail while shaping the // request and return `{ failed }` without reaching the wire, and a permit confirmed // before that would hold the charge for a send that never happened. An // adapter-owned ladder is the exception -- its own reservation is the confirmation, // and settling here first would hand it a dead permit, which it reads as an // exhausted request and stops sending on. result = await rebuildAndRefetch("oauth-account-429", () => { if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError(); }); } finally { sendBudgetState.pendingHopPermit = undefined; } if ("failed" in result) { // A no-op if the boundary was reached; a refund if the rebuild died before it. hop.permit?.release(); return result.failed; } upstreamResponse = result; // The hop's permit is already settled by the dispatch boundary above; continuing // only skips the remaining arms, it does not abandon a reservation. if (isNonReplayableResponse(upstreamResponse)) continue recovery; } catch (err) { // A local admission denial is terminal for the whole request: rotating to another // account cannot re-scope the key, so the refusal answers in place of the 429. const scopeRefusal = unwrapUpstreamRetryEvidenceError(err); if (!options.abortSignal?.aborted && scopeRefusal instanceof AdmissionModelDeniedError) { hop.permit?.release(); cleanupUpstreamAbort(); upstream.abort(); releaseCodexAuthContextProbeLease(admissionState.authCtx); return admissionModelDeniedResponse(projectAdmissionModelDeniedLog(scopeRefusal, logCtx)); } // A throw before the send — snapshot fetch, credential application, adapter // resolution — must hand the reservation back. Without this the ladder charges the // request for a send it never made, and a later recovery in the same request is // refused on an allowance nothing spent. release() is idempotent and a no-op once // used, so a throw from the rebuild keeps its charge. hop.permit?.release(); break; } } // Antigravity verify-account quarantine: a 403 demanding account verification // is terminal for THAT account — its grant still refreshes, so only a manual // verification followed by a re-login clears it. Mark it needsReauth (durable, // shown as `needs-reauth(verify)` in `ocx account list` with a `verify_account` // health reason in the management API, excluded from the pool until re-login) // and replay the same request on the next eligible account so the pool keeps // serving. while ( upstreamResponse.status === 403 && route.providerName === "google-antigravity" && transportState.genericFailoverAccountId ) { const refusal = classifyAntigravityRefusal(upstreamResponse.status, await readDisplaySafeErrorText(upstreamResponse.clone(), upstream.signal, "")); if (refusal.kind === "other") break; const sent = transportState.sentOAuthSnapshot; const failedAccountId = transportState.genericFailoverAccountId; // Fenced write: a 403 arriving after the credential rotated (refresh or // re-login) must not quarantine the new login. Without the matching sent // snapshot there is no owner for this write, so skip marking. if (!sent || sent.accountId !== failedAccountId) break; try { await markAccountNeedsReauthIfGeneration(route.providerName, failedAccountId, sent.generation, undefined, "verify_account"); } catch { // A failed quarantine write must preserve the refusal, not authorize another send. break recovery; } if (!antigravityPoolActivated || transportState.genericFailovers >= transportState.genericFailoverLimit) break; const adapterOwnsDispatch = transportState.activeAdapter.fetchResponse !== undefined; const hop = reserveCredentialHop( "auth-recovery", `${route.providerName}|${route.modelId}|adapter-recovery-oauth-verify`, !adapterOwnsDispatch && genericRefetchCountsSends(), ); if (!hop.allowed) break; // Auth-refusal rotation, not the rate-limit one: a verification refusal // must not record rate-limit cooldown semantics against the account. const nextAccountId = rotateAntigravityAccountOnAuthRefusal( antigravityPoolActivated, failedAccountId, sent.generation, route.modelId, ); if (!nextAccountId) { hop.permit?.release(); break; } // The refusal body stays alive until the replacement owns the outcome: it // is also the preserveFailureResponse below, so cancelling it early would // hand back a 403 with a destroyed body when the rebuild is refused. const failedResponse = upstreamResponse; try { // The FULL snapshot, not just the bearer: Antigravity pairs an // account-matched projectId with its token, so a token-only swap would mix // one account's credential with another's routing data. const snapshot = await failoverAccountSnapshot(route.providerName, nextAccountId); if (!await applyFailoverSnapshot(snapshot)) { hop.permit?.release(); break; } invalidateSameTargetRequest(); transportState.activeAdapter = resolveSelectionAdapter( resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, route.staticPolicy), config.cacheRetention, ); bindRouteReasoningReplayScope({ parsed, providerName: route.providerName, provider: route.provider, adapterName: transportState.activeAdapter.name, oauthCredentialSnapshot: transportState.replayOAuthCredentialSnapshot, }); sealRequestAttemptIdentity(logCtx.activeAttempt, logCtx.provider, transportState.activeAdapter.name, logCtx.accountLogLabel); recordAttemptCredentialSource(logCtx.activeAttempt, route.providerName, route.provider, transportState.activeAdapter.name); sendBudgetState.pendingHopPermit = hop.permit; let result: Response | { failed: Response }; try { result = await rebuildAndRefetch("oauth-account-403", () => { if (!adapterOwnsDispatch && hop.permit?.use() === false) throw new SendBudgetExhaustedError(); }, failedResponse); } finally { sendBudgetState.pendingHopPermit = undefined; } if ("failed" in result) { hop.permit?.release(); if (result.failed !== failedResponse) return result.failed; // Preserve common redaction and combo consumption without another recovery send. break recovery; } transportState.genericFailovers += 1; try { void failedResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } upstreamResponse = result; if (isNonReplayableResponse(upstreamResponse)) continue recovery; } catch { hop.permit?.release(); break; } } } // Unknown provenance is deliberately fail-soft in pre-flight: after a restart, TTL expiry, // or LRU eviction, a valid same-backend blob must survive. A decoder's own 4xx identity is // the missing authoritative signal. Rebuild once through the same sanitation path used by a // known route switch; invalidating is mandatory because `parsed` mutates in place and the // same-target cache would otherwise replay the rejected bytes verbatim. const opaqueBlobRecovery = await attemptOpaqueBlobRecovery({ response: upstreamResponse, outboundBody: transportState.sameTargetRequest?.body, adapterName: transportState.activeAdapter.name, parsed, guard: opaqueBlobRecoveryGuard, signal: upstream.signal, }, recovery => { invalidateSameTargetRequest(); return rebuildAndRefetch(recovery); }); if (opaqueBlobRecovery.kind === "failed") return opaqueBlobRecovery.response; if (opaqueBlobRecovery.kind === "recovered") { upstreamResponse = opaqueBlobRecovery.response; continue recovery; } // Anthropic 413 request_too_large: rebuild once with every image one tier lower // (spiral guard: single attempt). The biased response re-enters the 429 check above. if (shouldAttemptImageTierRetry({ status: upstreamResponse.status, adapterName: transportState.activeAdapter.name, parsed, alreadyAttempted: imageRetryAttempted, })) { imageRetryAttempted = true; transportState.imageTierBias = 1; invalidateSameTargetRequest(); const failedResponse = await retainFailureResponse(); const result = await rebuildAndRefetch("image-413", undefined, failedResponse); if ("failed" in result) { if (result.failed !== failedResponse) return result.failed; break recovery; } try { void failedResponse.body?.cancel().catch(() => {}); } catch { /* closed */ } upstreamResponse = result; continue recovery; } // Console Go (opencode-zen / opencode-go) intermittently rejects a body it accepts seconds // later with 400 invalid_request_error / "Invalid upload request." Replay the // byte-identical request once after the exact gateway rejection. if (!consoleGoUploadRetryGuard.attempted) { const uploadRejectionBody = await consoleGoUploadRejectionBody( upstreamResponse, consoleGoUploadRetryGuard.attempted, upstream.signal, ); if (uploadRejectionBody !== undefined && isTransientConsoleGoUploadRejection({ status: upstreamResponse.status, errorBody: uploadRejectionBody, outboundUrl: transportState.sameTargetRequest?.url, })) { consoleGoUploadRetryGuard.attempted = true; try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } if (!upstream.signal.aborted) { try { await sleepWithAbort(CONSOLE_GO_UPLOAD_RETRY_DELAY_MS, upstream.signal); } catch { cleanupUpstreamAbort(); return clientCancelledResponse(); } } if (upstream.signal.aborted) { cleanupUpstreamAbort(); return clientCancelledResponse(); } const result = await rebuildAndRefetch("console-go-upload-retry"); if ("failed" in result) return result.failed; upstreamResponse = result; continue recovery; } } // Learn a refused rung and retry at the next published one. Direct requests are limited // to one repair; combo candidates may continue down the finite ladder within the send budget. const downgrade = await refusedReasoningEffortDowngrade(upstreamResponse, upstream.signal, reasoningEffortDowngradeGuard, options.comboAttempt, { provider: route.provider, modelId: parsed.modelId, requested: parsed.options.reasoning }); if (downgrade) { reasoningEffortDowngradeGuard.attempted = true; parsed.options.reasoning = downgrade.effort; // The same-target cache keys on parsed identity, so a mutated effort needs a token bump. invalidateSameTargetRequest(); // A combo keeps the refusal, so an unfunded rung still reaches combo failover as the original 400. const refusal = options.comboAttempt ? await retainFailureResponse() : undefined; if (!refusal) try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } const result = await rebuildAndRefetch("reasoning-effort-downgrade", undefined, refusal); if ("failed" in result) { if (result.failed !== refusal) return result.failed; break recovery; } if (refusal) try { void refusal.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ } upstreamResponse = result; continue recovery; } break; } if (!upstreamResponse.ok) { if (route.providerName === "kiro") upstreamResponse = await normalizeFinalKiroHttpError(upstreamResponse, upstream.signal); if (options.comboAttempt) { // No pre-read guard: `consumeComboFailure` -> `readBoundedResponseBody` reads // `response.body` itself with the abort signal threaded through, and the combo // contract is that this body's getter is touched exactly once. A guard here would be // a second `.body` access for no gain, since the bounded reader owns settlement. const failure = await consumeComboFailure(upstreamResponse, options.abortSignal, Date.now(), logCtx, !isProxyError(upstreamResponse)) .finally(cleanupUpstreamAbort); options.onConsumedComboFailure?.(failure); return failure.response; } let errorText: string; try { errorText = await readDisplaySafeErrorText( upstreamResponse, upstream.signal, "unknown error", ); } finally { cleanupUpstreamAbort(); } // The provider's own body (not a proxy-normalized one) may name its closed error class (#8446). if (!isProxyError(upstreamResponse)) recordUpstreamProviderErrorDetail(logCtx, upstreamResponse.status, errorText, upstreamResponse); if (upstreamResponse.status === 413) { if (options.clientModelLabel) projectLocalModelRefusalLog(logCtx, options.clientModelLabel); return clientRequestedStream ? streamingContextOverflowResponse(clientFacingModelText(options.clientModelLabel), translatorBudget) : jsonContextOverflowResponse(); } const policyRefusal = rewriteUpstreamPolicyRefusal({ status: upstreamResponse.status, errorText, stream: clientRequestedStream, modelId: clientFacingModelText(options.clientModelLabel), // The same host check covers the openai-chat wire: both xAI hosts serve Chat too. destinationIsXai: isXaiResponsesDestination(route.provider), translatorBudget, turnAdmissionLease: options.turnAdmissionLease, }); if (policyRefusal) { // Codex-facing incomplete/content_filter. openai-responses passthrough // uses the same helper after its 413 block. return policyRefusal; } if (!isFixedCodexAccount(admissionState.authCtx)) { recordSubagentQuotaFailureForThreadSpawn( req.headers, subagentQuotaFailureModel, upstreamResponse.status === 429 || upstreamResponse.status === 402 ? upstreamResponse.status : `Provider error ${upstreamResponse.status}: ${redactSecretString(errorText.slice(0, 500))}`, config, requestState.subagentFallbackAccountId, ); } // The last pool account just refused on spent quota and recovery found no replacement: // answer the usage limit now (#6991), so Codex stops instead of retrying into the local // refusal that every later attempt meets until the first window reopens. const poolUsageLimit = upstreamResponse.status === 429 && anthropicInstance && route.provider.authMode === "oauth" ? anthropicRoutingFor(anthropicInstance).anthropicPoolUsageLimitAfterRefusal(Date.now(), transportState.anthropicRouteDecision, route.modelId) : null; // Combo children returned above; the flag is passed anyway so this site states the same rule. if (poolUsageLimit) return anthropicPoolCooldownResponse(poolUsageLimit, { comboAttempt: options.comboAttempt, logCtx }); // Upstreams occasionally echo request details in error bodies — scrub token-shaped // material before it reaches the client-facing error surface. const upstreamRetryAfter = upstreamResponse.headers.get("retry-after"); const { normalized, message } = formatAdapterUpstreamFailure({ status: upstreamResponse.status, errorText, route, upstreamRetryAfter, denialMessage: fallback => antigravityDenial.message(route.modelId, fallback), logCtx, }); options.onCompactionRecoveryAdapterEvent?.({ type: "error", status: upstreamResponse.status, errorType: normalized.type, code: normalized.code, message: "Structured upstream failure observed before client formatting", }); const retryAfter = normalized.cyberPolicy ? undefined : resolveClientRetryAfter({ status: upstreamResponse.status, message, upstreamRetryAfter, includeDefault: !(transportState.anthropicPoolAccountId && classifyAnthropic429(upstreamResponse.headers) === "request-scoped-unknown"), }); return carryPolicyCandidateRefusal(upstreamResponse, formatUpstreamFailureResponse(upstreamResponse, normalized.code, upstreamResponse.status, normalized.cyberPolicy ? (normalized.type ?? CYBER_POLICY_ERROR_CODE) : "upstream_error", message, { ...(normalized.cyberPolicy ? { code: CYBER_POLICY_ERROR_CODE } : {}), ...(retryAfter !== undefined ? { retryAfter } : {}), }, )); } } bindAnthropicRejectionResponse(transportState.sameTargetRequest, upstreamResponse); cancelBodyOnAbort(upstreamResponse.body, upstream.signal); return { upstream, cleanupUpstreamAbort, get connectMs() { return resolveConnectTimeoutMs(config.connectTimeoutMs, { localUpstream: this.localUpstream }); }, get stallTimeoutMs() { return resolveStallTimeoutMs(config.stallTimeoutSec, { localUpstream: this.localUpstream }); }, get localUpstream() { return isLocalUpstream(transportState.sameTargetRequest?.url ?? route.provider.baseUrl); }, upstreamResponse, thinkingSignatureRecovery, refetchThinkingSignature, rateLimitPolicy, get rateLimitRetries(): typeof rateLimitRetries { return rateLimitRetries; }, set rateLimitRetries(value: typeof rateLimitRetries) { rateLimitRetries = value; }, }; } export type AdapterExchange = Exclude>, Response>;