Skip to content

Commit 364424a

Browse files
t3dotggclaude
andcommitted
fix(server): memory projection store honors omitSettledLeafNodes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 550c37e commit 364424a

2 files changed

Lines changed: 119 additions & 81 deletions

File tree

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

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,89 @@ const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
382382
]);
383383
});
384384

385+
const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
386+
const store = yield* ProjectionStore.ProjectionStoreV2;
387+
const now = yield* DateTime.now;
388+
const threadId = yield* addRolledBackRecoveryCandidate("settled-leaf-nodes");
389+
const settledRun = (yield* store.getThreadProjection(threadId)).runs[0]!;
390+
const activeRun = {
391+
...settledRun,
392+
id: RunId.make("run:settled-leaf-nodes:active"),
393+
ordinal: 2,
394+
userMessageId: MessageId.make("message:settled-leaf-nodes:active"),
395+
rootNodeId: NodeId.make("node:settled-leaf-nodes:active-root"),
396+
status: "running" as const,
397+
completedAt: null,
398+
};
399+
yield* store.apply({
400+
id: EventId.make("event:settled-leaf-nodes:active-run"),
401+
type: "run.created",
402+
threadId,
403+
runId: activeRun.id,
404+
nodeId: activeRun.rootNodeId,
405+
driver,
406+
providerInstanceId,
407+
occurredAt: now,
408+
payload: activeRun,
409+
});
410+
const nodes = [
411+
["settled-root", settledRun, "root_turn", "completed"],
412+
["settled-tool", settledRun, "tool_call", "completed"],
413+
["settled-message", settledRun, "assistant_message", "failed"],
414+
["settled-reasoning", settledRun, "reasoning", "cancelled"],
415+
["settled-subagent", settledRun, "subagent", "completed"],
416+
["settled-run-live-tool", settledRun, "tool_call", "running"],
417+
["active-run-done-tool", activeRun, "tool_call", "completed"],
418+
] as const;
419+
for (const [name, run, kind, status] of nodes) {
420+
const id = NodeId.make(`node:settled-leaf-nodes:${name}`);
421+
yield* store.apply({
422+
id: EventId.make(`event:settled-leaf-nodes:${name}`),
423+
type: "node.updated",
424+
threadId,
425+
runId: run.id,
426+
nodeId: id,
427+
driver,
428+
occurredAt: now,
429+
payload: {
430+
id,
431+
threadId,
432+
runId: run.id,
433+
rootNodeId: kind === "root_turn" ? id : run.rootNodeId!,
434+
parentNodeId: kind === "root_turn" ? null : run.rootNodeId,
435+
kind,
436+
status,
437+
countsForRun: kind === "root_turn",
438+
providerThreadId: null,
439+
providerTurnId: null,
440+
nativeItemRef: null,
441+
runtimeRequestId: null,
442+
checkpointScopeId: null,
443+
startedAt: now,
444+
completedAt: status === "running" ? null : now,
445+
},
446+
});
447+
}
448+
const nodeNames = (records: { readonly nodes: ReadonlyArray<{ readonly id: string }> }) =>
449+
records.nodes
450+
.map((node) => node.id.replace("node:settled-leaf-nodes:", ""))
451+
.filter((name) => nodes.some(([candidate]) => candidate === name))
452+
.toSorted();
453+
454+
assert.deepEqual(
455+
nodeNames(yield* store.getThreadRecords(threadId, ["nodes"], { omitSettledLeafNodes: true })),
456+
["active-run-done-tool", "settled-root", "settled-run-live-tool", "settled-subagent"],
457+
);
458+
assert.deepEqual(
459+
nodeNames(yield* store.getThreadRecords(threadId, ["nodes"])),
460+
nodes.map(([name]) => name).toSorted(),
461+
);
462+
});
463+
464+
it.effect("memory command reads skip finished leaf nodes of finished runs", () =>
465+
commandReadsSkipSettledLeafNodes.pipe(Effect.provide(ProjectionStore.layerMemory)),
466+
);
467+
385468
it.effect("memory worktree path lookup reads only active threads", () =>
386469
threadWorktreePathsReadActiveThreads.pipe(Effect.provide(ProjectionStore.layerMemory)),
387470
);
@@ -657,87 +740,9 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
657740
assert.deepEqual(recovery?.restartCancelledBackgroundWork, work);
658741
}),
659742
);
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-
}),
743+
it.effect(
744+
"command reads skip finished leaf nodes of finished runs",
745+
() => commandReadsSkipSettledLeafNodes,
741746
);
742747
it.effect("limits turn-start history to the requested runs, including an empty selection", () =>
743748
Effect.gen(function* () {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,35 @@ export interface ProjectionRecordFilter {
308308
// runs. Command decisions only read root, subagent and active-run nodes.
309309
readonly omitSettledLeafNodes?: boolean;
310310
}
311+
312+
type ProjectionNode = OrchestrationV2ThreadProjection["nodes"][number];
313+
type ProjectionRun = OrchestrationV2ThreadProjection["runs"][number];
314+
const settledLeafNodeKinds: ReadonlySet<ProjectionNode["kind"]> = new Set([
315+
"tool_call",
316+
"assistant_message",
317+
"reasoning",
318+
]);
319+
const settledNodeStatuses: ReadonlySet<ProjectionNode["status"]> = new Set([
320+
"completed",
321+
"interrupted",
322+
"failed",
323+
"cancelled",
324+
"rolled_back",
325+
]);
326+
const activeRunStatuses: ReadonlySet<ProjectionRun["status"]> = new Set([
327+
"preparing",
328+
"queued",
329+
"starting",
330+
"running",
331+
"waiting",
332+
]);
333+
334+
// In-memory twin of the omitSettledLeafNodes SQL clause in readCanonicalProjection.
335+
const isSettledLeafNode = (node: ProjectionNode, runs: ReadonlyArray<ProjectionRun>) =>
336+
settledLeafNodeKinds.has(node.kind) &&
337+
settledNodeStatuses.has(node.status) &&
338+
node.runId !== null &&
339+
!runs.some((run) => run.id === node.runId && activeRunStatuses.has(run.status));
311340
export type ProjectionRecordField = Exclude<
312341
keyof OrchestrationV2ThreadProjection,
313342
"thread" | "updatedAt" | "visibleTurnItems"
@@ -6440,6 +6469,10 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
64406469
filter.turnItemStatuses.includes(row.status)) &&
64416470
(filter?.turnItemRunId === undefined || filter.turnItemRunId === row.runId),
64426471
),
6472+
nodes:
6473+
filter?.omitSettledLeafNodes === true
6474+
? projection.nodes.filter((node) => !isSettledLeafNode(node, projection.runs))
6475+
: projection.nodes,
64436476
};
64446477
return Object.fromEntries([
64456478
["thread", projection.thread],

0 commit comments

Comments
 (0)