Skip to content

Commit fcf0650

Browse files
fix(server): cancelling a queued task reads the child's runs once
A turn the queue promoted between the ownership check and the queued-run sweep was neither cancelled nor interrupted, so task_cancel returned cancel_requested while it kept running. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 22d4c20 commit fcf0650

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ describe("OrchestratorMcpService", () => {
823823
readonly name: string;
824824
// C's turn also ends, and D's queued turn starts, before C's interrupt lands.
825825
readonly endsBeforeInterrupt: boolean;
826+
// The promotion lands right after the cancel's Nth read of the child's runs.
827+
readonly promotesAfterRunsRead?: number;
826828
}) {
827829
const parentThreadId = ThreadId.make(`thread:${input.name}-parent`);
828830
const childThreadId = ThreadId.make(`thread:${input.name}-child`);
@@ -865,12 +867,25 @@ describe("OrchestratorMcpService", () => {
865867
runtimeRequests: [],
866868
}) as unknown as OrchestrationV2ThreadProjection;
867869
const child = yield* Ref.make(childProjection(["running", "queued", "queued"]));
870+
const runsReads = yield* Ref.make(0);
868871
const layerDependencies = Layer.mergeAll(
869872
NodeServices.layer,
870873
NodeCrypto.layer,
871874
Layer.mock(ThreadManagementService.ThreadManagementService)({
872-
getThreadRecords: (threadId) =>
873-
threadId === parentThreadId ? Effect.succeed(parentProjection) : Ref.get(child),
875+
getThreadRecords: (threadId, records) =>
876+
threadId === parentThreadId
877+
? Effect.succeed(parentProjection)
878+
: Effect.tap(Ref.get(child), () =>
879+
records.length === 1 && records[0] === "runs"
880+
? Effect.flatMap(
881+
Ref.updateAndGet(runsReads, (reads) => reads + 1),
882+
(reads) =>
883+
reads === input.promotesAfterRunsRead
884+
? Ref.set(child, childProjection(["completed", "running", "queued"]))
885+
: Effect.void,
886+
)
887+
: Effect.void,
888+
),
874889
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
875890
listAppOwnedChildThreadIds: () => Effect.succeed([]),
876891
stopDelegatedTasks: () => Effect.void,
@@ -939,6 +954,22 @@ describe("OrchestratorMcpService", () => {
939954
}),
940955
);
941956

957+
it.effect("stops a queued follow-up task whose turn starts right after the cancel reads it", () =>
958+
Effect.gen(function* () {
959+
const cancelled = yield* cancelPromotedTask({
960+
name: "mcp-cancel-promoted-after-read",
961+
endsBeforeInterrupt: false,
962+
promotesAfterRunsRead: 2,
963+
});
964+
assert.equal(cancelled.status, "cancel_requested");
965+
assert.deepEqual(cancelled.commands, [
966+
{ type: "queued-run.cancel", runId: cancelled.runId },
967+
{ type: "run.interrupt", runId: cancelled.runId },
968+
{ type: "delegated_task.completion-delivery.dispose", runId: undefined },
969+
]);
970+
}),
971+
);
972+
942973
it.effect("never stops the turn the queue starts after a cancelled task's turn ends", () =>
943974
Effect.gen(function* () {
944975
const cancelled = yield* cancelPromotedTask({

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,8 +2668,11 @@ const make = Effect.gen(function* () {
26682668
),
26692669
),
26702670
);
2671+
// One snapshot for the check and the sweep: a turn promoted between two reads would
2672+
// be neither cancelled nor interrupted.
2673+
const runs = yield* readRuns;
26712674
// The child's live turn belongs to another task, so only this task's queued turns go.
2672-
if (!delegatedTaskYieldsLiveRun(yield* readRuns, input.taskId)) {
2675+
if (!delegatedTaskYieldsLiveRun(runs, input.taskId)) {
26732676
yield* threadManagement
26742677
.dispatch({
26752678
type: "thread.stop",
@@ -2697,7 +2700,7 @@ const make = Effect.gen(function* () {
26972700
),
26982701
);
26992702
}
2700-
for (const run of yield* readRuns) {
2703+
for (const run of runs) {
27012704
if (
27022705
run.status !== "queued" ||
27032706
(run.delegatedTaskId !== input.taskId && run.id !== current.childRunId)

0 commit comments

Comments
 (0)