@@ -5846,6 +5846,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
58465846 );
58475847 const absorbedWakes = yield* Ref.make(new Set<string>());
58485848 const heldProposedPlansByToolUseId = new Map<string, string>();
5849+ // /clear moves a live process to a new native session; persist that one so a reopen resumes it.
5850+ const clearedNativeSessions = new Map<string, string>();
58495851 const runtimeContext = yield* Effect.context<never>();
58505852 const runPromise = Effect.runPromiseWith(runtimeContext);
58515853
@@ -5867,6 +5869,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
58675869 },
58685870 };
58695871 }
5872+ const nativeSessionId = clearedNativeSessions.get(
5873+ event.providerThread.nativeThreadRef?.nativeId ?? "",
5874+ );
5875+ if (nativeSessionId !== undefined) {
5876+ event = {
5877+ ...event,
5878+ providerThread: {
5879+ ...event.providerThread,
5880+ nativeThreadRef: {
5881+ driver: CLAUDE_PROVIDER,
5882+ nativeId: nativeSessionId,
5883+ strength: "strong",
5884+ },
5885+ nativeConversationHeadRef: null,
5886+ },
5887+ };
5888+ }
58705889 }
58715890 return Queue.offer(events, event).pipe(Effect.asVoid);
58725891 };
@@ -11999,6 +12018,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1199912018 path,
1200012019 });
1200112020 const closedExistingNativeThreadId = existing !== null ? existing.nativeThreadId : null;
12021+ clearedNativeSessions.delete(nativeThreadId);
1200212022 if (existing !== null) {
1200312023 yield* existing.query.close.pipe(Effect.ignore);
1200412024 if (existing.nativeThreadId !== nativeThreadId) {
@@ -12159,6 +12179,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1215912179 context.capabilities = new Set(message.capabilities ?? []);
1216012180 context.claudeCodeVersion = message.claude_code_version;
1216112181 }
12182+ if (
12183+ message.type === "system" &&
12184+ message.subtype === "init" &&
12185+ message.session_id.length > 0 &&
12186+ message.session_id !== context.nativeThreadId
12187+ ) {
12188+ clearedNativeSessions.set(context.nativeThreadId, message.session_id);
12189+ }
1216212190 if (
1216312191 message.type === "system" &&
1216412192 (message.subtype === "init" || message.subtype === "status") &&
@@ -12903,7 +12931,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1290312931
1290412932 const closeLiveQueryForNativeThread = Effect.fnUntraced(function* (nativeThreadId: string) {
1290512933 const existing = yield* Ref.get(queryContext);
12906- if (existing === null || existing.nativeThreadId !== nativeThreadId) {
12934+ if (
12935+ existing === null ||
12936+ (existing.nativeThreadId !== nativeThreadId &&
12937+ clearedNativeSessions.get(existing.nativeThreadId) !== nativeThreadId)
12938+ ) {
1290712939 return;
1290812940 }
1290912941
@@ -12925,7 +12957,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1292512957 );
1292612958 if (ownedLiveQuery) {
1292712959 yield* finalizeActiveTurnAfterQueryExit(existing);
12928- yield* endSubagentCallsOfClosedQuery(nativeThreadId, "interrupted");
12960+ yield* endSubagentCallsOfClosedQuery(existing. nativeThreadId, "interrupted");
1292912961 } else {
1293012962 yield* endSubagentsOfReplacedQuery(existing);
1293112963 }
0 commit comments