The best way to control Claude Code, Codex and more from any computer or phone
fix(server): provider switches on portable forks keep inherited conversation (#36839)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 14, 2026 at 18:43 UTC
2e331341862bf850d9dc65e523083c42e392b745
2 files changed
+170
-10
apps/server/src/orchestration-v2/Orchestrator.ts
+15
-10
@@ -2785,6 +2785,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2785
runs: projection.runs,
2786
strategy: handoffStrategy,
2787
items: [
2788
+ ...(needsFullContext && contextBoundary === 0
2789
+ ? yield* readInheritedItems(projection.thread)
2790
+ : []),
2791
...(needsFullContext && latestCompletedRun !== undefined
2792
? legacyImportItems
2793
: []),
@@ -8519,12 +8522,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
8522
(requiresFullProviderSwitchContext ? 0 : (targetLastCompletedRun?.ordinal ?? 0)) &&
8523
run.ordinal <= latestHandoffRun.ordinal,
8524
);
8525
+ const providerSwitchNeedsFullContext =
8526
+ targetProviderThread === undefined ||
8527
+ targetProviderThread.nativeThreadRef === null ||
8528
+ requiresFullProviderSwitchContext;
8529
const providerSwitchItems =
8530
providerSwitchCoveredRuns.length === 0
8531
? []
8532
: [
8526
- ...(latestCompletedRun !== undefined &&
8527
- (targetProviderThread === undefined || requiresFullProviderSwitchContext)
8533
+ ...(providerSwitchNeedsFullContext && contextBoundary === 0
8534
+ ? yield* readInheritedItems(projection.thread)
8535
+ : []),
8536
+ ...(latestCompletedRun !== undefined && providerSwitchNeedsFullContext
8537
? legacyImportItems
8538
: []),
8539
...(yield* readHandoffItems(
@@ -8549,10 +8558,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
8558
threadId: command.threadId,
8559
providerInstanceId: modelSelection.instanceId,
8560
capabilities,
8552
- strategy:
8553
- targetProviderThread === undefined || requiresFullProviderSwitchContext
8554
- ? "full_thread_summary"
8555
- : "delta_context",
8561
+ strategy: providerSwitchNeedsFullContext ? "full_thread_summary" : "delta_context",
8562
}),
8563
);
8564
}
@@ -8578,10 +8584,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
8584
from: providerSwitchCoveredRuns[0]!.ordinal,
8585
to: providerSwitchCoveredRuns.at(-1)!.ordinal,
8586
},
8581
- strategy:
8582
- targetProviderThread === undefined || requiresFullProviderSwitchContext
8583
- ? "full_thread_summary"
8584
- : "delta_since_target_last_seen",
8587
+ strategy: providerSwitchNeedsFullContext
8588
+ ? "full_thread_summary"
8589
+ : "delta_since_target_last_seen",
8590
items: providerSwitchItems,
8591
runs: projection.runs,
8592
createdAt: now,
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+155
@@ -4429,4 +4429,159 @@ describe("orchestration v2 provider switching", () => {
4429
"reissues imported v1 context when a queued provider starts after the first provider fails",
4430
() => importedFailureScenario(true),
4431
);
4432
+
4433
+ it.live.each([
4434
+ "immediately",
4435
+ "behind a running turn",
4436
+ "after a cancelled queued switch",
4437
+ ] as const)(
4438
+ "hands a portable fork's inherited conversation to a provider switched %s",
4439
+ (variant) =>
4440
+ Effect.gen(function* () {
4441
+ const queued = variant !== "immediately";
4442
+ const cancelled = variant === "after a cancelled queued switch";
4443
+ const key = cancelled ? "cancelled" : queued ? "queued" : "immediate";
4444
+ const cwd = yield* checkpointWorkspace(`fork-switch-history-${key}`);
4445
+ const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
4446
+ const started = yield* Deferred.make<void>();
4447
+ const release = yield* Deferred.make<void>();
4448
+ const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
4449
+ makeTestAdapter({
4450
+ instanceId: CODEX_MODEL_SELECTION.instanceId,
4451
+ driver: CODEX_DRIVER,
4452
+ capabilities: CodexProviderCapabilitiesV2,
4453
+ modelSelection: CODEX_MODEL_SELECTION,
4454
+ responseByRunOrdinal: { 1: "The release marker is violet." },
4455
+ capturedTurns,
4456
+ }),
4457
+ makeTestAdapter({
4458
+ instanceId: CLAUDE_MODEL_SELECTION.instanceId,
4459
+ driver: CLAUDE_DRIVER,
4460
+ capabilities: ClaudeProviderCapabilitiesV2,
4461
+ modelSelection: CLAUDE_MODEL_SELECTION,
4462
+ responseByRunOrdinal: {},
4463
+ capturedTurns,
4464
+ ...(queued ? { holdFirstTurn: started, releaseFirstTurn: release } : {}),
4465
+ }),
4466
+ ]);
4467
+ const sourceThreadId = ThreadId.make(`thread:fork-switch-history:${key}:source`);
4468
+ const forkThreadId = ThreadId.make(`thread:fork-switch-history:${key}:fork`);
4469
+ yield* Effect.gen(function* () {
4470
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
4471
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
4472
+ const send = (threadId: ThreadId, name: string, modelSelection: ModelSelection) =>
4473
+ orchestrator.dispatch({
4474
+ type: "message.dispatch",
4475
+ createdBy: "user",
4476
+ creationSource: "web",
4477
+ commandId: CommandId.make(`command:fork-switch-history:${key}:${name}`),
4478
+ threadId,
4479
+ messageId: MessageId.make(`message:fork-switch-history:${key}:${name}`),
4480
+ text: `Prompt ${name}`,
4481
+ attachments: [],
4482
+ modelSelection,
4483
+ dispatchMode: { type: "start_immediately" },
4484
+ });
4485
+ const settle = (threadId: ThreadId) =>
4486
+ worker.drain().pipe(
4487
+ Effect.andThen(waitForIdle(threadId)),
4488
+ Effect.tap(() => worker.drain()),
4489
+ );
4490
+ yield* orchestrator.dispatch({
4491
+ type: "thread.create",
4492
+ createdBy: "user",
4493
+ creationSource: "web",
4494
+ commandId: CommandId.make(`command:fork-switch-history:${key}:create`),
4495
+ threadId: sourceThreadId,
4496
+ projectId,
4497
+ title: "Fork switch history",
4498
+ modelSelection: CODEX_MODEL_SELECTION,
4499
+ runtimeMode: "full-access",
4500
+ interactionMode: "default",
4501
+ branch: null,
4502
+ worktreePath: null,
4503
+ });
4504
+ yield* send(sourceThreadId, "source", CODEX_MODEL_SELECTION);
4505
+ const source = yield* settle(sourceThreadId);
4506
+ yield* orchestrator.dispatch({
4507
+ type: "thread.fork",
4508
+ createdBy: "user",
4509
+ creationSource: "web",
4510
+ commandId: CommandId.make(`command:fork-switch-history:${key}:fork`),
4511
+ sourceThreadId,
4512
+ targetThreadId: forkThreadId,
4513
+ sourcePoint: { type: "run", runId: source.runs[0]!.id },
4514
+ title: "Fork",
4515
+ });
4516
+ // A first turn on another provider resolves the fork through portable context.
4517
+ yield* send(forkThreadId, "fork-claude", CLAUDE_MODEL_SELECTION);
4518
+ if (queued) {
4519
+ yield* Deferred.await(started);
4520
+ yield* worker.drain();
4521
+ yield* send(
4522
+ forkThreadId,
4523
+ cancelled ? "fork-codex-cancelled" : "fork-codex",
4524
+ CODEX_MODEL_SELECTION,
4525
+ );
4526
+ if (cancelled) {
4527
+ // The cancelled run leaves its reserved Codex row behind without a native thread.
4528
+ const pending = (yield* orchestrator.getThreadProjection(forkThreadId)).runs.at(-1)!;
4529
+ yield* orchestrator.dispatch({
4530
+ type: "queued-run.cancel",
4531
+ commandId: CommandId.make(`command:fork-switch-history:${key}:cancel`),
4532
+ threadId: forkThreadId,
4533
+ runId: pending.id,
4534
+ });
4535
+ }
4536
+ yield* Deferred.succeed(release, undefined);
4537
+ if (cancelled) {
4538
+ yield* settle(forkThreadId);
4539
+ yield* send(forkThreadId, "fork-codex", CODEX_MODEL_SELECTION);
4540
+ }
4541
+ } else {
4542
+ yield* settle(forkThreadId);
4543
+ yield* send(forkThreadId, "fork-codex", CODEX_MODEL_SELECTION);
4544
+ }
4545
+ yield* settle(forkThreadId);
4546
+ yield* send(forkThreadId, "fork-claude-again", CLAUDE_MODEL_SELECTION);
4547
+ yield* settle(forkThreadId);
4548
+
4549
+ const turns = yield* Ref.get(capturedTurns);
4550
+ const turnFor = (name: string) =>
4551
+ turns.find((turn) => turn.text.endsWith(`Prompt ${name}`))!.text;
4552
+ const switched = turnFor("fork-codex");
4553
+ // The switched provider gets the conversation the fork shows, oldest first.
4554
+ assert.include(switched, "Prompt source");
4555
+ const positions = ["Prompt source", "release marker is violet", "Prompt fork-claude"].map(
4556
+ (text) => switched.indexOf(text),
4557
+ );
4558
+ assert.deepEqual(
4559
+ positions,
4560
+ positions.toSorted((a, b) => a - b),
4561
+ );
4562
+ // Claude already saw the inherited rows, so its return only carries the Codex run.
4563
+ const returned = turnFor("fork-claude-again");
4564
+ assert.include(returned, "Prompt fork-codex");
4565
+ assert.notInclude(returned, "Prompt source");
4566
+ }).pipe(
4567
+ Effect.provide(
4568
+ ProviderReplayHarness.layerWithRegistry(
4569
+ {
4570
+ name: `fork-switch-history-${key}`,
4571
+ runtimePolicyOverride: {
4572
+ cwd,
4573
+ approvalPolicy: "never",
4574
+ sandboxPolicy: {
4575
+ type: "readOnly",
4576
+ access: { type: "fullAccess" },
4577
+ networkAccess: false,
4578
+ },
4579
+ },
4580
+ },
4581
+ layerRegistry,
4582
+ ),
4583
+ ),
4584
+ );
4585
+ }),
4586
+ );
4587
});