Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): try the bundled fonts again after a failed load (SILENT-002)
marcoteb43e7183d6565
diff --git a/mcp-host/src/workflow/__tests__/fonts.ready.test.ts b/mcp-host/src/workflow/__tests__/fonts.ready.test.tsindex 54f2611c8..f319a32e6 100644--- a/mcp-host/src/workflow/__tests__/fonts.ready.test.ts+++ b/mcp-host/src/workflow/__tests__/fonts.ready.test.ts@@ -7,6 +7,7 @@ import { GlobalFonts } from '@napi-rs/canvas'afterEach(() => {vi.restoreAllMocks()+ vi.useRealTimers()})/** A letter of each script the PDF warns about. */@@ -131,3 +132,95 @@ describe('ensureFontsReady', () => {}})})++describe('a failed bundled-font load', () => {+ const MINUTE = 60_000++ async function freshFonts() {+ vi.resetModules()+ vi.useFakeTimers({ toFake: ['Date'] })+ vi.setSystemTime(new Date('2026-10-16T10:00:00Z'))+ const { logger } = await import('../../logger')+ const error = vi.spyOn(logger, 'error').mockImplementation(() => undefined)+ const info = vi.spyOn(logger, 'info').mockImplementation(() => undefined)+ const system = vi.spyOn(GlobalFonts, 'registerFromPath')+ return { error, info, system, fonts: await import('../fonts') }+ }++ const later = (ms: number) => vi.setSystemTime(Date.now() + ms)++ it('loads the bundled faces once when nothing fails', async () => {+ const register = vi.spyOn(GlobalFonts, 'register')+ const { error, fonts } = await freshFonts()++ fonts.ensureFontsReady()+ later(60 * MINUTE)+ fonts.ensureFontsReady()+ expect(register).toHaveBeenCalledTimes(2)+ expect(error).not.toHaveBeenCalled()+ })++ it('tries again after a passing failure, registers only the missing face, and the system faces once', async () => {+ const register = vi.spyOn(GlobalFonts, 'register').mockImplementationOnce(() => null)+ const { error, info, system, fonts } = await freshFonts()++ fonts.ensureFontsReady()+ expect(error).toHaveBeenCalledWith(+ { err: 'the renderer refused the regular face', attempts: 1, retryInMs: 30_000 },+ 'Bundled Roboto faces could not be loaded'+ )+ const systemCalls = system.mock.calls.length+ expect(register).toHaveBeenCalledTimes(2)++ // Not again before the delay.+ later(10_000)+ fonts.ensureFontsReady()+ expect(register).toHaveBeenCalledTimes(2)++ later(20_000)+ fonts.ensureFontsReady()+ // Only the regular face, which was refused; the bold one is already in.+ expect(register).toHaveBeenCalledTimes(3)+ expect(register.mock.calls[2][1]).toBe(fonts.CHART_FONT_FAMILY)+ expect(info).toHaveBeenCalledWith(+ { attempts: 2 },+ 'Bundled Roboto faces loaded after a failed try'+ )++ later(60 * MINUTE)+ fonts.ensureFontsReady()+ expect(register).toHaveBeenCalledTimes(3)+ expect(error).toHaveBeenCalledTimes(1)+ expect(system.mock.calls.length).toBe(systemCalls)+ })++ it('stops trying after a persistent failure and keeps drawing with the system faces', async () => {+ const register = vi.spyOn(GlobalFonts, 'register').mockImplementation(() => {+ throw new Error('unreadable face')+ })+ const { error, system, fonts } = await freshFonts()++ fonts.ensureFontsReady()+ const systemCalls = system.mock.calls.length+ expect(systemCalls).toBeGreaterThan(0)+ for (const wait of [30_000, 5 * MINUTE, 30 * MINUTE]) {+ later(wait)+ fonts.ensureFontsReady()+ }+ expect(register).toHaveBeenCalledTimes(4)+ expect(error.mock.calls.map(([fields]) => fields)).toEqual([+ { err: 'unreadable face', attempts: 1, retryInMs: 30_000 },+ { err: 'unreadable face', attempts: 2, retryInMs: 5 * MINUTE },+ { err: 'unreadable face', attempts: 3, retryInMs: 30 * MINUTE },+ { err: 'unreadable face', attempts: 4 },+ ])+ expect(error.mock.calls[3][1]).toBe(+ 'Bundled Roboto faces could not be loaded; system faces are used from now on'+ )++ later(24 * 60 * MINUTE)+ fonts.ensureFontsReady()+ expect(register).toHaveBeenCalledTimes(4)+ expect(system.mock.calls.length).toBe(systemCalls)+ })+})diff --git a/mcp-host/src/workflow/fonts.ts b/mcp-host/src/workflow/fonts.tsindex 53538f3fe..4ca5ce571 100644--- a/mcp-host/src/workflow/fonts.ts+++ b/mcp-host/src/workflow/fonts.ts@@ -72,7 +72,23 @@ const SYSTEM_FONT_DIRS = [const MISSING_PROBE = '\uE000'let faces: RobotoFaces | undefined-let ready = false++/**+ * When the bundled faces are tried again after a failed load. A failure can+ * pass (a read under memory or descriptor pressure), but a damaged package+ * fails every time, so the tries are few and spaced; after the last one the+ * system faces are used for the life of the process.+ */+const BUNDLED_RETRY_DELAYS_MS = [30_000, 5 * 60_000, 30 * 60_000]++const bundled = {+ /** Faces registered so far, so a retry registers only the ones still missing. */+ registered: new Set<string>(),+ failures: 0,+ nextAttemptAt: 0,+ settled: false,+}+let systemFontsRegistered = falsefunction loadRobotoFaces(): RobotoFaces {if (faces) return faces@@ -126,27 +142,59 @@ function registerSystemFonts(): void {}/**- * Register the bundled faces (and any system faces) exactly once. Safe to call- * from every render path; later calls are free.+ * Register the bundled faces and the system faces. Safe to call from every+ * render path: once the bundled faces are in, or their retries are spent,+ * later calls are free.*/export function ensureFontsReady(): void {- if (ready) return- ready = true+ if (!bundled.settled && Date.now() >= bundled.nextAttemptAt) loadBundledFaces()+ if (!systemFontsRegistered) {+ systemFontsRegistered = true+ // Also the fallback: charts draw in these while the bundled faces are missing.+ registerSystemFonts()+ }+}++function loadBundledFaces(): void {try {const f = loadRobotoFaces()- // register answers null, without throwing, for a face it cannot use.- const refused = [- GlobalFonts.register(f.normal, CHART_FONT_FAMILY) ? undefined : 'regular',- GlobalFonts.register(f.bold, `${CHART_FONT_FAMILY} Bold`) ? undefined : 'bold',- ].filter(Boolean)+ const refused: string[] = []+ for (const [label, data, alias] of [+ ['regular', f.normal, CHART_FONT_FAMILY],+ ['bold', f.bold, `${CHART_FONT_FAMILY} Bold`],+ ] as const) {+ if (bundled.registered.has(label)) continue+ // register answers null, without throwing, for a face it cannot use.+ if (GlobalFonts.register(data, alias)) bundled.registered.add(label)+ else refused.push(label)+ }if (refused.length > 0) {throw new Error(`the renderer refused the ${refused.join(' and ')} face`)}+ bundled.settled = true+ if (bundled.failures > 0) {+ logger.info(+ { attempts: bundled.failures + 1 },+ 'Bundled Roboto faces loaded after a failed try'+ )+ }} catch (err) {- // The system faces still register, so charts draw in them rather than in nothing.- logger.error({ err: errorText(err) }, 'Bundled Roboto faces could not be loaded')+ const delay = BUNDLED_RETRY_DELAYS_MS[bundled.failures]+ bundled.failures+++ if (delay === undefined) {+ bundled.settled = true+ logger.error(+ { err: errorText(err), attempts: bundled.failures },+ 'Bundled Roboto faces could not be loaded; system faces are used from now on'+ )+ } else {+ bundled.nextAttemptAt = Date.now() + delay+ logger.error(+ { err: errorText(err), attempts: bundled.failures, retryInMs: delay },+ 'Bundled Roboto faces could not be loaded'+ )+ }}- registerSystemFonts()}function errorText(err: unknown): string {