Repository navigation
perf(server): thread shell item counts no longer sum every run the thread has had #39391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+235
−24
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
apps/server/src/persistence/Migrations/102_ThreadItemCounts.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<readonly [string, number]>) => | ||
| 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([]); | ||
| }), | ||
| ); | ||
| }); |
112 changes: 112 additions & 0 deletions
112
apps/server/src/persistence/Migrations/102_ThreadItemCounts.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| `; | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep hidden-run exclusion for items whose thread differs from their run's thread.
selectShellItemCountsnow requirescounts.thread_id = r.thread_id. The previous query matched count rows only byrun_id. An item whosethread_iddiffers from its run'sthread_idis therefore no longer excluded when that run becomesrolled_back,queued, orcancelled. The migration fixture shows this shape withlate:binthread-2andrun-lateinthread-1.This state can also occur in production. For an open child-thread turn item with a null
runId,RunExecutionService.tsemits the parent run ID while keeping the childthreadId, and the accept predicate admits child-thread items throughownsChildThread. Any count read for that child thread can then keep items that the run-status rules exclude.Restore run-status exclusion for each item's own thread. Subtract hidden-run items from each count row's thread, using the run's status regardless of which thread owns the run. The lookup may need an index that supports run-ID access by status. Extend the existing migration fixture with the
thread-2/run-latestatus transition, and assert thethread-2count afterrun-latebecomesrolled_back.