# Concepts

This page names the parts a Durable app is made of and what each one guarantees. Read it once before [writing a workflow](https://durablesdk.com/docs/guide/writing-a-workflow.md); the commands it points at are `durable-sdk doctor --host` and `durable-sdk explain <id>`.

## The model in one picture

```text
  trigger (a route, a webhook, a schedule, a button)
        │  execute(input, { actor })
        ▼
  ┌─────────────────────────────────────────────────────────────┐
  │ intent  key = f(input)     one run per key while it lives   │
  │ run     attempt 1, 2, n    claimed by lease, fenced         │
  │   step  read → compare → write → ledger row (before/after)  │
  │   step  read → compare → write → ledger row                 │
  │   ctx.emit(fact)      written with the run's final status   │
  └───────────┬──────────────────────────────┬──────────────────┘
              │ failure                      │ completed
              ▼                              ▼
     compensate in reverse             outbox ──► listeners (each a run)
     (undo rows in the ledger)
              ▼
     limbo: FAILED / COMPENSATED until a person redrives or dismisses
```

Everything runs inside your app's functions on its Postgres database. There is no worker, no broker and no control plane in the request path: the engine is a library, its tables sit in a schema of their own (`durable`), and a cron that requests `/jobs/dispatch` once a minute is the only moving part outside the app.

## Runs and intent keys

A **workflow** is a definition: a name, a key, an example input and a body. An **intent** is a workflow plus an input. Its **key** is a pure function of the input (`key: ["orderId", "requestId"]`), so the same intent from ten callers is one **run**, and the eleventh caller gets the first run's result. A key never reads the clock: the moment a request happened rides in the input as `requestId`, and a definition whose key reads `Date.now()` is refused at boot.

A run executes its steps in order. Each step's input, output and log lines land on a row before the body goes on, so when an attempt dies (a crash, a function limit, a lost lease) the next attempt replays the completed steps from their rows and re-invokes only what did not finish. The key lives as long as the run does, plus a window after it completes (14 days for an unledgered run, 30 for a ledgered one), so a resend inside the window is answered from memory.

## Steps and undo

A **step** is the unit of work and of memory. A step that changes the world either carries an `undo` with its `effect` (`provider`, `ledgered` or `external`) or says `irreversible: "<reason>"`; the door refuses one that says neither. Step bodies may run twice after a crash, so every write reads before it writes and does nothing when the world already matches. A write a provider cannot repeat goes through an external step under a `writeKey`, a compare-and-set on the image the step read, and is never retried automatically.

When a later step fails, the saga runs the completed steps' undos in reverse and the run lands `COMPENSATED`; each undo is its own ledger row. [Steps and undo](https://durablesdk.com/docs/guide/steps-and-undo.md) has the forms.

## Facts and listeners

A run that wants the rest of the app to react emits a **fact** (`ctx.emit("hello.said", { name })`). A fact is a row of the catalog (`src/events/catalog.ts`): a name, a typed payload and a clock-free key. It is written in the same transaction as the run's final status, so a fact of an attempt that threw is dropped (a run that ended through `ctx.fail()` writes its facts with status `FAILED`), and it is inserted-if-absent under its key, so a resend collapses.

A **listener** (`on("hello.said", "greet-once", body)`, in `src/listeners.ts`) is itself a keyed run, `listener.<name>:<outboxId>`, so a redelivery is harmless. Delivery is bounded (at most eight transport attempts with backoff); a listener whose run fails parks the row for a person. [Events and listeners](https://durablesdk.com/docs/guide/events-and-listeners.md) has the rest.

## The ledger

Every write a step makes leaves a row with the image it found and the image it left, under the run, the step, the actor and the attempt, chained by `hash = sha256(prevHash + content)` per scope. The ledger is append-only by interface, by retention and by a Postgres trigger. `verify` recomputes every chain and names the first broken link. A compensation that restored state has its own `<step>.undo` row. Nothing personal belongs in an image: the redaction policy blanks a secret by name and by shape before any row is written.

## Limbo

A run that failed is never retried by code. It waits in **limbo**, `FAILED` or `COMPENSATED`, with its error, its step rows and the remedy on its row, until a person **redrives** it (the next attempt, replaying what landed), **dismisses** it with a reason, or **undoes** what it did. Limbo is a view of the console and the first thing an agent reads with `durable-sdk explain <runId>`.

## The dispatcher and the sweeper

The **dispatcher** delivers facts to listeners in partition order and wakes parked runs: a timer at its moment, a fact wait when a matching fact lands, a signal when a person presses it. A write that lands a fact kicks the dispatcher inline in the same invocation. The **sweeper** is the minute cron at `/jobs/dispatch`: it resumes any run whose lease expired, delivers any row a kick lost, and fires any timer that is due. Nothing depends on the inline kick arriving; the cron is the fallback for every wake, so the worst case for a lost wake is a minute. On the hosted platform a preview deployment has no cron (`tick: request-only`): its wakes ride on requests alone, said on [Hosting](https://durablesdk.com/docs/hosting.md).

## The operator API

Every app serves its own **operator API** at `/admin/api/*`: the runs, a run's steps and images, the ledger of a scope, limbo, the facts, the limits, and the actions (redrive, dismiss, undo, signal, cancel), each action a keyed, ledgered run under the operator's actor. The console tab of the dashboard, the CLI and the MCP tools are all clients of this one contract. [Console](https://durablesdk.com/docs/operate/console.md) shows it; the gate and the session are on [Security](https://durablesdk.com/docs/operate/security.md).

## Glossary

| Term         | Meaning                                                                                                         |
| ------------ | --------------------------------------------------------------------------------------------------------------- |
| actor        | An opaque principal id on every run and ledger row, `<prefix>:<id>`; `system:` and `operator:` are the engine's |
| attempt      | One invocation of a run's body; the next attempt replays the rows the last one landed                           |
| compensation | The undos of completed steps run in reverse after a failure; the run lands `COMPENSATED`                        |
| effect       | What a step with an undo changed: `provider`, `ledgered` or `external`; a step with none is a `read`            |
| fact         | A catalogued event a run emits, written with the run's final status, delivered by the outbox                    |
| intent       | A workflow plus an input; its key names the one run it may have                                                 |
| key          | A pure function of the input, never the clock; `requestId` carries the moment                                   |
| lease        | The claim one attempt holds on a run; a step row and a finish are fenced by `(runId, attempt)`                  |
| ledger       | The append-only, hash-chained rows of before/after images per scope                                             |
| limbo        | Where a failed run waits for a person: redrive, dismiss or undo                                                 |
| listener     | A reaction to a fact, itself a keyed run                                                                        |
| partition    | The unit of delivery order: facts of one partition reach their listeners in order                               |
| redrive      | A person's restart of a run in limbo as its next attempt                                                        |
| scope        | The unit whose ledger chain and lock are independent of every other: a tenant, a store, a till                  |
| step         | One unit of work and of memory inside a run; replayed from its row after a crash                                |
| sweeper      | The minute cron at `/jobs/dispatch`: resumes, delivers and fires whatever an inline wake lost                   |
| wait         | A parked run: a timer, a fact, a signal, a child or a slot; no function is held meanwhile                       |
| writeKey     | The identity of one external write, so a redrive finds what landed instead of writing again                     |

Every sentence a door throws is a refusal with an id; `durable-sdk explain <id>` prints its trap and remedy offline, and [Refusals](https://durablesdk.com/docs/reference/refusals.md) lists them all.

---

Prompt for an agent:

```text
Read https://durablesdk.com/docs/concepts.md, then explain the model a Durable app runs under before writing or debugging a workflow in it; run durable-sdk doctor --host before and after; a refused line is a refusal id: durable-sdk explain <id>
```
