Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(mcp-host): require pre-prune to wait for the window-derived threshold (#731)
Alfredo Lopez72af06c08144
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 400669dc5..ed84de275 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.386",+ "version": "0.1.387","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.386",+ "version": "0.1.387","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex b27bfac6c..8a5bc9f79 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.386",+ "version": "0.1.387","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.tsindex 0bcb99ea7..e469d0c2f 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.test.ts@@ -2339,3 +2339,75 @@ describe('TaskExecutor context manager message bound (#731)', () => {expect(managed).toBe(msgs)})})++describe('TaskExecutor history compaction threshold follows the context window (#731)', () => {+ // ~100k tokens by either count (tiktoken reads one token per `word `, the+ // byte heuristic 125k): above the 80k literal default, below 0.8 of a 1M+ // window. The old tool result sits outside the protected tail of three turns.+ const OLD_RESULT = 'word '.repeat(100_000)+ function prunableHistory(): ChatMessage[] {+ return [+ { role: 'system', content: 'sys' },+ { role: 'user', content: 'q0' },+ {+ role: 'assistant',+ content: '',+ tool_calls: [{ id: 'tc0', name: 'fetch', arguments: { url: 'https://example.test' } }],+ },+ { role: 'tool', tool_call_id: 'tc0', name: 'fetch', content: OLD_RESULT },+ { role: 'user', content: 'q1' },+ { role: 'assistant', content: 'a1' },+ { role: 'user', content: 'q2' },+ { role: 'assistant', content: 'a2' },+ { role: 'user', content: 'q3' },+ { role: 'assistant', content: 'a3' },+ ]+ }++ // The config mock of this file carries no pre-prune fields; these are the+ // defaults `config.ts` deploys (T-B pins the master switch).+ const DEPLOYED_PRE_PRUNE = {+ compactionPrePruneEnabled: true,+ compactionPrePruneDedup: true,+ compactionPrePruneOneLine: true,+ compactionPrePruneJsonTruncate: true,+ compactionPrePruneStripMedia: true,+ compactionPrePruneMaxArgsBytes: 4096,+ compactionPrePruneSummaryTokens: 200,+ compactionPrePruneProtectedTailTurns: 3,+ }+ const mutableConfig = appConfig as unknown as Record<string, unknown>+ let previousConfig: Record<string, unknown>+ beforeEach(() => {+ previousConfig = Object.fromEntries(+ Object.keys(DEPLOYED_PRE_PRUNE).map(key => [key, mutableConfig[key]])+ )+ Object.assign(mutableConfig, DEPLOYED_PRE_PRUNE)+ })+ afterEach(() => {+ Object.assign(mutableConfig, previousConfig)+ })++ // Runs a task whose rehydrated history is `prunableHistory()` and returns the+ // old tool result as it reached the loop.+ async function oldResultReachingLoop(contextWindowTokens: number): Promise<string | undefined> {+ vi.mocked(runToolUseLoop).mockResolvedValueOnce({ type: 'response', content: 'ok' } as any)+ const conversationManager = new ConversationManager()+ vi.spyOn(conversationManager, 'buildMessageHistory').mockReturnValue(prunableHistory())+ const deps = createDeps({ conversationManager, contextWindowTokens })+ 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[1]?.find(m => m.role === 'tool')?.content+ }++ it('T-R2-4b a 1M window leaves a 100k-token history untouched', async () => {+ expect(await oldResultReachingLoop(1_000_000)).toBe(OLD_RESULT)+ })++ it('T-R2-4c witness: a 100k window pre-prunes the same history', async () => {+ const reached = await oldResultReachingLoop(100_000)+ expect(reached).toBeDefined()+ expect(reached!.length).toBeLessThan(OLD_RESULT.length / 10)+ })+})diff --git a/mcp-host/src/core/conversation/__tests__/compaction.test.ts b/mcp-host/src/core/conversation/__tests__/compaction.test.tsindex bf88ca66b..5a8ac51b4 100644--- a/mcp-host/src/core/conversation/__tests__/compaction.test.ts+++ b/mcp-host/src/core/conversation/__tests__/compaction.test.ts@@ -108,3 +108,44 @@ describe('compactConversation', () => {expect(() => validateToolLinkages(compacted)).not.toThrow()})})++describe('compactConversation — pre-prune is gated at the threshold (#731)', () => {+ // One old turn whose tool result is far over the pre-prune summary threshold+ // (200 tokens), then three short turns the pre-prune protects. Four turns+ // stay under the default `maxTurns` of 5, so only the pre-prune can change it.+ const OLD_RESULT = 'x'.repeat(40_000)+ function prunableHistory(): ChatMessage[] {+ return [+ { role: 'system', content: 'sys' },+ { role: 'user', content: 'q0' },+ {+ role: 'assistant',+ content: '',+ tool_calls: [{ id: 'tc0', name: 'fetch', arguments: { url: 'https://example.test' } }],+ },+ { role: 'tool', tool_call_id: 'tc0', name: 'fetch', content: OLD_RESULT },+ { role: 'user', content: 'q1' },+ { role: 'assistant', content: 'a1' },+ { role: 'user', content: 'q2' },+ { role: 'assistant', content: 'a2' },+ { role: 'user', content: 'q3' },+ { role: 'assistant', content: 'a3' },+ ]+ }+ const prePruneOn = { enabled: true }++ it('T-R2-4a returns the input untouched below the threshold even with pre-prune on', () => {+ const messages = prunableHistory()+ expect(compactConversation(messages, undefined, 1_000_000, undefined, prePruneOn)).toBe(+ messages+ )+ })++ it('T-R2-4a witness: the same history is pre-pruned above the threshold', () => {+ const messages = prunableHistory()+ const compacted = compactConversation(messages, undefined, 1_000, undefined, prePruneOn)+ const oldResult = compacted.find(m => m.role === 'tool')+ expect(oldResult?.content).not.toBe(OLD_RESULT)+ expect(oldResult!.content.length).toBeLessThan(OLD_RESULT.length / 10)+ })+})