From ffd2d59a4169c5da710d36e40bc1774b2c0ceecc Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 26 Oct 2026 00:02:18 +0000 Subject: [PATCH 1/3] test(server): Claude Stop after a session-scope close interrupts a closed CLI Regression test for pingdotgg/t3code#25146. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 64891a4444d4..c85aec174d47 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -2626,7 +2626,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 stopTask?: ( taskId: string, @@ -7710,6 +7710,62 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("Stop after the session scope closed its query does not write to the closed CLI", () => + 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(() => + 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. + 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)); + }).pipe( + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ); + it.effect("a settled Stop leaves a turn that replaced the closing process alone", () => Effect.scoped( Effect.gen(function* () { From 5256c6a5d895ea8972dc081863c3db7deecf0534 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 26 Oct 2026 00:08:12 +0000 Subject: [PATCH 2/3] fix(server): Stop after a Claude session closed no longer writes to the closed CLI Closing a Claude session scope interrupts the SDK stream fiber before closeSession closes the query, so the query's `closed` signal could stay unset and the stale live query looked writable. A later cleanup Stop (requestRuntimeRestart) then called interrupt on the dead transport and failed with "ProcessTransport is not ready for writing". closeSession now marks the query it closed as closed, and interruptTurn skips interrupt/close for a query that has already closed. Fixes pingdotgg/t3code#25146 Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Adapters/ClaudeAdapterV2.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index e15d52e07f1c..e48bb11abc28 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -10145,8 +10145,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( next.add(turnInput.providerTurnId); return next; }); - yield* existing.query.interrupt; - yield* existing.query.close.pipe(Effect.ignore); + if (!(yield* Deferred.isDone(existing.closed))) { + yield* existing.query.interrupt; + yield* existing.query.close.pipe(Effect.ignore); + } const closed = yield* Deferred.await(existing.closed).pipe( Effect.timeoutOption("10 seconds"), ); @@ -10348,6 +10350,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const existing = yield* Ref.get(queryContext); if (existing !== null) { yield* existing.query.close.pipe(Effect.ignore); + // The scope interrupts the stream fiber first, so it may never mark its query closed. + yield* Deferred.succeed(existing.closed, undefined); } yield* Effect.yieldNow; yield* queryRunner.assertComplete.pipe( From 8b8966272b407a8a3e07ea1f58c477ed5d053d5f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 01:09:52 +0000 Subject: [PATCH 3/3] fix(server): Stop racing a Claude session teardown no longer fails on the closing CLI Teardown marks the query closed before closing it, and Stop treats an interrupt write that fails once the query is closed as settled. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 34 +++++++++++++------ .../Adapters/ClaudeAdapterV2.ts | 15 ++++++-- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 41c24029175b..04b50064a0ef 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -10789,7 +10789,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); - it.effect("Stop after the session scope closed its query does not write to the closed CLI", () => + 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; @@ -10799,14 +10802,20 @@ describe("ClaudeAdapterV2 background wake turns", () => { queryClosed = true; }).pipe(Effect.andThen(Queue.shutdown(sdkMessages))), interrupt: Effect.suspend(() => - queryClosed - ? Effect.fail( - new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ - method: "interrupt", - cause: new Error("ProcessTransport is not ready for writing"), - }), - ) - : Effect.void, + 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; @@ -10823,8 +10832,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { }), ); // A workspace handoff detaches the session, closing its scope while the turn streams. - yield* Scope.close(sessionScope, Exit.void); - assert.isTrue(queryClosed); + if (teardown === "before Stop") { + yield* Scope.close(sessionScope, Exit.void); + assert.isTrue(queryClosed); + } const interrupted = yield* Effect.exit( harness.runtime.interruptTurn({ @@ -10838,6 +10849,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { ); assert.isTrue(Exit.isSuccess(interrupted), String(interrupted)); + assert.isTrue(queryClosed); }).pipe( Effect.provide( Layer.mergeAll( diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 87976e3b86d5..9fd860aea96b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -11572,7 +11572,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( return next; }); if (!(yield* Deferred.isDone(existing.closed))) { - yield* existing.query.interrupt.pipe(Effect.timeoutOption("10 seconds")); + 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( @@ -11769,9 +11777,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const closeSession = Effect.fnUntraced(function* () { const existing = yield* Ref.get(queryContext); if (existing !== null) { - yield* existing.query.close.pipe(Effect.ignore); - // The scope interrupts the stream fiber first, so it may never mark its query closed. + // 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; yield* queryRunner.assertComplete.pipe(