The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(workflow): keep runs alive for if/else and skip edges across the continuation boundary (#26271)
Thomas Trompette4a76dd26709d
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/find-child-step-ids.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/find-child-step-ids.util.spec.tsnew file mode 100644index 00000000000..c32981a4abc--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/find-child-step-ids.util.spec.ts@@ -0,0 +1,53 @@+import {+ createMockCodeStep,+ createMockIfElseStep,+} from 'src/modules/workflow/workflow-executor/utils/create-mock-workflow-steps.util';+import { findChildStepIds } from 'src/modules/workflow/workflow-executor/utils/find-child-step-ids.util';++describe('findChildStepIds', () => {+ it('returns the next step ids of a regular step', () => {+ const step = createMockCodeStep('step-1', ['step-2', 'step-3']);++ expect(findChildStepIds({ step })).toEqual(+ expect.arrayContaining(['step-2', 'step-3']),+ );+ expect(findChildStepIds({ step })).toHaveLength(2);+ });++ it('returns an empty array when a step has no next step ids', () => {+ const step = createMockCodeStep('step-1');++ expect(findChildStepIds({ step })).toEqual([]);+ });++ it('returns the branch children of an if/else step', () => {+ const step = createMockIfElseStep('ifElse', [+ { id: 'ifBranch', filterGroupId: 'fg', nextStepIds: ['ifChild'] },+ { id: 'elseBranch', nextStepIds: ['elseChild'] },+ ]);++ expect(findChildStepIds({ step })).toEqual(+ expect.arrayContaining(['ifChild', 'elseChild']),+ );+ expect(findChildStepIds({ step })).toHaveLength(2);+ });++ it('deduplicates a step reachable through several branches', () => {+ const step = createMockIfElseStep('ifElse', [+ { id: 'ifBranch', filterGroupId: 'fg', nextStepIds: ['merge'] },+ { id: 'elseBranch', nextStepIds: ['merge'] },+ ]);++ expect(findChildStepIds({ step })).toEqual(['merge']);+ });++ it('ignores next step ids an if/else step carries outside its branches', () => {+ const step = createMockIfElseStep(+ 'ifElse',+ [{ id: 'ifBranch', filterGroupId: 'fg', nextStepIds: ['ifChild'] }],+ ['strayChild'],+ );++ expect(findChildStepIds({ step })).toEqual(['ifChild']);+ });+});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/workflow-should-keep-running.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/workflow-should-keep-running.util.spec.tsindex a86550b1295..6aa42473bad 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/workflow-should-keep-running.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/workflow-should-keep-running.util.spec.ts@@ -1,8 +1,14 @@import { StepStatus } from 'twenty-shared/workflow';+import { createMockIfElseStep } from 'src/modules/workflow/workflow-executor/utils/create-mock-workflow-steps.util';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';import { workflowShouldKeepRunning } from 'src/modules/workflow/workflow-executor/utils/workflow-should-keep-running.util';+const ifElseStep = createMockIfElseStep('ifElse', [+ { id: 'ifBranch', filterGroupId: 'fg', nextStepIds: ['ifChild'] },+ { id: 'elseBranch', nextStepIds: ['elseChild'] },+]);+describe('workflowShouldKeepRunning', () => {describe('should return true if', () => {it('running or pending step exists', () => {@@ -37,6 +43,62 @@ describe('workflowShouldKeepRunning', () => {expect(workflowShouldKeepRunning({ steps, stepInfos })).toBeTruthy();});++ it('if/else step with a not started selected branch child exists', () => {+ const steps = [+ ifElseStep,+ { id: 'ifChild' } as WorkflowAction,+ { id: 'elseChild' } as WorkflowAction,+ ];++ const stepInfos = {+ ifElse: {+ status: StepStatus.SUCCESS,+ result: { matchingBranchId: 'ifBranch' },+ },+ ifChild: { status: StepStatus.NOT_STARTED },+ elseChild: { status: StepStatus.NOT_STARTED },+ };++ expect(workflowShouldKeepRunning({ steps, stepInfos })).toBeTruthy();+ });++ it('skipped step with a not started child left to skip exists', () => {+ const steps = [+ { id: 'step-1', nextStepIds: ['step-2'] } as WorkflowAction,+ { id: 'step-2', nextStepIds: ['step-3'] } as WorkflowAction,+ { id: 'step-3' } as WorkflowAction,+ ];++ const stepInfos = {+ 'step-1': { status: StepStatus.SKIPPED },+ 'step-2': { status: StepStatus.SKIPPED },+ 'step-3': { status: StepStatus.NOT_STARTED },+ };++ expect(workflowShouldKeepRunning({ steps, stepInfos })).toBeTruthy();+ });++ it('skipped branch still has to reach a merge step', () => {+ const steps = [+ ifElseStep,+ { id: 'ifChild', nextStepIds: ['merge'] } as WorkflowAction,+ { id: 'elseChild', nextStepIds: ['merge'] } as WorkflowAction,+ { id: 'merge' } as WorkflowAction,+ ];++ const stepInfos = {+ ifElse: {+ status: StepStatus.SUCCESS,+ result: { matchingBranchId: 'ifBranch' },+ },+ ifChild: { status: StepStatus.SUCCESS },+ elseChild: { status: StepStatus.NOT_STARTED },+ merge: { status: StepStatus.NOT_STARTED },+ };++ expect(workflowShouldKeepRunning({ steps, stepInfos })).toBeTruthy();+ });});describe('should return false', () => {@@ -75,5 +137,38 @@ describe('workflowShouldKeepRunning', () => {expect(workflowShouldKeepRunning({ steps, stepInfos })).toBeFalsy();});++ it('if/else step whose branch children are all resolved', () => {+ const steps = [+ ifElseStep,+ { id: 'ifChild' } as WorkflowAction,+ { id: 'elseChild' } as WorkflowAction,+ ];++ const stepInfos = {+ ifElse: {+ status: StepStatus.SUCCESS,+ result: { matchingBranchId: 'ifBranch' },+ },+ ifChild: { status: StepStatus.SUCCESS },+ elseChild: { status: StepStatus.SKIPPED },+ };++ expect(workflowShouldKeepRunning({ steps, stepInfos })).toBeFalsy();+ });++ it('skipped step whose children are already skipped', () => {+ const steps = [+ { id: 'step-1', nextStepIds: ['step-2'] } as WorkflowAction,+ { id: 'step-2' } as WorkflowAction,+ ];++ const stepInfos = {+ 'step-1': { status: StepStatus.SKIPPED },+ 'step-2': { status: StepStatus.SKIPPED },+ };++ expect(workflowShouldKeepRunning({ steps, stepInfos })).toBeFalsy();+ });});});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/find-child-step-ids.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/find-child-step-ids.util.tsnew file mode 100644index 00000000000..17780565af4--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/find-child-step-ids.util.ts@@ -0,0 +1,20 @@+import { isWorkflowIfElseAction } from 'src/modules/workflow/workflow-executor/workflow-actions/if-else/guards/is-workflow-if-else-action.guard';+import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';++export const findChildStepIds = ({+ step,+}: {+ step: WorkflowAction;+}): string[] => {+ if (isWorkflowIfElseAction(step)) {+ return [+ ...new Set(+ step.settings.input.branches.flatMap(+ (branch) => branch.nextStepIds ?? [],+ ),+ ),+ ];+ }++ return [...new Set(step.nextStepIds ?? [])];+};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/workflow-should-keep-running.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/workflow-should-keep-running.util.tsindex f4804ef47f2..36af6650c21 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/workflow-should-keep-running.util.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/workflow-should-keep-running.util.ts@@ -1,7 +1,12 @@+import { isDefined } from 'twenty-shared/utils';import { StepStatus, type WorkflowRunStepInfos } from 'twenty-shared/workflow';import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';+import { TERMINAL_STEP_STATUSES } from 'src/modules/workflow/workflow-executor/constants/terminal-step-statuses.constant';+import { findChildStepIds } from 'src/modules/workflow/workflow-executor/utils/find-child-step-ids.util';import { shouldExecuteStep } from 'src/modules/workflow/workflow-executor/utils/should-execute-step.util';+import { shouldFailSafely } from 'src/modules/workflow/workflow-executor/utils/should-fail-safely.util';+import { shouldSkipStepExecution } from 'src/modules/workflow/workflow-executor/utils/should-skip-step-execution.util';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';export const workflowShouldKeepRunning = ({@@ -17,32 +22,38 @@ export const workflowShouldKeepRunning = ({),);- const completedStepWithNotStartedExecutableChildren = steps.some(- (step) =>- (stepInfos[step.id]?.status === StepStatus.SUCCESS ||- stepInfos[step.id]?.status === StepStatus.FAILED_SAFELY) &&- (step.nextStepIds ?? []).some((nextStepId) => {- const nextStep = steps.find(- (candidateStep) => candidateStep.id === nextStepId,- );-- if (!nextStep) {- return false;- }-- return (- stepInfos[nextStepId]?.status === StepStatus.NOT_STARTED &&- shouldExecuteStep({- step: nextStep,- steps,- stepInfos,- workflowRunStatus: WorkflowRunStatus.RUNNING,- })- );- }),- );+ const terminalStepWithUnresolvedChildrenExists = steps.some((step) => {+ const status = stepInfos[step.id]?.status;- return (- runningOrPendingStepExists || completedStepWithNotStartedExecutableChildren- );+ if (!isDefined(status) || !TERMINAL_STEP_STATUSES.includes(status)) {+ return false;+ }++ return findChildStepIds({ step }).some((childStepId) => {+ if (stepInfos[childStepId]?.status !== StepStatus.NOT_STARTED) {+ return false;+ }++ const childStep = steps.find(+ (candidateStep) => candidateStep.id === childStepId,+ );++ if (!isDefined(childStep)) {+ return false;+ }++ return (+ shouldExecuteStep({+ step: childStep,+ steps,+ stepInfos,+ workflowRunStatus: WorkflowRunStatus.RUNNING,+ }) ||+ shouldSkipStepExecution({ step: childStep, steps, stepInfos }) ||+ shouldFailSafely({ step: childStep, steps, stepInfos })+ );+ });+ });++ return runningOrPendingStepExists || terminalStepWithUnresolvedChildrenExists;};