Skip to content

timelineWindowEventBudget assumes 0.06ms/event; measured 0.479ms/event on a 4-core host #1749

Open
Datahazed/bb
#5

Description

@Joesirven

Summary

timelineWindowEventBudget is calibrated at ~0.06 ms/event. Measured on a 4-core VPS the real cost is 0.479 ms/event at p50, roughly 8x higher. The budget's stated goal of "a cold build near 100ms" therefore costs about 720 ms on this class of hardware, and each build blocks the event loop.

The constant

packages/domain/src/feature-flags.ts:31-37:

/**
 * Measured on real threads a build costs ~0.06ms/event across the SQLite
 * read, JSON decode, and projection. 1500 keeps a cold build near 100ms; the
 * 10k-event thread that motivated the bound was ~670ms unbounded.
 */
timelineWindowEventBudget: 1_500,

Measured on this host

bb 0.38.0, Ubuntu 22.04.5, 4 cores / 4.9 GiB, Node v24.19.0.

Across 541 samples taken from the server's own Thread timeline build blocked the event loop logging:

metric value
p50 0.479 ms/event
p90 0.964 ms/event
max 5.55 ms/event

A representative logged build: eventRowCount: 1257, decodedEventCount: 1257, eventDataBytes: 1481754, returning responseRowCount: 96 from segmentLimit: 20. Stage timings were event-query 82 ms, event-json-decode 181 ms, thread-view-projection 69 ms. So 1257 events were read and decoded to produce 96 rows.

Aggregate impact in one session: 532 logged slow builds totalling 155 seconds of blocked event loop, plus 1124 suppressed, for 1656 slow builds. In a single hour: 342. Event-loop stall samples (n=375) showed maxDelay p50 of 1098 ms, p95 of 5058 ms, and a maximum of 50.6 seconds.

The budget rarely binds

Of 76 threads on this install, only 2 exceed 1500 events. Six exceed 1000 and nineteen exceed 500. So for 74 of 76 threads the budget never applies and the full event set is read on every build.

Because the cache key includes maxSeq (timeline-cache.ts:126-130), every appended event invalidates the entry, so an actively updating thread rebuilds continuously and the previous entry becomes garbage. Each build churns roughly 0.5-1.5 MB of transient parsed JSON, which is a significant contributor to server heap pressure (see the related heap-sizing issue).

Suggested fix

The flag is already overridable via BB_FF_TIMELINE_WINDOW_EVENT_BUDGET (packages/config/src/env-vars.ts:233), and lowering it to 400 on this host puts cold builds near 190 ms and makes the budget bind for 19 threads instead of 2. That works as an operator workaround.

The underlying issue is that a fixed event count is a poor proxy for cost when per-event cost varies 8x across hardware. Options:

  1. Calibrate the budget at startup from a measured sample rather than a hardcoded constant.
  2. Budget on decoded bytes rather than event count, since event-json-decode is the dominant stage.
  3. At minimum, document that the constant assumes fast hardware and should be lowered on small hosts, and mention the env var somewhere discoverable.

Worth noting the logging here is genuinely good. The Thread timeline build blocked the event loop records with per-stage timings are what made this measurable at all.

