Skip to content

fix(desktop): keep the streaming chat responsive on long reasoning replies - #545

Merged
huichen merged 4 commits into
mainfrom
claude/flash-thinking-freeze
Sep 10, 2026
Merged

huichen merged 4 commits into
mainfrom
claude/flash-thinking-freeze

Conversation

@huichen

@huichen huichen commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

Problem

With "show thinking" enabled, a high-throughput reasoning model (DeepSeek Flash)
freezes the desktop chat while the Agent keeps streaming normally — the Agent log
is fine, the UI stops responding, and it recovers once the reply ends.

Measured from the local stores, the scale difference is entirely in reasoning
volume, not answer or tool volume:

model runs reasoning KB/run answer KB/run tool_delta KB/run
future/deepseek-flash 19 437 170 14185
future/deepseek-v4-flash 8 260 62 3972
azure/gpt-6-astra 25 6 111 10951

Flash streams reasoning at p50 ≈ 550–650 chars/s (p90 ≈ 1000, peak 1618), and one
run accumulated 112 reasoning blocks / ~198 KB.

sample captures of the WebContent process while such a reply was streaming
attribute the frame time roughly half to JIT'd JS and half to WebKit layout:

 451  ??? (JIT'd web bundle)
  51  WebCore::RenderBlock::layout()
  37  WebCore::RenderLayer::parentClipRects(...)
  35  WebCore::RenderBlockFlow::layoutBlock(...)
  33  WebCore::RenderBlockFlow::layoutBlockChildren(...)
  25  WebCore::RenderBlock::paintObject(...)
  21  WebCore::RenderLayerCompositor::computeCompositingRequirements(...)
  18  WebCore::RenderFlexibleBox::layoutBlock(...)

Root cause

The Agent pushes run-runtime updates at the WebView's frame cadence (~60/s) and
every push made the frontend re-read the run journal and re-render the streaming
message. That per-push cost is not proportional to the tokens that arrived since
the last push:

  • JS half — re-projecting the reply walks every segment; the markdown worker
    was also permanently disabled by a single error (see below).
  • Layout half — mutating the growing tail forces the browser to re-compute
    preferred widths and re-lay-out the whole accumulated message, so the cost
    grows with the reply length.

A non-reasoning model never accumulates enough to reach the threshold, which is
why only Flash reproduces it.

Changes

1. Coalesced, rate-limited tick driver (liveStreamTick.ts, new)
Replaces two copies of an ad-hoc coalescing loop (send pipeline + re-attach
hook). At most one projection in flight, bursts collapse into a single trailing
run, successive projections spaced by 100ms instead of one per frame. stop()
is final so the settle path's authoritative final render can never be clobbered
by a late intermediate projection. This is the change that reduces total work
proportionally.

2. Layout containment on the growing block (MarkdownContent,
MessageBlock, ThinkingBlock)
Only the still-growing tail is marked data-streamed-block, and
contain: layout style (+ contain-intrinsic-size: auto none) is scoped to it.
The live block becomes a layout boundary, so a delta's relayout no longer
re-walks every settled text/thinking/code block. Paint is deliberately not
contained — these are plain in-flow text blocks and clipping the growing tail
(copy button, selection handles) would be a regression for no layout benefit.

3. ThinkingBlock memoized
Every reasoning block except the growing tail has identical props, so each push
was rebuilding a hundred-odd element subtrees.

4. Markdown worker no longer latches its fallback permanently
A single worker error used to set a flag that disabled the off-thread parser for
the rest of the thread's lifetime, moving every subsequent block parse onto the
UI thread with no way back. The retry budget now resets on a successful response
and only consecutive failures give up. The synchronous path also no longer
lets a parser throw (pathologically nested markdown) escape into the render tree.

Verification

  • tsc --noEmit clean, eslint "src/**/*.{ts,tsx}" clean, desktop 840 tests
    pass
    (90 files).
  • New tests: 7 for the tick driver (leading run, burst coalescing, no overlap,
    stop() finality, inactivity, afterProject, in-flight coalescing); 4 for the
    worker fallback; 1 DOM assertion that only the growing tail carries
    data-streamed-block.
  • The worker-fallback tests were checked against the old behaviour: restoring the
    permanent latch fails 3 of them.

Status / caveat

Rounds 1–3 (tick driver, memo, worker latch) were installed and tested by the
reporter, who still saw the freeze — that is what prompted the layout-containment
work in this PR, which is derived from the captured samples above. The
end-to-end fix has not yet been confirmed by a user-visible retest
, so please
treat the freeze as "addressed but unconfirmed" rather than fixed. If it
reproduces, the same capture method (sample <WebContent pid> while the reply
streams, then categorize leaf frames) will show whether the layout share actually
dropped and which path remains.

Scope

Desktop TypeScript only — no Rust, no wire-contract or schema changes.

…plies

The Agent pushes run-runtime updates at the WebView's frame cadence (~60/s),
and every push made the frontend re-read the run journal and re-render the
streaming message. That per-push cost is not proportional to the tokens that
arrived since the last push: re-projecting the reply walks every segment,
re-parses the growing block, and the browser re-lays-out the message — so it
grows with the accumulated reply. A high-throughput reasoning model streams a
few hundred KB of reasoning in a couple of minutes (observed: ~437 KB/run vs
~6 KB/run for a non-reasoning model), and the chat stops responding while the
Agent keeps streaming normally.

Three fixes:

- Add a coalesced, rate-limited tick driver (liveStreamTick) and use it for
  both streaming paths (the send pipeline and the re-attach hook), replacing
  two copies of an ad-hoc coalescing loop. At most one projection stays in
  flight, bursts collapse into a single trailing run, and successive
  projections are spaced by 100ms instead of one per frame. stop() is final so
  the settle path's authoritative final render can never be clobbered by a
  late intermediate projection.

- Memoize ThinkingBlock. Every reasoning block except the growing tail has
  identical props, so without the memo each push rebuilt their whole element
  subtree — a hundred-odd blocks for a long reasoning reply.

- Stop latching the markdown worker's synchronous fallback permanently. A
  single worker error used to disable the off-thread parser for the rest of
  the thread's lifetime, moving every subsequent block parse onto the UI
  thread with no way back; the retry budget now resets on a successful
  response and only consecutive failures give up. The synchronous path also
  no longer lets a parser throw (pathologically nested markdown) escape into
  the render tree.
…ly streams

WebContent samples captured while a long reasoning reply was streaming show the
frame time dominated by re-laying-out the whole message on every pushed delta:
`RenderBlock::layout`, `RenderBlockFlow::layoutBlock`, `computePreferredLogical
Widths`, `paintObject`, `performFlexLayout`. A high-throughput reasoning model
accumulates a few hundred KB of thinking across a hundred-odd blocks, so each
delta's relayout re-walks all of it and the chat freezes while the Agent keeps
streaming normally.

Mark only the still-growing tail with `data-streamed-block` and scope
`contain: layout style` (+ `contain-intrinsic-size: auto none`) to it. The live
block becomes a layout boundary: its size no longer depends on its children, so
the dirty tail stops forcing the browser to re-measure every settled text,
thinking, and code block. Paint is deliberately not contained — these are plain
in-flow text blocks and clipping the growing tail would be a regression for no
layout benefit.
@huichen
huichen merged commit db0e2d4 into main Sep 10, 2026
12 checks passed
@huichen
huichen deleted the claude/flash-thinking-freeze branch September 10, 2026 13:14
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.

1 participant