@devries-cameron20 / t3code-1 / commits / 5f2bb934d8
Fork 0

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

perf(server): user-only message reads use the user-message index for ordinals (#33009)

Julius Marminge committed Nov 10, 2026 at 10:43 UTC 5f2bb934d8f2529535d659724507723be7b8fb02
2 files changed +22 -2
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+18 -1
@@ -811,6 +811,23 @@ it.effect("record reads filter notification sources without dropping matching me
811 matching.messages.map((message) => [message.id, message.text]),
812 [["monitor", "Complete body for monitor"]],
813 );
814 + const { statements, tracer } = traceSqlStatements();
815 + const userMessages = yield* store
816 + .getThreadRecords(threadId, ["messages"], { messageRoles: ["user"] })
817 + .pipe(Effect.withTracer(tracer));
818 + assert.lengthOf(userMessages.messages, 3);
819 + const ordinalLookup = statements.find((statement) => statement.includes("MIN(ordinal)"));
820 + assert.isDefined(ordinalLookup);
821 + const sql = yield* SqlClient.SqlClient;
822 + const plan = (yield* sql.unsafe<{ readonly detail: string }>(
823 + `EXPLAIN QUERY PLAN ${ordinalLookup}`,
824 + [threadId, "[]"],
825 + )).map((row) => row.detail);
826 + // User-only reads must stay on the partial index instead of scanning every turn item.
827 + assert.isTrue(
828 + plan.some((detail) => detail.includes("turn_items_user_message_idx")),
829 + plan.join("\n"),
830 + );
831 assert.lengthOf((yield* store.getThreadRecords(threadId, ["messages"])).messages, 4);
832 assert.lengthOf(
833 (yield* store.getThreadRecords(threadId, ["messages"], {
@@ -3119,7 +3136,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3136 parent_item_id, ordinal, type, status, updated_at, payload_json
3137 ) VALUES (
3138 ${`turn-item:bounded-sql-history:ordinal-${suffix}`}, ${itemThreadId}, NULL, NULL, NULL,
3122 - NULL, NULL, ${ordinal}, 'assistant_message', 'completed', ${nowIso},
3139 + NULL, NULL, ${ordinal}, 'user_message', 'completed', ${nowIso},
3140 ${encodeUnknownJsonString({ messageId })}
3141 )
3142 `;
apps/server/src/orchestration-v2/ProjectionStore.ts
+4 -1
@@ -3834,10 +3834,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3834 (filter.messageRoles.includes("user") && filter.messageRoles.includes("assistant")))
3835 ? undefined
3836 : messages.map((message) => message.id);
3837 + // The literal `type = 'user_message'` lets SQLite pick the partial user-message index.
3838 + const userOnly = messages.every((message) => message.role === "user");
3839 const rows = yield* sql<{ message_id: string; ordinal: number }>`
3840 SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal
3841 FROM orchestration_v2_projection_turn_items
3840 - WHERE thread_id = ${threadId} AND type IN ('user_message', 'assistant_message')
3842 + WHERE thread_id = ${threadId}
3843 + AND ${userOnly ? sql`type = 'user_message'` : sql`type IN ('user_message', 'assistant_message')`}
3844 ${loadedMessageIds === undefined ? sql`` : sql`AND json_extract(payload_json, '$.messageId') IN (SELECT value FROM json_each(${encodeIdList(loadedMessageIds)}))`}
3845 GROUP BY json_extract(payload_json, '$.messageId')
3846 `;