Skip to content

Commit bd33236

Browse files
fix(server): thread lists keep syncing past 32,766 threads
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6838e31 commit bd33236

2 files changed

Lines changed: 67 additions & 7 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,62 @@ it.effect("lists only active subagent IDs without loading shell data", () =>
13181318
}).pipe(Effect.provide(layerTest)),
13191319
);
13201320

1321+
// regression: #25621 SQLite rejects statements with more than 32,766 bound parameters
1322+
it.effect("reads a shell snapshot with more threads than SQLite allows bound parameters", () =>
1323+
Effect.gen(function* () {
1324+
const store = yield* ProjectionStore.ProjectionStoreV2;
1325+
const sql = yield* SqlClient.SqlClient;
1326+
const now = yield* DateTime.now;
1327+
const projectId = ProjectId.make("project:many-threads");
1328+
const threadId = ThreadId.make("thread:many:0");
1329+
yield* store.apply({
1330+
id: EventId.make("event:many:created"),
1331+
type: "thread.created",
1332+
threadId,
1333+
occurredAt: now,
1334+
payload: {
1335+
createdBy: "user",
1336+
creationSource: "web",
1337+
id: threadId,
1338+
projectId,
1339+
title: "Many threads",
1340+
providerInstanceId,
1341+
modelSelection,
1342+
runtimeMode: "full-access",
1343+
interactionMode: "default",
1344+
branch: null,
1345+
worktreePath: null,
1346+
activeProviderThreadId: null,
1347+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
1348+
forkedFrom: null,
1349+
createdAt: now,
1350+
updatedAt: now,
1351+
archivedAt: null,
1352+
settledOverride: null,
1353+
settledAt: null,
1354+
lastVisitedAt: null,
1355+
deletedAt: null,
1356+
},
1357+
});
1358+
const threadCount = 32_768;
1359+
yield* sql`
1360+
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${threadCount - 1})
1361+
INSERT INTO orchestration_v2_projection_threads (
1362+
thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
1363+
created_at, updated_at, archived_at, deleted_at, payload_json, provider_instance_id
1364+
)
1365+
SELECT 'thread:many:' || n.i, t.project_id, t.title, t.default_provider, t.runtime_mode,
1366+
t.interaction_mode, t.created_at, t.updated_at, t.archived_at, t.deleted_at,
1367+
json_set(t.payload_json, '$.id', 'thread:many:' || n.i), t.provider_instance_id
1368+
FROM n CROSS JOIN orchestration_v2_projection_threads t
1369+
WHERE t.thread_id = ${threadId}
1370+
`;
1371+
1372+
const snapshot = yield* store.getShellSnapshot({ projectId, location: "active" });
1373+
assert.strictEqual(snapshot.threads.length, threadCount);
1374+
}).pipe(Effect.provide(layerTest)),
1375+
);
1376+
13211377
it.layer(layerTest)("ProjectionStoreV2", (it) => {
13221378
// regression: #21969 keep corrupt plan JSON in the typed read error
13231379
it.effect("returns a typed read error for malformed stored plan JSON", () =>

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5784,6 +5784,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
57845784
)`;
57855785
};
57865786

5787+
// One bound JSON array instead of one parameter per id: SQLite caps a statement at 32,766.
5788+
const inJsonList = (ids: ReadonlyArray<string>) =>
5789+
sql`(SELECT value FROM json_each(${encodeIdList(ids)}))`;
5790+
57875791
const selectShellThreadRows = (
57885792
threadId?: ThreadId,
57895793
{
@@ -5988,7 +5992,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
59885992
worktreePath === undefined
59895993
? sql``
59905994
: sql` AND json_extract(t.payload_json, '$.worktreePath') = ${worktreePath}`
5991-
}${threadIds === undefined ? sql`` : sql` AND t.thread_id IN ${sql.in(threadIds)}`}
5995+
}${threadIds === undefined ? sql`` : sql` AND t.thread_id IN ${inJsonList(threadIds)}`}
59925996
ORDER BY t.updated_at ASC, t.thread_id ASC
59935997
`;
59945998

@@ -6001,14 +6005,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60016005
: sql<ShellRunRow>`
60026006
SELECT thread_id, run_id, ordinal
60036007
FROM orchestration_v2_projection_runs
6004-
WHERE thread_id IN ${sql.in(threadIds)}
6008+
WHERE thread_id IN ${inJsonList(threadIds)}
60056009
`;
60066010

60076011
const selectShellRunItemCounts = (threadIds: ReadonlyArray<ThreadId>) =>
60086012
sql<ShellRunItemCountRow>`
60096013
SELECT thread_id, run_id, COUNT(*) AS item_count
60106014
FROM orchestration_v2_projection_turn_items
6011-
WHERE run_id IS NOT NULL AND thread_id IN ${sql.in(threadIds)}
6015+
WHERE run_id IS NOT NULL AND thread_id IN ${inJsonList(threadIds)}
60126016
GROUP BY thread_id, run_id
60136017
`;
60146018

@@ -6023,7 +6027,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60236027
FROM (
60246028
SELECT thread_id, run_id, COUNT(*) AS item_count
60256029
FROM orchestration_v2_projection_turn_items
6026-
WHERE thread_id IN ${sql.in(threadIds)}
6030+
WHERE thread_id IN ${inJsonList(threadIds)}
60276031
GROUP BY thread_id, run_id
60286032
) counts
60296033
LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_id
@@ -6040,7 +6044,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60406044
: sql<PayloadRow & { readonly thread_id: string }>`
60416045
SELECT thread_id, payload_json
60426046
FROM orchestration_v2_projection_provider_threads
6043-
WHERE thread_id IN ${sql.in(threadIds)}
6047+
WHERE thread_id IN ${inJsonList(threadIds)}
60446048
`;
60456049

60466050
const selectShellPendingTurnItemRows = (
@@ -6056,7 +6060,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60566060
WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent')
60576061
AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled')
60586062
AND (i.run_id IS NULL OR r.status <> 'rolled_back')
6059-
${threadIds === undefined ? sql`` : sql`AND i.thread_id IN ${sql.in(threadIds)}`}
6063+
${threadIds === undefined ? sql`` : sql`AND i.thread_id IN ${inJsonList(threadIds)}`}
60606064
${
60616065
activeRunIds.length === 0
60626066
? sql``
@@ -6065,7 +6069,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60656069
UNION ALL
60666070
SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id
60676071
FROM orchestration_v2_projection_turn_items i
6068-
WHERE i.run_id IN ${sql.in(activeRunIds)}
6072+
WHERE i.run_id IN ${inJsonList(activeRunIds)}
60696073
AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction')
60706074
AND i.status IN ('running', 'waiting')
60716075
`

0 commit comments

Comments
 (0)