TypeScript
perf(server): cancelling a task reads only the tasks and runs it uses (#29652)
Julius Marminge71646e031a6d
diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.tsindex 47a6bda166c..d463b066b7e 100644--- a/apps/server/src/mcp/OrchestratorMcpService.test.ts+++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts@@ -65,6 +65,7 @@ import type { McpInvocationScope } from "./McpInvocationContext.ts";import { idleThreadProjection, liveThreadShell } from "./McpToolAccess.testkit.ts";import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";import { resolveRuntimeMode } from "./delegatedTaskModes.ts";+import type { ProjectionRecordFilter } from "../orchestration-v2/ProjectionStore.ts";import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";const decodeTarget = Schema.decodeUnknownEffect(OrchestratorMcpTarget);@@ -453,6 +454,7 @@ describe("OrchestratorMcpService", () => {getThreadRecords: (threadId) =>Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),+ listAppOwnedChildThreadIds: () => Effect.succeed([]),dispatch: (command) =>Ref.update(dispatched, (commands) => [...commands, command]).pipe(Effect.as({} as never),@@ -544,6 +546,7 @@ describe("OrchestratorMcpService", () => {? Effect.map(Ref.get(status), parentProjection): Effect.succeed(childProjection),getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),+ listAppOwnedChildThreadIds: () => Effect.succeed([]),delegatedTaskResultPending: () => Effect.succeed(false),stopDelegatedTasks: () => Effect.void,// The orchestrator's subagent.stop repair is covered in ThreadStop.test.ts.@@ -642,6 +645,7 @@ describe("OrchestratorMcpService", () => {getThreadRecords: (threadId) =>Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),+ listAppOwnedChildThreadIds: () => Effect.succeed([]),dispatch: (command) =>Ref.update(dispatched, (commands) => [...commands, command]).pipe(Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),@@ -723,6 +727,7 @@ describe("OrchestratorMcpService", () => {getThreadRecords: (threadId) =>Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),+ listAppOwnedChildThreadIds: () => Effect.succeed([]),dispatch: (command) =>Ref.update(dispatched, (commands) => [...commands, command]).pipe(Effect.andThen(@@ -810,6 +815,7 @@ describe("OrchestratorMcpService", () => {getThreadRecords: (threadId) =>Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),+ listAppOwnedChildThreadIds: () => Effect.succeed([]),dispatch: (command) =>Ref.update(dispatched, (commands) => [...commands, command]).pipe(Effect.andThen(@@ -865,16 +871,25 @@ describe("OrchestratorMcpService", () => {const taskId = NodeId.make("node:mcp-cancel-grandchild-task");const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);const stoppedBelow = yield* Ref.make(false);- const appOwnedTask = (id: string, threadId: ThreadId, childId: ThreadId) => ({+ const subagentReads = yield* Ref.make<ReadonlyArray<ProjectionRecordFilter | undefined>>([]);+ const appOwnedTask = (+ id: string,+ threadId: ThreadId,+ childId: ThreadId,+ settled?: "settled",+ ) => ({id: NodeId.make(id),threadId,origin: "app_owned",childThreadId: childId,driver: "codex",model: "gpt-5.6-terra",- result: null,- completionDelivery: { state: "pending" },+ status: settled === undefined ? "running" : "completed",+ result: settled === undefined ? null : "Done.",+ completionDelivery: { state: settled === undefined ? "pending" : "delivered" },});+ // Older tasks the parent and child dispatched and that have long settled.+ const settledThreadId = ThreadId.make("thread:mcp-cancel-grandchild-settled");const projections = new Map([[parentThreadId,@@ -886,7 +901,15 @@ describe("OrchestratorMcpService", () => {},runs: [],contextTransfers: [],- subagents: [appOwnedTask(taskId, parentThreadId, childThreadId)],+ subagents: [+ appOwnedTask(+ "node:mcp-cancel-grandchild-old",+ parentThreadId,+ settledThreadId,+ "settled",+ ),+ appOwnedTask(taskId, parentThreadId, childThreadId),+ ],},],[@@ -897,12 +920,30 @@ describe("OrchestratorMcpService", () => {contextTransfers: [],messages: [],subagents: [+ appOwnedTask(+ "node:mcp-cancel-grandchild-done",+ childThreadId,+ settledThreadId,+ "settled",+ ),appOwnedTask("node:mcp-cancel-grandchild-below", childThreadId, grandchildThreadId),],providerThreads: [],runtimeRequests: [],},],+ [+ settledThreadId,+ {+ thread: { id: settledThreadId },+ runs: [],+ contextTransfers: [],+ messages: [],+ subagents: [],+ providerThreads: [],+ runtimeRequests: [],+ },+ ],[grandchildThreadId,{@@ -919,7 +960,21 @@ describe("OrchestratorMcpService", () => {const layerDependencies = Layer.mergeAll(NodeServices.layer,Layer.mock(ThreadManagementService.ThreadManagementService)({- getThreadRecords: (threadId) => Effect.succeed(projections.get(threadId)!),+ getThreadRecords: (threadId, fields, filter) =>+ ((fields as ReadonlyArray<string>).includes("subagents")+ ? Ref.update(subagentReads, (reads) => [...reads, filter])+ : Effect.void+ ).pipe(Effect.as(projections.get(threadId)!)),+ listAppOwnedChildThreadIds: (threadId) =>+ Effect.succeed(+ projections+ .get(threadId)!+ .subagents.flatMap((task) =>+ task.origin === "app_owned" && task.childThreadId !== null+ ? [task.childThreadId]+ : [],+ ),+ ),getThreadShell: (threadId) =>Effect.succeed(threadId === grandchildThreadId && child.raisedDuringStop !== true@@ -982,6 +1037,12 @@ describe("OrchestratorMcpService", () => {const error = yield* service.cancelTask(scope, { taskId, clientRequestId: "cancel-grandchild-above-modes" }).pipe(Effect.flip);+ // No read decodes every task a thread has dispatched.+ for (const filter of yield* Ref.get(subagentReads)) {+ assert.isTrue(+ filter?.subagentIds !== undefined || filter?.omitSettledSubagents !== undefined,+ );+ }return {code: error.code,dispatched: yield* Ref.get(dispatched),diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.tsindex 910a1531aed..0d59dfd4940 100644--- a/apps/server/src/mcp/OrchestratorMcpService.ts+++ b/apps/server/src/mcp/OrchestratorMcpService.ts@@ -92,6 +92,10 @@ import {DispatchModeLimit,type DispatchModeRefusal,} from "../orchestration-v2/DispatchModeLimit.ts";+import type {+ ProjectionRecordField,+ ProjectionRecordFilter,+} from "../orchestration-v2/ProjectionStore.ts";import { runOutcome } from "../orchestration-v2/RestartBackgroundNote.ts";import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";@@ -1047,21 +1051,13 @@ const make = Effect.gen(function* () {),);- const loadProjection = (threadId: ThreadId, includeUserMessages = true) =>+ const loadThreadRecords = <K extends ProjectionRecordField>(+ threadId: ThreadId,+ fields: ReadonlyArray<K>,+ filter?: ProjectionRecordFilter,+ ) =>threadManagement- .getThreadRecords(- threadId,- [- "runs",- "messages",- "providerThreads",- "providerTurns",- "runtimeRequests",- "contextTransfers",- "turnItems",- ],- { turnItemTypes: [], messageRoles: includeUserMessages ? ["user"] : [] },- )+ .getThreadRecords(threadId, fields, filter).pipe(Effect.mapError((error) =>failure(@@ -1071,6 +1067,21 @@ const make = Effect.gen(function* () {),);+ const loadProjection = (threadId: ThreadId, includeUserMessages = true) =>+ loadThreadRecords(+ threadId,+ [+ "runs",+ "messages",+ "providerThreads",+ "providerTurns",+ "runtimeRequests",+ "contextTransfers",+ "turnItems",+ ],+ { turnItemTypes: [], messageRoles: includeUserMessages ? ["user"] : [] },+ );+// A dispatcher thread holds thousands of tasks, each with its full prompt; read only the ones named.const loadTasks = (threadId: ThreadId, taskIds: ReadonlyArray<NodeId>) =>threadManagement.getThreadRecords(threadId, ["subagents"], { subagentIds: taskIds }).pipe(@@ -1491,7 +1502,8 @@ const make = Effect.gen(function* () {"runtimeRequests","nodes",],- { messageRoles: ["user"], omitSettledLeafNodes: true },+ // Progress reads only active or undelivered tasks under the child.+ { messageRoles: ["user"], omitSettledLeafNodes: true, omitSettledSubagents: true },).pipe(Effect.mapError(threadManagementFailure));const childRun = delegatedTaskRun(childControls, task);@@ -2537,7 +2549,7 @@ const make = Effect.gen(function* () {return yield* remoteDelegation.cancel(scope, remoteTask, input);}const key = yield* requestKey(input.clientRequestId);- const parentProjection = yield* loadProjection(scope.thread.threadId);+ const parentProjection = yield* loadThreadRecords(scope.thread.threadId, []);// Cancelling stops the child and every task under it, each a write to a// thread its user may have raised above the parent's modes since it was// delegated. All of them are checked before anything is stopped.@@ -2554,13 +2566,11 @@ const make = Effect.gen(function* () {shell.interactionMode,);}- const { subagents } = yield* threadManagement- .getThreadRecords(threadId, ["subagents"])+ const childThreadIds = yield* threadManagement+ .listAppOwnedChildThreadIds(threadId).pipe(Effect.mapError(threadManagementFailure));- for (const task of subagents) {- if (task.origin === "app_owned" && task.childThreadId !== null) {- yield* assertStoppable(task.childThreadId);- }+ for (const childThreadId of childThreadIds) {+ yield* assertStoppable(childThreadId);}});yield* assertStoppable(childThreadId);@@ -2701,7 +2711,7 @@ const make = Effect.gen(function* () {status: cancelled ? "cancelled" : current.status,} satisfies OrchestratorMcpTaskCancelResult;}- const child = yield* loadProjection(childThreadId);+ const child = yield* loadThreadRecords(childThreadId, ["runs"]);if (current.workState !== "waiting_for_children" &&ThreadManagementService.latestActiveRun(child) === undefined &&diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 2d57df568a4..035bd9c73db 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -410,6 +410,9 @@ export interface OrchestratorV2Shape {fields: ReadonlyArray<K>,filter?: ProjectionRecordFilter,) => Effect.Effect<ProjectionRecords<K>, OrchestratorV2Error>;+ readonly listAppOwnedChildThreadIds: (+ threadId: ThreadId,+ ) => Effect.Effect<ReadonlyArray<ThreadId>, OrchestratorV2Error>;readonly getThreadProjection: (threadId: ThreadId,) => Effect.Effect<OrchestrationV2ThreadProjection, OrchestratorV2Error>;@@ -14046,6 +14049,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));},),+ listAppOwnedChildThreadIds: Effect.fn("OrchestratorV2.listAppOwnedChildThreadIds")(+ function (threadId) {+ return projectionStore+ .listAppOwnedChildThreadIds(threadId)+ .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));+ },+ ),getThreadProjection: Effect.fn("OrchestratorV2.getThreadProjection")(function (threadId) {return projectionStore.getThreadProjection(threadId)diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.tsindex d4475475497..179971019e6 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts@@ -1182,8 +1182,15 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {["running", settledRun, "running", null],["active-run-delivered", activeRun, "completed", "delivered"],] as const;+ // A follow-up reuses its task's child thread.+ const childThreadIds: Partial<Record<(typeof subagents)[number][0], ThreadId>> = {+ delivered: ThreadId.make("thread:settled-subagents:child-a"),+ running: ThreadId.make("thread:settled-subagents:child-a"),+ "no-delivery": ThreadId.make("thread:settled-subagents:child-b"),+ };for (const [index, [name, run, status, delivery]] of subagents.entries()) {const id = NodeId.make(`node:settled-subagents:${name}`);+ const childThreadId = childThreadIds[name] ?? null;yield* store.apply({id: EventId.make(`event:settled-subagents:item:${name}`),type: "turn-item.updated",@@ -1212,7 +1219,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {origin: "app_owned",driver,providerInstanceId,- childThreadId: null,+ childThreadId,prompt: "Do the work",result: null,},@@ -1236,7 +1243,7 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {driver,providerInstanceId,providerThreadId: null,- childThreadId: null,+ childThreadId,nativeTaskRef: null,prompt: "Do the work",title: name,@@ -1286,6 +1293,10 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {["delivered"],);assert.strictEqual(yield* store.getSubagentCount(threadId), subagents.length);+ assert.deepEqual(+ [...(yield* store.listAppOwnedChildThreadIds(threadId))],+ [childThreadIds.delivered, childThreadIds["no-delivery"]],+ );});const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () {diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.tsindex 66fc1d985e9..ab8d69f9a66 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts@@ -460,6 +460,10 @@ export interface ProjectionStoreV2Shape {) => Effect.Effect<ProjectionTimelinePage, ProjectionStoreV2Error>;readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;readonly getSubagentCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;+ /** Child threads of the thread's app-owned tasks, in task order, without decoding the tasks. */+ readonly listAppOwnedChildThreadIds: (+ threadId: ThreadId,+ ) => Effect.Effect<ReadonlyArray<ThreadId>, ProjectionStoreV2Error>;readonly getNextTurnItemOrdinal: (threadId: ThreadId,) => Effect.Effect<number, ProjectionStoreV2Error>;@@ -5388,6 +5392,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =Effect.map((rows) => rows[0]?.count ?? 0),Effect.mapError(controlReadError(threadId)),);+ const listAppOwnedChildThreadIds: ProjectionStoreV2Shape["listAppOwnedChildThreadIds"] = (+ threadId,+ ) =>+ sql<{ readonly child_thread_id: string }>`SELECT child_thread_id+ FROM orchestration_v2_projection_subagents+ WHERE thread_id = ${threadId} AND origin = 'app_owned' AND child_thread_id IS NOT NULL+ ORDER BY COALESCE(started_at, ''), subagent_id ASC`.pipe(+ Effect.map((rows) => [...new Set(rows.map((row) => ThreadId.make(row.child_thread_id)))]),+ Effect.mapError(controlReadError(threadId)),+ );const getNextTurnItemOrdinal: ProjectionStoreV2Shape["getNextTurnItemOrdinal"] = (threadId) =>sql<{ ordinal: number | null }>`SELECT MAX(ordinal) AS ordinalFROM orchestration_v2_projection_turn_items WHERE thread_id = ${threadId}`.pipe(@@ -7141,6 +7155,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =isNodeStored: readOnReader(isNodeStored),getMessageCount: readOnReader(getMessageCount),getSubagentCount: readOnReader(getSubagentCount),+ listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),getTurnItem: readOnReader(getTurnItem),getThreadRecords: (threadId, fields, filter) =>diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.tsindex 7932a03809c..77caf037125 100644--- a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts+++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts@@ -237,6 +237,7 @@ it.effect(getTimelinePage: () => Effect.die("Unused timeline read"),getMessageCount: () => Effect.die("unused message count"),getSubagentCount: () => Effect.die("unused subagent count"),+ listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"),getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"),getTurnItem: () => Effect.die("unused turn item read"),getThreadRecords: () => Effect.die("unused record read"),diff --git a/apps/server/src/orchestration-v2/ThreadManagementService.ts b/apps/server/src/orchestration-v2/ThreadManagementService.tsindex c1727567f9e..f0346b2bec4 100644--- a/apps/server/src/orchestration-v2/ThreadManagementService.ts+++ b/apps/server/src/orchestration-v2/ThreadManagementService.ts@@ -419,6 +419,7 @@ export interface ThreadManagementServiceShape {Orchestrator.OrchestratorV2Error>;readonly getThreadShell: Orchestrator.OrchestratorV2["Service"]["getThreadShell"];+ readonly listAppOwnedChildThreadIds: Orchestrator.OrchestratorV2["Service"]["listAppOwnedChildThreadIds"];/** Omit `projectId` to list every project's threads. Side chats are never listed. */readonly listProjectThreads: (input: {readonly projectId?: ProjectId | undefined;@@ -1526,6 +1527,13 @@ const make = Effect.gen(function* () {listOlderSettledThreads: orchestrator.listOlderSettledThreads,readShellSnapshot: orchestrator.readShellSnapshot,getThreadShell: orchestrator.getThreadShell,+ listAppOwnedChildThreadIds: Effect.fn("ThreadManagementService.listAppOwnedChildThreadIds")(+ function (threadId) {+ return ensureProjectionTranscript(threadId).pipe(+ Effect.andThen(orchestrator.listAppOwnedChildThreadIds(threadId)),+ );+ },+ ),listProjectThreads,sendToThread,assertProviderInstanceEnabled,diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.tsindex 2a259114b09..87838fd5a51 100644--- a/apps/server/src/relay/AgentAwarenessRelay.test.ts+++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts@@ -190,6 +190,7 @@ const makeTestRelay = Effect.fnUntraced(function* (return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };}),listActiveSubagentThreadIds: unused,+ listAppOwnedChildThreadIds: unused,countThreads: unused,listActiveShellIndex: () => Effect.succeed([]),listOlderSettledThreads: unused,