Repository navigation
fix(desktop): keep the streaming chat responsive on long reasoning replies - #545
Merged
Merged
Conversation
…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.
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.
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:
Flash streams reasoning at p50 ≈ 550–650 chars/s (p90 ≈ 1000, peak 1618), and one
run accumulated 112 reasoning blocks / ~198 KB.
samplecaptures of the WebContent process while such a reply was streamingattribute the frame time roughly half to JIT'd JS and half to WebKit layout:
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:
was also permanently disabled by a single error (see below).
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, andcontain: 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.
ThinkingBlockmemoizedEvery 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 --noEmitclean,eslint "src/**/*.{ts,tsx}"clean, desktop 840 testspass (90 files).
stop()finality, inactivity,afterProject, in-flight coalescing); 4 for theworker fallback; 1 DOM assertion that only the growing tail carries
data-streamed-block.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 replystreams, 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.