Skip to content

Commit 849b3f8

Browse files
t3dotggsybeeriaclaude
authored
fix(server): idle orphaned follow-ups can be cancelled without a running parent (pingdotgg#22581)
Co-authored-by: syberia <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 569bd02 commit 849b3f8

7 files changed

Lines changed: 398 additions & 64 deletions

File tree

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ProviderInstanceId,
1515
RunId,
1616
ThreadId,
17+
type OrchestrationV2ServerCommand,
1718
type OrchestrationV2ThreadProjection,
1819
type ServerProvider,
1920
} from "@t3tools/contracts";
@@ -366,6 +367,109 @@ describe("OrchestratorMcpService", () => {
366367
}),
367368
);
368369

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" }],
407+
contextTransfers: [],
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),
430+
),
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)({}),
440+
);
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+
}),
471+
);
472+
369473
it.effect("does not dispose delivery when the child stop fails", () =>
370474
Effect.gen(function* () {
371475
const parentThreadId = ThreadId.make("thread:mcp-cancel-failed-parent");

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

Lines changed: 27 additions & 0 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,6 +2401,7 @@ const make = Effect.gen(function* () {
23752401
(run.delegatedTaskId === input.taskId || run.id === current.childRunId),
23762402
)
23772403
) {
2404+
if (yield* cancelOrphan) return { taskId: input.taskId, status: "cancelled" };
23782405
return yield* failure(
23792406
"task_not_cancellable",
23802407
`Delegated task ${input.taskId} has no interruptible child run.`,

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

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ export class OrchestratorProjectionError extends Schema.TaggedError<Orchestrator
190190
}
191191
}
192192

193+
const isOrchestratorProjectionError = Schema.is(OrchestratorProjectionError);
194+
193195
export class OrchestratorDomainEventStreamError extends Schema.TaggedError<OrchestratorDomainEventStreamError>()(
194196
"OrchestratorDomainEventStreamError",
195197
{
@@ -9395,6 +9397,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
93959397
]);
93969398
const run = projection.runs.find((candidate) => candidate.id === command.runId);
93979399
const subagent = projection.subagents.find((candidate) => candidate.id === command.subagentId);
9400+
// An app-owned task stops through its child thread. Here the parent only cancels the child's
9401+
// orphaned tasks: the child is idle and no child run is bound to them (#19002).
9402+
if (
9403+
subagent?.origin === "app_owned" &&
9404+
subagent.childThreadId !== null &&
9405+
subagent.runId === command.runId
9406+
) {
9407+
const records = yield* loadProjectionForCommand(command, ["subagents", "turnItems"], {
9408+
turnItemTypes: ["subagent"],
9409+
turnItemStatuses: ["pending", "running", "waiting"],
9410+
});
9411+
return yield* cancelIdleOrphans({
9412+
command,
9413+
events,
9414+
projection: records,
9415+
childThreadId: subagent.childThreadId,
9416+
});
9417+
}
93989418
if (
93999419
subagent === undefined ||
94009420
subagent.runId !== command.runId ||
@@ -9837,23 +9857,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
98379857
return undefined;
98389858
});
98399859

9840-
const dispatchThreadStop = Effect.fn("Orchestrator.dispatchThreadStop")(function* (
9841-
command: Extract<OrchestrationV2ServerCommand, { readonly type: "thread.stop" }>,
9842-
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
9843-
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
9844-
) {
9845-
const projection = yield* projectionStore
9846-
.getThreadRecords(command.threadId, ["runs", "providerThreads", "turnItems", "subagents"], {
9847-
turnItemTypes: ["command_execution", "dynamic_tool", "subagent"],
9848-
turnItemStatuses: ["pending", "running", "waiting"],
9849-
})
9850-
.pipe(
9851-
Effect.mapError(
9852-
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
9853-
),
9854-
);
9855-
if (projection.thread.deletedAt !== null) return undefined;
9856-
const cancelIdleOrphans = Effect.gen(function* () {
9860+
const cancelIdleOrphans = Effect.fn("Orchestrator.cancelIdleOrphans")(
9861+
function* (input: {
9862+
readonly command: Extract<
9863+
OrchestrationV2ServerCommand,
9864+
{ readonly type: "run.interrupt" | "thread.stop" | "subagent.stop" }
9865+
>;
9866+
readonly events: Ref.Ref<Array<OrchestrationV2DomainEvent>>;
9867+
readonly projection: Pick<OrchestrationV2ThreadProjection, "subagents" | "turnItems">;
9868+
/** Limits the repair to tasks delegated to this child thread. */
9869+
readonly childThreadId?: ThreadId;
9870+
}) {
9871+
const { command, events, projection } = input;
98579872
const pending = yield* Ref.get(events);
98589873
const children = new Map<
98599874
ThreadId,
@@ -9869,7 +9884,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
98699884
if (
98709885
task.origin !== "app_owned" ||
98719886
task.childThreadId === null ||
9872-
!isOrchestrationV2WorkActive(task.status)
9887+
!isOrchestrationV2WorkActive(task.status) ||
9888+
(input.childThreadId !== undefined && task.childThreadId !== input.childThreadId)
98739889
)
98749890
continue;
98759891
const childThreadId = task.childThreadId;
@@ -9951,13 +9967,33 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
99519967
});
99529968
}
99539969
}
9954-
}).pipe(
9955-
Effect.mapError((cause) =>
9956-
Schema.is(OrchestratorProjectionError)(cause)
9957-
? cause
9958-
: new OrchestratorProjectionError({ threadId: command.threadId, cause }),
9970+
},
9971+
(effect, input) =>
9972+
effect.pipe(
9973+
Effect.mapError((cause) =>
9974+
isOrchestratorProjectionError(cause)
9975+
? cause
9976+
: new OrchestratorProjectionError({ threadId: input.command.threadId, cause }),
9977+
),
99599978
),
9960-
);
9979+
);
9980+
9981+
const dispatchThreadStop = Effect.fn("Orchestrator.dispatchThreadStop")(function* (
9982+
command: Extract<OrchestrationV2ServerCommand, { readonly type: "thread.stop" }>,
9983+
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
9984+
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
9985+
) {
9986+
const projection = yield* projectionStore
9987+
.getThreadRecords(command.threadId, ["runs", "providerThreads", "turnItems", "subagents"], {
9988+
turnItemTypes: ["command_execution", "dynamic_tool", "subagent"],
9989+
turnItemStatuses: ["pending", "running", "waiting"],
9990+
})
9991+
.pipe(
9992+
Effect.mapError(
9993+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
9994+
),
9995+
);
9996+
if (projection.thread.deletedAt !== null) return undefined;
99619997
const latestRun = projection.runs.findLast((run) => run.status !== "queued");
99629998
const target =
99639999
projection.runs.findLast(
@@ -9996,7 +10032,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
999610032
const enqueued = yield* Ref.get(interruptEffects);
999710033
yield* Ref.update(events, (existing) => [...existing, ...written]);
999810034
yield* Ref.update(effects, (existing) => [...existing, ...enqueued]);
9999-
yield* cancelIdleOrphans;
10035+
yield* cancelIdleOrphans({ command, events, projection });
1000010036
return interrupted.value;
1000110037
}
1000210038
yield* Effect.logWarning("thread.stop could not interrupt the running turn", {
@@ -10016,7 +10052,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1001610052
yield* markStoppedRun({ command, events, thread: projection.thread, run, now });
1001710053
}
1001810054
yield* holdStoppedThread({ command, events, projection, cohortRunIds: [], now });
10019-
yield* cancelIdleOrphans;
10055+
yield* cancelIdleOrphans({ command, events, projection });
1002010056
return undefined;
1002110057
});
1002210058

@@ -11296,6 +11332,25 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1129611332
case "run.interrupt":
1129711333
cancelUnsettledEffects = yield* dispatchRunInterrupt(command, events, effects);
1129811334
if (command.holdQueue === true) {
11335+
// Stop must still commit when a child can't be read, so a failed repair's
11336+
// partial events are dropped.
11337+
const repairEvents = yield* Ref.make(yield* Ref.get(events));
11338+
const repaired = yield* Effect.exit(
11339+
loadProjectionForCommand(command, ["subagents", "turnItems"], {
11340+
turnItemTypes: ["subagent"],
11341+
turnItemStatuses: ["pending", "running", "waiting"],
11342+
}).pipe(
11343+
Effect.flatMap((projection) =>
11344+
cancelIdleOrphans({ command, events: repairEvents, projection }),
11345+
),
11346+
),
11347+
);
11348+
if (Exit.isSuccess(repaired)) yield* Ref.set(events, yield* Ref.get(repairEvents));
11349+
else
11350+
yield* Effect.logWarning("Stop could not cancel orphaned delegated follow-ups", {
11351+
threadId: command.threadId,
11352+
cause: repaired.cause,
11353+
});
1129911354
yield* Ref.update(effects, (existing) => [
1130011355
...existing,
1130111356
{

0 commit comments

Comments
 (0)