Skip to content

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

TypeScript

fix(wrc): repair empty contextId and invalid recipe ownerRefs before no-op

Alfredo Lopeza6afc9f1fba0
diff --git a/workflow-recipes/package-lock.json b/workflow-recipes/package-lock.json
index aba5d2c98..dacbf81f8 100644
--- a/workflow-recipes/package-lock.json
+++ b/workflow-recipes/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "workload-recipes-controller",
- "version": "0.1.324",
+ "version": "0.1.325",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "workload-recipes-controller",
- "version": "0.1.324",
+ "version": "0.1.325",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@aws-sdk/client-s3": "^3.1009.0",
diff --git a/workflow-recipes/package.json b/workflow-recipes/package.json
index 62ddd2793..4c29e419b 100644
--- a/workflow-recipes/package.json
+++ b/workflow-recipes/package.json
@@ -1,6 +1,6 @@
{
"name": "workload-recipes-controller",
- "version": "0.1.324",
+ "version": "0.1.325",
"description": "Workload Recipes Controller (WRC) - WorkloadRecipe lifecycle management and MCP server interface",
"main": "dist/main.js",
"engines": {
diff --git a/workflow-recipes/src/reconciler/mcpDelegation.test.ts b/workflow-recipes/src/reconciler/mcpDelegation.test.ts
index fb897e774..5f7443ff6 100644
--- a/workflow-recipes/src/reconciler/mcpDelegation.test.ts
+++ b/workflow-recipes/src/reconciler/mcpDelegation.test.ts
@@ -1310,12 +1310,15 @@ describe('ensureRecipeContext', () => {
})
it('skips private Context replace when mcpServers and WRC labels already match', async () => {
+ // Helper-compatibility path: omitted recipeNamespace means same-ns ownership.
+ // A valid private no-op must already carry this recipe's ownerRef.
const mockCustomApi = {
createNamespacedCustomObject: vi.fn().mockRejectedValueOnce({ code: 409 }),
getNamespacedCustomObject: vi.fn().mockResolvedValue({
metadata: {
resourceVersion: '3',
labels: { 'clerum.io/recipe': 'test-recipe', 'clerum.io/managed-by': 'wrc' },
+ ownerReferences: [ownerRef],
},
spec: { contextId: 'wf-test-recipe', mcpServers: ['server-a', 'server-b'] },
}),
@@ -1339,6 +1342,35 @@ describe('ensureRecipeContext', () => {
expect(mockCustomApi.replaceNamespacedCustomObject).not.toHaveBeenCalled()
})
+ it('skips canonical private Context replace when servers and labels match without a recipe ownerRef', async () => {
+ const mockCustomApi = {
+ createNamespacedCustomObject: vi.fn().mockRejectedValueOnce({ code: 409 }),
+ getNamespacedCustomObject: vi.fn().mockResolvedValue({
+ metadata: {
+ resourceVersion: '3',
+ labels: { 'clerum.io/recipe': 'test-recipe', 'clerum.io/managed-by': 'wrc' },
+ },
+ spec: { contextId: 'wf-test-recipe', mcpServers: ['server-a', 'server-b'] },
+ }),
+ replaceNamespacedCustomObject: vi.fn().mockResolvedValue({}),
+ }
+ const deps: DelegationDeps = {
+ customApi: mockCustomApi as unknown as DelegationDeps['customApi'],
+ coreApi: {} as DelegationDeps['coreApi'],
+ }
+
+ await ensureRecipeContext(
+ deps,
+ 'test-recipe',
+ ['server-a', 'server-b'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+
+ expect(mockCustomApi.replaceNamespacedCustomObject).not.toHaveBeenCalled()
+ })
+
it('writes a private Context when the recipe label does not match', async () => {
const mockCustomApi = {
createNamespacedCustomObject: vi.fn().mockRejectedValueOnce({ code: 409 }),
@@ -1374,6 +1406,279 @@ describe('ensureRecipeContext', () => {
)
})
+ function contextApiWithLiveState(initial: {
+ metadata?: Record<string, unknown>
+ spec?: Record<string, unknown>
+ }) {
+ let live = structuredClone(initial)
+ const mockCustomApi = {
+ createNamespacedCustomObject: vi.fn().mockRejectedValue({ code: 409 }),
+ getNamespacedCustomObject: vi
+ .fn()
+ .mockImplementation(() => Promise.resolve(structuredClone(live))),
+ replaceNamespacedCustomObject: vi.fn().mockImplementation((args: { body: typeof live }) => {
+ const currentRv = Number(
+ (live.metadata as { resourceVersion?: string } | undefined)?.resourceVersion ?? '0'
+ )
+ live = {
+ ...args.body,
+ metadata: {
+ ...((args.body.metadata as object | undefined) ?? {}),
+ resourceVersion: String(currentRv + 1),
+ },
+ }
+ return Promise.resolve({})
+ }),
+ }
+ const deps: DelegationDeps = {
+ customApi: mockCustomApi as unknown as DelegationDeps['customApi'],
+ coreApi: {} as DelegationDeps['coreApi'],
+ }
+ return { mockCustomApi, deps, liveOf: () => live }
+ }
+
+ it('repairs a shared empty contextId once, then skips the next reconcile', async () => {
+ const recipe = makeRecipe({ spec: { ...makeRecipe().spec, contextRef: 'context1' } })
+ const { mockCustomApi, deps, liveOf } = contextApiWithLiveState({
+ metadata: {
+ resourceVersion: '9',
+ labels: {
+ 'clerum.io/recipe': 'other-recipe',
+ 'clerum.io/managed-by': 'wrc',
+ 'clerum.io/context-owner': 'admin',
+ },
+ },
+ spec: {
+ contextId: '',
+ mcpServers: ['existing-server', 'test-recipe-redis-mcp'],
+ },
+ })
+
+ await ensureRecipeContext(
+ deps,
+ recipe,
+ ['test-recipe-redis-mcp'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+ await ensureRecipeContext(
+ deps,
+ recipe,
+ ['test-recipe-redis-mcp'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+
+ expect(mockCustomApi.replaceNamespacedCustomObject).toHaveBeenCalledTimes(1)
+ expect(liveOf().spec?.contextId).toBe('context1')
+ })
+
+ it('repairs a private empty contextId once, then skips the next reconcile', async () => {
+ const { mockCustomApi, deps, liveOf } = contextApiWithLiveState({
+ metadata: {
+ resourceVersion: '3',
+ labels: { 'clerum.io/recipe': 'test-recipe', 'clerum.io/managed-by': 'wrc' },
+ ownerReferences: [ownerRef],
+ },
+ spec: { contextId: '', mcpServers: ['server-a', 'server-b'] },
+ })
+
+ await ensureRecipeContext(deps, 'test-recipe', ['server-a', 'server-b'], 'mcp-server', ownerRef)
+ await ensureRecipeContext(deps, 'test-recipe', ['server-a', 'server-b'], 'mcp-server', ownerRef)
+
+ expect(mockCustomApi.replaceNamespacedCustomObject).toHaveBeenCalledTimes(1)
+ expect(liveOf().spec?.contextId).toBe('wf-test-recipe')
+ })
+
+ it('keeps a non-empty contextId that differs from metadata.name and does not write', async () => {
+ const recipe = makeRecipe({ spec: { ...makeRecipe().spec, contextRef: 'context1' } })
+ const mockCustomApi = {
+ createNamespacedCustomObject: vi.fn().mockRejectedValueOnce({ code: 409 }),
+ getNamespacedCustomObject: vi.fn().mockResolvedValue({
+ metadata: {
+ resourceVersion: '9',
+ labels: { 'clerum.io/managed-by': 'wrc', 'clerum.io/recipe': 'other-recipe' },
+ },
+ spec: {
+ contextId: 'legacy-shared-id',
+ mcpServers: ['existing-server', 'test-recipe-redis-mcp'],
+ },
+ }),
+ replaceNamespacedCustomObject: vi.fn().mockResolvedValue({}),
+ }
+ const deps: DelegationDeps = {
+ customApi: mockCustomApi as unknown as DelegationDeps['customApi'],
+ coreApi: {} as DelegationDeps['coreApi'],
+ }
+
+ await ensureRecipeContext(
+ deps,
+ recipe,
+ ['test-recipe-redis-mcp'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+
+ expect(mockCustomApi.replaceNamespacedCustomObject).not.toHaveBeenCalled()
+ })
+
+ it('strips a stale cross-namespace recipe ownerRef from a canonical private Context once', async () => {
+ const { mockCustomApi, deps, liveOf } = contextApiWithLiveState({
+ metadata: {
+ resourceVersion: '3',
+ labels: { 'clerum.io/recipe': 'test-recipe', 'clerum.io/managed-by': 'wrc' },
+ ownerReferences: [ownerRef],
+ },
+ spec: { contextId: 'wf-test-recipe', mcpServers: ['server-a', 'server-b'] },
+ })
+
+ await ensureRecipeContext(
+ deps,
+ 'test-recipe',
+ ['server-a', 'server-b'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+ await ensureRecipeContext(
+ deps,
+ 'test-recipe',
+ ['server-a', 'server-b'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+
+ expect(mockCustomApi.replaceNamespacedCustomObject).toHaveBeenCalledTimes(1)
+ expect(liveOf().metadata).not.toHaveProperty('ownerReferences')
+ })
+
+ it('repairs empty contextId and a stale cross-namespace ownerRef in one write', async () => {
+ const { mockCustomApi, deps, liveOf } = contextApiWithLiveState({
+ metadata: {
+ resourceVersion: '3',
+ labels: { 'clerum.io/recipe': 'test-recipe', 'clerum.io/managed-by': 'wrc' },
+ ownerReferences: [ownerRef],
+ },
+ spec: { contextId: '', mcpServers: ['server-a', 'server-b'] },
+ })
+
+ await ensureRecipeContext(
+ deps,
+ 'test-recipe',
+ ['server-a', 'server-b'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+ await ensureRecipeContext(
+ deps,
+ 'test-recipe',
+ ['server-a', 'server-b'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+
+ expect(mockCustomApi.replaceNamespacedCustomObject).toHaveBeenCalledTimes(1)
+ expect(liveOf().spec?.contextId).toBe('wf-test-recipe')
+ expect(liveOf().metadata).not.toHaveProperty('ownerReferences')
+ })
+
+ it('preserves foreign ownerReferences and finalizers on a necessary Context write', async () => {
+ const foreignOwner = {
+ apiVersion: 'example.com/v1',
+ kind: 'BackupOwner',
+ name: 'backup',
+ uid: 'uid-foreign',
+ }
+ const recipe = makeRecipe({ spec: { ...makeRecipe().spec, contextRef: 'context1' } })
+ const mockCustomApi = {
+ createNamespacedCustomObject: vi.fn().mockRejectedValueOnce({ code: 409 }),
+ getNamespacedCustomObject: vi.fn().mockResolvedValue({
+ metadata: {
+ resourceVersion: '9',
+ labels: { 'clerum.io/recipe': 'other-recipe' },
+ ownerReferences: [foreignOwner],
+ finalizers: ['example.com/protect'],
+ },
+ spec: {
+ contextId: 'context1',
+ mcpServers: ['existing-server', 'test-recipe-redis-mcp'],
+ },
+ }),
+ replaceNamespacedCustomObject: vi.fn().mockResolvedValue({}),
+ }
+ const deps: DelegationDeps = {
+ customApi: mockCustomApi as unknown as DelegationDeps['customApi'],
+ coreApi: {} as DelegationDeps['coreApi'],
+ }
+
+ await ensureRecipeContext(
+ deps,
+ recipe,
+ ['test-recipe-redis-mcp'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+
+ const replaceCall = mockCustomApi.replaceNamespacedCustomObject.mock.calls[0]?.[0] as {
+ body?: { metadata?: { ownerReferences?: unknown; finalizers?: string[] } }
+ }
+ expect(replaceCall.body?.metadata?.ownerReferences).toEqual([foreignOwner])
+ expect(replaceCall.body?.metadata?.finalizers).toEqual(['example.com/protect'])
+ })
+
+ it('does not write a shared Context when only foreign metadata is present', async () => {
+ const recipe = makeRecipe({ spec: { ...makeRecipe().spec, contextRef: 'context1' } })
+ const mockCustomApi = {
+ createNamespacedCustomObject: vi.fn().mockRejectedValueOnce({ code: 409 }),
+ getNamespacedCustomObject: vi.fn().mockResolvedValue({
+ metadata: {
+ resourceVersion: '9',
+ labels: {
+ 'clerum.io/recipe': 'other-recipe',
+ 'clerum.io/managed-by': 'wrc',
+ },
+ annotations: { 'operator.io/note': 'keep' },
+ ownerReferences: [
+ {
+ apiVersion: 'example.com/v1',
+ kind: 'BackupOwner',
+ name: 'backup',
+ uid: 'uid-foreign',
+ },
+ ],
+ finalizers: ['example.com/protect'],
+ },
+ spec: {
+ contextId: 'context1',
+ mcpServers: ['existing-server', 'test-recipe-redis-mcp'],
+ },
+ }),
+ replaceNamespacedCustomObject: vi.fn().mockResolvedValue({}),
+ }
+ const deps: DelegationDeps = {
+ customApi: mockCustomApi as unknown as DelegationDeps['customApi'],
+ coreApi: {} as DelegationDeps['coreApi'],
+ }
+
+ await ensureRecipeContext(
+ deps,
+ recipe,
+ ['test-recipe-redis-mcp'],
+ 'mcp-server',
+ ownerRef,
+ 'sandbox-recipes'
+ )
+
+ expect(mockCustomApi.replaceNamespacedCustomObject).not.toHaveBeenCalled()
+ })
+
it('H04c — re-throws non-409 errors', async () => {
const mockCustomApi = {
createNamespacedCustomObject: vi.fn().mockRejectedValue(new Error('API error')),
diff --git a/workflow-recipes/src/reconciler/mcpDelegation.ts b/workflow-recipes/src/reconciler/mcpDelegation.ts
index 7cc02cdd4..08b0cc281 100644
--- a/workflow-recipes/src/reconciler/mcpDelegation.ts
+++ b/workflow-recipes/src/reconciler/mcpDelegation.ts
@@ -137,20 +137,123 @@ function authoredContextLabels(
}
}
-function contextReplaceIsNoOp(
- existingLabels: Record<string, string> | undefined,
- existingServers: readonly string[],
- mergedServers: readonly string[],
+type ContextOwnerReference = {
+ apiVersion: string
+ kind: string
+ name: string
+ uid: string
+ controller?: boolean
+ blockOwnerDeletion?: boolean
+}
+
+type ExistingContextSnapshot = {
+ metadata?: {
+ resourceVersion?: string
+ labels?: Record<string, string>
+ annotations?: Record<string, string>
+ ownerReferences?: ContextOwnerReference[]
+ finalizers?: string[]
+ }
+ spec?: { contextId?: string; mcpServers?: string[]; [key: string]: unknown }
+}
+
+function isThisRecipeOwner(
+ ref: ContextOwnerReference,
+ recipeOwner: ContextOwnerReference
+): boolean {
+ return ref.kind === recipeOwner.kind && ref.name === recipeOwner.name
+}
+
+function desiredOwnerReferences(
+ existing: ContextOwnerReference[] | undefined,
+ explicitContext: boolean,
+ sameNsAsRecipe: boolean,
+ recipeOwner: ContextOwnerReference
+): ContextOwnerReference[] {
+ const kept = (existing ?? []).filter(ref => !isThisRecipeOwner(ref, recipeOwner))
+ if (!explicitContext && sameNsAsRecipe) {
+ return [...kept, recipeOwner]
+ }
+ return kept
+}
+
+function sameOwnerReferences(
+ left: readonly ContextOwnerReference[],
+ right: readonly ContextOwnerReference[]
+): boolean {
+ if (left.length !== right.length) return false
+ const keys = new Set(right.map(ref => `${ref.kind}/${ref.name}/${ref.uid}`))
+ return left.every(ref => keys.has(`${ref.kind}/${ref.name}/${ref.uid}`))
+}
+
+function authoredLabelsMatch(
+ existingLabels: Record<string, string>,
authoredLabels: Record<string, string>
): boolean {
- if (!sameMcpServerSet(existingServers, mergedServers)) return false
- const labels = existingLabels ?? {}
for (const [key, value] of Object.entries(authoredLabels)) {
- if (labels[key] !== value) return false
+ if (existingLabels[key] !== value) return false
}
return true
}
+function observedContextId(value: unknown): string {
+ return typeof value === 'string' ? value : ''
+}
+
+/**
+ * Single projection for Context replace: skip only when servers, WRC-authored
+ * labels, normalized contextId, and the recipe-owned ownerRef relation already
+ * match the body this reconcile would write.
+ */
+function planContextReplacement(
+ existing: ExistingContextSnapshot,
+ args: {
+ contextName: string
+ explicitContext: boolean
+ sameNsAsRecipe: boolean
+ recipeName: string
+ serverNames: readonly string[]
+ ownerReference: ContextOwnerReference
+ }
+): {
+ existingLabels: Record<string, string>
+ authoredLabels: Record<string, string>
+ mergedServers: string[]
+ nextContextId: string
+ nextOwnerReferences: ContextOwnerReference[]
+} | null {
+ const existingServers = Array.isArray(existing.spec?.mcpServers) ? existing.spec.mcpServers : []
+ const mergedServers = args.explicitContext
+ ? Array.from(new Set([...existingServers, ...args.serverNames]))
+ : [...args.serverNames]
+ const existingLabels = existing.metadata?.labels ?? {}
+ const authoredLabels = authoredContextLabels(args.explicitContext, args.recipeName)
+ const observedId = observedContextId(existing.spec?.contextId)
+ const nextContextId = observedId || args.contextName
+ const existingOwners = existing.metadata?.ownerReferences ?? []
+ const nextOwnerReferences = desiredOwnerReferences(
+ existingOwners,
+ args.explicitContext,
+ args.sameNsAsRecipe,
+ args.ownerReference
+ )
+ if (
+ sameMcpServerSet(existingServers, mergedServers) &&
+ authoredLabelsMatch(existingLabels, authoredLabels) &&
+ observedId === nextContextId &&
+ sameOwnerReferences(existingOwners, nextOwnerReferences)
+ ) {
+ return null
+ }
+ return {
+ existingLabels,
+ authoredLabels,
+ mergedServers,
+ nextContextId,
+ nextOwnerReferences,
+ }
+}
+
/**
* Compute McpServer CRD name.
*
@@ -739,31 +842,46 @@ export async function ensureRecipeContext(
namespace,
plural: CONTEXT_PLURAL,
name: contextName,
- })) as {
- metadata?: {
- resourceVersion?: string
- labels?: Record<string, string>
- annotations?: Record<string, string>
- }
- spec?: { contextId?: string; mcpServers?: string[]; [key: string]: unknown }
- }
- const existingServers = Array.isArray(existing.spec?.mcpServers)
- ? existing.spec.mcpServers
- : []
- const mergedServers = explicitContext
- ? Array.from(new Set([...existingServers, ...serverNames]))
- : serverNames
- const existingLabels = existing.metadata?.labels ?? {}
+ })) as ExistingContextSnapshot
// Semantic post-merge equality. Do not stamp clerum.io/spec-hash on a
// shared Context: N recipe writers would flap the annotation the same
// way clerum.io/recipe already flaps resourceVersion (#460 Phase 1).
- const authoredLabels = authoredContextLabels(explicitContext, recipeName)
- if (contextReplaceIsNoOp(existingLabels, existingServers, mergedServers, authoredLabels)) {
+ const plan = planContextReplacement(existing, {
+ contextName,
+ explicitContext,
+ sameNsAsRecipe,
+ recipeName,
+ serverNames,
+ ownerReference,
+ })
+ if (!plan) {
return { wrote: false }
}
try {
+ const metadata: Record<string, unknown> = {
+ ...contextBody.metadata,
+ labels: {
+ ...plan.existingLabels,
+ ...plan.authoredLabels,
+ },
+ ...(existing.metadata?.annotations
+ ? { annotations: { ...existing.metadata.annotations } }
+ : {}),
+ ...(existing.metadata?.finalizers
+ ? { finalizers: [...existing.metadata.finalizers] }
+ : {}),
+ resourceVersion: existing.metadata?.resourceVersion,
+ }
+ // Always take ownerRefs from the plan so create-time recipe ownership
+ // cannot leak onto a shared or cross-namespace replace. Omit the key
+ // when empty so a replace still clears a stale list (full-object PUT).
+ if (plan.nextOwnerReferences.length > 0) {
+ metadata.ownerReferences = plan.nextOwnerReferences
+ } else {
+ delete metadata.ownerReferences
+ }
await deps.customApi.replaceNamespacedCustomObject({
group: CRD_GROUP,
version: CRD_VERSION,
@@ -772,21 +890,11 @@ export async function ensureRecipeContext(
name: contextName,
body: {
...contextBody,
- metadata: {
- ...contextBody.metadata,
- labels: {
- ...existingLabels,
- ...authoredLabels,
- },
- ...(existing.metadata?.annotations
- ? { annotations: { ...existing.metadata.annotations } }
- : {}),
- resourceVersion: existing.metadata?.resourceVersion,
- },
+ metadata,
spec: {
...(existing.spec ?? {}),
- contextId: existing.spec?.contextId || contextName,
- mcpServers: mergedServers,
+ contextId: plan.nextContextId,
+ mcpServers: plan.mergedServers,
},
},
})