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, 19:45 UTC
fix(server): Claude threads resume the session /clear started (#35054)
Julius Marminge committed
Nov 12, 2026 at 02:49 UTC
86ae1747ad62e301307bcf7a7ce7a6f1fcae3d16
2 files changed
+92
-2
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+58
@@ -3664,6 +3664,64 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3664
});
3665
const makeWakeHarness = makeWakeHarnessWithOptions();
3666
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
+
3725
it.effect.each(["release-after", "cancel-after", "release-before", "resume-pending"] as const)(
3726
"keeps attachment reservations across compaction: %s boundary",
3727
(outcome) =>
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+34
-2
@@ -5846,6 +5846,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5846
);
5847
const absorbedWakes = yield* Ref.make(new Set<string>());
5848
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>();
5851
const runtimeContext = yield* Effect.context<never>();
5852
const runPromise = Effect.runPromiseWith(runtimeContext);
5853
@@ -5867,6 +5869,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5869
},
5870
};
5871
}
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
+ }
5889
}
5890
return Queue.offer(events, event).pipe(Effect.asVoid);
5891
};
@@ -11999,6 +12018,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12018
path,
12019
});
12020
const closedExistingNativeThreadId = existing !== null ? existing.nativeThreadId : null;
12021
+ clearedNativeSessions.delete(nativeThreadId);
12022
if (existing !== null) {
12023
yield* existing.query.close.pipe(Effect.ignore);
12024
if (existing.nativeThreadId !== nativeThreadId) {
@@ -12159,6 +12179,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12179
context.capabilities = new Set(message.capabilities ?? []);
12180
context.claudeCodeVersion = message.claude_code_version;
12181
}
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
+ }
12190
if (
12191
message.type === "system" &&
12192
(message.subtype === "init" || message.subtype === "status") &&
@@ -12903,7 +12931,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12931
12932
const closeLiveQueryForNativeThread = Effect.fnUntraced(function* (nativeThreadId: string) {
12933
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
+ ) {
12939
return;
12940
}
12941
@@ -12925,7 +12957,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12957
);
12958
if (ownedLiveQuery) {
12959
yield* finalizeActiveTurnAfterQueryExit(existing);
12928
- yield* endSubagentCallsOfClosedQuery(nativeThreadId, "interrupted");
12960
+ yield* endSubagentCallsOfClosedQuery(existing.nativeThreadId, "interrupted");
12961
} else {
12962
yield* endSubagentsOfReplacedQuery(existing);
12963
}