Skip to content

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

TypeScript

merge: sync origin/dev into feat/llm-provider-lifecycle-control-api

Diego Zacarias5c71febe7b00
diff --cc control-api/src/routes/admin/resources.ts
index 11b3d173d,a66269ef3..ce881bb9b
--- a/control-api/src/routes/admin/resources.ts
+++ b/control-api/src/routes/admin/resources.ts
@@@ -11,8 -5,11 +11,12 @@@ import { enforceNamespace } from '../..
import { validateCommunicationChannelSpec } from '../../http/validateCommunicationChannelSpec.js'
import { validateMcpServerSpecPreflight } from '../../http/validateMcpServerSpec.js'
import { K8sGateway } from '../../k8s.js'
+import { getModelAllowlistState, isModelAllowed } from '../../services/llmAllowedModels.js'
- import { K8sConflictError } from '../../services/resourceService.js'
+ import {
+ K8sConflictError,
+ K8sNotFoundError,
+ type MutableResourceSnapshot,
+ } from '../../services/resourceService.js'
import { secretKeyNames } from '../../services/secretKeyNames.js'
import { ClerumResourceType } from '../../types.js'
import {
@@@ -24,14 -21,9 +28,15 @@@ import
extractK8sStatusCode,
preserveCommunicationChannelCredentialsSecretRef,
} from './communicationChannelSpecHelpers.js'
+import { enumerateHostModelReferences } from './hostModelReferences.js'
import { validateHostSecretRef } from './hostSecrets.js'
-import { validateHostSpec } from './hostSpecValidation.js'
+import { type HostSpecValidationDeps, validateHostSpec } from './hostSpecValidation.js'
+import {
+ type HostSpecIncoherenceToleratedEvent,
+ emitHostSpecIncoherenceTolerated,
+} from './hostWriteGateAudit.js'
+ import { collectResourceSpecFieldIssues, validateResourceName } from './resourceFieldValidation.js'
+import type { StaleModelWarning } from './staleModelWarning.js'
const PROVIDER_SETTINGS_FIELDS: Readonly<Record<string, readonly string[]>> = {
telegramSettings: ['botHandle', 'replyOnlyWhenMentioned'],
@@@ -427,11 -454,26 +500,30 @@@ export function createAdminResourcesRou
}
credentials?: Record<string, string>
}
+ // Fase 6 soft-quarantine warnings granted during Host validation, returned
+ // in the 201 body only after the CR persists (below). Empty for every
+ // non-host create.
+ const hostWarnings: StaleModelWarning[] = []
+ // FIX-A1: validate metadata.name (RFC1123) for ALL plurals BEFORE any spec
+ // validation or side effect (e.g. CC credentials Secret creation). An
+ // invalid name would otherwise reach K8s and its 422 would collapse to 500.
+ const nameIssue = validateResourceName(body.metadata?.name)
+ if (nameIssue) {
+ res.status(422).json(nameIssue)
+ return
+ }
+
+ // F0.3: identifier/display field validation for hosts + contexts (create =
+ // no ratchet; every present field is validated).
+ if ((plural === 'hosts' || plural === 'contexts') && body.spec) {
+ const fieldIssues = collectResourceSpecFieldIssues(plural, body.spec, null)
+ if (fieldIssues.length > 0) {
+ res.status(422).json({ errors: fieldIssues })
+ return
+ }
+ }
+
if (plural === 'communicationchannels' && body.spec) {
const errors = validateCommunicationChannelSpec(body.spec)
if (errors.length > 0) {
@@@ -607,15 -613,16 +699,24 @@@
return
}
}
+ if (
+ plural === 'contexts' &&
+ body.spec &&
+ contextDisplayNameNotPersisted(body.spec, created)
+ ) {
+ await rollbackPrunedContextCreate(gateway, body.metadata.name, ns)
+ sendPrunedDisplayNameError(res)
+ return
+ }
- res.status(201).json(created)
+ // Host CR persisted: attach any Fase 6 warnings additively (older clients
+ // ignore the field). Absent when there is nothing to warn about.
+ res
+ .status(201)
+ .json(
+ hostWarnings.length > 0
+ ? { ...(created as Record<string, unknown>), warnings: hostWarnings }
+ : created
+ )
})
)
@@@ -640,13 -647,39 +741,45 @@@
}
spec: Record<string, unknown>
}
+ // Pieza D tolerations granted during Host validation, emitted only after
+ // the CR persists (below). Empty for every non-host / non-tolerated write.
+ const hostTolerations: HostSpecIncoherenceToleratedEvent[] = []
+ // Fase 6 soft-quarantine warnings, returned in the 200 body only after the
+ // CR persists (below). Empty for every non-host / non-stale write.
+ const hostWarnings: StaleModelWarning[] = []
+ // 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 = null
+ try {
+ const current = (await gateway.getResource(plural, req.params.name, ns)) as
+ | (MutableResourceSnapshot & { spec?: Record<string, unknown> })
+ | null
+ currentSpec = 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.
+ // getResource wraps a namespaced 404 as K8sNotFoundError (httpStatus),
+ // which extractK8sStatusCode does not read, so check both.
+ if (!(err instanceof K8sNotFoundError) && extractK8sStatusCode(err) !== 404) throw err
+ }
+ const fieldIssues = collectResourceSpecFieldIssues(plural, body.spec, currentSpec)
+ if (fieldIssues.length > 0) {
+ res.status(422).json({ errors: fieldIssues })
+ return
+ }
+ }
+
if (plural === 'communicationchannels' && body.spec) {
const errors = validateCommunicationChannelSpec(body.spec)
if (errors.length > 0) {
@@@ -787,10 -759,13 +920,16 @@@
}
}
try {
- 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)
+ // Host CR persisted: NOW emit any Pieza D tolerations (never before the
+ // write lands, so a K8s conflict/error leaves no audit record).
+ for (const event of hostTolerations) emitHostSpecIncoherenceTolerated(event)
if (plural === 'communicationchannels' && body.spec) {
const missingField = missingPersistedProviderSetting(updateBody.spec, updated)
if (missingField) {
@@@ -805,13 -780,21 +944,27 @@@
return
}
}
+ if (
+ plural === 'contexts' &&
+ body.spec &&
+ contextDisplayNameNotPersisted(body.spec, updated)
+ ) {
+ // Deliberately NO spec restore (unlike the CC update rollback): the
+ // context's other spec fields persisted legitimately, and a PUT is
+ // idempotent — once the operator applies the CRD and replays the same
+ // request, spec.displayName round-trips and the whole spec converges.
+ // Restoring the prior spec would instead discard those legitimate
+ // edits. The loud 409 is the recovery signal.
+ sendPrunedDisplayNameError(res)
+ return
+ }
- res.status(200).json(updated)
+ res
+ .status(200)
+ .json(
+ hostWarnings.length > 0
+ ? { ...(updated as Record<string, unknown>), warnings: hostWarnings }
+ : updated
+ )
} catch (err) {
// AP-6: the caller sent the resourceVersion it read and the resource
// changed underneath it. Machine-readable so the UI can tell the