Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): give up a reference build that never ends, and retry with a pause
marcotebd52561a1b718
diff --git a/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts b/mcp-host/src/workflow/__tests__/officeBoilerplate.test.tsindex 751c68256..73f7bffd1 100644--- a/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts+++ b/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts@@ -1,7 +1,7 @@// The reference texts the delivered-package scan leaves to the payload check.import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'-const control = vi.hoisted(() => ({ failNextTempDir: false, hangDocx: false }))+const control = vi.hoisted(() => ({ failNextTempDir: false, hangDocx: false, docxBuilds: 0 }))vi.mock('fs', async original => {const actual = await original<typeof import('fs')>()@@ -21,8 +21,10 @@ vi.mock('../docxGenerator', async original => {const actual = await original<typeof import('../docxGenerator')>()return {...actual,- runGenerateDocx: (...args: Parameters<typeof actual.runGenerateDocx>) =>- control.hangDocx ? new Promise(() => {}) : actual.runGenerateDocx(...args),+ runGenerateDocx: (...args: Parameters<typeof actual.runGenerateDocx>) => {+ control.docxBuilds+++ return control.hangDocx ? new Promise(() => {}) : actual.runGenerateDocx(...args)+ },}})@@ -35,10 +37,12 @@ describe('boilerplateTexts', () => {beforeEach(() => {control.failNextTempDir = falsecontrol.hangDocx = false+ control.docxBuilds = 0})afterEach(() => {vi.useRealTimers()+ vi.restoreAllMocks()})it('holds no timestamp of the moment the reference was written', async () => {@@ -48,11 +52,16 @@ describe('boilerplateTexts', () => {expect(texts.some(text => /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(text))).toBe(false)})- it('tries a failed build again on the next call', async () => {- const { boilerplateTexts } = await freshModule()+ it('tries a failed build again once REFERENCE_RETRY_MS has passed, not on every call', async () => {+ const { REFERENCE_RETRY_MS, boilerplateTexts } = await freshModule()+ const now = vi.spyOn(Date, 'now').mockReturnValue(1_000_000)control.failNextTempDir = true- expect(await boilerplateTexts('xlsx')).toEqual([])- expect((await boilerplateTexts('xlsx')).length).toBeGreaterThan(0)+ expect(await boilerplateTexts('docx')).toEqual([])+ expect(await boilerplateTexts('docx')).toEqual([])+ expect(control.docxBuilds).toBe(0)+ now.mockReturnValue(1_000_000 + REFERENCE_RETRY_MS)+ expect((await boilerplateTexts('docx')).length).toBeGreaterThan(0)+ expect(control.docxBuilds).toBe(1)})it('does not wait past REFERENCE_WAIT_MS for a build that never ends', async () => {@@ -63,4 +72,18 @@ describe('boilerplateTexts', () => {await vi.advanceTimersByTimeAsync(REFERENCE_WAIT_MS)expect(await texts).toEqual([])})++ it('gives up a build that never ends, and builds again later', async () => {+ const { REFERENCE_BUILD_MS, REFERENCE_RETRY_MS, boilerplateTexts } = await freshModule()+ control.hangDocx = true+ vi.useFakeTimers()+ const first = boilerplateTexts('docx')+ await vi.advanceTimersByTimeAsync(REFERENCE_BUILD_MS)+ expect(await first).toEqual([])+ vi.useRealTimers()+ control.hangDocx = false+ vi.spyOn(Date, 'now').mockReturnValue(Date.now() + REFERENCE_BUILD_MS + REFERENCE_RETRY_MS)+ expect((await boilerplateTexts('docx')).length).toBeGreaterThan(0)+ expect(control.docxBuilds).toBe(2)+ })})diff --git a/mcp-host/src/workflow/officeBoilerplate.ts b/mcp-host/src/workflow/officeBoilerplate.tsindex 120b289f0..2eacca24e 100644--- a/mcp-host/src/workflow/officeBoilerplate.ts+++ b/mcp-host/src/workflow/officeBoilerplate.ts@@ -89,29 +89,45 @@ const SAMPLES: Record</** How long a call waits for the reference before checking without it. */export const REFERENCE_WAIT_MS = 10_000+/** How long a build of the reference may run before it is given up. */+export const REFERENCE_BUILD_MS = 60_000++/** How long after a failed or given-up build the next one waits. */+export const REFERENCE_RETRY_MS = 60_000+const references = new Map<OfficeFormat, Promise<string[]>>()+const failedAt = new Map<OfficeFormat, number>()/*** The texts a package of `format` holds on its own. Empty when the sample* cannot be written, or is not ready within REFERENCE_WAIT_MS, which leaves- * the scan as strict as before. A failed build is tried again on the next- * call.+ * the scan as strict as before. A build that fails, or runs past+ * REFERENCE_BUILD_MS, is tried again once REFERENCE_RETRY_MS has passed.*/export function boilerplateTexts(format: OfficeFormat): Promise<string[]> {let reference = references.get(format)if (!reference) {- reference = writeSample(format).catch((err: unknown) => {- references.delete(format)- logger.warn(- {- component: 'OfficeBoilerplate',- format,- error: err instanceof Error ? err.name : typeof err,- },- 'Could not build the reference package; protected values are checked against all its text'- )- return []- })+ const failed = failedAt.get(format)+ if (failed !== undefined && Date.now() - failed < REFERENCE_RETRY_MS) return Promise.resolve([])+ reference = withinBuildLimit(writeSample(format)).then(+ texts => {+ failedAt.delete(format)+ return texts+ },+ (err: unknown) => {+ references.delete(format)+ failedAt.set(format, Date.now())+ logger.warn(+ {+ component: 'OfficeBoilerplate',+ format,+ error: err instanceof Error ? err.name : typeof err,+ },+ 'Could not build the reference package; protected values are checked against all its text'+ )+ return []+ }+ )references.set(format, reference)}let timer: ReturnType<typeof setTimeout> | undefined@@ -128,6 +144,19 @@ export function boilerplateTexts(format: OfficeFormat): Promise<string[]> {return Promise.race([reference, waited]).finally(() => clearTimeout(timer))}+/** `build`, rejected once it has run REFERENCE_BUILD_MS. */+function withinBuildLimit<T>(build: Promise<T>): Promise<T> {+ let timer: ReturnType<typeof setTimeout> | undefined+ const limit = new Promise<never>((_, reject) => {+ timer = setTimeout(+ () => reject(new Error('the reference build ran too long')),+ REFERENCE_BUILD_MS+ )+ timer.unref?.()+ })+ return Promise.race([build, limit]).finally(() => clearTimeout(timer))+}+/** When a package was written, which its document properties hold. */const WRITE_TIME = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z/g