Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(wrc): interpret the run-lane NetworkPolicy decision exhaustively
Alfredo Lopez298e24e4915b
diff --git a/workflow-recipes/package-lock.json b/workflow-recipes/package-lock.jsonindex 9ea1a300f..704755935 100644--- a/workflow-recipes/package-lock.json+++ b/workflow-recipes/package-lock.json@@ -1,12 +1,12 @@{"name": "workload-recipes-controller",- "version": "0.1.392",+ "version": "0.1.393","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "workload-recipes-controller",- "version": "0.1.392",+ "version": "0.1.393","license": "SEE LICENSE IN LICENSE","dependencies": {"@aws-sdk/client-s3": "^3.1009.0",diff --git a/workflow-recipes/package.json b/workflow-recipes/package.jsonindex 45f3f5be6..e12985001 100644--- a/workflow-recipes/package.json+++ b/workflow-recipes/package.json@@ -1,6 +1,6 @@{"name": "workload-recipes-controller",- "version": "0.1.392",+ "version": "0.1.393","description": "Workload Recipes Controller (WRC) - WorkloadRecipe lifecycle management and MCP server interface","main": "dist/main.js","engines": {diff --git a/workflow-recipes/src/workflow/workflowReconciler.ts b/workflow-recipes/src/workflow/workflowReconciler.tsindex 18c81ccfe..9f1ec95c6 100644--- a/workflow-recipes/src/workflow/workflowReconciler.ts+++ b/workflow-recipes/src/workflow/workflowReconciler.ts@@ -4854,47 +4854,61 @@ export class WorkflowReconciler {existing,RUNTIME_HTTP_EGRESS_OWNED_ANNOTATIONS)- if (decision.action === 'unchanged') {- this.log.info(`Unchanged NetworkPolicy "${name}"; skipping update`, { namespace })- return { policy: name, action: 'unchanged' }- }- if (decision.action === 'conflict') {- this.log.warn(`NetworkPolicy "${name}" is not owned by this workflow; leaving it`, {- namespace,- reason: decision.reason,- })- return { policy: name, action: 'conflict', reason: decision.reason }- }- if (decision.action === 'retry') {- this.log.warn(`NetworkPolicy "${name}" is terminating; retrying later`, { namespace })- return { policy: name, action: 'retry', reason: decision.reason }- }- try {- await this.deps.networkingApi.replaceNamespacedNetworkPolicy({- name,- namespace,- body: buildNetworkPolicyReplacement(- policy,- existing,- RUNTIME_HTTP_EGRESS_OWNED_ANNOTATIONS- ),- })- this.log.info(`Updated NetworkPolicy "${name}"`, { namespace, reason: decision.reason })- return { policy: name, action: 'replaced' }- } catch (error: unknown) {- const code = getErrorCode(error)- // Deleted between the read and the PUT: the next pass recreates it.- if (code === 404) return this.networkPolicyAbsentAfterConflict(name, namespace)- // A stale resourceVersion gets one re-read and re-decision.- if (code !== 409) throw error- if (attempt === 1) {- // Contention, not a defect: another writer changed the policy again- // between the re-read and the retry. Leave it for a later pass.- this.log.warn(`NetworkPolicy "${name}" replace conflicted twice; retrying later`, {+ switch (decision.action) {+ case 'unchanged':+ this.log.info(`Unchanged NetworkPolicy "${name}"; skipping update`, { namespace })+ return { policy: name, action: 'unchanged' }+ case 'conflict':+ this.log.warn(`NetworkPolicy "${name}" is not owned by this workflow; leaving it`, {namespace,- reason: 'replace-conflicted-twice',+ reason: decision.reason,})- return { policy: name, action: 'retry', reason: 'replace-conflicted-twice' }+ return { policy: name, action: 'conflict', reason: decision.reason }+ case 'retry':+ this.log.warn(`NetworkPolicy "${name}" cannot be written yet; retrying later`, {+ namespace,+ reason: decision.reason,+ })+ return { policy: name, action: 'retry', reason: decision.reason }+ case 'replace':+ try {+ await this.deps.networkingApi.replaceNamespacedNetworkPolicy({+ name,+ namespace,+ body: buildNetworkPolicyReplacement(+ policy,+ existing,+ RUNTIME_HTTP_EGRESS_OWNED_ANNOTATIONS+ ),+ })+ this.log.info(`Updated NetworkPolicy "${name}"`, { namespace, reason: decision.reason })+ return { policy: name, action: 'replaced' }+ } catch (error: unknown) {+ const code = getErrorCode(error)+ // Deleted between the read and the PUT: the next pass recreates it.+ if (code === 404) return this.networkPolicyAbsentAfterConflict(name, namespace)+ // A stale resourceVersion gets one re-read and re-decision.+ if (code !== 409) throw error+ if (attempt === 1) {+ // Contention, not a defect: another writer changed the policy again+ // between the re-read and the retry. Leave it for a later pass.+ this.log.warn(`NetworkPolicy "${name}" replace conflicted twice; retrying later`, {+ namespace,+ reason: 'replace-conflicted-twice',+ })+ return { policy: name, action: 'retry', reason: 'replace-conflicted-twice' }+ }+ }+ break+ default: {+ // Unreachable while every action has a case; a new action fails to+ // compile here instead of falling through to a write.+ const unhandled: never = decision+ throw new Error(+ `NetworkPolicy "${name}": unhandled convergence action ${String(+ (unhandled as { action: unknown }).action+ )}`+ )}}existing = await this.readNetworkPolicyOrNull(name, namespace)diff --git a/workflow-recipes/tests/unit/workflow/workflowReconciler.test.ts b/workflow-recipes/tests/unit/workflow/workflowReconciler.test.tsindex a639d5263..cca0e406d 100644--- a/workflow-recipes/tests/unit/workflow/workflowReconciler.test.ts+++ b/workflow-recipes/tests/unit/workflow/workflowReconciler.test.ts@@ -2359,9 +2359,8 @@ describe('WorkflowReconciler — reconcile loop', () => {logs.entries.some(entry =>entry.level === 'warn' &&- String(entry.msg).includes(- 'NetworkPolicy "test-wf-snippet-runner-egress" is terminating'- )+ entry.reason === 'terminating' &&+ String(entry.msg).includes('NetworkPolicy "test-wf-snippet-runner-egress"'))).toBe(true)// The refresh rethrows the DNS error, so the prune's own error line is@@ -3682,41 +3681,56 @@ describe('WorkflowReconciler — reconcile loop', () => {})it('flags a retry for a run-lane policy that is being deleted and leaves it unwritten', async () => {- const { api, live, key } = makeApiserverNetworkingApi()- const reconciler = new WorkflowReconciler(makeDeps({ networkingApi: api as never }))- const spec = makeSpec({ agent: undefined, steps: [{ id: 'prepare', run: snippetRun() }] })+ const logs = captureRunLaneNetworkPolicyLogs()+ try {+ const { api, live, key } = makeApiserverNetworkingApi()+ const reconciler = new WorkflowReconciler(makeDeps({ networkingApi: api as never }))+ const spec = makeSpec({ agent: undefined, steps: [{ id: 'prepare', run: snippetRun() }] })- const first = await reconciler.reconcile('test-wf', 'uid-123', 'sandbox-recipes', spec)- expect(first.workflowPhase).not.toBe('failed')- expect(first.networkPolicyRetryPending).toBeFalsy()- const terminating = live.get(key('sandbox-recipes', 'test-wf-coord-to-wrc'))- expect(terminating).toBeDefined()- // Drift the spec too, so that without the deletion check the pass would PUT.- terminating!.spec = {- ...terminating!.spec!,- podSelector: { matchLabels: { drifted: 'yes' } },- }- terminating!.metadata = {- ...terminating!.metadata,- deletionTimestamp: new Date('2026-09-23T10:00:00Z'),- }- api.readNamespacedNetworkPolicy.mockClear()- api.replaceNamespacedNetworkPolicy.mockClear()+ const first = await reconciler.reconcile('test-wf', 'uid-123', 'sandbox-recipes', spec)+ expect(first.workflowPhase).not.toBe('failed')+ expect(first.networkPolicyRetryPending).toBeFalsy()+ const terminating = live.get(key('sandbox-recipes', 'test-wf-coord-to-wrc'))+ expect(terminating).toBeDefined()+ // Drift the spec too, so that without the deletion check the pass would PUT.+ terminating!.spec = {+ ...terminating!.spec!,+ podSelector: { matchLabels: { drifted: 'yes' } },+ }+ terminating!.metadata = {+ ...terminating!.metadata,+ deletionTimestamp: new Date('2026-09-23T10:00:00Z'),+ }+ api.readNamespacedNetworkPolicy.mockClear()+ api.replaceNamespacedNetworkPolicy.mockClear()+ logs.entries.length = 0- const second = await reconciler.reconcile('test-wf', 'uid-123', 'sandbox-recipes', spec)+ const second = await reconciler.reconcile('test-wf', 'uid-123', 'sandbox-recipes', spec)- expect(readPolicyNames(api)).toContain('test-wf-coord-to-wrc')- expect(second.networkPolicyRetryPending).toBe(true)- expect(second.phase).not.toBe('failed')- expect(second.workflowPhase).not.toBe('failed')- // A policy being deleted is not a conflict: the pass reached the apply- // and found none, so the owned set is empty rather than absent.- expect(second.networkPolicyOwnershipConditions).toEqual([])- expect(- api.replaceNamespacedNetworkPolicy.mock.calls.filter(- ([arg]) => arg.name === 'test-wf-coord-to-wrc'- )- ).toHaveLength(0)+ expect(readPolicyNames(api)).toContain('test-wf-coord-to-wrc')+ expect(second.networkPolicyRetryPending).toBe(true)+ expect(second.phase).not.toBe('failed')+ expect(second.workflowPhase).not.toBe('failed')+ // A policy being deleted is not a conflict: the pass reached the apply+ // and found none, so the owned set is empty rather than absent.+ expect(second.networkPolicyOwnershipConditions).toEqual([])+ expect(+ api.replaceNamespacedNetworkPolicy.mock.calls.filter(+ ([arg]) => arg.name === 'test-wf-coord-to-wrc'+ )+ ).toHaveLength(0)+ // The retry warn carries the decision's reason, not a fixed phrase.+ expect(+ logs.entries.filter(+ entry =>+ entry.level === 'warn' &&+ String(entry.msg).includes('test-wf-coord-to-wrc') &&+ String(entry.msg).includes('retrying later')+ )+ ).toEqual([expect.objectContaining({ reason: 'terminating' })])+ } finally {+ logs.restore()+ }})it('flags a retry, without failing, when a concurrent writer wins both replaces of a run-lane policy', async () => {