Skip to content

Epic: streaming answers and heavy views stay responsive on mid-range Android phones #2587

Open
1 of 8 issues completed

Description

@schlessera

Outcome

On a mid-range Android phone (Chrome for Android or Samsung Internet), a long streaming answer, a large graph and the chat composer keep the main thread free for input. A streaming answer stops re-parsing its completed blocks on every frame: per-frame markdown parsing is bounded by the size of the last, growing block. Graph layout no longer blocks the page. Typing in the composer never forces a layout of the transcript. Entry motion is CSS that honours prefers-reduced-motion everywhere, and the eager animation library is gone from the bundle.

Why now

A review of the UI against Google Chrome's public modern-web-guidance skill found main-thread work that grows with content size on paths that run every frame or every keystroke. The figures below are desktop measurements reported by that review. No Android device measurement or universal desktop-to-phone slowdown factor is established here; device/browser receipts remain part of the required proof:

  • Re-rendering a streaming text part takes 5.2 ms at 2 KB and 29.3 ms at 24 KB without highlighting, and 8.8 to 40 ms with it (renderToStaticMarkup). That cost is paid on every animation frame while the answer streams.
  • A force layout of 100 nodes takes 41 ms, and one of 500 nodes takes 469 ms, in one synchronous call.

The guides set the budget: "< 50ms: Execute synchronously. 50ms - 250ms: Slice tasks and yield with scheduler.yield(). > 250ms: Offload to a Web Worker." (performance/performance).

Shared context

  • Browser support policy. Chrome for Android stable (installed WebAPK) and Samsung Internet current (v30 = Chromium 143) keep full function. Firefox for Android degrades gracefully. Anything not in both Chromium targets gets feature detection and a named fallback. No polyfill dependencies.

  • Already in place, keep it:

    • Text and thinking deltas are buffered until an animation frame by the extracted createDeltaBuffer (packages/ui-react/src/ws-client/delta-buffer.ts:28); its flush (packages/ui-react/src/ws-client/delta-buffer.ts:33) applies each chunk in order and may make multiple store appends. Adjacent same-kind chunks coalesce in enqueue (packages/ui-react/src/ws-client/delta-buffer.ts:49). Non-delta frames flush first in handleFrame (packages/ui-react/src/ws-client/inbound.ts:333).
    • The transcript is windowed to 40 messages (WINDOW_SIZE, packages/ui-react/src/components/chat/chat-page.tsx:75). Offscreen message bodies are skipped with content-visibility: auto (.chat-message-body, packages/ui-react/src/theme.css:664-667). Both choices are deliberate: messages vary too much in height for a virtualiser.
    • MessageBubble and BrainMarkdown are memoized; unchanged props can avoid re-rendering, while subscribed state/context changes can still render them (BrainMarkdown, packages/ui-react/src/components/chat/brain-markdown.tsx:64-91).
  • Mermaid and Epic: modules render their own content, starting with brain-kit-styled diagrams that theme and print #2079. The epic Epic: modules render their own content, starting with brain-kit-styled diagrams that theme and print #2079 delivered the content-renderer seam. Mermaid now renders through it as a pack: MermaidContent wraps MermaidBlock (MermaidContent, packages/ui-react/src/content-renderers/mermaid-content.tsx:5-10). Its remaining open scope is the full definition of done and the generated-host foundation. The two Mermaid tasks here change only what happens inside the pack's component. They leave the seam (ContentRendererProps, packages/ui-sdk/src/client/content-renderers.ts:5-10), the manifest and the light/dark/print conformance contract alone, so they and Epic: modules render their own content, starting with brain-kit-styled diagrams that theme and print #2079 do not wait on each other.

  • Measuring. For before/after numbers on a device, use the Long Animation Frames API (performance/identify-heavy-scripts: "DO prefer the Long Animation Frames API over alternatives like the JS Self-Profiling API"). Record the numbers in the PR. They are not acceptance gates, because CI hardware is not a phone.

  • What the streaming work does not bound. ui-react: a streaming answer re-parses its whole text on every frame #2588 re-parses the last top-level block whole and does not split a text that contains link-reference or footnote definitions, so a single long growing block (a long code fence, for example) or an unsplit definition-bearing answer still costs more per frame as it grows. The linear whole-text preprocessing also still runs per frame: holdOpenLink and the block cuts in MarkdownContent (const content, packages/ui-react/src/components/chat/markdown-content.tsx:35-36) and the entity processing in the inner renderer (const BrainMarkdownInner, packages/ui-react/src/components/chat/brain-markdown.tsx:93-100). Performance evidence for this epic is scoped to the repeated parsing of completed blocks; none of it claims constant per-frame cost.

