Skip to content

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

TypeScript

fix(hcc): certify current context safety deltas

Alfredo Lopez8019cc0232c3
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index d2f941fe9..d28f9d006 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.179",
+ "version": "0.1.180",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.179",
+ "version": "0.1.180",
"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.json
index 219f71637..7fc22a607 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.179",
+ "version": "0.1.180",
"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.ts
index e9b94067e..9e45c729a 100644
--- a/host-context-controller/src/k8sClient.test.ts
+++ b/host-context-controller/src/k8sClient.test.ts
@@ -1080,6 +1080,104 @@ describe('McpServerWatcher startup', () => {
}
})
+ it('keeps readiness after a current Context safety delta while an older additive pass is pending', async () => {
+ const olderAdditivePass = deferred()
+ const currentDeltaSafety = deferred()
+ let contextWatchCallback:
+ | ((
+ type: string,
+ apiObj: {
+ metadata: { name: string; namespace: string }
+ spec: { contextId: string; mcpServers: string[] }
+ }
+ ) => Promise<void>)
+ | undefined
+ let networkPolicyPasses = 0
+ mocks.watch.mockImplementationOnce(async (path, _options, callback) => {
+ if (path.endsWith('/contexts')) contextWatchCallback = callback
+ return { abort: vi.fn() }
+ })
+ mocks.netPolFullReconcile.mockImplementation(async (...args: unknown[]) => {
+ networkPolicyPasses += 1
+ const options = args[2] as { onAuthoritativeRevocationComplete?: () => void } | undefined
+ options?.onAuthoritativeRevocationComplete?.()
+ if (networkPolicyPasses === 1) await olderAdditivePass.promise
+ })
+
+ const watcher = new McpServerWatcher()
+ await (watcher as any).startContextWatch('context-delta-rv')
+ ;(watcher as any).contextCacheSynced = true
+ ;(watcher as any).mcpServerCacheSynced = true
+ ;(watcher as any).hostCacheSynced = true
+ ;(watcher as any).mcpWatchGeneration = 1
+ ;(watcher as any).contexts.set('delta-context', {
+ name: 'delta-context',
+ namespace: 'mcp-server',
+ spec: { contextId: 'delta-context', mcpServers: [] },
+ })
+ const server = new ContextMapperServer(watcher, 0, undefined, undefined, () =>
+ watcher.isReadinessInventoryAuthoritative()
+ )
+ await server.start()
+ server.setReady(true)
+ const initialPass = (watcher as any).runInitialNetworkPolicyConvergence() as Promise<void>
+ ;(watcher as any).netPolReconciler.reconcileContext.mockImplementationOnce(
+ () => currentDeltaSafety.promise
+ )
+
+ try {
+ await vi.waitFor(() => expect(mocks.netPolFullReconcile).toHaveBeenCalledOnce())
+ expect((await requestReadyOverHttp(server)).statusCode).toBe(200)
+
+ const currentDelta = contextWatchCallback!('MODIFIED', {
+ metadata: { name: 'delta-context', namespace: 'mcp-server' },
+ spec: { contextId: 'delta-context', mcpServers: ['delta-server'] },
+ })
+
+ await vi.waitFor(() =>
+ expect((watcher as any).netPolReconciler.reconcileContext).toHaveBeenCalledOnce()
+ )
+ expect((await requestReadyOverHttp(server)).statusCode).toBe(503)
+ currentDeltaSafety.resolve(undefined)
+ await currentDelta
+ expect((await requestReadyOverHttp(server)).statusCode).toBe(200)
+ expect(mocks.netPolFullReconcile).toHaveBeenCalledOnce()
+ } finally {
+ olderAdditivePass.resolve(undefined)
+ await initialPass
+ await server.stop()
+ await watcher.stop()
+ }
+ })
+
+ it.each([
+ ['Context', 'contextDesiredRevision'],
+ ['McpServer', 'mcpServerDesiredRevision'],
+ ] as const)(
+ 'does not restore readiness from a stale %s delta safety certificate',
+ async (_kind, revisionField) => {
+ const watcher = new McpServerWatcher()
+ ;(watcher as any).contextCacheSynced = true
+ ;(watcher as any).mcpServerCacheSynced = true
+ ;(watcher as any).hostCacheSynced = true
+ ;(watcher as any).contextWatchGeneration = 4
+ ;(watcher as any).mcpWatchGeneration = 7
+ markNetworkPolicyRevocationAuthoritative(watcher)
+ const certificate = {
+ contextGeneration: (watcher as any).contextWatchGeneration,
+ serverGeneration: (watcher as any).mcpWatchGeneration,
+ contextRevision: (watcher as any).contextDesiredRevision,
+ serverRevision: (watcher as any).mcpServerDesiredRevision,
+ }
+
+ ;(watcher as any)[revisionField] += 1
+
+ expect((watcher as any).recordNetworkPolicySafetyCertificate(certificate)).toBe(false)
+ expect(watcher.isReadinessInventoryAuthoritative()).toBe(false)
+ await watcher.stop()
+ }
+ )
+
it.each([
['SharedFileSystem', 'LIST', 'sharedfilesystems', 'startSharedFileSystemWatch'],
['SharedFileSystem', 'WATCH', 'sharedfilesystems', 'startSharedFileSystemWatch'],
diff --git a/host-context-controller/src/k8sClient.ts b/host-context-controller/src/k8sClient.ts
index b924107bf..5c4b74f9e 100644
--- a/host-context-controller/src/k8sClient.ts
+++ b/host-context-controller/src/k8sClient.ts
@@ -2503,6 +2503,27 @@ export class McpServerWatcher implements McpServerProvider {
}
}
+ private recordNetworkPolicySafetyCertificate(
+ certificate: NetworkPolicySafetyCertificate
+ ): boolean {
+ if (
+ this.stopped ||
+ !this.contextCacheSynced ||
+ !this.mcpServerCacheSynced ||
+ this.contextWatchGeneration !== certificate.contextGeneration ||
+ this.mcpWatchGeneration !== certificate.serverGeneration ||
+ this.contextDesiredRevision !== certificate.contextRevision ||
+ this.mcpServerDesiredRevision !== certificate.serverRevision
+ ) {
+ return false
+ }
+ this.networkPolicyRevocationContextGeneration = certificate.contextGeneration
+ this.networkPolicyRevocationServerGeneration = certificate.serverGeneration
+ this.networkPolicyRevocationContextRevision = certificate.contextRevision
+ this.networkPolicyRevocationServerRevision = certificate.serverRevision
+ return true
+ }
+
private isNetworkPolicySafetyCertificateCurrent(
certificate: NetworkPolicySafetyCertificate
): boolean {
@@ -2611,6 +2632,12 @@ export class McpServerWatcher implements McpServerProvider {
const initialServers = [...this.servers.values()]
const contextInventoryGeneration = this.contextWatchGeneration
const serverInventoryGeneration = this.mcpWatchGeneration
+ const safetyCertificate: NetworkPolicySafetyCertificate = {
+ contextGeneration: contextInventoryGeneration,
+ serverGeneration: serverInventoryGeneration,
+ contextRevision: this.contextDesiredRevision,
+ serverRevision: this.mcpServerDesiredRevision,
+ }
const serverInventoryComplete = this.mcpServerCacheSynced
const contextInventoryAuthoritative = () =>
this.hasContextInventoryAuthority(contextInventoryGeneration)
@@ -2644,11 +2671,7 @@ export class McpServerWatcher implements McpServerProvider {
contextDesiredRevision: () => this.contextDesiredRevision,
serverDesiredRevision: () => this.mcpServerDesiredRevision,
onAuthoritativeRevocationComplete: () => {
- if (!contextInventoryAuthoritative() || !serverInventoryAuthoritative()) return
- this.networkPolicyRevocationContextGeneration = contextInventoryGeneration
- this.networkPolicyRevocationServerGeneration = serverInventoryGeneration
- this.networkPolicyRevocationContextRevision = this.contextDesiredRevision
- this.networkPolicyRevocationServerRevision = this.mcpServerDesiredRevision
+ if (!this.recordNetworkPolicySafetyCertificate(safetyCertificate)) return
authoritativeRevocationCompleted = true
// Start runtime convergence at the safety boundary, before this full
// pass enters additive Context work. Startup egress gates may now
@@ -3562,6 +3585,7 @@ export class McpServerWatcher implements McpServerProvider {
// Cache the context for cross-resource re-reconciliation
let desiredStateChanged = false
+ let deltaSafetyCertificate: NetworkPolicySafetyCertificate | undefined
if (type === 'ADDED' || type === 'MODIFIED') {
desiredStateChanged =
previous === undefined || !sameContextDesiredRevision(previous, context)
@@ -3571,6 +3595,19 @@ export class McpServerWatcher implements McpServerProvider {
}
if (desiredStateChanged) {
this.contextDesiredRevision += 1
+ // A same-identity Context MODIFIED event can complete its own stale
+ // allow revocation below. Once that exact delta is current, it is safe
+ // to restore readiness without waiting for an older pass's additive
+ // fleet. Deletes and identity changes remain fail-closed until the
+ // authoritative full pass certifies their broader cleanup.
+ if (type === 'MODIFIED' && previous?.spec.contextId === context.spec.contextId) {
+ deltaSafetyCertificate = {
+ contextGeneration: watchGeneration,
+ serverGeneration: this.mcpWatchGeneration,
+ contextRevision: this.contextDesiredRevision,
+ serverRevision: this.mcpServerDesiredRevision,
+ }
+ }
void this.runInitialNetworkPolicyConvergence()
}
@@ -3630,6 +3667,10 @@ export class McpServerWatcher implements McpServerProvider {
deleteAllowed
)
}
+
+ if (deltaSafetyCertificate) {
+ this.recordNetworkPolicySafetyCertificate(deltaSafetyCertificate)
+ }
} catch (error) {
console.error(
`[K8s] NetworkPolicy reconciliation failed for context ${context.name}:`,