# Durable workflows

A workflow is a process written as a state machine. The platform runs it, keeps its state through restarts and outages, retries, waits and reports every step. The design is ADR-0884 (proposed); the capability is MSG-6.

## A first workflow
```json
{
  "startAt": "Reserve",
  "states": {
    "Reserve": {
      "type": "task",
      "target": { "kind": "http", "resource": "containerApps/stock", "path": "/reserve", "method": "POST" },
      "timeoutSeconds": 20,
      "retry": [{ "errors": ["target-error", "timeout"], "intervalSeconds": 2, "backoffRate": 2.0, "maxAttempts": 3 }],
      "catch": [{ "errors": ["*"], "next": "Failed", "resultPath": "$.error" }],
      "resultPath": "$.reservation",
      "next": "Approve"
    },
    "Approve": { "type": "approval", "message": "Ship this order?", "timeoutSeconds": 86400, "onTimeout": "Failed", "next": "Done" },
    "Done": { "type": "succeed" },
    "Failed": { "type": "fail", "error": "OrderNotShipped", "cause": "reservation failed or nobody approved" }
  }
}
```
Make it with `egulu workflows create --workflow-id order-flow …`, in the console under Messaging, Workflows, or with `POST …/workflows?workflowId=order-flow`. Check a definition first with `:validate`: every refusal names the state and a JSON pointer (`/states/Reserve/retry/0/maxAttempts`).

## States
| Type | What it does |
|---|---|
| `task` | Calls a target. `timeoutSeconds` (default 30, at most 900), `retry[]`, `catch[]`. |
| `choice` | `choices[]` of `{when, next}` and a `default`. Conditions: `equals`, `notEquals`, `greaterThan`, `greaterThanOrEquals`, `lessThan`, `lessThanOrEquals`, `exists`, `isNull`, `in`, `startsWith`, `contains`, and `all`, `any`, `not`. No regular expressions. |
| `wait` | One of `seconds`, `secondsPath`, `timestamp`, `timestampPath`. The timer is stored in the database. |
| `parallel` | `branches[]`, each its own flow; the output is the array of branch outputs. |
| `map` | `itemsPath`, an `iterator` flow, `maxConcurrency` (1 to 40); the output is the array in item order. |
| `pass`, `succeed`, `fail` | Move data, end well, end badly with an error name. |
| `approval` | A person who holds the state's `permission` answers in the console or by API (`:answer`); `timeoutSeconds`, `onReject`, `onTimeout`, `separateApprover`. |

Data is one JSON document. `inputPath` narrows it, `input` is a template (a key ending in `.$` takes its value from a selector), `resultPath` says where the output goes. A selector is `$`, `.name`, `['name']` and `[n]` only, at most 16 segments.

## Targets
A task names something of its own project and location, never a URL: `http` (`containerApps/{id}` or `webApps/{id}`, a `path`, a `method`), `function`, `jobRun`, `queue`, `topic`, `bucketObject`. Every call carries an idempotency key, the same for every attempt of one entry into a state: `Idempotency-Key` and `webhook-id` on an HTTP call, the deduplication ID of a queue message, the event ID of a topic event. A call may be made more than once after a crash; a target that honours the key does the work once. HTTP calls are signed with the Standard Webhooks scheme; verify `webhook-signature` before you trust the call. Answers are cut at 256 KiB.

## Executions
`POST …/workflows/{w}/executions?executionId=…` starts one. The ID is the idempotency name: the same ID with the same input and version returns the same execution; another input is `409 already-exists`. An execution keeps an append-only history (`…/executions/{e}/events`), each payload shown cut at 4 KiB. `:stop` ends it. Triggers start executions: a `SCHEDULE` trigger by cron, a `TOPIC` trigger from a pull subscription you made and that only the trigger reads.

## Limits (defaults)
128 KiB and 100 states per definition; 4 levels of parallel and map; 10 branches; 1,000 map items; 256 KiB of data; 5,000 events and 2,000 transitions per execution (a loop that does not end stops with `transition-limit`); 30 days per execution; 200 versions; 50 workflows, 100 running executions and 100 triggers per project and location (quotas). One billing operation per state entered (`workflow-transitions`).

## What is not proven yet
Real calls to apps, functions, jobs and buckets wait for the lab (Stage B); queues and topics are proven on PostgreSQL 18. How an app gets the signing secret and the job-run target are open in ADR-0884.
