@devries-cameron20 / t3code Mirror · read-only / commits / 71646e031a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 07:00 UTC

perf(server): cancelling a task reads only the tasks and runs it uses (#29652)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 2, 2026 at 15:20 UTC 71646e031a6dc9db1309b3b9dff597f872cf2fd3
8 files changed +147 -30
apps/server/src/mcp/OrchestratorMcpService.test.ts
+66 -5
@@ -65,6 +65,7 @@ import type { McpInvocationScope } from "./McpInvocationContext.ts";
65 import { idleThreadProjection, liveThreadShell } from "./McpToolAccess.testkit.ts";
66 import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
67 import { resolveRuntimeMode } from "./delegatedTaskModes.ts";
68 +import type { ProjectionRecordFilter } from "../orchestration-v2/ProjectionStore.ts";
69 import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
70
71 const decodeTarget = Schema.decodeUnknownEffect(OrchestratorMcpTarget);
@@ -453,6 +454,7 @@ describe("OrchestratorMcpService", () => {
454 getThreadRecords: (threadId) =>
455 Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
456 getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
457 + listAppOwnedChildThreadIds: () => Effect.succeed([]),
458 dispatch: (command) =>
459 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
460 Effect.as({} as never),
@@ -544,6 +546,7 @@ describe("OrchestratorMcpService", () => {
546 ? Effect.map(Ref.get(status), parentProjection)
547 : Effect.succeed(childProjection),
548 getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
549 + listAppOwnedChildThreadIds: () => Effect.succeed([]),
550 delegatedTaskResultPending: () => Effect.succeed(false),
551 stopDelegatedTasks: () => Effect.void,
552 // The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.
@@ -642,6 +645,7 @@ describe("OrchestratorMcpService", () => {
645 getThreadRecords: (threadId) =>
646 Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
647 getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
648 + listAppOwnedChildThreadIds: () => Effect.succeed([]),
649 dispatch: (command) =>
650 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
651 Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),
@@ -723,6 +727,7 @@ describe("OrchestratorMcpService", () => {
727 getThreadRecords: (threadId) =>
728 Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
729 getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
730 + listAppOwnedChildThreadIds: () => Effect.succeed([]),
731 dispatch: (command) =>
732 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
733 Effect.andThen(
@@ -810,6 +815,7 @@ describe("OrchestratorMcpService", () => {
815 getThreadRecords: (threadId) =>
816 Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
817 getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
818 + listAppOwnedChildThreadIds: () => Effect.succeed([]),
819 dispatch: (command) =>
820 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
821 Effect.andThen(
@@ -865,16 +871,25 @@ describe("OrchestratorMcpService", () => {
871 const taskId = NodeId.make("node:mcp-cancel-grandchild-task");
872 const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);
873 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 + ) => ({
881 id: NodeId.make(id),
882 threadId,
883 origin: "app_owned",
884 childThreadId: childId,
885 driver: "codex",
886 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" },
890 });
891 + // Older tasks the parent and child dispatched and that have long settled.
892 + const settledThreadId = ThreadId.make("thread:mcp-cancel-grandchild-settled");
893 const projections = new Map([
894 [
895 parentThreadId,
@@ -886,7 +901,15 @@ describe("OrchestratorMcpService", () => {
901 },
902 runs: [],
903 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 + ],
913 },
914 ],
915 [
@@ -897,12 +920,30 @@ describe("OrchestratorMcpService", () => {
920 contextTransfers: [],
921 messages: [],
922 subagents: [
923 + appOwnedTask(
924 + "node:mcp-cancel-grandchild-done",
925 + childThreadId,
926 + settledThreadId,
927 + "settled",
928 + ),
929 appOwnedTask("node:mcp-cancel-grandchild-below", childThreadId, grandchildThreadId),
930 ],
931 providerThreads: [],
932 runtimeRequests: [],
933 },
934 ],
935 + [
936 + settledThreadId,
937 + {
938 + thread: { id: settledThreadId },
939 + runs: [],
940 + contextTransfers: [],
941 + messages: [],
942 + subagents: [],
943 + providerThreads: [],
944 + runtimeRequests: [],
945 + },
946 + ],
947 [
948 grandchildThreadId,
949 {
@@ -919,7 +960,21 @@ describe("OrchestratorMcpService", () => {
960 const layerDependencies = Layer.mergeAll(
961 NodeServices.layer,
962 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 + ),
978 getThreadShell: (threadId) =>
979 Effect.succeed(
980 threadId === grandchildThreadId && child.raisedDuringStop !== true
@@ -982,6 +1037,12 @@ describe("OrchestratorMcpService", () => {
1037 const error = yield* service
1038 .cancelTask(scope, { taskId, clientRequestId: "cancel-grandchild-above-modes" })
1039 .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 + }
1046 return {
1047 code: error.code,
1048 dispatched: yield* Ref.get(dispatched),
apps/server/src/mcp/OrchestratorMcpService.ts
+33 -23
@@ -92,6 +92,10 @@ import {
92 DispatchModeLimit,
93 type DispatchModeRefusal,
94 } from "../orchestration-v2/DispatchModeLimit.ts";
95 +import type {
96 + ProjectionRecordField,
97 + ProjectionRecordFilter,
98 +} from "../orchestration-v2/ProjectionStore.ts";
99 import { runOutcome } from "../orchestration-v2/RestartBackgroundNote.ts";
100 import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
101 import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
@@ -1047,21 +1051,13 @@ const make = Effect.gen(function* () {
1051 ),
1052 );
1053
1050 - const loadProjection = (threadId: ThreadId, includeUserMessages = true) =>
1054 + const loadThreadRecords = <K extends ProjectionRecordField>(
1055 + threadId: ThreadId,
1056 + fields: ReadonlyArray<K>,
1057 + filter?: ProjectionRecordFilter,
1058 + ) =>
1059 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)
1061 .pipe(
1062 Effect.mapError((error) =>
1063 failure(
@@ -1071,6 +1067,21 @@ const make = Effect.gen(function* () {
1067 ),
1068 );
1069
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 +
1085 // A dispatcher thread holds thousands of tasks, each with its full prompt; read only the ones named.
1086 const loadTasks = (threadId: ThreadId, taskIds: ReadonlyArray<NodeId>) =>
1087 threadManagement.getThreadRecords(threadId, ["subagents"], { subagentIds: taskIds }).pipe(
@@ -1491,7 +1502,8 @@ const make = Effect.gen(function* () {
1502 "runtimeRequests",
1503 "nodes",
1504 ],
1494 - { messageRoles: ["user"], omitSettledLeafNodes: true },
1505 + // Progress reads only active or undelivered tasks under the child.
1506 + { messageRoles: ["user"], omitSettledLeafNodes: true, omitSettledSubagents: true },
1507 )
1508 .pipe(Effect.mapError(threadManagementFailure));
1509 const childRun = delegatedTaskRun(childControls, task);
@@ -2537,7 +2549,7 @@ const make = Effect.gen(function* () {
2549 return yield* remoteDelegation.cancel(scope, remoteTask, input);
2550 }
2551 const key = yield* requestKey(input.clientRequestId);
2540 - const parentProjection = yield* loadProjection(scope.thread.threadId);
2552 + const parentProjection = yield* loadThreadRecords(scope.thread.threadId, []);
2553 // Cancelling stops the child and every task under it, each a write to a
2554 // thread its user may have raised above the parent's modes since it was
2555 // delegated. All of them are checked before anything is stopped.
@@ -2554,13 +2566,11 @@ const make = Effect.gen(function* () {
2566 shell.interactionMode,
2567 );
2568 }
2557 - const { subagents } = yield* threadManagement
2558 - .getThreadRecords(threadId, ["subagents"])
2569 + const childThreadIds = yield* threadManagement
2570 + .listAppOwnedChildThreadIds(threadId)
2571 .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);
2574 }
2575 });
2576 yield* assertStoppable(childThreadId);
@@ -2701,7 +2711,7 @@ const make = Effect.gen(function* () {
2711 status: cancelled ? "cancelled" : current.status,
2712 } satisfies OrchestratorMcpTaskCancelResult;
2713 }
2704 - const child = yield* loadProjection(childThreadId);
2714 + const child = yield* loadThreadRecords(childThreadId, ["runs"]);
2715 if (
2716 current.workState !== "waiting_for_children" &&
2717 ThreadManagementService.latestActiveRun(child) === undefined &&
apps/server/src/orchestration-v2/Orchestrator.ts
+10
@@ -410,6 +410,9 @@ export interface OrchestratorV2Shape {
410 fields: ReadonlyArray<K>,
411 filter?: ProjectionRecordFilter,
412 ) => Effect.Effect<ProjectionRecords<K>, OrchestratorV2Error>;
413 + readonly listAppOwnedChildThreadIds: (
414 + threadId: ThreadId,
415 + ) => Effect.Effect<ReadonlyArray<ThreadId>, OrchestratorV2Error>;
416 readonly getThreadProjection: (
417 threadId: ThreadId,
418 ) => Effect.Effect<OrchestrationV2ThreadProjection, OrchestratorV2Error>;
@@ -14046,6 +14049,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14049 .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
14050 },
14051 ),
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 + ),
14059 getThreadProjection: Effect.fn("OrchestratorV2.getThreadProjection")(function (threadId) {
14060 return projectionStore
14061 .getThreadProjection(threadId)
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+13 -2
@@ -1182,8 +1182,15 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1182 ["running", settledRun, "running", null],
1183 ["active-run-delivered", activeRun, "completed", "delivered"],
1184 ] 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 + };
1191 for (const [index, [name, run, status, delivery]] of subagents.entries()) {
1192 const id = NodeId.make(`node:settled-subagents:${name}`);
1193 + const childThreadId = childThreadIds[name] ?? null;
1194 yield* store.apply({
1195 id: EventId.make(`event:settled-subagents:item:${name}`),
1196 type: "turn-item.updated",
@@ -1212,7 +1219,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1219 origin: "app_owned",
1220 driver,
1221 providerInstanceId,
1215 - childThreadId: null,
1222 + childThreadId,
1223 prompt: "Do the work",
1224 result: null,
1225 },
@@ -1236,7 +1243,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1243 driver,
1244 providerInstanceId,
1245 providerThreadId: null,
1239 - childThreadId: null,
1246 + childThreadId,
1247 nativeTaskRef: null,
1248 prompt: "Do the work",
1249 title: name,
@@ -1286,6 +1293,10 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
1293 ["delivered"],
1294 );
1295 assert.strictEqual(yield* store.getSubagentCount(threadId), subagents.length);
1296 + assert.deepEqual(
1297 + [...(yield* store.listAppOwnedChildThreadIds(threadId))],
1298 + [childThreadIds.delivered, childThreadIds["no-delivery"]],
1299 + );
1300 });
1301
1302 const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () {
apps/server/src/orchestration-v2/ProjectionStore.ts
+15
@@ -460,6 +460,10 @@ export interface ProjectionStoreV2Shape {
460 ) => Effect.Effect<ProjectionTimelinePage, ProjectionStoreV2Error>;
461 readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
462 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>;
467 readonly getNextTurnItemOrdinal: (
468 threadId: ThreadId,
469 ) => Effect.Effect<number, ProjectionStoreV2Error>;
@@ -5388,6 +5392,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5392 Effect.map((rows) => rows[0]?.count ?? 0),
5393 Effect.mapError(controlReadError(threadId)),
5394 );
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 + );
5405 const getNextTurnItemOrdinal: ProjectionStoreV2Shape["getNextTurnItemOrdinal"] = (threadId) =>
5406 sql<{ ordinal: number | null }>`SELECT MAX(ordinal) AS ordinal
5407 FROM orchestration_v2_projection_turn_items WHERE thread_id = ${threadId}`.pipe(
@@ -7141,6 +7155,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7155 isNodeStored: readOnReader(isNodeStored),
7156 getMessageCount: readOnReader(getMessageCount),
7157 getSubagentCount: readOnReader(getSubagentCount),
7158 + listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),
7159 getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),
7160 getTurnItem: readOnReader(getTurnItem),
7161 getThreadRecords: (threadId, fields, filter) =>
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+1
@@ -237,6 +237,7 @@ it.effect(
237 getTimelinePage: () => Effect.die("Unused timeline read"),
238 getMessageCount: () => Effect.die("unused message count"),
239 getSubagentCount: () => Effect.die("unused subagent count"),
240 + listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"),
241 getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"),
242 getTurnItem: () => Effect.die("unused turn item read"),
243 getThreadRecords: () => Effect.die("unused record read"),
apps/server/src/orchestration-v2/ThreadManagementService.ts
+8
@@ -419,6 +419,7 @@ export interface ThreadManagementServiceShape {
419 Orchestrator.OrchestratorV2Error
420 >;
421 readonly getThreadShell: Orchestrator.OrchestratorV2["Service"]["getThreadShell"];
422 + readonly listAppOwnedChildThreadIds: Orchestrator.OrchestratorV2["Service"]["listAppOwnedChildThreadIds"];
423 /** Omit `projectId` to list every project's threads. Side chats are never listed. */
424 readonly listProjectThreads: (input: {
425 readonly projectId?: ProjectId | undefined;
@@ -1526,6 +1527,13 @@ const make = Effect.gen(function* () {
1527 listOlderSettledThreads: orchestrator.listOlderSettledThreads,
1528 readShellSnapshot: orchestrator.readShellSnapshot,
1529 getThreadShell: orchestrator.getThreadShell,
1530 + listAppOwnedChildThreadIds: Effect.fn("ThreadManagementService.listAppOwnedChildThreadIds")(
1531 + function (threadId) {
1532 + return ensureProjectionTranscript(threadId).pipe(
1533 + Effect.andThen(orchestrator.listAppOwnedChildThreadIds(threadId)),
1534 + );
1535 + },
1536 + ),
1537 listProjectThreads,
1538 sendToThread,
1539 assertProviderInstanceEnabled,
apps/server/src/relay/AgentAwarenessRelay.test.ts
+1
@@ -190,6 +190,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
190 return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
191 }),
192 listActiveSubagentThreadIds: unused,
193 + listAppOwnedChildThreadIds: unused,
194 countThreads: unused,
195 listActiveShellIndex: () => Effect.succeed([]),
196 listOlderSettledThreads: unused,