Skip to content

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

TypeScript

fix(hcc): compare desired metadata semantically

Alfredo Lopezb53e1b8b73c6
diff --git a/host-context-controller/src/mcpServerSafety.test.ts b/host-context-controller/src/mcpServerSafety.test.ts
index d68d7f029..19f75caf3 100644
--- a/host-context-controller/src/mcpServerSafety.test.ts
+++ b/host-context-controller/src/mcpServerSafety.test.ts
@@ -58,8 +58,9 @@ describe('sameMcpServerDesiredRevision', () => {
expect(sameMcpServerDesiredRevision(expected, makeServer({ generation: undefined }))).toBe(true)
})
- it('uses JSON equality for spec, annotations, and labels', () => {
+ it('compares desired object fields by semantic content, not Kubernetes map key order', () => {
const expected = makeServer({
+ annotations: { first: '1', second: '2' },
labels: { first: '1', second: '2' },
})
@@ -75,6 +76,15 @@ describe('sameMcpServerDesiredRevision', () => {
})
)
).toBe(false)
+ expect(
+ sameMcpServerDesiredRevision(
+ expected,
+ makeServer({
+ annotations: { second: '2', first: '1' },
+ labels: expected.labels,
+ })
+ )
+ ).toBe(true)
expect(
sameMcpServerDesiredRevision(
expected,
@@ -89,9 +99,10 @@ describe('sameMcpServerDesiredRevision', () => {
expected,
makeServer({
labels: { second: '2', first: '1' },
+ annotations: expected.annotations,
})
)
- ).toBe(false)
+ ).toBe(true)
})
it('ignores the controller-owned network-ready handshake annotation', () => {
diff --git a/host-context-controller/src/mcpServerSafety.ts b/host-context-controller/src/mcpServerSafety.ts
index cacbbe2a4..7f8d8438d 100644
--- a/host-context-controller/src/mcpServerSafety.ts
+++ b/host-context-controller/src/mcpServerSafety.ts
@@ -16,6 +16,24 @@ function desiredAnnotations(annotations: Record<string, string> | undefined): Re
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
@@ -31,10 +49,10 @@ export function sameMcpServerDesiredRevision(
}
return (
- JSON.stringify(expected.spec) === JSON.stringify(current.spec) &&
- JSON.stringify(desiredAnnotations(expected.annotations)) ===
- JSON.stringify(desiredAnnotations(current.annotations)) &&
- JSON.stringify(expected.labels ?? {}) === JSON.stringify(current.labels ?? {})
+ stableJson(expected.spec) === stableJson(current.spec) &&
+ stableJson(desiredAnnotations(expected.annotations)) ===
+ stableJson(desiredAnnotations(current.annotations)) &&
+ stableJson(expected.labels ?? {}) === stableJson(current.labels ?? {})
)
}