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 context pressure to count the system prompt (#739)

Alfredo Lopezee75f55579ca
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 0184dca7b..3098efa67 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.420",
+ "version": "0.1.421",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.420",
+ "version": "0.1.421",
"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 b9296a15a..bc3e9f28a 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.420",
+ "version": "0.1.421",
"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 85a7f3933..d0f66dbc4 100644
--- a/mcp-host/src/agent/__tests__/taskExecutor.test.ts
+++ b/mcp-host/src/agent/__tests__/taskExecutor.test.ts
@@ -2646,3 +2646,57 @@ describe('TaskExecutor history compaction threshold follows the context window (
})
})
})
+
+// The loop's context manager counts the system prompt the next request carries
+// (R9-14, L-6). It reaches the manager through `LoopConfig.systemPrompt`; both
+// prompt paths must set it, daily-log snapshot included.
+describe('TaskExecutor hands the system prompt to the loop (R9-14)', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ })
+
+ async function loopSystemPrompt(deps: TaskExecutorDeps): Promise<unknown> {
+ vi.mocked(runToolUseLoop).mockResolvedValueOnce({ type: 'response', content: 'ok' } as any)
+ await new TaskExecutor(createTask('hi'), deps).run()
+ const call = vi.mocked(runToolUseLoop).mock.calls.at(-1)
+ if (!call) throw new Error('Expected runToolUseLoop to be called')
+ return call[0].systemPrompt
+ }
+
+ it('T-R9-14f the legacy path passes the assembled identity with its daily logs', async () => {
+ const workspaceService = {
+ assembleSystemPrompt: vi.fn(async () => 'IDENTITY\n\n## Daily Log\nDAILY-LOG-ENTRY'),
+ } as any
+ const systemPrompt = await loopSystemPrompt(createDeps({ workspaceService }))
+
+ expect(workspaceService.assembleSystemPrompt).toHaveBeenCalledTimes(1)
+ expect(systemPrompt).toEqual(expect.stringContaining('DAILY-LOG-ENTRY'))
+ expect(systemPrompt).toEqual(expect.stringContaining('powered by the test-model model'))
+ })
+
+ it('T-R9-14g the prompt-cache path passes both tiers with the daily-log snapshot', async () => {
+ const previous = appConfig.promptCacheEnabled
+ appConfig.promptCacheEnabled = true
+ try {
+ const workspaceService = {
+ readIdentityFiles: vi.fn(async () => ({
+ identity: 'IDENTITY-FILE',
+ soul: '',
+ agents: '',
+ user: '',
+ })),
+ snapshotDailyLogs: vi.fn(async () => 'DAILY-SNAPSHOT-ENTRY'),
+ } as any
+ const systemPrompt = await loopSystemPrompt(
+ createDeps({ workspaceService, promptCache: new PromptCache() })
+ )
+
+ // Witness: the cache path ran, so the prompt below came from its parts.
+ expect(workspaceService.snapshotDailyLogs).toHaveBeenCalledTimes(1)
+ expect(systemPrompt).toEqual(expect.stringContaining('IDENTITY-FILE'))
+ expect(systemPrompt).toEqual(expect.stringContaining('DAILY-SNAPSHOT-ENTRY'))
+ } finally {
+ appConfig.promptCacheEnabled = previous
+ }
+ })
+})
diff --git a/mcp-host/src/core/extensions/__tests__/contextManager.test.ts b/mcp-host/src/core/extensions/__tests__/contextManager.test.ts
index 1ad83bba2..7549c9b95 100644
--- a/mcp-host/src/core/extensions/__tests__/contextManager.test.ts
+++ b/mcp-host/src/core/extensions/__tests__/contextManager.test.ts
@@ -300,6 +300,19 @@ describe('InLoopContextManager', () => {
const compacted = manager.manage(msgs, makeFakeConversation(), { tools })
expect(compacted.filter(m => m.role === 'user')).toHaveLength(3)
})
+
+ it('T-R9-14d counts the system prompt against its threshold (R9-14)', () => {
+ const msgs = generateMessages(10)
+ const systemPrompt = '## Daily Log (frozen at session start)\n' + 'entry '.repeat(400)
+ // One token above the messages alone: the two outcomes below can only
+ // differ by the system prompt term.
+ const manager = new InLoopContextManager(estimateTokens(msgs) + 1, 3)
+
+ expect(manager.manage(msgs, makeFakeConversation())).toBe(msgs)
+ const compacted = manager.manage(msgs, makeFakeConversation(), { systemPrompt })
+ expect(compacted.filter(m => m.role === 'user')).toHaveLength(3)
+ expect(compacted.at(-1)?.content).toContain('Response for turn 10')
+ })
})
describe('splitTurns', () => {
diff --git a/mcp-host/src/core/orchestration/__tests__/manageMessagesForIteration.test.ts b/mcp-host/src/core/orchestration/__tests__/manageMessagesForIteration.test.ts
index d7406439b..a8ca84f8f 100644
--- a/mcp-host/src/core/orchestration/__tests__/manageMessagesForIteration.test.ts
+++ b/mcp-host/src/core/orchestration/__tests__/manageMessagesForIteration.test.ts
@@ -42,4 +42,39 @@ describe('manageMessagesForIteration', () => {
expect(manage).toHaveBeenCalledTimes(1)
expect(manage).toHaveBeenCalledWith(messages, conversation, { tools: definitions })
})
+
+ it('T-R9-14e hands the loop system prompt to the context manager (R9-14)', async () => {
+ // The system prompt is not in `messages` (the reasoning port prepends it or
+ // ships it out of band), yet it travels in the same capped request.
+ const toolRegistry: ToolRegistry = {
+ get: () => null,
+ listDefinitions: () => [],
+ 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 },
+ })
+ config.systemPrompt = 'identity\n\n## Daily Log (frozen at session start)\nentry'
+ 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: [],
+ systemPrompt: 'identity\n\n## Daily Log (frozen at session start)\nentry',
+ })
+ })
})
diff --git a/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts b/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
index 50fe5e3e5..e97f85bde 100644
--- a/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
+++ b/mcp-host/src/core/tokenizer/__tests__/contextManagerDryRun.test.ts
@@ -194,3 +194,73 @@ describe('PressureContextManager pressure includes the tool schemas', () => {
expect(counter.count).toHaveBeenCalledWith(msgs, tools)
})
})
+
+// The system prompt travels in the same request as the messages and the tools:
+// identity files, the daily-log snapshot frozen at session start and the tool
+// guidance. It is not part of `messages`, so the gauge must add it in every
+// branch of `computePressure`, as it adds the tools (R9-14, L-6).
+describe('PressureContextManager pressure includes the system prompt', () => {
+ function turns(count: number): ChatMessage[] {
+ const msgs: ChatMessage[] = []
+ for (let i = 1; i <= count; i++) {
+ msgs.push({ role: 'user', content: `question ${i} a b c d e f` })
+ msgs.push({ role: 'assistant', content: `answer ${i} g h i j k l` })
+ }
+ return msgs
+ }
+ const asSystemMessage = (content: string): ChatMessage => ({ role: 'system', content })
+
+ // Messages at 0.7 of the window; the system prompt alone is about one window.
+ function setup() {
+ const msgs = turns(10)
+ const maxTokens = Math.ceil(heuristicCount(msgs) / 0.7)
+ const systemPrompt = '## Daily Log (frozen at session start)\n' + 'entry '.repeat(maxTokens)
+ return { msgs, maxTokens, systemPrompt }
+ }
+
+ it('T-R9-14a messages at 0.7 plus the system prompt over 0.8 are not passed through', async () => {
+ const { msgs, maxTokens, systemPrompt } = setup()
+ // Witness the arithmetic, so the two outcomes below can only differ by the
+ // system prompt term.
+ expect(heuristicCount(msgs) / maxTokens).toBeLessThan(0.8)
+ expect(heuristicCount([asSystemMessage(systemPrompt), ...msgs]) / maxTokens).toBeGreaterThan(
+ 0.95
+ )
+ const manager = new PressureContextManager(maxTokens)
+
+ expect(await manager.manage(msgs, makeFakeConversation())).toBe(msgs)
+ const result = await manager.manage(msgs, makeFakeConversation(), { systemPrompt })
+
+ // The emergency tier ran: the last three turns remain, the newest intact.
+ expect(result.filter(m => m.role === 'user')).toHaveLength(3)
+ expect(result.at(-1)?.content).toBe(msgs.at(-1)?.content)
+ })
+
+ it('T-R9-14b dry-run decides with the system prompt and hands it to the counter', async () => {
+ const { msgs, maxTokens, systemPrompt } = setup()
+ 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(), { systemPrompt })
+
+ expect(result.filter(m => m.role === 'user')).toHaveLength(3)
+ expect(counter.count).toHaveBeenCalledWith([asSystemMessage(systemPrompt), ...msgs], [])
+ })
+
+ it('T-R9-14c the counter-driven path hands the system prompt to the counter', async () => {
+ const { msgs, systemPrompt } = setup()
+ const counter = makeCounter(0)
+ const manager = new PressureContextManager(1_000, undefined, undefined, counter, {
+ dryRun: false,
+ })
+
+ const result = await manager.manage(msgs, makeFakeConversation(), { systemPrompt })
+
+ // The counter reports 0, so its number decided: passthrough by reference.
+ expect(result).toBe(msgs)
+ expect(counter.count).toHaveBeenCalledTimes(1)
+ expect(counter.count).toHaveBeenCalledWith([asSystemMessage(systemPrompt), ...msgs], [])
+ })
+})