Constraints and non-goals

  • The frame batching, the transcript window and content-visibility stay as they are.
  • Markdown output stays the same for the same final text, including the D49 link rules and holdOpenLink (holdOpenLink, packages/ui-react/src/lib/stream-link-hold.ts:104; docs/decisions/design-kit/links.md).
  • The design kit's reduced-motion rule is non-negotiable (packages/ui-kit/src/tokens.css:1454-1482). D54 rules that closing an overlay is instant (docs/decisions/design-kit/overlays.md).
  • Mermaid keeps ruling 5 ("Every surface prints white") and ruling 6 (the theme contract) of docs/decisions/module-renderers.md.
  • Not in this epic:
    • a transcript virtualiser
    • moving markdown parsing into a worker
    • incremental parsing within a growing block, and making the whole-text preprocessing incremental (file separately if a recording shows it matters)
    • graphology's continuous FA2 supervisor
    • aria-busy on the streaming answer: the transcript is not a live region, and ui-components/progress advises against aria-busy on a generic <div>
    • any change to the content-renderer seam

Breakdown

In the order they can land. None blocks another.

  1. ui-react: a streaming answer re-parses its whole text on every frame #2588: delivered through perf(ui-react): parse a streaming answer block by block (#2588) #4240 (c2870f8ce). Completed top-level blocks keep their parsed output and the growing block is re-parsed within the limits above.
  2. ui-react: tool-input deltas bypass frame batching and walk the whole buffer on every write #2589: batch tool-input deltas per frame, and stop rebuilding every message on each store write.
  3. ui-react: the soft-keyboard hook forces a layout on every keystroke and polls every 250 ms #2590: track the composer's height with a ResizeObserver instead of reading layout on every input and on a timer.
  4. ui-react: force graph layout blocks the main thread for up to half a second #2591: run the fixed-iteration ForceAtlas2 layout in a worker, with a yielding main-thread fallback.
  5. ui-react: framer-motion ignores reduced motion in five components and loads with the chat #2592: replace framer-motion with CSS entry motion that honours reduced motion, and remove the dependency.
  6. ui-react: Mermaid renders throwaway print SVGs while a fence streams #2593: render Mermaid's print SVG once streaming ends, at background priority.
  7. ui-react: the README shell keeps re-rendering the hidden chat on every streamed frame #2595: document <Activity> for keeping Chat mounted while another view shows, after auditing ChatPage's effects.

Generated-host counterpart: schlessera/brain-hosting-template#35 supplies the graph layout worker URL in the generated PWA. It is already a native child; package worker/fallback work stays independently scoped, and a missing host URL cannot be reported as measured phone worker performance.

Related, not in this epic: #2594 (insert Mermaid SVG as parsed nodes; a security-hardening task directly under #2553) touches the same Mermaid files as #2593, so land those two one after the other.

Definition of done

  • A 24 KB streaming answer re-parses only its last block per frame, shown by the render-count test from ui-react: a streaming answer re-parses its whole text on every frame #2588. A Long Animation Frames recording on an Android device is attached to the closing comment as evidence, not as a gate, and states the size of the largest growing block in the recorded answer.
  • A 500-node force layout produces no main-thread task over 50 ms in Chromium when a worker URL is configured. Without one, the work arrives in slices.
  • framer-motion is no longer in packages/ui-react/package.json or bun.lock, and every entry animation is static under prefers-reduced-motion: reduce.
  • Every task's Android criterion (feature detection plus its named fallback) has been checked in Firefox as the non-Chromium engine.

2026-10-24: narrowed the Outcome from "no frame of streaming work grows with the answer" to what #2588 establishes (completed blocks are not re-parsed), named the remaining large-block and whole-text-preprocessing limitation, and scoped the performance evidence accordingly; #2594 moved to #2553 and is now a reference; the earlier triage note is in the comments.

2026-10-24 continued triage at main dc5d3c73282c29a3f0098306d398743ee18ec791: Included already-attached hosting-template #35 in the active breakdown and preserved the explicit configured-worker versus yielding-fallback boundary. Desktop measurements are not phone guarantees, and the required Android recording remains unexecuted by triage. No new attachment or performance result.

