diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index ef2be98c81..4c1ac69512 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -411,6 +411,18 @@ export type ClaudeAgentSdkProtocolLogEvent = readonly direction: "incoming"; readonly stage: "decoded"; readonly payload: SDKMessage; + } + | { + // Failure frame: without it the native log ends mid-conversation with + // no trace of WHY (audit plan #4 — failed opens/streams left the + // "ground truth" log unable to explain any failed turn). + readonly direction: "incoming"; + readonly stage: "decoded"; + readonly payload: { + readonly type: "runner.error"; + readonly method: string; + readonly message: string; + }; }; export type ClaudeAgentSdkProtocolLogger = ( @@ -498,6 +510,22 @@ export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer< }); const logProtocolEvent = (event: ClaudeAgentSdkProtocolLogEvent) => protocolLogger === undefined ? Effect.void : protocolLogger(event); + const logRunnerFailure = + (method: string) => + (effect: Effect.Effect): Effect.Effect => + effect.pipe( + Effect.tapError((error) => + logProtocolEvent({ + direction: "incoming", + stage: "decoded", + payload: { + type: "runner.error", + method, + message: makeProviderFailure({ cause: error }).message, + }, + }), + ), + ); const promptQueue = yield* Queue.unbounded(); const prompt = Stream.fromQueue(promptQueue).pipe( Stream.catchCause((cause) => @@ -512,7 +540,7 @@ export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer< options: input.options, }), catch: (cause) => queryRunnerError(cause, "query"), - }); + }).pipe(logRunnerFailure("query.open")); yield* logProtocolEvent({ direction: "outgoing", stage: "decoded", @@ -533,6 +561,17 @@ export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer< payload: message, }), ), + Stream.tapError((error) => + logProtocolEvent({ + direction: "incoming", + stage: "decoded", + payload: { + type: "runner.error", + method: "messages.stream", + message: makeProviderFailure({ cause: error }).message, + }, + }), + ), ), offer: (message) => Queue.offer(promptQueue, message).pipe( @@ -547,12 +586,14 @@ export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer< }, }), ), + logRunnerFailure("prompt.offer"), ), setModel: (model) => Effect.tryPromise({ try: () => queryRuntime.setModel(model), catch: (cause) => queryRunnerError(cause, "setModel"), }).pipe( + logRunnerFailure("query.set_model"), Effect.tap(() => logProtocolEvent({ direction: "outgoing", diff --git a/apps/server/src/orchestration-v2/Adapters/CursorAgentSdk.ts b/apps/server/src/orchestration-v2/Adapters/CursorAgentSdk.ts index ab96f548af..04c9c2d0af 100644 --- a/apps/server/src/orchestration-v2/Adapters/CursorAgentSdk.ts +++ b/apps/server/src/orchestration-v2/Adapters/CursorAgentSdk.ts @@ -18,6 +18,7 @@ import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; import { ServerConfig } from "../../config.ts"; +import { makeProviderFailure } from "../ProviderFailure.ts"; import { type EventNdjsonLogger, makeEventNdjsonLogger, @@ -197,6 +198,18 @@ export type CursorAgentSdkProtocolLogEvent = readonly type: "agent.close"; readonly agentId: string; }; + } + | { + // Failure frame: SDK rejections previously left the native log ending + // mid-conversation with no trace of why (audit plan #4 — thread + // 721fc23c's failed turns were unexplainable from the log). + readonly direction: "incoming"; + readonly stage: "decoded"; + readonly payload: { + readonly type: "runner.error"; + readonly method: string; + readonly message: string; + }; }; export type CursorAgentSdkProtocolLogger = ( @@ -316,6 +329,22 @@ export const cursorAgentSdkRunnerLiveLayer: Layer.Layer protocolLogger === undefined ? Effect.void : protocolLogger(event); + const logRunnerFailure = + (method: string) => + (effect: Effect.Effect): Effect.Effect => + effect.pipe( + Effect.tapError((error) => + log({ + direction: "incoming", + stage: "decoded", + payload: { + type: "runner.error", + method, + message: makeProviderFailure({ cause: error }).message, + }, + }), + ), + ); yield* log({ direction: "outgoing", @@ -334,7 +363,7 @@ export const cursorAgentSdkRunnerLiveLayer: Layer.Layer runnerError(cause, `agent.${input.operation}`), - }); + }).pipe(logRunnerFailure(`agent.${input.operation}`)); yield* log({ direction: "incoming", @@ -406,7 +435,7 @@ export const cursorAgentSdkRunnerLiveLayer: Layer.Layer runnerError(cause, "run.start"), - }); + }).pipe(logRunnerFailure("run.start")); runId = run.id; yield* log({ direction: "incoming", @@ -449,6 +478,7 @@ export const cursorAgentSdkRunnerLiveLayer: Layer.Layer