Activity

  1. added theissue type on Aug 18, 2026
  2. added
    host
    on Aug 18, 2026
  3. SawyerHood commented on Aug 18, 2026

    @SawyerHood
    Collaborator

    Reproduction & root-cause report: https://get-bb.github.io/reports/issues/1749.html

    • Verdict: PARTIALLY REPRODUCED · root-cause confidence: high
    • Root cause (short): timelineWindowEventBudget bounds synchronous timeline builds by a fixed event count justified by a one-host "0.06ms/event" figure, but build cost is CPU- and payload-byte-dependent (SQLite read + JSON.parse ~80%), so on slow hosts a full 1500-event window blocks the event loop far longer than the ~100ms the constant promises; the existing 4 MiB byte cut is not operator-tunable and never engages for latency.
    • Linked PR review: docs: record that the timeline event budget is hardware dependent #1756 → REQUEST CHANGES — Docs-only; bakes one VPS's unverifiable and selection-biased numbers (derived only from >=150ms slow-build log lines) into the shared feature-flags.ts JSDoc, and the configuration.md recipe omits that the log samples only the slow tail and that a whole finished turn is always returned even above the
    • Proposed fix: Promote the existing THREAD_TIMELINE_EVENT_DATA_BYTE_LIMIT (4 MiB, applyTimelineWindowByteBudget / findStoredTimelineWindowByteBudgetFloor) to a feature flag timelineWindowByteBudget (BB_FF_TIMELINE_WINDOW_BYTE_BUDGET) threaded like eventBudget, default ~1.5 MiB with 4 MiB kept as the memory clamp; document both knobs (configuration.md, guide template, bb-cli skill) and reword the source comment to say the count is a hardware/payload-dependent proxy. Avoid runtime calibration unless the timeline cache and latest-rows keys also include the effective budget. Structural fix (out of scope): increm…
    • Caveats: Revision: fixed the major finding — the "rebuilds continuously / per appended event" claim is now marked Verified (cache key) / Partially (rebuild rate), citing the client-side trailing-refetch pacing in realtime-cache-registry.ts#L135-L157 (min 50ms, max 1s, ~50% duty cycle, from PR Bound timeline windows by event count #882); root-cause paragraph reworded to sustained-but-coalesced rebuilds. Minors: renamed the informational test sc…

    The report has copy-paste repro steps, screenshots where the bug is visual, code permalinks at 16ceb3a54, and repro artifacts. It was independently re-run by a second agent (see its Verification section).

    AGENT GENERATED: by Claude Opus 5

  4. added
    partial-repro
    on Aug 18, 2026
  5. uje-m commented on Aug 31, 2026

    @uje-m

    +1 with a fast-host repro — build cost is bimodal (p50 0.2 ms/event, max 270 ms/event), worst builds block the loop for 26–40s, and the stalls take down host heartbeats

    Environment: bb 0.40.0 (stable), Ubuntu 24.04, Ryzen 7 5800X (8C/16T), 60 GiB RAM, NVMe, Node v24.15.0. Two actively-polled heavy threads out of 60+; long-lived agent threads with heavy tool outputs are my normal working pattern, not an outlier.

    From one day of the same Thread timeline build blocked the event loop logging — 2,038 logged slow builds on a single server instance:

    build totalDurationMs eventRowCount eventDataBytes responseRowCount
    worst 39,867 448 240,882 15
    2nd 36,048 1,503 1,543,385 205
    3rd 28,046 1,503 1,690,756 236

    Per-event cost for the worst thread (n=524 builds): p50 0.2 ms/event, max 270 ms/event.

    Two observations the count-based budget can't capture:

    1. This is a fast host and the p50 is fine — so it's not the slow-CPU axis from the OP. The distribution is bimodal: most builds are cheap, and periodically one blocks the loop for 26–40s.
    2. The worst build decoded the smallest payload of the three (241 KB → 39.9s), so raw decode bytes don't explain the tail either. That pattern points at the synchronous SQLite read/decode contending with other loop work or a checkpoint during those builds — inference only, since these log lines don't carry stage timings like the OP's.

    Causal isolation: while the two heavy threads were actively polled, the server logged 87 event-loop stall reports per 15 minutes (maxDelay commonly 3–7s, worst 13.7s). When those threads went idle — same install, same 60+ threads — stalls dropped to 0–5 per 10 minutes for the rest of the day.

    Blast radius beyond UI latency: the host daemon's heartbeat lease is 30s. During the stall windows the daemon's event posts failed (fetch failed) and the server dropped the host — surfacing to the user as "Host disconnected" and "Failed to send message" while the box sat at load ~3. So one thread's slow timeline build can take down message delivery for the whole install.

    Also noting: the timelineWindowing experiment was enabled mid-incident; it mounts nearby rows client-side, and as expected the server-side build cost above is unchanged.

    Happy to attach raw log lines — they carry threadId/totalDurationMs/eventRowCount/decodedEventCount/eventDataBytes/responseRowCount/segmentLimit/selectionStrategy/pageKind, same shape as the OP's logging.

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

    hostpartial-reproperf

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions