TypeScript
fix(server): thread lists load beyond SQLite variable limit (#25696)
Julius Marminge0d51375ef765
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.tsindex 2af87ecf4fe..17b8bb81886 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts@@ -1374,6 +1374,70 @@ it.effect("lists only active subagent IDs without loading shell data", () =>}).pipe(Effect.provide(layerTest)),);+// regression: #25621 SQLite rejects statements binding more than 32,766 variables+it.effect("lists every thread in the shell snapshot past SQLite's bound variable limit", () =>+ Effect.gen(function* () {+ const store = yield* ProjectionStore.ProjectionStoreV2;+ const sql = yield* SqlClient.SqlClient;+ const now = yield* DateTime.now;+ const templateId = ThreadId.make("thread:many:00000");+ yield* store.apply({+ id: EventId.make(`event:${templateId}:created`),+ type: "thread.created",+ threadId: templateId,+ occurredAt: now,+ payload: {+ createdBy: "user",+ creationSource: "web",+ id: templateId,+ projectId: ProjectId.make("project:many"),+ title: "Many",+ providerInstanceId,+ modelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ activeProviderThreadId: null,+ lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: templateId },+ forkedFrom: null,+ createdAt: now,+ updatedAt: now,+ archivedAt: null,+ settledOverride: null,+ settledAt: null,+ lastVisitedAt: null,+ deletedAt: null,+ },+ });+ const threadCount = 32_767;+ yield* sql`+ CREATE TEMP TABLE many_threads AS+ WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${threadCount - 1})+ SELECT t.*, n.i AS clone_index FROM orchestration_v2_projection_threads t, n+ WHERE t.thread_id = ${templateId}+ `;+ yield* sql`+ UPDATE many_threads SET+ thread_id = printf('thread:many:%05d', clone_index),+ payload_json = json_set(+ payload_json,+ '$.id', printf('thread:many:%05d', clone_index),+ '$.lineage.rootThreadId', printf('thread:many:%05d', clone_index)+ )+ `;+ yield* sql`ALTER TABLE many_threads DROP COLUMN clone_index`;+ yield* sql`INSERT INTO orchestration_v2_projection_threads SELECT * FROM many_threads`;++ const [unscoped, active] = yield* Effect.all([+ store.getShellSnapshot(),+ store.getShellSnapshot({ location: "active" }),+ ]);+ assert.strictEqual(unscoped.threads.length, threadCount);+ assert.strictEqual(active.threads.length, threadCount);+ }).pipe(Effect.provide(layerTest)),+);+it.layer(layerTest)("ProjectionStoreV2", (it) => {// regression: #21969 keep corrupt plan JSON in the typed read errorit.effect("returns a typed read error for malformed stored plan JSON", () =>diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.tsindex 2a21bfae4bd..287a30a9d90 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts@@ -5940,7 +5940,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =worktreePath === undefined? sql``: sql` AND json_extract(t.payload_json, '$.worktreePath') = ${worktreePath}`- }${threadIds === undefined ? sql`` : sql` AND t.thread_id IN ${sql.in(threadIds)}`}+ }${threadIds === undefined ? sql`` : sql` AND t.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))`}ORDER BY t.updated_at ASC, t.thread_id ASC`;@@ -5953,14 +5953,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =: sql<ShellRunRow>`SELECT thread_id, run_id, ordinalFROM orchestration_v2_projection_runs- WHERE thread_id IN ${sql.in(threadIds)}+ WHERE thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))`;const selectShellRunItemCounts = (threadIds: ReadonlyArray<ThreadId>) =>sql<ShellRunItemCountRow>`SELECT thread_id, run_id, COUNT(*) AS item_countFROM orchestration_v2_projection_turn_items- WHERE run_id IS NOT NULL AND thread_id IN ${sql.in(threadIds)}+ WHERE run_id IS NOT NULL AND thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))GROUP BY thread_id, run_id`;@@ -5975,7 +5975,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =FROM (SELECT thread_id, run_id, COUNT(*) AS item_countFROM orchestration_v2_projection_turn_items- WHERE thread_id IN ${sql.in(threadIds)}+ WHERE thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))GROUP BY thread_id, run_id) countsLEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_id@@ -5992,7 +5992,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =: sql<PayloadRow & { readonly thread_id: string }>`SELECT thread_id, payload_jsonFROM orchestration_v2_projection_provider_threads- WHERE thread_id IN ${sql.in(threadIds)}+ WHERE thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))`;const selectShellPendingTurnItemRows = (@@ -6008,7 +6008,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent')AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled')AND (i.run_id IS NULL OR r.status <> 'rolled_back')- ${threadIds === undefined ? sql`` : sql`AND i.thread_id IN ${sql.in(threadIds)}`}+ ${threadIds === undefined ? sql`` : sql`AND i.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))`}${activeRunIds.length === 0? sql``@@ -6017,7 +6017,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =UNION ALLSELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_idFROM orchestration_v2_projection_turn_items i- WHERE i.run_id IN ${sql.in(activeRunIds)}+ WHERE i.run_id IN (SELECT value FROM json_each(${encodeIdList(activeRunIds)}))AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction')AND i.status IN ('running', 'waiting')`