Skip to content

Commit 888405f

Browse files
fix(server): thread setting changes no longer end a Claude session's background work
Changing the runtime mode, worktree, or provider instance detached the Claude session at once, killing background subagents and shells running in the CLI process. The thread mutation now probes each session it would detach with the same background-work check message sends use, and refuses the change with a retryable message while work is still running. Archive and settle keep stopping idle background work as before. Fixes #16000 AI-assisted; verified with the tests and tools listed above.
1 parent fb8d08e commit 888405f

1 file changed

Lines changed: 37 additions & 24 deletions

File tree

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

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4071,6 +4071,42 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
40714071
})
40724072
: null;
40734073

4074+
// Archive and settle stop idle background work; serialized commands prevent starts racing detach.
4075+
// Settlement rejects active or blocked runs.
4076+
const detachSessionIds = new Set(
4077+
command.type === "thread.archive" || command.type === "thread.settle"
4078+
? (providerContext?.providerSessions ?? []).map((session) => session.id)
4079+
: command.type === "thread.metadata.update" &&
4080+
command.worktreePath !== undefined &&
4081+
command.worktreePath !== thread.worktreePath
4082+
? (providerContext?.providerSessions ?? []).map((session) => session.id)
4083+
: command.type === "thread.runtime-mode.set"
4084+
? (providerContext?.providerSessions ?? [])
4085+
.filter(
4086+
(session) => !session.capabilities.sessions.supportsRuntimeModeSwitchInSession,
4087+
)
4088+
.map((session) => session.id)
4089+
: (providerSwitchPlan?.releaseProviderSessionIds ?? []),
4090+
);
4091+
const liveSessions = (providerContext?.providerSessions ?? []).filter(
4092+
(session) =>
4093+
detachSessionIds.has(session.id) &&
4094+
session.status !== "stopped" &&
4095+
session.status !== "error",
4096+
);
4097+
if (command.type !== "thread.archive" && command.type !== "thread.settle") {
4098+
for (const session of liveSessions) {
4099+
if (yield* sessionHasBackgroundWork(session, providerContext!.providerThreads)) {
4100+
return yield* new OrchestratorDispatchError({
4101+
commandId: command.commandId,
4102+
commandType: command.type,
4103+
cause:
4104+
"The current session is still running background agents or commands, and this change would end them. Wait for them to finish, or press Stop, then try again.",
4105+
});
4106+
}
4107+
}
4108+
}
4109+
40744110
const now = yield* DateTime.now;
40754111
if (command.type === "thread.metadata.update" && command.limitRecovery != null) {
40764112
const projection = yield* loadProjectionForCommand(
@@ -4862,30 +4898,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
48624898
yield* closePendingLinkRequests({ command, events, threadId: command.threadId, now });
48634899
}
48644900

4865-
// Archive and settle stop idle background work; serialized commands prevent starts racing detach.
4866-
// Settlement rejects active or blocked runs.
4867-
const detachSessionIds = new Set(
4868-
command.type === "thread.archive" || command.type === "thread.settle"
4869-
? (providerContext?.providerSessions ?? []).map((session) => session.id)
4870-
: command.type === "thread.metadata.update" &&
4871-
command.worktreePath !== undefined &&
4872-
command.worktreePath !== thread.worktreePath
4873-
? (providerContext?.providerSessions ?? []).map((session) => session.id)
4874-
: command.type === "thread.runtime-mode.set"
4875-
? (providerContext?.providerSessions ?? [])
4876-
.filter(
4877-
(session) => !session.capabilities.sessions.supportsRuntimeModeSwitchInSession,
4878-
)
4879-
.map((session) => session.id)
4880-
: (providerSwitchPlan?.releaseProviderSessionIds ?? []),
4881-
);
4882-
if (detachSessionIds.size > 0) {
4883-
const liveSessions = (providerContext?.providerSessions ?? []).filter(
4884-
(session) =>
4885-
detachSessionIds.has(session.id) &&
4886-
session.status !== "stopped" &&
4887-
session.status !== "error",
4888-
);
4901+
if (liveSessions.length > 0) {
48894902
yield* Effect.forEach(
48904903
liveSessions,
48914904
(session) =>

0 commit comments

Comments
 (0)