diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index dabe53aeae46f..79478def6dfd1 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -7178,6 +7178,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { if (/^SEARCH (r|run) /.test(detail)) { assert.include(detail, "COVERING INDEX", detail); } + // A thread's item count reads one count row per hidden run, not one per run it had. + if (detail.startsWith("SEARCH counts ")) { + assert.include(detail, "run_key=?", detail); + } if (detail.includes("orchestration_v2_projection_turn_items")) { // Only held input is read from the sparse index; transcript history stays unread. assert.include( diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 930639b5d6bbe..a8d06679c801a 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -8565,30 +8565,32 @@ export const layer: Layer.Layer = const selectShellItemCounts = (threadIds: ReadonlyArray) => sql` - SELECT counts.thread_id, - SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' - THEN counts.item_count - CASE - WHEN r.status IN ('queued', 'cancelled') THEN ( - SELECT COUNT(*) - FROM orchestration_v2_projection_turn_items held - INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx - WHERE held.run_id = counts.run_key - AND held.type = 'user_message' - AND json_valid(held.payload_json) - AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' - ) - ELSE 0 - END - ELSE 0 END) AS item_count, - SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) - AS runless_item_count - FROM orchestration_v2_projection_turn_item_counts counts - -- The primary key would load each run's whole row just for its status. - LEFT JOIN orchestration_v2_projection_runs r - INDEXED BY orchestration_v2_projection_runs_run_status_idx - ON r.run_id = counts.run_key - WHERE counts.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)})) - GROUP BY counts.thread_id + -- Only the thread's hidden runs are visited, so settled history costs nothing here. + SELECT totals.thread_id, + totals.item_count - COALESCE(( + SELECT SUM(CASE WHEN r.status = 'rolled_back' THEN counts.item_count ELSE ( + SELECT COUNT(*) + FROM orchestration_v2_projection_turn_items held + INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx + WHERE held.run_id = counts.run_key + AND held.type = 'user_message' + AND json_valid(held.payload_json) + AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' + ) END) + FROM orchestration_v2_projection_runs r + INDEXED BY orchestration_v2_projection_runs_hidden_items_idx + JOIN orchestration_v2_projection_turn_item_counts counts + ON counts.thread_id = r.thread_id AND counts.run_key = r.run_id + WHERE r.thread_id = totals.thread_id + AND r.status IN ('rolled_back', 'queued', 'cancelled') + ), 0) AS item_count, + COALESCE(( + SELECT runless.item_count + FROM orchestration_v2_projection_turn_item_counts runless + WHERE runless.thread_id = totals.thread_id AND runless.run_key = '' + ), 0) AS runless_item_count + FROM orchestration_v2_projection_thread_item_counts totals + WHERE totals.thread_id IN (SELECT value FROM json_each(${encodeIdList(threadIds)})) `; const selectShellProviderThreadRows = (threadIds?: ReadonlyArray) => diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index c3fc7aacef055..6f0b74e029606 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -109,6 +109,7 @@ import Migration0098 from "./Migrations/098_ActiveShellIndexIndexes.ts"; import Migration0099 from "./Migrations/099_BackgroundRecoveryIndexes.ts"; import Migration0100 from "./Migrations/100_TurnStartReadIndexes.ts"; import Migration0101 from "./Migrations/101_DelegatedTaskReadIndexes.ts"; +import Migration0102 from "./Migrations/102_ThreadItemCounts.ts"; const migrationEntries = [ [1, "OrchestrationEvents", Migration0001], @@ -213,6 +214,7 @@ const migrationEntries = [ [99, "BackgroundRecoveryIndexes", Migration0099], [100, "TurnStartReadIndexes", Migration0100], [101, "DelegatedTaskReadIndexes", Migration0101], + [102, "ThreadItemCounts", Migration0102], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/102_ThreadItemCounts.test.ts b/apps/server/src/persistence/Migrations/102_ThreadItemCounts.test.ts new file mode 100644 index 0000000000000..678186e55b81c --- /dev/null +++ b/apps/server/src/persistence/Migrations/102_ThreadItemCounts.test.ts @@ -0,0 +1,89 @@ +import { assert, it } from "@effect/vitest"; +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +import { runMigrations } from "../Migrations.ts"; + +const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); + +layer("102_ThreadItemCounts", (it) => { + it.effect("matches a recount of items without a run or with an existing run", () => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* runMigrations({ toMigrationInclusive: 101 }); + const upsertItem = (id: string, threadId: string, runId: string | null) => sql` + INSERT INTO orchestration_v2_projection_turn_items ( + turn_item_id, thread_id, run_id, ordinal, type, status, updated_at, payload_json + ) + VALUES ( + ${id}, ${threadId}, ${runId}, 1, 'assistant_message', 'completed', + '2026-11-19T00:00:00.000Z', '{}' + ) + ON CONFLICT(turn_item_id) DO UPDATE SET thread_id = excluded.thread_id, run_id = excluded.run_id + `; + const insertRun = (runId: string, threadId: string) => sql` + INSERT INTO orchestration_v2_projection_runs ( + run_id, thread_id, ordinal, provider, status, requested_at, payload_json + ) + VALUES ( + ${runId}, ${threadId}, + (SELECT COUNT(*) FROM orchestration_v2_projection_runs WHERE thread_id = ${threadId}), + 'codex', 'completed', '2026-11-19T00:00:00.000Z', '{}' + ) + `; + const totals = () => sql<{ readonly thread_id: string; readonly item_count: number }>` + SELECT thread_id, item_count FROM orchestration_v2_projection_thread_item_counts + ORDER BY thread_id + `; + const recount = () => sql<{ readonly thread_id: string; readonly item_count: number }>` + SELECT item.thread_id, COUNT(*) AS item_count + FROM orchestration_v2_projection_turn_items item + WHERE item.run_id IS NULL + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs run WHERE run.run_id = item.run_id) + GROUP BY item.thread_id + ORDER BY item.thread_id + `; + const assertTotals = (expected: ReadonlyArray) => + Effect.gen(function* () { + const actual = yield* totals(); + assert.deepEqual(actual, yield* recount()); + assert.deepEqual( + actual.map((row) => [row.thread_id, row.item_count]), + expected.map(([threadId, count]) => [threadId, count]), + ); + }); + + yield* insertRun("run-1", "thread-1"); + yield* upsertItem("runless", "thread-1", null); + yield* upsertItem("run-1:a", "thread-1", "run-1"); + yield* upsertItem("late:a", "thread-1", "run-late"); + yield* runMigrations(); + yield* assertTotals([["thread-1", 2]]); + + yield* upsertItem("run-1:b", "thread-1", "run-1"); + yield* upsertItem("late:b", "thread-2", "run-late"); + yield* assertTotals([["thread-1", 3]]); + + yield* insertRun("run-late", "thread-1"); + yield* assertTotals([ + ["thread-1", 4], + ["thread-2", 1], + ]); + + yield* upsertItem("run-1:b", "thread-2", null); + yield* upsertItem("late:a", "thread-1", "run-missing"); + yield* assertTotals([ + ["thread-1", 2], + ["thread-2", 2], + ]); + + yield* sql`DELETE FROM orchestration_v2_projection_runs WHERE run_id = 'run-late'`; + yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = 'run-1:b'`; + yield* assertTotals([["thread-1", 2]]); + + yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE thread_id = 'thread-1'`; + yield* assertTotals([]); + }), + ); +}); diff --git a/apps/server/src/persistence/Migrations/102_ThreadItemCounts.ts b/apps/server/src/persistence/Migrations/102_ThreadItemCounts.ts new file mode 100644 index 0000000000000..5cbfee859cd45 --- /dev/null +++ b/apps/server/src/persistence/Migrations/102_ThreadItemCounts.ts @@ -0,0 +1,112 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +/** + * One total per thread beside 085's per-run counts, so a shell read does not sum every run the + * thread has had. It holds the items without a run or with an existing run, the rows the shell + * read used to sum; the read still applies the run status rules. Triggers keep it exact. + */ +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + yield* sql` + CREATE TABLE orchestration_v2_projection_thread_item_counts ( + thread_id TEXT PRIMARY KEY, + item_count INTEGER NOT NULL + ) WITHOUT ROWID + `; + yield* sql` + INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count) + SELECT counts.thread_id, SUM(counts.item_count) + FROM orchestration_v2_projection_turn_item_counts counts + WHERE counts.run_key = '' + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = counts.run_key) + GROUP BY counts.thread_id + `; + + // The shell read subtracts only these runs. ProjectionStore.selectShellItemCounts must spell + // the same predicate for SQLite to use this partial index. + yield* sql` + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_runs_hidden_items_idx + ON orchestration_v2_projection_runs(thread_id, status, run_id) + WHERE status IN ('rolled_back', 'queued', 'cancelled') + `; + + // 085's triggers upsert per-run counts, so an item write fires the insert or the update here. + yield* sql` + CREATE TRIGGER orchestration_v2_thread_item_counts_count_insert + AFTER INSERT ON orchestration_v2_projection_turn_item_counts + WHEN new.run_key = '' + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = new.run_key) + BEGIN + INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count) + VALUES (new.thread_id, new.item_count) + ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count; + END + `; + yield* sql` + CREATE TRIGGER orchestration_v2_thread_item_counts_count_update + AFTER UPDATE ON orchestration_v2_projection_turn_item_counts + BEGIN + UPDATE orchestration_v2_projection_thread_item_counts + SET item_count = item_count - old.item_count + WHERE thread_id = old.thread_id + AND (old.run_key = '' + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = old.run_key)); + INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count) + SELECT new.thread_id, new.item_count + WHERE new.run_key = '' + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = new.run_key) + ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count; + DELETE FROM orchestration_v2_projection_thread_item_counts + WHERE thread_id = old.thread_id AND item_count <= 0; + END + `; + yield* sql` + CREATE TRIGGER orchestration_v2_thread_item_counts_count_delete + AFTER DELETE ON orchestration_v2_projection_turn_item_counts + WHEN old.run_key = '' + OR EXISTS (SELECT 1 FROM orchestration_v2_projection_runs WHERE run_id = old.run_key) + BEGIN + UPDATE orchestration_v2_projection_thread_item_counts + SET item_count = item_count - old.item_count + WHERE thread_id = old.thread_id; + DELETE FROM orchestration_v2_projection_thread_item_counts + WHERE thread_id = old.thread_id AND item_count <= 0; + END + `; + + // Items can be written before their run; they count from when it exists. + yield* sql` + CREATE TRIGGER orchestration_v2_thread_item_counts_run_insert + AFTER INSERT ON orchestration_v2_projection_runs + BEGIN + INSERT INTO orchestration_v2_projection_thread_item_counts (thread_id, item_count) + SELECT thread_id, COUNT(*) + FROM orchestration_v2_projection_turn_items + WHERE run_id = new.run_id + GROUP BY thread_id + ON CONFLICT (thread_id) DO UPDATE SET item_count = item_count + excluded.item_count; + END + `; + yield* sql` + CREATE TRIGGER orchestration_v2_thread_item_counts_run_delete + AFTER DELETE ON orchestration_v2_projection_runs + BEGIN + UPDATE orchestration_v2_projection_thread_item_counts + SET item_count = item_count - ( + SELECT COUNT(*) + FROM orchestration_v2_projection_turn_items item + WHERE item.run_id = old.run_id + AND item.thread_id = orchestration_v2_projection_thread_item_counts.thread_id + ) + WHERE thread_id IN ( + SELECT thread_id FROM orchestration_v2_projection_turn_items WHERE run_id = old.run_id + ); + DELETE FROM orchestration_v2_projection_thread_item_counts + WHERE item_count <= 0 AND thread_id IN ( + SELECT thread_id FROM orchestration_v2_projection_turn_items WHERE run_id = old.run_id + ); + END + `; +}); diff --git a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts index b9feabbf89b88..1703789b6b479 100644 --- a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts +++ b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts @@ -81,6 +81,7 @@ describe("V2 preview upgrade", () => { [99, "BackgroundRecoveryIndexes"], [100, "TurnStartReadIndexes"], [101, "DelegatedTaskReadIndexes"], + [102, "ThreadItemCounts"], ]); assert.deepStrictEqual(yield* runMigrations(), []); assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports); @@ -205,6 +206,7 @@ describe("V2 preview upgrade", () => { [99, "BackgroundRecoveryIndexes"], [100, "TurnStartReadIndexes"], [101, "DelegatedTaskReadIndexes"], + [102, "ThreadItemCounts"], ]); }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), );