Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
docs(guardrails): remove guardrails spec/design suite from the repo
Andy Pavia1725704a929a
diff --git a/docs/features/mcp-host-guardrails/README.md b/docs/features/mcp-host-guardrails/README.mddeleted file mode 100644index 364d07ec9..000000000--- a/docs/features/mcp-host-guardrails/README.md+++ /dev/null@@ -1,33 +0,0 @@-# MCP Host Guardrails--One guardrail/policy framework for the MCP Host, spanning **two lanes**:--- **LLM lane** — guardrails on the model *completion* call (mutate/moderate the request, transform the- response, recover on failure).-- **Tool lane** — guardrails on *tool execution* (permission rules + `Pre/PostToolUse` hooks + admin- policy + approvals).--Both lanes use one shared decision engine (the `allow`/`ask`/`deny` logic, admin policy, at-most-once-execution, audit) and one shared "how hooks run and are trusted" layer.--## Spec--**→ [`mcp-host-guardrails-spec.md`](./mcp-host-guardrails-spec.md)** — the single, self-contained design-spec. Read top-to-bottom: overview → the shared `GuardrailBoundary` machine → decision algebra →-execution safety → the one `Host.spec.guardrails` config block → the tool lane → the LLM lane → hook-trust & delivery → evidence → open decisions → invariants.--## Status--**Draft for discussion.** No runtime, CRD, or deployment change is authorized by this spec — it is a-specification.--## History--Superseded material is archived under [`legacy/`](./legacy/), in two folders:--- [`legacy/split-suite/`](./legacy/split-suite/) — the five-document version this spec merges (S0- architecture, S1 core, S2 tool lane, S3 LLM lane, S4 hook trust & delivery).-- [`legacy/early-drafts/`](./legacy/early-drafts/) — the earlier exploratory drafts that preceded the- suite (the original LLM-hooks spec, the tool-lane-vs-LLM-hooks comparison, and the first unified- architecture sketch).diff --git a/docs/features/mcp-host-guardrails/legacy/early-drafts/README.md b/docs/features/mcp-host-guardrails/legacy/early-drafts/README.mddeleted file mode 100644index 2bd2a5aa4..000000000--- a/docs/features/mcp-host-guardrails/legacy/early-drafts/README.md+++ /dev/null@@ -1,28 +0,0 @@-# MCP Host Guardrails — spec suite--Guardrail / policy specs for the MCP Host, across **two lanes**:--- **LLM lane** — hooks on the model *completion* call (mutate/moderate the request, transform the- response).-- **Tool lane** — permission rules + `Pre/PostToolUse` hooks + admin policy over *tool execution*- (a colleague's draft, tracked externally as a Notion/PDF; not committed here).--## Documents--| Doc | Role |-|---|---|-| [`mcp-host-guardrails-unified-architecture.md`](./mcp-host-guardrails-unified-architecture.md) | **S0 · umbrella** — the shared spine + trust substrate that unifies the two lanes, and how the disagreements resolve |-| [`guardrails-vs-llm-hooks-comparison.md`](./guardrails-vs-llm-hooks-comparison.md) | analysis — where the two lanes **agree**, what each **emphasizes**, and where they **disagree** |-| [`llm-hooks-spec.md`](./llm-hooks-spec.md) | **S3 · LLM-lane adapter** — pre/post-call hooks (guardrail/PII/prompt-shaping/token-trim) + the marketplace/remote-hook substrate |--## Planned (not yet written)--Per the umbrella §2, the full suite is:--- **S1 · Guardrail Core** — decision algebra + aggregation, contributor contract, at-most-once/resume,- admin-policy model + HCC delivery, bounded audit. *(new; mostly lifted from the tool-lane spec.)*-- **S2 · Tool-lane adapter** — the colleague's spec, re-homed on S1.-- **S4 · Hook trust & delivery substrate** — Tier A (local, digest-verified) + Tier B- (remote/marketplace) and their governance. *(new; the main reconciliation work.)*--Status: all drafts, for discussion — no runtime/CRD/deployment change is authorized by these documents.diff --git a/docs/features/mcp-host-guardrails/legacy/early-drafts/guardrails-vs-llm-hooks-comparison.md b/docs/features/mcp-host-guardrails/legacy/early-drafts/guardrails-vs-llm-hooks-comparison.mddeleted file mode 100644index 0e7e8b8b2..000000000--- a/docs/features/mcp-host-guardrails/legacy/early-drafts/guardrails-vs-llm-hooks-comparison.md+++ /dev/null@@ -1,123 +0,0 @@-# Comparison · Guardrails spec vs LLM-hooks spec — where they agree & what each emphasizes--| | |-|---|---|-| **Colleague's doc** | *(DRAFT) MCP Host Guardrails — Permission Rules, Hooks & Admin Policy Specification* (draft target contract; not implemented) |-| **Our doc** | [`llm-hooks-spec.md`](./llm-hooks-spec.md) — LLM pre/post-call hooks + marketplace |-| **This doc** | where the two align (§2), what each emphasizes (§3–4), and where they genuinely disagree (§5) |-------## 1 · They govern different lanes (so "agree" = shared principles, not same scope)--Before the comparison: the two specs sit on **different lanes** and both are needed.--- **Colleague's — the tool lane.** Guardrails on *tool execution* (native + MCP): `PreToolUse`/- `PostToolUse` hooks, host permission rules (`allow`/`ask`/`deny`), Context admin policy, approvals.-- **Ours — the LLM lane.** Hooks on *the model completion call*: `pre_call`/`moderation`/`post_call`/- `on_error` for guardrail/PII/prompt-shaping/token-trim, plus a marketplace for remote hooks.--A request runs **our lane** (mutate/moderate the model call → the model emits a tool call) then **his-lane** (authorize/execute that tool call). They **stack**; neither replaces the other. So the-agreements below are shared *principles*, applied to two different boundaries.-------## 2 · Where you agree (shared design principles)--Strong alignment — both specs independently landed on the same guardrail philosophy:--| Shared principle | Colleague (Guardrails) | Ours (LLM-hooks) |-|---|---|---|-| **One mandatory mediation boundary; fail-closed** | G1 universal mediation; decision-making hooks default closed | `HookedLlmPort` single funnel; fail-closed default for guardrail/PII |-| **Hooks never hold credentials/secrets** | G10 + §9.2: no LLM keys, K8s creds, or secret env in the hook | §6: hooks never receive the LLM provider credential |-| **Hook trust via allowlist + content digest** | G10: preinstalled, admin-allowlisted, digest-verified | image-allowlist + sha256 bundle digest + `trust_level` |-| **Admin/policy delivered by HCC as an atomic artifact; Host can't weaken it** | G4/G13: HCC materializes a read-only, digest-checked policy artifact | HCC reconciles `LlmHook` CRs; `global`/`context` scope, mcp-host only reads |-| **Pre = mutate/reject; Post = redact/transform; Post can't undo side effects** | §9.3/§9.4 | `pre_call` continue/reject; `post_call_success` transforms the response |-| **Rewrite safety: a rewrite replaces the whole input and is revalidated** | G7 | `pre_call` `patch` applied by mcp-host, request re-validated before dispatch |-| **Fire/execute at-most-once, safe across retries** | G11/G12 (at-most-once execution; Post once) | `HookedLlmPort` fires once per logical request, **above** failover |-| **Bounded, redacted observability** | §13/G15: fixed-enum metric labels; audit uses digests, not raw args | §6: audit-logged; telemetry on raw counts; redacted content projection |-| **No-config compatibility (unconfigured = current behavior)** | G8 | PR1 lands an empty chain — pass-through, zero behavior change |-| **Bounded hook execution (timeout + failure mode)** | `timeoutMs` + `failureMode` | per-hook `timeoutMs` + `failMode` + `onUnavailable` breaker |-| **Evaluate the real resolved identity, not a guessed one** | G2 resolved identity; provenance from the registry | `ctx` carries the real model/provider + `usageContext` |--Net: on **trust model primitives** (digests, allowlists, no-secrets, HCC-delivered admin artifact,-bounded audit) and **hook lifecycle** (pre-mutate/reject, post-transform, at-most-once, revalidation,-no-config safety) you are aligned.-------## 3 · What your colleague focused more on--Areas his spec develops in depth that ours barely touches (mostly because they're tool-lane concerns):--- **A declarative permission-rule engine.** Host rules with `allow`/`ask`/`deny`, **typed argument- predicates** (`path`/`url`/`command`/`json` with RFC-6901 pointers, symlink-aware path containment,- URL credential rejection, no-regex bounded wildcards), provenance-aware matching, and **strict- aggregation** (`deny > force_ask > allow > no_decision`). Ours has *no* declarative rule layer — our- hooks are code/services, not argument-level allow/ask/deny rules.-- **Human approval / interactive consent.** `force_ask` bound to the exact tool-call id + effective-input- digest, interactive vs unattended by resolver availability, and a full **approval snapshot / batch /- resume** state machine. Ours has no human-in-the-loop concept at the LLM lane.-- **Exactly-once execution + resumability.** Suspended snapshots, resume re-checking admin policy without- rerunning Pre hooks, idempotency keys, and a **doom-loop guard** (3 identical resolved-tool+input →- deny). Ours treats the model call as a single completion — far less execution state.-- **A non-overridable admin hierarchy.** "Admin cannot be weakened" (G4) as a first-class invariant —- Context policy strictly dominates Host rules/hooks/approvals. Ours has `global`/`context` scope but not- a strict *dominance* rule.-- **Local sandboxed hook execution.** He solves the *trusted local executable* problem — least-privilege- child processes, per-launch digest/owner/realpath verification, no shell, bounded stdio. (Ours sidesteps- this by pushing hooks **out of process** instead — see §4.)-- **Persistence / prompt-injection hardening.** Routing raw `file_write` through the `WorkspaceService`- scanner and treating `daily/*` memory as prompt-affecting. Not in our scope.-- **Heavy formal apparatus.** 15 invariants, a 17-row **threat model** (T1–T17 with preventive +- detective controls and residual owners), 30 **acceptance scenarios** (A1–A30), a compatibility matrix,- a work-package dependency graph, and an **external-semantics survey** (OpenAI/Claude/Cursor/OpenCode- hook contracts). Ours is lighter on threat matrix / acceptance scenarios.-------## 4 · What your spec focuses more on (the mirror)--The inverse — areas ours develops that his doesn't address (his spec never touches the model call):--- **Intercepting the LLM completion itself** — parallel `moderation`, PII redaction of model- input/output, `prompt-shaping`, `token-trim`, and `on_error` **recover** (substituting a safe response).- His hooks act only *after* the model has chosen a tool.-- **Remote + marketplace distribution.** Installable third-party hooks: a registry `entry_type:- 'llm_hook'`, an `install-hook` saga, image-allowlist/digest/egress trust, and **three delivery modes**- (self-hosted pod, in-cluster Service, external endpoint) with a `llm-hooks` namespace. His hooks are- **local-only, admin-allowlisted** — explicitly *not* a marketplace/sidecar/WASM.-- **Content-exposure governance for third-party code** — need-based + trust-gated message/response access.- Not a concern for his local, no-network hooks.-------## 5 · Where you disagree--The disagreements cluster in the **hook execution & trust model** — where, on the *same* question, the-two specs make opposing choices. The first five are fundamental (a single "hook" mechanism cannot-satisfy both); the last two are expressiveness gaps rather than contradictions.--| Question | Colleague (Guardrails) | Ours (LLM-hooks) | |-|---|---|---|---|-| **Where a hook runs** | a local child process inside the Host workload; non-goals **exclude** sidecar / WASM / remote | a **remote** service (own pod / in-cluster Service / external endpoint) | fundamental |-| **Network & side-effects** | **forbidden** — a deterministic JSON transform, "no independent external side effects," network stays outside the hook contract | **required** — guardrail/moderation/PII hooks call external services (e.g. `egressBindings` to a vendor API) | fundamental |-| **Who may author / install** | admin-**allowlisted**, digest-pinned; "no arbitrary user scripts" | **third-party marketplace** installs, gated by `trust_level` tiers | fundamental |-| **When a hook is down** | strict **fail-closed** — declared-but-unavailable policy ⇒ Host not Ready; digest mismatch never falls back | a per-hook **`onUnavailable: breaker`** may trip **fail-open** (with alerting) after N failures | fundamental (safety posture) |-| **May a hook fabricate the outcome?** | **no** — Post redacts the *model-visible view* only; it can't alter the stored outcome, and a deny yields a bounded error | **yes** — `on_error` **`recover`** substitutes a full safe response for a blocked/failed call | fundamental |-| **Decision expressiveness** | 4-valued `allow / force_ask / deny / no_decision`, **human `force_ask`**, and strict multi-source aggregation with precedence | `continue`/`reject` + moderation pass/fail + hard block; **no** human `ask`, no `defer`/`no_decision`, no aggregation | gap |-| **Config authority** | a **non-overridable** Context allowlist of hook id+phase+**digest** the Host can't exceed | scope (`global`/`context`) on a CRD; no per-digest allowlist the Host is bound to | gap |--**Reading it:** several of these are *lane-justified* — a tool guardrail genuinely should be a-deterministic local check, and an LLM moderation genuinely needs to call a service — so they're not-"someone is wrong." But they are the exact points that **collide if anyone tries to unify the two into-one "hook" mechanism**, and two stand out as posture disagreements worth an explicit decision even while-the specs stay separate:--1. **local-no-network vs remote-network** hooks (the trust boundary is inverted), and-2. **strict fail-closed vs fail-open breaker** when a hook is unavailable.--Everything *else* composes cleanly (shared digest/allowlist/no-secrets/HCC-artifact/bounded-audit-primitives on two stacked lanes) — so the recommendation stands: keep them as **two separate mechanisms**-and say so in both docs, but align deliberately on those two posture questions.diff --git a/docs/features/mcp-host-guardrails/legacy/early-drafts/llm-hooks-spec.md b/docs/features/mcp-host-guardrails/legacy/early-drafts/llm-hooks-spec.mddeleted file mode 100644index be5b6e83c..000000000--- a/docs/features/mcp-host-guardrails/legacy/early-drafts/llm-hooks-spec.md+++ /dev/null@@ -1,473 +0,0 @@-# Spec · LLM hooks — implementation & marketplace install--| | |-|---|---|-| **Type** | design spec (implementable) |-| **Date** | 2026-08-10 |-| **Status** | draft for approval |-| **Code head** | `evenfire` @ `dev` |-| **Model** | a request-lifecycle hook chain (`pre_call` / `moderation` / `post_call_success` / `post_call_failure`) realized **in-process** at the mcp-host `LlmPort` seam, plus a second class of hook that is an **installable marketplace artifact running out-of-process** (a self-hosted pod, an in-cluster Service, or an external endpoint) |-------## 0 · The two hook classes--| Class | Runs where | Examples | Distribution | Trust |-|---|---|---|---|---|-| **Built-in** | inside mcp-host (`HookedLlmPort` + `core/extensions/prePrune.ts`) | `prompt-shaping`, `token-trim` | compiled into mcp-host, enabled by config | first-party |-| **Remote** | out-of-process: self-hosted pod, in-cluster Service, or external endpoint (§3) | `guardrail`, `pii-redact` (3rd-party) | **marketplace** → `LlmHook` CRD | image-allowlist + sha256 digest + egress policy + trust_level |--Both classes implement the **same `LlmHook` interface** and run in the **same ordered chain** at the-same seam. A remote hook is just an `LlmHook` whose methods make an HTTP call to its pod. Third-party-code never runs inside the mcp-host process (which holds LLM credentials + conversation state).-------## 1 · Runtime seam (shared by both classes)--Every completion funnels through `LlmPort.completeWithTools` (`mcp-host/src/core/interfaces.ts:33`);-`LlmPortAdapter` (`core/adapters/llmPortAdapter.ts:93`) is the one implementation, sitting under an-optional `FailoverLlmPort` (`core/adapters/failoverLlmPort.ts:56`). We insert a decorator **above-failover** so each hook fires **once per logical request**.--### 1.1 · `LlmHook` interface · `mcp-host/src/llm/hooks/types.ts` (new)-```ts-export interface LlmHookContext {- readonly callType: 'complete' | 'completeWithTools'- readonly model: string- readonly provider: LlmProvider- readonly host: { hostRef?: string; contextRef?: string } // NOT llmSecretName — see §6- readonly usage?: UsageContext // user_id, team_id, recipe_name, task_id, traceContext- readonly signal?: AbortSignal- readonly state: Record<string, unknown> // mutable, shared across ONE request's hooks-}-export type PreCallOutcome =- | { action: 'continue'; request?: ToolCompletionRequest } // optionally-mutated request- | { action: 'reject'; error: LlmError } // veto before any tokens spent-export type OnErrorOutcome =- | { action: 'reshape'; error: LlmError } // transform the surfaced error- | { action: 'recover'; response: ToolCompletionResponse } // substitute a safe response-export interface LlmHook {- readonly name: string- readonly failMode: 'open' | 'closed'- readonly timeoutMs: number- preCall?(req: ToolCompletionRequest, ctx: LlmHookContext): Promise<PreCallOutcome>- moderate?(req: ToolCompletionRequest, ctx: LlmHookContext): Promise<void> // throw = fail- postCallSuccess?(req: ToolCompletionRequest, res: ToolCompletionResponse, ctx: LlmHookContext): Promise<ToolCompletionResponse>- // Invoked whenever the request fails to produce a model response — a preCall- // reject, a moderation block, or a call failure (after failover). May reshape- // the surfaced error OR RECOVER with a substitute response. `recover` is- // trust-gated + audited (§6).- onError?(req: ToolCompletionRequest, err: LlmError, ctx: LlmHookContext): Promise<OnErrorOutcome>-}-```--### 1.2 · `HookedLlmPort` decorator · `mcp-host/src/core/adapters/hookedLlmPort.ts` (new)-Implements `LlmPort`, wraps an inner `LlmPort`, holds `readonly hooks: LlmHook[]` (built-ins first,-then remote hooks by `order`). Behavior:-- **preCall** in order; thread the mutated request forward. A `reject` (or a fail-closed- error/timeout, per `failMode`) does **not** dispatch — it seeds an `error` and jumps straight to- the onError chain (so a recovery hook can substitute a safe response with zero tokens spent).-- **moderate ‖ dispatch**: `linkedAbort` chained to `req.signal`; `Promise.all([inner.completeWithTools(req), ...moderate()])`.- A rejected `moderate` calls `linkedAbort.abort()` and seeds a `ContentFiltered` `LlmError`- (`core/errors.ts`); a call failure (after failover) seeds its `LlmError`. Either routes to onError.- The happy path (call ok **and** every moderation passes) goes to postCallSuccess.-- **postCallSuccess** folds in **reverse** order (onion) and returns the response.-- **onError** — the single convergence point for preCall-reject, moderation-block, and call-failure.- Folds hooks in **reverse** order; each returns `reshape` (update the error, continue) or `recover`- (short-circuit: return the substitute response immediately — remaining onError hooks are skipped- and postCallSuccess is **not** re-run, since a recovered response is synthesized, not model output).- If the chain ends with no `recover`, throw the final reshaped error.-- `modelName()`/`getTokenCounter?()` delegate to inner. Per-hook calls wrapped in `withTimeout(hook.timeoutMs)`.--### 1.3 · Lifecycle state machine--```mermaid-stateDiagram-v2- [*] --> BuildContext : completeWithTools(request)- BuildContext --> PreCall : ctx = usage + model + state{}-- state PreCall {- [*] --> RunHooks- RunHooks --> RunHooks : continue — mutate request, next hook- RunHooks --> [*] : all hooks approved- }-- PreCall --> OnError : a hook rejects (or fail-closed error/timeout) — no dispatch- PreCall --> Dispatch : approved (request possibly mutated)-- state Dispatch {- [*] --> UpstreamCall- UpstreamCall --> CallSucceeded : response- UpstreamCall --> CallFailed : throws (after failover)- --- [*] --> Moderation- Moderation --> ModerationPassed : all moderate() resolve- Moderation --> ModerationFailed : a moderate() rejects → abort call- }-- Dispatch --> OnError : CallFailed or ModerationFailed (seeds the error)- Dispatch --> PostCall : CallSucceeded and ModerationPassed-- state PostCall {- [*] --> Transform- Transform --> Transform : redact / transform (reverse order)- Transform --> [*] : chain done- }- PostCall --> Success-- state OnError {- [*] --> Handle- Handle --> Handle : reshape error (reverse order)- Handle --> [*] : chain done or a hook recovers- }- OnError --> Recovered : a hook returns recover- OnError --> Failed : no recover — final reshaped error-- Recovered --> [*] : return substitute ToolCompletionResponse- Success --> [*] : return ToolCompletionResponse- Failed --> [*] : throw reshaped LlmError-```--`OnError` is the single convergence point: a preCall reject, a moderation block, and a call failure-all seed an error into it, and any hook may **recover** (return a safe response) instead of the error.--### 1.4 · Wiring · `mcp-host/src/agent/taskExecutor.ts`-In `buildLoopConfig` (`taskExecutor.ts:1057`), after `effectiveLlmPort` is built (primary or-failover-wrapped, `:1017–1055`): `effectiveLlmPort = new HookedLlmPort(effectiveLlmPort, chain)`.-Build `LlmHookContext.usage` from the existing `buildDefaultUsageContext()` (`:1436`). The compaction-lane (`stateMachine.ts:821`) gets only built-in trim/observability hooks (flag on the chain builder).-------## 2 · Built-in hooks (in-process, first-party)--Implement as `LlmHook`s compiled into mcp-host, under `mcp-host/src/llm/hooks/builtins/`:-| type | method(s) | implementation notes |-|---|---|---|-| `prompt-shaping` | `preCall` | inject a system `systemPromptParts` entry; force `temperature`/`max_tokens`/`tool_choice`. |-| `token-trim` | `preCall` | reduce **input** tokens to a budget; **reuse the existing `core/extensions/prePrune.ts` passes** internally (they already do toggleable pre-LLM context pruning) — the hook is a thin `LlmHook` wrapper so ordering/config are uniform with the chain. |--Registered in a `Map<type, (config)=>LlmHook>` (`mcp-host/src/llm/hooks/builtins/registry.ts`),-enabled/ordered via config (§4). No marketplace, no network hop.--```mermaid-flowchart TB- subgraph CFG["Host CRD · spec.llmHooks (config)"]- C1["prompt-shaping<br/>(order, failMode, config)"]- C2["token-trim<br/>(budget, config)"]- end-- CFG --> REG["builtins/registry.ts<br/>Map<type, factory>"]- REG --> PS["prompt-shaping : LlmHook"]- REG --> TT["token-trim : LlmHook"]-- subgraph CHAIN["HookedLlmPort · built-ins run first (preCall only)"]- direction TB- PS --> PSA["force temperature / max_tokens / tool_choice<br/>append systemPromptParts"]- TT --> Q{"input tokens > budget?"}- Q -->|no| PASS["pass through unchanged"]- Q -->|yes| PP["reuse core/extensions/prePrune.ts passes<br/>drop / compact oldest messages"]- PP --> Q2{"under budget?"}- Q2 -->|no| PP- Q2 -->|yes| TRIM["trimmed request"]- end-- PSA --> OUT["mutated ToolCompletionRequest<br/>→ next hook / dispatch"]- PASS --> OUT- TRIM --> OUT-```--Both built-ins are `preCall`-only (no `moderate`/`postCall`); they mutate the request and hand it to-the next hook. Remote marketplace hooks (§3) run **after** the built-ins, by `order`.-------## 3 · Remote hooks (installable, out-of-process)--### 3.1 · `RemoteLlmHook` adapter · `mcp-host/src/llm/hooks/remoteHook.ts` (new)-An `LlmHook` whose methods POST to the hook's endpoint — its in-cluster `Service` (`image`/`service`-modes) or an external URL dialed directly with SSRF validation (`remote` mode, §3.5), resolved from the-`LlmHook` CRD (§3.3). Only the lifecycle methods the CRD's `lifecyclePoints` declare are wired; others-are `undefined` (skipped by the chain).--### 3.2 · Hook protocol (HTTP/JSON, `mcp-host → hook Service`)-Versioned under `/v1`. **Auth: a short-lived RS256 bearer token** (reuse the broker-token signer,-`control-api/src/utils/auth/*`) over a connection confined by NetworkPolicy (ingress-from-mcp-host-only).-No mTLS.-| Endpoint | Request body (redacted projection — §6) | Response |-|---|---|---|-| `POST /v1/pre_call` | `{ messages, tools?, model, params, usage, state, config }` | `{ action:'continue', patch?:{messages?,params?,systemPromptParts?} } \| { action:'reject', code, message }` |-| `POST /v1/moderate` | `{ messages, model, usage, config }` | `200 {}` = pass · `4xx { code, message }` = fail |-| `POST /v1/post_call` | `{ response:{content,tool_calls,finish_reason}, usage, state, config }` | `{ response }` (possibly redacted) |-| `POST /v1/on_error` | `{ error:{code,message,retryable}, messages, model, usage, state, config }` | `{ action:'reshape', error:{code,message} } \| { action:'recover', response:{content,tool_calls?} }` |--`RemoteLlmHook` maps a `pre_call` `patch` onto the local `ToolCompletionRequest` (mcp-host applies the-patch — the hook never gets the raw request object), maps `reject`/moderation-4xx onto an `LlmError`,-and maps an `on_error` `recover` onto a substitute `ToolCompletionResponse` (trust-gated + audited, §6).-`timeoutMs`/`failMode` come from the CRD.--### 3.3 · `LlmHook` CRD · `charts/clerum-crds/crds/llmhook.yaml` (new)-Namespaced `clerum.io/v1alpha1`, **modeled field-for-field on `mcpserver.yaml`** (including its-`image`-vs-`remote` split) so it reuses the same image-trust + egress machinery. Reconciled by the-**host-context-controller** (new `host-context-controller/src/llmHookReconciler.ts`, mirroring the-McpServer reconcile): `image` mode materializes a Deployment+Service+NetworkPolicy in `llm-hooks`;-`service` and `remote` modes deploy nothing — `service` wires an in-cluster Service reference, and-`remote` is dialed **directly** by mcp-host over its existing public-egress lane (§3.5).-```yaml-spec:- # Delivery — exactly ONE of image | service | remote (mirrors McpServer's image vs remote):- target:- image: # SELF-HOSTED: HCC deploys a Deployment + Service- ref: registry.evenfire.ai/hooks/pii-redact:1.2.0 # image-allowlist enforced- port: 8080- imagePullSecrets: [ ... ] # reuse evenfire-registry-pull- envSecret: pii-redact-creds # hook's OWN credentials (never the LLM secret)- egressBindings: [ { toFQDN: moderation.vendor.com, ports: [443] } ]- security: { addCapabilities: [] }- # service: # INTERNAL: an existing in-cluster Service — no pod deployed- # name: shared-guardrail; namespace: platform; port: 8080- # remote: # EXTERNAL: HTTPS endpoint mcp-host dials DIRECTLY (no proxy pod; §3.5)- # baseUrl: https://guardrail.vendor.com; authHeadersSecret: guardrail-creds- lifecyclePoints: [preCall, moderate, postCallSuccess, onError] # which /v1 endpoints it serves- order: 100 # chain position (built-ins run before)- scope: context # global | context (see §3.4)- failMode: closed # closed = a hook failure rejects the request- timeoutMs: 5000- onUnavailable: # per-hook down-behavior- mode: breaker # breaker | strict- failureThreshold: 5 # trips after N consecutive failures- cooldownMs: 30000 # fail-open + alert while tripped; re-probe after- appliesTo: # which requests this intercepts- models: ['*']- sourceKinds: ['channel','desktop','workflow']- config: { ... } # opaque, passed to the hook- managed: true # owned by HCC vs authored directly-```-- **Delivery modes** (three): `image` = HCC deploys a Deployment+Service and manages its lifecycle;- `service` = mcp-host calls an **existing in-cluster Service** (name/ns/port), nothing is deployed;- `remote` = an **external HTTPS endpoint mcp-host dials directly** (no proxy pod — §3.5), so no- third-party image runs in-cluster. The `image`-only fields (`imagePullSecrets`/`envSecret`/- `egressBindings`/`security`) apply solely to that mode.-- **Namespace:** `image`-mode hook workloads deploy into a dedicated platform namespace- **`llm-hooks`** (env `CONTROL_API_LLM_HOOKS_NAMESPACE`, default `llm-hooks`) — parallel to- `mcp-server` / `sandbox-recipes` / `sandbox-ui`. **Both** `global` and `context` scopes live in it;- context-scoping is enforced by a `context` label + `appliesTo` + NetworkPolicy (ingress only from the- matching mcp-host), **not** by per-context namespaces — keeping the egress lane a single- `mcp-host` → `llm-hooks` hop. On per-tenant managed clusters (where Host namespaces are- `mcp-host-<slug>`), the hooks namespace follows the same per-tenant override.-- **`scope`:** `global` applies to every Host in the deployment; `context` only to its Context/team.- mcp-host merges both (§3.4).-- **`onUnavailable`** (per-hook): `strict` = a down fail-closed hook blocks every call;- `breaker` = after `failureThreshold` consecutive failures the hook trips to fail-open (loud- metric/alert) for `cooldownMs`, then re-probes. `RemoteLlmHook` owns the breaker state.--**NetworkPolicy** (generated, `image` mode): the hook Service in `llm-hooks` accepts ingress **only**-from mcp-host pods on `port`; egress **only** per `egressBindings`; **capability floor** reuses-`@clerum/workflow-recipe-capability-policy`. `service` mode adds an egress lane to the referenced-Service; `remote` mode uses mcp-host's existing public-egress lane, validated app-layer (§3.5) — no-in-cluster hook pod and no proxy.--**Ownership (who reconciles what):** the marketplace install saga (§5.2) only writes the `LlmHook` CR-(+ its credential Secret). The **host-context-controller** reconciles that CR to the live-Deployment/Service/NetworkPolicy on every add/update/delete (`image` mode) — the same actor that-reconciles `McpServer`. mcp-host merely **watches** the CRs to build its hook chain (§3.4); it never-deploys anything.--### 3.4 · mcp-host discovery + hot reload · `mcp-host/src/llm/hooks/hookChainProvider.ts` (new)-mcp-host **watches `LlmHook` CRDs** in **two scopes** (global + per-Context): the-operator namespace (`scope: global`) and its own Context namespace (`scope: context`). It filters by-`appliesTo`, builds `RemoteLlmHook`s, and orders the chain as: **built-ins → global remote (by-`order`) → context remote (by `order`)**. Rebuild on CRD add/update/delete in either scope and hand-to the loop (hot reload, mirroring `main.ts` `refreshFailoverPolicy:406`). Each `RemoteLlmHook` owns-its `onUnavailable` breaker state so a down hook degrades per its declared mode, not globally.--### 3.5 · `remote`-mode egress — direct, matching the status quo--A `remote` hook is reached by **mcp-host dialing the external endpoint directly** — **not** through a-per-hook egress-proxy pod (the `McpServer.spec.remote` nginx pattern). This is a deliberate-simplification, and it adds **no new network-exposure class**, because it is exactly how mcp-host-already reaches external LLM providers today.--**Why it matches the status quo (current code):**-- mcp-host's namespace is default-deny (`deploy/base/mcp-host/networkpolicies.yaml:6-17`), with one- **broad external lane**: `443/TCP → 0.0.0.0/0` (and `80`), `except: []`- (`networkpolicies.yaml:108-121`). No per-host allowlist, no FQDN policy, no proxy.-- **LLM completions already ride that lane directly** — `new Anthropic({apiKey})` → `api.anthropic.com`- (`llm/claude.ts:30`), OpenAI-compatible providers use compiled-in baseURL constants- (`llm/registryCore.ts`). The provider baseURL set is a *functional table, not a network allowlist*.-- So a `remote` hook endpoint is the **same shape of call** as an LLM completion. Routing it through a- proxy while the LLM call goes direct would be inconsistent for no security gain — the proxy pattern- exists to fence *untrusted connector pods*, not the trusted mcp-host reaching out.--**Why the proxy's job is unnecessary here.** The nginx egress proxy on the MCP path does two things —-(a) fence an untrusted vendor pod's egress, and (b) hide the upstream credential from that pod. Neither-applies: mcp-host is first-party trusted code (there is no untrusted pod to fence), and it already holds-credentials (LLM secrets), so holding one hook's outbound auth is not a new trust-boundary crossing.--**Destination safety is enforced app-layer (primary control):**-1. **Config-only provenance.** The dial target comes *only* from the reconciled, admin-vetted,- `trust_level`-gated `LlmHook` CR — never from a model output, tool result, user input, or a hook's- own response. The destination is a control-plane fact, not a data-plane input.-2. **Reuse the existing SSRF guard.** The hook dial routes through the **same validation the- `http_request` tool already uses** (`core/tools/httpRequest.ts` + `core/safety/safety.ts`):- `https://` only, reject IP literals, **block RFC-1918 / loopback / link-local / metadata- `169.254.169.254` / `*.cluster.local`**, resolve A+AAAA and reject if any is private (fail-closed on- DNS failure), **DNS-pin** (connect to the validated IP with the original Host/SNI), and **do not- follow redirects**. This is a reuse, not new code, and it is what actually stops the one- high-severity threat — an admin-configured `baseUrl` pointing back at the cluster or metadata.-3. **Payload minimization** stays governed by the need-based/`trust_level` content projection (§6);- egress governs *destination*, the projection governs *payload*.--**L3 is the backstop, not the control.** The broad `0.0.0.0/0:443` lane bounds a *compromised* mcp-host-only weakly — but that is the **pre-existing posture for LLM calls**, so hooks do not regress it. Teams-that want network-level containment can add a Calico domain policy (`destination.domains`) scoped to the-admin-declared hook hosts + provider hosts, ideally programmed by HCC per `remote` hook — no proxy pod,-per-host precision, kept in sync with the CR. Note the current mcp-host external lane does **not**-exclude private/metadata ranges at L3 (`except: []`) — which is exactly why control (2) is load-bearing-rather than optional.-------## 4 · Config (built-ins) · Host CRD + `mcp-host/src/config.ts`-Built-in hook selection/order stays on the Host CRD (`spec.llmHooks`, parsed by-`mcp-host/src/llm/hooks/parseLlmHooks.ts`, hot-reloaded like `spec.llmPolicy`). Remote hooks are-**not** listed here — their presence is the `LlmHook` CRD itself. `CLERUM_LLM_HOOK_TIMEOUT_MS` default.-------## 5 · Marketplace registration & install (answering "how do they get into the registry")--Clone the connector path end-to-end. Nothing new in the trust model.--### 5.1 · Registry entry type · `evenfire-registry` (PR0) + `control-api/src/services/registryClient.ts`-Add `'llm_hook'` to `RegistryEntry.entry_type` (`registryClient.ts:198`) with a `hook_meta`:-`{ target: image|service|remote, lifecyclePoints[], credential_schema, defaultConfig, requiredEgress[], appliesToDefaults }`.-The `evenfire-registry` schema is **ours to extend** — the entry-type + `hook_meta` addition is **PR0**-(§7), a prerequisite we own, not an external coordination. `getEntryVersion` / `credential-schema` /-`bundle` / `digest` are already generic — reused as-is.--### 5.2 · Install saga · `control-api/src/routes/admin/registry.ts`-New `POST /admin/registry/install-hook`, cloned from `install-recipe` (`:1360+`) / `install` (`:961`):-1. `getEntryVersion` → **verify bundle `sha256` digest** (tamper check, cf. `:999`).-2. Fetch + validate `credential_schema`; create a K8s `Secret` from user creds (cf. `:1214`).-3. Build the `LlmHook` spec's `target` from `hook_meta` (`image` | `service` | `remote`), plus- lifecyclePoints, egress, envSecret ref.-4. **Image-allowlist preflight** via `@clerum/image-policy` `classifyPluginImage` (cf. `:1174`).-5. Stamp `clerum.io/catalog-id` + `catalog-version` annotations (`catalogAnnotations`, `:102`).-6. Create the `LlmHook` CRD; **rollback saga** on any failure (delete CRD + Secret).-7. Fire-and-forget `reportInstall` (`:1331`).--### 5.3 · Installed-state + catalog · `registry.ts:658`-Extend `getInstalledRegistryState` to list `LlmHook` CRDs and match by `catalog-id`, so-`GET /admin/registry/catalog` (`:718`) shows hooks as "installed."--### 5.4 · control-ui-Add a **"Guardrails / Hooks"** tab to `components/MarketplaceTabs.tsx`; an install form cloned from-`components/RegistryInstallForm/` (credential-schema-driven); surface `trust_level`/`quality_tier`-(already on the entry). Route alias `/marketplace/hooks → /registry`.--### 5.5 · Trust — 100% reuse-Image allowlist (`@clerum/image-policy`, `config.allowedPluginImagePrefixes`), sha256 bundle digest,-per-org pull secret (`registryPullSecretService`), capability whitelist-(`@clerum/workflow-recipe-capability-policy`), `trust_level`/`quality_tier`, install-time credential-`Secret`, per-workload egress NetworkPolicy. **No new trust primitives.**--### 5.6 · Publish/author path-A hook author ships a container image implementing the `/v1` protocol + a registry manifest-(`entry_type: llm_hook`, `hook_meta`). Publish via `POST /admin/registry/entries` (`publishEntry`,-`:764`) with org scoping. A reference hook image + template lives under `mcp-servers/`-style samples.-------## 6 · Security invariants-- **No third-party code in the mcp-host pod** — remote hooks run out-of-process (self-hosted pod in- `llm-hooks`, an existing in-cluster Service, or an external endpoint mcp-host dials directly — §3.5);- mcp-host only makes an authenticated HTTP call.-- **`remote`-mode destination safety** (§3.5): the dial target is **config-only** (the admin-vetted,- `trust_level`-gated `LlmHook` CR — never data-derived), validated by the reused `http_request` SSRF- guard (`https`-only, block private/loopback/link-local/metadata/`*.cluster.local`, DNS-pin,- no-redirect, fail-closed). The broad `0.0.0.0/0:443` NetworkPolicy is only an L3 backstop.-- **Hooks never receive LLM provider credentials.** `LlmHookContext.host` omits `llmSecretName`.-- **Content exposure is need-based + trust-gated.** A remote hook receives message/response- **content** only for the lifecycle points that require it — `moderate`/`postCallSuccess` get content,- `preCall` param-shaping gets model+params+metadata but not message bodies — **and** only if the- entry's `trust_level` clears a configurable bar (`config.minHookTrustLevel`); below the bar, install- is refused or the hook is content-starved. Every content-bearing hook call is audit-logged- (hook name, lifecycle point, request id — not the content).-- Hook's **own** credentials live in its **own** `Secret` (`envSecret`), never the LLM secret.-- NetworkPolicy: hook ingress **only** from mcp-host; egress **only** per `egressBindings`.-- **Fail-closed** default for `guardrail`/`pii-redact`; `token-trim`/observability fail-open. Per-hook.-- **`onError` `recover` fabricates the assistant's response**, so it is as powerful as content access:- only a hook whose `trust_level` clears the bar may recover, every recovery is audit-logged (hook- name + request id), and the substitute response is tagged **hook-originated** in telemetry — never- attributed to the model. A recovered response bypasses `postCallSuccess` (it is not model output).-- Bounded per-hook `timeoutMs`; a slow/hung hook cannot stall the reasoning loop; a rejected- `moderate` **aborts** the in-flight call so no tokens are wasted.-- `postCallSuccess` redaction runs **before** the response reaches the caller; telemetry stays in the- adapter below on raw counts (redacted content never re-serialized to logs/billing).-- Install-time: image allowlist + sha256 digest + trust_level surfaced for consent.-------## 7 · Sequencing / PRs-0. **PR0 (registry schema — prerequisite, we own it):** add `entry_type: 'llm_hook'` + `hook_meta` to- `evenfire-registry`. Gates PR3's install saga; no runtime effect on its own.-1. **PR1 (runtime core):** `LlmHook` types (incl. `onError` reshape/recover) + `HookedLlmPort` +- built-ins (`prompt-shaping`, `token-trim` on prePrune) + `taskExecutor` wiring (main lane + aux- lane restricted to trim/observability). Remote chain empty. Unit tests. No marketplace.-2. **PR2 (remote plumbing):** hook `/v1` protocol + `RemoteLlmHook` + `LlmHook` CRD + HCC- `llmHookReconciler` + mcp-host watch/`hookChainProvider` + hot reload.-3. **PR3 (control-api marketplace):** `entry_type: llm_hook`, `install-hook` saga, installed-state,- trust reuse.-4. **PR4 (control-ui):** marketplace Hooks tab + install form + installed badges.-5. **PR5:** metrics, docs (`docs/llm-providers/llm-hooks.md`), reference hook image.--## 8 · Design decisions-1. **Dedicated `LlmHook` CRD** (not a WorkflowRecipe capability block) → independent lifecycle + its own- marketplace `entry_type`. Recipe-bundled hooks are a possible later addition.-2. **Two classes:** built-ins in-process (no hop) vs remote (marketplace).-3. **Install scope = both** `global` (operator ns, all Hosts) **and** `context` (team ns); merge order- built-ins → global → context (§3.3/§3.4).-4. **Down-behavior = per-hook** `onUnavailable: strict | breaker`; breaker trips to fail-open with- alerting after N failures, then re-probes after cooldown (§3.3).-5. **`onError` may reshape OR recover:** a trust-gated, audited hook can substitute a safe response for- a preCall-reject / moderation-block / call-failure; otherwise the error surfaces (a block is a- `ContentFiltered` `LlmError`). Recovery bypasses `postCallSuccess` and is tagged hook-originated- (§1 / §6).-6. **Content exposure = need-based + trust-gated** (§6): content only to lifecycle points that need it,- gated by `trust_level`, audit-logged.-7. **Aux/compaction lane** runs only built-in `token-trim` + observability — no guardrail/PII on- internal summarization calls (§1.4).-8. **Protocol auth = short-lived RS256 token + NetworkPolicy** (ingress-from-mcp-host-only); no mTLS- (§3.2).-9. **Registry entry type** `entry_type: 'llm_hook'` + `hook_meta` is added in **PR0** (§7).--**Risks / out of scope:**-- **Streaming** is out of scope for the initial version (single-shot lane); `postCall`/`onError` take a- full response, so a future `postCallStreaming` chunk hook slots onto the same interface.-- **Latency:** each remote hook is a round-trip; mitigated by parallel moderation, in-process- built-ins, per-hook timeout, and `appliesTo` scoping.-- **Protocol versioning:** the `/v1` prefix; a CRD's `lifecyclePoints` must match what the image serves- (declared in `hook_meta`, probed at install).--## 9 · Verification-1. **HookedLlmPort unit tests** (mock inner + fake hooks): preCall order/mutation; a preCall reject, a- moderation block, and a call failure all route to the onError chain; parallel moderation fail aborts- the call; postCall reverse fold; onError **reshape** and **recover** (recover short-circuits, skips- remaining onError + postCall, returns the substitute response); per-hook timeout + `failMode`; empty- chain = pass-through.-2. **Built-in tests:** `token-trim` brings an over-budget message set under budget via the real token- counter; `prompt-shaping` forces params + injects a system part.-3. **RemoteLlmHook tests** (mock hook server): each `/v1` endpoint mapped correctly; reject/4xx →- `LlmError`; patch application; timeout → `failMode`.-4. **CRD/reconciler tests:** HCC materializes Deployment+Service+NetworkPolicy; ingress-from-mcp-host- only; egress per `egressBindings`; capability floor.-5. **Install saga tests** (mock registry): digest mismatch rejects; credential Secret created; CRD- stamped with catalog-id; rollback on failure; installed-state reflects the hook.-6. **Integration:** install a reference `pii-redact` hook from a mock registry → it appears installed- → a reasoning turn's response is redacted by the remote hook, fired once across a failover.-7. **Recovery gating:** an `on_error` `recover` from a below-bar `trust_level` hook is refused (the- error surfaces); an allowed recovery returns the substitute response, bypasses `postCallSuccess`,- and is tagged hook-originated in telemetry + audit-logged.-8. Confirm no LLM secret ever appears in a hook request body or hook logs.diff --git a/docs/features/mcp-host-guardrails/legacy/early-drafts/mcp-host-guardrails-unified-architecture.md b/docs/features/mcp-host-guardrails/legacy/early-drafts/mcp-host-guardrails-unified-architecture.mddeleted file mode 100644index c7db9d7ac..000000000--- a/docs/features/mcp-host-guardrails/legacy/early-drafts/mcp-host-guardrails-unified-architecture.md+++ /dev/null@@ -1,157 +0,0 @@-# Spec · MCP Host Guardrails — Unified Architecture (umbrella)--| | |-|---|---|-| **Type** | architecture / umbrella spec — decomposes into the suite in §2 |-| **Status** | draft for discussion |-| **Combines** | *(DRAFT) MCP Host Guardrails — Permission Rules, Hooks & Admin Policy* (tool lane) + [`llm-hooks-spec.md`](./llm-hooks-spec.md) (LLM lane) |-| **Companion** | [`guardrails-vs-llm-hooks-comparison.md`](./guardrails-vs-llm-hooks-comparison.md) — the agree / emphasize / disagree analysis this design resolves |-------## 0 · Thesis--The two specs are the same machine on two lanes: **intercept a call → run an ordered chain of policy-contributors → aggregate to a decision → execute at-most-once → emit bounded audit.** They differ in-*what* they intercept (an LLM completion vs a resolved tool call) and in the *trust model of their-hooks* (remote/marketplace vs local/digest-verified).--So the combination is **not** one merged document. It is:--1. a **shared guardrail spine** (the common decision/policy/audit machine),-2. **two lane adapters** (the two existing specs, re-homed on that spine), and-3. a **shared hook trust & delivery substrate** that makes the two hook models coexist as explicit tiers.--Everything the two specs *agree* on (comparison §2) becomes the spine; everything they *disagree* on-(comparison §5) is resolved once, at the spine (§3 below).-------## 1 · The unifying abstraction — one `GuardrailBoundary`--Define the boundary once; each lane instantiates it over its own call/result types.--| Stage | Shared contract | LLM lane (`llm-hooks`) | Tool lane (Guardrails) |-|---|---|---|---|-| **Intercept** | a call reaches a mandatory boundary (G1) | `LlmPort.completeWithTools` (`HookedLlmPort`) | resolved tool call before execution |-| **Context** | identity + resolved target + original/effective input | model, `usageContext` | resolved tool, provenance, args |-| **Contributors** | ordered chain of *rules* + *hooks* + *validators*, each emitting a decision | moderation, prompt-shaping, token-trim, PII hooks | permission rules, Pre hooks, hard validators |-| **Decision** | `allow / ask / deny / no_decision`, strict aggregation `deny > ask > allow > no_decision` | reject→`deny`, moderation-fail→`deny`, continue→`allow` | as authored |-| **Rewrite** | full input replacement, canonicalize + **revalidate** (G7) | `pre_call` `patch` | Pre-hook `updatedInput` |-| **Approve** | if `ask`: exact-call resolver, else fail-closed | (usually N/A — see §6) | `force_ask` snapshot/resume |-| **Execute** | **at-most-once**, snapshot-safe across resume (G11/G12) | one completion per logical request (above failover) | `executeSingleTool`, batch/resume |-| **Post** | transform the *model-visible* result; never alter the authoritative outcome | `post_call_success`; `on_error` recover (gated, §3) | `PostToolUse` |-| **Evidence** | bounded-enum metrics + redacted, digest-based audit (G15) | §6 audit | §13 audit |--A **contributor** is the shared unit: `{ phase: pre|post, source: admin_rule|host_rule|hook|validator,-decision, reasonCode, rewrite?, capabilities? }`. Rules are declarative contributors; hooks are executable-contributors. The lane supplies the input/result types; the spine supplies the algebra.-------## 2 · Spec suite (thinking in multiple specs)--| Spec | Owns | Sourced from |-|---|---|---|-| **S0 — this doc** | the umbrella: shared abstraction, reconciliation, spec map, migration | new |-| **S1 — Guardrail Core** | decision algebra + strict aggregation, the contributor contract, at-most-once + snapshot/resume, admin-policy model + **HCC atomic-artifact delivery**, bounded metrics/audit, reason-code registry | shared parts of both (Guardrails §3–6, §10, §11, §13; llm-hooks §1.2–1.3, §6) |-| **S2 — Tool-lane adapter** | permission **rules** + typed argument predicates, provenance, `Pre/PostToolUse`, approvals/`force_ask`, doom-loop, persistence/scanner hardening | the colleague's spec, re-homed |-| **S3 — LLM-lane adapter** | `pre_call`/`moderation`/`post_call`/`on_error`, built-ins (`prompt-shaping`/`token-trim`/PII), `HookedLlmPort` placement above failover | `llm-hooks-spec.md`, re-homed |-| **S4 — Hook trust & delivery substrate** | the **two hook tiers** (§3.1), digest/allowlist verification (Tier A), marketplace registry + `install` saga + image-allowlist + `trust_level` (Tier B), egress policy, credential isolation | Guardrails §9 + llm-hooks §3/§5, unified |--S1 and S4 are the new work; S2 and S3 are the existing specs demoted to **adapters** that conform to S1-and consume S4. Neither existing doc is thrown away — each keeps its lane-specific depth.-------## 3 · Reconciling the disagreements (resolved at the spine)--The comparison's §5 conflicts, decided once:--| Disagreement | Unified resolution | Why it holds |-|---|---|---|-| **Where a hook runs** (local vs remote) | Not either/or — **two tiers** (§3.1). A hook declares its tier; the spine + admin policy decide which tiers a given lane/Context may use. | A deterministic tool check and a network moderation call are both legitimate; the tier makes the trust rules explicit rather than global. |-| **Network & side-effects** | Derived from tier: **Tier A = no network / deterministic**; **Tier B = network-permitted, egress-allowlisted**. | Preserves the colleague's determinism guarantee where it matters and our vendor-call capability where it's needed. |-| **Who authors/installs** | **Admin policy governs both tiers.** Tier A = admin-allowlisted digests. Tier B = marketplace install that must satisfy a Context allowlist + `trust_level` floor. | Adopts his non-overridable Context authority as the single governance model for both. |-| **When a hook is down** | **Fail-closed is the default and is mandatory for any decision-authoritative (deny-capable) hook.** A `breaker`/fail-open is permitted **only** for non-authoritative (advisory/observability) hooks and **only** when admin policy opts in. | Keeps his strict posture for anything that can block; allows our availability trade-off exactly where it's safe. |-| **May a hook fabricate the outcome?** | Recovery/substitution is a **named capability** (`may_substitute_result`) an admin must grant; the substitute is **clearly hook-originated + audited** and **never alters the stored authoritative outcome** (his G12 holds). | Our `on_error recover` survives, but as a gated, auditable exception rather than a default power. |-| **Decision expressiveness** | Adopt the **4-valued algebra + strict aggregation for both lanes**. The LLM lane gains `ask`/`no_decision` (used rarely — §6). | One decision model; the LLM lane's simpler cases are just the subset it uses. |-| **Config authority** | The **non-overridable Context admin policy** governs rules, allowed hook tiers, allowed hook digests/entries, allowed approval policies, and limits — for **both** lanes. | Single governance surface; marketplace installs and local hooks both bind to it. |--### 3.1 · The tiered hook model (the load-bearing move)--| | **Tier A — trusted local** | **Tier B — remote / marketplace** |-|---|---|---|-| Runs as | least-privilege child process in the workload | own pod / in-cluster Service / external endpoint |-| Network | none (deterministic transform) | permitted, egress-allowlisted |-| Trust | admin-allowlisted, digest-pinned | image-allowlist + sha256 digest + `trust_level` |-| Author | first-party / operator-vetted | third-party marketplace |-| Fail-mode | fail-closed (may be deny-authoritative) | fail-closed if deny-authoritative; `breaker` only if advisory + admin-opted-in |-| Typical use | tool-lane permission checks, high-assurance guardrails | LLM moderation/PII that call vendor services |--**Rule:** admin policy declares, per Context and per lane, which tiers are admissible and which-capabilities (`may_deny`, `may_rewrite`, `may_substitute_result`) each hook is granted. A lane can require-Tier A for anything decision-authoritative and allow Tier B only for advisory contributions — which is-exactly how the two specs' instincts differ, now expressible instead of contradictory.-------## 4 · How the existing specs slot in (no rework of their depth)--- **Tool lane (S2)** already speaks the spine's language: its `allow/ask/deny/no_decision`, strict- aggregation, `Pre/PostToolUse`, at-most-once/resume, and admin policy **are** the spine's canonical- forms. It contributes almost all of S1. Its hooks are **Tier A**.-- **LLM lane (S3)** maps on cleanly: `pre_call`→pre contributor (rewrite/deny), `moderation`→pre- contributor (deny/ask), `post_call_success`→post contributor, `on_error recover`→post with- `may_substitute_result`, `token-trim`/`prompt-shaping`→rewrite contributors. Its remote hooks are- **Tier B**; its built-ins are in-process **Tier A**-equivalent (first-party, no network).--Net: the tool-lane spec is ~the spine already; the LLM-lane spec is the same machine specialized to the-model call plus the Tier-B substrate.-------## 5 · Sequencing / migration--1. **S1 — Guardrail Core** first: lift the decision algebra, contributor contract, at-most-once/resume,- admin-policy model, and audit/metrics out of the tool-lane spec into a lane-neutral core.-2. **S4 — Trust substrate**: define Tier A (local digest-verified execution) + Tier B (marketplace/remote)- and the admin-policy tier/capability grants.-3. **S2 / S3 — adapters**: re-home each existing spec as an adapter that *conforms to* S1 and *consumes*- S4. Most of their text is unchanged; the delta is pointing shared concepts at S1/S4 instead of- redefining them.-4. Both existing docs get a one-line header: "Lane adapter of the unified guardrail architecture (S0);- shared model in S1, trust substrate in S4."--No production code, CRD, or deployment change is authorized by this document (consistent with both-source specs); this is a specification decomposition.-------## 6 · Open decisions for the authors--Genuine forks the unification surfaces — worth an explicit call before S1 is written:--1. **Does the LLM lane need `ask` (human approval before a model call)?** Likely rare (cost/risk gating on- an expensive model, or a jailbreak-suspected turn). If never needed, the LLM adapter uses only- `allow`/`deny`/`no_decision` and `ask` is a tool-lane-only feature of the shared algebra.-2. **May tool-lane guardrails ever be Tier B (remote)?** The colleague's spec says local-only. Holding- that line is safest; allowing a remote tool guardrail (strongly gated) would let teams reuse a shared- policy service across lanes. Decide whether Tier B is admissible on the tool lane at all.-3. **Is `may_substitute_result` allowed on the tool lane** (substitute a tool result), or LLM-only?- His model forbids altering a tool outcome; keeping substitution LLM-only preserves that.-4. **Where does Guardrail Core live** — an in-process mcp-host library, or a shared module both the tool- and LLM lanes import? (Both lanes already run inside mcp-host, so a shared library is the likely form.)-5. **Unify the reason-code registries** into one enum, or keep per-lane registries that share a bounded-label- discipline? A single registry eases cross-lane dashboards.-6. **Capability granularity** — is `{may_deny, may_rewrite, may_substitute_result}` the right minimal set,- or do we also need `may_ask` and `may_add_context` as separately grantable?-------## 7 · Explicitly out of scope (stays lane-specific)--The spine must **not** absorb lane mechanics that don't generalize: the tool lane's typed argument-predicates (`path`/`url`/`command`/`json`), provenance-from-registry, doom-loop guard, and persistence-scanner; and the LLM lane's `token-trim`/`prompt-shaping` built-ins, model-context projection, and-`HookedLlmPort` placement above failover. These live in S2/S3, not S1.diff --git a/docs/features/mcp-host-guardrails/legacy/split-suite/00-unified-architecture.md b/docs/features/mcp-host-guardrails/legacy/split-suite/00-unified-architecture.mddeleted file mode 100644index dc1684f5d..000000000--- a/docs/features/mcp-host-guardrails/legacy/split-suite/00-unified-architecture.md+++ /dev/null@@ -1,125 +0,0 @@-# S0 · MCP Host Guardrails — Unified Architecture--| | |-|---|---|-| **Type** | architecture / umbrella spec |-| **Status** | draft for discussion |-| **Suite** | S1 [core](./01-guardrail-core.md) · S2 [tool-lane](./02-tool-lane-adapter.md) · S3 [llm-lane](./03-llm-lane-adapter.md) · S4 [trust & delivery](./04-hook-trust-and-delivery.md) |-| **Scope** | specification only — no runtime/CRD/deployment change is authorized here |-------## 0 · Thesis--The MCP Host applies guardrails on two lanes — the **LLM completion** and **tool execution** — and both-are the same machine: *intercept a call → run an ordered chain of policy contributors → aggregate to a-decision → execute at-most-once → emit bounded audit.* They differ only in **what** they intercept and-in the **trust model of their hooks**.--So the suite splits that machine into three parts rather than merging everything into one document:--- **One shared decision engine — S1 · Guardrail Core.** The actual guardrail logic that *both* lanes- use: the `allow`/`ask`/`deny` decision and how contributions aggregate, running each call- at-most-once, the admin policy, and the audit trail.-- **Two thin per-lane layers — S2 (tool) and S3 (LLM).** Each one only describes what is specific to- its kind of call (tool arguments vs an LLM request) and hands everything else to S1.-- **One shared "how hooks run and are trusted" layer — S4.** Custom hooks are **installed as- containers** (any language), run out-of-process as a pod / in-cluster Service / external endpoint; the- platform's own hooks are **built-ins** compiled into mcp-host. There is no local-executable or- CRD-scripting hook.--At runtime the two lanes **stack**: a request first passes the LLM-lane gate, the model it returns emits-a tool call, and that call then passes the tool-lane gate before it executes — the tool result feeds the-next turn.--```mermaid-flowchart LR- REQ(["request / task"]) --> G1["S3 · LLM-lane gate<br/>pre_call · moderate · post_call · on_error"]- G1 -->|allowed request| MODEL["LLM completion"]- MODEL -->|"emits a tool call"| G2["S2 · Tool-lane gate<br/>rules · Pre/PostToolUse · approval"]- G2 -->|allowed call| EXEC["tool executes<br/>(at-most-once)"]- EXEC -.->|"result feeds the next turn"| G1- G1 -.->|deny / transform| OUT(["caller"])- G2 -.->|deny| OUT-```--Both gates are the *same* machine — one `GuardrailBoundary` (§1), configured by the shared engine (S1)-and delivered with hooks from S4. What differs is only the lane-specific configuration in S2 / S3.-------## 1 · The unifying abstraction — one `GuardrailBoundary`--Each lane instantiates one boundary over its own call/result types (defined fully in S1):--| Stage | Shared contract (S1) | LLM lane (S3) | Tool lane (S2) |-|---|---|---|---|-| Intercept | a call reaches a mandatory boundary | `HookedLlmPort` at `LlmPort` | resolved tool call before execution |-| Contributors | ordered chain of **rules + hooks + validators** | moderation, prompt-shaping, token-trim, PII | permission rules, Pre hooks, hard validators |-| Decision | `allow / ask / deny / no_decision`, strict aggregation `deny > ask > allow > no_decision` | reject→`deny`, moderation-fail→`deny`, continue→`allow` | as authored |-| Rewrite | full input replacement, canonicalize + revalidate | `pre_call` patch | Pre-hook `updatedInput` |-| Execute | **at-most-once**, snapshot-safe | one completion per logical request (above failover) | `executeSingleTool`, batch/resume |-| Post | transform the model-visible result; never alter the authoritative outcome | `post_call`; `on_error` recover (gated) | `PostToolUse` |-| Evidence | bounded-enum metrics + redacted, digest-based audit | S3 audit | S2 audit |--A **contributor** is the shared unit: `{ phase: pre|post, source: admin_rule|host_rule|hook|validator,-decision, reasonCode, rewrite?, capabilities? }`. Rules are declarative contributors; hooks are-executable contributors delivered per S4.-------## 2 · How the lanes' differences reconcile--The two lanes independently agree on the trust primitives (universal mediation, no-secrets-in-hooks,-digest/allowlist trust, HCC-delivered admin policy, at-most-once, bounded audit). Where they diverge,-the resolution lives in the shared engine (S1):--| Divergence | Resolution | Home |-|---|---|---|-| Hook **execution model** (local executable vs remote service) | **No local-executable hook.** Custom hooks are **installed containers** (any language) run out-of-process; only first-party **built-ins** run in-process. The deterministic, deny-authoritative in-process logic a local executable was meant to provide is served instead by the declarative rules (S2) + built-ins. | S4 |-| Hook **network / side-effects** | installed hooks are egress-allowlisted; built-ins have no network of their own | S4 |-| Who **installs** / trust | one **non-overridable admin policy on the Host CRD** (the mcp-host's own CR; the runtime can't mutate it): allowed installed-hook digests + a `trust_level` floor + capability grants | S1 + S4 |-| **Fail-open vs fail-closed** on hook-down | fail-closed is mandatory for any deny-authoritative hook; a `breaker` fail-open is allowed only for advisory hooks the admin opts in | S1 + S4 |-| Hook **fabricating the outcome** | a named, admin-granted, audited `may_substitute_result` capability that never alters the stored outcome | S1 + S4 |-| **Decision expressiveness** | the 4-valued algebra + strict aggregation for both lanes | S1 |--The load-bearing move is dropping the local-executable model entirely, leaving two kinds of hook:-**built-in** (first-party, compiled into mcp-host, in-process) and **installed** (a container in any-language, run out-of-process as a pod / Service / external endpoint). Custom logic is therefore authored-in the language of choice as an ordinary container — not a script wedged into the CRD — while the-high-assurance core stays in-process as declarative rules (S2) + built-ins. The Host-CRD admin policy-(S1 §7) declares, per lane, which installed hooks (by digest + `trust_level`) and which capabilities are-admissible — so the two specs' opposite instincts are settled by choosing containers for all custom logic.-------## 3 · Sequencing--1. **S1 (Core)** — lift the decision algebra, contributor contract, at-most-once/resume, admin-policy- model, and audit out of the lane specifics into a lane-neutral core.-2. **S4 (Hook trust & delivery)** — define the built-in vs installed model, installed-hook delivery/trust,- and the admin-policy hook/capability grants.-3. **S2 / S3 (adapters, in parallel)** — each conforms to S1 and consumes S4; keeps its lane depth.--Dependencies: S1 → {S4, S2, S3}; S4 → {S2, S3}. The open decisions in §4 gate S1's scope.-------## 4 · Open decisions--1. Does the **LLM lane need `ask`** (human approval before a model call)? If not, `ask` is tool-lane-only.-2. **Installed-hook latency on the tool lane** — a container round-trip per `Pre/PostToolUse` adds latency- to every guarded tool call. Acceptable, or do the hottest checks need an in-process built-in fast-path?-3. Is **`may_substitute_result`** allowed on the tool lane, or LLM-only?-4. Is **Guardrail Core** an in-process shared library (both lanes run in mcp-host) or a separate module?-5. **Unify the reason-code registries**, or keep per-lane with a shared bounded-label discipline?-6. Minimal **capability set** — is `{may_deny, may_rewrite, may_substitute_result}` enough, or also- `may_ask` / `may_add_context`?-------## 5 · Out of scope for the shared engine (S1)--S1 must not absorb lane mechanics: the tool lane's typed argument predicates, provenance, doom-loop,-and persistence scanner (S2); and the LLM lane's `token-trim`/`prompt-shaping` built-ins and-`HookedLlmPort` placement (S3). Those stay in the adapters.diff --git a/docs/features/mcp-host-guardrails/legacy/split-suite/01-guardrail-core.md b/docs/features/mcp-host-guardrails/legacy/split-suite/01-guardrail-core.mddeleted file mode 100644index 1817c6493..000000000--- a/docs/features/mcp-host-guardrails/legacy/split-suite/01-guardrail-core.md+++ /dev/null@@ -1,167 +0,0 @@-# S1 · Guardrail Core (lane-neutral)--| | |-|---|---|-| **Type** | design spec |-| **Status** | draft for discussion |-| **Consumed by** | S2 [tool-lane](./02-tool-lane-adapter.md), S3 [llm-lane](./03-llm-lane-adapter.md) |-| **Depends on** | S4 [trust & delivery](./04-hook-trust-and-delivery.md) for how hook contributors are invoked |--The lane-neutral machine every guarded call passes through. A lane supplies the concrete **input** and-**result** types; this core supplies the decision algebra, the contributor contract, execution safety,-admin policy, and evidence.-------## 1 · The `GuardrailBoundary`--One mandatory boundary per guarded call: `Intake → Contributors → Aggregate → (Approve) → Execute → Post-→ Evidence`. It is universal — native, remote, bridged, first-run, resumed, interactive, and unattended-calls all pass the same boundary before execution.--Lane-supplied types: `Input` (e.g. tool arguments / an LLM request), `Result` (tool result / LLM-response), `ResolvedIdentity` (the *real* target after any bridge resolution — never inferred from a name-pattern).--```mermaid-flowchart LR- IN["Intake<br/>resolve identity + input"] --> C["Contributors (pre)<br/>rules + hooks + validators"]- C --> AGG{"Aggregate<br/>deny > ask > allow > no_decision"}- AGG -->|deny| DENY["Denied<br/>bounded result + audit"]- AGG -->|ask| APPR{"Approve<br/>exact-call resolver?"}- AGG -->|allow / no_decision| EXEC["Execute<br/>at-most-once"]- APPR -->|approved| EXEC- APPR -->|no resolver / denied| DENY- EXEC --> POST["Post contributors<br/>transform model-visible result"]- POST --> EV["Evidence<br/>bounded metrics + redacted audit"]- DENY --> EV-```--## 2 · Contributor contract--A **contributor** is the unit every source produces:--```-Contributor = {- phase: 'pre' | 'post'- source: 'admin_rule' | 'host_rule' | 'hook' | 'validator' | 'current' // 'current' = the existing approval path- sourceId: string // stable configured id- decision: 'allow' | 'ask' | 'deny' | 'no_decision'- reasonCode: <bounded enum>- rewrite?: Input // full replacement of the input (pre only)- substitute?: Result // only if granted may_substitute_result (§6)- audit: { modelReason?, userReason?, details? } // redacted, bounded; not copied across surfaces-}-```--- **Rules** are declarative contributors (the lane defines their match language). **Hooks** are executable- contributors; *how* a hook is executed (local child-process vs remote call) is S4's concern, not the- algebra's.-- `modelReason` / `userReason` / `details` serve different consumers and MUST NOT be copied between- surfaces without explicit redaction.--## 3 · Decision algebra & aggregation--- **Strictness order:** `deny > ask > allow > no_decision`. The boundary outcome is the **strictest**- contribution across all matching sources; every contribution is retained in audit.-- **`no_decision`** means "no guardrail source decided" → the lane's *defer* behavior (the existing approval path).-- **Unmatched default is explicit:** when a lane has a non-empty rule set, an unmatched call contributes- `ask` (not silently `allow`); when it has no guardrail config at all, absence contributes `no_decision`.- A default is never synthesized when guardrails are absent (no-config compatibility, §7).-- **Tie-break for presentation only:** when one bounded reason must be chosen for a metric or a- model-facing result, break ties deterministically (admin_rule → hook order → host_rule → lexical- sourceId). This selects *presentation*, never severity.-- **Malformed input fails closed:** an unknown decision value or an out-of-range predicate is rejected at- admission; it never aggregates as permissive.--## 4 · Rewrite safety--A `pre` contributor's `rewrite` **replaces the entire `Input`**, which is then canonicalized and-**re-validated** before the next contributor sees it. A contributor can never *weaken* an earlier-stricter decision. Rewrites are re-evaluated, so an earlier `allow` on the original input is never reused.--## 5 · Execution safety — at-most-once, snapshot, resume--- **At-most-once:** an approved call executes exactly once; suspension, restart, retry, and batch resume- never execute a call more than once.-- **Snapshot:** a suspended call persists an immutable snapshot — resolved identity, original + effective- input digests, matched rule/hook ids + decisions + rewrite digests, admin-policy digest, execution- mode, and state (`pending | approved | denied | executing | executed | post_complete`).-- **Resume:** load the snapshot, verify identity + effective-input digest, verify hook artifacts still- match stored digests **without re-running pre hooks**, **re-load current admin policy and deny if it is- now stricter or missing**, atomically claim `executing`, run once, then run post hooks under a stable- idempotency key. An approval for a different digest/identity/expired request is invalid.--## 6 · Capabilities--Contributor powers beyond `allow/deny` are **explicit, admin-granted** capabilities (§ admin policy):-`may_rewrite`, `may_ask`, `may_substitute_result`, `may_add_context`. In particular **`substitute` never-alters the stored authoritative outcome** — it changes only the *caller-visible* result, is clearly-tagged as contributor-originated, and is audited.--## 7 · Administrative policy--- **Shape** — a **single** admin-authored block, `Host.spec.guardrails`, on the **Host CRD** (the- mcp-host's own CR that HCC reconciles to instantiate the process). There is **no separate `adminPolicy`- sub-object**: the whole Host CR is admin-authored and the runtime can't mutate it, so everything in the- block is already non-overridable. A mcp-host runs exactly the hooks this block lists — installed hooks- are **opt-in per Host**. Rendered into the Host `openAPIV3Schema`:--```yaml-guardrails:- type: object- properties:- rules: # ordered allow/ask/deny rules — tool-lane predicate shape in S2 §2- type: array- items: { type: object } # { id, action: allow|ask|deny, reasonCode, match{ tool, arguments[] } }- hooks: # installed hooks this mcp-host runs, by phase (each an installed LlmHook CR, S4)- type: object # each entry: { id: string, digest: string } (expected sha256 of the hook image)- properties:- preToolUse: { type: array, items: { type: object } }- postToolUse: { type: array, items: { type: object } }- preCall: { type: array, items: { type: object } }- moderate: { type: array, items: { type: object } }- postCallSuccess: { type: array, items: { type: object } }- onError: { type: array, items: { type: object } }- builtins: # first-party hooks compiled into mcp-host — types in S3 §3- type: array- items: { type: object } # { type: prompt-shaping|token-trim|…, order, failMode, timeoutMs, config }- minInstalledHookTrustLevel: { type: string, enum: [low, mid, high] } # trust floor for installed hooks- approvalPolicies: { type: array, items: { type: string, enum: [cli_only, channel_users, designated_approvers] } }- capabilityCeiling: { type: array, items: { type: string, enum: [may_deny, may_rewrite, may_substitute_result, may_add_context] } }- limits:- type: object- properties:- maxRules: { type: integer, default: 100 }- maxHooksPerPhase: { type: integer, default: 8 }- maxHookTimeoutMs: { type: integer, default: 5000 }- maxHookOutputBytes: { type: integer, default: 65536 }-```-- **Non-overridable by construction:** the Host CR is authored only through control-api admin RBAC and- reconciled by HCC; the **mcp-host runtime cannot mutate its own Host CR**, so it cannot weaken the- guardrails. Within the block, no rule or hook can weaken a `deny` — the strictest contribution wins (§3).-- **Delivery:** the admin policy rides the path HCC **already** uses to instantiate the mcp-host from its- Host CR — no cross-object artifact. HCC validates schema/digests/limits and normalizes it; mcp-host- reads its Host state. If policy is declared but the Host is not reconciled/Ready, guarded execution- **fails closed**; a malformed policy never replaces the last valid one; the enforced policy digest is- recorded for integrity/audit and never falls back to a weaker policy on mismatch.-- **No-config compatibility:** with no admin policy, host rules, or hooks, behavior is identical to- current — absence contributes `no_decision`.--## 8 · Evidence — metrics & audit--- **Metrics** use **fixed-enum labels only** (`decision`, `source`, `execution_mode`, `phase`, `outcome`,- `reason_code`, plus lane-supplied bounded dimensions). Ids, paths, args, free-form reasons, and digests- are **forbidden** as labels.-- **Audit** events carry timestamps, identity linkage, original + resolved identities, decision + source- ids, bounded reason codes, original/effective **input digests**, policy/rule/hook digests, execution- state, and redaction status. Raw sensitive inputs, hook stdout/stderr, and credentials are excluded- unless a separate protected-trace contract permits a redacted form.-- **Reason-code registry** is a single bounded enum (see §4/§8 open decision in S0 on per-lane vs unified).--## 9 · Invariants (lane-neutral)--Universal mediation · resolved-identity evaluation · strict aggregation · rewrite-then-revalidate ·-at-most-once · authoritative-outcome-immutable · admin-cannot-be-weakened (policy on the Host CRD, which-the mcp-host runtime can't mutate) · authoritative-delivery (HCC-reconciled, digest-checked) ·-bounded/redacted evidence · no-config compatibility.diff --git a/docs/features/mcp-host-guardrails/legacy/split-suite/02-tool-lane-adapter.md b/docs/features/mcp-host-guardrails/legacy/split-suite/02-tool-lane-adapter.mddeleted file mode 100644index 1c558cc6b..000000000--- a/docs/features/mcp-host-guardrails/legacy/split-suite/02-tool-lane-adapter.md+++ /dev/null@@ -1,136 +0,0 @@-# S2 · Tool-lane adapter--| | |-|---|---|-| **Type** | design spec (lane adapter) |-| **Status** | draft for discussion |-| **Conforms to** | S1 [core](./01-guardrail-core.md) · **Consumes** S4 [trust & delivery](./04-hook-trust-and-delivery.md) |-| **Source of record** | the colleague's draft *MCP Host Guardrails — Permission Rules, Hooks & Admin Policy* (full tool-lane detail); this adapter re-homes it on S1/S4 |--Specializes the `GuardrailBoundary` over **tool execution** (native + MCP tools). Everything not below —-the decision algebra, aggregation, at-most-once/resume, admin policy, audit — comes from S1; hook delivery-(installed containers, any language) comes from S4.-------## 1 · Lane types--- **`Input`** = resolved-tool arguments; **`Result`** = the tool result; **`ResolvedIdentity`** = the real- tool after dynamic-bridge resolution.-- **Provenance** (`native` vs `mcp`, server + name) comes from the **registry**, never inferred from a- `__` name pattern. A `clerum__tool_call` bridge rule evaluates the *resolved* target; it MAY additionally- constrain the original bridge identity but never instead of the resolved target.--```mermaid-flowchart TB- T["resolved tool call<br/>(provenance from registry)"] --> R["permission rules<br/>(typed predicates)"]- T --> P["PreToolUse hooks (installed)<br/>sequential · may rewrite"]- R --> A{"aggregate (S1)<br/>+ doom-loop guard"}- P --> A- A -->|deny| D["Denied<br/>bounded result + audit"]- A -->|ask| AP{"approval resolver?"}- A -->|allow / no_decision| HV["hard validators + containment"]- AP -->|approved / interactive| HV- AP -->|unattended · none| D- HV --> X["execute at-most-once"]- X --> PT["PostToolUse (once)<br/>redact model-visible result"]-```--## 2 · Permission rules (declarative contributors)--`Host.spec.guardrails.rules[]` with `action: allow | ask | deny`, matched on provenance + **typed-argument predicates**. The containing `Host.spec.guardrails` block (rules, hooks, knobs) is defined in-**S1 §7**; this is the tool-lane shape of a `rules[]` **item**:--```yaml-# item of Host.spec.guardrails.rules (S1 §7)-type: object-required: [id, action, match]-properties:- id: { type: string }- action: { type: string, enum: [allow, ask, deny] }- reasonCode: { type: string }- match:- type: object- required: [tool]- properties:- tool:- type: object- required: [provenance]- properties:- provenance: { type: string, enum: [native, mcp] } # from the registry, never string-parsed- server: { type: string }- name: { type: string }- arguments:- type: array- items:- type: object- required: [type, pointer, op]- properties:- type: { type: string, enum: [path, url, command, json] }- pointer: { type: string } # constrained RFC 6901 JSON Pointer- op: { type: string } # per type — see the predicate list below- value: { x-kubernetes-preserve-unknown-fields: true }-```--The `preToolUse`/`postToolUse` **hooks** this lane runs are listed under `Host.spec.guardrails.hooks` (S1 §7).--Predicate `op` values by `type`:--- **`path`** — `equals | under | outside`; normalized, symlink-aware, boundary-safe containment; prefix- strings are forbidden.-- **`url`** — `scheme_in | host_in | port_in | path_under`; parsed + normalized; credentials-in-URL rejected.-- **`command`** — `executable_is | argv_prefix`; a structured executable + argv, not a parsed shell string.-- **`json`** — `exists | equals | one_of | contains`; size- and depth-bounded; object-key order canonicalized.--Predicates name constrained RFC-6901 JSON Pointers; invalid pointers / excess depth / excess count / over--limit values are **rejected at admission**. Wildcards support only `*` and `?`, anchored, bounded length-— no regex. Array order is never security precedence (all matching rules contribute; S1 aggregates).--Unmatched default follows S1: non-empty rules → `ask`; absent → `no_decision` (defer to the existing approval path).--## 3 · Pre/PostToolUse hooks--Executable contributors, delivered as **installed** hooks (S4): a container image in any language, run-out-of-process (pod / in-cluster Service / external endpoint), reached over the `/v1` protocol, and-governed by image-allowlist + digest + `trust_level` + NetworkPolicy. There is no local-executable /-CRD-scripting hook (S4 §1); deny-authoritative custom logic that must be guaranteed-local is a built-in.--- **PreToolUse** — runs sequentially **before** approval; returns `{decision, reasonCode, updatedInput,- additionalContext}`. `updatedInput` (only with `allow`/`ask`) is a full input replacement, canonicalized- + revalidated (S1 §4). Failure/timeout → declared fail-mode (decision-making pre hooks default **closed**).-- **PostToolUse** — runs **once** after the single execution attempt; may redact/replace the *model-visible*- result, add bounded context, or recommend stop. It **cannot** undo side effects, mark the execution- denied, or alter the stored outcome (S1 §5/§6).--## 4 · Approvals — interactive & unattended--Execution mode is set by the **availability of an exact-call approval resolver**, not by cron origin.--- **Interactive** (`ask`/`force_ask`): one pending request bound to the resolved tool + effective-input- digest + rule/hook digests; choices are `approve_once | deny`. A broad wildcard/prior approval **cannot** satisfy- an explicit `ask`. This creates no persistent rule.-- **Unattended:** explicit `deny` → deny; `ask` with no resolver → **deny** (`approval_unavailable`); `allow`- → proceed through hard validators + containment; `no_decision` → preserve current unattended behavior.-- The existing `*`, exact-tool, and MCP-server approvals apply **only** inside `no_decision`/defer.--Snapshot/resume for suspended calls uses S1 §5; batches are left-to-right with earlier denied/completed-results preserved.--## 5 · Doom-loop guard--Within one task, three consecutive identical `(resolved tool, effective-input digest)` calls →-`deny (repeated_identical_call)`. Any intervening different tool or input resets the counter; the counter-is persisted with task state.--## 6 · Persistence / prompt-injection hardening--Rules and hooks evaluate memory and file tools including canonical paths after rewrites, but **path denial-is not a substitute for content scanning**: raw `file_write` MUST route through the `WorkspaceService`-scanner, and `daily/*` memory is treated as prompt-affecting persistent state.--## 7 · Hard validators & containment (unchanged)--Existing structural validators, MCP scoping, token restrictions, NetworkPolicy, and the stateless-cron-manage containment remain authoritative and independent of guardrail outcome — a guardrail `allow`-never bypasses them.diff --git a/docs/features/mcp-host-guardrails/legacy/split-suite/03-llm-lane-adapter.md b/docs/features/mcp-host-guardrails/legacy/split-suite/03-llm-lane-adapter.mddeleted file mode 100644index 93de1b4f2..000000000--- a/docs/features/mcp-host-guardrails/legacy/split-suite/03-llm-lane-adapter.md+++ /dev/null@@ -1,130 +0,0 @@-# S3 · LLM-lane adapter--| | |-|---|---|-| **Type** | design spec (lane adapter) |-| **Status** | draft for discussion |-| **Conforms to** | S1 [core](./01-guardrail-core.md) · **Consumes** S4 [trust & delivery](./04-hook-trust-and-delivery.md) (built-ins + installed hooks) |--Specializes the `GuardrailBoundary` over the **LLM completion call**. Decision algebra, aggregation,-at-most-once, admin policy, and audit come from S1; hook delivery (in-process built-ins and remote hooks)-comes from S4.-------## 1 · Lane types & placement--- **`Input`** = the `ToolCompletionRequest` (messages, tools, params, `usageContext`); **`Result`** =- the `ToolCompletionResponse`; **`ResolvedIdentity`** = model + provider.-- The boundary is a **`HookedLlmPort`** decorator (`mcp-host/src/core/adapters/hookedLlmPort.ts`) wrapping- the effective `LlmPort` **above failover**, so contributors fire **once per logical request**, not per- fallback attempt. Wired in `taskExecutor.buildLoopConfig`.--## 2 · Lifecycle → S1 contributor mapping--| LLM-lane point | S1 contributor |-|---|---|-| `pre_call` (mutate) | `pre` contributor with `rewrite` (`may_rewrite`) |-| `pre_call` (reject) / `moderation` fail | `pre` contributor `decision: deny` (`ContentFiltered`) |-| `post_call_success` | `post` contributor transforming the model-visible `Result` |-| `on_error` recover | `post`/error contributor with `substitute` (`may_substitute_result`, S4-gated) |-| `token-trim` / `prompt-shaping` | `pre` contributors with `rewrite` |--**Moderation runs concurrently** with the upstream call (`Promise.all([call, ...moderate()])`); a-moderation `deny` aborts the in-flight call via a linked `AbortSignal` so no tokens are wasted. The happy-path (call ok **and** all moderation pass) proceeds to `post`. A `pre` reject, a moderation block, and a-call failure all converge on the `on_error` contributor chain, where a gated hook may `substitute` a safe-`Result` (bypassing `post`, never model output) or the aggregated error surfaces.--### 2.1 · Lifecycle state machine--```mermaid-stateDiagram-v2- [*] --> PreCall : completeWithTools(request)- state PreCall {- [*] --> RunHooks- RunHooks --> RunHooks : continue — rewrite, next- RunHooks --> [*] : all approved- }- PreCall --> OnError : a hook rejects (deny) — no dispatch- PreCall --> Dispatch : approved (possibly rewritten)-- state Dispatch {- [*] --> Call- Call --> CallOk : response- Call --> CallFail : throws (after failover)- --- [*] --> Moderation- Moderation --> ModPass : all moderate() resolve- Moderation --> ModFail : a moderate() rejects → abort call- }-- Dispatch --> OnError : CallFail or ModFail (seeds the error)- Dispatch --> PostCall : CallOk and ModPass- PostCall --> Success-- state OnError {- [*] --> Handle- Handle --> Handle : reshape (reverse order)- Handle --> [*] : chain done or a hook recovers- }- OnError --> Recovered : a hook substitutes (may_substitute_result)- OnError --> Failed : no recover — aggregated error-- Recovered --> [*] : return substitute response- Success --> [*] : return response- Failed --> [*] : throw aggregated error-```--`OnError` is the single convergence point for a `pre` reject, a moderation block, and a call failure; any-gated hook may `recover` (return a safe response) instead of surfacing the error.--## 3 · Built-in hooks (in-process)--First-party, compiled into mcp-host, no network — built-in hooks (S4 §2):--- **`prompt-shaping`** — inject a system prompt part; force `temperature`/`max_tokens`/`tool_choice`.-- **`token-trim`** — reduce input tokens to a budget, reusing the existing `core/extensions/prePrune.ts`- passes; exposed as an `LlmHook` so ordering/config are uniform with the chain.--Both are `pre`-only rewrite contributors. Registered in `mcp-host/src/llm/hooks/builtins/registry.ts`,-selected/ordered by `Host.spec.guardrails.builtins` (S1 §7).--## 4 · Installed hooks--Guardrail / PII-redaction and other custom contributors run as **installed** hooks (S4): a container in-any language reached over the `/v1` protocol, declared by an `LlmHook` CR, `trust_level`-gated, and-delivered by any of the three modes (self-hosted pod / in-cluster Service / direct external egress). Their-content exposure is need-based + trust-gated (S4). A mcp-host runs the installed hooks its-`Host.spec.guardrails.hooks` block lists (opt-in — S1 §7), resolving each via its `LlmHook` CR; the chain-is **built-ins → installed** in the configured `order`.--## 5 · Aux/compaction lane--The internal summarization/compaction calls (`stateMachine.ts`) run only **`token-trim` + observability**-contributors — no guardrail/PII on internal calls — via an explicit per-lane flag on the chain builder.--## 6 · Config — `Host.spec.guardrails.builtins`--LLM built-in selection/order lives in the `builtins` array of `Host.spec.guardrails` (the single block-defined in S1 §7), parsed by `mcp-host/src/llm/hooks/parseLlmHooks.ts` and hot-reloaded like-`spec.llmPolicy`. Installed hooks are declared by their `LlmHook` CR (S4) and listed under-`guardrails.hooks`, not here. Shape of a `builtins[]` item:--```yaml-# item of Host.spec.guardrails.builtins (S1 §7)-type: object-required: [type]-properties:- type: { type: string, enum: [prompt-shaping, token-trim] }- order: { type: integer, default: 100 }- failMode: { type: string, enum: [open, closed] }- timeoutMs: { type: integer, minimum: 1 }- config: { type: object, x-kubernetes-preserve-unknown-fields: true }-```--## 7 · Lane-specific notes--`ask`/human approval is generally **unused** on this lane (S0 open decision 1) — the LLM adapter typically-uses only `allow`/`deny`/`no_decision`. `HookedLlmPort` placement above failover and the built-ins are-lane mechanics that stay here, not in S1.diff --git a/docs/features/mcp-host-guardrails/legacy/split-suite/04-hook-trust-and-delivery.md b/docs/features/mcp-host-guardrails/legacy/split-suite/04-hook-trust-and-delivery.mddeleted file mode 100644index 7dc1562d1..000000000--- a/docs/features/mcp-host-guardrails/legacy/split-suite/04-hook-trust-and-delivery.md+++ /dev/null@@ -1,234 +0,0 @@-# S4 · Hook trust & delivery--| | |-|---|---|-| **Type** | design spec |-| **Status** | draft for discussion |-| **Consumed by** | S2 [tool-lane](./02-tool-lane-adapter.md), S3 [llm-lane](./03-llm-lane-adapter.md) |-| **Governed by** | S1 [core](./01-guardrail-core.md) admin policy (which installed hooks + capabilities are admissible) |--How a hook (an executable contributor, S1 §2) is trusted, delivered, and invoked. A hook is either-**built-in** (first-party, compiled into mcp-host) or **installed** (a container in any language, run-out-of-process). Admin policy on the Host CRD (S1 §7) declares which installed hooks and capabilities a lane may use.-------## 1 · Two kinds of hook--| | **Built-in** | **Installed** |-|---|---|---|-| What it is | first-party code **compiled into mcp-host** | a **container image**, authored in any language |-| Runs | in-process, no separate process | out-of-process — a pod / in-cluster Service / external endpoint |-| Installed by | ships with the platform | anyone, via the marketplace (§6) |-| Trust | first-party | image-allowlist + sha256 digest + `trust_level` + NetworkPolicy |-| Examples | `prompt-shaping`, `token-trim` | `guardrail`, `pii-redact`, custom tool checks |--**There is no local-executable / CRD-scripting hook.** Custom logic is a container in the author's-language of choice — not a script or executable wedged into the CRD, which is awkward to author and learn.-The high-assurance, deterministic checks that might otherwise justify a local executable are served-**in-process** instead: by the declarative permission **rules** (S2 — evaluated by mcp-host, not a hook)-and by the built-ins above. An operator who wants custom guaranteed-local logic contributes a built-in.--**Capabilities are admin-granted per hook** (S1 §6): `may_deny`, `may_rewrite`, `may_substitute_result`,-`may_add_context`. An installed hook may be deny-authoritative, but then it must fail-closed (§7).--## 2 · Built-in hooks (in-process)--First-party contributors compiled into mcp-host, selected/ordered by config (not a CRD). They run-in-process — no separate process, no network of their own, full first-party trust. The LLM lane ships-`prompt-shaping` and `token-trim` (S3 §3); the tool lane's decision logic is the declarative-permission-rule engine (S2 §2), also in-process. Built-ins are the deterministic, always-available core;-everything custom is an installed hook.--## 3 · Installed hooks — delivery & invocation--### 3.1 · The `/v1` protocol-mcp-host POSTs each contributor call to **`{endpoint}{path}/v1/{lifecyclePoint}`**, where `{endpoint}`-is resolved from the `target` (the pod / Service / URL) and `{path}` is the hook's `spec.path` (default-`/`). Because the path is per-hook, **one pod can host hundreds of hook functions** — many `LlmHook`s-point at the *same* `service` with a different `path` — so you don't run a pod per function (which keeps-the compute footprint, not just the bytes, low). **Auth:** a short-lived RS256 bearer token (broker-token-signer) over a NetworkPolicy-confined connection; no mTLS. **Bodies** are a redacted, need-based-projection (§5). Only the lifecycle endpoints a hook's `lifecyclePoints` declares are called — so one path-can serve both a pre and a post contributor (via `…/v1/pre_call` and `…/v1/post_call`).--| Endpoint (under `{endpoint}{path}`) | Request body (redacted projection — §5) | Response |-|---|---|---|-| `POST …/v1/pre_call` | `{ messages, tools?, model, params, usage, state, config }` | `{ action:'continue', patch?:{messages?,params?,systemPromptParts?} } \| { action:'reject', code, message }` |-| `POST …/v1/moderate` | `{ messages, model, usage, config }` | `200 {}` = pass · `4xx { code, message }` = fail |-| `POST …/v1/post_call` | `{ response:{content,tool_calls,finish_reason}, usage, state, config }` | `{ response }` (possibly redacted) |-| `POST …/v1/on_error` | `{ error:{code,message,retryable}, messages, model, usage, state, config }` | `{ action:'reshape', error:{code,message} } \| { action:'recover', response:{content,tool_calls?} }` |--`RemoteLlmHook` applies a `pre_call` `patch` onto the local request (the hook never gets the raw request-object), maps `reject`/`moderate`-4xx onto a `deny` contributor, and maps `on_error` `recover` onto a-`substitute` contributor (gated by `may_substitute_result`, §5/S1 §6).--### 3.2 · The `LlmHook` CRD (declaration)-Namespaced `clerum.io/v1alpha1`, reconciled by the **host-context-controller** (`llmHookReconciler.ts`).-Full schema (rendered as the k8s CRD `openAPIV3Schema` under `charts/clerum-crds/crds/llmhook.yaml`):--```yaml-apiVersion: apiextensions.k8s.io/v1-kind: CustomResourceDefinition-metadata: { name: llmhooks.clerum.io }-spec:- group: clerum.io- scope: Namespaced- names: { kind: LlmHook, listKind: LlmHookList, plural: llmhooks, singular: llmhook, shortNames: [llmhook] }- versions:- - name: v1alpha1- served: true- storage: true- schema:- openAPIV3Schema:- type: object- required: [spec]- properties:- spec:- type: object- required: [target, lifecyclePoints]- properties:- target: # exactly one of image | service | remote- type: object- oneOf: [{required: [image]}, {required: [service]}, {required: [remote]}]- properties:- image:- type: object- required: [ref, port]- properties:- ref: { type: string } # image-allowlist enforced at install- port: { type: integer, minimum: 1, maximum: 65535 }- imagePullSecrets: { type: array, items: { type: string } }- envSecret: { type: string } # hook's OWN creds (never the LLM secret)- egressBindings: { type: array, items: { type: object,- properties: { toFQDN: {type: string}, cidr: {type: string},- ports: {type: array, items: {type: integer}} } } }- security:- type: object- properties: { addCapabilities: { type: array, items: { type: string } } }- service:- type: object- required: [name, namespace, port]- properties:- name: { type: string }- namespace: { type: string }- port: { type: integer, minimum: 1, maximum: 65535 }- remote:- type: object- required: [baseUrl]- properties:- baseUrl: { type: string, pattern: '^https://' } # §4 SSRF-validated at dial- authHeadersSecret: { type: string }- path: { type: string, default: '/' } # API path on the endpoint; calls go to {endpoint}{path}/v1/{point}.- # Many LlmHooks can share one `service` pod via distinct paths.- lifecyclePoints:- type: array- minItems: 1- items: { type: string, enum: [preCall, moderate, postCallSuccess, onError] }- order: { type: integer, default: 100 }- failMode: { type: string, enum: [open, closed], default: closed }- timeoutMs: { type: integer, default: 5000, minimum: 1 }- onUnavailable:- type: object- properties:- mode: { type: string, enum: [strict, breaker], default: strict }- failureThreshold: { type: integer, default: 5, minimum: 1 }- cooldownMs: { type: integer, default: 30000, minimum: 0 }- capabilities: # admin-granted (S1 §6); enforced ⊆ guardrails.capabilityCeiling (S1 §7)- type: array- items: { type: string, enum: [may_deny, may_rewrite, may_substitute_result, may_add_context] }- appliesTo:- type: object- properties:- models: { type: array, items: { type: string } }- sourceKinds: { type: array, items: { type: string,- enum: [channel, desktop, workflow, cron, plugin_workload_sdk] } }- config: { type: object, x-kubernetes-preserve-unknown-fields: true }- managed: { type: boolean, default: true }- x-kubernetes-validations: # CEL- - rule: "!has(self.target.remote) || self.target.remote.baseUrl.startsWith('https://')"- message: "remote.baseUrl must be https://"- - rule: "!(has(self.capabilities) && self.capabilities.exists(c, c == 'may_deny')) || self.failMode == 'closed'"- message: "a deny-authoritative hook (may_deny) must fail closed"-```--### 3.3 · Delivery modes & reconcile flow-Exactly one `target`: **`image`** — HCC deploys a Deployment+Service+NetworkPolicy in the **`llm-hooks`**-namespace (`CONTROL_API_LLM_HOOKS_NAMESPACE`), ingress only from mcp-host, egress per `egressBindings`;-**`service`** — an existing in-cluster Service, nothing deployed; **`remote`** — an external HTTPS endpoint-mcp-host dials directly (§4), no proxy pod.--**Shared hook-server (one pod, many functions).** A single hook-server pod in `llm-hooks` can host-hundreds of functions, each at a distinct `path`. Deploy the pod **once** — either as one `image` LlmHook,-or an operator-managed Deployment+Service — then register each function as a lightweight **`service`-mode-LlmHook** pointing at that Service with its own `path` (`/pii-redact`, `/budget-guard`, …) and its own-`lifecyclePoints`/`order`/`capabilities`. mcp-host addresses each at `{service}{path}/v1/{point}`. This-keeps the pod count (and thus the always-on compute cost, §7) flat as the number of contributors grows,-while each function stays an independently-governed contributor in the Host's `guardrails.hooks` list.--```mermaid-flowchart LR- subgraph CP["control plane"]- UI["control-ui<br/>marketplace"] --> SAGA["control-api<br/>install-hook saga"]- SAGA -->|writes| CR["LlmHook CR<br/>(+ credential Secret)"]- SAGA -->|digest + image-allowlist + trust_level| SAGA- end- CR --> HCC["host-context-controller<br/>llmHookReconciler"]- HCC -->|image| DEP["Deployment + Service + NetworkPolicy<br/>(llm-hooks ns)"]- HCC -->|service / remote| NOP["no workload deployed"]- CR -. watch .-> MH["mcp-host<br/>hookChainProvider"]- MH --> RH["RemoteLlmHook"]- RH -->|image / service| DEP- RH -->|remote: direct egress + SSRF §4| EXT["external endpoint"]-```--control-api's saga only **writes** the CR + Secret; **HCC** reconciles it to workloads; **mcp-host** only-**watches** the CRs to resolve the hooks its `Host.spec.guardrails.hooks` block lists (installed hooks are-opt-in per Host — S1 §7; there is no auto-apply scope) and builds `RemoteLlmHook`s — it deploys nothing.--## 4 · `remote`-mode egress (direct, matching the status quo)--mcp-host dials a `remote` endpoint directly over its existing broad public-egress lane — the same way it-already reaches external LLM providers (`deploy/base/mcp-host/networkpolicies.yaml:108-121` — `443 →-0.0.0.0/0`; `llm/claude.ts` calls `api.anthropic.com` directly). No per-hook proxy pod: the proxy pattern-exists to fence *untrusted connector pods*, which does not apply to the trusted mcp-host reaching out.--**Destination safety is app-layer (the primary control):**-1. **Config-only provenance** — the target comes only from the admin-vetted, `trust_level`-gated `LlmHook`- CR, never from model/tool/user/hook data.-2. **Reuse the existing SSRF guard** (`core/tools/httpRequest.ts` + `core/safety/safety.ts`): `https` only;- block RFC-1918 / loopback / link-local / metadata `169.254.169.254` / `*.cluster.local`; resolve A+AAAA- and reject if any is private (fail-closed on DNS failure); **DNS-pin** to the validated IP; **no- redirects**.-3. **Payload** stays governed by the content projection (§5).--**L3 is a backstop only.** The broad lane is the pre-existing LLM-call posture; hooks add no new class of-egress. Optional: a Calico `destination.domains` policy scoped to the admin-declared hook hosts, programmed-by HCC per `remote` hook — per-host precision without a proxy pod.--## 5 · Content exposure (installed hooks)--A remote hook receives message/response **content** only for the lifecycle points that need it-(`moderate`/`post_call` get content; `pre_call` shaping gets model+params+metadata, not bodies) **and** only-if the entry's `trust_level` clears `config.minHookTrustLevel`. Below the bar, install is refused or the hook-is content-starved. Every content-bearing call is audit-logged (hook + phase + request id, not the content).-`LlmHookContext.host` omits `llmSecretName`; the hook's own credentials live in its own Secret.--## 6 · Marketplace (installed-hook distribution)--- Registry `entry_type: 'llm_hook'` with `hook_meta { target(image|service|remote), lifecyclePoints[],- credential_schema, defaultConfig, requiredEgress[], appliesToDefaults }`. Extending the `evenfire-registry`- schema is a prerequisite we own.-- `POST /admin/registry/install-hook` saga (cloned from `install-recipe`): verify sha256 bundle digest →- validate + store credential Secret → build the `LlmHook` `target` → image-allowlist preflight → stamp- `catalog-id/version` → create CR → rollback on failure → `reportInstall`. Installed-state derived from CRs.-- **Trust reuse (no new primitives):** `@clerum/image-policy` allowlist, sha256 digest, per-org pull secret,- `@clerum/workflow-recipe-capability-policy`, `trust_level`/`quality_tier`, install-time credential Secret,- per-workload egress NetworkPolicy.--## 7 · Fail-posture--Fail-closed is the default and **mandatory for any deny-authoritative hook** (built-in or installed). A `breaker`-(trip to fail-open after N failures, alert, re-probe) is permitted **only** for non-authoritative-(advisory/observability) hooks and **only** when admin policy opts in. `RemoteLlmHook` owns per-hook breaker-state so a down hook degrades per its declared mode, not globally.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.mddeleted file mode 100644index 54598f176..000000000--- a/docs/features/mcp-host-guardrails/mcp-host-guardrails-spec-v3.md+++ /dev/null@@ -1,1628 +0,0 @@-# MCP Host Guardrails — Design Spec--| Field | Value |-|---|---|-| **Type** | design spec (architecture + implementable) |-| **Status** | draft for discussion |-| **Scope** | specification only — no runtime/CRD/deployment change is authorized here |-------## 1 · Overview--The MCP Host applies guardrails on **two lanes**:--- **LLM lane** — the model *completion* call: mutate/moderate the request, transform the response,- recover on failure.-- **Tool lane** — *tool execution* (native + MCP tools): permission rules, `Pre/PostToolUse` hooks,- approvals.--Both lanes are the **same machine**: *intercept a call → run an ordered chain of policy contributors →-aggregate to a decision → execute at-most-once → emit bounded audit.* They differ only in **what** they-intercept and in the **trust model of their hooks**. So instead of two parallel guardrail stacks, this-spec is:--- **One shared decision engine** (§2–§4) — the `allow`/`ask`/`deny` logic, how contributions aggregate,- at-most-once execution, and the audit trail. Both lanes use it verbatim.-- **One shared config** (§5) — a single admin-authored `Host.spec.guardrails` block on the Host CRD.-- **Two thin per-lane layers** (§6 tool, §7 LLM) — each describes only what is specific to its kind of- call and hands everything else to the engine.-- **One shared "how hooks run and are trusted" layer** (§8) — custom hooks are **installed as- containers** (any language, out-of-process); the platform's own hooks are **built-ins** compiled into- mcp-host. There is no local-executable or CRD-scripting hook.--### 1.1 · The two lanes stack at runtime--A request first passes the LLM-lane gate; the model it returns emits a tool call; that call then passes-the tool-lane gate before it executes; the tool result feeds the next turn.--```mermaid-flowchart LR- REQ(["request / task"]) --> G1["LLM-lane gate (§7)<br/>pre_call · moderate · post_call · on_error"]- G1 -->|allowed request| MODEL["LLM completion"]- MODEL -->|"emits a tool call"| G2["Tool-lane gate (§6)<br/>rules · Pre/PostToolUse · approval"]- G2 -->|allowed call| EXEC["tool executes<br/>(at-most-once)"]- EXEC -.->|"result feeds the next turn"| G1- G1 -.->|deny / transform| OUT(["caller"])- G2 -.->|deny| OUT-```--Both gates are the *same* `GuardrailBoundary` (§2), configured by the shared engine and delivered with-hooks from §8. Only the lane-specific configuration in §6 / §7 differs.--### 1.2 · Two kinds of hook (the load-bearing simplification)--There are exactly **two** kinds of hook — there is **no** local-executable / CRD-scripting model:--- **Built-in** — first-party code **compiled into mcp-host**, run in-process, no network of its own- (e.g. `prompt-shaping`, `token-trim`). The deterministic, always-available core. **Only the Evenfire- core team adds built-in in-process hooks** — they ship with the platform and are not authorable by- operators or third parties, who use installed hooks instead.-- **Installed** — a **container image** in any language, run out-of-process (pod / in-cluster Service /- external endpoint), reached over the `/v1` protocol (e.g. `guardrail`, `pii-redact`, custom checks).--Custom logic is therefore authored in the language of choice as an ordinary container — not a script-wedged into a CRD — while the high-assurance core stays in-process as declarative **rules** (§6) +-built-ins. The Host-CRD admin policy (§5) declares which installed hooks (by digest, subject to a-`trust_level` floor) and which capabilities are admissible.-------## 2 · The `GuardrailBoundary` (the shared machine)--One mandatory boundary per guarded call:-`Intake → Contributors → Aggregate → (Approve) → Execute → Post → Evidence`. It is universal — native,-remote, bridged, first-run, resumed, interactive, and unattended calls all pass the same boundary before-execution.--Each lane instantiates the boundary over its own types:--| Type | Tool lane (§6) | LLM lane (§7) |-|---|---|---|-| `Input` | resolved-tool arguments | the `ToolCompletionRequest` (messages, tools, params, `usageContext`) |-| `Result` | the tool result | the `ToolCompletionResponse` |-| `ResolvedIdentity` | the *real* tool after dynamic-bridge resolution | model + provider |--`ResolvedIdentity` is always the *real* target after any bridge resolution — **never inferred from a name-pattern**.--```mermaid-flowchart LR- IN["Intake<br/>resolve identity + input"] --> C["Contributors (pre)<br/>rules + hooks + validators"]- C --> AGG{"Aggregate<br/>deny > ask > allow > no_decision"}- AGG -->|deny| DENY["Denied<br/>bounded result + audit"]- AGG -->|ask| APPR{"Approve<br/>exact-call resolver?"}- AGG -->|allow / no_decision| EXEC["Execute<br/>at-most-once"]- APPR -->|approved| EXEC- APPR -->|no resolver / denied| DENY- EXEC --> POST["Post contributors<br/>transform model-visible result"]- POST --> EV["Evidence<br/>bounded metrics + redacted audit"]- DENY --> EV-```--### 2.1 · Contributor contract--A **contributor** is the unit every policy source produces:--```-Contributor = {- phase: 'pre' | 'post'- source: 'admin_rule' | 'host_rule' | 'hook' | 'validator' | 'current' // 'current' = the existing approval path- sourceId: string // stable configured id- decision: 'allow' | 'ask' | 'deny' | 'no_decision'- reasonCode: <bounded enum>- rewrite?: Input // full replacement of the input (pre only)- substitute?: Result // only if granted may_substitute_result (§4.3)- audit: { modelReason?, userReason?, details? } // redacted, bounded; not copied across surfaces-}-```--- **Rules** are declarative contributors (the lane defines their match language, §6.1). **Hooks** are- executable contributors; *how* a hook runs (in-process built-in vs remote container) is §8's concern,- not the algebra's.-- `modelReason` / `userReason` / `details` serve different consumers and MUST NOT be copied between- surfaces without explicit redaction.-------## 3 · Decision algebra & aggregation--- **Strictness order:** `deny > ask > allow > no_decision`. The boundary outcome is the **strictest**- contribution across all matching sources; every contribution is retained in audit.-- **`no_decision`** means "no guardrail source decided" → the lane's *defer* behavior (the existing- approval path).-- **Unmatched default is explicit:** when a lane has a non-empty rule set, an unmatched call contributes- `ask` (not silently `allow`); when it has no guardrail config at all, absence contributes- `no_decision`. A default is never synthesized when guardrails are absent (no-config compatibility, §5).-- **Tie-break for presentation only:** when one bounded reason must be chosen for a metric or a- model-facing result, break ties deterministically (`admin_rule` → hook order → `host_rule` → lexical- `sourceId`). This selects *presentation*, never severity.-- **Malformed input fails closed:** an unknown decision value or an out-of-range predicate is rejected at- admission; it never aggregates as permissive.-------## 4 · Execution safety--### 4.1 · Rewrite safety--A `pre` contributor's `rewrite` **replaces the entire `Input`**, which is then canonicalized and-**re-validated** before the next contributor sees it. A contributor can never *weaken* an earlier-stricter decision. Rewrites are re-evaluated, so an earlier `allow` on the original input is never reused.--**Rewrite re-aggregation (algorithm).** When a `pre` contributor returns a `rewrite`:--1. Canonicalize + structurally **re-validate** the new `Input` (§6.1 admission checks); a rewrite that- fails validation is rejected `rewrite_invalid` (fail-closed).-2. **Restart the `pre` chain from the beginning** against the new `Input`. Every rule and pre hook- therefore always evaluates the **final** input, and the boundary outcome is the aggregate over that- final input — so no rule can be *bypassed* by a later rewrite (it re-runs and sees the rewritten input)- and no earlier `allow` is carried over.-3. **Termination:** each contributor's `rewrite` is honored **at most once** per call (by `sourceId`); a- further rewrite from the same source is ignored. With N `pre` contributors the chain restarts ≤ N times.--The input after the last honored rewrite is what executes and is digested into the snapshot (§4.2).--### 4.2 · At-most-once, snapshot, resume--- **At-most-once:** an approved call executes exactly once; suspension, restart, retry, and batch resume- never execute a call more than once.-- **Snapshot:** a suspended call persists an immutable snapshot — resolved identity, original + effective- input digests, matched rule/hook ids + decisions + rewrite digests, admin-policy digest, execution- mode, and state (`pending | approved | denied | executing | executed | post_complete`).-- **Resume:** load the snapshot; verify resolved identity + effective-input digest match. **Re-run the- declarative rules** (cheap, in-process) against the effective input and **re-aggregate**; verify- installed-hook artifacts still match stored digests **without re-calling the hooks**; **re-load the- current admin policy**. Proceed only if the fresh decision is allow/approved and **no stricter than the- snapshot** — if a rule now denies, the policy is missing/stricter, an artifact digest changed, or the- approval was for a different digest/identity/expired request → **deny**. Then atomically claim- `executing`, run once, and run post hooks under a stable idempotency key. *(Resume re-runs the rules — a- cheap, in-process re-aggregation — not merely a policy-digest comparison.)*--### 4.3 · Capabilities--A **hook's** powers beyond its default `allow` are **explicit, admin-granted** capabilities (§5/§8.2),-enforced on the hook's response (§8.1) — a hook lacking a capability cannot exercise it:--| Capability | Grants a hook | Key constraints |-|---|---|---|-| `may_deny` | contribute a **blocking `deny`** (a `reject`, or a `moderate` 4xx) | deny-authoritative ⇒ **must fail closed** (§8.6 / CRD CEL); without the grant a `reject` is downgraded to `no_decision` (§8.1) |-| `may_rewrite` | **replace the input** — a `pre_call` patch (`messages`/`params`) or a tool `updatedInput` | full replacement, canonicalized + **re-validated & re-aggregated** (§4.1); **cannot** touch the system prompt (§8.1); never weakens a prior stricter decision |-| `may_substitute_result` | return a **caller-visible result** in place of the real one (`on_error recover`) | never alters the **stored authoritative outcome**; on the LLM lane **text-only — no `tool_calls`** (§8.1); tagged contributor-originated + audited |-| `may_add_context` | **add bounded context** to the model (`additionalContext`) | injected as **untrusted** tool/hook output, **never** at system level (§12.4); size-bounded |--A **rule**'s `allow`/`ask`/`deny` needs no capability — admin authorship is the grant; hooks have **no-`may_ask`** (decided, §10 #6) — a hook wanting a human in the loop `deny`s and the admin writes an `ask`-rule. `may_substitute_result` is **LLM-lane-only** (§10 #3).-------## 5 · Administrative policy — the one config block--All guardrail configuration is a **single** admin-authored block, `Host.spec.guardrails`, on the **Host-CRD** (the mcp-host's own CR that HCC reconciles to instantiate the process). There is **no separate-`adminPolicy` sub-object**: the whole Host CR is admin-authored and the runtime can't mutate it, so-everything in the block is already non-overridable. A mcp-host runs exactly the hooks this block lists —-installed hooks are **opt-in per Host**.--Rendered into the Host `openAPIV3Schema`:--```yaml-guardrails:- type: object- properties:- rules: # ordered allow/ask/deny rules — tool-lane predicate shape in §6.1- type: array- items: { type: object } # { id, action: allow|ask|deny, reasonCode, match{ tool, arguments[] } }- hooks: # installed hooks this mcp-host runs, by phase (each an installed LlmHook CR, §8)- type: object # each entry: { id, digest } — id = LlmHook CR name (llm-hooks ns); digest = expected image sha256 (§8.2 resolution)- properties:- preToolUse: { type: array, items: { type: object } }- postToolUse: { type: array, items: { type: object } }- preCall: { type: array, items: { type: object } }- moderate: { type: array, items: { type: object } }- postCallSuccess: { type: array, items: { type: object } }- onError: { type: array, items: { type: object } }- builtins: # first-party hooks compiled into mcp-host — types in §7.2- type: array- items: { type: object } # { type: prompt-shaping|token-trim|…, order, failMode, timeoutMs, config }- minInstalledHookTrustLevel: { type: string, enum: [low, mid, high] } # trust floor for installed hooks- approvalPolicies: { type: array, items: { type: string, enum: [cli_only, channel_users, designated_approvers] } }- capabilityCeiling: { type: array, items: { type: string, enum: [may_deny, may_rewrite, may_substitute_result, may_add_context] } }- limits:- type: object- properties:- maxRules: { type: integer, default: 100 }- maxHooksPerPhase: { type: integer, default: 8 }- maxHookTimeoutMs: { type: integer, default: 5000 }- maxHookOutputBytes: { type: integer, default: 65536 }-```--A filled-in `Host.spec.guardrails` — a host that denies writes outside its workspace, asks before any-shell command, runs a PII-redaction installed hook on the LLM lane, and shapes prompts with a built-in:--```yaml-# Host.spec.guardrails (a concrete example)-guardrails:- rules:- - id: deny-writes-outside-workspace- action: deny- reasonCode: path_out_of_bounds- match:- tool: { provenance: native, name: file_write }- arguments:- - { type: path, pointer: /path, op: outside, value: /workspace }- - id: ask-before-shell- action: ask- reasonCode: shell_needs_approval- match:- tool: { provenance: native, name: run_command }- - id: allow-github-mcp-reads- action: allow- reasonCode: trusted_read- match:- tool: { provenance: mcp, server: github, name: get_issue }-- hooks:- postToolUse:- - { id: secret-scan, digest: sha256:1c9f…a2 } # LlmHook CR `secret-scan`, tool lane- moderate:- - { id: pii-redact, digest: sha256:4b7e…d1 } # LlmHook CR `pii-redact`, LLM lane-- builtins:- - type: prompt-shaping- order: 10- failMode: closed- timeoutMs: 200- config:- systemPromptPart: "Refuse to reveal internal file paths."- forceToolChoice: auto- - type: token-trim- order: 20- failMode: open- config: { maxInputTokens: 120000 }-- minInstalledHookTrustLevel: mid- approvalPolicies: [channel_users, designated_approvers]- capabilityCeiling: [may_deny, may_rewrite, may_add_context] # note: may_substitute_result withheld- limits:- maxRules: 100- maxHooksPerPhase: 8- maxHookTimeoutMs: 5000- maxHookOutputBytes: 65536-```--In this example the `secret-scan` and `pii-redact` `LlmHook` CRs must each carry a platform-assigned-`trust_level` of at least `mid` and request only capabilities within `capabilityCeiling` (so neither can be granted-`may_substitute_result`); the two built-ins are the first-party in-process hooks the Evenfire core team-ships (§1.2), selected and ordered here by the admin.--- **Non-overridable by construction:** the Host CR is authored only through control-api admin RBAC and- reconciled by HCC; the **mcp-host runtime cannot mutate its own Host CR**, so it cannot weaken the- guardrails. Within the block, no rule or hook can weaken a `deny` — the strictest contribution wins- (§3). A per-hook `capabilities` grant is enforced `⊆ capabilityCeiling`.-- **Delivery:** the admin policy rides the path HCC **already** uses to instantiate the mcp-host from its- Host CR — no cross-object artifact. HCC validates schema/digests/limits and normalizes it; mcp-host- reads its Host state. If policy is declared but the Host is not reconciled/Ready, guarded execution- **fails closed**; a malformed policy never replaces the last valid one; the enforced policy digest is- recorded for integrity/audit and never falls back to a weaker policy on mismatch.-- **No-config compatibility:** with no admin policy, host rules, or hooks, behavior is identical to- current — absence contributes `no_decision`.-------## 6 · Tool lane--Specializes the boundary over **tool execution**. `Input` = resolved-tool arguments; `Result` = the tool-result; `ResolvedIdentity` = the real tool after dynamic-bridge resolution. **Provenance** (`native` vs-`mcp`, server + name) comes from the **registry**, never inferred from a `__` name pattern. A-`clerum__tool_call` bridge rule evaluates the *resolved* target; it MAY additionally constrain the-original bridge identity but never instead of the resolved target.--**Terminology.**--| Term | Means (tool lane) | Not to be confused with |-|---|---|---|-| **provenance** (`native` or `mcp`) | where the resolved tool comes from, read from the **registry** | a `__` name pattern — provenance is **never** inferred from the tool name |-| **resolved identity** | the **real** target after dynamic-bridge (`clerum__tool_call`) resolution — what rules/hooks evaluate | the **bridge identity** (the `clerum__tool_call` wrapper); a rule may *also* constrain the bridge, never *instead of* the resolved target |-| **rule** | a **declarative** contributor — `allow`/`ask`/`deny` + typed predicates, admin-authored (§6.1) | a **hook** — an *executable* contributor (a container), §6.2 |-| **PreToolUse hook** | runs **before** approval; may decide (`allow`/`ask`/`deny`) and return `updatedInput` | **PostToolUse hook** — runs **once after** execution; may redact the model-visible result but cannot deny, undo side effects, or alter the stored outcome |-| **original input** | the arguments **as the model emitted** them | **effective input** — the arguments **after** a Pre-hook `updatedInput`/rewrite, re-validated & re-aggregated (§4.1) |-| **`allow`** | an **affirmative grant** over the whole call (§6.1) | **`no_decision`** — *no guardrail decided*; defers to the existing approval path (§3) |-| **interactive** | mode where an **exact-call approval resolver is available** | **unattended** — no resolver, so an `ask` becomes `deny`; set by resolver availability, **not** by cron origin (§6.3) |-| **hard validators / containment** | structural validators, MCP scoping, NetworkPolicy — **always authoritative** | a guardrail **decision** — an `allow` never bypasses them (§6.6) |-| **doom-loop guard** | denies 3 **consecutive identical** `(tool, input-digest)` calls (§6.4) | a rate/quota limit |--```mermaid-flowchart TB- T["resolved tool call<br/>(provenance from registry)"] --> R["permission rules<br/>(typed predicates)"]- T --> P["PreToolUse hooks (installed)<br/>sequential · may rewrite"]- R --> A{"aggregate (§3)<br/>+ doom-loop guard"}- P --> A- A -->|deny| D["Denied<br/>bounded result + audit"]- A -->|ask| AP{"approval resolver?"}- A -->|allow / no_decision| HV["hard validators + containment"]- AP -->|approved / interactive| HV- AP -->|unattended · none| D- HV --> X["execute at-most-once"]- X --> PT["PostToolUse (once)<br/>redact model-visible result"]-```--**Reading the flow.** Every tool call passes this gate once, before it runs:--1. The call is resolved to its **real identity** — provenance from the registry, and any dynamic- `clerum__tool_call` bridge resolved to the true target — then checked in parallel by **permission rules**- (§6.1) and **PreToolUse hooks** (§6.2).-2. All contributions **aggregate** with strict precedence — **`deny > ask > allow > no_decision`** (§3) —- together with the doom-loop guard (§6.4); the strictest wins and nothing can weaken a `deny`.-3. The outcome routes: **`deny`** → blocked (bounded result + audit); **`ask`** → an **approval resolver**- decides — a human may approve when interactive, otherwise an unattended `ask` becomes a `deny` (§6.3);- **`allow` / `no_decision`** → proceed.-4. **Hard validators + containment** (§6.6) run next and are **always authoritative** — a guardrail `allow`- never bypasses them.-5. The call **executes at-most-once** (§4.2).-6. A **PostToolUse** hook may transform the *model-visible* result, but cannot undo the side effect, deny- the call, or change the stored outcome (§6.2).--In one sentence: *a tool call is resolved to its real identity, checked by admin rules + optional hook-containers, aggregated with `deny > ask > allow > no_decision`, sent to human approval if it's `ask`, then-run once behind the always-on hard validators — and the model only ever sees a post-processed result.*--### 6.1 · Permission rules (declarative contributors)--**In plain terms.** A permission rule is an *if-this-then-that* the admin writes: *"if a tool call looks-like X, then allow / ask / deny it."* The "looks like X" part matches on **which tool** it is (from the-registry) and on the **values of specific arguments** — e.g. *"the file path is outside `/workspace`"* or-*"the URL host is `api.github.com`."* The "then" part is one of three verdicts: **`allow`** (let it run),-**`ask`** (require a human's approval first), or **`deny`** (block it). You can write many rules; if-several match the same call, the **strictest** verdict wins — a `deny` always beats an `allow`. Rules are-just **data, not code**, so they're safe, fast, and always evaluated in-process. The rest of this section-is the precise vocabulary for writing the "looks like X" conditions.--`Host.spec.guardrails.rules[]` with `action: allow | ask | deny`, matched on provenance + **typed-argument predicates**. The `rules[]` **item** shape:--```yaml-# item of Host.spec.guardrails.rules (§5)-type: object-required: [id, action, match]-properties:- id: { type: string }- action: { type: string, enum: [allow, ask, deny] }- reasonCode: { type: string }- match:- type: object- required: [tool]- properties:- tool:- type: object- required: [provenance]- properties:- provenance: { type: string, enum: [native, mcp] } # from the registry, never string-parsed- server: { type: string }- name: { type: string }- arguments:- type: array- items:- type: object- required: [type, pointer, op]- properties:- type: { type: string, enum: [path, url, command, json] }- pointer: { type: string } # constrained RFC 6901 JSON Pointer- op: { type: string } # per type — see the predicate list below- value: { x-kubernetes-preserve-unknown-fields: true }-```--Predicate `op` values by `type`:--- **`path`** — `equals | under | outside`; normalized, symlink-aware, boundary-safe containment; prefix- strings are forbidden.-- **`url`** — `scheme_in | host_in | port_in | path_under`; parsed + normalized; credentials-in-URL rejected.-- **`command`** — `executable_is | argv_prefix`; a structured executable + argv, not a parsed shell string.-- **`json`** — `exists | equals | one_of | contains`; size- and depth-bounded; object-key order canonicalized.--Predicates name constrained RFC-6901 JSON Pointers; invalid pointers / excess depth / excess count /-over-limit values are **rejected at admission**. Wildcards support only `*` and `?`, anchored, bounded-length — no regex. Array order is never security precedence (all matching rules contribute; §3-aggregates). Unmatched default follows §3: non-empty rules → `ask`; absent → `no_decision` (defer to the-existing approval path).--A filled `rules` array exercising all four predicate types (each rule is one item of the shape above):--```yaml-# Host.spec.guardrails.rules (a concrete example)-rules:- # path — deny writing anywhere outside the workspace- - id: deny-writes-outside-workspace- action: deny- reasonCode: path_out_of_bounds- match:- tool: { provenance: native, name: file_write }- arguments:- - { type: path, pointer: /path, op: outside, value: /workspace }-- # url — allow http_request only to GitHub over https on 443- - id: allow-github-https-only- action: allow- reasonCode: trusted_egress- match:- tool: { provenance: native, name: http_request }- arguments:- - { type: url, pointer: /url, op: scheme_in, value: [https] }- - { type: url, pointer: /url, op: host_in, value: [api.github.com] }- - { type: url, pointer: /url, op: port_in, value: [443] }-- # command — deny a destructive shell invocation (structured executable + argv, not a shell string)- - id: deny-rm-rf- action: deny- reasonCode: destructive_command- match:- tool: { provenance: native, name: run_command }- arguments:- - { type: command, pointer: /command, op: executable_is, value: rm }- - { type: command, pointer: /command, op: argv_prefix, value: [-rf] }-- # json — ask before a GitHub MCP write targeting a protected repo- - id: ask-github-write-protected-repo- action: ask- reasonCode: write_needs_approval- match:- tool: { provenance: mcp, server: github, name: create_issue }- arguments:- - { type: json, pointer: /repository, op: one_of, value: [octo/prod, octo/infra] }-```--Given a `file_write` to `/etc/passwd`, `deny-writes-outside-workspace` contributes `deny` and — being the-strictest across all matching rules — wins (§3). Given `run_command` with `rm -rf /tmp/x`, `deny-rm-rf`-matches (both predicates true) and denies; `rm -v file` does not match (`argv_prefix` fails) and falls to-the unmatched default (`ask`, since the rule set is non-empty).--**Predicate limits — defense-in-depth, not a sandbox.** Argument predicates constrain the *declared*-arguments only. For **shell-capable tools they are advisory, not authoritative**: a `command` predicate-sees `argv[0]`, so `deny-rm-rf` above does **not** stop `sh -c 'rm -rf /'`, `xargs rm`, `find … -delete`,-or `python -c …` — the dangerous executable is hidden inside a shell string the predicate cannot parse.-Likewise a `path`/`url` predicate is evaluated *before* execution and is **not** TOCTOU-safe (a symlink or-DNS answer can change between check and use). Authoritative containment for these cases is the-hard-validator / sandbox layer (§6.6), never the arg predicate; write `deny` rules as **one** layer of-defense, not the only one (§12.3).--**`allow` is an affirmative grant over the *whole* call, not a filter on the arguments it names.** An-`allow` never overrides a `deny` (§3) or bypasses the hard validators (§6.6) — but a matching `allow`-replaces the unmatched-default `ask` with `allow` for that call, **including any argument the rule did not-constrain**. An `allow` that pins only `/url` still permits arbitrary `/method`, `/headers`, `/body`. Write-`allow` rules that constrain **every** security-relevant argument, and prefer the safer idiom — **`deny`-the dangerous + rely on the `ask` default** — using `allow` only to pre-approve fully-specified, known-safe-calls.--### 6.2 · Pre/PostToolUse hooks--**In plain terms.** Hooks are the *custom* checks — small programs (containers) an operator plugs in when-the built-in rules aren't enough. They run at two moments: a **PreToolUse** hook runs *before* the tool-executes and can inspect the call, rewrite its arguments, or vote `allow`/`ask`/`deny` — so it's a-gatekeeper that can stop the call. A **PostToolUse** hook runs *after* the tool has already executed and-can only clean up what the **model sees** of the result (e.g. strip a secret from the output). The key-asymmetry: **Pre can stop a call; Post cannot** — by the time Post runs, the action already happened, so it-can't undo it or retroactively deny it.--Executable contributors, delivered as **installed** hooks (§8): a container image in any language, run-out-of-process, reached over the `/v1` protocol, governed by image-allowlist + digest + `trust_level` +-NetworkPolicy. There is no local-executable / CRD-scripting hook; deny-authoritative guaranteed-local-logic is a built-in. The hooks this lane runs are listed under `Host.spec.guardrails.hooks` (§5).--- **PreToolUse** — runs sequentially **before** approval; returns `{decision, reasonCode, updatedInput,- additionalContext}`. `updatedInput` (only with `allow`/`ask`) is a full input replacement,- canonicalized + revalidated (§4.1). Failure/timeout → declared fail-mode (decision-making pre hooks- default **closed**).-- **PostToolUse** — runs **once** after the single execution attempt; may redact/replace the- *model-visible* result, add bounded context, or recommend stop. It **cannot** undo side effects, mark- the execution denied, or alter the stored outcome.--### 6.3 · Approvals — interactive & unattended--**In plain terms.** When a rule or hook says **`ask`**, someone has to approve the call before it runs.-Whether that's even possible comes down to one thing: *is there a person (or channel) available to answer-right now?* — **not** whether the task is a cron job. If yes (**interactive**), they get a one-time-approve-or-deny prompt for that exact call; a blanket "allow everything" set earlier does **not** count as-answering an explicit `ask`. If no one is available (**unattended**), an `ask` **fails safe — it becomes a-`deny`** rather than silently running.--Execution mode is set by the **availability of an exact-call approval resolver**, not by cron origin.--- **Interactive** (`ask`/`force_ask`): one pending request bound to the resolved tool + effective-input- digest + rule/hook digests; choices are `approve_once | deny`. A broad wildcard/prior approval **cannot**- satisfy an explicit `ask`. This creates no persistent rule.-- **Unattended:** explicit `deny` → deny; `ask` with no resolver → **deny** (`approval_unavailable`);- `allow` → proceed through hard validators + containment; `no_decision` → **defer to the existing approval- path (unchanged)**.-- **`no_decision` is defined as a true no-op for the guardrail layer:** it hands to today's approval- mechanism — the existing `*`, exact-tool, and MCP-server approvals and their current default — and adds- nothing. This preserves no-config compatibility (§5): absence = `no_decision` = today's behavior. An- operator who wants fail-safe under automation writes an explicit `deny`/`ask` **rule** (the guardrail- layer's job); `no_decision` never silently tightens *or* loosens the status quo.-- The existing `*`, exact-tool, and MCP-server approvals apply **only** inside `no_decision`/defer.--Snapshot/resume for suspended calls uses §4.2; batches are left-to-right with earlier denied/completed-results preserved.--### 6.4 · Doom-loop guard--Within one task, three consecutive identical `(resolved tool, effective-input digest)` calls →-`deny (repeated_identical_call)`. Any intervening different tool or input resets the counter; the counter-is persisted with task state.--### 6.5 · Persistence / prompt-injection hardening--Rules and hooks evaluate memory and file tools including canonical paths after rewrites, but **path denial-is not a substitute for content scanning**: raw `file_write` MUST route through the `WorkspaceService`-scanner, and `daily/*` memory is treated as prompt-affecting persistent state.--### 6.6 · Hard validators & containment (unchanged)--Existing structural validators, MCP scoping, token restrictions, NetworkPolicy, and the stateless-cron-manage containment remain authoritative and independent of guardrail outcome — a guardrail `allow`-never bypasses them.-------## 7 · LLM lane--**In plain terms.** This is the *other* gate: instead of guarding tool execution (§6), it guards the moment-the agent **talks to the model**. Same machine, different subject — here the "call" is the request sent to-the LLM and the "result" is the model's response. It lets guardrails **reshape or block the request** before-it's sent (e.g. redact PII, shape the prompt), **check the response** the model returns, and **recover** if-the call fails. It sits *above* provider failover, so these checks run **once per logical request**, not-once per retry.--Specializes the boundary over the **LLM completion call**. `Input` = the `ToolCompletionRequest`;-`Result` = the `ToolCompletionResponse`; `ResolvedIdentity` = model + provider.--The boundary is a **`HookedLlmPort`** decorator (`mcp-host/src/core/adapters/hookedLlmPort.ts`) wrapping-the effective `LlmPort` **above failover**, so contributors fire **once per logical request**, not per-fallback attempt. Wired in `taskExecutor.buildLoopConfig`.--### 7.1 · Lifecycle → contributor mapping--| LLM-lane point | Contributor |-|---|---|-| `pre_call` (mutate) | `pre` contributor with `rewrite` (`may_rewrite`) |-| `pre_call` (reject) / `moderation` fail | `pre` contributor `decision: deny` (`ContentFiltered`) |-| `post_call_success` | `post` contributor transforming the model-visible `Result` |-| `on_error` recover | `post`/error contributor with `substitute` (`may_substitute_result`, §8-gated) |-| `token-trim` / `prompt-shaping` | `pre` contributors with `rewrite` |--**Moderation runs concurrently** with the upstream call (`Promise.all([call, ...moderate()])`); a-moderation `deny` aborts the in-flight call via a linked `AbortSignal` so no tokens are wasted. The happy-path (call ok **and** all moderation pass) proceeds to `post`. A `pre` reject, a moderation block, and a-call failure all converge on the `on_error` contributor chain, where a gated hook may `substitute` a safe-`Result` (bypassing `post`, never model output) or the aggregated error surfaces.--```mermaid-stateDiagram-v2- [*] --> PreCall : completeWithTools(request)- state PreCall {- [*] --> RunHooks- RunHooks --> RunHooks : continue — rewrite, next- RunHooks --> [*] : all approved- }- PreCall --> OnError : a hook rejects (deny) — no dispatch- PreCall --> Dispatch : approved (possibly rewritten)-- state Dispatch {- [*] --> Call- Call --> CallOk : response- Call --> CallFail : throws (after failover)- --- [*] --> Moderation- Moderation --> ModPass : all moderate() resolve- Moderation --> ModFail : a moderate() rejects → abort call- }-- Dispatch --> OnError : CallFail or ModFail (seeds the error)- Dispatch --> PostCall : CallOk and ModPass- PostCall --> Success-- state OnError {- [*] --> Handle- Handle --> Handle : reshape (reverse order)- Handle --> [*] : chain done or a hook recovers- }- OnError --> Recovered : a hook substitutes (may_substitute_result)- OnError --> Failed : no recover — aggregated error-- Recovered --> [*] : return substitute response- Success --> [*] : return response- Failed --> [*] : throw aggregated error-```--**Reading the diagram.** The happy path runs top to bottom; every failure funnels to one place.--- **PreCall** — the request first runs through the pre-call hooks in order. Each may rewrite it and pass it- on, or reject it. A reject skips the model entirely and jumps to **OnError**; otherwise the (possibly- rewritten) request goes to **Dispatch**.-- **Dispatch — two things at once.** The request is **sent to the model** (→ succeeds, or fails after- failover) *while* **moderation** runs in parallel (→ all checks pass, or one rejects and aborts the- in-flight call so no tokens are wasted). Running both together means a bad request is stopped without- waiting on the model.-- **After Dispatch** — if the call succeeded **and** moderation passed, **PostCall** hooks transform the- model's response and it returns as **Success**. If **either** failed, it goes to **OnError**.-- **OnError — the single failure funnel.** A pre-reject, a moderation block, and a call failure all land- here. Error hooks run; a gated hook may **recover** with a safe substitute response (**Recovered**), or if- none does, the aggregated error is thrown (**Failed**).--So there are exactly three exits — **Success** (the real, post-processed response), **Recovered** (a hook's-safe stand-in), or **Failed** (an error). `OnError` is the single convergence point for a `pre` reject, a-moderation block, and a call failure — the one spot where a gated hook can still turn a failure into a safe-answer via `recover` instead of surfacing the error.--### 7.2 · Built-in hooks (in-process)--First-party, compiled into mcp-host, no network:--- **`prompt-shaping`** — inject a system prompt part; force `temperature`/`max_tokens`/`tool_choice`.-- **`token-trim`** — reduce input tokens to a budget, reusing `core/extensions/prePrune.ts`; exposed as a- hook so ordering/config are uniform with the chain.--Both are `pre`-only rewrite contributors, registered in `mcp-host/src/llm/hooks/builtins/registry.ts`,-selected/ordered by `Host.spec.guardrails.builtins` (§5). Shape of a `builtins[]` item:--```yaml-# item of Host.spec.guardrails.builtins (§5)-type: object-required: [type]-properties:- type: { type: string, enum: [prompt-shaping, token-trim] }- order: { type: integer, default: 100 }- failMode: { type: string, enum: [open, closed] }- timeoutMs: { type: integer, minimum: 1 }- config: { type: object, x-kubernetes-preserve-unknown-fields: true }-```--### 7.3 · Installed hooks on this lane--Guardrail / PII-redaction and other custom contributors run as **installed** hooks (§8), declared by an-`LlmHook` CR, `trust_level`-gated, delivered by any of the three modes. The chain is-**built-ins → installed** in the configured `order`. Content exposure is need-based + trust-gated (§8.4).--### 7.4 · Aux/compaction lane & notes--Internal summarization/compaction calls (`stateMachine.ts`) run **`token-trim` + observability +-PII-redaction** contributors, selected by an explicit per-lane flag on the chain builder. Redaction runs-in **transform-only** mode: it may **rewrite** (scrub PII/secrets *before* the content reaches the-provider) but **cannot block** — any `deny`/`ask` on this lane is downgraded to `no_decision` and audited,-so a guardrail can never wedge routine compaction. Deny-capable **moderation is therefore not run here**-(its verdict couldn't be enforced without risking a stuck task); the leak it would otherwise guard against-— sensitive conversation content reaching the model provider during compaction — is closed by redaction-instead. So internal calls are both **un-blockable and PII-scrubbed**. `ask`/human approval is generally-**tool-lane-only** (decided, §10 #1); the LLM adapter uses only-`allow`/`deny`/`no_decision`.-------## 8 · Hooks — trust & delivery--How a hook (an executable contributor, §2.1) is trusted, delivered, and invoked. Built-ins (§7.2) run-in-process with full first-party trust and no network of their own — nothing below applies to them. The-rest of this section is about **installed** hooks.--### 8.1 · The `/v1` protocol--mcp-host POSTs each contributor call to **`{endpoint}{path}/v1/{lifecyclePoint}`**, where `{endpoint}` is-resolved from the `target` (the pod / Service / URL) and `{path}` is the hook's `spec.path` (default `/`).-Because the path is per-hook, **one pod can host hundreds of hook functions** — `image`-mode `LlmHook`s-that share the **same image digest** are backed by one pod (§8.2 digest-dedup) and differ only by `path`,-so you don't run a pod per function (keeping the compute footprint, not just the bytes, low). **Auth:** a-short-lived RS256 bearer token (broker-token signer) over-a NetworkPolicy-confined connection; no mTLS. **Bodies** are a redacted, need-based projection (§8.4).-Only the lifecycle endpoints a hook's `lifecyclePoints` declares are called — so one path can serve both a-pre and a post contributor (`…/v1/pre_call` and `…/v1/post_call`). The `/v1/{point}` endpoint names map to-the CRD `lifecyclePoints` values: `pre_call`→`preCall`, `post_call`→`postCallSuccess`, `on_error`→`onError`,-`moderate`→`moderate`.--| Endpoint (under `{endpoint}{path}`) | Request body (redacted projection — §8.4) | Response |-|---|---|---|-| `POST …/v1/pre_call` | `{ messages, tools?, model, params, usage, state, config }` | `{ action:'continue', patch?:{messages?,params?} }` or `{ action:'reject', code, message }` |-| `POST …/v1/moderate` | `{ messages, model, usage, config }` | `200 {}` = pass · `4xx { code, message }` = fail |-| `POST …/v1/post_call` | `{ response:{content,tool_calls,finish_reason}, usage, state, config }` | `{ response }` (possibly redacted) |-| `POST …/v1/on_error` | `{ error:{code,message,retryable}, messages, model, usage, state, config }` | `{ action:'reshape', error:{code,message} }` or `{ action:'recover', response:{content} }` (text-only — no `tool_calls`, §8.1) |--`RemoteLlmHook` applies a `pre_call` `patch` onto the local request (the hook never gets the raw request-object), maps `reject`/`moderate`-4xx onto a `deny` contributor, and maps `on_error` `recover` onto a-`substitute` contributor (gated by `may_substitute_result`, §4.3). Capabilities are admin-granted per hook-(§5): `may_deny`, `may_rewrite`, `may_substitute_result`, `may_add_context`. An installed hook may be-deny-authoritative, but then it must fail-closed (§8.6).--**Wire contract.** Each call is an HTTP `POST` of `application/json` to `{endpoint}{path}/v1/{point}` with:--- **Auth** — the **authoritative** caller control is the **network layer**: the hook's per-hook ingress- NetworkPolicy admits only the referencing mcp-host pods over a default-deny baseline (N1/N7), so only an- authorized mcp-host can open the connection; no mTLS. An app-layer `Authorization: Bearer <short-lived- RS256 JWT>` is a **deferred, advisory** hardening (a hook *may* verify the signature + audience for- defense-in-depth) — but mcp-host does **not** currently mint a per-hook token, and because a hook is an- untrusted third-party container it cannot be forced to verify one, so the token is not relied upon.- Decided: the NetworkPolicy admission is the caller boundary; per-hook token infra is out of scope until a- concrete need (e.g. per-path authz across co-located hooks, which are same-image by construction anyway).-- **Versioning** — the `v1` in the path is the contract version: changes within `v1` are **additive-only**- (new optional fields); any breaking change is a new `/v2` path served alongside.-- **Timeout — bounded in time, idle-independent.** A dial is bounded by the hook's `timeoutMs` (≤- `maxHookTimeoutMs`, §5) enforced as an **absolute deadline** (`AbortSignal`), not a socket idle timer — a- peer that trickles one byte at a time cannot keep the request alive. Above the transport, mcp-host races- every dial against a **hard per-hook deadline** (belt-and-braces, §8.6): even a transport that fails to- settle resolves to **unavailable**, so a hook can never hang the awaiting turn. Exceeding either bound is- treated as **unavailable** (below).-- **Body cap — point-aware, bounded while streaming.** Response caps are **per lifecycle point**: a- `pre_call` `may_rewrite` output IS the whole (possibly compressed) conversation — legitimately large — so- it gets a **generous** cap (`maxHookRewriteBytes`, default **5 MiB ≈ 1M tokens**), while a `moderate`- verdict, `post_call` redaction, or `on_error` recovery gets a **tight** cap (`maxHookOutputBytes`, default- **1 MiB**). A single tight cap for all silently discards rewrites (they blow past 64 KiB) and no-ops the- hook — see §12.4. The transports enforce only the **larger** ceiling for memory safety (streamed + the- connection destroyed once exceeded — no buffering spike); the per-point application cap is checked after- parse. An over-cap response is **logged** (never a silent no-op) and mapped to **unavailable**, but flagged- `oversized` so the §8.6 breaker does **not** count "response too big" as a hook-down failure.-- **Status semantics** — `200` returns the endpoint's action body (§8.1 table); a `moderate` `4xx- {code,message}` is a **fail** (→ `deny`). **`5xx`, timeout, connection error, a malformed/oversized body,- or a `4xx` that isn't a valid action** are all treated as **hook-unavailable** → the hook's declared- **fail-mode** (`strict` fail-closed, or `breaker`, §8.6) — never a silent `allow` — and are audited.-- **Idempotency** — every call carries a request id; `post_call`/`on_error` reuse a stable idempotency key- across resume so a hook is never double-applied (§4.2).--**Capabilities are enforced on the response, not merely declared.** An installed hook is an untrusted-out-of-process container, so mcp-host validates **every** hook response against that hook's granted-capabilities and **discards any ungranted action**: a `patch` from a hook without `may_rewrite` is ignored,-a `reject` from a hook without `may_deny` is downgraded to `no_decision`, and a `recover`/`substitute` from-a hook without `may_substitute_result` is dropped — each rejected attempt is audited. The install-time-ceiling (§5) and this response-time check are **both** required; a container is never trusted to self-limit.-Ungranted fields never widen a hook's effect beyond its grant.--**Hooks are subtractive on actions — tool calls come only from the model.** A hook response may **drop** or-**redact** `tool_calls`, but it **MUST NOT add, synthesize, or rewrite** one into a different call. mcp-host-discards any `tool_call` in a hook response that is not present — by identity + argument digest — in the-model's own output, and audits it. Consequently an `on_error` **`recover` substitute is text-only** (no-`tool_calls`): recovery returns a safe terminal *result* to the caller, never new agent actions — which is-what `may_substitute_result` already promises (it changes the caller-visible result, **not** the-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 — 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--Namespaced `clerum.io/v1alpha1`, reconciled by the **host-context-controller** (`llmHookReconciler.ts`).-`LlmHook` CRs and their credential Secrets live in the dedicated **`llm-hooks`** namespace (the same-namespace as the hook pods), which holds **only** hook config and secrets — so a hook's free-string-`envSecret` can reference only *hook* secrets, never the mcp-host LLM key (`mcp-host` ns) or a Host's-secret (§12.1). Full schema (rendered as the k8s CRD `openAPIV3Schema` under-`charts/clerum-crds/crds/llmhook.yaml`):--```yaml-apiVersion: apiextensions.k8s.io/v1-kind: CustomResourceDefinition-metadata: { name: llmhooks.clerum.io }-spec:- group: clerum.io- scope: Namespaced- names: { kind: LlmHook, listKind: LlmHookList, plural: llmhooks, singular: llmhook, shortNames: [llmhook] }- versions:- - name: v1alpha1- served: true- storage: true- schema:- openAPIV3Schema:- type: object- required: [spec]- properties:- spec:- type: object- required: [target, lifecyclePoints]- properties:- target: # exactly one of image | service | remote- type: object- oneOf: [{required: [image]}, {required: [service]}, {required: [remote]}]- properties:- image:- type: object- required: [ref, port]- properties:- ref: { type: string } # @clerum/image-policy allowlist + digest preflight at install — same posture as mcp-server (§8.5/§12.4)- port: { type: integer, minimum: 1, maximum: 65535 }- imagePullSecrets: { type: array, items: { type: string } }- envSecret: { type: string } # hook's OWN creds, resolved in the llm-hooks ns (never the LLM secret)- egressBindings: { type: array, items: { type: object,- properties: { toFQDN: {type: string}, cidr: {type: string},- ports: {type: array, items: {type: integer}} } } }- security:- type: object- properties:- addCapabilities: # ⊆ @clerum/workflow-recipe-capability-policy; enforced at admission + reconcile- type: array- default: []- items: { type: string, enum: [CHOWN, FOWNER, DAC_OVERRIDE, NET_BIND_SERVICE] }- service:- type: object- required: [name, namespace, port]- properties:- name: { type: string }- namespace: { type: string }- port: { type: integer, minimum: 1, maximum: 65535 }- remote:- type: object- required: [baseUrl]- properties:- baseUrl: { type: string, pattern: '^https://' } # §8.3 SSRF-validated at dial- authHeadersSecret: { type: string }- path: { type: string, default: '/' } # API path on the endpoint; calls go to {endpoint}{path}/v1/{point}.- # image-mode hooks sharing an image digest are co-located on one pod,- # routed by distinct paths (§8.2 digest-dedup); paths must be unique per pod.- lifecyclePoints:- type: array- minItems: 1- maxItems: 4 # bounds the CEL exists() cost below- items: { type: string, enum: [preCall, moderate, postCallSuccess, onError] }- contentAccess: # §8.4/§8.7 content projection: content = bodies, metadata = no bodies.- type: string # moderate/postCallSuccess/onError are inherently content; only a- enum: [metadata, content] # preCall-only hook may be metadata. Omit ⇒ platform infers.- order: { type: integer, default: 100 }- failMode: { type: string, enum: [open, closed], default: closed }- timeoutMs: { type: integer, default: 5000, minimum: 1 }- onUnavailable:- type: object- properties:- mode: { type: string, enum: [strict, breaker], default: strict }- failureThreshold: { type: integer, default: 5, minimum: 1 }- cooldownMs: { type: integer, default: 30000, minimum: 0 }- capabilities: # admin-granted (§4.3); enforced ⊆ guardrails.capabilityCeiling (§5)- type: array- items: { type: string, enum: [may_deny, may_rewrite, may_substitute_result, may_add_context] }- config: { type: object, x-kubernetes-preserve-unknown-fields: true }- x-kubernetes-validations: # CEL- - rule: "!has(self.target.remote) || self.target.remote.baseUrl.startsWith('https://')"- message: "remote.baseUrl must be https://"- - rule: "!(has(self.capabilities) && self.capabilities.exists(c, c == 'may_deny')) || self.failMode == 'closed'"- message: "a deny-authoritative hook (may_deny) must fail closed"- - rule: "!has(self.contentAccess) || self.contentAccess == 'content' || !self.lifecyclePoints.exists(p, p == 'moderate' || p == 'postCallSuccess' || p == 'onError')"- message: "contentAccess: metadata is not allowed for a moderate/postCallSuccess/onError hook"- status: # reconciler-written; surfaced in control-ui, like McpServer.status- type: object- properties:- observedDigest: { type: string } # image digest actually running- readyReplicas: { type: integer } # ready replicas of the hook Deployment (image target)- lastReconciled: { type: string } # RFC3339- conditions: # standard K8s conditions (Deployed, Ready, …) — the- # reconcile state is a conditions[] array, NOT flat- # phase/message fields. control-ui reads .conditions.- type: array- items:- type: object- required: [type, status]- properties:- type: { type: string } # e.g. Ready, Deployed- status: { type: string, enum: ['True','False','Unknown'] }- reason: { type: string }- message: { type: string } # human-readable explanation- lastTransitionTime: { type: string } # RFC3339- observedGeneration: { type: integer }- subresources:- status: {} # HCC writes status; the mcp-host runtime cannot-```--Four filled-in `LlmHook` CRs. `pii-redact` is an **`image`** hook on its own pod; `budget-guard` and-`fees-guard` are two **`image`** hooks that **share the same image digest**, so HCC co-locates them on-**one** pod and routes them by `path` (digest-dedup, below); `acme-scan` is a vendor **`remote`** endpoint:--```yaml-# image — its own digest ⇒ its own pod; HCC deploys Deployment+Service+NetworkPolicy in llm-hooks ns-apiVersion: clerum.io/v1alpha1-kind: LlmHook-metadata: { name: pii-redact, namespace: llm-hooks }-spec:- target:- image:- ref: registry.evenfire.io/hooks/pii-redact@sha256:4b7e…d1 # image-allowlist + digest at install- port: 8080- envSecret: pii-redact-creds # the hook's OWN creds, never the LLM secret- egressBindings:- - { toFQDN: api.presidio.example, ports: [443] }- # path omitted ⇒ '/'- lifecyclePoints: [moderate, postCallSuccess] # POSTed at {svc}/v1/moderate, /v1/post_call- order: 20- failMode: closed # deny-authoritative → must fail closed (CEL)- capabilities: [may_deny, may_rewrite]- config: { redactionMode: strict } # opaque hook settings — never a trust input (§8.4)-----# image — SAME digest as fees-guard below ⇒ co-located on ONE pod, distinguished by `path`-apiVersion: clerum.io/v1alpha1-kind: LlmHook-metadata: { name: budget-guard, namespace: llm-hooks }-spec:- target:- image: { ref: registry.evenfire.io/hooks/spend-guard@sha256:aa01…f9, port: 8080 }- path: /budget-guard # calls go to {svc}/budget-guard/v1/{point}- lifecyclePoints: [preCall]- order: 10- failMode: open # advisory only → may fail open- onUnavailable: { mode: breaker, failureThreshold: 5, cooldownMs: 30000 }- capabilities: [may_add_context]-----# image — SAME digest as budget-guard ⇒ reuses that pod; NOT a second deployment-apiVersion: clerum.io/v1alpha1-kind: LlmHook-metadata: { name: fees-guard, namespace: llm-hooks }-spec:- target:- image: { ref: registry.evenfire.io/hooks/spend-guard@sha256:aa01…f9, port: 8080 }- path: /fees-guard # calls go to {svc}/fees-guard/v1/{point}- lifecyclePoints: [preCall]- order: 11- failMode: open- capabilities: [may_add_context]-----# remote — external HTTPS endpoint mcp-host dials directly (SSRF-validated, §8.3); no proxy pod-apiVersion: clerum.io/v1alpha1-kind: LlmHook-metadata: { name: acme-scan, namespace: llm-hooks }-spec:- target:- remote:- baseUrl: https://guardrails.acme.example # https only (CEL)- authHeadersSecret: acme-scan-auth- lifecyclePoints: [moderate]- order: 30- failMode: closed- capabilities: [may_deny]- config: { ruleset: pii-default } # opaque hook settings — never a trust input (§8.4)-```--`spend-guard@sha256:aa01…f9` is deployed **once**; `budget-guard` and `fees-guard` both resolve to that-pod's Service and are addressed at `/budget-guard/v1/pre_call` and `/fees-guard/v1/pre_call`. For any of-these to fire, the host's `Host.spec.guardrails.hooks` must list them by id + digest under the matching-phase (§5) — `pii-redact` and `acme-scan` under `moderate`, `budget-guard`/`fees-guard` under `preCall` —-and each hook's `capabilities` must stay within `capabilityCeiling`. The CEL rules reject a `remote` hook-whose `baseUrl` is not `https://`, and any `may_deny` hook set to `failMode: open`.--**Delivery modes & reconcile flow.** Exactly one `target`: **`image`** — HCC deploys **(or reuses — see-digest-dedup)** a Deployment+Service+NetworkPolicy in the **`llm-hooks`** namespace-(`CONTROL_API_LLM_HOOKS_NAMESPACE`), ingress only from the mcp-hosts that reference it (§12.2), egress per-`egressBindings`; **`service`** — an existing in-cluster Service (e.g. operator-managed), nothing deployed;-**`remote`** — an external-HTTPS endpoint mcp-host dials directly (§8.3), no proxy pod.--**Container hardening (untrusted image).** The hook image is untrusted third-party code even when the CR is-operator-authored, so HCC deploys it with the **same hardened pod `securityContext` as managed mcp-server-workloads**: `capabilities.drop: [ALL]`, `runAsNonRoot`, non-root uid/gid (≥1000),-`allowPrivilegeEscalation: false`, `seccompProfile: RuntimeDefault`; no privileged / hostPath / hostNetwork-path is exposed. `security.addCapabilities` is constrained to the shared-`@clerum/workflow-recipe-capability-policy` allowlist (`CHOWN`, `FOWNER`, `DAC_OVERRIDE`,-`NET_BIND_SERVICE`) and enforced at **both** admission (the CRD enum) and reconcile — a hook can never-obtain `NET_RAW`, `SYS_ADMIN`, `NET_ADMIN`, `SYS_PTRACE`, etc. (§12.1).--**Digest-dedup (one pod, many functions).** HCC does not blindly deploy one pod per `image`-mode hook. It-derives a **pod key** from the hook's pod-level fields — image `ref` (digest), `port`, `envSecret`,-`egressBindings`, `security` — and deploys **one** Deployment+Service+NetworkPolicy per distinct pod key.-Every `image` hook sharing that pod key (which, because the key includes the digest, means the *same-image*, hence the same code, creds, and egress) is **co-located on that one pod** and addressed by its own-`path`; hooks with a different pod key get their own pod. The shared pod's NetworkPolicy admits **exactly-the mcp-hosts whose Host CR references it**, re-reconciled as that set changes — so reachability tracks the-admin-authored `guardrails.hooks` lists (§12.2). HCC reference-counts the pod key and garbage-collects the-workload when its last referencing hook is removed; `path` must be unique within a pod key. This keeps pod-count (and always-on compute, §8.6) flat as functions grow, **without** unioning egress across unrelated-hooks: co-tenants are the same image by construction, so they already share one egress/trust boundary — a-hook with different creds or egress necessarily has a different pod key and its own pod. Each co-tenant-remains an independently-governed contributor (its own `lifecyclePoints`, `order`, `capabilities`,-`failMode`) in each referencing Host's `guardrails.hooks` list, and each independently passed the same-install-time digest + allowlist + `trust_level` check.--```mermaid-flowchart LR- subgraph CP["control plane"]- UI["control-ui<br/>registry install"] --> SAGA["control-api<br/>install-hook saga"]- SAGA -->|writes| CR["LlmHook CR<br/>(+ credential Secret)"]- SAGA -->|digest + image-allowlist + trust_level| SAGA- end- CR --> HCC["host-context-controller<br/>llmHookReconciler"]- HCC -->|"image: new pod key"| DEP["Deployment + Service + NetworkPolicy<br/>(llm-hooks ns)"]- HCC -->|"image: existing pod key"| REUSE["reuse pod, add path route<br/>(ref-count++)"]- HCC -->|service / remote| NOP["no workload deployed"]- CR -. watch .-> MH["mcp-host<br/>hookChainProvider"]- MH --> RH["RemoteLlmHook"]- RH -->|image / service| DEP- RH -->|remote: direct egress + SSRF §8.3| EXT["external endpoint"]-```--control-api's saga only **writes** the CR + Secret; **HCC** reconciles it to workloads **and reports-hook-pod health into `LlmHook.status`** (ready/running/failed — the same way it surfaces mcp-server state;-control-ui reads it, §13.4); **mcp-host** only **watches** the CRs to resolve the hooks its-`Host.spec.guardrails.hooks` block lists (opt-in per Host — §5; there is no auto-apply scope) and builds-`RemoteLlmHook`s — it deploys nothing.--**Credential rotation (`reconcileByEnvSecret`).** The hook's `envSecret` **name** is part of the pod key,-but its **contents** are folded into a separate **credentials-revision** — a digest of the Secret's data-stamped on the pod template — so rotating the Secret in place (same name, new value) changes the template-and **rolls the hook pod onto the new credentials**, exactly the mcp-server mechanism. HCC **MUST**-therefore watch the `llm-hooks` Secrets and re-reconcile on change — a `reconcileByEnvSecret` for `LlmHook`-**mirroring `McpServer`** — fanning the event out to every pod key whose `envSecret` names the changed-Secret, so a rotation converges **immediately** rather than on the periodic resync.--**Hook resolution (`hooks` entry → CR).** A `guardrails.hooks` entry is `{ id, digest }`: **`id`** is the-`LlmHook` **CR name** in the `llm-hooks` namespace, **`digest`** the expected image sha256. mcp-host-resolves `id` → the `LlmHook` CR and checks `entry.digest == status.observedDigest` (the digest actually-running, §8.2 `status`). On **mismatch or unresolved CR**, the hook is **not loaded**: **fail-closed** (the-guarded call denies) if the entry is deny-authoritative (`may_deny`), else **skipped-with-alert** for an-advisory hook. This binds the admin's intended digest to the pod actually answering, closing the gap that-mcp-host can't see the image over HTTP.--**Resolution is live, not boot-only.** mcp-host **MUST** re-resolve `Host.spec.guardrails` on every `Host`-and `LlmHook` watch event, not only at startup, so an in-place CR edit — a changed `capabilities`, `path`,-`order`, or `failMode`, or a moved endpoint — takes effect on the running mcp-host **without a restart**.-Granting `may_deny` (or tightening `failMode`) via a CR edit must apply on the next guarded call, not on the-next reboot.--**Image updates are a coordinated re-install, never an in-place edit.** Because the ref digest is **pinned**-(`{ id, digest }`), an `image`-target update **MUST** go through the control-api install/upgrade flow, which-bumps the `LlmHook` CR image digest **and** every referencing Host's `guardrails.hooks[].digest` in one-transaction. A bare `kubectl edit` of the `LlmHook` image is **not** a supported update path: it moves the-pod key and `status.observedDigest` while the referencing Hosts still pin the old digest, so the digest-check fails and the hook stops serving (fail-closed for `may_deny`, §8.2) rather than upgrading — the pin-deliberately binds the admin-reviewed digest to the pod actually answering.--### 8.3 · `remote`-mode egress (direct, matching the status quo)--mcp-host dials a `remote` endpoint directly over its existing broad public-egress lane — the same way it-already reaches external LLM providers (`deploy/base/mcp-host/networkpolicies.yaml:108-121` —-`443 → 0.0.0.0/0`; `llm/claude.ts` calls `api.anthropic.com` directly). No per-hook proxy pod: the proxy-pattern exists to fence *untrusted connector pods*, which does not apply to the trusted mcp-host reaching-out.--**Destination safety is app-layer (the primary control):**-1. **Config-only provenance** — the target comes only from the admin-vetted, `trust_level`-gated `LlmHook`- CR, never from model/tool/user/hook data.-2. **Reuse the existing SSRF guard** (`core/tools/httpRequest.ts` + `core/safety/safety.ts`): block- RFC-1918 / loopback / link-local / metadata `169.254.169.254` / `*.cluster.local`; resolve A+AAAA and- reject if any is private (fail-closed on DNS failure); **DNS-pin** to the validated IP; **no redirects**.- (The reused guard permits both `http` and `https`; **`https` for remote hooks is enforced separately** by- the `remote.baseUrl` `^https://` pattern + CEL in §8.2 — not by this guard.)-3. **Payload** stays governed by the content projection (§8.4).--**L3 (network) — range-scoped, no cluster/internal reach.** The broad external lane MUST NOT be an-unqualified `0.0.0.0/0`: its `ipBlock.except` **MUST** exclude the cluster **pod, service, and node** CIDRs-and all private / link-local / metadata / reserved ranges — `10.0.0.0/8`, `172.16.0.0/12`,-`192.168.0.0/16`, `169.254.0.0/16` (incl. metadata `169.254.169.254`), `100.64.0.0/10`, `127.0.0.0/8`,-`0.0.0.0/8` (plus the IPv6 equivalents on a dual-stack cluster). This is the **network mirror of the-app-layer `isPrivateIp` guard**, so even a path that bypasses the app guard cannot reach a cluster-internal-or metadata IP on 443/80 — the same lane both external LLM providers and `remote` hooks use. Cluster-internal-destinations remain reachable **only** through the narrow, explicit per-destination lanes (HCC gateway,-approval gateway, DNS, API server, Context mcp-servers, llm-hooks), which are separate additive allows and-are unaffected by the `except`. This is **range**-scoping, not **domain**-scoping — per-host domain precision-(N6) stays app-layer, with an optional Calico `destination.domains` policy as future defense-in-depth.--Hook (`egressBindings`) egress carries the same guarantee by construction: HCC validates every binding CIDR-with `isAllowedExternalEgressCidr` at reconcile and **rejects** any that overlaps a private / metadata /-reserved range (§8.2), so a hook can never declare a cluster-internal destination in the first place.--### 8.4 · Content exposure--An installed hook receives message/response **content** only where it is needed **and** only if it clears-the admin's trust floor. `moderate` / `post_call` / `on_error` are inherently content-bearing; `pre_call`-declares its need via `spec.contentAccess` — a **metadata-only shaper** (`metadata`: model+params+usage, no-bodies) or a **content-aware rewriter** (`content`: full projected messages, e.g. an input-token-optimizer). The archetypes and the content×egress→trust matrix are catalogued in §8.7. **The trust decision uses only inputs the hook author does not-control:** a hook's **`trust_level`** (low/mid/high) is **assigned by the platform at install**-(registry vetting) and stamped onto the CR by the saga — the author cannot set it — and the floor is the-admin-authored **`Host.spec.guardrails.minInstalledHookTrustLevel`** (§5). Content is granted only when-`trust_level ≥ minInstalledHookTrustLevel` (a hook below the floor does not run at all). The hook's own-`spec.config` is **opaque hook settings passed through to the container and is never consulted for any-trust or exposure decision** — a hook cannot self-certify its own access. Every content-bearing call is-audit-logged (hook + phase + request id, not the content). The mcp-host LLM secret name is **omitted by-construction**: a hook's `/v1` request body is built only from the redacted content projection above-(messages/response/params/usage) — there is no host-context object threaded to hooks, so `llmSecretName`-never appears anywhere on the hook path. The hook's own credentials live in its own Secret (`envSecret`,-resolved only in `llm-hooks`, §12.1/N9).--**Content + egress are separated by default (exfiltration control).** A **content-bearing** hook — any hook-that receives message/response bodies: `moderate` / `post_call` / `on_error`, or a `pre_call` hook with-`contentAccess: content` — **MUST NOT** declare `egressBindings` unless its `trust_level` is `high`. A hook-that both *sees content* and *can reach the network* is an exfiltration path, so the two are combined only-for an explicitly high-trust, vetted hook (e.g. an approved PII processor). A **content-free** hook-(`pre_call` with `contentAccess: metadata`) has no conversation to leak and so **may** carry egress at-low/mid trust (§8.7 matrix). This is enforced at **install** (the saga/HCC refuses a content-bearing hook with egress below-`high`), since `trust_level` is set at install, not in the CR. A **`remote`-mode** content-bearing hook is-external egress *by construction* — its `target` **is** the destination, so there are no `egressBindings`-to gate — and is therefore admissible **only at `trust_level: high`**, the same content-plus-network bar.-Even at `high`, egress remains a **residual**-exfiltration channel — including DNS-tunneling via `toFQDN` lookups and timing channels — that-`trust_level` mitigates but cannot eliminate; that is why content-bearing calls are audit-logged. Prefer-content-free (`pre_call`-metadata) hooks, or in-cluster (`image`/`service`) content hooks with **no**-`egressBindings` (§12.4).--**The content projection (`projectForHook`) — normative contract.** mcp-host **MUST** build every `/v1`-request body from a per-point projection, never by handing the hook the raw `ToolCompletionRequest`/response.-The projection has exactly two jobs — it **scopes** (selects fields), it does **not scrub** (never masks-values inside a field it does send, so a PII/moderation hook still sees the raw content it exists to-inspect; redaction stays a hook's `post_call` **output** transform, never an input filter). Per point:--| Point (§8.1) | `contentAccess` | Body fields sent |-|---|---|---|-| `pre_call` | `metadata` | `model, params, usage, config`, tool **schemas** (names/params) — **no `messages`** |-| `pre_call` | `content` | the above **+** non-system `messages` |-| `moderate` | (always `content`) | non-system `messages`, `model, usage, config` |-| `post_call` | (always `content`) | `response{content,tool_calls,finish_reason}, usage` |-| `on_error` | (always `content`) | `error`, non-system `messages`, `model, usage` |--Two invariants hold for **every** projection, regardless of `contentAccess`:-1. **System prompt is never exposed.** All system-role messages are stripped (the modern `systemPromptParts`- path already keeps it out of `messages`; the projection closes the legacy in-band path) — an installed- hook never sees the first-party system prompt, only edits non-system messages (§8.1).-2. **`contentAccess` is a self-enforcing declaration, not self-certification (§12.1).** Declaring `metadata`- only *reduces* what the hook receives (runtime-enforced by this projection); declaring `content` only- *tightens* its own install gate. So an author-set `contentAccess` can never widen access — the- platform-assigned `trust_level` is untouched — which is why it is admissible as a CR field.--The install gate (§8.5) keys "content-bearing" on `contentAccess`: `moderate`/`post_call`/`on_error` always,-and `pre_call` unless it declared `contentAccess: metadata`. **Implemented** — `projectForHook`-(`mcp-host/src/core/guardrails/hooks/hookProjection.ts`) enforces the projection at every point and the-install gate (`control-api`) keys on `contentAccess`, so the two agree by construction (the gate and the-runtime landed together, §12.4). A `metadata` `pre_call` shaper genuinely receives no message bodies and so-may egress at low/mid trust; a `content` `pre_call` optimizer is gated like any content hook.--### 8.5 · Registry install (organization-scoped)--Installed hooks come **only from the registry**, and only from an **organization-scoped** catalog — the-org's own vetted entries, not a public/global marketplace, and **not** arbitrary "private" bring-your-own-images (out of scope for v1). An admin installs a hook from that catalog; there is no path to run an image-that isn't a registry entry.--- Registry `entry_type: 'llm-hook'` (hyphen, as the code checks it — `registry.ts`), **org-scoped**, with- `hook_meta { target(image|service|remote), lifecyclePoints[], path, defaultConfig, requiredEgress[] }`.- The **`credential_schema` is fetched via a separate registry endpoint** (`getCredentialSchema()`) at- install time, not carried as a `hook_meta` subfield. Extending the `evenfire-registry` schema is a- prerequisite we own (the schema lives in the external registry service, not this repo).-- `POST /admin/registry/install-hook` saga (cloned from `install-recipe`): verify sha256 bundle digest →- validate + store credential Secret → build the `LlmHook` `target` → image-allowlist preflight → stamp- `catalog-id/version` → create CR → rollback on failure → `reportInstall`. Installed-state derived from- CRs.-- **Trust reuse (no new primitives):** `@clerum/image-policy` allowlist, sha256 digest, per-org pull- secret, `@clerum/workflow-recipe-capability-policy`, `trust_level`/`quality_tier`, install-time- credential Secret, per-workload egress NetworkPolicy.-- **Deferred:** a public/cross-org marketplace; private BYO-image hooks with no registry entry.--### 8.6 · Fail-posture--Fail-closed is the default and **mandatory for any deny-authoritative hook** (built-in or installed). A-`breaker` (trip to fail-open after N failures, alert, re-probe) is permitted **only** for non-authoritative-(advisory/observability) hooks and **only** when admin policy opts in (`onUnavailable.mode: breaker`,-§8.2). A `may_deny` hook always dials `strict` — its breaker is ignored so it can never be tripped into-failing open. Breaker state is **per-hook, not global** (one hook tripping never affects another): because-the hooked LLM port is rebuilt per logical request, the state lives in a process-shared-`HookBreakerRegistry` keyed by hook identity (`endpoint+path`), so a trip survives across requests and a-down hook is short-circuited (not re-dialed and re-timed-out) until its cooldown elapses, then re-probed-once. (`mcp-host/src/core/guardrails/hooks/hookBreaker.ts`.)--### 8.7 · Hook archetypes (what we expect people to install)--A hook is not defined by its vendor brand but by **four independent axes**, and those axes — not the name-on the box — decide how it is delivered, what data it sees, and what trust tier it needs. Everything in-§8.1–§8.6 is the machinery; this section is the catalog of shapes that machinery is built to carry.--- **Lifecycle point(s)** (§7.1/§8.1) — `pre_call`, `moderate`, `post_call`, `on_error`. One hook may serve- several (e.g. a firewall that screens the prompt at `moderate` and the answer at `post_call`).-- **Content need** (`spec.contentAccess`: `metadata` | `content`) — whether the hook receives message /- response **bodies** or only `model+params+usage` metadata. `moderate` / `post_call` / `on_error` are- **inherently `content`**. `pre_call` is the **only** point with two flavors: a **metadata-only shaper**- (`metadata`) or a **content-aware rewriter** (`content`, e.g. an input-token optimizer). See "Two flavors- of `pre_call`" below.-- **Egress** — **none** (in-cluster `image`/`service` under default-deny, N3/N5) or **network**- (`egressBindings`, or a `remote` target which is egress by construction).-- **Delivery / trust** — `image` (HCC-deployed, hardened pod, N8), `service` (existing in-cluster Service),- `remote` (external SaaS), or a first-party **built-in** (in-process, full trust, **outside §8.4- entirely** — §7.2).--**The gating rule is one line (§8.4): _content **and** egress ⇒ `trust_level: high`._** Content alone, or-egress alone, is admissible at low/mid trust — the danger was never "sees content," it is "sees content-**and** can ship it out."--| Sees content? | Can egress? | Trust needed | Why |-|---|---|---|---|-| no (`metadata`) | no | **low** | shapes params only; reaches nothing |-| no (`metadata`) | yes | **low/mid** | can call out, but has no conversation to leak |-| yes (`content`) | no | **low/mid** | reads prompt/response, but default-deny egress means it can't exfiltrate |-| yes (`content`) | yes (`egressBindings` / `remote`) | **high** | sees content **and** can ship it out — the exfiltration path §8.4 gates |--**Two flavors of `pre_call`.** `pre_call` is where a hook acts *before* the model call, and it splits by-content need:-- **Metadata-only shaper** (`contentAccess: metadata`) — tunes `params`, adds untrusted context, or gates- on request metadata, **without** seeing message bodies. Content-free, so it may carry `egressBindings` at- low trust (e.g. a budget guard that prices a request against a billing service).-- **Content-aware rewriter** (`contentAccess: content`) — reads the messages to **rewrite** them, e.g. an- input-token optimizer (prompt compression / LLMLingua-style, semantic dedup, RAG-context pruning). It is- content-bearing and gated as such; done as an in-cluster hook with **no egress** it stays low/mid trust- (it compresses locally and can't exfiltrate). The system prompt stays immutable (§8.1) — an optimizer- edits only non-system messages. Platform-owned token optimization should instead be the **`token-trim`- built-in** (§7.2), which runs in-process and sidesteps §8.4 altogether.--**Archetype catalog.**--| # | Archetype | Point(s) | `contentAccess` | Egress | Capabilities | Delivery | Trust | Representative |-|---|---|---|---|---|---|---|---|---|-| 1 | Inbound moderation / prompt-injection screen | `moderate` | content | remote / image+egress | `may_deny` (failMode closed) | `remote` or `image` | **high** | OpenAI Moderation, Google Model Armor (prompt), CrowdStrike Falcon AIDR, Aporia, Akto |-| 2 | Outbound response moderation / DLP | `post_call` | content | remote / image+egress | `may_deny` \| `may_substitute_result` | `remote` or `image` | **high** | same vendors, response side |-| 3 | In-cluster PII redactor | `post_call` (+`moderate`) | content | **none** | `may_substitute_result` | `image` | **low/mid** | Presidio-based redactor |-| 4 | Content-aware input optimizer | `pre_call` | content | **none** | `may_rewrite` | `image` | **low/mid** | token/prompt compression, semantic dedup, RAG pruning |-| 5 | Metadata-only shaper / policy guard | `pre_call` | metadata | allowed (low trust) | `may_rewrite` (params) \| `may_add_context` | `image` or `remote` | **low/mid** | budget / fees guard, deployment param tuning |-| 6 | Error fallback / recovery | `on_error` | content | none | `may_substitute_result` (text-only) | `image` | **low/mid** | safe canned-response provider |-| 7 | First-party built-ins | any | n/a (in-process) | n/a | full trust | **built-in** (§7.2) | trusted plane | `prompt-shaping`, `token-trim` |--**Worked examples.** Archetypes 1–3 follow the §8.2 CRs (`acme-scan`, `pii-redact`). The two below make-the `pre_call` split concrete — a content-aware optimizer that stays **low-trust because it has no egress**,-and a metadata-only guard that **may egress at low trust because it sees no content**:--```yaml-# Archetype 4 — content-aware INPUT-TOKEN OPTIMIZER.-# Reads messages to compress them (contentAccess: content) but declares NO-# egressBindings → content-without-egress → low/mid trust is sufficient: it-# rewrites the prompt locally and cannot exfiltrate it. System prompt is-# immutable to it (§8.1); it edits only non-system messages.-apiVersion: clerum.io/v1alpha1-kind: LlmHook-metadata: { name: prompt-compactor, namespace: llm-hooks }-spec:- target:- image: { ref: registry.evenfire.io/hooks/prompt-compactor@sha256:c0ffee…, port: 8080 }- # no egressBindings ⇒ default-deny egress (N5): local compute only- contentAccess: content # needs the message bodies to compress them- lifecyclePoints: [preCall] # POSTed at {svc}/v1/pre_call- order: 5 # run early, before content moderation- failMode: open # advisory optimizer → may fail open- onUnavailable: { mode: breaker, failureThreshold: 5, cooldownMs: 30000 }- capabilities: [may_rewrite] # rewrites (drops/compresses) non-system messages-----# Archetype 5 — METADATA-ONLY BUDGET GUARD.-# Sees model+params+usage but NOT message bodies (contentAccess: metadata), so-# it may carry egressBindings at low trust: it can call a billing service but has-# no conversation content to leak.-apiVersion: clerum.io/v1alpha1-kind: LlmHook-metadata: { name: budget-guard, namespace: llm-hooks }-spec:- target:- image:- ref: registry.evenfire.io/hooks/budget-guard@sha256:b00c…, port: 8080- egressBindings:- - { toFQDN: billing.internal.example, ports: [443] } # priced against a billing API- contentAccess: metadata # model+params+usage only — no bodies- lifecyclePoints: [preCall]- order: 10- failMode: open- capabilities: [may_add_context] # annotates the request with budget context (untrusted-framed, §12.4)-```--**Status: implemented.** `spec.contentAccess` is in the CRD schema (optional, CEL-guarded so it can't-contradict an inherently-content point). The runtime **content projection** (`projectForHook`, §8.4) scopes-every `/v1` body per point and always strips the system prompt, and the install gate keys on `contentAccess`-— so a `metadata` `pre_call` shaper genuinely receives no message bodies and may egress at low/mid trust,-while a `content` (or default) `pre_call` hook is gated like any content hook. Gate and projection agree by-construction.-------## 9 · Evidence — metrics & audit--- **Metrics** use **fixed-enum labels only** — `lane`, `decision`, `source`, `execution_mode`, `phase`,- `outcome`, `reason_code`, plus lane-supplied bounded dimensions. Ids, paths, args, free-form reasons, and- digests are **forbidden** as labels.-- **Audit** events carry the fixed field set below. Raw sensitive inputs, hook stdout/stderr/response- bodies, and credentials are **excluded** unless a separate protected-trace contract permits a redacted- form.--```-AuditEvent = {- ts, lane, boundaryId, requestId,- originalIdentity, resolvedIdentity, // resolved is the real target (§2)- originalInputDigest, effectiveInputDigest,- decision, source, sourceId, reasonCode, // reasonCode from the per-lane enum below- policyDigest, ruleDigests[], hookDigests[],- executionState, redactionStatus-}-```--- **Reason-code registry** is **per-lane** — each lane keeps its own bounded enum under the same- fixed-cardinality/no-PII label discipline (decided, §10 #5). Starter sets (extensible, but always bounded):- - **Tool lane:** `path_out_of_bounds`, `destructive_command`, `write_needs_approval`, `trusted_read`,- `trusted_egress`, `repeated_identical_call`, `approval_unavailable`, `rewrite_invalid`, `rewrite_loop`.- - **LLM lane:** `content_filtered`, `moderation_blocked`, `recovered`, `hook_unavailable`,- `rewrite_invalid`, `rewrite_loop`.- - **Shared engine:** `denied_by_rule`, `denied_by_hook`, `no_decision`, `allowed`.-------## 10 · Decisions (resolved)--1. **LLM-lane `ask`? — No.** `ask` (human approval) is **tool-lane-only**; the LLM lane uses- `allow`/`deny`/`no_decision`. Models are called constantly in the loop, so approval belongs on *actions*,- not on model calls (§7.4).-2. **Installed-hook latency fast-path? — No fast-path in v1.** The in-process **rules** engine and- **built-ins** handle the hot path with no round-trip; installed `Pre/PostToolUse` hooks are opt-in per- Host and pay the container round-trip only where added. Revisit only if profiling shows a bottleneck.-3. **`may_substitute_result` on the tool lane? — LLM-only.** Tool-lane result transformation is covered by- **PostToolUse redaction** (bounded, model-visible-only); `substitute` is granted only to LLM-lane hooks- (its home is `on_error recover`, §8.1). It stays in the capability set but isn't granted tool-lane hooks.-4. **Guardrail engine placement? — In-process shared library.** Both lanes already run inside mcp-host- (`HookedLlmPort` in-process; tool execution in mcp-host), so the engine is a library both import — no- network hop on the decision path. Installed **hooks** are the out-of-process extension point, not the- engine.-5. **Reason-code registries? — Per-lane, shared discipline.** Each lane keeps its own bounded enum (codes- genuinely differ); all obey the same §9 rules (fixed-cardinality, no free-form/PII labels).-6. **`may_ask` capability? — No.** The set stays `{may_deny, may_rewrite, may_substitute_result,- may_add_context}` (§4.3/§5/§8.2). A hook wanting a human in the loop `deny`s and the admin writes an- `ask` *rule*; hooks don't demand approval in v1.-------## 11 · Invariants--Universal mediation · resolved-identity evaluation · strict aggregation · rewrite-then-revalidate ·-at-most-once · authoritative-outcome-immutable · admin-cannot-be-weakened (policy on the Host CRD, which-the mcp-host runtime can't mutate) · authoritative-delivery (HCC-reconciled, digest-checked) · no-local-executable/CRD-scripting hook (built-in or installed container only) · fail-closed for any-deny-authoritative hook · response-capabilities-enforced (§8.1) · actions-from-model-only (hooks may-redact `tool_calls` but never introduce them; §8.1) · system-prompt-first-party (installed hooks edit only-non-system messages/params; the system prompt is immutable to them; §8.1) · trust-from-platform-not-self-(`trust_level` is install-assigned and hook `config` is never a trust input; content/run gates compare it-to the admin floor, §8.4) · bounded/redacted evidence · no-config compatibility.--### 11.1 · Network & pod isolation invariants--The enforced isolation model between mcp-host and the `llm-hooks` namespace, stated in full so the-guarantee and its mechanism can be reviewed together. Enforcement is **symmetric and HCC-owned**: the-mcp-host runtime never widens its own reach (§12.1), and every allowed flow is a narrowly-scoped,-controller-generated NetworkPolicy on top of a deny-everything baseline. All invariants below are-**Enforced** (implemented).--| # | Invariant | What it guarantees & how it is enforced | Status |-|---|---|---|---|-| N1 | **CRD-scoped reach** | An mcp-host pod may open a connection to an installed hook **only** when its admin-authored Host CR lists that hook in `guardrails.hooks`. This is enforced at both ends: the host's egress policy permits only the hook pods/ports it references, and each hook's ingress policy admits only the mcp-hosts that reference it. A host that doesn't reference a hook is denied on both sides — it never appears in that hook's ingress allow, and the hook never appears in the host's egress allow. | Enforced |-| N2 | **Symmetric enforcement** | Reachability is gated independently on both sides of a connection: the source host must have an egress allow **and** the destination hook must have an ingress allow. The two policies live in different namespaces and are generated separately, so a bug or gap on one side alone cannot open a path. This is deliberate defense-in-depth — losing either policy still leaves the other blocking. | Enforced |-| N3 | **`llm-hooks` default-deny (both directions)** | The namespace begins in a deny-everything state: one NetworkPolicy selects all pods and governs both ingress **and** egress with no rules, so every hook pod is denied all inbound and all outbound by default. Every allowed flow is therefore something HCC explicitly opened; nothing is reachable, and no hook can reach out, unless a controller-generated policy permits it. Reply traffic on an already-accepted inbound connection is still allowed (connection tracking), so denying egress does not break a hook responding to an mcp-host. | Enforced |-| N4 | **Mutual hook isolation** | A hook cannot initiate a connection to another hook, to any other pod in `llm-hooks`, or to any other namespace, unless HCC has added an explicit allow. On the receiving side, each hook's ingress admits only mcp-host pods — never other hooks — so hook-to-hook traffic is refused at the destination regardless of the source's intent. With default-deny-egress (N3), both the initiating and the receiving direction of hook-to-hook communication are closed. | Enforced |-| N5 | **Allowlist-only hook egress** | When a hook legitimately needs to call out (e.g. a remote moderation API), each destination must be declared in its CRD `egressBindings`, which HCC validates (SSRF / private-range checks) before turning into an egress allow. There is **no implicit egress — not even DNS** — so a hook that declares nothing can reach nothing outbound. A hook's outbound surface is thus an explicit, reviewable allowlist rather than open internet access. | Enforced |-| N6 | **Remote guardrails reach only their domain** | A `remote` hook is dialed only at the `baseUrl` host declared in its CRD. The shared SSRF guard enforces this at the app layer: https-only, resolve-and-pin the host's public IP, reject private/loopback/link-local/metadata ranges, and follow no redirects — so the connection can only land on the declared host. A network-layer Calico FQDN policy was considered and deliberately **not** required; the app-layer pin is the accepted enforcement. | Enforced (app-layer) |-| N7 | **Least-privilege scoped policies** | All mcp-host↔`llm-hooks` communication rides narrowly-scoped, HCC-generated NetworkPolicies — never a namespace-wide or wildcard allow. Ingress policies are scoped to a specific hook pod (by pod-key or Service selector) and the exact port; egress policies are scoped to the specific hook pods a host references. The interim namespace-wide egress policy is being replaced by this per-host scoped version. | Enforced |-| N8 | **Hardened, unprivileged hook pods** | Every hook container runs **non-root** with a **read-only root filesystem**, drops all Linux capabilities, forbids privilege escalation, uses the `RuntimeDefault` seccomp profile, and mounts **no** ServiceAccount token. HCC stamps these on the pod/container `securityContext` — they are not left to the image. This hardening is what makes pod-level (rather than strict per-hook) isolation acceptable under co-location (§12.5). | Enforced |-| N9 | **Secret isolation** | A hook's `envSecret` is resolved only within `llm-hooks` and can never name the mcp-host LLM key or any Host secret. Because hooks and their secrets are confined to their own namespace under scoped RBAC, one hook cannot read another hook's — or the platform's — credentials. Secret material never crosses the namespace boundary into a hook. | Enforced |-| N10 | **Runtime can't widen its own reach** | The per-Host mcp-host ServiceAccount may **read** (`get`/`list`/`watch`) `LlmHook` CRs only within its own **tenant-scoped** `llm-hooks` namespace — so it sees only its tenant's hooks (multi-tenant uses a per-tenant namespace prefix; cross-tenant isolation comes from that namespace boundary, not from withholding read) — and has **no write** verbs, so it cannot mutate any `Host`/`LlmHook` CR or touch any NetworkPolicy. A compromised mcp-host pod therefore cannot rewrite guardrail policy or open new network paths for itself; policy authorship stays entirely in the trusted control plane (control-api / HCC). | Enforced |-| N11 | **Fail-closed provisioning** | If HCC cannot build a correct scoped policy for a hook — e.g. the referenced Service has no selector, or a reference can't be resolved — it declines to create an over-broad allow and leaves the hook unreachable under the default-deny baseline. Reachability only appears when a precise allow is successfully provisioned; a provisioning failure fails **closed**, not open. (The separate hook-resolution fail-posture at load time is still open — see §12.4.) | Enforced |-| N12 | **No internal reach via broad egress** | The broad *external* egress lanes — mcp-host's `0.0.0.0/0:443\|80` LLM / `http_request` lane, and each hook's `egressBindings` — exclude the cluster pod/service/node CIDRs and all private/link-local/metadata/reserved ranges, so neither mcp-host nor a hook can reach a cluster-internal or metadata IP through them. Enforced by `ipBlock.except` on the mcp-host lane (the network mirror of the app-layer `isPrivateIp` guard, §8.3) and by `isAllowedExternalEgressCidr` validation of hook `egressBindings` at reconcile. Cluster-internal destinations stay reachable only through the narrow, explicit per-destination lanes. **Ops:** the same canonical CIDR set is applied through **two mechanisms** for two different lanes. (1) The **static** mcp-host base-manifest lane: its `except` is **filled by kustomize `replacements`** (`deploy/base/kustomization.yaml`) from the canonical `PublicEgressExceptionSet` (`deploy/base/public-egress-exceptions.yaml`); the base NP keeps `except: []`, and a raw (non-kustomize) base apply leaves it empty — which is why the dev cluster needed a live patch. (2) The **HCC-generated** mcp-server public-web egress lane: `networkPolicyReconciler` writes the same ranges directly from its `PUBLIC_EGRESS_EXCEPT_CIDRS` code constant (not via kustomize), and validates every hook `egressBinding` against it with `isAllowedExternalEgressCidr`. Same list, two owners (static base vs. controller). | Enforced |-------## 12 · Security considerations--### 12.1 · Trust boundaries--- **mcp-host** is trusted to mediate, but it processes **untrusted model output** and- attacker-influenceable tool arguments (prompt injection). Every guarded call passes the boundary (§2)- before execution; identity is always the resolved target, never a name pattern.-- **Installed hooks are untrusted third-party containers.** They are governed by image-allowlist + sha256- digest + `trust_level` + NetworkPolicy + admin-granted capabilities, receive only a redacted, need-based- content projection (§8.4), and are capability-enforced on their **responses**, not just their- declarations (§8.1). A container is never trusted to self-limit: all trust/exposure gates compare the- **platform-assigned `trust_level`** to the **admin floor**, never a value in the hook's own `config`- (§8.4).-- **control-api / HCC are the trusted control plane.** The **Host CR is admin-authored** through- control-api RBAC and is **immutable to the mcp-host runtime** — the per-Host ServiceAccount is read-only- on its own Host CR — so guardrail policy cannot be weakened from inside the pod.-- **CR authorship is a trusted-plane action.** `Host` and `LlmHook` CRs are created only by the- operator/admin through control-api (or platform controllers), never by untrusted principals — a party- that can create these CRs is already in the TCB (it could deploy an arbitrary pod or read any secret- regardless). So the installed-hook **image allowlist** and **secret references** are **supply-chain and- containment** controls — vetting a third-party *registry* image and bounding a hook's reach — not- defenses against a malicious CR author. Containment still matters because the hook **container itself** is- untrusted third-party code: it is confined by a hardened `securityContext` + capability allowlist (§8.2),- the dedicated `llm-hooks` namespace, NetworkPolicy, need-based content projection (§8.4), and- response-capability enforcement (§8.1).--### 12.2 · Hook-pod sharing boundary--An mcp-host can reach an installed hook pod only if its **admin-authored** Host CR lists that hook in-`guardrails.hooks` (§5), and the pod's NetworkPolicy admits **exactly the mcp-hosts that reference it**-(§8.2). Co-locating several functions on one pod (digest-dedup) is an **optimization of that admin-decision, not an independent sharing channel**: two Hosts share a pod only because an admin listed the same-hook in both. This is the **same admin-defined + NetworkPolicy-enforced** model the platform already uses-to share one mcp-server across the Hosts of a Context. A content-bearing shared hook has a larger data-surface than a shared mcp-server (full message/response content vs. one tool's arguments); that exposure is-an **operator risk-acceptance**, managed by `trust_level` + the content/egress separation of §8.4 and by-simply not co-listing a hook that must stay isolated. Cross-tenant leakage does not arise from digest-matching: in the managed model tenants are separate namespaces (HCC never spans them), and in single-cluster-mode all Hosts are one operator's own domain. A per-hook pod-isolation knob is intentionally **not**-specified until a concrete requirement exists.--### 12.3 · Predicate limits--Argument predicates (§6.1) constrain declared arguments only and are **defense-in-depth, not a sandbox**:-`command` predicates see `argv[0]` and are bypassable through a shell wrapper (`sh -c …`), and `path`/`url`-predicates are not TOCTOU-safe. Authoritative containment is the hard-validator / sandbox layer (§6.6),-which a guardrail `allow` never bypasses.--### 12.4 · Known residual risks & follow-ups--- **`pre_call` rewrite silently discarded by a too-tight response cap — RESOLVED (§8.1/§8.6).** The response- cap was a single hardcoded 64 KiB (`maxHookOutputBytes` existed in config but was never wired). A- `pre_call` `may_rewrite` hook (e.g. a token compactor) returns the whole rewritten conversation, which- blows past 64 KiB → mcp-host marked it `unavailable` → for a `failMode: open` advisory hook that's *no- contribution* → the original **uncompressed** prompt went to the model, invisibly (the hook logged 200;- the fail-open path logged nothing). It also poisoned the breaker (each oversized response counted as a- failure → trip after N → the hook skipped for the cooldown, including small payloads that would fit).- Fixed: **point-aware caps** (generous `maxHookRewriteBytes` 5 MiB for `pre_call`, tight `maxHookOutputBytes`- 1 MiB for verdicts/redactions), both **wired from the admin `limits` block**; an over-cap response is now- **logged** (never silent) and flagged **`oversized`** so the breaker does not count it. The streaming cap- still enforces the larger ceiling for OOM safety.-- **Hook transport liveness — RESOLVED (§8.1/§8.6).** Earlier the `remote` transport dropped the abort- signal and relied on a socket **idle** timer (reset by a trickling peer) and capped the body only **after**- fully buffering it — so an active-but-unbounded hook endpoint (a compromised/hijacked vendor domain, not- only a malicious publisher) could hang a `pre_call`/`moderate` dial forever (the promise never settled, so- `failMode` was bypassed, not applied) or OOM the pod; with `maxConcurrent` session slots held until a task- settles, a few trickling sockets could deny the whole Host. Fixed four ways: (1) the SSRF transport honors- the abort `signal` as an absolute deadline (`requestPinned` destroys on abort); (2) the body is capped- **while streaming** on both the external and in-cluster paths (never fully buffered → no `ERR_STRING_TOO_- LONG`); (3) `RemoteLlmHook` races every dial against a **hard per-hook deadline** so a never-settling- transport still yields `unavailable` → `failMode`; and (4) `main.ts` installs `uncaughtException`/- `unhandledRejection` guards so a stray listener throw is logged, not a silent exit. A fully-silent peer,- connection-refused/DNS/5xx/malformed-JSON, and the SSRF pinning were already handled. The native- `http_request` tool shares `requestPinned` and had the same latent weakness — it now passes an absolute- deadline (`AbortSignal`) and a byte ceiling (10 MiB, above its 50 KB display-truncation) so the same- trickle/flood bounds apply there too.-- **Content projection & content/egress gate — RESOLVED (§8.4/§8.7).** Earlier the install gate classified- only `moderate`/`postCallSuccess` as content-bearing while the runtime handed the full- `ToolCompletionRequest` (messages) to `pre_call`/`on_error` with no projection, so a low/mid-trust- `pre_call`/`on_error` egress hook could exfiltrate the whole conversation. Now: (1) `projectForHook`- (`mcp-host/src/core/guardrails/hooks/hookProjection.ts`) builds every `/v1` body from a per-point- projection — `metadata` `pre_call` gets no message bodies, content points get non-system messages, and the- system prompt (`role:'system'` + `systemPromptParts`) plus internal fields are stripped from **every**- projection; and (2) the install gate keys on `spec.contentAccess` (§8.5), so a `metadata` `pre_call`- shaper may egress at low/mid trust while any content-bearing hook with egress requires `high`. Gate and- projection agree by construction — the residual exposure is closed. Remaining nuance: the projection- **scopes** fields, it does not **scrub** values (a content hook still sees raw PII to detect it, §8.4);- content-bearing egress hooks therefore remain an operator risk-acceptance managed by `trust_level` (below).-- **`remote`-hook egress is range-scoped, not domain-scoped.** At the **network** layer the broad lane- excludes all cluster/private/metadata ranges via `ipBlock.except` (N12/§8.3), so mcp-host and hooks cannot- reach a cluster-internal or metadata IP on 443/80. What the network layer does **not** do is pin the- *domain* — any *public* host on 443/80 is reachable — so per-hook domain precision is **app-layer** (N6):- the shared SSRF guard is https-only, DNS-pinned, rejects private/metadata ranges, and follows no redirects,- so a `remote` hook reaches only its `baseUrl` host. The app-layer pin is the **accepted enforcement** for- N6; a network-layer Calico `destination.domains` policy is **not** required (decided; OSS Calico can't- express it anyway) but remains a future defense-in-depth option (Calico Enterprise / Cilium / egress proxy).-- **Image trust matches the mcp-server posture** — a code-side `@clerum/image-policy` allowlist + digest- preflight at **install** (§8.5), gated by the same enforcement flag; **not** an admission webhook and not- verified at call time. Because CR authorship is a trusted-plane action (§12.1), this is a- supply-chain/integrity control on the operator's own install flow, and it inherits mcp-server's known- limitation rather than adding a hook-specific one; a platform-wide admission-time image policy, if ever- adopted, would cover hooks too. The `llm-hooks` namespace's write-access is part of the TCB (§8.2).-- **`may_add_context`** is a model-steering (prompt-injection) capability: added context must be delimited- as *untrusted* tool/hook output, never injected at system level.-- **Content-bearing hooks** can still exfiltrate within their egress allowlist (DNS-tunneling, timing);- §8.4 separates content from egress by default, but `trust_level` mitigates rather than eliminates the- residual channel.-- **Unattended `no_decision`** is defined as a true no-op that defers to the existing approval path (§6.3),- preserving no-config compatibility — operators fail-safe by writing an explicit `deny`/`ask` rule, not by- relying on `no_decision`.-- **Rewrite / resume** must re-aggregate the **full** deny-capable chain against the effective input- (§4.1/§4.2), not compare policy digests alone.-- **Hook transport** caller authentication is at the **network layer** (per-hook ingress admits only the- referencing mcp-hosts, N1/N7); the app-layer bearer + hook-side verification (§8.1) is **deferred/advisory**- and not currently minted, and there is no mTLS. The Service→pod binding in the hook namespace is part of- the TCB.-- **Hook-supplied `code`/`message`** strings are untrusted response data — length-bounded, never used as- metric labels, redacted in audit.--### 12.5 · Network & pod isolation — co-location note--The enforced network & pod isolation invariants (**N1–N11**) are stated in full, with mechanism and-status, in **§11.1**. This section records the one design consideration behind them.--**On co-location (digest-dedup) and N1/N8.** Digest-dedup co-locates hooks that share an **identical image**-on one pod, so a host reaching that pod can reach a co-located sibling by `path`. This is acceptable-because the sibling is **the same image by construction** (an admin co-listed identical hooks, §12.2) and-the container is hardened per **N8** (non-root, read-only root fs, dropped caps) — there is no distinct,-more-privileged hook to escalate into. Strict one-pod-per-hook isolation is therefore **not** required;-N1 holds at pod granularity, and image hardening (N8) covers the intra-pod boundary.-------## 13 · Implementation plan--**Status: implemented (Phases 1–4).** The LlmHook CRD (`charts/clerum-crds/crds/llmhook.yaml`), its-reconciler (`host-context-controller/src/llmHookReconciler.ts`), the `/v1` protocol client-(`mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts`), the in-process engine + hooked LLM port-(`mcp-host/src/core/guardrails/`, `core/adapters/hookedLlmPort.ts`), the org-registry install/upgrade-sagas (`control-api/src/routes/admin/registry.ts`), and the control-ui hook dashboard-(`control-ui/app/guardrails/`) are all in code, and the network/pod isolation invariants (§11.1 N1–N12)-are enforced in the cluster. The plan below is retained as the design-of-record for how the build was-**phased so each phase is independently shippable** and de-risks the next, following the §10 decisions:-the engine is **in-process** (#4), **rules + built-ins carry the hot path** (#2), **installed hooks are-the only out-of-process piece** (#4), and `ask` is **tool-lane-only** (#1). Known deltas between this spec-and the code are tracked in §12.4.--### 13.0 · Prerequisites (own before the phase that needs them)--- **`llm-hooks` namespace** + `CONTROL_API_LLM_HOOKS_NAMESPACE` config — before Phase 3.-- **`@clerum/image-policy`** allowlist entries + enforcement flag for hook images (§8.2/§12.4) — before Phase 3.-- **`evenfire-registry`** schema extension for `entry_type: llm-hook` + `hook_meta` (§8.5) — before Phase 4.--### 13.1 · Phase 1 — Core engine + tool-lane rules (in-process, no hooks)--- The lane-neutral **`GuardrailBoundary`** engine as an in-process library in mcp-host (§2–§4): contributor- contract, strict aggregation `deny>ask>allow>no_decision`, rewrite-then-revalidate, at-most-once +- snapshot/resume, capabilities.-- **Tool-lane adapter** (§6): the declarative **rules** engine (typed `path`/`url`/`command`/`json`- predicates), provenance from the registry, doom-loop guard, and integration with the existing hard- validators/containment (§6.6).-- **Admin policy** (§5): the `Host.spec.guardrails` block (rules + limits) added to the **existing Host CRD**- (no new CRD yet), reconciled by HCC into mcp-host.-- **Approvals** (§6.3): interactive vs unattended by resolver availability; **evidence** (§9).-- **Correctness to nail here (engine behaviors):** rewrite re-aggregation (F8), resume re-evaluation (F10),- unattended `no_decision` (F7).-- **Ships:** admin-authored allow/ask/deny tool policy with **zero new infra** — no hooks, no new CRD, no LLM lane.-- **Services:** mcp-host (engine + tool adapter); control-api + HCC (Host `guardrails` block delivery).--### 13.2 · Phase 2 — LLM lane + built-ins (still in-process)--- **`HookedLlmPort`** decorator above failover (§7); the lifecycle state machine- (`pre_call`/`moderate`/`post_call`/`on_error`), one firing per logical request.-- **Built-in hooks** `prompt-shaping` + `token-trim` in-process (§7.2), selected via `guardrails.builtins`.-- **Aux/compaction lane** (§7.4): `token-trim` + observability now; the **transform-only PII-redaction**- that §7.4 requires depends on a redaction contributor, so it lands with Phase 3 (or a first-party- redaction built-in if pulled forward).-- **Ships:** LLM-lane request shaping + response handling + recovery with first-party built-ins; still no- third-party containers.-- **Services:** mcp-host.--### 13.3 · Phase 3 — Installed hooks (out-of-process) + `LlmHook` CRD + reconciler--- **New `LlmHook` CRD** (§8.2) at `charts/clerum-crds/crds/llmhook.yaml`.-- **`llmHookReconciler`** in host-context-controller: `image`/`service`/`remote` delivery, **digest-dedup**- pod key, NetworkPolicy = union of referencing Hosts, **container hardening** (securityContext + capability- allowlist, §8.2), the `llm-hooks` namespace.-- **`/v1` protocol client** (`RemoteLlmHook`) in mcp-host: the four lifecycle points, RS256 bearer auth,- redacted content projection, **response-capability enforcement**, and the subtractive-actions +- system-prompt-immutable invariants (§8.1).-- **Content/egress separation** + `trust_level` gating (§8.4); **`remote`-mode SSRF** reuse (§8.3).-- Wire installed hooks into both lanes (Pre/PostToolUse; `moderate`/etc.); compaction PII-redaction (§7.4)- now available.-- **Prereqs:** `llm-hooks` namespace, image-policy entries (§13.0).-- **Ships:** third-party guardrail/PII hooks in any language.-- **Services:** HCC (reconciler), mcp-host (`RemoteLlmHook`). CRs are applied directly (kubectl/dev) for- testing in this phase; the **org-registry install path + control-ui management arrive in Phase 4**.--### 13.4 · Phase 4 — Org-registry install + control-ui management--- **Org-scoped registry** `entry_type: 'llm-hook'` + `hook_meta` (§8.5); the **`install-hook` saga** in- control-api (digest verify → credential Secret → image-allowlist preflight → create CR → rollback).- Installed hooks come **only** from this catalog — no private BYO-image hooks.-- **control-ui — a hook-management dashboard** (mirrors the mcp-server experience; all admin-RBAC-gated- via control-api):- - **Cluster-wide view** of all in-cluster hooks + live **pod status** from `LlmHook.status`- (ready/running/failed), like the mcp-server state view.- - **Per-mcp-host view** — see, add, and remove that Host's hooks; **add** = enable a **built-in** (→- `guardrails.builtins`) or **install an org-registry hook** (→ `install-hook` saga → `LlmHook` CR +- `guardrails.hooks`).- - **Uninstall** an in-cluster hook (delete the `LlmHook` CR; HCC GCs the pod when its last referrer- is gone).- - **Per-hook pod status** detail (health/ready), like mcp-server.-- **Prereq:** registry schema extension (§13.0).-- **Ships:** admins install org-vetted hooks and manage/monitor them from the UI.-- **Services:** control-api (install-hook saga + status read), control-ui, evenfire-registry, HCC (status).--### 13.5 · Sequencing--Phases are ordered **1 → 2 → 3 → 4**, each building on the prior and independently shippable/testable.-Phases 1 and 2 are both in-process mcp-host work and may partially overlap; Phase 3 introduces the-CRD/reconciler/namespace (the largest step); Phase 4 adds the org-registry install path + the control-ui-management dashboard. Prerequisites gate their phases: the `llm-hooks` namespace + image-policy before-Phase 3, the registry schema before Phase 4.diff --git a/docs/features/mcp-host-guardrails/phase-4-hcc-reconciler-design.md b/docs/features/mcp-host-guardrails/phase-4-hcc-reconciler-design.mddeleted file mode 100644index 6457296b7..000000000--- a/docs/features/mcp-host-guardrails/phase-4-hcc-reconciler-design.md+++ /dev/null@@ -1,271 +0,0 @@-# Phase 4 — host-context-controller `LlmHook` reconciler — Implementation Design--| | |-|---|---|-| **Type** | implementation design (component-scoped; implementable) |-| **Status** | draft for review — no code authorized yet |-| **Scope** | `host-context-controller` only. Consumes the `LlmHook` CRD (§8.2) and `Host.spec.guardrails` (§5), both already landed (bucket B slice A). |-| **Parent** | [`mcp-host-guardrails-spec.md`](./mcp-host-guardrails-spec.md) §8.2, §8.5, §12.1 |-| **Depends on** | slice A (CRD) ✅. Independent of slice D (control-api install saga) and the external registry schema — this reconciler acts on `LlmHook` CRs regardless of who wrote them. |-------## 0 · Executive summary--host-context-controller (HCC) already reconciles several `clerum.io` CRDs into workloads with a hand-rolled-`@kubernetes/client-node` watch model — each CRD kind has its own reconciler class, watch loop, and status-write-back (`McpServerReconciler`, `HostReconciler`, `NetworkPolicyReconciler`, … constructed in-`McpServerWatcher`, `src/k8sClient.ts:622-701`). This design adds an `LlmHookReconciler` that turns an-`image`-target `LlmHook` into a `Deployment + Service + NetworkPolicy` in the `llm-hooks` namespace, and-writes `LlmHook.status`. `service` and `remote` targets deploy **nothing** (status-only).--**The one hard divergence from every existing reconciler: digest-dedup makes this N-CRs → 1-workload.**-Multiple `image` hooks that share a **pod key** (image digest + port + envSecret + egressBindings + security)-are **co-located on a single pod** and addressed by distinct `spec.path`. Every existing reconciler is-1-CR → 1-workload, GC'd by a single controlling `ownerReference`. That model **cannot** express a shared pod-(K8s allows one controlling owner; deleting that CR would cascade-delete a pod other hooks still use). So the-three mechanisms below replace the owner-ref pattern: **reference-counted, label-owned workloads**,-**per-pod-key serialization**, and a **Host→LlmHook reverse index** for the NetworkPolicy ingress set.--Mirror, don't reinvent: reuse `McpServerReconciler`'s hardened pod securityContext-(`hardenedContainerSecurityContext`, `src/reconciler.ts:934`), `@clerum/image-policy`,-`@clerum/workflow-recipe-capability-policy`, the create-then-409-`replaceWithConflictRetry` apply helpers-(`src/utils.ts`), the ownership-verifying delete helpers, and the status-subresource JSON-Patch writer-(`patchNamespacedCustomObjectStatus`, `src/reconciler.ts:1750`). The `NetworkPolicyReconciler` L0–L3 layering-(`src/networkPolicyReconciler.ts`) is the template for the hook pod's default-deny + infra + ingress + egress.-------## 1 · The pod-key model (digest-dedup)--### 1.1 · Definition--The **pod key** is a stable hash over the *pod-level* fields of an `image` target — the fields that must be-identical for two hooks to safely share a pod:--```-podKey = sha256short(canonicalJSON({- imageRef: spec.target.image.ref, // digest-pinned (§8.5)- port: spec.target.image.port,- envSecret: spec.target.image.envSecret ?? '',- egressBindings: normalize(spec.target.image.egressBindings ?? []), // sorted, canonicalized- addCapabilities: sort(spec.target.image.security?.addCapabilities ?? []),-}))-```--`spec.path`, `spec.lifecyclePoints`, `spec.order`, `spec.capabilities`, `spec.failMode`, `spec.config`,-`spec.onUnavailable`, and `spec.timeoutMs` are **NOT** in the pod key — they are per-hook routing/policy that-mcp-host applies, not pod-shaping. Two hooks with the same pod key but different paths co-locate; the same-image with a *different* envSecret or egressBindings gets its **own** pod key → its own pod (a different-trust/egress boundary, §12.1 — egress is never unioned across unrelated hooks).--Workload resources are named from the pod key, not the CR: `llmhook-<podKey>` for the Deployment/Service, so-the name is stable across the set of co-located hooks. Labels:-`clerum.io/managed-by=host-context-controller`, `clerum.io/hook-pod-key=<podKey>`, `app=llmhook-<podKey>`.--### 1.2 · Path uniqueness within a pod key--Co-located hooks are routed by `spec.path`; a duplicate `path` across two hooks sharing a pod key is a-**config error** the apiserver cannot catch (CEL can't validate cross-object). At reconcile, before applying-the workload, the reconciler collects all `LlmHook`s with the same pod key and checks path uniqueness. On a-collision it sets the offending CR(s) to a `Ready=False, reason=DuplicatePath` condition and does **not**-route the loser (fail-closed for that hook), while still serving the others. No existing reconciler does-cross-object validation — this is new.-------## 2 · Reconcile flow (per `LlmHook` CR)--`reconcile(hook)` — entry point, serialized (see §4). Mirrors `McpServerReconciler.performReconcile`-(`src/reconciler.ts:1807`):--1. **Classify target.** `service` / `remote` → **status-only**: write `Ready=True, reason=NoWorkload`- (nothing to deploy; mcp-host dials the Service/remote directly). Return. (Contrast McpServer `remote`,- which deploys an nginx proxy — hooks do **not**.)-2. **image target** → compute `podKey`. Load the full co-located set (all `LlmHook`s in `llm-hooks` with the- same pod key) from the in-memory cache.-3. **Validate:** path uniqueness (§1.2); `security.addCapabilities ⊆` the workflow-recipe capability- allowlist; `envSecret` (if set) exists in `llm-hooks` (mirror `validateSecret`, `src/reconciler.ts:436`,- which also yields the credentials-revision digest that forces rolling restarts on secret rotation).-4. **Ensure workload for the pod key** (idempotent; see §3 for who "owns" it):- - `buildDeployment(podKey, set)` — mirror `buildDeployment` (`:812`): hardened securityContext,- `automountServiceAccountToken:false`, image `ref`, container port, `envSecret` mount, the- credentials-revision annotation on the pod template. One container (the shared image); routing across- co-tenant paths is mcp-host's concern, not the pod's.- - `buildService(podKey)` — ClusterIP, mirror `buildService` (`:1161`).- - `buildNetworkPolicy(podKey)` — see §5.- - Apply via the existing create-then-409 `replaceWithConflictRetry` helpers (`ensureDeployment`/- `ensureService`, `:1209`/`:1253`) with the preserve-assigned-fields merges.-5. **Readiness + status.** Poll rollout (`readDeploymentRollout`/`pollReadiness`, `:255`/`:310`), extract- `observedDigest` (§6), then write each member CR's status (§6): `Ready`, `readyReplicas`,- `observedDigest`, `lastReconciled`.--`reconcileDelete(name)` — mirror `reconcileDelete` (`:2080`) but drive the **reference-counted** GC (§3): the-workload is torn down only when the *last* hook for its pod key is gone.-------## 3 · Reference-counted, label-owned GC (replaces owner-ref cascade)--**Problem.** A single controlling `ownerReference` from one `LlmHook` to a shared Deployment is wrong: K8s-permits one controller owner, and deleting that CR would garbage-collect a pod the other co-tenant hooks-still need.--**Design.** HCC owns the shared workload itself and reference-counts by pod key:--- **No controlling owner-ref.** The Deployment/Service/NetworkPolicy carry the `clerum.io/hook-pod-key` label- and `clerum.io/managed-by=host-context-controller`, but **no** `ownerReferences` (or at most- non-controlling refs to every member for observability — non-controlling refs do not cascade-delete).- Ownership for delete decisions is established by the labels, re-read and verified before any delete (mirror- `deleteDeploymentIfHccOwned`, `:1291-1416`).-- **Reference count = live members.** On every reconcile (add/update/delete) the reconciler derives the- current member set for the pod key from the cache: `members(podKey) = { LlmHooks with that pod key }`.- - `members.length > 0` → ensure the workload exists and its NetworkPolicy ingress reflects the current- referencing Hosts (§5).- - `members.length === 0` → delete the Deployment+Service+NetworkPolicy for that pod key (verify HCC- ownership by label first).-- **Trigger on delete.** When an `LlmHook` is deleted, compute its (former) pod key from the cached object- before eviction, drop it from the cache, then run the pod-key reconcile which now sees a smaller (possibly- empty) member set. This is the same "recompute from cache after the event" shape the McpServer watch- callback already uses (`getMcpServerWatchCallback`, `src/k8sClient.ts:2284`).-- **Startup sweep / orphan GC.** On `fullReconcile` (and a periodic resync timer), list all `llmhook-*`- Deployments by label and delete any whose `hook-pod-key` has zero live members — covers pods orphaned by a- missed delete event or a crash between CR-delete and workload-delete. (Existing reconcilers rely on- owner-ref cascade for this; we cannot, so the sweep is mandatory, not optional.)--**Finalizer (decision — §9 #1):** default to **no CR finalizer**, using the recompute-and-sweep model above,-because a finalizer on a shared resource complicates the "last one out" logic and can wedge deletes if HCC is-down. The startup sweep is the backstop. Revisit if we observe orphan pods surviving a delete storm.-------## 4 · Concurrency — serialize by pod key, not CR name--`McpServerReconciler.reconcile` (`:1793`) serializes by **CR name** via an `inFlight: Map<name, Promise>`-tail-chain. That is unsafe here: two different `LlmHook`s mutating the **same pod key** race on the same-Deployment/Service/NetworkPolicy (409/replace storms, lost path routes, ingress flapping).--**Design.** Key the in-flight serialization by **pod key** for image targets (`inFlight: Map<podKey, Promise>`),-so all work touching one shared workload runs strictly sequentially, while different pod keys run-concurrently. `service`/`remote` targets have no workload and can serialize by CR name (or skip). A single-`LlmHook` update that *changes* its pod key (e.g. image bump) must chain on **both** the old and new pod keys-(tear-down of the old, ensure of the new) to avoid interleaving.-------## 5 · NetworkPolicy — Host reverse-index ingress + per-hook egress--Reuse the `NetworkPolicyReconciler` layering (`src/networkPolicyReconciler.ts:5-14`):--- **L0/L1 baseline for `llm-hooks`.** `ensureDefaultPolicies` (`:194`) currently runs per known runtime- namespace; add `llm-hooks` so hook pods get default-deny ingress+egress + infra egress (DNS, K8s API, HCC- API). Without this, hook pods get no DNS/egress baseline. (§7 config plumbing.)-- **Ingress (the reverse index).** The shared pod's ingress must admit **exactly** the mcp-hosts whose `Host`- CR references any co-located hook — i.e. `∪ over members m of { Hosts H : H.spec.guardrails.hooks[*][*].id- == m.name }`. This mirrors the L2 "ingress admits exactly the referencing namespaces/pods" builder- (`reconcileContext`, `:435`, `namespaceSelector`+`podSelector` at `:485-533`), but the referencing set- comes from `Host.spec.guardrails.hooks` instead of `Context.spec.mcpServers`.- - Requires a **Host→LlmHook reverse index**: on every `Host` change, recompute which hook pod keys the Host- references and re-reconcile those pods' NetworkPolicies. Add a fan-out from the Host watch into the hook- reconciler, analogous to how CC counts fan into `HostReconciler` (`src/k8sClient.ts:667`). `Host` is- already watched (`HostReconciler`); extend its callback to also trigger hook-NP reconciles.-- **Egress.** Per the hook's own `egressBindings` (L3, `reconcileExternalEgress`, `:732`, with the SSRF- blocklist `PUBLIC_EGRESS_EXCEPT_CIDRS`, `:51`). Because egress is part of the pod key, all co-tenants share- one egress boundary by construction — egress is never unioned across unrelated hooks (§12.1). **Caveat**- carried from slice A: `LlmHook.egressBindings` is currently looser than `McpServer.egressBindings` (no- private-range CEL); the reconciler must apply the same SSRF/private-range validation the NP reconciler- already enforces for McpServer egress, and set `Ready=False, reason=InvalidEgress` on a rejected binding.-------## 6 · `observedDigest` extraction + status write-back--- **Status shape (slice A decision):** K8s `conditions[]` (like McpServer/Host) **plus** flat- `observedDigest`, `readyReplicas`, `lastReconciled`. Write via the status subresource JSON-Patch helper- (`writeStatusCondition`/`updateStatusConditions`/`patchNamespacedCustomObjectStatus`, `:1642/:1585/:1750`)- with the `resourceVersion` `test` op + retry-on-409/422. **Every member CR of a shared pod gets its own- status** (they share a pod but are distinct CRs).-- **`observedDigest` (new capability).** Existing readiness reads only replica counts. To report the digest- *actually running*, read the pod's container `image` (resolved digest) for the pod-key Deployment — needs- `pods` get/list RBAC in `llm-hooks` (the McpServer Role does **not** grant pods; the workflow-recipes Role- does — mirror that). mcp-host binds `Host.spec.guardrails.hooks[].digest == status.observedDigest` at load- and fails-closed (`may_deny`) / skips-with-alert (advisory) on mismatch (§8.2) — so a wrong/empty- `observedDigest` is a safety signal, not cosmetic.-- **Condition vocabulary (proposed):** `Ready` (True when rollout complete + path unique + secret resolved),- with `reason ∈ { NoWorkload, Deploying, Ready, DuplicatePath, SecretNotFound, InvalidEgress,- DigestMismatch, ReconcileError }`. Keep it a bounded enum (§9 metrics discipline).-------## 7 · Wiring, config, RBAC, namespace baseline--1. **Types** — `LlmHookCRD`/`LlmHookSpec`/`LlmHookStatus` in `src/types.ts` (analogous to `McpServerCRD`).-2. **Reconciler** — `src/llmHookReconciler.ts` (`LlmHookReconciler`), constructed in `McpServerWatcher`- (`src/k8sClient.ts:622-701`).-3. **Watch** — add `PLURAL_LLMHOOKS='llmhooks'`, `getLlmHookWatchCallback()` + `startLlmHookWatch()`- (mirror `:2284`/`:2386`), a `llmHooks: Map` cache + abort handle, start in `start()` next to- `startMcpServerWatch` (`:1788`), `stop()` cleanup, and a periodic resync timer (feeds the §3 sweep).- **Watch namespace = `llm-hooks`** (new `config` entry, `src/config.ts`, alongside `namespace`/- `hostNamespace`/`channelsNamespace`). Extend the `Host` watch callback to fan out hook-NP reconciles (§5).-4. **Constants** — `LLMHOOK_LABEL='clerum.io/llmhook'`, `HOOK_PODKEY_LABEL='clerum.io/hook-pod-key'`- (`src/constants.ts`).-5. **RBAC** — new `deploy/base/llm-hooks/rbac.yaml` (namespace-scoped `Role`+`RoleBinding` binding the HCC- ServiceAccount into `llm-hooks`), modeled on `deploy/base/mcp-server/rbac.yaml:75-128`, granting in- `llm-hooks`: `llmhooks` [get,list,watch,patch], `llmhooks/status` [get,patch], `deployments` (apps),- `services`/`configmaps`/`secrets`, `networkpolicies` (networking.k8s.io), **and `pods` [get,list]** (for- `observedDigest`). Wire into the kustomization; add a sibling assertion to `src/rbacManifest.test.ts`- (which reads `deploy/base/mcp-server/rbac.yaml`).-6. **Namespace baseline** — ensure `ensureDefaultPolicies` covers `llm-hooks` (§5).-------## 8 · Failure modes / edge cases--| Case | Behavior |-|---|---|-| Duplicate `path` in a pod key | Loser(s) `Ready=False reason=DuplicatePath`, not routed; others served. |-| `envSecret` missing | `Ready=False reason=SecretNotFound`; no rollout (mirror McpServer secret gate). |-| Image bump (pod key changes) | Chain on old+new pod keys (§4): tear down old workload if it now has 0 members; ensure new. |-| Last hook for a pod key deleted | Reference count → 0 → delete workload (label-ownership verified). |-| Missed delete event / crash | Startup + periodic sweep deletes label-orphaned `llmhook-*` workloads with 0 members. |-| Host reference added/removed | Host-watch fan-out re-reconciles the affected pod keys' NetworkPolicy ingress. |-| Invalid egress binding | `Ready=False reason=InvalidEgress`; workload not exposed to that egress. |-| `service`/`remote` target | Status-only `Ready=True reason=NoWorkload`; nothing deployed. |-------## 9 · Open decisions / risks--1. **Finalizer vs sweep** — proposed **no finalizer**, rely on recompute + startup/periodic sweep (§3).- Simpler and crash-safe; the risk is a longer orphan window on a missed event (bounded by the resync- interval). Decide the resync interval (proposed 5 min, matching existing resyncs).-2. **Non-controlling owner-refs to all members?** — optional, for `kubectl`-visible ownership. They don't- cascade-delete, so they're safe, but they churn on every membership change. Proposed: **labels only**,- skip owner-refs, to avoid ref churn. Decide.-3. **`observedDigest` source** — read from the Deployment's pod template image (fast, but reflects *intended*- digest) vs a live pod's container status (reflects *running* digest, needs `pods` RBAC + handles- mid-rollout). Proposed: **live pod** for true "actually running" semantics (§8.2 binds on it).-4. **Egress hardening** — the reconciler must apply McpServer-grade SSRF/private-range validation to the- looser `LlmHook.egressBindings` (slice A gap). Confirm we reuse the NP reconciler's existing validators- rather than trusting the CRD.-5. **Cross-namespace `service` target** — a `service` target names an arbitrary `{namespace}`; mcp-host dials- it directly. HCC deploys nothing but should it validate the Service exists / is in an allowed namespace?- Proposed: status-only, no validation in v1 (admin-authored CRs); flag if we want an allowlist.-------## 10 · Test plan (vitest, mirroring existing reconciler tests)--- **Pod-key hashing** — same pod-level fields → same key; differing envSecret/egress/caps → different key;- path/order/capabilities do NOT affect the key.-- **Dedup** — two hooks same pod key + distinct paths → one Deployment/Service; distinct pod keys → two.-- **Path collision** — two hooks same pod key + same path → loser `DuplicatePath`, winner served.-- **Reference-counted GC** — delete one of two co-tenants → workload stays; delete the last → workload- deleted; sweep removes a label-orphan with 0 members.-- **Concurrency** — interleaved reconciles of two CRs sharing a pod key serialize (no double-create); image- bump chains old+new keys.-- **NetworkPolicy ingress** — reflects exactly the referencing Hosts; Host add/remove re-reconciles;- invalid egress → `InvalidEgress`, not exposed.-- **Status** — `conditions[]` + `observedDigest`/`readyReplicas`/`lastReconciled` written per member;- `service`/`remote` → `NoWorkload`.-- **RBAC manifest** — assertion test for the new `llm-hooks` Role (incl. `pods` get/list).