UNPKG

@bitkyc08/opencodex

Version:

Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code

1,468 lines • 84.6 kB
import { sealCredentialRefusal } from "./credential-recovery-attempt";
import { MessagesSseFrameContext, messagesSseEchoRenamedError, messagesSseFrameHooks } from "../lib/messages-sse-error-presentation";
import { createMessagesSseErrorFilter } from "./messages-sse-error-filter";
import type { MessagesFailureVerdictSink } from "../claude/failure-verdict";
import { upstreamErrorIsLocal } from "./request-log-failure-detail";
import { boundedMessagesDisplayCount, ownedAnthropicErrorPayload, ownedAnthropicErrorResponse, messagesOverflowPresentation, messagesRelayedOverflowPresentation } from "../lib/messages-error-body";
import { attachComboFailureEvidence, isProxyError, markProxyError } from "../lib/client-error-body";
import { captureAccountAutoPauseAttempt, wasAccountAutoPaused } from "../oauth/account-auto-pause";
import { createPhysicalSendReporter, reportDispatchSends, type RequestExecutionBudget, type SingleUseDispatchPermit } from "../lib/request-execution-budget";
import { createInferenceSendBudget, expandInferenceOAuthSendBudget } from "./inference/context";
import { GENERIC_OAUTH_MAX_ACCOUNTS_PER_REQUEST } from "../oauth/generic-account-failover";
/**
 * Managed native Messages lane (PF-08, behind `protocols.rollout.managedMessagesNative`).
 *
 * A Messages request whose settled route is a proxy-managed key on the `anthropic` adapter is
 * sent as Messages: the source body (after the ingress's managed-client steps) cut to a field
 * allowlist, the wire model, and the provider's own key. With `managedMessagesNativeOAuth` on
 * (PF-10), an Anthropic OAuth account is sent the same way with the access token the
 * existing OAuth selection resolves at dispatch (`messages-native-oauth.ts`). Modelled on the native Chat lane and
 * built on the same shared pieces — attempt row, finish-once final log, spend tracker, proactive
 * key selection, key failover and 429 replay, connection policy — so nothing the Responses
 * pipeline enforces is bypassed.
 *
 * Authority. The ingress hands over caller `anthropic-beta` for allowlisting and an opaque
 * observed CLI identity handle for first-party compatibility. Neither grants credential authority. The caller-forward passthrough in
 * `claude-messages.ts` (the caller's own Anthropic credential) is a different branch decided
 * before this one, and nothing here can reach it or be reached from it.
 *
 * Loaded lazily by `claude-messages.ts` only when the switch is on and a route is eligible.
 */
import { resolveAnthropicModelRouteForInstance } from "../oauth/anthropic-model-routes";
import { anthropicRefusalCredentialChanged, bindAnthropicRefusalCredentialForSend, rotateAnthropicAccountOnResponseForInstance } from "../oauth/anthropic-account-refusal";
import { captureAnthropicPhysicalSendOwnership, anthropicPhysicalSendOwnershipIsCurrent,
  type AnthropicPhysicalSendOwnership } from "../oauth/anthropic-send-ownership";
import { anthropicModelQuotaFor } from "../oauth/anthropic-model-quota";
import { captureConfigGeneration } from "../lib/state-store-sweeper";
import { recordAnthropicAccountQuotaFromHeadersForInstance } from "../providers/quota";
import { credentialGeneration, getAccountCredentialWithStatus } from "../oauth/store";
import { ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST } from "../oauth/anthropic-routing";
import { enforceAnthropicImageLimits } from "../adapters/anthropic-image-guard";
import { normalizeAnthropicImages } from "../adapters/anthropic-image-normalize";
import { formatAnthropicErrorBody, resolveAnthropicMessagesUrl } from "../adapters/anthropic";
import {
  anthropicMessagesNativeWireBody,
  buildAnthropicMessagesPassthroughRequest,
  type AnthropicMessagesPassthroughRequest,
} from "../adapters/anthropic/passthrough";
import { effortFromOutputConfig, nativeAnthropicProjection, resolveInboundModel } from "../claude/inbound";
import { anthropicErrorBody, claudeOverflowSsePayload, claudePromptTooLongMessage, collectAnthropicMessage, isContextOverflowText, isThroughputLimitText } from "../claude/outbound";
import type { AdmissionLease } from "../lib/admission";
import { readBoundedResponseBody } from "../lib/bounded-body";
import { classifyError } from "../lib/errors";
import { relaySseWithPayloadRewrite } from "./sse-payload-rewrite";
import { EMPTY_BYTES, joinSseFrameBytes, sseDelimiterLengthAt } from "./sse-frame-buffer";
import { redactSecretString } from "../lib/redact";
import { formatClientRetryAfter, resolveClientRetryAfter } from "../lib/retry-after";
import { enrichOpenCodeZenUpstreamMessage } from "../providers/opencode-zen-rate-limit";
import { isTranslatorBudgetExceededError, type TranslatorBudget } from "../lib/translator-budget";
import {
  applyReplayRefusalClientHeaders,
  applyUpstreamRecoveryInit,
  SendBudgetExhaustedError,
  fetchWithResetRetry,
  fetchWithTransientRetry,
  isNonReplayableResponse,
  isReplayRefusalResponse,
  isTransientUpstreamStatus,
  markResponseNonReplayable,
  prepareSameTarget429Wait,
  REPLAY_REFUSED_STATUS,
  retainReplayRefusal,
  UpstreamRetryEvidenceError,
  TRANSIENT_RETRY_MAX_ATTEMPTS,
  type UpstreamSendRecovery,
  UPSTREAM_RESET_REPLAY_REFUSED_CODE,
} from "../lib/upstream-retry";
import { distributedKeyPhysicalFetch, distributedKeyResponseOwner, recordDistributedResponseFailure } from "../providers/api-key-response-ownership";
import { passiveKeyPhysicalFetch } from "../providers/passive-key-observation";
import { isProviderApiKeyPaused, ProviderApiKeyPausedError } from "../providers/api-key-pause";
import { providerAffinityGenerationIsCurrent, providerApiKeySelectionIsCurrent, resolveCurrentProviderApiKeyTransport } from "../providers/api-key-selection";
import { RequestPacingQueueOverloadError } from "../providers/request-pacing";
import {
  hasKeyPoolFailover,
  rateLimitRetryDelayMs,
  rateLimitRetryPolicyFor,
  rotateProviderTransportOn401,
  rotateProviderTransportOn429,
  selectProactiveApiKeyTransport,
  transientRetryPolicyFor,
} from "../providers/key-failover";
import { stampApiKeyAccountLabel } from "../providers/label";
import { affinityLaneFromRequest } from "./request-log-conversation";
import { publicOAuthErrorPresentation, OAuthAccountPausedError, OAuthLoginRequiredError, publicOAuthAuthenticationErrorMessage } from "../oauth";
import { AnthropicAccountCooldownError, formatAnthropicProviderForLog, anthropicRoutingFor } from "../oauth/anthropic-routing";
import { configuredAnthropicInstance } from "../providers/anthropic-instance";
import { resolveProtocolSettings } from "../protocols/settings";
import { addProtocolEntryReason, markProtocolBlocked } from "../protocols/trace";
import type { OcxProviderTransport } from "../providers/xai-transport";
import { preservesPhysicalComboProvider, resolveComboId } from "../combos";
import { captureRouteStaticPolicy, routedProviderConfig, routeModel, type RouteResult } from "../router";
import { POLICY_NAMESPACE, resolvePolicyProfileId } from "../routing/profile";
import type { AnthropicClientIdentity } from "../adapters/anthropic/client-identity";
import { logDroppedAnthropicBetas } from "../adapters/anthropic/beta-allowlist";
import type { OcxConfig, OcxProviderConfig, OcxUsage } from "../types";
import { resolveWireProtocolOverride } from "./adapter-resolve";
import {
  anthropicUsageToOcx,
  estimateClaudeRequestTokens,
  resolvePassthroughBodyGuard,
  sanitizePassthroughToolCallIds,
  tapAnthropicSseForLog,
} from "./messages-shared";
import { beginInferenceAttempt, type InferenceAttempt } from "./inference/attempt";
import type { ComboProtocolSource } from "./responses/core-combo-native";
import type { ProtocolEnvelope } from "../protocols/envelope";
import { observeNativeMessagesReplayBoundary, preflightNativeMessagesResponse } from "./messages-native-preflight";
import { createFinalRequestLog, type FinalRequestLogMeta } from "./inference/final-log";
import { registerTurn, unregisterTurn } from "./lifecycle";
import { nativeMessagesDeclineReason, type NativeMessagesSelector } from "./messages-native-eligibility";
import { messagesSecondaryInstanceUnavailable } from "./messages-native-selector";
import { guardNativeMessagesEmptyToolCatalog, nativeMessagesClientToolName, nativeMessagesInlineToolNames } from "./messages-native-tool-guard";
import { UNDECLARED_TOOL_CALL_ERROR_CODE, undeclaredToolCallMessage } from "./responses-undeclared-tool-guard";
import {
  nativeOAuthBindingIsCurrent,
  NativeOAuthSelectionChangedError,
  NativeOAuthModelRouteError,
  resolveNativeOAuthBindingForInstance,
  restoreOAuthToolNamesInMessage,
  restoreOAuthToolNamesInSse,
  type NativeOAuthBinding,
} from "./messages-native-oauth";
import {
  noteProviderAttemptSend,
  recordAttemptRequestedEffort,
  recordFirstOutput,
  recordKeyWireAttemptUsage,
  type RequestLogContext,
} from "./request-log";
import { recordKeyAttemptFailure } from "./request-log-key-failure";
import { fetchWithHeaderTimeout, physicalSendObserverInit, providerFetch, safeHostLabel, sendWithConnectionPolicy } from "./responses/fetch-helpers";
import { isLocalUpstream } from "../lib/local-upstream";
import { resolveConnectTimeoutMs, resolveJsonBodyReadTimeoutMs } from "../stall-timeout";
import { anthropicBillingPhysicalFetch } from "../providers/anthropic-billing-ownership";
import { rotateAnthropicBillingKeyOnResponse } from "../providers/anthropic-billing-recovery";
import { linkAbortSignal } from "./responses/core-lifetime";
import { attachRequestSpendTracker } from "./responses/request-spend";
import { carryWorkflowRefusal, workflowRefusalResponse, workflowSendCeilingRefusalResponse } from "./workflow-refusal";
import { sseFieldValue } from "../lib/sse-decoder";
import { admissionModelDeniedResponse, projectAdmissionModelDeniedLog, type AdmissionModelScope } from "./admission-model-scope";
import { nativeMessagesToolScopeDenial } from "./messages-native-scope";
import { retainUpstreamMessagesRequestId } from "./messages-response-headers";

