Skip to content

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

TypeScript

fix(llm-hooks): skip no-op Deployment and NetworkPolicy replaces

Alfredo Lopez173a813b38e3
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index 1e6e9ab15..3f201c337 100644
--- a/host-context-controller/package-lock.json
+++ b/host-context-controller/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-host-context-controller",
- "version": "0.1.278",
+ "version": "0.1.279",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.278",
+ "version": "0.1.279",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@clerum/codex-catalog-projection": "file:../packages/codex-catalog-projection",
diff --git a/host-context-controller/package.json b/host-context-controller/package.json
index 75f0d8c32..a0b974a32 100644
--- a/host-context-controller/package.json
+++ b/host-context-controller/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-host-context-controller",
- "version": "0.1.278",
+ "version": "0.1.279",
"description": "Host Context Controller - provides McpServer CRDs to mcp-host via REST API",
"main": "dist/main.js",
"engines": {
diff --git a/host-context-controller/src/__tests__/deploymentWriterInventory.test.ts b/host-context-controller/src/__tests__/deploymentWriterInventory.test.ts
new file mode 100644
index 000000000..a3e1b8e46
--- /dev/null
+++ b/host-context-controller/src/__tests__/deploymentWriterInventory.test.ts
@@ -0,0 +1,70 @@
+import { describe, expect, it } from 'vitest'
+import { readFileSync, readdirSync, statSync } from 'node:fs'
+import { join, relative } from 'node:path'
+
+const SRC_ROOT = join(__dirname, '..')
+
+const GATED = new Set([
+ 'hostReconciler.ts',
+ 'reconciler.ts',
+ 'sharedFileSystemReconciler.ts',
+ 'llmHookReconciler.ts',
+])
+
+const EXEMPT = new Set(['k8s/gfsK8sApi.ts', 'statelessLifecycleExecutor.ts'])
+
+const CALL = /replaceNamespacedDeployment\s*\(\s*\{/g
+
+function productionTsFiles(dir: string): string[] {
+ const out: string[] = []
+ for (const entry of readdirSync(dir)) {
+ if (entry === '__tests__' || entry.endsWith('.test.ts')) continue
+ const full = join(dir, entry)
+ if (statSync(full).isDirectory()) {
+ out.push(...productionTsFiles(full))
+ continue
+ }
+ if (entry.endsWith('.ts')) out.push(full)
+ }
+ return out
+}
+
+describe('Deployment writer inventory', () => {
+ it('every replaceNamespacedDeployment call is gated or explicitly exempt', () => {
+ const hits: Array<{ rel: string; count: number; gated: boolean }> = []
+ for (const file of productionTsFiles(SRC_ROOT)) {
+ const rel = relative(SRC_ROOT, file).replaceAll('\\', '/')
+ const source = readFileSync(file, 'utf8')
+ const count = source.match(CALL)?.length ?? 0
+ if (count === 0) continue
+ const isGated = GATED.has(rel)
+ const isExempt = EXEMPT.has(rel)
+ expect(
+ isGated || isExempt,
+ `${rel} calls replaceNamespacedDeployment ${count} time(s) but is on neither the gated nor the exempt list`
+ ).toBe(true)
+ if (isGated) {
+ expect(
+ source.includes('deploymentMatchesDesired'),
+ `${rel} is gated but does not reference deploymentMatchesDesired`
+ ).toBe(true)
+ }
+ if (isExempt) {
+ expect(
+ source.includes('isUpToDate: deploymentMatchesDesired'),
+ `${rel} is exempt and must not wire the Deployment no-op gate`
+ ).toBe(false)
+ }
+ hits.push({ rel, count, gated: isGated })
+ }
+
+ expect(hits.map(h => h.rel).sort()).toEqual([...GATED, ...EXEMPT].sort())
+ expect(hits.reduce((sum, h) => sum + h.count, 0)).toBe(8)
+ expect(
+ hits
+ .filter(h => h.gated)
+ .map(h => h.rel)
+ .sort()
+ ).toEqual([...GATED].sort())
+ })
+})
diff --git a/host-context-controller/src/__tests__/llmHookDeploymentNoopGate.test.ts b/host-context-controller/src/__tests__/llmHookDeploymentNoopGate.test.ts
new file mode 100644
index 000000000..1f5046d40
--- /dev/null
+++ b/host-context-controller/src/__tests__/llmHookDeploymentNoopGate.test.ts
@@ -0,0 +1,157 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import type * as k8s from '@kubernetes/client-node'
+import {
+ type MockAppsApi,
+ asAppsApi,
+ asCoreApi,
+ asCustomApi,
+ asNetworkingApi,
+ createMockAppsApi,
+ createMockCoreApi,
+ createMockCustomApi,
+ createMockNetworkingApi,
+} from '../../test/__fixtures__/testMocks'
+import { updatedLogs } from '../../test/__fixtures__/updatedLogs'
+import { LlmHookReconciler, computePodKey } from '../llmHookReconciler'
+import type { HostCRD, LlmHookCRD } from '../types'
+import { deploymentMatchesDesired, preserveDeploymentAnnotations } from '../utils'
+import { asApiserverDeployment } from './asApiserverDeployment'
+import { cloneAndMutateLeaf, collectLeafPaths, formatLeafPath } from './mutateJsonLeaves'
+
+vi.mock('../config', () => ({
+ config: {
+ llmHooksNamespace: 'llm-hooks',
+ hostNamespace: 'mcp-host',
+ mcpServerImagePullPolicy: 'IfNotPresent',
+ },
+}))
+
+const IMG = 'registry.example.com/hook@sha256:' + 'a'.repeat(64)
+const CREDENTIALS_REVISION_ANNOTATION = 'clerum.io/credentials-revision'
+
+function makeHook(): LlmHookCRD {
+ return {
+ name: 'pre-hook',
+ namespace: 'llm-hooks',
+ generation: 1,
+ spec: {
+ target: { image: { ref: IMG, port: 8080 } },
+ path: '/',
+ lifecyclePoints: ['preCall'],
+ },
+ }
+}
+
+function mergedForCompare(desired: k8s.V1Deployment, existing: k8s.V1Deployment): k8s.V1Deployment {
+ return preserveDeploymentAnnotations(
+ {
+ ...desired,
+ metadata: { ...desired.metadata, resourceVersion: existing.metadata?.resourceVersion },
+ },
+ existing
+ )
+}
+
+describe('LlmHook ensureDeployment no-op gate', () => {
+ let appsApi: MockAppsApi
+ let reconciler: LlmHookReconciler
+ const hook = makeHook()
+ const podKey = computePodKey(hook)!
+ const members = [hook]
+
+ beforeEach(() => {
+ appsApi = createMockAppsApi()
+ reconciler = new LlmHookReconciler(
+ {} as k8s.KubeConfig,
+ new Map<string, LlmHookCRD>(),
+ new Map<string, HostCRD>(),
+ {
+ appsApi: asAppsApi(appsApi),
+ coreApi: asCoreApi(createMockCoreApi()),
+ customApi: asCustomApi(createMockCustomApi()),
+ networkingApi: asNetworkingApi(createMockNetworkingApi()),
+ }
+ )
+ appsApi.createNamespacedDeployment.mockRejectedValue({ code: 409 })
+ })
+
+ it('FIXTURE-1: LlmHook builder output differs from the default-filled live object', () => {
+ const desired = (reconciler as any).buildDeployment(
+ podKey,
+ members,
+ 'rev-1'
+ ) as k8s.V1Deployment
+ expect(asApiserverDeployment(desired)).not.toEqual(desired)
+ })
+
+ it('predicate: merged builder vs fixture is a no-op', () => {
+ const desired = (reconciler as any).buildDeployment(
+ podKey,
+ members,
+ 'rev-1'
+ ) as k8s.V1Deployment
+ const existing = asApiserverDeployment(desired)
+ expect(deploymentMatchesDesired(mergedForCompare(desired, existing), existing)).toBe(true)
+ })
+
+ it('LlmHook mutation sweep: every desired leaf is detectable', () => {
+ const desired = (reconciler as any).buildDeployment(
+ podKey,
+ members,
+ 'rev-1'
+ ) as k8s.V1Deployment
+ const existing = asApiserverDeployment(desired)
+ const undetectable: string[] = []
+ for (const path of collectLeafPaths(desired)) {
+ const mutated = cloneAndMutateLeaf(desired, path) as k8s.V1Deployment
+ if (deploymentMatchesDesired(mergedForCompare(mutated, existing), existing)) {
+ undetectable.push(formatLeafPath(path))
+ }
+ }
+ expect(undetectable, `undetectable leaf path(s): ${undetectable.join(', ')}`).toEqual([])
+ })
+
+ it('NOOP-LLMDEP-1: equivalent Deployment skips replace and Updated logs', async () => {
+ const desired = (reconciler as any).buildDeployment(
+ podKey,
+ members,
+ 'rev-1'
+ ) as k8s.V1Deployment
+ appsApi.readNamespacedDeployment.mockResolvedValue(asApiserverDeployment(desired))
+ const log = vi.spyOn(console, 'log')
+ try {
+ await (reconciler as any).ensureDeployment(podKey, members, 'rev-1')
+ expect(appsApi.replaceNamespacedDeployment).not.toHaveBeenCalled()
+ expect(updatedLogs(log, 'Updated', 'Deployment')).toEqual([])
+ } finally {
+ log.mockRestore()
+ }
+ })
+
+ it('ROTATE-LLMDEP-1: credentials revision change replaces once', async () => {
+ const live = (reconciler as any).buildDeployment(podKey, members, 'rev-old') as k8s.V1Deployment
+ appsApi.readNamespacedDeployment.mockResolvedValue(asApiserverDeployment(live))
+ await (reconciler as any).ensureDeployment(podKey, members, 'rev-new')
+ expect(appsApi.replaceNamespacedDeployment).toHaveBeenCalledOnce()
+ const body = (
+ appsApi.replaceNamespacedDeployment.mock.calls[0][0] as { body: k8s.V1Deployment }
+ ).body
+ expect(body.spec?.template?.metadata?.annotations?.[CREDENTIALS_REVISION_ANNOTATION]).toBe(
+ 'rev-new'
+ )
+ })
+
+ it('IMAGE-LLMDEP-1: image.ref bump replaces once', async () => {
+ const live = (reconciler as any).buildDeployment(podKey, members, 'rev-1') as k8s.V1Deployment
+ appsApi.readNamespacedDeployment.mockResolvedValue(asApiserverDeployment(live))
+ const bumped = makeHook()
+ bumped.spec.target.image!.ref = 'registry.example.com/hook@sha256:' + 'b'.repeat(64)
+ const newKey = computePodKey(bumped)!
+ await (reconciler as any).ensureDeployment(newKey, [bumped], 'rev-1')
+ expect(appsApi.replaceNamespacedDeployment).toHaveBeenCalledOnce()
+ const body = (
+ appsApi.replaceNamespacedDeployment.mock.calls[0][0] as { body: k8s.V1Deployment }
+ ).body
+ expect(body.spec?.template?.spec?.containers?.[0]?.image).toBe(bumped.spec.target.image!.ref)
+ })
+})
diff --git a/host-context-controller/src/__tests__/llmHookNetworkPolicyNoopGate.test.ts b/host-context-controller/src/__tests__/llmHookNetworkPolicyNoopGate.test.ts
new file mode 100644
index 000000000..6ffe58615
--- /dev/null
+++ b/host-context-controller/src/__tests__/llmHookNetworkPolicyNoopGate.test.ts
@@ -0,0 +1,197 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import type * as k8s from '@kubernetes/client-node'
+import {
+ type MockCoreApi,
+ type MockNetworkingApi,
+ asAppsApi,
+ asCoreApi,
+ asCustomApi,
+ asNetworkingApi,
+ createMockAppsApi,
+ createMockCoreApi,
+ createMockCustomApi,
+ createMockNetworkingApi,
+} from '../../test/__fixtures__/testMocks'
+import { updatedLogs } from '../../test/__fixtures__/updatedLogs'
+import {
+ HOST_LABEL,
+ LLMHOOK_LABEL,
+ MANAGED_BY_LABEL,
+ MANAGED_BY_VALUE,
+ POLICY_TYPE_LABEL,
+} from '../constants'
+import { LlmHookReconciler, computePodKey, serviceTargetNpName } from '../llmHookReconciler'
+import type { HostCRD, LlmHookCRD } from '../types'
+import { asApiserverNetworkPolicy, updatedPolicyLogs } from './asApiserverNetworkPolicy'
+
+vi.mock('../config', () => ({
+ config: {
+ llmHooksNamespace: 'llm-hooks',
+ hostNamespace: 'mcp-host',
+ mcpServerImagePullPolicy: 'IfNotPresent',
+ },
+}))
+
+const IMG = 'registry.example.com/hook@sha256:' + 'a'.repeat(64)
+
+function makeImageHook(): LlmHookCRD {
+ return {
+ name: 'pre-hook',
+ namespace: 'llm-hooks',
+ generation: 1,
+ spec: {
+ target: { image: { ref: IMG, port: 8080 } },
+ path: '/',
+ lifecyclePoints: ['preCall'],
+ },
+ }
+}
+
+function makeServiceHook(): LlmHookCRD {
+ return {
+ name: 'svc-hook',
+ namespace: 'llm-hooks',
+ generation: 1,
+ spec: {
+ target: { service: { name: 'hook-svc', namespace: 'llm-hooks', port: 8080 } },
+ path: '/',
+ lifecyclePoints: ['preCall'],
+ },
+ }
+}
+
+function makeHost(hookId: string): HostCRD {
+ return {
+ name: 'chatllm',
+ namespace: 'mcp-host',
+ spec: {
+ host: 'chatllm',
+ contextRef: 'ctx',
+ secretRef: 'secret',
+ guardrails: { hooks: { preCall: [{ id: hookId }] } },
+ },
+ }
+}
+
+describe('LlmHook NetworkPolicy no-op gate', () => {
+ let networkingApi: MockNetworkingApi
+ let coreApi: MockCoreApi
+ let reconciler: LlmHookReconciler
+ let hooks: Map<string, LlmHookCRD>
+ let hosts: Map<string, HostCRD>
+
+ beforeEach(() => {
+ networkingApi = createMockNetworkingApi()
+ coreApi = createMockCoreApi()
+ hooks = new Map()
+ hosts = new Map()
+ reconciler = new LlmHookReconciler({} as k8s.KubeConfig, hooks, hosts, {
+ appsApi: asAppsApi(createMockAppsApi()),
+ coreApi: asCoreApi(coreApi),
+ customApi: asCustomApi(createMockCustomApi()),
+ networkingApi: asNetworkingApi(networkingApi),
+ })
+ networkingApi.createNamespacedNetworkPolicy.mockRejectedValue({ code: 409 })
+ })
+
+ it('NOOP-LLMNP-1: image-hook policy (caller :811) skips replace', async () => {
+ const hook = makeImageHook()
+ const podKey = computePodKey(hook)!
+ const desired = (reconciler as any).buildNetworkPolicy(
+ podKey,
+ [hook],
+ []
+ ) as k8s.V1NetworkPolicy
+ networkingApi.readNamespacedNetworkPolicy.mockResolvedValue(asApiserverNetworkPolicy(desired))
+ const log = vi.spyOn(console, 'log')
+ try {
+ await (reconciler as any).ensureNetworkPolicy(podKey, [hook], [])
+ expect(networkingApi.replaceNamespacedNetworkPolicy).not.toHaveBeenCalled()
+ expect(updatedPolicyLogs(log, `NetworkPolicy "${desired.metadata?.name}"`)).toEqual([])
+ } finally {
+ log.mockRestore()
+ }
+ })
+
+ it('NOOP-LLMNP-2: service-target policy (caller :889) skips replace', async () => {
+ const hook = makeServiceHook()
+ coreApi.readNamespacedService.mockResolvedValue({
+ spec: { selector: { app: 'hook-svc' } },
+ })
+ const desired: k8s.V1NetworkPolicy = {
+ apiVersion: 'networking.k8s.io/v1',
+ kind: 'NetworkPolicy',
+ metadata: {
+ name: serviceTargetNpName(hook.name),
+ namespace: 'llm-hooks',
+ labels: {
+ [MANAGED_BY_LABEL]: MANAGED_BY_VALUE,
+ [POLICY_TYPE_LABEL]: 'service-hook-ingress',
+ [LLMHOOK_LABEL]: hook.name,
+ },
+ },
+ spec: {
+ podSelector: { matchLabels: { app: 'hook-svc' } },
+ policyTypes: ['Ingress'],
+ ingress: [],
+ },
+ }
+ networkingApi.readNamespacedNetworkPolicy.mockResolvedValue(asApiserverNetworkPolicy(desired))
+ await (reconciler as any).ensureServiceTargetNetworkPolicy(hook)
+ expect(networkingApi.replaceNamespacedNetworkPolicy).not.toHaveBeenCalled()
+ })
+
+ it('NOOP-LLMNP-3: host-egress policy (caller :964) skips replace', async () => {
+ const hook = makeImageHook()
+ const host = makeHost(hook.name)
+ hooks.set(hook.name, hook)
+ hosts.set(host.name, host)
+ const rules = (await (reconciler as any).buildHostEgressRules(
+ host
+ )) as k8s.V1NetworkPolicyEgressRule[]
+ expect(rules.length).toBeGreaterThan(0)
+ const desired: k8s.V1NetworkPolicy = {
+ apiVersion: 'networking.k8s.io/v1',
+ kind: 'NetworkPolicy',
+ metadata: {
+ name: `mcp-host-${host.name}-egress-llm-hooks`,
+ namespace: 'mcp-host',
+ labels: {
+ [MANAGED_BY_LABEL]: MANAGED_BY_VALUE,
+ [HOST_LABEL]: host.name,
+ [POLICY_TYPE_LABEL]: 'llm-hooks-egress',
+ },
+ },
+ spec: {
+ podSelector: {
+ matchLabels: { [HOST_LABEL]: host.name, [MANAGED_BY_LABEL]: MANAGED_BY_VALUE },
+ },
+ policyTypes: ['Egress'],
+ egress: rules,
+ },
+ }
+ networkingApi.readNamespacedNetworkPolicy.mockResolvedValue(asApiserverNetworkPolicy(desired))
+ await (reconciler as any).ensureHostEgressNetworkPolicy(host)
+ expect(networkingApi.replaceNamespacedNetworkPolicy).not.toHaveBeenCalled()
+ })
+
+ it('WRITE-LLMNP-1: added egress rule replaces once', async () => {
+ const hook = makeImageHook()
+ const podKey = computePodKey(hook)!
+ const empty = (reconciler as any).buildNetworkPolicy(podKey, [hook], []) as k8s.V1NetworkPolicy
+ networkingApi.readNamespacedNetworkPolicy.mockResolvedValue(asApiserverNetworkPolicy(empty))
+ const extra: k8s.V1NetworkPolicyEgressRule[] = [
+ { to: [{ ipBlock: { cidr: '1.2.3.4/32' } }], ports: [{ port: 443, protocol: 'TCP' }] },
+ ]
+ const log = vi.spyOn(console, 'log')
+ try {
+ await (reconciler as any).ensureNetworkPolicy(podKey, [hook], extra)
+ expect(networkingApi.replaceNamespacedNetworkPolicy).toHaveBeenCalledOnce()
+ expect(updatedLogs(log, 'Updated', 'NetworkPolicy')).toEqual([
+ `[LlmHook] Updated NetworkPolicy "${empty.metadata?.name}"`,
+ ])
+ } finally {
+ log.mockRestore()
+ }
+ })
+})
diff --git a/host-context-controller/src/llmHookReconciler.ts b/host-context-controller/src/llmHookReconciler.ts
index c9c62f9a4..d6c4848df 100644
--- a/host-context-controller/src/llmHookReconciler.ts
+++ b/host-context-controller/src/llmHookReconciler.ts
@@ -38,7 +38,9 @@ import {
import { isAllowedExternalEgressCidr, isPublicDnsHostname } from './networkPolicyReconciler'
import { HostCRD, LlmHookCRD, LlmHookCondition, LlmHookImageTarget, LlmHookStatus } from './types'
import {
+ deploymentMatchesDesired,
getErrorCode,
+ networkPolicyMatchesDesired,
preserveDeploymentAnnotations,
preserveObjectAnnotations,
preserveServiceAssignedFields,
@@ -767,6 +769,7 @@ export class LlmHookReconciler {
logPrefix: LOG,
body: deployment,
mergeExisting: preserveDeploymentAnnotations,
+ isUpToDate: deploymentMatchesDesired,
read: () =>
this.appsApi.readNamespacedDeployment({ name, namespace: config.llmHooksNamespace }),
replace: body =>
@@ -827,6 +830,7 @@ export class LlmHookReconciler {
logPrefix: LOG,
body: policy,
mergeExisting: preserveObjectAnnotations,
+ isUpToDate: networkPolicyMatchesDesired,
read: () => this.networkingApi.readNamespacedNetworkPolicy({ name, namespace }),
replace: body => this.networkingApi.replaceNamespacedNetworkPolicy({ name, namespace, body }),
})