Skip to content

Commit 5c7e4f2

Browse files
only21milclaude
andauthored
perf(server): t3_thread_read loads a page's items in one query (pingdotgg#23176)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2a93356 commit 5c7e4f2

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
@@ -6366,6 +6366,18 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
63666366
});
63676367
assert.deepEqual(followingPage.items, targetAfterRollback.visibleTurnItems.slice(2));
63686368
assert.isFalse(followingPage.hasMore);
6369+
const tracedTimelinePage = (limit: number) =>
6370+
Effect.gen(function* () {
6371+
const { statements, tracer } = traceSqlStatements();
6372+
const page = yield* projectionStore
6373+
.getTimelinePage(targetThreadId, { view: "activity", limit })
6374+
.pipe(Effect.withTracer(tracer));
6375+
return { page, statements };
6376+
});
6377+
const singleItemPage = yield* tracedTimelinePage(1);
6378+
const fullPage = yield* tracedTimelinePage(10);
6379+
assert.deepEqual(fullPage.page.items, targetAfterRollback.visibleTurnItems);
6380+
assert.lengthOf(fullPage.statements, singleItemPage.statements.length);
63696381

63706382
assert.deepEqual(
63716383
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
@@ -5763,6 +5763,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
57635763
: row.sourceItemId === options.itemId,
57645764
);
57655765
const page = matching.slice(0, options.limit);
5766+
const sources = yield* encodeTimelineSources(
5767+
page
5768+
.filter((row) => row.synthetic === undefined)
5769+
.map((row) => ({ threadId: row.sourceThreadId, id: row.sourceItemId })),
5770+
);
5771+
const payloads = yield* sql<{
5772+
thread_id: string;
5773+
turn_item_id: string;
5774+
payload_json: string;
5775+
}>`
5776+
SELECT item.thread_id, item.turn_item_id, item.payload_json
5777+
FROM orchestration_v2_projection_turn_items item JOIN json_each(${sources}) wanted
5778+
ON item.thread_id = json_extract(wanted.value, '$.threadId')
5779+
AND item.turn_item_id = json_extract(wanted.value, '$.id')`;
5780+
const byIdentity = new Map(
5781+
payloads.map((row) => [`${row.thread_id}:${row.turn_item_id}`, row.payload_json]),
5782+
);
57665783
const items = yield* Effect.forEach(page, (row) =>
57675784
Effect.gen(function* () {
57685785
if (row.synthetic !== undefined)
@@ -5773,19 +5790,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
57735790
visibility: row.visibility,
57745791
item: row.synthetic,
57755792
};
5776-
const payloads =
5777-
yield* sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_turn_items
5778-
WHERE thread_id = ${row.sourceThreadId} AND turn_item_id = ${row.sourceItemId}`;
5779-
const decoded = yield* decodeRows(
5780-
decodeTurnItemPayload,
5781-
row.sourceThreadId,
5782-
)(payloads);
5783-
const item = decoded[0];
5784-
if (item === undefined)
5793+
const payload = byIdentity.get(`${row.sourceThreadId}:${row.sourceItemId}`);
5794+
if (payload === undefined)
57855795
return yield* new ProjectionStoreReadError({
57865796
threadId,
57875797
cause: "Timeline item disappeared during snapshot",
57885798
});
5799+
const item = yield* decodeTurnItemPayload(payload).pipe(
5800+
Effect.mapError(
5801+
(cause) =>
5802+
new ProjectionStoreReadError({ threadId: row.sourceThreadId, cause }),
5803+
),
5804+
);
57895805
return {
57905806
position: row.position,
57915807
sourceThreadId: row.sourceThreadId,

0 commit comments

Comments
 (0)