Skip to content

Commit 3034dfb

Browse files
t3dotggclaude
andcommitted
fix(server): task_cancel clears an orphan whose child run finished
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0cb7599 commit 3034dfb

2 files changed

Lines changed: 125 additions & 113 deletions

File tree

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

Lines changed: 98 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -367,99 +367,107 @@ describe("OrchestratorMcpService", () => {
367367
}),
368368
);
369369

370-
it.effect("cancels an orphaned follow-up through its parent when the child is idle", () =>
371-
Effect.gen(function* () {
372-
const parentThreadId = ThreadId.make("thread:mcp-orphan-parent");
373-
const childThreadId = ThreadId.make("thread:mcp-orphan-child");
374-
const taskId = NodeId.make("node:mcp-orphan-task");
375-
const parentRunId = RunId.make("run:mcp-orphan-parent");
376-
const dispatched = yield* Ref.make<ReadonlyArray<OrchestrationV2ServerCommand>>([]);
377-
const status = yield* Ref.make("running");
378-
const parentProjection = (taskStatus: string) =>
379-
({
380-
thread: { id: parentThreadId },
381-
runs: [],
370+
it.effect.each(["no runs", "a finished run"] as const)(
371+
"cancels an orphaned follow-up through its parent when the child has %s",
372+
(childState) =>
373+
Effect.gen(function* () {
374+
const parentThreadId = ThreadId.make("thread:mcp-orphan-parent");
375+
const childThreadId = ThreadId.make("thread:mcp-orphan-child");
376+
const taskId = NodeId.make("node:mcp-orphan-task");
377+
const parentRunId = RunId.make("run:mcp-orphan-parent");
378+
const dispatched = yield* Ref.make<ReadonlyArray<OrchestrationV2ServerCommand>>([]);
379+
const status = yield* Ref.make("running");
380+
const parentProjection = (taskStatus: string) =>
381+
({
382+
thread: { id: parentThreadId },
383+
runs: [],
384+
contextTransfers: [],
385+
subagents: [
386+
{
387+
id: taskId,
388+
threadId: parentThreadId,
389+
runId: parentRunId,
390+
origin: "app_owned",
391+
childThreadId,
392+
driver: "codex",
393+
model: "gpt-5.6-terra",
394+
status: taskStatus,
395+
result: null,
396+
completionDelivery: { state: "pending" },
397+
},
398+
],
399+
}) as unknown as OrchestrationV2ThreadProjection;
400+
// A finished run whose binding to the follow-up was lost (#19002).
401+
const childProjection = {
402+
thread: { id: childThreadId },
403+
runs:
404+
childState === "no runs"
405+
? []
406+
: [{ id: RunId.make("run:mcp-orphan-child"), ordinal: 1, status: "completed" }],
382407
contextTransfers: [],
383-
subagents: [
384-
{
385-
id: taskId,
386-
threadId: parentThreadId,
387-
runId: parentRunId,
388-
origin: "app_owned",
389-
childThreadId,
390-
driver: "codex",
391-
model: "gpt-5.6-terra",
392-
status: taskStatus,
393-
result: null,
394-
completionDelivery: { state: "pending" },
395-
},
396-
],
397-
}) as unknown as OrchestrationV2ThreadProjection;
398-
const childProjection = {
399-
thread: { id: childThreadId },
400-
runs: [],
401-
contextTransfers: [],
402-
messages: [],
403-
subagents: [],
404-
providerThreads: [],
405-
runtimeRequests: [],
406-
} as unknown as OrchestrationV2ThreadProjection;
407-
const layerDependencies = Layer.mergeAll(
408-
NodeServices.layer,
409-
Layer.mock(ThreadManagementService.ThreadManagementService)({
410-
getThreadRecords: (threadId) =>
411-
threadId === parentThreadId
412-
? Effect.map(Ref.get(status), parentProjection)
413-
: Effect.succeed(childProjection),
414-
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
415-
// The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
416-
dispatch: (command) =>
417-
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
418-
Effect.andThen(
419-
command.type === "subagent.stop" ? Ref.set(status, "cancelled") : Effect.void,
408+
messages: [],
409+
subagents: [],
410+
providerThreads: [],
411+
runtimeRequests: [],
412+
turnItems: [],
413+
} as unknown as OrchestrationV2ThreadProjection;
414+
const layerDependencies = Layer.mergeAll(
415+
NodeServices.layer,
416+
Layer.mock(ThreadManagementService.ThreadManagementService)({
417+
getThreadRecords: (threadId) =>
418+
threadId === parentThreadId
419+
? Effect.map(Ref.get(status), parentProjection)
420+
: Effect.succeed(childProjection),
421+
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
422+
delegatedTaskResultPending: () => Effect.succeed(false),
423+
// The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
424+
dispatch: (command) =>
425+
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
426+
Effect.andThen(
427+
command.type === "subagent.stop" ? Ref.set(status, "cancelled") : Effect.void,
428+
),
429+
Effect.as({} as never),
420430
),
421-
Effect.as({} as never),
422-
),
423-
}),
424-
ServerSettings.layerTest(),
425-
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
426-
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
427-
list: () => Effect.succeed([]),
428-
}),
429-
Layer.mock(ProjectService.ProjectService)({}),
430-
Layer.mock(SecretRequests.SecretRequests)({}),
431-
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
432-
);
433-
const scope: McpInvocationScope = {
434-
environmentId: EnvironmentId.make("environment:mcp-orphan"),
435-
requestNamespace: "provider-session:mcp-orphan",
436-
thread: {
437-
threadId: parentThreadId,
438-
providerSessionId: "provider-session:mcp-orphan",
439-
providerInstanceId: ProviderInstanceId.make("codex"),
440-
},
441-
client: undefined,
442-
capabilities: new Set(["orchestration"]),
443-
issuedAt: 1,
444-
};
445-
446-
yield* Effect.gen(function* () {
447-
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
448-
const result = yield* service.cancelTask(scope, {
449-
taskId,
450-
clientRequestId: "cancel-orphan-task",
451-
});
452-
assert.deepEqual(result, { taskId, status: "cancelled" });
453-
const [stop, ...rest] = yield* Ref.get(dispatched);
454-
assert.isEmpty(rest);
455-
assert.equal(stop?.type, "subagent.stop");
456-
if (stop?.type !== "subagent.stop") return;
457-
assert.deepEqual(
458-
{ threadId: stop.threadId, runId: stop.runId, subagentId: stop.subagentId },
459-
{ threadId: parentThreadId, runId: parentRunId, subagentId: taskId },
431+
}),
432+
ServerSettings.layerTest(),
433+
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
434+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
435+
list: () => Effect.succeed([]),
436+
}),
437+
Layer.mock(ProjectService.ProjectService)({}),
438+
Layer.mock(SecretRequests.SecretRequests)({}),
439+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
460440
);
461-
}).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies))));
462-
}),
441+
const scope: McpInvocationScope = {
442+
environmentId: EnvironmentId.make("environment:mcp-orphan"),
443+
requestNamespace: "provider-session:mcp-orphan",
444+
thread: {
445+
threadId: parentThreadId,
446+
providerSessionId: "provider-session:mcp-orphan",
447+
providerInstanceId: ProviderInstanceId.make("codex"),
448+
},
449+
client: undefined,
450+
capabilities: new Set(["orchestration"]),
451+
issuedAt: 1,
452+
};
453+
454+
yield* Effect.gen(function* () {
455+
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
456+
const result = yield* service.cancelTask(scope, {
457+
taskId,
458+
clientRequestId: "cancel-orphan-task",
459+
});
460+
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");
464+
if (stop?.type !== "subagent.stop") return;
465+
assert.deepEqual(
466+
{ threadId: stop.threadId, runId: stop.runId, subagentId: stop.subagentId },
467+
{ threadId: parentThreadId, runId: parentRunId, subagentId: taskId },
468+
);
469+
}).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies))));
470+
}),
463471
);
464472

