Repository navigation
Make timeline pushes cost closer to the rows that changed - #7244
Merged
ymichael merged 4 commits intoNov 9, 2026
Merged
Conversation
ymichael
added a commit
that referenced
this pull request
Nov 7, 2026
S4's decoded-selection cache kept up to 4 decoded selections per thread for 64 threads per connection, bounded by count only, and nothing dropped them on thread delete, history rewrite or idle. It retained row data the selection memo and the stored-event decode cache had already let go: reading the 12 harness threads in 4 page shapes kept 261 MB alive in it. The decoded rows now live on the selection memo entry whose rows they decode. A memo hit hands the entry's decoded rows to the build as the prefix to extend, and the build stores the extended result back on the new entry. The decoded cache therefore has exactly the memo's bounds (16 entries, 4 idle, 16M data chars) and is forgotten wherever the memo forgets an entry: thread delete, rewrite generation or data_version change, context-clear epoch change and eviction. The separate per-thread map and its prefix scan are gone. Retained heap after reading app, phone, CLI and nested windows (forced GC): scenario main #7244 c381f52 this 12 harness threads +63 MB +321 MB +67 MB 12 harness + 293 recent +109 MB +295 MB +108 MB Warm push after one appended event (harness push scenario, 9 alternating pairs x 12 appends, median of per-run medians): kdrbtee92b 2.5 -> 2.4 ms, u738kqjs29 (active) 8.8 -> 9.1 ms; the last 3 interleaved pairs were 8.8 vs 8.8 ms. Rows read, decoded and folded per push are identical. Warm cache-hit reads stay at 0.0-0.1 ms on all 12 threads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
marked this pull request as ready for review
November 9, 2026 18:05
ymichael
added a commit
that referenced
this pull request
Nov 9, 2026
S4's decoded-selection cache kept up to 4 decoded selections per thread for 64 threads per connection, bounded by count only, and nothing dropped them on thread delete, history rewrite or idle. It retained row data the selection memo and the stored-event decode cache had already let go: reading the 12 harness threads in 4 page shapes kept 261 MB alive in it. The decoded rows now live on the selection memo entry whose rows they decode. A memo hit hands the entry's decoded rows to the build as the prefix to extend, and the build stores the extended result back on the new entry. The decoded cache therefore has exactly the memo's bounds (16 entries, 4 idle, 16M data chars) and is forgotten wherever the memo forgets an entry: thread delete, rewrite generation or data_version change, context-clear epoch change and eviction. The separate per-thread map and its prefix scan are gone. Retained heap after reading app, phone, CLI and nested windows (forced GC): scenario main #7244 c381f52 this 12 harness threads +63 MB +321 MB +67 MB 12 harness + 293 recent +109 MB +295 MB +108 MB Warm push after one appended event (harness push scenario, 9 alternating pairs x 12 appends, median of per-run medians): kdrbtee92b 2.5 -> 2.4 ms, u738kqjs29 (active) 8.8 -> 9.1 ms; the last 3 interleaved pairs were 8.8 vs 8.8 ms. Rows read, decoded and folded per push are identical. Warm cache-hit reads stay at 0.0-0.1 ms on all 12 threads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/timeline-s4-hit-path-window-sized-costs-thr_jn4p352bnn
branch
2 times, most recently
from
November 9, 2026 18:57
1e4176d to
3a5f841
Compare
A push for one new event still re-ran several whole-window passes after the selection memo and projection fold hit. This removes the biggest of them without changing any output byte: - Settled delegations: with deferContent the server dropped their child rows after building and mapping them. thread-view now skips building them (omitSettledDelegationChildRows) and only checks whether any child row exists, so the result stays null vs [] exact. - bb thread tell detection: parseThreadTellCommand scanned every command (2 MB on u738kqjs29) several times per build. Results are now cached by command string, bounded like the shell intent cache. - Interrupted turns: once any turn in the window was interrupted, every fold finish structuredClone'd the whole projection state. It now deep-copies only when interrupted-turn finalization would change something (a running call or a pending message in that turn). - Decode: a selection that extends the previous one reuses its decoded prefix and byte count instead of re-decoding every row. - Pagination byte budget: per-leaf and per-page JSON.stringify is replaced by byte counts cached per row object; unchanged latest-window rows and window fields are reused from the previous build when they serialize identically, so the tag, delta and byte counts only touch rows that changed. Warm push after one appended event, in-process harness push scenario, 4 alternating base/fix pairs x 12 appends, load avg 14-19: thread base ms fix ms kdrbtee92b 4.0 2.8 u738kqjs29 (active) 15.4 9.8 8nnix6m53a (active) 5.9 3.0 gyi25h9awq (active) 10.6 4.7 wg5iwwrezi (active) 10.9 4.3 txqr3djnmd (active) 8.0 2.5 mwktgwihcc (active) 8.1 3.2 Parity: latest, phone, CLI, nested, nested+deferred, summaryOnly and three older pages for all 947 snapshot threads with >=1,000 events, plus a 13-step warm-vs-cold replay with deltas on 112 threads, hash identical to main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
S4's decoded-selection cache kept up to 4 decoded selections per thread for 64 threads per connection, bounded by count only, and nothing dropped them on thread delete, history rewrite or idle. It retained row data the selection memo and the stored-event decode cache had already let go: reading the 12 harness threads in 4 page shapes kept 261 MB alive in it. The decoded rows now live on the selection memo entry whose rows they decode. A memo hit hands the entry's decoded rows to the build as the prefix to extend, and the build stores the extended result back on the new entry. The decoded cache therefore has exactly the memo's bounds (16 entries, 4 idle, 16M data chars) and is forgotten wherever the memo forgets an entry: thread delete, rewrite generation or data_version change, context-clear epoch change and eviction. The separate per-thread map and its prefix scan are gone. Retained heap after reading app, phone, CLI and nested windows (forced GC): scenario main #7244 c381f52 this 12 harness threads +63 MB +321 MB +67 MB 12 harness + 293 recent +109 MB +295 MB +108 MB Warm push after one appended event (harness push scenario, 9 alternating pairs x 12 appends, median of per-run medians): kdrbtee92b 2.5 -> 2.4 ms, u738kqjs29 (active) 8.8 -> 9.1 ms; the last 3 interleaved pairs were 8.8 vs 8.8 ms. Rows read, decoded and folded per push are identical. Warm cache-hit reads stay at 0.0-0.1 ms on all 12 threads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…dget The latest-window row reuse kept the pageable rows of the last 32 windows per connection in a separate map. The bound was a count, not a size, and the map never dropped a thread's windows on history rewrite, compaction or memo eviction. The kept rows now live on the selection memo entry for their selection, one set per (deferContent, includeNestedRows) variant. Their JSON bytes count toward the memo's 16M budget along with the selection's data chars. They go away wherever the memo drops an entry: forget on rewrite or compaction, context-clear epoch change and eviction. A rewritten generation doesn't carry them over. After pagination, only rows whose JSON size pagination already measured are kept. Those are the only rows reuse helps, and their size costs nothing extra. Charging every pageable row instead stringified the older context rows pagination never touches, which doubled u738kqjs29's first CLI read after an app read (27 -> 57 ms). Retained heapUsed after reading app, phone, CLI and nested windows of each thread (forced GC; kept rows = heap freed by dropping them once the windows instance is gone): read set 49f4151 this 12 harness threads +66.5 MB (5.9) +66.5 MB (1.0) 305 threads +108.3 MB (3.2) +108.4 MB (1.2) 305, harness threads read last +128.6 MB (5.9) +112.4 MB (1.0) 318, 17 largest threads last +149.1 MB (14.4) +140.3 MB (0.0) Responses are identical to 49f4151: 4,652 forward and 2,462 reverse-order hashes over 162 threads, and 0 warm-vs-cold mismatches over 1,024 append-replay steps. Harness timings, 4 alternating pairs, are within 1% in total, and every rows-read and bytes metric is identical. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tion The projection cache from #7245 keyed built rows by includeNestedRows only. Deferred reads now omit settled delegation children, so a CLI read after an app read could reuse rows with those children missing. The cache now keys rows by both flags; this test reads the app window and then the CLI window and checks the subagent's children survive. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/timeline-s4-hit-path-window-sized-costs-thr_jn4p352bnn
branch
from
November 9, 2026 19:32
3a5f841 to
efe7e8a
Compare
ymichael
deleted the
bb/timeline-s4-hit-path-window-sized-costs-thr_jn4p352bnn
branch
November 9, 2026 19:45
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
With the selection memo and projection fold both hitting, a push for one new event still ran several passes over the whole latest window (timeline perf audit, REPORT #5). On the snapshot threads the biggest were:
deferSettledTimelineRow(572 of 634 rows on kdrbtee92b).parseThreadTellCommandscanned every command's text several times per build (2 MB per pass on u738kqjs29, about 14% of a push).structuredCloneon the whole projection state, becausependingFinalizationByTurnIdis never cleared (6 of 150 sampled active threads, 1–1.8 ms per push).JSON.stringifyon every leaf, then on every returned segment.What changed
buildThreadTimelineFromEventstakes a newomitSettledDelegationChildRowsoption. The server sets it whendeferContentis on, and settled delegations then skip building their child rows. Emptiness is still checked lazily, so the output keeps the exactnullvs[]that deferral produced.parseThreadTellCommandresults are cached by command string, bounded the same way asshellIntentCache(16k entries, 8M chars), and every call returns a fresh object.copyProjectionStateForFinishdeep-copies only when interrupted-turn finalization would actually change something: a running call, or a pending message or cell scoped to that turn.interruptedTurnFinalizationChangesStateimplements the check.timeline-selection-decode.ts/timeline-selection-memo.ts: when a selection extends the previous one, it reuses the decoded prefix and its byte count. The decoded rows live on the selection memo entry whose rows they decode, so they have exactly the memo's bounds (16 entries, 4 idle, 16M data chars) and go away wherever the memo drops an entry: thread delete, rewrite generation ordata_versionchange, context-clear epoch change, and eviction.timeline-content-pagination.ts/timeline-pagination.ts: JSON byte counts are cached per row object, and array and parent sizes are derived from their parts (exact for any UTF-8).timeline-window-reuse.ts: unchanged latest-window rows, and window fields like todos and provider commands, are reused from the previous build when they serialize identically, key order included. The tag, delta and cache accounting then only touch rows that changed.getOrBuildnow passes the stale response to the builder for this.timeline-selection-memo.ts: the previous build's pageable rows, which the next build reuses rows from, live on the selection memo entry, one set perdeferContent/includeNestedRowsvariant. Their JSON bytes count toward the memo's 16M budget along with the selection's data chars, and they go away wherever the memo drops an entry. Only rows whose JSON size pagination already measured are kept, so the size costs nothing extra (e515883).includeNestedRows. Deferred reads now leave out settled delegations' child rows, so a CLI read after an app read could reuse the app's rows and lose a subagent's children.timeline-projection-cache.tsnow keys rows byincludeNestedRowsandomitSettledDelegationChildRows. The first non-deferred read after an app read builds its own rows; the projection is still shared. In the selection memo, a selection shared by several entries counts its data once, and kept pageable rows count per entry. An entry that adopts a sibling's selection starts with no kept rows instead of sharing the sibling's map.Not changed:
getTimelineGroupingContextrunning again whenever child (parented) events are appended.How you verified
Before/after. Harness push scenario: warm latest read with
afterTagafter appending oneitem/started. In-process on a clone of the Nov 7 snapshot. 4 alternating main/branch pairs × 12 appends, load average 14–19 (other slices were benchmarking at the same time). Median of per-run medians:An earlier round of 3 pairs at load average 37–58 showed the same direction, with −16% to −61% and much wider spread.
Memory. The first version of this PR kept decoded selections in a separate per-connection cache: 4 per thread for 64 threads, bounded by count only, never dropped on thread delete, rewrite or idle. It retained rows the memo and the stored-event decode cache had already released. They now live on the memo entries (49f4151). Retained
heapUsedafter one instance reads the app, phone, CLI and nested windows of each thread, after a forced GC, on a clone of the snapshot:Push and warm timings for 49f4151 vs c381f52: harness
push+warm, 9 alternating pairs × 12 appends (3 of them reversed-order, 3 interleaved), load average 7–9. Median of per-run medians:Rows read, decoded and folded per push are identical, and
event-json-decodestays under 0.1 ms on both, so the prefix is still reused.Memory, kept pageable rows (e515883). Before this commit the reused rows sat in a separate per-connection map of the last 32 windows. The bound was a count, and nothing dropped a thread's windows on rewrite, compaction or memo eviction. Same measurement as above. In brackets: heap freed by dropping the kept rows once the windows instance is gone.
On this branch alone the old map stayed small in the default read order, because the last 32 windows read were small threads. With the larger merged integration branch (#7250) it measured 33 MB at 305 threads.
The first version of this commit charged every pageable row. That stringified the older context rows pagination never measures, and it doubled u738kqjs29's first CLI, nested and summary reads after an app read (27 → 57 ms). Keeping only measured rows removed that cost.
Timings for e515883 vs 49f4151: harness
cold,warm,variants,capacityandpush. Push threads were kdrbtee92b plus u738kqjs29, wg5iwwrezi and txqr3djnmd forced active, 12 appends, 5 repeats. 4 alternating pairs, each started at load below 8 (4.3–6.3). Median of per-run medians:Every rows-read, decoded, bytes and response-bytes metric is identical between the two.
Parity. Responses were hashed on main and on the branch over a read-only clone of the snapshot:
Result: 9,723 / 9,723 hashes identical to main, and warm equals cold at every replay step. Retained-output availability, which depends on
Date.now(), was normalized before hashing. Re-run on 49f4151 with the same script on a fresh clone: 9,723 / 9,723 identical to main and to c381f52, warm equals cold at every replay step.e515883 vs 49f4151, using the integration's parity and append-replay scripts on fresh clones:
Tests added. I ran the
structuredClonetest against the old condition, where it fails. The others test code paths that don't exist on main, so they weren't run there.thread-view/test/settled-delegation-child-rows.test.ts: omitted child rows equal deferring the full rows, including nested, childless and pending delegations.thread-view/test/event-projection-fold.test.ts: fold matches cold after every event for an interrupted turn with nothing pending, and finishing does not callstructuredClone.thread-view/test/tool-call-parsing*.test.ts: cached results are independent objects, repeated parses are reused, and the existing linearity benchmarks now use unique commands so the cache can't mask them.server/test/services/threads/timeline-selection-decode.test.ts: prefix reuse and byte count. The memo entry carries the decoded rows and a reused selection extends them. Decoded rows are dropped on history rewrite and thread forget, and when the memo evicts the entry for its data-char budget or its entry cap.server/test/services/threads/timeline-selection-decode.test.ts, kept pageable rows (e515883). A build reuses no row objects after the memo forgets the thread. Kept rows count toward the memo budget, so a second thread of the same size evicts the first. Both tests fail on 49f4151, where the rebuilt rows are still the old objects.server/test/services/threads/timeline-window-reuse.test.ts: unchanged rows keep identity across an append and serialize like a fresh build; reuse only happens on exact JSON equality, including key order.server/test/services/threads/timeline-content-pagination.test.ts: derived byte counts equalBuffer.byteLength(JSON.stringify(rows))for nested multibyte rows.server/test/services/threads/timeline-window-sharing.test.ts: the CLI window built after the app window keeps a settled subagent's child rows. It fails without the cache-key change ([null]instead of[1]). The existing sharing test now expects a row rebuild for each new row shape (includeNestedRows×deferContent).Commands run.
turbo run typecheck lint --filter=@bb/server, plus vitest ontest/services/threadsandtest/provider-corpus(34 files, 293 tests pass, 1 skipped). All withenv -u BB_ACCOUNT_POOL_PARENT_URL -u BB_SERVER_URL -u BB_CLI.pnpm exec turbo run typecheck test --filter=@bb/server --filter=@bb/thread-view --filter=@bb/provider-parityturbo run typecheck lint --filter=@bb/server, plus vitest ontest/services/threads/timeline*,thread-events,conversation-outline*,test/provider-corpus,event-pruningandinternal-event-append-ownership(25 files, 241 tests pass, 1 skipped)After rebasing onto main at 226d128 (#7245, #7238, #7254, #7284):
turbo run typecheck lint --filter=@bb/server,turbo run typecheck test --filter=@bb/thread-view --filter=@bb/provider-parity, and vitest ontest/services/threadsandtest/provider-corpus(38 files, 312 tests pass, 1 skipped). Push harness, 4 threads × 12 appends, rebased branch vs main at be3ca2c: warm push −37% to −64%, identical response bytes (load average 12–38, so the exact numbers are noisy).Refs #4674 (does not fix it)
🤖 Generated with Claude Code