Skip to content

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

TypeScript

fix(mcp-host): read an image target's title from the right, in one pass

marcoteb3134e06e45db
diff --git a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
index 6d91688b8..df427cf44 100644
--- a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
+++ b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
@@ -283,6 +283,31 @@ describe('image targets', () => {
const [span] = inlineSpans('![Sales](image (1).png)')
expect(span).toEqual({ text: 'Sales', image: 'image (1).png' })
})
+
+ it('reads a title as the one-pattern reader it replaced did, without its backtracking', () => {
+ const titled = /^(.*?)\s+(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\))$/
+ const angled = /^<([^<>\n]*)>/
+ // No backslash or ampersand, so unescaping and entity decoding leave the name as read.
+ const alphabet = ['a', 'b', '.', ' ', '\t', '\n', '\r', '\u2028', '"', "'", '(', ')', '<', '>']
+ let seed = 1
+ const next = () => (seed = (seed * 1103515245 + 12345) % 2147483648)
+ for (let i = 0; i < 20000; i++) {
+ let input = ''
+ for (let n = next() % 12; n > 0; n--) input += alphabet[next() % alphabet.length]
+ const t = input.trim()
+ const expected = angled.exec(t)?.[1] ?? titled.exec(t)?.[1] ?? t
+ expect(imageTarget(input), JSON.stringify(input)).toBe(expected)
+ }
+ })
+
+ it('reads a target with a long run of spaces in linear time', () => {
+ const spaces = ' '.repeat(100000)
+ for (const inside of [`a${spaces}b`, `a${spaces}"title"`, `a${spaces}(b`, `${spaces}x`]) {
+ const started = performance.now()
+ imageTarget(inside)
+ expect(performance.now() - started).toBeLessThan(200)
+ }
+ })
})
describe('block quotes', () => {
@@ -342,6 +367,8 @@ describe('hostile input', () => {
imageOpen: '![a]('.repeat(40000),
imageGroups: `![a](${'(x)'.repeat(60000)}`,
imageGroupsRepeated: `![a](${'(x)'.repeat(5)}`.repeat(10000),
+ imageTargetSpaces: `![a](a${' '.repeat(100000)}b)`,
+ imageTitleSpaces: `![a](a${' '.repeat(100000)}"t")`,
link: '[a](http://x'.repeat(16000),
linkEscapes: `[${'\\]'.repeat(100000)}`,
bracket: '['.repeat(200000),
diff --git a/mcp-host/src/workflow/inlineMarkup.ts b/mcp-host/src/workflow/inlineMarkup.ts
index 21860602c..1f42a4b78 100644
--- a/mcp-host/src/workflow/inlineMarkup.ts
+++ b/mcp-host/src/workflow/inlineMarkup.ts
@@ -446,8 +446,30 @@ function unescapeMarkdown(text: string): string {
export function imageTarget(inside: string): string {
const t = inside.trim()
const angle = /^<([^<>\n]*)>/.exec(t)
- const titled = /^(.*?)\s+(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\))$/.exec(t)
- return decodeEntities(unescapeMarkdown(angle ? angle[1] : titled ? titled[1] : t))
+ const titled = angle ? undefined : beforeTitle(t)
+ return decodeEntities(unescapeMarkdown(angle ? angle[1] : (titled ?? t)))
+}
+
+const CLOSES_TITLE: Record<string, string> = { '"': '"', "'": "'", ')': '(' }
+const LINE_TERMINATOR = /[\n\r\u2028\u2029]/
+
+/**
+ * `t` without a trailing title (`"…"`, `'…'` or `(…)` after whitespace), or
+ * undefined when it has none. Read from the right, so a long run of spaces
+ * costs one pass; a target that spans a line has no title, as before.
+ */
+function beforeTitle(t: string): string | undefined {
+ const open = CLOSES_TITLE[t[t.length - 1]]
+ if (open === undefined || t.length < 2) return undefined
+ const start = t.lastIndexOf(open, t.length - 2)
+ if (start <= 0) return undefined
+ const title = t.slice(start + 1, -1)
+ if (title.includes('\n') || (open === '(' && title.includes(')'))) return undefined
+ let end = start
+ while (end > 0 && /\s/.test(t[end - 1])) end--
+ if (end === start) return undefined
+ const target = t.slice(0, end)
+ return LINE_TERMINATOR.test(target) ? undefined : target
}
/** One stretch of inline text and the formatting it carries. */