# Guarantees

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

Stated precisely, because the difference between them is the whole point.
Each carries its status tag.

**Exactly once.**

- **One run per key, for as long as the key lives.** `execute` creates the
  run and its key in one transaction; a concurrent second caller either gets
  `in_progress` or the first run's result. A key under another workflow's
  name is a `KeyCollision`, thrown before any run exists. The key lives for
  as long as its run is `RUNNING`, `FAILED`, `COMPENSATED` or `UNDONE`, for
  **14 days** after an unledgered `COMPLETED` run (deleted with the run) and
  **30 days** after a ledgered one; `undo` releases it on purpose. After
  that, or after an undo, the same intent starts a new run. Inside the
  window the memo is by content: an intent A, then B, then A again (the
  same order, edited and edited back) is skipped the second time; put a
  version or request id in the input when that matters (ADR-0041 "keys
  live with their run").
- **One completion per run.** `finish` is fenced by `(runId, attempt)`, so
  an attempt that lost its lease cannot finish; the same fence holds on
  step records and ledger appends, and the lease's `signal` fires inside a
  step (ADR-0041 wave 3).
- **One fact per key.** A fact is inserted-if-absent under the key your
  descriptor returns; the engine adds nothing to it. Put the attempt (or a
  request id) in the key when a redrive of the same intent is a new fact,
  and leave it out when it is the same fact (step 4
  shows both). Facts of an attempt that threw are dropped and only
  `run.failed` is written; facts of a run that called `ctx.fail()` are
  written with status `FAILED`. `emit()` outside a run reports
  `collapsed: true` when the key already exists, and commits on its own:
  a host write that must commit with its fact belongs inside a workflow,
  whose fact is written with the run's final status (ADR-0046 §C as
  landed).
- **One landing per relayed fact.** A copy lands under
  `relay:<origin>:<key>` by insert-if-absent; a resend collapses; a copy is
  never relayed again, for every peer of the mesh (ADR-0046 §H).
- **One child per key under a fan-out, one decrement per child.**
  `ctx.fanOut` inserts its children parked through one statement per chunk,
  idempotent on the children's keys (a chunk run again after a crash lands
  nothing twice), and each child's finish decrements the parent's join
  counter in that finish statement and clears its pointer, so a landing
  counts once however many attempts the child took; the finish that brings
  the counter to zero, or the failures past the tolerance, fires the join
  in the same statement (ADR-0076 FP-037).

**At least once.**

