> ## 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.

# wait node

> Pause a run for a fixed time, until an instant, until an outside system reports a state, or until a person answers a task.

A `wait` pauses the run without holding any process open, so a run can wait minutes or days and
still pick up where it left off. Use `until` when you know the wake time in advance, a polling
wait when you have to keep asking an outside system, and a task wait when you created a task with
`mode: create` and did something else before waiting for the answer.

A `wait` has five forms. Each body must match exactly one of them.

| Form | Body fields | Ends when |
| - | - | - |
| Duration | `duration` | The time has passed |
| Instant | `until` | The clock reaches the instant |
| Polling | `poll`, `until`, `every`, `timeout`, `output`, `on_timeout`, optional `next_poll` | A result matches `until`, or `timeout` runs out |
| RSL log | `rsl_log`, `until`, `timeout`, `output`, `on_timeout` | The log's decision matches, or `timeout` runs out |
| Task | `task`, `output` | A person answers the task |

## Syntax

### Duration

```yaml wait id=let-billing-settle theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
duration: 10m
```

### Instant

```yaml wait id=wait-for-effective-date theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
until: $inputs.cancel_at
```

### Polling

```yaml wait id=wait-for-payment theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
poll:
  function: '@local.get-payment'
  input:
    paymentId: $inputs.payment_id
until:
  path: status
  in: [settled, returned]
  schema: payment-final
every: 15m
next_poll:
  path: retryAfterSeconds
  unit: s
  at_most: 6h
timeout: 5d
output: payment
on_timeout:
  fail: refund-payment-not-final
```

### RSL log

```yaml wait id=wait-for-decision theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl_log: { program: northwind-small-commercial, log: $inputs.log }
until: { path: decision, in: [clear, refused] }
timeout: 5d
output: case_status
on_timeout: { fail: decision-not-reached }
```

### Task

```yaml wait id=wait-for-refund-approval theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
task: $approval_task
output: approval
```

## Fields

### Durations

`duration`, `every`, `timeout` and `next_poll.at_most` take a duration: a positive whole number
followed by one unit, `ms`, `s`, `m`, `h` or `d`. For example `90s`, `15m` or `5d`. Fractions,
signs, spaces and combined units such as `1h30m` are invalid.

### Duration form

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `duration` | duration | Yes | None | How long to wait before the next step runs |

### Instant form

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `until` | reference or quoted string | Yes | None | The instant at which to continue |

`until` is either one reference or one quoted RFC 3339 date-time, such as
`'2026-12-31T05:00:00Z'`. A reference must point to a value declared as
`{ type: string, format: date-time }`; a plain `string` is refused. Templates, `??` and built
values are invalid. An instant that has already passed continues at once.

The instant form binds no output. Later steps keep reading the original value.

### Polling form

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `poll.function` | callable reference | Yes | None | The effect to call on each poll: a local `ts effect` (`@local.<id>`) or a catalog effect, such as an Effective API operation |
| `poll.input` | object | Yes | None | The input sent on every poll. It is checked against the function's input schema before each call |
| `until.path` | dot-separated member path | Yes | None | The member of the function's result to test, such as `status`. No `$` prefix and no array index |
| `until.equals` | scalar | One of | None | End the wait when the member equals this value |
| `until.in` | list of scalars | One of | None | End the wait when the member equals any value in this nonempty list |
| `until.schema` | schema id | Yes | None | The schema of a matching result. It becomes the type of `output` |
| `every` | duration | Yes | None | The time between polls, and the shortest interval a `next_poll` hint can ask for |
| `next_poll.path` | dot-separated member path | No | None | An `integer` member of the result that says how long to wait before the next poll |
| `next_poll.unit` | `ms`, `s`, `m` or `h` | With `next_poll` | None | The unit of that integer |
| `next_poll.at_most` | duration | No | No cap | The longest interval a hint can ask for. Must be at least `every` |
| `timeout` | duration | Yes | None | The total time the wait may take, counted from when polling starts |
| `output` | name | Yes | None | The name the matching result, or the `on_timeout` value, is bound to |
| `on_timeout` | object | Yes | None | Exactly one of `fail` or `value`. See below |

Use exactly one of `until.equals` and `until.in`.

