Skip to content

Commit 805251b

Browse files
fix(server): thread setting changes no longer end a Claude session's background work (pingdotgg#35029)
1 parent 84e69da commit 805251b

2 files changed

Lines changed: 162 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
@@ -4073,6 +4073,42 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
40734073
})
40744074
: null;
40754075

4076+
// Archive and settle stop idle background work; serialized commands prevent starts racing detach.
4077+
// Settlement rejects active or blocked runs.
4078+
const detachSessionIds = new Set(
4079+
command.type === "thread.archive" || command.type === "thread.settle"
4080+
? (providerContext?.providerSessions ?? []).map((session) => session.id)
4081+
: command.type === "thread.metadata.update" &&
4082+
command.worktreePath !== undefined &&
4083+
command.worktreePath !== thread.worktreePath
4084+
? (providerContext?.providerSessions ?? []).map((session) => session.id)
4085+
: command.type === "thread.runtime-mode.set"
4086+
? (providerContext?.providerSessions ?? [])
4087+
.filter(
4088+
(session) => !session.capabilities.sessions.supportsRuntimeModeSwitchInSession,
4089+
)
4090+
.map((session) => session.id)
4091+
: (providerSwitchPlan?.releaseProviderSessionIds ?? []),
4092+
);
4093+
const liveSessions = (providerContext?.providerSessions ?? []).filter(
4094+
(session) =>
4095+
detachSessionIds.has(session.id) &&
4096+
session.status !== "stopped" &&
4097+
session.status !== "error",
4098+
);
4099+
if (command.type !== "thread.archive" && command.type !== "thread.settle") {
4100+
for (const session of liveSessions) {
4101+
if (yield* sessionHasBackgroundWork(session, providerContext!.providerThreads)) {
4102+
return yield* new OrchestratorDispatchError({
4103+
commandId: command.commandId,
4104+
commandType: command.type,
4105+
cause:
4106+
"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.",
4107+
});
4108+
}
4109+
}
4110+
}
4111+
40764112
const now = yield* DateTime.now;
40774113
if (command.type === "thread.metadata.update" && command.limitRecovery != null) {
40784114
const projection = yield* loadProjectionForCommand(
@@ -4864,30 +4900,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
48644900
yield* closePendingLinkRequests({ command, events, threadId: command.threadId, now });
48654901
}
48664902

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

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,131 @@ describe("orchestration v2 provider switching", () => {
28962896
),
28972897
);
28982898

2899+
// regression: #16000 thread setting changes must not end a Claude session's background work
2900+
it.live.each([
2901+
["runtime mode", "runtime-mode"],
2902+
["worktree", "worktree"],
2903+
["provider instance", "instance"],
2904+
] as const)("keeps background work alive when the %s changes on an idle thread", ([, change]) =>
2905+
Effect.scoped(
2906+
Effect.gen(function* () {
2907+
const cwd = yield* checkpointWorkspace(`settings-background-${change}`);
2908+
const nextCwd = yield* checkpointWorkspace(`settings-background-${change}-next`);
2909+
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
2910+
const backgroundWork = yield* Ref.make(false);
2911+
const closedSessions = yield* Ref.make<ReadonlyArray<string>>([]);
2912+
const secondSelection: ModelSelection = {
2913+
...CLAUDE_MODEL_SELECTION,
2914+
instanceId: ProviderInstanceId.make("claude-second-account"),
2915+
};
2916+
const threadId = ThreadId.make(`thread:settings-background-${change}`);
2917+
yield* Effect.gen(function* () {
2918+
const orchestrator = yield* Orchestrator.OrchestratorV2;
2919+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2920+
yield* orchestrator.dispatch({
2921+
type: "thread.create",
2922+
createdBy: "user",
2923+
creationSource: "web",
2924+
commandId: CommandId.make(`command:settings-background-${change}:create`),
2925+
threadId,
2926+
projectId: ProjectId.make(`project:settings-background-${change}`),
2927+
title: "Settings change with background work",
2928+
modelSelection: CLAUDE_MODEL_SELECTION,
2929+
runtimeMode: "full-access",
2930+
interactionMode: "default",
2931+
branch: null,
2932+
worktreePath: cwd,
2933+
});
2934+
yield* orchestrator.dispatch({
2935+
type: "message.dispatch",
2936+
createdBy: "user",
2937+
creationSource: "web",
2938+
commandId: CommandId.make(`command:settings-background-${change}:send`),
2939+
threadId,
2940+
messageId: MessageId.make(`message:settings-background-${change}:send`),
2941+
text: "Start background work",
2942+
attachments: [],
2943+
modelSelection: CLAUDE_MODEL_SELECTION,
2944+
dispatchMode: { type: "start_immediately" },
2945+
});
2946+
yield* worker.drain();
2947+
const afterFirst = yield* waitForIdle(threadId);
2948+
const session = afterFirst.providerSessions[0];
2949+
assert.equal(session?.status, "ready");
2950+
// The turn ended but a background subagent keeps running in the CLI process.
2951+
yield* Ref.set(backgroundWork, true);
2952+
2953+
const commandId = CommandId.make(`command:settings-background-${change}:change`);
2954+
yield* orchestrator
2955+
.dispatch(
2956+
change === "runtime-mode"
2957+
? {
2958+
type: "thread.runtime-mode.set",
2959+
commandId,
2960+
threadId,
2961+
runtimeMode: "auto-accept-edits",
2962+
}
2963+
: change === "worktree"
2964+
? { type: "thread.metadata.update", commandId, threadId, worktreePath: nextCwd }
2965+
: {
2966+
type: "thread.model-selection.set",
2967+
commandId,
2968+
threadId,
2969+
modelSelection: secondSelection,
2970+
},
2971+
)
2972+
.pipe(Effect.ignore);
2973+
yield* worker.drain();
2974+
2975+
const projection = yield* orchestrator.getThreadProjection(threadId);
2976+
// Refusing or deferring are both fine; closing the busy process is not.
2977+
assert.deepEqual(yield* Ref.get(closedSessions), []);
2978+
assert.deepEqual(
2979+
projection.providerSessions.map((candidate) => [candidate.id, candidate.status]),
2980+
[[session!.id, "ready"]],
2981+
);
2982+
}).pipe(
2983+
Effect.provide(
2984+
ProviderReplayHarness.layerWithRegistry(
2985+
{
2986+
name: `settings-background-${change}`,
2987+
runtimePolicyOverride: {
2988+
cwd,
2989+
approvalPolicy: "never",
2990+
sandboxPolicy: {
2991+
type: "readOnly",
2992+
access: { type: "fullAccess" },
2993+
networkAccess: false,
2994+
},
2995+
},
2996+
},
2997+
ProviderAdapterRegistry.layerFromAdapters([
2998+
makeTestAdapter({
2999+
instanceId: CLAUDE_MODEL_SELECTION.instanceId,
3000+
driver: CLAUDE_DRIVER,
3001+
capabilities: ClaudeProviderCapabilitiesV2,
3002+
modelSelection: CLAUDE_MODEL_SELECTION,
3003+
responseByRunOrdinal: { 1: "Background work started" },
3004+
capturedTurns,
3005+
backgroundWork,
3006+
closedSessions,
3007+
}),
3008+
makeTestAdapter({
3009+
instanceId: secondSelection.instanceId,
3010+
driver: CLAUDE_DRIVER,
3011+
capabilities: ClaudeProviderCapabilitiesV2,
3012+
modelSelection: secondSelection,
3013+
responseByRunOrdinal: {},
3014+
capturedTurns,
3015+
}),
3016+
]),
3017+
),
3018+
),
3019+
);
3020+
}),
3021+
),
3022+
);
3023+
28993024
it.live.each(["immediately", "behind a running turn"] as const)(
29003025
"starts a fresh provider session %s and never hands earlier runs to a later provider",
29013026
(variant) =>

0 commit comments

Comments
 (0)