export {
  isNativeMessagesRouteEligible,
  nativeMessagesDeclineReason,
  type NativeMessagesDeclineReason,
} from "./messages-native-eligibility";

type Rec = Record<string, unknown>;

const MAX_NATIVE_MESSAGES_JSON_BYTES = 32 * 1024 * 1024;
const MAX_NATIVE_MESSAGES_ERROR_BYTES = 64 * 1024;

class NativeMessagesSpendRefusal extends Error {}

/** Project only proxy-owned workflow refusals onto the native Messages error wire. */
async function nativeWorkflowRefusalResponse(refusal: Response): Promise<Response> {
  const { error } = await refusal.json() as { error: { message: string; type: string; code?: string } };
  const projected = new Response(JSON.stringify(anthropicErrorBody(refusal.status, error.message, error.type, error.code)), {
    status: refusal.status, headers: refusal.headers,
  });
  if (isNonReplayableResponse(refusal)) markResponseNonReplayable(projected);
  return carryWorkflowRefusal(refusal, projected);
}

/** A rebuild for a destination that cannot carry the body's opaque state, under `reject`. */
class NativeOpaqueStateRefusal extends Error {
  constructor() {
    super("The selected route cannot carry thinking signatures or redacted_thinking blocks");
  }
}

function isRec(value: unknown): value is Rec {
  return value !== null && typeof value === "object" && !Array.isArray(value);
}

export interface HandleNativeMessagesOptions {
  req: Request;
  config: OcxConfig;
  logCtx: RequestLogContext;
  admission?: import("./auth-cors").DataPlaneAdmission;
  logIds?: { requestId: string; start: number; turnAdmissionLease?: AdmissionLease };
  /** The settled, eligible route (see `isNativeMessagesRouteEligible`). */
  route: RouteResult;
  /**
   * The Messages body after the ingress's managed-client steps. Owned by this lane from here
   * on: a fresh envelope copy when an envelope exists, otherwise the ingress's own body.
   */
  body: Rec;
  /** The selector the client sent, echoed on the request log. */
  requestedModel: string;
  /** The model the answer reports, when it differs from `requestedModel` (a combo the route directive chose). */
  echoModel?: string;
  translatorBudget: TranslatorBudget;
  /** The facts the ingress judged eligibility with; re-applied if key selection changes. */
  selector?: NativeMessagesSelector;
  /**
   * The caller's `anthropic-beta` header, handed over by the ingress. The builder keeps only
   * allowlisted values. Client identity is a separate bounded opaque handle.
   */
  callerAnthropicBeta?: string | null;
  clientIdentity?: AnthropicClientIdentity;
  sessionKey?: string | null;
  modelScope?: AdmissionModelScope;
}

export type NativeMessagesFinishLog = (status: number, message?: string, meta?: FinalRequestLogMeta) => void;
type FinishLog = NativeMessagesFinishLog;

interface NativeMessagesExecution extends HandleNativeMessagesOptions {
  finishLog: NativeMessagesFinishLog;
  sendBudget?: RequestExecutionBudget;
  comboDispatchPermit?: SingleUseDispatchPermit;
  turnAdmissionLease?: AdmissionLease;
  onFirstOutput?: () => void;
}

/** The combo retains routing, attempts and the final row; the native lane owns only this send. */
export function createNativeMessagesComboSource(input: Omit<HandleNativeMessagesOptions, "route" | "body"> & {
  envelope: ProtocolEnvelope;
}): ComboProtocolSource {
  return {
    inbound: "messages",
    envelope: input.envelope,
    selector: input.selector,
    dispatchNativeChild: child => runNativeMessagesAttempt({
      ...input, logIds: undefined, logCtx: child.childLog, route: child.route,
      body: nativeAnthropicProjection(child.body, input.translatorBudget),
      selector: { ...input.selector, routeSelector: String(child.body.model ?? "") },
      finishLog: child.finishLog, sendBudget: child.sendBudget,
      comboDispatchPermit: child.comboDispatchPermit, turnAdmissionLease: child.turnAdmissionLease,
      onFirstOutput: child.onFirstOutput,
    }, child.attemptHandle),
  };
}

/** Relay bytes unchanged, observing raw progress and recording first output once `ready`. */
function observeNativeMessagesChunks(
  body: ReadableStream<Uint8Array>, onFirst?: () => void, ready = () => true, onChunk?: () => void,
): ReadableStream<Uint8Array> {
  const reader = body.getReader();
  let seen = false;
  return new ReadableStream<Uint8Array>({
    async pull(controller) {
      const { done, value } = await reader.read();
      if (done) {
        controller.close();
        return;
      }
      if (value.byteLength > 0) {
        onChunk?.();
        if (!seen && onFirst && ready()) {
          seen = true;
          onFirst();
        }
      }
      controller.enqueue(value);
    },
    cancel(reason) {
      return reader.cancel(reason);
    },
  });
}

/** How far into a stream the `message_start` frame is looked for before relaying untouched. */
const MODEL_ECHO_SCAN_BYTES = 64 * 1024;

/** The `message_start` frame with `message.model` set to `model`, or undefined for any other frame. */
function messageStartWithModel(frame: string, model: string): { text: string; canonicalError: boolean } | undefined {
  const lines = frame.split(/\r\n|\r|\n/);
  const data = lines
    .map(line => sseFieldValue(line, "data"))
    .filter((value): value is string => value !== null)
    .join("\n");
  if (!data) return undefined;
  let parsed: unknown;
  try { parsed = JSON.parse(data); } catch { return undefined; }
  if (!isRec(parsed) || parsed.type !== "message_start" || !isRec(parsed.message)) return undefined;
  parsed.message.model = model;
  const replacement = `data: ${JSON.stringify(parsed)}`;
  // Keep the original canonical framing for error-tagged starts. The final filter
  // separately determines whether the upstream error tag qualifies for normalization.
  const event = lines.map(line => sseFieldValue(line, "event")).findLast(value => value !== null);
  if (event === "error") {
    return { text: `event: message_start\n${replacement}\n\n`, canonicalError: true };
  }
  let replaced = false;
  const text = frame.replace(/[^\r\n]*(?:\r\n|\r|\n)/g, line => {
    const ending = line.match(/(?:\r\n|\r|\n)$/)![0];
    if (sseFieldValue(line.slice(0, -ending.length), "data") === null) return line;
    // Retain a field on this line so adjacent CR and LF terminators cannot fuse.
    if (replaced) return "data:" + ending;
    replaced = true;
    return replacement + ending;
  });
  return { text, canonicalError: false };
}

/**
 * Echo the client's selector in `message_start.message.model`, as the translated lane does
 * (`responsesSseToAnthropicSse` is given the requested model). Works on bytes: frames before and
 * including `message_start` are split on the blank line, and everything after is relayed as is.
 */
export function echoRequestedModel(body: ReadableStream<Uint8Array>, model: string, frames: MessagesSseFrameContext): ReadableStream<Uint8Array> {
  const reader = body.getReader();
  const encoder = new TextEncoder();
  const decoder = new TextDecoder();
  let carry = EMPTY_BYTES;
  let carryLength = 0;
  let scanFrom = 0;
  let scanning = true;
  let scanned = 0;
  let pendingLf: "relay" | "drop" | undefined;
  const take = (suffix: Uint8Array): Uint8Array => {
    if (carryLength === 0) return suffix;
    const frame = joinSseFrameBytes([carry.subarray(0, carryLength), suffix]);
    carry = EMPTY_BYTES;
    carryLength = 0;
    return frame;
  };
  const retain = (suffix: Uint8Array): void => {
    if (suffix.byteLength === 0) return;
    const nextLength = carryLength + suffix.byteLength;
    if (nextLength > carry.byteLength) {
      const grown = new Uint8Array(Math.min(MODEL_ECHO_SCAN_BYTES, Math.max(1024, nextLength, carry.byteLength * 2)));
      grown.set(carry.subarray(0, carryLength));
      carry = grown;
    }
    carry.set(suffix, carryLength);
    carryLength = nextLength;
  };
  return new ReadableStream<Uint8Array>({
    async pull(controller) {
      // Keep reading until there is something to enqueue or the upstream closes.
      while (true) {
        const { done, value: chunk } = await reader.read();
        if (done) {
          if (carryLength > 0) controller.enqueue(take(EMPTY_BYTES));
          controller.close();
          return;
        }
        if (chunk.byteLength === 0) continue;
        const scanBytes = Math.min(chunk.byteLength, MODEL_ECHO_SCAN_BYTES - scanned);
        if (scanning) scanned += scanBytes;
        let continuationBytes = 0;
        let value = chunk;
        let relayedLf = false;
        if (pendingLf && chunk[0] === 10) {
          relayedLf = pendingLf === "relay";
          if (relayedLf) controller.enqueue(chunk.subarray(0, 1));
          continuationBytes = 1;
          value = chunk.subarray(1);
        }
        pendingLf = undefined;
        if (value.byteLength === 0) {
          if (scanned === MODEL_ECHO_SCAN_BYTES) scanning = false;
          if (relayedLf) return;
          continue;
        }
        if (!scanning) {
          controller.enqueue(value);
          return;
        }
        const carried = carryLength;
        const length = carried + Math.max(0, scanBytes - continuationBytes);
        const byteAt = (index: number): number => index < carried ? carry[index]! : value[index - carried]!;
        let start = 0;
        let index = scanFrom;
        while (index < length) {
          const delimiter = sseDelimiterLengthAt(index, length, byteAt);
          if (delimiter === undefined) break;
          if (delimiter === 0) {
            index += 1;
            continue;
          }
          const end = index + delimiter;
          const endsOnCr = end === length && byteAt(end - 1) === 13;
          const frame = take(value.subarray(Math.max(0, start - carried), end - carried));
          start = end;
          index = end;
          const decoded = decoder.decode(frame);
          const rewritten = messageStartWithModel(decoded, model);
          // A bare CR completes the frame now. A later LF belongs to that same delimiter.
          if (endsOnCr) {
            if (end - carried === value.byteLength) {
              pendingLf = rewritten?.canonicalError ? "drop" : "relay";
            } else if (rewritten?.canonicalError && value[end - carried] === 10) {
              start += 1;
            }
          }
          if (rewritten === undefined) {
            controller.enqueue(frame);
            continue;
          }
          const bytes = encoder.encode(rewritten.text);
          if (rewritten.canonicalError && messagesSseEchoRenamedError(decoded)) frames.register(bytes, { echoRenamedError: true });
          controller.enqueue(bytes);
          scanning = false;
          break;
        }
        const suffix = value.subarray(Math.max(0, start - carried));
        if (!scanning || scanned === MODEL_ECHO_SCAN_BYTES) {
          scanning = false;
          const remaining = take(suffix);
          if (remaining.byteLength > 0) controller.enqueue(remaining);
          if (relayedLf || start > 0 || remaining.byteLength > 0) return;
          continue;
        }
        scanFrom = index - start;
        retain(suffix);
        if (relayedLf || start > 0) return;
      }
    },
    cancel(reason) {
      carry = EMPTY_BYTES;
      carryLength = 0;
      return reader.cancel(reason);
    },
  });
}

