Repository navigation
Remove the fixed per-push cost of building the latest timeline window - #7033
Merged
Merged
Conversation
Each push rebuilt and re-prepared the same handful of SQL statements through the query builder, re-parsed app settings and experiments, and read the rows the server had just written back from the database. - The push-path event queries (latest sequence, appended range, timeline window, budget floor advance, context-clear boundary, context-window and token usage rows) are prepared once per connection and reused. - App settings and experiments keep their parsed value per connection and re-parse only when the stored rows differ. - Committed event rows are remembered per thread; the selection memo takes them instead of reading the appended range back, after an id-only check of that range. Rows longer than the inline output limit, or that the SQL window filters decide, still come from the database. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Nov 4, 2026
## Human comments ## What was wrong #7011 (updateWhenIdle settings) reverted two PRs that had merged about 30 minutes before its commit was created: #7028 (merge child notices while the parent is blocked, skip `/compact` turns) and #7030 (count only the latest workflow run in GitHub PR check status). Its body mentions neither, so it looks accidental. - #7011 is a single commit, `6b17f1bc04`, whose parent is #7028's merge commit `920804af63`. Even so, the commit's own diff rewrites 11 files to their earlier content: - In #7028's 9 files it is an exact inverse of #7028, apart from four exports #7011 added to `packages/db/src/data/index.ts`. - #7030's 3 files in `plugins/github-pr-status/host/` went back to their pre-#7030 content. - That pattern fits a branch whose files came from an older checkout and were committed onto a newer base. - CI stayed green because each revert also removed the matching tests. - No other PR was affected. Every file the commit touched was compared with its earlier versions on main; only these two PRs match. What users got back after #7011: - **#7028:** a parent blocked on a question, a starting turn or an offline host collects a separate notice for every child update again (on Nov 3 one parent got ten from one child). `/compact` turns on a child send the parent "No final output was recorded." - **#7030:** bb shows green PRs as failing ("22 passed, 4 failed") when GitHub cancelled a duplicate CI run. ## What changed - Cherry-picks `b0e91c38f8` (#7030) and `920804af63` (#7028) onto current main with `-x`. #7030 applies unchanged. - #7028 conflicted only in `coalesced-system-notices.ts`, because #7029 (merged after #7011) added withdrawing a pending notice there. The result is #7028's version with #7029's withdraw path kept: the pending lookup has no `now` and a merge writes `windowEndsAt`. This is the combination #7029 was tested with before #7011 landed. `child-thread-notifications.ts` resolved to main's existing version, which already guards on a scheduled row. - Restores the #7029 test that depends on #7028: an ownership change undone after its 2 s window but before delivery is withdrawn. Its test helper comes back with it. - `internal/events.ts` and `db/data/events.ts` merged cleanly with the later #7031 and #7033 changes. - No daemon wire, CLI, config or plugin SDK surface change; `check-plugin-sdk-version` reports the surface unchanged. ## How you verified - With main's current code and the restored tests, 7 tests fail. With this branch they pass: - `thread-ownership-notices`: "withdraw a move undone after its window but before delivery" - `system-notice-dispatch`: "merge later outcomes into a row still waiting on the parent" - `child-completion-turn-output`: "does not notify the parent for a turn that only compacted context" - `github-pr-status` `git-host` / `batch`: superseded workflow runs, including on a later checks page and for drafts found through `gh pr view` - Server: ownership notices, notice dispatch, child completion output, notice kinds, child outcomes, parenting, plugin events, queue updates, sidebar status and child notifications all pass (112 tests). - `turbo run test` for the GitHub PR Status plugin, `@bb/db` and `@bb/templates` passes (10/10 tasks). `turbo run typecheck` for server, db, app and the plugin passes (10/10 tasks). > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
After #7012 almost every push extends the kept selection and resumes the kept projection, but each push still paid about 4.5 ms of fixed cost that does not depend on the event. A probed replay of #7012 split a typical 11–12 ms push into: reading the appended rows back (1.7–1.8 ms) and other fixed queries and parsing (2.8–2.9 ms), on top of the window-proportional work. A CPU profile of main's push path showed why. About three quarters of each of those queries was the query builder rebuilding the SQL and better-sqlite3 re-preparing the statement on every call, not running it. For example, reading one appended row took 0.75 ms through the builder against 0.15 ms for the same SQL prepared once. App settings and experiments were also re-read and re-parsed with zod on every push (about 0.7 ms together), and the rows the server had just written were read back from the database twice: once for the range, once through the timeline window filters.
What changed
One read path; the same queries, prepared once.
packages/db/src/data/events.ts,connection.ts). The latest thread sequence, the appended range, the timeline window rows, the budget floor advance, the context-clear boundary, and the context-window and token-usage rows are now prepared statements cached per connection throughprepareCachedQuery. A newprepareCachedQueryVariantkeys variants whose SQL depends on arguments (inline output limit, excluded types, an optional bound). The SQL is unchanged apart from placeholders.app-settings.ts,experiments.ts, newstored-setting-rows.ts).getAppSettingsandgetExperimentskeep the parsed value per connection and re-parse only when the stored rows differ, checked with one small prepared read. That covers every way the rows change (the setting writers, raw SQL, other connections, rollbacks) without a separate invalidation path. The cached value is deep-frozen, so a caller that tried to mutate it would fail loudly; no caller does.appended-event-rows.ts,timeline-selection-memo.ts):insertStoredEventRow, which every event insert goes through, remembers each committed row per thread after its transaction commits (afterCommit): 64 threads, 128 rows each, rows up to 64,000 characters.listAppendedEventRowsreturns those rows after an id-only query confirms they are exactly the rows in the appended range. It falls back to reading the range when any row is missing, or when a row is longer than the inline output limit, since SQLite would truncate it.selectAppendedTimelineWindowRowsderives the window rows from the appended rows by excluding the excluded types. Ranges with background task progress or diagnostic event types, which the SQL filters decide, still run the window query.Not changed: what any query returns, the selection and projection rules, and the cold and older-page paths. No wire, protocol, CLI or settings change.
docs/timeline-pagination.mddescribes the appended rows and prepared queries.How you verified
Data: the scrubbed
subset.db(300 threads) andheavy.db(all 353 production threads with 2,000+ events), as migrated clones, never a copy of~/.bb/bb.db. Baseline: origin/main 4f973b8 (#7012).The replay harness removes each thread's last 300 events and re-inserts them one at a time through
insertEvents, the server's write path, so the hand-off runs. It flips the thread's status the way the server does and reads the latest window with the previous tag throughcreateThreadTimelineWindows().read(the push'sbuildWindow+ delta). 12 subset threads and 17 heavy threads, two interleaved rounds at background priority on a shared machine.Output is identical to main:
Push CPU and latency (total push read CPU; per-push CPU p50 / p95; rounds 1 / 2):
GC collections: 562 / 560 → 432 / 429 (subset), 1,092 / 1,097 → 908 / 914 (heavy). The fixed cost is about 4 ms per push, so the median moves most. p95 on heavy is dominated by work that grows with the window, which this PR does not touch; its round-to-round spread is about ±5%.
Tests.
packages/db/test/data/appended-event-rows.test.ts:pnpm exec turbo run typecheck --filter=@bb/db --filter=@bb/server;@bb/dbvitest: the new file,events,app-settings,experiments,event-rewrite-generation(120 tests);@bb/servervitest:test/services/threads,timeline-pagination-correctness,timeline-streaming-memo,public-thread-timeline-*,public-thread-diagnostic-events, and the settings and experiments route tests (486 tests). Four files timed out under background priority while the benchmark ran and passed when rerun with longer timeouts.🤖 Generated with Claude Code