Skip to content

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

TypeScript

feat(hcc): count successful replaceWithConflictRetry writes by kind

Alfredo Lopez8efa6c227e9e
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index 1393c6c3b..eacc27b90 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.266",
+ "version": "0.1.267",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.266",
+ "version": "0.1.267",
"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.json
index b178f5fd1..80eff8d53 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.266",
+ "version": "0.1.267",
"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 ab838428a..112255b4a 100644
--- a/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts
+++ b/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts
@@ -1,12 +1,20 @@
import { describe, expect, it, vi } from 'vitest'
import { ApiException } from '@kubernetes/client-node'
import type * as k8s from '@kubernetes/client-node'
+import { registry } from '../metrics'
import {
preserveServiceAssignedFields,
replaceWithConflictRetry,
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')
+ const snapshot = await metric.get()
+ return snapshot.values.find(entry => entry.labels.kind === kind)?.value ?? 0
+}
+
function apiException(code: number): ApiException<unknown> {
return new ApiException(code, 'test', {}, {})
}
@@ -290,4 +298,53 @@ describe('replaceWithConflictRetry order and retry', () => {
expect(replace.mock.calls[0][0].metadata?.resourceVersion).toBe('9')
expect(read).toHaveBeenCalledTimes(2)
})
+
+ it('WRITE-1: successful replace increments clerum_hcc_writes_total{kind=Service}', async () => {
+ const replace = vi.fn().mockResolvedValue({})
+ const before = await readWritesTotal('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,
+ })
+
+ expect(replace).toHaveBeenCalledOnce()
+ expect(await readWritesTotal('Service')).toBe(before + 1)
+ expect(await registry.metrics()).toContain('clerum_hcc_writes_total')
+ log.mockRestore()
+ })
+
+ it('WRITE-2: isUpToDate skip and read-404 do not increment', async () => {
+ const replace = vi.fn()
+ const before = await readWritesTotal('Service')
+
+ await replaceWithConflictRetry({
+ description: 'Service "svc"',
+ logPrefix: '[Test]',
+ body: desired,
+ mergeExisting: preserveServiceAssignedFields,
+ isUpToDate: () => true,
+ read: async () => existing,
+ replace,
+ })
+
+ await replaceWithConflictRetry({
+ description: 'Service "svc"',
+ logPrefix: '[Test]',
+ body: desired,
+ read: async () => {
+ throw { code: 404 }
+ },
+ replace,
+ })
+
+ expect(replace).not.toHaveBeenCalled()
+ expect(await readWritesTotal('Service')).toBe(before)
+ })
})
diff --git a/host-context-controller/src/metrics.ts b/host-context-controller/src/metrics.ts
index b599259e9..91c31ee13 100644
--- a/host-context-controller/src/metrics.ts
+++ b/host-context-controller/src/metrics.ts
@@ -279,6 +279,14 @@ export const hostFleetRequestsTotal = counter({
labelNames: ['result'] as const,
})
+// #493: successful replace() only, inside replaceWithConflictRetry. Kind is
+// next.kind ?? 'unknown'. Direct Role PUTs stay invisible until G5.
+export const writesTotal = counter({
+ name: 'clerum_hcc_writes_total',
+ help: 'Successful Kubernetes replace() calls issued through replaceWithConflictRetry, 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/utils.ts b/host-context-controller/src/utils.ts
index 1a82991e3..e0d681e4c 100644
--- a/host-context-controller/src/utils.ts
+++ b/host-context-controller/src/utils.ts
@@ -1,4 +1,5 @@
import * as k8s from '@kubernetes/client-node'
+import { writesTotal } from './metrics'
/** Extract HTTP status code from a K8s client error (handles both error formats). */
export function getErrorCode(error: unknown): number | undefined {
@@ -19,7 +20,7 @@ export function getErrorCode(error: unknown): number | undefined {
* simultaneously, repeat — amplifying API load instead of converging.
*/
export async function replaceWithConflictRetry<
- T extends { metadata?: { resourceVersion?: string } },
+ T extends { metadata?: { resourceVersion?: string }; kind?: string },
>(opts: {
description: string
logPrefix: string
@@ -74,6 +75,7 @@ export async function replaceWithConflictRetry<
if (mutationAllowed && !mutationAllowed()) return
try {
await replace(next)
+ writesTotal.inc({ kind: next.kind ?? 'unknown' })
const suffix = attempt > 1 ? ` (after ${attempt} attempts)` : ''
console.log(`${logPrefix} Updated ${description}${suffix}`)
return