Skip to content

Multi-ID timeline parent lookup scans every event in long threads #5598

Closed

Description

@Danielalnajjar

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 main at 4363ba27d6f942d6b42ae61c8f85800e756b9085, Node 22.19.0, using BB's migrated in-memory DB test harness.
  • Standalone planner reproduction below used the macOS sqlite3 CLI 3.54.0. No remote machine or custom data directory is required for the query-plan defect.

Steps to reproduce

  1. Run this copy-pasteable, stats-less SQLite reproduction. Its table and two indexes retain the relevant columns and ordering of BB's migrated events table:

    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
  2. 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 commandExecution ID, and captures its emitted SQL. With the new test but the old events.ts query, 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.

  3. 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: IN IDs, literal partial-index kind predicate, and ORDER BY sequence with 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:") plus migrate(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

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

  1. added theissue type on Oct 16, 2026
  2. added
    threads
    confirmed-repro
    on Oct 16, 2026
  3. bb-slop-cop commented on Oct 16, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority Medium · Effort Low. Applied missing values and labels threads, perf, and confirmed-repro; no existing classification was replaced.
    • Report: https://get-bb.github.io/reports/issues/5598.html
    • Verdict: REPRODUCED · root-cause confidence high.
    • Evidence: A newly authored regression against the actual exported lookup fails at the query-plan assertion in two clean trusted-main checkouts, with both uncapped and 32,000-character output. Returned-row assertions pass. The report embeds the complete regression patch and repeat commands; released-app latency was not measured.
    • Root cause: The multi-ID parent lookup selects events_thread_sequence_idx (thread_id=?), traversing thread history instead of seeking item IDs through the existing delegating-item partial index.
    • No new PR: Open draft PR fix(db): seek missing timeline parents by item #5599 already explicitly closes this issue. No duplicate fix was pushed, and linked PR code was not executed.

    AGENT GENERATED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reproperfthreads

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions