Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): report every face the renderer refuses, and only scripts no face draws
marcoteb4bd8223d3e54
diff --git a/mcp-host/src/workflow/__tests__/fonts.ready.test.ts b/mcp-host/src/workflow/__tests__/fonts.ready.test.tsindex ff37de708..54f2611c8 100644--- a/mcp-host/src/workflow/__tests__/fonts.ready.test.ts+++ b/mcp-host/src/workflow/__tests__/fonts.ready.test.ts@@ -1,6 +1,6 @@/**- * Font registration on an image whose bundled faces cannot be read: the failure- * is reported once, and the system faces are still registered for charts.+ * Font registration on an image whose faces cannot be read or are refused: the+ * failure is reported, and the faces that remain are still used.*/import { afterEach, describe, expect, it, vi } from 'vitest'import { GlobalFonts } from '@napi-rs/canvas'@@ -9,6 +9,29 @@ afterEach(() => {vi.restoreAllMocks()})+/** A letter of each script the PDF warns about. */+const LETTER: Record<string, number> = {+ CJK: 0x4e2d,+ Arabic: 0x0627,+ Hebrew: 0x05d0,+ Devanagari: 0x0915,+ Bengali: 0x0995,+ Gurmukhi: 0x0a15,+ Gujarati: 0x0a95,+ Oriya: 0x0b15,+ Tamil: 0x0b95,+ Telugu: 0x0c15,+ Kannada: 0x0c95,+ Malayalam: 0x0d15,+ Thai: 0x0e01,+ Lao: 0x0e81,+ Khmer: 0x1780,+ Myanmar: 0x1000,+ Armenian: 0x0531,+ Georgian: 0x10d0,+ Ethiopic: 0x1200,+}+describe('ensureFontsReady', () => {it('reports bundled faces it cannot register and still registers the system faces', async () => {vi.resetModules()@@ -33,19 +56,78 @@ describe('ensureFontsReady', () => {expect(error).toHaveBeenCalledTimes(1)})- it('reports a bundled face the renderer refuses without throwing', async () => {+ it('reports a bundled face the renderer refuses without throwing, and still tries the other', async () => {vi.resetModules()const { logger } = await import('../../logger')const error = vi.spyOn(logger, 'error').mockImplementation(() => undefined)// register answers null, not an error, for a face it cannot use.- vi.spyOn(GlobalFonts, 'register').mockImplementationOnce(() => null)+ const register = vi.spyOn(GlobalFonts, 'register').mockImplementationOnce(() => null)const fonts = await import('../fonts')fonts.ensureFontsReady()+ expect(register).toHaveBeenCalledTimes(2)expect(error).toHaveBeenCalledTimes(1)expect(error).toHaveBeenCalledWith(- expect.objectContaining({ err: expect.stringContaining('regular') }),+ expect.objectContaining({ err: 'the renderer refused the regular face' }),'Bundled Roboto faces could not be loaded')})++ it('reports a system face the renderer refuses without throwing', async () => {+ vi.resetModules()+ const { logger } = await import('../../logger')+ const warn = vi.spyOn(logger, 'warn').mockImplementation(() => undefined)+ const system = vi.spyOn(GlobalFonts, 'registerFromPath').mockImplementation(() => null)+ const fonts = await import('../fonts')++ fonts.ensureFontsReady()+ expect(system).toHaveBeenCalled()+ expect(warn).toHaveBeenCalledWith(+ { file: system.mock.calls[0][0], err: 'the renderer refused the face' },+ 'Font face could not be registered'+ )+ })++ it('falls back to the bundled face when the renderer refuses the PDF body face', async () => {+ vi.resetModules()+ const { logger } = await import('../../logger')+ const warn = vi.spyOn(logger, 'warn').mockImplementation(() => undefined)+ const fonts = await import('../fonts')+ fonts.ensureFontsReady()+ const register = GlobalFonts.registerFromPath.bind(GlobalFonts)+ vi.spyOn(GlobalFonts, 'registerFromPath').mockImplementation((file, alias) =>+ alias === fonts.PDF_FONT_FAMILY ? null : register(file, alias)+ )++ const body = fonts.pdfFontDescriptors()[fonts.PDF_FONT_FAMILY]+ expect(Buffer.isBuffer(body.normal)).toBe(true)+ const refused = warn.mock.calls.find(+ ([, msg]) => msg === 'PDF body face could not be registered; the bundled Roboto face is used'+ )+ if (!refused) {+ // Only an image with a body face of its own registers one; CI installs Noto and DejaVu.+ expect(process.env.CI, 'CI installs the body faces').not.toBe('true')+ return+ }+ expect(refused[0]).toEqual(expect.objectContaining({ err: 'the renderer refused the face' }))+ })++ it('names in its warning only scripts no face draws in a PDF', async () => {+ vi.resetModules()+ const { logger } = await import('../../logger')+ const warn = vi.spyOn(logger, 'warn').mockImplementation(() => undefined)+ const fonts = await import('../fonts')+ const glyphs = fonts.pdfGlyphSource()+ glyphs.isFallback('none')++ const call = warn.mock.calls.find(+ ([, msg]) =>+ msg === 'No installed face draws these scripts, so PDFs leave their characters out'+ )+ const named: string[] = (call?.[0] as { scripts?: string[] } | undefined)?.scripts ?? []+ for (const script of named) {+ expect(LETTER[script], script).toBeDefined()+ expect(glyphs.familyFor(LETTER[script], fonts.PDF_FONT_FAMILY), script).toBeUndefined()+ }+ })})diff --git a/mcp-host/src/workflow/__tests__/fonts.test.ts b/mcp-host/src/workflow/__tests__/fonts.test.tsindex 35438d301..1c5588a0c 100644--- a/mcp-host/src/workflow/__tests__/fonts.test.ts+++ b/mcp-host/src/workflow/__tests__/fonts.test.ts@@ -75,24 +75,48 @@ describe('font provisioning', () => {describe('missingScripts', () => {const ALL = (_prefix: string) => true+ const NONE = (_cp: number) => falseit('reports no script whose preferred face alone is missing', () => {// Debian ships Noto Sans Arabic but not its UI build.- expect(missingScripts(prefix => prefix !== 'NotoSansArabicUI', true)).toEqual([])- expect(missingScripts(ALL, true)).toEqual([])+ expect(missingScripts(prefix => prefix !== 'NotoSansArabicUI', true, NONE)).toEqual([])+ expect(missingScripts(ALL, true, NONE)).toEqual([])})it('reports a script none of whose faces is installed', () => {- expect(missingScripts(prefix => !prefix.includes('Arabic'), true)).toEqual(['Arabic'])- expect(missingScripts(ALL, false)).toEqual(['CJK'])+ expect(missingScripts(prefix => !prefix.includes('Arabic'), true, NONE)).toEqual(['Arabic'])+ expect(missingScripts(ALL, false, NONE)).toEqual(['CJK'])})it('reports the sixteen further scripts on an image with only the Arabic and Hebrew faces', () => {- const alpine = (prefix: string) => /Arabic|Hebrew/.test(prefix)- const missing = missingScripts(alpine, true)+ const arabicAndHebrew = (prefix: string) => /Arabic|Hebrew/.test(prefix)+ const missing = missingScripts(arabicAndHebrew, true, NONE)expect(missing).toHaveLength(16)expect(missing).toEqual(expect.arrayContaining(['Devanagari', 'Thai', 'Ethiopic']))})++ it('does not report a script another installed face draws', () => {+ // DejaVu Sans draws Armenian and Georgian.+ const dejaVu = (cp: number) => cp === 0x0531 || cp === 0x10d0+ const missing = missingScripts(prefix => /Arabic|Hebrew/.test(prefix), true, dejaVu)+ expect(missing).toHaveLength(14)+ expect(missing).not.toContain('Armenian')+ expect(missing).not.toContain('Georgian')+ expect(missingScripts(ALL, false, cp => cp === 0x4e2d)).toEqual([])+ })++ it('asks which letters are drawn only for scripts with no face of their own', () => {+ const asked: number[] = []+ missingScripts(+ prefix => prefix !== 'NotoSansThai',+ true,+ cp => {+ asked.push(cp)+ return true+ }+ )+ expect(asked).toEqual([0x0e01])+ })})describe('glyph coverage', () => {diff --git a/mcp-host/src/workflow/fonts.ts b/mcp-host/src/workflow/fonts.tsindex c8f41e5eb..53538f3fe 100644--- a/mcp-host/src/workflow/fonts.ts+++ b/mcp-host/src/workflow/fonts.ts@@ -116,7 +116,7 @@ function registerSystemFonts(): void {}for (const file of files) {try {- GlobalFonts.registerFromPath(file)+ if (!GlobalFonts.registerFromPath(file)) throw new Error('the renderer refused the face')} catch (err) {// A face the renderer cannot parse is not available; the image's package may be damaged.logger.warn({ file, err: errorText(err) }, 'Font face could not be registered')@@ -135,11 +135,12 @@ export function ensureFontsReady(): void {try {const f = loadRobotoFaces()// register answers null, without throwing, for a face it cannot use.- if (!GlobalFonts.register(f.normal, CHART_FONT_FAMILY)) {- throw new Error('the renderer refused the regular face')- }- if (!GlobalFonts.register(f.bold, `${CHART_FONT_FAMILY} Bold`)) {- throw new Error('the renderer refused the bold face')+ const refused = [+ GlobalFonts.register(f.normal, CHART_FONT_FAMILY) ? undefined : 'regular',+ GlobalFonts.register(f.bold, `${CHART_FONT_FAMILY} Bold`) ? undefined : 'bold',+ ].filter(Boolean)+ if (refused.length > 0) {+ throw new Error(`the renderer refused the ${refused.join(' and ')} face`)}} catch (err) {// The system faces still register, so charts draw in them rather than in nothing.@@ -284,7 +285,8 @@ const LATIN_PROBES = ['A', 'g', '0']function coverageScore(file: string): number {const alias = '__ClerumProbeFace'try {- GlobalFonts.registerFromPath(file, alias)+ // A refused face would leave the alias on the face probed before it.+ if (!GlobalFonts.registerFromPath(file, alias)) return -1} catch {return -1}@@ -425,11 +427,13 @@ function resolvePdfFaces(): PdfFaces {// in the face the PDF will embed.const file = Array.isArray(chosen.normal) ? chosen.normal[0] : chosen.normaltry {- if (typeof file === 'string') GlobalFonts.registerFromPath(file, PDF_FONT_FAMILY)+ if (typeof file === 'string' && !GlobalFonts.registerFromPath(file, PDF_FONT_FAMILY)) {+ throw new Error('the renderer refused the face')+ }pdfFaces = chosenreturn pdfFaces} catch (err) {- // Unparseable face: fall through to the bundled one, which covers less.+ // Unparseable or refused face: fall through to the bundled one, which covers less.logger.warn({ file, err: errorText(err) },'PDF body face could not be registered; the bundled Roboto face is used'@@ -530,43 +534,55 @@ const FALLBACK_PREFIXES = [* are listed; fontkit loops forever on some Meetei Mayek, Kaithi and Nastaliq* sequences, so no installed face is used unlisted.*/-const MORE_SCRIPT_PREFIXES = [- 'NotoSansDevanagari',- 'NotoSansBengali',- 'NotoSansGurmukhi',- 'NotoSansGujarati',- 'NotoSansOriya',- 'NotoSansTamil',- 'NotoSansTelugu',- 'NotoSansKannada',- 'NotoSansMalayalam',- 'NotoSansThai',- 'NotoSansLao',- 'NotoSansKhmer',- 'NotoSansMyanmar',- 'NotoSansArmenian',- 'NotoSansGeorgian',- 'NotoSansEthiopic',+const MORE_SCRIPTS: ReadonlyArray<readonly [prefix: string, letter: number]> = [+ ['NotoSansDevanagari', 0x0915],+ ['NotoSansBengali', 0x0995],+ ['NotoSansGurmukhi', 0x0a15],+ ['NotoSansGujarati', 0x0a95],+ ['NotoSansOriya', 0x0b15],+ ['NotoSansTamil', 0x0b95],+ ['NotoSansTelugu', 0x0c15],+ ['NotoSansKannada', 0x0c95],+ ['NotoSansMalayalam', 0x0d15],+ ['NotoSansThai', 0x0e01],+ ['NotoSansLao', 0x0e81],+ ['NotoSansKhmer', 0x1780],+ ['NotoSansMyanmar', 0x1000],+ ['NotoSansArmenian', 0x0531],+ ['NotoSansGeorgian', 0x10d0],+ ['NotoSansEthiopic', 0x1200],]--/** The scripts the fallback faces are there for, each with the faces that draw it. */-const SCRIPT_FACES: ReadonlyArray<readonly [script: string, prefixes: readonly string[]]> = [- ['Arabic', SCRIPT_FALLBACK_PREFIXES.filter(prefix => prefix.includes('Arabic'))],- ['Hebrew', SCRIPT_FALLBACK_PREFIXES.filter(prefix => prefix.includes('Hebrew'))],- ...MORE_SCRIPT_PREFIXES.map(prefix => [prefix.slice('NotoSans'.length), [prefix]] as const),+const MORE_SCRIPT_PREFIXES = MORE_SCRIPTS.map(([prefix]) => prefix)++/** The scripts the fallback faces are there for: a letter of each, and the faces named for it. */+const SCRIPT_FACES: ReadonlyArray<+ readonly [script: string, letter: number, prefixes: readonly string[]]+> = [+ ['Arabic', 0x0627, SCRIPT_FALLBACK_PREFIXES.filter(prefix => prefix.includes('Arabic'))],+ ['Hebrew', 0x05d0, SCRIPT_FALLBACK_PREFIXES.filter(prefix => prefix.includes('Hebrew'))],+ ...MORE_SCRIPTS.map(+ ([prefix, letter]) => [prefix.slice('NotoSans'.length), letter, [prefix]] as const+ ),]+/** A Han character, drawn by the CJK collections. */+const CJK_LETTER = 0x4e2d+/**- * The scripts no installed face draws, given which face prefixes resolve and- * whether a CJK collection is installed. A script with any face of its own is- * not missing, so an image without only the preferred one (Debian has no Noto- * Sans Arabic UI) is not reported.+ * The scripts no installed face draws. A script with a face named for it is+ * not missing, even without the preferred one (Debian has no Noto Sans Arabic+ * UI); one without is missing only when no other face draws its letter+ * (DejaVu Sans draws Armenian and Georgian). `draws` is asked only for those.*/-export function missingScripts(hasFace: (prefix: string) => boolean, hasCjk: boolean): string[] {- const missing = SCRIPT_FACES.filter(([, prefixes]) => !prefixes.some(hasFace)).map(- ([script]) => script- )- return hasCjk ? missing : ['CJK', ...missing]+export function missingScripts(+ hasFace: (prefix: string) => boolean,+ hasCjk: boolean,+ draws: (cp: number) => boolean+): string[] {+ const missing = SCRIPT_FACES.filter(+ ([, letter, prefixes]) => !prefixes.some(hasFace) && !draws(letter)+ ).map(([script]) => script)+ return hasCjk || draws(CJK_LETTER) ? missing : ['CJK', ...missing]}interface FallbackFamily {@@ -623,6 +639,8 @@ function embeds(faces: PdfFaces, texts: string | string[]): boolean {}}+const CJK_FALLBACK = 'Fallback-CJK'+function discoverFallbacks(exclude: Set<string>): FallbackFamily[] {const out: FallbackFamily[] = []const seen = new Set<string>()@@ -638,18 +656,10 @@ function discoverFallbacks(exclude: Set<string>): FallbackFamily[] {const cjkFile = cjk && faceFile(cjk.normal)if (cjk && cjkFile && !exclude.has(cjkFile)) {seen.add(cjkFile)- out.push({ family: 'Fallback-CJK', faces: cjk })+ out.push({ family: CJK_FALLBACK, faces: cjk })}FALLBACK_PREFIXES.forEach(add)MORE_SCRIPT_PREFIXES.forEach(add)- const missing = missingScripts(prefix => facesForPrefix(prefix) !== undefined, cjk !== undefined)- if (missing.length > 0) {- // Once per process: the faces are looked up once (glyphSource).- logger.warn(- { component: 'fonts', scripts: missing },- 'No installed face draws these scripts, so PDFs leave their characters out'- )- }return out}@@ -686,10 +696,26 @@ class GlyphSource implements PdfGlyphSource {if (file) exclude.add(file)}this.fallbacks = discoverFallbacks(exclude)+ this.reportMissingScripts(this.fallbacks)}return this.fallbacks}+ /** Logged once per process, since the faces are looked up once (glyphSource). */+ private reportMissingScripts(fallbacks: FallbackFamily[]): void {+ const missing = missingScripts(+ prefix => facesForPrefix(prefix) !== undefined,+ fallbacks.some(f => f.family === CJK_FALLBACK),+ cp => this.familyFor(cp, PDF_FONT_FAMILY) !== undefined+ )+ if (missing.length > 0) {+ logger.warn(+ { component: 'fonts', scripts: missing },+ 'No installed face draws these scripts, so PDFs leave their characters out'+ )+ }+ }+private infoOf(family: string): FaceInfo | undefined {if (!this.info.has(family)) {const faces = this.facesOf(family)@@ -765,8 +791,10 @@ class GlyphSource implements PdfGlyphSource {this.registered.add(alias)ensureFontsReady()try {- if (Buffer.isBuffer(face)) GlobalFonts.register(face, alias)- else GlobalFonts.registerFromPath(faceFile(face)!, alias)+ const key = Buffer.isBuffer(face)+ ? GlobalFonts.register(face, alias)+ : GlobalFonts.registerFromPath(faceFile(face)!, alias)+ if (!key) throw new Error('the renderer refused the face')} catch (err) {// Measured with the canvas default instead; only line breaking is approximate.logger.warn(