Skip to content

Commit 17c0954

Browse files
navidnadaliclaude
andcommitted
perf(server): finalize reads only the task's node, card and delivery message
The terminal-result read loaded every parent node, subagent card and user message to use one of each. It now asks for the task's node and card by id, and reads the delivery message it may join by id, with that delivery's other tasks. The status mirror for a follow-up run reads the node and card the same way. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f126ef3 commit 17c0954

2 files changed

Lines changed: 219 additions & 18 deletions

File tree

‎apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts‎

Lines changed: 199 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,6 +2311,108 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
23112311
},
23122312
);
23132313

2314+
// The run-update listener mirrors a follow-up's status onto its task outside any test fiber, so
2315+
// its statements are recorded from the layer down, for one parent thread at a time.
2316+
let recordReadsOf: ThreadId | undefined;
2317+
const recordedReads: Array<string> = [];
2318+
const layerRecordingReads = Layer.succeed(Statement.CurrentTransformer, (statement) => {
2319+
const [query, params] = statement.compile();
2320+
if (recordReadsOf !== undefined && params.includes(recordReadsOf)) recordedReads.push(query);
2321+
return Effect.succeed(statement);
2322+
});
2323+
2324+
it.layer(
2325+
Layer.merge(layerTest, ProviderContinuationRequests.layer).pipe(
2326+
Layer.provide(layerRecordingReads),
2327+
),
2328+
)("subagent follow-up status", (it) => {
2329+
it.effect("mirrors a follow-up's status reading only its task's node and card", () =>
2330+
Effect.gen(function* () {
2331+
const orchestrator = yield* Orchestrator.OrchestratorV2;
2332+
const eventSink = yield* EventSink.EventSinkV2;
2333+
const now = yield* DateTime.now;
2334+
const parentThreadId = ThreadId.make("thread:narrow-status-parent");
2335+
const projectId = ProjectId.make("project:narrow-status");
2336+
const parentRunId = RunId.make("run:narrow-status-parent");
2337+
const rootNodeId = NodeId.make("node:narrow-status-parent");
2338+
yield* seedParentWithTerminalTask({
2339+
threadId: parentThreadId,
2340+
projectId,
2341+
runId: parentRunId,
2342+
rootNodeId,
2343+
taskId: NodeId.make("node:narrow-status-seed"),
2344+
deliveryState: "acknowledged",
2345+
now,
2346+
});
2347+
const child = yield* seedRestartCancelledChild({
2348+
parentThreadId,
2349+
projectId,
2350+
parentRunId,
2351+
rootNodeId,
2352+
name: "narrow-status-child",
2353+
completionWake: "always",
2354+
continuationPending: false,
2355+
runStatus: "completed",
2356+
now,
2357+
});
2358+
yield* orchestrator.recoverDelegatedTasks;
2359+
for (const name of ["active", "queued"]) {
2360+
yield* orchestrator.dispatch({
2361+
type: "message.dispatch",
2362+
createdBy: "agent",
2363+
creationSource: "mcp",
2364+
commandId: CommandId.make(`command:narrow-status:${name}`),
2365+
threadId: child.childThreadId,
2366+
senderThreadId: parentThreadId,
2367+
messageId: MessageId.make(`message:narrow-status:${name}`),
2368+
text: name,
2369+
attachments: [],
2370+
dispatchMode: { type: "queue_after_active" },
2371+
});
2372+
}
2373+
const queued = (yield* orchestrator.getThreadProjection(child.childThreadId)).runs.find(
2374+
(run) => run.ordinal === 3,
2375+
)!;
2376+
assert.equal(queued.status, "queued");
2377+
const afterSequence = yield* eventSink.latestSequence();
2378+
recordReadsOf = parentThreadId;
2379+
yield* orchestrator.dispatch({
2380+
type: "thread.stop",
2381+
threadId: child.childThreadId,
2382+
commandId: CommandId.make("command:narrow-status:stop"),
2383+
});
2384+
yield* eventSink.stream({ afterSequence, eventType: "subagent.updated" }).pipe(
2385+
Stream.filter(
2386+
(stored) =>
2387+
stored.event.type === "subagent.updated" &&
2388+
stored.event.payload.id === queued.delegatedTaskId &&
2389+
stored.event.payload.status === "idle",
2390+
),
2391+
Stream.take(1),
2392+
Stream.runHead,
2393+
);
2394+
recordReadsOf = undefined;
2395+
const paused = yield* orchestrator.getThreadProjection(parentThreadId);
2396+
assert.equal(paused.nodes.find((row) => row.id === queued.delegatedTaskId)?.status, "idle");
2397+
assert.equal(
2398+
paused.turnItems.find(
2399+
(row) => row.type === "subagent" && row.subagentId === queued.delegatedTaskId,
2400+
)?.status,
2401+
"idle",
2402+
);
2403+
const nodeReads = recordedReads.filter((query) =>
2404+
/SELECT payload_json\s+FROM orchestration_v2_projection_(nodes|turn_items)\b/.test(query),
2405+
);
2406+
assert.isNotEmpty(nodeReads);
2407+
assert.deepEqual(
2408+
nodeReads.filter((query) => !/AND (node\.)?node_id IN/.test(query)),
2409+
[],
2410+
"a parent node or card read is not narrowed",
2411+
);
2412+
}),
2413+
);
2414+
});
2415+
23142416
it.layer(layerTest)("delegated tasks across a server restart", (it) => {
23152417
it.effect("queues a child result without creating a provider history handoff", () =>
23162418
Effect.gen(function* () {
@@ -2404,11 +2506,12 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24042506
}),
24052507
);
24062508

2407-
// Finalizing a child reads, from its parent, only that child's tasks and the transfers it sent,
2408-
// and the other tasks of a delivery it joins by id.
2509+
// Finalizing a child reads, from its parent, only that child's task, node, card and the transfers
2510+
// it sent, and the message and other tasks of a delivery it joins by id.
24092511
it.effect("finalizing a delegated child reads only that child's records from its parent", () =>
24102512
Effect.gen(function* () {
24112513
const orchestrator = yield* Orchestrator.OrchestratorV2;
2514+
const sink = yield* EventSink.EventSinkV2;
24122515
const now = yield* DateTime.now;
24132516
const threadId = ThreadId.make("thread:narrow-finalize-parent");
24142517
const projectId = ProjectId.make("project:narrow-finalize");
@@ -2450,12 +2553,81 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24502553
runStatus: "completed",
24512554
now,
24522555
});
2556+
const cards = [
2557+
{ taskId: siblingTaskId, status: "completed" as const, ordinal: 100 },
2558+
{ taskId: child.taskId, status: "running" as const, ordinal: 101 },
2559+
];
2560+
yield* sink.write({
2561+
commandId: CommandId.make("command:narrow-finalize-cards"),
2562+
events: cards.flatMap(({ taskId, status, ordinal }) => [
2563+
{
2564+
id: EventId.make(`event:narrow-finalize-node:${taskId}`),
2565+
type: "node.updated" as const,
2566+
threadId,
2567+
runId,
2568+
nodeId: taskId,
2569+
occurredAt: now,
2570+
payload: {
2571+
id: taskId,
2572+
threadId,
2573+
runId,
2574+
parentNodeId: rootNodeId,
2575+
rootNodeId,
2576+
kind: "subagent" as const,
2577+
status,
2578+
countsForRun: false,
2579+
providerThreadId: null,
2580+
providerTurnId: null,
2581+
nativeItemRef: null,
2582+
runtimeRequestId: null,
2583+
checkpointScopeId: null,
2584+
startedAt: now,
2585+
completedAt: status === "completed" ? now : null,
2586+
},
2587+
},
2588+
{
2589+
id: EventId.make(`event:narrow-finalize-card:${taskId}`),
2590+
type: "turn-item.updated" as const,
2591+
threadId,
2592+
runId,
2593+
nodeId: taskId,
2594+
occurredAt: now,
2595+
payload: {
2596+
id: TurnItemId.make(`turn-item:narrow-finalize:${taskId}`),
2597+
type: "subagent" as const,
2598+
threadId,
2599+
runId,
2600+
nodeId: taskId,
2601+
providerThreadId: null,
2602+
providerTurnId: null,
2603+
nativeItemRef: null,
2604+
parentItemId: null,
2605+
ordinal,
2606+
status,
2607+
title: null,
2608+
startedAt: now,
2609+
completedAt: status === "completed" ? now : null,
2610+
updatedAt: now,
2611+
subagentId: taskId,
2612+
origin: "app_owned" as const,
2613+
driver,
2614+
providerInstanceId: modelSelection.instanceId,
2615+
childThreadId: taskId === child.taskId ? child.childThreadId : null,
2616+
prompt: `Run ${taskId}.`,
2617+
result: status === "completed" ? "child finished" : null,
2618+
},
2619+
},
2620+
]),
2621+
});
24532622
const parentReads: Array<string> = [];
24542623
const observe: Statement.Transformer = (statement) => {
24552624
const [query, params] = statement.compile();
24562625
if (
24572626
params.includes(threadId) &&
2458-
/FROM orchestration_v2_projection_(subagents|context_transfers)\b/.test(query)
2627+
(/FROM orchestration_v2_projection_(subagents|context_transfers)\b/.test(query) ||
2628+
/SELECT payload_json\s+FROM orchestration_v2_projection_(nodes|turn_items|messages)\b/.test(
2629+
query,
2630+
))
24592631
) {
24602632
parentReads.push(query);
24612633
}
@@ -2486,6 +2658,30 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24862658
[],
24872659
"a parent context-transfer read is not narrowed",
24882660
);
2661+
// Nodes and cards by the task's node, messages by id.
2662+
for (const [table, narrowed] of [
2663+
["nodes", /AND node\.node_id IN/],
2664+
["turn_items", /AND node_id IN/],
2665+
["messages", /AND message_id IN/],
2666+
] as const) {
2667+
const reads = parentReads.filter((query) =>
2668+
query.includes(`FROM orchestration_v2_projection_${table}`),
2669+
);
2670+
assert.isNotEmpty(reads, table);
2671+
assert.deepEqual(
2672+
reads.filter((query) => !narrowed.test(query)),
2673+
[],
2674+
`a parent ${table} read is not narrowed`,
2675+
);
2676+
}
2677+
// The child's node and card still end with its task.
2678+
assert.equal(projection.nodes.find((row) => row.id === child.taskId)?.status, "completed");
2679+
assert.equal(
2680+
projection.turnItems.find(
2681+
(row) => row.type === "subagent" && row.subagentId === child.taskId,
2682+
)?.status,
2683+
"completed",
2684+
);
24892685
// The delivery the child joins still names the sibling it was opened for.
24902686
const message = projection.messages.find((candidate) => candidate.id === messageId);
24912687
assert.deepEqual(message?.delegatedCompletion?.taskIds, [siblingTaskId, child.taskId]);

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12477,7 +12477,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1247712477
const parent = yield* projectionStore.getThreadRecords(
1247812478
childControls.thread.lineage.parentThreadId,
1247912479
["nodes", "turnItems"],
12480-
{ turnItemTypes: ["subagent"] },
12480+
{ turnItemTypes: ["subagent"], nodeIds: [task.id], turnItemNodeIds: [task.id] },
1248112481
);
1248212482
const now = yield* DateTime.now;
1248312483
const scope = {
@@ -12544,15 +12544,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1254412544
turnItems: childResult.turnItems,
1254512545
};
1254612546
const parentThreadId = childControls.thread.lineage.parentThreadId;
12547-
// Only this child's tasks and result transfers; a delivery's other tasks are read by id below.
12547+
// Only this child's tasks, node, card and result transfers; a delivery is read by id below.
1254812548
const parentProjection = yield* projectionStore.getThreadRecords(
1254912549
parentThreadId,
12550-
["runs", "messages", "subagents", "nodes", "turnItems", "contextTransfers"],
12550+
["runs", "subagents", "nodes", "turnItems", "contextTransfers"],
1255112551
{
1255212552
turnItemTypes: ["subagent"],
12553-
messageRoles: ["user"],
1255412553
subagentChildThreadIds: [childThreadId],
1255512554
contextTransferSourceThreadIds: [childThreadId],
12555+
nodeIds: [options.task.id],
12556+
turnItemNodeIds: [options.task.id],
1255612557
},
1255712558
);
1255812559
const task = parentProjection.subagents.find(
@@ -12594,20 +12595,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1259412595
completedAt: now,
1259512596
updatedAt: now,
1259612597
};
12597-
// The delivery this result may join names its other tasks in its text.
12598-
const deliveryTaskIds = (parentRun?.delegatedCompletion?.delivery?.taskIds ?? []).filter(
12599-
(taskId) => !parentProjection.subagents.some((candidate) => candidate.id === taskId),
12600-
);
12601-
const deliveryTasks =
12602-
deliveryTaskIds.length === 0
12603-
? []
12604-
: (yield* projectionStore.getThreadRecords(parentThreadId, ["subagents"], {
12605-
subagentIds: deliveryTaskIds,
12606-
})).subagents;
12598+
// The delivery this result may join: its message, and the other tasks its text names.
12599+
const delivery = parentRun?.delegatedCompletion?.delivery ?? null;
12600+
const deliveryRecords =
12601+
delivery === null
12602+
? { messages: [], subagents: [] }
12603+
: yield* projectionStore.getThreadRecords(parentThreadId, ["messages", "subagents"], {
12604+
messageRoles: ["user"],
12605+
messageIds: [delivery.messageId],
12606+
subagentIds: delivery.taskIds.filter(
12607+
(taskId) =>
12608+
!parentProjection.subagents.some((candidate) => candidate.id === taskId),
12609+
),
12610+
});
1260712611
const completionPlan = yield* planDelegatedCompletionDelivery({
1260812612
parentProjection: {
1260912613
...parentProjection,
12610-
subagents: [...parentProjection.subagents, ...deliveryTasks],
12614+
messages: deliveryRecords.messages,
12615+
subagents: [...parentProjection.subagents, ...deliveryRecords.subagents],
1261112616
},
1261212617
parentRun,
1261312618
task,

0 commit comments

Comments
 (0)