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, 07:15 UTC
fix(server): messages after a fresh session no longer return to a pre-clear session woken by background work (#38824)
Julius Marminge committed
Nov 18, 2026 at 18:38 UTC
66235c7f2a92677b8356a61ecaca6e5a7a96ebb8
2 files changed
+134
-2
apps/server/src/orchestration-v2/Orchestrator.ts
+6
-2
@@ -1375,7 +1375,7 @@ function appendContextHandoffId(
1375
return handoffId === null ? handoffIds : Array.from(new Set([...handoffIds, handoffId]));
1376
}
1377
1378
-/** The provider thread a message continues: the active one, unless a fresh-session schedule run started it. */
1378
+/** The provider thread a message continues: the active one, unless a fresh-session schedule run started it or it predates the latest fresh start. */
1379
function conversationProviderThread(
1380
projection: Pick<
1381
OrchestrationV2ThreadProjection,
@@ -1391,8 +1391,12 @@ function conversationProviderThread(
1391
const active = projection.providerThreads.find(
1392
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
1393
);
1394
- if (active === undefined || !startedByScheduledFreshRun(active)) return active;
1394
+ if (active === undefined) return active;
1395
const boundary = contextBoundaryOrdinal(projection);
1396
+ // A pre-clear session that is still alive (background work) can become active again on its updates.
1397
+ if (!startedByScheduledFreshRun(active) && !providerThreadPredatesBoundary(active, boundary)) {
1398
+ return active;
1399
+ }
1400
return (
1401
projection.providerThreads
1402
.filter(
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+128
@@ -4643,6 +4643,134 @@ describe("orchestration v2 provider switching", () => {
4643
}),
4644
);
4645
4646
+ it.live(
4647
+ "keeps the post-clear conversation when the pre-clear session's background task settles",
4648
+ () =>
4649
+ Effect.gen(function* () {
4650
+ const cwd = yield* checkpointWorkspace("fresh-context-old-session-wake");
4651
+ const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4652
+ const backgroundWork = yield* Ref.make(false);
4653
+ const started = yield* Deferred.make<void>();
4654
+ const release = yield* Deferred.make<void>();
4655
+ const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
4656
+ makeTestAdapter({
4657
+ instanceId: CLAUDE_MODEL_SELECTION.instanceId,
4658
+ driver: CLAUDE_DRIVER,
4659
+ capabilities: ClaudeProviderCapabilitiesV2,
4660
+ modelSelection: CLAUDE_MODEL_SELECTION,
4661
+ responseByRunOrdinal: {},
4662
+ capturedTurns,
4663
+ backgroundWork,
4664
+ nativeThreadGeneration: yield* Ref.make(0),
4665
+ holdFirstTurn: started,
4666
+ releaseFirstTurn: release,
4667
+ }),
4668
+ ]);
4669
+ const threadId = ThreadId.make("thread:fresh-context-old-session-wake");
4670
+ yield* Effect.gen(function* () {
4671
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
4672
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4673
+ const eventSink = yield* EventSink.EventSinkV2;
4674
+ const dispatch = (name: string, freshContext = false) =>
4675
+ orchestrator.dispatch({
4676
+ type: "message.dispatch",
4677
+ createdBy: "user",
4678
+ creationSource: "web",
4679
+ commandId: CommandId.make(`command:fresh-context-old-session-wake:${name}`),
4680
+ threadId,
4681
+ messageId: MessageId.make(`message:fresh-context-old-session-wake:${name}`),
4682
+ text: name,
4683
+ attachments: [],
4684
+ modelSelection: CLAUDE_MODEL_SELECTION,
4685
+ dispatchMode: { type: "queue_after_active" },
4686
+ ...(freshContext ? { freshContext: true } : {}),
4687
+ });
4688
+ const send = (name: string) =>
4689
+ Effect.gen(function* () {
4690
+ yield* dispatch(name);
4691
+ yield* worker.drain();
4692
+ yield* waitForIdle(threadId);
4693
+ });
4694
+ yield* orchestrator.dispatch({
4695
+ type: "thread.create",
4696
+ createdBy: "user",
4697
+ creationSource: "web",
4698
+ commandId: CommandId.make("command:fresh-context-old-session-wake:create"),
4699
+ threadId,
4700
+ projectId: ProjectId.make("project:fresh-context-old-session-wake"),
4701
+ title: "Fresh context with an old session's background task",
4702
+ modelSelection: CLAUDE_MODEL_SELECTION,
4703
+ runtimeMode: "full-access",
4704
+ interactionMode: "default",
4705
+ branch: null,
4706
+ worktreePath: cwd,
4707
+ });
4708
+ yield* dispatch("before-clear");
4709
+ yield* Deferred.await(started);
4710
+ // Queued behind a running turn, the fresh run starts without waiting for its background work.
4711
+ yield* dispatch("after-clear", true);
4712
+ // The turn ends with a run_in_background command still running in its session.
4713
+ yield* Ref.set(backgroundWork, true);
4714
+ yield* Deferred.succeed(release, undefined);
4715
+ yield* worker.drain();
4716
+ yield* waitForIdle(threadId);
4717
+ yield* send("after-clear-reply");
4718
+ const turns = () => Ref.get(capturedTurns);
4719
+ const turnFor = (all: ReadonlyArray<CapturedTurn>, name: string) =>
4720
+ all.find((turn) => turn.text.endsWith(name))!;
4721
+ const oldProviderThreadId = turnFor(yield* turns(), "before-clear").providerThreadId;
4722
+ assert.notEqual(
4723
+ turnFor(yield* turns(), "after-clear").providerThreadId,
4724
+ oldProviderThreadId,
4725
+ );
4726
+ assert.equal(
4727
+ turnFor(yield* turns(), "after-clear-reply").nativeThreadId,
4728
+ turnFor(yield* turns(), "after-clear").nativeThreadId,
4729
+ );
4730
+ // The old Claude session reports its background task settled, as on task_notification.
4731
+ const oldProviderThread = (yield* orchestrator.getThreadProjection(
4732
+ threadId,
4733
+ )).providerThreads.find((providerThread) => providerThread.id === oldProviderThreadId)!;
4734
+ const settledAt = yield* DateTime.now;
4735
+ yield* eventSink.write({
4736
+ events: [
4737
+ {
4738
+ id: EventId.make("event:fresh-context-old-session-wake:old-session-settled"),
4739
+ type: "provider-thread.updated",
4740
+ threadId,
4741
+ occurredAt: settledAt,
4742
+ payload: { ...oldProviderThread, pendingBackgroundTasks: [], updatedAt: settledAt },
4743
+ },
4744
+ ],
4745
+ });
4746
+ yield* send("next-user-message");
4747
+ const all = yield* turns();
4748
+ assert.equal(
4749
+ turnFor(all, "next-user-message").nativeThreadId,
4750
+ turnFor(all, "after-clear").nativeThreadId,
4751
+ );
4752
+ }).pipe(
4753
+ Effect.provide(
4754
+ ProviderReplayHarness.layerWithRegistry(
4755
+ {
4756
+ name: "fresh-context-old-session-wake",
4757
+ runtimePolicyOverride: {
4758
+ cwd,
4759
+ approvalPolicy: "never",
4760
+ sandboxPolicy: {
4761
+ type: "readOnly",
4762
+ access: { type: "fullAccess" },
4763
+ networkAccess: false,
4764
+ },
4765
+ },
4766
+ },
4767
+ layerRegistry,
4768
+ ),
4769
+ ),
4770
+ );
4771
+ }),
4772
+ );
4773
+
4774
it.live("keeps the thread a queued fresh run reserved for that run", () =>
4775
Effect.gen(function* () {
4776
const cwd = yield* checkpointWorkspace("fresh-context-reserved");