Skip to content

The open alternative to Salesforce, designed for AI.

fix(workflow): keep converging steps when an if/else branch is skipped (#24136)

Thomas Trompette36666d014052
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/get-effective-parent-status.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/get-effective-parent-status.util.spec.ts
new file mode 100644
index 00000000000..040fd2742da
--- /dev/null
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/get-effective-parent-status.util.spec.ts
@@ -0,0 +1,120 @@
+import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
+
+import { getEffectiveParentStatus } from 'src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util';
+import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
+
+const recordStep = {
+ id: 'parent',
+ type: 'RECORD_UPDATE',
+ name: 'parent',
+ nextStepIds: ['child'],
+ settings: { input: {} },
+} as unknown as WorkflowAction;
+
+const ifElseStep = {
+ id: 'ifElse',
+ type: 'IF_ELSE',
+ name: 'If/Else',
+ nextStepIds: [],
+ settings: {
+ input: {
+ branches: [
+ { id: 'ifBranch', filterGroupId: 'fg', nextStepIds: ['ifChild'] },
+ { id: 'elseBranch', nextStepIds: ['elseChild'] },
+ ],
+ stepFilterGroups: [],
+ stepFilters: [],
+ },
+ },
+} as unknown as WorkflowAction;
+
+describe('getEffectiveParentStatus', () => {
+ it('should return the raw status for a non if/else parent', () => {
+ const stepInfos: WorkflowRunStepInfos = {
+ parent: { status: StepStatus.SUCCESS },
+ };
+
+ expect(
+ getEffectiveParentStatus({
+ parentStep: recordStep,
+ childStepId: 'child',
+ stepInfos,
+ }),
+ ).toBe(StepStatus.SUCCESS);
+ });
+
+ it('should return the raw status for the child on the branch that was taken', () => {
+ const stepInfos: WorkflowRunStepInfos = {
+ ifElse: {
+ status: StepStatus.SUCCESS,
+ result: { matchingBranchId: 'ifBranch' },
+ },
+ };
+
+ expect(
+ getEffectiveParentStatus({
+ parentStep: ifElseStep,
+ childStepId: 'ifChild',
+ stepInfos,
+ }),
+ ).toBe(StepStatus.SUCCESS);
+ });
+
+ it('should read as SKIPPED for the child on a branch that was not taken', () => {
+ const stepInfos: WorkflowRunStepInfos = {
+ ifElse: {
+ status: StepStatus.SUCCESS,
+ result: { matchingBranchId: 'ifBranch' },
+ },
+ };
+
+ expect(
+ getEffectiveParentStatus({
+ parentStep: ifElseStep,
+ childStepId: 'elseChild',
+ stepInfos,
+ }),
+ ).toBe(StepStatus.SKIPPED);
+ });
+
+ it('should return the raw status when the if/else has no matching branch (skipped/failed itself)', () => {
+ const stepInfos: WorkflowRunStepInfos = {
+ ifElse: { status: StepStatus.SKIPPED },
+ };
+
+ expect(
+ getEffectiveParentStatus({
+ parentStep: ifElseStep,
+ childStepId: 'ifChild',
+ stepInfos,
+ }),
+ ).toBe(StepStatus.SKIPPED);
+ });
+
+ it('should propagate FAILED_SAFELY to the child on the taken branch', () => {
+ const stepInfos: WorkflowRunStepInfos = {
+ ifElse: {
+ status: StepStatus.FAILED_SAFELY,
+ result: { matchingBranchId: 'ifBranch' },
+ },
+ };
+
+ expect(
+ getEffectiveParentStatus({
+ parentStep: ifElseStep,
+ childStepId: 'ifChild',
+ stepInfos,
+ }),
+ ).toBe(StepStatus.FAILED_SAFELY);
+ });
+
+ it('should return undefined when the parent has no status yet', () => {
+ expect(
+ getEffectiveParentStatus({
+ parentStep: ifElseStep,
+ childStepId: 'ifChild',
+ stepInfos: {},
+ }),
+ ).toBeUndefined();
+ });
+});
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-child-step.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-child-step.util.spec.ts
index f4d811fff46..58d1fbe8be7 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-child-step.util.spec.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/should-execute-child-step.util.spec.ts
@@ -3,6 +3,8 @@ import { StepStatus, WorkflowActionType } from 'twenty-shared/workflow';
import { shouldExecuteChildStep } from 'src/modules/workflow/workflow-executor/utils/should-execute-child-step.util';
import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
+const CHILD_STEP_ID = 'child-step-under-test';
+
describe('shouldExecuteChildStep', () => {
const parentSteps = [
{
@@ -33,6 +35,7 @@ describe('shouldExecuteChildStep', () => {
it('should return true when there are no parent steps', () => {
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps: [],
stepInfos: {},
});
@@ -51,6 +54,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -69,6 +73,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -87,6 +92,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -147,6 +153,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps: multiParentSteps,
stepInfos,
});
@@ -165,6 +172,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -183,6 +191,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -201,6 +210,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -219,6 +229,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -237,6 +248,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -255,6 +267,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -273,6 +286,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -291,6 +305,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -321,6 +336,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps: singleParent,
stepInfos,
});
@@ -351,6 +367,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps: singleParent,
stepInfos,
});
@@ -366,6 +383,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -420,6 +438,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps: multiParentSteps,
stepInfos,
});
@@ -449,6 +468,7 @@ describe('shouldExecuteChildStep', () => {
);
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps: manyParentSteps,
stepInfos,
});
@@ -467,6 +487,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps,
stepInfos,
});
@@ -497,6 +518,7 @@ describe('shouldExecuteChildStep', () => {
};
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps: singleParent,
stepInfos,
});
@@ -526,6 +548,7 @@ describe('shouldExecuteChildStep', () => {
);
const result = shouldExecuteChildStep({
+ childStepId: CHILD_STEP_ID,
parentSteps: manyParentSteps,
stepInfos,
});
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util.ts
new file mode 100644
index 00000000000..31d5847497a
--- /dev/null
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util.ts
@@ -0,0 +1,41 @@
+import { isDefined } from 'twenty-shared/utils';
+import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
+
+import { isWorkflowIfElseAction } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/guards/is-workflow-if-else-action.guard';
+import { type WorkflowIfElseResult } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/types/workflow-if-else-result.type';
+import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
+
+export const getEffectiveParentStatus = ({
+ parentStep,
+ childStepId,
+ stepInfos,
+}: {
+ parentStep: WorkflowAction;
+ childStepId: string;
+ stepInfos: WorkflowRunStepInfos;
+}): StepStatus | undefined => {
+ const parentStatus = stepInfos[parentStep.id]?.status;
+
+ if (
+ !isWorkflowIfElseAction(parentStep) ||
+ parentStep.nextStepIds?.includes(childStepId)
+ ) {
+ return parentStatus;
+ }
+
+ const matchingBranchId = (
+ stepInfos[parentStep.id]?.result as WorkflowIfElseResult | undefined
+ )?.matchingBranchId;
+
+ if (!isDefined(matchingBranchId)) {
+ return parentStatus;
+ }
+
+ const matchingBranch = parentStep.settings.input.branches.find(
+ (branch) => branch.id === matchingBranchId,
+ );
+
+ return matchingBranch?.nextStepIds.includes(childStepId)
+ ? parentStatus
+ : StepStatus.SKIPPED;
+};
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-execute-child-step.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-execute-child-step.util.ts
index 718835bc44f..ffa0337f718 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-execute-child-step.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-execute-child-step.util.ts
@@ -1,26 +1,34 @@
import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
+import { getEffectiveParentStatus } from 'src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util';
import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
export const shouldExecuteChildStep = ({
parentSteps,
+ childStepId,
stepInfos,
}: {
parentSteps: WorkflowAction[];
+ childStepId: string;
stepInfos: WorkflowRunStepInfos;
}) => {
if (parentSteps.length === 0) {
return true;
}
- const hasSuccessfulParentStep = parentSteps.some(
- (parentStep) => stepInfos[parentStep.id]?.status === StepStatus.SUCCESS,
+
+ const statuses = parentSteps.map((parentStep) =>
+ getEffectiveParentStatus({ parentStep, childStepId, stepInfos }),
+ );
+
+ const hasSuccessfulParentStep = statuses.some(
+ (status) => status === StepStatus.SUCCESS,
);
- const areAllParentsCompleted = parentSteps.every(
- (parentStep) =>
- stepInfos[parentStep.id]?.status === StepStatus.SUCCESS ||
- stepInfos[parentStep.id]?.status === StepStatus.STOPPED ||
- stepInfos[parentStep.id]?.status === StepStatus.SKIPPED,
+ const areAllParentsCompleted = statuses.every(
+ (status) =>
+ status === StepStatus.SUCCESS ||
+ status === StepStatus.STOPPED ||
+ status === StepStatus.SKIPPED,
);
return hasSuccessfulParentStep && areAllParentsCompleted;
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-execute-step.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-execute-step.util.ts
index 1bd6ab557f6..7f41472fa62 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-execute-step.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-execute-step.util.ts
@@ -39,6 +39,7 @@ export const shouldExecuteStep = ({
return shouldExecuteChildStep({
parentSteps,
+ childStepId: step.id,
stepInfos,
});
};
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-fail-safely.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-fail-safely.util.ts
index 88d95a8dcea..4df50cafdfb 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-fail-safely.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-fail-safely.util.ts
@@ -1,7 +1,9 @@
+import { isDefined } from 'twenty-shared/utils';
import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
import { TERMINAL_STEP_STATUSES } from 'src/modules/workflow/workflow-executor/constants/terminal-step-statuses.constant';
import { findParentSteps } from 'src/modules/workflow/workflow-executor/utils/find-parent-steps.util';
+import { getEffectiveParentStatus } from 'src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util';
import { isWorkflowIteratorAction } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/guards/is-workflow-iterator-action.guard';
import { shouldFailSafelyIteratorStep } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-fail-safely-iterator-step.util';
import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
@@ -29,13 +31,16 @@ export const shouldFailSafely = ({
return false;
}
- const areAllParentsTerminal = parentSteps.every((parentStep) =>
- TERMINAL_STEP_STATUSES.includes(stepInfos[parentStep.id]?.status),
+ const statuses = parentSteps.map((parentStep) =>
+ getEffectiveParentStatus({ parentStep, childStepId: step.id, stepInfos }),
);
- const hasFailedSafelyParent = parentSteps.some(
- (parentStep) =>
- stepInfos[parentStep.id]?.status === StepStatus.FAILED_SAFELY,
+ const areAllParentsTerminal = statuses.every(
+ (status) => isDefined(status) && TERMINAL_STEP_STATUSES.includes(status),
+ );
+
+ const hasFailedSafelyParent = statuses.some(
+ (status) => status === StepStatus.FAILED_SAFELY,
);
return areAllParentsTerminal && hasFailedSafelyParent;
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-skip-step-execution.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-skip-step-execution.util.ts
index 895fce00806..b4a17d30758 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-skip-step-execution.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/should-skip-step-execution.util.ts
@@ -1,6 +1,7 @@
import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
import { findParentSteps } from 'src/modules/workflow/workflow-executor/utils/find-parent-steps.util';
+import { getEffectiveParentStatus } from 'src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util';
import { isWorkflowIteratorAction } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/guards/is-workflow-iterator-action.guard';
import { shouldSkipIteratorStepExecution } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-skip-iterator-step-execution.util';
import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
@@ -28,10 +29,17 @@ export const shouldSkipStepExecution = ({
return false;
}
- return parentSteps.every(
- (parentStep) =>
- stepInfos[parentStep.id]?.status === StepStatus.SKIPPED ||
- stepInfos[parentStep.id]?.status === StepStatus.STOPPED ||
- stepInfos[parentStep.id]?.status === StepStatus.FAILED_SAFELY,
- );
+ return parentSteps.every((parentStep) => {
+ const status = getEffectiveParentStatus({
+ parentStep,
+ childStepId: step.id,
+ stepInfos,
+ });
+
+ return (
+ status === StepStatus.SKIPPED ||
+ status === StepStatus.STOPPED ||
+ status === StepStatus.FAILED_SAFELY
+ );
+ });
};
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/__tests__/get-next-step-ids-for-if-else.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/__tests__/get-next-step-ids-for-if-else.util.spec.ts
new file mode 100644
index 00000000000..63f0d869c4b
--- /dev/null
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/__tests__/get-next-step-ids-for-if-else.util.spec.ts
@@ -0,0 +1,52 @@
+import { getNextStepIdsForIfElse } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/get-next-step-ids-for-if-else.util';
+import { type WorkflowIfElseAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
+
+const buildIfElseStep = (branchNextStepIds: string[][]) =>
+ ({
+ id: 'ifElse',
+ type: 'IF_ELSE',
+ name: 'If/Else',
+ nextStepIds: [],
+ settings: {
+ input: {
+ branches: branchNextStepIds.map((nextStepIds, index) => ({
+ id: `branch-${index}`,
+ filterGroupId:
+ index === branchNextStepIds.length - 1 ? undefined : 'fg',
+ nextStepIds,
+ })),
+ stepFilterGroups: [],
+ stepFilters: [],
+ },
+ },
+ }) as unknown as WorkflowIfElseAction;
+
+describe('getNextStepIdsForIfElse', () => {
+ it('should hand back every branch root so each one evaluates itself', () => {
+ expect(
+ getNextStepIdsForIfElse({
+ executedStep: buildIfElseStep([
+ ['ifStep'],
+ ['elseIfStep'],
+ ['elseStep'],
+ ]),
+ }),
+ ).toEqual({ nextStepIdsToExecute: ['ifStep', 'elseIfStep', 'elseStep'] });
+ });
+
+ it('should hand back a root shared by several branches only once', () => {
+ expect(
+ getNextStepIdsForIfElse({
+ executedStep: buildIfElseStep([['merge'], ['merge']]),
+ }),
+ ).toEqual({ nextStepIdsToExecute: ['merge'] });
+ });
+
+ it('should ignore a branch with no next step', () => {
+ expect(
+ getNextStepIdsForIfElse({
+ executedStep: buildIfElseStep([['ifStep'], []]),
+ }),
+ ).toEqual({ nextStepIdsToExecute: ['ifStep'] });
+ });
+});
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/get-next-step-ids-for-if-else.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/get-next-step-ids-for-if-else.util.ts
index cf8cec3a98d..696711b6dd4 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/get-next-step-ids-for-if-else.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/if-else/utils/get-next-step-ids-for-if-else.util.ts
@@ -1,48 +1,15 @@
-import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';
-import { type WorkflowIfElseResult } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/types/workflow-if-else-result.type';
import { type WorkflowIfElseAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
export const getNextStepIdsForIfElse = ({
executedStep,
- executedStepOutput,
}: {
executedStep: WorkflowIfElseAction;
- executedStepOutput: WorkflowActionOutput;
-}): {
- nextStepIdsToExecute?: string[];
- nextStepIdsToSkip?: string[];
- nextStepIdsToFailSafely?: string[];
-} => {
- const ifElseResult = executedStepOutput.result as
- | WorkflowIfElseResult
- | undefined;
-
- const branches = executedStep.settings.input.branches;
-
- if (ifElseResult?.matchingBranchId) {
- const matchingBranch = branches.find(
- (branch) => branch.id === ifElseResult.matchingBranchId,
- );
-
- const nonMatchingBranches = branches.filter(
- (branch) => branch.id !== ifElseResult.matchingBranchId,
- );
-
- return {
- nextStepIdsToExecute: matchingBranch?.nextStepIds,
- nextStepIdsToSkip: nonMatchingBranches.flatMap(
+}): { nextStepIdsToExecute: string[] } => ({
+ nextStepIdsToExecute: [
+ ...new Set(
+ executedStep.settings.input.branches.flatMap(
(branch) => branch.nextStepIds,
),
- };
- }
-
- if (executedStepOutput.shouldFailSafely) {
- return {
- nextStepIdsToFailSafely: branches.flatMap((branch) => branch.nextStepIds),
- };
- }
-
- return {
- nextStepIdsToSkip: branches.flatMap((branch) => branch.nextStepIds),
- };
-};
+ ),
+ ],
+});
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/__tests__/get-next-step-ids-for-iterator.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/__tests__/get-next-step-ids-for-iterator.util.spec.ts
new file mode 100644
index 00000000000..73c90223499
--- /dev/null
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/__tests__/get-next-step-ids-for-iterator.util.spec.ts
@@ -0,0 +1,56 @@
+import { getNextStepIdsForIterator } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-next-step-ids-for-iterator.util';
+import { type WorkflowIteratorAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
+
+const iteratorStep = {
+ id: 'iterator',
+ type: 'ITERATOR',
+ name: 'Iterator',
+ nextStepIds: ['afterLoop'],
+ settings: { input: { initialLoopStepIds: ['loopBody'] } },
+} as unknown as WorkflowIteratorAction;
+
+describe('getNextStepIdsForIterator', () => {
+ it('should run the loop body while items remain', () => {
+ expect(
+ getNextStepIdsForIterator({
+ executedStep: iteratorStep,
+ executedStepOutput: { result: { hasProcessedAllItems: false } },
+ }),
+ ).toEqual({ nextStepIdsToExecute: ['loopBody'] });
+ });
+
+ it('should fall through to the after-loop steps once every item is processed', () => {
+ expect(
+ getNextStepIdsForIterator({
+ executedStep: iteratorStep,
+ executedStepOutput: { result: { hasProcessedAllItems: true } },
+ }),
+ ).toBeUndefined();
+ });
+
+ // A terminated iterator has to hand its after-loop steps back too, otherwise a step
+ // converging on them is never evaluated again once its other parent has finished.
+ it('should skip the loop body and still evaluate the after-loop steps when skipped', () => {
+ expect(
+ getNextStepIdsForIterator({
+ executedStep: iteratorStep,
+ executedStepOutput: { shouldSkipStepExecution: true },
+ }),
+ ).toEqual({
+ nextStepIdsToSkip: ['loopBody'],
+ nextStepIdsToExecute: ['afterLoop'],
+ });
+ });
+
+ it('should fail the loop body safely and still evaluate the after-loop steps', () => {
+ expect(
+ getNextStepIdsForIterator({
+ executedStep: iteratorStep,
+ executedStepOutput: { shouldFailSafely: true },
+ }),
+ ).toEqual({
+ nextStepIdsToFailSafely: ['loopBody'],
+ nextStepIdsToExecute: ['afterLoop'],
+ });
+ });
+});
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-next-step-ids-for-iterator.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-next-step-ids-for-iterator.util.ts
index 6af288e5ff3..c370210dce5 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-next-step-ids-for-iterator.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-next-step-ids-for-iterator.util.ts
@@ -26,11 +26,17 @@ export const getNextStepIdsForIterator = ({
: (executedStep.settings.input.initialLoopStepIds ?? []);
if (executedStepOutput.shouldSkipStepExecution) {
- return { nextStepIdsToSkip: initialLoopStepIds };
+ return {
+ nextStepIdsToSkip: initialLoopStepIds,
+ nextStepIdsToExecute: executedStep.nextStepIds,
+ };
}
if (executedStepOutput.shouldFailSafely) {
- return { nextStepIdsToFailSafely: initialLoopStepIds };
+ return {
+ nextStepIdsToFailSafely: initialLoopStepIds,
+ nextStepIdsToExecute: executedStep.nextStepIds,
+ };
}
const iteratorStepResult = executedStepOutput.result as
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-execute-iterator-step.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-execute-iterator-step.util.ts
index e841b6a6dbd..18c2e7c88dd 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-execute-iterator-step.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-execute-iterator-step.util.ts
@@ -3,6 +3,7 @@ import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
import { TERMINAL_STEP_STATUSES } from 'src/modules/workflow/workflow-executor/constants/terminal-step-statuses.constant';
import { findParentSteps } from 'src/modules/workflow/workflow-executor/utils/find-parent-steps.util';
+import { getEffectiveParentStatus } from 'src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util';
import { shouldExecuteChildStep } from 'src/modules/workflow/workflow-executor/utils/should-execute-child-step.util';
import { stepHasBeenStarted } from 'src/modules/workflow/workflow-executor/utils/step-has-been-started.util';
import { getAllStepIdsInLoop } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-all-step-ids-in-loop.util';
@@ -54,9 +55,15 @@ export const shouldExecuteIteratorStep = ({
);
if (hasFailureFromOwnLoop) {
- const areAllParentsTerminal = stepsToCheck.every((parentStep) =>
- TERMINAL_STEP_STATUSES.includes(stepInfos[parentStep.id]?.status),
- );
+ const areAllParentsTerminal = stepsToCheck.every((parentStep) => {
+ const status = getEffectiveParentStatus({
+ parentStep,
+ childStepId: step.id,
+ stepInfos,
+ });
+
+ return isDefined(status) && TERMINAL_STEP_STATUSES.includes(status);
+ });
return areAllParentsTerminal;
}
@@ -64,6 +71,7 @@ export const shouldExecuteIteratorStep = ({
return shouldExecuteChildStep({
parentSteps: stepsToCheck,
+ childStepId: step.id,
stepInfos,
});
};
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-fail-safely-iterator-step.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-fail-safely-iterator-step.util.ts
index f871b62a894..ab8cef231d4 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-fail-safely-iterator-step.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-fail-safely-iterator-step.util.ts
@@ -3,6 +3,7 @@ import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
import { TERMINAL_STEP_STATUSES } from 'src/modules/workflow/workflow-executor/constants/terminal-step-statuses.constant';
import { findParentSteps } from 'src/modules/workflow/workflow-executor/utils/find-parent-steps.util';
+import { getEffectiveParentStatus } from 'src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util';
import { stepHasBeenStarted } from 'src/modules/workflow/workflow-executor/utils/step-has-been-started.util';
import { getAllStepIdsInLoop } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-all-step-ids-in-loop.util';
import {
@@ -40,30 +41,35 @@ export const shouldFailSafelyIteratorStep = ({
return false;
}
- const areAllExternalParentsTerminal = externalParentSteps.every(
- (parentStep) =>
- TERMINAL_STEP_STATUSES.includes(stepInfos[parentStep.id]?.status),
+ const externalParentStatuses = externalParentSteps.map((parentStep) =>
+ getEffectiveParentStatus({ parentStep, childStepId: step.id, stepInfos }),
);
- const hasFailedSafelyExternalParent = externalParentSteps.some(
- (parentStep) =>
- stepInfos[parentStep.id]?.status === StepStatus.FAILED_SAFELY,
+ const areAllExternalParentsTerminal = externalParentStatuses.every(
+ (status) => isDefined(status) && TERMINAL_STEP_STATUSES.includes(status),
+ );
+
+ const hasFailedSafelyExternalParent = externalParentStatuses.some(
+ (status) => status === StepStatus.FAILED_SAFELY,
);
return areAllExternalParentsTerminal && hasFailedSafelyExternalParent;
}
- const areAllParentsTerminal = allParentSteps.every((parentStep) =>
- TERMINAL_STEP_STATUSES.includes(stepInfos[parentStep.id]?.status),
+ const allParentStatuses = allParentSteps.map((parentStep) =>
+ getEffectiveParentStatus({ parentStep, childStepId: step.id, stepInfos }),
+ );
+
+ const areAllParentsTerminal = allParentStatuses.every(
+ (status) => isDefined(status) && TERMINAL_STEP_STATUSES.includes(status),
);
if (!areAllParentsTerminal) {
return false;
}
- const hasFailedSafelyParent = allParentSteps.some(
- (parentStep) =>
- stepInfos[parentStep.id]?.status === StepStatus.FAILED_SAFELY,
+ const hasFailedSafelyParent = allParentStatuses.some(
+ (status) => status === StepStatus.FAILED_SAFELY,
);
if (!hasFailedSafelyParent) {
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-skip-iterator-step-execution.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-skip-iterator-step-execution.util.ts
index 5ef0345476b..466d1aff906 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-skip-iterator-step-execution.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/should-skip-iterator-step-execution.util.ts
@@ -2,6 +2,7 @@ import { isDefined } from 'twenty-shared/utils';
import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';
import { findParentSteps } from 'src/modules/workflow/workflow-executor/utils/find-parent-steps.util';
+import { getEffectiveParentStatus } from 'src/modules/workflow/workflow-executor/utils/get-effective-parent-status.util';
import { stepHasBeenStarted } from 'src/modules/workflow/workflow-executor/utils/step-has-been-started.util';
import { getAllStepIdsInLoop } from 'src/modules/workflow/workflow-executor/workflow-actions/iterator/utils/get-all-step-ids-in-loop.util';
import {
@@ -38,10 +39,17 @@ export const shouldSkipIteratorStepExecution = ({
return false;
}
- return parentSteps.every(
- (parentStep) =>
- stepInfos[parentStep.id]?.status === StepStatus.SKIPPED ||
- stepInfos[parentStep.id]?.status === StepStatus.STOPPED ||
- stepInfos[parentStep.id]?.status === StepStatus.FAILED_SAFELY,
- );
+ return parentSteps.every((parentStep) => {
+ const status = getEffectiveParentStatus({
+ parentStep,
+ childStepId: step.id,
+ stepInfos,
+ });
+
+ return (
+ status === StepStatus.SKIPPED ||
+ status === StepStatus.STOPPED ||
+ status === StepStatus.FAILED_SAFELY
+ );
+ });
};
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
index 1bce3310f1a..c5d01a13a9d 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
@@ -263,10 +263,7 @@ export class WorkflowExecutorWorkspaceService {
}
if (isWorkflowIfElseAction(executedStep)) {
- return getNextStepIdsForIfElse({
- executedStep,
- executedStepOutput,
- });
+ return getNextStepIdsForIfElse({ executedStep });
}
return { nextStepIdsToExecute: executedStep.nextStepIds };