Skip to content

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

TypeScript

fix(mcp-host): count the system prompt in context pressure (#739)

Alfredo Lopez4e9bd40ea5fb
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 3098efa67..5d92d2349 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.421",
+ "version": "0.1.422",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.421",
+ "version": "0.1.422",
"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 bc3e9f28a..efb1c6de2 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.421",
+ "version": "0.1.422",
"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 fb75ab158..40021557d 100644
--- a/mcp-host/src/agent/taskExecutor.ts
+++ b/mcp-host/src/agent/taskExecutor.ts
@@ -1469,9 +1469,18 @@ export class TaskExecutor {
this.contextMaxTokens()
)
const parts = await this.maybeGetOrBuildParts(registry.listDefinitions())
+ const identity = parts ? undefined : await this.buildSystemIdentity(llmPort)
const reasoning = parts
? reasoningFactory.createWithParts(parts)
- : reasoningFactory.create(await this.buildSystemIdentity(llmPort))
+ : reasoningFactory.create(identity)
+ // R9-14 — the text of the system prompt `reasoning` sends, for the context
+ // manager to count. The cache path joins its tiers as `LlmPortAdapter`
+ // does; the legacy path runs the builder `DefaultReasoningPort` runs, over
+ // the registry's full tool list, a superset of what the loop presents.
+ const systemPrompt = parts
+ ? [parts.stable, parts.context].filter(s => s.length > 0).join('\n\n')
+ : new DefaultPromptBuilder().buildSystemPrompt(registry.listDefinitions(), identity, metadata)
+ .content
const contextManager = new PressureContextManager(
this.contextMaxTokens(),
@@ -1530,6 +1539,7 @@ export class TaskExecutor {
toolProgressInterval: appConfig.nativeTool.toolProgressInterval,
})
loopConfig.abortSignal = this.abortController.signal
+ loopConfig.systemPrompt = systemPrompt
loopConfig.imageSourceIdentity = this.providerChainRequiresImageSourceIdentity()
loopConfig.onAttachments = attachments =>
mergeCollectedAttachments(this.completedAttachments, attachments)
diff --git a/mcp-host/src/core/extensions/contextManager.ts b/mcp-host/src/core/extensions/contextManager.ts
index 452d58719..fff24f0d8 100644
--- a/mcp-host/src/core/extensions/contextManager.ts
+++ b/mcp-host/src/core/extensions/contextManager.ts
@@ -168,6 +168,19 @@ export async function tierDecisionTokens(
return tokenCounter.count(messages, tools)
}
+/**
+ * The messages a pressure count measures: the loop's messages, preceded by the
+ * system prompt as the one system message it becomes on the wire (R9-14). The
+ * prompt is counted only; it never enters the history the manager returns.
+ */
+function withSystemPrompt(
+ messages: ChatMessage[],
+ systemPrompt: string | undefined
+): ChatMessage[] {
+ if (systemPrompt === undefined) return messages
+ return [{ role: 'system', content: systemPrompt }, ...messages]
+}
+
type PressureTier = 'passthrough' | 'workspace' | 'summarize' | 'truncate'
function tierFor(pressure: number): PressureTier {
@@ -348,7 +361,8 @@ export class PressureContextManager implements ContextManager {
}
const tools = options?.tools ?? []
- const pressure = await this.computePressure(messages, tools)
+ const systemPrompt = options?.systemPrompt
+ const pressure = await this.computePressure(messages, tools, systemPrompt)
if (pressure < COMPACTION_PRESSURE_THRESHOLD) {
return messages // Passthrough — does NOT touch compactionState (no attempt made).
@@ -370,7 +384,7 @@ export class PressureContextManager implements ContextManager {
clerumPrePruneSavingsTokensTotal.inc(savings)
}
this.emitPrePruneEvent(conversation.id, result)
- const newPressure = await this.computePressure(working, tools)
+ const newPressure = await this.computePressure(working, tools, systemPrompt)
if (newPressure < COMPACTION_PRESSURE_THRESHOLD) {
return working // pre-prune alone was enough — skip the tier.
}
@@ -523,8 +537,12 @@ export class PressureContextManager implements ContextManager {
* contract bounds it, the message-count ratio (#731): the contract refuses
* `messages.length > maxMessages` whatever the token count.
*/
- private async computePressure(messages: ChatMessage[], tools: ToolDefinition[]): Promise<number> {
- const tokenPressure = await this.computeTokenPressure(messages, tools)
+ private async computePressure(
+ messages: ChatMessage[],
+ tools: ToolDefinition[],
+ systemPrompt: string | undefined
+ ): Promise<number> {
+ const tokenPressure = await this.computeTokenPressure(messages, tools, systemPrompt)
if (this.maxMessages === undefined) return tokenPressure
return Math.max(tokenPressure, messages.length / this.maxMessages)
}
@@ -536,16 +554,18 @@ export class PressureContextManager implements ContextManager {
* 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).
+ * Every branch counts `tools` and the system prompt with the messages: all
+ * three travel in the request the provider caps (#731, R9-14).
*/
private async computeTokenPressure(
messages: ChatMessage[],
- tools: ToolDefinition[]
+ tools: ToolDefinition[],
+ systemPrompt: string | undefined
): Promise<number> {
- const decided = await tierDecisionTokens(messages, tools, this.tokenCounter, this.dryRun)
+ const counted = withSystemPrompt(messages, systemPrompt)
+ const decided = await tierDecisionTokens(counted, tools, this.tokenCounter, this.dryRun)
if (this.tokenCounter && this.dryRun) {
- await this.observeDryrunDelta(this.tokenCounter, messages, tools, decided)
+ await this.observeDryrunDelta(this.tokenCounter, counted, tools, decided)
}
return decided / this.maxTokens
}
@@ -764,9 +784,11 @@ export class InLoopContextManager implements ContextManager {
if (conversation.pending_approval !== undefined) {
return messages
}
- // The tool schemas travel with every request, so they count against the
- // threshold exactly as in PressureContextManager.computePressure.
- const estimated = estimateTokens(messages) + heuristicCountTools(options?.tools ?? [])
+ // The tool schemas and the system prompt travel with every request, so they
+ // count against the threshold exactly as in PressureContextManager.computePressure.
+ const estimated =
+ estimateTokens(withSystemPrompt(messages, options?.systemPrompt)) +
+ heuristicCountTools(options?.tools ?? [])
if (estimated < this.thresholdTokens) {
return messages // Passthrough
diff --git a/mcp-host/src/core/interfaces.ts b/mcp-host/src/core/interfaces.ts
index 0fae23562..1091710ee 100644
--- a/mcp-host/src/core/interfaces.ts
+++ b/mcp-host/src/core/interfaces.ts
@@ -210,6 +210,14 @@ export interface ContextManageOptions {
* so the count errs high.
*/
tools?: ToolDefinition[]
+
+ /**
+ * The system prompt that travels with the next request: identity files, the
+ * daily-log snapshot and the tool guidance. It is not part of `messages` (the
+ * reasoning port prepends it or ships it out of band), yet it shares the
+ * request cap, so pressure counts it as one system message.
+ */
+ systemPrompt?: string
}
export interface ContextManager {
diff --git a/mcp-host/src/core/orchestration/loopConfig.ts b/mcp-host/src/core/orchestration/loopConfig.ts
index 14cecda49..d24740ff9 100644
--- a/mcp-host/src/core/orchestration/loopConfig.ts
+++ b/mcp-host/src/core/orchestration/loopConfig.ts
@@ -43,6 +43,14 @@ export interface LoopConfig {
contextManager: ContextManager
toolOutputProcessor: ToolOutputProcessor
+ /**
+ * The system prompt `reasoning` sends with every request, including the
+ * daily-log snapshot. The loop hands it to `contextManager` so pressure
+ * counts it (R9-14). Set by `TaskExecutor.buildLoopConfig()`; absent means
+ * the caller sends no system prompt of its own.
+ */
+ systemPrompt?: string
+
/**
* Tool-lane guardrail (spec §6). Absent = no guardrails configured = today's
* behavior (no-config compatibility, spec §5). Consulted in `executeToolCalls`
diff --git a/mcp-host/src/core/orchestration/toolUseLoopRuntime.ts b/mcp-host/src/core/orchestration/toolUseLoopRuntime.ts
index 2a5a72610..0e63b140d 100644
--- a/mcp-host/src/core/orchestration/toolUseLoopRuntime.ts
+++ b/mcp-host/src/core/orchestration/toolUseLoopRuntime.ts
@@ -47,6 +47,7 @@ export async function manageMessagesForIteration(
// defensive guard and tier selection can see `pending_approval`/state.
const managed = await config.contextManager.manage(messages, config.conversation, {
tools: config.toolRegistry.listDefinitions(),
+ systemPrompt: config.systemPrompt,
})
if (managed.length < beforeCount) {
if (logCompaction) {