465473
it.effect("does not dispose delivery when the child stop fails", () =>

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
type ScheduledTaskUpsertInput,
6161
type ServerProvider,
6262
ThreadId,
63+
isOrchestrationV2WorkActive,
6364
} from "@t3tools/contracts";
6465
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
6566
import {
@@ -2357,7 +2358,32 @@ const make = Effect.gen(function* () {
23572358
),
23582359
),
23592360
);
2361+
// A follow-up that lost its child run (#19002) stays running on the parent, while its
2362+
// status here comes from the child's last run. The parent cancels it, and any other
2363+
// orphan of the same child, when that child is idle.
2364+
const cancelOrphan = Effect.gen(function* () {
2365+
if (parentTask?.runId == null || !isOrchestrationV2WorkActive(parentTask.status))
2366+
return false;
2367+
yield* threadManagement
2368+
.dispatch({
2369+
type: "subagent.stop",
2370+
commandId: stableCommandId({
2371+
scope,
2372+
requestKey: key,
2373+
operation: "cancel-task-orphan",
2374+
}),
2375+
threadId: scope.thread.threadId,
2376+
runId: parentTask.runId,
2377+
subagentId: parentTask.id,
2378+
})
2379+
.pipe(Effect.mapError(threadManagementFailure));
2380+
const { subagents } = yield* threadManagement
2381+
.getThreadRecords(scope.thread.threadId, ["subagents"])
2382+
.pipe(Effect.mapError(threadManagementFailure));
2383+
return subagents.some((task) => task.id === input.taskId && task.status === "cancelled");
2384+
});
23602385
if (isTerminalTaskStatus(current.status)) {
2386+
if (yield* cancelOrphan) return { taskId: input.taskId, status: "cancelled" };
23612387
yield* stopWithinLimit;
23622388
yield* disposeCompletionDelivery;
23632389
return {
@@ -2375,29 +2401,7 @@ const make = Effect.gen(function* () {
23752401
(run.delegatedTaskId === input.taskId || run.id === current.childRunId),
23762402
)
23772403
) {
2378-
// A follow-up that lost its child run (#19002) has nothing to interrupt. The parent
2379-
// cancels it, and any other orphan of the same child, when that child is idle.
2380-
if (parentTask?.runId != null) {
2381-
yield* threadManagement
2382-
.dispatch({
2383-
type: "subagent.stop",
2384-
commandId: stableCommandId({
2385-
scope,
2386-
requestKey: key,
2387-
operation: "cancel-task-orphan",
2388-
}),
2389-
threadId: scope.thread.threadId,
2390-
runId: parentTask.runId,
2391-
subagentId: parentTask.id,
2392-
})
2393-
.pipe(Effect.mapError(threadManagementFailure));
2394-
const { subagents } = yield* threadManagement
2395-
.getThreadRecords(scope.thread.threadId, ["subagents"])
2396-
.pipe(Effect.mapError(threadManagementFailure));
2397-
if (subagents.some((task) => task.id === input.taskId && task.status === "cancelled")) {
2398-
return { taskId: input.taskId, status: "cancelled" };
2399-
}
2400-
}
2404+
if (yield* cancelOrphan) return { taskId: input.taskId, status: "cancelled" };
24012405
return yield* failure(
24022406
"task_not_cancellable",
24032407
`Delegated task ${input.taskId} has no interruptible child run.`,

0 commit comments

Comments
 (0)