Repository navigation
Seek turn and item in timeline backfill reads instead of walking the thread - #7295
Merged
ymichael merged 1 commit intoNov 9, 2026
Conversation
ymichael
force-pushed
the
bb/fix-timeline-backfill-reads-that-walk-the-thread-thr_qdy9sx4b5p
branch
from
November 9, 2026 20:24
83bf35e to
9d0ef11
Compare
…thread listStoredItemLifecycleRowsByItems and listStoredBufferedTextDeltaRowsByItems only constrained thread_id (and sequence) through events_thread_sequence_idx, then filtered every event in the thread. They now name events_thread_turn_type_item_sequence_idx and push thread and type into each scope group of the OR, so every item seeks (thread, turn, type, item). Their known-slow entries in the query-plan allowlist are removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/fix-timeline-backfill-reads-that-walk-the-thread-thr_qdy9sx4b5p
branch
from
November 9, 2026 21:19
9d0ef11 to
e826a6c
Compare
ymichael
deleted the
bb/fix-timeline-backfill-reads-that-walk-the-thread-thr_qdy9sx4b5p
branch
November 9, 2026 21:27
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
When a timeline window starts in the middle of an item,
apps/server/src/services/threads/timeline.tsbackfills that item with two reads inpackages/db/src/data/events.ts:listStoredItemLifecycleRowsByItemsandlistStoredBufferedTextDeltaRowsByItems. Both putthread_idand the event type outside the per-scopeORthat matches(scope_kind, turn_id, item_id). The database has nosqlite_stat1, so SQLite picksevents_thread_sequence_idx (thread_id=?)(the deltas read also usessequence<?) to avoid a sort, then filters every earlier event in the thread. That costs 7–8 ms per call on a 53k-event production thread. The query-plan guard prototype (#7292) flagged both functions oncetimeline-item-backfill.test.tsexercised them.What changed
events_thread_turn_type_item_sequence_idxwithINDEXED BY, like Seek the type and turn indexes in five events queries that walked the thread #7288. They passthread_id, the type list and (for deltas) thesequence <cutoff intoscopedItemRefsPredicateas shared predicates, the same waylistItemEventSpansByItemsalready does. Each scope group now seeks(thread_id, turn_id, type, item_id[, sequence<]). Items from several turns use aMULTI-INDEX ORwith one seek per group. Thread-scoped items seekturn_id IS NULL.storedEventRowSqlFields, because Drizzle needssqlfields whenfromis asqlfragment. Rows and their order (ORDER BY sequence) are unchanged.packages/db/test/query-plans.test.tspins both plans, for one item and for items from two turns plus a thread-scoped item.packages/db/test/data/timeline-item-backfill.test.tsis copied unchanged from Fail tests on slow SQLite query plans #7292. It covers turn scoping, type filtering and thebeforeSequencecutoff, and adding an identical file merges cleanly whichever PR lands first. Once both PRs land, the two allowlist entries Fail tests on slow SQLite query plans #7292 adds for these functions are stale and should be deleted.No schema, migration or wire changes.
How you verified
Before/after on real data. I called the real functions on the production-subset database from #7292's investigation (325k events). The heavy thread is
thr_gtbhvapit2(53k events, max sequence 117k). The reasoning case usesthr_6e9knccemi(26k events), because the heavy thread has no reasoning deltas. Inputs are real items from those threads: recent delegations, the latest agent message with stored deltas, and a mix of two turns. The script and theEXPLAIN QUERY PLANwere the same on main and on this branch. Each number is the median of 51 runs. Returned rows are identical (ids, sequences and data lengths compared).Plans on that database:
SEARCH events USING INDEX events_thread_sequence_idx (thread_id=?)(lifecycle), or(thread_id=? AND sequence<?)(deltas).SEARCH events USING INDEX events_thread_turn_type_item_sequence_idx (thread_id=? AND turn_id=? AND type=? AND item_id=?), withAND sequence<?added for deltas, once per scope group, followed by a temp B-tree forORDER BY sequence.Tests.
events_thread_sequence_idxplans above and pass here.query-plans.test.tsandtimeline-item-backfill.test.ts: 56 tests pass.apps/server/test/services/threads/timeline*files (185 tests) pass.turbo run typecheck lint --filter=@bb/db --filter=@bb/serverpasses.🤖 Generated with Claude Code