Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): spend the XLSX regex budget on matching and size huge titles
marcoteb55f1f0591a64
diff --git a/mcp-host/src/workflow/__tests__/internalTools.xlsx.values.test.ts b/mcp-host/src/workflow/__tests__/internalTools.xlsx.values.test.tsindex ceb935bdd..cb41a2f8a 100644--- a/mcp-host/src/workflow/__tests__/internalTools.xlsx.values.test.ts+++ b/mcp-host/src/workflow/__tests__/internalTools.xlsx.values.test.ts@@ -514,10 +514,44 @@ describe('regex rules', () => {expect(Date.now() - started).toBeLessThan(15_000)expect(result.success, result.error).toBe(true)expect(result.content).toContain('regex took too long to test')+ // A rule after the budget ran out is not blamed on its own pattern.+ expect(result.content).toMatch(/regex was skipped: the regex rules before it used the 500 ms/)const ws = await sheetOf(result)expect(fillOf(ws.getCell('A2'))).not.toBe('FFFEE2E2')}, 30_000)+ it('runs thousands of cheap regex rules within the budget', async () => {+ // Setting up matching per rule would use the budget before the rules do.+ const sheet = (s: number) => ({+ name: `S${s}`,+ rows: [['Code'], ...Array.from({ length: 20 }, (_, i) => [`FAIL-${i}`])],+ conditionalFormatting: [+ {+ column: 'Code',+ rules: Array.from({ length: 40 }, (_, r) => ({+ regex: `^FAIL-${r % 20}$`,+ fillColor: '#fee2e2',+ })),+ },+ ],+ })+ const result = await generate({+ filename: 'many.xlsx',+ sheets: Array.from({ length: 50 }, (_, s) => sheet(s)),+ })+ expect(result.success, result.error).toBe(true)+ expect(result.content ?? '').not.toContain('regex')+ }, 30_000)++ it('sizes a title of very many lines', async () => {+ const result = await generate({+ filename: 'title.xlsx',+ sheets: [{ name: 'T', titleRow: { text: 'line\n'.repeat(200_000) }, rows: [] }],+ })+ expect(result.success, result.error).toBe(true)+ expect(result.content).toContain('titleRow.text was cut to 32,767 characters')+ }, 30_000)+it('still runs ordinary regex rules on a large sheet', async () => {const rows = [['Code'], ...Array.from({ length: 20_000 }, (_, i) => [`R-${i}`])]const { result, ws } = await oneSheet(rows, {diff --git a/mcp-host/src/workflow/xlsxRules.ts b/mcp-host/src/workflow/xlsxRules.tsindex c7bd82d1d..05d5df1c9 100644--- a/mcp-host/src/workflow/xlsxRules.ts+++ b/mcp-host/src/workflow/xlsxRules.ts@@ -36,20 +36,33 @@ const MAX_REGEX_INPUT_LENGTH = 4096/** Time the regex rules of one workbook may take together. */const REGEX_BUDGET_MS = 500+// Wrapped in a function so the script can run again in the same context.const MATCH_ALL = new vm.Script(- 'const re = new RegExp(pattern); const out = []; for (const t of texts) out.push(re.test(t)); out'+ '(() => { const re = new RegExp(pattern); const out = []; for (const t of texts) out.push(re.test(t)); return out })()')-/** Time left for the regex rules of one workbook. */+/**+ * Time left for the regex rules of one workbook. One context serves them all,+ * so the budget pays for matching, not for setting up a context per rule.+ */export class RegexBudget {private remaining = REGEX_BUDGET_MS+ private context?: vm.Context++ /** Whether the rules before this one used up the time. */+ get exhausted(): boolean {+ return this.remaining <= 0+ }/** Which of `texts` match `pattern`, or undefined when the time ran out. */run(pattern: string, texts: string[]): boolean[] | undefined {- if (this.remaining <= 0) return undefined+ if (this.exhausted) return undefined+ this.context ??= vm.createContext({})+ this.context.pattern = pattern+ this.context.texts = textsconst started = Date.now()try {- const out = MATCH_ALL.runInContext(vm.createContext({ pattern, texts }), {+ const out = MATCH_ALL.runInContext(this.context, {timeout: Math.max(1, Math.round(this.remaining)),}) as boolean[]return Array.from(out)@@ -156,12 +169,16 @@ export function prepareRules(const regex = compileRegex(rule.regex, `${field}.regex`, warnings)if (!regex) returnconst inputs = [...new Set(texts.map(t => t.slice(0, MAX_REGEX_INPUT_LENGTH)))]+ const spent = budget.exhaustedconst hits = budget.run(regex.source, inputs)if (!hits) {warnings.push(- `${field}.regex took too long to test (over ${REGEX_BUDGET_MS} ms for all regex ` +- 'rules), so the rule was skipped; use contains, or a pattern without repeated ' +- 'alternatives such as (a|a)*.'+ spent+ ? `${field}.regex was skipped: the regex rules before it used the ${REGEX_BUDGET_MS} ms ` ++ 'all regex rules of a workbook may take; use contains where a plain match will do.'+ : `${field}.regex took too long to test (over ${REGEX_BUDGET_MS} ms for all regex ` ++ 'rules), so the rule was skipped; use contains, or a pattern without repeated ' ++ 'alternatives such as (a|a)*.')return}diff --git a/mcp-host/src/workflow/xlsxWorkbook.ts b/mcp-host/src/workflow/xlsxWorkbook.tsindex a4c1ac524..851ab15ef 100644--- a/mcp-host/src/workflow/xlsxWorkbook.ts+++ b/mcp-host/src/workflow/xlsxWorkbook.ts@@ -424,7 +424,9 @@ function fitTitle(ws: ExcelJS.Worksheet, row: ExcelJS.Row, text: string, span: nconst lines = text.split('\n').map(line => visualWidth(line) * TITLE_CHAR_WIDTH + 2)const column = ws.getColumn(1)if (span === 1 && column.width === undefined) {- column.width = Math.min(Math.max(...lines, DEFAULT_COLUMN_WIDTH), MAX_TITLE_COLUMN_WIDTH)+ let widest = DEFAULT_COLUMN_WIDTH+ for (const needed of lines) widest = Math.max(widest, needed)+ column.width = Math.min(widest, MAX_TITLE_COLUMN_WIDTH)}let available = 0for (let c = 1; c <= span; c++) available += ws.getColumn(c).width ?? DEFAULT_COLUMN_WIDTH@@ -520,7 +522,7 @@ function writeSheet(const row = ws.addRow([titleText])rowNumber = 1const span = Math.max(width, 1)- title = { row, text: titleCell.text, span }+ title = { row, text: typeof titleText === 'string' ? titleText : titleCell.text, span }if (span > 1) ws.mergeCells(1, 1, 1, span)const font: Partial<ExcelJS.Font> = {bold: true,