Skip to content

Studio: bound the code highlighter cache for tool cells, previews and READMEs - #10609

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
claxman:fix/9230-tool-code-bounded-plugin
Oct 5, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
claxman:fix/9230-tool-code-bounded-plugin

Conversation

@claxman

@claxman claxman commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9230.

@streamdown/code 1.1.1 memoises every tokenisation in a module-level Map that is never evicted. Chat already uses the bounded createCodePlugin from code-plugin.ts, but three surfaces still imported the package export:

  • tool-code-cell.tsx: one entry per finished tool cell (the cell renders plain while streaming, so it is not per refresh window).
  • markdown-preview.tsx: one entry per distinct document. Editable previews (recipe note nodes, prompt storage) mint one per edit.
  • model-readme.tsx: one entry per README.

All three now import a single shared createCodePlugin() instance from shared-code-plugin.ts, so they keep one set of Shiki highlighters as before and the cache is capped at 512 fences / 512K characters. Same engine and default themes as the package export.

tests/streamdown-code-bounded.test.ts fails on any runtime import of @streamdown/code under src/ (type imports are allowed) and checks the three surfaces use the shared plugin.

Measurements (Node 24, forced GC, real plugin code)

Scenario @streamdown/code export shared bounded plugin
200 finished ~19 KB Python tool cells 227.6 MB retained 28.9 MB
Typing a 6 KB fence in a preview, 300 edits 51.3 MB retained 0.3 MB

Token parity (content, colours, both themes) against the package export: identical on Python, JS, TS, bash, JSON, YAML, Rust, plain text and an unknown language, with a theme-swap negative control detected. The one difference is a c++ fence, where the bounded plugin resolves the alias and colours return as a keyword, matching chat.

npm run typecheck passes and npm test passes (10101/10101).

Before / after

Markdown attachment preview of the same .md file on the merge base (left) and this PR (right). The Python block is identical. In the c++ block, the bounded plugin resolves the alias, so return and 0 are now coloured (return light-theme colour #24292E -> #D73A49); this is the only token whose style changed.

Markdown preview before and after

tool-code-cell imported the default @streamdown/code plugin, whose token
cache has no cap. Use the bounded createCodePlugin with the chat throttle
off so a growing tool fence is not kept as every prefix.
ToolCodeCell skips shiki while streaming, so throttle: false never ran.
Keep the bounded createCodePlugin import swap only.
@claxman

This comment has been minimized.

@danielhanchen danielhanchen self-assigned this Oct 4, 2026
# Conflicts:
#	studio/frontend/src/components/assistant-ui/tool-code-cell.tsx
…ugin

Markdown previews and model READMEs also used the @streamdown/code export,
whose token cache never evicts; editable previews mint an entry per edit.
Share one createCodePlugin instance across the three surfaces so they keep
a single set of Shiki highlighters, and replace the tool-cell AST test with
a tree-wide guard against runtime imports of @streamdown/code.
@danielhanchen danielhanchen changed the title Studio: bound the tool code cell highlighter Studio: bound the code highlighter cache for tool cells, previews and READMEs Oct 5, 2026
@shimmyshimmer

Copy link
Copy Markdown
Member

Review record for #10609, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#411.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 901452f2c (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 901452f2c3

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 901452f2c (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 5, 2026
@danielhanchen
danielhanchen merged commit 8254d6e into unslothai:main Oct 5, 2026
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.

Studio: tool code cells still stream through the unbounded @streamdown/code token cache

3 participants