> ## Documentation Index
> Fetch the complete documentation index at: https://docs.canary.effectiveai.app/llms.txt
> Use this file to discover all available pages before exploring further.

# repeat node

> Run a block of steps in bounded rounds until a round's result passes a test.

A `repeat` runs one heading body in rounds. After each round it tests a value the round produced,
and either stops or starts another round, up to a fixed limit. Use it for review and rework
cycles, objection rounds and verification loops: work that runs at least once and whose next step
depends on what the round produced. Use [`for-each`](https://docs.canary.effectiveai.app/procedures/reference/nodes/for-each) to run
the same steps once per item of a list.

## Syntax

```yaml repeat id=review-notice theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
body: '#review-round'
on_continue: '#revise-notice'
carry:
  current_draft:
    schema: notice-draft
    initial: $inputs.draft
until:
  value: $review.value.verdict
  in: [approved]
max_rounds: 3
on_exhausted:
  fail: notice-not-approved
next:
  current_draft: $revised.value
collect:
  feedback_history:
    schema: feedback-list
    from: $review.value.feedback
outputs:
  approved_notice:
    schema: notice-draft
    from: $current_draft
```

## Fields

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `body` | quoted `'#block'` | Yes | None | The child heading that runs once per round |
| `on_continue` | quoted `'#block'` | No | None | A child heading that runs only between rounds, after a round that didn't pass `until` and before the next one |
| `carry` | mapping | No | None | State the next round reads. Each entry has a `schema` and a required `initial` value |
| `until.value` | reference | Yes | None | A value the round body produces. It is tested after every round |
| `until.in` | list | One of `in`, `not-in` | None | Stop when `until.value` equals any of these values |
| `until.not-in` | list | One of `in`, `not-in` | None | Stop when `until.value` equals none of these values |
| `max_rounds` | positive integer | Yes | None | The most rounds the loop may run, at most 500 |
| `on_exhausted` | `continue` or `fail` mapping | Yes | None | What happens when the last allowed round doesn't pass `until`. See [When rounds run out](#when-rounds-run-out) |
| `next` | mapping | With `carry` | None | A new value for every `carry` entry, used by the next round |
| `collect` | mapping | No | None | Lists gathered from every round. Each entry has an array `schema` and a `from` reference |
| `outputs` | mapping | No | None | Values from the final round that later steps can read. Each entry has a `schema` and a `from` reference |

The fence takes no attributes after `id`, and its body takes no other fields.

`$round` is the current round number, starting at 1. It can be used in the round body and in
`on_continue`, including in `title` and `instructions` templates. Inside `on_continue` it is the
number of the round that just finished.

### Round values and carry

A round's steps produce values that only that round can see. Those values drive `until`, and
`collect` and `outputs` copy them out of the loop.

`carry` is different: it is state the next round reads, such as a draft that gets revised. Many
loops need no carry.

* Each carry entry needs a real `initial` value. A value with no sensible starting value is a
  round value, not carry.
* `next` must give every carry entry a new value. Its expressions can read round values,
  `on_continue` values and the current carry. All `next` entries are computed together from the
  finished round, so one entry never sees another's new value.
* A carry name can't reuse a name that is already visible, such as a procedure input.

### `until`

`until.value` must be a value the round body produces on every path. It can't be carry or a value
from outside the loop, because those don't change during a round. The values in `in` or `not-in`
are typed YAML values that must fit the tested member's schema, for example `in: [true]` for a
boolean.

### `on_continue`

`on_continue` runs only when the loop has already decided to run another round. It never runs
before round 1 or after the last round. Put rework there, not in the round body:

| Round body | `on_continue` body |
| - | - |
| Runs once in every round | Runs only between rounds |
| Produces the result `until` tests | Responds to a round that didn't pass |
| Runs in the first and last round | Never runs before round 1 or after the last round |

`on_continue` can read the finished round's values, the carry and values from outside the loop.
Its own values can be used by `next` and then disappear.

### When rounds run out

When the round numbered `max_rounds` finishes without passing `until`:

* `on_exhausted: continue` closes the loop with the final round's `outputs` and `collect`, and the
  run continues. Route on the result afterwards if running out is a business outcome.
* `on_exhausted: { fail: <code> }` fails the run. The run's `failure.code` is `repeat_exhausted`,
  and its message is your code, such as `notice-not-approved`. Codes are lowercase and hyphenated.
  Nothing is copied out of the loop.

### Headings

The repeat follows the same heading rules as a [`choice`](https://docs.canary.effectiveai.app/procedures/reference/nodes/choice#heading-bodies):
the repeat is the last fence in its section, `body` and `on_continue` are direct child headings of
that section, placed first and together, and the run continues at the next heading after them.
`on_continue` sits beside the round body, not inside it.

A round body can hold any steps, including tasks, agents, waits and nested constructs. If a round
body reaches [`complete`](https://docs.canary.effectiveai.app/procedures/reference/nodes/complete), the run ends.

## Example

Compliance reviews a cancellation notice. If it asks for changes, an agent session revises the
draft and compliance reviews again, at most three times. The group and policy values are
illustrative.

````markdown notice-rework.procedure.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure@v0
id: notice-rework
inputs:
  policy_number: { type: string }
  draft: { schema: notice-draft }
outputs:
  result: { schema: notice-result }
---

# Notice rework

Review a cancellation notice until compliance approves it, at most three times.

## Review the notice

```yaml repeat id=review-notice
body: '#review-round'
on_continue: '#revise-notice'
carry:
  current_draft:
    schema: notice-draft
    initial: $inputs.draft
until:
  value: $review.value.verdict
  in: [approved]
max_rounds: 3
on_exhausted:
  fail: notice-not-approved
next:
  current_draft: $revised.value
collect:
  feedback_history:
    schema: feedback-list
    from: $review.value.feedback
outputs:
  approved_notice:
    schema: notice-draft
    from: $current_draft
```

### Review round {#review-round}

Compliance reads the current draft and approves it or asks for changes.

```yaml task id=compliance-review
assignees: [group:compliance]
title: 'Review cancellation notice for {{ $inputs.policy_number }} (round {{ $round }})'
instructions: |
  Check the notice text below against the state's cancellation rules.

  {{ $current_draft.text }}
input:
  schema: notice-draft
  value: $current_draft
result:
  schema: review-verdict
output: review
```

### Revise the notice {#revise-notice}

Runs only when compliance asked for changes and another round is left.

```yaml agent id=revise
title: 'Revise notice for {{ $inputs.policy_number }}'
instructions: |
  Revise the notice to address this feedback: {{ $review.value.feedback }}
input:
  schema: revise-input
  value:
    draft: $current_draft
    feedback: $review.value.feedback
result:
  schema: notice-draft
agent:
  preset: general-agent
output: revised
```

## Return the approved notice

```yaml complete id=finish
value:
  notice: $approved_notice
  feedback: $feedback_history
```

## Resources

```yaml schema id=notice-draft
type: object
additionalProperties: false
required: [text]
properties:
  text: { type: string }
```

```yaml schema id=review-verdict
type: object
additionalProperties: false
required: [verdict, feedback]
properties:
  verdict: { enum: [approved, changes-requested] }
  feedback: { type: string }
```

```yaml schema id=revise-input
type: object
additionalProperties: false
required: [draft, feedback]
properties:
  draft: { $ref: '#/schemas/notice-draft' }
  feedback: { type: string }
```

```yaml schema id=feedback-list
type: array
maxItems: 3
items: { type: string }
```

```yaml schema id=notice-result
type: object
additionalProperties: false
required: [notice, feedback]
properties:
  notice: { $ref: '#/schemas/notice-draft' }
  feedback: { $ref: '#/schemas/feedback-list' }
```
````

If compliance approves in round 2, the loop runs the agent once, between rounds 1 and 2. The run
ends with the round 2 draft and two feedback entries. If round 3 still asks for changes, the run
fails with `failure.code` `repeat_exhausted` and the message `notice-not-approved`.

## On retry and resume

* **Each round is its own step.** A step in round 2 is a different occurrence from the same step
  in round 1, with its own identity. Each round's task, agent session or call is created once, and
  an effect's `operation.idempotencyKey` differs between rounds.
* **Collected values are kept.** A finished round's `collect` values are recorded when the round
  ends. A resumed run doesn't gather them again.
* **Exits happen in one step.** When the loop ends, its `outputs` and `collect` lists become
  visible together. If the loop's end also ends an enclosing body, both close together.
* **The loop's own steps record nothing.** Testing `until`, computing `next` and starting the next
  round don't call anything. Steps inside the bodies behave as they do anywhere else.

## Common errors

| Code | Cause | Fix |
| - | - | - |
| `PROC-REPEAT-001` | An unknown or missing field, a bad `until` list, a bad `on_exhausted`, a bad cap such as `max_rounds: 0` or above 500 (`max_rounds must be at most the pinned limit of 500.`), or `next` missing with `carry` or present without it | Fix the field; give every carry entry an `initial` and a `next` value |
| `PROC-REPEAT-002` | A `next` expression reads a name that neither the round nor `on_continue` binds | Read a round value, an `on_continue` value or the current carry |
| `PROC-REPEAT-003` | `until.value` tests carry or an outside value (`until.value must test a round output, not carry or an enclosing value.`), a reference in `until`, `collect` or `outputs` is unknown or optional, an `in` value doesn't fit the schema, or a round value isn't bound on every path | Test a value the round body binds on every path |
| `PROC-REGION-001` to `PROC-REGION-003` | A body heading is missing, misplaced or used by another construct | Follow the [heading rules](https://docs.canary.effectiveai.app/procedures/reference/nodes/choice.md#heading-bodies) |
| `PROC-GRAPH-005` | A carry or output name collides with a name already visible, such as a procedure input | Rename the carry or output |

## See also

* [for-each node](https://docs.canary.effectiveai.app/procedures/reference/nodes/for-each.md): one run of the body per list item
* [choice node](https://docs.canary.effectiveai.app/procedures/reference/nodes/choice.md): routing inside a round, and the heading rules
* [task node](https://docs.canary.effectiveai.app/procedures/reference/nodes/task.md) and [agent node](https://docs.canary.effectiveai.app/procedures/reference/nodes/agent.md): review and rework steps
* [Writes to your systems](https://docs.canary.effectiveai.app/procedures/concepts/writes-to-your-systems.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.