# The model

> Generated from the SDK's own source (`packages/durable-core/README.md`) at build; edit there.

```
  trigger (route, webhook, cron, button)
        │  execute(input, { actor })
        ▼
  ┌─────────────────────────────────────────────────────────────┐
  │ intent  key = f(input)     one run per key while it lives   │
  │ run     attempt 1, 2, …    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)               └─► relay ──► peer apps
              ▼
     limbo: FAILED / COMPENSATED until a person redrives or dismisses
```

An **intent** is a workflow plus an input; its key is a pure function of the
input, so the same intent from ten triggers is one run and the eleventh
caller gets the memoized result (for as long as the key lives, see
Guarantees). A **run** executes **steps** in order, records
each step's input, output and log lines, and replays completed steps from
their stored output when an attempt dies and another claims the run by
lease. A step that changes the world writes a **ledger** row with the image
it found and the image it left, chained by hash per scope, never updated or
deleted. A **fact** emitted by a run is written in the same transaction as
the run's final status and delivered by the outbox to **listeners**, each of
which is itself a keyed run, and, when peers are configured, to other apps.
A run that fails compensates what it did and waits in **limbo** for a person.
A run whose history outgrows its replay closes itself and goes on as a
fresh run under the same key (`ctx.continueAsNew(input)` after a step: the
closed run `CONTINUED` naming the next, the next starting at once in the
same invocation with its steps from seq 1, the ledger chain unbroken;
ADR-0076 FP-032).

A run may declare a **state** (ADR-0076 FP-031; Temporal's Query and
Update): `ctx.state`, a plain object the body reads and assigns, written
onto the run row in the next step boundary's own statement and at the
landing, bounded as a step output is; `queries` on the definition are read
over the stored state through `GET runs/:id/query/:name` with no row
written, and `updates` are validated in the route before any row, landed
as one signal under their id (a resend the same run), applied once at the
body's `ctx.updates()` door as one step row whose output carries the
values and the state after them, and answered to the caller who holds
for them ([a query and an update in eight lines](https://durablesdk.com/docs/workflows.md#a-query-and-an-update-in-eight-lines)).

### Scope and actor

Two words carry most of the design and neither is a storefront's:

- **`scope`** is the unit whose ledger chain, advisory lock and relay
  ordering are independent of every other: a tenant, a store, a clinic, a
  till. A single-tenant app uses one scope (`defaultScope: "app"`). It is the
  first column you would partition by if you sharded. In the first host, a warehouse, it is the
  shop domain, spelled `shop` in the tables; the core types say `scope`.
- **`actor`** is an opaque principal id, `<prefix>:<id>`, recorded on every
  run and ledger row. `system:` and `operator:` are the engine's; a host
  registers its own prefixes through the `PrincipalGrammar` adapter
  (`{ prefixes }`, and nothing else: the actor itself is what the host's
  gate answers, `user:<opaque id>` from its session, and the label a
  prefix shows in the console is the host's read,
  `consoleApi({ reads: { labelActors } })`):

  ```ts fragment
  principal: {
    prefixes: ["user", "pos"], // user:<opaque id>, pos:<till id>; system: and operator: are the core's
  },
  ```

  An actor is never PII: `user:8f3a…`, not `user:jane@example.com`.

---

Prompt for an agent:

```text
Read https://durablesdk.com/docs/concepts/model.md, then apply what it says to this app; run durable-sdk doctor --host before and after; a refused line is a refusal id: durable-sdk explain <id>
```
