Skip to content

Commit 1e428b6

Browse files
only21milclaude
andcommitted
perf(server): read a timeline page's payloads in one query
getTimelinePage ran one SELECT per stored item on the page, so a default t3_thread_read (50 items) cost 50 sequential payload queries. Read them in one json_each join, as getThreadHistoryPage already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a8bb769 commit 1e428b6

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4743,6 +4743,18 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
47434743
});
47444744
assert.deepEqual(followingPage.items, targetAfterRollback.visibleTurnItems.slice(2));
47454745
assert.isFalse(followingPage.hasMore);
4746+
const tracedTimelinePage = (limit: number) =>
4747+
Effect.gen(function* () {
4748+
const { statements, tracer } = traceSqlStatements();
4749+
const page = yield* projectionStore
4750+
.getTimelinePage(targetThreadId, { view: "activity", limit })
4751+
.pipe(Effect.withTracer(tracer));
4752+
return { page, statements };
4753+
});
4754+
const singleItemPage = yield* tracedTimelinePage(1);
4755+
const fullPage = yield* tracedTimelinePage(10);
4756+
assert.deepEqual(fullPage.page.items, targetAfterRollback.visibleTurnItems);
4757+
assert.lengthOf(fullPage.statements, singleItemPage.statements.length);
47464758

47474759
assert.deepEqual(
47484760
targetAfterRollback.visibleTurnItems.map((row) => [

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5530,6 +5530,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55305530
: row.sourceItemId === options.itemId,
55315531
);
55325532
const page = matching.slice(0, options.limit);
5533+
const sources = yield* encodeTimelineSources(
5534+
page
5535+
.filter((row) => row.synthetic === undefined)
5536+
.map((row) => ({ threadId: row.sourceThreadId, id: row.sourceItemId })),
5537+
);
5538+
const payloads = yield* sql<{
5539+
thread_id: string;
5540+
turn_item_id: string;
5541+
payload_json: string;
5542+
}>`
5543+
SELECT item.thread_id, item.turn_item_id, item.payload_json
5544+
FROM orchestration_v2_projection_turn_items item JOIN json_each(${sources}) wanted
5545+
ON item.thread_id = json_extract(wanted.value, '$.threadId')
5546+
AND item.turn_item_id = json_extract(wanted.value, '$.id')`;
5547+
const byIdentity = new Map(
5548+
payloads.map((row) => [`${row.thread_id}:${row.turn_item_id}`, row.payload_json]),
5549+
);
55335550
const items = yield* Effect.forEach(page, (row) =>
55345551
Effect.gen(function* () {
55355552
if (row.synthetic !== undefined)
@@ -5540,19 +5557,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55405557
visibility: row.visibility,
55415558
item: row.synthetic,
55425559
};
5543-
const payloads =
5544-
yield* sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_turn_items
5545-
WHERE thread_id = ${row.sourceThreadId} AND turn_item_id = ${row.sourceItemId}`;
5546-
const decoded = yield* decodeRows(
5547-
decodeTurnItemPayload,
5548-
row.sourceThreadId,
5549-
)(payloads);
5550-
const item = decoded[0];
5551-
if (item === undefined)
5560+
const payload = byIdentity.get(`${row.sourceThreadId}:${row.sourceItemId}`);
5561+
if (payload === undefined)
55525562
return yield* new ProjectionStoreReadError({
55535563
threadId,
55545564
cause: "Timeline item disappeared during snapshot",
55555565
});
5566+
const item = yield* decodeTurnItemPayload(payload).pipe(
5567+
Effect.mapError(
5568+
(cause) =>
5569+
new ProjectionStoreReadError({ threadId: row.sourceThreadId, cause }),
5570+
),
5571+
);
55565572
return {
55575573
position: row.position,
55585574
sourceThreadId: row.sourceThreadId,

0 commit comments

Comments
 (0)