Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): close a code fence only on a line that holds nothing else

marcoteb6129c6f78131
diff --git a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
index bd360be9d..2e48f87c9 100644
--- a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
+++ b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
@@ -381,6 +381,13 @@ describe('code fences', () => {
expect(closesFence(' `````', '````')).toBe(true)
})
+ it('keeps a fence open on a line that adds text after the run, as CommonMark does', () => {
+ expect(closesFence('```js', '```')).toBe(false)
+ expect(closesFence('``` x', '```')).toBe(false)
+ expect(closesFence('``` \t', '```')).toBe(true)
+ expect(closesFence('```\r', '```')).toBe(true)
+ })
+
it('reads a long run of backticks in linear time', () => {
const line = `${'`'.repeat(200000)}\r`
const started = performance.now()
diff --git a/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts b/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts
index 2cf5283c5..ac3e2f9a1 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.docx.test.ts
@@ -295,6 +295,18 @@ describe('generate_docx headings', () => {
})
})
+describe('generate_docx code blocks', () => {
+ it('keeps a fence line with a language inside the block, as code', async () => {
+ const xml = await documentXml({
+ body: '```\nExample:\n```js\nlet a = 1\n```\nAfter',
+ })
+ const text = paragraphs(xml).map(p =>
+ [...p.matchAll(/<w:t[^>]*>([^<]*)</g)].map(m => m[1]).join('')
+ )
+ expect(text).toEqual(expect.arrayContaining(['```js', 'let a = 1', 'After']))
+ })
+})
+
describe('generate_docx lists', () => {
function levels(xml: string): number[] {
return [...xml.matchAll(/<w:ilvl w:val="(\d+)"\/>/g)].map(m => Number(m[1]))
diff --git a/mcp-host/src/workflow/inlineMarkup.ts b/mcp-host/src/workflow/inlineMarkup.ts
index 91958e43c..b99c226ee 100644
--- a/mcp-host/src/workflow/inlineMarkup.ts
+++ b/mcp-host/src/workflow/inlineMarkup.ts
@@ -397,10 +397,15 @@ export function openingFence(line: string): { marker: string; language: string }
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. */
+/**
+ * Whether `line` closes the block `marker` opened: a run of its character at
+ * least as long, and nothing else. A line such as ```js inside the block is
+ * code, as CommonMark reads it.
+ */
export function closesFence(line: string, marker: string): boolean {
- const t = line.trimStart()
- return t[0] === marker[0] && fenceRun(t) >= marker.length
+ const t = line.trim()
+ const run = fenceRun(t)
+ return t[0] === marker[0] && run >= marker.length && run === t.length
}
/** Length of the run of backticks or tildes that starts `text`. */