Skip to content

Commit f126ef3

Browse files
navidnadaliclaude
andcommitted
perf(server): the terminal finalize read is narrowed to the finishing child
Finalizing a finished delegated child read the parent a second time to build its result and delivery, and that read still loaded every task, every transfer and every provider thread of the parent. It now asks for the child's tasks and the transfers it sent, reads the other tasks of a delivery it joins by id, and no longer loads provider threads and turns it does not use. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 71fc57a commit f126ef3

2 files changed

Lines changed: 67 additions & 34 deletions

File tree

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

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,8 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24042404
}),
24052405
);
24062406

2407-
// Finalizing a child reads, from its parent, only that child's tasks and the transfers it sent.
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.
24082409
it.effect("finalizing a delegated child reads only that child's records from its parent", () =>
24092410
Effect.gen(function* () {
24102411
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -2413,15 +2414,31 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24132414
const projectId = ProjectId.make("project:narrow-finalize");
24142415
const runId = RunId.make("run:narrow-finalize-parent");
24152416
const rootNodeId = NodeId.make("node:narrow-finalize-parent");
2417+
const siblingTaskId = NodeId.make("node:narrow-finalize-seed");
2418+
const messageId = MessageId.make(`message:delegated-delivery:${threadId}`);
24162419
yield* seedParentWithTerminalTask({
24172420
threadId,
24182421
projectId,
24192422
runId,
24202423
rootNodeId,
2421-
taskId: NodeId.make("node:narrow-finalize-seed"),
2422-
deliveryState: "acknowledged",
2424+
taskId: siblingTaskId,
2425+
deliveryState: "claimed",
2426+
completionWake: "always",
2427+
deliveryTaskIds: [siblingTaskId],
24232428
now,
24242429
});
2430+
yield* orchestrator.dispatch({
2431+
type: "message.dispatch",
2432+
commandId: CommandId.make("command:narrow-finalize-delivery"),
2433+
threadId,
2434+
messageId,
2435+
text: `Delegated task ${siblingTaskId} reached a terminal state.`,
2436+
attachments: [],
2437+
dispatchMode: { type: "queue_after_active" },
2438+
createdBy: "agent",
2439+
creationSource: "server",
2440+
delegatedCompletion: { parentRunId: runId, generation: 1, taskIds: [siblingTaskId] },
2441+
});
24252442
const child = yield* seedRestartCancelledChild({
24262443
parentThreadId: threadId,
24272444
projectId,
@@ -2447,27 +2464,33 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
24472464
yield* orchestrator.recoverDelegatedTasks.pipe(
24482465
Effect.provideService(Statement.CurrentTransformer, observe),
24492466
);
2450-
const task = (yield* orchestrator.getThreadProjection(threadId)).subagents.find(
2451-
(candidate) => candidate.id === child.taskId,
2452-
);
2467+
const projection = yield* orchestrator.getThreadProjection(threadId);
2468+
const task = projection.subagents.find((candidate) => candidate.id === child.taskId);
24532469
assert.equal(task?.status, "completed");
2454-
// The finalize read names the child: its tasks by child thread, its transfers by source.
2455-
assert.isTrue(
2456-
parentReads.some(
2457-
(query) =>
2458-
query.includes("FROM orchestration_v2_projection_subagents") &&
2459-
/AND child_thread_id (=|IN)/.test(query),
2460-
),
2461-
"the parent's subagent read is not narrowed to the child",
2470+
// Every finalize read names its rows: tasks by child thread or id, transfers by source.
2471+
const subagentReads = parentReads.filter((query) =>
2472+
query.includes("FROM orchestration_v2_projection_subagents"),
24622473
);
2463-
assert.isTrue(
2464-
parentReads.some(
2465-
(query) =>
2466-
query.includes("FROM orchestration_v2_projection_context_transfers") &&
2467-
/AND source_thread_id (=|IN)/.test(query),
2468-
),
2469-
"the parent's context-transfer read is not narrowed to the child",
2474+
const transferReads = parentReads.filter((query) =>
2475+
query.includes("FROM orchestration_v2_projection_context_transfers"),
2476+
);
2477+
assert.isNotEmpty(subagentReads);
2478+
assert.isNotEmpty(transferReads);
2479+
assert.deepEqual(
2480+
subagentReads.filter((query) => !/AND (child_thread_id|subagent_id) (=|IN)/.test(query)),
2481+
[],
2482+
"a parent subagent read is not narrowed",
24702483
);
2484+
assert.deepEqual(
2485+
transferReads.filter((query) => !/AND source_thread_id (=|IN)/.test(query)),
2486+
[],
2487+
"a parent context-transfer read is not narrowed",
2488+
);
2489+
// The delivery the child joins still names the sibling it was opened for.
2490+
const message = projection.messages.find((candidate) => candidate.id === messageId);
2491+
assert.deepEqual(message?.delegatedCompletion?.taskIds, [siblingTaskId, child.taskId]);
2492+
assert.include(message?.text ?? "", `Delegated task ${siblingTaskId} [completed]`);
2493+
assert.include(message?.text ?? "", `Delegated task ${child.taskId}`);
24712494
}),
24722495
);
24732496

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12252,7 +12252,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1225212252
)(function* (input: {
1225312253
readonly parentProjection: Pick<
1225412254
OrchestrationV2ThreadProjection,
12255-
"thread" | "messages" | "runs" | "subagents" | "providerTurns"
12255+
"thread" | "messages" | "runs" | "subagents"
1225612256
>;
1225712257
readonly parentRun: OrchestrationV2Run | undefined;
1225812258
readonly task: OrchestrationV2Subagent;
@@ -12544,19 +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.
1254712548
const parentProjection = yield* projectionStore.getThreadRecords(
1254812549
parentThreadId,
12549-
[
12550-
"runs",
12551-
"messages",
12552-
"subagents",
12553-
"providerThreads",
12554-
"providerTurns",
12555-
"nodes",
12556-
"turnItems",
12557-
"contextTransfers",
12558-
],
12559-
{ turnItemTypes: ["subagent"], messageRoles: ["user"] },
12550+
["runs", "messages", "subagents", "nodes", "turnItems", "contextTransfers"],
12551+
{
12552+
turnItemTypes: ["subagent"],
12553+
messageRoles: ["user"],
12554+
subagentChildThreadIds: [childThreadId],
12555+
contextTransferSourceThreadIds: [childThreadId],
12556+
},
1256012557
);
1256112558
const task = parentProjection.subagents.find(
1256212559
(candidate) =>
@@ -12597,8 +12594,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1259712594
completedAt: now,
1259812595
updatedAt: now,
1259912596
};
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;
1260012607
const completionPlan = yield* planDelegatedCompletionDelivery({
12601-
parentProjection,
12608+
parentProjection: {
12609+
...parentProjection,
12610+
subagents: [...parentProjection.subagents, ...deliveryTasks],
12611+
},
1260212612
parentRun,
1260312613
task,
1260412614
updatedTask,

0 commit comments

Comments
 (0)