Skip to content

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

TypeScript

fix(mcp-host): bound the text width caches by what they hold

marcotebbdc0f97f1b1d
diff --git a/mcp-host/src/workflow/fonts.ts b/mcp-host/src/workflow/fonts.ts
index 34114a2a4..56cb7be70 100644
--- a/mcp-host/src/workflow/fonts.ts
+++ b/mcp-host/src/workflow/fonts.ts
@@ -869,6 +869,8 @@ function discoverFallbacks(exclude: Set<string>): FallbackFamily[] {
/** Longest text whose measured width is kept, and how many widths are kept at once. */
const MEASURED_TEXT_CACHED = 64
const MEASURED_WIDTHS_KEPT = 50_000
+/** Face choices kept at once, by family and code point. */
+const CHOSEN_KEPT = 50_000
class GlyphSource implements PdfGlyphSource {
private readonly info = new Map<string, FaceInfo | undefined>()
@@ -949,6 +951,9 @@ class GlyphSource implements PdfGlyphSource {
else {
hit = this.fallbackList().find(f => this.covers(f.family, cp) && this.usable(f))?.family
}
+ // One entry per family and code point a document uses; a text of many
+ // distinct ones (a dictionary of Han characters) starts it over.
+ if (this.chosen.size >= CHOSEN_KEPT) this.chosen.clear()
this.chosen.set(key, hit)
return hit
}
diff --git a/mcp-host/src/workflow/pptxText.ts b/mcp-host/src/workflow/pptxText.ts
index 37740cd05..c8b77053a 100644
--- a/mcp-host/src/workflow/pptxText.ts
+++ b/mcp-host/src/workflow/pptxText.ts
@@ -50,6 +50,9 @@ function canvasContext(): SKRSContext2D {
}
const widthCache = new Map<string, number>()
+/** Longest text whose width is kept, and how many widths are kept at once. */
+const CACHED_TEXT_LENGTH = 64
+const CACHED_WIDTHS = 20_000
/** Width of `text` in ems of the deck font. */
function widthEm(text: string, bold: boolean): number {
@@ -66,8 +69,11 @@ function widthEm(text: string, bold: boolean): number {
}
const measured = narrow ? ctx.measureText(narrow).width / 100 : 0
const em = measured * (bold ? BOLD_WIDTH_FACTOR : REGULAR_WIDTH_FACTOR) + wide
- if (widthCache.size > 20_000) widthCache.clear()
- widthCache.set(key, em)
+ // Only short texts repeat (words, labels); a whole cell is measured once.
+ if (text.length <= CACHED_TEXT_LENGTH) {
+ if (widthCache.size >= CACHED_WIDTHS) widthCache.clear()
+ widthCache.set(key, em)
+ }
return em
}