Repository navigation
Description
Context
MermaidBlock is the component of the Mermaid content-renderer pack. The pack is wrapped by MermaidContent (MermaidContent, packages/ui-react/src/content-renderers/mermaid-content.tsx:5-10) on the seam that #2079 delivered. Besides the screen SVG, it prepares a print-theme SVG ahead of time, because "Browser printing cannot wait for a render in beforeprint" (packages/ui-react/src/components/chat/mermaid-block.tsx:60-72). That second effect starts a full print render 250 ms after any change of source.
While a fence streams, every pause of 250 ms or more renders the print SVG of a half-written diagram, which is thrown away a moment later. Pauses are common: model latency, tool calls, or a slow connection. Every render, screen or print, goes through one serialized chain (serialized, packages/ui-react/src/lib/mermaid.ts:60-67). Each one adds an entry to the 100-entry SVG cache (svgCache, same file:69-70), so a mid-stream print render also delays the next screen render and evicts useful cache entries.
Ruling 5 of docs/decisions/module-renderers.md binds this: "Every surface prints white", including the browser's own print of the chat. Without a print SVG, the block prints its screen SVG (mermaid-block.tsx:84, :95-98), so a finished answer must never print the screen SVG.
The completion-to-ready window. Between the moment streaming ends and the moment the print render for the final source resolves, a finished answer can already be printed. Today that window is 250 ms plus a print render, and during it the block prints either its screen SVG (no printSvg yet) or a print SVG left over from an earlier source, because printSvg keeps the last good result (mermaid-block.tsx:60-72). Moving the print render to after streaming does not remove the window, so this issue defines what prints inside it.
How this relates to #2079. This changes only when the pack's component schedules its print render. It does not touch ContentRendererProps, the registry, the manifest or the conformance suite. Neither this nor #2079's remaining work waits on the other. #2079's definition of done, that Mermaid "renders identically through the seam" and prints white, must still hold.
Guide performance/schedule-tasks-by-priority: "background: Tasks that are not time-critical (e.g., analytics, prefetching)." The guide's polyfill fallback is replaced by setTimeout, under this project's no-polyfill policy.
Scope
- Give
MermaidBlocka "the enclosing text is still streaming" signal through a package-internal React context.MarkdownContentalready knows it (streaming,packages/ui-react/src/components/chat/markdown-content.tsx:22-31) and provides the context. Outside a streaming message the context defaults to not streaming. - While streaming, do not schedule the print render.
- Once not streaming (immediately, for file and share mode), schedule one print render for the current source with
scheduler.postTask(fn, { priority: "background" })when available, otherwisesetTimeout(fn, 250). Cancel it on source change or unmount (TaskController, orclearTimeout). - Record the source each print SVG was rendered for. A print SVG whose source differs from the current source is never shown, not even in print.
- Inside the window (no print SVG for the current source yet), print media shows the current source as the block's
<pre>code view in the print theme on white paper, and hides the screen SVG. It never prints the screen-theme SVG and never a stale print SVG. When the print render resolves, print media switches to the print SVG.
Out of scope
- The screen render's debounce and throttle (
mermaid-block.tsx:38-58). - A
streamingfield onContentRendererProps, or any other seam change. - The static and export paths, which already render in the print theme.
- Inserting SVG nodes rather than markup (ui-react: Mermaid SVG serialized as XML is re-parsed as HTML on insertion #2594).
Acceptance criteria
- While the context says streaming, a source change followed by a 1 s quiet period triggers no
renderMermaidSvg(…, "print")call. A unit test with a spied renderer fails on the tree before the change. - When streaming ends, exactly one print render runs for the final source. File mode (no streaming context) still renders the print SVG without any streaming transition.
-
scheduler.postTaskis feature-detected. With it removed, thesetTimeoutfallback produces the same result (unit test). - Real-browser proof: after a streamed Mermaid answer completes, emulating print media shows the print-theme SVG (
[data-mermaid-print]visible, screen SVG hidden). Extendpackages/ui-react/tests/browser/mermaid-content-renderer.layout.tsx. - Immediate-print proof, with the print render deliberately delayed (a held renderer promise): emulating print media right after streaming ends shows the current source's code view on a white print ground, the screen SVG hidden and no
[data-mermaid-print]. A stale print SVG from an earlier source is not visible. Releasing the render then shows the print SVG. This test fails on the current tree (which prints the screen SVG in that window). - A unit test shows a print SVG rendered for source A is not shown after the source changes to B.
- Android support:
scheduler.postTaskexists in both Chromium targets. A browser without it (an older Firefox for Android) takes thesetTimeoutfallback.
Verification
bun run test packages/ui-react/tests
node scripts/visual.mjs --project=ui-react-layout --filter=mermaid-content-renderer
node scripts/visual.mjs --project=ui-react-layout --filter=content-renderers
bun run typecheck
bun run lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT:commit prefix, and the doc updated in the same commit. - This changes a published package's behaviour, so it needs a changeset.
The context is internal and the seam is unchanged. Patch changeset for @schlessera/brain-ui-react.
2026-10-24: defined the completion-to-ready window and what prints inside it (the current source's code view on white, never the screen SVG or a stale print SVG), and added immediate-print coverage with the print render deliberately delayed; eventual readiness alone could not prove ruling 5.
Activity