Skip to content

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

TypeScript

test(mcp-host): require compaction pressure to count the tool schemas (#731)

Alfredo Lopez62796724ca84
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 7b1c62e7f..d75c834a9 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.382",
+ "version": "0.1.383",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.382",
+ "version": "0.1.383",
"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 762c1abff..61c1f2f95 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.382",
+ "version": "0.1.383",
"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/orchestration/__tests__/manageMessagesForIteration.test.ts b/mcp-host/src/core/orchestration/__tests__/manageMessagesForIteration.test.ts
new file mode 100644
index 000000000..d7406439b
--- /dev/null
+++ b/mcp-host/src/core/orchestration/__tests__/manageMessagesForIteration.test.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it, vi } from 'vitest'
+import { makeFakeConversation } from '../../conversation/__testing__/makeFakeConversation'
+import type { ReasoningPort, ToolRegistry } from '../../interfaces'
+import { BasicSafety } from '../../safety/safety'
+import type { ChatMessage, ToolDefinition } from '../../types'
+import { SimpleEventEmitter } from '../eventEmitter'
+import { buildLoopConfig } from '../loopConfig'
+import { manageMessagesForIteration } from '../toolUseLoopRuntime'
+
+describe('manageMessagesForIteration', () => {
+ it('T-R2-2d hands the registry tool definitions to the context manager (#731)', async () => {
+ // The tool schemas travel in the request the contract caps; the manager can
+ // only count them if the loop passes them (review r2, M2b). The registry's
+ // full list is a superset of what the loop presents, so the count errs high.
+ const definitions: ToolDefinition[] = [
+ { name: 'crm_search_contacts', description: 'Search', parameters: { type: 'object' } },
+ ]
+ const toolRegistry: ToolRegistry = {
+ get: () => null,
+ listDefinitions: () => definitions,
+ register: vi.fn(),
+ }
+ const reasoning: ReasoningPort = {
+ respondWithTools: vi.fn(),
+ continueWithToolResults: vi.fn(),
+ }
+ const manage = vi.fn((messages: ChatMessage[]) => messages)
+ const conversation = makeFakeConversation()
+ const config = buildLoopConfig({
+ reasoning,
+ toolRegistry,
+ safety: new BasicSafety(),
+ events: new SimpleEventEmitter(),
+ conversation,
+ contextManager: { manage },
+ })
+ const messages: ChatMessage[] = [{ role: 'user', content: 'hello' }]
+
+ const managed = await manageMessagesForIteration(config, messages, 0, true)
+
+ expect(managed).toBe(messages)
+ expect(manage).toHaveBeenCalledTimes(1)
+ expect(manage).toHaveBeenCalledWith(messages, conversation, { tools: definitions })
+ })
+})
diff --git a/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts b/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
index 577ac6de9..08c17c8e3 100644
--- a/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
+++ b/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
@@ -9,8 +9,8 @@ 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'
-import { heuristicCount } from '../heuristic'
+import type { ChatMessage, ToolDefinition } from '../../types'
+import { heuristicCount, heuristicCountTools } from '../heuristic'
import { tokenizerDryrunTierMismatchTotal } from '../metrics'
import type { TokenCounter } from '../tokenCounter'
@@ -126,3 +126,67 @@ describe('PressureContextManager dry-run', () => {
expect(result).toBe(msgs) // passthrough
})
})
+
+// The tool schemas travel in the same request the contract caps, so the gauge
+// that decides when to compact must count them (review r2, M2b). Every branch
+// of `computePressure` — no counter, dry-run, counter-driven — must see them.
+describe('PressureContextManager pressure includes the tool schemas', () => {
+ function bulkyTools(): ToolDefinition[] {
+ return [
+ {
+ name: 'crm_search_contacts',
+ description: 'Search CRM contacts',
+ parameters: {
+ type: 'object',
+ properties: { q: { type: 'string', description: 'x'.repeat(4_000) } },
+ },
+ },
+ ]
+ }
+
+ it('T-R2-2a tools push a conversation below 0.8 into compaction on the heuristic path', async () => {
+ const msgs = tinyMessages()
+ const tools = bulkyTools()
+ const maxTokens = Math.ceil(heuristicCount(msgs) / 0.7)
+ // Witness the arithmetic, so the two outcomes below can only differ by the tools term.
+ expect(heuristicCount(msgs) / maxTokens).toBeLessThan(0.8)
+ expect((heuristicCount(msgs) + heuristicCountTools(tools)) / maxTokens).toBeGreaterThanOrEqual(
+ 0.95
+ )
+ const manager = new PressureContextManager(maxTokens)
+
+ expect(await manager.manage(msgs, makeFakeConversation())).toBe(msgs)
+ expect(await manager.manage(msgs, makeFakeConversation(), { tools })).not.toBe(msgs)
+ })
+
+ it('T-R2-2b dry-run decides from messages plus tools and hands the tools to the counter', async () => {
+ const msgs = tinyMessages()
+ const tools = bulkyTools()
+ const maxTokens = Math.ceil(heuristicCount(msgs) / 0.7)
+ const counter = makeCounter(0) // would pass through if it decided
+ const manager = new PressureContextManager(maxTokens, undefined, undefined, counter, {
+ dryRun: true,
+ })
+
+ const result = await manager.manage(msgs, makeFakeConversation(), { tools })
+
+ expect(result).not.toBe(msgs)
+ expect(counter.count).toHaveBeenCalledWith(msgs, tools)
+ })
+
+ it('T-R2-2c the counter-driven path hands the tools to the counter', async () => {
+ const msgs = tinyMessages()
+ const tools = bulkyTools()
+ const counter = makeCounter(0)
+ const manager = new PressureContextManager(1_000, undefined, undefined, counter, {
+ dryRun: false,
+ })
+
+ const result = await manager.manage(msgs, makeFakeConversation(), { tools })
+
+ // The counter reports 0, so its number decided: passthrough by reference.
+ expect(result).toBe(msgs)
+ expect(counter.count).toHaveBeenCalledTimes(1)
+ expect(counter.count).toHaveBeenCalledWith(msgs, tools)
+ })
+})