Skip to content

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

TypeScript

fix(mcp-host): count a thrown heartbeat round as failed, not completed (#148)

Alfredo Lopezbaec7a21379a
diff --git a/mcp-host/src/__tests__/mcp/statusHeartbeat.test.ts b/mcp-host/src/__tests__/mcp/statusHeartbeat.test.ts
index 90eba8774..66eb93628 100644
--- a/mcp-host/src/__tests__/mcp/statusHeartbeat.test.ts
+++ b/mcp-host/src/__tests__/mcp/statusHeartbeat.test.ts
@@ -7,6 +7,7 @@ function metrics() {
runStarted: vi.fn<() => void>(),
runSkipped: vi.fn<() => void>(),
runFinished: vi.fn<(summary: McpStatusRefreshSummary) => void>(),
+ runErrored: vi.fn<(aborted: boolean) => void>(),
}
}
@@ -76,4 +77,31 @@ describe('McpStatusHeartbeat', () => {
expect(refreshAllServerStatus).toHaveBeenCalledTimes(1)
vi.useRealTimers()
})
+
+ it('classifies a thrown (non-aborted) round as errored, not completed', async () => {
+ vi.useFakeTimers()
+ const refreshAllServerStatus = vi
+ .fn<(options: { timeoutMs?: number; signal?: AbortSignal }) => Promise<typeof summary>>()
+ .mockRejectedValue(new Error('tracker exploded'))
+ const observedMetrics = metrics()
+ const onError = vi.fn<(error: unknown) => void>()
+ const heartbeat = new McpStatusHeartbeat({
+ intervalMs: 30_000,
+ timeoutMs: 25_000,
+ getRefresher: () => ({ refreshAllServerStatus }),
+ metrics: observedMetrics,
+ onError,
+ })
+
+ heartbeat.start()
+ await vi.advanceTimersByTimeAsync(0)
+
+ // A genuine throw is a failed round, never a silent 'completed' — the
+ // idle-memory gate (#148) relies on runs_total distinguishing the two.
+ expect(observedMetrics.runErrored).toHaveBeenCalledWith(false)
+ expect(observedMetrics.runFinished).not.toHaveBeenCalled()
+ expect(onError).toHaveBeenCalledTimes(1)
+ heartbeat.stop()
+ vi.useRealTimers()
+ })
})
diff --git a/mcp-host/src/__tests__/mcp/statusHeartbeatMetrics.test.ts b/mcp-host/src/__tests__/mcp/statusHeartbeatMetrics.test.ts
index df6edfbf6..3ea8865ba 100644
--- a/mcp-host/src/__tests__/mcp/statusHeartbeatMetrics.test.ts
+++ b/mcp-host/src/__tests__/mcp/statusHeartbeatMetrics.test.ts
@@ -54,4 +54,21 @@ describe('MCP status heartbeat metrics', () => {
expect(scrape).toContain('clerum_mcp_status_heartbeat_tools 66')
expect(scrape).toContain('clerum_mcp_status_heartbeat_output_schemas 60')
})
+
+ it('records a thrown round as failed and an aborted throw as aborted', async () => {
+ const registry = new Registry()
+ const metrics = new McpStatusHeartbeatMetrics(registry)
+
+ metrics.runStarted()
+ metrics.runErrored(false)
+ metrics.runStarted()
+ metrics.runErrored(true)
+
+ const scrape = await registry.metrics()
+ expect(scrape).toContain('clerum_mcp_status_heartbeat_runs_total{outcome="failed"} 1')
+ expect(scrape).toContain('clerum_mcp_status_heartbeat_runs_total{outcome="aborted"} 1')
+ // A thrown round must never be counted as a clean completion.
+ expect(scrape).toContain('clerum_mcp_status_heartbeat_runs_total{outcome="completed"} 0')
+ expect(scrape).toContain('clerum_mcp_status_heartbeat_in_flight 0')
+ })
})
diff --git a/mcp-host/src/mcp/statusHeartbeat.ts b/mcp-host/src/mcp/statusHeartbeat.ts
index accea88eb..c429e97bf 100644
--- a/mcp-host/src/mcp/statusHeartbeat.ts
+++ b/mcp-host/src/mcp/statusHeartbeat.ts
@@ -1,5 +1,5 @@
import type { McpToolCallOptions } from './client'
-import type { McpManager, McpStatusRefreshSummary } from './manager'
+import type { McpManager } from './manager'
import {
type McpStatusHeartbeatMetricsPort,
mcpStatusHeartbeatMetrics,
@@ -73,7 +73,10 @@ export class McpStatusHeartbeat {
} satisfies McpToolCallOptions)
this.metrics.runFinished(summary)
} catch (error) {
- this.metrics.runFinished(failedSummary(controller.signal.aborted))
+ // A throw never yields an authoritative summary. Classify it explicitly so
+ // the run is counted as errored (aborted vs failed) instead of a summary
+ // with failed:0 that runFinished would misread as a clean 'completed'.
+ this.metrics.runErrored(controller.signal.aborted)
this.options.onError?.(error)
} finally {
clearTimeout(timeout)
@@ -81,14 +84,3 @@ export class McpStatusHeartbeat {
}
}
}
-
-function failedSummary(aborted: boolean): McpStatusRefreshSummary {
- return {
- serverCount: 0,
- succeeded: 0,
- failed: 0,
- toolCount: 0,
- outputSchemaCount: 0,
- aborted,
- }
-}
diff --git a/mcp-host/src/mcp/statusHeartbeatMetrics.ts b/mcp-host/src/mcp/statusHeartbeatMetrics.ts
index 190295092..3f882e9db 100644
--- a/mcp-host/src/mcp/statusHeartbeatMetrics.ts
+++ b/mcp-host/src/mcp/statusHeartbeatMetrics.ts
@@ -9,6 +9,7 @@ export interface McpStatusHeartbeatMetricsPort {
runStarted(): void
runSkipped(): void
runFinished(summary: McpStatusRefreshSummary): void
+ runErrored(aborted: boolean): void
}
function counter<Label extends string>(
@@ -91,6 +92,18 @@ export class McpStatusHeartbeatMetrics implements McpStatusHeartbeatMetricsPort
this.runs.inc({ outcome: 'skipped' satisfies Outcome })
}
+ /**
+ * A round that threw. It never produced an authoritative summary, so only the
+ * outcome counter and in-flight gauge move (last-known gauges are preserved):
+ * an aborted throw (stop/timeout) is 'aborted', any other throw is 'failed'.
+ * Never 'completed' — the idle-memory gate (#148) proves zero failures from
+ * this series, so a real error must count as one.
+ */
+ runErrored(aborted: boolean): void {
+ this.inFlight.dec()
+ this.runs.inc({ outcome: (aborted ? 'aborted' : 'failed') satisfies Outcome })
+ }
+
runFinished(summary: McpStatusRefreshSummary): void {
this.inFlight.dec()
const outcome: Outcome = summary.aborted