Repository navigation
Studio: colour chat code blocks from the worker while scrolling and streaming - #17160
Conversation
On WebKit there is no requestIdleCallback, so each mid-scroll fence step falls back to a timer that lands inside the scroll. The tokens there come from the worker, so reaching a block mid-scroll swaps its markup without colouring it, and scroll plus settle time on macOS WebKit rose from about 0.75 s to 4 to 5 s. Keep the mid-scroll path where idle callbacks exist and let WebKit wait for the reader to stop, as before.
…treaming Settled chat fences are tokenized by the highlight worker on every engine (before: WebKit and fences over 20,000 characters only), with the request posted at once and only the answer waiting in the quiet-work queue. Fences within three viewports are pre-tokenized in the worker during wheel and touch scrolls, a fence already on screen with no tokens is still tokenized in the latch within an 8 ms budget (never on WebKit), and the fence queue finishes several fences per step within 6 ms, without a 100 ms timer per step where requestIdleCallback is missing. A streamed fence tokenizes a growth of up to 1,024 characters at once, so each line is coloured as it lands instead of after the 250 ms refresh. Worker results share one style object per distinct style and drop the grammar state, which halves their structured clone. Rendered HTML is unchanged; without a worker everything falls back to the main thread as before.
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 886bc177b7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| unreached.delete(gate); | ||
| fenceSpeculator.remove(gate); | ||
| gate.warm(true); | ||
| gate.warm(true, performance.now() - started < LATCH_INLINE_MS && gateInView(gate)); |
There was a problem hiding this comment.
Forward the visibility argument through the registered warm callback
For an on-screen Chromium fence whose tokens have not been cached, this call's second argument never reaches FenceBlock's warm: useFenceReached registers the gate with warm: (tokens) => warmRef.current(tokens), discarding inView. Consequently, inView always defaults to false, so cold settled fences larger than 512 characters go to the worker even when the latch has inline budget available, allowing them to paint plain until its reply arrives. Update the hook's callback signature and registered wrapper to forward the visibility argument.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 73302c9581: the registered gate now passes the on-screen flag through to warm, so a latched fence that is already visible tokenizes inline again. The wiring test asserts the forwarding.
| const seeded = new Map<G, number>(); | ||
| let seededChars = 0; | ||
| let ranked: G[] | null = null; | ||
| const distance = new Map<G, number>(); |
There was a problem hiding this comment.
Release removed gates from the distance cache
After speculation ranks a long chat and the user navigates away, remove() clears each gate from the existing collections but not from this new strong-reference map. The map is cleared only by a subsequent order() call, and with no candidates left, no such call is scheduled. Because the speculator is module-global and each gate holds DOM nodes and component callbacks, the previous chat's detached DOM and source data remain retained indefinitely unless another chat triggers ranking. Delete distance entries when gates are removed, or use weak keys; a forced-GC reproduction confirms a removed gate remains alive until another ranking clears this map.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 73302c9581: remove() now drops the gate from the distance cache too, so an unmounted fence is not kept alive until the next rerank. Added a test that the cache empties after removal (it fails without the fix).
…the speculation distance cache [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
# Conflicts: # studio/frontend/src/components/assistant-ui/code-fence-defer.tsx # studio/frontend/tests/desktop-webkit-paths.test.ts [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
… their style Token spans each carried their colours as an inline style, and WebKit shares no computed style between elements with one, so a coloured block cost about 3x the style work (194 vs 60 ms for a 1250-node block). Every declaration a token can carry comes from the two bundled themes, so each is now a class written once at module load; the classes set the same custom properties, and a value outside the themes still goes inline. [skip ci]
|
@codex review |
|
A staging run of this PR failed Those are the two actions closest to what this PR changes (code-block highlighting while scrolling and streaming), so I would check whether either action errors out or times out on the 100K rung with the worker highlighter, and so records no comparable observation, before treating it as harness noise. The per-action logs are in the job's uploaded studiobench artifacts. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Always-coloured code blocks while scrolling and streaming, now without the WebKit scroll cost: the worker colours a block before its markup swaps (every engine), and token colours go through shared classes so WebKit can share their computed style.
Update: token colours through shared classes (b86272f)
The remaining macOS WebKit cost of colouring mid-scroll was not tokenising: each long frame carried 10 to 25 ms of JS and then 100 to 300 ms of style for a 700 to 1,700 node block, because every token span held its colours inline and WebKit shares no computed style between elements with an inline style (one 1,250-node block: 194 ms of style inline, 60 ms with classes).
fence-token-classes.tswrites one class per declaration the two bundled themes can produce, once, before any chat renders (WebKit restyles the whole document when a sheet changes later); spans use those classes and keep inline style only for a value outside the themes. Computed colour, style, weight and decoration match the inline version on all 3,071 spans in light and dark (WebKit).Clean runners (staging run 42635577083), main
ae176c8c18twice (A/A), arm order rotated, median (range):ubuntu Chromium CPU 4x: uncoloured frames 0% (main 70-75%), scroll long tasks 820 ms (main about 20 s), colour after stop 110 ms. Width change remains about 1.6x main on macOS WebKit (inherited from the worker path); that is the one open gap.
Follows #16865 (capped, scrollable chat code blocks with colour while scrolling), now on main. #17149 (merged) holds #16865's mid-scroll fence work on engines without
requestIdleCallback; this branch runs it mid-scroll on every engine again, because the worker now colours a block before its markup swaps.Problem
Measured against #16865 and against main (its merge base) in a seeded long chat with 180 code fences of mixed lengths (2 to about 450 lines) in 12 languages:
requestIdleCallback, so every mid-scroll step of the fence queue waited on a 100 ms timer, and the queue ran one fence per step.Fix
requestIdleCallbacka mid-scroll step runs right after the frame instead of after a 100 ms timer.Results
On top of the scroll-regression fix (clean runners, run 2)
Arms: pre-#16865
340b48c6f1, main997441a41ctwice (A/A), the fix6712bff6bdtwice (A/A), and this branch on the fix886bc177b7. 3 repeats per leg, arm order rotated, every served page matched its build. Median (range).macos-15 WebKit
ubuntu-latest Chromium, CPU 4x
The fix does not touch Chromium (it has
requestIdleCallback), so its Chromium arms read as main. On macOS WebKit this branch buys full colour mid-scroll and while streaming at about main's pre-fix scroll cost; on Chromium at 4x it colours everything at about a third of main's scroll cost, still above pre-#16865.Clean GitHub runners, run 1 (on main, before the fix)
Arms: pre-#16865 main
340b48c6f1, current main997441a41cbuilt twice (A/A), and this branch on current main. One job per OS and repeat, 3 repeats with the arm order rotated, each arm's served page checked against its own build. Median (range).ubuntu-latest Chromium
ubuntu-latest Chromium, CPU 4x
macos-15 WebKit
macos-15 Chromium
Loaded development host (earlier, against #16865 before it merged)
Median of 3 repeats, arms interleaved per repeat (range in brackets). main = the merge base of #16865. 60-turn chat, 180 fences, 1400x1000 viewport, 200 wheel ticks of 120 px every 8 ms from the bottom, then a scripted stream into a 143-line Python fence. "Long tasks" are Long Tasks API durations in Chromium and the sum of frames over 50 ms in WebKit.
Chromium
Chromium, CPU throttled 4x
WebKit (Playwright, macOS user agent)
Two earlier interleaved runs of the same comparison (with an earlier revision of this branch) agree on the colour rows: this branch at 0 to 1% uncoloured scroll frames in Chromium and WebKit unthrottled. At 4x the figure is unstable on this host for both branches: this branch read 0 to 1%, 29% and 75% in different runs, #16865 0 to 26% and 90%; the plain blocks are ones not yet reached (the reach step starves under throttling plus host load), not ones waiting for tokens, 0% plain streamed lines in Chromium and 1 to 3% in WebKit, and 0 ms streaming lag. Their cost rows moved up to 2x between runs on the loaded host (see Not covered).
Where this branch costs more than #16865, all in WebKit: opening (3.1 s against 2.7 s in long frames), scroll plus settle (4.5 s against 3.1 s, because the colouring #16865 leaves until after the stop now happens during the scroll), and the worst single keystroke after the scroll (4.0 s against 2.9 s; main reads 1.3 s, so most of that gap predates this branch). A long-callback probe around the typing window found no fence work running there in either arm; the likeliest cause is that this branch leaves every block the scroll passed coloured, so the page holds more highlighted spans when typing restyles it, but that is not proven.
CPU profile of the same fast scroll in Chromium: on #16865 the Shiki regex engine and TextMate scanner are the largest JavaScript cost (about 0.9 s of the scroll window; 2.4 s of opening the chat); on this branch neither appears in the main thread's top functions, and what remains is applying worker replies (0.24 s),
getSelection(0.21 s, the existing selection guard) and layout reads.Skipping layout for lines outside a capped block (measured separately, not included)
Asked alongside the worker change: lines of a capped block far outside its box are still laid out. I measured two ways of skipping that work, each against this branch on its own, and neither is in this PR.
content-visibility: autowithcontain-intrinsic-size: auto 1lhon each line of a settled block'scode(only with line numbers and wrap off, since style containment would number every line 1 and wrapped lines have no fixed height). Same bench, 3 interleaved repeats:Opening is unchanged, and width changes were already cheap. Scrolling the page gets much worse, because every line becomes its own containment root and the browser has to recheck them all as the page moves. With three 2,000-line blocks: forced relayout on a width change 1.4 against 1.3 ms in Chromium (a block over 240 lines lays out at max-content width, so its lines do not relayout when the column changes) and 39 against 44 ms in WebKit, mount to colour within noise. It also changes behaviour: a long line below the box no longer counts toward the block's horizontal scroll width until it is scrolled into view (3 to 4 of 180 blocks shrank), and in one WebKit run two blocks came out 6 px taller.
The WebKitGTK line window on every engine (lines outside the window render as plain text; this applies to blocks over 240 lines), three 2,000-line blocks: frames while scrolling inside a block improve (worst frame 63 to 27 ms in Chromium, 165 to 24 ms in WebKit), but a width change costs more (forced relayout 1.2 to 45 ms in Chromium, 36 to 114 ms in WebKit), mounting gets worse in WebKit (worst frame 771 to 1548 ms) and Chromium long tasks rise (1.36 to 1.73 s). This matches the existing comment that line-window updates regress fast scrollbar drags in Chromium. It is a possible follow-up limited to scrolling inside very long blocks, not a general win.
Behaviour
<pre>HTML of 43 fully coloured blocks over 12 languages is byte-identical between main, Studio: cap chat code blocks at a scrollable height and keep them coloured while scrolling #16865 and this branch, in Chromium and WebKit (same hash in both engines). Light and dark themes, Shiki CSS variables, copy, wrap, line numbers and Expand code are untouched; they read the same tokens.Tests
npm test: 15,133 of 15,136 pass. Failures: the KaTeX stylesheet Vite test (fails with a symlinkednode_moduleson this host, with or without this change) and once a wall-clock test (background-load-notice: a hung read is abandoned), which passed in the other full run and 3 of 3 reruns.node scripts/typecheck.mjs --testspasses. ESLint on the changed files reports nothing new.playwright_long_thread_gate.py: PASS on the final commit (40 turns 0.83 ms/key selector matching, 200 turns 0.74 ms/key,:has()0.15 to 0.16 ms/key, area matrix 30 areas x 14 pages). Two earlier runs on this host failed in the area matrix (source_cardhover never opened in liquid-3d at 200 turns, and once a 60 s page-load timeout); the same gate on Studio: cap chat code blocks at a scrollable height and keep them coloured while scrolling #16865 failed once too, on a different check (dialog style recalc 35 ms over a 30 ms budget), so these read as load flakes.playwright_code_block_flicker.py(Chromium): every variant behaved as its contract says (tree: 0 collapses, 0 placeholder frames, 0 sweep shift over 3 repeats).playwright_find_in_page.py: Chromium 226/226. WebKit is flaky on this host on both sides: Studio: cap chat code blocks at a scrollable height and keep them coloured while scrolling #16865 failed 1 of 3 runs (one check), this branch failed 4 of 4 runs (one check in three of them, a cascade of 27 in one), with a different failing check ID every run. That fixture has no code fences, so none of the changed code runs there; this is not proven harmless on a quiet host.Not covered