- **Step bodies.** A step may run again after a crash between its write and
  its record, which is why every step reads before it writes and no-ops
  when the world already matches. That makes an **absolute** desired state
  idempotent; a **relative** change (a counter, "+qty") is not, and needs
  its own keyed row (step 5).
  A step replays from its stored output; `defineExternalStep` reconciles
  the pending record from the stored `desired`, and `retry: { idempotent:
false }` is never repeated automatically (ADR-0041 wave 3).
  Its result names two ids apart by type: `resourceId` (the ledger's,
  `LedgerResourceId`) and `providerId` (the provider's, `ProviderId`, read
  from the image under the spec's `providerIdKey`); `id` is a deprecated
  alias of `resourceId` (2026-09-26), kept while a stored output of the
  previous release (`id` alone) can still replay: the saga upgrades such
  an output on the memo hit (`replayedExternalResult`).
- **Lightweight runs** (ADR-0076 FP-038). A body that reads and computes
  and never writes may declare `lightweight: true` on `defineWorkflow` or
  on `on(…)`: one run row and no step row, every `ctx.step` a `read` kept
  in memory and landed on the run row's `stepOutputs` with the finish
  (schema version 29), the facts in the finish statement and the key as
  on every run. A crash before the landing re-runs the body whole on the
  wake, so the door refuses by name a step with an undo or an
  irreversible reason, a recorded write or an external step, a wait, a
  child, a batch, a poll, a hook with a handler and `cancelScheduled`
  (the `lightweight.*` rows of `REFUSALS`), and the double-run proof
  judges such a definition at the finish boundary alone, a provider or
  host write inside it its finding. The run detail says `lightweight`
  with the outputs off the run row (API 2.142).
- **Listener delivery.** A listener run is keyed
  `listener.<name>:<outboxId>`, so redelivery is harmless. Delivery is
  bounded: **at most `MAX_ATTEMPTS` transport attempts** (8, spelled once
  in `src/events/limits.ts`) with backoff, then the row is
  parked; a reaction whose run **fails** parks the row on that first
  failure. A parked row is listed on the console's Events page and counted
  on `/health`; a person's **Reset** (re-arm the row) or **Redrive** (the
  listener's own run) is the only way back (ADR-0031; the backoff on
  `retryAt`, ADR-0041 wave 2). Since ADR-0076 FP-028 a listener may
  declare its own policy on the registration, `retry: { maxReceiveCount,
backoff, maxAge }` (the row exhausts at its own limit, written onto it
  as `attemptLimit`, or past its age, under the throwing listener's
  name, `exhaustedBy`) and `concurrency: { max, key }` (the listener
  workflow's cap), and one listener's exhausted rows are its
  dead-letter queue: `GET events?exhausted=<listener>` lists them and the
  operator action `events.redrive { listener, since, velocity }` puts
  them back oldest first at a velocity, attempts at zero, the moment
  kept. Since FP-027 a delivered fact stays replayable `archiveDays`
  from its own moment (retention moves it into `OutboxArchive` in place
  of deleting it at `outboxDays`, and prunes it there at `archiveDays`;
  0 is the archive off), and the operator action `event.replay { since,
until, listener?, partition?, execute? }` re-delivers a window's rows
  from the outbox and the archive to one listener or every record
  listener as new runs keyed `listener.<name>:<id>:replay:<requestId>`,
  the payload's `requestId` under the `replay:` prefix, so keyed work
  starts afresh and a host's loop guard fires no rule twice. Since FP-029 `debounce: { key, period, mode, maxDelay }` folds
  one key's emits into one run per window on the engine's own rows (a
  settings row by compare-and-set and the delayed fact `event.debounced`)
  and `throttle: { limit, period, burst, key }` is a token bucket on the
  settings rows that parks the excess in emit order, never a drop and
  never an attempt charged.
- **Relay transport.** Batches are claimed, posted, acknowledged by id and
  reclaimed after a lease; order is preserved per origin and per partition,
  never globally: one lease per peer, one batch in flight under it, with a
  sequence number the receiver checks (ADR-0046 §H).

**What the ledger proves.**

- For every scope, the sequence of before/after images of every write the
  engine made, each row carrying the run, the step, the actor and
  `hash = sha256(prevHash + content)`. `verify` recomputes every chain and
  names the first broken link: an edited image, a re-dated row, a deleted
  tail or a deleted partition; each row carries the attempt that wrote
  it (ADR-0041 wave 3).
- A compensation that changed state has its own `<step>.undo` row with the
  image it found and the image it restored; a step that could not undo says
  why (`irreversible: '<reason>'`), so `COMPENSATED` means every
  compensable step was undone and the run's error names what was not (the
  operator's Undo writes `undo` rows the same way; the per-step outcomes
  and the `compensate` XOR `irreversible` type are ADR-0041 wave 3's).
- The ledger is never updated or deleted: by interface (no such method) and
  by retention (no rule may name it) and by the Postgres trigger
  `ledger_append_only` (ADR-0041 wave 3).
- Each app's chain head is recorded in every peer's ledger, so a tamper on
  one app is detectable from any other (ADR-0046 §H, `ledger.head`).

**Never.**

- A failed run is never retried by code; only a person's `redrive` moves it
  (ADR-0031).
- A secret never lands in a row or a log line: every stored copy goes
  through one serializer under a redaction policy, and a `Secret<T>` value
  is refused by the serializer and the logger. A one-time value is handed
  back with `ctx.reply()` and never stored (ADR-0041 wave 1).
- A database credential never crosses apps; only the facts you publish do,
  under a publication policy per peer (ADR-0046 §H).

---

Prompt for an agent:

```text
Read https://durablesdk.com/docs/concepts/guarantees.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>
```
