An open source design system that's fully customizable and agent ready
TypeScript
fix(markdown): keep the streaming cache across open fences and chunk boundaries (#5407)
Cindy Zhangcbb991e91e6d
diff --git a/.changeset/markdown-incremental-cache-holds.md b/.changeset/markdown-incremental-cache-holds.mdnew file mode 100644index 0000000000..d92151d2e9--- /dev/null+++ b/.changeset/markdown-incremental-cache-holds.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown streaming: `parseMarkdownIncremental` no longer throws away its settled blocks while a code fence is open, or when a chunk happens to end on a newline — both re-parsed the whole document, so a long streamed response got slower the longer it grew. Blocks rebuilt across a streamed 500-paragraph document: 126,756 → 1,869.+@cixzhangdiff --git a/packages/core/src/Markdown/incremental.test.ts b/packages/core/src/Markdown/incremental.test.tsindex 63bbbf6061..9ba7b58bda 100644--- a/packages/core/src/Markdown/incremental.test.ts+++ b/packages/core/src/Markdown/incremental.test.ts@@ -131,12 +131,26 @@ describe('parseMarkdownIncremental', () => {expect(state.settledText).toBe('');});- it('unclosed fence keeps everything unsettled', () => {+ it('settles what precedes an unclosed fence, and nothing inside it', () => {const state = createIncrementalState();const input ='# Title\n\n```python\ndef foo():\n pass\n\n# still inside fence';parseMarkdownIncremental(input, state);- expect(state.settledBlocks).toHaveLength(0);+ expect(state.settledText).toBe('# Title');+ expect(state.settledBlocks).toHaveLength(1);+ });++ it('does not hold back lines inside an unclosed fence', () => {+ const state = createIncrementalState();+ const blocks = parseMarkdownIncremental(+ 'Intro\n\n```ts\ntype Id = string | number;',+ state,+ );+ const code = blocks.find(block => block.type === 'codeblock');+ expect(code).toMatchObject({+ type: 'codeblock',+ content: 'type Id = string | number;',+ });});it('handles task list streaming', () => {diff --git a/packages/core/src/Markdown/parser.perf.test.ts b/packages/core/src/Markdown/parser.perf.test.tsindex 0e2a759b19..302cdae905 100644--- a/packages/core/src/Markdown/parser.perf.test.ts+++ b/packages/core/src/Markdown/parser.perf.test.ts@@ -8,10 +8,7 @@ import {} from './parser';import type {BlockNode, ParseOptions} from './parser';-function generateAIResponse(- paragraphs: number,- {codeBlocks = true}: {codeBlocks?: boolean} = {},-): string {+function generateAIResponse(paragraphs: number): string {const sections: string[] = [];for (let i = 0; i < paragraphs; i++) {const mod = i % 6;@@ -23,9 +20,7 @@ function generateAIResponse(break;case 1:sections.push(- codeBlocks- ? '```typescript\nfunction process(data: Record<string, unknown>[]) {\n return data.filter(item => item.valid)\n .map(item => transform(item))\n .reduce((acc, val) => merge(acc, val), {});\n}\n```'- : `Paragraph ${i} continues the explanation with a second block of prose, keeping the block mix realistic without opening a fence.`,+ '```typescript\nfunction process(data: Record<string, unknown>[]) {\n return data.filter(item => item.valid)\n .map(item => transform(item))\n .reduce((acc, val) => merge(acc, val), {});\n}\n```',);break;case 2:@@ -265,29 +260,27 @@ describe('parseMarkdownIncremental cache', () => {);// A chunk re-parses its unsettled tail, not the document — so the typical// chunk's cost is a constant, and a 10x longer document does not move it.- expect(medians).toEqual([2, 2, 2]);+ expect(medians).toEqual([1, 1, 1]);});- it('bounds the p95 chunk, not just the median one, at every document length', () => {+ it('bounds the worst chunk, not just the typical one, at every document length', () => {const chunkSize = 50;- const p95s = [50, 200, 500].map(paragraphs =>- percentile(- blocksBuiltPerChunk(- generateAIResponse(paragraphs, {codeBlocks: false}),- chunkSize,- ),- 0.95,+ const worst = [50, 200, 500].map(paragraphs =>+ Math.max(+ ...blocksBuiltPerChunk(generateAIResponse(paragraphs), chunkSize),),);console.log(- ` p95 blocks built per chunk (50/200/500 paragraphs): ${p95s.join('/')}`,+ ` worst chunk's blocks built (50/200/500 paragraphs): ${worst.join('/')}`,);- // The median above cannot see a cache that holds for the typical chunk and- // collapses on a minority of them — p50-flat, still O(N). Fence-free takes- // out the worse of the two paths that still dump the whole cache on main- // (#5378); the other is live at ~2% of chunks, which puts the cliff just- // under p98 — so p95 is the bound with margin rather than the highest one.- expect(p95s).toEqual([3, 3, 3]);+ // A cache that dumps on a minority of chunks is flat at the median and at+ // p95 while the total work is still quadratic: the blank-line path of+ // #5378 hit ~1.4% of chunks on this fixture, and no percentile bound saw+ // it. The worst chunk does, and each collapse costs more as the document+ // grows — so this asserts the worst one, and that a 10x document does not+ // move it. Deterministic: same fixture, same chunking, integers with no+ // timing input.+ expect(worst).toEqual([3, 3, 3]);});it('keeps the cache when source ranges are asked for', () => {diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex e3f4efc9c4..774d34fd22 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -1586,12 +1586,26 @@ export function createIncrementalState(): IncrementalState {/*** Find the line-index of the last blank line that is NOT inside a fenced code- * block. Returns -1 when nothing is settled (unclosed fence or no blank line).+ * block, and report whether a fence is still open at the end of the input.+ * Returns -1 when nothing is settled.+ *+ * This index must never move backwards as more of the document arrives. The+ * caller's cache is keyed on the settled text staying a prefix of what it was,+ * so a boundary that retracts by one line costs a re-parse of every block in+ * the document. Two things used to retract it: a blank last line, which is+ * just the newline the stream has written so far and stops being blank as soon+ * as the next chunk appends to it; and an open fence, which used to collapse+ * the boundary to -1 even though the content before the fence opened cannot be+ * changed by anything typed inside it.*/-function findSettledBoundary(lines: string[]): number {+function findSettledBoundary(lines: string[]): {+ boundary: number;+ openFence: boolean;+} {let inFence = false;let fenceMarker = '';let lastBoundary = -1;+ let boundaryBeforeFence = -1;for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {const line = lines[lineIndex];@@ -1602,6 +1616,7 @@ function findSettledBoundary(lines: string[]): number {if (!inFence) {inFence = true;fenceMarker = fenceMatch[1];+ boundaryBeforeFence = lastBoundary;} else if (fenceMatch[1].startsWith(fenceMarker[0]) &&fenceMatch[1].length >= fenceMarker.length@@ -1611,12 +1626,20 @@ function findSettledBoundary(lines: string[]): number {}}- if (!inFence && line.trim() === '' && lineIndex > 0) {+ if (+ !inFence &&+ line.trim() === '' &&+ lineIndex > 0 &&+ lineIndex < lines.length - 1+ ) {lastBoundary = lineIndex;}}- return inFence ? -1 : lastBoundary;+ return {+ boundary: inFence ? boundaryBeforeFence : lastBoundary,+ openFence: inFence,+ };}/**@@ -1945,17 +1968,23 @@ export function parseMarkdownIncremental(linkDefs.size > 0 ? {...opts, linkDefs} : opts;const lines = input.split('\n');- const boundary = findSettledBoundary(lines);+ const {boundary, openFence} = findSettledBoundary(lines);if (boundary < 0) {- // Inside an unclosed fence or no blank-line boundary — full re-parse+ // Nothing settled yet — no blank line, or a fence opened before the first+ // one — so there is no prefix to keep and the whole input is re-parsed.state.prevInput = input;return parseMarkdownImpl(input, parseOpts);}const settledText = lines.slice(0, boundary).join('\n');const unsettledRaw = lines.slice(boundary).join('\n').trim();- const unsettledText = trimUnsettledStructural(unsettledRaw);+ // Structural trimming holds back lines that look like an incomplete list or+ // table, which inside a fence is ordinary code: a TypeScript union or a `- `+ // would disappear from the code block as it streams.+ const unsettledText = openFence+ ? unsettledRaw+ : trimUnsettledStructural(unsettledRaw);let settledBlocks: BlockNode[];