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, 00:45 UTC
perf(server): MCP tool calls no longer read the caller's whole thread (#33295)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Nov 10, 2026 at 07:18 UTC
9b352a04defad2ec5b1af7245a707f2361265d55
8 files changed
+316
-177
apps/server/src/mcp/OrchestratorMcpService.test.ts
+10
-11
@@ -91,15 +91,6 @@ describe("OrchestratorMcpService", () => {
91
const parentProjection = {
92
thread: { id: parentThreadId },
93
runs: [],
94
- contextTransfers: [
95
- {
96
- id: resultTransferId,
97
- type: "subagent_result",
98
- sourceThreadId: childThreadId,
99
- targetThreadId: parentThreadId,
100
- sourcePoint: { runId: childRunId },
101
- },
102
- ],
94
subagents: [
95
{
96
id: taskId,
@@ -131,7 +122,15 @@ describe("OrchestratorMcpService", () => {
122
completedAt: DateTime.makeUnsafe("2026-10-03T10:05:00Z"),
123
},
124
],
134
- contextTransfers: [],
125
+ contextTransfers: [
126
+ {
127
+ id: resultTransferId,
128
+ type: "subagent_result",
129
+ sourceThreadId: childThreadId,
130
+ targetThreadId: parentThreadId,
131
+ sourcePoint: { runId: childRunId },
132
+ },
133
+ ],
134
messages: [
135
{
136
id: MessageId.make("message:mcp-ack-result"),
@@ -160,6 +159,7 @@ describe("OrchestratorMcpService", () => {
159
return hasNestedWork
160
? {
161
...childProjection,
162
+ contextTransfers: [],
163
subagents: [
164
{ ...parentProjection.subagents[0]!, status: "running" as const },
165
],
@@ -169,7 +169,6 @@ describe("OrchestratorMcpService", () => {
169
const snapshot = hasNestedWork
170
? {
171
...parentProjection,
172
- contextTransfers: [],
172
subagents: parentProjection.subagents.map((task) => ({
173
...task,
174
result: null,
apps/server/src/mcp/OrchestratorMcpService.ts
+43
-100
@@ -583,8 +583,9 @@ function directAppOwnedChildTasks(
583
);
584
}
585
586
+// The child's transfers include the results it sent, so the parent's transfers are never read.
587
function taskResultTransfer(
587
- parent: Pick<OrchestrationV2ThreadProjection, "thread" | "contextTransfers">,
588
+ parentThreadId: ThreadId,
589
child: Pick<OrchestrationV2ThreadProjection, "runs" | "contextTransfers">,
590
task: OrchestrationV2Subagent,
591
) {
@@ -599,18 +600,18 @@ function taskResultTransfer(
600
.map((run) => run.id),
601
);
602
if (originalRun !== undefined) resultRunIds.add(originalRun.id);
602
- return parent.contextTransfers.find(
603
+ return child.contextTransfers.find(
604
(transfer) =>
605
transfer.type === "subagent_result" &&
606
transfer.sourceThreadId === task.childThreadId &&
606
- transfer.targetThreadId === parent.thread.id &&
607
+ transfer.targetThreadId === parentThreadId &&
608
((transfer.sourcePoint.runId !== undefined && resultRunIds.has(transfer.sourcePoint.runId)) ||
609
(transfer.sourcePoint.runId === undefined && originalRun?.delegatedTaskId === undefined)),
610
);
611
}
612
613
function pageIncludesTerminalTaskResult(input: {
613
- readonly parent: Pick<OrchestrationV2ThreadProjection, "thread" | "contextTransfers">;
614
+ readonly parentThreadId: ThreadId;
615
readonly page: ReadonlyArray<OrchestrationV2ThreadProjection["visibleTurnItems"][number]>;
616
readonly task: OrchestrationV2Subagent;
617
readonly target: Pick<
@@ -619,7 +620,7 @@ function pageIncludesTerminalTaskResult(input: {
620
>;
621
readonly maxChars: number;
622
}): boolean {
622
- const transfer = taskResultTransfer(input.parent, input.target, input.task);
623
+ const transfer = taskResultTransfer(input.parentThreadId, input.target, input.task);
624
if (transfer === undefined) return false;
625
const run =
626
transfer.sourcePoint.runId === undefined
@@ -1092,20 +1093,11 @@ const make = Effect.gen(function* () {
1093
),
1094
);
1095
1095
- const loadProjection = (threadId: ThreadId, includeUserMessages = true) =>
1096
- loadThreadRecords(
1097
- threadId,
1098
- [
1099
- "runs",
1100
- "messages",
1101
- "providerThreads",
1102
- "providerTurns",
1103
- "runtimeRequests",
1104
- "contextTransfers",
1105
- "turnItems",
1106
- ],
1107
- { turnItemTypes: [], messageRoles: includeUserMessages ? ["user"] : [] },
1108
- );
1096
+ // Runs on every MCP call, and long threads hold thousands of records; callers need only active runs.
1097
+ const loadCallerRecords = <K extends "runs">(threadId: ThreadId, fields: ReadonlyArray<K>) =>
1098
+ loadThreadRecords(threadId, fields, {
1099
+ runStatuses: ThreadManagementService.ACTIVE_RUN_STATUSES,
1100
+ });
1101
1102
// A dispatcher thread holds thousands of tasks, each with its full prompt; read only the ones named.
1103
const loadTasks = (threadId: ThreadId, taskIds: ReadonlyArray<NodeId>) =>
@@ -1116,43 +1108,9 @@ const make = Effect.gen(function* () {
1108
),
1109
);
1110
1119
- // Task waits read on every wake of a parent that may hold thousands of
1120
- // tasks, so they read only the task they watch and what its status needs.
1121
- const loadTaskParent = (threadId: ThreadId) =>
1122
- threadManagement
1123
- .getThreadRecords(threadId, ["runs", "contextTransfers"])
1124
- .pipe(
1125
- Effect.mapError((error) =>
1126
- failure(
1127
- "orchestration_error",
1128
- `Unable to read thread ${threadId}: ${errorMessage(error)}`,
1129
- ),
1130
- ),
1131
- );
1132
-
1133
- const loadProjectThread = (
1134
- projectId: OrchestrationV2ThreadProjection["thread"]["projectId"],
1135
- threadId: ThreadId,
1136
- ) =>
1137
- threadManagement
1138
- .getProjectThreadRecords(
1139
- { projectId, threadId },
1140
- [
1141
- "runs",
1142
- "messages",
1143
- "subagents",
1144
- "providerThreads",
1145
- "providerTurns",
1146
- "runtimeRequests",
1147
- "contextTransfers",
1148
- "turnItems",
1149
- ],
1150
- { turnItemTypes: [], messageRoles: ["user"] },
1151
- )
1152
- .pipe(Effect.mapError(threadManagementFailure));
1153
-
1154
- const loadCaller = Effect.fn("OrchestratorMcpService.loadCaller")(function* (
1111
+ const loadCaller = Effect.fn("OrchestratorMcpService.loadCaller")(function* <K extends "runs">(
1112
scope: McpInvocationScope,
1113
+ fields: ReadonlyArray<K>,
1114
) {
1115
yield* requireCapability(scope);
1116
if (scope.thread === undefined) {
@@ -1161,8 +1119,7 @@ const make = Effect.gen(function* () {
1119
limits: clientModeCeiling(scope.client),
1120
} as const;
1121
}
1164
- // No loadCaller consumer reads the caller's own prompts.
1165
- const parent = yield* loadProjection(scope.thread.threadId, false);
1122
+ const parent = yield* loadCallerRecords(scope.thread.threadId, fields);
1123
return {
1124
parent,
1125
limits: {
@@ -1178,7 +1135,7 @@ const make = Effect.gen(function* () {
1135
) {
1136
yield* requireCapability(scope);
1137
const threadScope = yield* requireThreadScope(scope, operation);
1181
- const parent = yield* loadProjection(threadScope.thread.threadId);
1138
+ const parent = yield* loadCallerRecords(threadScope.thread.threadId, ["runs"]);
1139
return { scope: threadScope, parent } as const;
1140
});
1141
@@ -1199,7 +1156,6 @@ const make = Effect.gen(function* () {
1156
1157
const loadTargetThread = Effect.fn("OrchestratorMcpService.loadTargetThread")(function* (
1158
threadId: ThreadId,
1202
- includeHistory = true,
1159
) {
1160
const shell = yield* threadManagement
1161
.getThreadShell(threadId)
@@ -1207,24 +1163,21 @@ const make = Effect.gen(function* () {
1163
if (shell === null || shell.deletedAt !== null) {
1164
return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`);
1165
}
1210
- // Native wait owns its history reads; keep legacy import and project validation here.
1211
- return yield* includeHistory
1212
- ? loadProjectThread(shell.projectId, threadId)
1213
- : threadManagement
1214
- .getProjectThreadRecords({ projectId: shell.projectId, threadId }, [])
1215
- .pipe(Effect.mapError(threadManagementFailure));
1166
+ // Keeps legacy import and project validation; callers read only the thread row.
1167
+ return yield* threadManagement
1168
+ .getProjectThreadRecords({ projectId: shell.projectId, threadId }, [])
1169
+ .pipe(Effect.mapError(threadManagementFailure));
1170
});
1171
1218
- const loadScopedThread = Effect.fn("OrchestratorMcpService.loadScopedThread")(function* (
1219
- scope: McpInvocationScope,
1220
- threadId: ThreadId,
1221
- includeHistory = true,
1222
- ) {
1223
- const caller = yield* loadCaller(scope);
1224
- const target =
1172
+ const loadScopedThread = Effect.fn("OrchestratorMcpService.loadScopedThread")(function* <
1173
+ K extends "runs",
1174
+ >(scope: McpInvocationScope, threadId: ThreadId, callerFields: ReadonlyArray<K>) {
1175
+ const caller = yield* loadCaller(scope, callerFields);
1176
+ // Only the thread row: the caller's own record holds just its active runs.
1177
+ const target: Pick<OrchestrationV2ThreadProjection, "thread"> =
1178
caller.parent !== undefined && threadId === caller.parent.thread.id
1179
? caller.parent
1227
- : yield* loadTargetThread(threadId, includeHistory);
1180
+ : yield* loadTargetThread(threadId);
1181
return { ...caller, target } as const;
1182
});
1183
@@ -1278,7 +1231,7 @@ const make = Effect.gen(function* () {
1231
scope: McpInvocationScope,
1232
threadId: ThreadId,
1233
) {
1281
- const { parent } = yield* loadCaller(scope);
1234
+ const { parent } = yield* loadCaller(scope, []);
1235
const shell = yield* threadManagement
1236
.getThreadShell(threadId)
1237
.pipe(Effect.mapError(threadManagementFailure));
@@ -1514,7 +1467,6 @@ const make = Effect.gen(function* () {
1467
if (task.childThreadId === null) {
1468
return yield* readRemoteTask(scope, task, waitTimedOut, acknowledgeTerminal);
1469
}
1517
- const parentProjection = yield* loadTaskParent(scope.thread.threadId);
1470
const childControls = yield* threadManagement
1471
.getThreadRecords(
1472
task.childThreadId,
@@ -1594,7 +1546,7 @@ const make = Effect.gen(function* () {
1546
: taskResultRun !== undefined && isTerminalTaskStatus(status)
1547
? subagentResultForRun(childProjection, taskResultRun).text
1548
: null;
1597
- const resultTransfers = parentProjection.contextTransfers.filter(
1549
+ const resultTransfers = childControls.contextTransfers.filter(
1550
(transfer) =>
1551
transfer.type === "subagent_result" &&
1552
transfer.sourceThreadId === task.childThreadId &&
@@ -1608,7 +1560,7 @@ const make = Effect.gen(function* () {
1560
? resultTransfers.find((transfer) => transfer.sourcePoint.runId === undefined)
1561
: undefined) ??
1562
null);
1611
- const resultTransfer = taskResultTransfer(parentProjection, childProjection, task) ?? null;
1563
+ const resultTransfer = taskResultTransfer(scope.thread.threadId, childProjection, task) ?? null;
1564
const terminalStatus = terminalRun === undefined ? null : taskStatusForRun(terminalRun);
1565
const latestTerminalSummary = canExposeTaskRunResult(terminalRun)
1566
? terminalRun.id === childRun?.id
@@ -1677,6 +1629,7 @@ const make = Effect.gen(function* () {
1629
task.completionDelivery?.state !== "acknowledged" &&
1630
task.completionDelivery?.state !== "disposed"
1631
) {
1632
+ const parentProjection = yield* loadCallerRecords(scope.thread.threadId, ["runs"]);
1633
const observingRun = ThreadManagementService.latestActiveRun({
1634
runs: parentProjection.runs.filter(
1635
(run) => run.providerInstanceId === scope.thread.providerInstanceId,
@@ -1762,17 +1715,7 @@ const make = Effect.gen(function* () {
1715
task.completionDelivery?.state !== "acknowledged" &&
1716
task.completionDelivery?.state !== "disposed"
1717
) {
1765
- const parentThreadId = scope.thread.threadId;
1766
- const parentProjection = yield* threadManagement
1767
- .getThreadRecords(parentThreadId, ["runs"])
1768
- .pipe(
1769
- Effect.mapError((error) =>
1770
- failure(
1771
- "orchestration_error",
1772
- `Unable to read thread ${parentThreadId}: ${errorMessage(error)}`,
1773
- ),
1774
- ),
1775
- );
1718
+ const parentProjection = yield* loadCallerRecords(scope.thread.threadId, ["runs"]);
1719
const observingRun = ThreadManagementService.latestActiveRun({
1720
runs: parentProjection.runs.filter(
1721
(run) => run.providerInstanceId === scope.thread.providerInstanceId,
@@ -2015,7 +1958,7 @@ const make = Effect.gen(function* () {
1958
1959
return OrchestratorMcpService.of({
1960
scheduleTask: Effect.fn("OrchestratorMcpService.scheduleTask")(function* (scope, input) {
2018
- const { parent, limits } = yield* loadCaller(scope);
1961
+ const { parent, limits } = yield* loadCaller(scope, ["runs"]);
1962
const projectId = yield* resolveProjectTarget(parent, input.projectId);
1963
yield* assertLiveCallerForOtherProject(scope, parent, projectId);
1964
const project = yield* requireProject(projectId);
@@ -2076,7 +2019,7 @@ const make = Effect.gen(function* () {
2019
}),
2020
listScheduledTasks: Effect.fn("OrchestratorMcpService.listScheduledTasks")(
2021
function* (scope, input) {
2079
- const { parent, limits } = yield* loadCaller(scope);
2022
+ const { parent, limits } = yield* loadCaller(scope, ["runs"]);
2023
const projectId = input.projectId ?? parent?.thread.projectId;
2024
const { tasks } = yield* scheduledTasks
2025
.list()
@@ -2095,7 +2038,7 @@ const make = Effect.gen(function* () {
2038
),
2039
updateScheduledTask: Effect.fn("OrchestratorMcpService.updateScheduledTask")(
2040
function* (scope, input) {
2098
- const { parent, limits } = yield* loadCaller(scope);
2041
+ const { parent, limits } = yield* loadCaller(scope, ["runs"]);
2042
const existing = yield* loadScheduledTask(input.scheduledTaskId, limits);
2043
yield* assertLiveCallerForOtherProject(scope, parent, existing.projectId);
2044
if (
@@ -2171,7 +2114,7 @@ const make = Effect.gen(function* () {
2114
),
2115
deleteScheduledTask: Effect.fn("OrchestratorMcpService.deleteScheduledTask")(
2116
function* (scope, input) {
2174
- const { parent, limits } = yield* loadCaller(scope);
2117
+ const { parent, limits } = yield* loadCaller(scope, ["runs"]);
2118
const existing = yield* loadScheduledTask(input.scheduledTaskId, limits);
2119
yield* assertLiveCallerForOtherProject(scope, parent, existing.projectId);
2120
yield* scheduledTasks
@@ -2337,7 +2280,7 @@ const make = Effect.gen(function* () {
2280
}),
2281
2282
capabilities: Effect.fn("OrchestratorMcpService.capabilities")(function* (scope, input = {}) {
2340
- const { parent, limits } = yield* loadCaller(scope);
2283
+ const { parent, limits } = yield* loadCaller(scope, []);
2284
const providers = yield* loadProviders;
2285
const orchestrationCapableInstanceIds = yield* loadOrchestrationCapableInstanceIds();
2286
const settings = yield* serverSettings.getSettings.pipe(
@@ -2968,7 +2911,7 @@ const make = Effect.gen(function* () {
2911
),
2912
);
2913
}
2971
- const projection = yield* loadProjection(threadId);
2914
+ const projection = yield* loadThreadRecords(threadId, ["runs"]);
2915
const run = projection.runs.at(-1);
2916
yield* threadManagement
2917
.dispatch({
@@ -3014,7 +2957,7 @@ const make = Effect.gen(function* () {
2957
},
2958
),
2959
listThreads: Effect.fn("OrchestratorMcpService.listThreads")(function* (scope, input) {
3017
- const { parent } = yield* loadCaller(scope);
2960
+ const { parent } = yield* loadCaller(scope, []);
2961
const projectId = input.allProjects
2962
? null
2963
: yield* resolveProjectTarget(parent, input.projectId);
@@ -3118,7 +3061,7 @@ const make = Effect.gen(function* () {
3061
(input.textOffset ?? 0) === 0
3062
) {
3063
for (const task of tasks) {
3121
- const transfer = taskResultTransfer(parent, target, task);
3064
+ const transfer = taskResultTransfer(parent.thread.id, target, task);
3065
const resultRunId = transfer?.sourcePoint.runId ?? delegatedTaskRun(target, task)?.id;
3066
const resultRunIds = resultRunId === undefined ? [] : [resultRunId];
3067
const resultRecords = yield* threadManagement
@@ -3131,7 +3074,7 @@ const make = Effect.gen(function* () {
3074
.pipe(Effect.mapError(threadManagementFailure));
3075
if (
3076
pageIncludesTerminalTaskResult({
3134
- parent,
3077
+ parentThreadId: parent.thread.id,
3078
page,
3079
task,
3080
target: { ...target, ...resultRecords },
@@ -3173,7 +3116,7 @@ const make = Effect.gen(function* () {
3116
} satisfies OrchestratorMcpThreadReadResult;
3117
}),
3118
sendToThread: Effect.fn("OrchestratorMcpService.sendToThread")(function* (scope, input) {
3176
- const { parent, limits, target } = yield* loadScopedThread(scope, input.threadId);
3119
+ const { parent, limits, target } = yield* loadScopedThread(scope, input.threadId, ["runs"]);
3120
yield* assertLiveCallerForOtherThread(scope, parent, target);
3121
yield* resolveRuntimeMode(limits.runtimeMode, target.thread.runtimeMode);
3122
yield* resolveInteractionMode(limits.interactionMode, target.thread.interactionMode);
@@ -3224,7 +3167,7 @@ const make = Effect.gen(function* () {
3167
} satisfies OrchestratorMcpThreadSendResult;
3168
}),
3169
waitForThread: Effect.fn("OrchestratorMcpService.waitForThread")(function* (scope, input) {
3227
- const { parent, target } = yield* loadScopedThread(scope, input.threadId, false);
3170
+ const { parent, target } = yield* loadScopedThread(scope, input.threadId, []);
3171
const result = yield* threadManagement
3172
.waitForThread({
3173
projectId: target.thread.projectId,
@@ -3274,7 +3217,7 @@ const make = Effect.gen(function* () {
3217
} satisfies OrchestratorMcpThreadWaitResult;
3218
}),
3219
interruptThread: Effect.fn("OrchestratorMcpService.interruptThread")(function* (scope, input) {
3277
- const { parent, limits, target } = yield* loadScopedThread(scope, input.threadId);
3220
+ const { parent, limits, target } = yield* loadScopedThread(scope, input.threadId, ["runs"]);
3221
yield* assertLiveCallerForOtherThread(scope, parent, target);
3222
yield* resolveRuntimeMode(limits.runtimeMode, target.thread.runtimeMode);
3223
yield* resolveInteractionMode(limits.interactionMode, target.thread.interactionMode);
apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
+81
-4
@@ -216,13 +216,34 @@ describe("orchestrator MCP toolkit", () => {
216
},
217
CodexOrchestratorReplayHarness,
218
);
219
+ // SQL spans carry no bound parameters, so this records which thread each tool read.
220
+ const threadRecordReads: Array<{
221
+ readonly threadId: ThreadId;
222
+ readonly fields: ReadonlyArray<string>;
223
+ }> = [];
224
const layerOrchestration = Layer.merge(
225
layerOrchestrator,
221
- ThreadManagementService.layer.pipe(
222
- Layer.provide(layerOrchestrator),
223
- Layer.provide(ServerSettings.layerTest()),
226
+ Layer.effect(
227
+ ThreadManagementService.ThreadManagementService,
228
+ Effect.gen(function* () {
229
+ const service = yield* ThreadManagementService.ThreadManagementService;
230
+ return ThreadManagementService.ThreadManagementService.of({
231
+ ...service,
232
+ getThreadRecords: (threadId, fields, filter) =>
233
+ Effect.sync(() => threadRecordReads.push({ threadId, fields })).pipe(
234
+ Effect.andThen(service.getThreadRecords(threadId, fields, filter)),
235
+ ),
236
+ });
237
+ }),
238
+ ).pipe(
239
Layer.provide(
225
- CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)),
240
+ ThreadManagementService.layer.pipe(
241
+ Layer.provide(layerOrchestrator),
242
+ Layer.provide(ServerSettings.layerTest()),
243
+ Layer.provide(
244
+ CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)),
245
+ ),
246
+ ),
247
),
248
),
249
);
@@ -603,6 +624,9 @@ describe("orchestrator MCP toolkit", () => {
624
);
625
const transferReads: Array<string> = [];
626
const messageReads: Array<string> = [];
627
+ const runReads: Array<string> = [];
628
+ const projectionTableReads: Array<string> = [];
629
+ const acknowledgementReads: Array<string> = [];
630
const tracer = Tracer.make({
631
span(options) {
632
const span = new Tracer.NativeSpan(options);
@@ -611,11 +635,29 @@ describe("orchestrator MCP toolkit", () => {
635
end(endTime, exit);
636
const query = span.attributes.get("db.query.text");
637
if (typeof query !== "string") return;
638
+ let parent = Option.getOrUndefined(span.parent);
639
+ while (parent?._tag === "Span") {
640
+ if (
641
+ parent.name === "Orchestrator.dispatchDelegatedTaskCompletionDeliveryResolution"
642
+ ) {
643
+ acknowledgementReads.push(query);
644
+ break;
645
+ }
646
+ parent = Option.getOrUndefined(parent.parent);
647
+ }
648
+ for (const [, table] of query.matchAll(
649
+ /FROM orchestration_v2_projection_(\w+)/gi,
650
+ )) {
651
+ projectionTableReads.push(table!);
652
+ }
653
if (
654
/SELECT[\s\S]*FROM orchestration_v2_projection_context_transfers/i.test(query)
655
) {
656
transferReads.push(query);
657
}
658
+ if (/SELECT\s+payload_json\s+FROM orchestration_v2_projection_runs/i.test(query)) {
659
+ runReads.push(query);
660
+ }
661
if (
662
/SELECT\s+payload_json\s+FROM orchestration_v2_projection_messages/i.test(query)
663
) {
@@ -687,6 +729,18 @@ describe("orchestrator MCP toolkit", () => {
729
expect(read.isError).not.toBe(true);
730
expect(transferReads.length).toBeGreaterThan(0);
731
expect(read.structuredContent).toEqual(parentlessRead.structuredContent);
732
+ // The acknowledgement reads the task and its spawning run by id, not the parent's history.
733
+ expect(acknowledgementReads.length).toBeGreaterThan(0);
734
+ expect(
735
+ acknowledgementReads.filter(
736
+ (query) =>
737
+ /FROM orchestration_v2_projection_(nodes|run_attempts)\b/.test(query) ||
738
+ (/FROM orchestration_v2_projection_runs\b/.test(query) &&
739
+ !query.includes("AND run_id IN")) ||
740
+ (/FROM orchestration_v2_projection_subagents\b/.test(query) &&
741
+ !query.includes("AND subagent_id IN")),
742
+ ),
743
+ ).toEqual([]);
744
const afterRead = yield* orchestrator.getThreadProjection(parentThreadId);
745
expect(
746
afterRead.subagents.find((task) => task.id === queuedFollowup.taskId)
@@ -694,11 +748,34 @@ describe("orchestrator MCP toolkit", () => {
748
).toBe("acknowledged");
749
750
const messageReadsBeforeCapabilities = messageReads.length;
751
+ const tableReadsBeforeCapabilities = projectionTableReads.length;
752
const capabilities = yield* invoke("orchestrator_capabilities", {}).pipe(
753
Effect.withTracer(tracer),
754
);
755
expect(capabilities.isError).not.toBe(true);
756
expect(messageReads).toHaveLength(messageReadsBeforeCapabilities);
757
+ // The caller check reads only the thread row, never its history.
758
+ expect(new Set(projectionTableReads.slice(tableReadsBeforeCapabilities))).toEqual(
759
+ new Set(["threads"]),
760
+ );
761
+ const runReadsBeforeStatus = runReads.length;
762
+ const transferReadsBeforeStatus = transferReads.length;
763
+ const threadRecordReadsBeforeStatus = threadRecordReads.length;
764
+ const statusAgain = yield* invoke("task_status", { taskId: delegated.taskId }).pipe(
765
+ Effect.withTracer(tracer),
766
+ );
767
+ expect(statusAgain.isError).not.toBe(true);
768
+ // A task wait reads result transfers and runs from the child; the parent's stay unread.
769
+ expect(runReads).toHaveLength(runReadsBeforeStatus + 1);
770
+ expect(transferReads).toHaveLength(transferReadsBeforeStatus + 1);
771
+ expect(
772
+ threadRecordReads
773
+ .slice(threadRecordReadsBeforeStatus)
774
+ .filter(
775
+ ({ fields }) => fields.includes("runs") || fields.includes("contextTransfers"),
776
+ )
777
+ .map(({ threadId }) => threadId),
778
+ ).toEqual([delegated.childThreadId]);
779
}).pipe(Effect.provide(layerTest));
780
}),
781
),
apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+67
@@ -1055,6 +1055,73 @@ it.layer(layerTest)("delegated completion delivery repairs", (it) => {
1055
}),
1056
);
1057
1058
+ it.effect("acknowledging a queued wake's results rewrites it, then cancels it", () =>
1059
+ Effect.gen(function* () {
1060
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
1061
+ const now = yield* DateTime.now;
1062
+ const threadId = ThreadId.make("queued-wake-ack-parent");
1063
+ const runId = RunId.make("queued-wake-ack-origin");
1064
+ const taskId = NodeId.make("queued-wake-ack-first");
1065
+ const siblingId = NodeId.make("queued-wake-ack-sibling");
1066
+ yield* seedParentWithTerminalTask({
1067
+ threadId,
1068
+ projectId: ProjectId.make("queued-wake-ack-project"),
1069
+ runId,
1070
+ rootNodeId: NodeId.make("queued-wake-ack-root"),
1071
+ taskId,
1072
+ deliveryState: "claimed",
1073
+ deliveryTaskIds: [taskId, siblingId],
1074
+ siblingTasks: [{ id: siblingId, deliveryState: "claimed" }],
1075
+ completionWake: "always",
1076
+ now,
1077
+ });
1078
+ const delivery = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!
1079
+ .delegatedCompletion!.delivery!;
1080
+ yield* orchestrator.dispatch({
1081
+ type: "message.dispatch",
1082
+ createdBy: "agent",
1083
+ creationSource: "server",
1084
+ commandId: CommandId.make("queued-wake-ack-wake"),
1085
+ threadId,
1086
+ messageId: delivery.messageId,
1087
+ text: "Delegated tasks completed",
1088
+ attachments: [],
1089
+ modelSelection,
1090
+ dispatchMode: { type: "queue_after_active" },
1091
+ delegatedCompletion: {
1092
+ parentRunId: runId,
1093
+ generation: delivery.generation,
1094
+ taskIds: delivery.taskIds,
1095
+ },
1096
+ });
1097
+ const acknowledge = (id: NodeId) =>
1098
+ orchestrator.dispatch({
1099
+ type: "delegated_task.completion-delivery.acknowledge",
1100
+ commandId: CommandId.make(`queued-wake-ack:${id}`),
1101
+ parentThreadId: threadId,
1102
+ taskId: id,
1103
+ observedByRunId: runId,
1104
+ });
1105
+
1106
+ yield* acknowledge(taskId);
1107
+ const rewritten = yield* orchestrator.getThreadProjection(threadId);
1108
+ const wake = rewritten.runs.find((run) => run.userMessageId === delivery.messageId)!;
1109
+ assert.equal(wake.status, "queued");
1110
+ const message = rewritten.messages.find((row) => row.id === delivery.messageId);
1111
+ assert.deepEqual(message?.delegatedCompletion?.taskIds, [siblingId]);
1112
+ assert.equal(message?.text, `Delegated task ${siblingId} [completed]:\nchild finished`);
1113
+
1114
+ yield* acknowledge(siblingId);
1115
+ const cancelled = yield* orchestrator.getThreadProjection(threadId);
1116
+ assert.equal(cancelled.runs.find((run) => run.id === wake.id)?.status, "cancelled");
1117
+ assert.equal(
1118
+ cancelled.nodes.find((node) => node.id === wake.rootNodeId)?.status,
1119
+ "cancelled",
1120
+ );
1121
+ assert.isNull(cancelled.runs.find((run) => run.id === runId)?.delegatedCompletion?.delivery);
1122
+ }),
1123
+ );
1124
+
1125
it.effect.each([
1126
{ command: "acknowledge", before: "running", after: "failed", capture: false },
1127
{ command: "acknowledge", before: "waiting", after: "completed", capture: true },
apps/server/src/orchestration-v2/Orchestrator.ts
+40
-47
@@ -2783,21 +2783,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2783
>,
2784
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
2785
) {
2786
- const projection = yield* projectionStore
2787
- .getThreadRecords(
2788
- command.parentThreadId,
2789
- ["subagents", "runs", "nodes", "attempts", "turnItems"],
2790
- { turnItemTypes: [] },
2791
- )
2792
- .pipe(
2793
- Effect.mapError(
2794
- (cause) =>
2795
- new OrchestratorProjectionError({
2796
- threadId: command.parentThreadId,
2797
- cause,
2798
- }),
2799
- ),
2800
- );
2786
+ const projection = yield* loadProjectionForCommand(command, ["subagents"], {
2787
+ subagentIds: [command.taskId],
2788
+ });
2789
const task = projection.subagents.find(
2790
(candidate) => candidate.id === command.taskId && candidate.origin === "app_owned",
2791
);
@@ -2814,6 +2802,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2802
: "disposed";
2803
const now = yield* DateTime.now;
2804
const emitEvent = emit(events, command);
2805
+ const {
2806
+ runs: [parentRun],
2807
+ } = yield* loadProjectionForCommand(command, ["runs"], {
2808
+ runIds: task.runId === null ? [] : [task.runId],
2809
+ });
2810
// A late wait can arrive after finalization held the result for its still-live spawning run.
2811
if (isUnobservedAcknowledgement(command)) {
2812
if (
@@ -2821,23 +2814,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2814
task.completionDelivery?.state !== "pending"
2815
)
2816
return;
2824
- const parentRun = projection.runs.find((run) => run.id === task.runId);
2817
if (parentRun !== undefined && isLiveRun(parentRun)) return;
2826
- const records = yield* projectionStore
2827
- .getThreadRecords(command.parentThreadId, ["messages", "providerTurns"], {
2828
- messageRoles: ["user"],
2829
- })
2830
- .pipe(
2831
- Effect.mapError(
2832
- (cause) =>
2833
- new OrchestratorProjectionError({
2834
- threadId: command.parentThreadId,
2835
- cause,
2836
- }),
2837
- ),
2838
- );
2818
+ const records = yield* loadProjectionForCommand(
2819
+ command,
2820
+ ["runs", "subagents", "messages", "providerTurns"],
2821
+ { messageRoles: ["user"] },
2822
+ );
2823
const plan = yield* planDelegatedCompletionDelivery({
2840
- parentProjection: { ...projection, ...records },
2824
+ parentProjection: records,
2825
parentRun,
2826
task,
2827
updatedTask: task,
@@ -2907,10 +2891,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2891
payload: updatedTask,
2892
});
2893
2910
- const parentRun =
2911
- task.runId === null
2912
- ? undefined
2913
- : projection.runs.find((candidate) => candidate.id === task.runId);
2894
const cohort = parentRun?.delegatedCompletion;
2895
const delivery = cohort?.delivery;
2896
if (
@@ -2925,17 +2905,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2905
return;
2906
}
2907
2928
- const { messages } = yield* projectionStore
2929
- .getThreadRecords(command.parentThreadId, ["messages"], {
2930
- messageRoles: ["user"],
2931
- messageIds: [delivery.messageId],
2932
- })
2933
- .pipe(
2934
- Effect.mapError(
2935
- (cause) => new OrchestratorProjectionError({ threadId: command.parentThreadId, cause }),
2936
- ),
2937
- );
2938
- const deliveryProjection = { ...projection, messages };
2908
+ const { messages } = yield* loadProjectionForCommand(command, ["messages"], {
2909
+ messageRoles: ["user"],
2910
+ messageIds: [delivery.messageId],
2911
+ });
2912
+ const deliveryRunId = completionDeliveryMessage({ messages }, delivery)?.runId ?? null;
2913
+ const { runs } = yield* loadProjectionForCommand(command, ["runs"], {
2914
+ runIds: deliveryRunId === null ? [] : [deliveryRunId],
2915
+ });
2916
+ const deliveryProjection = { messages, runs };
2917
const remainingTaskIds = delivery.taskIds.filter((taskId) => taskId !== task.id);
2918
const deliveryRun = completionDeliveryRun(deliveryProjection, delivery);
2919
const clearDelivery =
@@ -2965,10 +2943,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2943
2944
if (deliveryRun?.status === "queued") {
2945
if (remainingTaskIds.length === 0) {
2946
+ const rootNodeFilter = {
2947
+ nodeIds: deliveryRun.rootNodeId === null ? [] : [deliveryRun.rootNodeId],
2948
+ };
2949
yield* emitQueuedRunCancellation({
2950
command,
2951
events,
2971
- projection,
2952
+ // Attempts have no id filter; a wake that never started has no attempt to cancel.
2953
+ projection:
2954
+ deliveryRun.activeAttemptId === null
2955
+ ? {
2956
+ attempts: [],
2957
+ ...(yield* loadProjectionForCommand(command, ["nodes"], rootNodeFilter)),
2958
+ }
2959
+ : yield* loadProjectionForCommand(command, ["nodes", "attempts"], rootNodeFilter),
2960
run: deliveryRun,
2961
now,
2962
});
@@ -2976,6 +2964,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2964
}
2965
const message = completionDeliveryMessage(deliveryProjection, delivery);
2966
if (message !== undefined) {
2967
+ const { subagents: remainingTasks } = yield* loadProjectionForCommand(
2968
+ command,
2969
+ ["subagents"],
2970
+ { subagentIds: remainingTaskIds },
2971
+ );
2972
yield* emitEvent({
2973
type: "message.updated",
2974
threadId: command.parentThreadId,
@@ -2985,7 +2978,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2978
occurredAt: now,
2979
payload: {
2980
...message,
2988
- text: delegatedCompletionWakeDetail(remainingTaskIds, projection.subagents),
2981
+ text: delegatedCompletionWakeDetail(remainingTaskIds, remainingTasks),
2982
delegatedCompletion: {
2983
parentRunId: parentRun.id,
2984
generation: delivery.generation,
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+47
@@ -694,6 +694,53 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
694
records.runs.map((run) => run.id),
695
["run:idle-compaction:rolled-back", "run:idle-compaction:4"],
696
);
697
+ const { statements, tracer } = traceSqlStatements();
698
+ const statusOnly = yield* store
699
+ .getThreadRecords(threadId, ["runs"], {
700
+ runStatuses: ["preparing", "starting", "running", "rolled_back"],
701
+ })
702
+ .pipe(Effect.withTracer(tracer));
703
+ assert.deepEqual(
704
+ statusOnly.runs.map((run) => run.id),
705
+ ["run:idle-compaction:rolled-back"],
706
+ );
707
+ const runQuery = statements.find((statement) =>
708
+ statement.includes("FROM orchestration_v2_projection_runs"),
709
+ );
710
+ assert.isDefined(runQuery);
711
+ const sql = yield* SqlClient.SqlClient;
712
+ // Read only the matching runs, not every run in ordinal order.
713
+ const runPlan = (yield* sql.unsafe<{ readonly detail: string }>(
714
+ `EXPLAIN QUERY PLAN ${runQuery}`,
715
+ ))
716
+ .map((row) => row.detail)
717
+ .join("\n");
718
+ assert.notInclude(runPlan, "orchestration_v2_projection_runs_thread_ordinal_idx");
719
+ assert.match(runPlan, /SEARCH orchestration_v2_projection_runs USING INDEX \w*status\w*/);
720
+ // An id filter reads by primary key, not every row of the thread.
721
+ const byId = traceSqlStatements();
722
+ const found = yield* store
723
+ .getThreadRecords(threadId, ["runs", "nodes", "subagents", "messages"], {
724
+ runIds: [RunId.make("run:idle-compaction:4")],
725
+ nodeIds: [nodeId],
726
+ subagentIds: [nodeId],
727
+ messageIds: [MessageId.make("message:idle-compaction:4")],
728
+ })
729
+ .pipe(Effect.withTracer(byId.tracer));
730
+ assert.deepEqual(
731
+ found.runs.map((run) => run.id),
732
+ ["run:idle-compaction:4"],
733
+ );
734
+ for (const table of ["runs", "nodes", "subagents", "messages"]) {
735
+ const query = byId.statements.find((statement) =>
736
+ statement.includes(`FROM orchestration_v2_projection_${table}`),
737
+ );
738
+ assert.isDefined(query);
739
+ const plan = (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`))
740
+ .map((row) => row.detail)
741
+ .join("\n");
742
+ assert.include(plan, `USING INDEX sqlite_autoindex_orchestration_v2_projection_${table}_1`);
743
+ }
744
assert.deepEqual(
745
records.providerTurns.map((turn) => turn.id),
746
["turn:idle-newest"],
apps/server/src/orchestration-v2/ProjectionStore.ts
+20
-9
@@ -345,6 +345,7 @@ export interface ProjectionRecordFilter {
345
readonly turnItemRunIds?: ReadonlyArray<RunId | null>;
346
readonly turnItemNodeIds?: ReadonlyArray<NodeId>;
347
readonly runIds?: ReadonlyArray<RunId>;
348
+ /** Without `includeLatestRun`, reads through a status index and sorts the matches, so keep it to statuses few runs hold. */
349
readonly runStatuses?: ReadonlyArray<OrchestrationV2Run["status"]>;
350
/** Keeps the thread's highest-ordinal run even when `runStatuses` excludes it. */
351
readonly includeLatestRun?: true;
@@ -3167,6 +3168,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3168
`;
3169
});
3170
3171
+ // An id filter reads by primary key. `+` keeps SQLite off the thread index, which walks
3172
+ // every row of the thread to find the few ids.
3173
+ const threadColumn = (ids: ReadonlyArray<unknown> | undefined, column = "thread_id") =>
3174
+ sql.literal(ids === undefined ? column : `+${column}`);
3175
+
3176
const readCanonicalProjection = Effect.fn("ProjectionStore.readCanonicalProjection")(
3177
function* (
3178
threadId: ThreadId,
@@ -3400,21 +3406,26 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3406
? sql<PayloadRow>`
3407
SELECT payload_json
3408
FROM orchestration_v2_projection_runs
3403
- WHERE thread_id = ${threadId}
3409
+ WHERE ${threadColumn(filter?.runIds)} = ${threadId}
3410
${filter?.runIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.runIds)}))`}
3411
${
3412
filter?.runStatuses === undefined
3407
- ? sql``
3408
- : sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
3409
- OR (${filter.includeLatestRun === true ? 1 : 0} = 1 AND run_id = (
3413
+ ? sql`ORDER BY ordinal ASC`
3414
+ : filter.includeLatestRun !== true
3415
+ ? // `+ordinal` stops SQLite from walking every run in the (thread_id, ordinal)
3416
+ // index to skip a sort; a status index reads only matching runs.
3417
+ sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
3418
+ ORDER BY +ordinal ASC`
3419
+ : sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
3420
+ OR run_id = (
3421
SELECT run_id FROM orchestration_v2_projection_runs
3422
WHERE thread_id = ${threadId}
3423
AND (status <> 'cancelled' OR json_extract(payload_json, '$.startedAt') IS NOT NULL)
3424
ORDER BY ordinal DESC
3425
LIMIT 1
3415
- )))`
3426
+ ))
3427
+ ORDER BY ordinal ASC`
3428
}
3417
- ORDER BY ordinal ASC
3429
`
3430
: sql<PayloadRow>`
3431
SELECT payload_json FROM orchestration_v2_projection_runs
@@ -3464,7 +3475,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3475
? sql<PayloadRow>`
3476
SELECT payload_json
3477
FROM orchestration_v2_projection_nodes AS node
3467
- WHERE node.thread_id = ${threadId}
3478
+ WHERE ${threadColumn(filter?.nodeIds, "node.thread_id")} = ${threadId}
3479
${filter?.nodeIds === undefined ? sql`` : sql`AND node.node_id IN (SELECT value FROM json_each(${encodeIdList(filter.nodeIds)}))`}
3480
${
3481
filter?.omitSettledLeafNodes === true
@@ -3503,7 +3514,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3514
? sql<PayloadRow>`
3515
SELECT payload_json
3516
FROM orchestration_v2_projection_subagents AS subagent
3506
- WHERE thread_id = ${threadId}
3517
+ WHERE ${threadColumn(filter?.subagentIds)} = ${threadId}
3518
${filter?.subagentIds === undefined ? sql`` : sql`AND subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentIds)}))`}
3519
${filter?.subagentRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentRunIds)}))`}
3520
${
@@ -3638,7 +3649,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3649
: window === undefined
3650
? sql<PayloadRow>`
3651
SELECT payload_json FROM orchestration_v2_projection_messages
3641
- WHERE thread_id = ${threadId}
3652
+ WHERE ${threadColumn(filter?.messageIds)} = ${threadId}
3653
${filter?.messageIds === undefined ? sql`` : sql`AND message_id IN (SELECT value FROM json_each(${encodeIdList(filter.messageIds)}))`}
3654
${filter?.messageRoles === undefined ? sql`` : sql`AND role IN (SELECT value FROM json_each(${encodeIdList(filter.messageRoles)}))`}
3655
${filter?.monitorNotificationsOnly === true ? sql`AND json_extract(payload_json, '$.notification.source.kind') = 'monitor'` : sql``}
apps/server/src/orchestration-v2/ThreadManagementService.ts
+8
-6
@@ -504,13 +504,15 @@ export class ThreadManagementService extends Context.Service<
504
ThreadManagementServiceShape
505
>()("t3/orchestration-v2/ThreadManagementService") {}
506
507
+export const ACTIVE_RUN_STATUSES = [
508
+ "preparing",
509
+ "starting",
510
+ "running",
511
+ "waiting",
512
+] as const satisfies ReadonlyArray<OrchestrationV2Run["status"]>;
513
+
514
export function isActiveRun(run: OrchestrationV2Run): boolean {
508
- return (
509
- run.status === "preparing" ||
510
- run.status === "starting" ||
511
- run.status === "running" ||
512
- run.status === "waiting"
513
- );
515
+ return (ACTIVE_RUN_STATUSES as ReadonlyArray<OrchestrationV2Run["status"]>).includes(run.status);
516
}
517
518
export function isTerminalRunStatus(