Skip to content

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

TypeScript

fix(guardrails): preserve original system prompt across pre_call patch (n4)

Andy Pavia17c58eab0a26
diff --git a/docs/features/mcp-host-guardrails/mcp-host-guardrails-spec-v3.md b/docs/features/mcp-host-guardrails/mcp-host-guardrails-spec-v3.md
index effc1878e..8418cb0a6 100644
--- a/docs/features/mcp-host-guardrails/mcp-host-guardrails-spec-v3.md
+++ b/docs/features/mcp-host-guardrails/mcp-host-guardrails-spec-v3.md
@@ -771,13 +771,17 @@ what `may_substitute_result` already promises (it changes the caller-visible res
authoritative action flow, §4.3/§6). The same rule applies to a `post_call` transform: it may remove
`tool_calls` but never introduce them.
-**The system prompt is immutable to installed hooks.** A `pre_call` `patch` may edit only **non-system**
-`messages` and `params`; the system prompt / any system-role message is preserved from the original
-request and cannot be added, replaced, or overridden by an installed hook — neither via a
-`systemPromptParts` field (removed) **nor** by injecting a system-role entry into the `messages` patch,
-which mcp-host drops and audits. Authoritative system-prompt shaping stays a **first-party** privilege: the
-`prompt-shaping` built-in (§7.2). Any context an installed hook adds is message-level and untrusted-framed
-(§12.4) — a `may_rewrite` grant never confers system-level authority.
+**The system prompt is immutable to installed hooks — neither added nor removed.** A `pre_call` `patch`
+may edit only the **non-system** `messages` and `params`; the original system prefix is **preserved
+byte-for-byte** and cannot be added, replaced, overridden, **or deleted**. mcp-host enforces this
+structurally: a `messages` patch replaces only the non-system messages, and the original system-role
+message(s) are **spliced back at the front** — so a hook can neither *inject* a system-role entry (it is
+stripped from the patch) nor *drop* the existing one (it is re-added from the original request, not taken
+from the patch). This holds on both prompt paths: the tiered path carries the system prompt out-of-band in
+`systemPromptParts` (never in the hook-visible `messages`, and untouched by the patch), and the legacy path
+carries it inside `messages` where the splice restores it. Authoritative system-prompt shaping stays a
+**first-party** privilege: the `prompt-shaping` built-in (§7.2). Any context an installed hook adds is
+message-level and untrusted-framed (§12.4) — a `may_rewrite` grant never confers system-level authority.
### 8.2 · The `LlmHook` CRD
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 3c51202a0..0196acbe4 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.255",
+ "version": "0.1.256",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.255",
+ "version": "0.1.256",
"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 1eda4c1bd..58aeb4c96 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.255",
+ "version": "0.1.256",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/guardrails/hooks/__tests__/remoteLlmHook.test.ts b/mcp-host/src/core/guardrails/hooks/__tests__/remoteLlmHook.test.ts
index 60e46dce9..6a31d8e82 100644
--- a/mcp-host/src/core/guardrails/hooks/__tests__/remoteLlmHook.test.ts
+++ b/mcp-host/src/core/guardrails/hooks/__tests__/remoteLlmHook.test.ts
@@ -66,6 +66,25 @@ describe('pre_call', () => {
expect(c?.rewrite?.temperature).toBe(0.1)
})
+ it('preserves the ORIGINAL system prefix — a patch cannot DELETE it (N4)', async () => {
+ // Legacy path: the system prompt lives inside request.messages. A may_rewrite
+ // patch that omits it must not drop it — mcp-host splices the original back.
+ const original: ChatMessage[] = [
+ { role: 'system', content: 'FIRST-PARTY SYSTEM PROMPT' },
+ { role: 'user', content: 'original user' },
+ ]
+ const patch = { messages: [{ role: 'user', content: 'rewritten' }] }
+ const h = new RemoteLlmHook(
+ desc({ capabilities: ['may_rewrite'] }),
+ fetcher({ status: 200, body: { action: 'continue', patch }, unavailable: false })
+ )
+ const msgs = (await h.preCall(req(original)))?.rewrite?.messages ?? []
+ expect(msgs[0]).toEqual({ role: 'system', content: 'FIRST-PARTY SYSTEM PROMPT' })
+ expect(msgs.filter(m => m.role === 'system')).toHaveLength(1) // exactly the original, no more
+ expect(msgs.some(m => m.role === 'user' && m.content === 'rewritten')).toBe(true)
+ expect(msgs.some(m => m.role === 'user' && m.content === 'original user')).toBe(false)
+ })
+
it('continue + patch WITHOUT may_rewrite → patch dropped (F4)', async () => {
const h = new RemoteLlmHook(
desc({ capabilities: [] }),
diff --git a/mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts b/mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts
index 5324fc5e3..b6e1e0da9 100644
--- a/mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts
+++ b/mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts
@@ -207,7 +207,13 @@ export class RemoteLlmHook {
): ToolCompletionRequest {
const next: ToolCompletionRequest = { ...request }
if (Array.isArray(patch.messages)) {
- next.messages = stripSystemRole(patch.messages as ChatMessage[])
+ // N4: the system prompt is immutable to installed hooks — neither added nor
+ // REMOVED. The patch replaces only the non-system messages; the original
+ // system prefix is spliced back so a hook can't drop it by omission (on the
+ // legacy path the system prompt lives inside `messages`; on the tiered path
+ // there is none here and it rides `systemPromptParts`, preserved by the spread).
+ const originalSystem = request.messages.filter(m => m.role === 'system')
+ next.messages = [...originalSystem, ...stripSystemRole(patch.messages as ChatMessage[])]
}
const params = isRecord(patch.params) ? patch.params : undefined
if (params) {