Repository navigation
Description
Summary
When a timeline page has two or more missing parent item IDs, listStoredDelegatingItemRowsByItemIds can scan every event in that thread even though BB already has a thread-and-item partial index for the lookup. On a long-lived thread, this can block the server event loop while the timeline and unrelated requests load. This is not specific to Crew or a Captain workflow: ordinary Claude Code local_bash progress can supply parent IDs that are correctly absent from the delegating-item result.
Versions and environment
- Observed with bb 0.46.0 desktop on macOS 27.0.1; Claude Code 2.1.301.
- Source-path reproduction on
mainat4363ba27d6f942d6b42ae61c8f85800e756b9085, Node 22.19.0, using BB's migrated in-memory DB test harness. - Standalone planner reproduction below used the macOS
sqlite3CLI 3.54.0. No remote machine or custom data directory is required for the query-plan defect.
Steps to reproduce
-
Run this copy-pasteable, stats-less SQLite reproduction. Its table and two indexes retain the relevant columns and ordering of BB's migrated
eventstable:sqlite3 :memory: <<'SQL' CREATE TABLE events (thread_id TEXT NOT NULL, item_id TEXT, item_kind TEXT, type TEXT NOT NULL, sequence INTEGER NOT NULL); CREATE UNIQUE INDEX events_thread_sequence_idx ON events(thread_id, sequence); CREATE INDEX events_delegating_item_lookup_idx ON events(thread_id, item_id, sequence, item_kind) WHERE item_kind IN ('toolCall', 'delegation'); EXPLAIN QUERY PLAN SELECT * FROM events WHERE thread_id = 't' AND item_id IN ('a', 'b', 'c') AND item_kind IN ('toolCall', 'delegation') AND type IN ('item/started', 'item/completed') ORDER BY sequence; SQL
-
In BB's real migrated in-memory DB, the regression test in the proposed fix invokes the exported lookup with three IDs, including a non-delegating
commandExecutionID, and captures its emitted SQL. With the new test but the oldevents.tsquery, its row assertion passes and its EXPLAIN assertion fails on the same thread-sequence plan. With the two-line query fix, both pass. The test uses the timeline's numeric output cap. -
The user-facing case is a long thread whose latest window references two or more parents outside the visible window. Open its latest timeline page. The query's cost then grows with total thread events, including when the requested parent IDs have no delegating rows.
Expected vs actual
Expected: seek requested IDs through events_delegating_item_lookup_idx (thread_id=? AND item_id=?), returning only matching toolCall/delegation lifecycle rows in sequence order.
Actual standalone output (and the old-code migrated-DB test's plan):
QUERY PLAN
`--SEARCH events USING INDEX events_thread_sequence_idx (thread_id=?)
That access path reads the thread's events to find a few item IDs. In one 177k-event local thread, a timeline build recorded about 9.3 seconds, with about 9.0 seconds in a nested DB call matching this multi-ID lookup shape. That observation motivated the investigation; the deterministic test above establishes the access-path bug, not that this one query explains every long-timeline cost.
Evidence
- Lookup at the tested base commit:
INIDs, literal partial-index kind predicate, andORDER BY sequencewith ordinary.from(events). - Existing partial index:
(thread_id, item_id, sequence, item_kind)for toolCall/delegation rows. - The test captures the actual exported function's SQL on
createConnection(":memory:")plusmigrate(db), not a mocked DB. Red:events_thread_sequence_idx (thread_id=?); green:events_delegating_item_lookup_idx (thread_id=? AND item_id=?), with identical full returned rows and order. - Investigation BB thread:
thr_58t6bg78qg(local to the reporting server; not a public URL).
What you ruled out
- Not Latest timeline page decodes every event of an open turn on each build, bypassing timelineWindowEventBudget #4674: that issue reads and decodes an entire open turn. This lookup can scan the entire thread to resolve only a few missing parent IDs, even if the current turn is small.
- Not Timeline response cache admits nothing above 200 rows, so identical requests each rebuild the projection #3951: identical-response cache admission is separate from this query's access path.
- Slow / command lookup on remote servers: server stalls (hourly vacuum, timeline builds) dominate, not round trips #5192 tracks broad server stalls and cites timeline builds, but not this specific missing-parent index plan.
- A single requested ID already uses the partial index. The stats-less planner switches to the thread-sequence index at multiple IDs.
ANALYZEis not required to demonstrate the failure and is not proposed as a production workaround.
Suggested priority and effort
Performance bug in long-running threads; it can delay every request sharing the server event loop but does not lose data. The source fix is small and uses an existing index, with one query-plan regression. Remaining timeline costs are separate.
AGENT GENERATED
Activity
bb-slop-cop commentedon Oct 16, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotthreads,perf, andconfirmed-repro; no existing classification was replaced.events_thread_sequence_idx (thread_id=?), traversing thread history instead of seeking item IDs through the existing delegating-item partial index.fix(db): seek missing timeline parents by item (#5599)
fix(db): seek background-task and request-outcome events by index (#5655