Skip to content

Studio: cap chat code blocks at a scrollable height and colour only the lines in view - #16864

Closed
shimmyshimmer wants to merge 7 commits into
mainfrom
studio-codeblock-capped
Closed

shimmyshimmer wants to merge 7 commits into
mainfrom
studio-codeblock-capped

Conversation

@shimmyshimmer

@shimmyshimmer shimmyshimmer commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Long code blocks in chat used to grow to the full length of the code, so a 4,000 line answer made the page thousands of pixels taller and every line of it got coloured. Code blocks now stop at a fixed height and scroll inside, like ChatGPT, and only the lines you can see get coloured.

This is one of two alternative PRs. This one keeps main's behaviour of colouring after you stop scrolling. The alternative, #16865, also colours while you scroll.

What changes

  • Capped height. A chat code block's body stops at min(30rem, 55vh) and scrolls inside (--unsloth-code-block-max-height overrides it). Printing removes the cap, so printed chats still show all the code.
  • Colour only what is in view. A capped block colours the lines in view plus one box height above and below, then stops. Scrolling inside the box colours more. A 4,000 line block colours 59 lines at rest, where main eventually colours all 4,000.
  • Streaming follows the newest line. A block that is still streaming stays scrolled to its last line until you scroll up inside it. Only the reader's own wheel, touch, key or pointer input stops the follow, and when the stream finishes the new body keeps the old one's scroll place instead of jumping back to the first line.
  • Header. The language label is a code icon plus the language's own name ("Python", "HTML", "TypeScript", "Bash"; unknown tags get a capital first letter) instead of the raw lowercase tag. Less space above it (the action buttons moved up with it), code lines up with the label, code fades out under the header once scrolled, and there is no padding under the code.
  • Actions. Word wrap, Expand code, Download, then Copy last. The icons are one even step smaller (14 px at the default size, even so they stay centred in their buttons).
  • Expand code. A capped block that holds more than it shows gets an Expand code button (Collapse code puts the cap back); the code does not shift when it expands.
  • Scrollbars. The vertical scrollbar is always visible and sits against the block's right edge, with a stable gutter so the code's right padding never changes and an unfaded strip under the top fade. The sideways scrollbar is a thin 6 px bar with no track, shown only while the pointer is over the block, and it takes the place of the bottom padding so a wide block is no taller. Long finished blocks sit in a paint-contained wrapper that clipped the vertical scrollbar; the wrapper now takes the body's bleed so the bar paints.
  • Per-block word wrap button. Each chat code block gets an Enable / Disable word wrap button. It starts from Appearance > Code > Word wrap and overrides it for that block only, in either direction. Reasoning fragments do not get it, since they are not one block.
  • Line wrap icons. Every wrap toggle (code blocks, the debug log in Settings, and the in-app browser file viewer) now uses the Hugeicons line-wrap and line-wrap-disable icons. They are MIT stroke-rounded icons that @hugeicons/core-free-icons 4.4.0 does not ship yet, so they are traced into lib/hugeicons-derived.ts until it does.
  • Code icon. Every source code icon in Studio (artifact cards, code and web file icons in attachments and the Library, the browser panel's source toggle, the remote code consent dialog, and the new header) now uses Hugeicons CodeXmlIcon. The plain CodeIcon is unchanged.

Performance

Headless Chromium against a running Studio, two seeded chats (8 blocks of 160 to 300 lines, and 4 blocks of 2,000 to 4,000 lines), CPU throttled 4x:

main this PR
Opening the normal chat, total main thread stalls 3.1 s 1.7 s
Opening the normal chat, longest stall 0.7 s 0.4 s
Opening the long code chat, total stalls 2.7 s 1.4 s
Opening the long code chat, longest stall 0.44 s 0.27 s
Coloured lines in a 4,000 line block at rest all 4,000, over time 59

Shiki still tokenizes a whole block once when it comes near the screen; this saves the rendering of the coloured spans, not the tokenizing.

Verification

  • npm run typecheck, npm test (15,000 pass), npm run build.
  • New and updated tests in code-fence-body.test.ts, appearance-code-display.test.ts and debug-log-export.test.ts cover the cap and its print override, the in-view colouring, streaming follow, the per-block wrap override the icon stubs and the header language names.
  • In a local Studio: the cap, the inner scroll colouring, the fade, the alignment, the wrap toggle in both directions, the icons in both states, the hover-only sideways scrollbar and the always-visible vertical one (checked with classic scrollbars in light and dark). During a fast page scroll this branch still shows uncoloured blocks until scrolling stops, as main does.
  • Live streaming with Gemma 4 E2B: the block followed its last line to the end and stayed there after the stream finished; after a 150 px scroll up mid-stream it held its place through the rest of the stream and the swap to the finished block.

Not tested here

  • Safari, Firefox and the Desktop app webviews. Timings are Chromium only.
  • Long wrapped lines: the in-view window still works from line boxes, but its margin covers fewer source lines.

shimmyshimmer and others added 3 commits November 5, 2026 22:03
…d stop following only on the reader's own scroll
…nly sideways scrollbar, smaller actions with Copy last, code-xml icon for source code

The vertical scrollbar of long finished code blocks was clipped by the paint-contained wrapper; the wrapper now takes the body's bleed so the scrollbar paints.
@shimmyshimmer

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T08:09:19.584353Z 04f78e7 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 318819ec62

ℹ️ 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".

Comment on lines +1222 to +1223
const next = innerLineWindow(code.current, body);
if (next !== null && !lineWindowColoured(tokens, progress, next)) setScrolled((n) => n + 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Resume paused highlighting when expanding a code block

For a long completed block whose visible lines have finished highlighting, clicking Expand code leaves the newly revealed lines permanently uncoloured. This branch only listens for inner scroll events, and after expansion innerLineWindow() returns null, so even a resulting scroll event cannot restart the work. CodeBlockActions changes the wrapper attribute without rerendering the memoized FenceBody, and scrolling the outer chat does not trigger this listener either. Observe changes to the body's size or expansion state and resume highlighting when the cap is removed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 983938a: while a capped block waits for a scroll, a ResizeObserver on the body also rechecks, and colouring resumes once the body stops scrolling internally (Expand code) or its view has uncoloured lines.

): void {
const following = useRef(true);
const mounted = tokens !== null && tokens.length > 0;
const key = `${language ?? ""}\n${source.slice(0, 200)}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scope saved scroll positions to an individual code block

When two Python blocks share their first 200 characters—for example, repeated import boilerplate—the module-wide cache treats them as the same block, including across chats. Scrolling up in the first block causes a newly mounted, unrelated streaming block to restore that offset and set following.current to false, so it no longer follows its newest lines despite the reader never scrolling it. Use a stable message/block identity to transfer scroll state across the streaming-to-finished remount without sharing it between distinct blocks.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 983938a: only a body that streamed saves its place (on unmount), and only a finished body mounting within the next 2 s takes it, once. A new streaming block never restores, so it always starts following. Checked live: a reader's place survives the stream finishing, and a followed stream still ends at the bottom.

…nd a code block's scroll place only from a finished stream to its own remount
@shimmyshimmer

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 983938a5a8

ℹ️ 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".

Comment on lines +4091 to +4092
max-height: var(--unsloth-code-block-max-height, min(30rem, 55vh));
overflow-y: auto;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Account for inner scrolling in the opt-in line window

When VITE_UNSLOTH_DEFER_FENCE_HIGHLIGHT=window is enabled, the new cap lets readers scroll beyond the highlighted lines without ever colouring the newly visible code. useLineWindow() still calculates contentTop from the stationary wrapper's bounding rectangle and uses the outer chat viewport; scrolling this body does not change either value, so its highlighting window remains near the beginning of the block. The new innerLineWindow() logic cannot compensate because useFenceHighlightLimit() explicitly excludes windowing mode. Update the windowing geometry to incorporate the body's scroll offset and viewport, or exclude that mode from the height cap.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: window is an opt-in build or runtime mode, not the shipped default (defer), and capped blocks in the default mode are coloured by the inner-view path. The line window is left as is for that experimental mode.

@shimmyshimmer

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 04f78e7428

ℹ️ 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".

@danielhanchen danielhanchen self-assigned this Nov 7, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 7, 2026
…oured while scrolling (unslothai#16865)

Same capped, scrollable chat code blocks as unslothai#16864, plus colour while
scrolling: code blocks no longer show as plain text during a fast scroll
and colour in only after you stop. This is one of two alternative PRs;
pick one.

## What this adds on top of unslothai#16864

Since unslothai#14242 made `defer` the default fence mode, colouring waits until
input has been quiet for 300 ms, so during a fast scroll every block
that comes into view is plain text and colours about 0.9 s after you
stop. With blocks capped, the work a block needs when it comes into view
is bounded (the lines in its box, at most one batch per frame), so it no
longer has to wait:

- `fence-highlight-queue.ts`: an entry can be marked as allowed to run
mid-scroll; the queue runs those ahead of older ones that still wait for
quiet.
- `schedule-idle-task.ts`: `nonScrollInputQuietIn()` ignores wheel,
scroll and touchmove (clicks, keys and pointer holds still count).
- `code-fence-defer.tsx`: reaching a block, and colouring the lines in
view inside a capped block, are marked mid-scroll safe. Everything else,
including the rest of a long block and grammar warm-up, still waits for
quiet. Holding a scrollbar thumb or a text selection in a block still
blocks colouring, as before.

Everything else (cap, Expand code, stable scrollbar gutter, in-view
colouring, streaming follow, header with code icon and language name,
action order and size, hover-only sideways scrollbar, per-block word
wrap, line wrap and code icons) is identical to unslothai#16864.

## Performance

Headless Chromium against a running Studio, same seeded chats as unslothai#16864,
fast wheel scroll (200 ticks):

| | main | unslothai#16864 | this PR |
| --- | --- | --- | --- |
| Frames showing an uncoloured block while scrolling (normal chat) | 448
of 604 | 214 of 613 | 0 of 611 |
| Time from stopping until every visible block is coloured | about 0.9 s
| about 1.6 s | about 10 ms |
| Page scroll, long code chat, CPU 4x: frames over 50 ms | 0 | not
measured | 0 |
| Page scroll, long code chat, CPU 4x: total stalls | 0 s | not measured
| 0.18 s |

Opening a chat costs the same as unslothai#16864 (about half of main's stall time
at 4x CPU).

## Verification

- `npm run typecheck`, `npm test` (15,001 pass), `npm run build`.
- On top of unslothai#16864's tests: a queue test that a mid-scroll entry runs
while older work waits for quiet, the updated `blocked` shape check, and
`noteInput` accepting a call without an event.
- In a local Studio: blocks stay coloured during a fast page scroll and
while scrolling inside a long block.
- Live streaming with Gemma 4 E2B: the block followed its last line to
the end and stayed there after the stream finished; after a 150 px
scroll up mid-stream it held its place through the rest of the stream
and the swap to the finished block.

## Not tested here

- Same as unslothai#16864: Safari, Firefox and the Desktop webviews.
- Frame timing while scrolling inside a single long block (colour was
checked, stalls were not timed).



## Screenshots

Same scripted reply with a 93-line Python block streamed into main
(left) and this PR (right), Chromium 1500x1000, message scrolled to its
start. Main lays out all 1632 px of the block in the page; this PR caps
it at 480 px, scrolls inside (following the end of the stream), and adds
the header with the language, word wrap, Expand code, download and copy.
The headless browser hides scrollbars.

![Long code block, main vs this
PR](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/16865/pr16865_before_after-cf032167d7d2.png)

---------

Co-authored-by: Unsloth <[email protected]>
Co-authored-by: danielhanchen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants