Skip to content

Latest timeline page decodes every event of an open turn on each build, bypassing timelineWindowEventBudget #4674

Open

Description

@mistakeknot

Summary

Building a thread's latest timeline page reads and decodes every event of every turn that touches the page, whatever timelineWindowEventBudget says. Agent turns can stay open for hours. When they do, each latest-page build decodes tens of thousands of events and tens of MB of JSON synchronously on the server's event loop. Every other request waits behind it, including the thread list and plugin RPCs.

This is related to #1749 (the per-event cost) and #3951 (rebuilds that are not shared), but it is a separate cause. The budget is bypassed entirely once a single turn is larger than the budget.

Where

apps/server/src/services/threads/timeline.ts, in selectStandardTimelineEventRows (upstream 049d6a932, around line 999). The group-context-query loop calls listStoredTimelineTurnEventRows with sequenceStart: epochSequenceStart and beforeSequence: maxSeq + 1 for every turn id on the page. It repeats until no new turn ids appear. A running turn with N events therefore costs N rows for every build, both at the tip and on older pages of the same turn.

The tests in apps/server/test/services/threads/timeline-in-turn-window.test.ts pin this behaviour. One example is "bounds a running turn that is larger than the whole budget", which asserts that the budgeted eventRowCount equals the unbudgeted count. The behaviour exists because leaf-index content cursors (contentCursor.beforeLeaf, contentPage.total) and stable turn summaries need the whole turn projected. So this looks like a design limit rather than a one-line bug.

Measured (one server, one day)

bb 0.44.0 (fork of upstream; the timeline code above is unchanged from upstream), Ubuntu 24.04, 32 cores, Node 22.22, about 20 concurrently running agent threads.

  • 371 Event loop stalled records in one day. The median stall was 0.77 s and the worst 5.9 s.
  • 254 of the 371 had slowestWork = timeline-build.
  • A representative profile: pageKind: latest, segmentLimit: 20, eventRowCount: 13463, eventDataBytes: 26.9 MB.
  • The two threads behind most of the stalls each had one open turn: 9,995 events (21 MB) over 17 hours, and 18,361 events over 39 hours.
  • A thread with 75,000 events in total but small turns caused no stalls. The cost follows turn size, not thread size.
  • The latest-selection memo (timeline-selection-memo.ts) absorbs only delta appends. Any non-delta event, such as a tool item starting or completing, misses the memo and triggers a full rebuild. An active agent produces those every few seconds.

Expected

A latest-page build's cost is bounded by the page, or at least is not repeated in full on every append to an open turn.

Options we see

  1. Cache decoded and projected output for the open turn, keyed by thread and max sequence. Extend it incrementally on append, and coalesce latest builds per thread. There is no wire change. We are prototyping this in our fork and will report numbers here.
  2. Move the decode and projection off the event loop (worker or chunked).
  3. Make open-turn content pagination sequence-based, so a page reads only its window. This changes the contract.

Workaround

We now hand off (replace) any thread whose recent turn exceeds 5,000 events once the thread goes idle. This caps the damage, but agents that block inside one turn still grow it until they stop.

