From cc33d794d49ee5f52d8ebc9723fdf1987841900e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 06:09:07 +0700 Subject: [PATCH 1/2] perf(server): thread shell item counts no longer sum every run the thread has had (#39391) Ported from pingdotgg/t3code@2bba2564ca. The migration takes Onicode's next free number, 107, and its comments name Onicode migration 91 (upstream 85, the same TurnItemCounts). Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/ProjectionStore.test.ts | 4 + .../src/orchestration-v2/ProjectionStore.ts | 50 ++++---- apps/server/src/persistence/Migrations.ts | 2 + .../Migrations/107_ThreadItemCounts.test.ts | 89 ++++++++++++++ .../Migrations/107_ThreadItemCounts.ts | 112 ++++++++++++++++++ .../reconcileV2PreviewMigration.test.ts | 2 + scripts/upstream-sync.ledger.tsv | 1 + 7 files changed, 236 insertions(+), 24 deletions(-) create mode 100644 apps/server/src/persistence/Migrations/107_ThreadItemCounts.test.ts create mode 100644 apps/server/src/persistence/Migrations/107_ThreadItemCounts.ts diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 501085d8f6..eb0d21bc3b 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -7198,6 +7198,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 840b3e94a4..80cd66024c 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -8650,30 +8650,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 d754fad0a5..e883c83cc0 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -114,6 +114,7 @@ import Migration0103 from "./Migrations/103_ActiveShellIndexIndexes.ts"; import Migration0104 from "./Migrations/104_BackgroundRecoveryIndexes.ts"; import Migration0105 from "./Migrations/105_TurnStartReadIndexes.ts"; import Migration0106 from "./Migrations/106_DelegatedTaskReadIndexes.ts"; +import Migration0107 from "./Migrations/107_ThreadItemCounts.ts"; const migrationEntries = [ [1, "OrchestrationEvents", Migration0001], @@ -223,6 +224,7 @@ const migrationEntries = [ [104, "BackgroundRecoveryIndexes", Migration0104], [105, "TurnStartReadIndexes", Migration0105], [106, "DelegatedTaskReadIndexes", Migration0106], + [107, "ThreadItemCounts", Migration0107], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/107_ThreadItemCounts.test.ts b/apps/server/src/persistence/Migrations/107_ThreadItemCounts.test.ts new file mode 100644 index 0000000000..dc90e434dc --- /dev/null +++ b/apps/server/src/persistence/Migrations/107_ThreadItemCounts.test.ts @@ -0,0 +1,89 @@ +import { assert, it } from "@effect/vitest"; +import * as NodeSqliteClient from "@onicode/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("107_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: 106 }); + 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/107_ThreadItemCounts.ts b/apps/server/src/persistence/Migrations/107_ThreadItemCounts.ts new file mode 100644 index 0000000000..4af5683f0f --- /dev/null +++ b/apps/server/src/persistence/Migrations/107_ThreadItemCounts.ts @@ -0,0 +1,112 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +/** + * One total per thread beside 091'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') + `; + + // 091'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 441b261792..1bab368b46 100644 --- a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts +++ b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts @@ -86,6 +86,7 @@ describe("V2 preview upgrade", () => { [104, "BackgroundRecoveryIndexes"], [105, "TurnStartReadIndexes"], [106, "DelegatedTaskReadIndexes"], + [107, "ThreadItemCounts"], ]); assert.deepStrictEqual(yield* runMigrations(), []); assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports); @@ -215,6 +216,7 @@ describe("V2 preview upgrade", () => { [104, "BackgroundRecoveryIndexes"], [105, "TurnStartReadIndexes"], [106, "DelegatedTaskReadIndexes"], + [107, "ThreadItemCounts"], ]); }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), ); diff --git a/scripts/upstream-sync.ledger.tsv b/scripts/upstream-sync.ledger.tsv index bcfab04060..969a534fb8 100644 --- a/scripts/upstream-sync.ledger.tsv +++ b/scripts/upstream-sync.ledger.tsv @@ -2796,3 +2796,4 @@ babc83baf1 take fix(web): PR viewer renders GitHub :emoji: shortcodes (#39315) 6b6950adfe take taken as is (Android-only Kotlin; ReviewDiffGeometryTest passes) 57db750b8f take taken as is (Copilot ACP only) cb391b5b57 skip relay AgentAwarenessRelay only, removed in Onicode; Onicode's push path retries failed activity sends every 60 s only while agents work, so a failed final completed update has the same gap: queued as a follow-up +2bba2564ca take migration renumbered to Onicode 107; comments name Onicode 091 (upstream 085, TurnItemCounts) From 6926a573a69503d8f81f2a96700d6e1bc6ef2fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4ng=20Tr=E1=BA=A7n?= Date: Fri, 20 Nov 2026 06:14:53 +0700 Subject: [PATCH 2/2] docs(internals): shell item counts read a per-thread total Follow-up to the port of t3code@2bba2564ca. Co-Authored-By: Claude Opus 5.5 --- docs/internals/performance-regressions.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/internals/performance-regressions.md b/docs/internals/performance-regressions.md index 7415a546a2..8ca8e1dc2a 100644 --- a/docs/internals/performance-regressions.md +++ b/docs/internals/performance-regressions.md @@ -125,8 +125,9 @@ sessions. The orchestrator still validates the current thread before applying se Read receipts use `ProjectionStore.getThread` to load only canonical thread metadata. A visit advances `lastVisitedAt` monotonically without changing `updatedAt` or decoding history. Shell -queries read item counts from a per-thread, per-run table that turn-item triggers keep exact, and -use a partial index for nonterminal background items, including idle items. Latest user-message +queries read item counts from a per-thread total that triggers keep exact, subtract only +rolled-back, queued and cancelled runs, and use a partial index for nonterminal background items, +including idle items. Latest user-message reads use an index ordered by update time. A projection read that orders messages by their first user or assistant turn item groups those