Skip to content

Commit 550c37e

Browse files
t3dotggclaude
andcommitted
perf(server): commands no longer decode a busy thread's finished tool nodes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 742c229 commit 550c37e

3 files changed

Lines changed: 109 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
940940

941941
// Command decisions need control records, not historical assistant/tool output.
942942
// Handoff preparation explicitly reads its history after choosing a strategy.
943+
// This runs inside the write transaction for every command, so finished leaf
944+
// nodes of finished runs stay unread: busy threads hold thousands of them.
943945
const readCommandProjection = (threadId: ThreadId) =>
944946
projectionStore
945947
.getThreadRecords(
@@ -958,7 +960,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
958960
"checkpointScopes",
959961
"contextTransfers",
960962
],
961-
{ turnItemTypes: ["user_message", "error"], messageRoles: ["user"] },
963+
{
964+
turnItemTypes: ["user_message", "error"],
965+
messageRoles: ["user"],
966+
omitSettledLeafNodes: true,
967+
},
962968
)
963969
.pipe(
964970
Effect.map((records): OrchestrationV2ThreadProjection => ({

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,88 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
657657
assert.deepEqual(recovery?.restartCancelledBackgroundWork, work);
658658
}),
659659
);
660+
it.effect("command reads skip finished leaf nodes of finished runs", () =>
661+
Effect.gen(function* () {
662+
const store = yield* ProjectionStore.ProjectionStoreV2;
663+
const now = yield* DateTime.now;
664+
const threadId = yield* addRolledBackRecoveryCandidate("settled-leaf-nodes");
665+
const settledRun = (yield* store.getThreadProjection(threadId)).runs[0]!;
666+
const activeRun = {
667+
...settledRun,
668+
id: RunId.make("run:settled-leaf-nodes:active"),
669+
ordinal: 2,
670+
userMessageId: MessageId.make("message:settled-leaf-nodes:active"),
671+
rootNodeId: NodeId.make("node:settled-leaf-nodes:active-root"),
672+
status: "running" as const,
673+
completedAt: null,
674+
};
675+
yield* store.apply({
676+
id: EventId.make("event:settled-leaf-nodes:active-run"),
677+
type: "run.created",
678+
threadId,
679+
runId: activeRun.id,
680+
nodeId: activeRun.rootNodeId,
681+
driver,
682+
providerInstanceId,
683+
occurredAt: now,
684+
payload: activeRun,
685+
});
686+
const nodes = [
687+
["settled-root", settledRun, "root_turn", "completed"],
688+
["settled-tool", settledRun, "tool_call", "completed"],
689+
["settled-message", settledRun, "assistant_message", "failed"],
690+
["settled-reasoning", settledRun, "reasoning", "cancelled"],
691+
["settled-subagent", settledRun, "subagent", "completed"],
692+
["settled-run-live-tool", settledRun, "tool_call", "running"],
693+
["active-run-done-tool", activeRun, "tool_call", "completed"],
694+
] as const;
695+
for (const [name, run, kind, status] of nodes) {
696+
const id = NodeId.make(`node:settled-leaf-nodes:${name}`);
697+
yield* store.apply({
698+
id: EventId.make(`event:settled-leaf-nodes:${name}`),
699+
type: "node.updated",
700+
threadId,
701+
runId: run.id,
702+
nodeId: id,
703+
driver,
704+
occurredAt: now,
705+
payload: {
706+
id,
707+
threadId,
708+
runId: run.id,
709+
rootNodeId: kind === "root_turn" ? id : run.rootNodeId!,
710+
parentNodeId: kind === "root_turn" ? null : run.rootNodeId,
711+
kind,
712+
status,
713+
countsForRun: kind === "root_turn",
714+
providerThreadId: null,
715+
providerTurnId: null,
716+
nativeItemRef: null,
717+
runtimeRequestId: null,
718+
checkpointScopeId: null,
719+
startedAt: now,
720+
completedAt: status === "running" ? null : now,
721+
},
722+
});
723+
}
724+
const nodeNames = (records: { readonly nodes: ReadonlyArray<{ readonly id: string }> }) =>
725+
records.nodes
726+
.map((node) => node.id.replace("node:settled-leaf-nodes:", ""))
727+
.filter((name) => nodes.some(([candidate]) => candidate === name))
728+
.toSorted();
729+
730+
assert.deepEqual(
731+
nodeNames(
732+
yield* store.getThreadRecords(threadId, ["nodes"], { omitSettledLeafNodes: true }),
733+
),
734+
["active-run-done-tool", "settled-root", "settled-run-live-tool", "settled-subagent"],
735+
);
736+
assert.deepEqual(
737+
nodeNames(yield* store.getThreadRecords(threadId, ["nodes"])),
738+
nodes.map(([name]) => name).toSorted(),
739+
);
740+
}),
741+
);
660742
it.effect("limits turn-start history to the requested runs, including an empty selection", () =>
661743
Effect.gen(function* () {
662744
const store = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ export interface ProjectionRecordFilter {
304304
readonly runIds?: ReadonlyArray<RunId>;
305305
readonly turnItemTypes?: ReadonlyArray<OrchestrationV2TurnItem["type"]>;
306306
readonly turnItemStatuses?: ReadonlyArray<OrchestrationV2TurnItem["status"]>;
307+
// Skips finished tool call, assistant message and reasoning nodes of finished
308+
// runs. Command decisions only read root, subagent and active-run nodes.
309+
readonly omitSettledLeafNodes?: boolean;
307310
}
308311
export type ProjectionRecordField = Exclude<
309312
keyof OrchestrationV2ThreadProjection,
@@ -3069,9 +3072,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30693072
: window === undefined
30703073
? sql<PayloadRow>`
30713074
SELECT payload_json
3072-
FROM orchestration_v2_projection_nodes
3073-
WHERE thread_id = ${threadId}
3074-
ORDER BY COALESCE(started_at, ''), node_id ASC
3075+
FROM orchestration_v2_projection_nodes AS node
3076+
WHERE node.thread_id = ${threadId}
3077+
${
3078+
filter?.omitSettledLeafNodes === true
3079+
? sql`AND NOT (
3080+
node.kind IN ('tool_call', 'assistant_message', 'reasoning')
3081+
AND node.status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3082+
AND node.run_id IS NOT NULL
3083+
AND NOT EXISTS (
3084+
SELECT 1 FROM orchestration_v2_projection_runs AS run
3085+
WHERE run.run_id = node.run_id
3086+
AND run.status IN ('preparing', 'queued', 'starting', 'running', 'waiting')
3087+
)
3088+
)`
3089+
: sql``
3090+
}
3091+
ORDER BY COALESCE(node.started_at, ''), node.node_id ASC
30753092
`
30763093
: sql<PayloadRow & { readonly position: number }>`
30773094
-- Keep retained IDs outermost so SQLite looks up each node by its

0 commit comments

Comments
 (0)