Skip to content

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

feat(probe): report whether a failed hop advances the routing chain

Jack Rudenko346310f0c440
diff --git a/packages/cli/src/providers/probe-live.ts b/packages/cli/src/providers/probe-live.ts
index 95578dec..810e5ed2 100644
--- a/packages/cli/src/providers/probe-live.ts
+++ b/packages/cli/src/providers/probe-live.ts
@@ -13,6 +13,11 @@
*/
import { extractUpstreamStatus, sseDataPayload } from "../handlers/shared/anthropic-error.js";
+import {
+ type HopFailureReason,
+ UNHANDLED_ERROR_HEADER,
+ decideHopFailure,
+} from "../handlers/shared/hop-decision.js";
import {
hasActionableLink,
hasModelUnsupportedWording,
@@ -59,8 +64,26 @@ export interface ProbeResult {
* the response's `x-claudish-signed-with`, so it is per request.
*/
signedWith?: string;
+ /**
+ * What a routing chain would do after this hop's answer, by the runtime's rule
+ * (`decideHopFailure`). Absent on "live", and on a result the probe never sent
+ * (`key-missing`): a caller that walks a chain treats absent as "stop".
+ */
+ chainAction?: "advance" | "stop";
+ /** Why: a `HopFailureReason`, or a probe-only cause. */
+ chainReason?: ProbeChainReason;
}
+/** Why a probed hop advances or stops the chain. */
+export type ProbeChainReason =
+ | HopFailureReason
+ /** The stream failed after a 200, which the runtime already returned. */
+ | "after-200"
+ /** The probe's own deadline; the runtime has none per hop, so it would wait. */
+ | "probe-timeout"
+ /** The probe could not reach the proxy at all. */
+ | "proxy-unreachable";
+
/**
* Minimum streaming window (ms) used when deriving tokens/sec. A response whose
* whole (token-capped) body lands in ~one chunk has TTFT ≈ total, so the raw
@@ -260,10 +283,25 @@ export async function probeLink(
const latencyMs = Date.now() - startedAt;
const name = e?.name || "";
const msg = String(e?.message || e);
+ // A routing chain has no per-hop deadline, so a real request would wait on
+ // this hop: the chain stops here. An unreachable proxy says nothing about
+ // the hop, so nothing is claimed about later hops either.
if (name === "TimeoutError" || name === "AbortError" || /timeout/i.test(msg)) {
- return { state: "timeout", latencyMs, errorMessage: msg };
+ return {
+ state: "timeout",
+ latencyMs,
+ errorMessage: msg,
+ chainAction: "stop",
+ chainReason: "probe-timeout",
+ };
}
- return { state: "network-error", latencyMs, errorMessage: msg };
+ return {
+ state: "network-error",
+ latencyMs,
+ errorMessage: msg,
+ chainAction: "stop",
+ chainReason: "proxy-unreachable",
+ };
}
// TTFB: response headers are back. Stages before this are network/connect +
@@ -271,10 +309,17 @@ export async function probeLink(
const ttfbMs = Date.now() - startedAt;
if (!response.ok) {
- const body = await safeReadBody(response);
+ // The whole body decides the hop (wording rules can sit past the display cut);
+ // the first 500 characters are what the probe shows.
+ const fullBody = await readBodyText(response);
+ const body = fullBody.slice(0, DISPLAY_BODY_CHARS);
return annotateOAuthHint(
// A request that was signed and then failed still names its signer.
- { ...classifyHttpError(response.status, body, ttfbMs), ...signedWithOf(response) },
+ {
+ ...classifyHttpError(response.status, body, ttfbMs),
+ ...signedWithOf(response),
+ ...chainDecisionOf(response, fullBody, link.provider),
+ },
link.provider,
isOAuth
);
@@ -309,6 +354,11 @@ export async function probeLink(
latencyMs: totalMs,
timing,
...signedWithOf(response),
+ // A failure after a 200: the runtime already handed that 200 to Claude
+ // Code, and FallbackHandler never sees a stream-time failure as an error.
+ ...(rest.state !== "live"
+ ? { chainAction: "stop" as const, chainReason: "after-200" as const }
+ : {}),
},
link.provider,
isOAuth
@@ -396,15 +446,42 @@ function annotateOAuthHint(result: ProbeResult, provider: string, isOAuth: boole
};
}
-async function safeReadBody(response: Response): Promise<string> {
+/** How much of an error body a probe result shows. */
+const DISPLAY_BODY_CHARS = 500;
+
+async function readBodyText(response: Response): Promise<string> {
try {
- const text = await response.text();
- return text.slice(0, 500);
+ return await response.text();
} catch {
return "";
}
}
+/**
+ * What a routing chain would do after this failed hop: the runtime's own rule
+ * (`decideHopFailure`, shared with `FallbackHandler`). A 500 marked
+ * `UNHANDLED_ERROR_HEADER` is a hop whose handler threw, which a chain advances past.
+ */
+function chainDecisionOf(
+ response: Response,
+ body: string,
+ provider: string
+): Pick<ProbeResult, "chainAction" | "chainReason"> {
+ if (response.headers.get(UNHANDLED_ERROR_HEADER) === "1") {
+ const thrown = decideHopFailure({ kind: "thrown" });
+ return { chainAction: thrown.action, chainReason: thrown.reason };
+ }
+ const decision = decideHopFailure({
+ kind: "response",
+ status: response.status,
+ headers: response.headers,
+ body,
+ provider,
+ name: provider,
+ });
+ return { chainAction: decision.action, chainReason: decision.reason };
+}
+
// `extractUpstreamStatus` used to live here as a private copy. It is shared with
// `fallback-handler.ts` now (#148), because both have to read the same field to
// tell a remapped terminal error apart from a real 400, and two private readers