---
title: "Steps and replay"
description: "How a workflow run replays its body, and the rules for its durable steps."
---

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

# Steps and replay

A workflow's body runs again from the top each time a run resumes. Its steps make that safe: each one runs its work once and answers its recorded result on every replay.

## Deterministic replay

When a workflow resumes after a sleep or a signal wait, Forge replays the workflow body from the top and returns the cached JSON result for every step that has already completed. Don't keep state in module variables: a step's retries run in the attempt's own sandbox and see what earlier tries left there, and a later attempt may start in a new one. Keep what the run needs in step results.

To guarantee that replays are deterministic, the workflow body's top-level context `{ runId, step }` has **no** `forge` client, **no** `org` connections, and **no** network access:

- Calling `fetch` outside `step.do` immediately rejects with a `TypeError`.
- Inside `step.do(name, work, options)`, the `work(s)` callback receives a `WorkflowStepContext` (`s`) with `s.orgId`, `s.principal`, `s.actingAs`, `s.extension.sourceRepoId`, `s.branch`, `s.repo.id`, `s.forge`, `s.org.connections`, `s.browser` ([`ctx.browser`](https://buildrforge.com/docs/extensions/browser/)), `s.email` ([`ctx.email`](https://buildrforge.com/docs/extensions/email/)), `s.systemOne` ([`ctx.systemOne`](https://buildrforge.com/docs/extensions/system-one/)), `s.rooms`, and `s.events` ([Streams and rooms](https://buildrforge.com/docs/extensions/streams-and-rooms/)), and `globalThis.fetch` is wired to the step's scoped outbound network for the duration of that step.
- Each step executes as whoever started the run (`enqueued_by_id`): the member who started it (directly or through a tool), or the extension itself when a schedule's, trigger's, webhook's, mailbox's, or public share guest's tool started it. `enqueued_by_extension` names that extension, which can be another extension than the workflow's own when its run called this extension's tool; it is null when a person started the run. It runs under a grant scoped to that step and holds only `org` connections.
- Each run works on a branch of its own (`s.branch`), shared across all of its steps. Writing to any other branch fails with `RunBranchRequired`. Forge merges what the run committed into the target branch when the run completes, and discards its branch if the run errors or is terminated. See [Where a background run writes](https://buildrforge.com/docs/extensions/tools/#where-a-background-run-writes).

## Step methods and rules

The `step` object provides four durable operations:

| Method | Signature | Behavior |
| --- | --- | --- |
| `step.do` | `step.do(name, async (s) => result, options?)` | Executes `work` once and persists its JSON return value (up to 1 MiB). On subsequent replays of the workflow body, returns the recorded value without re-running `work`. |
| `step.sleep` | `step.sleep(name, duration)` | Pauses the workflow for `duration` (either milliseconds as a number or a unit string such as `"30 seconds"`, `"15 minutes"`, `"2 hours"`, or `"3 days"`). |
| `step.sleepUntil` | `step.sleepUntil(name, timestamp)` | Pauses the workflow until `timestamp` (a `Date` instance or epoch milliseconds). A time that has already passed returns at once. |
| `step.waitForSignal` | `step.waitForSignal(signal, { timeout? })` | Pauses the workflow until one of its declared `signals` is answered, and resolves to `{ payload, sentBy }`. See [Signal waits](https://buildrforge.com/docs/extensions/workflows/signals/#signal-waits). |

Every step in a workflow run must obey these naming and option rules:

- **Unique step names per run**: Each call to `step.do`, `step.sleep`, or `step.sleepUntil` within a single workflow run must use a distinct `name` between 1 and 200 characters. Reusing a step name in the same run rejects with a `TypeError`, and step names cannot start with the reserved prefix `"~forge/"`. `step.waitForSignal` takes no name: Forge numbers each wait by the order in which the body makes it.
- **Retries and timeouts**: `step.do` accepts an optional `{ retries, timeout }` object:
  - `retries.limit`: Maximum number of retry attempts when `work` throws. Without `retries`, a step retries 5 times, so its work runs at most 6 times.
  - `retries.delay`: Delay between retries (`WorkflowDuration`).
  - `retries.backoff`: `"constant"`, `"linear"`, or `"exponential"`.
  - `timeout`: How long one attempt of the step's `work` may run (`WorkflowDuration`), up to 5 minutes; without one, 30 seconds. A longer timeout rejects with a `TypeError`.
- **Time and CPU limits**: Each attempt of a step's `work` stops after its `timeout` of wall-clock time, or 30 seconds without one, its `s.forge` calls included. The body may also run at most 30 seconds from one step to the next. Every step's work and the body in one attempt of the workflow share one budget of 10 seconds of CPU. Sleeps and signal waits end an attempt, so a run as a whole has no limit.
- **Step failure handling**: If a step throws on every retry attempt, `step.do` rejects with a `WorkflowStepError` whose `step` property names the failed step, and whose message places what the work threw in your own files, as `index.ts, line 12, column 5`. A step whose result Forge can't record, such as a value that isn't JSON or a result over 1 MiB, fails at once without a retry, because it fails the same way each time.

Source: https://buildrforge.com/docs/extensions/workflows/steps-and-replay/index.mdx
