From 9e5dcf2e877edb05b9522407c24fdef5c8565719 Mon Sep 17 00:00:00 2001 From: Navid Nadali Date: Thu, 19 Nov 2026 16:11:09 +0000 Subject: [PATCH 1/2] fix(server): shell reads no longer walk a thread's finished tool calls The thread shell's pending-item read has a partial index that holds only unsettled command, tool and subagent items. Since the (thread_id, type, status) index was added, SQLite picks that one instead and visits every finished command, tool and subagent item of the thread, so each shell read grows with the thread's history. Force the partial index, as the active shell index list already does. The rows returned are the same. Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/ProjectionStore.test.ts | 63 ++++++++++++++++++- .../src/orchestration-v2/ProjectionStore.ts | 1 + 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index a1c9ff8eebc46..0166a6eb16283 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -7068,7 +7068,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { Effect.gen(function* () { const projectionStore = yield* ProjectionStore.ProjectionStoreV2; const sql = yield* SqlClient.SqlClient; - const { projectId } = yield* assertProjectScopedShells("sql-shell-stop-index"); + const { projectId, activeThreadId } = + yield* assertProjectScopedShells("sql-shell-stop-index"); const { statements, tracer } = traceSqlStatements(); yield* projectionStore .getShellSnapshot({ projectId, location: "active" }) @@ -7121,6 +7122,66 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { } } } + + // Settled command, tool, and subagent items stay out of the pending-item read. + const pendingQuery = statements.find((statement) => + statement.includes("AS owning_provider_instance_id"), + ); + assert.isDefined(pendingQuery); + const pendingPlan = yield* planOf(pendingQuery); + assert.include( + pendingPlan.find((detail) => detail.startsWith("SEARCH i ")) ?? "", + "orchestration_v2_projection_turn_items_shell_pending_idx", + pendingPlan.join("\n"), + ); + const nowIso = DateTime.formatIso(yield* DateTime.now); + yield* sql` + WITH RECURSIVE history(position) AS ( + SELECT 1 UNION ALL SELECT position + 1 FROM history WHERE position < 1000 + ) + INSERT INTO orchestration_v2_projection_turn_items ( + turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id, + parent_item_id, ordinal, type, status, updated_at, payload_json + ) + SELECT 'item:shell-history:' || position, ${activeThreadId}, NULL, NULL, NULL, NULL, + NULL, position, 'command_execution', 'completed', ${nowIso}, + json_object( + 'id', 'item:shell-history:' || position, 'threadId', ${activeThreadId}, + 'runId', NULL, 'nodeId', NULL, 'providerThreadId', NULL, 'providerTurnId', NULL, + 'nativeItemRef', NULL, 'parentItemId', NULL, 'ordinal', position, + 'status', 'completed', 'title', 'history', 'input', 'echo', 'output', 'ok', + 'exitCode', 0, 'startedAt', ${nowIso}, 'completedAt', ${nowIso}, + 'updatedAt', ${nowIso}, 'type', 'command_execution' + ) + FROM history + `; + const shellStatements: Array]> = []; + yield* projectionStore.getThreadShell(activeThreadId).pipe( + Effect.provideService(Statement.CurrentTransformer, (statement) => { + shellStatements.push(statement.compile()); + return Effect.succeed(statement); + }), + ); + const pendingRead = shellStatements.find(([query]) => + query.includes("AS owning_provider_instance_id"), + ); + assert.isDefined(pendingRead); + { + // VM steps are deterministic; the read must cost the same without that history. + const copyPath = "file:/shell-pending-steps?vfs=memdb"; + using copy = new NodeSqlite.DatabaseSync(copyPath); + yield* sql`VACUUM INTO ${copyPath}`; + const pendingSteps = () => { + const statement = copy.prepare(pendingRead[0]); + statement.all(...(pendingRead[1] as ReadonlyArray)); + return statement.stat("vmStep"); + }; + const historySteps = pendingSteps(); + copy.exec( + "DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id LIKE 'item:shell-history:%'", + ); + assert.isAtMost(historySteps, pendingSteps() * 1.1); + } }), ); diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index f945d82b75105..7d898c0857edc 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -8484,6 +8484,7 @@ export const layer: Layer.Layer = ) => sql` SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i + INDEXED BY orchestration_v2_projection_turn_items_shell_pending_idx LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r From 6538014ca1a68623fef81735760f9d2d61cc9bfb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 17:55:08 +0000 Subject: [PATCH 2/2] test(server): keep only the pending-item plan assertion Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/ProjectionStore.test.ts | 53 +------------------ 1 file changed, 2 insertions(+), 51 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index c35b8c9f4fc49..5a1baf63d997a 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -7068,8 +7068,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { Effect.gen(function* () { const projectionStore = yield* ProjectionStore.ProjectionStoreV2; const sql = yield* SqlClient.SqlClient; - const { projectId, activeThreadId } = - yield* assertProjectScopedShells("sql-shell-stop-index"); + const { projectId } = yield* assertProjectScopedShells("sql-shell-stop-index"); const { statements, tracer } = traceSqlStatements(); yield* projectionStore .getShellSnapshot({ projectId, location: "active" }) @@ -7123,7 +7122,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { } } - // Settled command, tool, and subagent items stay out of the pending-item read. + // Without the hint the planner walks every settled command, tool, and subagent item. const pendingQuery = statements.find((statement) => statement.includes("AS owning_provider_instance_id"), ); @@ -7134,54 +7133,6 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { "orchestration_v2_projection_turn_items_shell_pending_idx", pendingPlan.join("\n"), ); - const nowIso = DateTime.formatIso(yield* DateTime.now); - yield* sql` - WITH RECURSIVE history(position) AS ( - SELECT 1 UNION ALL SELECT position + 1 FROM history WHERE position < 1000 - ) - INSERT INTO orchestration_v2_projection_turn_items ( - turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id, - parent_item_id, ordinal, type, status, updated_at, payload_json - ) - SELECT 'item:shell-history:' || position, ${activeThreadId}, NULL, NULL, NULL, NULL, - NULL, position, 'command_execution', 'completed', ${nowIso}, - json_object( - 'id', 'item:shell-history:' || position, 'threadId', ${activeThreadId}, - 'runId', NULL, 'nodeId', NULL, 'providerThreadId', NULL, 'providerTurnId', NULL, - 'nativeItemRef', NULL, 'parentItemId', NULL, 'ordinal', position, - 'status', 'completed', 'title', 'history', 'input', 'echo', 'output', 'ok', - 'exitCode', 0, 'startedAt', ${nowIso}, 'completedAt', ${nowIso}, - 'updatedAt', ${nowIso}, 'type', 'command_execution' - ) - FROM history - `; - const shellStatements: Array]> = []; - yield* projectionStore.getThreadShell(activeThreadId).pipe( - Effect.provideService(Statement.CurrentTransformer, (statement) => { - shellStatements.push(statement.compile()); - return Effect.succeed(statement); - }), - ); - const pendingRead = shellStatements.find(([query]) => - query.includes("AS owning_provider_instance_id"), - ); - assert.isDefined(pendingRead); - { - // VM steps are deterministic; the read must cost the same without that history. - const copyPath = "file:/shell-pending-steps?vfs=memdb"; - using copy = new NodeSqlite.DatabaseSync(copyPath); - yield* sql`VACUUM INTO ${copyPath}`; - const pendingSteps = () => { - const statement = copy.prepare(pendingRead[0]); - statement.all(...(pendingRead[1] as ReadonlyArray)); - return statement.stat("vmStep"); - }; - const historySteps = pendingSteps(); - copy.exec( - "DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id LIKE 'item:shell-history:%'", - ); - assert.isAtMost(historySteps, pendingSteps() * 1.1); - } }), );