diff --git a/.changeset/dirty-worms-relate.md b/.changeset/dirty-worms-relate.md new file mode 100644 index 00000000000..f6756958f5e --- /dev/null +++ b/.changeset/dirty-worms-relate.md @@ -0,0 +1,5 @@ +--- +'@mastra/core': patch +--- + +fix hanging and excessive workflow execution diff --git a/packages/core/src/workflows/workflow.test.ts b/packages/core/src/workflows/workflow.test.ts index caf0327eec6..3bf6b8e8c2b 100644 --- a/packages/core/src/workflows/workflow.test.ts +++ b/packages/core/src/workflows/workflow.test.ts @@ -837,6 +837,127 @@ describe('Workflow', async () => { // @ts-ignore expect(results.increment.output).toEqual({ newValue: 10 }); }); + + it('should run a while loop after a `.after` call', async () => { + const step1Action = vi.fn(); + const step2Action = vi.fn(); + const step3Action = vi.fn(); + const step4Action = vi.fn(); + const step5Action = vi.fn(); + + const step1 = new Step({ + id: 'step1', + execute: step1Action, + }); + + const step2 = new Step({ + id: 'step2', + execute: step2Action, + }); + + const step3 = new Step({ + id: 'step3', + execute: step3Action, + }); + + const step4 = new Step({ + id: 'step4', + execute: step4Action, + }); + + const step5 = new Step({ + id: 'step5', + execute: step5Action, + }); + + const wf = new Workflow({ + name: 'counter-workflow', + triggerSchema: z.object({ + target: z.number(), + startValue: z.number(), + }), + }); + + wf.step(step1) + .then(step2) + .after(step2) + .while(async () => false, step3) + .then(step4) + .step(step5) + .commit(); + + const run = wf.createRun(); + await run.start({ triggerData: { target: 10, startValue: 0 } }); + + expect(step5Action).toHaveBeenCalledTimes(1); + expect(step4Action).toHaveBeenCalledTimes(1); + expect(step3Action).toHaveBeenCalledTimes(0); + expect(step2Action).toHaveBeenCalledTimes(1); + expect(step1Action).toHaveBeenCalledTimes(1); + }); + + it('should run a `.step` call once after a while loop', async () => { + const step1Action = vi.fn(); + const step2Action = vi.fn(); + const step3Action = vi.fn().mockImplementation(async ({ context }) => { + count++; + return { count }; + }); + const step4Action = vi.fn(); + const step5Action = vi.fn(); + + let count = 0; + + const step1 = new Step({ + id: 'step1', + execute: step1Action, + }); + + const step2 = new Step({ + id: 'step2', + execute: step2Action, + }); + + const step3 = new Step({ + id: 'step3', + execute: step3Action, + }); + + const step4 = new Step({ + id: 'step4', + execute: step4Action, + }); + + const step5 = new Step({ + id: 'step5', + execute: step5Action, + }); + + const wf = new Workflow({ + name: 'counter-workflow', + triggerSchema: z.object({ + target: z.number(), + startValue: z.number(), + }), + }); + + wf.step(step1) + .then(step2) + .after(step2) + .while(async () => count < 10, step3) + .then(step4) + .step(step5) + .commit(); + + const run = wf.createRun(); + await run.start({ triggerData: { target: 10, startValue: 0 } }); + + expect(step5Action).toHaveBeenCalledTimes(1); + expect(step4Action).toHaveBeenCalledTimes(1); + expect(step3Action).toHaveBeenCalledTimes(10); + expect(step2Action).toHaveBeenCalledTimes(1); + expect(step1Action).toHaveBeenCalledTimes(1); + }); }); describe('if-else branching', () => { @@ -3176,8 +3297,8 @@ describe('Workflow', async () => { .step(startStep) .if( async () => true, - new Workflow({ name: 'nested-workflow-c' }).step(otherStep), - new Workflow({ name: 'nested-workflow-d' }).step(otherStep), + new Workflow({ name: 'nested-workflow-c' }).step(otherStep).commit(), + new Workflow({ name: 'nested-workflow-d' }).step(otherStep).commit(), ) .then(finalStep) .commit(); diff --git a/packages/core/src/workflows/workflow.ts b/packages/core/src/workflows/workflow.ts index d212473e464..e736dc411de 100644 --- a/packages/core/src/workflows/workflow.ts +++ b/packages/core/src/workflows/workflow.ts @@ -25,6 +25,11 @@ import { isVariableReference, isWorkflow, updateStepInHierarchy, workflowToStep import type { WorkflowResultReturn } from './workflow-instance'; import { WorkflowInstance } from './workflow-instance'; +type WorkflowBuilder> = Pick< + T, + 'step' | 'then' | 'after' | 'while' | 'until' | 'if' | 'else' | 'afterEvent' | 'commit' +>; + export class Workflow< TSteps extends Step[] = Step[], TStepId extends string = string, @@ -51,6 +56,7 @@ export class Workflow< // registers stepIds on `after` calls #afterStepStack: string[] = []; #lastStepStack: string[] = []; + #lastBuilderType: 'step' | 'then' | 'after' | 'while' | 'until' | 'if' | 'else' | 'afterEvent' | null = null; #ifStack: { condition: StepConfig['when']; elseStepKey: string; @@ -101,13 +107,13 @@ export class Workflow< >( next: TWorkflow, config?: StepConfig, CondStep, VarStep, TTriggerSchema, Steps>, - ): this; + ): WorkflowBuilder; step< TStep extends StepAction, CondStep extends StepVariableType, VarStep extends StepVariableType, Steps extends StepAction[] = TSteps, - >(step: TStep, config?: StepConfig): this; + >(step: TStep, config?: StepConfig): WorkflowBuilder; step< TStepLike extends StepAction | Workflow, CondStep extends StepVariableType, @@ -126,7 +132,7 @@ export class Workflow< TTriggerSchema, Steps >, - ): this { + ): WorkflowBuilder { if (Array.isArray(next)) { const nextSteps: StepAction[] = next.map(step => { if (isWorkflow(step)) { @@ -182,7 +188,7 @@ export class Workflow< this.#steps[stepKey] = step; - const parentStepKey = this.#afterStepStack[this.#afterStepStack.length - 1]; + const parentStepKey = this.#getParentStepKey({ loop_check: true }); const stepGraph = this.#stepSubscriberGraph[parentStepKey || '']; const serializedStepGraph = this.#serializedStepSubscriberGraph[parentStepKey || '']; @@ -203,8 +209,130 @@ export class Workflow< this.#serializedStepGraph.initial.push(graphEntry); } this.#lastStepStack.push(stepKey); + this.#lastBuilderType = 'step'; + return this as WorkflowBuilder; + } - return this; + #__internalStep< + TWorkflow extends Workflow, + CondStep extends StepVariableType, + VarStep extends StepVariableType, + Steps extends StepAction[] = TSteps, + >( + next: TWorkflow, + config?: StepConfig, CondStep, VarStep, TTriggerSchema, Steps>, + internalUse?: boolean, + ): WorkflowBuilder; + #__internalStep< + TStep extends StepAction, + CondStep extends StepVariableType, + VarStep extends StepVariableType, + Steps extends StepAction[] = TSteps, + >( + step: TStep, + config?: StepConfig, + internalUse?: boolean, + ): WorkflowBuilder; + #__internalStep< + TStepLike extends StepAction | Workflow, + CondStep extends StepVariableType, + VarStep extends StepVariableType, + Steps extends StepAction[] = TSteps, + >( + next: TStepLike extends StepAction ? TStepLike : Workflow, + config?: StepConfig< + TStepLike extends StepAction + ? TStepLike + : TStepLike extends Workflow + ? ReturnType + : never, + CondStep, + VarStep, + TTriggerSchema, + Steps + >, + internalUse?: boolean, + ): WorkflowBuilder { + if (Array.isArray(next)) { + const nextSteps: StepAction[] = next.map(step => { + if (isWorkflow(step)) { + const asStep = step.toStep(); + return asStep; + } else { + return step as StepAction; + } + }); + nextSteps.forEach(step => this.#__internalStep(step, config, internalUse)); + this.after(nextSteps); + this.#__internalStep( + new Step({ + id: `__after_${next.map(step => step?.id ?? step?.name).join('_')}`, + execute: async ({ context }) => { + return { success: true }; + }, + }), + undefined, + internalUse, + ); + return this; + } + + const { variables = {} } = config || {}; + + const requiredData: Record = {}; + + // Add valid variables to requiredData + for (const [key, variable] of Object.entries(variables)) { + if (variable && isVariableReference(variable)) { + requiredData[key] = variable; + } + } + + const step: StepAction = isWorkflow(next) + ? // @ts-ignore + workflowToStep(next, { mastra: this.#mastra }) + : (next as StepAction); + + const stepKey = this.#makeStepKey(step); + const when = config?.['#internal']?.when || config?.when; + + const graphEntry: StepNode = { + step, + config: { + ...this.#makeStepDef(stepKey), + ...config, + loopLabel: config?.['#internal']?.loopLabel, + loopType: config?.['#internal']?.loopType, + serializedWhen: typeof when === 'function' ? when.toString() : when, + data: requiredData, + }, + }; + + this.#steps[stepKey] = step; + + const parentStepKey = this.#getParentStepKey(); + const stepGraph = this.#stepSubscriberGraph[parentStepKey || '']; + const serializedStepGraph = this.#serializedStepSubscriberGraph[parentStepKey || '']; + + // if we are in an after chain and we have a stepGraph + if (parentStepKey && stepGraph) { + // if the stepGraph has an initial, but it doesn't contain the current step, add it to the initial + if (!stepGraph.initial.some(step => step.step.id === stepKey)) { + stepGraph.initial.push(graphEntry); + if (serializedStepGraph) serializedStepGraph.initial.push(graphEntry); + } + // add the current step to the stepGraph + stepGraph[stepKey] = []; + if (serializedStepGraph) serializedStepGraph[stepKey] = []; + } else { + // Normal step addition to main graph + if (!this.#stepGraph[stepKey]) this.#stepGraph[stepKey] = []; + this.#stepGraph.initial.push(graphEntry); + this.#serializedStepGraph.initial.push(graphEntry); + } + this.#lastStepStack.push(stepKey); + this.#lastBuilderType = 'step'; + return this as WorkflowBuilder; } #makeStepKey(step: Step | Workflow) { @@ -299,6 +427,10 @@ export class Workflow< const stepGraph = this.#stepSubscriberGraph[parentStepKey || '']; const serializedStepGraph = this.#serializedStepSubscriberGraph[parentStepKey || '']; + if (parentStepKey && this.#lastBuilderType === 'after') { + return this.step(step, config); + } + if (parentStepKey && stepGraph && stepGraph[lastStepKey]) { stepGraph[lastStepKey].push(graphEntry); if (serializedStepGraph && serializedStepGraph[lastStepKey]) serializedStepGraph[lastStepKey].push(graphEntry); @@ -312,7 +444,8 @@ export class Workflow< this.#serializedStepGraph[lastStepKey].push(graphEntry); } - return this; + this.#lastBuilderType = 'then'; + return this as WorkflowBuilder; } private loop< @@ -323,7 +456,7 @@ export class Workflow< applyOperator: (op: string, value: any, target: any) => { status: string }, condition: StepConfig['when'], fallbackStep: FallbackStep, - loopType?: 'while' | 'until', + loopType: 'while' | 'until', ) { const lastStepKey = this.#lastStepStack[this.#lastStepStack.length - 1]; // If no last step, we can't do anything @@ -341,10 +474,14 @@ export class Workflow< execute: async ({ context }: any) => { if (typeof condition === 'function') { const result = await condition({ context }); - if (loopType === 'while') { - return { status: result ? 'continue' : 'complete' }; - } else { - return { status: result ? 'complete' : 'continue' }; + + switch (loopType) { + case 'while': + return { status: result ? 'continue' : 'complete' }; + case 'until': + return { status: result ? 'complete' : 'continue' }; + default: + throw new Error(`Invalid loop type: ${loopType}`); } } @@ -400,44 +537,43 @@ export class Workflow< }); // Then create a branch after the check step that loops back to the fallback step - this.after(checkStep) - .step(fallbackStep, { - when: async ({ context }) => { - const checkStepResult = context.steps?.[checkStepKey]; - if (checkStepResult?.status !== 'success') { - return WhenConditionReturnValue.ABORT; - } + this.after(checkStep); + this.#__internalStep(fallbackStep, { + when: async ({ context }) => { + const checkStepResult = context.steps?.[checkStepKey]; + if (checkStepResult?.status !== 'success') { + return WhenConditionReturnValue.ABORT; + } - const status = checkStepResult?.output?.status; - return status === 'continue' ? WhenConditionReturnValue.CONTINUE : WhenConditionReturnValue.CONTINUE_FAILED; - }, - '#internal': { - // @ts-ignore - when: condition!, - loopType: loopType!, - }, - }) - .then(checkStep, { - '#internal': { - loopLabel: `${fallbackStepKey} ${loopType} loop check`, - }, - }) - .step(loopFinishedStep, { - when: async ({ context }) => { - const checkStepResult = context.steps?.[checkStepKey]; - if (checkStepResult?.status !== 'success') { - return WhenConditionReturnValue.CONTINUE_FAILED; - } + const status = checkStepResult?.output?.status; + return status === 'continue' ? WhenConditionReturnValue.CONTINUE : WhenConditionReturnValue.CONTINUE_FAILED; + }, + '#internal': { + // @ts-ignore + when: condition!, + loopType: loopType!, + }, + }).then(checkStep, { + '#internal': { + loopLabel: `${fallbackStepKey} ${loopType} loop check`, + }, + }); + this.#__internalStep(loopFinishedStep, { + when: async ({ context }) => { + const checkStepResult = context.steps?.[checkStepKey]; + if (checkStepResult?.status !== 'success') { + return WhenConditionReturnValue.CONTINUE_FAILED; + } - const status = checkStepResult?.output?.status; - return status === 'complete' ? WhenConditionReturnValue.CONTINUE : WhenConditionReturnValue.CONTINUE_FAILED; - }, - '#internal': { - loopLabel: `${fallbackStepKey} ${loopType} loop finished`, - //@ts-ignore - loopType, - }, - }); + const status = checkStepResult?.output?.status; + return status === 'complete' ? WhenConditionReturnValue.CONTINUE : WhenConditionReturnValue.CONTINUE_FAILED; + }, + '#internal': { + loopLabel: `${fallbackStepKey} ${loopType} loop finished`, + //@ts-ignore + loopType, + }, + }); return this; } @@ -466,7 +602,13 @@ export class Workflow< } }; - return this.loop(applyOperator, condition, fallbackStep, 'while'); + const res = this.loop(applyOperator, condition, fallbackStep, 'while') as Pick< + WorkflowBuilder, + 'then' | 'commit' + >; + this.#lastBuilderType = 'while'; + + return res; } until< @@ -496,7 +638,12 @@ export class Workflow< } }; - return this.loop(applyOperator, condition, fallbackStep, 'until'); + const res = this.loop(applyOperator, condition, fallbackStep, 'until') as Pick< + WorkflowBuilder, + 'then' | 'commit' + >; + this.#lastBuilderType = 'until'; + return res; } if>( @@ -566,7 +713,8 @@ export class Workflow< const elseStepKey = `__${lastStep.id}_else`; this.#ifStack.push({ condition, elseStepKey, condStep: lastStep }); - return this; + this.#lastBuilderType = 'if'; + return this as WorkflowBuilder; } else() { @@ -594,16 +742,19 @@ export class Workflow< }, ); - return this; + this.#lastBuilderType = 'else'; + return this as WorkflowBuilder; } - after>(steps: TStep | TStep[]): Omit; + after>( + steps: TStep | TStep[], + ): Omit, 'then' | 'after'>; after>( steps: TWorkflow | TWorkflow[], - ): Omit; + ): Omit, 'then' | 'after'>; after | Workflow>( steps: TStep | Workflow | (TStep | Workflow)[], - ): Omit { + ): Omit, 'then' | 'after'> { const stepsArray = Array.isArray(steps) ? steps : [steps]; const stepKeys = stepsArray.map(step => this.#makeStepKey(step)); @@ -617,7 +768,8 @@ export class Workflow< this.#serializedStepSubscriberGraph[compoundKey] = { initial: [] }; } - return this as Omit; + this.#lastBuilderType = 'after'; + return this as Omit, 'then' | 'after'>; } afterEvent(eventName: string) { @@ -646,7 +798,8 @@ export class Workflow< this.after(lastStep).step(eventStep).after(eventStep); - return this; + this.#lastBuilderType = 'afterEvent'; + return this as WorkflowBuilder; } /** @@ -751,6 +904,19 @@ export class Workflow< return this.#runs.get(runId)?.executionSpan; } + #getParentStepKey({ loop_check } = { loop_check: false }) { + let parentStepKey = undefined; + for (let i = this.#afterStepStack.length - 1; i >= 0; i--) { + const stepKey = this.#afterStepStack[i]; + if (stepKey && this.#stepSubscriberGraph[stepKey] && (loop_check ? !stepKey.includes('loop_check') : true)) { + parentStepKey = stepKey; + break; + } + } + + return parentStepKey; + } + #makeStepDef[]>( stepId: TStepId, ): StepDef[TStepId] {