0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00
mastra/workflows/_test-utils
Arthur Fiorette 7277766d6c
fix(core): reset retryCount on each evented loop iteration (#30383)
## Description

On the evented engine, each iteration of a `dountil`/`dowhile` loop
whose body is a step now starts with `retryCount` 0 and gets the step's
full `retries`, as on the default engine.

### Why

The evented engine carried `retryCount` from one iteration into the
next, so iteration N started at N-1 plus every retry earlier iterations
had used. A body with `retries` ran out of budget on a later iteration:
with `retries: 1`, a single failure in iteration 3 failed the run
instead of being retried. Step code reading `retryCount` also saw that
inflated number. The default engine has counted retries per iteration
since #23593.

```ts
const body = createStep({ id: 'body', retries: 1, execute: async ({ retryCount }) => { /* ... */ } });
workflow.dountil(body, async ({ iterationCount }) => iterationCount >= 4).commit();

// retryCount seen by the body when iteration 3 fails once
// before: 0, 1, 2 and the run fails
// after:  0, 0, 0, 1, 0 and the run succeeds
```

### Behavior change

- Step code that reads `retryCount` inside an evented loop body now gets
the attempt within the current iteration. The iteration number is
available to the loop condition as `iterationCount`.
- The loop condition's `retryCount` is now the body's attempt in the
current iteration on every path. One path used to pass attempt + 1. The
default engine passes -1 to the condition; that difference is left as
is.

### Not affected

`foreach` loops, and loops whose body is a nested workflow (those
already started each iteration at 0).

### Tests

New case in the shared workflow suite
(`workflows/_test-utils/src/domains/run-count.ts`): a `dountil` body
with `retries: 1` that throws once in iteration 3. It runs on the
default and evented engines and is skipped on Inngest with the other
`retryCount` cases. Without the fix it fails on the evented engine with
`expected [ 0, 1, 2 ] to deeply equal [ 0, 0, 0, 1, 0 ]`.

## Related issue(s)

Linear PLTFRM-3202. No GitHub issue.

## 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
- [ ] Test update

## Checklist

- [x] I have linked the related issue(s) in the description above
- [ ] I have made corresponding changes to the documentation (if
applicable)
- [x] 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
-->

<!-- coderabbit-ai-generated:v1 -->

ELI5: When a loop step fails and retries, the retry count from earlier
loop passes was carried into later passes. This could use up the retry
limit early. This change resets the count so each loop pass gets its
full retry limit.

## Summary

This pull request changes how the evented workflow engine tracks
`retryCount` for `dountil` and `dowhile` loops whose body is a step.

## Changes

-
`packages/core/src/workflows/evented/workflow-event-processor/loop.ts`:
When a loop iteration runs again, `processWorkflowLoop` passes
`retryCount: 0` instead of the current retry count. Each iteration
starts with the body's full retry budget.
-
`packages/core/src/workflows/evented/workflow-event-processor/index.ts`:
In `runLeafStep`, the loop-condition path passes `retryCount` to
`#processLoop` without adding 1.
- `.changeset/bright-terms-crash.md`: Adds a patch changeset for
`@mastra/core`. It documents that `dountil` and `dowhile` body retries
start at 0 on each iteration. It also states that `retryCount` is scoped
to the current iteration.
- `workflows/_test-utils/src/domains/run-count.ts`: Adds
`createRunCountWorkflows` and `createRunCountTests`. The new workflow is
a loop whose body allows one retry and throws once on the third
iteration. The test asserts that the run succeeds with recorded retry
counts `[0, 0, 0, 1, 0]` and final output `4`.

Diff size: 4 files, 63 lines added, 2 lines removed.

## Not changed

- The default engine's behavior is unchanged.
- `foreach` loops and loops with nested workflow bodies are not affected
by the diff shown.

## Verification

- The shared test case runs on the default and evented engines. It is
skipped on Inngest with the other `retryCount` cases.
- The author reports that, without the fix, the evented case fails with
`expected [ 0, 1, 2 ] to deeply equal [ 0, 0, 0, 1, 0 ]`. The author
reports this result. This review did not run the tests.
- CI results: not available.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
History 2026-11-14 01:01:42 -03:00
..
src fix(core): reset retryCount on each evented loop iteration (#30383) 2026-11-14 01:01:42 -03:00
CHANGELOG.md chore: version - exit prerelease mode 2026-09-23 09:15:38 +00:00
lint-staged.config.js chore: migrate repository formatting to oxfmt (#19807) 2026-07-24 16:18:33 +02:00
package.json chore(security): Vanta 2026-10-30 - zod ^4.6.7, @hono/node-server 2.1.5 (direct bumps) (#28983) 2026-10-30 15:30:35 -04:00
tsconfig.json test(workflows): typecheck the workflow conformance suite (#28225) 2026-10-26 16:34:37 +01:00
vitest.config.ts feat(core): re-enable the evented workflow engine on the durable agent loop and refactor the agent loop structure (#24569) 2026-09-24 22:27:04 -05:00