Skip to content

Commit fd9b2c3

Browse files
only21milclaudemaria-rcksjuliusmarminge
authored
fix(server): cancelling a background-only delegated task reports it interrupted (#21676)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent d835e43 commit fd9b2c3

10 files changed

Lines changed: 675 additions & 61 deletions

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ describe("OrchestratorMcpService", () => {
594594
listAppOwnedChildThreadIds: () => Effect.succeed([]),
595595
delegatedTaskResultPending: () => Effect.succeed(false),
596596
stopDelegatedTasks: () => Effect.void,
597+
cancelDelegatedTask: ({ command }) =>
598+
Ref.update(dispatched, (commands) => [...commands, command]),
597599
// The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
598600
dispatch: (command) =>
599601
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
@@ -697,6 +699,12 @@ describe("OrchestratorMcpService", () => {
697699
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
698700
Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),
699701
),
702+
stopDelegatedTasks: () => Effect.void,
703+
cancelDelegatedTask: ({ command, stopDescendants }) =>
704+
stopDescendants(() => Effect.die("No remote descendants in this test")).pipe(
705+
Effect.andThen(Ref.update(dispatched, (commands) => [...commands, command])),
706+
Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),
707+
),
700708
}),
701709
ServerSettings.layerTest(),
702710
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -786,6 +794,10 @@ describe("OrchestratorMcpService", () => {
786794
),
787795
),
788796
stopDelegatedTasks: () => Effect.void,
797+
cancelDelegatedTask: ({ command, stopDescendants }) =>
798+
stopDescendants(() => Effect.die("No remote descendants in this test")).pipe(
799+
Effect.andThen(Ref.update(dispatched, (commands) => [...commands, command])),
800+
),
789801
}),
790802
ServerSettings.layerTest(),
791803
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -876,6 +888,10 @@ describe("OrchestratorMcpService", () => {
876888
),
877889
),
878890
stopDelegatedTasks: () => Effect.void,
891+
cancelDelegatedTask: ({ command, stopDescendants }) =>
892+
stopDescendants(() => Effect.die("No remote descendants in this test")).pipe(
893+
Effect.andThen(Ref.update(dispatched, (commands) => [...commands, command])),
894+
),
879895
}),
880896
ServerSettings.layerTest(),
881897
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -1041,6 +1057,10 @@ describe("OrchestratorMcpService", () => {
10411057
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
10421058
Effect.as({} as never),
10431059
),
1060+
cancelDelegatedTask: ({ command, stopDescendants }) =>
1061+
stopDescendants(() => Effect.die("No remote descendants in this test")).pipe(
1062+
Effect.andThen(Ref.update(dispatched, (commands) => [...commands, command])),
1063+
),
10441064
stopDelegatedTasks: () =>
10451065
Effect.gen(function* () {
10461066
const limit = yield* DispatchModeLimit;

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,11 @@ const make = Effect.gen(function* () {
15341534
: childRun,
15351535
)
15361536
: workState === "result_available"
1537-
? taskStatusForRun(taskResultRun ?? childRun)
1537+
? // A cancelled task's finished turn settles it as interrupted.
1538+
task.cancelledRunId !== undefined &&
1539+
taskStatusForRun(taskResultRun ?? childRun) === "completed"
1540+
? "interrupted"
1541+
: taskStatusForRun(taskResultRun ?? childRun)
15381542
: taskStatusForRun(taskResultRun ?? childRun) === "queued"
15391543
? "queued"
15401544
: "running";
@@ -2640,13 +2644,25 @@ const make = Effect.gen(function* () {
26402644
const stopChild = Effect.gen(function* () {
26412645
const commandId = stableCommandId({ scope, requestKey: key, operation: "cancel-task" });
26422646
const reason = agentStopReason(input.reason);
2647+
const command = {
2648+
type: "thread.stop" as const,
2649+
commandId,
2650+
threadId: childThreadId,
2651+
stopDelegatedTasks: false,
2652+
reason,
2653+
};
26432654
yield* threadManagement
2644-
.dispatch({
2645-
type: "thread.stop",
2646-
commandId,
2647-
threadId: childThreadId,
2648-
stopDelegatedTasks: false,
2649-
reason,
2655+
.cancelDelegatedTask({
2656+
parentThreadId: scope.thread.threadId,
2657+
taskId: input.taskId,
2658+
command,
2659+
stopDescendants: (remoteDispatch) =>
2660+
threadManagement.stopDelegatedTasks({
2661+
threadId: childThreadId,
2662+
commandId,
2663+
reason,
2664+
remoteDispatch,
2665+
}),
26502666
})
26512667
.pipe(
26522668
Effect.mapError((error) =>
@@ -2656,16 +2672,6 @@ const make = Effect.gen(function* () {
26562672
),
26572673
),
26582674
);
2659-
yield* threadManagement
2660-
.stopDelegatedTasks({ threadId: childThreadId, commandId, reason })
2661-
.pipe(
2662-
Effect.mapError((error) =>
2663-
failure(
2664-
"task_not_cancellable",
2665-
`Stopped delegated task ${input.taskId}, but not every task it delegated: ${errorMessage(error)}`,
2666-
),
2667-
),
2668-
);
26692675
const stopped = yield* threadManagement
26702676
.getThreadRecords(childThreadId, ["runs"])
26712677
.pipe(Effect.mapError(threadManagementFailure));

0 commit comments

Comments
 (0)