From 37f248dd24e666c3f500460d403d939617841f1e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 07:23:56 +0000 Subject: [PATCH 1/2] test(claude): resuming after /clear and a process replacement targets the cleared session Failing regression test for #24152. AI-assisted; verified with the tests and tools listed above. --- .../Adapters/ClaudeAdapterV2.test.ts | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index eb6d4a8b5d0a..08806440c31f 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -3628,6 +3628,64 @@ describe("ClaudeAdapterV2 background wake turns", () => { }); const makeWakeHarness = makeWakeHarnessWithOptions(); + it.effect("resumes the session a /clear selected after the Claude process is replaced", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarnessWithOptions({ + freshQueueOnReopen: true, + close: (queue) => Queue.shutdown(queue), + }); + const turn = (ordinal: number, providerThread: OrchestrationV2ProviderThread) => + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread, + now: DateTime.makeUnsafe(0), + attemptId: RunAttemptId.make(`clear-session-${ordinal}`), + text: ordinal === 1 ? "/clear" : "Continue the cleared conversation", + attachments: [], + providerTurnOrdinal: ordinal, + }); + yield* harness.runtime.startTurn(turn(1, harness.providerThread)); + // Claude Code reports the session /clear started in its next init frame. + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "init", + session_id: "cleared-native-session", + uuid: "00000000-0000-4000-8000-000000000501", + }), + ); + yield* harness.offerAndWait(turnOneResult); + yield* Queue.take(harness.terminalReceipts); + const latest = (): OrchestrationV2ProviderThread => { + const event = harness.events.findLast( + (candidate) => candidate.type === "provider_thread.updated", + ); + return event?.type === "provider_thread.updated" + ? event.providerThread + : harness.providerThread; + }; + assert.equal(latest().nativeThreadRef?.nativeId, "cleared-native-session"); + yield* harness.runtime.interruptTurn({ + providerThread: latest(), + providerTurnId: lastProviderTurn(harness.events).id, + requestRuntimeRestart: true, + }); + yield* harness.runtime.startTurn(turn(2, latest())); + assert.equal(harness.processQueues.length, 2); + assert.equal(harness.getOpenedOptions()?.resume, "cleared-native-session"); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect.each(["release-after", "cancel-after", "release-before", "resume-pending"] as const)( "keeps attachment reservations across compaction: %s boundary", (outcome) => From d10bf093ba5354b7098bd4c589db2d4f7fe1d876 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 09:22:23 +0000 Subject: [PATCH 2/2] fix(server): Claude threads resume the session /clear started After a native /clear, Claude Code moves the live process to a new session and reports it in the next system/init, but the adapter kept persisting the session id it created the thread with. Every later reopen (restart, idle release, interrupt, fork) resumed the pre-clear conversation, or failed with "No conversation found" when that session had no transcript. The adapter now records the session a root init reports when it differs from the live process's id and writes that id (with no stale conversation head) into every provider thread update for that process. The next turn takes the existing native-identity-change path and reopens Claude with resume on the new session; closing before fork or rollback also finds the live process by that id. Fixes pingdotgg/t3code#24152 AI-assisted; verified with the tests and tools listed above. --- .../Adapters/ClaudeAdapterV2.ts | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 52782393470a..785bac7bb39a 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -5778,6 +5778,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ); const absorbedWakes = yield* Ref.make(new Set()); const heldProposedPlansByToolUseId = new Map(); + // /clear moves a live process to a new native session; persist that one so a reopen resumes it. + const clearedNativeSessions = new Map(); const runtimeContext = yield* Effect.context(); const runPromise = Effect.runPromiseWith(runtimeContext); @@ -5799,6 +5801,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }, }; } + const nativeSessionId = clearedNativeSessions.get( + event.providerThread.nativeThreadRef?.nativeId ?? "", + ); + if (nativeSessionId !== undefined) { + event = { + ...event, + providerThread: { + ...event.providerThread, + nativeThreadRef: { + driver: CLAUDE_PROVIDER, + nativeId: nativeSessionId, + strength: "strong", + }, + nativeConversationHeadRef: null, + }, + }; + } } return Queue.offer(events, event).pipe(Effect.asVoid); }; @@ -11758,6 +11777,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( path, }); const closedExistingNativeThreadId = existing !== null ? existing.nativeThreadId : null; + clearedNativeSessions.delete(nativeThreadId); if (existing !== null) { yield* existing.query.close.pipe(Effect.ignore); if (existing.nativeThreadId !== nativeThreadId) { @@ -11918,6 +11938,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( context.capabilities = new Set(message.capabilities ?? []); context.claudeCodeVersion = message.claude_code_version; } + if ( + message.type === "system" && + message.subtype === "init" && + message.session_id.length > 0 && + message.session_id !== context.nativeThreadId + ) { + clearedNativeSessions.set(context.nativeThreadId, message.session_id); + } if ( message.type === "system" && (message.subtype === "init" || message.subtype === "status") && @@ -12658,7 +12686,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const closeLiveQueryForNativeThread = Effect.fnUntraced(function* (nativeThreadId: string) { const existing = yield* Ref.get(queryContext); - if (existing === null || existing.nativeThreadId !== nativeThreadId) { + if ( + existing === null || + (existing.nativeThreadId !== nativeThreadId && + clearedNativeSessions.get(existing.nativeThreadId) !== nativeThreadId) + ) { return; } @@ -12680,7 +12712,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ); if (ownedLiveQuery) { yield* finalizeActiveTurnAfterQueryExit(existing); - yield* endSubagentCallsOfClosedQuery(nativeThreadId, "interrupted"); + yield* endSubagentCallsOfClosedQuery(existing.nativeThreadId, "interrupted"); } else { yield* endSubagentsOfReplacedQuery(existing); }