The best way to control Claude Code, Codex and more from any computer or phone
fix(server): preserve active turns when switching provider instances (#22755)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 11, 2026 at 00:50 UTC
94f54a5710a9e5be20465e8e83b995a607e27718
6 files changed
+220
-4
apps/server/src/mcp/threadAccess.ts
+2
-1
@@ -48,7 +48,8 @@ export const dispatchFailure = (
48
error: OrchestratorV2Error | ThreadManagement.ThreadManagementProviderInstanceDisabledError,
49
) =>
50
error._tag === "ThreadManagementProviderInstanceDisabledError" ||
51
- error._tag === "OrchestratorRuntimeModeAboveCeilingError"
51
+ error._tag === "OrchestratorRuntimeModeAboveCeilingError" ||
52
+ error._tag === "OrchestratorProviderSelectionBusyError"
53
? new OrchestratorMcpFailure({ code: "invalid_request", message: error.message })
54
: (error._tag === "OrchestratorDispatchError" ||
55
error._tag === "OrchestratorCommandRejectedError") &&
apps/server/src/mcp/toolkits/core.test.ts
+6
@@ -35,6 +35,7 @@ import {
35
OrchestratorCommandRejectedError,
36
OrchestratorDispatchError,
37
OrchestratorProjectionError,
38
+ OrchestratorProviderSelectionBusyError,
39
} from "../../orchestration-v2/Orchestrator.ts";
40
41
import * as ServerSecretStore from "../../auth/ServerSecretStore.ts";
@@ -227,6 +228,11 @@ it("bounds public command rejections and redacts internal dispatch causes", () =
228
new OrchestratorCommandRejectedError({ ...command, cause: "Run is not queued." }),
229
).message,
230
).toBe("Run is not queued.");
231
+ const busy = new OrchestratorProviderSelectionBusyError({
232
+ commandId: command.commandId,
233
+ threadId,
234
+ });
235
+ expect(dispatchFailure(busy)).toMatchObject({ code: "invalid_request", message: busy.message });
236
for (const cause of [
237
undefined,
238
"",
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+188
@@ -62,6 +62,194 @@ const layerTest = Layer.mergeAll(
62
),
63
);
64
65
+it.effect.each(
66
+ (["provider.switch", "thread.model-selection.set"] as const).flatMap((type) =>
67
+ [
68
+ { active: true, selectionChange: "instance" },
69
+ { active: false, selectionChange: "instance" },
70
+ { active: true, selectionChange: "next-turn" },
71
+ { active: true, selectionChange: "restart" },
72
+ ].map((scenario) => ({ type, ...scenario })),
73
+ ),
74
+)("$type $selectionChange ($active)", ({ type, active, selectionChange }) =>
75
+ Effect.gen(function* () {
76
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
77
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
78
+ const outbox = yield* EffectOutbox.EffectOutboxV2;
79
+ const threadId = ThreadId.make("thread:instance-switch");
80
+ const now = yield* DateTime.now;
81
+ const sessionId = ProviderSessionId.make("session:instance-switch");
82
+ const providerThreadId = ProviderThreadId.make("provider-thread:instance-switch");
83
+ const runId = RunId.make("run:instance-switch");
84
+ const nodeId = NodeId.make("node:instance-switch");
85
+ const attemptId = RunAttemptId.make("attempt:instance-switch");
86
+ yield* orchestrator.dispatch({
87
+ type: "thread.create",
88
+ commandId: CommandId.make("create-instance-switch"),
89
+ threadId,
90
+ projectId: ProjectId.make("project:instance-switch"),
91
+ title: "Instance switch",
92
+ modelSelection,
93
+ runtimeMode: "full-access",
94
+ interactionMode: "default",
95
+ branch: null,
96
+ worktreePath: null,
97
+ createdBy: "user",
98
+ creationSource: "web",
99
+ });
100
+ yield* projections.apply({
101
+ id: EventId.make("instance-switch:session"),
102
+ type: "provider-session.attached",
103
+ threadId,
104
+ occurredAt: now,
105
+ payload: {
106
+ id: sessionId,
107
+ driver: adapter.driver,
108
+ providerInstanceId: instanceId,
109
+ status: "ready",
110
+ cwd: "/repo",
111
+ model: modelSelection.model,
112
+ capabilities: CodexProviderCapabilitiesV2,
113
+ createdAt: now,
114
+ updatedAt: now,
115
+ lastError: null,
116
+ },
117
+ });
118
+ yield* projections.apply({
119
+ id: EventId.make("instance-switch:provider-thread"),
120
+ type: "provider-thread.updated",
121
+ threadId,
122
+ occurredAt: now,
123
+ payload: {
124
+ id: providerThreadId,
125
+ driver: adapter.driver,
126
+ providerInstanceId: instanceId,
127
+ providerSessionId: sessionId,
128
+ appThreadId: threadId,
129
+ ownerNodeId: null,
130
+ nativeThreadRef: {
131
+ driver: adapter.driver,
132
+ nativeId: "native:instance-switch",
133
+ strength: "strong",
134
+ },
135
+ nativeConversationHeadRef: null,
136
+ status: "idle",
137
+ firstRunOrdinal: 1,
138
+ lastRunOrdinal: 1,
139
+ handoffIds: [],
140
+ forkedFrom: null,
141
+ createdAt: now,
142
+ updatedAt: now,
143
+ },
144
+ });
145
+ if (active) {
146
+ yield* projections.apply({
147
+ id: EventId.make("instance-switch:run"),
148
+ type: "run.created",
149
+ threadId,
150
+ occurredAt: now,
151
+ payload: {
152
+ id: runId,
153
+ threadId,
154
+ ordinal: 1,
155
+ providerInstanceId: instanceId,
156
+ modelSelection,
157
+ providerThreadId,
158
+ userMessageId: MessageId.make("message:instance-switch"),
159
+ rootNodeId: nodeId,
160
+ activeAttemptId: attemptId,
161
+ status: "running",
162
+ requestedAt: now,
163
+ startedAt: now,
164
+ completedAt: null,
165
+ checkpointId: null,
166
+ contextHandoffId: null,
167
+ },
168
+ });
169
+ yield* projections.apply({
170
+ id: EventId.make("instance-switch:turn"),
171
+ type: "provider-turn.updated",
172
+ threadId,
173
+ runId,
174
+ occurredAt: now,
175
+ payload: {
176
+ id: ProviderTurnId.make("provider-turn:instance-switch"),
177
+ providerThreadId,
178
+ nodeId,
179
+ runAttemptId: attemptId,
180
+ nativeTurnRef: { driver: adapter.driver, nativeId: "native:turn", strength: "strong" },
181
+ ordinal: 1,
182
+ status: "running",
183
+ startedAt: now,
184
+ completedAt: null,
185
+ },
186
+ });
187
+ }
188
+ const before = yield* projections.getThreadProjection(threadId);
189
+ const sequence = yield* orchestrator.getThreadEventSequence(threadId);
190
+ const commandId = CommandId.make("switch-instance");
191
+ const target = {
192
+ instanceId:
193
+ selectionChange === "instance" ? ProviderInstanceId.make("codex-other") : instanceId,
194
+ model: selectionChange === "instance" ? modelSelection.model : "gpt-6",
195
+ };
196
+ const outcome = yield* orchestrator
197
+ .dispatch({
198
+ type,
199
+ commandId,
200
+ threadId,
201
+ modelSelection: target,
202
+ })
203
+ .pipe(Effect.result);
204
+ const after = yield* projections.getThreadProjection(threadId);
205
+ if (active && selectionChange !== "next-turn") {
206
+ assert.deepEqual(
207
+ after,
208
+ before,
209
+ "a refused switch must preserve the turn, session and selection",
210
+ );
211
+ assert.deepEqual(
212
+ yield* outbox.listByCommandId(commandId),
213
+ [],
214
+ "a refused switch must not release the provider session",
215
+ );
216
+ assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence);
217
+ assert.ok(Result.isFailure(outcome));
218
+ assert.instanceOf(outcome.failure, Orchestrator.OrchestratorProviderSelectionBusyError);
219
+ } else {
220
+ assert.ok(Result.isSuccess(outcome));
221
+ assert.deepEqual(after.thread.modelSelection, target);
222
+ assert.equal(after.thread.providerInstanceId, target.instanceId);
223
+ assert.deepEqual(after.runs, before.runs);
224
+ assert.deepEqual(after.providerTurns, before.providerTurns);
225
+ }
226
+ }).pipe(
227
+ Effect.provide(
228
+ Layer.mergeAll(
229
+ layerDatabase,
230
+ ProjectionStore.layer.pipe(Layer.provide(layerDatabase)),
231
+ EffectOutbox.layer.pipe(Layer.provide(layerDatabase)),
232
+ ProviderReplayHarness.layerWithRegistry(
233
+ { name: "instance-switch" },
234
+ ProviderAdapterRegistry.layerFromAdapters([
235
+ {
236
+ ...adapter,
237
+ planSelectionTransition: () =>
238
+ Effect.succeed(
239
+ selectionChange === "restart"
240
+ ? { type: "restart_session" as const }
241
+ : { type: "apply_on_next_turn" as const },
242
+ ),
243
+ },
244
+ { ...adapter, instanceId: ProviderInstanceId.make("codex-other") },
245
+ ]),
246
+ { databaseLayer: layerDatabase, runEffectWorker: false },
247
+ ),
248
+ ),
249
+ ),
250
+ ),
251
+);
252
+
253
it.effect(
254
"records approval feedback and targets only its runtime request without dispatching a parent turn",
255
() =>
apps/server/src/orchestration-v2/Orchestrator.ts
+23
-1
@@ -291,6 +291,15 @@ export class OrchestratorThreadAboveModeLimitError extends Schema.TaggedError<Or
291
}
292
}
293
294
+export class OrchestratorProviderSelectionBusyError extends Schema.TaggedError<OrchestratorProviderSelectionBusyError>()(
295
+ "OrchestratorProviderSelectionBusyError",
296
+ { commandId: CommandId, threadId: ThreadId },
297
+) {
298
+ override get message(): string {
299
+ return `Thread ${this.threadId} has an active turn. Wait for it to finish, then retry the provider selection change.`;
300
+ }
301
+}
302
+
303
const isOrchestratorDispatchError = Schema.is(OrchestratorDispatchError);
304
305
/** The ceiling refusal under however many dispatch errors wrapped it on the way out. */
@@ -355,6 +364,7 @@ export const OrchestratorV2Error = Schema.Union([
364
OrchestratorCommandIdConflictError,
365
OrchestratorSubagentThreadReadOnlyError,
366
OrchestratorThreadAboveModeLimitError,
367
+ OrchestratorProviderSelectionBusyError,
368
OrchestratorRuntimeModeAboveCeilingError,
369
]);
370
export type OrchestratorV2Error = typeof OrchestratorV2Error.Type;
@@ -3832,12 +3842,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
3842
}),
3843
),
3844
);
3835
- return yield* providerSwitchService
3845
+ const plan = yield* providerSwitchService
3846
.plan({
3847
projection: providerContext!,
3848
targetModelSelection: command.modelSelection,
3849
})
3850
.pipe(mapDispatchError(command));
3851
+ if (plan.instanceChanged || plan.releaseProviderSessionIds.length > 0) {
3852
+ const projection = yield* loadProjectionForCommand(command, ["runs"], {
3853
+ turnItemTypes: [],
3854
+ });
3855
+ if (projection.runs.some(isActiveRun)) {
3856
+ return yield* new OrchestratorProviderSelectionBusyError({
3857
+ commandId: command.commandId,
3858
+ threadId: command.threadId,
3859
+ });
3860
+ }
3861
+ }
3862
+ return plan;
3863
})
3864
: null;
3865
apps/server/src/orchestration-v2/ThreadMessageIntake.ts
+1
@@ -24,6 +24,7 @@ function dispatchWasNotAccepted(
24
case "OrchestratorCommandIdConflictError":
25
case "OrchestratorSubagentThreadReadOnlyError":
26
case "OrchestratorThreadAboveModeLimitError":
27
+ case "OrchestratorProviderSelectionBusyError":
28
case "OrchestratorRuntimeModeAboveCeilingError":
29
case "ThreadManagementProviderInstanceDisabledError":
30
return true;
apps/server/src/orchestration-v2/runtimeLayer.test.ts
-2
@@ -6287,7 +6287,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
6287
it.effect.each([
6288
["server", modelSelection],
6289
["provider", modelSelection],
6290
- ["provider", { ...modelSelection, instanceId: alternateInstanceId }],
6290
] as const)(
6291
"delivers a queued monitor update from %s after conversation selection %j",
6292
([creationSource, currentSelection]) =>
@@ -6400,7 +6399,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
6399
6400
it.effect.each([
6401
{ ...modelSelection, model: "gpt-5.5", options: [{ id: "reasoningEffort", value: "high" }] },
6403
- { instanceId: alternateInstanceId, model: "gpt-5.5" },
6402
])(
6403
"starts a queued delegated completion on a selection set while it waited: %j",
6404
(newerSelection) =>