# State machines

Every status write goes through a guard
([`app/domain/statemachine.py`](../app/domain/statemachine.py)); an illegal move
raises `409` instead of silently corrupting lifecycle state. Workers scan on the
**active** set, not a hardcoded `"pending"`, so any non-terminal decision can
still expire.

## Decision lifecycle

```mermaid
stateDiagram-v2
    [*] --> pending
    pending --> in_review
    in_review --> escalated
    escalated --> in_review
    in_review --> approved_pending_clearance

    pending --> approved
    pending --> rejected
    pending --> changes_requested
    pending --> timed_out
    pending --> cancelled

    in_review --> approved
    in_review --> rejected
    in_review --> changes_requested
    in_review --> timed_out
    in_review --> cancelled

    escalated --> approved
    escalated --> rejected
    escalated --> changes_requested
    escalated --> timed_out
    escalated --> cancelled

    approved_pending_clearance --> approved
    approved_pending_clearance --> rejected
    approved_pending_clearance --> timed_out
    approved_pending_clearance --> cancelled

    approved --> [*]
    rejected --> [*]
    changes_requested --> [*]
    timed_out --> [*]
    cancelled --> [*]
```

| Status | Meaning |
|--------|---------|
| `pending` | Created; tasks fanned out; awaiting first action. |
| `in_review` | At least one reviewer has acted; quorum not yet reached. |
| `escalated` | An SLA breach or explicit `escalate` moved it up an escalation path; returns to `in_review` on the next action. |
| `approved_pending_clearance` | Primary approval complete, but a further-clearance profile must finish before terminal effects fire. **No terminal webhook / mail / RunFlow call happens yet.** |
| `approved` · `rejected` · `changes_requested` · `timed_out` · `cancelled` | **Terminal.** Written only by `terminalize()`. |

**Terminal set:** `{approved, rejected, changes_requested, timed_out, cancelled}`.
**Active set** (what workers scan): every non-terminal status above.

## Task lifecycle

```mermaid
stateDiagram-v2
    [*] --> open
    open --> assigned
    assigned --> reassigned
    reassigned --> assigned
    assigned --> completed
    open --> cancelled
    open --> expired
    assigned --> cancelled
    assigned --> expired
    completed --> [*]
    cancelled --> [*]
    expired --> [*]
```

| Status | Meaning |
|--------|---------|
| `open` | Created, not yet assigned. |
| `assigned` | Assigned to a reviewer (or their delegate). **Actionable.** |
| `reassigned` | Transient state while delegation/escalation reroutes it, then back to `assigned`. |
| `completed` · `cancelled` · `expired` | **Terminal.** |

`open` and `assigned` are the **actionable** states — only there can an action be
applied.

## How the two connect

When an action is applied ([`apply_action`](../app/domain/actions.py)):

1. Lock the decision row (`FOR UPDATE`), then the task row — always in that order.
2. Guard: the decision is non-terminal and the task is actionable; enforce
   assignee + separation-of-duties.
3. Insert the `Action`, complete the task.
4. Re-evaluate quorum for the current stage **from persisted rows**
   (`tasks LEFT JOIN actions` on completing types) — see
   [engine.py](../app/engine.py). Modes: `all_of`, `any_of`, `n_of_m`.
5. Route the outcome:
   - stage rejected → `terminalize(rejected)`
   - stage approved, more stages remain → instantiate the next stage
     (bump `current_stage_index`) in the same transaction
   - last stage approved → evaluate clearance → `terminalize(approved)` or
     `approved_pending_clearance`

`terminalize()` is the **only** writer of a terminal status: it guards the
transition, cancels open tasks, revokes unconsumed one-click tokens, and enqueues
the terminal side effects (webhook / mail / RunFlow) with a `dedup_key` so they
can fire exactly once.

See [sequence-diagrams.md](sequence-diagrams.md) for the runtime flows.
