@devries-cameron20 / t3code Mirror · read-only / commits / 5c7e4f276a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 07:00 UTC

perf(server): t3_thread_read loads a page's items in one query (#23176)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Victor Vogel committed Nov 1, 2026 at 15:57 UTC 5c7e4f276a14d992f260ef726bd97815e8b09f78
2 files changed +37 -9
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+12
@@ -6366,6 +6366,18 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
6366 });
6367 assert.deepEqual(followingPage.items, targetAfterRollback.visibleTurnItems.slice(2));
6368 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);
6381
6382 assert.deepEqual(
6383 targetAfterRollback.visibleTurnItems.map((row) => [
apps/server/src/orchestration-v2/ProjectionStore.ts
+25 -9
@@ -5763,6 +5763,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5763 : row.sourceItemId === options.itemId,
5764 );
5765 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 + );
5783 const items = yield* Effect.forEach(page, (row) =>
5784 Effect.gen(function* () {
5785 if (row.synthetic !== undefined)
@@ -5773,19 +5790,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5790 visibility: row.visibility,
5791 item: row.synthetic,
5792 };
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)
5795 return yield* new ProjectionStoreReadError({
5796 threadId,
5797 cause: "Timeline item disappeared during snapshot",
5798 });
5799 + const item = yield* decodeTurnItemPayload(payload).pipe(
5800 + Effect.mapError(
5801 + (cause) =>
5802 + new ProjectionStoreReadError({ threadId: row.sourceThreadId, cause }),
5803 + ),
5804 + );
5805 return {
5806 position: row.position,
5807 sourceThreadId: row.sourceThreadId,