Skip to content

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

TypeScript

fix(hcc): background all initial convergence lanes

Alfredo Lopez8ebb2d8140d9
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index e5333af94..9a3aff834 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.159",
+ "version": "0.1.160",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.159",
+ "version": "0.1.160",
"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 221dc92d7..51dc2ded1 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.159",
+ "version": "0.1.160",
"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 76ecbe85f..0c3672ac3 100644
--- a/host-context-controller/src/k8sClient.test.ts
+++ b/host-context-controller/src/k8sClient.test.ts
@@ -280,9 +280,16 @@ describe('McpServerWatcher startup', () => {
expect(mocks.sfsFullReconcile).toHaveBeenCalledWith([])
})
- it('completes safe bootstrap while the initial Host fleet reconciliation continues in background', async () => {
- const initialFleet = deferred()
- mocks.hostFullReconcile.mockImplementationOnce(() => initialFleet.promise)
+ it('completes safe bootstrap while every initial convergence lane continues in background', async () => {
+ const initialServerFleet = deferred()
+ const initialNetworkPolicies = deferred()
+ const initialHostFleet = deferred()
+ const initialSfsFleet = deferred()
+ const initialGfsFleet = deferred()
+ mocks.serverFullReconcile.mockImplementationOnce(() => initialServerFleet.promise)
+ mocks.netPolFullReconcile.mockImplementationOnce(() => initialNetworkPolicies.promise)
+ mocks.hostFullReconcile.mockImplementationOnce(() => initialHostFleet.promise)
+ mocks.sfsFullReconcile.mockImplementationOnce(() => initialSfsFleet.promise)
mocks.listNamespacedCustomObject.mockImplementation(async ({ plural }: { plural: string }) => {
if (plural === 'communicationchannels') {
return { metadata: { resourceVersion: 'safe-bootstrap-rv' }, items: [] }
@@ -302,13 +309,22 @@ describe('McpServerWatcher startup', () => {
const startGlobalFileSystemWatch = vi
.spyOn(watcher as any, 'startGlobalFileSystemWatch')
.mockResolvedValue(undefined)
+ const gfsFullReconcile = vi
+ .spyOn((watcher as any).gfsReconciler, 'fullReconcile')
+ .mockImplementationOnce(() => initialGfsFleet.promise)
const server = new ContextMapperServer(watcher, 0)
await server.start()
const bootstrap = watcher.start()
try {
- await vi.waitFor(() => expect(mocks.hostFullReconcile).toHaveBeenCalledOnce())
+ await vi.waitFor(() => {
+ expect(mocks.serverFullReconcile).toHaveBeenCalledOnce()
+ expect(mocks.netPolFullReconcile).toHaveBeenCalledOnce()
+ expect(mocks.hostFullReconcile).toHaveBeenCalledOnce()
+ expect(mocks.sfsFullReconcile).toHaveBeenCalledOnce()
+ expect(gfsFullReconcile).toHaveBeenCalledOnce()
+ })
await expect(bootstrap).resolves.toBeUndefined()
server.setReady(true)
@@ -322,7 +338,11 @@ describe('McpServerWatcher startup', () => {
expect(startGlobalFileSystemWatch).toHaveBeenCalledOnce()
expect(watcher.isCommunicationChannelCacheSynced()).toBe(true)
} finally {
- initialFleet.resolve(undefined)
+ initialServerFleet.resolve(undefined)
+ initialNetworkPolicies.resolve(undefined)
+ initialHostFleet.resolve(undefined)
+ initialSfsFleet.resolve(undefined)
+ initialGfsFleet.resolve(undefined)
await bootstrap.catch(() => undefined)
await server.stop()
watcher.stop()
@@ -431,6 +451,7 @@ describe('McpServerWatcher startup', () => {
await watcher.start()
+ await vi.waitFor(() => expect(mocks.serverFullReconcile).toHaveBeenCalledOnce())
expect(eventLog.slice(0, 3)).toEqual(['defaults', 'egress', 'runtime'])
expect(mocks.serverFullReconcile).toHaveBeenCalledWith([
expect.objectContaining({ name: 'web-search' }),
@@ -476,6 +497,7 @@ describe('McpServerWatcher startup', () => {
await watcher.start()
+ await vi.waitFor(() => expect(mocks.serverFullReconcile).toHaveBeenCalledOnce())
expect(mocks.serverFullReconcile).toHaveBeenCalledWith([])
expect(netPol.reconcileExternalEgress).toHaveBeenCalledTimes(1)
diff --git a/host-context-controller/src/k8sClient.ts b/host-context-controller/src/k8sClient.ts
index f579a74c6..69b0083fe 100644
--- a/host-context-controller/src/k8sClient.ts
+++ b/host-context-controller/src/k8sClient.ts
@@ -865,10 +865,13 @@ export class McpServerWatcher implements McpServerProvider {
* Hosts through their per-Host chains (step 7) and requests — without awaiting
* — a coalesced background full pass (step 8).
*/
- private recoverHostInventoryAndWatch(): Promise<HostCRD[]> {
+ private recoverHostInventoryAndWatch(
+ convergenceReason = 'Host watch recovery convergence',
+ ccLifecycleGeneration?: number
+ ): Promise<HostCRD[]> {
if (this.stopped) return Promise.resolve([])
if (this.hostRecoveryInFlight) return this.hostRecoveryInFlight
- const recovery = this.performHostInventoryRecovery()
+ const recovery = this.performHostInventoryRecovery(convergenceReason, ccLifecycleGeneration)
this.hostRecoveryInFlight = recovery
const clear = (): void => {
if (this.hostRecoveryInFlight === recovery) this.hostRecoveryInFlight = null
@@ -877,7 +880,10 @@ export class McpServerWatcher implements McpServerProvider {
return recovery
}
- private async performHostInventoryRecovery(): Promise<HostCRD[]> {
+ private async performHostInventoryRecovery(
+ convergenceReason: string,
+ ccLifecycleGeneration?: number
+ ): Promise<HostCRD[]> {
const startedAt = Date.now()
// Capture the pre-recovery inventory so step 7 can distinguish genuinely
// new/changed Hosts from ones the fleet pass will converge anyway.
@@ -939,7 +945,7 @@ export class McpServerWatcher implements McpServerProvider {
this.enqueueRecoveredHostDeletes(snapshot.hosts, previousNames)
// Request a coalesced background full pass, but do NOT await it before
// declaring watch recovery complete.
- void this.requestHostFleetReconcile('Host watch recovery convergence', undefined, 'full')
+ void this.requestHostFleetReconcile(convergenceReason, ccLifecycleGeneration, 'full')
return snapshot.hosts
} catch (error) {
// Keep authority unknown, record the failure, and use the existing
@@ -1661,36 +1667,17 @@ export class McpServerWatcher implements McpServerProvider {
console.error('[K8s] Failed to ensure default NetworkPolicies before startup:', error)
}
- let initialExternalEgressFailures = new Set<string>()
- if (serverInventoryComplete) {
- initialExternalEgressFailures =
- await this.reconcileInitialExternalEgressBeforeRuntime(initialServers)
- }
-
- // Full McpServer reconciliation (Deployments + Services). Servers with
- // egressBindings are reconciled only after their external egress policies
- // have converged, matching the ADDED/MODIFIED watch-event contract.
- if (serverInventoryComplete) {
- const runtimeServers = initialServers.filter(
- server => !initialExternalEgressFailures.has(this.externalEgressRetryKey(server))
- )
- console.log('[K8s] Running initial full reconciliation...')
- await this.reconciler.fullReconcile(runtimeServers)
- }
-
+ let initialContexts: ContextCRD[] = []
+ let contextInventoryComplete = false
try {
- const initialContexts = await listAllContexts()
+ initialContexts = await listAllContexts()
+ contextInventoryComplete = true
for (const context of initialContexts) {
this.contexts.set(context.name, context)
}
- console.log('[K8s] Running initial NetworkPolicy reconciliation...')
- await this.netPolReconciler.fullReconcile(initialContexts, initialServers, {
- serverInventoryComplete,
- ensureDefaults: false,
- })
} catch (error) {
console.error(
- '[K8s] Skipping initial NetworkPolicy reconciliation because context discovery failed:',
+ '[K8s] Skipping initial NetworkPolicy background convergence because context discovery failed:',
error
)
}
@@ -1741,60 +1728,61 @@ export class McpServerWatcher implements McpServerProvider {
this.scheduleCommunicationChannelCacheRecovery()
}
- console.log(
- `[K8s] Running initial Host reconciliation... (ccCacheSynced=${this.ccCacheSynced})`
- )
- const initialLifecycleGeneration =
- this.ccLifecycleGeneration === 0
- ? this.beginCommunicationChannelLifecycleTransition()
- : this.ccLifecycleGeneration
- // Bootstrap owns cache/watch continuity and the safe CC lifecycle boundary;
- // full Host convergence can then proceed independently. Waiting for every
- // Host to settle here made the HCC readiness probe proportional to fleet
- // size, even though urgent Host watch events retain their own per-Host
- // serialization and the fleet request carries its established retry path.
- void this.requestHostFleetReconcile(
- 'initial Host reconciliation',
- initialLifecycleGeneration,
- 'full'
- )
-
- // ── SharedFileSystem initial load + reconciliation ──
+ // ── SharedFileSystem initial inventory ──
+ let initialSfses: SharedFileSystemCRD[] = []
+ let sfsInventoryComplete = false
try {
- const initialSfses = await listAllSharedFileSystems()
+ initialSfses = await listAllSharedFileSystems()
+ sfsInventoryComplete = true
for (const sfs of initialSfses) {
this.sharedFileSystems.set(sfs.name, sfs)
}
- console.log('[K8s] Running initial SharedFileSystem reconciliation...')
- await this.sharedFileSystemReconciler.fullReconcile(initialSfses)
} catch (error) {
console.error(
- '[K8s] Skipping initial SharedFileSystem reconciliation because discovery failed:',
+ '[K8s] Skipping initial SharedFileSystem background convergence because discovery failed:',
error
)
}
- // ── GlobalFileSystem (gfs) initial load + reconciliation ──
+ // ── GlobalFileSystem (gfs) initial inventory ──
// gfs is a cluster singleton (DISTINCT from SharedFileSystem). A failed
// discovery (e.g. the CRD not installed yet) is skipped, not fatal — the
// next HCC restart / resync retries. Live updates land via the watch
- // (follow-up); the deploy applies the singleton before HCC reconciles it.
+ // and the periodic resync; the deploy applies the singleton before HCC
+ // reconciles it.
+ let initialGfses: GlobalFileSystemCRD[] = []
+ let gfsInventoryComplete = false
try {
- const initialGfses = await listAllGlobalFileSystems()
- console.log('[K8s] Running initial GlobalFileSystem reconciliation...')
- await this.gfsReconciler.fullReconcile(initialGfses)
+ initialGfses = await listAllGlobalFileSystems()
+ gfsInventoryComplete = true
} catch (error) {
console.error(
- '[K8s] Skipping initial GlobalFileSystem reconciliation because discovery failed:',
+ '[K8s] Skipping initial GlobalFileSystem background convergence because discovery failed:',
error
)
}
+ // Safety-critical bootstrap ends only after every ordinary watch is
+ // established and the Host snapshot is paired with its resourceVersion-
+ // continuing watch. The full reconciliation work launched below is
+ // deliberately not part of readiness.
await this.startMcpServerWatch()
await this.startContextWatch()
await this.startSharedFileSystemWatch()
await this.startGlobalFileSystemWatch()
+ console.log(
+ `[K8s] Starting initial Host background convergence... (ccCacheSynced=${this.ccCacheSynced})`
+ )
+ const initialLifecycleGeneration =
+ this.ccLifecycleGeneration === 0
+ ? this.beginCommunicationChannelLifecycleTransition()
+ : this.ccLifecycleGeneration
+ await this.recoverHostInventoryAndWatch(
+ 'initial Host reconciliation',
+ initialLifecycleGeneration
+ )
+
const resyncSec = config.hostResyncIntervalSec
if (resyncSec > 0) {
this.resyncTimer = setInterval(() => {
@@ -1840,6 +1828,82 @@ export class McpServerWatcher implements McpServerProvider {
`[K8s] External egress periodic DNS resync enabled (every ${externalEgressResyncSec}s)`
)
}
+
+ // Full convergence is observable and retains each lane's existing retry or
+ // periodic-resync contract, but it no longer extends provider.start() or
+ // the readiness probe. Calls are launched only after cache/watch bootstrap
+ // above so live events remain authoritative while these passes run.
+ if (serverInventoryComplete) {
+ void this.runInitialMcpServerConvergence(initialServers)
+ }
+ if (contextInventoryComplete) {
+ void this.runInitialNetworkPolicyConvergence(
+ initialContexts,
+ initialServers,
+ serverInventoryComplete
+ )
+ }
+ if (sfsInventoryComplete) {
+ void this.runInitialSharedFileSystemConvergence(initialSfses)
+ }
+ if (gfsInventoryComplete) {
+ void this.runInitialGlobalFileSystemConvergence(initialGfses)
+ }
+ }
+
+ private async runInitialMcpServerConvergence(initialServers: McpServerCRD[]): Promise<void> {
+ try {
+ const initialExternalEgressFailures =
+ await this.reconcileInitialExternalEgressBeforeRuntime(initialServers)
+ if (this.stopped) return
+ // Servers with egressBindings reconcile only after their external egress
+ // policies have converged, matching the ADDED/MODIFIED watch contract.
+ const runtimeServers = initialServers.filter(
+ server => !initialExternalEgressFailures.has(this.externalEgressRetryKey(server))
+ )
+ console.log('[K8s] Running initial McpServer background reconciliation...')
+ await this.reconciler.fullReconcile(runtimeServers)
+ } catch (error) {
+ console.error('[K8s] Initial McpServer background reconciliation failed:', error)
+ }
+ }
+
+ private async runInitialNetworkPolicyConvergence(
+ initialContexts: ContextCRD[],
+ initialServers: McpServerCRD[],
+ serverInventoryComplete: boolean
+ ): Promise<void> {
+ try {
+ console.log('[K8s] Running initial NetworkPolicy background reconciliation...')
+ await this.netPolReconciler.fullReconcile(initialContexts, initialServers, {
+ serverInventoryComplete,
+ ensureDefaults: false,
+ })
+ } catch (error) {
+ console.error('[K8s] Initial NetworkPolicy background reconciliation failed:', error)
+ }
+ }
+
+ private async runInitialSharedFileSystemConvergence(
+ initialSfses: SharedFileSystemCRD[]
+ ): Promise<void> {
+ try {
+ console.log('[K8s] Running initial SharedFileSystem background reconciliation...')
+ await this.sharedFileSystemReconciler.fullReconcile(initialSfses)
+ } catch (error) {
+ console.error('[K8s] Initial SharedFileSystem background reconciliation failed:', error)
+ }
+ }
+
+ private async runInitialGlobalFileSystemConvergence(
+ initialGfses: GlobalFileSystemCRD[]
+ ): Promise<void> {
+ try {
+ console.log('[K8s] Running initial GlobalFileSystem background reconciliation...')
+ await this.gfsReconciler.fullReconcile(initialGfses)
+ } catch (error) {
+ console.error('[K8s] Initial GlobalFileSystem background reconciliation failed:', error)
+ }
}
/**
diff --git a/scripts/e2e/e2e-hcc-readiness-bootstrap.sh b/scripts/e2e/e2e-hcc-readiness-bootstrap.sh
index 339f226fd..0f2381085 100644
--- a/scripts/e2e/e2e-hcc-readiness-bootstrap.sh
+++ b/scripts/e2e/e2e-hcc-readiness-bootstrap.sh
@@ -36,7 +36,7 @@ HCC_DEPLOY="${HCC_DEPLOY:-host-context-controller}"
HOST_NS="${MCP_HOST_NS:-mcp-host}"
RUN_ID="$(date +%s)-$$"
BLOCKER_NAME="$(truncate_rfc1123 "e2e-hcc-readiness-blocker-${RUN_ID}")"
-START_MARKER='Running initial Host reconciliation'
+START_MARKER='Starting initial Host background convergence'
COMPLETE_MARKER='Completed Host reconciliation after initial Host reconciliation'
FAIL_MARKER='Host reconciliation after initial Host reconciliation failed'
ORIGINAL_CONTROL_API_BASE_URL=""