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, 19:45 UTC
perf(server): thread list reads no longer scan a long thread's runs and items (#35167)
Replaces #34747, #31828, #34666 and #27975. Co-authored-by: Bogdan <bogdan@bowd.io> Co-authored-by: Michael Zhou <joei4cm@gmail.com> Co-authored-by: Pyseph <45090858+Pyseph@users.noreply.github.com> Co-authored-by: Braulio Oliveira <brauliobo@gmail.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Theo Browne committed
Nov 12, 2026 at 03:29 UTC
3e03072eb634a0cc5e6b3b6429d5f7656970fe68
9 files changed
+227
-28
apps/server/src/orchestration-v2/FoundationPersistence.test.ts
+2
@@ -3956,6 +3956,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
3956
});
3957
const afterUpdate = yield* projectionStore.getThreadProjection(threadId);
3958
assert.equal(afterUpdate.turnItems[0]?.ordinal, ordinals[0]);
3959
+ assert.equal((yield* projectionStore.getThreadShell(threadId))?.itemCount, 151);
3960
assert.equal((yield* maintenance.verify).valid, true);
3961
3962
yield* sql`
@@ -3970,6 +3971,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
3971
const rebuilt = yield* maintenance.rebuild;
3972
assert.isTrue(rebuilt.valid);
3973
assert.lengthOf((yield* projectionStore.getThreadProjection(threadId)).turnItems, 151);
3974
+ assert.equal((yield* projectionStore.getThreadShell(threadId))?.itemCount, 151);
3975
}),
3976
);
3977
});
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+62
-11
@@ -5359,7 +5359,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5359
}),
5360
);
5361
5362
- it.effect("looks up shell stop requests through the interrupt request index", () =>
5362
+ it.effect("reads shell runs and item counts without walking a thread's history", () =>
5363
Effect.gen(function* () {
5364
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
5365
const sql = yield* SqlClient.SqlClient;
@@ -5368,20 +5368,44 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5368
yield* projectionStore
5369
.getShellSnapshot({ projectId, location: "active" })
5370
.pipe(Effect.withTracer(tracer));
5371
+ const planOf = (statement: string) =>
5372
+ sql
5373
+ .unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${statement}`)
5374
+ .pipe(Effect.map((rows) => rows.map((row) => row.detail)));
5375
5376
const threadQuery = statements.find((statement) =>
5377
statement.includes("AS latest_run_stop_requested"),
5378
);
5379
assert.isDefined(threadQuery);
5376
- const stopRequestLookups = (yield* sql.unsafe<{ readonly detail: string }>(
5377
- `EXPLAIN QUERY PLAN ${threadQuery}`,
5378
- ))
5379
- .map((row) => row.detail)
5380
- .filter((detail) => detail.startsWith("SEARCH stop_request "));
5380
+ const plan = yield* planOf(threadQuery);
5381
+ const stopRequestLookups = plan.filter((detail) => detail.startsWith("SEARCH stop_request "));
5382
assert.lengthOf(stopRequestLookups, 2);
5383
for (const lookup of stopRequestLookups) {
5384
assert.include(lookup, "orchestration_v2_projection_turn_items_stop_request_idx");
5385
}
5386
+ // The active, activity, and pending-secret lookups; walking the ordinal index instead
5387
+ // reads every run of an idle thread.
5388
+ const activeRunLookups = plan.filter(
5389
+ (detail) => detail.startsWith("SEARCH r ") && detail.includes("(thread_id=?"),
5390
+ );
5391
+ assert.lengthOf(activeRunLookups, 4);
5392
+ for (const lookup of activeRunLookups) {
5393
+ assert.include(lookup, "orchestration_v2_projection_runs_thread_status_idx");
5394
+ }
5395
+ const lastExecutedIndex = plan.findIndex((detail) =>
5396
+ detail.includes("orchestration_v2_projection_runs_last_executed_idx"),
5397
+ );
5398
+ assert.notStrictEqual(lastExecutedIndex, -1);
5399
+ // The lookup that follows the search is the sort it no longer needs.
5400
+ assert.notInclude(plan[lastExecutedIndex + 1] ?? "", "USE TEMP B-TREE");
5401
+
5402
+ const countQueries = statements.filter((statement) => statement.includes("item_count"));
5403
+ assert.isNotEmpty(countQueries);
5404
+ for (const countQuery of countQueries) {
5405
+ for (const detail of yield* planOf(countQuery)) {
5406
+ assert.notInclude(detail, "orchestration_v2_projection_turn_items");
5407
+ }
5408
+ }
5409
}),
5410
);
5411
@@ -5636,6 +5660,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5660
},
5661
});
5662
}
5663
+ let runWrites = 0;
5664
const applyRun = (
5665
threadId: ThreadId,
5666
suffix: string,
@@ -5644,9 +5669,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5669
) => {
5670
const runId = RunId.make(`run:shell-item-counts:${suffix}`);
5671
const rootNodeId = NodeId.make(`node:shell-item-counts:${suffix}`);
5672
+ runWrites += 1;
5673
return projectionStore
5674
.apply({
5649
- id: EventId.make(`event:shell-item-counts:${suffix}:run`),
5675
+ id: EventId.make(`event:shell-item-counts:${suffix}:run:${runWrites}`),
5676
type: "run.created",
5677
threadId,
5678
runId,
@@ -5673,11 +5699,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5699
})
5700
.pipe(Effect.as(runId));
5701
};
5676
- let ordinal = 0;
5677
- const applyItem = (threadId: ThreadId, runId: RunId | null) => {
5678
- ordinal += 1;
5702
+ let items = 0;
5703
+ let writes = 0;
5704
+ // Passing `rewrite` writes that earlier item again, possibly under another run.
5705
+ const applyItem = (threadId: ThreadId, runId: RunId | null, rewrite?: number) => {
5706
+ const ordinal = rewrite ?? (items += 1);
5707
+ writes += 1;
5708
return projectionStore.apply({
5680
- id: EventId.make(`event:shell-item-counts:item:${ordinal}`),
5709
+ id: EventId.make(`event:shell-item-counts:write:${writes}`),
5710
type: "turn-item.updated",
5711
threadId,
5712
...(runId === null ? {} : { runId }),
@@ -5730,6 +5759,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5759
);
5760
assert.equal(shell?.itemCount, itemCount);
5761
}
5762
+
5763
+ // Writing an item again keeps its count; moving it to another run moves the count.
5764
+ const itemCountOf = (threadId: ThreadId) =>
5765
+ projectionStore.getThreadShell(threadId).pipe(Effect.map((shell) => shell?.itemCount));
5766
+ yield* applyItem(busyThreadId, completedRunId, 3);
5767
+ assert.equal(yield* itemCountOf(busyThreadId), 4);
5768
+ yield* applyItem(busyThreadId, rolledBackRunId, 3);
5769
+ assert.equal(yield* itemCountOf(busyThreadId), 3);
5770
+ yield* applyItem(busyThreadId, null, 3);
5771
+ assert.equal(yield* itemCountOf(busyThreadId), 4);
5772
+
5773
+ // A run's rollback uncounts its items, and writing the run again counts them back.
5774
+ yield* applyRun(busyThreadId, "busy-completed", 1, true);
5775
+ assert.equal(yield* itemCountOf(busyThreadId), 3);
5776
+ yield* applyRun(busyThreadId, "busy-completed", 1, false);
5777
+ assert.equal(yield* itemCountOf(busyThreadId), 4);
5778
+ // Items written before their run exist count once it does.
5779
+ const lateRunId = RunId.make("run:shell-item-counts:late");
5780
+ yield* applyItem(otherThreadId, lateRunId);
5781
+ assert.equal(yield* itemCountOf(otherThreadId), 1);
5782
+ yield* applyRun(otherThreadId, "late", 2, false);
5783
+ assert.equal(yield* itemCountOf(otherThreadId), 2);
5784
}),
5785
);
5786
apps/server/src/orchestration-v2/ProjectionStore.ts
+14
-14
@@ -6727,7 +6727,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6727
active.status AS active_run_status,
6728
(
6729
SELECT r.status
6730
+ -- Read active runs by status. Walking the ordinal index to skip this sort
6731
+ -- reads every run of a thread with none active.
6732
FROM orchestration_v2_projection_runs r
6733
+ INDEXED BY orchestration_v2_projection_runs_thread_status_idx
6734
WHERE r.thread_id = t.thread_id
6735
AND r.status IN ('preparing', 'starting', 'running', 'waiting')
6736
AND NOT ${isSilentShellRun("r")}
@@ -6742,6 +6745,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6745
r.requested_at
6746
)
6747
FROM orchestration_v2_projection_runs r
6748
+ INDEXED BY orchestration_v2_projection_runs_thread_status_idx
6749
WHERE r.thread_id = t.thread_id
6750
AND r.status IN ('preparing', 'starting', 'running', 'waiting')
6751
AND NOT ${isSilentShellRun("r")}
@@ -6804,6 +6808,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6808
SELECT secret.payload_json
6809
-- Keep runs outermost so completed history is never scanned for a pending secret.
6810
FROM orchestration_v2_projection_runs r
6811
+ INDEXED BY orchestration_v2_projection_runs_thread_status_idx
6812
CROSS JOIN orchestration_v2_projection_turn_items secret
6813
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
6814
WHERE r.thread_id = t.thread_id
@@ -6852,6 +6857,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6857
LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = (
6858
SELECT r.run_id
6859
FROM orchestration_v2_projection_runs r
6860
+ INDEXED BY orchestration_v2_projection_runs_thread_status_idx
6861
WHERE r.thread_id = t.thread_id
6862
AND r.status IN ('preparing', 'starting', 'running')
6863
AND NOT ${isSilentShellRun("r")}
@@ -6923,27 +6929,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6929
6930
const selectShellRunItemCounts = (threadIds: ReadonlyArray<ThreadId>) =>
6931
sql<ShellRunItemCountRow>`
6926
- SELECT thread_id, run_id, COUNT(*) AS item_count
6927
- FROM orchestration_v2_projection_turn_items
6928
- WHERE run_id IS NOT NULL AND thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
6929
- GROUP BY thread_id, run_id
6932
+ SELECT thread_id, run_key AS run_id, item_count
6933
+ FROM orchestration_v2_projection_turn_item_counts
6934
+ WHERE run_key <> '' AND thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
6935
`;
6936
6932
- // Count all selected threads in one index walk; fork sources still need per-run counts.
6937
const selectShellItemCounts = (threadIds: ReadonlyArray<ThreadId>) =>
6938
sql<ShellItemCountRow>`
6939
SELECT counts.thread_id,
6936
- SUM(CASE WHEN counts.run_id IS NULL OR r.status <> 'rolled_back'
6940
+ SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back'
6941
THEN counts.item_count ELSE 0 END) AS item_count,
6938
- SUM(CASE WHEN counts.run_id IS NULL THEN counts.item_count ELSE 0 END)
6942
+ SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END)
6943
AS runless_item_count
6940
- FROM (
6941
- SELECT thread_id, run_id, COUNT(*) AS item_count
6942
- FROM orchestration_v2_projection_turn_items
6943
- WHERE thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
6944
- GROUP BY thread_id, run_id
6945
- ) counts
6946
- LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_id
6944
+ FROM orchestration_v2_projection_turn_item_counts counts
6945
+ LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key
6946
+ WHERE counts.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
6947
GROUP BY counts.thread_id
6948
`;
6949
apps/server/src/persistence/Migrations.ts
+4
@@ -86,6 +86,8 @@ import Migration0080 from "./Migrations/080_ScheduledTaskThreadMode.ts";
86
import Migration0081 from "./Migrations/081_ProjectionMessageSearchText.ts";
87
import Migration0082 from "./Migrations/082_TurnItemConversationIndex.ts";
88
import Migration0083 from "./Migrations/083_CommandReadIndexes.ts";
89
+import Migration0084 from "./Migrations/084_LastExecutedRunIndex.ts";
90
+import Migration0085 from "./Migrations/085_TurnItemCounts.ts";
91
92
const migrationEntries = [
93
[1, "OrchestrationEvents", Migration0001],
@@ -172,6 +174,8 @@ const migrationEntries = [
174
[81, "ProjectionMessageSearchText", Migration0081],
175
[82, "TurnItemConversationIndex", Migration0082],
176
[83, "CommandReadIndexes", Migration0083],
177
+ [84, "LastExecutedRunIndex", Migration0084],
178
+ [85, "TurnItemCounts", Migration0085],
179
] as const;
180
181
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/084_LastExecutedRunIndex.ts
new
+18
@@ -0,0 +1,18 @@
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
+ // The shell's last executed run lookup sorts every run of the thread. This index carries
8
+ // that order and the same predicate the lookup filters on. A database that already has
9
+ // the index keeps it, so a hand-applied one does not fail the migration.
10
+ yield* sql`
11
+ CREATE INDEX IF NOT EXISTS orchestration_v2_projection_runs_last_executed_idx
12
+ ON orchestration_v2_projection_runs(
13
+ thread_id, (completed_at IS NULL) DESC, completed_at DESC, ordinal DESC, run_id DESC
14
+ )
15
+ WHERE status <> 'queued'
16
+ AND NOT (status = 'cancelled' AND json_extract(payload_json, '$.startedAt') IS NULL)
17
+ `;
18
+});
apps/server/src/persistence/Migrations/085_TurnItemCounts.test.ts
new
+60
@@ -0,0 +1,60 @@
1
+import { assert, it } from "@effect/vitest";
2
+import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3
+import * as Effect from "effect/Effect";
4
+import * as SqlClient from "effect/sql/SqlClient";
5
+
6
+import { runMigrations } from "../Migrations.ts";
7
+
8
+const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" }));
9
+
10
+layer("085_TurnItemCounts", (it) => {
11
+ it.effect("backfills item counts, then follows upserts, moves, and deletes", () =>
12
+ Effect.gen(function* () {
13
+ const sql = yield* SqlClient.SqlClient;
14
+ yield* runMigrations({ toMigrationInclusive: 84 });
15
+ const upsert = (id: string, threadId: string, runId: string | null) => sql`
16
+ INSERT INTO orchestration_v2_projection_turn_items (
17
+ turn_item_id, thread_id, run_id, ordinal, type, status, updated_at, payload_json
18
+ )
19
+ VALUES (
20
+ ${id}, ${threadId}, ${runId}, 1, 'assistant_message', 'completed',
21
+ '2026-10-29T00:00:00.000Z', '{}'
22
+ )
23
+ ON CONFLICT(turn_item_id) DO UPDATE SET
24
+ thread_id = excluded.thread_id,
25
+ run_id = excluded.run_id,
26
+ status = excluded.status
27
+ `;
28
+ yield* upsert("runless-1", "thread-1", null);
29
+ yield* upsert("runless-2", "thread-1", null);
30
+ yield* upsert("run-1", "thread-1", "run-1");
31
+
32
+ yield* runMigrations();
33
+
34
+ const counts = () =>
35
+ sql<{ readonly thread_id: string; readonly run_key: string; readonly item_count: number }>`
36
+ SELECT thread_id, run_key, item_count
37
+ FROM orchestration_v2_projection_turn_item_counts
38
+ ORDER BY thread_id, run_key
39
+ `;
40
+ assert.deepEqual(yield* counts(), [
41
+ { thread_id: "thread-1", run_key: "", item_count: 2 },
42
+ { thread_id: "thread-1", run_key: "run-1", item_count: 1 },
43
+ ]);
44
+
45
+ yield* upsert("run-1", "thread-1", "run-1");
46
+ yield* upsert("run-2", "thread-2", "run-2");
47
+ yield* upsert("runless-2", "thread-1", "run-1");
48
+ assert.deepEqual(yield* counts(), [
49
+ { thread_id: "thread-1", run_key: "", item_count: 1 },
50
+ { thread_id: "thread-1", run_key: "run-1", item_count: 2 },
51
+ { thread_id: "thread-2", run_key: "run-2", item_count: 1 },
52
+ ]);
53
+
54
+ yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE thread_id = 'thread-1'`;
55
+ assert.deepEqual(yield* counts(), [
56
+ { thread_id: "thread-2", run_key: "run-2", item_count: 1 },
57
+ ]);
58
+ }),
59
+ );
60
+});
apps/server/src/persistence/Migrations/085_TurnItemCounts.ts
new
+60
@@ -0,0 +1,60 @@
1
+import * as Effect from "effect/Effect";
2
+import * as SqlClient from "effect/sql/SqlClient";
3
+
4
+/**
5
+ * Shell reads count items per (thread, run) here instead of walking every turn item. Triggers
6
+ * keep it exact for every writer, including projection rebuilds and dev-database pruning.
7
+ */
8
+export default Effect.gen(function* () {
9
+ const sql = yield* SqlClient.SqlClient;
10
+
11
+ // run_key is '' for items without a run; NULLs never conflict in a primary key.
12
+ yield* sql`
13
+ CREATE TABLE orchestration_v2_projection_turn_item_counts (
14
+ thread_id TEXT NOT NULL,
15
+ run_key TEXT NOT NULL,
16
+ item_count INTEGER NOT NULL,
17
+ PRIMARY KEY (thread_id, run_key)
18
+ ) WITHOUT ROWID
19
+ `;
20
+ yield* sql`
21
+ INSERT INTO orchestration_v2_projection_turn_item_counts (thread_id, run_key, item_count)
22
+ SELECT thread_id, COALESCE(run_id, ''), COUNT(*)
23
+ FROM orchestration_v2_projection_turn_items
24
+ GROUP BY thread_id, run_id
25
+ `;
26
+ // An upsert that hits an existing item fires only the update trigger.
27
+ yield* sql`
28
+ CREATE TRIGGER orchestration_v2_turn_item_counts_insert
29
+ AFTER INSERT ON orchestration_v2_projection_turn_items
30
+ BEGIN
31
+ INSERT INTO orchestration_v2_projection_turn_item_counts (thread_id, run_key, item_count)
32
+ VALUES (new.thread_id, COALESCE(new.run_id, ''), 1)
33
+ ON CONFLICT (thread_id, run_key) DO UPDATE SET item_count = item_count + 1;
34
+ END
35
+ `;
36
+ yield* sql`
37
+ CREATE TRIGGER orchestration_v2_turn_item_counts_update
38
+ AFTER UPDATE OF thread_id, run_id ON orchestration_v2_projection_turn_items
39
+ WHEN old.thread_id IS NOT new.thread_id OR old.run_id IS NOT new.run_id
40
+ BEGIN
41
+ UPDATE orchestration_v2_projection_turn_item_counts SET item_count = item_count - 1
42
+ WHERE thread_id = old.thread_id AND run_key = COALESCE(old.run_id, '');
43
+ DELETE FROM orchestration_v2_projection_turn_item_counts
44
+ WHERE thread_id = old.thread_id AND run_key = COALESCE(old.run_id, '') AND item_count <= 0;
45
+ INSERT INTO orchestration_v2_projection_turn_item_counts (thread_id, run_key, item_count)
46
+ VALUES (new.thread_id, COALESCE(new.run_id, ''), 1)
47
+ ON CONFLICT (thread_id, run_key) DO UPDATE SET item_count = item_count + 1;
48
+ END
49
+ `;
50
+ yield* sql`
51
+ CREATE TRIGGER orchestration_v2_turn_item_counts_delete
52
+ AFTER DELETE ON orchestration_v2_projection_turn_items
53
+ BEGIN
54
+ UPDATE orchestration_v2_projection_turn_item_counts SET item_count = item_count - 1
55
+ WHERE thread_id = old.thread_id AND run_key = COALESCE(old.run_id, '');
56
+ DELETE FROM orchestration_v2_projection_turn_item_counts
57
+ WHERE thread_id = old.thread_id AND run_key = COALESCE(old.run_id, '') AND item_count <= 0;
58
+ END
59
+ `;
60
+});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+4
@@ -63,6 +63,8 @@ describe("V2 preview upgrade", () => {
63
[81, "ProjectionMessageSearchText"],
64
[82, "TurnItemConversationIndex"],
65
[83, "CommandReadIndexes"],
66
+ [84, "LastExecutedRunIndex"],
67
+ [85, "TurnItemCounts"],
68
]);
69
assert.deepStrictEqual(yield* runMigrations(), []);
70
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -169,6 +171,8 @@ describe("V2 preview upgrade", () => {
171
[81, "ProjectionMessageSearchText"],
172
[82, "TurnItemConversationIndex"],
173
[83, "CommandReadIndexes"],
174
+ [84, "LastExecutedRunIndex"],
175
+ [85, "TurnItemCounts"],
176
]);
177
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
178
);
docs/internals/performance-regressions.md
+3
-3
@@ -111,9 +111,9 @@ sessions. The orchestrator still validates the current thread before applying se
111
112
Read receipts use `ProjectionStore.getThread` to load only canonical thread metadata. A visit
113
advances `lastVisitedAt` monotonically without changing `updatedAt` or decoding history. Shell
114
-queries use a covering thread/run index for item counts and a partial index for nonterminal
115
-background items, including idle items. Latest user-message reads use an index ordered by update
116
-time.
114
+queries read item counts from a per-thread, per-run table that turn-item triggers keep exact, and
115
+use a partial index for nonterminal background items, including idle items. Latest user-message
116
+reads use an index ordered by update time.
117
118
Command palette results keep live VCS and linked pull-request queries leased to the visible
119
viewport, including a small overscan region. Cached badges remain available while leases are