2026-10-29 source review at main a8e80eea1ec045579ee7d69d6019bce5e78afa5e: Verified and refreshed the actual delta buffering/flush/frame anchors and whole-text entity preprocessing. Corrected the overstatement that each frame makes exactly one store write: the implementation appends each remaining chunk. This is source inspection, not new Android performance evidence or changed batching. Seven package children plus the generated-host worker-URL child remain open. Preserve the existing priority, accepted rulings, exclusions and native membership; the epic remains without agent-ready. No implementation, new extension seam, runtime qualification or release commitment is supplied by this correction.

2026-10-29 triage at main 36074ef4c5d14bb41108a26160a8ab8bbe1d4e87: verified moved named source anchors and refreshed current navigation. Historical receipts, scope, unresolved prerequisites and native membership remain unchanged; no new runtime proof is claimed.

2026-11-02 triage at main a84d9bfbcb4e608b504aa0a9feb1144ad9a0b77e: Reverified the 40-message window, per-frame delta coalescing/flush and whole-text preprocessing limitations. Refreshed the moved window constant; no device timing or new performance result is inferred. Keep the epic outside agent-ready.

2026-11-04 triage at main 53b862123d7b8e4c9d039859fabdc9fc6ad4cd36: Followed the delivered connection refactor to its real delta-buffer and inbound owners; no batching or phone-performance improvement is inferred. No new runtime qualification or implementation is claimed.

Triage 2026-11-09 at main 9c7eb25825fdad5f4485db0df472188805edb450: Verified the moved active declaration/use and repaired its navigation. Commit-bound historical failures and source ranges retain their identities; scope, rulings, child prerequisites, priority and no epic readiness are preserved. This source review is not a new runtime result.

2026-11-14 triage at main faa480e8c864d547cd8b4eb75965b7d609284770: Refreshed verified current-source pointers; historical receipts, decisions and remaining qualification requirements retain their identities.

2026-11-15 triage at main 3f8ff29341fbe25665cea42659f92e6f3d3ccef0: Corrected current-source navigation by inspecting the named declaration or guard. Existing decisions, historical receipts and remaining prerequisites are unchanged; no new runtime qualification is claimed.

2026-11-17 triage at main 9e7429a914a8aaf5967f074fa9e09984968fc9dc: Refreshed the moved current-source anchor after inspecting its declaration. Existing findings, native outcomes and verification requirements remain unchanged.

2026-11-20 triage — Verified #2588 on main bd751d434 and updated the rendering pointers.

Activity

  1. added
    type: feat
    area: ui-react
    priority: p1
    epic
    on Oct 22, 2026
  2. schlessera commented on Oct 22, 2026

    @schlessera
    OwnerAuthor

    2026-10-22 triage at main aa8c8966b59f031f054f4876dbca84048b40ae4f: refreshed the named transcript-window, containment and memoized-markdown anchors. Distinguished reported desktop measurements from unmeasured Android extrapolation; actual device Long Animation Frames evidence remains required by the outcome. Native tasks, p1, scope exclusions and no epic readiness remain; this is source review, not a new performance measurement.

  3. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    2026-10-24 triage at main dc5d3c73282c29a3f0098306d398743ee18ec791: Included already-attached hosting-template #35 in the active breakdown and preserved the explicit configured-worker versus yielding-fallback boundary. Desktop measurements are not phone guarantees, and the required Android recording remains unexecuted by triage. No new attachment or performance result.

  4. schlessera commented on Nov 10, 2026

    @schlessera
    OwnerAuthor

    2026-11-10 triage at main 3aadc8df5: citation drift only: handleFrame is at inbound.ts:332 (was 331) and the reduced-motion rule at tokens.css:1454-1482 (was 1407-1435). All eight native children remain open; no merged PR since the 2026-11-04 pass references them. Breakdown, labels and native membership unchanged.

  5. schlessera commented on Nov 20, 2026

    @schlessera
    OwnerAuthor

    Epic state — 2026-11-20. #2588 is closed through #4240 (c2870f8ce). #2589–#2593, #2595 and schlessera/brain-hosting-template#35 remain open. The Android Long Animation Frames recording is still named in the definition of done.

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

    area: ui-reactepicpriority: p1type: feat

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions