Skip to content

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

TypeScript

test(mcp-host): pin the compaction backoff warning and logger routing (#731)

Alfredo Lopez1e5d39c4e534
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 7920f127f..77595a5ff 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.370",
+ "version": "0.1.371",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.370",
+ "version": "0.1.371",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index fbb6fd5d6..1e5649454 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.370",
+ "version": "0.1.371",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/extensions/__tests__/contextManager.antiThrash.test.ts b/mcp-host/src/core/extensions/__tests__/contextManager.antiThrash.test.ts
index 0df01445b..1e6cb48a0 100644
--- a/mcp-host/src/core/extensions/__tests__/contextManager.antiThrash.test.ts
+++ b/mcp-host/src/core/extensions/__tests__/contextManager.antiThrash.test.ts
@@ -12,6 +12,7 @@
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { Counter, register } from 'prom-client'
+import { logger } from '../../../logger'
import { makeFakeConversation } from '../../conversation/__testing__/makeFakeConversation'
import { SimpleEventEmitter } from '../../orchestration/eventEmitter'
import {
@@ -165,6 +166,43 @@ describe('PressureContextManager — T1.4 anti-thrash', () => {
expect(await counterValue(clerumCompactionTotal, { outcome: 'thrashing' })).toBe(1)
})
+ it('T-E1 the backoff warns once through the service logger and keeps counting (#731)', async () => {
+ // The backoff already emits a core event and a metric. Neither reaches the
+ // pod log, and the pod log is what an operator reads when a conversation
+ // stops being compacted — so the turn proceeded uncompacted with nothing
+ // written down anywhere they would look (#731).
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {})
+ const manager = new PressureContextManager(truncateMaxTokens, undefined, undefined, undefined, {
+ taskId: 'task-E1',
+ })
+ let msgs = buildIneffectiveFixture()
+ msgs = await manager.manage(msgs, conv)
+ msgs = await manager.manage(msgs, conv)
+ msgs = await manager.manage(msgs, conv) // backoff transition
+
+ expect(warnSpy).toHaveBeenCalledTimes(1)
+ expect(warnSpy).toHaveBeenCalledWith(
+ expect.objectContaining({
+ taskId: 'task-E1',
+ conversationId: conv.id,
+ pressure: expect.any(Number),
+ lastRatio: expect.any(Number),
+ messageCount: expect.any(Number),
+ }),
+ 'Compaction backoff: history cannot be shrunk; proceeding uncompacted'
+ )
+
+ for (let i = 0; i < 4; i++) {
+ msgs = await manager.manage(msgs, conv)
+ }
+ // Liveness witness for the negative assertion that follows: the counter
+ // proves the four extra calls ran and took the backoff path. Without it,
+ // "still 1" would pass just as happily if `manage()` had never been called
+ // again.
+ expect(await counterValue(clerumCompactionTotal, { outcome: 'thrashing' })).toBe(5)
+ expect(warnSpy).toHaveBeenCalledTimes(1)
+ })
+
it('§8.1 case 5: backoff persists for the rest of the task; ratio histogram is not observed after backoff', async () => {
const events = new SimpleEventEmitter()
const captured: AgentEvent[] = []
diff --git a/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts b/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
index fb6d58b6a..577ac6de9 100644
--- a/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
+++ b/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
@@ -6,6 +6,7 @@
* - when `dryRun: false`, the counter drives the tier directly.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { logger } from '../../../logger'
import { makeFakeConversation } from '../../conversation/__testing__/makeFakeConversation'
import { PressureContextManager } from '../../extensions/contextManager'
import type { ChatMessage } from '../../types'
@@ -83,6 +84,41 @@ describe('PressureContextManager dry-run', () => {
expect(result).not.toBe(msgs)
})
+ it('T-E1b a failing dry-run counter is reported through the service logger (#731)', async () => {
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {})
+ const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ const msgs = tinyMessages()
+ const counter = makeCounter(0)
+ counter.count = vi.fn(async () => {
+ throw new Error('counter unavailable')
+ })
+ const manager = new PressureContextManager(
+ Math.ceil(heuristicCount(msgs) / 0.5),
+ undefined,
+ undefined,
+ counter,
+ { dryRun: true }
+ )
+
+ const result = await manager.manage(msgs, makeFakeConversation())
+ // Liveness witness for the `console.warn` negative below: a decision was
+ // still made, from the heuristic, so the catch branch really executed.
+ // Without this the `not.toHaveBeenCalled` would pass on a `manage()` that
+ // threw or never reached the counter at all.
+ expect(result).toBe(msgs)
+ expect(counter.count).toHaveBeenCalledTimes(1)
+
+ expect(warnSpy).toHaveBeenCalledTimes(1)
+ expect(warnSpy).toHaveBeenCalledWith(
+ expect.objectContaining({ err: expect.any(Error) }),
+ 'dryrun counter failed; using heuristic'
+ )
+ expect(consoleSpy).not.toHaveBeenCalled()
+
+ warnSpy.mockRestore()
+ consoleSpy.mockRestore()
+ })
+
it('falls back to heuristic when no counter is provided', async () => {
const msgs = tinyMessages()
const manager = new PressureContextManager(1_000_000) // huge budget
diff --git a/mcp-host/src/llm/__tests__/contextOverrunJourney.test.ts b/mcp-host/src/llm/__tests__/contextOverrunJourney.test.ts
index 7e9094bd9..694c6dda4 100644
--- a/mcp-host/src/llm/__tests__/contextOverrunJourney.test.ts
+++ b/mcp-host/src/llm/__tests__/contextOverrunJourney.test.ts
@@ -16,7 +16,7 @@
* differ only in their fixture's shape, and the compaction counters are what
* tell the two no-op-looking outcomes apart.
*/
-import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { type Counter, register } from 'prom-client'
import { hashCanonicalCodexRequest } from '@clerum/llm-provider-attempt-contract'
import { minifiedMcpResult } from '../../__tests__/fixtures/minifiedMcpResult'
@@ -26,6 +26,7 @@ import { PressureContextManager, clerumCompactionTotal } from '../../core/extens
import { SimpleEventEmitter } from '../../core/orchestration/eventEmitter'
import { validateToolLinkages } from '../../core/orchestration/toolUseLoop'
import type { AgentEvent, ChatMessage, ToolDefinition } from '../../core/types'
+import { logger } from '../../logger'
import { CodexSubscriptionProvider } from '../codexSubscription'
const requestHash = 'a'.repeat(64)
@@ -149,6 +150,10 @@ describe('#731 context-overrun journey', () => {
register.resetMetrics()
})
+ afterEach(() => {
+ vi.restoreAllMocks()
+ })
+
it('J1 an MCP-heavy multi-turn history compacts and the contract accepts the request (#731)', async () => {
const msgs = mcpHeavyHistory(32, 35_000)
// Precondition, asserted rather than assumed: the uncompacted history is
@@ -187,6 +192,7 @@ describe('#731 context-overrun journey', () => {
})
it('J2 a single long agentic turn reaches backoff and is refused as ContextLengthExceeded (#731)', async () => {
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {})
const events = new SimpleEventEmitter()
const captured: AgentEvent[] = []
events.on('compaction:thrashing', e => captured.push(e))
@@ -215,6 +221,14 @@ describe('#731 context-overrun journey', () => {
).toBe(1)
expect(captured).toHaveLength(1)
expect(captured[0].data).toMatchObject({ taskId: 'task-J2', consecutiveCount: 2 })
+ // The operator-facing half of the same transition: the event goes to the
+ // bus, this goes to the pod log. The thrashing counter above is the
+ // liveness witness that makes "exactly once" mean something here (#731).
+ expect(warnSpy).toHaveBeenCalledTimes(1)
+ expect(warnSpy).toHaveBeenCalledWith(
+ expect.objectContaining({ taskId: 'task-J2' }),
+ 'Compaction backoff: history cannot be shrunk; proceeding uncompacted'
+ )
// State: the backoff returns its input untouched. That unchanged array is
// the one legitimate no-op in this system, which is why J1 pins