Skip to content

Commit 86ae174

Browse files
fix(server): Claude threads resume the session /clear started (#35054)
1 parent a33d50a commit 86ae174

2 files changed

Lines changed: 92 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,6 +3664,64 @@ describe("ClaudeAdapterV2 background wake turns", () => {
36643664
});
36653665
const makeWakeHarness = makeWakeHarnessWithOptions();
36663666

3667+
it.effect("resumes the session a /clear selected after the Claude process is replaced", () =>
3668+
Effect.gen(function* () {
3669+
const harness = yield* makeWakeHarnessWithOptions({
3670+
freshQueueOnReopen: true,
3671+
close: (queue) => Queue.shutdown(queue),
3672+
});
3673+
const turn = (ordinal: number, providerThread: OrchestrationV2ProviderThread) =>
3674+
makeClaudeTestTurnInput({
3675+
threadId: harness.threadId,
3676+
providerThread,
3677+
now: DateTime.makeUnsafe(0),
3678+
attemptId: RunAttemptId.make(`clear-session-${ordinal}`),
3679+
text: ordinal === 1 ? "/clear" : "Continue the cleared conversation",
3680+
attachments: [],
3681+
providerTurnOrdinal: ordinal,
3682+
});
3683+
yield* harness.runtime.startTurn(turn(1, harness.providerThread));
3684+
// Claude Code reports the session /clear started in its next init frame.
3685+
yield* harness.offerAndWait(
3686+
claudeSdkFrame({
3687+
type: "system",
3688+
subtype: "init",
3689+
session_id: "cleared-native-session",
3690+
uuid: "00000000-0000-4000-8000-000000000501",
3691+
}),
3692+
);
3693+
yield* harness.offerAndWait(turnOneResult);
3694+
yield* Queue.take(harness.terminalReceipts);
3695+
const latest = (): OrchestrationV2ProviderThread => {
3696+
const event = harness.events.findLast(
3697+
(candidate) => candidate.type === "provider_thread.updated",
3698+
);
3699+
return event?.type === "provider_thread.updated"
3700+
? event.providerThread
3701+
: harness.providerThread;
3702+
};
3703+
assert.equal(latest().nativeThreadRef?.nativeId, "cleared-native-session");
3704+
yield* harness.runtime.interruptTurn({
3705+
providerThread: latest(),
3706+
providerTurnId: lastProviderTurn(harness.events).id,
3707+
requestRuntimeRestart: true,
3708+
});
3709+
yield* harness.runtime.startTurn(turn(2, latest()));
3710+
assert.equal(harness.processQueues.length, 2);
3711+
assert.equal(harness.getOpenedOptions()?.resume, "cleared-native-session");
3712+
}).pipe(
3713+
Effect.scoped,
3714+
Effect.provide(
3715+
Layer.mergeAll(
3716+
IdAllocator.layer,
3717+
McpProviderSessions.layer,
3718+
NodeServices.layer,
3719+
NodeCrypto.layer,
3720+
),
3721+
),
3722+
),
3723+
);
3724+
36673725
it.effect.each(["release-after", "cancel-after", "release-before", "resume-pending"] as const)(
36683726
"keeps attachment reservations across compaction: %s boundary",
36693727
(outcome) =>

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)