Skip to content

Commit a24c909

Browse files
committed
fix(server): commit delegated cancellation with its stop
1 parent a0f6c4b commit a24c909

7 files changed

Lines changed: 205 additions & 151 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.test.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,12 @@ describe("OrchestratorMcpService", () => {
630630
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
631631
Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),
632632
),
633-
cancelDelegatedTask: ({ stop }) => stop,
633+
stopDelegatedTasks: () => Effect.void,
634+
cancelDelegatedTask: ({ command, stopDescendants }) =>
635+
stopDescendants.pipe(
636+
Effect.andThen(Ref.update(dispatched, (commands) => [...commands, command])),
637+
Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),
638+
),
634639
}),
635640
ServerSettings.layerTest(),
636641
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -716,7 +721,10 @@ describe("OrchestratorMcpService", () => {
716721
),
717722
),
718723
stopDelegatedTasks: () => Effect.void,
719-
cancelDelegatedTask: ({ stop }) => stop,
724+
cancelDelegatedTask: ({ command, stopDescendants }) =>
725+
stopDescendants.pipe(
726+
Effect.andThen(Ref.update(dispatched, (commands) => [...commands, command])),
727+
),
720728
}),
721729
ServerSettings.layerTest(),
722730
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -803,7 +811,10 @@ describe("OrchestratorMcpService", () => {
803811
),
804812
),
805813
stopDelegatedTasks: () => Effect.void,
806-
cancelDelegatedTask: ({ stop }) => stop,
814+
cancelDelegatedTask: ({ command, stopDescendants }) =>
815+
stopDescendants.pipe(
816+
Effect.andThen(Ref.update(dispatched, (commands) => [...commands, command])),
817+
),
807818
}),
808819
ServerSettings.layerTest(),
809820
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -916,7 +927,10 @@ describe("OrchestratorMcpService", () => {
916927
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
917928
Effect.as({} as never),
918929
),
919-
cancelDelegatedTask: ({ stop }) => stop,
930+
cancelDelegatedTask: ({ command, stopDescendants }) =>
931+
stopDescendants.pipe(
932+
Effect.andThen(Ref.update(dispatched, (commands) => [...commands, command])),
933+
),
920934
stopDelegatedTasks: () =>
921935
Effect.gen(function* () {
922936
const limit = yield* DispatchModeLimit;

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ const make = Effect.gen(function* () {
15611561
)
15621562
: workState === "result_available"
15631563
? // A cancelled task's finished turn settles it as interrupted.
1564-
task.cancelRequestedAt !== undefined &&
1564+
task.cancelledRunId !== undefined &&
15651565
taskStatusForRun(taskResultRun ?? childRun) === "completed"
15661566
? "interrupted"
15671567
: taskStatusForRun(taskResultRun ?? childRun)
@@ -2375,40 +2375,35 @@ const make = Effect.gen(function* () {
23752375
const stopChild = Effect.gen(function* () {
23762376
const commandId = stableCommandId({ scope, requestKey: key, operation: "cancel-task" });
23772377
const reason = agentStopReason(input.reason);
2378-
const stopThread = threadManagement
2379-
.dispatch({
2380-
type: "thread.stop",
2381-
commandId,
2382-
threadId: current.childThreadId,
2383-
stopDelegatedTasks: false,
2384-
reason,
2385-
})
2386-
.pipe(
2387-
Effect.mapError((error) =>
2388-
failure(
2389-
"task_not_cancellable",
2390-
`Unable to stop delegated task ${input.taskId}: ${errorMessage(error)}`,
2391-
),
2392-
),
2393-
);
2394-
// An unsettled task records the cancel, so it settles as interrupted once its turn ends.
2395-
yield* isTerminalTaskStatus(current.status)
2396-
? stopThread
2397-
: threadManagement.cancelDelegatedTask({
2398-
parentThreadId: scope.thread.threadId,
2399-
taskId: input.taskId,
2400-
stop: stopThread,
2401-
});
2402-
yield* threadManagement
2403-
.stopDelegatedTasks({ threadId: current.childThreadId, commandId, reason })
2404-
.pipe(
2405-
Effect.mapError((error) =>
2406-
failure(
2407-
"task_not_cancellable",
2408-
`Stopped delegated task ${input.taskId}, but not every task it delegated: ${errorMessage(error)}`,
2409-
),
2378+
const command = {
2379+
type: "thread.stop" as const,
2380+
commandId,
2381+
threadId: current.childThreadId,
2382+
stopDelegatedTasks: false,
2383+
reason,
2384+
};
2385+
const stopDescendants = threadManagement.stopDelegatedTasks({
2386+
threadId: current.childThreadId,
2387+
commandId,
2388+
reason,
2389+
});
2390+
yield* (
2391+
isTerminalTaskStatus(current.status)
2392+
? stopDescendants.pipe(Effect.andThen(threadManagement.dispatch(command)))
2393+
: threadManagement.cancelDelegatedTask({
2394+
parentThreadId: scope.thread.threadId,
2395+
taskId: input.taskId,
2396+
command,
2397+
stopDescendants,
2398+
})
2399+
).pipe(
2400+
Effect.mapError((error) =>
2401+
failure(
2402+
"task_not_cancellable",
2403+
`Unable to stop delegated task ${input.taskId}: ${errorMessage(error)}`,
24102404
),
2411-
);
2405+
),
2406+
);
24122407
const stopped = yield* threadManagement
24132408
.getThreadRecords(current.childThreadId, ["runs"])
24142409
.pipe(Effect.mapError(threadManagementFailure));

‎apps/server/src/mcp/TaskCancelNativeSubagent.integration.test.ts‎

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ const driver = ProviderDriverKind.make("claudeAgent");
5656
const instanceId = ProviderInstanceId.make("claudeAgent");
5757
const modelSelection = { instanceId, model: "claude-test" };
5858

59-
// A delegated child ends its turn while a background native subagent (#17154) or shell (#16739)
60-
// it started keeps running. task_cancel arrives after the run settles, during its checkpoint, or
61-
// while the turn runs; "recorded" exits before anything settles. Each case then restarts.
59+
// Cancellation races with turn completion and checkpoint capture; "recorded" restarts before the Stop outbox runs.
6260
const cancelBackgroundOnlyTask = (
6361
background: "subagent" | "command",
6462
{
6563
cancelAt = "settled",
66-
}: { readonly cancelAt?: "settled" | "checkpoint" | "turn" | "recorded" | "monitor" } = {},
64+
}: {
65+
readonly cancelAt?: "settled" | "checkpoint" | "turn" | "recorded" | "monitor" | "refused";
66+
} = {},
6767
) =>
6868
Effect.scoped(
6969
Effect.gen(function* () {
@@ -171,7 +171,33 @@ const cancelBackgroundOnlyTask = (
171171
);
172172
const layerOrchestration = Layer.merge(
173173
layerOrchestrator,
174-
ThreadManagementService.layer.pipe(
174+
Layer.effect(
175+
ThreadManagementService.ThreadManagementService,
176+
Effect.map(ThreadManagementService.ThreadManagementService, (service) =>
177+
cancelAt === "refused"
178+
? {
179+
...service,
180+
stopDelegatedTasks: () =>
181+
Effect.fail(
182+
new Orchestrator.OrchestratorDispatchError({
183+
commandId: CommandId.make("refused-descendant-stop"),
184+
commandType: "thread.stop",
185+
cause: "descendant refused stop",
186+
}),
187+
),
188+
}
189+
: service,
190+
),
191+
).pipe(
192+
Layer.provide(
193+
ThreadManagementService.layer.pipe(
194+
Layer.provide(layerOrchestrator),
195+
Layer.provide(ServerSettings.layerTest()),
196+
Layer.provide(
197+
CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)),
198+
),
199+
),
200+
),
175201
Layer.provide(layerOrchestrator),
176202
Layer.provide(ServerSettings.layerTest()),
177203
Layer.provide(
@@ -412,7 +438,8 @@ const cancelBackgroundOnlyTask = (
412438
yield* Fiber.join(waiting);
413439
});
414440
if (cancelAt !== "turn") yield* endTurn;
415-
const turnSettled = cancelAt !== "checkpoint" && cancelAt !== "turn";
441+
const turnSettled =
442+
cancelAt !== "checkpoint" && cancelAt !== "turn" && cancelAt !== "recorded";
416443
if (turnSettled) yield* worker.drain();
417444
if (cancelAt === "monitor") {
418445
// A monitor notification then ran and completed while the shell kept running.
@@ -452,29 +479,45 @@ const cancelBackgroundOnlyTask = (
452479
parent.subagents.find((entry) => entry.id === task.id),
453480
);
454481
if (cancelAt === "recorded") {
455-
const unsettled = (yield* parentTask)!;
456-
yield* (yield* EventSink.EventSinkV2).write({
457-
commandId: CommandId.make("command:runtime-reconcile:cancel-recorded"),
458-
events: [
459-
{
460-
id: EventId.make("event:cancel-recorded"),
461-
type: "subagent.updated",
462-
threadId: parentThreadId,
463-
nodeId: task.id,
464-
occurredAt: now,
465-
payload: { ...unsettled, cancelRequestedAt: now },
466-
},
467-
],
468-
});
469-
yield* orchestrator.dispatch({
470-
type: "thread.stop",
471-
commandId: CommandId.make("stop-recorded"),
472-
threadId: task.childThreadId!,
482+
const commandId = CommandId.make("stop-recorded");
483+
yield* orchestrator.cancelDelegatedTask({
484+
parentThreadId,
485+
taskId: task.id,
486+
command: {
487+
type: "thread.stop",
488+
commandId,
489+
threadId: task.childThreadId!,
490+
stopDelegatedTasks: false,
491+
},
492+
stopDescendants: Effect.void,
473493
});
474-
yield* worker.drain();
494+
const committed = yield* eventSink.readByCommandId({ commandId }).pipe(Stream.runCollect);
495+
assert.isTrue(
496+
committed.some(
497+
({ event }) =>
498+
event.type === "subagent.updated" && event.payload.cancelledRunId !== undefined,
499+
),
500+
);
501+
assert.isTrue(
502+
committed.some(
503+
({ event }) =>
504+
event.type === "turn-item.updated" &&
505+
event.payload.type === "run_interrupt_request",
506+
),
507+
);
475508
assert.equal((yield* parentTask)?.status, "running");
476509
return { scope, taskId: task.id, settled: undefined };
477510
}
511+
if (cancelAt === "refused") {
512+
yield* Effect.flip(
513+
service.cancelTask(scope, { taskId: task.id, clientRequestId: "refused-cancel" }),
514+
);
515+
const after = yield* parentTask;
516+
assert.equal(after?.status, "running");
517+
assert.equal(after?.result, null);
518+
assert.isUndefined(after?.cancelledRunId);
519+
return undefined;
520+
}
478521
// Settling the task ends the async question it left, after its own row.
479522
const settled = yield* watch(
480523
(event) =>
@@ -532,12 +575,15 @@ const cancelBackgroundOnlyTask = (
532575

533576
return { scope, taskId: task.id, settled: settledTask };
534577
}).pipe(Effect.provide(layerTest));
578+
if (stopped === undefined) return;
535579
restarted = true;
536580

537581
// A server restarted on the same database runs startup recovery and keeps the outcome.
538582
yield* Effect.gen(function* () {
539583
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
540584
const orchestrator = yield* Orchestrator.OrchestratorV2;
585+
yield* (yield* EffectWorker.OrchestrationEffectWorkerV2).drain();
586+
yield* orchestrator.recoverDelegatedTasks;
541587
const task = (yield* orchestrator.getThreadProjection(
542588
stopped.scope.thread!.threadId,
543589
)).subagents.find((entry) => entry.id === stopped.taskId);
@@ -571,6 +617,10 @@ it.effect("task_cancel ends the shell's question though a monitor ran after the
571617
cancelBackgroundOnlyTask("command", { cancelAt: "monitor" }),
572618
);
573619

574-
it.effect("a recorded cancel settles at startup when the server exited before settling it", () =>
620+
it.effect("cancellation and Stop commit together and recover before the outbox ran", () =>
575621
cancelBackgroundOnlyTask("subagent", { cancelAt: "recorded" }),
576622
);
623+
624+
it.effect("a refused descendant stop does not settle or dispose its parent task", () =>
625+
cancelBackgroundOnlyTask("command", { cancelAt: "refused" }),
626+
);

‎apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
16051605
threadId: parentThreadId,
16061606
nodeId: settled.id,
16071607
occurredAt: now,
1608-
payload: { ...settled, cancelRequestedAt: now, cancelledRunId: child.childRunId },
1608+
payload: { ...settled, cancelledRunId: child.childRunId },
16091609
},
16101610
],
16111611
});
@@ -1737,7 +1737,13 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
17371737
yield* orchestrator.cancelDelegatedTask({
17381738
parentThreadId,
17391739
taskId: run.delegatedTaskId!,
1740-
stop: Effect.void,
1740+
command: {
1741+
type: "thread.stop",
1742+
commandId: CommandId.make(`cancel-followup-${outcome}`),
1743+
threadId: child.childThreadId,
1744+
stopDelegatedTasks: false,
1745+
},
1746+
stopDescendants: Effect.void,
17411747
});
17421748
}
17431749
yield* eventSink.write({
@@ -2293,13 +2299,19 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
22932299
yield* orchestrator.cancelDelegatedTask({
22942300
parentThreadId: threadId,
22952301
taskId: child.taskId,
2296-
stop: Effect.void,
2302+
command: {
2303+
type: "thread.stop",
2304+
commandId: CommandId.make("cancel-finished-child"),
2305+
threadId: child.childThreadId,
2306+
stopDelegatedTasks: false,
2307+
},
2308+
stopDescendants: Effect.void,
22972309
});
22982310
const task = (yield* orchestrator.getThreadProjection(threadId)).subagents.find(
22992311
(row) => row.id === child.taskId,
23002312
);
23012313
assert.equal(task?.status, "completed");
2302-
assert.isUndefined(task?.cancelRequestedAt);
2314+
assert.isUndefined(task?.cancelledRunId);
23032315
}),
23042316
);
23052317

0 commit comments

Comments
 (0)