The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(workflow): stop corrupting run state when a loop resets a deleted step (#25352)
Thomas Trompetteaa5c43c37af6
diff --git a/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version-step/utils/__tests__/remove-step.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version-step/utils/__tests__/remove-step.spec.tsindex 4905845c3a7..b344ff8bdb2 100644--- a/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version-step/utils/__tests__/remove-step.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version-step/utils/__tests__/remove-step.spec.ts@@ -351,6 +351,54 @@ describe('removeStep', () => {expect(hasEmptyNode).toBe(false);});+ it('should clear the If/Else own nextStepIds', () => {+ const ifElseStep = {+ id: '2',+ name: 'If/Else',+ type: WorkflowActionType.IF_ELSE,+ settings: {+ input: {+ stepFilterGroups: [],+ stepFilters: [],+ branches: [+ {+ id: 'branch-if',+ filterGroupId: 'fg-1',+ nextStepIds: ['3'],+ },+ {+ id: 'branch-else',+ nextStepIds: ['4'],+ },+ ],+ },+ outputSchema: {},+ errorHandlingOptions: {+ retryOnFailure: { value: 0 },+ continueOnFailure: { value: false },+ },+ },+ valid: true,+ nextStepIds: ['3', '4'],+ } as WorkflowAction;++ const step1 = createMockAction('1', ['2']);+ const step3 = createMockAction('3');+ const step4 = createMockAction('4');++ const result = removeStep({+ existingTrigger: mockTrigger,+ existingSteps: [step1, ifElseStep, step3, step4],+ stepIdToDelete: '3',+ });++ const updatedIfElse = result.updatedSteps?.find(+ (step) => step.id === '2',+ ) as WorkflowAction;++ expect(updatedIfElse.nextStepIds).toEqual([]);+ });+it('should handle removing a step that is part of iteratorLoopStepIds', () => {const step1 = createMockAction('1', ['2']);const iteratorStep = {diff --git a/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version-step/utils/remove-step.ts b/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version-step/utils/remove-step.tsindex 608a026de40..0d091147fb4 100644--- a/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version-step/utils/remove-step.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-builder/workflow-version-step/utils/remove-step.ts@@ -168,6 +168,7 @@ const updateIfElseStepOnDeletion = ({return {...step,+ nextStepIds: [],settings: {...step.settings,input: {diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/build-step-infos-reset.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/build-step-infos-reset.util.spec.tsnew file mode 100644index 00000000000..8a27a5a6359--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/build-step-infos-reset.util.spec.ts@@ -0,0 +1,97 @@+import {+ StepStatus,+ workflowRunStateStepInfosSchema,+ type WorkflowRunStepInfo,+} from 'twenty-shared/workflow';++import { buildStepInfosReset } from 'src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util';++describe('buildStepInfosReset', () => {+ it('should reset every given step and archive its outcome in history', () => {+ const stepInfos: Record<string, WorkflowRunStepInfo> = {+ step1: { status: StepStatus.SUCCESS, result: { id: '1' } },+ step2: { status: StepStatus.SKIPPED },+ };++ const result = buildStepInfosReset({+ stepIds: ['step1', 'step2'],+ stepInfos,+ });++ expect(result).toEqual({+ step1: {+ status: StepStatus.NOT_STARTED,+ result: undefined,+ error: undefined,+ history: [+ { status: StepStatus.SUCCESS, result: { id: '1' }, error: undefined },+ ],+ },+ step2: {+ status: StepStatus.NOT_STARTED,+ result: undefined,+ error: undefined,+ history: [+ { status: StepStatus.SKIPPED, result: undefined, error: undefined },+ ],+ },+ });+ });++ it('should leave out steps that were not given', () => {+ const result = buildStepInfosReset({+ stepIds: ['step1'],+ stepInfos: {+ step1: { status: StepStatus.SUCCESS },+ step2: { status: StepStatus.SUCCESS },+ },+ });++ expect(Object.keys(result)).toEqual(['step1']);+ });++ it('should append to the existing history', () => {+ const result = buildStepInfosReset({+ stepIds: ['step1'],+ stepInfos: {+ step1: {+ status: StepStatus.FAILED,+ error: 'second failure',+ history: [{ status: StepStatus.FAILED, error: 'first failure' }],+ },+ },+ });++ expect(result.step1.history).toEqual([+ { status: StepStatus.FAILED, error: 'first failure' },+ { status: StepStatus.FAILED, error: 'second failure', result: undefined },+ ]);+ });++ it('should skip step ids that have no step info', () => {+ const result = buildStepInfosReset({+ stepIds: ['step1', 'unknown-step'],+ stepInfos: { step1: { status: StepStatus.SUCCESS } },+ });++ expect(Object.keys(result)).toEqual(['step1']);+ });++ it('should build step infos that match the workflow run state schema', () => {+ const stepInfos = { step1: { status: StepStatus.SUCCESS } };++ const persistedStepInfos = JSON.parse(+ JSON.stringify({+ ...stepInfos,+ ...buildStepInfosReset({+ stepIds: ['step1', 'unknown-step'],+ stepInfos,+ }),+ }),+ );++ expect(+ workflowRunStateStepInfosSchema.safeParse(persistedStepInfos).success,+ ).toBe(true);+ });+});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util.tsnew file mode 100644index 00000000000..b5b3ba07cf0--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util.ts@@ -0,0 +1,37 @@+import { isDefined } from 'twenty-shared/utils';+import { StepStatus, type WorkflowRunStepInfo } from 'twenty-shared/workflow';++export const buildStepInfosReset = ({+ stepIds,+ stepInfos,+}: {+ stepIds: string[];+ stepInfos: Record<string, WorkflowRunStepInfo>;+}): Record<string, WorkflowRunStepInfo> => {+ return stepIds.reduce(+ (acc, stepId) => {+ const stepInfo = stepInfos[stepId];++ if (!isDefined(stepInfo)) {+ return acc;+ }++ acc[stepId] = {+ status: StepStatus.NOT_STARTED,+ result: undefined,+ error: undefined,+ history: [+ ...(stepInfo.history ?? []),+ {+ result: stepInfo.result,+ error: stepInfo.error,+ status: stepInfo.status,+ },+ ],+ };++ return acc;+ },+ {} as Record<string, WorkflowRunStepInfo>,+ );+};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/iterator.workflow-action.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/iterator.workflow-action.tsindex 07d17f0b45d..429551f5259 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/iterator.workflow-action.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/iterator.workflow-action.ts@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';import { isString } from '@sniptt/guards';import { isDefined, resolveInput } from 'twenty-shared/utils';-import { StepStatus, WorkflowRunStepInfo } from 'twenty-shared/workflow';+import { WorkflowRunStepInfo } from 'twenty-shared/workflow';import { WorkflowAction as WorkflowActionInterface } from 'src/modules/workflow/workflow-executor/interfaces/workflow-action.interface';@@ -12,6 +12,7 @@ import {} from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';import { type WorkflowActionInput } from 'src/modules/workflow/workflow-executor/types/workflow-action-input';import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';+import { buildStepInfosReset } from 'src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util';import { findStepOrThrow } from 'src/modules/workflow/workflow-executor/utils/find-step-or-throw.util';import { isWorkflowIteratorAction } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/guards/is-workflow-iterator-action.guard';import { type WorkflowIteratorActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/types/workflow-iterator-action-settings.type';@@ -143,27 +144,24 @@ export class IteratorWorkflowAction implements WorkflowActionInterface {let stepInfosToUpdate: Record<string, WorkflowRunStepInfo> = {};if (!hasProcessedAllItems) {- const subStepsInfos = await this.buildSubStepInfosReset({+ const stepIdsInLoop = getAllStepIdsInLoop({iteratorStepId,initialLoopStepIds,- stepInfos,steps,});stepInfosToUpdate = {...stepInfosToUpdate,- ...subStepsInfos,+ ...buildStepInfosReset({ stepIds: stepIdsInLoop, stepInfos }),};}- const iteratorStepInfo = await this.buildIteratorStepInfoReset({- iteratorStepId,- iteratorStepInfo: stepInfos[iteratorStepId],- });-stepInfosToUpdate = {...stepInfosToUpdate,- ...iteratorStepInfo,+ ...this.buildIteratorStepInfoReset({+ iteratorStepId,+ iteratorStepInfo: stepInfos[iteratorStepId],+ }),};await this.workflowRunWorkspaceService.updateWorkflowRunStepInfos({@@ -173,63 +171,28 @@ export class IteratorWorkflowAction implements WorkflowActionInterface {});}- private async buildSubStepInfosReset({- iteratorStepId,- initialLoopStepIds,- stepInfos,- steps,- }: {- iteratorStepId: string;- initialLoopStepIds: string[];- stepInfos: Record<string, WorkflowRunStepInfo>;- steps: WorkflowAction[];- }) {- const stepIdsToReset = getAllStepIdsInLoop({- iteratorStepId,- initialLoopStepIds,- steps,- });-- return stepIdsToReset.reduce(- (acc, stepId) => {- acc[stepId] = {- status: StepStatus.NOT_STARTED,- result: undefined,- error: undefined,- history: [- ...(stepInfos[stepId]?.history ?? []),- {- result: stepInfos[stepId]?.result,- error: stepInfos[stepId]?.error,- status: stepInfos[stepId]?.status,- },- ],- };-- return acc;- },- {} as Record<string, WorkflowRunStepInfo>,- );- }-- private async buildIteratorStepInfoReset({+ private buildIteratorStepInfoReset({iteratorStepId,iteratorStepInfo,}: {iteratorStepId: string;- iteratorStepInfo: WorkflowRunStepInfo;- }) {+ iteratorStepInfo?: WorkflowRunStepInfo;+ }): Record<string, WorkflowRunStepInfo> {+ if (!isDefined(iteratorStepInfo)) {+ return {};+ }+return {[iteratorStepId]: {...iteratorStepInfo,result: undefined,error: undefined,history: [- ...(iteratorStepInfo?.history ?? []),+ ...(iteratorStepInfo.history ?? []),{- result: iteratorStepInfo?.result,- error: iteratorStepInfo?.error,- status: iteratorStepInfo?.status,+ result: iteratorStepInfo.result,+ error: iteratorStepInfo.error,+ status: iteratorStepInfo.status,},],},diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/__tests__/get-all-step-ids-in-loop.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/__tests__/get-all-step-ids-in-loop.util.spec.tsindex 3d4f8edb407..13a76f1a75a 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/__tests__/get-all-step-ids-in-loop.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/__tests__/get-all-step-ids-in-loop.util.spec.ts@@ -345,6 +345,45 @@ describe('getAllStepIdsInLoop', () => {expect(result).toEqual(['step2']);});+ it('should ignore references to steps that no longer exist', () => {+ const steps = [+ createMockIteratorStep('iterator1', ['step2'], []),+ createMockCodeStep('step2', ['deleted-step', 'iterator1']),+ ];++ const result = getAllStepIdsInLoop({+ iteratorStepId: 'iterator1',+ initialLoopStepIds: ['step2'],+ steps,+ });++ expect(result).toEqual(['step2']);+ });++ it('should ignore a dangling nextStepIds reference on an If/Else step', () => {+ const steps = [+ createMockIteratorStep('iterator1', ['ifElse1'], []),+ createMockIfElseStep(+ 'ifElse1',+ [+ { id: 'branch1', nextStepIds: ['step2'] },+ { id: 'branch2', nextStepIds: ['step3'] },+ ],+ ['deleted-step'],+ ),+ createMockCodeStep('step2', ['iterator1']),+ createMockCodeStep('step3', ['iterator1']),+ ];++ const result = getAllStepIdsInLoop({+ iteratorStepId: 'iterator1',+ initialLoopStepIds: ['ifElse1'],+ steps,+ });++ expect(result).toEqual(['ifElse1', 'step2', 'step3']);+ });+it('should prevent infinite loops with circular references', () => {const steps = [createMockIteratorStep('iterator1', ['step2'], []),diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-all-step-ids-in-loop.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-all-step-ids-in-loop.util.tsindex c3bd2c6cd51..b11537158cb 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-all-step-ids-in-loop.util.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-all-step-ids-in-loop.util.ts@@ -22,7 +22,6 @@ const traverseSteps = ({}visitedStepIds.add(stepId);- allStepIdsInLoop.add(stepId);const step = steps.find((s) => s.id === stepId);@@ -30,6 +29,8 @@ const traverseSteps = ({continue;}+ allStepIdsInLoop.add(stepId);+if (isWorkflowIteratorAction(step)) {const nestedIteratorInput = step.settings.input as WorkflowIteratorActionInput;