Skip to content

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

TypeScript

fix(mcp-host): draw a gauge whose gaugeMax is not positive on the default dial, and say so

marcoteb0394880996bd
diff --git a/mcp-host/src/workflow/__tests__/internalTools.charts.delivery.test.ts b/mcp-host/src/workflow/__tests__/internalTools.charts.delivery.test.ts
index 80719e16a..8be5ec12b 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.charts.delivery.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.charts.delivery.test.ts
@@ -259,6 +259,15 @@ describe('chart size limits', () => {
expect(size.width / size.height).toBeCloseTo(4000 / 100, 1)
})
+ it('draws the default size for a width or height that is not a positive number', async () => {
+ const result = await chart().execute({ ...yearly, width: -5, height: 'tall' }, outputDir)
+ expect(result.success).toBe(true)
+ expect(result.content).toContain('width -5 is not a positive number; used 800.')
+ expect(result.content).toContain('height tall is not a positive number; used 400.')
+ const size = pngSize(result.artifact!.path)
+ expect(size.width / size.height).toBeCloseTo(800 / 400, 2)
+ })
+
it('charges the quota for the PNG written, not for the canvas it was drawn on', async () => {
const prev = process.env.CLERUM_WORKFLOW_OUTPUT_QUOTA_MB
process.env.CLERUM_WORKFLOW_OUTPUT_QUOTA_MB = '1'
diff --git a/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts b/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts
index b9803ea9e..3999b7a95 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts
@@ -598,6 +598,59 @@ describe('gauge readout', () => {
expect(r.content).toContain('-5')
expect(r.content).toContain('below 0')
})
+
+ /** The dial segments [arc, rest] of the gauge `args` draws, and the notes it returns. */
+ async function dial(args: Record<string, unknown>): Promise<{ data: unknown; notes: string }> {
+ const seen: unknown[] = []
+ const draw = Chart.prototype.draw
+ const spy = vi.spyOn(Chart.prototype, 'draw').mockImplementation(function (this: Chart) {
+ seen.push([...this.data.datasets[0].data])
+ return draw.call(this)
+ })
+ try {
+ const r = await findTool('clerum__generate_chart').execute(
+ { filename: 'dial.png', type: 'gauge', ...args },
+ outputDir
+ )
+ expect(r.success, r.error).toBe(true)
+ return { data: seen[seen.length - 1], notes: r.content ?? '' }
+ } finally {
+ spy.mockRestore()
+ }
+ }
+
+ const reading = (value: number) => ({ data: { datasets: [{ data: [value] }] } })
+
+ it.each([-5, 0])('draws the default dial and says so when gaugeMax is %s', async gaugeMax => {
+ const { data, notes } = await dial({ ...reading(40), gaugeMax })
+ expect(data).toEqual([40, 60])
+ expect(notes).toContain(`gaugeMax ${gaugeMax} is not a positive number; used 100.`)
+ expect(notes).not.toContain('shown full')
+ expect(notes).not.toContain('shown empty')
+ })
+
+ it('measures a reading against the dial it draws when gaugeMax is not positive', async () => {
+ const over = await dial({ ...reading(150), gaugeMax: -5 })
+ expect(over.data).toEqual([100, 0])
+ expect(over.notes).toContain('gaugeMax -5 is not a positive number; used 100.')
+ expect(over.notes).toContain(
+ 'the gauge value 150 exceeds gaugeMax 100, so the dial is shown full'
+ )
+ })
+
+ it('keeps a positive gaugeMax, and the default when none is sent', async () => {
+ expect(await dial({ ...reading(40), gaugeMax: 50 })).toEqual({
+ data: [40, 10],
+ notes: expect.not.stringContaining('gaugeMax'),
+ })
+ expect((await dial({ ...reading(0.25), gaugeMax: 0.5 })).data).toEqual([0.25, 0.25])
+ expect((await dial({ ...reading(40), gaugeMax: '50' })).data).toEqual([40, 10])
+ for (const unset of [{}, { gaugeMax: null }]) {
+ const { data, notes } = await dial({ ...reading(40), ...unset })
+ expect(data).toEqual([40, 60])
+ expect(notes).not.toContain('gaugeMax')
+ }
+ })
})
describe('colors follow their points', () => {
diff --git a/mcp-host/src/workflow/__tests__/internalTools.dashboard.fidelity.test.ts b/mcp-host/src/workflow/__tests__/internalTools.dashboard.fidelity.test.ts
index 8817ffe91..0f1434b83 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.dashboard.fidelity.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.dashboard.fidelity.test.ts
@@ -591,6 +591,41 @@ describe('gauge', () => {
})
expect(notes).not.toContain('labels')
})
+
+ const gauge = async (extra: Record<string, unknown>) => {
+ const { html, notes } = await render({
+ data: { title: 'T', charts: [{ type: 'gauge', datasets: [{ data: [40] }], ...extra }] },
+ })
+ const [spec] = chartSpecs(html)
+ return {
+ dial: (spec.datasets as Array<{ data: number[] }>)[0].data,
+ readout: spec.gauge,
+ notes,
+ }
+ }
+
+ it.each([-5, 0, 'none'])('says it drew 0..100 when gaugeMax is %j', async gaugeMax => {
+ const { dial, readout, notes } = await gauge({ gaugeMax })
+ expect(dial).toEqual([40, 60])
+ expect(readout).toEqual({ value: '40', max: '100' })
+ expect(notes).toContain(
+ `data.charts[0]: gaugeMax ${gaugeMax} is not a positive number; used 100.`
+ )
+ })
+
+ it('keeps a positive gaugeMax, written as a number or as text, without a note', async () => {
+ for (const [extra, max] of [
+ [{ gaugeMax: 50 }, 50],
+ [{ gaugeMax: '1,200' }, 1200],
+ [{}, 100],
+ [{ gaugeMax: null }, 100],
+ ] as const) {
+ const { dial, readout, notes } = await gauge(extra)
+ expect(dial).toEqual([40, max - 40])
+ expect(readout).toEqual({ value: '40', max: String(max) })
+ expect(notes).not.toContain('gaugeMax')
+ }
+ })
})
describe('funnel stages', () => {
diff --git a/mcp-host/src/workflow/chartGenerator.ts b/mcp-host/src/workflow/chartGenerator.ts
index d358ec8e9..1c8c1454b 100644
--- a/mcp-host/src/workflow/chartGenerator.ts
+++ b/mcp-host/src/workflow/chartGenerator.ts
@@ -368,7 +368,8 @@ function resolveChartType(
if (!ds) return o
const values = (ds.data as unknown[]).filter((v): v is number => typeof v === 'number')
const reading = values[0] ?? 0
- const max = Math.max(0, Number(args.gaugeMax) || 100)
+ // The dial and its notes use this one validated ceiling.
+ const max = positiveNumber('gaugeMax', args.gaugeMax, 100, o.notes)
// Only the arc is bounded by the dial; the readout prints the reading.
const arc = Math.min(Math.max(reading, 0), max)
if (reading > max) {
@@ -542,8 +543,12 @@ function isVisuallyEmpty(datasets: ChartDataset[]): boolean {
return sawNumber
}
-function clampDimension(
- field: 'width' | 'height',
+/**
+ * `value` as a positive finite number; `fallback` when it is unset, and also
+ * when it is not one, which `warnings` records.
+ */
+function positiveNumber(
+ field: string,
value: unknown,
fallback: number,
warnings: string[]
@@ -554,6 +559,16 @@ function clampDimension(
warnings.push(`${field} ${String(value)} is not a positive number; used ${fallback}.`)
return fallback
}
+ return n
+}
+
+function clampDimension(
+ field: 'width' | 'height',
+ value: unknown,
+ fallback: number,
+ warnings: string[]
+): number {
+ const n = positiveNumber(field, value, fallback, warnings)
if (n > MAX_CHART_DIMENSION) {
warnings.push(
`${field} ${n} is over the ${MAX_CHART_DIMENSION} maximum; used ${MAX_CHART_DIMENSION}.`
diff --git a/mcp-host/src/workflow/dashboardCharts.ts b/mcp-host/src/workflow/dashboardCharts.ts
index 9ab1d8fa2..f35519cb9 100644
--- a/mcp-host/src/workflow/dashboardCharts.ts
+++ b/mcp-host/src/workflow/dashboardCharts.ts
@@ -334,6 +334,9 @@ function translate(
chart.legend = false
const requestedMax = coerceNumber(spec.gaugeMax)
const max = requestedMax !== undefined && requestedMax > 0 ? requestedMax : 100
+ if (max !== requestedMax && spec.gaugeMax !== undefined && spec.gaugeMax !== null) {
+ warnings.push(`gaugeMax ${String(spec.gaugeMax)} is not a positive number; used 100.`)
+ }
const values = firstSeriesValues(datasets[0])
const raw = values[0] ?? 0
if (values.length > 1) {