Skip to content

Commit 5a7a3b5

Browse files
perf(server): cancelling a task reads only the tasks and runs it uses
cancelTask decoded every task a thread had dispatched, with full prompts, for the child and every app-owned thread under it, and read the parent's and child's runs, provider threads/turns, runtime requests and context transfers when it used only the parent's modes and the child's runs. The mode check now walks child thread ids from a column-only store read, the parent read takes only the thread row, the child read only its runs, and the task status read skips settled, delivered tasks under the child. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 065a433 commit 5a7a3b5

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>;
@@ -14043,6 +14046,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1404314046
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
1404414047
},
1404514048
),
14049+
listAppOwnedChildThreadIds: Effect.fn("OrchestratorV2.listAppOwnedChildThreadIds")(
14050+
function (threadId) {
14051+
return projectionStore
14052+
.listAppOwnedChildThreadIds(threadId)
14053+
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
14054+
},
14055+
),
1404614056
getThreadProjection: Effect.fn("OrchestratorV2.getThreadProjection")(function (threadId) {
1404714057
return projectionStore
1404814058
.getThreadProjection(threadId)

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,15 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
10181018
["running", settledRun, "running", null],
10191019
["active-run-delivered", activeRun, "completed", "delivered"],
10201020
] as const;
1021+
// A follow-up reuses its task's child thread.
1022+
const childThreadIds: Partial<Record<(typeof subagents)[number][0], ThreadId>> = {
1023+
delivered: ThreadId.make("thread:settled-subagents:child-a"),
1024+
running: ThreadId.make("thread:settled-subagents:child-a"),
1025+
"no-delivery": ThreadId.make("thread:settled-subagents:child-b"),
1026+
};
10211027
for (const [index, [name, run, status, delivery]] of subagents.entries()) {
10221028
const id = NodeId.make(`node:settled-subagents:${name}`);
1029+
const childThreadId = childThreadIds[name] ?? null;
10231030
yield* store.apply({
10241031
id: EventId.make(`event:settled-subagents:item:${name}`),
10251032
type: "turn-item.updated",
@@ -1048,7 +1055,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
10481055
origin: "app_owned",
10491056
driver,
10501057
providerInstanceId,
1051-
childThreadId: null,
1058+
childThreadId,
10521059
prompt: "Do the work",
10531060
result: null,
10541061
},
@@ -1072,7 +1079,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
10721079
driver,
10731080
providerInstanceId,
10741081
providerThreadId: null,
1075-
childThreadId: null,
1082+
childThreadId,
10761083
nativeTaskRef: null,
10771084
prompt: "Do the work",
10781085
title: name,
@@ -1122,6 +1129,10 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
11221129
["delivered"],
11231130
);
11241131
assert.strictEqual(yield* store.getSubagentCount(threadId), subagents.length);
1132+
assert.deepEqual(
1133+
[...(yield* store.listAppOwnedChildThreadIds(threadId))],
1134+
[childThreadIds.delivered, childThreadIds["no-delivery"]],
1135+
);
11251136
});
11261137

11271138
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
@@ -450,6 +450,10 @@ export interface ProjectionStoreV2Shape {
450450
) => Effect.Effect<ProjectionTimelinePage, ProjectionStoreV2Error>;
451451
readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
452452
readonly getSubagentCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
453+
/** Child threads of the thread's app-owned tasks, in task order, without decoding the tasks. */
454+
readonly listAppOwnedChildThreadIds: (
455+
threadId: ThreadId,
456+
) => Effect.Effect<ReadonlyArray<ThreadId>, ProjectionStoreV2Error>;
453457
readonly getNextTurnItemOrdinal: (
454458
threadId: ThreadId,
455459
) => Effect.Effect<number, ProjectionStoreV2Error>;
@@ -5351,6 +5355,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53515355
Effect.map((rows) => rows[0]?.count ?? 0),
53525356
Effect.mapError(controlReadError(threadId)),
53535357
);
5358+
const listAppOwnedChildThreadIds: ProjectionStoreV2Shape["listAppOwnedChildThreadIds"] = (
5359+
threadId,
5360+
) =>
5361+
sql<{ readonly child_thread_id: string }>`SELECT child_thread_id
5362+
FROM orchestration_v2_projection_subagents
5363+
WHERE thread_id = ${threadId} AND origin = 'app_owned' AND child_thread_id IS NOT NULL
5364+
ORDER BY COALESCE(started_at, ''), subagent_id ASC`.pipe(
5365+
Effect.map((rows) => [...new Set(rows.map((row) => ThreadId.make(row.child_thread_id)))]),
5366+
Effect.mapError(controlReadError(threadId)),
5367+
);
53545368
const getNextTurnItemOrdinal: ProjectionStoreV2Shape["getNextTurnItemOrdinal"] = (threadId) =>
53555369
sql<{ ordinal: number | null }>`SELECT MAX(ordinal) AS ordinal
53565370
FROM orchestration_v2_projection_turn_items WHERE thread_id = ${threadId}`.pipe(
@@ -7104,6 +7118,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
71047118
isNodeStored: readOnReader(isNodeStored),
71057119
getMessageCount: readOnReader(getMessageCount),
71067120
getSubagentCount: readOnReader(getSubagentCount),
7121+
listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),
71077122
getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),
71087123
getTurnItem: readOnReader(getTurnItem),
71097124
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)