Skip to content

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

TypeScript

fix(mcp-host): gate history pre-prune at the window-derived threshold (#731)

Alfredo Lopez06f948538a3a
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index ed84de275..2f051a291 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.387",
+ "version": "0.1.388",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.387",
+ "version": "0.1.388",
"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 8a5bc9f79..b6c7576a5 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.387",
+ "version": "0.1.388",
"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/taskExecutor.ts b/mcp-host/src/agent/taskExecutor.ts
index 1dc576796..d2a9c757e 100644
--- a/mcp-host/src/agent/taskExecutor.ts
+++ b/mcp-host/src/agent/taskExecutor.ts
@@ -20,7 +20,10 @@ import { deriveAutoTitle } from '../core/conversation/sessionTitle'
import { LlmError, LlmErrorCode } from '../core/errors'
import { ApprovalController } from '../core/extensions/approvalController'
import type { ApprovalConfig } from '../core/extensions/approvalTypes'
-import { PressureContextManager } from '../core/extensions/contextManager'
+import {
+ COMPACTION_PRESSURE_THRESHOLD,
+ PressureContextManager,
+} from '../core/extensions/contextManager'
import {
UnifiedApprovalGateController,
buildConnectRequiredApproval,
@@ -1015,18 +1018,27 @@ export class TaskExecutor {
private async runAgentLoop(): Promise<LoopResult> {
const historyStart = Date.now()
let messages = this.deps.conversationManager.buildMessageHistory(this.conversation!)
- messages = compactConversation(messages, undefined, undefined, this.getOrCreateTokenCounter(), {
- enabled: appConfig.compactionPrePruneEnabled,
- options: {
- protectedTailTurns: appConfig.compactionPrePruneProtectedTailTurns,
- summaryThresholdTokens: appConfig.compactionPrePruneSummaryTokens,
- maxArgsBytes: appConfig.compactionPrePruneMaxArgsBytes,
- dedupEnabled: appConfig.compactionPrePruneDedup,
- oneLineSummariesEnabled: appConfig.compactionPrePruneOneLine,
- jsonSafeTruncateEnabled: appConfig.compactionPrePruneJsonTruncate,
- stripMediaEnabled: appConfig.compactionPrePruneStripMedia,
- },
- })
+ // #731: the history is compacted at the same share of the model's window
+ // at which the context manager starts compacting, not at a fixed count.
+ const compactionThreshold = Math.floor(COMPACTION_PRESSURE_THRESHOLD * this.contextMaxTokens())
+ messages = compactConversation(
+ messages,
+ undefined,
+ compactionThreshold,
+ this.getOrCreateTokenCounter(),
+ {
+ enabled: appConfig.compactionPrePruneEnabled,
+ options: {
+ protectedTailTurns: appConfig.compactionPrePruneProtectedTailTurns,
+ summaryThresholdTokens: appConfig.compactionPrePruneSummaryTokens,
+ maxArgsBytes: appConfig.compactionPrePruneMaxArgsBytes,
+ dedupEnabled: appConfig.compactionPrePruneDedup,
+ oneLineSummariesEnabled: appConfig.compactionPrePruneOneLine,
+ jsonSafeTruncateEnabled: appConfig.compactionPrePruneJsonTruncate,
+ stripMediaEnabled: appConfig.compactionPrePruneStripMedia,
+ },
+ }
+ )
// History rehydration + pre-prune compaction are part of the session-load
// cost the user pays before the first model byte.
this.turnTiming?.addSessionLoadMs(Date.now() - historyStart)
diff --git a/mcp-host/src/core/conversation/compaction.ts b/mcp-host/src/core/conversation/compaction.ts
index 44f4e10f8..83431829f 100644
--- a/mcp-host/src/core/conversation/compaction.ts
+++ b/mcp-host/src/core/conversation/compaction.ts
@@ -233,25 +233,28 @@ export function compactConversation(
counter?: TokenCounter,
prePruneOpts?: { enabled: boolean; options?: PrePruneOptions }
): ChatMessage[] {
- // T1.2: optional pre-prune BEFORE the threshold check. The caller passes the
- // env-gated flag, and `CLERUM_COMPACTION_PRE_PRUNE` now defaults to `true`
- // (#731), so this runs on every call - including the ones that return early
- // below because the conversation is under the threshold. Pre-prune is a
- // no-op if it doesn't mutate anything, so cheap to leave in the call chain,
- // but it does mean `working` can differ from `messages` on that early return.
- let working = messages
- if (prePruneOpts?.enabled) {
- const result = prePrune(messages, prePruneOpts.options)
- working = result.messages
- }
-
// P.2: prefer the provider-aware counter when available. `countSync` is a
// best-effort path that never makes a network call — for Anthropic it
// returns the heuristic upper bound, for OpenAI/tiktoken it's exact after
// warmup. The caller (`runAgentLoop`) issues warmup at executor start.
- const tokenCount = counter ? counter.countSync(working) : heuristicCount(working)
- if (tokenCount < threshold) {
- return working
+ const count = (msgs: ChatMessage[]) => (counter ? counter.countSync(msgs) : heuristicCount(msgs))
+
+ // #731: under the threshold the history is returned untouched, by
+ // reference. `CLERUM_COMPACTION_PRE_PRUNE` defaults to `true`, so running
+ // pre-prune first would rewrite every history on every task, whatever the
+ // window. This mirrors `PressureContextManager.manage()`, which passes
+ // through below its threshold before pre-pruning.
+ if (count(messages) < threshold) {
+ return messages
+ }
+
+ // T1.2: pre-prune, then the turn cut only if pre-prune was not enough.
+ let working = messages
+ if (prePruneOpts?.enabled) {
+ working = prePrune(messages, prePruneOpts.options).messages
+ if (count(working) < threshold) {
+ return working
+ }
}
const systemMsgs = working.filter(m => m.role === 'system')
diff --git a/mcp-host/src/core/extensions/contextManager.ts b/mcp-host/src/core/extensions/contextManager.ts
index 4a8c6cc10..c8bf9d3f0 100644
--- a/mcp-host/src/core/extensions/contextManager.ts
+++ b/mcp-host/src/core/extensions/contextManager.ts
@@ -138,10 +138,17 @@ export interface PressureContextManagerOptions {
maxMessages?: number
}
+/**
+ * The pressure at which compaction starts. Below it `manage()` passes the
+ * history through untouched; `taskExecutor` scales it by the context window to
+ * gate history compaction at rehydration (#731).
+ */
+export const COMPACTION_PRESSURE_THRESHOLD = 0.8
+
type PressureTier = 'passthrough' | 'workspace' | 'summarize' | 'truncate'
function tierFor(pressure: number): PressureTier {
- if (pressure < 0.8) return 'passthrough'
+ if (pressure < COMPACTION_PRESSURE_THRESHOLD) return 'passthrough'
if (pressure < 0.85) return 'workspace'
if (pressure < 0.95) return 'summarize'
return 'truncate'
@@ -320,7 +327,7 @@ export class PressureContextManager implements ContextManager {
const tools = options?.tools ?? []
const pressure = await this.computePressure(messages, tools)
- if (pressure < 0.8) {
+ if (pressure < COMPACTION_PRESSURE_THRESHOLD) {
return messages // Passthrough — does NOT touch compactionState (no attempt made).
}
@@ -341,7 +348,7 @@ export class PressureContextManager implements ContextManager {
}
this.emitPrePruneEvent(conversation.id, result)
const newPressure = await this.computePressure(working, tools)
- if (newPressure < 0.8) {
+ if (newPressure < COMPACTION_PRESSURE_THRESHOLD) {
return working // pre-prune alone was enough — skip the tier.
}
}