Skip to content

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

TypeScript

feat(hcc): count skipped no-op writes by kind

Alfredo Lopez6187b12ed8a3
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index 2cce0454f..a61b084ec 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.274",
+ "version": "0.1.275",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.274",
+ "version": "0.1.275",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@clerum/codex-catalog-projection": "file:../packages/codex-catalog-projection",
diff --git a/host-context-controller/package.json b/host-context-controller/package.json
index f81478385..206246de0 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.274",
+ "version": "0.1.275",
"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/__tests__/replaceWithConflictRetryOrder.test.ts b/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts
index db2551592..f9439e29c 100644
--- a/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts
+++ b/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts
@@ -8,13 +8,21 @@ import {
serviceMatchesDesired,
} from '../utils'
-async function readWritesTotal(kind: string): Promise<number> {
- const metric = registry.getSingleMetric('clerum_hcc_writes_total')
- if (!metric) throw new Error('clerum_hcc_writes_total is not registered')
+async function readLabeledCounter(name: string, kind: string): Promise<number> {
+ const metric = registry.getSingleMetric(name)
+ if (!metric) throw new Error(`${name} is not registered`)
const snapshot = await metric.get()
return snapshot.values.find(entry => entry.labels.kind === kind)?.value ?? 0
}
+async function readWritesTotal(kind: string): Promise<number> {
+ return readLabeledCounter('clerum_hcc_writes_total', kind)
+}
+
+async function readWriteSkipsTotal(kind: string): Promise<number> {
+ return readLabeledCounter('clerum_hcc_write_skips_total', kind)
+}
+
function apiException(code: number): ApiException<unknown> {
return new ApiException(code, 'test', {}, {})
}
@@ -348,6 +356,56 @@ describe('replaceWithConflictRetry order and retry', () => {
expect(await readWritesTotal('Service')).toBe(before)
})
+ it('SKIP-1: isUpToDate skip increments clerum_hcc_write_skips_total{kind=Service}', async () => {
+ const replace = vi.fn()
+ const beforeWrites = await readWritesTotal('Service')
+ const beforeSkips = await readWriteSkipsTotal('Service')
+
+ await replaceWithConflictRetry({
+ description: 'Service "svc"',
+ logPrefix: '[Test]',
+ body: desired,
+ mergeExisting: preserveServiceAssignedFields,
+ isUpToDate: () => true,
+ read: async () => existing,
+ replace,
+ })
+
+ expect(replace).not.toHaveBeenCalled()
+ expect(await readWritesTotal('Service')).toBe(beforeWrites)
+ expect(await readWriteSkipsTotal('Service')).toBe(beforeSkips + 1)
+ expect(await registry.metrics()).toContain('clerum_hcc_write_skips_total')
+ })
+
+ it('SKIP-2: successful replace and read-404 do not increment write skips', async () => {
+ const replace = vi.fn().mockResolvedValue({})
+ const beforeSkips = await readWriteSkipsTotal('Service')
+ const log = vi.spyOn(console, 'log').mockImplementation(() => {})
+
+ await replaceWithConflictRetry({
+ description: 'Service "svc"',
+ logPrefix: '[Test]',
+ body: desired,
+ mergeExisting: preserveServiceAssignedFields,
+ isUpToDate: () => false,
+ read: async () => existing,
+ replace,
+ })
+
+ await replaceWithConflictRetry({
+ description: 'Service "svc"',
+ logPrefix: '[Test]',
+ body: desired,
+ read: async () => {
+ throw { code: 404 }
+ },
+ replace,
+ })
+
+ expect(await readWriteSkipsTotal('Service')).toBe(beforeSkips)
+ log.mockRestore()
+ })
+
it('WRITE-3: 409 then successful replace increments once, not per attempt', async () => {
const firstRead: k8s.V1Service = {
...existing,
diff --git a/host-context-controller/src/metrics.ts b/host-context-controller/src/metrics.ts
index 4c15c0f02..76e35cb21 100644
--- a/host-context-controller/src/metrics.ts
+++ b/host-context-controller/src/metrics.ts
@@ -301,6 +301,15 @@ export const writesTotal = counter({
labelNames: ['kind'] as const,
})
+// Twin of writesTotal. Incremented when isUpToDate returns true, before the
+// helper returns without replace(). Label set is {kind} only — policy_type
+// belongs to #526.
+export const writeSkipsTotal = counter({
+ name: 'clerum_hcc_write_skips_total',
+ help: 'No-op Kubernetes replaces skipped by replaceWithConflictRetry because the merged object was already up to date, by object kind.',
+ labelNames: ['kind'] as const,
+})
+
export const hostCleanupDeferredTotal = counter({
name: 'clerum_hcc_host_cleanup_deferred_total',
help: 'Orphan Host cleanup deferrals by bounded reason.',
diff --git a/host-context-controller/src/networkPolicyReconciler.egressFailStatic.test.ts b/host-context-controller/src/networkPolicyReconciler.egressFailStatic.test.ts
index 3d8e358a6..f28a29320 100644
--- a/host-context-controller/src/networkPolicyReconciler.egressFailStatic.test.ts
+++ b/host-context-controller/src/networkPolicyReconciler.egressFailStatic.test.ts
@@ -49,6 +49,7 @@ vi.mock('./metrics', () => ({
networkPolicySafetyPassDurationSeconds: { observe: vi.fn() },
networkPolicySafetyPassPoliciesTotal: { inc: vi.fn() },
writesTotal: { inc: vi.fn() },
+ writeSkipsTotal: { inc: vi.fn() },
}))
function makeMockNetworkingApi() {
diff --git a/host-context-controller/src/networkPolicyReconciler.orphanSweep.test.ts b/host-context-controller/src/networkPolicyReconciler.orphanSweep.test.ts
index 2939d003f..ecdccb240 100644
--- a/host-context-controller/src/networkPolicyReconciler.orphanSweep.test.ts
+++ b/host-context-controller/src/networkPolicyReconciler.orphanSweep.test.ts
@@ -37,6 +37,7 @@ vi.mock('./metrics', () => ({
netPolOrphansDeletedTotal: { inc: vi.fn() },
netPolOrphanSweepCappedTotal: { inc: vi.fn() },
writesTotal: { inc: vi.fn() },
+ writeSkipsTotal: { inc: vi.fn() },
}))
function makeMockNetworkingApi() {
return {
diff --git a/host-context-controller/src/networkPolicyReconciler.test.ts b/host-context-controller/src/networkPolicyReconciler.test.ts
index 6946b3bfb..218a4ff41 100644
--- a/host-context-controller/src/networkPolicyReconciler.test.ts
+++ b/host-context-controller/src/networkPolicyReconciler.test.ts
@@ -80,6 +80,7 @@ vi.mock('./metrics', () => ({
netPolOrphansDeletedTotal: { inc: vi.fn() },
netPolOrphanSweepCappedTotal: { inc: vi.fn() },
writesTotal: { inc: vi.fn() },
+ writeSkipsTotal: { inc: vi.fn() },
}))
function makeMockNetworkingApi() {
diff --git a/host-context-controller/src/utils.ts b/host-context-controller/src/utils.ts
index 326e3a340..b9415b8d9 100644
--- a/host-context-controller/src/utils.ts
+++ b/host-context-controller/src/utils.ts
@@ -1,5 +1,5 @@
import * as k8s from '@kubernetes/client-node'
-import { writesTotal } from './metrics'
+import { writeSkipsTotal, writesTotal } from './metrics'
/** Extract HTTP status code from a K8s client error (handles both error formats). */
export function getErrorCode(error: unknown): number | undefined {
@@ -71,7 +71,10 @@ export async function replaceWithConflictRetry<
metadata: { ...desired.metadata, resourceVersion: existing.metadata?.resourceVersion },
}
const next = mergeExisting ? mergeExisting(base, existing) : base
- if (isUpToDate?.(next, existing)) return
+ if (isUpToDate?.(next, existing)) {
+ writeSkipsTotal.inc({ kind: next.kind ?? 'unknown' })
+ return
+ }
if (mutationAllowed && !mutationAllowed()) return
try {
await replace(next)