Skip to content

Seek turn and item in timeline backfill reads instead of walking the thread - #7295

Merged
ymichael merged 1 commit into
mainfrom
bb/fix-timeline-backfill-reads-that-walk-the-thread-thr_qdy9sx4b5p
Nov 9, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/fix-timeline-backfill-reads-that-walk-the-thread-thr_qdy9sx4b5p

Conversation

@ymichael

@ymichael ymichael commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

When a timeline window starts in the middle of an item, apps/server/src/services/threads/timeline.ts backfills that item with two reads in packages/db/src/data/events.ts: listStoredItemLifecycleRowsByItems and listStoredBufferedTextDeltaRowsByItems. Both put thread_id and the event type outside the per-scope OR that matches (scope_kind, turn_id, item_id). The database has no sqlite_stat1, so SQLite picks events_thread_sequence_idx (thread_id=?) (the deltas read also uses sequence<?) 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 once timeline-item-backfill.test.ts exercised them.

What changed

  • Both reads now name events_thread_turn_type_item_sequence_idx with INDEXED BY, like Seek the type and turn indexes in five events queries that walked the thread #7288. They pass thread_id, the type list and (for deltas) the sequence < cutoff into scopedItemRefsPredicate as shared predicates, the same way listItemEventSpansByItems already does. Each scope group now seeks (thread_id, turn_id, type, item_id[, sequence<]). Items from several turns use a MULTI-INDEX OR with one seek per group. Thread-scoped items seek turn_id IS NULL.
  • The selected columns switch to the existing storedEventRowSqlFields, because Drizzle needs sql fields when from is a sql fragment. Rows and their order (ORDER BY sequence) are unchanged.
  • packages/db/test/query-plans.test.ts pins both plans, for one item and for items from two turns plus a thread-scoped item.
  • packages/db/test/data/timeline-item-backfill.test.ts is copied unchanged from Fail tests on slow SQLite query plans #7292. It covers turn scoping, type filtering and the beforeSequence cutoff, 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 uses thr_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 the EXPLAIN QUERY PLAN were 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).

Call Main This PR
lifecycle, 1 delegation 7.39 ms 0.20 ms
lifecycle, 3 delegations in one turn 8.47 ms 0.22 ms
lifecycle, 2 items in two turns 8.11 ms 0.21 ms
buffered deltas, agent message 7.29 ms 0.11 ms
buffered deltas, reasoning (26k-event thread) 1.18 ms 0.12 ms
buffered deltas, 2 items in two turns 8.27 ms 0.13 ms

Plans on that database:

  • Main: SEARCH events USING INDEX events_thread_sequence_idx (thread_id=?) (lifecycle), or (thread_id=? AND sequence<?) (deltas).
  • This PR: SEARCH events USING INDEX events_thread_turn_type_item_sequence_idx (thread_id=? AND turn_id=? AND type=? AND item_id=?), with AND sequence<? added for deltas, once per scope group, followed by a temp B-tree for ORDER BY sequence.

Tests.

  • The new plan tests fail on main with the events_thread_sequence_idx plans above and pass here.
  • query-plans.test.ts and timeline-item-backfill.test.ts: 56 tests pass.
  • The 22 apps/server/test/services/threads/timeline* files (185 tests) pass.
  • turbo run typecheck lint --filter=@bb/db --filter=@bb/server passes.
  • I ran only these targeted tests locally; CI runs the full suites.

AGENT GENERATED

🤖 Generated with Claude Code

@ymichael
ymichael force-pushed the bb/fix-timeline-backfill-reads-that-walk-the-thread-thr_qdy9sx4b5p branch from 83bf35e to 9d0ef11 Compare November 9, 2026 20:24
…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
ymichael force-pushed the bb/fix-timeline-backfill-reads-that-walk-the-thread-thr_qdy9sx4b5p branch from 9d0ef11 to e826a6c Compare November 9, 2026 21:19
@ymichael
ymichael merged commit ccdc846 into main Nov 9, 2026
31 of 32 checks passed
@ymichael
ymichael deleted the bb/fix-timeline-backfill-reads-that-walk-the-thread-thr_qdy9sx4b5p branch November 9, 2026 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant