Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(hcc): re-wire H2 TTL-aware external-egress resync into the coordinator [R4-M1]
Alfredo Lopezf6dc860afd8f
diff --git a/host-context-controller/src/externalEgressConvergenceCoordinator.test.ts b/host-context-controller/src/externalEgressConvergenceCoordinator.test.tsindex 8d515f8de..176a62759 100644--- a/host-context-controller/src/externalEgressConvergenceCoordinator.test.ts+++ b/host-context-controller/src/externalEgressConvergenceCoordinator.test.ts@@ -55,6 +55,7 @@ function coordinator(overrides: CoordinatorOverrides = {}) {enqueue: async (_selected, work) => work(),mutate,replay,+ externalEgressRefreshMinTtlMs: () => Infinity,...overrides,})return { instance, mutate, replay, servers }@@ -762,7 +763,7 @@ describe('ExternalEgressConvergenceCoordinator', () => {const { instance, mutate, servers } = coordinator()servers.set(selected.name, selected)- instance.startPeriodicResync(1)+ instance.startPeriodicResync(1, 1)await vi.advanceTimersByTimeAsync(1000)expect(mutate).toHaveBeenCalledOnce()@@ -770,4 +771,74 @@ describe('ExternalEgressConvergenceCoordinator', () => {await vi.advanceTimersByTimeAsync(5000)expect(mutate).toHaveBeenCalledOnce()})++ it('arms one resync at a time, rescheduling only after each pass completes (H2)', async () => {+ vi.useFakeTimers()+ vi.spyOn(Math, 'random').mockReturnValue(0)+ const selected = server('web-search')+ const release: Array<() => void> = []+ const mutate = vi.fn(+ () =>+ new Promise<void>(resolve => {+ release.push(resolve)+ })+ )+ const { instance, servers } = coordinator({ mutate })+ servers.set(selected.name, selected)++ instance.startPeriodicResync(10, 5)+ // First pass fires at the configured interval (10s) and then blocks.+ await vi.advanceTimersByTimeAsync(10_000)+ expect(mutate).toHaveBeenCalledOnce()++ // Complete the pass off the fixed grid (at t=17s). The successor is armed+ // relative to completion (t=27s), NOT on a fixed 20s/30s interval grid.+ await vi.advanceTimersByTimeAsync(7_000)+ release[0]()+ await flushMicrotasks()++ // A fixed setInterval would fire a second pass at t=20s; the self-+ // rescheduling timer must not, because it only re-armed at completion.+ await vi.advanceTimersByTimeAsync(3_000)+ expect(mutate).toHaveBeenCalledOnce()++ // The successor fires 10s after completion (t=27s).+ await vi.advanceTimersByTimeAsync(7_000)+ expect(mutate).toHaveBeenCalledTimes(2)++ instance.stop()+ release.forEach(resolve => resolve())+ })++ it('advances the next periodic resync to <= observed TTL/2 (H2, issue #299)', async () => {+ vi.useFakeTimers()+ vi.spyOn(Math, 'random').mockReturnValue(0)+ const selected = server('web-search')+ // The mutation observes a low DNS TTL, ratcheting the coordinator's source+ // down; scheduleNext reads it AFTER the pass and must advance to <= TTL/2.+ let minObservedTtlMs = Infinity+ const mutate = vi.fn(async () => {+ minObservedTtlMs = 20_000+ })+ const { instance, servers } = coordinator({+ mutate,+ externalEgressRefreshMinTtlMs: () => minObservedTtlMs,+ })+ servers.set(selected.name, selected)++ // Interval 60s, floor 5s. No TTL observed yet, so the first pass fires at 60s.+ instance.startPeriodicResync(60, 5)+ await vi.advanceTimersByTimeAsync(59_999)+ expect(mutate).not.toHaveBeenCalled()+ await vi.advanceTimersByTimeAsync(1)+ expect(mutate).toHaveBeenCalledOnce()++ // TTL/2 = 10s now governs the next delay, far below the 60s backstop.+ await vi.advanceTimersByTimeAsync(9_999)+ expect(mutate).toHaveBeenCalledOnce()+ await vi.advanceTimersByTimeAsync(1)+ expect(mutate).toHaveBeenCalledTimes(2)++ instance.stop()+ })})diff --git a/host-context-controller/src/externalEgressConvergenceCoordinator.ts b/host-context-controller/src/externalEgressConvergenceCoordinator.tsindex 6f3aba598..45b24d020 100644--- a/host-context-controller/src/externalEgressConvergenceCoordinator.ts+++ b/host-context-controller/src/externalEgressConvergenceCoordinator.ts@@ -44,6 +44,10 @@ type ExternalEgressCoordinatorDependencies = {server: McpServerCRD,retry: ExternalEgressRetryHandle) => Promise<void>+ // Smallest DNS TTL (ms) observed across external-egress resolutions, or+ // Infinity if none. startPeriodicResync reads it to advance the next resync to+ // <= TTL/2 (H2, issue #299).+ externalEgressRefreshMinTtlMs: () => number}export type ExternalEgressStartupGates = {@@ -96,6 +100,26 @@ function delay(ms: number): Promise<void> {return new Promise(resolve => setTimeout(resolve, ms))}+/**+ * External-egress resync delay (ms), H2 (issue #299). The configured interval is+ * the backstop; a finite observed DNS TTL advances the delay to <= TTL/2 so a+ * rotating low-TTL host is sampled every rotation. The floor is the hard lower+ * bound to avoid a hot-loop. Pure and unit-testable; consumed by+ * {@link ExternalEgressConvergenceCoordinator.startPeriodicResync}.+ */+export function externalEgressResyncDelayMs(+ intervalSec: number,+ floorSec: number,+ minObservedTtlMs = Infinity+): number {+ const floorMs = floorSec * 1000+ const configuredMs = intervalSec * 1000+ const ttlAwareMs = Number.isFinite(minObservedTtlMs)+ ? Math.min(configuredMs, minObservedTtlMs / 2)+ : configuredMs+ return Math.max(ttlAwareMs, floorMs)+}+/*** Owns temporal coordination for external-egress convergence. Kubernetes* authority and mutations remain behind callbacks supplied by the watcher.@@ -109,17 +133,33 @@ export class ExternalEgressConvergenceCoordinator {private readonly retrySlots = new Map<string, object>()private readonly inFlight = new Map<string, Promise<void>>()private resyncInFlight: Promise<void> | null = null- private resyncTimer: ReturnType<typeof setInterval> | null = null+ private resyncTimer: ReturnType<typeof setTimeout> | null = nullprivate stopped = falseconstructor(private readonly dependencies: ExternalEgressCoordinatorDependencies) {}- startPeriodicResync(intervalSec: number): void {+ startPeriodicResync(intervalSec: number, floorSec: number): void {if (this.stopped || intervalSec <= 0 || this.resyncTimer) return- this.resyncTimer = setInterval(() => {- void this.runResync()- }, intervalSec * 1000)- console.log(`[K8s] External egress periodic DNS resync enabled (every ${intervalSec}s)`)+ // Self-rescheduling (not a fixed setInterval) so the delay can advance to+ // <= observed TTL/2 (H2, issue #299), and rescheduling AFTER each pass+ // completes guarantees no overlapping resyncs. runResync still dedupes any+ // in-flight pass; the floor is the hard lower bound against a hot-loop.+ const scheduleNext = (): void => {+ if (this.stopped) return+ const delayMs = externalEgressResyncDelayMs(+ intervalSec,+ floorSec,+ this.dependencies.externalEgressRefreshMinTtlMs()+ )+ this.resyncTimer = setTimeout(() => {+ void this.runResync().finally(scheduleNext)+ }, delayMs)+ this.resyncTimer.unref?.()+ }+ scheduleNext()+ console.log(+ `[K8s] External egress periodic DNS resync enabled (every ${intervalSec}s, advancing to <= TTL/2)`+ )}runResync(): Promise<void> {@@ -362,7 +402,7 @@ export class ExternalEgressConvergenceCoordinator {this.stopped = truethis.retryLimiter.cancelPending()if (this.resyncTimer) {- clearInterval(this.resyncTimer)+ clearTimeout(this.resyncTimer)this.resyncTimer = null}for (const timer of this.retryTimers.values()) {diff --git a/host-context-controller/src/k8sClient.test.ts b/host-context-controller/src/k8sClient.test.tsindex 0304adcb3..7f4d5a4e9 100644--- a/host-context-controller/src/k8sClient.test.ts+++ b/host-context-controller/src/k8sClient.test.ts@@ -152,8 +152,10 @@ const mocks = vi.hoisted(() => {}})-// Mutable so a test can enable the external-egress resync timer (R1-M4); every-// other test keeps externalEgressResyncIntervalSec: 0 (reset in afterEach).+// Kept at externalEgressResyncIntervalSec: 0 in every test (reset in afterEach):+// the external-egress periodic resync timer now lives in the convergence+// coordinator and is unit-tested in externalEgressConvergenceCoordinator.test.ts,+// so the k8sClient integration tests leave it disabled to avoid a stray timer.const mockConfig = vi.hoisted(() => ({devMode: false,namespace: 'mcp-server',diff --git a/host-context-controller/src/k8sClient.ts b/host-context-controller/src/k8sClient.tsindex eefa960a7..4d396c00b 100644--- a/host-context-controller/src/k8sClient.ts+++ b/host-context-controller/src/k8sClient.ts@@ -19,6 +19,9 @@ import {type ExternalEgressRetryHandle,type ExternalEgressWatchEventType,} from './externalEgressConvergenceCoordinator'+// Re-exported for the H2 unit tests that import it from this module (the pure+// delay helper now lives with its sole consumer, the convergence coordinator).+export { externalEgressResyncDelayMs } from './externalEgressConvergenceCoordinator'import { gfsDefaultFactoryConfig } from './gfsConfig'import { GfsReconciler } from './gfsReconciler'import { ControlApiGfsSeedClient } from './gfsSeedClient'@@ -111,24 +114,6 @@ const PLURAL_COMMUNICATIONCHANNELS = 'communicationchannels'const INITIAL_CONVERGENCE_RETRY_DELAYS_MS = [5000, 15000, 30000, 60000, 300000]const INVENTORY_CACHE_RECOVERY_RETRY_MS = 5000-/**- * External-egress resync delay (ms), H2 (issue #299). The configured interval is- * the backstop; a finite observed DNS TTL advances the delay to <= TTL/2 so a- * rotating low-TTL host is sampled every rotation. The floor is the hard lower- * bound to avoid a hot-loop. Pure and unit-testable.- */-export function externalEgressResyncDelayMs(- intervalSec: number,- floorSec: number,- minObservedTtlMs = Infinity-): number {- const floorMs = floorSec * 1000- const configuredMs = intervalSec * 1000- const ttlAwareMs = Number.isFinite(minObservedTtlMs)- ? Math.min(configuredMs, minObservedTtlMs / 2)- : configuredMs- return Math.max(ttlAwareMs, floorMs)-}const COMMUNICATION_CHANNEL_CACHE_RECOVERY_RETRY_MS = 5000const HOST_CACHE_RECOVERY_RETRY_MS = 5000const HOST_WATCH_RECONCILE_RETRY_DELAYS_MS = [5000, 15000, 30000]@@ -737,6 +722,7 @@ export class McpServerWatcher implements McpServerProvider {mutate: (type, server, options) => this.performExternalEgressMutation(type, server, options),replay: (type, server, retry) =>this.reconcileMcpServerWatchEvent(type, server, this.mcpWatchGeneration, retry),+ externalEgressRefreshMinTtlMs: () => this.netPolReconciler.externalEgressRefreshMinTtlMs,})private readonly initialConvergenceRetryTimers = new Map<InitialConvergenceLane,@@ -2529,8 +2515,13 @@ export class McpServerWatcher implements McpServerProvider {// #205 delegates external-egress periodic resync to the convergence// coordinator. Its runResyncCore drives reconcileExternalEgress, so the// #299 sliding-window accumulation still converges on DNS rotation between- // McpServer events (the coordinator, not a standalone timer, owns cadence).- this.externalEgressCoordinator.startPeriodicResync(externalEgressResyncSec)+ // McpServer events. The coordinator owns cadence with a self-rescheduling+ // timer that advances to <= observed TTL/2 (H2, issue #299), bounded by the+ // configured floor.+ this.externalEgressCoordinator.startPeriodicResync(+ externalEgressResyncSec,+ config.externalEgressRefreshFloorSec+ )// Full convergence is observable and retains each lane's existing retry or// periodic-resync contract, but it no longer extends provider.start().