From 20a1f53ae232216043cd5a1b466c206bde41ac83 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 3 Jul 2026 02:36:14 -0700 Subject: [PATCH] Preserve nested failure causes in projected provider errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../orchestration-v2/ProviderFailure.test.ts | 22 +++++++++ .../src/orchestration-v2/ProviderFailure.ts | 49 +++++++++++++++++-- .../orchestration-v2/RunExecutionService.ts | 5 +- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderFailure.test.ts b/apps/server/src/orchestration-v2/ProviderFailure.test.ts index 68cd5e2d22..94a8d500af 100644 --- a/apps/server/src/orchestration-v2/ProviderFailure.test.ts +++ b/apps/server/src/orchestration-v2/ProviderFailure.test.ts @@ -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: { diff --git a/apps/server/src/orchestration-v2/ProviderFailure.ts b/apps/server/src/orchestration-v2/ProviderFailure.ts index 866d774066..d797f1e8f2 100644 --- a/apps/server/src/orchestration-v2/ProviderFailure.ts +++ b/apps/server/src/orchestration-v2/ProviderFailure.ts @@ -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; + readonly code: string | undefined; +} { + const messages: Array = []; + let code: string | undefined; + const seen = new Set(); + 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; diff --git a/apps/server/src/orchestration-v2/RunExecutionService.ts b/apps/server/src/orchestration-v2/RunExecutionService.ts index e8750e3687..d4cd798c17 100644 --- a/apps/server/src/orchestration-v2/RunExecutionService.ts +++ b/apps/server/src/orchestration-v2/RunExecutionService.ts @@ -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)),