Skip to content

fix(app): keep thread timeline content in place while rows mount and replies stream - #8548

Open
smsunarto wants to merge 21 commits into
get-bb:mainfrom
smsunarto:scott/bb-32-layout-shifts-bb-core-thread-timeline-and-thr_996frfm8it
Open

smsunarto wants to merge 21 commits into
get-bb:mainfrom
smsunarto:scott/bb-32-layout-shifts-bb-core-thread-timeline-and-thr_996frfm8it

Conversation

@smsunarto

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

On a slow connection, the thread timeline moved content that was already on screen (#8547):

  • Phone rows gained content-visibility two frames after painting, and Chrome scores the children of a painted row that becomes contained.
  • The scroll keeper re-pinned in a ResizeObserver, which runs after Chrome has recorded the shift.
  • A streaming reply rendered as two Markdown roots, re-creating blocks at the boundary. An image of unknown size grew with no DOM mutation, and the live row was contained.
  • Once a reply contained HTML, every later token re-rendered the whole message, and tables rewrapped when their column was measured.
  • GenUI fences showed raw code, then swapped to the plugin card at a different height.

What changed

timeline-cold-render (timeline-row-containment.ts, ThreadTimelineRows.tsx)

  • Phone rows are contained from their first render, so containment never changes on a painted row.
  • The live last row stays uncontained until a row follows it.

streaming-reflow (scroll-position-keeper.ts, ConversationMessageContent.tsx, streaming-markdown-split.ts, markdown-preview.tsx)

  • The keeper pins in the same task as the content change. A MutationObserver reads the content height and re-measures only when it changed.
  • A streaming reply is one Markdown document, so finished blocks keep their DOM nodes.
  • An image of unknown size stays size-contained at 0×0 until it loads, so its growth arrives through a class change the keeper sees.
  • Markdown tables size to their column before first paint. The geometry is cached per timeline list and dropped when the list resizes with no table mounted.

streaming HTML (markdown-incremental-pieces.ts, markdown-html.tsx, lazy-markdown-html.tsx, App.tsx)

  • HTML no longer forces whole-document rendering. Each piece with HTML renders on its own, and settled pieces keep their element identity.
  • Piece boundaries come from parse5's fragment parser (the copy rehype-raw already uses, now a direct dependency of @bb/app). So attribute text like title="x < y", case, void elements and implied end tags can't break nesting.
  • A message first seen with HTML splits into the same pieces later tokens reuse, so an open <details> keeps its state.
  • Before the HTML renderer loads, a message renders as one whole-message fallback, and switches to pieces when it arrives (useSyncExternalStore).
  • The renderer preloads when a thread route opens, without delaying the thread view.

sent-message-swap (ThreadTimelineRows.tsx)

  • When the server acknowledges an optimistic row, the canonical row keeps its React key and virtual-list measurement key. Expansion state and measured height carry over.

genui-fence-swap (markdown-code-fence-renderers.tsx)

  • While a fence waits for its plugin, it holds the remembered box of the card it last became on this device.

No wire, CLI or plugin API changes. HOST_DAEMON_PROTOCOL_VERSION is unchanged.

How you verified

Layout shifts were measured with a Chrome harness on source-built dev instances:

  • Slow 4G (562.5 ms RTT, 180 kB/s, cache off), 4x CPU.
  • A fixed fixture database restored per run.
  • Values are the summed layout-shift score per load for each cause (Chrome 156). Before = main at e0c49e2e5d; after = this branch.
Cause Page 390 before → after 1440 before → after
Phone rows reveal (cold open) long thread 0.664 → 0.002 —
Phone rows reveal (cold open) PR-review threads 0.261 / 0.179 → 0.0002 / 0.0002 —
Cold open, reply with HTML HTML thread 0.869 → 0.0002 0.046 → 0.0098 (residual)
Streaming reply plain send 1.921 → 0.040 (residual) 0.479 → 0.002–0.019
Streaming reply send with HTML 2.35–2.42 → 0.040 0.60–0.63 → 0.018–0.026
Sent message row swap send 0.121 → 0 —
Code fence → plugin card cold, 3 threads 0.059 / 0.059 / 0.030 → 0.002 each 0.021 → 0.021 (residual)
Code fence → plugin card returning device 0.105 → 0.002 0.018 → 0
Unread divider returning + new messages — 0.011 → 0 measured (residual)

Checks:

  • pnpm exec turbo run typecheck test lint lint:type-aware --filter=@bb/app on the branch: 10/10 tasks.
    • Lint 0/0, type-aware lint 0/0.
    • 713 files, 7598 passed, 19 skipped.
  • The same checks plus build and apps/app/scripts/check-bundle-budget.mjs on this branch merged with main at 4d7a9af609: 11/11 tasks, 7597 passed, bundle budget OK.
  • @bb/mobile not run: no theme tokens or shared-ui files changed.
  • git diff --check: clean.
  • Tests that fail before and pass after include:
    • the keeper's same-task pin and its height gate;
    • the image size-containment tests;
    • the fence waiting-box test;
    • HTML pieces: streamed chunks match the whole document, the parse5 edge cases, cold-to-incremental identity, and an open <details> keeping its node;
    • one placeholder while the renderer loads, and no settled HTML re-render after it arrives;
    • table geometry per column and on resize;
    • the sent-row key across acknowledgement;
    • the thread opening while the HTML renderer is still pending.

Accepted residuals:

  • Cold GenUI fence on a never-seen device, 0.021 on one 1440 thread: nothing before plugin registration names the fence's renderer or its height. The remembered-box path covers returning devices.
  • First-view unsized image: Markdown carries no dimensions, so the image grows once when it loads. About 0.023 on desktop for a centred image, and 0.076 in 1 of 6 phone HTML streams when it lands at the end of the turn.
  • Unread divider, expected about 0.011. It renders with rows as they arrive. It measured 0 in two returning runs, but nothing here is designed to prevent it.
  • Cold HTML reply at 1440 (0.0098): if the thread paints before the HTML renderer arrives, the whole-message fallback swaps to the real render. The thread view doesn't wait for the renderer, so plain-text threads open as soon as their own code is ready.
  • The scroll keeper's per-mutation height read: every height-changing mutation batch is pinned in its own MutationObserver callback, which costs one layout read per batch. Deferring reads (one per frame, or one per task with later changes pinned in rAF or a posted task) brought phone sends back to 0.13–0.30, against 0.011 for the per-mutation read. Most height-changing batches arrive within one task, and Chrome records the shift before a deferred pin lands. A named test in scroll-position-keeper.test.ts pins this.
  • Smaller remaining sources: streaming table column rebalance (about 0.003–0.008), the working indicator, and the phone end-of-turn checkbox entry (about 0.040).

Closes #8547

AGENT GENERATED

Arming content-visibility:auto two frames after a row has painted turns
on paint containment late. Chrome then scores every painted child of
that row as moved by the scroller's offset (0.71 on a phone cold load
of a long thread, 0.96 in a minimal repro), although nothing moves on
screen. Rows now carry contain: layout paint style from their first
render, so arming only starts skipping offscreen content and the shift
is gone (0 in the repro, also after scrolling skipped rows back in).
…changes

