Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): read a code fence of more than three backticks whole
marcotebaea7bf84f119
diff --git a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts b/mcp-host/src/workflow/__tests__/inlineMarkup.test.tsindex c28680d98..f47d713e7 100644--- a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts+++ b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts@@ -1,10 +1,12 @@import { describe, expect, it } from 'vitest'import {+ closesFence,decodeEntities,htmlToMarkdownInline,htmlToPlainText,imageTarget,inlineSpans,+ openingFence,quoteParagraphs,withoutClosingHashes,} from '../inlineMarkup'@@ -174,6 +176,28 @@ describe('block quotes', () => {})})+describe('code fences', () => {+ it('takes the whole run of backticks or tildes as the fence', () => {+ expect(openingFence('````markdown')).toEqual({ marker: '````', language: 'markdown' })+ expect(openingFence(' ~~~ js ')).toEqual({ marker: '~~~', language: 'js' })+ expect(openingFence('``x')).toBeUndefined()+ })++ it('closes a fence only on a run of its character at least as long', () => {+ expect(closesFence('```', '````')).toBe(false)+ expect(closesFence('~~~~', '```')).toBe(false)+ expect(closesFence(' `````', '````')).toBe(true)+ })++ it('reads a long run of backticks in linear time', () => {+ const line = `${'`'.repeat(200000)}\r`+ const started = performance.now()+ openingFence(line)+ closesFence(line, '```')+ expect(performance.now() - started).toBeLessThan(2000)+ })+})+describe('hostile input', () => {// Each input is about 200 KB on one line: an unclosed construct repeated, so// a pattern that rescans the rest of the line from every opening is quadratic.diff --git a/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts b/mcp-host/src/workflow/__tests__/internalTools.docx.test.tsindex 9d2019290..f9b3ae0e6 100644--- a/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts+++ b/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts@@ -270,6 +270,12 @@ describe('generate_docx code blocks', () => {expect(printed.indexOf('bash')).toBeLessThan(printed.indexOf('kubectl get pods'))expect(printed.filter(t => t === 'bash')).toHaveLength(1)})++ it('reads a fence of four backticks whole and keeps a shorter one inside it as code', async () => {+ const printed = texts(await documentXml({ body: '````markdown\n```js\nx\n```\n````\nAfter' }))+ expect(printed.slice(0, 4)).toEqual(['markdown', '```js', 'x', '```'])+ expect(printed).toContain('After')+ })})describe('generate_docx headings', () => {diff --git a/mcp-host/src/workflow/__tests__/internalTools.pdf.layout.test.ts b/mcp-host/src/workflow/__tests__/internalTools.pdf.layout.test.tsindex 25977f47f..330735ac7 100644--- a/mcp-host/src/workflow/__tests__/internalTools.pdf.layout.test.ts+++ b/mcp-host/src/workflow/__tests__/internalTools.pdf.layout.test.ts@@ -350,6 +350,17 @@ describe('PDF body text', () => {expect(fenced.pages[0].text).not.toMatch(/1\..*1\./)})+ it('reads a fence of four backticks whole and keeps a shorter one inside it as code', async () => {+ const { pages } = await render({+ filename: 'q.pdf',+ body: '````markdown\n```js\nx\n```\n````\nAfter',+ })+ const text = allText(pages)+ expect(text).toContain('markdown')+ expect(text).not.toContain('`markdown')+ expect(text).toMatch(/```js.*x.*```.*After/s)+ })+it('reads the HTML tags and entities models write', async () => {const { pages } = await render({filename: 'h.pdf',diff --git a/mcp-host/src/workflow/docxMarkdown.ts b/mcp-host/src/workflow/docxMarkdown.tsindex 326154b76..c976c57ed 100644--- a/mcp-host/src/workflow/docxMarkdown.ts+++ b/mcp-host/src/workflow/docxMarkdown.ts@@ -6,7 +6,13 @@ import { inlineRuns, textRuns } from './docxInline'import { docxDirection, isRtlText } from './docxScript'import { DOCX_LIST_LEVELS, type DocxListNumbering, type DocxPalette, docxHex } from './docxStyle'import { buildDocxTable } from './docxTable'-import { imageTarget, quoteParagraphs, withoutClosingHashes } from './inlineMarkup'+import {+ closesFence,+ imageTarget,+ openingFence,+ quoteParagraphs,+ withoutClosingHashes,+} from './inlineMarkup'export interface DocxBodyContext {palette: DocxPalette@@ -19,7 +25,6 @@ export interface DocxBodyContext {const BULLET_RE = /^[-*+]\s+/const ORDERED_RE = /^(\d{1,9})[.)]\s+/const HEADING_RE = /^(#{1,6})\s+(.*)$/-const FENCE_RE = /^(```|~~~)/const QUOTE_RE = /^>\s?/const IMAGE_LINE_RE = /^!\[([^\]\n]*)\]\(((?:[^()\n]|\([^()\n]*\))*)\)$/const SEPARATOR_CELL_RE = /^:?-+:?$/@@ -147,7 +152,7 @@ function startsOtherBlock(line: string): boolean {if (t.startsWith('|')) return falsereturn (HEADING_RE.test(t) ||- FENCE_RE.test(t) ||+ openingFence(t) !== undefined ||QUOTE_RE.test(t) ||IMAGE_LINE_RE.test(t) ||isListLine(line)@@ -192,14 +197,13 @@ export function bodyToDocxChildren(body: string, ctx: DocxBodyContext): (Paragraconst line = lines[i]const trimmed = line.trim()- const fence = FENCE_RE.exec(trimmed)+ const fence = openingFence(trimmed)if (fence) {- const marker = fence[1]// The language after the opening fence is noted above the block, as in the PDF.- const language = trimmed.slice(marker.length).trim()+ const { marker, language } = fenceconst code: string[] = []i++- while (i < lines.length && !lines[i].trimStart().startsWith(marker)) {+ while (i < lines.length && !closesFence(lines[i], marker)) {code.push(lines[i])i++}diff --git a/mcp-host/src/workflow/inlineMarkup.ts b/mcp-host/src/workflow/inlineMarkup.tsindex 9dd22af48..707f443d3 100644--- a/mcp-host/src/workflow/inlineMarkup.ts+++ b/mcp-host/src/workflow/inlineMarkup.ts@@ -315,6 +315,28 @@ export function withoutClosingHashes(text: string): string {return /\s/.test(t[end - 1]) ? t.slice(0, end).trimEnd() : t}+/** The opening of a fenced code block: its run of backticks or tildes and the language after it. */+export function openingFence(line: string): { marker: string; language: string } | undefined {+ const t = line.trim()+ const run = fenceRun(t)+ return run >= 3 ? { marker: t.slice(0, run), language: t.slice(run).trim() } : undefined+}++/** Whether `line` closes the block `marker` opened: a run of its character at least as long. */+export function closesFence(line: string, marker: string): boolean {+ const t = line.trimStart()+ return t[0] === marker[0] && fenceRun(t) >= marker.length+}++/** Length of the run of backticks or tildes that starts `text`. */+function fenceRun(text: string): number {+ const ch = text[0]+ if (ch !== '`' && ch !== '~') return 0+ let n = 1+ while (text[n] === ch) n+++ return n+}+/*** The paragraphs of a block quote, from its lines without their `>` marker. A* quoted line left empty ends a paragraph; the lines of one paragraph run on,diff --git a/mcp-host/src/workflow/internalTools.ts b/mcp-host/src/workflow/internalTools.tsindex f6f1a1ed6..1d9b2f82a 100644--- a/mcp-host/src/workflow/internalTools.ts+++ b/mcp-host/src/workflow/internalTools.ts@@ -92,9 +92,11 @@ import {sanitizeForFont,} from './fonts'import {+ closesFence,htmlToPlainLines,htmlToPlainText,inlineSpans,+ openingFence,quoteParagraphs,withoutClosingHashes,} from './inlineMarkup'@@ -1835,13 +1837,12 @@ function bodyToContent(body: string, palette: PdfPalette, env: PdfBodyEnv): Cont// Fenced code block. Everything up to the closing fence is verbatim, so a// shell snippet or a config sample keeps its spacing instead of being// reflowed into paragraphs.- const fence = /^(```|~~~)(.*)$/.exec(trimmed)+ const fence = openingFence(trimmed)if (fence) {- const marker = fence[1]- const language = fence[2].trim()+ const { marker, language } = fenceconst code: string[] = []i++- while (i < lines.length && !lines[i].trimStart().startsWith(marker)) {+ while (i < lines.length && !closesFence(lines[i], marker)) {code.push(lines[i])i++}