The best way to control Claude Code, Codex and more from any computer or phone
perf(server): skip unused parent history in linked task status (#27913)
Co-authored-by: maria-rcks <maria@kuuro.net>
Adamulek123 committed
Nov 3, 2026 at 02:32 UTC
3c59595a6449d8b04524ed7cb2b5dc04f17bae31
3 files changed
+92
-24
apps/server/src/mcp/OrchestratorMcpService.test.ts
+51
-17
@@ -3,6 +3,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
3
import { assert, describe, it } from "@effect/vitest";
4
import {
5
CommandId,
6
+ ContextTransferId,
7
EnvironmentId,
8
GitCommandError,
9
MessageId,
@@ -83,12 +84,21 @@ describe("OrchestratorMcpService", () => {
84
const childThreadId = ThreadId.make("thread:mcp-ack-child");
85
const childRunId = RunId.make("run:mcp-ack-child");
86
const taskId = NodeId.make("node:mcp-ack-task");
87
+ const resultTransferId = ContextTransferId.make("transfer:mcp-ack-result");
88
const acknowledgementCommandIds = yield* Ref.make<ReadonlyArray<string>>([]);
89
const acknowledgementAttempts = yield* Ref.make(0);
90
const parentProjection = {
91
thread: { id: parentThreadId },
92
runs: [],
91
- contextTransfers: [],
93
+ contextTransfers: [
94
+ {
95
+ id: resultTransferId,
96
+ type: "subagent_result",
97
+ sourceThreadId: childThreadId,
98
+ targetThreadId: parentThreadId,
99
+ sourcePoint: { runId: childRunId },
100
+ },
101
+ ],
102
subagents: [
103
{
104
id: taskId,
@@ -121,38 +131,57 @@ describe("OrchestratorMcpService", () => {
131
},
132
],
133
contextTransfers: [],
124
- messages: [],
134
+ messages: [
135
+ {
136
+ id: MessageId.make("message:mcp-ack-result"),
137
+ runId: childRunId,
138
+ role: "assistant",
139
+ text: "terminal result",
140
+ createdAt: DateTime.makeUnsafe("2026-10-03T10:10:00Z"),
141
+ updatedAt: DateTime.makeUnsafe("2026-10-03T10:10:00Z"),
142
+ },
143
+ ],
144
+ turnItems: [],
145
subagents: [],
146
providerThreads: [],
147
runtimeRequests: [],
148
} as unknown as OrchestrationV2ThreadProjection;
149
let hasNestedWork = true;
150
+ let settleAfterTaskRead = false;
151
const layerDependencies = Layer.mergeAll(
152
NodeServices.layer,
153
NodeCrypto.layer,
154
Layer.mock(ThreadManagementService.ThreadManagementService)({
155
getThreadRecords: (threadId) =>
135
- Effect.succeed(
136
- threadId === parentThreadId
137
- ? hasNestedWork
138
- ? {
139
- ...parentProjection,
140
- subagents: parentProjection.subagents.map((task) => ({
141
- ...task,
142
- result: null,
143
- status: "running" as const,
144
- })),
145
- }
146
- : parentProjection
147
- : hasNestedWork
156
+ Effect.sync(() => {
157
+ if (threadId !== parentThreadId) {
158
+ return hasNestedWork
159
? {
160
...childProjection,
161
subagents: [
162
{ ...parentProjection.subagents[0]!, status: "running" as const },
163
],
164
}
154
- : childProjection,
155
- ),
165
+ : childProjection;
166
+ }
167
+ const snapshot = hasNestedWork
168
+ ? {
169
+ ...parentProjection,
170
+ contextTransfers: [],
171
+ subagents: parentProjection.subagents.map((task) => ({
172
+ ...task,
173
+ result: null,
174
+ status: "running" as const,
175
+ })),
176
+ }
177
+ : parentProjection;
178
+ if (settleAfterTaskRead) {
179
+ settleAfterTaskRead = false;
180
+ hasNestedWork = false;
181
+ }
182
+ return snapshot;
183
+ }),
184
+ delegatedTaskResultPending: () => Effect.succeed(false),
185
dispatch: (command) =>
186
Ref.update(acknowledgementCommandIds, (commandIds) => [
187
...commandIds,
@@ -195,16 +224,21 @@ describe("OrchestratorMcpService", () => {
224
assert.equal(pending.status, "running");
225
assert.equal(pending.workState, "waiting_for_children");
226
assert.isNull(pending.summary);
227
+ assert.isNull(pending.resultContextTransferId);
228
assert.equal(yield* Ref.get(acknowledgementAttempts), 0);
229
hasNestedWork = false;
230
const error = yield* service.taskStatus(scope, taskId).pipe(Effect.flip);
231
assert.equal(error.code, "orchestration_error");
232
233
+ // Completion lands after task selection, before the remaining parent/child reads.
234
+ hasNestedWork = true;
235
+ settleAfterTaskRead = true;
236
const result = yield* service.taskStatus(scope, taskId);
237
assert.equal(result.status, "completed");
238
assert.equal(result.summary, "terminal result");
239
assert.equal(result.latestTerminalRunId, childRunId);
240
assert.equal(result.latestTerminalStatus, "completed");
241
+ assert.equal(result.resultContextTransferId, resultTransferId);
242
const commandIds = yield* Ref.get(acknowledgementCommandIds);
243
assert.equal(commandIds.length, 2);
244
assert.notEqual(commandIds[0], commandIds[1]);
apps/server/src/mcp/OrchestratorMcpService.ts
+4
-6
@@ -1093,11 +1093,9 @@ const make = Effect.gen(function* () {
1093
1094
// Task waits read on every wake of a parent that may hold thousands of
1095
// tasks, so they read only the task they watch and what its status needs.
1096
- const loadTaskParent = (threadId: ThreadId, taskId: NodeId) =>
1096
+ const loadTaskParent = (threadId: ThreadId) =>
1097
threadManagement
1098
- .getThreadRecords(threadId, ["runs", "subagents", "contextTransfers"], {
1099
- subagentIds: [taskId],
1100
- })
1098
+ .getThreadRecords(threadId, ["runs", "contextTransfers"])
1099
.pipe(
1100
Effect.mapError((error) =>
1101
failure(
@@ -1472,8 +1470,7 @@ const make = Effect.gen(function* () {
1470
acknowledgementOperation = "task-status-acknowledge",
1471
): Effect.fn.Return<OrchestratorMcpDelegateTaskResult, OrchestratorMcpFailure> {
1472
yield* requireCapability(scope);
1475
- const parentProjection = yield* loadTaskParent(scope.thread.threadId, taskId);
1476
- const task = parentProjection.subagents.find(
1473
+ const task = (yield* loadTasks(scope.thread.threadId, [taskId])).find(
1474
(candidate) =>
1475
candidate.id === taskId &&
1476
candidate.origin === "app_owned" &&
@@ -1488,6 +1485,7 @@ const make = Effect.gen(function* () {
1485
if (task.childThreadId === null) {
1486
return yield* readRemoteTask(scope, task, waitTimedOut, acknowledgeTerminal);
1487
}
1488
+ const parentProjection = yield* loadTaskParent(scope.thread.threadId);
1489
const childControls = yield* threadManagement
1490
.getThreadRecords(
1491
task.childThreadId,
apps/server/src/peer/RemoteDelegation.test.ts
+37
-1
@@ -29,8 +29,8 @@ import * as Queue from "effect/Queue";
29
import * as Ref from "effect/Ref";
30
import * as Schedule from "effect/Schedule";
31
import * as Stream from "effect/Stream";
32
-import * as Tracer from "effect/Tracer";
32
import * as SubscriptionRef from "effect/SubscriptionRef";
33
+import * as Tracer from "effect/Tracer";
34
import type * as SqlClient from "effect/sql/SqlClient";
35
import { McpSchema, McpServer } from "effect/ai";
36
@@ -829,6 +829,42 @@ it.effect("a task delegated to the box wakes the laptop's parent when it ends th
829
expect(retried.taskId).toBe(delegated.taskId);
830
expect((yield* a.orchestrator.getThreadProjection(parentThreadId)).subagents).toHaveLength(1);
831
832
+ const historyReads: Array<string> = [];
833
+ const historyTracer = Tracer.make({
834
+ span(options) {
835
+ const span = new Tracer.NativeSpan(options);
836
+ const end = span.end.bind(span);
837
+ span.end = (endTime, exit) => {
838
+ end(endTime, exit);
839
+ const query = span.attributes.get("db.query.text");
840
+ if (
841
+ typeof query === "string" &&
842
+ /\bSELECT\s+payload_json\s+FROM orchestration_v2_projection_(?:runs|context_transfers)\b/i.test(
843
+ query,
844
+ )
845
+ ) {
846
+ historyReads.push(query);
847
+ }
848
+ };
849
+ return span;
850
+ },
851
+ });
852
+ const running = yield* a
853
+ .call("task_status", { taskId: delegated.taskId })
854
+ .pipe(Effect.withTracer(historyTracer));
855
+ expect(running.structuredContent).toMatchObject({
856
+ taskId: delegated.taskId,
857
+ childThreadId: null,
858
+ remoteChild: delegated.remoteChild,
859
+ status: "running",
860
+ workState: "working",
861
+ summary: null,
862
+ hasPendingQuestion: false,
863
+ pendingApproval: null,
864
+ });
865
+ // A running task there needs its local row, not the parent's run or transfer history.
866
+ expect(historyReads).toEqual([]);
867
+
868
yield* Ref.set(thread.reply, "All 412 tests pass on the box.");
869
yield* Ref.set(thread.status, "completed");
870
yield* Deferred.succeed(thread.finished, undefined);