/** A minimal valid Messages stream for a streaming caller whose upstream answered JSON. */
function messageAsSse(message: Rec): string {
  const frames: string[] = [];
  const emit = (name: string, data: Rec) => frames.push(`event: ${name}\ndata: ${JSON.stringify(data)}\n\n`);
  emit("message_start", { type: "message_start", message: { ...message, content: [], stop_reason: null } });
  const blocks = Array.isArray(message.content) ? message.content.filter(isRec) : [];
  blocks.forEach((block, index) => {
    emit("content_block_start", { type: "content_block_start", index, content_block: block });
    emit("content_block_stop", { type: "content_block_stop", index });
  });
  emit("message_delta", {
    type: "message_delta",
    delta: { stop_reason: message.stop_reason ?? "end_turn", stop_sequence: message.stop_sequence ?? null },
    usage: message.usage ?? {},
  });
  emit("message_stop", { type: "message_stop" });
  return frames.join("");
}

/** Reject a body the Messages API cannot carry before anything is sent. */
async function prepareNativeBody(body: Rec, signal: AbortSignal): Promise<void> {
  if (!Array.isArray(body.messages)) return;
  // The adapter normally owns these; this lane bypasses it, so it runs the same steps as the
  // caller-forward passthrough: tier-normalize and guard images, then repair tool-call ids.
  await normalizeAnthropicImages(body.messages, { abortSignal: signal });
  enforceAnthropicImageLimits(body.messages);
  sanitizePassthroughToolCallIds(body.messages);
}

/**
 * Handle one eligible Messages request natively. Opens the attempt, owns the request's final
 * log row, and answers in the Messages wire (SSE or JSON per the caller's own `stream`).
 * Deliberately not `async`: an extra await turn changes when the relayed stream is first pulled.
 */
export function handleNativeMessages(options: HandleNativeMessagesOptions): Promise<Response> {
  const { logCtx, logIds, route } = options;
  const attemptHandle = beginInferenceAttempt(logCtx, { provider: route.providerName, model: route.modelId, adapter: "anthropic" });
  const finalLog = createFinalRequestLog(logIds, logCtx);
  const finishLog: FinishLog = (status, message, meta = { closeReason: "non_stream" }) => {
    if (finalLog.finished()) return;
    if (message) logCtx.upstreamError = redactSecretString(message).slice(0, 500);
    finalLog.finish(status, meta);
  };
  return runNativeMessagesAttempt({ ...options, finishLog }, attemptHandle);
}

