import { recordUpstreamProviderErrorDetail, recordUpstreamValidationDetail, type FailureDetail } from "../request-log-failure-detail"; import { carryPolicyCandidateRefusal } from "./policy-request-scope"; import { comboFailureEvidence, carryProxyError, carryProxyErrorPresentation, proxyErrorPresentation } from "../../lib/client-error-body"; import { carryWorkflowRefusal } from "../workflow-refusal"; import { codexAccountModelRefusal, codexAccountModelRefusalHardStopCode, isMalformedResponseFormatRequest, isOpenRouterOpaque400Envelope, isUnofferedToolCallRefusal, isProviderModerationRefusal, upstreamEnvelopeRootCode, type CodexAccountModelRefusal, } from "../../combos/failover"; import { parseRetryAfterMs } from "../../combos"; import type { ConsumedComboFailure, HandleResponsesOptions } from "./core-options"; import type { OcxUsage } from "../../types"; import { readBoundedResponseBody } from "../../lib/bounded-body"; import { carryAnthropicRefusalCredential } from "../../oauth/anthropic-account-refusal"; import { carryCodexAccountCooldownResponse } from "../../codex/account-cooldown-response"; import { codexQuotaFailureMessage, codexQuotaOutcomeMeta } from "./core-codex-account"; import { isRateLimitOrQuotaFailureMessage, isCyberPolicyCode, isCyberPolicyMessage, CYBER_POLICY_ERROR_CODE, CYBER_POLICY_FALLBACK_MESSAGE, isModelNotInPlanRefusal, markVerifiedModelNotInPlanResponse, namesModelNotInPlanCode, } from "../../lib/errors"; import { normalizeUpstreamErrorText } from "./core-errors"; import { resolveClientRetryAfter } from "../../lib/retry-after"; import { formatErrorResponse } from "../../bridge"; import { upstreamCodeForComboFailure } from "../../bridge/errors"; import { OLLAMA_LATE_ATTRIBUTION_LIMIT_MESSAGE } from "../../adapters/ollama-native"; import { carryReplayRefusal, hasNoRetryDirective, isAutomaticReplayBlocked, isReplayRefusalCode, isReplayRefusalResponse, markResponseNonReplayable, UPSTREAM_RESET_REPLAY_REFUSED_CODE } from "../../lib/upstream-retry"; import { MODEL_NOT_ALLOWED_FOR_KEY } from "../admission-model-scope"; import { usageFromResponsesPayload } from "../request-log"; import type { ResponsesTerminalStatus } from "../../bridge"; import { stampMetaMusePlanRefusal } from "../../providers/meta-muse-plan-refusal"; import type { RequestFailureCause } from "../../usage/telemetry-contract"; export function sanitizedRetryAfter(value: string | null, now: number): string | undefined { const trimmed = value?.trim(); if (!trimmed || trimmed.length > 128) return undefined; return parseRetryAfterMs(trimmed, now) !== undefined ? trimmed : undefined; } export async function consumeComboFailure( response: Response, signal?: AbortSignal, now = Date.now(), logCtx?: { failureDetail?: FailureDetail; causeHint?: RequestFailureCause }, /** True only when `response` is the provider's own HTTP answer, so its closed error class is evidence. */ rawUpstream = false, ): Promise { // Read before the body: the marker lives on this Response object, and the failure below is // rebuilt as a new one that would otherwise lose it. let nonReplayable = isAutomaticReplayBlocked(response); const syntheticRefusal = isReplayRefusalResponse(response); let replayRefusal = syntheticRefusal; const fallback = `Provider error ${response.status}`; let classificationText = fallback; let codexModelRefusal: CodexAccountModelRefusal | undefined; let malformedResponseFormat: boolean | undefined; let openRouterOpaque400: boolean | undefined; let unofferedToolCallRefusal: boolean | undefined; let providerModerationRefusal: boolean | undefined; let usage: OcxUsage | undefined; let upstreamCode: string | undefined; let upstreamRootCode: string | undefined; let upstreamMessage: string | undefined; let upstreamType: string | undefined; // Whether the body itself confirms a quota/rate-limit refusal, computed on the SAME read as // the classification below. `shouldRetryCodexPoolAccountQuota` cannot be called here without // a second body read, so this mirrors its normalization: raw 402/429, or a 5xx whose intact, // display-safe body carries a recognized quota message. let quotaConfirmedByBody = false; const serverError = response.status >= 500 && response.status < 600; // A local key-scope refusal is already the client's answer: rebuilding it as an upstream_error // would hide the stable model_not_allowed_for_key type clients branch on. let scopeRefusalBody: string | undefined; let verifiedPlanRefusal = false; try { const legacy = comboFailureEvidence(response); if (legacy !== undefined) void response.body?.cancel().catch(() => undefined); const body = await readBoundedResponseBody(legacy === undefined ? response : new Response(legacy), { signal, reportUtf8Validity: serverError }); // A 5xx body counts as quota or classification evidence only when it decoded as valid // UTF-8, matching shouldRetryCodexPoolAccountQuota. A malformed byte keeps the status-only // fallback, with one exception: a cyber-policy refusal must still stop the combo, so the // replacement-decoded text may carry that verdict and nothing else. const utf8Trusted = !serverError || body.utf8Valid === true; if (utf8Trusted) usage = usageFromComboFailureText(body.text); if (serverError && utf8Trusted && body.displaySafe && !body.truncated) { const quotaMessage = codexQuotaFailureMessage(body.text); quotaConfirmedByBody = quotaMessage !== undefined && isRateLimitOrQuotaFailureMessage(quotaMessage); } if (body.displaySafe && !body.truncated) { if (logCtx && rawUpstream) recordUpstreamProviderErrorDetail(logCtx, response.status, body.text, response); else if (logCtx) recordUpstreamValidationDetail(logCtx, response.status, body.text); // A combo child's row is attributed from the same evidence the parent uses: Meta's Muse // Code refusal is a plan restriction, not a rejected credential (#8352). if (logCtx) stampMetaMusePlanRefusal(logCtx, response.status, body.text); if (response.status === 400) { codexModelRefusal = codexAccountModelRefusal(response.status, body.text); malformedResponseFormat = isMalformedResponseFormatRequest(response.status, body.text); openRouterOpaque400 = isOpenRouterOpaque400Envelope(body.text); // The intact bounded body, not the 500-char display prefix: a long refusal envelope // would fail the classifier's JSON parse on truncated text and stop the combo. unofferedToolCallRefusal = isUnofferedToolCallRefusal(response.status, body.text); } // Judged on the whole body: the classification text below is cut at 500 characters, and a // hard refusal after the cut must still veto a moderation hop. if (response.status === 400 || response.status === 422) { providerModerationRefusal = isProviderModerationRefusal(response.status, body.text, undefined, true); } // Status-independent: the nested record is what normalization selects, so a root verdict // beside it has to be captured here or the conflict rule never sees it. upstreamRootCode = upstreamEnvelopeRootCode(body.text); const normalized = normalizeUpstreamErrorText(body.text, fallback); if (utf8Trusted || isCyberPolicyCode(normalized.code) || isCyberPolicyMessage(normalized.safeText)) { classificationText = normalized.safeText; upstreamCode = codexAccountModelRefusalHardStopCode(response.status, body.text) ?? upstreamCodeForComboFailure(response, normalized.code); // Only the adapter's own normalized refusal may claim the plan-refusal hop; another // upstream echoing the code in any spelling keeps the meaning it had before, which was none. if (namesModelNotInPlanCode(upstreamCode) && !isModelNotInPlanRefusal(response, upstreamCode)) upstreamCode = undefined; verifiedPlanRefusal = isModelNotInPlanRefusal(response, upstreamCode); upstreamMessage = normalized.message; upstreamType = normalized.type; } if (response.status === 403 && proxyErrorPresentation(response) === "scope" && upstreamType === MODEL_NOT_ALLOWED_FOR_KEY) { scopeRefusalBody = body.text; } } } catch (error) { if (signal?.aborted) throw error; classificationText = fallback; } const cyberFailure = isCyberPolicyCode(upstreamCode) || isCyberPolicyMessage(classificationText); replayRefusal ||= isReplayRefusalCode(upstreamCode); nonReplayable ||= replayRefusal; const normalizedUpstreamCode = cyberFailure ? CYBER_POLICY_ERROR_CODE : replayRefusal && !isReplayRefusalCode(upstreamCode) ? UPSTREAM_RESET_REPLAY_REFUSED_CODE : upstreamCode; const message = cyberFailure ? upstreamMessage ?? (isCyberPolicyCode(upstreamCode) ? CYBER_POLICY_FALLBACK_MESSAGE : classificationText) : classificationText === fallback ? fallback : `${fallback}: ${classificationText}`; const upstreamRetryAfter = response.headers.get("retry-after"); // Past HTTP dates are an immediate retry directive, just like the numeric value zero. // Normalize before the client helper discards them and substitutes a default delay. const effectiveRetryAfter = parseRetryAfterMs(upstreamRetryAfter, now) === undefined && parseRetryAfterMs(upstreamRetryAfter, now, { preserveImmediate: true }) !== undefined ? "0" : upstreamRetryAfter; // Client response may get the synthetic "2" fallback; cooldown metadata must not — // otherwise coolComboTarget treats it as a 2s cooldown instead of the 60s default. const clientRetryAfter = replayRefusal ? undefined : resolveClientRetryAfter({ status: response.status, message, upstreamRetryAfter: effectiveRetryAfter, now, }); const cooldownRetryAfter = syntheticRefusal ? undefined : resolveClientRetryAfter({ status: response.status, message, upstreamRetryAfter: effectiveRetryAfter, now, includeDefault: false, }); const failureResponse = !replayRefusal && scopeRefusalBody !== undefined ? new Response(scopeRefusalBody, { status: 403, headers: { "Content-Type": "application/json" } }) : formatErrorResponse( response.status, cyberFailure ? (upstreamType ?? CYBER_POLICY_ERROR_CODE) : "upstream_error", message, { ...(normalizedUpstreamCode !== undefined ? { code: normalizedUpstreamCode } : {}), ...(clientRetryAfter !== undefined ? { retryAfter: clientRetryAfter } : {}), fixedMessage: hasNoRetryDirective(response), }, ); if (nonReplayable) markResponseNonReplayable(failureResponse); carryProxyError(response, failureResponse); carryWorkflowRefusal(response, failureResponse); carryReplayRefusal(response, failureResponse); carryProxyErrorPresentation(response, failureResponse); carryPolicyCandidateRefusal(response, failureResponse); if (verifiedPlanRefusal) markVerifiedModelNotInPlanResponse(failureResponse); carryAnthropicRefusalCredential(response, failureResponse); carryCodexAccountCooldownResponse(response, failureResponse); return { response: failureResponse, ...(nonReplayable ? { nonReplayable: true } : {}), ...(verifiedPlanRefusal ? { verifiedPlanRefusal: true } : {}), classificationText, ...(codexModelRefusal !== undefined ? { codexModelRefusal } : {}), ...(malformedResponseFormat !== undefined ? { malformedResponseFormat } : {}), ...(openRouterOpaque400 !== undefined ? { openRouterOpaque400 } : {}), ...(unofferedToolCallRefusal !== undefined ? { unofferedToolCallRefusal } : {}), ...(providerModerationRefusal !== undefined ? { providerModerationRefusal } : {}), ...(normalizedUpstreamCode !== undefined ? { upstreamCode: normalizedUpstreamCode } : {}), // A rewritten identity (policy, replay refusal) is the whole verdict; the root's own code // would only re-open a decision that rewrite already settled. ...(!cyberFailure && !replayRefusal && upstreamRootCode !== undefined ? { upstreamRootCode } : {}), ...(upstreamMessage !== undefined ? { upstreamMessage } : {}), ...(upstreamType !== undefined ? { upstreamType } : {}), ...(!cyberFailure && cooldownRetryAfter !== undefined ? { retryAfter: cooldownRetryAfter } : {}), // The EFFECTIVE classification decides, not the raw status. An upstream that wraps a quota // refusal in a 5xx still carries `x-codex-*-reset-at`, and gating on 402/429 alone threw // those away, so the combo target came back up immediately instead of waiting for the // window it was told about. `cyberFailure` stays excluded: a policy block is not a quota. ...(!cyberFailure && !replayRefusal && (response.status === 429 || response.status === 402 || quotaConfirmedByBody) ? { resetAt: codexQuotaOutcomeMeta(response).resetAt } : {}), ...(usage ? { usage } : {}), }; } export function usageFromComboFailureText(text: string): OcxUsage | undefined { try { const payload = JSON.parse(text) as Record; const nested = payload.response; const source = nested && typeof nested === "object" && !Array.isArray(nested) ? nested as Record : payload; return usageFromResponsesPayload(source.usage); } catch { return undefined; } } export function createChildPassthroughCallbackGate(options: HandleResponsesOptions) { type Pending = | { kind: "terminal"; status: ResponsesTerminalStatus } | { kind: "cancel" }; let state: "pending" | "committed" | "discarded" = "pending"; let pending: Pending | undefined; let accepted = false; let pendingModel: string | undefined; let completionAccepted = false; let completionRejected = false; const publish = (value: Pending): void => { if (value.kind === "terminal") options.onNativePassthroughTerminal?.(value.status); else options.onNativePassthroughCancel?.(); }; const publishCompletion = (): void => { if (state !== "committed" || completionRejected || pendingModel === undefined) return; const model = pendingModel; pendingModel = undefined; options.onResponseComplete?.(model); }; const receive = (value: Pending): void => { if (state === "discarded" || accepted) return; accepted = true; if (value.kind === "cancel" || value.status !== "completed") { completionRejected = true; pendingModel = undefined; } if (state === "committed") return publish(value); pending ??= value; }; return { onTerminal: (status: ResponsesTerminalStatus) => receive({ kind: "terminal", status }), onCancel: () => receive({ kind: "cancel" }), onResponseComplete: (model: string) => { if (state === "discarded" || completionRejected || completionAccepted || !model.trim()) return; completionAccepted = true; pendingModel = model; publishCompletion(); }, commit: () => { if (state !== "pending") return; state = "committed"; if (pending) publish(pending); pending = undefined; publishCompletion(); }, discard: () => { state = "discarded"; pending = undefined; pendingModel = undefined; }, }; } export function buildComboChildHeaders(parentHeaders: HeadersInit): Headers { const childHeaders = new Headers(parentHeaders); // A provisional caller credential is not authoritative for a Combo child. childHeaders.delete("authorization"); childHeaders.delete("chatgpt-account-id"); // Combo children re-serialize already-decoded JSON. Keeping transport metadata from // the parent would make the child decoder treat plain JSON as compressed bytes. childHeaders.delete("content-length"); childHeaders.delete("content-encoding"); return childHeaders; } /** * A member's local Ollama late-attribution refusal: the exact proxy-owned constant on the exact * local wire shape, from a replayable attempt. The caller replaces only the client response with * the literal constant envelope; classification inputs are never rewritten. */ export function isLateAttributionRefusal(failure: ConsumedComboFailure): boolean { return !failure.nonReplayable && failure.response.status === 413 && failure.upstreamCode === "request_too_large" && failure.upstreamMessage === OLLAMA_LATE_ATTRIBUTION_LIMIT_MESSAGE; }