The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 17:15 UTC
perf(server): shell reads of a worktree or family no longer parse every thread payload (#36146)
Julius Marminge committed
Nov 13, 2026 at 04:32 UTC
2d8a843e6be9ca170f3bce98cc92ad76617b00c4
5 files changed
+64
-11
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+19
-1
@@ -2807,11 +2807,25 @@ it.effect("SQL shells read a subagent family and a worktree's threads", () =>
2807
assert.deepEqual([...(yield* family(id("missing")))], []);
2808
2809
// Includes another root's idle child on the same path, which a shell may leave out.
2810
- const onWorktree = yield* store.getShellSnapshot({ location: "active", worktreePath });
2810
+ const { statements, tracer } = traceSqlStatements();
2811
+ const onWorktree = yield* store
2812
+ .getShellSnapshot({ location: "active", worktreePath })
2813
+ .pipe(Effect.withTracer(tracer));
2814
assert.sameMembers(
2815
onWorktree.threads.map((thread) => thread.id),
2816
[id("root"), id("child"), id("other-child")],
2817
);
2818
+ // A path names a few threads; scanning every thread parses every payload.
2819
+ const sql = yield* SqlClient.SqlClient;
2820
+ const threadQuery = statements.find((statement) =>
2821
+ statement.includes("AS latest_run_stop_requested"),
2822
+ );
2823
+ assert.isDefined(threadQuery);
2824
+ const threadPlan = (yield* sql.unsafe<{ readonly detail: string }>(
2825
+ `EXPLAIN QUERY PLAN ${threadQuery}`,
2826
+ [],
2827
+ )).map((row) => row.detail);
2828
+ assert.notInclude(threadPlan, "SCAN t", threadPlan.join("\n"));
2829
// A fork keeps its deleted source's root, so the deleted root still names the live tree.
2830
yield* store.apply({
2831
id: EventId.make("event:family:root-deleted"),
@@ -6629,6 +6643,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
6643
assert.isNotEmpty(countQueries);
6644
for (const countQuery of countQueries) {
6645
for (const detail of yield* planOf(countQuery)) {
6646
+ // A thread's counts read only each run's status, never its row and payload.
6647
+ if (/^SEARCH (r|run) /.test(detail)) {
6648
+ assert.include(detail, "COVERING INDEX", detail);
6649
+ }
6650
if (detail.includes("orchestration_v2_projection_turn_items")) {
6651
// Only held input is read from the sparse index; transcript history stays unread.
6652
assert.include(
apps/server/src/orchestration-v2/ProjectionStore.ts
+15
-10
@@ -7666,12 +7666,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7666
(
7667
SELECT item.payload_json
7668
FROM orchestration_v2_projection_turn_items item
7669
- INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id
7670
- WHERE r.run_id = presented.run_id
7671
- AND r.status = 'failed'
7669
+ WHERE presented.status = 'failed'
7670
AND item.thread_id = t.thread_id
7671
+ AND item.run_id = presented.run_id
7672
AND item.type = 'error' AND item.status = 'failed'
7674
- AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId')
7673
+ AND item.node_id IS json_extract(presented.payload_json, '$.rootNodeId')
7674
ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC
7675
LIMIT 1
7676
) AS terminal_failure_payload_json,
@@ -7789,9 +7788,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7788
projectId === undefined ? sql`` : sql` AND t.project_id = ${projectId}`
7789
}${
7790
location === "active"
7792
- ? sql` AND json_extract(t.payload_json, '$.archivedAt') IS NULL`
7791
+ ? sql` AND t.archived_at IS NULL`
7792
: location === "archive"
7794
- ? sql` AND json_extract(t.payload_json, '$.archivedAt') IS NOT NULL`
7793
+ ? sql` AND t.archived_at IS NOT NULL`
7794
: sql``
7795
}${
7796
unsettledOnly
@@ -7800,7 +7799,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7799
}${
7800
familyOf === undefined
7801
? sql``
7803
- : sql` AND json_extract(t.payload_json, '$.lineage.rootThreadId') = (
7802
+ : sql` AND CASE WHEN json_valid(t.payload_json)
7803
+ THEN json_extract(t.payload_json, '$.lineage.rootThreadId') END = (
7804
SELECT json_extract(member.payload_json, '$.lineage.rootThreadId')
7805
FROM orchestration_v2_projection_threads member
7806
WHERE member.thread_id = ${familyOf}
@@ -7808,7 +7808,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7808
}${
7809
worktreePath === undefined
7810
? sql``
7811
- : sql` AND json_extract(t.payload_json, '$.worktreePath') = ${worktreePath}`
7811
+ : sql` AND CASE WHEN json_valid(t.payload_json)
7812
+ THEN json_extract(t.payload_json, '$.worktreePath') END = ${worktreePath}`
7813
}${threadIds === undefined ? sql`` : sql` AND t.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))`}
7814
ORDER BY t.updated_at ASC, t.thread_id ASC
7815
`;
@@ -7836,6 +7837,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7837
WHEN EXISTS (
7838
SELECT 1
7839
FROM orchestration_v2_projection_runs AS run
7840
+ INDEXED BY orchestration_v2_projection_runs_run_status_idx
7841
WHERE run.run_id = per_run.run_key
7842
AND (
7843
run.status IN ('queued', 'cancelled')
@@ -7882,7 +7884,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7884
SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END)
7885
AS runless_item_count
7886
FROM orchestration_v2_projection_turn_item_counts counts
7885
- LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key
7887
+ -- The primary key would load each run's whole row just for its status.
7888
+ LEFT JOIN orchestration_v2_projection_runs r
7889
+ INDEXED BY orchestration_v2_projection_runs_run_status_idx
7890
+ ON r.run_id = counts.run_key
7891
WHERE counts.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
7892
GROUP BY counts.thread_id
7893
`;
@@ -8742,7 +8747,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
8747
) AS has_remote_tasks
8748
FROM orchestration_v2_projection_threads t
8749
WHERE t.deleted_at IS NULL
8745
- AND json_extract(t.payload_json, '$.archivedAt') IS NULL
8750
+ AND t.archived_at IS NULL
8751
ORDER BY t.updated_at ASC, t.thread_id ASC
8752
`;
8753
return rows.map((row): ActiveShellIndexEntry => ({
apps/server/src/persistence/Migrations.ts
+2
@@ -96,6 +96,7 @@ import Migration0085 from "./Migrations/085_TurnItemCounts.ts";
96
import Migration0086 from "./Migrations/086_QueuedTurnInputIndex.ts";
97
import Migration0087 from "./Migrations/087_ProjectionProjectsCustomTitle.ts";
98
import Migration0088 from "./Migrations/088_TurnItemTypeStatusIndex.ts";
99
+import Migration0089 from "./Migrations/089_ShellThreadReadIndexes.ts";
100
101
const migrationEntries = [
102
[1, "OrchestrationEvents", Migration0001],
@@ -187,6 +188,7 @@ const migrationEntries = [
188
[86, "QueuedTurnInputIndex", Migration0086],
189
[87, "ProjectionProjectsCustomTitle", Migration0087],
190
[88, "TurnItemTypeStatusIndex", Migration0088],
191
+ [89, "ShellThreadReadIndexes", Migration0089],
192
] as const;
193
194
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/089_ShellThreadReadIndexes.ts
new
+26
@@ -0,0 +1,26 @@
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
+ // Keep these expressions identical to ProjectionStore's shell worktree and family filters.
8
+ // json_valid keeps a malformed payload writable, as the other payload indexes do.
9
+ yield* sql`
10
+ CREATE INDEX IF NOT EXISTS orchestration_v2_projection_threads_worktree_path_idx
11
+ ON orchestration_v2_projection_threads(
12
+ CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.worktreePath') END
13
+ )
14
+ `;
15
+ yield* sql`
16
+ CREATE INDEX IF NOT EXISTS orchestration_v2_projection_threads_root_thread_idx
17
+ ON orchestration_v2_projection_threads(
18
+ CASE WHEN json_valid(payload_json) THEN json_extract(payload_json, '$.lineage.rootThreadId') END
19
+ )
20
+ `;
21
+ // Shell item counts read only a run's status; the primary key would load the whole row.
22
+ yield* sql`
23
+ CREATE INDEX IF NOT EXISTS orchestration_v2_projection_runs_run_status_idx
24
+ ON orchestration_v2_projection_runs(run_id, status)
25
+ `;
26
+});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -68,6 +68,7 @@ describe("V2 preview upgrade", () => {
68
[86, "QueuedTurnInputIndex"],
69
[87, "ProjectionProjectsCustomTitle"],
70
[88, "TurnItemTypeStatusIndex"],
71
+ [89, "ShellThreadReadIndexes"],
72
]);
73
assert.deepStrictEqual(yield* runMigrations(), []);
74
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -179,6 +180,7 @@ describe("V2 preview upgrade", () => {
180
[86, "QueuedTurnInputIndex"],
181
[87, "ProjectionProjectsCustomTitle"],
182
[88, "TurnItemTypeStatusIndex"],
183
+ [89, "ShellThreadReadIndexes"],
184
]);
185
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
186
);