Repository navigation
Cache the read queries the warm-read check still listed - #8490
Merged
Merged
Conversation
The thread list, thread detail, head state, outline, count, child summary, prompt history and execution-default reads compiled their SQL on every call. They now bind values as placeholders and reuse one prepared statement per connection, keyed by shape where options change the SQL. Thread and event type lists bind as one JSON array through json_each. The warm-read check drops its "not cached yet" list. Tests that counted prepare calls as a proxy for query executions now count executions, and the query-plan test guard looks statement methods up at call time so execution recording sees cached statements. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Dec 2, 2026
…#8572) ## Human comments ## What was wrong #8497 listed five timeline readers in `packages/db/src/data/events.ts` that rebuilt their Drizzle query and called SQLite `prepare()` on every call. Two of them, `listTodoSnapshotEventRowsForThread` and `getLatestModelFallbackRowSinceTurnRequest`, were already cached on main by #8490. The other three were not: `isTimelineCursorSequencePresent`, `listTimelineInterruptionRows` and `listTimelineWindowHintsDescending`. Each call paid for compiling the SQL again, and SQLite prepared the statement again. ## What changed - The three readers now build their query once per connection through `prepareCachedQuery`, with placeholders for their fixed scalar arguments. This is the same pattern their sibling readers use. - `listTimelineWindowHintsDescending` changes from a raw `db.all(sql…)` to the query builder. It keeps the same `INDEXED BY events_thread_type_sequence_idx`, the same predicates and the same `DESC` order and limit. - The three readers accept `DbQueryConnection`, so calls inside a transaction use the same cache. - `EXPLAIN QUERY PLAN` is unchanged. All three still search their original index, and none of them builds a temp B-tree. ## How you verified - Added the three readers to the case table in `packages/db/test/data/events-preparation.test.ts`. The table checks correct results for fresh bindings and inside transactions, with zero `prepare()` calls after warm-up. All three new cases fail on main and pass with this change. - `pnpm exec turbo run typecheck test --filter=@bb/db`: 822 tests pass. - `pnpm exec turbo run typecheck --filter=@bb/server` passes. The server timeline, request-path-statements and thread-derived-cache-equivalence tests pass: 32 files, 298 tests. - The issue's repro script reports 0 prepares per call for all five readers. Before this change, three of them reported 1 per call. - Real data: the events of a 52,700-event thread copied from a production `bb.db`, with 2,000 warm calls per reader. Old and new code return identical results. | Reader | main | this PR | |---|---|---| | `isTimelineCursorSequencePresent` | 46.7 µs | 6.4 µs | | `listTimelineInterruptionRows` | 142.1 µs | 58.8 µs | | `listTimelineWindowHintsDescending` | 26.5 µs | 24.6 µs | Fixes #8497 > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Human comments
What was wrong
#8463 added a warm-read check to
apps/server/test/perf/request-path-statements.test.ts, and it listed 13 read functions that compile their SQL again on every call. Each builds a fresh Drizzle query, so every thread list, thread detail, head state, conversation outline, thread count, child summary, prompt history and default-execution-options request recompiles the SQL and prepares a new SQLite statement on the server's main thread.What changed
packages/db/src/data/events.ts,threads.ts,prompt-history.ts,project-execution-defaults.ts: the 13 reads bind their values as placeholders and reuse one prepared statement per connection throughprepareCachedQuery.prepareCachedQueryVariant, with a key that holds every option that changes the SQL and nothing else:countThreads,listThreadsWithPendingInteractionState,listStoredEventRows, and the root and full conversation outline rows.listStoredEventRowskeepstype = ?for a single type, so the merge path'sORDER BY … LIMITstill walks the index.json_each, like Cache prepared statements for timeline window readers #8453's turn IDs. This covers thread IDs in the thread list and several event types inlistStoredEventRows. The query plans keep the same index searches (shown below).countThreadsWhereandlistThreadsWhereare gone. Their four callers each have a fixed-shape statement.request-path-statements.test.tsdrops the "not cached yet" list. Every read path now compiles each fixed query once; the varying-shape list is unchanged.preparecalls as "the query ran": a cached statement is never prepared again, so those counts go to zero.apps/server/test/helpers/statement-executions.tsrecords statement executions, cached or not.PRAGMA data_versioncheck that runs on that path.packages/db/src/query-plan-test-guard.ts: its per-statement wrappers look upall/get/run/iterateon the prototype at call time instead of binding them at prepare time. Without this, execution recording would miss statements prepared before it started, and only on platforms where the guard is installed (it is a no-op on Windows).No wire, CLI, schema or stored-data changes.
How you verified
Real data. A
VACUUM INTOcopy of a productionbb.db(1,297 threads, 1.19M events). Main's and this branch's versions of the four data files ran in alternating processes, three runs each. The numbers are median warm CPU per call, over the 30 busiest threads, 30 largest parents and every project. Every reader returned identical rows on main and on the branch, in all six runs.Query plans on the same copy.
type IN (SELECT value FROM json_each(?))keepsSEARCH events USING COVERING INDEX events_thread_type_sequence_idx (thread_id=? AND type=?).id IN (SELECT value FROM json_each(?))keepsSEARCH threads USING INDEX sqlite_autoindex_threads_1 (id=?).New
packages/db/test/data/read-preparation.test.tscovers 23 reads and shapes. Each one first reads an empty subject, which warms the cache, then reads a populated subject, then alternates between them, and asserts the expected rows with no newpreparecalls. Two mutations fail it:listStoredEventRowsstatement: 5 failures;The warm-read check passes with the "not cached yet" list removed.
pnpm exec turbo run typecheck lint lint:type-aware test --filter=@bb/db: 819 tests pass.pnpm exec turbo run typecheck lint lint:type-aware --filter=@bb/serverpasses.A full
@bb/serverrun before the test migration failed only the two prepare-counting tests and one builtin-plugin hot-reload test. That last test passes on its own. All five migrated files pass.🤖 Generated with Claude Code