Skip to content

Commit 94f54a5

Browse files
fix(server): preserve active turns when switching provider instances (#22755)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 8c9af54 commit 94f54a5

6 files changed

Lines changed: 220 additions & 4 deletions

File tree

‎apps/server/src/mcp/threadAccess.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export const dispatchFailure = (
4848
error: OrchestratorV2Error | ThreadManagement.ThreadManagementProviderInstanceDisabledError,
4949
) =>
5050
error._tag === "ThreadManagementProviderInstanceDisabledError" ||
51-
error._tag === "OrchestratorRuntimeModeAboveCeilingError"
51+
error._tag === "OrchestratorRuntimeModeAboveCeilingError" ||
52+
error._tag === "OrchestratorProviderSelectionBusyError"
5253
? new OrchestratorMcpFailure({ code: "invalid_request", message: error.message })
5354
: (error._tag === "OrchestratorDispatchError" ||
5455
error._tag === "OrchestratorCommandRejectedError") &&

‎apps/server/src/mcp/toolkits/core.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
OrchestratorCommandRejectedError,
3636
OrchestratorDispatchError,
3737
OrchestratorProjectionError,
38+
OrchestratorProviderSelectionBusyError,
3839
} from "../../orchestration-v2/Orchestrator.ts";
3940

4041
import * as ServerSecretStore from "../../auth/ServerSecretStore.ts";
@@ -227,6 +228,11 @@ it("bounds public command rejections and redacts internal dispatch causes", () =
227228
new OrchestratorCommandRejectedError({ ...command, cause: "Run is not queued." }),
228229
).message,
229230
).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 });
230236
for (const cause of [
231237
undefined,
232238
"",

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,194 @@ const layerTest = Layer.mergeAll(
6262
),
6363
);
6464

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+
65253
it.effect(
66254
"records approval feedback and targets only its runtime request without dispatching a parent turn",
67255
() =>

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ export class OrchestratorThreadAboveModeLimitError extends Schema.TaggedError<Or
291291
}
292292
}
293293

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+
294303
const isOrchestratorDispatchError = Schema.is(OrchestratorDispatchError);
295304

296305
/** The ceiling refusal under however many dispatch errors wrapped it on the way out. */
@@ -355,6 +364,7 @@ export const OrchestratorV2Error = Schema.Union([
355364
OrchestratorCommandIdConflictError,
356365
OrchestratorSubagentThreadReadOnlyError,
357366
OrchestratorThreadAboveModeLimitError,
367+
OrchestratorProviderSelectionBusyError,
358368
OrchestratorRuntimeModeAboveCeilingError,
359369
]);
360370
export type OrchestratorV2Error = typeof OrchestratorV2Error.Type;
@@ -3832,12 +3842,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
38323842
}),
38333843
),
38343844
);
3835-
return yield* providerSwitchService
3845+
const plan = yield* providerSwitchService
38363846
.plan({
38373847
projection: providerContext!,
38383848
targetModelSelection: command.modelSelection,
38393849
})
38403850
.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;
38413863
})
38423864
: null;
38433865

‎apps/server/src/orchestration-v2/ThreadMessageIntake.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function dispatchWasNotAccepted(
2424
case "OrchestratorCommandIdConflictError":
2525
case "OrchestratorSubagentThreadReadOnlyError":
2626
case "OrchestratorThreadAboveModeLimitError":
27+
case "OrchestratorProviderSelectionBusyError":
2728
case "OrchestratorRuntimeModeAboveCeilingError":
2829
case "ThreadManagementProviderInstanceDisabledError":
2930
return true;

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6287,7 +6287,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
62876287
it.effect.each([
62886288
["server", modelSelection],
62896289
["provider", modelSelection],
6290-
["provider", { ...modelSelection, instanceId: alternateInstanceId }],
62916290
] as const)(
62926291
"delivers a queued monitor update from %s after conversation selection %j",
62936292
([creationSource, currentSelection]) =>
@@ -6400,7 +6399,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
64006399

64016400
it.effect.each([
64026401
{ ...modelSelection, model: "gpt-5.5", options: [{ id: "reasoningEffort", value: "high" }] },
6403-
{ instanceId: alternateInstanceId, model: "gpt-5.5" },
64046402
])(
64056403
"starts a queued delegated completion on a selection set while it waited: %j",
64066404
(newerSelection) =>

0 commit comments

Comments
 (0)