Skip to content

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

TypeScript

fix(mcp-host): class the minus sign and zero-width joiners as Unicode does

marcoteb657845f7511e
diff --git a/mcp-host/src/workflow/__tests__/pdfBidi.test.ts b/mcp-host/src/workflow/__tests__/pdfBidi.test.ts
index 44d0a283d..71ab6c311 100644
--- a/mcp-host/src/workflow/__tests__/pdfBidi.test.ts
+++ b/mcp-host/src/workflow/__tests__/pdfBidi.test.ts
@@ -23,6 +23,15 @@ describe('bidi classes', () => {
expect(bidiClass(' '.codePointAt(0)!)).toBe('WS')
})
+ it('classes number signs and zero-width joiners as the Unicode data does', () => {
+ expect(bidiClass(0x2212)).toBe('ES')
+ expect(bidiClass(0xfb29)).toBe('ES')
+ expect(bidiClass(0xff0d)).toBe('ES')
+ expect(bidiClass(0x2213)).toBe('ET')
+ for (const cp of [0x200c, 0x200d, 0x200b, 0x2060, 0xfeff, 0xad])
+ expect(bidiClass(cp)).toBe('BN')
+ })
+
it('only flags text a right-to-left paragraph would reorder', () => {
expect(hasRtl('Revenue 2026 ≥ 5')).toBe(false)
expect(hasRtl('Revenue \u0645\u0631\u062D\u0628\u0627')).toBe(true)
@@ -30,6 +39,36 @@ describe('bidi classes', () => {
})
describe('display order', () => {
+ it('sets a minus sign as it sets a hyphen-minus', () => {
+ expect(display('\u05D0 \u22125')).toBe(display('\u05D0 -5').replace('-', '\u2212'))
+ expect(display('\u05D0 3\u22125')).toBe(display('\u05D0 3-5').replace('-', '\u2212'))
+ })
+
+ it('resolves the text around a zero-width joiner as if it were not there', () => {
+ const texts = [
+ '\u0645\u06CC\u200C\u062E\u0648\u0627\u0647\u0645 12%',
+ '\u05D0 1\u200D% b',
+ 'a \u0628\u200D1, 2',
+ '\u05D0\u200C(a)\u200D \u0661',
+ '1\u200B+2 \u05D0',
+ ]
+ const joiner = (cp: number) => cp >= 0x200b && cp <= 0x200d
+ for (const text of texts) {
+ const cps = [...text].map(c => c.codePointAt(0)!)
+ const joined = resolveParagraph(cps)
+ const bare = resolveParagraph(cps.filter(cp => !joiner(cp)))
+ const kept = joined.levels.filter((_, i) => !joiner(cps[i]))
+ expect(kept, JSON.stringify(text)).toEqual(bare.levels)
+ expect(joined.base).toBe(bare.base)
+ }
+ })
+
+ it('keeps a zero-width joiner beside the letter before it', () => {
+ const text = '\u05D0 \u0628\u200D 12'
+ const order = display(text)
+ expect(order.indexOf('\u200D')).toBe(order.indexOf('\u0628') - 1)
+ })
+
it('reverses a Hebrew paragraph word by word and keeps numbers left to right', () => {
// "price 100" in Hebrew reads right to left, so the number ends up at the left.
expect(display('\u05DE\u05D7\u05D9\u05E8 100')).toBe('100 \u05E8\u05D9\u05D7\u05DE')
diff --git a/mcp-host/src/workflow/pdfBidi.ts b/mcp-host/src/workflow/pdfBidi.ts
index e38551089..2c1e960e6 100644
--- a/mcp-host/src/workflow/pdfBidi.ts
+++ b/mcp-host/src/workflow/pdfBidi.ts
@@ -11,7 +11,19 @@
* supported.
*/
-export type BidiClass = 'L' | 'R' | 'AL' | 'EN' | 'AN' | 'ES' | 'ET' | 'CS' | 'NSM' | 'WS' | 'ON'
+export type BidiClass =
+ | 'L'
+ | 'R'
+ | 'AL'
+ | 'EN'
+ | 'AN'
+ | 'ES'
+ | 'ET'
+ | 'CS'
+ | 'NSM'
+ | 'BN'
+ | 'WS'
+ | 'ON'
const MARK = /[\p{Mn}\p{Me}]/u
const LETTER = /[\p{L}\p{Mc}\p{Nd}\p{Nl}]/u
@@ -63,7 +75,21 @@ const TERMINATORS: ReadonlyArray<readonly [number, number]> = [
[0x066a, 0x066a],
[0x2030, 0x2034],
[0x20a0, 0x20cf],
- [0x2212, 0x2213],
+ [0x2213, 0x2213],
+]
+
+/** Plus and minus signs, U+2212 MINUS SIGN among them. */
+const NUMBER_SIGNS = new Set([
+ 0x2b, 0x2d, 0x207a, 0x207b, 0x208a, 0x208b, 0x2212, 0xfb29, 0xfe62, 0xfe63, 0xff0b, 0xff0d,
+])
+
+/** Zero-width joiners and other characters the rules pass over (X9). */
+const BOUNDARY_NEUTRALS: ReadonlyArray<readonly [number, number]> = [
+ [0xad, 0xad],
+ [0x180e, 0x180e],
+ [0x200b, 0x200d],
+ [0x2060, 0x2064],
+ [0xfeff, 0xfeff],
]
const SEPARATORS = new Set([0x2c, 0x2e, 0x2f, 0x3a, 0xa0, 0x060c, 0x202f, 0x2044, 0xfe50, 0xfe52])
@@ -75,9 +101,10 @@ export function bidiClass(cp: number): BidiClass {
if (inRanges(cp, ARABIC_DIGITS)) return 'AN'
if (inRanges(cp, TERMINATORS)) return 'ET'
if (SEPARATORS.has(cp)) return 'CS'
+ if (NUMBER_SIGNS.has(cp)) return 'ES'
+ if (inRanges(cp, BOUNDARY_NEUTRALS)) return 'BN'
if (inRanges(cp, STRONG_AL)) return 'AL'
if (inRanges(cp, STRONG_R)) return 'R'
- if (cp === 0x2b || cp === 0x2d) return 'ES'
if (cp === 0x20 || cp === 0x09 || cp === 0x0c || (cp >= 0x2000 && cp <= 0x200a) || cp === 0x3000)
return 'WS'
if (cp === 0x200e || LETTER.test(ch)) return 'L'
@@ -137,9 +164,32 @@ function bracketPairs(codePoints: number[], t: BidiClass[]): Array<[number, numb
return pairs.sort((a, b) => a[0] - b[0])
}
-/** Resolve the embedding level of each code point of one paragraph. */
+/**
+ * Resolve the embedding level of each code point of one paragraph. A boundary
+ * neutral such as a zero-width joiner takes no part in the rules (X9); it gets
+ * the level of the character before it, so it stays inside its word.
+ */
export function resolveParagraph(codePoints: number[]): BidiParagraph {
const classes = codePoints.map(bidiClass)
+ const kept = classes.flatMap((cls, i) => (cls === 'BN' ? [] : [i]))
+ const resolved = resolveLevels(
+ kept.map(i => codePoints[i]),
+ kept.map(i => classes[i])
+ )
+ const levels: number[] = new Array<number>(classes.length)
+ kept.forEach((i, k) => (levels[i] = resolved.levels[k]))
+ let previous = kept.length > 0 ? levels[kept[0]] : resolved.base
+ for (let i = 0; i < levels.length; i++) {
+ if (classes[i] === 'BN') levels[i] = previous
+ else previous = levels[i]
+ }
+ return { base: resolved.base, levels, classes }
+}
+
+function resolveLevels(
+ codePoints: number[],
+ classes: BidiClass[]
+): { base: 0 | 1; levels: number[] } {
const n = classes.length
const firstStrong = classes.map(strongDirection).find(Boolean)
const base: 0 | 1 = firstStrong === 'R' ? 1 : 0
@@ -222,7 +272,7 @@ export function resolveParagraph(codePoints: number[]): BidiParagraph {
if (base === 0) return cls === 'R' ? 1 : cls === 'AN' || cls === 'EN' ? 2 : 0
return cls === 'L' || cls === 'EN' || cls === 'AN' ? 2 : 1
})
- return { base, levels, classes }
+ return { base, levels }
}
/**