Skip to content

Commit f32c23c

Browse files
t3dotggclaude
andauthored
fix(server): Stop also stops delegated tasks and pull request watches (pingdotgg#16002)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 2bffcc5 commit f32c23c

18 files changed

Lines changed: 1271 additions & 183 deletions

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ describe("OrchestratorMcpService", () => {
316316
}),
317317
);
318318

319-
it.effect("does not dispose delivery when the child interrupt fails", () =>
319+
it.effect("does not dispose delivery when the child stop fails", () =>
320320
Effect.gen(function* () {
321321
const parentThreadId = ThreadId.make("thread:mcp-cancel-failed-parent");
322322
const childThreadId = ThreadId.make("thread:mcp-cancel-failed-child");
@@ -355,7 +355,7 @@ describe("OrchestratorMcpService", () => {
355355
Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
356356
dispatch: (command) =>
357357
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
358-
Effect.andThen(Effect.fail(new Error("simulated interrupt failure") as never)),
358+
Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),
359359
),
360360
}),
361361
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -386,13 +386,13 @@ describe("OrchestratorMcpService", () => {
386386
assert.equal(error.code, "task_not_cancellable");
387387
assert.deepEqual(
388388
(yield* Ref.get(dispatched)).map((command) => (command as { type: string }).type),
389-
["run.interrupt"],
389+
["thread.stop"],
390390
);
391391
}).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
392392
}),
393393
);
394394

