# How it works

This page draws the model that [Concepts](https://durablesdk.com/docs/concepts.md) names and
[Design paradigms](https://durablesdk.com/docs/why/paradigms.md) defends: seven pictures, each with
the words the engine uses for its parts. The commands it points at are
`durable-sdk explain <runId>`, `durable-sdk limits`,
`durable-sdk ledger export --out <file>`,
`durable-sdk ledger verify --file <export>` and `durable-sdk doctor --host`.

## A run, end to end

> Diagram: A trigger becomes a key, the key one run, the run its steps in order, each write a row on the ledger chained to the row before it, and the facts at the end each listener's own run.

A trigger (a route, a webhook, a schedule, a button) calls
`execute(input, { actor })`. The key is a pure function of the input,
`key: ["checkoutId", "requestId"]`, and never reads the clock: the moment
rides in the input as `requestId`. The run and its key commit in one
transaction, so a second trigger of the same key, from another caller or
a retried request, joins the run in flight and gets its result. It never
starts a second one.

The body runs its steps in order. A read step leaves its row and no
image. A write reads before it writes, writes once, and leaves a before
and an after image on the ledger under the run, the step, the actor and
the attempt. A write a provider cannot repeat runs as an external step
under a `writeKey`, the intent recorded before the call, so a redrive
after a lost answer finds what landed instead of writing again. Each
step's input and output land on a row before the body goes on; after a
crash the next attempt replays the finished rows and re-invokes only
what did not finish.

At the end the run emits its facts through `ctx.emit()`. A fact is a
row of the catalog with a typed payload and a clock-free key, written in
the same transaction as the run's final status and inserted-if-absent
under its key, so a fact of an attempt that threw is dropped and a
resend collapses. The outbox delivers it to each listener, and a
listener is itself a keyed run, `listener.<name>:<outboxId>`, so a
redelivery is harmless. [Events and listeners](https://durablesdk.com/docs/guide/events-and-listeners.md)
has the forms.

## A step and its undo

> Diagram: When step 3 throws, the saga undoes step 2 and then step 1, each undo its own row on the chain; an undo that finds the world moved stops, and the run waits in limbo for a person.

Every step that changes the world carries an `undo` with its `effect`
(`provider`, `ledgered` or `external`) or says `irreversible: "<why>"`;
the door refuses one that says neither. When step 3 throws, the saga
runs the finished steps' undos in the reverse of their completion order:
step 2's, then step 1's. Each undo gets the step's stored output and
writes its own `<step>.undo` row with the image it found and the image
it restored. When every compensable step is undone the run lands
`COMPENSATED`; a step that said `irreversible` is left standing, listed
on the run's `compensation`, and the error names it.

An undo is a compare-and-set on the before image, never an overwrite.
One that finds the world moved under it (a later actor changed the row)
fails by name and stops: nothing is written over what the later actor
did, the run lands `FAILED` with the step named, and it waits in limbo
for a person. Nothing is retried in the dark.
[Steps and undo](https://durablesdk.com/docs/guide/steps-and-undo.md) has the three forms and
the external step.

## The ledger's chain

> Diagram: Each scope is its own chain: every row hashes the row before it, and verify recomputes the chain from genesis and names the first link that does not hold.

Every write a step makes leaves a row with the image it found and the
image it left. Rows chain per scope (a tenant, a store, a till): each
carries `prevHash`, the hash of the row before it, and
`hash = sha256(prevHash + content)`, from a genesis row per scope. The
append takes the scope's lock, so one scope's writers queue and scopes
append side by side.

The ledger is append-only three times over: the store's interface has no
update and no delete for it, the retention rules never prune it, and a
database trigger (`ledger_append_only`, `BEFORE UPDATE OR DELETE` on
every row, with a truncate twin on every partition) refuses the
statement. The operator API's `verify` read recomputes every chain from
genesis and names the first broken link;
`durable-sdk ledger export --out <file>` writes the rows out and
`durable-sdk ledger verify --file <export>` walks the export offline. Nothing personal belongs in an image: the
redaction policy blanks a secret by name and by shape before a row is
written ([Data in the ledger](https://durablesdk.com/docs/concepts/ledger-and-data.md)).

## The lifecycle

> Diagram: A run is RUNNING or WAITING until its body returns or a step throws; a failure compensates into limbo, where a person redrives, dismisses or undoes it.

`execute` claims the run with a lease and runs it `RUNNING`. A wait (a
timer, a fact, a signal, a child, a slot) parks it `WAITING` and holds
no function; the dispatcher wakes it as the next attempt. The body's
return lands it `COMPLETED`, its facts in the same commit. A step that
throws, a cancel a person pressed (ended at the next step boundary as
`Cancelled`) or a run past its `timeout` sends it into compensation, and
it lands `COMPENSATED` when every undo held, `FAILED` when one could not
or a step was irreversible.

`FAILED` and `COMPENSATED` are limbo. A run there is never retried by
code: a person redrives it (attempt n+1 under the same run id, replaying
the rows that landed), dismisses it with a reason, or undoes it, which
lands `UNDONE` and releases the key. The one exception is a transient
cause the engine recognises, redriven once by itself. A run that
outgrows its history closes `CONTINUED` under the same key
([Changing a workflow](https://durablesdk.com/docs/guide/changing-a-workflow.md)).
`durable-sdk explain <runId> --api <url>` prints a run's sentence and
its remedy.

## The dispatcher

> Diagram: Once a minute the tick runs its lanes over the partitions and claims what is due; a wake between ticks does the same work early, and the sweeper catches whatever a wake lost.

The engine has one moving part outside your app: a cron that requests
`GET /jobs/dispatch` once a minute behind a bearer. The tick runs its
lanes side by side; each lane takes partitions, and a partition's facts
reach their listeners in order. A lane claims a run or a row with a
lease, fenced by `(runId, attempt)`, so two ticks never act on one run;
the claimed run resumes, the claimed fact runs its listener.

Between ticks a wake does the same work early: a write that lands a fact
kicks the dispatcher inline in the same invocation, a timer fires at its
moment, a signal when a person presses it. Nothing depends on the kick
arriving. The sweeper is the tick's own second pass: it resumes any run
whose lease expired, delivers any row a kick lost and fires any timer
that is due, so the worst case for a lost wake is a minute. Retention
runs on its own schedule (`retention.apply`), prunes finished runs,
their steps and delivered rows past their windows, and never touches the
ledger. On the hosted platform a preview has no cron and ticks on a
request ([The platform](https://durablesdk.com/docs/hosting/platform.md)).

## Where it runs

> Diagram: The engine is a library in your own process and a schema in your own database; the cron is the one thing outside, and the hosted platform runs the same tree.

Durable is a library. Your routes, your workflows and the engine run in
one process: a serverless function, or a long-lived one. The engine's
tables sit in a schema of their own, `durable`, in the database your
app already has, made by `durable-sdk migrate` and never modelled by
your ORM; a fact and your own row share one commit. No worker, no
broker, no control plane: the request path is your function and your
Postgres, and the cron is the one thing outside.

The platform is a seam, `Platform`, with one adapter per platform: the
function's deadline, so a run parks before the kill; the wake and the
keep-alive; the identity. A platform with no wake runs on the cron tick
alone. Hosted, the tree runs on a project with a database beside it and
the cron set for you. On a two-vCPU CI runner with Postgres over
loopback, a ten-step run of read steps is 12 statements with
`completion: "behind"` (22 under the default) and 12 rows, about 4,000
runs a minute at eight starters in one process on a ten-connection pool
(develop's bench run 40945075490, 2026-10-29);
[Why Durable](https://durablesdk.com/docs/why/compared.md) has the numbers beside the
alternatives.

## What is stored, what is derived

> Diagram: The rows are written once and read many ways: a run's result and its resume point, the console's views, limbo, the limits, the verify verdict and the agent's files are derived from them and the catalogues, and nothing is stored twice.

The engine keeps one record: the run rows, the step rows, the ledger,
the fact rows and the outbox, in the `durable` schema. Everything the
console shows is a read over them and the catalogues (the registered
workflows, the facts, the route table), computed when it is asked for
and never stored a second time: a run's result as `execute` answers it
for a run already landed and as `durable-sdk explain <runId> --api <url>`
says it, its resume point on a redrive (the finished step rows replayed,
the rest re-invoked), the overview, the pressure view, the caps, the
issues (limbo grouped by cause), the limits in force
(`durable-sdk limits --api <url>`), the facts a run emitted and the runs
they started. The operator API's `verify` read recomputes every chain
from its rows, and `durable-sdk ledger verify --file <export>` walks an
export to the same verdict with no database at all. The agent's
surfaces are derived the same way: `/llms.txt` and the `.md` twins from
the docs' pages, `/openapi.json` from the route table.

The world is never mirrored into a table the workflow trusts over it. A
step reads it (a provider's answer, a store's stock) and the answer is
the step's row; a write leaves the image it found and the image it left
on the ledger; a decision is made on what a step read, never on a copy.
Where the engine keeps a derived copy of its own (the console's day
summaries, the last full walk of the ledger) the row is stamped
`computedAt`, served stale and recomputed after the response, and a
broken ledger is never served from a copy.
[Design paradigms](https://durablesdk.com/docs/why/paradigms.md) states this as the eleventh
rule, with the failure it removes; [The till](https://durablesdk.com/docs/why/derived-state.md)
tells it as a cash register, the tape against the stored number.

## Where to go next

- [Design paradigms](https://durablesdk.com/docs/why/paradigms.md): the eleven rules behind the
  pictures, each with the failure it removes.
- [Why Durable](https://durablesdk.com/docs/why/compared.md): the same model beside a cluster, a
  queue and a job table.
- [Use cases](https://durablesdk.com/docs/why/use-cases.md): ten shapes it fits.
- [Writing a workflow](https://durablesdk.com/docs/guide/writing-a-workflow.md): the ten-line
  file; [Concepts](https://durablesdk.com/docs/concepts.md) for the glossary.

---

Prompt for an agent:

```text
Read https://durablesdk.com/docs/why/how-it-works.md, then explain how a run, a step, the ledger, the lifecycle and the dispatcher fit together before writing or debugging a workflow in this app; run durable-sdk doctor --host before and after; a refused line is a refusal id: durable-sdk explain <id>
```
