Skip to content

Commit 71646e0

Browse files
perf(server): cancelling a task reads only the tasks and runs it uses (#29652)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent bad610b commit 71646e0

8 files changed

Lines changed: 147 additions & 30 deletions

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

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import type { McpInvocationScope } from "./McpInvocationContext.ts";
6565
import { idleThreadProjection, liveThreadShell } from "./McpToolAccess.testkit.ts";
6666
import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
6767
import { resolveRuntimeMode } from "./delegatedTaskModes.ts";
68+
import type { ProjectionRecordFilter } from "../orchestration-v2/ProjectionStore.ts";
6869
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
6970

7071
const decodeTarget = Schema.decodeUnknownEffect(OrchestratorMcpTarget);
@@ -453,6 +454,7 @@ describe("OrchestratorMcpService", () => {
453454
getThreadRecords: (threadId) =>
454455
Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
455456
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
457+
listAppOwnedChildThreadIds: () => Effect.succeed([]),
456458
dispatch: (command) =>
457459
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
458460
Effect.as({} as never),
@@ -544,6 +546,7 @@ describe("OrchestratorMcpService", () => {
544546
? Effect.map(Ref.get(status), parentProjection)
545547
: Effect.succeed(childProjection),
546548
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
549+
listAppOwnedChildThreadIds: () => Effect.succeed([]),
547550
delegatedTaskResultPending: () => Effect.succeed(false),
548551
stopDelegatedTasks: () => Effect.void,
549552
// The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
@@ -642,6 +645,7 @@ describe("OrchestratorMcpService", () => {
642645
getThreadRecords: (threadId) =>
643646
Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
644647
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
648+
listAppOwnedChildThreadIds: () => Effect.succeed([]),
645649
dispatch: (command) =>
646650
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
647651
Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),
@@ -723,6 +727,7 @@ describe("OrchestratorMcpService", () => {
723727
getThreadRecords: (threadId) =>
724728
Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
725729
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
730+
listAppOwnedChildThreadIds: () => Effect.succeed([]),
726731
dispatch: (command) =>
727732
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
728733
Effect.andThen(
@@ -810,6 +815,7 @@ describe("OrchestratorMcpService", () => {
810815
getThreadRecords: (threadId) =>
811816
Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
812817
getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
818+
listAppOwnedChildThreadIds: () => Effect.succeed([]),
813819
dispatch: (command) =>
814820
Ref.update(dispatched, (commands) => [...commands, command]).pipe(
815821
Effect.andThen(
@@ -865,16 +871,25 @@ describe("OrchestratorMcpService", () => {
865871
const taskId = NodeId.make("node:mcp-cancel-grandchild-task");
866872
const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);
867873
const stoppedBelow = yield* Ref.make(false);
868-
const appOwnedTask = (id: string, threadId: ThreadId, childId: ThreadId) => ({
874+
const subagentReads = yield* Ref.make<ReadonlyArray<ProjectionRecordFilter | undefined>>([]);
875+
const appOwnedTask = (
876+
id: string,
877+
threadId: ThreadId,
878+
childId: ThreadId,
879+
settled?: "settled",
880+
) => ({
869881
id: NodeId.make(id),
870882
threadId,
871883
origin: "app_owned",
872884
childThreadId: childId,
873885
driver: "codex",
874886
model: "gpt-5.6-terra",
875-
result: null,
876-
completionDelivery: { state: "pending" },
887+
status: settled === undefined ? "running" : "completed",
888+
result: settled === undefined ? null : "Done.",
889+
completionDelivery: { state: settled === undefined ? "pending" : "delivered" },
877890
});
891+
// Older tasks the parent and child dispatched and that have long settled.
892+
const settledThreadId = ThreadId.make("thread:mcp-cancel-grandchild-settled");
878893
const projections = new Map([
879894
[
880895
parentThreadId,
@@ -886,7 +901,15 @@ describe("OrchestratorMcpService", () => {
886901
},
887902
runs: [],
888903
contextTransfers: [],
889-
subagents: [appOwnedTask(taskId, parentThreadId, childThreadId)],
904+
subagents: [
905+
appOwnedTask(
906+
"node:mcp-cancel-grandchild-old",
907+
parentThreadId,
908+
settledThreadId,
909+
"settled",
910+
),
911+
appOwnedTask(taskId, parentThreadId, childThreadId),
912+
],
890913
},
891914
],
892915
[
@@ -897,12 +920,30 @@ describe("OrchestratorMcpService", () => {
897920
contextTransfers: [],
898921
messages: [],
899922
subagents: [
923+
appOwnedTask(
924+
"node:mcp-cancel-grandchild-done",
925+
childThreadId,
926+
settledThreadId,
927+
"settled",
928+
),
900929
appOwnedTask("node:mcp-cancel-grandchild-below", childThreadId, grandchildThreadId),
901930
],
902931
providerThreads: [],
903932
runtimeRequests: [],
904933
},
905934
],
935+
[
936+
settledThreadId,
937+
{
938+
thread: { id: settledThreadId },
939+
runs: [],
940+
contextTransfers: [],
941+
messages: [],
942+
subagents: [],
943+
providerThreads: [],
944+
runtimeRequests: [],
945+
},
946+
],
906947
[
907948
grandchildThreadId,
908949
{
@@ -919,7 +960,21 @@ describe("OrchestratorMcpService", () => {
919960
const layerDependencies = Layer.mergeAll(
920961
NodeServices.layer,
921962
Layer.mock(ThreadManagementService.ThreadManagementService)({
922-
getThreadRecords: (threadId) => Effect.succeed(projections.get(threadId)!),
963+
getThreadRecords: (threadId, fields, filter) =>
964+
((fields as ReadonlyArray<string>).includes("subagents")
965+
? Ref.update(subagentReads, (reads) => [...reads, filter])
966+
: Effect.void
967+
).pipe(Effect.as(projections.get(threadId)!)),
968+
listAppOwnedChildThreadIds: (threadId) =>
969+
Effect.succeed(
970+
projections
971+
.get(threadId)!
972+
.subagents.flatMap((task) =>
973+
task.origin === "app_owned" && task.childThreadId !== null
974+
? [task.childThreadId]
975+
: [],
976+
),
977+
),
923978
getThreadShell: (threadId) =>
924979
Effect.succeed(
925980
threadId === grandchildThreadId && child.raisedDuringStop !== true
@@ -982,6 +1037,12 @@ describe("OrchestratorMcpService", () => {
9821037
const error = yield* service
9831038
.cancelTask(scope, { taskId, clientRequestId: "cancel-grandchild-above-modes" })
9841039
.pipe(Effect.flip);
1040+
// No read decodes every task a thread has dispatched.
1041+
for (const filter of yield* Ref.get(subagentReads)) {
1042+
assert.isTrue(
1043+
filter?.subagentIds !== undefined || filter?.omitSettledSubagents !== undefined,
1044+
);
1045+
}
9851046
return {
9861047
code: error.code,
9871048
dispatched: yield* Ref.get(dispatched),

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

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ import {
9292
DispatchModeLimit,
9393
type DispatchModeRefusal,
9494
} from "../orchestration-v2/DispatchModeLimit.ts";
95+
import type {
96+
ProjectionRecordField,
97+
ProjectionRecordFilter,
98+
} from "../orchestration-v2/ProjectionStore.ts";
9599
import { runOutcome } from "../orchestration-v2/RestartBackgroundNote.ts";
96100
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
97101
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
@@ -1047,21 +1051,13 @@ const make = Effect.gen(function* () {
10471051
),
10481052
);
10491053

1050-
const loadProjection = (threadId: ThreadId, includeUserMessages = true) =>
1054+
const loadThreadRecords = <K extends ProjectionRecordField>(
1055+
threadId: ThreadId,
1056+
fields: ReadonlyArray<K>,
1057+
filter?: ProjectionRecordFilter,
1058+
) =>
10511059
threadManagement
1052-
.getThreadRecords(
1053-
threadId,
1054-
[
1055-
"runs",
1056-
"messages",
1057-
"providerThreads",
1058-
"providerTurns",
1059-
"runtimeRequests",
1060-
"contextTransfers",
1061-
"turnItems",
1062-
],
1063-
{ turnItemTypes: [], messageRoles: includeUserMessages ? ["user"] : [] },
1064-
)
1060+
.getThreadRecords(threadId, fields, filter)
10651061
.pipe(
10661062
Effect.mapError((error) =>
10671063
failure(
@@ -1071,6 +1067,21 @@ const make = Effect.gen(function* () {
10711067
),
10721068
);
10731069

1070+
const loadProjection = (threadId: ThreadId, includeUserMessages = true) =>
1071+
loadThreadRecords(
1072+
threadId,
1073+
[
1074+
"runs",
1075+
"messages",
1076+
"providerThreads",
1077+
"providerTurns",
1078+
"runtimeRequests",
1079+
"contextTransfers",
1080+
"turnItems",
1081+
],
1082+
{ turnItemTypes: [], messageRoles: includeUserMessages ? ["user"] : [] },
1083+
);
1084+
10741085
// A dispatcher thread holds thousands of tasks, each with its full prompt; read only the ones named.
10751086
const loadTasks = (threadId: ThreadId, taskIds: ReadonlyArray<NodeId>) =>
10761087
threadManagement.getThreadRecords(threadId, ["subagents"], { subagentIds: taskIds }).pipe(
@@ -1491,7 +1502,8 @@ const make = Effect.gen(function* () {
14911502
"runtimeRequests",
14921503
"nodes",
14931504
],
1494-
{ messageRoles: ["user"], omitSettledLeafNodes: true },
1505+
// Progress reads only active or undelivered tasks under the child.
1506+
{ messageRoles: ["user"], omitSettledLeafNodes: true, omitSettledSubagents: true },
14951507
)
14961508
.pipe(Effect.mapError(threadManagementFailure));
14971509
const childRun = delegatedTaskRun(childControls, task);
@@ -2537,7 +2549,7 @@ const make = Effect.gen(function* () {
25372549
return yield* remoteDelegation.cancel(scope, remoteTask, input);
25382550
}
25392551
const key = yield* requestKey(input.clientRequestId);
2540-
const parentProjection = yield* loadProjection(scope.thread.threadId);
2552+
const parentProjection = yield* loadThreadRecords(scope.thread.threadId, []);
25412553
// Cancelling stops the child and every task under it, each a write to a
25422554
// thread its user may have raised above the parent's modes since it was
25432555
// delegated. All of them are checked before anything is stopped.
@@ -2554,13 +2566,11 @@ const make = Effect.gen(function* () {
25542566
shell.interactionMode,
25552567
);
25562568
}
2557-
const { subagents } = yield* threadManagement
2558-
.getThreadRecords(threadId, ["subagents"])
2569+
const childThreadIds = yield* threadManagement
2570+
.listAppOwnedChildThreadIds(threadId)
25592571
.pipe(Effect.mapError(threadManagementFailure));
2560-
for (const task of subagents) {
2561-
if (task.origin === "app_owned" && task.childThreadId !== null) {
2562-
yield* assertStoppable(task.childThreadId);
2563-
}
2572+
for (const childThreadId of childThreadIds) {
2573+
yield* assertStoppable(childThreadId);
25642574
}
25652575
});
25662576
yield* assertStoppable(childThreadId);
@@ -2701,7 +2711,7 @@ const make = Effect.gen(function* () {
27012711
status: cancelled ? "cancelled" : current.status,
27022712
} satisfies OrchestratorMcpTaskCancelResult;
27032713
}
2704-
const child = yield* loadProjection(childThreadId);
2714+
const child = yield* loadThreadRecords(childThreadId, ["runs"]);
27052715
if (
27062716
current.workState !== "waiting_for_children" &&
27072717
ThreadManagementService.latestActiveRun(child) === undefined &&

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ export interface OrchestratorV2Shape {
410410
fields: ReadonlyArray<K>,
411411
filter?: ProjectionRecordFilter,
412412
) => Effect.Effect<ProjectionRecords<K>, OrchestratorV2Error>;
413+
readonly listAppOwnedChildThreadIds: (
414+
threadId: ThreadId,
415+
) => Effect.Effect<ReadonlyArray<ThreadId>, OrchestratorV2Error>;
413416
readonly getThreadProjection: (
414417
threadId: ThreadId,
415418
) => Effect.Effect<OrchestrationV2ThreadProjection, OrchestratorV2Error>;
@@ -14046,6 +14049,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1404614049
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
1404714050
},
1404814051
),
14052+
listAppOwnedChildThreadIds: Effect.fn("OrchestratorV2.listAppOwnedChildThreadIds")(
14053+
function (threadId) {
14054+
return projectionStore
14055+
.listAppOwnedChildThreadIds(threadId)
14056+
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
14057+
},
14058+
),
1404914059
getThreadProjection: Effect.fn("OrchestratorV2.getThreadProjection")(function (threadId) {
1405014060
return projectionStore
1405114061
.getThreadProjection(threadId)

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,15 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
11821182
["running", settledRun, "running", null],
11831183
["active-run-delivered", activeRun, "completed", "delivered"],
11841184
] as const;
1185+
// A follow-up reuses its task's child thread.
1186+
const childThreadIds: Partial<Record<(typeof subagents)[number][0], ThreadId>> = {
1187+
delivered: ThreadId.make("thread:settled-subagents:child-a"),
1188+
running: ThreadId.make("thread:settled-subagents:child-a"),
1189+
"no-delivery": ThreadId.make("thread:settled-subagents:child-b"),
1190+
};
11851191
for (const [index, [name, run, status, delivery]] of subagents.entries()) {
11861192
const id = NodeId.make(`node:settled-subagents:${name}`);
1193+
const childThreadId = childThreadIds[name] ?? null;
11871194
yield* store.apply({
11881195
id: EventId.make(`event:settled-subagents:item:${name}`),
11891196
type: "turn-item.updated",
@@ -1212,7 +1219,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
12121219
origin: "app_owned",
12131220
driver,
12141221
providerInstanceId,
1215-
childThreadId: null,
1222+
childThreadId,
12161223
prompt: "Do the work",
12171224
result: null,
12181225
},
@@ -1236,7 +1243,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
12361243
driver,
12371244
providerInstanceId,
12381245
providerThreadId: null,
1239-
childThreadId: null,
1246+
childThreadId,
12401247
nativeTaskRef: null,
12411248
prompt: "Do the work",
12421249
title: name,
@@ -1286,6 +1293,10 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
12861293
["delivered"],
12871294
);
12881295
assert.strictEqual(yield* store.getSubagentCount(threadId), subagents.length);
1296+
assert.deepEqual(
1297+
[...(yield* store.listAppOwnedChildThreadIds(threadId))],
1298+
[childThreadIds.delivered, childThreadIds["no-delivery"]],
1299+
);
12891300
});
12901301

12911302
const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,10 @@ export interface ProjectionStoreV2Shape {
460460
) => Effect.Effect<ProjectionTimelinePage, ProjectionStoreV2Error>;
461461
readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
462462
readonly getSubagentCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
463+
/** Child threads of the thread's app-owned tasks, in task order, without decoding the tasks. */
464+
readonly listAppOwnedChildThreadIds: (
465+
threadId: ThreadId,
466+
) => Effect.Effect<ReadonlyArray<ThreadId>, ProjectionStoreV2Error>;
463467
readonly getNextTurnItemOrdinal: (
464468
threadId: ThreadId,
465469
) => Effect.Effect<number, ProjectionStoreV2Error>;
@@ -5388,6 +5392,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53885392
Effect.map((rows) => rows[0]?.count ?? 0),
53895393
Effect.mapError(controlReadError(threadId)),
53905394
);
5395+
const listAppOwnedChildThreadIds: ProjectionStoreV2Shape["listAppOwnedChildThreadIds"] = (
5396+
threadId,
5397+
) =>
5398+
sql<{ readonly child_thread_id: string }>`SELECT child_thread_id
5399+
FROM orchestration_v2_projection_subagents
5400+
WHERE thread_id = ${threadId} AND origin = 'app_owned' AND child_thread_id IS NOT NULL
5401+
ORDER BY COALESCE(started_at, ''), subagent_id ASC`.pipe(
5402+
Effect.map((rows) => [...new Set(rows.map((row) => ThreadId.make(row.child_thread_id)))]),
5403+
Effect.mapError(controlReadError(threadId)),
5404+
);
53915405
const getNextTurnItemOrdinal: ProjectionStoreV2Shape["getNextTurnItemOrdinal"] = (threadId) =>
53925406
sql<{ ordinal: number | null }>`SELECT MAX(ordinal) AS ordinal
53935407
FROM orchestration_v2_projection_turn_items WHERE thread_id = ${threadId}`.pipe(
@@ -7141,6 +7155,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
71417155
isNodeStored: readOnReader(isNodeStored),
71427156
getMessageCount: readOnReader(getMessageCount),
71437157
getSubagentCount: readOnReader(getSubagentCount),
7158+
listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),
71447159
getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),
71457160
getTurnItem: readOnReader(getTurnItem),
71467161
getThreadRecords: (threadId, fields, filter) =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ it.effect(
237237
getTimelinePage: () => Effect.die("Unused timeline read"),
238238
getMessageCount: () => Effect.die("unused message count"),
239239
getSubagentCount: () => Effect.die("unused subagent count"),
240+
listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"),
240241
getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"),
241242
getTurnItem: () => Effect.die("unused turn item read"),
242243
getThreadRecords: () => Effect.die("unused record read"),

0 commit comments

Comments
 (0)