Skip to content

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

TypeScript

fix(mcp-host): close the gaps a review found in the delivered-file check

marcotebfab953a5a849
diff --git a/mcp-host/src/__tests__/artifacts/officePackageText.test.ts b/mcp-host/src/__tests__/artifacts/officePackageText.test.ts
index c204bda69..7377749b0 100644
--- a/mcp-host/src/__tests__/artifacts/officePackageText.test.ts
+++ b/mcp-host/src/__tests__/artifacts/officePackageText.test.ts
@@ -202,6 +202,7 @@ describe('officePackageHasText, numbers as they are shown', () => {
],
['a built-in id it does not model', () => workbook('5', { id: 5 })],
['a system date format', () => workbook('46024', { id: 164, code: '[$-F800]dddd' })],
+ ['another calendar', () => workbook('46024', { id: 164, code: '[$-1070000]yyyy-mm-dd' })],
[
'an ISO date cell',
() => pack({ 'xl/worksheets/sheet1.xml': '<c r="A1" t="d"><v>2026-01-02</v></c>' }),
@@ -230,6 +231,17 @@ describe('officePackageHasText, numbers as they are shown', () => {
).toBe(false)
})
+ it('counts each rendering against the bound, not each number', async () => {
+ // One cell under the built-in short date and time is rendered in 84 patterns.
+ const dated = await workbook('46024.5', { id: 22 })
+ const bound = { ...OFFICE_PACKAGE_LIMITS, maxShownRenders: 83 }
+ expect(officePackageHasText(dated, () => false, bound)).toBeNull()
+ expect(officePackageHasText(dated, () => false, { ...bound, maxShownRenders: 84 })).toBe(false)
+ // A plain grouping format is one rendering.
+ const grouped = await workbook('3141592653', { id: 3 })
+ expect(officePackageHasText(grouped, () => false, { ...bound, maxShownRenders: 1 })).toBe(false)
+ })
+
it('reads no numbers when told not to', async () => {
const xlsx = await workbook('46024', { id: 164, code: '[$-F800]dddd' })
expect(officePackageHasText(xlsx, () => false, OFFICE_PACKAGE_LIMITS, null)).toBe(false)
diff --git a/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts b/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts
index 4e95491a6..bb753b061 100644
--- a/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts
+++ b/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import {
builtinFormatCode,
isModelledFormatCode,
+ renderCount,
shownForms,
withoutSeparators,
} from '../../artifacts/shownNumbers'
@@ -33,6 +34,17 @@ describe('isModelledFormatCode', () => {
expect(isModelledFormatCode(code)).toBe(true)
})
+ it.each(['[$-409]yyyy-mm-dd', '[$€-407]#,##0', '[$-de-DE]dd.mm.yyyy', '[$USD-409]0', '[$€]0'])(
+ 'reads %s, a currency or a plain locale',
+ code => {
+ expect(isModelledFormatCode(code)).toBe(true)
+ }
+ )
+
+ it('reads built-in 47 as Excel shows it', () => {
+ expect(builtinFormatCode(47)).toBe('mm:ss.0')
+ })
+
it('reads every built-in format a package can name by id', () => {
const ids = [
0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 37, 38, 39, 40, 45, 46,
@@ -47,6 +59,12 @@ describe('isModelledFormatCode', () => {
['an unterminated quote', '"x'],
['a system long date', '[$-F800]dddd, mmmm dd, yyyy'],
['a system date', '[$-x-sysdate]dddd'],
+ ['a system date by a padded id', '[$-0F800]dddd'],
+ ['the Thai Buddhist calendar by id', '[$-1070000]yyyy-mm-dd'],
+ ['the Thai Buddhist calendar by tag', '[$-th-TH,107]yyyy-mm-dd'],
+ ['native digits by id', '[$-D000409]0'],
+ ['Japanese era years', '[$-ja-JP-x-gannen]ggge'],
+ ['a numbering extension', '[$-th-TH-u-nu-thai]0'],
['native digits', '[DBNum1]0'],
['another calendar', '[B2]yyyy'],
['a code over 255 characters', `"${'x'.repeat(260)}"0`],
@@ -81,6 +99,17 @@ describe('shownForms', () => {
})
})
+describe('renderCount', () => {
+ it.each([
+ ['#,##0', false, 1],
+ ['mmm d, yyyy', false, 4],
+ ['mm-dd-yy', true, 14],
+ ['m/d/yy h:mm', true, 84],
+ ])('renders %s (short date %s) %i times', (code, shortDate, count) => {
+ expect(renderCount(code, shortDate)).toBe(count)
+ })
+})
+
describe('withoutSeparators', () => {
it.each([
['1,234.5', '12345'],
diff --git a/mcp-host/src/artifacts/officePackageText.ts b/mcp-host/src/artifacts/officePackageText.ts
index c14733067..bcb91a346 100644
--- a/mcp-host/src/artifacts/officePackageText.ts
+++ b/mcp-host/src/artifacts/officePackageText.ts
@@ -12,6 +12,7 @@ import {
builtinFormatCode,
isModelledFormatCode,
isShortDateId,
+ renderCount,
shownForms,
showsIntegerAsStored,
} from './shownNumbers'
@@ -26,10 +27,14 @@ export const OFFICE_PACKAGE_LIMITS = {
maxDepth: 2,
/**
* Numbers rendered through their format across the package and every
- * package inside it (about 0.6 s at this bound); a larger package is not
- * attached.
+ * package inside it; a larger package is not attached.
*/
maxShownValues: 250_000,
+ /**
+ * Renderings those numbers take (shownNumbers.renderCount): a built-in
+ * short date is rendered in dozens of patterns. About 0.6 s at this bound.
+ */
+ maxShownRenders: 500_000,
}
export type OfficePackageLimits = typeof OFFICE_PACKAGE_LIMITS
@@ -78,7 +83,7 @@ export function officePackageHasText(
buffer,
matches,
limits,
- { inflated: 0, shownValues: 0 },
+ { inflated: 0, shownValues: 0, shownRenders: 0 },
0,
matchesShown ?? undefined
)
@@ -101,7 +106,7 @@ export function officePackageTexts(
return false
},
{ ...limits, maxShownValues: 0 },
- { inflated: 0, shownValues: 0 },
+ { inflated: 0, shownValues: 0, shownRenders: 0 },
0,
undefined
)
@@ -111,6 +116,7 @@ export function officePackageTexts(
interface ScanBudget {
inflated: number
shownValues: number
+ shownRenders: number
}
function searchPackage(
@@ -275,6 +281,8 @@ function numbersShow(
const render = (value: number, code: string, shortDate: boolean): boolean | null => {
if (!shortDate && showsIntegerAsStored(code, value)) return false
if (++budget.shownValues > limits.maxShownValues) return null
+ budget.shownRenders += renderCount(code, shortDate)
+ if (budget.shownRenders > limits.maxShownRenders) return null
return shownForms(value, code, shortDate).some(matches)
}
const sheets = [...parts.keys()].filter(name => SHEET_PART.test(name))
diff --git a/mcp-host/src/artifacts/shownNumbers.ts b/mcp-host/src/artifacts/shownNumbers.ts
index ef0852606..a00dd8022 100644
--- a/mcp-host/src/artifacts/shownNumbers.ts
+++ b/mcp-host/src/artifacts/shownNumbers.ts
@@ -3,9 +3,9 @@
* protected value it displays is found although the file only stores the
* number: a date serial under a date format, digits under a grouping format or
* beside quoted text. A spreadsheet renders in its viewer's locale, so a value
- * is compared without separators (any locale's grouping reads the same), in
- * each language's month and day names, and, for the built-in short date, in
- * the date patterns locales commonly use.
+ * is compared without separators (any locale's grouping reads the same), with
+ * month and day names in English, German, French and Spanish, and, for the
+ * built-in short date, in the date patterns locales commonly use.
*/
import type * as Numfmt from 'numfmt'
@@ -15,7 +15,6 @@ export const MAX_FORMAT_CODE_LENGTH = 255
let formatter: typeof Numfmt | undefined
function numfmt(): typeof Numfmt {
// Loaded on first use: only a scan with protected values configured needs it.
- // eslint-disable-next-line @typescript-eslint/no-require-imports
formatter ??= require('numfmt') as typeof Numfmt
return formatter
}
@@ -51,7 +50,8 @@ const BUILTIN_FORMATS: Readonly<Record<number, string>> = {
40: '#,##0.00;[Red](#,##0.00)',
45: 'mm:ss',
46: '[h]:mm:ss',
- 47: 'mmss.0',
+ // As Excel shows it; ExcelJS's own table spells it 'mmss.0'.
+ 47: 'mm:ss.0',
48: '##0.0E+0',
49: '@',
}
@@ -85,8 +85,22 @@ export function isShortDateId(id: number): boolean {
return SHORT_DATE_IDS.has(id)
}
-/** A locale tag that makes a viewer use its system format: [$-F800], [$-x-sysdate]. */
-const SYSTEM_LOCALE = /^-(?:F[0-9A-F]{3}|x-sys)/i
+/**
+ * Whether a locale tag (the part of [$...] after '$') leaves numbers and dates
+ * as this module renders them: a currency with no locale, a plain locale id
+ * ([$-409], [$€-407]) or a plain language tag ([$-de-DE]). An id with
+ * calendar or digit bytes ([$-1070000], [$-D000409]), a system format
+ * ([$-F800], [$-x-sysdate]), a calendar suffix ([$-th-TH,107]) or a tag
+ * extension ([$-ja-JP-x-gannen], [$-th-TH-u-nu-thai]) is shown otherwise.
+ */
+function isModelledLocale(value: string): boolean {
+ const dash = value.indexOf('-')
+ if (dash < 0) return true
+ const tag = value.slice(dash + 1)
+ // Ids from 0xF000 are system formats; bytes above 0xFFFF pick a calendar or digits.
+ if (/^[0-9A-F]{1,8}$/i.test(tag)) return parseInt(tag, 16) < 0xf000
+ return /^[a-z]{2,3}(?:-[a-z0-9]{2,8})*$/i.test(tag) && !/-[a-z](?:-|$)/i.test(tag)
+}
/**
* Whether this module renders `code` the way a spreadsheet would: a code it
@@ -103,7 +117,7 @@ export function isModelledFormatCode(code: string): boolean {
token =>
token.type !== 'dbnum' &&
token.type !== 'natnum' &&
- !(token.type === 'locale' && SYSTEM_LOCALE.test(String(token.value)))
+ !(token.type === 'locale' && !isModelledLocale(String(token.value)))
)
}
@@ -133,7 +147,39 @@ export function withoutSeparators(text: string): string {
return text.replace(SEPARATORS, '')
}
+/** Month and day names, and AM/PM, are words a locale translates. */
+const NAMED = /m{3,}|d{3,}|AM\/PM|A\/P/i
+
+/** The codes shownForms renders a value under. */
+function renderedCodes(code: string, shortDate: boolean): readonly string[] {
+ if (!shortDate) return [code]
+ return code.includes('h')
+ ? SHORT_DATES.flatMap(date => SHORT_TIMES.map(time => `${date} ${time}`))
+ : SHORT_DATES
+}
+
const MEMO_LIMIT = 20_000
+const renderCounts = new Map<string, number>()
+
+/**
+ * How many renderings shownForms makes for one value under `code`, which is
+ * what a scan's time budget counts: a built-in short date is rendered in
+ * every common pattern, a pattern with names in every name locale.
+ */
+export function renderCount(code: string, shortDate = false): number {
+ const key = `${shortDate ? 'S' : 'C'}\u0000${code}`
+ let count = renderCounts.get(key)
+ if (count === undefined) {
+ count = renderedCodes(code, shortDate).reduce(
+ (n, each) => n + (NAMED.test(each) ? 1 + NAME_LOCALES.length : 1),
+ 0
+ )
+ if (renderCounts.size >= MEMO_LIMIT) renderCounts.clear()
+ renderCounts.set(key, count)
+ }
+ return count
+}
+
const memo = new Map<string, string[]>()
/**
@@ -147,15 +193,9 @@ export function shownForms(value: number, code: string, shortDate = false): stri
if (known) return known
const nf = numfmt()
const forms = new Set<string>()
- const codes = shortDate
- ? code.includes('h')
- ? SHORT_DATES.flatMap(date => SHORT_TIMES.map(time => `${date} ${time}`))
- : SHORT_DATES
- : [code]
- for (const each of codes) {
+ for (const each of renderedCodes(code, shortDate)) {
forms.add(withoutSeparators(nf.format(each, value, { locale: 'en', throws: false })))
- // Month and day names, and AM/PM, are words a locale translates.
- if (/m{3,}|d{3,}|AM\/PM|A\/P/i.test(each)) {
+ if (NAMED.test(each)) {
for (const locale of NAME_LOCALES) {
forms.add(withoutSeparators(nf.format(each, value, { locale, throws: false })))
}
diff --git a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
index 1b9327518..5846bc302 100644
--- a/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
+++ b/mcp-host/src/core/tools/__tests__/generatedArtifactAttachments.test.ts
@@ -4,6 +4,7 @@ import JSZip from 'jszip'
import * as os from 'os'
import * as path from 'path'
import { zipEntries } from '../../../workflow/__tests__/support/zipEntries'
+import { INTERNAL_TOOLS } from '../../../workflow/internalTools'
import type { ArtifactMetadata } from '../../../workflow/types'
import { CALLER_OUTPUTS_DIR } from '../../../workspace/callerOutputs'
import type { NativeToolConfig } from '../../interfaces'
@@ -749,6 +750,88 @@ describe('generated internal artifact attachments', () => {
expect(ordinary.attachments ?? []).toHaveLength(1)
})
+ it.each([
+ ['without grouping', '3141592653'],
+ ['grouped the German way', '3.141.592.653'],
+ ])(
+ 'does not attach a chart image whose value label shows the protected value %s',
+ async (_label, secret) => {
+ const result = await toolWith('clerum__generate_chart', [secret]).execute({
+ filename: 'g.png',
+ type: 'bar',
+ showValues: true,
+ valueFormat: 'plain',
+ decimals: 0,
+ // Drawn "3,141,592,653", rounded: the arguments never spell it.
+ data: { labels: ['A'], datasets: [{ label: 'S', data: [3141592652.6] }] },
+ })
+ expect(result.is_error).toBe(false)
+ expect(result.attachments ?? []).toHaveLength(0)
+ }
+ )
+
+ it('attaches a chart image whose separate labels only spell a protected value run together', async () => {
+ const result = await toolWith('clerum__generate_chart', ['3405', '1203']).execute({
+ filename: 'line.png',
+ type: 'line',
+ showValues: true,
+ data: {
+ labels: ['a', 'b', 'c', 'd'],
+ datasets: [{ label: 'S', data: [120, 340, 560, 410] }],
+ },
+ })
+ expect(result.is_error).toBe(false)
+ expect(result.attachments ?? []).toHaveLength(1)
+ })
+
+ it('does not attach markdown that prints a protected value whole once and split once', async () => {
+ const markdown = toolWith('clerum__generate_markdown', ['probe-redaction-value'])
+ const leaked = await markdown.execute({
+ filename: 'twice.md',
+ content: 'a: probe-redaction-value\n\nb: probe-**redaction**-value',
+ })
+ expect(leaked.is_error).toBe(false)
+ expect(leaked.attachments ?? []).toHaveLength(0)
+ // Control: the whole occurrence alone is redacted and attached.
+ const whole = await markdown.execute({
+ filename: 'once.md',
+ content: 'a: probe-redaction-value',
+ })
+ expect(whole.attachments ?? []).toHaveLength(1)
+ expect(decoded(whole.attachments![0]!).toString('utf8')).toBe('a: [REDACTED:PROBE_0]')
+ })
+
+ it("does not attach a sheet that shows a protected value which is also the generator's own text", async () => {
+ // "1000" is in every workbook's styles; a cell of 999.6 shown as an integer reads "1,000".
+ const leaked = await toolWith('clerum__generate_xlsx', ['1000']).execute(
+ sheet(999.6, 'integer')
+ )
+ expect(leaked.is_error).toBe(false)
+ expect(leaked.attachments ?? []).toHaveLength(0)
+ })
+
+ it('delivers only the bytes the generator wrote', async () => {
+ const tool = INTERNAL_TOOLS.find(t => t.name === 'clerum__generate_markdown')!
+ const original = tool.execute.bind(tool)
+ // Another call writes the same name between this call's write and its delivery.
+ vi.spyOn(tool, 'execute').mockImplementationOnce(async (args, dir, options) => {
+ const result = await original(args, dir, options)
+ fs.writeFileSync(result.artifact!.path, '# someone else\n')
+ return result
+ })
+ const raced = await toolWith('clerum__generate_markdown', []).execute({
+ filename: 'race.md',
+ content: '# mine\n',
+ })
+ expect(raced.is_error).toBe(false)
+ expect(raced.attachments ?? []).toHaveLength(0)
+ const calm = await toolWith('clerum__generate_markdown', []).execute({
+ filename: 'race.md',
+ content: '# mine\n',
+ })
+ expect(calm.attachments ?? []).toHaveLength(1)
+ })
+
it("attaches ordinary documents when a protected value is also the generators' own text", async () => {
const common = ['Office', '1000', 'Calibri', 'General', 'main', 'false', 'solid']
const calls: Array<[string, Record<string, unknown>]> = [
diff --git a/mcp-host/src/core/tools/generatedArtifactAttachments.ts b/mcp-host/src/core/tools/generatedArtifactAttachments.ts
index 5543cf36b..d7a636d03 100644
--- a/mcp-host/src/core/tools/generatedArtifactAttachments.ts
+++ b/mcp-host/src/core/tools/generatedArtifactAttachments.ts
@@ -1,3 +1,4 @@
+import { createHash } from 'crypto'
import * as fs from 'fs'
import {
isBinaryArtifactFormat,
@@ -13,6 +14,7 @@ import {
isSafeArtifactFilename,
openExistingArtifactFile,
} from '../../workflow/artifactPaths'
+import { printedForms } from '../../workflow/toolText'
import type { ArtifactMetadata } from '../../workflow/types'
import type { Attachment } from '../types'
@@ -65,6 +67,16 @@ type BuildGeneratedArtifactAttachmentParams = {
* package scan cannot tell it from the generator's own text.
*/
packageBoilerplate?: readonly string[]
+ /**
+ * What the file shows that its arguments do not spell (a chart's drawn
+ * labels), each string as drawn; compared without separators.
+ */
+ shownText?: readonly string[]
+ /**
+ * The SHA-256 of the bytes the generator wrote. Other bytes found under the
+ * name (another call wrote it since) are not delivered.
+ */
+ writtenSha256?: string
}
function extension(filename: string | undefined): string {
@@ -126,6 +138,25 @@ function textContainsRedactableSecretEntry(
})
}
+/**
+ * Whether a string the file shows holds an entry, both without separators: a
+ * label drawn "3,141,592,653" shows "3141592653" and "3.141.592.653" alike.
+ */
+function shownTextHoldsSecretEntry(
+ shown: readonly string[] | undefined,
+ entries: ArtifactSecretEntry[]
+): boolean {
+ if (!shown?.length) return false
+ const values = entries
+ .map(entry => withoutSeparators(entry.value))
+ .filter(value => value.length >= 4)
+ if (values.length === 0) return false
+ return shown.some(text => {
+ const bare = withoutSeparators(text)
+ return values.some(value => bare.includes(value))
+ })
+}
+
/**
* Every string, number and key in `value` as written. A serialized form
* escapes quotes and backslashes, so a secret holding either would not match it.
@@ -232,7 +263,18 @@ export function buildGeneratedArtifactAttachment(
try {
opened = openExistingArtifactFile(params.outputDir, params.artifact.name)
const fileBuffer = readOpenedArtifactBuffer(opened, params.maxBytes)
+ if (
+ params.writtenSha256 !== undefined &&
+ createHash('sha256').update(fileBuffer).digest('hex') !== params.writtenSha256
+ ) {
+ warnSkip('artifact_replaced_before_delivery', params.sourceTool, params.artifact)
+ return null
+ }
const secretEntries = params.secretEntriesProvider?.() ?? []
+ if (shownTextHoldsSecretEntry(params.shownText, secretEntries)) {
+ warnSkip('binary_redaction_unavailable', params.sourceTool, params.artifact)
+ return null
+ }
if (isBinaryArtifactFormat(expectedFormat)) {
// Binary artifacts are not redacted byte-by-byte here, so one that may
// hold a secret is withheld: when the source payload that produced it
@@ -252,12 +294,17 @@ export function buildGeneratedArtifactAttachment(
const packageEntries = secretEntries.filter(
entry => !boilerplate.some(text => text.includes(entry.value))
)
- if (packageEntries.length > 0 && OFFICE_PACKAGE_FORMATS.has(expectedFormat)) {
- // Numbers are compared as shown without separators, so any locale's
- // grouping of a protected value matches.
- const shownValues = packageEntries
- .map(entry => withoutSeparators(entry.value))
- .filter(value => value.length >= 4)
+ // Numbers are compared as shown without separators, so any locale's
+ // grouping of a protected value matches. Every entry counts here: a
+ // number a cell or a chart shows comes from the arguments, never from
+ // the generator's own text.
+ const shownValues = secretEntries
+ .map(entry => withoutSeparators(entry.value))
+ .filter(value => value.length >= 4)
+ if (
+ (packageEntries.length > 0 || shownValues.length > 0) &&
+ OFFICE_PACKAGE_FORMATS.has(expectedFormat)
+ ) {
const found = officePackageHasText(
fileBuffer,
text => textContainsRedactableSecretEntry(text, packageEntries),
@@ -288,6 +335,18 @@ export function buildGeneratedArtifactAttachment(
}
}
const redacted = redactArtifactForDelivery(expectedFormat, fileBuffer, secretEntries)
+ // A value printed whole once is redacted, but the same value split by
+ // markup elsewhere in the file is not: read the redacted text as printed.
+ if (
+ !isBinaryArtifactFormat(expectedFormat) &&
+ secretEntries.length > 0 &&
+ printedForms(redacted.buffer.toString('utf8')).some(text =>
+ textContainsRedactableSecretEntry(text, secretEntries)
+ )
+ ) {
+ warnSkip('text_redaction_unavailable', params.sourceTool, params.artifact)
+ return null
+ }
if (redacted.buffer.byteLength > params.maxBytes) {
warnSkip('redacted_artifact_too_large', params.sourceTool, params.artifact)
return null
diff --git a/mcp-host/src/core/tools/nativeToolRegistry.ts b/mcp-host/src/core/tools/nativeToolRegistry.ts
index bda9727ab..58d74ab1b 100644
--- a/mcp-host/src/core/tools/nativeToolRegistry.ts
+++ b/mcp-host/src/core/tools/nativeToolRegistry.ts
@@ -178,6 +178,8 @@ class InternalToolAdapter implements Tool {
sourcePayload: [params, printedForms(params), result.shownText ?? []],
secretEntriesProvider: this.attachmentOptions.secretEntriesProvider,
packageBoilerplate,
+ shownText: result.shownText,
+ writtenSha256: result.writtenSha256,
})
: null
const images = result.success
diff --git a/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts b/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts
new file mode 100644
index 000000000..751c68256
--- /dev/null
+++ b/mcp-host/src/workflow/__tests__/officeBoilerplate.test.ts
@@ -0,0 +1,66 @@
+// 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 }))
+
+vi.mock('fs', async original => {
+ const actual = await original<typeof import('fs')>()
+ return {
+ ...actual,
+ mkdtempSync: (prefix: string) => {
+ if (control.failNextTempDir) {
+ control.failNextTempDir = false
+ throw Object.assign(new Error('no space'), { code: 'ENOSPC' })
+ }
+ return actual.mkdtempSync(prefix)
+ },
+ }
+})
+
+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),
+ }
+})
+
+async function freshModule() {
+ vi.resetModules()
+ return import('../officeBoilerplate')
+}
+
+describe('boilerplateTexts', () => {
+ beforeEach(() => {
+ control.failNextTempDir = false
+ control.hangDocx = false
+ })
+
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('holds no timestamp of the moment the reference was written', async () => {
+ const { boilerplateTexts } = await freshModule()
+ const texts = await boilerplateTexts('docx')
+ expect(texts.length).toBeGreaterThan(0)
+ 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()
+ control.failNextTempDir = true
+ expect(await boilerplateTexts('xlsx')).toEqual([])
+ expect((await boilerplateTexts('xlsx')).length).toBeGreaterThan(0)
+ })
+
+ 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
+ vi.useFakeTimers()
+ const texts = boilerplateTexts('docx')
+ await vi.advanceTimersByTimeAsync(REFERENCE_WAIT_MS)
+ expect(await texts).toEqual([])
+ })
+})
diff --git a/mcp-host/src/workflow/artifactOutput.ts b/mcp-host/src/workflow/artifactOutput.ts
index 25edcb798..93d1fc582 100644
--- a/mcp-host/src/workflow/artifactOutput.ts
+++ b/mcp-host/src/workflow/artifactOutput.ts
@@ -72,6 +72,8 @@ export interface OutputTarget {
filePath: string
/** A file already at `filePath` will be overwritten. */
replacesExisting: boolean
+ /** The SHA-256 of the bytes writeArtifact published here. */
+ writtenSha256?: string
}
/** Where to write `filename`; a file already there is replaced. */
@@ -158,6 +160,7 @@ export function writeArtifact(target: OutputTarget, data: string | Uint8Array):
}
fs.renameSync(partial, target.filePath)
published = true
+ target.writtenSha256 = createHash('sha256').update(data).digest('hex')
} finally {
if (!published) {
try {
@@ -240,6 +243,7 @@ export function artifactResult(
createdAt: new Date().toISOString(),
},
content: parts.join('\n'),
+ ...(target.writtenSha256 ? { writtenSha256: target.writtenSha256 } : {}),
}
}
diff --git a/mcp-host/src/workflow/drawnText.ts b/mcp-host/src/workflow/drawnText.ts
index 493487b16..656ca413a 100644
--- a/mcp-host/src/workflow/drawnText.ts
+++ b/mcp-host/src/workflow/drawnText.ts
@@ -21,7 +21,11 @@ export function recordDrawnText(ctx: TextDrawing): string[] {
return drawn
}
-/** The drawn strings once each, and all of them joined, so a value drawn in pieces reads whole. */
+/**
+ * The drawn strings once each. Each is checked on its own: run together, the
+ * labels 340 and 560 would read "3405". A value that comes from the arguments
+ * is checked whole there, however it is drawn.
+ */
export function drawnTexts(drawn: string[]): string[] {
- return [...new Set(drawn), drawn.join('')]
+ return [...new Set(drawn)]
}
diff --git a/mcp-host/src/workflow/officeBoilerplate.ts b/mcp-host/src/workflow/officeBoilerplate.ts
index 8a81956f1..fed319970 100644
--- a/mcp-host/src/workflow/officeBoilerplate.ts
+++ b/mcp-host/src/workflow/officeBoilerplate.ts
@@ -86,16 +86,22 @@ const SAMPLES: Record<
},
}
+/** How long a call waits for the reference before checking without it. */
+export const REFERENCE_WAIT_MS = 10_000
+
const references = new Map<OfficeFormat, Promise<string[]>>()
/**
* The texts a package of `format` holds on its own. Empty when the sample
- * cannot be written, which leaves the scan as strict as before.
+ * 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.
*/
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',
@@ -108,9 +114,26 @@ export function boilerplateTexts(format: OfficeFormat): Promise<string[]> {
})
references.set(format, reference)
}
- return reference
+ let timer: ReturnType<typeof setTimeout> | undefined
+ const waited = new Promise<string[]>(resolve => {
+ timer = setTimeout(() => {
+ logger.warn(
+ { component: 'OfficeBoilerplate', format },
+ 'The reference package is not ready; protected values are checked against all its text'
+ )
+ resolve([])
+ }, REFERENCE_WAIT_MS)
+ timer.unref?.()
+ })
+ return Promise.race([reference, waited]).finally(() => clearTimeout(timer))
}
+/**
+ * When the sample was written (its document properties), which no other
+ * package shares, so it is no part of the reference.
+ */
+const TIMESTAMP = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z/g
+
async function writeSample(format: OfficeFormat): Promise<string[]> {
const sample = SAMPLES[format]
const run = await sample.load()
@@ -120,7 +143,7 @@ async function writeSample(format: OfficeFormat): Promise<string[]> {
if (!result.success || !result.artifact) throw new Error(result.error ?? 'no artifact')
const texts = officePackageTexts(fs.readFileSync(result.artifact.path))
if (!texts) throw new Error('the reference package could not be read')
- return texts
+ return texts.map(text => text.replace(TIMESTAMP, '\u0000'))
} finally {
fs.rmSync(dir, { recursive: true, force: true })
}
diff --git a/mcp-host/src/workflow/types.ts b/mcp-host/src/workflow/types.ts
index 57496316d..0e78df198 100644
--- a/mcp-host/src/workflow/types.ts
+++ b/mcp-host/src/workflow/types.ts
@@ -218,6 +218,12 @@ export interface InternalToolResult {
* arguments; it is never sent to the model.
*/
shownText?: string[]
+ /**
+ * The SHA-256 of the bytes the generator wrote, so the attachment check
+ * delivers those bytes and not a file written under the same name since. It
+ * is never sent to the model.
+ */
+ writtenSha256?: string
}
/**