@devries-cameron20 / t3code Mirror · read-only / commits / d749cfd819
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 21:00 UTC

fix(server): skip Claude interrupts after session teardown (#25159)

Julius Marminge committed Nov 11, 2026 at 16:07 UTC d749cfd8192e9b98dd9e764e00709d20049b40b5
2 files changed +89 -3
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+74 -1
@@ -3333,7 +3333,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3333
3334 const makeWakeHarnessWithOptions = (options?: {
3335 readonly close?: (sdkMessages: Queue.Queue<SDKMessage>) => Effect.Effect<void>;
3336 - readonly interrupt?: Effect.Effect<void>;
3336 + readonly interrupt?: Effect.Effect<void, ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError>;
3337 readonly setPermissionMode?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["setPermissionMode"];
3338 readonly applyFlagSettings?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["applyFlagSettings"];
3339 readonly stopTask?: (
@@ -12031,6 +12031,79 @@ describe("ClaudeAdapterV2 background wake turns", () => {
12031 ),
12032 );
12033
12034 + it.effect.each([
12035 + { teardown: "before Stop" as const },
12036 + { teardown: "during Stop's interrupt write" as const },
12037 + ])("Stop with session teardown $teardown does not fail on the closed CLI", ({ teardown }) =>
12038 + Effect.gen(function* () {
12039 + const sessionScope = yield* Scope.make();
12040 + let queryClosed = false;
12041 + const harness = yield* makeWakeHarnessWithOptions({
12042 + close: (sdkMessages) =>
12043 + Effect.sync(() => {
12044 + queryClosed = true;
12045 + }).pipe(Effect.andThen(Queue.shutdown(sdkMessages))),
12046 + interrupt: Effect.suspend(() =>
12047 + teardown === "before Stop" ? Effect.void : Scope.close(sessionScope, Exit.void),
12048 + ).pipe(
12049 + Effect.andThen(
12050 + Effect.suspend(() =>
12051 + queryClosed
12052 + ? Effect.fail(
12053 + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
12054 + method: "interrupt",
12055 + cause: new Error("ProcessTransport is not ready for writing"),
12056 + }),
12057 + )
12058 + : Effect.void,
12059 + ),
12060 + ),
12061 + ),
12062 + }).pipe(Scope.provide(sessionScope));
12063 + const now = yield* DateTime.now;
12064 + const attemptId = RunAttemptId.make("attempt-claude-closed-scope-stop");
12065 + const idAllocator = yield* IdAllocator.IdAllocatorV2;
12066 + yield* harness.runtime.startTurn(
12067 + makeClaudeTestTurnInput({
12068 + threadId: harness.threadId,
12069 + providerThread: harness.providerThread,
12070 + now,
12071 + attemptId,
12072 + text: "Hand this thread off to a worktree.",
12073 + attachments: [],
12074 + }),
12075 + );
12076 + // A workspace handoff detaches the session, closing its scope while the turn streams.
12077 + if (teardown === "before Stop") {
12078 + yield* Scope.close(sessionScope, Exit.void);
12079 + assert.isTrue(queryClosed);
12080 + }
12081 +
12082 + const interrupted = yield* Effect.exit(
12083 + harness.runtime.interruptTurn({
12084 + providerThread: harness.providerThread,
12085 + providerTurnId: idAllocator.derive.providerTurn({
12086 + driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
12087 + nativeTurnId: `turn:${attemptId}`,
12088 + }),
12089 + requestRuntimeRestart: true,
12090 + }),
12091 + );
12092 +
12093 + assert.isTrue(Exit.isSuccess(interrupted), String(interrupted));
12094 + assert.isTrue(queryClosed);
12095 + }).pipe(
12096 + Effect.provide(
12097 + Layer.mergeAll(
12098 + IdAllocator.layer,
12099 + McpProviderSessions.layer,
12100 + NodeServices.layer,
12101 + NodeCrypto.layer,
12102 + ),
12103 + ),
12104 + ),
12105 + );
12106 +
12107 it.effect("a settled Stop leaves a turn that replaced the closing process alone", () =>
12108 Effect.scoped(
12109 Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+15 -2
@@ -12063,8 +12063,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12063 next.add(turnInput.providerTurnId);
12064 return next;
12065 });
12066 - yield* existing.query.interrupt.pipe(Effect.timeoutOption("10 seconds"));
12067 - yield* existing.query.close.pipe(Effect.ignore);
12066 + if (!(yield* Deferred.isDone(existing.closed))) {
12067 + yield* existing.query.interrupt.pipe(
12068 + Effect.timeoutOption("10 seconds"),
12069 + // Teardown can close the CLI between the check and the write.
12070 + Effect.catch((error) =>
12071 + Deferred.isDone(existing.closed).pipe(
12072 + Effect.flatMap((closed) => (closed ? Effect.void : Effect.fail(error))),
12073 + ),
12074 + ),
12075 + );
12076 + yield* existing.query.close.pipe(Effect.ignore);
12077 + }
12078 const closed = yield* Deferred.await(existing.closed).pipe(
12079 Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS),
12080 );
@@ -12307,6 +12317,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12317 const closeSession = Effect.fnUntraced(function* () {
12318 const existing = yield* Ref.get(queryContext);
12319 if (existing !== null) {
12320 + // Marked first so a concurrent Stop skips or tolerates writes to the closing CLI;
12321 + // the scope interrupts the stream fiber, so it may never mark its query closed.
12322 + yield* Deferred.succeed(existing.closed, undefined);
12323 yield* existing.query.close.pipe(Effect.ignore);
12324 }
12325 yield* Effect.yieldNow;