Activity

  1. added theissue type on Oct 2, 2026
  2. added
    threads
    on Oct 2, 2026
  3. mistakeknot commented on Oct 2, 2026

    @mistakeknot
    ContributorAuthor

    Numbers from the fork prototype of option 1. Like the issue, it makes no wire-contract change, and the five pinned tests in timeline-in-turn-window.test.ts pass unchanged.

    There are two parts:

    • Widened latest-selection memo. It now also reuses the selection for top-level item/started and item/completed rows of the kinds agentMessage, commandExecution, fileChange, fileRead and reasoning, in turns it already knows. Tool calls, parented rows and turn lifecycle rows still rebuild.
    • Per-thread build coalescer for latest pages of active threads. For builds of 50 ms or more, it serves the last snapshot for a window of clamp(4 × build time, 250 ms, 500 ms). It then sends one trailing events-appended refresh. That refresh survives cache eviction and replacement, and is dropped on delete and history rewrite. Cache entries are keyed on every projection input, including the thread title, workspace root and plan command.

    Synthetic run: one running turn of 10,000 events, then 200 mixed appends (tool item started, output delta, item completed, message delta).

    builds full selection rebuilds total build ms
    before 201 100 ~10,000
    memo only 201 0 ~4,700
    memo + coalescer 8 0 ~400

    The response once the burst settles equals a cold build.

    The first build after a cache miss still costs the full projection, so this mitigates the problem rather than fixing it. A truly page-bounded build would still need option 2 or option 3. Happy to send this as a PR if the approach is acceptable.

  4. added
    confirmed-repro
    on Oct 2, 2026
  5. bb-slop-cop commented on Oct 2, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority: High · Effort: High · threads, perf.
    • Report: Reproduction and root-cause report.
    • Verdict: REPRODUCED · root-cause confidence: high · reproduction label: confirmed-repro.
    • Root cause: Turn-context expansion loads whole retained turns before response pagination; item lifecycle appends miss the selection memo and repeat full projection. Cached decode visits do not necessarily mean fresh JSON parsing.
    • Evidence: Two clean trusted-main checkouts reproduce a 100-leaf page selecting 6,003 events / 12.97 MB; one command append raises this to 6,005. The bounded-source regression fails in both; 47 relevant existing tests pass. The report includes the complete reproduction test.
    • No PR: A safe root-cause repair requires incremental projection/cache design or pagination-contract changes, outside the simple-fix criteria.

    AGENT GENERATED

  6. brsbl commented on Oct 8, 2026

    @brsbl
    Collaborator

    More production evidence for this, from a remote server running 0.45.1-nightly.37657450135.1 (04c6371b8a), Linux with 4 vCPU and an 8 GB bb.db. It covers about 30h of server.{50,51,52}.log and was read-only:

    • thr_46gxq5dyzh (one hot thread):
      • 254 logged Thread timeline build blocked the event loop entries, plus about 1,700 suppressed.
      • 123s of logged build time.
      • Worst build 7.1s, with eventRowCount 1,165 and 572 KB, for a latest page that returns 3–75 rows.
    • Slow TRANSACTION DEFERRED: 2,907 entries totaling 1,024s. The max of 9,466ms is the same as the worst timeline build.
    • Stall overlap: timeline builds overlap 98 of the 162 event-loop stalls longer than 1s.
    • User impact: while a long turn is running, each viewer refetch blocks every other request, including server→daemon RPCs, for 0.2–9s. That shows up as multi-second / command lookup.

    The largest stall on this server is a separate cause, filed as #5192.

    From Investigate slow command lookup

    AGENT GENERATED

  7. AnobleSCM commented on Oct 30, 2026

    @AnobleSCM

    More production evidence, from a different trigger: a community plugin that reads only contextWindowUsage.

    Setup: BB 0.48.0 on macOS, one server, Context Meter 0.1.6 (Hazihell/bb-plugin-context-meter), overnight Oct 29–30, 2026. Read-only, from the server log.

    Trigger: on every thread/contextWindowUsage/updated or thread/tokenUsage/updated change, the plugin calls threads.timeline({ summaryOnly: "true", segmentLimit: "1" }) to read contextWindowUsage. Those one-segment latest builds still selected the whole open turn.

    Numbers:

    • One thread's open turn held about 10,200 to 10,600 events (about 34 MB). Its segmentLimit: 1 builds took 8.4 to 15.3 s each (median 11.4 s).
    • 45 event-loop stalls of 3 s or more overnight; 40 of them fell within 12 minutes, when the loop was blocked for 386 of 720 s. The longest stall was 17.5 s.
    • 25 of the 45 stalls have a logged build inside them; 24 of those were segmentLimit: 1 builds.
    • That thread had about 13,600 events in all. As the issue says, the cost followed turn size, not thread size.
    • A/B check the next morning, in 10-minute windows: 21 one-segment builds with the plugin on, 0 with it disabled, then 2 and 28 after it was enabled again. No very long turn was running that morning, so there was no felt difference.

    Ask: a way to read contextWindowUsage without selecting turn events. For example, summaryOnly could skip event selection when no segments are needed, a small usage endpoint could serve it, or thread:changed metadata could carry the values. Meter-style plugins would then stay cheap whatever the turn size.

    AGENT GENERATED

  8. AnobleSCM commented on Oct 30, 2026

    @AnobleSCM

    Correction to the ask in our earlier comment: BB already has the cheap read we asked for. GET /threads/:id/context (#3540) returns contextWindowUsage from the usage rows alone, with no timeline build. We've pointed the plugin author to it (Hazihell/bb-plugin-context-meter#1), which removes that plugin's trigger.

    It doesn't change the main issue here: a latest-page build still selects the whole open turn.

    AGENT GENERATED

  9. AnobleSCM commented on Nov 7, 2026

    @AnobleSCM

    Another trigger for this, still present on 0.49.0: BB's own CLI. bb thread show <id> reads pendingTodos with threads.timeline({ summaryOnly: "true" }). On the server, buildThreadTimelineWithProfile runs the full buildThreadTimelineInternal latest-page build inside runEventLoopWorkSync, then returns rows: []. So every status check pays for a whole latest-page build and throws the rows away. bb thread commands and bb thread options read the same way.

    Measured on 0.49.0 (self-hosted server on macOS, Nov 7, 2026). One bb thread show <id> --json on an idle thread with a cold cache:

    • pageKind: latest, segmentLimit: 20, selectionStrategy: standard-window
    • eventRowCount 6,390, decodedEventCount 6,390, eventDataBytes 25,960,077 (about 26 MB)
    • 627 ms total build; the event loop stalled for 680 ms

    Why it matters: agents that poll their child threads with bb thread show repeat that build on every check. On Nov 6, on 0.48.1, latest-page builds on our largest threads took up to 50.3 s (21,397 events decoded, 15 MB). Builds that long dropped the phone and both Macs from the server at the same moment, the symptom in #6499. Our agents now poll with bb thread list --json, and we moved the largest threads to fresh ones. There have been no tunnel drops since.

    Ask:

    1. Let summaryOnly skip row selection and decoding, or give pendingTodos its own cheap read, as GET /threads/:id/context does for usage.
    2. Run long timeline builds off the main event loop, so heartbeats survive a slow build.
    3. Document the cheap way for an agent to poll thread status. In our test, bb thread wait on an idle thread caused no build.

    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