diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 1add2822a01e9..a2f9490528066 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -17144,6 +17144,132 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + // regression: #26082 a killed Claude process names its signal on the failed turn + it.effect("names the signal that killed the Claude process on the failed turn", () => + Effect.scoped( + Effect.gen(function* () { + const changes = yield* makeChangeSignal; + const fileSystem = yield* FileSystem.FileSystem; + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-claude-v2-process-signal-", + }); + const sdkMessages = yield* Queue.unbounded< + SDKMessage, + ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError + >(); + const events: Array = []; + const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({ + instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID, + settings: DEFAULT_CLAUDE_SETTINGS, + environment: {}, + attachmentsDir, + fileSystem, + path: yield* Path.Path, + random: yield* SecureRandom.SecureRandom, + idAllocator, + queryRunner: { + allocateSessionId: Effect.succeed(WAKE_NATIVE_SESSION), + open: () => + Effect.succeed({ + messages: Stream.fromQueue(sdkMessages), + offer: () => Effect.void, + setModel: () => Effect.void, + applyFlagSettings: () => Effect.void, + setPermissionMode: () => Effect.void, + backgroundTasks: () => Effect.succeed(false), + mcpServerStatus: Effect.succeed([]), + readMcpResource: () => Effect.die("unexpected MCP resource read"), + callMcpTool: () => Effect.die("unexpected MCP tool call"), + listPermissionRules: Effect.die("unexpected permission rules read"), + stopTask: () => Effect.void, + interrupt: Effect.void, + close: Queue.shutdown(sdkMessages), + }), + forkSession: () => Effect.die("unused forkSession"), + subagentLaunchToolUseId: () => Effect.succeed(null), + assertComplete: Effect.void, + }, + }); + const threadId = ThreadId.make("thread-claude-process-signal"); + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("provider-session-claude-process-signal"), + modelSelection: CLAUDE_TEST_MODEL_SELECTION, + runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY, + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection: CLAUDE_TEST_MODEL_SELECTION, + runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY, + }); + yield* runtime.events.pipe( + Stream.runForEach((event) => + Effect.sync(() => { + events.push(event); + }).pipe(Effect.andThen(changes.notify)), + ), + Effect.forkScoped, + ); + yield* runtime.startTurn( + makeClaudeTestTurnInput({ + threadId, + providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-process-signal"), + text: "Write a long reply.", + attachments: [], + }), + ); + yield* Queue.offer( + sdkMessages, + makeAssistantTextFrame({ + uuid: "00000000-0000-4000-8000-000000026082", + text: "Here is the first part", + }), + ); + // Mirrors ProcessTransport.getProcessExitError in @anthropic-ai/claude-agent-sdk. + const processExit = Object.assign( + new Error( + "Claude Code process terminated by signal SIGABRT. stderr: fork failed: Resource temporarily unavailable", + ), + { + telemetryMessage: "Claude Code process terminated by signal SIGABRT", + errorClass: "process_killed_by_signal", + signal: "SIGABRT", + }, + ); + yield* Queue.fail( + sdkMessages, + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "messages", + cause: processExit, + }), + ); + + yield* changes.until(() => events.some((event) => event.type === "turn.terminal")); + const terminal = events.find((event) => event.type === "turn.terminal"); + assert.equal(terminal?.status, "failed"); + if (terminal?.status !== "failed") return; + assert.include( + terminal.failure.message, + "Claude Code process terminated by signal SIGABRT", + ); + assert.notInclude(terminal.failure.message, "stderr"); + assert.equal(terminal.failure.code, "process_killed_by_signal"); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + const WORKFLOW_TASK_ID = "task-workflow-run"; const WORKFLOW_TOOL_USE_ID = "toolu-workflow-run"; const makeWorkflowHarnessWithOptions = ( diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 7b63b7efc0b65..c097f081bb707 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -716,6 +716,26 @@ function queryRunnerError(cause: unknown, method: string): ClaudeAgentSdkQueryRu : new ClaudeAgentSdkQueryRunnerError({ cause, method }); } +const CLAUDE_PROCESS_EXIT_ERROR_CLASSES: ReadonlySet = new Set([ + "process_killed_by_signal", + "process_exited_nonzero", +]); + +// The SDK's exit Error appends Claude's stderr tail to `message`; `telemetryMessage` omits it. +function claudeProcessExitFailure( + cause: Cause.Cause, +): { readonly message: string; readonly code: string } | undefined { + const error = Cause.squash(cause); + const sdkError = isClaudeAgentSdkQueryRunnerError(error) ? error.cause : error; + if (typeof sdkError !== "object" || sdkError === null) return undefined; + const { errorClass, telemetryMessage } = sdkError as Record; + return typeof errorClass === "string" && + CLAUDE_PROCESS_EXIT_ERROR_CLASSES.has(errorClass) && + typeof telemetryMessage === "string" + ? { message: `${telemetryMessage}.`, code: errorClass } + : undefined; +} + function closeClaudeQuery(queryRuntime: ClaudeQuery) { return Effect.try({ try: () => queryRuntime.close(), @@ -8400,6 +8420,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( : { failure: makeProviderFailure({ cause: cause === undefined ? undefined : Cause.squash(cause), + ...(cause === undefined ? {} : claudeProcessExitFailure(cause)), class: "transport_error", }), }),