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 when history reaches the contract message bound (#731)

Alfredo Lopez354ddbb4898b
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 14d27dc12..ca7a156e3 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.384",
+ "version": "0.1.385",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.384",
+ "version": "0.1.385",
"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 5d560e00c..715a92333 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.384",
+ "version": "0.1.385",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/agent/__tests__/taskExecutor.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.test.ts
index 5d9a7912a..0bcb99ea7 100644
--- a/mcp-host/src/agent/__tests__/taskExecutor.test.ts
+++ b/mcp-host/src/agent/__tests__/taskExecutor.test.ts
@@ -1,8 +1,10 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { config as appConfig } from '../../config'
import { LlmPortAdapter } from '../../core/adapters/llmPortAdapter'
+import { makeFakeConversation } from '../../core/conversation/__testing__/makeFakeConversation'
import { ConversationManager } from '../../core/conversation/conversation'
import { LlmError, LlmErrorCode } from '../../core/errors'
+import { PressureContextManager } from '../../core/extensions/contextManager'
import { SimpleEventEmitter } from '../../core/orchestration/eventEmitter'
import { parseCodexToolPresentation } from '../../core/orchestration/toolPresentationPolicy'
import { executeSingleTool, runToolUseLoop } from '../../core/orchestration/toolUseLoop'
@@ -2290,3 +2292,50 @@ describe('#654 failover identity + per-attempt image guard', () => {
expect(imageInput).toHaveBeenCalledWith('claude', 'claude-haiku-4-5')
})
})
+
+describe('TaskExecutor context manager message bound (#731)', () => {
+ function smallTurns(count: number): ChatMessage[] {
+ const msgs: ChatMessage[] = [{ role: 'system', content: 'sys' }]
+ for (let i = 0; msgs.length < count; i++) {
+ msgs.push(
+ i % 2 === 0 ? { role: 'user', content: `q${i}` } : { role: 'assistant', content: `a${i}` }
+ )
+ }
+ return msgs
+ }
+
+ // Runs a task for `providerType` and hands back the context manager the loop
+ // received, so its behaviour — not its private fields — is what is asserted.
+ async function loopContextManager(providerType: string) {
+ vi.mocked(runToolUseLoop).mockResolvedValueOnce({ type: 'response', content: 'ok' } as any)
+ const deps = createDeps({
+ llmProvider: {
+ completeSingleTurn: vi.fn(),
+ completeSingleTurnWithTools: vi.fn(),
+ getProviderType: () => providerType,
+ } as any,
+ })
+ await new TaskExecutor(createTask('hi'), deps).run()
+ const loopConfig = vi.mocked(runToolUseLoop).mock.calls.at(-1)?.[0]
+ if (!loopConfig) throw new Error('Expected runToolUseLoop to receive a loop config')
+ return loopConfig.contextManager
+ }
+
+ it('T-R2-3c a codex-subscription task compacts past the contract message bound', async () => {
+ const msgs = smallTurns(1_025)
+ const managed = await (
+ await loopContextManager('codex-subscription')
+ ).manage(msgs, makeFakeConversation())
+ expect(managed.length).toBeLessThan(1_024)
+ })
+
+ it('T-R2-3d an openai task, with no attempt contract, is not bounded by message count', async () => {
+ const msgs = smallTurns(1_025)
+ const contextManager = await loopContextManager('openai')
+ // Liveness witness: the loop's default manager always passes through, so the
+ // passthrough below proves nothing unless this is the pressure manager.
+ expect(contextManager).toBeInstanceOf(PressureContextManager)
+ const managed = await contextManager.manage(msgs, makeFakeConversation())
+ expect(managed).toBe(msgs)
+ })
+})
diff --git a/mcp-host/src/core/extensions/__tests__/contextManager.test.ts b/mcp-host/src/core/extensions/__tests__/contextManager.test.ts
index a2fabe4c1..b0d5d6adb 100644
--- a/mcp-host/src/core/extensions/__tests__/contextManager.test.ts
+++ b/mcp-host/src/core/extensions/__tests__/contextManager.test.ts
@@ -505,3 +505,38 @@ describe('PressureContextManager — #731 MCP-heavy history', () => {
)
})
})
+
+// The provider attempt contracts refuse a request whose `messages` array exceeds
+// `LIMITS.maxMessages`, however few tokens it carries. Many small tool turns reach
+// that bound at low token pressure, so compaction never ran and every later turn
+// was refused the same way (review r2, M3).
+describe('PressureContextManager message-count pressure', () => {
+ function smallTurns(count: number): ChatMessage[] {
+ const msgs: ChatMessage[] = [{ role: 'system', content: 'sys' }]
+ for (let i = 0; msgs.length < count; i++) {
+ msgs.push(
+ i % 2 === 0 ? { role: 'user', content: `q${i}` } : { role: 'assistant', content: `a${i}` }
+ )
+ }
+ return msgs
+ }
+
+ it('T-R2-3a compacts 1025 small messages below a maxMessages of 1024', async () => {
+ const msgs = smallTurns(1_025)
+ expect(msgs).toHaveLength(1_025)
+ // Token pressure alone is far below 0.8: the same input passes through
+ // without the bound, which is the refusal loop this test closes.
+ expect(estimateTokens(msgs) / 1_000_000).toBeLessThan(0.8)
+ expect(await new PressureContextManager(1_000_000).manage(msgs, makeFakeConversation())).toBe(
+ msgs
+ )
+
+ const bounded = new PressureContextManager(1_000_000, undefined, undefined, undefined, {
+ maxMessages: 1_024,
+ })
+ const managed = await bounded.manage(msgs, makeFakeConversation())
+
+ expect(managed.length).toBeLessThan(1_024)
+ expect(() => validateToolLinkages(managed)).not.toThrow()
+ })
+})
diff --git a/mcp-host/src/llm/__tests__/registry.test.ts b/mcp-host/src/llm/__tests__/registry.test.ts
index 2be3ad7b6..ffe6ab5d4 100644
--- a/mcp-host/src/llm/__tests__/registry.test.ts
+++ b/mcp-host/src/llm/__tests__/registry.test.ts
@@ -6,6 +6,8 @@
* guard in `isLlmProvider` (§1), and the factory fail-safe (§5.7).
*/
import { describe, expect, it, vi } from 'vitest'
+import { LIMITS as GROK_LIMITS } from '@clerum/grok-provider-attempt-contract'
+import { LIMITS as CODEX_LIMITS } from '@clerum/llm-provider-attempt-contract'
import { apiKeysFromEnv, createLLMProvider } from '../index'
import { makeProvider } from '../registry'
import { ALL_PROVIDERS, descriptorFor, isLlmProvider, primarySlot } from '../registryCore'
@@ -248,3 +250,14 @@ describe('codex-subscription zero-slot broker', () => {
delete process.env.MCP_HOST_CODEX_SUBSCRIPTION_ENABLED
})
})
+
+describe('provider registry — contract message bound (#731)', () => {
+ it('T-R2-3b subscription providers carry their attempt contract maxMessages', () => {
+ // Read from the contracts, never written as a literal: origin/main still ships
+ // 128 where dev ships 1024, and a literal would drift from whichever it copied.
+ expect(descriptorFor('codex-subscription').maxMessages).toBe(CODEX_LIMITS.maxMessages)
+ expect(descriptorFor('grok-subscription').maxMessages).toBe(GROK_LIMITS.maxMessages)
+ // A provider without an attempt contract carries no message bound.
+ expect(descriptorFor('openai').maxMessages).toBeUndefined()
+ })
+})