**The function must have an effect.** A pure `ts function` can't be polled: called with the same
input, it can't see anything change. See [Functions and effects](https://docs.canary.effectiveai.app/procedures/reference/nodes/function-and-effect.md).

**The matched schema narrows the result.** `until.schema` describes a result that has matched. It
must have the same shape as the function's result schema. It may only:

* add existing property names to `required`;
* replace an `enum` with a nonempty subset of its values; or
* replace an `enum` with a `const` that was one of its values.

It must require `until.path` and admit the `equals` value, or every value in `in`. This lets a
function return `{ status: pending }` while it waits and a typed `{ status: settled, ... }` when it
is done.

When the member at `until.path` is a plain string in the result schema, with no `enum`, the
matched schema keeps it a plain string. The wait still ends on the values you list, but a later
[`choice`](https://docs.canary.effectiveai.app/procedures/reference/nodes/choice) can't route on that member directly. Derive a
closed value first with a pure `ts function` called through [`call`](https://docs.canary.effectiveai.app/procedures/reference/nodes/call).
Results of Effective API operations are the common case: their string values stay open so new
values don't break running procedures.

**A hint sets the next interval.** With `next_poll`, after a result that doesn't match, the next
interval is the hinted number times `unit`, raised to at least `every` and lowered to at most
`at_most`. A missing or `null` hint uses `every`. A hint can never extend the wait past `timeout`.
Without `next_poll`, polls are `every` apart. There is no other backoff syntax: a function that
wants backoff returns growing hints.

**`on_timeout` ends the wait when time runs out.**

* `fail: <code>` fails the run. The code is lowercase words joined by hyphens, such as
  `refund-payment-not-final`. The run's `failure.code` is `poll_timeout`, and its message is your
  code. Nothing is bound.
* `value: <object>` binds this value to `output` and continues. The value may hold literals and
  plain references, but no templates, optional references or `??`. It must match `until.schema`.

### RSL log form

Use this form to wait until an [RSL submission log](https://docs.canary.effectiveai.app/rsl/programs-on-effective.md#submission-logs)
on one of your programs reaches a decision.

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `rsl_log.program` | string | Yes | None | The program's ID, written as a literal. It can't be a reference |
| `rsl_log.log` | reference or string | Yes | None | The log's ID |
| `until` | object | Yes | None | As for a polling wait, tested against the log's status: `path`, then `equals` or `in` |
| `until.schema` | schema id | No | The status result | A narrower schema for the match, checked by the polling rules |
| `timeout` | duration | Yes | None | The total time the wait may take |
| `output` | name | Yes | None | Bound to the log's status `{ seq, status, decision }`, or to the `on_timeout` value |
| `on_timeout` | object | Yes | None | Exactly one of `fail` or `value`, as for a polling wait |

An RSL log wait reads the log's status, the same result as
[Get the status of a log](https://docs.canary.effectiveai.app/api-reference/your-programs/get-the-status-of-a-log.md). It has no
`poll`, `every` or `next_poll`.

* It reads the status when it starts. Every write to the log wakes it to read again at once. It
  also reads every 15 minutes in case a wake is lost.
* It routes on `decision`: `refused`, `incomplete`, `referred`, `clear`, `bound` or `closed`.
  `decision` is deprecated in favor of `status.status`; see the status operation for its dates.
* A log whose decision is `bound` or `closed` takes no more writes, so it can never match later.
  If a read returns one of them and it doesn't match `until`, the wait applies `on_timeout` at
  once. List `bound` or `closed` in `until` to end on them as a match instead.
* A read the API refuses, such as an unknown log or a program the run can't read, fails the run.

### Task form

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `task` | reference | Yes | None | The task handle from a [`task`](https://docs.canary.effectiveai.app/procedures/reference/nodes/task) with `mode: create` |
| `output` | name | Yes | None | The name the person's answer is bound to |

`task` is the exact handle, such as `$approval_task`. A member such as `$approval_task.url` is
invalid. The wait doesn't create the task again or change who it is assigned to. `output` has the
same shape as a blocking task's output, so read the answer under `.value`, such as
`$approval.value.approved`.

## Example

Wait ten minutes before the next step:

```yaml wait id=let-billing-settle theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
duration: 10m
```

### Cancel on the effective date

This procedure holds a Northwind Mutual cancellation until the effective date and time the caller
passes in. The instant input needs a `date-time` schema.

````markdown cancellation-on-effective-date.procedure.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure@v0
id: cancellation-on-effective-date

inputs:
  policy_number: { type: string }
  cancel_at: { schema: instant }
outputs:
  cancellation: { schema: cancellation }
---

# Cancel on the effective date

Hold the cancellation until its effective date and time, then record it.

## Wait for the effective date

The run sleeps until `cancel_at`. If that time has already passed, it continues at once.

```yaml wait id=wait-for-effective-date
until: $inputs.cancel_at
```

## Record the cancellation

```yaml complete id=cancelled
output: cancellation
value:
  policyNumber: $inputs.policy_number
  effectiveAt: $inputs.cancel_at
```

## Resources

```yaml schema id=instant
type: string
format: date-time
```

```yaml schema id=cancellation
type: object
additionalProperties: false
required: [policyNumber, effectiveAt]
properties:
  policyNumber: { type: string }
  effectiveAt: { type: string, format: date-time }
```
````

### Wait for a refund payment

This procedure asks an illustrative billing system, `billing.northwind.example`, about a refund
payment until it is settled or returned. The billing system can say how long to wait before
asking again; the wait never asks more often than every 15 minutes or less often than every 6
hours, and gives up after 5 days.

````markdown wait-for-refund-payment.procedure.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure@v0
id: wait-for-refund-payment

inputs:
  policy_number: { type: string }
  payment_id: { type: string }
outputs:
  refund: { schema: refund-result }
---

# Wait for a refund payment

Wait until the billing system reports the refund payment as settled or returned.

## Wait for the payment

```yaml wait id=wait-for-payment
poll:
  function: '@local.get-payment'
  input:
    paymentId: $inputs.payment_id
until:
  path: status
  in: [settled, returned]
  schema: payment-final
every: 15m
next_poll:
  path: retryAfterSeconds
  unit: s
  at_most: 6h
timeout: 5d
output: payment
on_timeout:
  fail: refund-payment-not-final
```

## Return the result

```yaml complete id=refund-final
output: refund
value:
  policyNumber: $inputs.policy_number
  paymentId: $inputs.payment_id
  status: $payment.status
```

## Resources

```ts effect id=get-payment input=payment-request result=payment-observation uses=fetch
async ({ paymentId }, { fetch, signal }) => {
  const response = await fetch(`https://billing.northwind.example/payments/${paymentId}`, {
    signal,
  });
  if (!response.ok) throw new Error(`Billing returned ${response.status}`);
  const body: unknown = await response.json();
  if (
    typeof body !== 'object' ||
    body === null ||
    !('status' in body) ||
    (body.status !== 'pending' && body.status !== 'settled' && body.status !== 'returned')
  ) {
    throw new Error('Billing returned an unexpected payment');
  }
  const hint =
    'retryAfterSeconds' in body && typeof body.retryAfterSeconds === 'number'
      ? Math.ceil(body.retryAfterSeconds)
      : null;
  return { status: body.status, retryAfterSeconds: hint };
};
```

```yaml schema id=payment-request
type: object
additionalProperties: false
required: [paymentId]
properties:
  paymentId: { type: string }
```

```yaml schema id=payment-observation
type: object
additionalProperties: false
required: [status, retryAfterSeconds]
properties:
  status: { enum: [pending, settled, returned] }
  retryAfterSeconds: { type: [integer, 'null'] }
```

```yaml schema id=payment-final
type: object
additionalProperties: false
required: [status, retryAfterSeconds]
properties:
  status: { enum: [settled, returned] }
  retryAfterSeconds: { type: [integer, 'null'] }
```

```yaml schema id=refund-result
type: object
additionalProperties: false
required: [policyNumber, paymentId, status]
properties:
  policyNumber: { type: string }
  paymentId: { type: string }
  status: { enum: [settled, returned] }
```
````

`payment-final` narrows `status` to the two values that end the wait, so a later `choice` on
`$payment.status` only has to cover `settled` and `returned`.

### Create a task, then wait for it

A `task` with `mode: create` returns a handle as soon as the task exists. Steps between the task
and the wait can use the handle, for example to post `$approval_task.url` to your team's chat.

```yaml task id=create-refund-approval theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
mode: create
assignees:
  - user:underwriting.lead@northwind.example
title: Approve the refund for {{ $inputs.policy_number }}?
instructions: |
  Check the refund amount against the cancellation worksheet, then approve or deny.
result:
  schema: refund-approval
output: approval_task
```

```yaml wait id=wait-for-refund-approval theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
task: $approval_task
output: approval
```

`$approval.value.approved` then holds the answer.

## On retry and resume

* **A waiting run holds nothing open.** It resumes after restarts and deployments and wakes at
  the same time it would have.
* **An instant is fixed once.** `until` is read once, when the wait starts, and the instant is
  recorded. A value that isn't an RFC 3339 date-time fails the run with `schema_mismatch`.
* **A polling wait has one deadline.** It is set when polling starts. Time spent inside the
  function counts against it, as does the time between polls.
* **Polls never overlap.** The next poll starts only after the previous one has finished.
* **Each poll is its own call.** Each call has its own stable identity. If a poll's outcome can't
  be determined, the run fails for you to check, rather than polling again.
* **Effective API reads keep trying.** When an Effective API read gets no usable answer (HTTP
  408, 429 or 5xx, or no response), the wait repeats the same read until the API answers or the
  deadline passes. An outage of hours doesn't end a wait of days. A read the API refuses (any other
  4xx) fails the run.
* **The deadline wins once.** When the deadline passes during a poll, that poll is cancelled and
  `on_timeout` is applied once. A result that arrives later is recorded but can't reopen the wait
  or bind `output`.
* **Only the match binds.** Results that don't match are recorded in the run's history but never
  bound. The matching result, or the `on_timeout` value, binds to `output` in one step.
* **Hints replay the same way.** The interval after a hint is worked out from the recorded result,
  so a resumed run keeps the same schedule. A hint that is negative or not a whole number fails the
  run with `schema_mismatch`.
* **A poll may run early.** Effective can wake a polling wait early when the source it reads has
  changed, as it does for every write to an RSL log. A wake only causes another read; the wait
  still ends only on a match. Write poll functions so that an extra read is harmless.
* **A task answer is never lost.** If the person answers before the task wait starts, the answer
  is kept and delivered when the wait starts.

## Common errors

| Code | Cause | Fix |
| - | - | - |
| `PROC-WAIT-001` | The body matches no form. For example, a polling wait without `timeout` or `on_timeout`, `until` with both or neither of `equals` and `in`, `in: []`, `every` given as a list, or `until: '{{ $closure.date }}T01:00:00Z'` (an instant can't be a template) | Use exactly the fields of one form |
| `PROC-WAIT-001` | An `rsl_log` wait with `poll`, `every` or `next_poll`, a `program` that is a reference, or a `log` that is a number | Write the program ID as a literal and the log as a reference or string |
| `PROC-WAIT-003` | `poll.function` doesn't resolve, or is a pure function | Poll a `ts effect` or a catalog effect |
| `PROC-WAIT-004` | `until.path`, the `equals` or `in` values, `until.schema`, `next_poll.path` or the `task` handle is invalid. For example, `until.schema` makes `decision` a `const` that leaves out a value listed in `in`, `next_poll.path` names a string, or `task` names `$approval_task.url` | Make the matched schema a narrowing of the result schema that admits every listed value |
| `PROC-SCHEMA-005` | `until` references a value whose schema isn't `{ type: string, format: date-time }`, such as an input declared `type: string` | Declare the instant with a `date-time` schema |
| `PROC-DATA-004` | A reference names an input, value or member that doesn't exist | Check the name and that the member is required |

`next_poll.at_most` shorter than `every`, and `on_timeout` with both `fail` and `value`, are
also refused.

## See also

* [`task`](https://docs.canary.effectiveai.app/procedures/reference/nodes/task): create a task with `mode: create`.
* [`call`](https://docs.canary.effectiveai.app/procedures/reference/nodes/call): call a function or effect once.
* [Functions and effects](https://docs.canary.effectiveai.app/procedures/reference/nodes/function-and-effect.md): write the effect a
  polling wait calls.
* [`choice`](https://docs.canary.effectiveai.app/procedures/reference/nodes/choice): branch on the value that ended a wait.
* [Triggers](https://docs.canary.effectiveai.app/procedures/reference/triggers.md): start runs on a schedule. A `wait` is a single
  timer, not a schedule.


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