395-
it.effect("returns cancel requested when post-interrupt disposal fails", () =>
395+
it.effect("returns cancel requested when post-stop disposal fails", () =>
396396
Effect.gen(function* () {
397397
const parentThreadId = ThreadId.make("thread:mcp-cancel-dispose-failed-parent");
398398
const childThreadId = ThreadId.make("thread:mcp-cancel-dispose-failed-child");
@@ -437,6 +437,7 @@ describe("OrchestratorMcpService", () => {
437437
: Effect.succeed({} as never),
438438
),
439439
),
440+
stopDelegatedTasks: () => Effect.void,
440441
}),
441442
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
442443
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
@@ -467,7 +468,7 @@ describe("OrchestratorMcpService", () => {
467468
assert.equal(result.status, "cancel_requested");
468469
assert.deepEqual(
469470
(yield* Ref.get(dispatched)).map((command) => (command as { type: string }).type),
470-
["run.interrupt", "delegated_task.completion-delivery.dispose"],
471+
["thread.stop", "delegated_task.completion-delivery.dispose"],
471472
);
472473
}).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
473474
}),

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

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,43 +1662,60 @@ const make = Effect.gen(function* () {
16621662
),
16631663
),
16641664
);
1665-
// Published task results stay terminal. Later child-thread messages do not
1666-
// reopen the task, so cancelling it must not interrupt those separate runs.
1665+
// Cancelling stops the child thread the way Stop does, including work that came
1666+
// after a published result: follow-up runs, pull request watch wakes, and the
1667+
// tasks it delegated. The published result stays terminal.
1668+
const stopChild = Effect.gen(function* () {
1669+
const commandId = stableCommandId({ scope, requestKey: key, operation: "cancel-task" });
1670+
const reason = input.reason;
1671+
yield* threadManagement
1672+
.dispatch({
1673+
type: "thread.stop",
1674+
commandId,
1675+
threadId: current.childThreadId,
1676+
...(reason === undefined ? {} : { reason }),
1677+
})
1678+
.pipe(
1679+
Effect.mapError((error) =>
1680+
failure(
1681+
"task_not_cancellable",
1682+
`Unable to stop delegated task ${input.taskId}: ${errorMessage(error)}`,
1683+
),
1684+
),
1685+
);
1686+
// A retry with the same clientRequestId repeats only the stops that failed.
1687+
yield* threadManagement
1688+
.stopDelegatedTasks({ threadId: current.childThreadId, commandId, reason })
1689+
.pipe(
1690+
Effect.mapError((error) =>
1691+
failure(
1692+
"task_not_cancellable",
1693+
`Stopped delegated task ${input.taskId}, but not every task it delegated: ${errorMessage(error)}`,
1694+
),
1695+
),
1696+
);
1697+
});
16671698
if (isTerminalTaskStatus(current.status)) {
1699+
yield* stopChild;
16681700
yield* disposeCompletionDelivery;
16691701
return {
16701702
taskId: input.taskId,
16711703
status: current.status,
16721704
} satisfies OrchestratorMcpTaskCancelResult;
16731705
}
1706+
// A task waiting on its own delegates has work to stop below it. One with neither
1707+
// a running turn nor delegates (such as one awaiting a restart) keeps its delivery.
16741708
const child = yield* loadProjection(current.childThreadId);
1675-
const activeRun = ThreadManagementService.latestActiveRun(child);
1676-
if (activeRun === undefined) {
1709+
if (
1710+
current.workState !== "waiting_for_children" &&
1711+
ThreadManagementService.latestActiveRun(child) === undefined
1712+
) {
16771713
return yield* failure(
16781714
"task_not_cancellable",
16791715
`Delegated task ${input.taskId} has no interruptible child run.`,
16801716
);
16811717
}
1682-
yield* threadManagement
1683-
.dispatch({
1684-
type: "run.interrupt",
1685-
commandId: stableCommandId({
1686-
scope,
1687-
requestKey: key,
1688-
operation: "cancel-task",
1689-
}),
1690-
threadId: current.childThreadId,
1691-
runId: activeRun.id,
1692-
...(input.reason === undefined ? {} : { reason: input.reason }),
1693-
})
1694-
.pipe(
1695-
Effect.mapError((error) =>
1696-
failure(
1697-
"task_not_cancellable",
1698-
`Unable to interrupt delegated task ${input.taskId}: ${errorMessage(error)}`,
1699-
),
1700-
),
1701-
);
1718+
yield* stopChild;
17021719
yield* disposeCompletionDelivery.pipe(
17031720
Effect.catchCause((cause) =>
17041721
Effect.logWarning("orchestrator-mcp.cancel-task.delivery-dispose-failed", {

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ describe("orchestrator MCP toolkit", () => {
16631663
).toBe(true);
16641664
const completedTaskCancelCall = yield* invoke("task_cancel", {
16651665
taskId: delegated.taskId,
1666-
reason: "Must not interrupt a later unrelated child run.",
1666+
reason: "Stop the child's later work too.",
16671667
clientRequestId: "cancel-completed-delegated-task-1",
16681668
});
16691669
const completedTaskCancel = yield* decodeTaskCancelResult(
@@ -1681,36 +1681,19 @@ describe("orchestrator MCP toolkit", () => {
16811681
result: delegatedResult,
16821682
completionDelivery: { state: "disposed" },
16831683
});
1684-
expect(
1685-
(yield* orchestrator.getThreadProjection(delegated.childThreadId)).runs.find(
1686-
(run) => run.id === activeChildFollowup.runId,
1687-
)?.status,
1688-
).toBe("running");
1689-
const activeChildCleanupCall = yield* invoke("t3_thread_interrupt", {
1690-
threadId: delegated.childThreadId,
1691-
runId: activeChildFollowup.runId,
1692-
reason: "Clean up the active follow-up after verifying task cancellation isolation.",
1693-
clientRequestId: "interrupt-delegated-child-followup-1",
1694-
});
1695-
const activeChildCleanup = yield* decodeThreadInterruptResult(
1696-
activeChildCleanupCall.structuredContent,
1697-
).pipe(Effect.orDie);
1698-
expect(activeChildCleanup).toMatchObject({
1699-
runId: activeChildFollowup.runId,
1700-
status: "interrupt_requested",
1701-
});
1684+
// Cancelling a finished task still stops the child thread's later work.
17021685
yield* waitForProjection(orchestrator, delegated.childThreadId, (projection) =>
17031686
projection.runs.some(
17041687
(run) => run.id === activeChildFollowup.runId && run.status === "interrupted",
17051688
),
17061689
);
1707-
const delegatedStatusAfterCleanupCall = yield* invoke("task_status", {
1690+
const delegatedStatusAfterCancelCall = yield* invoke("task_status", {
17081691
taskId: delegated.taskId,
17091692
});
1710-
const delegatedStatusAfterCleanup = yield* decodeDelegateTaskResult(
1711-
delegatedStatusAfterCleanupCall.structuredContent,
1693+
const delegatedStatusAfterCancel = yield* decodeDelegateTaskResult(
1694+
delegatedStatusAfterCancelCall.structuredContent,
17121695
).pipe(Effect.orDie);
1713-
expect(delegatedStatusAfterCleanup).toMatchObject({
1696+
expect(delegatedStatusAfterCancel).toMatchObject({
17141697
childRunId: delegated.childRunId,
17151698
status: "completed",
17161699
summary: delegatedResult,

‎apps/server/src/mcp/toolkits/orchestrator/tools.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("orchestrator MCP tool guidance", () => {
2626
);
2727
assert.include(
2828
OrchestratorToolkit.tools.task_cancel.description ?? "",
29-
"without interrupting later child-thread runs",
29+
"This includes later child-thread runs, even after the task is terminal",
3030
);
3131
});
3232

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const DelegateTaskTool = Tool.make("delegate_task", {
7171

7272
const TaskStatusTool = Tool.make("task_status", {
7373
description:
74-
"Needs an agent running inside a T3 thread. Read a T3-owned delegated task created by this parent thread. childRunId identifies the original delegated run. workState distinguishes working, waiting_for_children, and result_available; a completed turn with live nested work is not a completed task. summary is the final task result, including provider errors on failure, and remains stable after publication. hasPendingChildRuns reports later queued or executing turns in the backing child thread, even after the task is terminal; it does not reopen the task or extend task_cancel to those turns. latestTerminal* provides later non-monitor turn results. Reading a terminal result acknowledges its automatic parent delivery.",
74+
"Needs an agent running inside a T3 thread. Read a T3-owned delegated task created by this parent thread. childRunId identifies the original delegated run. workState distinguishes working, waiting_for_children, and result_available; a completed turn with live nested work is not a completed task. summary is the final task result, including provider errors on failure, and remains stable after publication. hasPendingChildRuns reports later queued or executing turns in the backing child thread, even after the task is terminal; it does not reopen the task, and task_cancel stops those turns too. latestTerminal* provides later non-monitor turn results. Reading a terminal result acknowledges its automatic parent delivery.",
7575
parameters: OrchestratorMcpTaskStatusInput,
7676
success: OrchestratorMcpDelegateTaskResult,
7777
failure: OrchestratorMcpFailure,
@@ -85,7 +85,7 @@ const TaskStatusTool = Tool.make("task_status", {
8585

8686
const TaskCancelTool = Tool.make("task_cancel", {
8787
description:
88-
"Needs an agent running inside a T3 thread. Request interruption of an active T3-owned delegated task and dispose its automatic parent delivery. For a terminal task, return its existing status and dispose delivery without interrupting later child-thread runs, even when task_status reports hasPendingChildRuns=true. Published task results remain available. Use t3_thread_interrupt for a later active run.",
88+
"Needs an agent running inside a T3 thread. Stop a T3-owned delegated task and dispose its automatic parent delivery. Its child thread stops like a user Stop: the running turn is interrupted, queued turns are held, pull request watches end, and the tasks it delegated stop too. This includes later child-thread runs, even after the task is terminal. A terminal task returns its existing status, and published task results remain available.",
8989
parameters: OrchestratorMcpTaskCancelInput,
9090
success: OrchestratorMcpTaskCancelResult,
9191
failure: OrchestratorMcpFailure,

‎apps/server/src/mcp/toolkits/pullRequests/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
289289

290290
const WatchPullRequestTool = Tool.make("watch_pull_request", {
291291
description:
292-
"Have T3 Code watch an open pull request for this thread, linking it first if needed. T3 Code checks it every minute and wakes you with a message when a check fails, the required checks pass, someone else comments or reviews, or the branch starts to conflict with its base. Use this to monitor or babysit a pull request instead of polling, sleeping, or running a watcher. Only comments posted after this call wake you, so handle the existing ones first, then end your turn. A wake is news, not a merge decision: check readiness yourself before merging. Watching ends when the pull request merges or closes, when its thread settles, when T3 Code cannot read it for 15 minutes, or when you call unwatch_pull_request. Unsettle the thread before starting a new watch.",
292+
"Have T3 Code watch an open pull request for this thread, linking it first if needed. T3 Code checks it every minute and wakes you with a message when a check fails, the required checks pass, someone else comments or reviews, or the branch starts to conflict with its base. Use this to monitor or babysit a pull request instead of polling, sleeping, or running a watcher. Only comments posted after this call wake you, so handle the existing ones first, then end your turn. A wake is news, not a merge decision: check readiness yourself before merging. Watching ends when the pull request merges or closes, when its thread settles, when T3 Code cannot read it for 15 minutes, when the user stops this thread, or when you call unwatch_pull_request. Unsettle the thread before starting a new watch.",
293293
parameters: PullRequestTargetInput,
294294
success: WatchPullRequestResult,
295295
failure: PullRequestToolError,

0 commit comments

Comments
 (0)