Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(hcc): null the safety certificate when the delete fence is lost [R3-M1]
Alfredo Lopez77feb69ad657
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.jsonindex 8f2ff7d00..d5d339954 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.211",+ "version": "0.1.212","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-host-context-controller",- "version": "0.1.211",+ "version": "0.1.212","license": "SEE LICENSE IN LICENSE","dependencies": {"@clerum/image-policy": "file:../packages/image-policy",diff --git a/host-context-controller/package.json b/host-context-controller/package.jsonindex 337cf9818..1539613da 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.211",+ "version": "0.1.212","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/k8sClient.test.ts b/host-context-controller/src/k8sClient.test.tsindex e462bae8e..d57bd367a 100644--- a/host-context-controller/src/k8sClient.test.ts+++ b/host-context-controller/src/k8sClient.test.ts@@ -3834,6 +3834,32 @@ describe('McpServerWatcher startup', () => {await watcher.stop()})+ it('does not start runtime fleet convergence while the safety inventory is uncertified', async () => {+ // M1: with the revocation counters aligned, the ONLY thing that can null the+ // certificate is the lost safety fence. Fence lost -> cert null -> the fleet+ // lane must not run; fence restored -> it runs exactly once.+ const watcher = new McpServerWatcher()+ ;(watcher as any).contextCacheSynced = true+ ;(watcher as any).mcpServerCacheSynced = true+ ;(watcher as any).contextWatchGeneration = 4+ ;(watcher as any).mcpWatchGeneration = 7+ markNetworkPolicyRevocationAuthoritative(watcher)+ try {+ mocks.hasCertifiedSafetyInventory.mockReturnValue(false)+ await (watcher as any).runInitialMcpServerConvergence()+ expect(mocks.serverFullReconcile).not.toHaveBeenCalled()++ mocks.hasCertifiedSafetyInventory.mockReturnValue(true)+ await (watcher as any).runInitialMcpServerConvergence()+ expect(mocks.serverFullReconcile).toHaveBeenCalledOnce()+ } finally {+ // vi.clearAllMocks() clears calls, not implementations — restore explicitly+ // or every later test would re-gate on a stale false.+ mocks.hasCertifiedSafetyInventory.mockReturnValue(true)+ await watcher.stop()+ }+ })+it('invalidates readiness when either desired policy revision advances', async () => {const watcher = new McpServerWatcher();(watcher as any).hostCacheSynced = truediff --git a/host-context-controller/src/k8sClient.ts b/host-context-controller/src/k8sClient.tsindex c980b2ec1..3f4795cc3 100644--- a/host-context-controller/src/k8sClient.ts+++ b/host-context-controller/src/k8sClient.ts@@ -2584,6 +2584,7 @@ export class McpServerWatcher implements McpServerProvider {this.stopped ||!this.contextCacheSynced ||!this.mcpServerCacheSynced ||+ !this.netPolReconciler.hasCertifiedSafetyInventory() ||this.networkPolicyRevocationContextGeneration !== this.contextWatchGeneration ||this.networkPolicyRevocationServerGeneration !== this.mcpWatchGeneration ||this.networkPolicyRevocationContextRevision !== this.contextDesiredRevision ||diff --git a/host-context-controller/src/readinessSafetyFence.integration.test.ts b/host-context-controller/src/readinessSafetyFence.integration.test.tsindex 079ad8281..4bd14f5e6 100644--- a/host-context-controller/src/readinessSafetyFence.integration.test.ts+++ b/host-context-controller/src/readinessSafetyFence.integration.test.ts@@ -322,7 +322,7 @@ describe('readiness withdrawal on a real lost delete fence', () => {expect(await readyStatus(server)).toBe(200)})- it('does not let a scoped delta certify readiness while the last authoritative pass left the inventory uncertified', async () => {+ it('short-circuits a scoped delta at the certificate source while the last authoritative pass left the inventory uncertified', async () => {const reconciler = (watcher as unknown as { netPolReconciler: any }).netPolReconcilerconst w = watcher as unknown as Record<string, any>@@ -401,10 +401,104 @@ describe('readiness withdrawal on a real lost delete fence', () => {spec: { contextId: 'alpha', mcpServers: [] },})- // The delta declined to certify: it warned, it recorded no certificate- // (the revocation counters were not advanced to the delta's bumped- // revision), and readiness stays withdrawn even after every counter is- // realigned so the fence is the only remaining gate.+ // M1: the fence was lost before this delta, so+ // currentNetworkPolicySafetyCertificate() returns null at the capture and no+ // delta certificate is ever minted — the delta short-circuits at the source+ // and never reaches the per-delta certification branch. No warn is emitted;+ // nothing is recorded; readiness stays withdrawn via the independent+ // readiness fence even after every counter is realigned.+ expect(+ warnSpy.mock.calls.some(call => String(call[0]).includes('Not certifying the scoped delta'))+ ).toBe(false)+ expect(w.networkPolicyRevocationContextRevision).not.toBe(w.contextDesiredRevision)+ expect(reconciler.hasCertifiedSafetyInventory()).toBe(false)+ alignRevocationCounters(watcher)+ expect(await readyStatus(server)).toBe(503)+ } finally {+ warnSpy.mockRestore()+ }+ })++ it('declines to certify a scoped delta whose certificate outlived a fence lost mid-flight, and warns', async () => {+ // Coverage preservation for the per-delta fence check (k8sClient.ts :3801):+ // the delta certificate is minted while the fence is still certified, then a+ // CONCURRENT authoritative pass loses its delete fence mid-flight. M1's+ // source short-circuit cannot catch this — only the per-delta check can. This+ // test is green with AND without M1; it guards the TOCTOU window, not M1.+ const reconciler = (watcher as unknown as { netPolReconciler: any }).netPolReconciler+ const w = watcher as unknown as Record<string, any>++ await w.startContextWatch('ctx-rv-2')+ const contextWatchCall = mocks.watch.mock.calls.find(([path]) =>+ String(path).endsWith('/namespaces/mcp-server/contexts')+ )+ if (!contextWatchCall) throw new Error('Context watch was never registered')+ const fireContextEvent = contextWatchCall[2] as (type: string, apiObj: unknown) => Promise<void>++ const priorContext = {+ name: 'alpha',+ namespace: 'mcp-server',+ uid: 'ctx-alpha-uid',+ generation: 1,+ spec: { contextId: 'alpha', mcpServers: [] },+ }+ w.contexts.set('alpha', priorContext)+ alignRevocationCounters(watcher)++ // Baseline clean authoritative pass: certified, gate open.+ mocks.listNamespacedNetworkPolicy.mockResolvedValue({ items: [] })+ mocks.deleteNamespacedNetworkPolicy.mockResolvedValue({})+ await reconciler.fullReconcile([priorContext], [], {+ ensureDefaults: false,+ contextInventoryAuthoritative: () => true,+ serverInventoryAuthoritative: () => true,+ onAuthoritativeRevocationComplete: () => {},+ })+ alignRevocationCounters(watcher)+ expect(await readyStatus(server)).toBe(200)++ // The delta certificate is captured now, while the fence is still certified.+ // The FIRST scoped LIST the delta issues runs a concurrent authoritative pass+ // that loses its own delete fence — degrading the fence AFTER the delta minted+ // its certificate but BEFORE the delta reaches its certification branch.+ let fenceBrokenMidFlight = false+ mocks.listNamespacedNetworkPolicy.mockImplementation(+ async ({ namespace, labelSelector }: { namespace?: string; labelSelector?: string }) => {+ const selector = labelSelector ?? ''+ const scoped =+ selector.includes(`${CONTEXT_LABEL}=`) || selector.includes('external-egress')+ if (scoped && !fenceBrokenMidFlight) {+ fenceBrokenMidFlight = true+ mocks.deleteNamespacedNetworkPolicy.mockRejectedValue(+ Object.assign(new Error('the object has been modified'), { code: 409 })+ )+ await reconciler+ .fullReconcile([priorContext], [], {+ ensureDefaults: false,+ contextInventoryAuthoritative: () => true,+ serverInventoryAuthoritative: () => true,+ onAuthoritativeRevocationComplete: () => {},+ })+ .then(+ () => {+ throw new Error('interleaved authoritative pass should have lost its fence')+ },+ () => {}+ )+ }+ return { items: namespace === 'mcp-server' && !scoped ? [stalePolicy] : [] }+ }+ )+ const warnSpy = vi.spyOn(console, 'warn')+ try {+ await fireContextEvent('MODIFIED', {+ metadata: { name: 'alpha', namespace: 'mcp-server', uid: 'ctx-alpha-uid', generation: 2 },+ spec: { contextId: 'alpha', mcpServers: [] },+ })++ // The interleave actually ran (no vacuous pass) and the per-delta check+ // withheld certification: it warned, recorded nothing, readiness stays 503.+ expect(fenceBrokenMidFlight).toBe(true)expect(warnSpy.mock.calls.some(call => String(call[0]).includes('Not certifying the scoped delta'))).toBe(true)