Skip to content

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

TypeScript

feat(guardrails): wire the doom-loop guard into the tool lane (phase 1)

Andy Pavia582b868a2133
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index db7f63476..6b495a027 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.236",
+ "version": "0.1.237",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.236",
+ "version": "0.1.237",
"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 e3f15e2b6..c4d1823eb 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.236",
+ "version": "0.1.237",
"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/index.ts b/mcp-host/src/core/guardrails/index.ts
index 88075a729..c71aaf858 100644
--- a/mcp-host/src/core/guardrails/index.ts
+++ b/mcp-host/src/core/guardrails/index.ts
@@ -21,4 +21,6 @@ export type {
} from './tool/toolLaneAdapter'
export { resolveToolIdentity, resolveToolIdentityFromRegistry } from './tool/provenance'
export type { ToolIdentity } from './tool/provenance'
+export { recordAndCheck } from './tool/doomLoop'
+export type { DoomLoopState } from './tool/doomLoop'
export { guardrailDecisionsTotal, guardrailBoundaryDurationMs } from './metrics'
diff --git a/mcp-host/src/core/orchestration/__tests__/guardrailGate.test.ts b/mcp-host/src/core/orchestration/__tests__/guardrailGate.test.ts
index f13c622d6..885829c53 100644
--- a/mcp-host/src/core/orchestration/__tests__/guardrailGate.test.ts
+++ b/mcp-host/src/core/orchestration/__tests__/guardrailGate.test.ts
@@ -144,6 +144,29 @@ describe('guardrail gate in executeToolCalls', () => {
expect(tool.calls).toHaveLength(0)
})
+ it('doom-loop: 3rd consecutive identical call is denied (§6.4)', async () => {
+ const tool = new StubTool('do_thing')
+ const config = makeConfig(tool, fixedGuardrail('allow'))
+ // Same conversation across calls carries the doom-loop counter.
+ const r1 = await executeToolCalls([call], config, 0)
+ const r2 = await executeToolCalls([{ ...call, id: 'c2' }], config, 1)
+ const r3 = await executeToolCalls([{ ...call, id: 'c3' }], config, 2)
+ expect(r1.toolResults[0].is_error).toBe(false)
+ expect(r2.toolResults[0].is_error).toBe(false)
+ expect(r3.toolResults[0].is_error).toBe(true)
+ expect(r3.toolResults[0].content).toContain('doom-loop')
+ expect(tool.calls).toHaveLength(2) // 3rd blocked
+ })
+
+ it('doom-loop resets when a different call intervenes (§6.4)', async () => {
+ const tool = new StubTool('do_thing')
+ const config = makeConfig(tool, fixedGuardrail('allow'))
+ await executeToolCalls([call], config, 0)
+ await executeToolCalls([{ id: 'x', name: 'do_thing', arguments: { a: 2 } }], config, 1) // different args → reset
+ const r3 = await executeToolCalls([{ ...call, id: 'c3' }], config, 2)
+ expect(r3.toolResults[0].is_error).toBe(false) // counter was reset
+ })
+
it('ask + matching one-shot approval → proceeds and clears pending', async () => {
const tool = new StubTool('do_thing')
const conversation: Partial<Conversation> = {
diff --git a/mcp-host/src/core/orchestration/toolUseLoopToolBatch.ts b/mcp-host/src/core/orchestration/toolUseLoopToolBatch.ts
index d6de8f343..55e5256cd 100644
--- a/mcp-host/src/core/orchestration/toolUseLoopToolBatch.ts
+++ b/mcp-host/src/core/orchestration/toolUseLoopToolBatch.ts
@@ -1,6 +1,6 @@
import { randomUUID } from 'crypto'
import { extractToolIntent, getDisplayName } from '../../progress/intentExtraction.js'
-import { recordDecision, resolveToolIdentityFromRegistry } from '../guardrails'
+import { recordAndCheck, recordDecision, resolveToolIdentityFromRegistry } from '../guardrails'
import type { ChatMessage, PendingApproval, TokenUsage, ToolCall, ToolResult } from '../types'
import type { LoopConfig } from './loopConfig'
import { executeSingleTool, reportToolComplete, reportToolStart } from './toolUseLoopSingleTool'
@@ -240,6 +240,36 @@ export async function executeToolCalls(
config.toolRegistry,
call.arguments
)
+
+ // Doom-loop guard (spec §6.4): deny 3 consecutive identical (tool, input)
+ // calls within a task. Best-effort runaway/cost guard, not a security
+ // control (alternation evades it, §12.4/N12). Cross-turn state lives on the
+ // conversation (ephemeral; resets on resume).
+ const dlKey = `${identity.provenance}:${identity.server ?? ''}:${identity.name}:${JSON.stringify(call.arguments)}`
+ const dl = recordAndCheck(config.conversation.guardrail_doom_loop ?? { count: 0 }, dlKey)
+ config.conversation.guardrail_doom_loop = dl.state
+ if (dl.tripped) {
+ recordDecision('tool', 'deny', 'current', 'denied', 'repeated_identical_call')
+ events.emit({
+ type: 'guardrail:decision',
+ data: {
+ toolName: call.name,
+ decision: 'deny',
+ reasonCode: 'repeated_identical_call',
+ source: 'current',
+ iteration,
+ },
+ timestamp: new Date(),
+ })
+ toolResults.push({
+ tool_call_id: call.id,
+ name: call.name,
+ content: 'Blocked: repeated identical tool call (doom-loop guard).',
+ is_error: true,
+ })
+ continue
+ }
+
const gd = await config.guardrails.decide(identity, call.arguments)
events.emit({
type: 'guardrail:decision',
diff --git a/mcp-host/src/core/types.ts b/mcp-host/src/core/types.ts
index d78fdd527..8a0b8be1f 100644
--- a/mcp-host/src/core/types.ts
+++ b/mcp-host/src/core/types.ts
@@ -306,6 +306,13 @@ export interface Conversation {
turns: Turn[]
pending_approval?: PendingApproval
auto_approved_tools: Set<string>
+ /**
+ * Guardrail doom-loop counter (spec §6.4) — tracks consecutive identical
+ * `(resolved tool, effective-input)` tool calls across turns within a task.
+ * Ephemeral (like `auto_approved_tools`): not persisted; resets on resume.
+ * Absent until the first guarded tool call.
+ */
+ guardrail_doom_loop?: { lastKey?: string; count: number }
created_at: Date
updated_at: Date
/**