Skip to content

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

TypeScript

fix(hcc): bind preserved wake bootstraps

Alfredo Lopez02600fe5e145
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index 2d4c773c8..22477625c 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.362",
+ "version": "0.1.363",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.362",
+ "version": "0.1.363",
"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 75a8356e2..9a6cea2b6 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.362",
+ "version": "0.1.363",
"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/hostReconciler.ts b/host-context-controller/src/hostReconciler.ts
index ecb01b8c7..6a0d059b9 100644
--- a/host-context-controller/src/hostReconciler.ts
+++ b/host-context-controller/src/hostReconciler.ts
@@ -392,6 +392,10 @@ const RUNTIME_TOKEN_AUDIENCE_ANNOTATION = 'clerum.io/runtime-token-audience'
const RUNTIME_TOKEN_SCHEMA_VERSION_ANNOTATION = 'clerum.io/runtime-token-schema-version'
const RUNTIME_TOKEN_BOOTSTRAP_STATE_ANNOTATION = 'clerum.io/runtime-token-bootstrap-state'
const RUNTIME_TOKEN_ROLLOUT_REQUIRED_ANNOTATION = 'clerum.io/runtime-token-rollout-required'
+const RUNTIME_TOKEN_BOOTSTRAP_DEPLOYMENT_UID_ANNOTATION =
+ 'clerum.io/runtime-token-bootstrap-deployment-uid'
+const RUNTIME_TOKEN_BOOTSTRAP_APPLIED_REVISION_ANNOTATION =
+ 'clerum.io/runtime-token-bootstrap-applied-revision'
const RUNTIME_TOKEN_HAS_CHANNEL_INGRESS_ANNOTATION = 'clerum.io/runtime-token-has-channel-ingress'
const RUNTIME_TOKEN_FRONTS_OAUTH_ANNOTATION = 'clerum.io/runtime-token-fronts-oauth-server'
const RUNTIME_TOKEN_ISSUER = 'control-api'
@@ -501,6 +505,7 @@ type BootstrapOptions = {
forceFreshForWake?: boolean
targetSuspended?: boolean
refreshGfsOnly?: boolean
+ preserveDeploymentTemplateOnWake?: boolean
}
type RuntimeTokenProvision = {
@@ -1561,6 +1566,24 @@ export class HostReconciler {
return (deployment?.status?.readyReplicas ?? 0) > 0
}
+ private static bootstrapBindingMatchesDeployment(
+ record: Pick<k8s.V1Secret, 'metadata'>,
+ deployment: k8s.V1Deployment | null
+ ): boolean {
+ const deploymentIdentity = deployment?.metadata?.uid
+ const boundDeploymentIdentity =
+ record.metadata?.annotations?.[RUNTIME_TOKEN_BOOTSTRAP_DEPLOYMENT_UID_ANNOTATION]
+ const appliedRevision = HostReconciler.deploymentRuntimeTokenRevision(deployment)
+ const boundAppliedRevision =
+ record.metadata?.annotations?.[RUNTIME_TOKEN_BOOTSTRAP_APPLIED_REVISION_ANNOTATION]
+ return (
+ !!deploymentIdentity &&
+ boundDeploymentIdentity === deploymentIdentity &&
+ appliedRevision !== '' &&
+ boundAppliedRevision === appliedRevision
+ )
+ }
+
private static credentialRecordBelongsToHost(
record: { metadata?: { labels?: Record<string, string> } },
host: HostCRD
@@ -1622,11 +1645,16 @@ export class HostReconciler {
* readiness 503s forever, and the pod never becomes Ready. Reuse is only
* safe for a pod that stays up and never re-reads the Secret (it uses its
* in-memory rotated tokens), which is exactly deploymentReady && replicas>=1.
+ *
+ * A wake bootstrap already bound to this exact Deployment UID and applied
+ * runtime revision is the one startup exception: its replica-only wake starts
+ * that Deployment with the newly mounted material. Any identity or revision
+ * change must mint again.
*/
private static deploymentNeedsFreshBootstrap(
deployment: k8s.V1Deployment | null,
currentRevision: string,
- bootstrapIsFresh: boolean,
+ credentialRecord: k8s.V1Secret | null,
options: BootstrapOptions
): boolean {
if (options.forceFreshForWake || !deployment) return true
@@ -1635,8 +1663,16 @@ export class HostReconciler {
if (options.targetSuspended === true) return false
if ((deployment.spec?.replicas ?? 1) === 0) return true
if (HostReconciler.deploymentReady(deployment)) return false
+ if (
+ credentialRecord?.metadata?.annotations?.[RUNTIME_TOKEN_BOOTSTRAP_STATE_ANNOTATION] ===
+ RUNTIME_TOKEN_BOOTSTRAP_STATE_FRESH &&
+ HostReconciler.bootstrapBindingMatchesDeployment(credentialRecord, deployment)
+ ) {
+ return false
+ }
return (
- !bootstrapIsFresh ||
+ credentialRecord?.metadata?.annotations?.[RUNTIME_TOKEN_BOOTSTRAP_STATE_ANNOTATION] !==
+ RUNTIME_TOKEN_BOOTSTRAP_STATE_FRESH ||
HostReconciler.deploymentRuntimeTokenRevision(deployment) !== currentRevision
)
}
@@ -1829,11 +1865,16 @@ export class HostReconciler {
? HostReconciler.runtimeTokenSecretRevisionFromSecret(existing)
: null
const nowMs = Date.now()
+ const preserveWakeTemplate =
+ options.preserveDeploymentTemplateOnWake === true &&
+ !!deployment &&
+ HostReconciler.deploymentBelongsToHost(deployment, host) &&
+ (deployment.spec?.replicas ?? 1) === 0 &&
+ HostReconciler.deploymentRuntimeTokenRevision(deployment) !== ''
if (options.refreshGfsOnly) {
const trustedDeployment =
HostReconciler.deploymentBelongsToHost(deployment, host) &&
- (deployment?.spec?.replicas ?? 0) > 0 &&
- HostReconciler.deploymentReady(deployment)
+ (deployment?.spec?.replicas ?? 0) > 0
if (
!existing ||
!HostReconciler.credentialRecordBelongsToHost(existing, host) ||
@@ -1851,6 +1892,37 @@ export class HostReconciler {
)
return null
}
+ if (
+ existing.metadata?.annotations?.[RUNTIME_TOKEN_BOOTSTRAP_STATE_ANNOTATION] ===
+ RUNTIME_TOKEN_BOOTSTRAP_STATE_FRESH &&
+ HostReconciler.bootstrapBindingMatchesDeployment(existing, deployment)
+ ) {
+ const appliedRevision = HostReconciler.deploymentRuntimeTokenRevision(deployment)
+ if (HostReconciler.deploymentReady(deployment)) {
+ await this.coreApi.replaceNamespacedSecret({
+ name,
+ namespace: host.namespace,
+ body: {
+ ...existing,
+ metadata: {
+ ...existing.metadata,
+ annotations: {
+ ...(existing.metadata?.annotations ?? {}),
+ [RUNTIME_TOKEN_BOOTSTRAP_STATE_ANNOTATION]:
+ RUNTIME_TOKEN_BOOTSTRAP_STATE_CONSUMED,
+ [RUNTIME_TOKEN_ROLLOUT_REQUIRED_ANNOTATION]: 'false',
+ },
+ },
+ },
+ })
+ }
+ return {
+ revision: appliedRevision || existingRevision || '',
+ scopeHash:
+ existing.metadata?.annotations?.[RUNTIME_TOKEN_SCOPE_HASH_ANNOTATION] ?? '',
+ }
+ }
+ if (!HostReconciler.deploymentReady(deployment)) return null
if (
!HostReconciler.credentialRenewalWindowReached(
existing,
@@ -2012,7 +2084,7 @@ export class HostReconciler {
HostReconciler.deploymentNeedsFreshBootstrap(
deployment,
existingRevision,
- bootstrapIsFresh,
+ existing,
options
)
) {
@@ -2030,6 +2102,13 @@ export class HostReconciler {
const deploymentRevision = HostReconciler.deploymentRuntimeTokenRevision(deployment)
const rolloutMarker =
existing.metadata?.annotations?.[RUNTIME_TOKEN_ROLLOUT_REQUIRED_ANNOTATION]
+ const bootstrapBoundToAppliedDeployment =
+ HostReconciler.bootstrapBindingMatchesDeployment(existing, deployment)
+ const bootstrapPendingForAppliedDeployment =
+ bootstrapIsFresh && bootstrapBoundToAppliedDeployment
+ const bootstrapConsumedForAppliedDeployment =
+ existing.metadata?.annotations?.[RUNTIME_TOKEN_BOOTSTRAP_STATE_ANNOTATION] ===
+ RUNTIME_TOKEN_BOOTSTRAP_STATE_CONSUMED && bootstrapBoundToAppliedDeployment
const rolloutPending =
rolloutMarker === 'true' ||
(rolloutMarker === undefined &&
@@ -2057,6 +2136,11 @@ export class HostReconciler {
) {
annotationUpdates[GFS_TOKEN_HOST_GENERATION_ANNOTATION] = String(host.generation)
}
+ if (bootstrapPendingForAppliedDeployment && HostReconciler.deploymentReady(deployment)) {
+ annotationUpdates[RUNTIME_TOKEN_BOOTSTRAP_STATE_ANNOTATION] =
+ RUNTIME_TOKEN_BOOTSTRAP_STATE_CONSUMED
+ annotationUpdates[RUNTIME_TOKEN_ROLLOUT_REQUIRED_ANNOTATION] = 'false'
+ }
if (
bootstrapIsFresh &&
HostReconciler.deploymentReady(deployment) &&
@@ -2094,7 +2178,10 @@ export class HostReconciler {
})
this.recordGfsLifecycleEvidence(host, 'reused')
const selectedRevision =
- rolloutPending || HostReconciler.shouldRollForRuntimeSecret(deployment, false)
+ rolloutPending ||
+ (!bootstrapPendingForAppliedDeployment &&
+ !bootstrapConsumedForAppliedDeployment &&
+ HostReconciler.shouldRollForRuntimeSecret(deployment, false))
? existingRevision
: deploymentRevision || existingRevision
return {
@@ -2145,8 +2232,9 @@ export class HostReconciler {
throw new Error('mcp-host-runtime-token Secret body missing required credential keys')
}
const rolloutRequired =
- !existing ||
- HostReconciler.shouldRollForRuntimeSecret(deployment, decision.rolloutRequired)
+ !preserveWakeTemplate &&
+ (!existing ||
+ HostReconciler.shouldRollForRuntimeSecret(deployment, decision.rolloutRequired))
body.metadata = {
...body.metadata,
annotations: {
@@ -2165,6 +2253,14 @@ export class HostReconciler {
),
[RUNTIME_TOKEN_BOOTSTRAP_STATE_ANNOTATION]: RUNTIME_TOKEN_BOOTSTRAP_STATE_FRESH,
[RUNTIME_TOKEN_ROLLOUT_REQUIRED_ANNOTATION]: rolloutRequired ? 'true' : 'false',
+ ...(preserveWakeTemplate && deployment
+ ? {
+ [RUNTIME_TOKEN_BOOTSTRAP_DEPLOYMENT_UID_ANNOTATION]:
+ deployment.metadata?.uid ?? '',
+ [RUNTIME_TOKEN_BOOTSTRAP_APPLIED_REVISION_ANNOTATION]:
+ HostReconciler.deploymentRuntimeTokenRevision(deployment),
+ }
+ : {}),
[RUNTIME_TOKEN_HAS_CHANNEL_INGRESS_ANNOTATION]: String(hasChannelIngress),
[RUNTIME_TOKEN_FRONTS_OAUTH_ANNOTATION]: String(observedFrontsOAuth),
},
@@ -2181,7 +2277,12 @@ export class HostReconciler {
resourceName: name,
reason: decision.reason,
})
- return { revision, scopeHash }
+ return {
+ revision: preserveWakeTemplate
+ ? HostReconciler.deploymentRuntimeTokenRevision(deployment) || revision
+ : revision,
+ scopeHash,
+ }
}
const replaceBody = {
@@ -2205,9 +2306,11 @@ export class HostReconciler {
rolloutRequired,
})
return {
- revision: rolloutRequired
- ? revision
- : HostReconciler.deploymentRuntimeTokenRevision(deployment) || revision,
+ revision: preserveWakeTemplate
+ ? HostReconciler.deploymentRuntimeTokenRevision(deployment) || revision
+ : rolloutRequired
+ ? revision
+ : HostReconciler.deploymentRuntimeTokenRevision(deployment) || revision,
scopeHash,
}
} catch (err) {
@@ -4984,16 +5087,25 @@ export class HostReconciler {
}
const holdAppliedRuntime = async (wakeRequested: boolean): Promise<void> => {
const liveDeployment = await this.readHostDeploymentOrNull(host)
- const deploymentIdentityIsTrusted =
- !liveDeployment || HostReconciler.deploymentBelongsToHost(liveDeployment, host)
+ const preservedWakeTarget =
+ HostReconciler.deploymentBelongsToHost(liveDeployment, host) &&
+ (liveDeployment?.spec?.replicas ?? 1) === 0 &&
+ HostReconciler.deploymentRuntimeTokenRevision(liveDeployment) !== ''
const runtimeIsReady =
!!liveDeployment &&
HostReconciler.deploymentBelongsToHost(liveDeployment, host) &&
HostReconciler.deploymentReady(liveDeployment)
- const scaleUpForRecovery =
- lifecycle.effective.state === 'active' &&
- (!liveDeployment || (liveDeployment.spec?.replicas ?? 1) === 0)
- const allowScaleUp = deploymentIdentityIsTrusted && (wakeRequested || scaleUpForRecovery)
+ const scaleUpForRecovery = lifecycle.effective.state === 'active' && preservedWakeTarget
+ const allowScaleUp = preservedWakeTarget && (wakeRequested || scaleUpForRecovery)
+ if (!liveDeployment || ((wakeRequested || scaleUpForRecovery) && !preservedWakeTarget)) {
+ this.setStatus(host.name, {
+ deployed: !!liveDeployment,
+ ready: false,
+ message:
+ 'Waiting for a verified applied runtime before waking during CommunicationChannel inventory loss',
+ })
+ return
+ }
let provisioningRequired = false
if (allowScaleUp) {
// A pod starting from zero will consume bootstrap credentials. Mint and
@@ -5018,6 +5130,8 @@ export class HostReconciler {
const provision = await this.provisionRuntimeTokenRevision(host, {
forceFreshForWake: true,
targetSuspended: lifecycle.effective.state === 'suspended',
+ preserveDeploymentTemplateOnWake:
+ !!liveDeployment && (liveDeployment.spec?.replicas ?? 1) === 0,
})
if (!provision) {
this.setStatus(host.name, {
diff --git a/host-context-controller/test/hostReconciler.lifecycle.test.ts b/host-context-controller/test/hostReconciler.lifecycle.test.ts
index ce5cc1bcf..80d3d0692 100644
--- a/host-context-controller/test/hostReconciler.lifecycle.test.ts
+++ b/host-context-controller/test/hostReconciler.lifecycle.test.ts
@@ -296,6 +296,33 @@ function markPersistedRuntimeTrusted(
return deployment
}
+function encodedRuntimeRefreshMaterial(expiresAtMs: number): string {
+ const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url')
+ return [
+ encode({ alg: 'none', typ: 'JWT' }),
+ encode({ exp: Math.floor(expiresAtMs / 1000) }),
+ 'test-signature',
+ ].join('.')
+}
+
+function withReadableRuntimeRefreshMaterial(record: k8s.V1Secret): k8s.V1Secret {
+ const material =
+ (
+ HostReconciler as unknown as {
+ runtimeTokenSecretData(record: k8s.V1Secret): Record<string, string> | null
+ }
+ ).runtimeTokenSecretData(record) ?? {}
+ return {
+ ...record,
+ data: {
+ ...material,
+ [MCP_HOST_RUNTIME_TOKEN_SECRET_REFRESH_KEY]: Buffer.from(
+ encodedRuntimeRefreshMaterial(Date.now() + 3_600_000)
+ ).toString('base64'),
+ },
+ }
+}
+
/** Model persisted API state so a loss-of-authority test cannot invent a template. */
function persistHostDeployment(appsApi: MockAppsApi, host: HostCRD, initial: k8s.V1Deployment) {
let live = structuredClone(initial)
@@ -938,12 +965,14 @@ describe('HostReconciler stateless lifecycle — rejection matrix', () => {
appsApi.readNamespacedDeployment.mockImplementation(request =>
request.name === host.name ? Promise.reject({ code: 404 }) : read(request)
)
+ vi.mocked(issueMcpHostRuntimeTokens).mockClear()
await reconciler.reconcile(host)
expect(
appsApi.createNamespacedDeployment.mock.calls.filter(
([r]) => r.body.metadata.name === host.name
)
).toHaveLength(0)
+ expect(vi.mocked(issueMcpHostRuntimeTokens)).not.toHaveBeenCalled()
expect(reconciler.getStatus(host.name)).toMatchObject({ deployed: false, ready: false })
synced = true
await reconciler.reconcile(host)
@@ -1137,6 +1166,12 @@ describe('HostReconciler stateless lifecycle — rejection matrix', () => {
})
customApi.getNamespacedCustomObject.mockImplementation(async () => hostApiObject(host))
const applied = createReconciler().reconciler.buildDeployment(host)
+ applied.spec!.replicas = 0
+ applied.spec!.template!.metadata!.annotations = {
+ ...applied.spec!.template!.metadata!.annotations,
+ 'clerum.io/runtime-token-revision': 'applied-runtime-revision',
+ }
+ markPersistedRuntimeTrusted(applied, host, 0)
const live = persistHostDeployment(appsApi, host, applied)
const provision = vi
.spyOn(reconciler as any, 'provisionRuntimeTokenRevision')
@@ -1146,6 +1181,7 @@ describe('HostReconciler stateless lifecycle — rejection matrix', () => {
expect(provision).toHaveBeenCalledWith(host, {
forceFreshForWake: true,
targetSuspended: false,
+ preserveDeploymentTemplateOnWake: true,
})
expect(provision.mock.invocationCallOrder[0]).toBeLessThan(
appsApi.replaceNamespacedDeployment.mock.invocationCallOrder.at(-1)!
@@ -1448,6 +1484,11 @@ describe('HostReconciler stateless lifecycle — rejection matrix', () => {
const applied = reconciler.buildDeployment(host)
applied.spec!.replicas = 0
applied.spec!.template.metadata!.annotations = { 'example.org/applied': 'keep-exactly' }
+ applied.spec!.template!.metadata!.annotations = {
+ ...applied.spec!.template!.metadata!.annotations,
+ 'clerum.io/runtime-token-revision': 'applied-runtime-revision',
+ }
+ markPersistedRuntimeTrusted(applied, host, 0)
const live = persistHostDeployment(appsApi, host, applied)
const provision = vi
.spyOn(reconciler as any, 'provisionRuntimeTokenRevision')
@@ -1459,6 +1500,7 @@ describe('HostReconciler stateless lifecycle — rejection matrix', () => {
expect(provision).toHaveBeenCalledWith(host, {
forceFreshForWake: true,
targetSuspended: false,
+ preserveDeploymentTemplateOnWake: true,
})
expect(provision.mock.invocationCallOrder[0]).toBeLessThan(
appsApi.replaceNamespacedDeployment.mock.invocationCallOrder.at(-1)!
@@ -1467,6 +1509,135 @@ describe('HostReconciler stateless lifecycle — rejection matrix', () => {
expect(live().spec!.template).toEqual(applied.spec!.template)
})
+ it('does not roll a held wake bootstrap when the watch recovers before Ready', async () => {
+ let cacheSynced = false
+ const host = makeStatelessHost({
+ status: { lifecycle: { state: 'active', wakeHandledGeneration: 1 } },
+ })
+ const { reconciler, appsApi, coreApi, customApi } = createReconciler({
+ isCommunicationChannelCacheSynced: () => cacheSynced,
+ })
+ customApi.getNamespacedCustomObject.mockImplementation(async () => hostApiObject(host))
+ let runtimeRecord = runtimeCredentialRecord(host)
+ coreApi.readNamespacedSecret.mockImplementation(({ name }) =>
+ name?.includes('runtime-tokens')
+ ? Promise.resolve(runtimeRecord)
+ : Promise.resolve({ metadata: { resourceVersion: '1' }, data: {} } as any)
+ )
+ const applied = reconciler.buildDeployment(host)
+ applied.spec!.replicas = 0
+ applied.spec!.template!.metadata!.annotations = {
+ ...applied.spec!.template!.metadata!.annotations,
+ 'clerum.io/runtime-token-revision': 'applied-runtime-revision',
+ }
+ markPersistedRuntimeTrusted(applied, host, 0)
+ const live = persistHostDeployment(appsApi, host, applied)
+ const issueRuntimeMaterial = vi.mocked(issueMcpHostRuntimeTokens)
+ issueRuntimeMaterial.mockClear()
+
+ await reconciler.reconcile(host)
+
+ expect(issueRuntimeMaterial).toHaveBeenCalledOnce()
+ expect(live().spec!.replicas).toBe(1)
+ expect(live().spec!.template!.metadata!.annotations?.['clerum.io/runtime-token-revision']).toBe(
+ 'applied-runtime-revision'
+ )
+ const wakeWrite = coreApi.replaceNamespacedSecret.mock.calls.find(([request]) =>
+ request.name?.includes('runtime-tokens')
+ )?.[0].body as k8s.V1Secret
+ expect(wakeWrite.metadata?.annotations).toMatchObject({
+ 'clerum.io/runtime-token-bootstrap-state': 'fresh',
+ 'clerum.io/runtime-token-rollout-required': 'false',
+ 'clerum.io/runtime-token-bootstrap-deployment-uid': 'deployment-uid',
+ 'clerum.io/runtime-token-bootstrap-applied-revision': 'applied-runtime-revision',
+ })
+ runtimeRecord = withReadableRuntimeRefreshMaterial(wakeWrite)
+ issueRuntimeMaterial.mockClear()
+ appsApi.createNamespacedDeployment.mockClear()
+ appsApi.replaceNamespacedDeployment.mockClear()
+ cacheSynced = true
+ await reconciler.reconcile(host)
+
+ expect(issueRuntimeMaterial).not.toHaveBeenCalled()
+ expect(
+ appsApi.replaceNamespacedDeployment.mock.calls.filter(
+ ([request]) => request.body?.metadata?.name === host.name
+ )
+ ).toHaveLength(0)
+
+ live().status = { readyReplicas: 1 }
+ coreApi.replaceNamespacedSecret.mockClear()
+ await reconciler.reconcile(host)
+
+ expect(issueRuntimeMaterial).not.toHaveBeenCalled()
+ expect(
+ appsApi.replaceNamespacedDeployment.mock.calls.filter(
+ ([request]) => request.body?.metadata?.name === host.name
+ )
+ ).toHaveLength(0)
+ const consumedWrite = coreApi.replaceNamespacedSecret.mock.calls.find(([request]) =>
+ request.name?.includes('runtime-tokens')
+ )?.[0].body as k8s.V1Secret
+ expect(consumedWrite.metadata?.annotations?.['clerum.io/runtime-token-bootstrap-state']).toBe(
+ 'consumed'
+ )
+ runtimeRecord = consumedWrite
+
+ live().spec!.replicas = 0
+ live().status = { readyReplicas: 0 }
+ issueRuntimeMaterial.mockClear()
+ await reconciler.reconcile(host)
+
+ expect(issueRuntimeMaterial).toHaveBeenCalledOnce()
+ expect(live().spec!.replicas).toBe(1)
+ })
+
+ it('marks a newly created preserved wake bootstrap for scale-only delivery', async () => {
+ const host = makeStatelessHost()
+ const { reconciler, appsApi, coreApi } = createReconciler()
+ const deployment = trustedRuntimeDeployment(reconciler, host, {
+ replicas: 0,
+ readyReplicas: 0,
+ })
+ deployment.spec!.template!.metadata!.annotations = {
+ ...deployment.spec!.template!.metadata!.annotations,
+ 'clerum.io/runtime-token-revision': 'applied-runtime-revision',
+ }
+ appsApi.readNamespacedDeployment.mockResolvedValue(deployment)
+ coreApi.readNamespacedSecret.mockRejectedValue({ code: 404 })
+ const issueRuntimeMaterial = vi.mocked(issueMcpHostRuntimeTokens)
+ issueRuntimeMaterial.mockClear()
+
+ const first = await (reconciler as any).ensureMcpHostRuntimeTokenSecret(host, {
+ forceFreshForWake: true,
+ targetSuspended: false,
+ preserveDeploymentTemplateOnWake: true,
+ })
+
+ expect(first.revision).toBe('applied-runtime-revision')
+ const created = coreApi.createNamespacedSecret.mock.calls.find(([request]) =>
+ request.body?.metadata?.name?.includes('runtime-tokens')
+ )?.[0].body as k8s.V1Secret
+ expect(created.metadata?.annotations).toMatchObject({
+ 'clerum.io/runtime-token-bootstrap-state': 'fresh',
+ 'clerum.io/runtime-token-rollout-required': 'false',
+ 'clerum.io/runtime-token-bootstrap-applied-revision': 'applied-runtime-revision',
+ })
+
+ const readableCreated = withReadableRuntimeRefreshMaterial(created)
+ coreApi.readNamespacedSecret.mockResolvedValue(readableCreated)
+ deployment.spec!.replicas = 1
+ issueRuntimeMaterial.mockClear()
+
+ const second = await (reconciler as any).ensureMcpHostRuntimeTokenSecret(host, {
+ targetSuspended: false,
+ preserveDeploymentTemplateOnWake: true,
+ })
+
+ expect(issueRuntimeMaterial).not.toHaveBeenCalled()
+ expect(second.revision).toBe('applied-runtime-revision')
+ })
+
it('keeps an active held Deployment at zero when credential minting is unavailable', async () => {
const host = makeStatelessHost({
status: { lifecycle: { state: 'active', wakeHandledGeneration: 1 } },
@@ -1477,6 +1648,10 @@ describe('HostReconciler stateless lifecycle — rejection matrix', () => {
customApi.getNamespacedCustomObject.mockImplementation(async () => hostApiObject(host))
const applied = reconciler.buildDeployment(host)
applied.spec!.replicas = 0
+ applied.spec!.template!.metadata!.annotations = {
+ ...applied.spec!.template!.metadata!.annotations,
+ 'clerum.io/runtime-token-revision': 'applied-runtime-revision',
+ }
markPersistedRuntimeTrusted(applied, host, 0)
const live = persistHostDeployment(appsApi, host, applied)
appsApi.replaceNamespacedDeployment.mockClear()
@@ -1490,6 +1665,7 @@ describe('HostReconciler stateless lifecycle — rejection matrix', () => {
expect(provision).toHaveBeenCalledWith(host, {
forceFreshForWake: true,
targetSuspended: false,
+ preserveDeploymentTemplateOnWake: true,
})
expect(appsApi.replaceNamespacedDeployment).not.toHaveBeenCalled()
expect(live().spec!.replicas).toBe(0)