Skip to content

Commit 4e4ca96

Browse files
t3dotggclaude
andcommitted
fix(server): a live delegate keeps its result delivery, and task_cancel still stops the child
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8d0ab9a commit 4e4ca96

4 files changed

Lines changed: 47 additions & 14 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ describe("OrchestratorMcpService", () => {
420420
: Effect.succeed(childProjection),
421421
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
422422
delegatedTaskResultPending: () => Effect.succeed(false),
423+
stopDelegatedTasks: () => Effect.void,
423424
// The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
424425
dispatch: (command) =>
425426
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
@@ -458,9 +459,15 @@ describe("OrchestratorMcpService", () => {
458459
clientRequestId: "cancel-orphan-task",
459460
});
460461
assert.deepEqual(result, { taskId, status: "cancelled" });
461-
const [stop, ...rest] = yield* Ref.get(dispatched);
462-
assert.isEmpty(rest);
463-
assert.equal(stop?.type, "subagent.stop");
462+
const commands = yield* Ref.get(dispatched);
463+
// A child whose run finished can still run another turn's work, so it is stopped too.
464+
assert.deepEqual(
465+
commands.flatMap((command) =>
466+
command.type === "thread.stop" ? [command.threadId] : [],
467+
),
468+
childState === "a finished run" ? [childThreadId] : [],
469+
);
470+
const stop = commands.find((command) => command.type === "subagent.stop");
464471
if (stop?.type !== "subagent.stop") return;
465472
assert.deepEqual(
466473
{ threadId: stop.threadId, runId: stop.runId, subagentId: stop.subagentId },

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,12 +2382,13 @@ const make = Effect.gen(function* () {
23822382
return subagents.some((task) => task.id === input.taskId && task.status === "cancelled");
23832383
});
23842384
if (isTerminalTaskStatus(current.status)) {
2385-
if (yield* cancelOrphan) return { taskId: input.taskId, status: "cancelled" };
2385+
// The child can still run work for another turn, so it is stopped either way.
23862386
yield* stopWithinLimit;
2387+
const cancelled = yield* cancelOrphan;
23872388
yield* disposeCompletionDelivery;
23882389
return {
23892390
taskId: input.taskId,
2390-
status: current.status,
2391+
status: cancelled ? "cancelled" : current.status,
23912392
} satisfies OrchestratorMcpTaskCancelResult;
23922393
}
23932394
const child = yield* loadProjection(current.childThreadId);

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9412,14 +9412,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
94129412
const run = projection.runs.find((candidate) => candidate.id === command.runId);
94139413
const subagent = projection.subagents.find((candidate) => candidate.id === command.subagentId);
94149414
// A user Stop ends an app-owned task here, whatever state its child is in, so a task that lost
9415-
// its child run (#19002) cannot outlive Stop. The client then stops the child thread; a child
9416-
// run that settles later still records its real outcome. A task that already ended is written
9415+
// its child run (#19002) cannot outlive Stop. The client then stops the child thread. A live
9416+
// child run bound to the task keeps its delivery: when it settles it records the real outcome
9417+
// and wakes the parent, whose own task may wait on it. A task that already ended is written
94179418
// again, so a client holding a stale copy stops showing it as running.
94189419
if (
94199420
subagent?.origin === "app_owned" &&
94209421
subagent.childThreadId !== null &&
94219422
subagent.runId === command.runId
94229423
) {
9424+
const childThreadId = subagent.childThreadId;
9425+
const child = yield* projectionStore.getThreadRecords(childThreadId, ["runs"]).pipe(
9426+
Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
9427+
Effect.mapError(
9428+
(cause) => new OrchestratorProjectionError({ threadId: childThreadId, cause }),
9429+
),
9430+
);
9431+
const original =
9432+
child?.thread.forkedFrom?.type === "node" && child.thread.forkedFrom.nodeId === subagent.id;
9433+
const willSettle =
9434+
child?.runs.some(
9435+
(run) =>
9436+
(run.status === "queued" || isBlockingRun(run)) &&
9437+
(run.delegatedTaskId === subagent.id ||
9438+
(original && run.delegatedTaskId === undefined)),
9439+
) ?? false;
94239440
const records = yield* loadProjectionForCommand(command, ["nodes", "turnItems"], {
94249441
turnItemTypes: ["subagent"],
94259442
turnItemRunIds: [subagent.runId],
@@ -9429,9 +9446,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
94299446
return yield* writeAppOwnedTask({
94309447
command,
94319448
events,
9432-
task: isOrchestrationV2WorkActive(subagent.status)
9433-
? cancelledAppOwnedTask(subagent, now)
9434-
: subagent,
9449+
task: !isOrchestrationV2WorkActive(subagent.status)
9450+
? subagent
9451+
: willSettle
9452+
? { ...subagent, status: "cancelled", completedAt: now, updatedAt: now }
9453+
: cancelledAppOwnedTask(subagent, now),
94359454
nodes: records.nodes,
94369455
turnItems: records.turnItems,
94379456
now,

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ it.effect.each([
166166
["idle", "run.interrupt"],
167167
["idle", "subagent.stop"],
168168
["running", "subagent.stop"],
169+
["bound", "subagent.stop"],
169170
["archived", "thread.stop"],
170171
["missing", "thread.stop"],
171172
["running", "thread.stop"],
@@ -279,7 +280,9 @@ it.effect.each([
279280
const childRunStatus =
280281
childState === "running" || childState === "queued" || childState === "waiting"
281282
? childState
282-
: "completed";
283+
: childState === "bound"
284+
? "running"
285+
: "completed";
283286
yield* apply({
284287
type: "run.updated",
285288
threadId: childThreadId,
@@ -299,12 +302,14 @@ it.effect.each([
299302
...runAtStart,
300303
status: childRunStatus,
301304
completedAt: childRunStatus === "completed" ? now : null,
302-
...(childState === "correlated" ? { delegatedTaskId: taskId } : {}),
305+
...(childState === "correlated" || childState === "bound"
306+
? { delegatedTaskId: taskId }
307+
: {}),
303308
},
304309
});
305310
assert.equal(
306311
(yield* orchestrator.getThreadProjection(childThreadId)).runs[0]?.delegatedTaskId,
307-
childState === "correlated" ? taskId : undefined,
312+
childState === "correlated" || childState === "bound" ? taskId : undefined,
308313
);
309314
if (childState === "provider-active" || childState === "roster") {
310315
yield* apply({
@@ -445,9 +450,10 @@ it.effect.each([
445450
assert.equal(after.subagents.find((task) => task.id === taskId)?.status, expected);
446451
assert.equal(after.nodes.find((node) => node.id === taskId)?.status, expected);
447452
assert.equal(after.turnItems.find((item) => item.id === itemId)?.status, expected);
453+
// A live child run bound to the task still reports its outcome to the parent.
448454
assert.equal(
449455
after.subagents.find((task) => task.id === taskId)?.completionDelivery?.state,
450-
"disposed",
456+
childState === "bound" ? undefined : "disposed",
451457
);
452458
assert.equal(
453459
after.subagents.find((task) => task.id === original.id)?.result,

0 commit comments

Comments
 (0)