Skip to content

Guard timeline builds against superlinear growth - #7861

Merged
ymichael merged 1 commit into
mainfrom
bb/review-and-verify-pr-7835-thr_nb3pyvh4g9
Nov 19, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/review-and-verify-pr-7835-thr_nb3pyvh4g9

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The timeline build in @bb/thread-view had no check against cost growing faster than the data. #7754 replaced an indexed lookup with a per-file scan of every row. Behavior tests passed, and the regression stayed in until #7835. Adding a growth check found two more quadratic paths in group-event-projection-turns.ts:

  • applyExternalUserBoundaries filtered every root user message in the thread once per turn (turns × messages).
  • findRootTurnRunningAtSequence scanned every turn once per thread-scoped warning or interaction prompt (turns × prompts).

What changed

  • New test, packages/thread-view/test/timeline-build-growth.test.ts. It builds timelines for 16 thread shapes at 100 and at 400 units: files in one edit (same and reversed completion order), file changes, commands, command output deltas, assistant messages and deltas, reasoning deltas, tool calls, reads and searches, web searches, plan updates, delegations, user messages sent during a running turn, turns, and turns that each raise a thread-scoped permission prompt.
    • It counts work during the build: array callback, scan and copy methods, plus Array/Map/Set iteration and Object.keys/values/entries.
    • It requires 4× the input to cost less than 6× the work. Linear code measures about 4×; quadratic code about 16×.
    • It counts work rather than timing it, so it is deterministic and cannot flake under load.
    • The prototype wrappers exist only during the synchronous build and are restored in finally.
  • applyExternalUserBoundaries: sorts the boundary sequences once, then binary-searches each turn's window. The output is already ascending, so the per-turn sort is gone.
  • Thread-scoped messages: a single sweep resolves the running turn for all of them (findRootTurnsRunningAtThreadMessages). It walks turns sorted by start and keeps a stack that discards turns completed before the current message.
  • No wire, protocol, CLI, persisted-data or UI changes.

How you verified

  • The new test fails on the code it guards:
  • Real data, read-only export of 7 local threads (the four with the most turns, 1,370/798/641/720, plus the three with the most thread-scoped interaction prompts):
    • JSON.stringify(buildThreadTimelineFromEvents(...)) hashes are identical before and after, for completedTurnDisplay collapse/expand × includeNestedRows true/false.

    • Full-build medians:

      thread turns before after
      thr_cdfq9maj8q 1,370 ~395 ms ~345–357 ms
      thr_d5ej4e2gtv 798 ~357 ms ~303–322 ms

      Smaller threads show no measurable change.

  • pnpm exec turbo run test typecheck lint:type-aware lint --filter=@bb/thread-view passes.
  • pnpm exec turbo run test --filter=@bb/server -- test/services/threads/timeline test/public/public-thread-timeline test/timeline-history passes: 263 passed, 2 skipped. The skipped tests are history replays that need BB_TIMELINE_HISTORY_DB.

AGENT GENERATED

🤖 Generated with Claude Code

Add a deterministic growth check for buildThreadTimelineFromEvents. It
counts array, Map, Set and Object.keys/values/entries work during one
build and requires 4x the events to cost under 6x the work, across 16
thread shapes (large file changes, many commands, deltas, turns, steered
messages, thread-scoped prompts). It counts work instead of timing it,
so it cannot flake. It fails on the matchFileRows regression fixed in
#7835 (ratios 11.4 and 13.7).

It found two more quadratic paths in group-event-projection-turns:

- applyExternalUserBoundaries filtered every root user message for each
  turn. It now sorts the boundaries once and binary-searches each
  turn's window.
- Each thread-scoped warning or prompt scanned every turn to find the
  running one. One sweep over turns sorted by start now resolves all of
  them.

Real threads produce byte-identical timelines (7 threads, four option
combinations). Full builds of the 1,370-turn and 798-turn threads drop
from ~395 ms to ~350 ms and from ~357 ms to ~315 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 52af0dd into main Nov 19, 2026
39 checks passed
@ymichael
ymichael deleted the bb/review-and-verify-pr-7835-thr_nb3pyvh4g9 branch November 19, 2026 22:17
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