Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(hcc): converge from post-watch caches
Alfredo Lopez12ed30b6fdab
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.jsonindex 21274e92f..7883837a0 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.161",+ "version": "0.1.162","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-host-context-controller",- "version": "0.1.161",+ "version": "0.1.162","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 cdd4381c3..3816787d0 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.161",+ "version": "0.1.162","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 d1ca2f7b7..f5b337d51 100644--- a/host-context-controller/src/k8sClient.test.ts+++ b/host-context-controller/src/k8sClient.test.ts@@ -349,6 +349,81 @@ describe('McpServerWatcher startup', () => {}})+ it('starts every initial background convergence lane from the caches current after watches', async () => {+ const staleServer = {+ metadata: { name: 'stale-server', namespace: 'mcp-server' },+ spec: {+ contextRef: 'default',+ image: 'clerum/stale-server:test',+ transport: { type: 'streamableHttp' as const, port: 3000 },+ },+ }+ const currentServer = {+ name: 'current-server',+ namespace: 'mcp-server',+ spec: {+ contextRef: 'default',+ image: 'clerum/current-server:test',+ transport: { type: 'streamableHttp' as const, port: 3000 },+ },+ }+ const staleContext = {+ metadata: { name: 'stale-context', namespace: 'mcp-server' },+ spec: { contextId: 'stale-context', mcpServers: [] },+ }+ const currentContext = {+ name: 'current-context',+ namespace: 'mcp-server',+ spec: { contextId: 'current-context', mcpServers: [] },+ }+ const staleSfs = { metadata: { name: 'stale-sfs', namespace: 'mcp-host' }, spec: {} }+ const currentSfs = { name: 'current-sfs', namespace: 'mcp-host', spec: {} }+ const staleGfs = { metadata: { name: 'stale-gfs', namespace: 'mcp-host' }, spec: {} }+ const currentGfs = { name: 'current-gfs', namespace: 'mcp-host', spec: {} }+ mocks.listNamespacedCustomObject.mockImplementation(async ({ plural }: { plural: string }) => {+ if (plural === 'mcpservers') return { items: [staleServer] }+ if (plural === 'contexts') return { items: [staleContext] }+ if (plural === 'sharedfilesystems') return { items: [staleSfs] }+ if (plural === 'globalfilesystems') return { items: [staleGfs] }+ if (plural === 'communicationchannels') {+ return { metadata: { resourceVersion: 'current-cache-rv' }, items: [] }+ }+ return { items: [] }+ })+ const watcher = new McpServerWatcher()+ vi.spyOn(watcher as any, 'startMcpServerWatch').mockImplementation(async () => {+ ;(watcher as any).servers.clear()+ ;(watcher as any).servers.set(currentServer.name, currentServer)+ })+ vi.spyOn(watcher as any, 'startContextWatch').mockImplementation(async () => {+ ;(watcher as any).contexts.clear()+ ;(watcher as any).contexts.set(currentContext.name, currentContext)+ })+ vi.spyOn(watcher as any, 'startSharedFileSystemWatch').mockImplementation(async () => {+ ;(watcher as any).sharedFileSystems.clear()+ ;(watcher as any).sharedFileSystems.set(currentSfs.name, currentSfs)+ })+ vi.spyOn(watcher as any, 'startGlobalFileSystemWatch').mockImplementation(async () => {+ ;(watcher as any).globalFileSystems.clear()+ ;(watcher as any).globalFileSystems.set(currentGfs.name, currentGfs)+ })+ const gfsFullReconcile = vi.spyOn((watcher as any).gfsReconciler, 'fullReconcile')++ await watcher.start()++ await vi.waitFor(() => {+ expect(mocks.serverFullReconcile).toHaveBeenCalledWith([currentServer])+ expect(mocks.netPolFullReconcile).toHaveBeenCalledWith([currentContext], [currentServer], {+ serverInventoryComplete: true,+ ensureDefaults: false,+ })+ expect(mocks.sfsFullReconcile).toHaveBeenCalledWith([currentSfs])+ expect(gfsFullReconcile).toHaveBeenCalledWith([currentGfs])+ })++ watcher.stop()+ })+it('retries failed initial McpServer convergence without delaying safe bootstrap', async () => {vi.useFakeTimers()const server = {diff --git a/host-context-controller/src/k8sClient.ts b/host-context-controller/src/k8sClient.tsindex b05cf9e4c..82f1eee1f 100644--- a/host-context-controller/src/k8sClient.ts+++ b/host-context-controller/src/k8sClient.ts@@ -558,6 +558,7 @@ export class McpServerWatcher implements McpServerProvider {private hosts: Map<string, HostCRD> = new Map()private contexts: Map<string, ContextCRD> = new Map()private sharedFileSystems: Map<string, SharedFileSystemCRD> = new Map()+ private globalFileSystems: Map<string, GlobalFileSystemCRD> = new Map()private communicationChannels: Map<string, CommunicationChannelCRD> = new Map()// A watch callback can still settle after its stream reports completion.// Host reconciles change the mcp-host pod template, so stale callbacks must@@ -1765,6 +1766,9 @@ export class McpServerWatcher implements McpServerProvider {try {initialGfses = await listAllGlobalFileSystems()gfsInventoryComplete = true+ for (const gfs of initialGfses) {+ this.globalFileSystems.set(gfs.name, gfs)+ }} catch (error) {console.error('[K8s] Skipping initial GlobalFileSystem background convergence because discovery failed:',@@ -1841,28 +1845,26 @@ export class McpServerWatcher implements McpServerProvider {// 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.+ // the readiness probe. Each lane snapshots its cache only after cache/watch+ // bootstrap above, so a watch event accepted during startup is not undone by+ // a pre-watch inventory snapshot.if (serverInventoryComplete) {- void this.runInitialMcpServerConvergence(initialServers)+ void this.runInitialMcpServerConvergence()}if (contextInventoryComplete) {- void this.runInitialNetworkPolicyConvergence(- initialContexts,- initialServers,- serverInventoryComplete- )+ void this.runInitialNetworkPolicyConvergence(serverInventoryComplete)}if (sfsInventoryComplete) {- void this.runInitialSharedFileSystemConvergence(initialSfses)+ void this.runInitialSharedFileSystemConvergence()}if (gfsInventoryComplete) {- void this.runInitialGlobalFileSystemConvergence(initialGfses)+ void this.runInitialGlobalFileSystemConvergence()}}- private async runInitialMcpServerConvergence(initialServers: McpServerCRD[]): Promise<void> {+ private async runInitialMcpServerConvergence(): Promise<void> {try {+ const initialServers = [...this.servers.values()]const initialExternalEgressFailures =await this.reconcileInitialExternalEgressBeforeRuntime(initialServers)if (this.stopped) return@@ -1881,11 +1883,11 @@ export class McpServerWatcher implements McpServerProvider {}private async runInitialNetworkPolicyConvergence(- initialContexts: ContextCRD[],- initialServers: McpServerCRD[],serverInventoryComplete: boolean): Promise<void> {try {+ const initialContexts = [...this.contexts.values()]+ const initialServers = [...this.servers.values()]console.log('[K8s] Running initial NetworkPolicy background reconciliation...')await this.netPolReconciler.fullReconcile(initialContexts, initialServers, {serverInventoryComplete,@@ -1924,22 +1926,17 @@ export class McpServerWatcher implements McpServerProvider {this.initialConvergenceRetryTimers.delete(lane)if (this.stopped) returnif (lane === 'McpServer') {- void this.runInitialMcpServerConvergence([...this.servers.values()])+ void this.runInitialMcpServerConvergence()return}- void this.runInitialNetworkPolicyConvergence(- [...this.contexts.values()],- [...this.servers.values()],- true- )+ void this.runInitialNetworkPolicyConvergence(true)}, delayMs)this.initialConvergenceRetryTimers.set(lane, timer)}- private async runInitialSharedFileSystemConvergence(- initialSfses: SharedFileSystemCRD[]- ): Promise<void> {+ private async runInitialSharedFileSystemConvergence(): Promise<void> {try {+ const initialSfses = [...this.sharedFileSystems.values()]console.log('[K8s] Running initial SharedFileSystem background reconciliation...')await this.sharedFileSystemReconciler.fullReconcile(initialSfses)} catch (error) {@@ -1947,10 +1944,9 @@ export class McpServerWatcher implements McpServerProvider {}}- private async runInitialGlobalFileSystemConvergence(- initialGfses: GlobalFileSystemCRD[]- ): Promise<void> {+ private async runInitialGlobalFileSystemConvergence(): Promise<void> {try {+ const initialGfses = [...this.globalFileSystems.values()]console.log('[K8s] Running initial GlobalFileSystem background reconciliation...')await this.gfsReconciler.fullReconcile(initialGfses)} catch (error) {@@ -2041,8 +2037,10 @@ export class McpServerWatcher implements McpServerProvider {console.log(`[K8s] GlobalFileSystem watch event: ${type} for ${gfs.name}`)try {if (type === 'ADDED' || type === 'MODIFIED') {+ this.globalFileSystems.set(gfs.name, gfs)await this.gfsReconciler.reconcile(gfs)} else if (type === 'DELETED') {+ this.globalFileSystems.delete(gfs.name)await this.gfsReconciler.reconcileDelete(gfs)}} catch (error) {@@ -2278,6 +2276,8 @@ export class McpServerWatcher implements McpServerProvider {if (this.stopped) returntry {const gfses = await listAllGlobalFileSystems()+ this.globalFileSystems.clear()+ for (const gfs of gfses) this.globalFileSystems.set(gfs.name, gfs)await this.gfsReconciler.fullReconcile(gfses)} catch (err) {console.error(diff --git a/scripts/e2e/e2e-hcc-readiness-bootstrap.sh b/scripts/e2e/e2e-hcc-readiness-bootstrap.shindex 29a00de63..715edba2d 100644--- a/scripts/e2e/e2e-hcc-readiness-bootstrap.sh+++ b/scripts/e2e/e2e-hcc-readiness-bootstrap.sh@@ -87,6 +87,12 @@ blocker_holds_token_request() {grep -Fq 'holding POST /api/v1/auth/mcp-host/'}+control_api_service_excludes_blocker() {+ kctl get endpointslice -n "$HCC_NS" -l 'kubernetes.io/service-name=control-api' -o json |+ jq -e --arg blocker "$BLOCKER_NAME" \+ 'all(.items[]?.endpoints[]?; .targetRef.name != $blocker)' >/dev/null+}+secret_restored() {kctl get secret "$RUNTIME_SECRET" -n "$HOST_NS" >/dev/null 2>&1}@@ -111,6 +117,9 @@ cleanup() {fiif [ "$BLOCKER_CREATED" = 1 ]; then+ kctl delete networkpolicy "$BLOCKER_NAME" -n "$HCC_NS" \+ --ignore-not-found --wait=true --timeout=60s >/dev/null 2>&1 ||+ cleanup_failed=1kctl delete service "$BLOCKER_NAME" -n "$HCC_NS" \--ignore-not-found --wait=true --timeout=60s >/dev/null 2>&1 ||cleanup_failed=1@@ -172,7 +181,6 @@ metadata:name: ${BLOCKER_NAME}namespace: ${HCC_NS}labels:- app: control-apie2e.clerum.io/hcc-readiness: "${RUN_ID}"spec:replicas: 1@@ -182,7 +190,6 @@ spec:template:metadata:labels:- app: control-apie2e.clerum.io/hcc-readiness: "${RUN_ID}"spec:automountServiceAccountToken: false@@ -227,9 +234,32 @@ spec:- name: httpport: 8090targetPort: 8090+---+apiVersion: networking.k8s.io/v1+kind: NetworkPolicy+metadata:+ name: ${BLOCKER_NAME}+ namespace: ${HCC_NS}+ labels:+ e2e.clerum.io/hcc-readiness: "${RUN_ID}"+spec:+ podSelector:+ matchLabels:+ app: ${HCC_DEPLOY}+ policyTypes: [Egress]+ egress:+ - to:+ - podSelector:+ matchLabels:+ e2e.clerum.io/hcc-readiness: "${RUN_ID}"+ ports:+ - protocol: TCP+ port: 8090EOFkctl rollout status deployment "$BLOCKER_NAME" -n "$HCC_NS" --timeout=90s >/dev/null ||die "isolated token-issuance blocker did not become ready"+control_api_service_excludes_blocker ||+ die "isolated blocker was selected by the real control-api Service"ok "isolated in-cluster blocker is ready"HCC_MUTATED=1