@devries-cameron20 / t3code-1 / commits / 2bba2564ca
Fork 0

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

perf(server): thread shell item counts no longer sum every run the thread has had (#39391)

Julius Marminge committed Nov 19, 2026 at 15:03 UTC 2bba2564cafd651a1ca36de1b2b8492ad9124498
6 files changed +235 -24
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+4
@@ -7178,6 +7178,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
7178 if (/^SEARCH (r|run) /.test(detail)) {
7179 assert.include(detail, "COVERING INDEX", detail);
7180 }
7181 + // A thread's item count reads one count row per hidden run, not one per run it had.
7182 + if (detail.startsWith("SEARCH counts ")) {
7183 + assert.include(detail, "run_key=?", detail);
7184 + }
7185 if (detail.includes("orchestration_v2_projection_turn_items")) {
7186 // Only held input is read from the sparse index; transcript history stays unread.
7187 assert.include(
apps/server/src/orchestration-v2/ProjectionStore.ts
+26 -24
@@ -8565,30 +8565,32 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
8565
8566 const selectShellItemCounts = (threadIds: ReadonlyArray<ThreadId>) =>
8567 sql<ShellItemCountRow>`
8568 - SELECT counts.thread_id,
8569 - SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back'
8570 - THEN counts.item_count - CASE
8571 - WHEN r.status IN ('queued', 'cancelled') THEN (
8572 - SELECT COUNT(*)
8573 - FROM orchestration_v2_projection_turn_items held
8574 - INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx
8575 - WHERE held.run_id = counts.run_key
8576 - AND held.type = 'user_message'
8577 - AND json_valid(held.payload_json)
8578 - AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn'
8579 - )
8580 - ELSE 0
8581 - END
8582 - ELSE 0 END) AS item_count,
8583 - SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END)
8584 - AS runless_item_count
8585 - FROM orchestration_v2_projection_turn_item_counts counts
8586 - -- The primary key would load each run's whole row just for its status.
8587 - LEFT JOIN orchestration_v2_projection_runs r
8588 - INDEXED BY orchestration_v2_projection_runs_run_status_idx
8589 - ON r.run_id = counts.run_key
8590 - WHERE counts.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
8591 - GROUP BY counts.thread_id
8568 + -- Only the thread's hidden runs are visited, so settled history costs nothing here.
8569 + SELECT totals.thread_id,
8570 + totals.item_count - COALESCE((
8571 + SELECT SUM(CASE WHEN r.status = 'rolled_back' THEN counts.item_count ELSE (
8572 + SELECT COUNT(*)
8573 + FROM orchestration_v2_projection_turn_items held
8574 + INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx
8575 + WHERE held.run_id = counts.run_key
8576 + AND held.type = 'user_message'
8577 + AND json_valid(held.payload_json)
8578 + AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn'
8579 + ) END)
8580 + FROM orchestration_v2_projection_runs r
8581 + INDEXED BY orchestration_v2_projection_runs_hidden_items_idx
8582 + JOIN orchestration_v2_projection_turn_item_counts counts
8583 + ON counts.thread_id = r.thread_id AND counts.run_key = r.run_id
8584 + WHERE r.thread_id = totals.thread_id
8585 + AND r.status IN ('rolled_back', 'queued', 'cancelled')
8586 + ), 0) AS item_count,
8587 + COALESCE((
8588 + SELECT runless.item_count
8589 + FROM orchestration_v2_projection_turn_item_counts runless
8590 + WHERE runless.thread_id = totals.thread_id AND runless.run_key = ''
8591 + ), 0) AS runless_item_count
8592 + FROM orchestration_v2_projection_thread_item_counts totals
8593 + WHERE totals.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)}))
8594 `;
8595
8596 const selectShellProviderThreadRows = (threadIds?: ReadonlyArray<ThreadId>) =>
apps/server/src/persistence/Migrations.ts
+2
@@ -109,6 +109,7 @@ import Migration0098 from "./Migrations/098_ActiveShellIndexIndexes.ts";
109 import Migration0099 from "./Migrations/099_BackgroundRecoveryIndexes.ts";
110 import Migration0100 from "./Migrations/100_TurnStartReadIndexes.ts";
111 import Migration0101 from "./Migrations/101_DelegatedTaskReadIndexes.ts";
112 +import Migration0102 from "./Migrations/102_ThreadItemCounts.ts";
113
114 const migrationEntries = [
115 [1, "OrchestrationEvents", Migration0001],
@@ -213,6 +214,7 @@ const migrationEntries = [
214 [99, "BackgroundRecoveryIndexes", Migration0099],
215 [100, "TurnStartReadIndexes", Migration0100],
216 [101, "DelegatedTaskReadIndexes", Migration0101],
217 + [102, "ThreadItemCounts", Migration0102],
218 ] as const;
219
220 export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/102_ThreadItemCounts.test.ts new
+89
@@ -0,0 +1,89 @@
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("102_ThreadItemCounts", (it) => {
11 + it.effect("matches a recount of items without a run or with an existing run", () =>
12 + Effect.gen(function* () {
13 + const sql = yield* SqlClient.SqlClient;
14 + yield* runMigrations({ toMigrationInclusive: 101 });
15 + const upsertItem = (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-11-19T00:00:00.000Z', '{}'
22 + )
23 + ON CONFLICT(turn_item_id) DO UPDATE SET thread_id = excluded.thread_id, run_id = excluded.run_id
24 + `;
25 + const insertRun = (runId: string, threadId: string) => sql`
26 + INSERT INTO orchestration_v2_projection_runs (
27 + run_id, thread_id, ordinal, provider, status, requested_at, payload_json
28 + )
29 + VALUES (
30 + ${runId}, ${threadId},
31 + (SELECT COUNT(*) FROM orchestration_v2_projection_runs WHERE thread_id = ${threadId}),
32 + 'codex', 'completed', '2026-11-19T00:00:00.000Z', '{}'
33 + )
34 + `;
35 + const totals = () => sql<{ readonly thread_id: string; readonly item_count: number }>`
36 + SELECT thread_id, item_count FROM orchestration_v2_projection_thread_item_counts
37 + ORDER BY thread_id
38 + `;
39 + const recount = () => sql<{ readonly thread_id: string; readonly item_count: number }>`
40 + SELECT item.thread_id, COUNT(*) AS item_count
41 + FROM orchestration_v2_projection_turn_items item
42 + WHERE item.run_id IS NULL
43 + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs run WHERE run.run_id = item.run_id)
44 + GROUP BY item.thread_id
45 + ORDER BY item.thread_id
46 + `;
47 + const assertTotals = (expected: ReadonlyArray<readonly [string, number]>) =>
48 + Effect.gen(function* () {
49 + const actual = yield* totals();
50 + assert.deepEqual(actual, yield* recount());
51 + assert.deepEqual(
52 + actual.map((row) => [row.thread_id, row.item_count]),
53 + expected.map(([threadId, count]) => [threadId, count]),
54 + );
55 + });
56 +
57 + yield* insertRun("run-1", "thread-1");
58 + yield* upsertItem("runless", "thread-1", null);
59 + yield* upsertItem("run-1:a", "thread-1", "run-1");
60 + yield* upsertItem("late:a", "thread-1", "run-late");
61 + yield* runMigrations();
62 + yield* assertTotals([["thread-1", 2]]);
63 +
64 + yield* upsertItem("run-1:b", "thread-1", "run-1");
65 + yield* upsertItem("late:b", "thread-2", "run-late");
66 + yield* assertTotals([["thread-1", 3]]);
67 +
68 + yield* insertRun("run-late", "thread-1");
69 + yield* assertTotals([
70 + ["thread-1", 4],
71 + ["thread-2", 1],
72 + ]);
73 +
74 + yield* upsertItem("run-1:b", "thread-2", null);
75 + yield* upsertItem("late:a", "thread-1", "run-missing");
76 + yield* assertTotals([
77 + ["thread-1", 2],
78 + ["thread-2", 2],
79 + ]);
80 +
81 + yield* sql`DELETE FROM orchestration_v2_projection_runs WHERE run_id = 'run-late'`;
82 + yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = 'run-1:b'`;
83 + yield* assertTotals([["thread-1", 2]]);
84 +
85 + yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE thread_id = 'thread-1'`;
86 + yield* assertTotals([]);
87 + }),
88 + );
89 +});
apps/server/src/persistence/Migrations/102_ThreadItemCounts.ts new
+112
@@ -0,0 +1,112 @@
1 +import * as Effect from "effect/Effect";
2 +import * as SqlClient from "effect/sql/SqlClient";
3 +
4 +/**
5 + * One total per thread beside 085's per-run counts, so a shell read does not sum every run the
6 + * thread has had. It holds the items without a run or with an existing run, the rows the shell
7 + * read used to sum; the read still applies the run status rules. Triggers keep it exact.
8 + */
9 +export default Effect.gen(function* () {
10 + const sql = yield* SqlClient.SqlClient;
11 +
12 + yield* sql`
13 + CREATE TABLE orchestration_v2_projection_thread_item_counts (
14 + thread_id TEXT PRIMARY KEY,
15 + item_count INTEGER NOT NULL
16 + ) WITHOUT ROWID
17 + `;
18 + yield* sql`
19 + INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count)
20 + SELECT counts.thread_id, SUM(counts.item_count)
21 + FROM orchestration_v2_projection_turn_item_counts counts
22 + WHERE counts.run_key = ''
23 + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = counts.run_key)
24 + GROUP BY counts.thread_id
25 + `;
26 +
27 + // The shell read subtracts only these runs. ProjectionStore.selectShellItemCounts must spell
28 + // the same predicate for SQLite to use this partial index.
29 + yield* sql`
30 + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_runs_hidden_items_idx
31 + ON orchestration_v2_projection_runs(thread_id, status, run_id)
32 + WHERE status IN ('rolled_back', 'queued', 'cancelled')
33 + `;
34 +
35 + // 085's triggers upsert per-run counts, so an item write fires the insert or the update here.
36 + yield* sql`
37 + CREATE TRIGGER orchestration_v2_thread_item_counts_count_insert
38 + AFTER INSERT ON orchestration_v2_projection_turn_item_counts
39 + WHEN new.run_key = ''
40 + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = new.run_key)
41 + BEGIN
42 + INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count)
43 + VALUES (new.thread_id, new.item_count)
44 + ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count;
45 + END
46 + `;
47 + yield* sql`
48 + CREATE TRIGGER orchestration_v2_thread_item_counts_count_update
49 + AFTER UPDATE ON orchestration_v2_projection_turn_item_counts
50 + BEGIN
51 + UPDATE orchestration_v2_projection_thread_item_counts
52 + SET item_count = item_count - old.item_count
53 + WHERE thread_id = old.thread_id
54 + AND (old.run_key = ''
55 + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = old.run_key));
56 + INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count)
57 + SELECT new.thread_id, new.item_count
58 + WHERE new.run_key = ''
59 + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = new.run_key)
60 + ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count;
61 + DELETE FROM orchestration_v2_projection_thread_item_counts
62 + WHERE thread_id = old.thread_id AND item_count <= 0;
63 + END
64 + `;
65 + yield* sql`
66 + CREATE TRIGGER orchestration_v2_thread_item_counts_count_delete
67 + AFTER DELETE ON orchestration_v2_projection_turn_item_counts
68 + WHEN old.run_key = ''
69 + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = old.run_key)
70 + BEGIN
71 + UPDATE orchestration_v2_projection_thread_item_counts
72 + SET item_count = item_count - old.item_count
73 + WHERE thread_id = old.thread_id;
74 + DELETE FROM orchestration_v2_projection_thread_item_counts
75 + WHERE thread_id = old.thread_id AND item_count <= 0;
76 + END
77 + `;
78 +
79 + // Items can be written before their run; they count from when it exists.
80 + yield* sql`
81 + CREATE TRIGGER orchestration_v2_thread_item_counts_run_insert
82 + AFTER INSERT ON orchestration_v2_projection_runs
83 + BEGIN
84 + INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count)
85 + SELECT thread_id, COUNT(*)
86 + FROM orchestration_v2_projection_turn_items
87 + WHERE run_id = new.run_id
88 + GROUP BY thread_id
89 + ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count;
90 + END
91 + `;
92 + yield* sql`
93 + CREATE TRIGGER orchestration_v2_thread_item_counts_run_delete
94 + AFTER DELETE ON orchestration_v2_projection_runs
95 + BEGIN
96 + UPDATE orchestration_v2_projection_thread_item_counts
97 + SET item_count = item_count - (
98 + SELECT COUNT(*)
99 + FROM orchestration_v2_projection_turn_items item
100 + WHERE item.run_id = old.run_id
101 + AND item.thread_id = orchestration_v2_projection_thread_item_counts.thread_id
102 + )
103 + WHERE thread_id IN (
104 + SELECT thread_id FROM orchestration_v2_projection_turn_items WHERE run_id = old.run_id
105 + );
106 + DELETE FROM orchestration_v2_projection_thread_item_counts
107 + WHERE item_count <= 0 AND thread_id IN (
108 + SELECT thread_id FROM orchestration_v2_projection_turn_items WHERE run_id = old.run_id
109 + );
110 + END
111 + `;
112 +});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -81,6 +81,7 @@ describe("V2 preview upgrade", () => {
81 [99, "BackgroundRecoveryIndexes"],
82 [100, "TurnStartReadIndexes"],
83 [101, "DelegatedTaskReadIndexes"],
84 + [102, "ThreadItemCounts"],
85 ]);
86 assert.deepStrictEqual(yield* runMigrations(), []);
87 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -205,6 +206,7 @@ describe("V2 preview upgrade", () => {
206 [99, "BackgroundRecoveryIndexes"],
207 [100, "TurnStartReadIndexes"],
208 [101, "DelegatedTaskReadIndexes"],
209 + [102, "ThreadItemCounts"],
210 ]);
211 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
212 );