Skip to content

Commit 3d1847e

Browse files
Mika Hertlerclaude
andcommitted
fix(server): a setting change no longer detaches a busy session kept from an earlier hand-off
A queued hand-off keeps a busy source session attached. A later setting change that released it probed for background work only against the provider threads in the narrowed provider context (active and target), so the retained session's per-thread probe found no thread, reported no work, and the session was detached while its background work still ran. Probe against all of the thread's provider threads instead, loaded only when a live session would be detached. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5911884 commit 3d1847e

2 files changed

Lines changed: 151 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,9 +4211,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
42114211
session.status !== "stopped" &&
42124212
session.status !== "error",
42134213
);
4214-
if (command.type !== "thread.archive" && command.type !== "thread.settle") {
4214+
if (
4215+
command.type !== "thread.archive" &&
4216+
command.type !== "thread.settle" &&
4217+
liveSessions.length > 0
4218+
) {
4219+
// The provider context holds only the active and target provider threads; a session kept
4220+
// busy by an earlier queued hand-off owns neither, so probe every thread of this one.
4221+
const { providerThreads } = yield* projectionStore
4222+
.getThreadRecords(command.threadId, ["providerThreads"], {
4223+
omitSettledSubagentRecords: true,
4224+
})
4225+
.pipe(mapDispatchError(command));
42154226
for (const session of liveSessions) {
4216-
if (yield* sessionHasBackgroundWork(session, providerContext!.providerThreads)) {
4227+
if (yield* sessionHasBackgroundWork(session, providerThreads)) {
42174228
return yield* new OrchestratorDispatchError({
42184229
commandId: command.commandId,
42194230
commandType: command.type,

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,6 +3021,144 @@ describe("orchestration v2 provider switching", () => {
30213021
),
30223022
);
30233023

3024+
// regression: a session kept busy by a queued hand-off is still probed when a later change detaches it
3025+
it.live(
3026+
"refuses a later instance change while a retained hand-off session runs background work",
3027+
() =>
3028+
Effect.scoped(
3029+
Effect.gen(function* () {
3030+
const cwd = yield* checkpointWorkspace("retained-session-background");
3031+
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
3032+
const codexBackgroundWork = yield* Ref.make(true);
3033+
const firstStarted = yield* Deferred.make<void>();
3034+
const releaseFirst = yield* Deferred.make<void>();
3035+
const threadId = ThreadId.make("thread:retained-session-background");
3036+
yield* Effect.gen(function* () {
3037+
const orchestrator = yield* Orchestrator.OrchestratorV2;
3038+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3039+
const send = (ordinal: number, modelSelection: ModelSelection) =>
3040+
orchestrator.dispatch({
3041+
type: "message.dispatch",
3042+
createdBy: "user",
3043+
creationSource: "web",
3044+
commandId: CommandId.make(`command:retained-session-background:${ordinal}`),
3045+
threadId,
3046+
messageId: MessageId.make(`message:retained-session-background:${ordinal}`),
3047+
text: `Prompt ${ordinal}`,
3048+
attachments: [],
3049+
modelSelection,
3050+
dispatchMode: { type: "queue_after_active" },
3051+
});
3052+
const selectGrok = (attempt: string) =>
3053+
orchestrator.dispatch({
3054+
type: "thread.model-selection.set",
3055+
commandId: CommandId.make(`command:retained-session-background:select:${attempt}`),
3056+
threadId,
3057+
modelSelection: GROK_MODEL_SELECTION,
3058+
});
3059+
yield* orchestrator.dispatch({
3060+
type: "thread.create",
3061+
createdBy: "user",
3062+
creationSource: "web",
3063+
commandId: CommandId.make("command:retained-session-background:create"),
3064+
threadId,
3065+
projectId: ProjectId.make("project:retained-session-background"),
3066+
title: "Retained session with background work",
3067+
modelSelection: CODEX_MODEL_SELECTION,
3068+
runtimeMode: "full-access",
3069+
interactionMode: "default",
3070+
branch: null,
3071+
worktreePath: cwd,
3072+
});
3073+
yield* send(1, CODEX_MODEL_SELECTION);
3074+
yield* Deferred.await(firstStarted);
3075+
yield* send(2, CLAUDE_MODEL_SELECTION);
3076+
yield* Deferred.succeed(releaseFirst, undefined);
3077+
yield* worker.drain();
3078+
const afterHandoff = yield* waitForIdle(threadId);
3079+
assert.deepEqual(
3080+
afterHandoff.runs.map((run) => [run.providerInstanceId, run.status]),
3081+
[
3082+
[CODEX_MODEL_SELECTION.instanceId, "completed"],
3083+
[CLAUDE_MODEL_SELECTION.instanceId, "completed"],
3084+
],
3085+
);
3086+
// The queued hand-off kept the busy Codex session attached.
3087+
const codexSession = afterHandoff.providerSessions.find(
3088+
(session) => session.providerInstanceId === CODEX_MODEL_SELECTION.instanceId,
3089+
);
3090+
assert.equal(codexSession?.status, "ready");
3091+
3092+
const refused = yield* selectGrok("busy").pipe(Effect.flip);
3093+
assert.include(String(refused.cause), "still running background agents or commands");
3094+
yield* worker.drain();
3095+
const unchanged = yield* orchestrator.getThreadProjection(threadId);
3096+
assert.equal(unchanged.thread.providerInstanceId, CLAUDE_MODEL_SELECTION.instanceId);
3097+
assert.include(
3098+
unchanged.providerSessions.map((session) => session.id),
3099+
codexSession!.id,
3100+
);
3101+
3102+
yield* Ref.set(codexBackgroundWork, false);
3103+
yield* selectGrok("idle");
3104+
yield* worker.drain();
3105+
const switched = yield* orchestrator.getThreadProjection(threadId);
3106+
assert.equal(switched.thread.providerInstanceId, GROK_MODEL_SELECTION.instanceId);
3107+
assert.notInclude(
3108+
switched.providerSessions.map((session) => session.id),
3109+
codexSession!.id,
3110+
);
3111+
}).pipe(
3112+
Effect.provide(
3113+
ProviderReplayHarness.layerWithRegistry(
3114+
{
3115+
name: "retained-session-background",
3116+
runtimePolicyOverride: {
3117+
cwd,
3118+
approvalPolicy: "never",
3119+
sandboxPolicy: {
3120+
type: "readOnly",
3121+
access: { type: "fullAccess" },
3122+
networkAccess: false,
3123+
},
3124+
},
3125+
},
3126+
ProviderAdapterRegistry.layerFromAdapters([
3127+
makeTestAdapter({
3128+
instanceId: CODEX_MODEL_SELECTION.instanceId,
3129+
driver: CODEX_DRIVER,
3130+
capabilities: CodexProviderCapabilitiesV2,
3131+
modelSelection: CODEX_MODEL_SELECTION,
3132+
responseByRunOrdinal: { 1: "Codex started background work" },
3133+
capturedTurns,
3134+
pendingBackgroundWork: codexBackgroundWork,
3135+
holdFirstTurn: firstStarted,
3136+
releaseFirstTurn: releaseFirst,
3137+
}),
3138+
makeTestAdapter({
3139+
instanceId: CLAUDE_MODEL_SELECTION.instanceId,
3140+
driver: CLAUDE_DRIVER,
3141+
capabilities: ClaudeProviderCapabilitiesV2,
3142+
modelSelection: CLAUDE_MODEL_SELECTION,
3143+
responseByRunOrdinal: { 2: "Claude complete" },
3144+
capturedTurns,
3145+
}),
3146+
makeTestAdapter({
3147+
instanceId: GROK_MODEL_SELECTION.instanceId,
3148+
driver: GROK_DRIVER,
3149+
capabilities: AcpProviderCapabilitiesV2,
3150+
modelSelection: GROK_MODEL_SELECTION,
3151+
responseByRunOrdinal: {},
3152+
capturedTurns,
3153+
}),
3154+
]),
3155+
),
3156+
),
3157+
);
3158+
}),
3159+
),
3160+
);
3161+
30243162
it.live.each(["immediately", "behind a running turn"] as const)(
30253163
"starts a fresh provider session %s and never hands earlier runs to a later provider",
30263164
(variant) =>

0 commit comments

Comments
 (0)