Skip to content

Read only the page's window of flat-rendered turns, and cut long running turns into budget-sized pages - #7327

Closed
ymichael wants to merge 2 commits into
mainfrom
bb/flat-turn-windowed-timeline-reads-split-out-of-7-thr_gr9htkx467
Closed

ymichael wants to merge 2 commits into
mainfrom
bb/flat-turn-windowed-timeline-reads-split-out-of-7-thr_gr9htkx467

Conversation

@ymichael

@ymichael ymichael commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A timeline page read every turn it touched from the start of history. thr_u738kqjs29's 17-row latest page read 7,654 events, because a background task in the window had a delegation parent in a 7,400-event turn. And because a window only ever started at a user message, a long running turn with no message inside it was a single page however long it ran: with u738 cut off mid-turn so a 5,842-event turn is still running, main's latest page is 393 rows, reading 5,866 events. This is the structural cause in #4674.

What changed

Rebased onto main after #7541 (display-position row ownership). No schema or wire change; cursors stay timeline-v3.

  • Windowed reads (timeline.ts, timeline-selection-memo.ts, packages/db/src/data/events.ts). A turn whose rows render flat (a running turn, or any turn under the flat display) with more than a sixth of the event budget outside the window (250 events at the default 1500) is read only inside it. The page adds:

    • the complete events of items that cross the window edges: items still open at the window start, and any item whose item/started is not among its in-window events, including items that stream without one (Claude reasoning);
    • every lifecycle event of an interaction (for example a question) with an event in the window, through listStoredInteractionLifecycleRows, which no longer filters to thread scope;
    • delegation parents and children;
    • the turn's lifecycle, accepted-input, compaction and provider rows.

    All other turns are read whole, as on main, including finished turns under the collapsed display. The latest-selection memo appends to windowed turns and applies the same interaction check before it appends.

  • Grid cuts inside long running turns (selectTimelineWindowStart). When no request row fits the budget inside a running turn, the window starts on a half-budget grid, moved back to the start of the item it lands in. The cut is always a turn-scoped request, turn or item lifecycle row, so held older cursors survive Stop pruning from making apps refetch, and keep older cursors valid after pruning #7284's pruning. There is no grid cut once the cut's turn has finished.

  • Batching: every new query goes through queryRawRowsInSqliteVariableBatches.

  • docs/timeline-pagination.md describes what a page reads, the grid, and the older-row bound.

Changes since the previous revision, which conflicted with #7541:

Source +937 / −61; tests +1,053; docs +33.

How you verified

Real data, in-process on a VACUUM INTO copy of a production bb.db (Nov 17), main (f1c5304) vs this branch. Medians of 5 cold reads (caches dropped between reads) in one process, so the timings show direction, not a controlled A/B.

case main branch
u738 latest (app) 7,654 rows read, 126 ms 784, 22 ms
6vfi7ireqq latest 1,712 rows, 16 ms 101, 7 ms
u738 app walk, total rows read 34,668 13,239
u738 with its last turn running, latest 393 rows shown, 5,866 read, 66 ms 246 shown, 2,200 read, 44 ms
Codex threads (avim9t7kpe, gtbhvapit2) unchanged rows read; timings within noise
  • Paged walks of u738, 6vfi, avim9t7kpe and gtbhvapit2, in app and log views, are byte-identical to main.
  • In the running case, page boundaries differ by design. The rows walked across all pages equal one full build: 878 rows, same order, same content.
  • BB_TIMELINE_HISTORY_DB=~/.bb/bb.db pnpm exec turbo run test:timeline-history --filter=@bb/server, on a fresh copy of the live database, matches one full build for every thread at budgets 1500 and 120. The previous revision, merged with main, failed it on thr_gcuc46ug4j (both budgets) and thr_7vjjsfxsns (budget 120).

Tests.

  • New: a reasoning item that streams without item/started and a steer starting the page inside it; and a question asked before the page and answered inside it. Both fail without this revision's fixes.
  • Removing the memo's interaction check fails 13 equivalence and stress seeds.
  • Carried tests: bounded reads of a long running flat turn, cross-turn delegations, two compactions in one turn, streaming items longer than the budget, plan-mode banner and model fallback inside the turn, grid cuts surviving pruning, a finished turn whose budget floor lands on thread-scoped progress, and large-IN batching.
  • Scratch stress run, not committed: 210 extra equivalence seeds (mixed 100–159 and 500–559, interleaved 200–259, extension states 300–329). 208 pass; seeds 250 and 321 also fail on main, with a delegation ordered before operation rows.
  • Commands:
    • pnpm exec turbo run typecheck lint:type-aware --filter=@bb/server --filter=@bb/db
    • pnpm exec turbo run test check:query-costs --filter=@bb/db: 67 files pass
    • server test/services/threads, test/system, test/public: 1,576 pass
    • oxlint and oxfmt --check on the touched files

Refs #4674

AGENT GENERATED

🤖 Generated with Claude Code

@ymichael
ymichael force-pushed the bb/flat-turn-windowed-timeline-reads-split-out-of-7-thr_gr9htkx467 branch from 495f17e to 7462c36 Compare November 17, 2026 04:03
ymichael and others added 2 commits November 17, 2026 16:30
…ing turns into budget-sized pages

A timeline page read every turn it touched from the start of history.
thr_u738kqjs29's 17-row latest page read 7,654 events because a
background task in the window had a delegation parent in a 7,400-event
turn, and a long running turn with no user message inside it was a
single page however long it ran (#4674).

A turn whose rows render flat (a running turn, or any turn under the
flat display) with many events outside the window is now read only
inside the window, plus the complete events of items that cross the
window edges, delegation parents and children, and the turn's
lifecycle, accepted-input, compaction and provider rows. Finished turns
under the collapsed display are still read whole.

When no request row fits the event budget inside a running turn, the
window starts on a half-budget grid, moved back to the start of the
item it lands in. The cut is always a request, turn or item lifecycle
row, which pruning never deletes.

Batched queries render once and run directly when they fit under the
SQLite variable limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged with #7541, windowed reads of flat turns disagreed with a full
build in three ways:

- An item whose events in the window do not include its item/started,
  such as Claude reasoning that streams deltas without one, was rebuilt
  from its in-window events only, so its row started late. The page now
  reads every event of such an item instead of looking for an
  item/started before the window.
- A turn-scoped question asked before the window and answered inside it
  kept only its in-window lifecycle events and moved. The interaction
  lifecycle lookup no longer filters to thread scope, the page reads it
  for interactions in windowed turns, and the latest-window memo checks
  the same condition before appending.
- Older pages loaded delegation children only when the delegation
  started inside the window. #7541 owns rows by display position, so
  this dropped or misplaced rows; the rule is removed.

A turn is now read whole when at most a sixth of the event budget lies
outside the window (250 events at the default 1500). With the
equivalence tests' 24-event budget, that windows turns in every seed;
before, five builds in one seed did.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/flat-turn-windowed-timeline-reads-split-out-of-7-thr_gr9htkx467 branch from a5bcf69 to af1dd64 Compare November 18, 2026 00:58
@ymichael

Copy link
Copy Markdown
Collaborator Author

Closing at Michael's direction. The problem is real and stays open in #4674: a page reads whole turns, so a small latest page can read thousands of events. This fix adds about 940 source lines of special cases to the window selection, and it was rebuilt on the row-ownership code that #7795 has since deleted. The examples and measurements here are being reused as the benchmark for a simpler read path.

AGENT GENERATED

@ymichael ymichael closed this Nov 18, 2026
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