Skip to content

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

TypeScript

fix(mcp-host): start no reference build beside one that has not stopped, and wait only for the first

marcoteb5f82c40935a6
diff --git a/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts b/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts
index 73f7bffd1..bd2a49b84 100644
--- a/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts
+++ b/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts
@@ -1,7 +1,13 @@
// 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, docxBuilds: 0 }))
+const control = vi.hoisted(() => ({
+ failNextTempDir: false,
+ hangDocx: false,
+ docxBuilds: 0,
+ /** Ends the build that hangs, as a failure. */
+ release: undefined as (() => void) | undefined,
+}))
vi.mock('fs', async original => {
const actual = await original<typeof import('fs')>()
@@ -23,11 +29,24 @@ vi.mock('../docxGenerator', async original => {
...actual,
runGenerateDocx: (...args: Parameters<typeof actual.runGenerateDocx>) => {
control.docxBuilds++
- return control.hangDocx ? new Promise(() => {}) : actual.runGenerateDocx(...args)
+ if (!control.hangDocx) return actual.runGenerateDocx(...args)
+ return new Promise<Awaited<ReturnType<typeof actual.runGenerateDocx>>>(resolve => {
+ control.release = () => resolve({ success: false, error: 'released' })
+ })
},
}
})
+/** The docx reference, once a build in the background has written it. */
+async function builtTexts(texts: (format: 'docx') => Promise<string[]>): Promise<string[]> {
+ for (let tries = 0; tries < 200; tries++) {
+ const found = await texts('docx')
+ if (found.length > 0) return found
+ await new Promise(resolve => setTimeout(resolve, 50))
+ }
+ return []
+}
+
async function freshModule() {
vi.resetModules()
return import('../officeBoilerplate')
@@ -38,6 +57,7 @@ describe('boilerplateTexts', () => {
control.failNextTempDir = false
control.hangDocx = false
control.docxBuilds = 0
+ control.release = undefined
})
afterEach(() => {
@@ -60,10 +80,30 @@ describe('boilerplateTexts', () => {
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)
+ // The call that starts the retry checks without it; a later one has it.
+ expect(await boilerplateTexts('docx')).toEqual([])
+ expect(await builtTexts(boilerplateTexts)).not.toEqual([])
expect(control.docxBuilds).toBe(1)
})
+ it('does not make a call wait for a build that retries a failed one', async () => {
+ const { REFERENCE_RETRY_MS, boilerplateTexts } = await freshModule()
+ const now = vi.spyOn(Date, 'now').mockReturnValue(1_000_000)
+ control.failNextTempDir = true
+ expect(await boilerplateTexts('docx')).toEqual([])
+ now.mockReturnValue(1_000_000 + REFERENCE_RETRY_MS)
+ control.hangDocx = true
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
+ let settled = false
+ const texts = boilerplateTexts('docx').then(found => {
+ settled = true
+ return found
+ })
+ await vi.advanceTimersByTimeAsync(0)
+ expect(settled).toBe(true)
+ expect(await texts).toEqual([])
+ })
+
it('does not wait past REFERENCE_WAIT_MS for a build that never ends', async () => {
const { REFERENCE_WAIT_MS, boilerplateTexts } = await freshModule()
control.hangDocx = true
@@ -73,7 +113,7 @@ describe('boilerplateTexts', () => {
expect(await texts).toEqual([])
})
- it('gives up a build that never ends, and builds again later', async () => {
+ it('gives up a build that never ends, and builds again only once it has stopped', async () => {
const { REFERENCE_BUILD_MS, REFERENCE_RETRY_MS, boilerplateTexts } = await freshModule()
control.hangDocx = true
vi.useFakeTimers()
@@ -83,7 +123,11 @@ describe('boilerplateTexts', () => {
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)
+ // The given-up build still runs, so no second one starts beside it.
+ expect(await boilerplateTexts('docx')).toEqual([])
+ expect(control.docxBuilds).toBe(1)
+ control.release!()
+ expect(await builtTexts(boilerplateTexts)).not.toEqual([])
expect(control.docxBuilds).toBe(2)
})
})
diff --git a/mcp-host/src/workflow/officeBoilerplate.ts b/mcp-host/src/workflow/officeBoilerplate.ts
index 2eacca24e..cc9a5e74e 100644
--- a/mcp-host/src/workflow/officeBoilerplate.ts
+++ b/mcp-host/src/workflow/officeBoilerplate.ts
@@ -97,19 +97,27 @@ export const REFERENCE_RETRY_MS = 60_000
const references = new Map<OfficeFormat, Promise<string[]>>()
const failedAt = new Map<OfficeFormat, number>()
+/** Formats whose sample is being written, including a build given up that has not stopped. */
+const writing = new Set<OfficeFormat>()
/**
* 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 build that fails, or runs past
- * REFERENCE_BUILD_MS, is tried again once REFERENCE_RETRY_MS has passed.
+ * REFERENCE_BUILD_MS, is tried again once REFERENCE_RETRY_MS has passed and
+ * the given-up build has stopped, so builds that hang do not pile up. Only the
+ * first build makes a call wait: once one has failed, a call checks without
+ * the reference until a retry has built it.
*/
export function boilerplateTexts(format: OfficeFormat): Promise<string[]> {
let reference = references.get(format)
if (!reference) {
const failed = failedAt.get(format)
if (failed !== undefined && Date.now() - failed < REFERENCE_RETRY_MS) return Promise.resolve([])
- reference = withinBuildLimit(writeSample(format)).then(
+ if (writing.has(format)) return Promise.resolve([])
+ writing.add(format)
+ const build = writeSample(format).finally(() => writing.delete(format))
+ reference = withinBuildLimit(build).then(
texts => {
failedAt.delete(format)
return texts
@@ -130,6 +138,7 @@ export function boilerplateTexts(format: OfficeFormat): Promise<string[]> {
)
references.set(format, reference)
}
+ if (failedAt.has(format)) return Promise.resolve([])
let timer: ReturnType<typeof setTimeout> | undefined
const waited = new Promise<string[]>(resolve => {
timer = setTimeout(() => {