diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 8048046a5927..d2330829b096 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -3333,7 +3333,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { const makeWakeHarnessWithOptions = (options?: { readonly close?: (sdkMessages: Queue.Queue) => Effect.Effect; - readonly interrupt?: Effect.Effect; + readonly interrupt?: Effect.Effect; readonly setPermissionMode?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["setPermissionMode"]; readonly applyFlagSettings?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["applyFlagSettings"]; readonly stopTask?: ( @@ -12031,6 +12031,79 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect.each([ + { teardown: "before Stop" as const }, + { teardown: "during Stop's interrupt write" as const }, + ])("Stop with session teardown $teardown does not fail on the closed CLI", ({ teardown }) => + Effect.gen(function* () { + const sessionScope = yield* Scope.make(); + let queryClosed = false; + const harness = yield* makeWakeHarnessWithOptions({ + close: (sdkMessages) => + Effect.sync(() => { + queryClosed = true; + }).pipe(Effect.andThen(Queue.shutdown(sdkMessages))), + interrupt: Effect.suspend(() => + teardown === "before Stop" ? Effect.void : Scope.close(sessionScope, Exit.void), + ).pipe( + Effect.andThen( + Effect.suspend(() => + queryClosed + ? Effect.fail( + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "interrupt", + cause: new Error("ProcessTransport is not ready for writing"), + }), + ) + : Effect.void, + ), + ), + ), + }).pipe(Scope.provide(sessionScope)); + const now = yield* DateTime.now; + const attemptId = RunAttemptId.make("attempt-claude-closed-scope-stop"); + const idAllocator = yield* IdAllocator.IdAllocatorV2; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId, + text: "Hand this thread off to a worktree.", + attachments: [], + }), + ); + // A workspace handoff detaches the session, closing its scope while the turn streams. + if (teardown === "before Stop") { + yield* Scope.close(sessionScope, Exit.void); + assert.isTrue(queryClosed); + } + + const interrupted = yield* Effect.exit( + harness.runtime.interruptTurn({ + providerThread: harness.providerThread, + providerTurnId: idAllocator.derive.providerTurn({ + driver: ClaudeAdapterV2.CLAUDE_PROVIDER, + nativeTurnId: `turn:${attemptId}`, + }), + requestRuntimeRestart: true, + }), + ); + + assert.isTrue(Exit.isSuccess(interrupted), String(interrupted)); + assert.isTrue(queryClosed); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect("a settled Stop leaves a turn that replaced the closing process alone", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index c0e456d603c0..7bdd80435a28 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -12063,8 +12063,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( next.add(turnInput.providerTurnId); return next; }); - yield* existing.query.interrupt.pipe(Effect.timeoutOption("10 seconds")); - yield* existing.query.close.pipe(Effect.ignore); + if (!(yield* Deferred.isDone(existing.closed))) { + yield* existing.query.interrupt.pipe( + Effect.timeoutOption("10 seconds"), + // Teardown can close the CLI between the check and the write. + Effect.catch((error) => + Deferred.isDone(existing.closed).pipe( + Effect.flatMap((closed) => (closed ? Effect.void : Effect.fail(error))), + ), + ), + ); + yield* existing.query.close.pipe(Effect.ignore); + } const closed = yield* Deferred.await(existing.closed).pipe( Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS), ); @@ -12307,6 +12317,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const closeSession = Effect.fnUntraced(function* () { const existing = yield* Ref.get(queryContext); if (existing !== null) { + // Marked first so a concurrent Stop skips or tolerates writes to the closing CLI; + // the scope interrupts the stream fiber, so it may never mark its query closed. + yield* Deferred.succeed(existing.closed, undefined); yield* existing.query.close.pipe(Effect.ignore); } yield* Effect.yieldNow;