Skip to content

The agent that grows with you

fix(desktop): keep a markdown render failure inside its own message

Brooklyn Nicholsonfa92aa1984b0
diff --git a/apps/desktop/src/components/assistant-ui/markdown-text.overflow.test.tsx b/apps/desktop/src/components/assistant-ui/markdown-text.overflow.test.tsx
new file mode 100644
index 00000000000..3f886626d99
--- /dev/null
+++ b/apps/desktop/src/components/assistant-ui/markdown-text.overflow.test.tsx
@@ -0,0 +1,80 @@
+import { cleanup, render, screen } from '@testing-library/react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { MarkdownTextContent } from './markdown-text'
+
+// Regression coverage for the "workspace failed to render / Maximum call stack
+// size exceeded" crash loop (#78000, #85295).
+//
+// Two independent recursions in the markdown pipeline can overflow the JS call
+// stack while React is rendering:
+//
+// 1. Raw inline HTML — `rehype-raw` hands `<unk><unk>…` to parse5, and
+// `hast-util-from-parse5` recurses once per level of unclosed nesting. A
+// degenerating model emitting thousands of `<unk>` tokens as reasoning is
+// the reported trigger, and the payload persists to the session, so the
+// crash repeats on every reload.
+// 2. Deeply nested block structure — `> > > …` recurses in mdast→hast, which
+// no HTML guard can prevent.
+//
+// The throw escaping to the workspace boundary is what blanks the whole app, so
+// what matters is that every markdown surface stays up and keeps the text
+// readable. These mount the real production component; a wrapper that silently
+// stopped being applied has to fail here.
+afterEach(cleanup)
+
+// A stack overflow inside React's render logs through console.error; the test
+// asserts on rendered output, not on the noise.
+function renderQuietly(node: Parameters<typeof render>[0]) {
+ const spy = vi.spyOn(console, 'error').mockImplementation(() => undefined)
+
+ try {
+ return render(node)
+ } finally {
+ spy.mockRestore()
+ }
+}
+
+const DEGENERATE_UNK = `Let${'<unk>'.repeat(16_383)}`
+const DEEP_BLOCKQUOTE = `${'> '.repeat(10_000)}still readable`
+
+describe('markdown surface survives stack-overflow content', () => {
+ it.each([
+ ['degenerate <unk> reasoning run', DEGENERATE_UNK],
+ ['deeply nested blockquotes', DEEP_BLOCKQUOTE]
+ ])('renders %s without throwing, and keeps it readable', (_label, text) => {
+ const { container } = renderQuietly(<MarkdownTextContent isRunning={false} text={text} />)
+
+ expect(container.textContent).toBeTruthy()
+ })
+
+ // The crash is a property of the CONTENT, not of which part carries it: the
+ // same text arrives as an answer, as reasoning, or in a tool result, and all
+ // three render through this component. Guarding only one of them is what let
+ // the bug survive an earlier fix attempt.
+ it.each([
+ ['reasoning (disableArtifacts)', { disableArtifacts: true }],
+ ['assistant answer', {}]
+ ])('survives on the %s path', (_label, surfaceProps) => {
+ const { container } = renderQuietly(
+ <MarkdownTextContent isRunning={false} text={DEGENERATE_UNK} {...surfaceProps} />
+ )
+
+ expect(container.textContent).toBeTruthy()
+ })
+
+ it('still renders while the message is streaming', () => {
+ const { container } = renderQuietly(<MarkdownTextContent isRunning text={DEGENERATE_UNK} />)
+
+ expect(container.textContent).toBeTruthy()
+ })
+
+ it('leaves ordinary markdown on the rich path', async () => {
+ render(<MarkdownTextContent isRunning={false} text={'# Disk report\n\nC: is **full**'} />)
+
+ // A real heading element — the rich renderer ran, rather than degrading
+ // the whole message to plain text.
+ expect(await screen.findByRole('heading', { name: 'Disk report' })).toBeTruthy()
+ expect(screen.getByText('full')).toBeTruthy()
+ })
+})
diff --git a/apps/desktop/src/components/assistant-ui/markdown-text.tsx b/apps/desktop/src/components/assistant-ui/markdown-text.tsx
index e6245d85842..2158e54662c 100644
--- a/apps/desktop/src/components/assistant-ui/markdown-text.tsx
+++ b/apps/desktop/src/components/assistant-ui/markdown-text.tsx
@@ -14,6 +14,7 @@ import { ExpandableBlock } from '@/components/chat/expandable-block'
import { PreviewAttachment } from '@/components/chat/preview-attachment'
import { chunkByLines, SyntaxHighlighter } from '@/components/chat/shiki-highlighter'
import { ZoomableImage } from '@/components/chat/zoomable-image'
+import { ErrorBoundary } from '@/components/error-boundary'
import { detectArtifact } from '@/lib/artifact-detect'
import { normalizeExternalUrl, openExternalLink, PrettyLink } from '@/lib/external-link'
import { createMemoizedMathPlugin } from '@/lib/katex-memo'
@@ -602,23 +603,46 @@ function MarkdownTextSurface({
}
return (
- <StreamdownTextPrimitive
- components={components}
- containerClassName={cn(MARKDOWN_CONTAINER_CLASS_NAME, containerClassName)}
- containerProps={containerProps}
- defer={defer}
- lineNumbers={false}
- mode="streaming"
- // Incomplete-markdown repair runs in preprocessWithTailRepair on the
- // full accumulated text; the built-in tail-bounded remend is disabled
- // because a custom parseMarkdownIntoBlocksFn is supplied, and
- // parseIncompleteMarkdown stays false to avoid a second full-text
- // remend pass.
- parseIncompleteMarkdown={false}
- parseMarkdownIntoBlocksFn={parseMarkdownIntoBlocksCached}
- plugins={plugins}
- preprocess={preprocessWithTailRepair}
- />
+ // Last line of defence for the whole markdown surface — assistant answers,
+ // reasoning, tool output and user bubbles all render through here.
+ //
+ // The pipeline is recursive in several places we don't own (parse5 →
+ // `hast-util-from-parse5` on raw HTML, `mdast-util-to-hast` on nested
+ // block structure), so pathological content can still throw
+ // `RangeError: Maximum call stack size exceeded` from inside Streamdown's
+ // render. `clampHtmlNestingDepth` removes the reachable cause we found;
+ // this catches whatever we haven't. Without it the throw unwinds past
+ // MessageRenderBoundary — which deliberately re-throws anything that isn't
+ // the transient assistant-ui lookup race — and blanks the entire workspace
+ // behind "workspace failed to render", on every reload, because the
+ // offending message is replayed from the session each time.
+ //
+ // Degrading to HugeTextFallback keeps the text readable and the rest of
+ // the transcript alive. The error stays latched for this surface: content
+ // that overflowed the stack will overflow again, and remounting per token
+ // during streaming would cost far more than the plain rendering saves.
+ <ErrorBoundary
+ fallback={() => <HugeTextFallback containerClassName={containerClassName} text={text} />}
+ label="markdown-render"
+ >
+ <StreamdownTextPrimitive
+ components={components}
+ containerClassName={cn(MARKDOWN_CONTAINER_CLASS_NAME, containerClassName)}
+ containerProps={containerProps}
+ defer={defer}
+ lineNumbers={false}
+ mode="streaming"
+ // Incomplete-markdown repair runs in preprocessWithTailRepair on the
+ // full accumulated text; the built-in tail-bounded remend is disabled
+ // because a custom parseMarkdownIntoBlocksFn is supplied, and
+ // parseIncompleteMarkdown stays false to avoid a second full-text
+ // remend pass.
+ parseIncompleteMarkdown={false}
+ parseMarkdownIntoBlocksFn={parseMarkdownIntoBlocksCached}
+ plugins={plugins}
+ preprocess={preprocessWithTailRepair}
+ />
+ </ErrorBoundary>
)
}