Skip to content

Commit 7bab847

Browse files
navidnadaliclaude
andcommitted
perf(server): a card read by node seeks the node index
A turn-item read filtered by node ids still walked every turn item of the thread through the thread index, so finalizing a subagent visited every card of its parent to return one. It now seeks the node index, as the other id-filtered reads already do. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 26b902d commit 7bab847

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,62 @@ const parentRecordsNarrowToOneChild = Effect.gen(function* () {
22122212
assert.include(plan[0] ?? "", `USING INDEX ${index}`);
22132213
assert.notOk(plan.some((detail) => detail.startsWith("SCAN orchestration_v2_projection")));
22142214
}
2215+
// The finishing task's card is sought by its node, not picked from every card of the parent.
2216+
for (const [ordinal, name] of ["child-early", "sibling", "child-late"].entries()) {
2217+
const nodeId = NodeId.make(`node:${name}`);
2218+
yield* store.apply({
2219+
id: EventId.make(`event:card:${name}`),
2220+
type: "turn-item.updated",
2221+
threadId: parent,
2222+
nodeId,
2223+
driver,
2224+
occurredAt: recordsAt(ordinal),
2225+
payload: {
2226+
id: TurnItemId.make(`item:card:${name}`),
2227+
type: "subagent",
2228+
threadId: parent,
2229+
runId: null,
2230+
nodeId,
2231+
providerThreadId: null,
2232+
providerTurnId: null,
2233+
nativeItemRef: null,
2234+
parentItemId: null,
2235+
ordinal,
2236+
status: "completed",
2237+
title: name,
2238+
startedAt: recordsAt(ordinal),
2239+
completedAt: recordsAt(9),
2240+
updatedAt: recordsAt(9),
2241+
subagentId: nodeId,
2242+
origin: "app_owned",
2243+
driver,
2244+
providerInstanceId,
2245+
childThreadId: name === "sibling" ? sibling : child,
2246+
prompt: name,
2247+
result: null,
2248+
},
2249+
} as never);
2250+
}
2251+
const cardReads = traceSqlStatements();
2252+
const cards = yield* store
2253+
.getThreadRecords(parent, ["turnItems"], {
2254+
turnItemTypes: ["subagent"],
2255+
turnItemNodeIds: [NodeId.make("node:child-late")],
2256+
})
2257+
.pipe(Effect.withTracer(cardReads.tracer));
2258+
assert.deepEqual(
2259+
cards.turnItems.map((item) => String(item.id)),
2260+
["item:card:child-late"],
2261+
);
2262+
const cardPlan = yield* queryPlan(
2263+
cardReads.statements.find((statement) =>
2264+
statement.includes("FROM orchestration_v2_projection_turn_items"),
2265+
),
2266+
);
2267+
assert.include(
2268+
cardPlan[0] ?? "",
2269+
"USING INDEX orchestration_v2_projection_turn_items_node_ordinal_idx (node_id=?)",
2270+
);
22152271
// Several children read through the id list.
22162272
const both = yield* store.getThreadRecords(parent, fields, {
22172273
subagentChildThreadIds: [child, sibling],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3463,7 +3463,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
34633463
readsConversationItemsOnly(filter)
34643464
? sql`INDEXED BY orchestration_v2_projection_turn_items_conversation_idx
34653465
WHERE thread_id = ${threadId} AND type NOT IN ${CONVERSATION_INDEX_EXCLUDED_TYPES}`
3466-
: sql`WHERE thread_id = ${threadId}`
3466+
: sql`WHERE ${threadColumn(filter?.turnItemNodeIds)} = ${threadId}`
34673467
}
34683468
${filter?.turnItemRequestIds === undefined ? sql`` : sql`AND json_extract(payload_json, '$.requestId') IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRequestIds)}))`}
34693469
${filter?.turnItemTypes === undefined ? sql`` : sql`AND type IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemTypes)}))`}

0 commit comments

Comments
 (0)