Preserve nested failure causes in projected provider errors

Adapter errors are tagged wrappers whose message getter is a fixed string
("Claude Agent SDK query failed.", "Failed to start run ...") while the
real defect lives in .cause — makeProviderFailure only read the wrapper,
so every failed run persisted a generic message with the root cause
recorded nowhere (audit plan #2: 8 undebuggable failures across 5
threads). makeProviderFailure now walks the cause chain (bounded depth,
deduped) and joins the messages, and picks up the deepest error code.
The run-execution failure log also prints the Cause pretty-formatted
instead of a depth-elided object.

App-verified: breaking the Claude binary path now projects
"Claude Agent SDK query failed. ← Claude Code native binary not found at
/tmp/nonexistent-claude-binary. ..." as the user-visible error item.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Fable 5 committed 2026-07-12 11:51:17 +02:00
1 parent a0cdebce7e
commit 20a1f53ae2
3 files changed
+71 -5

No files matched your search

@@ -66,6 +66,28 @@ it("does not split a surrogate pair at the truncation boundary", () => {
assert.notMatch(failure.message.slice(0, -1), /[\uD800-\uDBFF]$/u);
});
it("preserves the nested cause chain of wrapper errors", () => {
const sdkError = new Error("spawn claude ENOENT");
(sdkError as Error & { code?: string }).code = "ENOENT";
const wrapper = new Error("Claude Agent SDK query failed.", { cause: sdkError });
const failure = makeProviderFailure({ cause: wrapper, class: "transport_error" });
assert.equal(failure.message, "Claude Agent SDK query failed. ← spawn claude ENOENT");
assert.equal(failure.code, "ENOENT");
});
it("uses the deepest available message without duplicating repeated ones", () => {
const failure = makeProviderFailure({
cause: {
message: "Failed to start run",
cause: { message: "Failed to start run", cause: "socket hang up" },
},
});
assert.equal(failure.message, "Failed to start run ← socket hang up");
});
it("does not serialize arbitrary provider causes", () => {
const failure = makeProviderFailure({
cause: {
@@ -87,6 +87,43 @@ function boundedText(value: string, maxLength: number): string {
return `${redacted.slice(0, end)}…`;
}
const MAX_FAILURE_CAUSE_DEPTH = 6;
/**
* Adapter errors are tagged wrappers whose `message` getter is a fixed
* human-readable string while the actual defect lives in `.cause` (often
* nested several levels deep). Walking the chain is what keeps the real
* failure reason debuggable after the fact — persisting only the wrapper
* message produced errors like "Claude Agent SDK query failed." with the
* underlying exception recorded nowhere.
*/
function causeChain(value: unknown): {
readonly messages: ReadonlyArray<string>;
readonly code: string | undefined;
} {
const messages: Array<string> = [];
let code: string | undefined;
const seen = new Set<unknown>();
let current: unknown = value;
for (let depth = 0; depth < MAX_FAILURE_CAUSE_DEPTH; depth += 1) {
if (current === null || current === undefined || seen.has(current)) break;
seen.add(current);
if (typeof current === "string") {
if (current.length > 0 && messages.at(-1) !== current) messages.push(current);
break;
}
if (typeof current !== "object" && !(current instanceof Error)) break;
const message =
current instanceof Error ? current.message : stringField(current, "message");
if (typeof message === "string" && message.length > 0 && messages.at(-1) !== message) {
messages.push(message);
}
code ??= stringField(current, "code");
current = (current as { readonly cause?: unknown }).cause;
}
return { messages, code };
}
export function makeProviderFailure(input: {
readonly cause?: unknown;
readonly message?: string | undefined;
@@ -94,12 +131,16 @@ export function makeProviderFailure(input: {
readonly class?: OrchestrationV2ProviderFailureClass;
readonly retryable?: boolean | null;
}): OrchestrationV2ProviderFailure {
const chain = causeChain(input.cause);
const chainMessage = chain.messages.length === 0 ? undefined : chain.messages.join(" ← ");
const rawMessage =
input.message ??
(input.cause instanceof Error ? input.cause.message : stringField(input.cause, "message")) ??
DEFAULT_PROVIDER_FAILURE_MESSAGE;
input.message === undefined
? (chainMessage ?? DEFAULT_PROVIDER_FAILURE_MESSAGE)
: chainMessage === undefined || chainMessage === input.message
? input.message
: `${input.message} ← ${chainMessage}`;
const message = boundedText(rawMessage, MAX_PROVIDER_FAILURE_MESSAGE_LENGTH);
const rawCode = input.code ?? stringField(input.cause, "code") ?? null;
const rawCode = input.code ?? chain.code ?? null;
const code =
rawCode === null ? null : boundedText(rawCode, MAX_PROVIDER_FAILURE_CODE_LENGTH) || null;
@@ -831,7 +831,10 @@ export const layer: Layer.Layer<
Effect.catchCause((cause) =>
Effect.logError("orchestration V2 provider turn start failed", {
runId: input.run.id,
cause,
// Pretty-printed: the structured object gets depth-elided to
// "[ [Object] ]" by the log sink, destroying the only copy
// of the real failure reason.
cause: Cause.pretty(cause),
}).pipe(
Effect.andThen(Fiber.interrupt(providerEventFiber)),
Effect.andThen(Ref.get(latestProviderThread)),