Skip to content

Commit 28a6972

Browse files
Mika Hertlerclaude
andcommitted
fix(server): a steer that switches provider no longer ends background work
Same rule as a queued start: when the steered run's session still runs background work, a hand-off restarts the run without detaching that session, and an account switch that would resume its native thread queues behind the run instead of replacing the session. A promoted queued message in that case stays queued. Reported on pingdotgg#32296 (Antigravity session/close kills background tasks). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c646c34 commit 28a6972

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6153,6 +6153,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
61536153
selectionTransition?.type === "create_with_handoff";
61546154
const requiresProviderSessionRestart =
61556155
canResumeAcrossInstances || selectionTransition?.type === "restart_session";
6156+
// Same rule as a queued start: a hand-off leaves a busy session up, and an account switch,
6157+
// which resumes that session's native thread, waits in the queue behind the run.
6158+
const projectedSession = input.projection.providerSessions.find(
6159+
(candidate) => candidate.id === providerSessionId,
6160+
);
6161+
const keepsBusySession =
6162+
(requiresProviderThreadHandoff || canResumeAcrossInstances) &&
6163+
projectedSession !== undefined &&
6164+
(yield* sessionHasBackgroundWork(projectedSession, input.projection.providerThreads));
6165+
if (keepsBusySession && canResumeAcrossInstances) return "queue_behind_run" as const;
61566166
if (requiresProviderThreadHandoff) {
61576167
const targetAdapter = yield* providerAdapters.get(input.modelSelection.instanceId).pipe(
61586168
Effect.mapError(
@@ -6282,7 +6292,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
62826292
updatedAt: now,
62836293
consumedAt: now,
62846294
};
6285-
restartSessionTransition = { type: "detach" };
6295+
restartSessionTransition = keepsBusySession ? null : { type: "detach" };
62866296
} else if (requiresProviderSessionRestart) {
62876297
const nextProviderSessionId = yield* mapDispatchError(input.command)(
62886298
idAllocator.allocate.providerSession({
@@ -7168,19 +7178,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
71687178
}
71697179
if (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") {
71707180
yield* ensureGoalCommandSupported(command, command, modelSelection.instanceId);
7171-
yield* dispatchSteerIntoRun({
7181+
const targetRunId = dispatchMode.targetRunId;
7182+
const steered = yield* dispatchSteerIntoRun({
71727183
command,
71737184
events,
71747185
effects,
71757186
projection,
71767187
modelSelection:
71777188
delegatedCompletion === undefined && command.notification === undefined
71787189
? modelSelection
7179-
: (projection.runs.find((run) => run.id === dispatchMode.targetRunId)?.modelSelection ??
7190+
: (projection.runs.find((run) => run.id === targetRunId)?.modelSelection ??
71807191
modelSelection),
71817192
delegatedCompletion,
71827193
...(command.notification === undefined ? {} : { notification: command.notification }),
7183-
targetRunId: dispatchMode.targetRunId,
7194+
targetRunId,
71847195
messageId: command.messageId,
71857196
text: dispatchText,
71867197
...(command.context ? { context: command.context } : {}),
@@ -7195,7 +7206,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
71957206
...(command.idleCompaction === undefined ? {} : { idleCompaction: command.idleCompaction }),
71967207
forceRestart: dispatchMode.type === "restart_active",
71977208
});
7198-
return;
7209+
if (steered !== "queue_behind_run") return;
7210+
dispatchMode = { type: "queue_after_active" };
71997211
}
72007212

72017213
const activeProviderThread =
@@ -11174,7 +11186,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1117411186
},
1117511187
});
1117611188

11177-
yield* dispatchSteerIntoRun({
11189+
const steered = yield* dispatchSteerIntoRun({
1117811190
command,
1117911191
events,
1118011192
effects,
@@ -11201,6 +11213,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1120111213
: { idleCompaction: queuedMessage.idleCompaction }),
1120211214
forceRestart: false,
1120311215
});
11216+
// The message is already queued behind the run, where the account switch waits.
11217+
if (steered === "queue_behind_run") {
11218+
return yield* new OrchestratorDispatchError({
11219+
commandId: command.commandId,
11220+
commandType: command.type,
11221+
cause:
11222+
"The current session is still running background agents or commands, and switching accounts now would end them. The message stays queued and starts once they finish.",
11223+
});
11224+
}
1120411225
});
1120511226

1120611227
const dispatchQueuedRunReorder = Effect.fn("Orchestrator.dispatchQueuedRunReorder")(function* (

0 commit comments

Comments
 (0)