Skip to content

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

TypeScript

fix(mcp-host): measure rehydrated history like the context manager (#739)

Alfredo Lopez28bad7276b37
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index b131b02f9..0a6553daf 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.413",
+ "version": "0.1.414",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.413",
+ "version": "0.1.414",
"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 2440156cd..c84fa6ae0 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.413",
+ "version": "0.1.414",
"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 d9ab0fe70..fb75ab158 100644
--- a/mcp-host/src/agent/taskExecutor.ts
+++ b/mcp-host/src/agent/taskExecutor.ts
@@ -23,6 +23,7 @@ import type { ApprovalConfig } from '../core/extensions/approvalTypes'
import {
COMPACTION_PRESSURE_THRESHOLD,
PressureContextManager,
+ tierDecisionTokens,
} from '../core/extensions/contextManager'
import {
UnifiedApprovalGateController,
@@ -1045,13 +1046,15 @@ export class TaskExecutor {
const historyStart = Date.now()
let messages = this.deps.conversationManager.buildMessageHistory(this.conversation!)
// #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.
+ // at which the context manager starts compacting, not at a fixed count,
+ // and measured the way the manager measures it for that decision (#739).
const compactionThreshold = Math.floor(COMPACTION_PRESSURE_THRESHOLD * this.contextMaxTokens())
- messages = compactConversation(
+ const tokenCounter = this.getOrCreateTokenCounter()
+ messages = await compactConversation(
messages,
undefined,
compactionThreshold,
- this.getOrCreateTokenCounter(),
+ msgs => tierDecisionTokens(msgs, [], tokenCounter, appConfig.tokenizerDryrun),
{
enabled: appConfig.compactionPrePruneEnabled,
options: {
diff --git a/mcp-host/src/core/conversation/__tests__/compaction.test.ts b/mcp-host/src/core/conversation/__tests__/compaction.test.ts
index 5a8ac51b4..d34f8cde4 100644
--- a/mcp-host/src/core/conversation/__tests__/compaction.test.ts
+++ b/mcp-host/src/core/conversation/__tests__/compaction.test.ts
@@ -30,7 +30,7 @@ describe('estimateTokens', () => {
})
describe('compactConversation', () => {
- it('should keep system message + last N turns (Risk 5.5)', () => {
+ it('should keep system message + last N turns (Risk 5.5)', async () => {
const messages: ChatMessage[] = [
{ role: 'system', content: 'You are an assistant.' },
{ role: 'user', content: 'Turn 1' },
@@ -44,7 +44,7 @@ describe('compactConversation', () => {
]
// Force compaction by setting threshold to 0
- const compacted = compactConversation(messages, 2, 0)
+ const compacted = await compactConversation(messages, 2, 0)
// System message preserved
expect(compacted[0]).toEqual({
@@ -60,7 +60,7 @@ describe('compactConversation', () => {
expect(compacted[4].content).toBe('Response 4')
})
- it('should not compact when under threshold', () => {
+ it('should not compact when under threshold', async () => {
const messages: ChatMessage[] = [
{ role: 'system', content: 'System' },
{ role: 'user', content: 'Hello' },
@@ -68,11 +68,11 @@ describe('compactConversation', () => {
]
// Default threshold is 80000 — these messages are tiny
- const result = compactConversation(messages)
+ const result = await compactConversation(messages)
expect(result).toEqual(messages)
})
- it("T1.3: never archives the last user message ('Active Task' anchor)", () => {
+ it("T1.3: never archives the last user message ('Active Task' anchor)", async () => {
const messages: ChatMessage[] = [
{ role: 'system', content: 'System' },
{ role: 'user', content: 'turn 1' },
@@ -83,13 +83,13 @@ describe('compactConversation', () => {
{ role: 'assistant', content: 'working' },
]
- const compacted = compactConversation(messages, 1, 0)
+ const compacted = await compactConversation(messages, 1, 0)
// Even with maxTurns=1 + threshold=0 (force aggressive compaction), the
// anchor pulls the cut back so the active user task survives.
expect(compacted.some(m => m.content === 'ACTIVE TASK')).toBe(true)
})
- it('T1.3: kept set never violates tool linkages after compaction', () => {
+ it('T1.3: kept set never violates tool linkages after compaction', async () => {
const messages: ChatMessage[] = [
{ role: 'system', content: 'System' },
{ role: 'user', content: 'old' },
@@ -104,7 +104,7 @@ describe('compactConversation', () => {
{ role: 'assistant', content: 'done' },
]
- const compacted = compactConversation(messages, 1, 0)
+ const compacted = await compactConversation(messages, 1, 0)
expect(() => validateToolLinkages(compacted)).not.toThrow()
})
})
@@ -134,16 +134,16 @@ describe('compactConversation — pre-prune is gated at the threshold (#731)', (
}
const prePruneOn = { enabled: true }
- it('T-R2-4a returns the input untouched below the threshold even with pre-prune on', () => {
+ it('T-R2-4a returns the input untouched below the threshold even with pre-prune on', async () => {
const messages = prunableHistory()
- expect(compactConversation(messages, undefined, 1_000_000, undefined, prePruneOn)).toBe(
+ expect(await compactConversation(messages, undefined, 1_000_000, undefined, prePruneOn)).toBe(
messages
)
})
- it('T-R2-4a witness: the same history is pre-pruned above the threshold', () => {
+ it('T-R2-4a witness: the same history is pre-pruned above the threshold', async () => {
const messages = prunableHistory()
- const compacted = compactConversation(messages, undefined, 1_000, undefined, prePruneOn)
+ const compacted = await 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)
diff --git a/mcp-host/src/core/conversation/__tests__/compactionTranscriptInvariant.test.ts b/mcp-host/src/core/conversation/__tests__/compactionTranscriptInvariant.test.ts
index 71c399815..a078cade7 100644
--- a/mcp-host/src/core/conversation/__tests__/compactionTranscriptInvariant.test.ts
+++ b/mcp-host/src/core/conversation/__tests__/compactionTranscriptInvariant.test.ts
@@ -51,7 +51,7 @@ describe('compaction ↔ durable transcript invariant (FU2, spec §8 "does not a
expect(messages).toHaveLength(12) // 6 turns × (user + assistant)
// Force compaction (threshold 0) keeping only the last 2 turns.
- const compacted = compactConversation(messages, 2, 0)
+ const compacted = await compactConversation(messages, 2, 0)
// Compaction actually ran on the context window.
expect(compacted.length).toBeLessThan(messages.length)
@@ -66,7 +66,7 @@ describe('compaction ↔ durable transcript invariant (FU2, spec §8 "does not a
const { manager, conv } = await buildConversationWithTurns(6)
const fullBefore = manager.buildMessageHistory(conv)
- compactConversation(fullBefore, 2, 0)
+ await compactConversation(fullBefore, 2, 0)
// buildMessageHistory re-derives from conversation.turns each call, so the
// post-compaction rebuild is identical — proving compaction did not erode
@@ -81,7 +81,7 @@ describe('compaction ↔ durable transcript invariant (FU2, spec §8 "does not a
const messages = manager.buildMessageHistory(conv)
const inputSnapshot = messages.map(m => ({ ...m }))
- const compacted = compactConversation(messages, 2, 0)
+ const compacted = await compactConversation(messages, 2, 0)
expect(compacted).not.toBe(messages)
expect(messages).toEqual(inputSnapshot) // input array left intact
diff --git a/mcp-host/src/core/conversation/compaction.ts b/mcp-host/src/core/conversation/compaction.ts
index 83431829f..b6ae581f2 100644
--- a/mcp-host/src/core/conversation/compaction.ts
+++ b/mcp-host/src/core/conversation/compaction.ts
@@ -1,6 +1,5 @@
import { type PrePruneOptions, prePrune } from '../extensions/prePrune'
import { heuristicCount } from '../tokenizer/heuristic'
-import type { TokenCounter } from '../tokenizer/tokenCounter'
import { ChatMessage } from '../types'
/**
@@ -226,25 +225,23 @@ export function applyAlignedCut(
* 3. Apply the T1.3 boundary aligner so cuts never split tool pairs and
* never archive the last user message.
*/
-export function compactConversation(
+export async function compactConversation(
messages: ChatMessage[],
maxTurns: number = 5,
threshold: number = 80000,
- counter?: TokenCounter,
+ measure: (msgs: ChatMessage[]) => Promise<number> = async msgs => heuristicCount(msgs),
prePruneOpts?: { enabled: boolean; options?: PrePruneOptions }
-): ChatMessage[] {
- // 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 count = (msgs: ChatMessage[]) => (counter ? counter.countSync(msgs) : heuristicCount(msgs))
+): Promise<ChatMessage[]> {
+ // #739: the caller (`runAgentLoop`) passes the measure behind
+ // `PressureContextManager`'s tier decision (`tierDecisionTokens`), so the
+ // threshold it scales from that manager's means the same history here.
// #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) {
+ if ((await measure(messages)) < threshold) {
return messages
}
@@ -252,7 +249,7 @@ export function compactConversation(
let working = messages
if (prePruneOpts?.enabled) {
working = prePrune(messages, prePruneOpts.options).messages
- if (count(working) < threshold) {
+ if ((await measure(working)) < threshold) {
return working
}
}
diff --git a/mcp-host/src/core/extensions/contextManager.ts b/mcp-host/src/core/extensions/contextManager.ts
index 191f85202..452d58719 100644
--- a/mcp-host/src/core/extensions/contextManager.ts
+++ b/mcp-host/src/core/extensions/contextManager.ts
@@ -145,6 +145,29 @@ export interface PressureContextManagerOptions {
*/
export const COMPACTION_PRESSURE_THRESHOLD = 0.8
+/**
+ * The token count behind `PressureContextManager`'s tier decision. Without a
+ * counter, or in the dry run, the byte heuristic decides; otherwise the
+ * counter does. `taskExecutor` measures the rehydrated history with this same
+ * function, so a history the manager passes through is never compacted at
+ * rehydration (#739).
+ *
+ * The `lastObservedInputTokens` shortcut (Hermes `update_from_response`) is
+ * intentionally NOT applied: the manager calls this once per loop iteration,
+ * so the per-decision count is affordable, and skipping `count()` would risk
+ * under-counting messages added since the last response. T2.2 (prompt cache)
+ * revisits this with a per-iteration shape diff.
+ */
+export async function tierDecisionTokens(
+ messages: ChatMessage[],
+ tools: ToolDefinition[],
+ tokenCounter: TokenCounter | undefined,
+ dryRun: boolean
+): Promise<number> {
+ if (!tokenCounter || dryRun) return estimateTokens(messages) + heuristicCountTools(tools)
+ return tokenCounter.count(messages, tools)
+}
+
type PressureTier = 'passthrough' | 'workspace' | 'summarize' | 'truncate'
function tierFor(pressure: number): PressureTier {
@@ -507,12 +530,12 @@ export class PressureContextManager implements ContextManager {
}
/**
- * Resolve the token pressure ratio according to the dry-run gate. Three branches:
+ * Resolve the token pressure ratio from `tierDecisionTokens`. Three branches:
* - No counter: legacy heuristic (preserves pre-P.2 behavior bit-for-bit).
- * - dryRun=true: compute BOTH numbers; the heuristic decides the tier; the
- * delta and any tier mismatch are emitted as metrics for the bake-week.
- * - dryRun=false: the counter (with `lastObservedInputTokens` shortcut)
- * drives the decision directly.
+ * - dryRun=true: the heuristic decides the tier; the counter's number is
+ * computed too, and the delta and any tier mismatch are emitted as
+ * metrics for the bake-week.
+ * - dryRun=false: the counter drives the decision directly.
* Every branch counts `tools` with the messages: both travel in the request
* the provider caps (#731).
*/
@@ -520,44 +543,37 @@ export class PressureContextManager implements ContextManager {
messages: ChatMessage[],
tools: ToolDefinition[]
): Promise<number> {
- if (!this.tokenCounter) {
- return (estimateTokens(messages) + heuristicCountTools(tools)) / this.maxTokens
+ const decided = await tierDecisionTokens(messages, tools, this.tokenCounter, this.dryRun)
+ if (this.tokenCounter && this.dryRun) {
+ await this.observeDryrunDelta(this.tokenCounter, messages, tools, decided)
}
- if (this.dryRun) {
- const heuristic = estimateTokens(messages) + heuristicCountTools(tools)
- let real: number
- try {
- real = await this.measureWithCounter(messages, tools)
- } catch (err) {
- logger.warn({ component: 'ContextManager', err }, 'dryrun counter failed; using heuristic')
- return heuristic / this.maxTokens
- }
- const heuristicTier = tierFor(heuristic / this.maxTokens)
- const realTier = tierFor(real / this.maxTokens)
- const delta = heuristic > 0 ? real / heuristic : 0
- tokenizerDryrunDelta.observe(
- { provider: this.tokenCounter.providerName, tier_chosen: heuristicTier },
- delta
- )
- if (heuristicTier !== realTier) {
- tokenizerDryrunTierMismatchTotal.inc({ from: heuristicTier, to: realTier })
- }
- return heuristic / this.maxTokens
- }
- return (await this.measureWithCounter(messages, tools)) / this.maxTokens
+ return decided / this.maxTokens
}
- private async measureWithCounter(
+ /** Dry-run metrics: how far the counter's number is from the heuristic's. */
+ private async observeDryrunDelta(
+ tokenCounter: TokenCounter,
messages: ChatMessage[],
- tools: ToolDefinition[]
- ): Promise<number> {
- // The `lastObservedInputTokens` shortcut (Hermes `update_from_response`)
- // is intentionally NOT applied here in the first PR: the call site runs
- // once per loop iteration so the per-decision call is affordable, and
- // skipping `count()` would risk under-counting messages added since the
- // last response. T2.2 (prompt cache) revisits this with a per-iteration
- // shape diff.
- return this.tokenCounter!.count(messages, tools)
+ tools: ToolDefinition[],
+ heuristic: number
+ ): Promise<void> {
+ let real: number
+ try {
+ real = await tokenCounter.count(messages, tools)
+ } catch (err) {
+ logger.warn({ component: 'ContextManager', err }, 'dryrun counter failed; using heuristic')
+ return
+ }
+ const heuristicTier = tierFor(heuristic / this.maxTokens)
+ const realTier = tierFor(real / this.maxTokens)
+ const delta = heuristic > 0 ? real / heuristic : 0
+ tokenizerDryrunDelta.observe(
+ { provider: tokenCounter.providerName, tier_chosen: heuristicTier },
+ delta
+ )
+ if (heuristicTier !== realTier) {
+ tokenizerDryrunTierMismatchTotal.inc({ from: heuristicTier, to: realTier })
+ }
}
/**