/** Execute on the ingress or combo's existing attempt, never opening a second final-row owner. */
async function runNativeMessagesAttempt(options: NativeMessagesExecution, attemptHandle: InferenceAttempt): Promise<Response> {
  const { req, config, logCtx, logIds, route, body, requestedModel, translatorBudget, finishLog } = options;
  const onFirstOutput = options.onFirstOutput ?? (logIds ? () => recordFirstOutput(logCtx, logIds.start) : undefined);
  const nativeInstance = configuredAnthropicInstance(config, route.providerName);
  const requestedStream = body.stream === true;
  const clientDeclaredEmptyToolCatalog = Array.isArray(body.tools) && body.tools.length === 0;
  const declaredInlineTools = clientDeclaredEmptyToolCatalog ? nativeMessagesInlineToolNames(body) : undefined;
  logCtx.inboundProtocol = "messages";
  logCtx.model = route.modelId;
  logCtx.provider = route.providerName;
  logCtx.spendPoolId = route.providerName;
  logCtx.providerAdapter = route.provider.adapter;
  logCtx.requestedModel = requestedModel;
  if (route.routeReason === "model-alias" || route.modelId !== requestedModel) logCtx.requestedAlias = requestedModel;
  logCtx.requestedServiceTier = typeof body.service_tier === "string" ? body.service_tier : undefined;
  if (logCtx.requestedEffort === undefined) {
    const effort = effortFromOutputConfig(body.output_config);
    const thinking = isRec(body.thinking) ? body.thinking : undefined;
    const budget = thinking?.budget_tokens;
    if (effort !== undefined) logCtx.requestedEffort = effort;
    else if (thinking?.type === "disabled") logCtx.requestedEffort = "none";
    else if (thinking?.type === "enabled" && typeof budget === "number"
      && Number.isSafeInteger(budget) && budget > 0) logCtx.requestedEffort = `budget:${budget}`;
  }
  // Reserve spend the way native Chat does: an input estimate that never enters usage, and the
  // caller's own output ceiling.
  if (logCtx.usageLogInputTokens === undefined) {
    logCtx.spendInputEstimateTokens = estimateClaudeRequestTokens(body, requestedModel);
  }
  if (typeof body.max_tokens === "number" && body.max_tokens > 0) {
    logCtx.spendOutputCeilingTokens = Math.trunc(body.max_tokens);
  }

  attemptHandle.seal(logCtx.accountLogLabel);
  recordAttemptRequestedEffort(logCtx);
  const { attempt } = attemptHandle;
  const bindUsage = (usage: OcxUsage | undefined) => {
    if (!usage) return;
    if (!recordKeyWireAttemptUsage(logCtx, usage)) {
      logCtx.usage = usage;
      attempt.usage = usage;
    }
  };
  // A combo child whose preflight accepted has committed: a later failure must not replay,
  // even when no content frame (and so no first output) was seen.
  let committed = false;
  const fail = (status: number, message: string, type?: string, code?: string, originalMessage = message): Response => {
    const safeMessage = redactSecretString(message);
    finishLog(status, safeMessage);
    const legacy = anthropicErrorBody(status, redactSecretString(originalMessage), type, code);
    const response = ownedAnthropicErrorResponse({ status, type, code, evidence: { status, message: originalMessage },
      localError: markProxyError({ message: safeMessage }), overflow: messagesRelayedOverflowPresentation("", legacy) }, legacy);
    if (committed) markResponseNonReplayable(response);
    return response;
  };
  const failEvidence = (status: number, message: string, type: string, contract?: "transport_failed", overflow?: boolean, rawError?: Rec, legacyBody?: Rec): Response => {
    finishLog(status, message);
    const legacy = legacyBody ?? anthropicErrorBody(status, redactSecretString(message), type);
    const response = ownedAnthropicErrorResponse({ status, type, evidence: { status, message, type: typeof rawError?.type === "string" ? rawError.type : undefined, code: typeof rawError?.code === "string" ? rawError.code : undefined }, contract,
      overflow: overflow ?? messagesRelayedOverflowPresentation("", legacy) }, legacy);
    if (committed) markResponseNonReplayable(response);
    return response;
  };
  const preparationFailure = (error: unknown): Response => {
    const message = error instanceof Error ? error.message : String(error);
    finishLog(400, message);
    return fail(400, error instanceof Error && isProxyError(error) ? error.message : "The proxy could not translate the Messages request.", "invalid_request_error", undefined, message);
  };
  // After preflight, first output waits for a content frame the log tap has seen.
  const watchFirstOutput = (body: ReadableStream<Uint8Array>) => onFirstOutput
    ? observeNativeMessagesChunks(body, onFirstOutput, () => logCtx.generationStartedAt !== undefined) : body;
  const localOAuthFailure = (error: unknown): Response | null => {
    if (error instanceof AnthropicAccountCooldownError) {
      const scope = error.routePosition === undefined ? "All Anthropic OAuth accounts" : "Anthropic OAuth accounts for this model route";
      const reset = error.usageLimit?.resetsAt;
      const date = typeof reset === "number" && Number.isFinite(reset) && reset >= 0 && reset <= 253402300799999
        ? new Date(Math.floor(reset / 1000) * 1000).toISOString().replace(".000Z", "Z") : undefined;
      const message = error.usageLimit === null ? `${scope} are temporarily rate-limited`
        : `${scope} have reached their usage limit${date ? `; the first one resets at ${date}` : ""}`;
      finishLog(429, error.message);
      const response = markProxyError(fail(429, message, "rate_limit_error"), date === undefined ? undefined : "usage");
      const advice = error.retryAfterSeconds !== null ? formatClientRetryAfter(error.retryAfterSeconds) : undefined;
      if (advice !== undefined) response.headers.set("retry-after", advice);
      return response;
    }
    if (error instanceof OAuthAccountPausedError) return markProxyError(fail(403, publicOAuthAuthenticationErrorMessage(error), "permission_error"), publicOAuthErrorPresentation(error));
    if (error instanceof OAuthLoginRequiredError) return markProxyError(fail(401, publicOAuthAuthenticationErrorMessage(error), "authentication_error"), publicOAuthErrorPresentation(error));
    return null;
  };

  if (route.provider.authMode === "oauth" && !nativeInstance) {
    return fail(401, "Configured Anthropic OAuth instance is unavailable", "authentication_error");
  }

  const toolScopeDenial = nativeMessagesToolScopeDenial(options.modelScope, route.providerName, requestedModel, body);
  if (toolScopeDenial) {
    projectAdmissionModelDeniedLog(toolScopeDenial, logCtx);
    finishLog(403, toolScopeDenial.message);
    return admissionModelDeniedResponse(toolScopeDenial);
  }
  let routeTarget: string;
  try { routeTarget = resolveAnthropicMessagesUrl(route.provider); }
  catch { return fail(400, "Invalid Anthropic Messages destination", "invalid_request_error"); }

  try {
    await prepareNativeBody(body, req.signal);
  } catch (error) {
    if (req.signal.aborted) return fail(499, "Client cancelled request", "api_error");
    if (isTranslatorBudgetExceededError(error)) {
      return fail(413, "request translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
    }
    // An AnthropicRequestError (empty tool id) or an image the guard cannot carry.
    return preparationFailure(error);
  }

  const upstream = new AbortController();
  const cleanupAbort = linkAbortSignal(upstream, req.signal);
  let streamTurnRegistered = false;
  const transferTurnToStream = () => {
    const lease = options.turnAdmissionLease ?? logIds?.turnAdmissionLease;
    if (!lease || typeof (lease as { bindAbortController?: unknown }).bindAbortController !== "function") return;
    registerTurn(upstream, lease);
    streamTurnRegistered = true;
  };
  const releaseStreamTurn = () => {
    if (!streamTurnRegistered) return;
    streamTurnRegistered = false;
    unregisterTurn(upstream);
  };
  // Set only at the final send boundary; queued refresh can replace the destination.
  let localUpstream = false;
  // An OAuth route is served by the account the existing OAuth selection commits now, at
  // dispatch; the token lives on this lane's provider copy only, never on the shared route.
  let oauthBinding: NativeOAuthBinding | undefined;
  const oauthProvider = (binding: NativeOAuthBinding): OcxProviderConfig => ({ ...route.provider, apiKey: binding.snapshot.accessToken });
  if (route.provider.authMode === "oauth") {
    try {
      oauthBinding = await resolveNativeOAuthBindingForInstance(nativeInstance!, config, { routeTarget, sessionKey: options.sessionKey, model: route.modelId });
    } catch (error) {
      cleanupAbort();
      upstream.abort();
      if (req.signal.aborted) return fail(499, "Client cancelled request", "api_error");
      const localRefusal = localOAuthFailure(error);
      if (localRefusal) return localRefusal;
      if (error instanceof NativeOAuthSelectionChangedError) return fail(409, error.message, "api_error");
      if (error instanceof NativeOAuthModelRouteError) return fail(400, "Invalid Anthropic model routes", "invalid_request_error", undefined, error.message);
      return markProxyError(fail(401, publicOAuthAuthenticationErrorMessage(error), "authentication_error"), publicOAuthErrorPresentation(error));
    }
  } else {
    // Same pre-dispatch key preference every direct send path applies (see chat-native.ts).
    try {
      const affinityLane = affinityLaneFromRequest(req, logCtx, config, options.admission);
      if (affinityLane) logCtx.affinityLane = affinityLane;
      const lastMessage = Array.isArray(body.messages) ? body.messages.at(-1) : undefined;
      const continuation = Array.isArray(lastMessage?.content)
        && lastMessage.content.some((block: unknown) => !!block && typeof block === "object" && (block as { type?: unknown }).type === "tool_result");
      const proactiveKeyProvider = selectProactiveApiKeyTransport(config, route.providerName, route.provider, undefined, Date.now(), { lane: affinityLane, model: route.modelId, log: logCtx, continuation });
      if (proactiveKeyProvider) route.provider = proactiveKeyProvider;
      if (isProviderApiKeyPaused(route.provider)) throw new ProviderApiKeyPausedError();
    } catch (error) {
      cleanupAbort(); upstream.abort();
      const localRefusal = localOAuthFailure(error);
      if (localRefusal) return localRefusal;
      if (!(error instanceof RequestPacingQueueOverloadError)) throw error;
      logCtx.terminalSource = "synthetic";
      const refusal = markProxyError(fail(429, error.message, "rate_limit_error"), error.reason === "keys_exhausted" ? undefined : "pacing");
      const advice = formatClientRetryAfter(error.retryAfterSeconds);
      if (advice !== undefined) refusal.headers.set("Retry-After", advice);
      return refusal;
    }
  }
  let activeProvider: OcxProviderConfig = oauthBinding ? oauthProvider(oauthBinding) : route.provider;
  stampApiKeyAccountLabel(logCtx, route.providerName, activeProvider);
  if (oauthBinding) logCtx.provider = formatAnthropicProviderForLog(route.providerName, oauthBinding.snapshot.accountId, config);
  const spendTracker = options.sendBudget ? undefined : attachRequestSpendTracker(req, logCtx);
  const physicalBudget = options.sendBudget ?? createInferenceSendBudget(req, logCtx);
  // Freeze funding before any refusal cools an account; selection still checks live eligibility.
  const fundedAccounts = oauthBinding ? Math.min(new Set([
    oauthBinding.snapshot.accountId,
    ...anthropicRoutingFor(oauthBinding.instance).getEligibleAnthropicAccounts(Date.now(), route.modelId),
  ]).size, GENERIC_OAUTH_MAX_ACCOUNTS_PER_REQUEST) : 0;
  const oauthFailoverLimit = Math.max(ANTHROPIC_POOL_MAX_FAILOVERS_PER_REQUEST, fundedAccounts - 1);
  if (oauthBinding) expandInferenceOAuthSendBudget(physicalBudget, fundedAccounts);
  // Observe-only spend is already booked by the tracker; count billing sends without booking again.
  const priorSends = Math.max(0, physicalBudget.used - (options.comboDispatchPermit ? 1 : 0));
  let physicalSendsUsed = 0;
  let activeRequest: AnthropicMessagesPassthroughRequest;
  let retainedRequestBytes = 0;
  const releaseRetainedRequest = () => {
    if (retainedRequestBytes === 0) return;
    translatorBudget.releaseRetained(retainedRequestBytes, { kind: "request_copies" });
    retainedRequestBytes = 0;
  };
  const retainRequest = (request: AnthropicMessagesPassthroughRequest) => {
    const bytes = Buffer.byteLength(request.body);
    translatorBudget.chargeRetained(bytes, { kind: "request_copies" });
    retainedRequestBytes = bytes;
  };
  // Every rebuild reads the same `body`; the builder copies and never mutates it, so no
  // credential, header or stripped block from an earlier build reaches the next one, and a build
  // for another credential domain decides opaque state from the full source again.
  const rejectUnrepresentable = resolveProtocolSettings(config).unrepresentable === "reject";
  const buildActiveRequest = () => {
    // Exact combo replay preserves TTL-less breakpoints; an explicit operator TTL still wins.
    const wireConfig = options.sendBudget && config.cacheRetention === undefined ? { ...config, cacheRetention: "short" as const } : config;
    const built = buildAnthropicMessagesPassthroughRequest(activeProvider, route.modelId, body, wireConfig, {
      callerAnthropicBeta: options.callerAnthropicBeta,
      providerAccountUuid: oauthBinding?.providerAccountUuid,
      clientIdentity: options.clientIdentity,
      sessionKey: oauthBinding?.sessionKey ?? options.sessionKey,
    });
    if (built.strippedOpaqueState && rejectUnrepresentable) throw new NativeOpaqueStateRefusal();
    if (built.droppedBetas) {
      addProtocolEntryReason(logCtx, "anthropic-beta-dropped");
      logDroppedAnthropicBetas({ droppedNames: built.droppedBetaNames, droppedUnrecognized: built.droppedUnrecognizedBetas });
    }
    if (built.strippedOpaqueState) addProtocolEntryReason(logCtx, "opaque-state-stripped");
    return built;
  };
  const rebuildFor = (provider: OcxProviderConfig) => {
    activeProvider = provider;
    stampApiKeyAccountLabel(logCtx, route.providerName, activeProvider);
    releaseRetainedRequest();
    activeRequest = buildActiveRequest();
    retainRequest(activeRequest);
  };
  try {
    activeRequest = buildActiveRequest();
    retainRequest(activeRequest);
  } catch (error) {
    releaseRetainedRequest();
    cleanupAbort();
    upstream.abort();
    if (isTranslatorBudgetExceededError(error)) {
      return fail(413, "request translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
    }
    if (error instanceof NativeOpaqueStateRefusal) {
      // Nothing was sent: this is a refusal before any upstream send.
      markProtocolBlocked(logCtx, { inbound: "messages", reasonCodes: ["feature-unrepresentable", "opaque-state-stripped"] });
      logCtx.errorCode = "unsupported_feature";
      return fail(400, error.message, "invalid_request_error", "unsupported_feature");
    }
    return preparationFailure(error);
  }

  // One inbound request owns one transient send allowance, captured before any key rotation.
  const requestTransientPolicy = transientRetryPolicyFor(activeProvider);
  let transientSendsUsed = 0;
  const sharedSendCap = requestTransientPolicy?.attempts ?? TRANSIENT_RETRY_MAX_ATTEMPTS;
  let initialSendStarted = false;
  const remainingTransientSends = (): number => Math.min(
    requestTransientPolicy ? Math.max(0, requestTransientPolicy.attempts - transientSendsUsed) : Number.POSITIVE_INFINITY,
    options.sendBudget ? Math.min(sharedSendCap, options.sendBudget.remainingBaseSends(sharedSendCap)
      + (!initialSendStarted && options.comboDispatchPermit ? 1 : 0)) : Number.POSITIVE_INFINITY,
  );
  const oauthPhysicalSendAvailable = (): boolean =>
    Math.max(priorSends + physicalSendsUsed, physicalBudget.used, physicalBudget.physicalStarted ?? 0)
      < Math.min(physicalBudget.policy.maxTotalModelSends, physicalBudget.physicalLimit ?? Number.POSITIVE_INFINITY)
        + (!initialSendStarted && options.comboDispatchPermit ? 1 : 0);
  const transientSendAvailable = (): boolean => remainingTransientSends() > 0;
  const selector: NativeMessagesSelector = options.selector ?? {};
  const nativeOAuthRouteIsCurrent = (binding: NativeOAuthBinding): boolean => {
    const provider = config.providers[binding.instance];
    if (configuredAnthropicInstance(config, binding.instance) !== binding.instance || !provider) return false;
    // Same normalization as the bound target (nativeOAuthRouteTarget): compare routed destinations.
    let routed: OcxProviderConfig;
    try { routed = routedProviderConfig(binding.instance, provider); } catch { return false; }
    return resolveAnthropicMessagesUrl(routed) === routeTarget
      && nativeMessagesDeclineReason({ ...route, provider: routed }, body, config, selector) === undefined;
  };

  // Keep the actual failed wire identity if a later recovery is refused before sending.
  // A 429 remains eligible for explicit same-key retries until that ladder is finished.
  let pendingDistributedFailure: (() => void) | undefined;
  let distributedResponseOwnsKey = true;
  const settleDistributedFailure = () => {
    const record = pendingDistributedFailure;
    pendingDistributedFailure = undefined;
    record?.();
  };
  /** Send the native Messages request under the transient retry policy, re-validating the selected OAuth account or API key at the dispatch boundary. */
  const send = async (recovery?: "rate-limit-429" | "oauth-401" | "oauth-account-403" | "key-429" | "key-401"): Promise<Response> => {
    const remaining = remainingTransientSends();
    if (oauthBinding && !oauthPhysicalSendAvailable()) throw new SendBudgetExhaustedError();
    if (options.sendBudget && remaining <= 0) throw new SendBudgetExhaustedError();
    if (requestTransientPolicy && remaining <= 0) {
      throw markProxyError(new Error("native Messages transient send budget exhausted before recovery dispatch"));
    }
    const fetchWithPolicy = requestTransientPolicy ? fetchWithTransientRetry : fetchWithResetRetry;
    const request = activeRequest;
    return await fetchWithPolicy(
      (transportRecovery?: UpstreamSendRecovery) => fetchWithHeaderTimeout(
        request.url,
        applyUpstreamRecoveryInit({ method: "POST", headers: request.headers, body: request.body }, transportRecovery),
        upstream.signal,
        0, // Final physical send owns the header clock after queued credential/destination refresh.
        requestedStream,
        providerFetch(activeProvider, undefined, {
          providerName: route.providerName,
          modelId: route.modelId,
          onPhysicalDispatch: options.sendBudget && options.comboDispatchPermit ? () => {
            if (!physicalBudget.spendEnforced) {
              // As native Chat: an observe-only start was already claimed before the send.
              reportDispatchSends(options.sendBudget!, 1, options.comboDispatchPermit, true);
              initialSendStarted = true;
            }
          } : undefined,
          dispatchOverride: async (_input, init, execute, reacquirePacing) => {
            if (oauthBinding) {
              if (!nativeOAuthRouteIsCurrent(oauthBinding)) throw new NativeOAuthSelectionChangedError();
              // The OAuth twin of the key check below: re-resolve through the same selection
              // owner when the committed account or its credential moved since the build.
              for (let attempt = 0; !nativeOAuthBindingIsCurrent(oauthBinding); attempt++) {
                if (attempt >= 3) throw new NativeOAuthSelectionChangedError();
                try {
                  oauthBinding = await resolveNativeOAuthBindingForInstance(nativeInstance!, config, { routeTarget, sessionKey: options.sessionKey, model: route.modelId });
                } catch (error) {
                  if (error instanceof OAuthAccountPausedError || error instanceof OAuthLoginRequiredError
                    || error instanceof AnthropicAccountCooldownError || error instanceof NativeOAuthModelRouteError) throw error;
                  throw new NativeOAuthSelectionChangedError();
                }
                rebuildFor(oauthProvider(oauthBinding));
                logCtx.provider = formatAnthropicProviderForLog(route.providerName, oauthBinding.snapshot.accountId, config);
              }
            } else {
              for (let attempt = 0; !providerApiKeySelectionIsCurrent(config, route.providerName, activeProvider); attempt++) {
                if (!providerAffinityGenerationIsCurrent(config, route.providerName, activeProvider)) throw markProxyError(new Error("Provider affinity selection is no longer available"));
                if (attempt >= 3) throw markProxyError(new Error("Provider key selection changed repeatedly before native Messages dispatch"));
                let current = resolveCurrentProviderApiKeyTransport(config, route.providerName, activeProvider);
                if (!current && activeProvider.requestPacing?.enabled
                  && (activeProvider.requestPacing.keys || activeProvider.requestPacing.keySelection === "round-robin")) {
                  const fresh = resolveCurrentProviderApiKeyTransport(config, route.providerName, { ...activeProvider, _apiKeyAttempt: undefined });
                  if (fresh) current = selectProactiveApiKeyTransport(config, route.providerName, fresh) ?? fresh;
                }
                if (!current || nativeMessagesDeclineReason({ ...route, provider: current }, body, config, selector) !== undefined) {
                  if (!current && isProviderApiKeyPaused(config.providers[route.providerName])) throw new ProviderApiKeyPausedError();
                  throw markProxyError(new Error("Provider key selection is no longer available for native Messages"));
                }
                rebuildFor(current);
                if (current.requestPacing?.enabled
                  && (current.requestPacing.keys || current.requestPacing.keySelection === "round-robin")) {
                  if (!reacquirePacing) throw markProxyError(new Error("Refreshed native Messages dispatch requires pacing readmission"));
                  await reacquirePacing(current, route.modelId);
                }
              }
            }
            // The retry closure may hold a pre-reselection request: send the current one whole.
            const wire = activeRequest;
            const headers = new Headers(wire.headers);
            const encoding = new Headers(init.headers).get("accept-encoding");
            if (!headers.has("accept-encoding") && encoding) headers.set("accept-encoding", encoding);
            const sendingBinding = oauthBinding;
            const snapshot = sendingBinding?.snapshot;
            const providerAccountUuid = sendingBinding?.providerAccountUuid;
            const writerGeneration = snapshot ? captureConfigGeneration() : 0;
            const ownsBearer = snapshot && snapshot.provider === nativeInstance
              && headers.get("authorization") === `Bearer ${snapshot.accessToken}` && !headers.has("x-api-key");
            if (oauthBinding && (!ownsBearer || wire.url !== oauthBinding.routeTarget
              || !nativeOAuthRouteIsCurrent(oauthBinding) || !nativeOAuthBindingIsCurrent(oauthBinding))) {
              throw new NativeOAuthSelectionChangedError();
            }
            const releaseFamily = snapshot && oauthBinding ? anthropicModelQuotaFor(oauthBinding.instance).claimAnthropicFamilyRevalidation(snapshot.accountId, route.modelId) : () => {};
            if (!releaseFamily) throw new AnthropicAccountCooldownError(1);
            const observeAutoPause = captureAccountAutoPauseAttempt(route.providerName, activeProvider, { config, oauth: sendingBinding?.snapshot }, headers);
            const selection = activeProvider._apiKeyAttempt;
            let dispatched: Response;
            const spendReport = createPhysicalSendReporter(physicalBudget, () => ({
              poolId: logCtx.spendPoolId ?? route.providerName, identityId: logCtx.accountLogLabel,
            }), options.comboDispatchPermit);
            let sendOwner: AnthropicPhysicalSendOwnership | null = null;
            try {
              if (init.signal?.aborted) throw init.signal.reason;
              // Capture before either accounting path so this request retains its start policy.
              if (spendReport.beforeSend && !spendReport.beforeSend()) {
                if ((logCtx.spendTracker as { refusals?: number } | undefined)?.refusals) throw new NativeMessagesSpendRefusal();
                throw new SendBudgetExhaustedError();
              }
              if (options.sendBudget) {
                // A combo child shares the request's send budget; a tracker of its own would book twice.
                if (physicalBudget.spendEnforced) initialSendStarted = true;
                else if (initialSendStarted && options.sendBudget.remainingBaseSends(sharedSendCap) <= 0) throw new SendBudgetExhaustedError();
              } else if (spendTracker) {
                if (!physicalBudget.spendEnforced && !spendTracker.charge({ workflowAlreadyCounted: true })) throw new NativeMessagesSpendRefusal();
              }
              // Last synchronous ownership reservation before the physical fetch. Retain this
              // exact incarnation across its await; a returned response can never reserve one.
              sendOwner = snapshot ? captureAnthropicPhysicalSendOwnership(snapshot) : null;
              if (snapshot && !sendOwner) throw new NativeOAuthSelectionChangedError();
              const physicalFetch = ((input, physicalInit) => sendWithConnectionPolicy(
                passiveKeyPhysicalFetch(distributedKeyPhysicalFetch(
                  anthropicBillingPhysicalFetch((activeProvider as OcxProviderTransport).fetch ?? execute, route.providerName, activeProvider),
                  route.providerName, activeProvider, wire.url, activeProvider.apiKeyTransport === "bearer" ? "authorization" : "x-api-key"), config, route.providerName, activeProvider),
                String(input), physicalInit, { providerName: route.providerName, provider: activeProvider },
              )) as typeof fetch;
              localUpstream = isLocalUpstream(wire.url);
              dispatched = await fetchWithHeaderTimeout(
                wire.url,
                applyUpstreamRecoveryInit({ ...init, method: "POST", headers, body: wire.body,
                  ...physicalSendObserverInit(observeAutoPause, wire.url, () => {
                    noteProviderAttemptSend(logCtx, route.providerName, activeProvider, logCtx.usageLogInputTokens, transportRecovery ?? recovery);
                    physicalSendsUsed++;
                  }),
                }, transportRecovery),
                init.signal ?? upstream.signal,
                resolveConnectTimeoutMs(config.connectTimeoutMs, { localUpstream }),
                requestedStream, physicalFetch,
              );
            } finally {
              try { if (physicalBudget.spendEnforced) spendReport(1); }
              finally {
                try { spendReport.close?.(); }
                finally { releaseFamily(); }
              }
            }
            if (ownsBearer && snapshot && sendOwner && sendingBinding) {
              try {
                const current = getAccountCredentialWithStatus(snapshot.provider, snapshot.accountId);
                if (anthropicPhysicalSendOwnershipIsCurrent(sendOwner) && nativeOAuthRouteIsCurrent(sendingBinding)
                  && current && !current.needsReauth && current.credential.access === snapshot.accessToken
                  && credentialGeneration(current.credential) === snapshot.generation
                  && current.credential.accountId === providerAccountUuid) {
                  bindAnthropicRefusalCredentialForSend(dispatched, sendOwner, providerAccountUuid);
                  recordAnthropicAccountQuotaFromHeadersForInstance(nativeInstance!, snapshot.accountId, dispatched.headers, writerGeneration, dispatched.status, route.modelId, snapshot.generation, sendOwner.credentialKey);
                }
              } catch { /* Passive observation must not fail the response. */ }
            }
            const ownsDistributedKey = !selection?.distributed
              || distributedKeyResponseOwner(dispatched, config, route.providerName) !== null;
            distributedResponseOwnsKey = ownsDistributedKey;
            const failureStatus = dispatched.status;
            pendingDistributedFailure = selection?.distributed && ownsDistributedKey && (failureStatus === 401 || failureStatus === 429)
              && !isNonReplayableResponse(dispatched)
              ? () => { recordDistributedResponseFailure(dispatched, config, route.providerName, req.signal); }
              : undefined;
            if (!dispatched.ok) await recordKeyAttemptFailure(logCtx, dispatched, init.signal ?? upstream.signal);
            return observeAutoPause ? observeAutoPause(dispatched) : dispatched;
          },
        }),
      ),
      {
        abortSignal: upstream.signal,
        label: safeHostLabel(request.url),
        ...(requestTransientPolicy
          ? {
            attempts: remaining,
            onSendsConsumed: (sends: number) => { transientSendsUsed += Math.max(0, sends); },
          }
          : {}),
      },
    );
  };
  const discard = (response: Response) => {
    try { void response.body?.cancel().catch(() => {}); } catch { /* already closed */ }
  };

  let response: Response;
  try {
    response = await send();
    let retryPolicy: ReturnType<typeof rateLimitRetryPolicyFor> | undefined;
    let retries = 0;
    let billingRecoveryActive = false;
    const keySendAvailable = () => transientSendAvailable()
      && (!billingRecoveryActive || Math.max(physicalSendsUsed, physicalBudget.used) < physicalBudget.policy.maxTotalModelSends);
    let autoPauseMoves = 0;
    const recoveryPasses = activeProvider._apiKeyAttempt?.distributed ? activeProvider.apiKeyPool?.length ?? 1 : 1;
    for (let pass = 0; ; pass++) {
      const priorResponse = response;
      for (; wasAccountAutoPaused(response) && !oauthBinding && config.providers[route.providerName]?.apiKey && autoPauseMoves < 3 && keySendAvailable(); autoPauseMoves++) {
        const fresh = resolveCurrentProviderApiKeyTransport(config, route.providerName, { ...activeProvider, _apiKeyAttempt: undefined });
        const next = fresh && (selectProactiveApiKeyTransport(config, route.providerName, fresh) ?? fresh);
        if (!next) break;
        sealCredentialRefusal(logCtx, response.status, "key-401");
        try { void response.body?.cancel().catch(() => {}); } catch { /* closed */ }
        rebuildFor(next);
        response = await send("key-401");
      }

      // A credential-scoped 401 on a static key pool says nothing about the sibling keys.
      while (response.status === 401 && distributedResponseOwnsKey && hasKeyPoolFailover(activeProvider) && keySendAvailable()) {
        const rotated = rotateProviderTransportOn401(config, route.providerName, activeProvider, {
          now: Date.now(),
          attemptedKey: activeProvider.apiKey,
          attemptedSelection: activeProvider._apiKeyAttempt,
        });
        pendingDistributedFailure = undefined; // Rotation already records this physical failure.
        if (!rotated) break;
        sealCredentialRefusal(logCtx, response.status, "key-401");
        discard(response);
        rebuildFor(rotated);
        response = await send("key-401");
      }
      while ((response.status === 400 || response.status === 402) && distributedResponseOwnsKey && hasKeyPoolFailover(activeProvider)) {
        const rotated = await rotateAnthropicBillingKeyOnResponse(response, config, route.providerName, activeProvider, { signal: upstream.signal });
        if (!rotated) break;
        billingRecoveryActive = true;
        if (!keySendAvailable()) break;
        sealCredentialRefusal(logCtx, response.status, "key-401");
        discard(response);
        rebuildFor(rotated);
        response = await send("key-401");
      }
      if (pass === 0) retryPolicy = oauthBinding ? undefined : rateLimitRetryPolicyFor(activeProvider);
      // A refusal this proxy synthesized for a reset replay is not a provider rate limit.
      while (
        response.status === 429
        && distributedResponseOwnsKey
        && !isNonReplayableResponse(response)
        && retryPolicy && providerAffinityGenerationIsCurrent(config, route.providerName, activeProvider)
        && retries < retryPolicy.attempts
        && keySendAvailable()
      ) {
        retries += 1;
        for await (const _ of prepareSameTarget429Wait({
          body: response.body,
          signal: upstream.signal,
          delayMs: rateLimitRetryDelayMs(retryPolicy, response.headers.get("retry-after"), Date.now()),
        })) { /* pre-stream wait */ }
        if (upstream.signal.aborted) throw upstream.signal.reason;
        response = await send("rate-limit-429");
      }
      while (response.status === 429 && distributedResponseOwnsKey && !isNonReplayableResponse(response) && hasKeyPoolFailover(activeProvider)) {
        const rotated = rotateProviderTransportOn429(config, route.providerName, activeProvider, {
          retryAfter: response.headers.get("retry-after"),
          now: Date.now(),
          attemptedKey: activeProvider.apiKey,
          attemptedSelection: activeProvider._apiKeyAttempt,
        });
        pendingDistributedFailure = undefined; // Rotation already records this physical failure.
        if (!rotated) break;
        // Rotation already recorded the cooldown; keep the terminal 429 when no send remains.
        if (!keySendAvailable()) break;
        sealCredentialRefusal(logCtx, response.status, "key-429");
        discard(response);
        rebuildFor(rotated);
        response = await send("key-429");
      }
      if (response === priorResponse || isNonReplayableResponse(response) || !keySendAvailable()) break;
      // Billing class transitions share the physical-send ceiling; distributed-only
      // transitions additionally retain their original bounded pool-pass allowance.
      if (wasAccountAutoPaused(response) && autoPauseMoves < 3) continue;
      if (distributedResponseOwnsKey && (
        (billingRecoveryActive && [401, 429].includes(response.status))
        || (activeProvider.adapter === "anthropic" && hasKeyPoolFailover(activeProvider) && [400, 402].includes(response.status))
      )) continue;
      if (!activeProvider._apiKeyAttempt?.distributed || !distributedResponseOwnsKey
        || (response.status !== 401 && response.status !== 429) || pass + 1 >= recoveryPasses) break;
    }
    settleDistributedFailure();
    const oauthRetryKey = {};
    const triedAccountIds = new Set<string>();
    let oauthFailovers = 0;
    while (oauthBinding && (response.status === 429 || response.status === 403 || response.status === 401)) {
      const sendingBinding = oauthBinding;
      const expectedRecoverySelection = sendingBinding.selection;
      triedAccountIds.add(sendingBinding.snapshot.accountId);
      const nextAccountId = await rotateAnthropicAccountOnResponseForInstance(sendingBinding.instance, response, {
        config, accountId: sendingBinding.snapshot.accountId, model: route.modelId,
        sessionKey: sendingBinding.sessionKey, decision: sendingBinding.routeDecision,
        excludedAccountIds: triedAccountIds,
        currentDecision: () => {
          if (!nativeOAuthRouteIsCurrent(sendingBinding)) throw new NativeOAuthSelectionChangedError();
          const resolved = resolveAnthropicModelRouteForInstance(sendingBinding.instance, config, route.modelId);
          if (resolved.error) throw new NativeOAuthModelRouteError(`Invalid Anthropic model routes: ${resolved.error}`);
          return resolved.decision;
        },
        signal: upstream.signal, requestKey: oauthRetryKey,
        canRetry: transientSendAvailable() && oauthFailovers < oauthFailoverLimit && oauthPhysicalSendAvailable(),
      });
      if (!nextAccountId) break;
      const recovery = response.status === 401 ? "oauth-401" : response.status === 403 ? "oauth-account-403" : "rate-limit-429";
      discard(response);
      oauthBinding = await resolveNativeOAuthBindingForInstance(nativeInstance!, config, { routeTarget,
        sessionKey: options.sessionKey, model: route.modelId, candidateAccountId: nextAccountId,
        expectedRecoverySelection, expectedRecoveryRouteDecision: sendingBinding.routeDecision,
      });
      const changed = anthropicRefusalCredentialChanged(response, oauthBinding.snapshot);
      if (changed) sealCredentialRefusal(logCtx, response.status, recovery);
      oauthFailovers++;
      rebuildFor(oauthProvider(oauthBinding));
      logCtx.provider = formatAnthropicProviderForLog(route.providerName, oauthBinding.snapshot.accountId, config);
      response = await send(changed ? recovery : "rate-limit-429");
    }
  } catch (error) {
    releaseRetainedRequest();
    cleanupAbort();
    upstream.abort();
    if (req.signal.aborted) return fail(499, "Client cancelled request", "api_error");
    settleDistributedFailure();
    const sendError = error instanceof UpstreamRetryEvidenceError ? error.cause : error;
    if (sendError instanceof RequestPacingQueueOverloadError) {
      logCtx.terminalSource = "synthetic";
      const refusal = markProxyError(fail(429, sendError.message, "rate_limit_error"), sendError.reason === "keys_exhausted" ? undefined : "pacing");
      const advice = formatClientRetryAfter(sendError.retryAfterSeconds);
      if (advice !== undefined) refusal.headers.set("Retry-After", advice);
      return refusal;
    }
    if (sendError instanceof SendBudgetExhaustedError) {
      logCtx.terminalSource = "synthetic";
      const refusal = workflowSendCeilingRefusalResponse(logCtx, req.headers.get("x-codex-parent-thread-id")?.trim() || undefined);
      if (refusal) { finishLog(429); return nativeWorkflowRefusalResponse(refusal); }
      return fail(429, sendError.message, "send_budget_exhausted", "send_budget_exhausted");
    }
    if (sendError instanceof NativeMessagesSpendRefusal) {
      const refusal = workflowRefusalResponse(logCtx.errorCode === "workflow_sends_exhausted"
        ? "workflow-sends-exhausted" : "workflow-spend-exhausted", logCtx, undefined, undefined, logCtx.spendRefusalDetail);
      finishLog(429);
      return nativeWorkflowRefusalResponse(refusal);
    }
    const localRefusal = localOAuthFailure(sendError);
    if (localRefusal) return localRefusal;
    if (sendError instanceof NativeOAuthSelectionChangedError) return fail(409, sendError.message, "api_error");
    if (sendError instanceof NativeOAuthModelRouteError) return fail(400, "Invalid Anthropic model routes", "invalid_request_error", undefined, sendError.message);
    if (sendError instanceof NativeOpaqueStateRefusal) {
      logCtx.errorCode = "unsupported_feature";
      return fail(400, sendError.message, "invalid_request_error", "unsupported_feature");
    }
    if (isTranslatorBudgetExceededError(error)) {
      return fail(413, "request translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
    }
    if (sendError instanceof Error && isProxyError(sendError)) return fail(502, sendError.message, "api_error");
    return failEvidence(502, error instanceof Error ? error.message : String(error), "api_error", "transport_failed");
  }
  releaseRetainedRequest();

  // The last pool account refused on spent quota and rotation found no replacement: answer the
  // usage limit with its reset (#6991), as the bridge lane does, not the upstream "try again later".
  const poolUsageLimit = response.status === 429 && oauthBinding
    ? anthropicRoutingFor(nativeInstance!).anthropicPoolUsageLimitAfterRefusal(Date.now(), oauthBinding.routeDecision, route.modelId)
    : null;
  if (poolUsageLimit) {
    discard(response);
    cleanupAbort();
    logCtx.errorCode = "usage_limit_reached";
    return retainUpstreamMessagesRequestId(localOAuthFailure(poolUsageLimit)!, response.headers);
  }
  if (!response.ok) {
    let bodyText = "";
    try {
      const read = await readBoundedResponseBody(response, { signal: upstream.signal, maxBytes: MAX_NATIVE_MESSAGES_ERROR_BYTES });
      if (read.displaySafe) bodyText = read.text;
    } catch { /* status-only fallback */ }
    cleanupAbort();
    if (req.signal.aborted) {
      upstream.abort();
      return fail(499, "Client cancelled request", "api_error");
    }
    return retainUpstreamMessagesRequestId(nativeMessagesErrorResponse(response, bodyText, finishLog, route.providerName, activeProvider), response.headers);
  }

  const contentType = response.headers.get("content-type")?.toLowerCase() ?? "";
  if (contentType.includes("text/event-stream") && response.body) {
    // `upstream` follows req.signal and is also what shutdown and turn release abort, so both
    // the client leaving and this lane's own abort read as a cancel, not as a failed stream.
    const rawActivity: { refresh?: () => void } = {};
    const bodyGuard = {
      ...resolvePassthroughBodyGuard(config, upstream.signal),
      ...(activeProvider.anthropicEofTolerance === true ? { eofTolerant: true } : {}),
      rawActivity,
    };
    const replay = { committed: false, pending: false };
    const raw = options.sendBudget ? observeNativeMessagesReplayBoundary(response.body, logCtx, replay) : response.body;
    const firstOutput = logIds ? onFirstOutput : undefined;
    const observed = firstOutput || bodyGuard.stallMs > 0
      ? observeNativeMessagesChunks(raw, firstOutput, undefined, () => rawActivity.refresh?.()) : raw;
    const renamed = activeRequest.oauthToolNames
      ? restoreOAuthToolNamesInSse(observed, activeRequest.oauthToolNames, translatorBudget)
      : observed;
    const frames = new MessagesSseFrameContext();
    const echoed = echoRequestedModel(renamed, options.echoModel ?? requestedModel, frames);
    let toolRefusal: string | undefined;
    const source = clientDeclaredEmptyToolCatalog
      ? guardNativeMessagesEmptyToolCatalog(echoed, translatorBudget, message => {
        toolRefusal = message;
        logCtx.errorCode = UNDECLARED_TOOL_CALL_ERROR_CODE;
      }, declaredInlineTools, frames)
      : echoed;
    if (requestedStream) {
      transferTurnToStream();
      const clientFilter = createMessagesSseErrorFilter(frames);
      const relayed = tapAnthropicSseForLog(source, logCtx, (status, meta) => {
        try {
          cleanupAbort();
          bindUsage(logCtx.usage);
          finishLog(toolRefusal ? 502 : status, toolRefusal, toolRefusal ? { ...meta, terminalStatus: "failed" } : meta);
        } finally {
          // A discarded child cancels its reader, not the shared parent turn. Detach its
          // controller before aborting local transport work, while the ladder remains bound.
          releaseStreamTurn();
          if (toolRefusal || meta.closeReason !== "terminal") upstream.abort();
        }
      }, bodyGuard, frames, nativeInstance ? clientFilter : undefined);
      // A configured Messages provider words an oversized-input refusal its own way; Claude Code
      // compacts only on Anthropic's wording (devlog/_plan/261009_claude_1m_default/010). The
      // rewrite buffers whole frames, so it sits after the tap. The raw observer feeds the tap's
      // stall deadline before any frame buffering. Real Anthropic pools keep one client relay.
      const clientStream = nativeInstance ? relayed : relaySseWithPayloadRewrite(relayed, claudeOverflowSsePayload, translatorBudget, messagesSseFrameHooks(frames), clientFilter);
      let delivered = retainUpstreamMessagesRequestId(new Response(clientStream, {
        status: 200,
        headers: { "Content-Type": "text/event-stream; charset=utf-8", "Cache-Control": "no-cache", Connection: "keep-alive" },
      }), response.headers);
      if (options.sendBudget) {
        try {
          const preflight = await preflightNativeMessagesResponse(delivered, logCtx, translatorBudget, replay);
          if (preflight.kind === "failed") { bindUsage(logCtx.usage); return preflight.response; }
          delivered = new Response(watchFirstOutput(preflight.response.body!), { status: preflight.response.status, headers: preflight.response.headers });
        } catch (error) {
          committed ||= replay.committed || replay.pending;
          cleanupAbort(); releaseStreamTurn(); upstream.abort();
          if (req.signal.aborted) return fail(499, "Client cancelled request", "api_error");
          if (isTranslatorBudgetExceededError(error)) return fail(502, "upstream translation buffer exceeded the safe limit", "api_error", "translation_buffer_limit");
          return fail(502, "upstream Messages stream failed before output", "api_error");
        }
      }
      return delivered;
    }
    // A non-streaming caller whose upstream streamed anyway: fold the stream into one message.
    const tapState: { closeReason?: FinalRequestLogMeta["closeReason"]; meta?: FinalRequestLogMeta } = {};
    let tapped = tapAnthropicSseForLog(source, logCtx, (_status, meta) => {
      tapState.closeReason = meta.closeReason;
      tapState.meta = meta;
    }, bodyGuard);
    const collectorVerdictSink: MessagesFailureVerdictSink = {};
    try {
      if (options.sendBudget) {
        const preflight = await preflightNativeMessagesResponse(new Response(tapped, { headers: response.headers }), logCtx, translatorBudget, replay);
        if (preflight.kind === "failed") {
          cleanupAbort(); upstream.abort(); bindUsage(logCtx.usage);
          finishLog(preflight.response.status, logCtx.upstreamError);
          return preflight.response;
        }
        committed = true;
        tapped = watchFirstOutput(preflight.response.body!);
      }
      const message = await collectAnthropicMessage(tapped, options.echoModel ?? requestedModel, translatorBudget, collectorVerdictSink);
      cleanupAbort();
      bindUsage(logCtx.usage);
      if (tapState.closeReason === "client_cancel" || req.signal.aborted) {
        return fail(499, "Client cancelled request", "api_error");
      }
      if (message.type === "error") {
        const error = isRec(message.error) ? message.error : {};
        const text = typeof error.message === "string" ? error.message : "upstream stream failed";
        if (toolRefusal) {
          upstream.abort();
          finishLog(502, toolRefusal, { closeReason: "terminal", terminalStatus: "failed" });
          return fail(502, toolRefusal, "api_error", UNDECLARED_TOOL_CALL_ERROR_CODE);
        }
        const baselineOverflow = isNativeOverflowError(error, text);
        // The tap's own verdict (a reset, a stall, the byte cap) closes this row with the same meta
        // as the streaming lane's row; a plain upstream error event keeps the non_stream row.
        const tapMeta = tapState.meta;
        if (tapMeta && (tapMeta.terminalStatus || tapMeta.closeReason !== "terminal")) finishLog(502, text, tapMeta);
        if (upstreamErrorIsLocal(logCtx) && (logCtx.terminalSource === "synthetic" || tapMeta?.closeReason === "body_stall" || tapMeta?.closeReason === "body_overflow")) {
          const count = boundedMessagesDisplayCount(bodyGuard.maxBytes);
          const seconds = boundedMessagesDisplayCount(Math.round(bodyGuard.stallMs / 1000));
          const localMessage = new Map<string, string>([
            ["body_overflow", count === undefined ? "anthropic passthrough body exceeded the safe limit" : `anthropic passthrough body exceeded ${count} bytes`],
            ["body_stall", seconds === undefined ? "anthropic passthrough body stalled" : `anthropic passthrough body stalled: no upstream bytes for ${seconds}s`],
            ["stream_error_stop", 'upstream ended the turn with stop_reason "error"'],
            ["stream_ended_early", "anthropic passthrough upstream stream ended before message_stop"],
            ["stream_failed", "The proxy could not reach or read the provider."],
          ]).get(logCtx.failureDetail?.code ?? "");
          if (localMessage !== undefined) return markProxyError(fail(502, localMessage, "api_error", undefined, text), logCtx.failureDetail?.code === "body_overflow" ? "body_overflow" : logCtx.failureDetail?.code === "body_stall" ? "body_stall" : undefined);
        }
        const legacy = anthropicErrorBody(baselineOverflow ? 400 : 502,
          redactSecretString(baselineOverflow ? claudePromptTooLongMessage(text) : text),
          baselineOverflow ? "invalid_request_error" : "api_error");
        const overflow = messagesOverflowPresentation((legacy.error as Rec).message);
        if (baselineOverflow) return failEvidence(400, text, "invalid_request_error", undefined, overflow, error, legacy);
        return failEvidence(502, text, "api_error", undefined, overflow, error, legacy);
      }
      const finalStatus = collectorVerdictSink.collectedErrorEvent ? 502 : 200;
      finishLog(finalStatus);
      const collected = collectorVerdictSink.collected ?? message;
      const originalError = isRec(collected.error) ? collected.error : {};
      const clientMessage = collectorVerdictSink.collectedErrorEvent ? ownedAnthropicErrorPayload({ status: 200,
        type: typeof originalError.type === "string" ? originalError.type : undefined,
        code: typeof originalError.code === "string" ? originalError.code : undefined,
        overflow: messagesRelayedOverflowPresentation(JSON.stringify(collected), collected),
        evidence: { status: 200, message: typeof originalError.message === "string" ? originalError.message : undefined },
      }) : message;
      const collectedResponse = retainUpstreamMessagesRequestId(Response.json(clientMessage, { status: finalStatus }), response.headers);
      if (committed && finalStatus >= 400) markResponseNonReplayable(collectedResponse);
      return collectedResponse;
    } catch (error) {
      committed ||= replay.committed || replay.pending;
      cleanupAbort();
      upstream.abort();
      bindUsage(logCtx.usage);
      if (req.signal.aborted) return fail(499, "Client cancelled request", "api_error");
      if (isTranslatorBudgetExceededError(error)) {
        return fail(413, "upstream translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
      }
      return failEvidence(502, error instanceof Error ? error.message : String(error), "api_error", "transport_failed");
    }
  }

  let read;
  try {
    const jsonBodyReadMs = resolveJsonBodyReadTimeoutMs(config.stallTimeoutSec, { localUpstream }, config.connectTimeoutMs);
    read = await readBoundedResponseBody(response, {
      signal: upstream.signal,
      maxBytes: MAX_NATIVE_MESSAGES_JSON_BYTES,
      totalTimeoutMs: localUpstream ? 0 : jsonBodyReadMs,
      inactivityTimeoutMs: jsonBodyReadMs,
    });
  } catch (error) {
    cleanupAbort();
    upstream.abort();
    if (req.signal.aborted) return fail(499, "Client cancelled request", "api_error");
    return failEvidence(502, error instanceof Error ? error.message : String(error), "api_error", "transport_failed");
  }
  cleanupAbort();
  if (read.oversized) {
    upstream.abort();
    return fail(502, "upstream response exceeded the safe limit", "api_error", "translation_buffer_limit");
  }
  let parsedMessage: unknown;
  try {
    parsedMessage = JSON.parse(read.text);
  } catch {
    return fail(502, "upstream returned malformed Messages JSON", "api_error");
  }
  if (!isRec(parsedMessage) || parsedMessage.type !== "message") {
    return fail(502, "upstream response was not a Messages result", "api_error");
  }
  const message = activeRequest.oauthToolNames
    ? restoreOAuthToolNamesInMessage(parsedMessage, activeRequest.oauthToolNames)
    : parsedMessage;
  const undeclaredTool = clientDeclaredEmptyToolCatalog ? nativeMessagesClientToolName(message, declaredInlineTools) : undefined;
  if (undeclaredTool !== undefined) {
    logCtx.errorCode = UNDECLARED_TOOL_CALL_ERROR_CODE;
    return fail(502, undeclaredToolCallMessage(), "api_error", UNDECLARED_TOOL_CALL_ERROR_CODE);
  }
  bindUsage(anthropicUsageToOcx(isRec(message.usage) ? message.usage : undefined));
  if (Array.isArray(message.content) && message.content.length > 0) onFirstOutput?.();
  try {
    translatorBudget.chargeRetained(Buffer.byteLength(read.text) * 2, { kind: "live_transient" });
  } catch (error) {
    if (isTranslatorBudgetExceededError(error)) {
      return fail(502, "upstream translation buffer exceeded the safe limit", "api_error", "translation_buffer_limit");
    }
    throw error;
  }
  // The client's selector, not the wire id, as the translated lane answers.
  message.model = options.echoModel ?? requestedModel;
  const serialized = JSON.stringify(message);
  finishLog(200);
  if (requestedStream) {
    return retainUpstreamMessagesRequestId(new Response(messageAsSse(message), {
      status: 200,
      headers: { "Content-Type": "text/event-stream; charset=utf-8", "Cache-Control": "no-cache" },
    }), response.headers);
  }
  return retainUpstreamMessagesRequestId(new Response(serialized, { status: 200, headers: { "Content-Type": "application/json" } }), response.headers);
}

/** A folded stream error that refuses an oversized input (same gate as the SSE rewrite). */
function isNativeOverflowError(error: Rec, message: string): boolean {
  const sized = error.type === "invalid_request_error" || error.type === "request_too_large";
  return sized && !isThroughputLimitText(message)
    && (error.code === "context_length_exceeded" || isContextOverflowText(message));
}

/**
 * A non-OK upstream answer in Anthropic shape. Status and retry policy follow the translated
 * Messages path, so a client sees the same contract on either lane: transient 5xx become 529
 * with a retry hint, and a proxy-side replay refusal keeps its no-retry marking.
 */
function nativeMessagesErrorResponse(response: Response, bodyText: string, finishLog: FinishLog, providerName: string, provider: OcxProviderConfig): Response {
  let upstreamType: string | undefined;
  let upstreamCode: string | undefined;
  let upstreamMessage: string | undefined;
  try {
    const parsed = JSON.parse(bodyText) as Rec;
    const details = isRec(parsed.error) ? parsed.error : parsed;
    if (typeof details.type === "string") upstreamType = details.type;
    if (typeof details.code === "string") upstreamCode = details.code;
    if (typeof details.message === "string" && details.message.trim()) {
      upstreamMessage = redactSecretString(details.message.trim());
    }
  } catch { /* keep generic classification */ }
  const detail = formatAnthropicErrorBody(response.status, response.headers, bodyText);
  const rawMessage = upstreamMessage
    ?? (detail ? `Provider error ${response.status}: ${detail}` : `Provider error ${response.status}`);
  const zenOptions = {
    status: response.status,
    providerName,
    baseUrl: provider.baseUrl,
    adapter: provider.adapter,
    authMode: provider.authMode,
    hasApiKey: Boolean(provider.apiKey?.trim()),
    upstreamRetryAfter: response.headers.get("retry-after"),
    upstreamErrorType: upstreamType,
    upstreamErrorCode: upstreamCode,
    supportsHttpSameKeyRetry: true,
  };
  const message = enrichOpenCodeZenUpstreamMessage(rawMessage, zenOptions);
  const clientZenMessage = message === rawMessage ? undefined : enrichOpenCodeZenUpstreamMessage(
    "The provider rate limit was reached; retry later.",
    { ...zenOptions, upstreamRetryAfter: /try again in \d/i.test(rawMessage) ? "0" : zenOptions.upstreamRetryAfter },
  );
  const classified = classifyError(
    response.status,
    upstreamType ?? (response.status === 401 ? "authentication_error"
      : response.status === 429 ? "rate_limit_error"
        : response.status >= 500 ? "api_error" : "invalid_request_error"),
    message,
  );
  const safeMessage = redactSecretString(classified.message);
  const replayRefusal = isReplayRefusalResponse(response);
  const transient = !replayRefusal && isTransientUpstreamStatus(response.status);
  const status = replayRefusal ? REPLAY_REFUSED_STATUS : transient ? 529 : response.status;
  const upstreamRetryAfter = response.headers.get("retry-after");
  const retryAfter = replayRefusal
    ? undefined
    : resolveClientRetryAfter({ status: response.status, message: safeMessage, upstreamRetryAfter })
      ?? (upstreamRetryAfter?.trim() === "0" ? "0" : undefined);
  finishLog(response.status, safeMessage);
  const headers = new Headers({ "Content-Type": "application/json" });
  const clientRetryAfter = formatClientRetryAfter(retryAfter);
  if (clientRetryAfter) headers.set("Retry-After", clientRetryAfter);
  else if (transient) headers.set("Retry-After", "2");
  if (replayRefusal) applyReplayRefusalClientHeaders(headers);
  // The native envelope keeps its shape (no added code); only an overflow's wording changes, so
  // Claude Code recognizes it and compacts (010).
  // Only a 400/413 refusal: the shared classifier also files a 429 "too many tokens per minute"
  // under context_length_exceeded, and that one must stay a rate limit.
  const legacyOverflow = !replayRefusal && (response.status === 400 || response.status === 413)
    && !isThroughputLimitText(safeMessage)
    && (classified.code === "context_length_exceeded" || upstreamCode === "context_length_exceeded"
      || isContextOverflowText(safeMessage));
  const legacy = anthropicErrorBody(status,
    legacyOverflow ? claudePromptTooLongMessage(safeMessage) : safeMessage,
    transient ? "overloaded_error" : upstreamType,
    replayRefusal ? UPSTREAM_RESET_REPLAY_REFUSED_CODE : undefined,
  );
  const overflow = messagesOverflowPresentation((legacy.error as Rec).message);
  const out = new Response(JSON.stringify(ownedAnthropicErrorPayload({
    status, type: transient ? "overloaded_error" : upstreamType,
    code: replayRefusal ? UPSTREAM_RESET_REPLAY_REFUSED_CODE : undefined,
    evidence: { status: response.status, type: upstreamType, code: upstreamCode, message: safeMessage }, overflow,
    ...(replayRefusal ? { contract: "replay_refused" as const } : {}),
    ...(clientZenMessage === undefined ? {} : { localError: markProxyError({ message: clientZenMessage }) }),
  })), { status, headers });
  attachComboFailureEvidence(out, JSON.stringify(legacy));
  return replayRefusal ? retainReplayRefusal(out) : out;
}

/**
 * The body `count_tokens` should count for this selector: what the native lane would send when
 * the route is eligible, else `undefined` so the caller keeps its existing estimate. Reads config
 * and the router's deterministic branches only; sends nothing.
 */
export function nativeMessagesCountBody(
  config: OcxConfig,
  cc: OcxConfig["claudeCode"],
  body: Rec,
  rows: Pick<NativeMessagesSelector, "effortRow" | "fastRow">,
  clientIdentity?: AnthropicClientIdentity,
): Rec | undefined {
  if (typeof body.model !== "string") return undefined;
  try {
    if (messagesSecondaryInstanceUnavailable(config, body.model, cc)) return undefined;
    const selectorId = resolveInboundModel(body.model, cc);
    // Combo and policy selectors are never eligible, and routing them would advance round-robin
    // state or run the policy evaluator for a request that sends nothing.
    if (resolvePolicyProfileId(config, selectorId) !== null || selectorId.startsWith(`${POLICY_NAMESPACE}/`)) return undefined;
    if (!preservesPhysicalComboProvider(config) && resolveComboId(config, selectorId) !== null) return undefined;
    const route = routeModel(config, selectorId);
    route.staticPolicy = captureRouteStaticPolicy(
      route.providerName, route.modelId, route.provider, route.staticPolicy.effectiveAlias, "anthropic",
    );
    route.provider = resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, "anthropic", route.staticPolicy);
    const instance = configuredAnthropicInstance(config, route.providerName);
    const selector: NativeMessagesSelector = {
      ...rows,
      routeSelector: selectorId,
      claudeCode: cc,
      ...(route.provider.authMode === "oauth" && instance
        ? { oauthFailoverQuorum: anthropicRoutingFor(instance).hasAnthropicFailoverQuorum() } : {}),
    };
    if (nativeMessagesDeclineReason(route, body, config, selector) !== undefined) return undefined;
    // The body without a credential: counting never resolves or refreshes an OAuth account.
    return anthropicMessagesNativeWireBody(route.provider, route.modelId, body, { clientIdentity }).wireBody;
  } catch {
    return undefined;
  }
}