Skip to content

The agent that grows with you

fix(desktop): inline math closed after an escaped backslash stays shielded (#92371)

Brooklyn Nicholson08453d7800e1
diff --git a/apps/desktop/src/components/assistant-ui/markdown-text.test.ts b/apps/desktop/src/components/assistant-ui/markdown-text.test.ts
index cbe9d479fce..b42b1ea43c7 100644
--- a/apps/desktop/src/components/assistant-ui/markdown-text.test.ts
+++ b/apps/desktop/src/components/assistant-ui/markdown-text.test.ts
@@ -584,4 +584,23 @@ describe('preprocessMarkdown', () => {
expect(output).toContain('$$E = mc^2$$')
})
+
+ it('shields inline math closed after an escaped backslash', () => {
+ // #92371: in `$x[2]\\$` the `\\` is an escaped backslash (a literal
+ // backslash, valid TeX), so the final `$` really closes the span. A
+ // one-character lookbehind on the closer saw the backslash and refused
+ // to shield, letting the prose citation-marker rewrite eat `[2]`.
+ const output = preprocessMarkdown(String.raw`Per the paper, $x[2]\\$ is the value.`)
+
+ expect(output).toBe(String.raw`Per the paper, $x[2]\\$ is the value.`)
+
+ // Minimal shape: the span containing only a+escaped-backslash.
+ expect(preprocessMarkdown(String.raw`$a\\$ plain`)).toBe(String.raw`$a\\$ plain`)
+ })
+
+ it('still escapes bare currency dollars next to an escaped-backslash span', () => {
+ // The fix must not widen the math branch into currency: an escaped `\$`
+ // stays a price opener, an escaped `\\` stays a literal backslash.
+ expect(preprocessMarkdown(String.raw`costs \$5 and $a\\$ ok`)).toBe(String.raw`costs \$5 and $a\\$ ok`)
+ })
})
diff --git a/apps/desktop/src/lib/markdown-preprocess.ts b/apps/desktop/src/lib/markdown-preprocess.ts
index 31f0bae06ab..3dfa62b3858 100644
--- a/apps/desktop/src/lib/markdown-preprocess.ts
+++ b/apps/desktop/src/lib/markdown-preprocess.ts
@@ -45,7 +45,15 @@ const INLINE_CODE_SPLIT_RE = /(`[^`\n]+`)/g
// must not end the span — the same distinction findClosingSingleDollar draws
// via isEscapedAt. The two alternatives are disjoint on their first character,
// so the body cannot backtrack ambiguously.
-const MATH_SPAN_SPLIT_RE = /((?<!\\)\$\$[\s\S]*?(?<!\\)\$\$|(?<!\\)\$(?:[^\n$\\]|\\[^\n])+?(?<!\\)\$)/g
+//
+// The inline branch deliberately has NO lookbehind on its CLOSING `$`: the
+// body alternation consumes escape pairs atomically, so any `$` the engine
+// reaches after the body is by construction unescaped — a trailing `\\$`
+// (escaped backslash, valid TeX) would otherwise defeat a one-character
+// lookbehind and unshield the span ($a\\$ mis-split, #92371). The display
+// branch keeps its guard: its `[\s\S]*?` body does not step over escape
+// pairs, so it genuinely needs it.
+const MATH_SPAN_SPLIT_RE = /((?<!\\)\$\$[\s\S]*?(?<!\\)\$\$|(?<!\\)\$(?:[^\n$\\]|\\[^\n])+?\$)/g
const LATEX_DISPLAY_OPEN_LINE_RE = /^([ \t]*(?:>[ \t]*)*(?:(?:[-+*]|\d+[.)])[ \t]+)?[ \t]*)\\{1,2}\[[ \t]*\r?$/
const LATEX_DISPLAY_CLOSE_LINE_RE = /^([ \t]*(?:>[ \t]*)*(?:(?:[-+*]|\d+[.)])[ \t]+)?[ \t]*)\\{1,2}\][ \t]*\r?$/
const CUSTOM_DISPLAY_MATH_LINE_RE = /^([ \t]*(?:>[ \t]*)*(?:(?:[-+*]|\d+[.)])[ \t]+)?[ \t]*)\[\/math\][ \t]*\r?$/