Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): read the CR once per hosts/contexts PUT
Diego Zacariasc3ac3e4099f9
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 9b40bbd85..fe8a44aa0 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.437",+ "version": "0.1.438","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.437",+ "version": "0.1.438","dependencies": {"@clerum/image-policy": "file:../packages/image-policy","@clerum/llm-providers": "file:../packages/llm-providers",diff --git a/control-api/package.json b/control-api/package.jsonindex 513de4d55..8d342b02d 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.437",+ "version": "0.1.438","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/k8s.ts b/control-api/src/k8s.tsindex c50ab23da..be27971b6 100644--- a/control-api/src/k8s.ts+++ b/control-api/src/k8s.ts@@ -258,9 +258,12 @@ export class K8sGateway {}spec: Record<string, unknown>},- namespace?: string+ namespace?: string,+ // N1 — forwards an already-read CR snapshot so ResourceService.updateResource+ // can skip a redundant apiserver GET (see the admin PUT ratchet in resources.ts).+ options?: Parameters<ResourceService['updateResource']>[4]): Promise<unknown> {- return this.resources.updateResource(plural, name, body, namespace)+ return this.resources.updateResource(plural, name, body, namespace, options)}/**diff --git a/control-api/src/routes/admin/resources.ts b/control-api/src/routes/admin/resources.tsindex d6b0770f0..7fa6bb489 100644--- a/control-api/src/routes/admin/resources.ts+++ b/control-api/src/routes/admin/resources.ts@@ -5,7 +5,11 @@ import { enforceNamespace } from '../../http/namespaceAudit.js'import { validateCommunicationChannelSpec } from '../../http/validateCommunicationChannelSpec.js'import { validateMcpServerSpecPreflight } from '../../http/validateMcpServerSpec.js'import { K8sGateway } from '../../k8s.js'-import { K8sConflictError, K8sNotFoundError } from '../../services/resourceService.js'+import {+ K8sConflictError,+ K8sNotFoundError,+ type MutableResourceSnapshot,+} from '../../services/resourceService.js'import { ClerumResourceType } from '../../types.js'import {registerCommunicationChannelCredentialsRoutes,@@ -480,16 +484,24 @@ export function createAdminResourcesRouter(gateway: K8sGateway): Router {spec: Record<string, unknown>}+ // N1 — the CR read by the ratchet below is handed to updateResource so a+ // hosts/contexts PUT reads the apiserver ONCE, not twice (updateResource+ // already reads `current` internally). Undefined for non hosts/contexts+ // plurals and on the 404 path, where updateResource reads afresh and+ // surfaces the real 404.+ let ratchetCurrent: MutableResourceSnapshot | undefined+// F0.3 RATCHET: for hosts + contexts, validate an identifier/display field// ONLY IF its value changed vs the current CR, so a legacy resource whose// spec.host / contextId is out of norm is never blocked for other edits.if ((plural === 'hosts' || plural === 'contexts') && body.spec) {let currentSpec: Record<string, unknown> | null = nulltry {- const current = (await gateway.getResource(plural, req.params.name, ns)) as {- spec?: Record<string, unknown>- } | null+ const current = (await gateway.getResource(plural, req.params.name, ns)) as+ | (MutableResourceSnapshot & { spec?: Record<string, unknown> })+ | nullcurrentSpec = recordValue(current?.spec)+ ratchetCurrent = current ?? undefined} catch (err) {// Missing resource: fall through with currentSpec=null (validate every// present field). The subsequent updateResource surfaces the real 404.@@ -561,7 +573,13 @@ export function createAdminResourcesRouter(gateway: K8sGateway): Router {}: bodytry {- const updated = await gateway.updateResource(plural, req.params.name, updateBody, ns)+ // Only the ratchet path (hosts/contexts) has a pre-read to hand off; the+ // CC/mcpservers paths call with the original 4-arg arity untouched.+ const updated = ratchetCurrent+ ? await gateway.updateResource(plural, req.params.name, updateBody, ns, {+ preReadCurrent: ratchetCurrent,+ })+ : await gateway.updateResource(plural, req.params.name, updateBody, ns)if (plural === 'communicationchannels' && body.spec) {const missingField = missingPersistedProviderSetting(updateBody.spec, updated)if (missingField) {diff --git a/control-api/src/services/resourceService.ts b/control-api/src/services/resourceService.tsindex bc1b5e07a..ccb26a7ef 100644--- a/control-api/src/services/resourceService.ts+++ b/control-api/src/services/resourceService.ts@@ -51,7 +51,7 @@ export class K8sConflictError extends Error {}}-type MutableResourceSnapshot = {+export type MutableResourceSnapshot = {metadata?: {annotations?: Record<string, string>labels?: Record<string, string>@@ -304,7 +304,19 @@ export class ResourceService {}spec: Record<string, unknown>},- namespace?: string+ namespace?: string,+ options?: {+ /**+ * N1 — a CR snapshot the caller ALREADY read for this same object (e.g.+ * the admin PUT ratchet read in resources.ts). When present it is reused+ * for the FIRST replace attempt instead of issuing a second apiserver+ * GET, collapsing the per-PUT read count from 2 to 1. On a 409 retry the+ * loop falls back to a fresh read (the pre-read is stale by definition),+ * so the last-write-wins semantics are unchanged. Never used past+ * attempt 1.+ */+ preReadCurrent?: MutableResourceSnapshot+ }): Promise<unknown> {const ns = this.resolveNamespace(plural, namespace)const intent = await persistHostIntent({ plural, action: 'update', namespace: ns, name })@@ -313,7 +325,11 @@ export class ResourceService {// same payload, so the loop collapses to a single attempt.const maxAttempts = readerResourceVersion ? 1 : 3for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {- const current = (await this.getResource(plural, name, ns)) as {+ const current = (+ attempt === 1 && options?.preReadCurrent+ ? options.preReadCurrent+ : await this.getResource(plural, name, ns)+ ) as {metadata?: {annotations?: Record<string, string>labels?: Record<string, string>