Commit 4c0e286
authored
Fix evented wflow nested loop (#16312)
## Description
<!-- Provide a brief description of the changes in this PR -->
Fix evented wflow nested loop
## Related Issue(s)
<!-- Link to the issue(s) this PR addresses, using hashtag notation:
Fixes #123 -->
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [x] Test update
## Checklist
- [ ] I have made corresponding changes to the documentation (if
applicable)
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have addressed all Coderabbit comments on this PR
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## ELI5 (Explain Like I'm 5)
This PR fixes a bug where a workflow run started inside a loop (a
"nested workflow") didn't let its parent loop resume or finish
correctly. It detects when a finished child run belongs to a loop step
in the parent and routes the completion into the loop handler so the
parent can continue; tests ensure nested do-until and do-while loops
work.
## Changes Made
### Bug Fix: Nested Loop Handling in Evented Workflows
- processWorkflowEnd now inspects
parentWorkflow.stepGraph[parentWorkflow.executionPath[0]] and, if that
step is a loop, calls processWorkflowLoop with the parent workflow's
richer execution metadata so the parent loop resumes/handles the nested
run correctly. If the parent step is not a loop it falls back to
publishing a workflow.step.end for the parent (including parentContext
and nestedRunId).
- Added cleanupRun to remove abort controllers, parent-child
relationship entries, and runFormats when a workflow completes;
cancelRunAndChildren recursively aborts child runs.
- parentChildRelationships map added to track child runId → parent runId
for nested workflows and used during cancellation/cleanup.
### Type/API Updates
- Expanded ParentWorkflow public type
(packages/core/src/workflows/evented/workflow-event-processor/index.ts)
to include execution/resume fields required by nested handling:
- Added: stepGraph: StepFlowEntry[], activeSteps: Record<string,
boolean>, resumeSteps: string[], resumeData: any, input: any
- Added optional: parentContext?: { workflowId: string; input: any }
- Retained existing fields (workflowId, runId, executionPath, resume,
stepResults, stepId, parentWorkflow?)
### Tests
- workflows/_test-utils/src/domains/loops.ts: expanded test utilities to
add nested loop coverage:
- New nested dountil and nested dowhile workflow registrations (inner
increment workflows used as loop bodies) and corresponding mocks.
- Tests exercise nested-until and nested-while scenarios and include
other loop cases (immediate-exit, non-nested loops).
### Release/Packaging
- .changeset/rare-bikes-wear.md: new changeset marking a patch for
@mastra/core describing "Fix nested loops in evented workflow".
## Files Touched (high level)
- packages/core/src/workflows/evented/workflow-event-processor/index.ts
— core logic: processWorkflowEnd branching, ParentWorkflow type
expansion, abort controller & parent-child tracking and cleanup.
- workflows/_test-utils/src/domains/loops.ts — added nested loop
workflow definitions and test mocks.
- .changeset/rare-bikes-wear.md — changeset entry.
## Impact
- Fixes nested-loop completion/resume behavior for evented workflows and
adds tests to prevent regressions.
- Low-risk patch scope but touches core event-processing logic;
reviewers should focus on correctness of processWorkflowEnd →
processWorkflowLoop interaction, cleanup/cancellation semantics, and the
ParentWorkflow shape used across event payloads.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent c6eb39e commit 4c0e286
3 files changed
Lines changed: 291 additions & 21 deletions
File tree
- .changeset
- packages/core/src/workflows/evented/workflow-event-processor
- workflows/_test-utils/src/domains
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 70 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
62 | 71 | | |
63 | 72 | | |
64 | 73 | | |
| |||
361 | 370 | | |
362 | 371 | | |
363 | 372 | | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
385 | 422 | | |
386 | 423 | | |
387 | 424 | | |
| |||
846 | 883 | | |
847 | 884 | | |
848 | 885 | | |
| 886 | + | |
849 | 887 | | |
850 | 888 | | |
851 | 889 | | |
852 | 890 | | |
853 | 891 | | |
| 892 | + | |
| 893 | + | |
854 | 894 | | |
855 | 895 | | |
856 | 896 | | |
| |||
909 | 949 | | |
910 | 950 | | |
911 | 951 | | |
| 952 | + | |
912 | 953 | | |
913 | 954 | | |
914 | 955 | | |
915 | 956 | | |
916 | 957 | | |
| 958 | + | |
| 959 | + | |
917 | 960 | | |
918 | 961 | | |
919 | 962 | | |
| |||
961 | 1004 | | |
962 | 1005 | | |
963 | 1006 | | |
| 1007 | + | |
964 | 1008 | | |
965 | 1009 | | |
966 | 1010 | | |
967 | 1011 | | |
968 | 1012 | | |
969 | 1013 | | |
| 1014 | + | |
| 1015 | + | |
970 | 1016 | | |
971 | 1017 | | |
972 | 1018 | | |
| |||
990 | 1036 | | |
991 | 1037 | | |
992 | 1038 | | |
| 1039 | + | |
993 | 1040 | | |
994 | 1041 | | |
995 | 1042 | | |
996 | 1043 | | |
997 | 1044 | | |
998 | 1045 | | |
| 1046 | + | |
| 1047 | + | |
999 | 1048 | | |
1000 | 1049 | | |
1001 | 1050 | | |
| |||
0 commit comments