Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(routing): return stream-failure errors verbatim from FallbackHandler

Jack Rudenko558547250fb8
diff --git a/packages/cli/src/handlers/fallback-handler.ts b/packages/cli/src/handlers/fallback-handler.ts
index 93bc2a27..44ce583d 100644
--- a/packages/cli/src/handlers/fallback-handler.ts
+++ b/packages/cli/src/handlers/fallback-handler.ts
@@ -14,7 +14,12 @@ import { logStderr } from "../logger.js";
import { ComposedHandler } from "./composed-handler.js";
import { extractUpstreamStatus } from "./shared/anthropic-error.js";
import { hasQuotaExhaustionWording } from "./shared/quota-exhaustion.js";
-import { isClaudishConnectionVerdict, isRecoveryHoldResponse } from "./shared/recovery-marker.js";
+import {
+ hasStreamFailureMarker,
+ isClaudishConnectionVerdict,
+ isRecoveryHoldResponse,
+ isStreamFailureResponse,
+} from "./shared/recovery-marker.js";
import type { ModelHandler } from "./types.js";
export interface FallbackCandidate {
@@ -113,6 +118,19 @@ export class FallbackHandler implements ModelHandler {
return response;
}
+ // ── THE STREAM-FAILURE MARKER, SAME PLACE AND SAME REASONS ────────────
+ //
+ // A non-streamed turn whose stream ended on an SSE `error` (the provider
+ // answered 200, then the stream failed) is answered by the JSON collector
+ // with an error response. In streaming mode the same failure never comes
+ // back through here as an error, so neither may this one: it is not the
+ // provider's verdict on the request, and its body can quote an upstream
+ // message containing quota wording. Returned verbatim, before the body is
+ // read, so it is neither advanced past nor folded with earlier errors.
+ if (isStreamFailureResponse(response)) {
+ return response;
+ }
+
// Clone before reading body so we can still return the original if needed
const errorBody = await response.clone().text();
@@ -233,6 +251,10 @@ export function isRetryableError(
// predicate rather than either half is what stops chain-safety from being a
// property of which arm happened to answer.
if (isClaudishConnectionVerdict(headers)) return false;
+ // A stream that failed after a 200 is not a provider's verdict either; see
+ // `STREAM_FAILURE_HEADER` in recovery-marker.ts. Above the wording match for
+ // the same reason as the line above.
+ if (hasStreamFailureMarker(headers)) return false;
// A spent subscription allowance is retryable AT THE CHAIN LEVEL: this
// provider cannot serve, but the next one can.
diff --git a/packages/cli/src/handlers/shared/recovery-marker.ts b/packages/cli/src/handlers/shared/recovery-marker.ts
index f2de46c8..71b85ae1 100644
--- a/packages/cli/src/handlers/shared/recovery-marker.ts
+++ b/packages/cli/src/handlers/shared/recovery-marker.ts
@@ -176,3 +176,53 @@ export function isRecoveryHoldResponse(
): boolean {
return hasRecoveryMarker(response?.headers);
}
+
+/**
+ * The THIRD marker: a stream that failed AFTER claudish flushed a 200.
+ *
+ * Once a stream parser has sent `message_start`, the only way left to report a
+ * failure is an SSE `error` event; Claude Code then discards the message and
+ * retries the turn. A NON-streamed request (`stream` absent or false) has no
+ * SSE: `sseResponseToJson` folds the parser's events into one JSON answer, and
+ * it answers such a failure with the Anthropic error body for the event's own
+ * error type. That answer must behave like the streamed form of the same
+ * failure, which never reaches `FallbackHandler` as an error at all:
+ *
+ * - it is not a provider's verdict on the request (the provider already
+ * answered 200), so it must never ADVANCE a routing chain. A status rule
+ * cannot hold it: a leg error quotes the upstream body, and
+ * `isRetryableError` reads quota wording before any status;
+ * - it must not be FOLDED by `formatCombinedError` with earlier candidates'
+ * errors either, which would replace its status.
+ *
+ * So `FallbackHandler.handle` returns a response carrying this header verbatim
+ * before reading the body, and `isRetryableError` answers `false` for it as its
+ * first statement. Unforgeable by the argument at the top of this file:
+ * `sseResponseToJson` builds a new `Response` whose headers start empty, and no
+ * upstream header is ever copied onto it.
+ */
+export const STREAM_FAILURE_HEADER = "x-claudish-stream-failure";
+
+/** The headers the collector's stream-failure answer carries. */
+export function streamFailureHeaders(): Record<string, string> {
+ return {
+ "content-type": "application/json",
+ [STREAM_FAILURE_HEADER]: RECOVERY_MARKER_VALUE,
+ };
+}
+
+/** Does this header bag carry the stream-failure marker? */
+export function hasStreamFailureMarker(headers: Headers | undefined | null): boolean {
+ try {
+ return headers?.get(STREAM_FAILURE_HEADER) === RECOVERY_MARKER_VALUE;
+ } catch {
+ return false;
+ }
+}
+
+/** Is this response a non-streamed turn whose stream ended on an SSE `error`? */
+export function isStreamFailureResponse(
+ response: { headers?: Headers } | null | undefined
+): boolean {
+ return hasStreamFailureMarker(response?.headers);
+}