Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
import type { McpServerCRD } from './types'
import { getErrorCode } from './utils'
const NETWORK_READY_ANNOTATION = 'clerum.io/network-ready'
/**
* This acknowledgement is written by HCC after it has applied a server's
* NetworkPolicies so WRC can admit its workload. It is not user intent and it
* cannot change any NetworkPolicy decision. Treating that write as desired
* state makes HCC invalidate its own authoritative revocation pass under a
* large startup fleet, coupling /ready to additive runtime convergence again.
*/
function desiredAnnotations(
annotations: Record<string, string> | undefined
): Record<string, string> {
if (!annotations?.[NETWORK_READY_ANNOTATION]) return annotations ?? {}
const { [NETWORK_READY_ANNOTATION]: _networkReady, ...remaining } = annotations
return remaining
}
/**
* Kubernetes represents objects such as labels and annotations as maps, whose
* wire key order is not meaningful or stable across LIST and WATCH payloads.
* Preserve array order (it is semantic) while sorting object keys so a
* status-only event cannot masquerade as a desired-state revision.
*/
function stableJson(value: unknown): string {
if (Array.isArray(value)) return `[${value.map(stableJson).join(',')}]`
if (value && typeof value === 'object') {
const record = value as Record<string, unknown>
return `{${Object.keys(record)
.sort()
.map(key => `${JSON.stringify(key)}:${stableJson(record[key])}`)
.join(',')}}`
}
return JSON.stringify(value) ?? 'undefined'
}
export function sameMcpServerDesiredRevision(
expected: McpServerCRD,
current: McpServerCRD
): boolean {
if (expected.name !== current.name || expected.namespace !== current.namespace) return false
if (expected.uid && current.uid && expected.uid !== current.uid) return false
if (
expected.generation !== undefined &&
current.generation !== undefined &&
expected.generation !== current.generation
) {
return false
}
return (
stableJson(expected.spec) === stableJson(current.spec) &&
stableJson(desiredAnnotations(expected.annotations)) ===
stableJson(desiredAnnotations(current.annotations)) &&
stableJson(expected.labels ?? {}) === stableJson(current.labels ?? {})
)
}
export function isMcpServerStatusOnlyUpdate(
previous: McpServerCRD,
current: McpServerCRD
): boolean {
return (
previous.uid === current.uid &&
previous.generation === current.generation &&
sameMcpServerDesiredRevision(previous, current)
)
}
export interface AuthoritativeMcpServerAbsenceCheck {
inventoryAuthoritative: () => boolean
resolveCurrent: () => McpServerCRD | undefined
readCurrent: () => Promise<unknown>
}
export async function confirmAuthoritativeMcpServerAbsence({
inventoryAuthoritative,
resolveCurrent,
readCurrent,
}: AuthoritativeMcpServerAbsenceCheck): Promise<boolean> {
if (!inventoryAuthoritative() || resolveCurrent()) return false
try {
await readCurrent()
return false
} catch (error: unknown) {
if (getErrorCode(error) !== 404) throw error
}
return inventoryAuthoritative() && !resolveCurrent()
}
/** The runtime owner is the only source of credential validation. */
export function shouldFailClosedForSecretFailure(reason: string | undefined): boolean {
return (
reason === 'SecretNotFound' || reason === 'SecretMissingKey' || reason === 'SecretAccessDenied'
)
}
/**
* HCC owns policies for both runtime owners, but its Secret verdict only
* controls HCC-managed runtime. WRC permits optional keys HCC cannot judge.
* A removed reference makes an old SecretResolved condition irrelevant.
* Before the first published verdict, policy intent is optimistic: allows may
* exist while the runtime owner validates the Secret before creating runtime
* resources. A published failure then revokes those allows through convergence.
*/
export function runtimeDesired(server: McpServerCRD): boolean {
if (server.spec.enabled === false) return false
if (server.spec.managed === false || !server.spec.envSecret) return true
const condition = server.status?.conditions?.find(item => item.type === 'SecretResolved')
// The runtime owner publishes False with a validation failure reason and
// True with SecretFound. Share its reason-based fail-closed predicate: a
// contradictory status must not override a known validation failure.
return !shouldFailClosedForSecretFailure(condition?.reason)
}
/** Policy effects also depend on the effective published runtime verdict. */
export function sameMcpServerPolicyRevision(
expected: McpServerCRD,
current: McpServerCRD
): boolean {
return (
sameMcpServerDesiredRevision(expected, current) &&
runtimeDesired(expected) === runtimeDesired(current)
)
}