The best way to control Claude Code, Codex and more from any computer or phone
fix(server): a setting change no longer detaches a busy session kept from an earlier hand-off (#35258)
Co-authored-by: Mika Hertler <mika@mikahertler.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Mika Hertler committed
Nov 14, 2026 at 19:30 UTC
a767d2329767031960b4dbb3567e21485ba1a2e7
2 files changed
+149
-2
apps/server/src/orchestration-v2/Orchestrator.ts
+13
-2
@@ -4449,9 +4449,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
4449
session.status !== "stopped" &&
4450
session.status !== "error",
4451
);
4452
- if (command.type !== "thread.archive" && command.type !== "thread.settle") {
4452
+ if (
4453
+ command.type !== "thread.archive" &&
4454
+ command.type !== "thread.settle" &&
4455
+ liveSessions.length > 0
4456
+ ) {
4457
+ // The provider context holds only the active and target provider threads; a session kept
4458
+ // busy by an earlier queued hand-off owns neither, so probe every thread of this one.
4459
+ const { providerThreads } = yield* projectionStore
4460
+ .getThreadRecords(command.threadId, ["providerThreads"], {
4461
+ omitSettledSubagentRecords: true,
4462
+ })
4463
+ .pipe(mapDispatchError(command));
4464
for (const session of liveSessions) {
4454
- if (yield* sessionHasBackgroundWork(session, providerContext!.providerThreads)) {
4465
+ if (yield* sessionHasBackgroundWork(session, providerThreads)) {
4466
return yield* new OrchestratorDispatchError({
4467
commandId: command.commandId,
4468
commandType: command.type,
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+136
@@ -3152,6 +3152,142 @@ describe("orchestration v2 provider switching", () => {
3152
}),
3153
);
3154
3155
+ // regression: a session kept by a queued hand-off is probed when a later change detaches it
3156
+ it.live(
3157
+ "refuses a later instance change while a retained hand-off session runs background work",
3158
+ () =>
3159
+ Effect.gen(function* () {
3160
+ const cwd = yield* checkpointWorkspace("retained-session-background");
3161
+ const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
3162
+ const codexBackgroundWork = yield* Ref.make(true);
3163
+ const firstStarted = yield* Deferred.make<void>();
3164
+ const releaseFirst = yield* Deferred.make<void>();
3165
+ const threadId = ThreadId.make("thread:retained-session-background");
3166
+ yield* Effect.gen(function* () {
3167
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
3168
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3169
+ const send = (ordinal: number, modelSelection: ModelSelection) =>
3170
+ orchestrator.dispatch({
3171
+ type: "message.dispatch",
3172
+ createdBy: "user",
3173
+ creationSource: "web",
3174
+ commandId: CommandId.make(`command:retained-session-background:${ordinal}`),
3175
+ threadId,
3176
+ messageId: MessageId.make(`message:retained-session-background:${ordinal}`),
3177
+ text: `Prompt ${ordinal}`,
3178
+ attachments: [],
3179
+ modelSelection,
3180
+ dispatchMode: { type: "queue_after_active" },
3181
+ });
3182
+ const selectGrok = (attempt: string) =>
3183
+ orchestrator.dispatch({
3184
+ type: "thread.model-selection.set",
3185
+ commandId: CommandId.make(`command:retained-session-background:select:${attempt}`),
3186
+ threadId,
3187
+ modelSelection: GROK_MODEL_SELECTION,
3188
+ });
3189
+ yield* orchestrator.dispatch({
3190
+ type: "thread.create",
3191
+ createdBy: "user",
3192
+ creationSource: "web",
3193
+ commandId: CommandId.make("command:retained-session-background:create"),
3194
+ threadId,
3195
+ projectId: ProjectId.make("project:retained-session-background"),
3196
+ title: "Retained session with background work",
3197
+ modelSelection: CODEX_MODEL_SELECTION,
3198
+ runtimeMode: "full-access",
3199
+ interactionMode: "default",
3200
+ branch: null,
3201
+ worktreePath: cwd,
3202
+ });
3203
+ yield* send(1, CODEX_MODEL_SELECTION);
3204
+ yield* Deferred.await(firstStarted);
3205
+ yield* send(2, CLAUDE_MODEL_SELECTION);
3206
+ yield* Deferred.succeed(releaseFirst, undefined);
3207
+ yield* worker.drain();
3208
+ const afterHandoff = yield* waitForIdle(threadId);
3209
+ assert.deepEqual(
3210
+ afterHandoff.runs.map((run) => [run.providerInstanceId, run.status]),
3211
+ [
3212
+ [CODEX_MODEL_SELECTION.instanceId, "completed"],
3213
+ [CLAUDE_MODEL_SELECTION.instanceId, "completed"],
3214
+ ],
3215
+ );
3216
+ // The queued hand-off kept the busy Codex session attached.
3217
+ const codexSession = afterHandoff.providerSessions.find(
3218
+ (session) => session.providerInstanceId === CODEX_MODEL_SELECTION.instanceId,
3219
+ );
3220
+ assert.equal(codexSession?.status, "ready");
3221
+
3222
+ const refused = yield* selectGrok("busy").pipe(Effect.flip);
3223
+ assert.include(String(refused.cause), "still running background agents or commands");
3224
+ yield* worker.drain();
3225
+ const unchanged = yield* orchestrator.getThreadProjection(threadId);
3226
+ assert.equal(unchanged.thread.providerInstanceId, CLAUDE_MODEL_SELECTION.instanceId);
3227
+ assert.include(
3228
+ unchanged.providerSessions.map((session) => session.id),
3229
+ codexSession!.id,
3230
+ );
3231
+
3232
+ yield* Ref.set(codexBackgroundWork, false);
3233
+ yield* selectGrok("idle");
3234
+ yield* worker.drain();
3235
+ const switched = yield* orchestrator.getThreadProjection(threadId);
3236
+ assert.equal(switched.thread.providerInstanceId, GROK_MODEL_SELECTION.instanceId);
3237
+ assert.notInclude(
3238
+ switched.providerSessions.map((session) => session.id),
3239
+ codexSession!.id,
3240
+ );
3241
+ }).pipe(
3242
+ Effect.provide(
3243
+ ProviderReplayHarness.layerWithRegistry(
3244
+ {
3245
+ name: "retained-session-background",
3246
+ runtimePolicyOverride: {
3247
+ cwd,
3248
+ approvalPolicy: "never",
3249
+ sandboxPolicy: {
3250
+ type: "readOnly",
3251
+ access: { type: "fullAccess" },
3252
+ networkAccess: false,
3253
+ },
3254
+ },
3255
+ },
3256
+ ProviderAdapterRegistry.layerFromAdapters([
3257
+ makeTestAdapter({
3258
+ instanceId: CODEX_MODEL_SELECTION.instanceId,
3259
+ driver: CODEX_DRIVER,
3260
+ capabilities: CodexProviderCapabilitiesV2,
3261
+ modelSelection: CODEX_MODEL_SELECTION,
3262
+ responseByRunOrdinal: { 1: "Codex started background work" },
3263
+ capturedTurns,
3264
+ pendingBackgroundWork: codexBackgroundWork,
3265
+ holdFirstTurn: firstStarted,
3266
+ releaseFirstTurn: releaseFirst,
3267
+ }),
3268
+ makeTestAdapter({
3269
+ instanceId: CLAUDE_MODEL_SELECTION.instanceId,
3270
+ driver: CLAUDE_DRIVER,
3271
+ capabilities: ClaudeProviderCapabilitiesV2,
3272
+ modelSelection: CLAUDE_MODEL_SELECTION,
3273
+ responseByRunOrdinal: { 2: "Claude complete" },
3274
+ capturedTurns,
3275
+ }),
3276
+ makeTestAdapter({
3277
+ instanceId: GROK_MODEL_SELECTION.instanceId,
3278
+ driver: GROK_DRIVER,
3279
+ capabilities: AcpProviderCapabilitiesV2,
3280
+ modelSelection: GROK_MODEL_SELECTION,
3281
+ responseByRunOrdinal: {},
3282
+ capturedTurns,
3283
+ }),
3284
+ ]),
3285
+ ),
3286
+ ),
3287
+ );
3288
+ }),
3289
+ );
3290
+
3291
it.live.each(["immediately", "behind a running turn"] as const)(
3292
"starts a fresh provider session %s and never hands earlier runs to a later provider",
3293
(variant) =>