The scroll keeper re-applied the bottom pin, or a scrolled-up reader's
row, only from a ResizeObserver callback. Chrome records layout shifts
before ResizeObserver callbacks run, so every streamed chunk scored the
Working row, action bar and the rows below the growth as moved, even
when the correction landed before paint. The keeper now also remeasures
from a MutationObserver on the timeline content, which runs in the same
task as the DOM change, so the position is corrected before Chrome looks.
A streaming reply was two Markdown roots: the settled prefix and the
live tail. When a block finished it was re-created in the settled root
and the tail root moved down by the block's height to show the next
block, so the reply scored a shift at every block boundary (up to 0.11
per step on desktop) and, without the bottom pin, visibly jumped.

The reply is now one MarkdownPreview over the settled prefix plus the
repaired tail. Its incremental pieces keep each finished block's
element and key, so the block stays in place and only the live tail
re-parses. A test pins the parse volume to what the two roots cost.
…r the plugin

A fence a plugin drew before showed a skeleton in its remembered box,
but only for the 3 s loading deadline. After it the fence fell back to
BB's code block at the code's own height, and when the plugin finally
registered (about 35 s on Slow 4G for GenUI) the card swapped in at the
remembered height: two shifts that pushed the rest of the reply. While
the fence waits for a plugin that is still loading, the remembered box
now keeps its size and shows the code, scrollable, with the status line
inside it, so the card replaces it in place. Without a remembered box,
or once loading fails, the fence behaves as before.

This branch has not been deployed

No deployments
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.

Thread timeline shifts already-rendered content on slow connections (phone row reveal, streaming reply, streaming HTML, GenUI fence swap)

1 participant