Skip to content
TypeScript

perf(server): skip unused parent history in linked task status (#27913)

Adamulek1233c59595a6449
diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts
index 9b12ec82754..94ed303fde5 100644
--- a/apps/server/src/mcp/OrchestratorMcpService.test.ts
+++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts
@@ -3,6 +3,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import {
CommandId,
+ ContextTransferId,
EnvironmentId,
GitCommandError,
MessageId,
@@ -83,12 +84,21 @@ describe("OrchestratorMcpService", () => {
const childThreadId = ThreadId.make("thread:mcp-ack-child");
const childRunId = RunId.make("run:mcp-ack-child");
const taskId = NodeId.make("node:mcp-ack-task");
+ const resultTransferId = ContextTransferId.make("transfer:mcp-ack-result");
const acknowledgementCommandIds = yield* Ref.make<ReadonlyArray<string>>([]);
const acknowledgementAttempts = yield* Ref.make(0);
const parentProjection = {
thread: { id: parentThreadId },
runs: [],
- contextTransfers: [],
+ contextTransfers: [
+ {
+ id: resultTransferId,
+ type: "subagent_result",
+ sourceThreadId: childThreadId,
+ targetThreadId: parentThreadId,
+ sourcePoint: { runId: childRunId },
+ },
+ ],
subagents: [
{
id: taskId,
@@ -121,38 +131,57 @@ describe("OrchestratorMcpService", () => {
},
],
contextTransfers: [],
- messages: [],
+ messages: [
+ {
+ id: MessageId.make("message:mcp-ack-result"),
+ runId: childRunId,
+ role: "assistant",
+ text: "terminal result",
+ createdAt: DateTime.makeUnsafe("2026-10-03T10:10:00Z"),
+ updatedAt: DateTime.makeUnsafe("2026-10-03T10:10:00Z"),
+ },
+ ],
+ turnItems: [],
subagents: [],
providerThreads: [],
runtimeRequests: [],
} as unknown as OrchestrationV2ThreadProjection;
let hasNestedWork = true;
+ let settleAfterTaskRead = false;
const layerDependencies = Layer.mergeAll(
NodeServices.layer,
NodeCrypto.layer,
Layer.mock(ThreadManagementService.ThreadManagementService)({
getThreadRecords: (threadId) =>
- Effect.succeed(
- threadId === parentThreadId
- ? hasNestedWork
- ? {
- ...parentProjection,
- subagents: parentProjection.subagents.map((task) => ({
- ...task,
- result: null,
- status: "running" as const,
- })),
- }
- : parentProjection
- : hasNestedWork
+ Effect.sync(() => {
+ if (threadId !== parentThreadId) {
+ return hasNestedWork
? {
...childProjection,
subagents: [
{ ...parentProjection.subagents[0]!, status: "running" as const },
],
}
- : childProjection,
- ),
+ : childProjection;
+ }
+ const snapshot = hasNestedWork
+ ? {
+ ...parentProjection,
+ contextTransfers: [],
+ subagents: parentProjection.subagents.map((task) => ({
+ ...task,
+ result: null,
+ status: "running" as const,
+ })),
+ }
+ : parentProjection;
+ if (settleAfterTaskRead) {
+ settleAfterTaskRead = false;
+ hasNestedWork = false;
+ }
+ return snapshot;
+ }),
+ delegatedTaskResultPending: () => Effect.succeed(false),
dispatch: (command) =>
Ref.update(acknowledgementCommandIds, (commandIds) => [
...commandIds,
@@ -195,16 +224,21 @@ describe("OrchestratorMcpService", () => {
assert.equal(pending.status, "running");
assert.equal(pending.workState, "waiting_for_children");
assert.isNull(pending.summary);
+ assert.isNull(pending.resultContextTransferId);
assert.equal(yield* Ref.get(acknowledgementAttempts), 0);
hasNestedWork = false;
const error = yield* service.taskStatus(scope, taskId).pipe(Effect.flip);
assert.equal(error.code, "orchestration_error");
+ // Completion lands after task selection, before the remaining parent/child reads.
+ hasNestedWork = true;
+ settleAfterTaskRead = true;
const result = yield* service.taskStatus(scope, taskId);
assert.equal(result.status, "completed");
assert.equal(result.summary, "terminal result");
assert.equal(result.latestTerminalRunId, childRunId);
assert.equal(result.latestTerminalStatus, "completed");
+ assert.equal(result.resultContextTransferId, resultTransferId);
const commandIds = yield* Ref.get(acknowledgementCommandIds);
assert.equal(commandIds.length, 2);
assert.notEqual(commandIds[0], commandIds[1]);
diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts
index b597574228a..66cecbf4ede 100644
--- a/apps/server/src/mcp/OrchestratorMcpService.ts
+++ b/apps/server/src/mcp/OrchestratorMcpService.ts
@@ -1093,11 +1093,9 @@ const make = Effect.gen(function* () {
// Task waits read on every wake of a parent that may hold thousands of
// tasks, so they read only the task they watch and what its status needs.
- const loadTaskParent = (threadId: ThreadId, taskId: NodeId) =>
+ const loadTaskParent = (threadId: ThreadId) =>
threadManagement
- .getThreadRecords(threadId, ["runs", "subagents", "contextTransfers"], {
- subagentIds: [taskId],
- })
+ .getThreadRecords(threadId, ["runs", "contextTransfers"])
.pipe(
Effect.mapError((error) =>
failure(
@@ -1472,8 +1470,7 @@ const make = Effect.gen(function* () {
acknowledgementOperation = "task-status-acknowledge",
): Effect.fn.Return<OrchestratorMcpDelegateTaskResult, OrchestratorMcpFailure> {
yield* requireCapability(scope);
- const parentProjection = yield* loadTaskParent(scope.thread.threadId, taskId);
- const task = parentProjection.subagents.find(
+ const task = (yield* loadTasks(scope.thread.threadId, [taskId])).find(
(candidate) =>
candidate.id === taskId &&
candidate.origin === "app_owned" &&
@@ -1488,6 +1485,7 @@ const make = Effect.gen(function* () {
if (task.childThreadId === null) {
return yield* readRemoteTask(scope, task, waitTimedOut, acknowledgeTerminal);
}
+ const parentProjection = yield* loadTaskParent(scope.thread.threadId);
const childControls = yield* threadManagement
.getThreadRecords(
task.childThreadId,
diff --git a/apps/server/src/peer/RemoteDelegation.test.ts b/apps/server/src/peer/RemoteDelegation.test.ts
index 26a697b58f4..00b7b5d6f69 100644
--- a/apps/server/src/peer/RemoteDelegation.test.ts
+++ b/apps/server/src/peer/RemoteDelegation.test.ts
@@ -29,8 +29,8 @@ import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
-import * as Tracer from "effect/Tracer";
import * as SubscriptionRef from "effect/SubscriptionRef";
+import * as Tracer from "effect/Tracer";
import type * as SqlClient from "effect/sql/SqlClient";
import { McpSchema, McpServer } from "effect/ai";
@@ -829,6 +829,42 @@ it.effect("a task delegated to the box wakes the laptop's parent when it ends th
expect(retried.taskId).toBe(delegated.taskId);
expect((yield* a.orchestrator.getThreadProjection(parentThreadId)).subagents).toHaveLength(1);
+ const historyReads: Array<string> = [];
+ const historyTracer = Tracer.make({
+ span(options) {
+ const span = new Tracer.NativeSpan(options);
+ const end = span.end.bind(span);
+ span.end = (endTime, exit) => {
+ end(endTime, exit);
+ const query = span.attributes.get("db.query.text");
+ if (
+ typeof query === "string" &&
+ /\bSELECT\s+payload_json\s+FROM orchestration_v2_projection_(?:runs|context_transfers)\b/i.test(
+ query,
+ )
+ ) {
+ historyReads.push(query);
+ }
+ };
+ return span;
+ },
+ });
+ const running = yield* a
+ .call("task_status", { taskId: delegated.taskId })
+ .pipe(Effect.withTracer(historyTracer));
+ expect(running.structuredContent).toMatchObject({
+ taskId: delegated.taskId,
+ childThreadId: null,
+ remoteChild: delegated.remoteChild,
+ status: "running",
+ workState: "working",
+ summary: null,
+ hasPendingQuestion: false,
+ pendingApproval: null,
+ });
+ // A running task there needs its local row, not the parent's run or transfer history.
+ expect(historyReads).toEqual([]);
+
yield* Ref.set(thread.reply, "All 412 tests pass on the box.");
yield* Ref.set(thread.status, "completed");
yield* Deferred.succeed(thread.finished, undefined);