@devries-cameron20 / t3code-1 / commits / 980f5d4128
Fork 0

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

fix(server): read the active shell index without parsing thread payloads (#37966)

Julius Marminge committed Nov 17, 2026 at 00:28 UTC 980f5d412863d1b00e832b877d2692b9cdab7dba
4 files changed +87 -33
apps/server/src/orchestration-v2/ProjectionStore.ts
+48 -33
@@ -9038,6 +9038,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
9038 return rows[0]?.count ?? 0;
9039 }, Effect.mapError(shellSnapshotReadError));
9040
9041 + // Migration 98 indexes these exact expressions, so the read takes them from the index instead
9042 + // of parsing every active thread's payload. Changing one here needs a matching index change.
9043 + const indexedField = (path: string, read: "json_extract" | "json_type" = "json_extract") =>
9044 + sql.literal(
9045 + `CASE WHEN json_valid(t.payload_json) THEN ${read}(t.payload_json, '${path}') END`,
9046 + );
9047 + // The planner otherwise scans the whole (thread_id, type, status) index for this list.
9048 + const PENDING_ITEMS_INDEX = sql.literal(
9049 + "INDEXED BY orchestration_v2_projection_turn_items_shell_pending_idx",
9050 + );
9051 const listActiveShellIndex = Effect.fn("ProjectionStore.listActiveShellIndex")(function* () {
9052 const rows = yield* sql<{
9053 readonly thread_id: string;
@@ -9057,48 +9067,53 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
9067 SELECT
9068 t.thread_id,
9069 t.project_id,
9060 - json_extract(t.payload_json, '$.lineage.relationshipToParent') AS relationship_to_parent,
9061 - json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id,
9062 - json_extract(t.payload_json, '$.lineage.rootThreadId') AS root_thread_id,
9063 - json_extract(t.payload_json, '$.lineage.sideChat') AS side_chat,
9064 - json_extract(t.payload_json, '$.settledOverride') AS settled_override,
9065 - json_extract(t.payload_json, '$.settledAt') AS settled_at,
9066 - json_extract(t.payload_json, '$.pinnedAt') AS pinned_at,
9067 - json_extract(t.payload_json, '$.pinOrderKey') AS pin_order_key,
9068 - json_extract(t.payload_json, '$.snoozedUntil') AS snoozed_until,
9070 + ${indexedField("$.lineage.relationshipToParent")} AS relationship_to_parent,
9071 + ${indexedField("$.lineage.parentThreadId")} AS parent_thread_id,
9072 + ${indexedField("$.lineage.rootThreadId")} AS root_thread_id,
9073 + ${indexedField("$.lineage.sideChat")} AS side_chat,
9074 + ${indexedField("$.settledOverride")} AS settled_override,
9075 + ${indexedField("$.settledAt")} AS settled_at,
9076 + ${indexedField("$.pinnedAt")} AS pinned_at,
9077 + ${indexedField("$.pinOrderKey")} AS pin_order_key,
9078 + ${indexedField("$.snoozedUntil")} AS snoozed_until,
9079 -- A superset of isLiveSubagentShell: the sources of its run status, pending request,
9070 - -- and pending background work.
9080 + -- and pending background work. Each source is one list for the whole read, found
9081 + -- through its partial index, so finished threads cost no lookups.
9082 (
9072 - EXISTS (
9073 - SELECT 1 FROM orchestration_v2_projection_runs r
9074 - WHERE r.thread_id = t.thread_id
9075 - AND r.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
9083 + t.thread_id IN (
9084 + SELECT r.thread_id FROM orchestration_v2_projection_runs r
9085 + WHERE r.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
9086 )
9077 - OR EXISTS (
9078 - SELECT 1 FROM orchestration_v2_projection_runtime_requests request
9079 - WHERE request.thread_id = t.thread_id AND request.status = 'pending'
9087 + OR t.thread_id IN (
9088 + SELECT request.thread_id FROM orchestration_v2_projection_runtime_requests request
9089 + WHERE request.status = 'pending'
9090 )
9091 -- Same predicates as the shell's pending item and link reads, for their partial indexes.
9082 - OR EXISTS (
9083 - SELECT 1 FROM orchestration_v2_projection_turn_items item
9084 - WHERE item.thread_id = t.thread_id
9085 - AND item.type IN ('command_execution', 'dynamic_tool', 'subagent')
9092 + OR t.thread_id IN (
9093 + SELECT item.thread_id
9094 + FROM orchestration_v2_projection_turn_items item ${PENDING_ITEMS_INDEX}
9095 + WHERE item.type IN ('command_execution', 'dynamic_tool', 'subagent')
9096 AND item.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled')
9097 )
9088 - OR EXISTS (
9089 - SELECT 1 FROM orchestration_v2_projection_turn_items link_request
9090 - WHERE link_request.thread_id = t.thread_id
9091 - AND link_request.type = 'link_request' AND link_request.status = 'waiting'
9092 - )
9093 - OR EXISTS (
9094 - SELECT 1 FROM orchestration_v2_projection_provider_threads provider_thread
9095 - WHERE provider_thread.thread_id = t.thread_id
9096 - AND json_array_length(provider_thread.payload_json, '$.pendingBackgroundTasks') > 0
9098 + OR t.thread_id IN (
9099 + SELECT link_request.thread_id FROM orchestration_v2_projection_turn_items link_request
9100 + WHERE link_request.type = 'link_request' AND link_request.status = 'waiting'
9101 )
9098 - OR EXISTS (
9099 - SELECT 1 FROM json_each(t.payload_json, '$.pullRequests') link
9100 - WHERE json_type(link.value, '$.watch') IS NOT NULL
9102 + OR t.thread_id IN (
9103 + SELECT provider_thread.thread_id
9104 + FROM orchestration_v2_projection_provider_threads provider_thread
9105 + WHERE CASE WHEN json_valid(provider_thread.payload_json)
9106 + THEN json_array_length(provider_thread.payload_json, '$.pendingBackgroundTasks') > 0
9107 + ELSE 0 END
9108 )
9109 + -- Only a thread with a pull request link reads its payload. CASE, not AND: SQLite
9110 + -- 3.50 evaluates both AND operands here.
9111 + OR CASE WHEN ${indexedField("$.pullRequests[0]", "json_type")} IS NOT NULL
9112 + THEN EXISTS (
9113 + SELECT 1 FROM json_each(t.payload_json, '$.pullRequests') link
9114 + WHERE json_type(link.value, '$.watch') IS NOT NULL
9115 + )
9116 + ELSE 0 END
9117 ) AS may_be_live,
9118 -- One list for the whole read, found through the child_thread_id index. A lookup per
9119 -- thread would parse the payload of every subagent row.
apps/server/src/persistence/Migrations.ts
+2
@@ -105,6 +105,7 @@ import Migration0094 from "./Migrations/094_TurnItemMessageOrdinalIndex.ts";
105 import Migration0095 from "./Migrations/095_TurnItemInlineWorkOutput.ts";
106 import Migration0096 from "./Migrations/096_RepairStaleTitleRegeneration.ts";
107 import Migration0097 from "./Migrations/097_IssueTrackerBindings.ts";
108 +import Migration0098 from "./Migrations/098_ActiveShellIndexIndexes.ts";
109
110 const migrationEntries = [
111 [1, "OrchestrationEvents", Migration0001],
@@ -205,6 +206,7 @@ const migrationEntries = [
206 [95, "TurnItemInlineWorkOutput", Migration0095],
207 [96, "RepairStaleTitleRegeneration", Migration0096],
208 [97, "IssueTrackerBindings", Migration0097],
209 + [98, "ActiveShellIndexIndexes", Migration0098],
210 ] as const;
211
212 export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/098_ActiveShellIndexIndexes.ts new
+35
@@ -0,0 +1,35 @@
1 +import * as Effect from "effect/Effect";
2 +import * as SqlClient from "effect/sql/SqlClient";
3 +
4 +export default Effect.gen(function* () {
5 + const sql = yield* SqlClient.SqlClient;
6 +
7 + // ProjectionStore.listActiveShellIndex reads these fields from the index instead of parsing
8 + // every active thread's payload. Keep the expressions identical to that query's.
9 + yield* sql`
10 + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_threads_active_shell_idx
11 + ON orchestration_v2_projection_threads(
12 + updated_at,
13 + thread_id,
14 + project_id,
15 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.lineage.relationshipToParent') END,
16 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.lineage.parentThreadId') END,
17 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.lineage.rootThreadId') END,
18 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.lineage.sideChat') END,
19 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.settledOverride') END,
20 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.settledAt') END,
21 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.pinnedAt') END,
22 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.pinOrderKey') END,
23 + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.snoozedUntil') END,
24 + CASE WHEN json_valid(payload_json) THEN json_type(payload_json, '$.pullRequests[0]') END
25 + )
26 + WHERE deleted_at IS NULL AND archived_at IS NULL
27 + `;
28 + yield* sql`
29 + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_provider_threads_pending_tasks_idx
30 + ON orchestration_v2_projection_provider_threads(thread_id)
31 + WHERE CASE WHEN json_valid(payload_json)
32 + THEN json_array_length(payload_json, '$.pendingBackgroundTasks') > 0
33 + ELSE 0 END
34 + `;
35 +});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -77,6 +77,7 @@ describe("V2 preview upgrade", () => {
77 [95, "TurnItemInlineWorkOutput"],
78 [96, "RepairStaleTitleRegeneration"],
79 [97, "IssueTrackerBindings"],
80 + [98, "ActiveShellIndexIndexes"],
81 ]);
82 assert.deepStrictEqual(yield* runMigrations(), []);
83 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -197,6 +198,7 @@ describe("V2 preview upgrade", () => {
198 [95, "TurnItemInlineWorkOutput"],
199 [96, "RepairStaleTitleRegeneration"],
200 [97, "IssueTrackerBindings"],
201 + [98, "ActiveShellIndexIndexes"],
202 ]);
203 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
204 );