Repository navigation
Scan only new thread-state rows on repeated latest-state lookups - #6838
Merged
Merged
Conversation
The latest-state lookup for a kind whose newest row is old or absent read and json_extracted every newer extensionState snapshot in the thread. The thread list repeats this every refresh, so threads whose provider publishes large snapshots often froze the server for most of a second each time. Remember, per connection, each (thread, kind) lookup's latest matching sequence and the newest state sequence already scanned, and on the next lookup scan only state rows past that watermark. No schema change: the existing partial index serves the bounded range seek. Cached entries are dropped when the thread's event rewrite generation or the database data_version changes, rescanned when the scanned watermark went backwards or the remembered row no longer matches, and never written from inside a transaction. Fixes #6825 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
listLatestThreadStateEventRowsByThreadIdsfinds the latest goal event orthread/extensionState/updatedrow of one kind per thread. Its partial index (events_thread_state_thread_sequence_idx) keys only(thread_id, sequence), not the kind. When the requested kind's latest row is old or absent, SQLite walks back through every newer extension-state row andjson_extracts itsdatato check the kind. #4931 removed the repeated re-scan but left this one scan per thread. The thread list repeats the lookup every refresh. With a provider that publishes ~20 KB snapshots per change (5,724 in one thread for the reporter), that means hundreds of MB of JSON parsed per refresh and 0.6–1.4 s event-loop freezes every ~2 s. The shape dates back to #2164 (Aug 21), which widened the index to all extension-state kinds; it is not a recent regression. Investigation report: https://get-bb.github.io/reports/issues/6825.htmlWhat changed
packages/db/src/data/events.ts: the lookup now keeps a per-connection watermark cache. For each (thread, kind) it remembers the latest matching sequence and the newest state sequence already scanned. Later lookups scan only state rows past that watermark, using a bounded(thread_id=? AND sequence>?)seek on the existing index, and then fetch the remembered row by(thread_id, sequence).eventswould stall startup on large databases (12.5 s on a 9 GB copy), and any malformeddatarow would fail the migration.plugins/provider-codex/src/async-questions.ts).PRAGMA data_versionchanges.DbQueryConnectiontoDbConnection; every caller already passes a connection.How you verified
New tests in
packages/db/test/data/events.test.ts(repeated latest-state lookups): appends, goal events, a rewind that reinserts at already-scanned sequences, a deleted remembered row, rows removed below the watermark, and a rolled-back transaction. I disabled each of the four guards in turn, and each time the matching test failed.packages/db/test/query-plans.test.ts: a warm lookup binds the previous watermark and uses asequence>?range seek onevents_thread_state_thread_sequence_idx, with no temp B-tree.Benchmark in the issue's shape (200 threads; 8 with 5,000 × 21 KB snapshots), thread-list lookup for
provider-codex/goal:pnpm exec turbo run test typecheck --filter=@bb/db(650 passed).turbo run typecheck --filter=@bb/server. Server suites covering the goal banner, timeline head state, session options and runtime display (174 passed).Ran
pnpm start:worktreeon this branch; the app and thread API respond normally.Fixes #6825
🤖 Generated with Claude Code