Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): wire the admin hook timeout to the transport (m1)
Andy Paviad364ac57e956
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 1a9181a97..a2b7f3efb 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.265",+ "version": "0.1.266","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.265",+ "version": "0.1.266","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 32ad1aefd..e7cad1ec5 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.265",+ "version": "0.1.266","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/adapters/hookedLlmPort.ts b/mcp-host/src/core/adapters/hookedLlmPort.tsindex daae663b4..e71aab0f8 100644--- a/mcp-host/src/core/adapters/hookedLlmPort.ts+++ b/mcp-host/src/core/adapters/hookedLlmPort.ts@@ -303,9 +303,13 @@ export function maybeWrapHookedLlmPort(const hooks = buildLlmLaneHooks(config?.hookDescriptors, {getAuthToken: deps.getAuthToken ?? (() => ''),fetchImpl: deps.fetchImpl,- // Per-point response caps from the admin limits block (§5); defaults in the fetcher.+ // Per-point response caps + per-call deadline from the admin limits block (§5);+ // defaults in the fetcher. The timeout has to travel with the byte caps: left+ // unwired, `limits.maxHookTimeoutMs` was accepted by the CRD and silently+ // ignored while its two sibling caps took effect.maxOutputBytes: config?.limits?.maxHookOutputBytes,maxRewriteBytes: config?.limits?.maxHookRewriteBytes,+ timeoutMs: config?.limits?.maxHookTimeoutMs,})const hasHooks =hooks.preCall.length > 0 ||diff --git a/mcp-host/src/core/guardrails/__tests__/hookTimeoutWiring.test.ts b/mcp-host/src/core/guardrails/__tests__/hookTimeoutWiring.test.tsnew file mode 100644index 000000000..a42eae906--- /dev/null+++ b/mcp-host/src/core/guardrails/__tests__/hookTimeoutWiring.test.ts@@ -0,0 +1,114 @@+import { describe, expect, it } from 'vitest'+import type { ToolCompletionRequest } from '../../types'+import type { FetchLike } from '../hooks/hookFetcher'+import type { HookDescriptor } from '../hooks/types'+import { buildLlmLaneHooks } from '../llm/llmLaneHooks'+import { buildToolLaneGuardrail } from '../tool/toolLaneAdapter'++/**+ * `limits.maxHookTimeoutMs` is an admin-settable Host field (host.yaml, default+ * 5000) that reached nothing: the CRD accepted it while `createHookFetcher` always+ * fell back to its own 5000ms, even though the two sibling caps+ * (maxHookOutputBytes / maxHookRewriteBytes) were wired through the same call.+ *+ * Asserted behaviourally rather than by spying on the wiring: the fetcher puts+ * `AbortSignal.timeout(timeoutMs)` on the request, so a fake transport can watch+ * WHEN that signal fires. A configured 25ms aborts almost immediately; the 5000ms+ * default does not fire inside the same window. A "did we call X with Y" assertion+ * would pass even if the value were dropped one layer further down.+ */++const CONFIGURED_MS = 25+/** Long enough for a 25ms signal to fire, far short of the 5000ms default. */+const OBSERVE_MS = 300++function descriptor(points: HookDescriptor['lifecyclePoints']): HookDescriptor {+ return {+ id: 'hook-1',+ endpoint: 'http://hook.llm-hooks.svc.cluster.local:8080',+ path: '/',+ lifecyclePoints: points,+ capabilities: [],+ failMode: 'open',+ order: 100,+ }+}++/** Resolves to the ms until the request's signal aborted, or null if it never did. */+function signalWatcher(): { fetchImpl: FetchLike; aborted: Promise<boolean> } {+ let settle: (v: boolean) => void = () => {}+ const aborted = new Promise<boolean>(r => {+ settle = r+ })+ const fetchImpl: FetchLike = async (_url, init) => {+ const signal = init?.signal+ if (!signal) {+ settle(false)+ } else if (signal.aborted) {+ settle(true)+ } else {+ signal.addEventListener('abort', () => settle(true))+ setTimeout(() => settle(false), OBSERVE_MS)+ }+ // Never resolve the body: the only thing under test is the deadline.+ return new Promise(() => {}) as unknown as Response+ }+ return { fetchImpl, aborted }+}++const request = { messages: [{ role: 'user', content: 'hi' }] } as ToolCompletionRequest++describe('limits.maxHookTimeoutMs reaches the hook transport', () => {+ it('LLM lane: a configured deadline aborts the dial', async () => {+ const { fetchImpl, aborted } = signalWatcher()+ const hooks = buildLlmLaneHooks([descriptor(['pre_call'])], {+ getAuthToken: () => 't',+ fetchImpl,+ timeoutMs: CONFIGURED_MS,+ })+ void hooks.preCall[0].preCall(request)++ expect(await aborted).toBe(true)+ })++ it('LLM lane: without one, the dial is still pending at the same point', async () => {+ const { fetchImpl, aborted } = signalWatcher()+ const hooks = buildLlmLaneHooks([descriptor(['pre_call'])], {+ getAuthToken: () => 't',+ fetchImpl,+ })+ void hooks.preCall[0].preCall(request)++ // Proves the previous assertion came from CONFIGURED_MS, not from something+ // that aborts every dial regardless.+ expect(await aborted).toBe(false)+ })++ it('tool lane: the admin deadline applies there too', async () => {+ const { fetchImpl, aborted } = signalWatcher()+ const guardrail = buildToolLaneGuardrail(+ {+ hookDescriptors: [descriptor(['pre_tool_use'])],+ limits: { maxHookTimeoutMs: CONFIGURED_MS },+ },+ { getAuthToken: () => 't', fetchImpl }+ )+ void guardrail!.decide({ provenance: 'native', name: 'shell' }, {})++ expect(await aborted).toBe(true)+ })++ it('tool lane: an explicit dep still wins over the admin limit', async () => {+ const { fetchImpl, aborted } = signalWatcher()+ const guardrail = buildToolLaneGuardrail(+ {+ hookDescriptors: [descriptor(['pre_tool_use'])],+ limits: { maxHookTimeoutMs: 60_000 },+ },+ { getAuthToken: () => 't', fetchImpl, timeoutMs: CONFIGURED_MS }+ )+ void guardrail!.decide({ provenance: 'native', name: 'shell' }, {})++ expect(await aborted).toBe(true)+ })+})diff --git a/mcp-host/src/core/guardrails/llm/llmLaneHooks.ts b/mcp-host/src/core/guardrails/llm/llmLaneHooks.tsindex cc19163c8..04fbdb9bb 100644--- a/mcp-host/src/core/guardrails/llm/llmLaneHooks.ts+++ b/mcp-host/src/core/guardrails/llm/llmLaneHooks.ts@@ -22,6 +22,8 @@ export interface LlmLaneHookDeps {/** Per-point response caps (spec §5/§8.1). Defaults applied in `createHookFetcher`. */maxOutputBytes?: numbermaxRewriteBytes?: number+ /** Per-call hook deadline (§5 `limits.maxHookTimeoutMs`). Default in `createHookFetcher`. */+ timeoutMs?: number}export function buildLlmLaneHooks(@@ -41,6 +43,7 @@ export function buildLlmLaneHooks(fetchImpl: deps.fetchImpl,maxOutputBytes: deps.maxOutputBytes,maxRewriteBytes: deps.maxRewriteBytes,+ timeoutMs: deps.timeoutMs,}))if (d.lifecyclePoints.includes('pre_call')) preCall.push(hook)diff --git a/mcp-host/src/core/guardrails/tool/toolLaneAdapter.ts b/mcp-host/src/core/guardrails/tool/toolLaneAdapter.tsindex ed0374597..efbe63429 100644--- a/mcp-host/src/core/guardrails/tool/toolLaneAdapter.ts+++ b/mcp-host/src/core/guardrails/tool/toolLaneAdapter.ts@@ -20,6 +20,8 @@ import { compileRules, evaluateRules } from './rules'export interface ToolLaneGuardrailDeps {getAuthToken?: () => stringfetchImpl?: FetchLike+ /** Per-call hook deadline (§5 `limits.maxHookTimeoutMs`). Default in `createHookFetcher`. */+ timeoutMs?: number}/**@@ -40,6 +42,7 @@ function buildToolLaneHooks(createHookFetcher({getAuthToken: deps.getAuthToken ?? (() => ''),fetchImpl: deps.fetchImpl,+ timeoutMs: deps.timeoutMs,}))if (d.lifecyclePoints.includes('pre_tool_use')) pre.push(hook)@@ -92,7 +95,13 @@ export function buildToolLaneGuardrail(hookDeps: ToolLaneGuardrailDeps = {}): ToolLaneGuardrail | undefined {const compiled = compileRules(config?.rules, config?.limits?.maxRules)- const hooks = buildToolLaneHooks(config?.hookDescriptors, hookDeps)+ // The admin deadline applies to tool-lane hooks too; an explicit hookDeps value+ // (tests) still wins. NOTE: the per-point byte caps are deliberately NOT wired+ // here — that is a separate gap from the one this fixes.+ const hooks = buildToolLaneHooks(config?.hookDescriptors, {+ ...hookDeps,+ timeoutMs: hookDeps.timeoutMs ?? config?.limits?.maxHookTimeoutMs,+ })if (!compiled.hasRules && hooks.pre.length === 0 && hooks.post.length === 0) return undefinedconst deps: CoreBoundaryDeps<ToolLaneInput, ToolLaneResult, ToolIdentity> = {