
# Smoke flows

A smoke flow is a list of HTTP steps that `agc check` runs on the mirror and every `agc ship` rehearsal runs on a branch of production. It passes when every step gets an expected status. Write one per thing that must keep working, and one that races each invariant.

```toml
[[check.smoke]]
name  = "twenty parents race for the last spot"
steps = [
  "POST /api/camps {name: 'Race {run}', capacity: 1} -> 201 as camp",
  "POST /api/camps/{camp.id}/bookings {email: 'p{run}-{i}@example.test'} -> 201|409 x20",
]
```

## Ask your agent

```text
Add an Agent Cloud smoke flow where twenty parents try to book the last spot of a new camp at the same moment, and make sure the overbooking invariant would catch it.
```

## Steps

```bash runnable
agc up --detach
agc check
```

The flow's result in `data.results` lists each step (`step`, `ok`, `detail`) with its status on the mirror. The race above gives `"detail": "20 at once: 1×201, 19×409"`, and the flow `"detail": "2 steps passed"`.

## How it works

**Racing flows.** A limit (capacity, uniqueness, a balance) usually breaks only when requests race. The flow above passes on status alone (`201|409`), so it can't tell whether the app overbooked. The invariant run after it, on the same database, does.

`agc check` and `agc ship` warn when the app has invariants but no flow has a non-`GET` step with `xN` above 1. The lint doesn't match flows to invariants, so you still have to.

**Flows create their own data.** A rehearsal runs on a branch of production, where the rows your mirror has may not exist, and seed data never does. Make every flow create what it uses, and put `{run}` in anything unique so a second run doesn't collide with the first.

**Order and isolation.** Flows run first, then invariants on the same database: the mirror's under `agc check`, the rehearsal branch (against the new build) in a ship. A flow stops at its first failing step, but other flows still run. A passing single-request step over 1000 ms adds a `slow-route` warning naming it.

**What you see where.**

| | `agc check` (mirror) | Rehearsal (`agc ship`) |
|---|---|---|
| Failed step shows | Status and the first 160 characters of the body | "the response didn't match", never status or body |

**Mail and workers.** A [`MAIL`](#mail) step counts captured messages per address. A release with a worker is rehearsed twice, once while Agent Cloud kills it mid-job ([Background work](/docs/build/background-work-and-workers.md#how-it-works)). The same steps must send the same mail both times, or nothing goes live.

**Sign-in state check.** An app whose code reads `AGC_AUTH_URL` also gets the platform check `sign-in checks state`, under `agc check` and in every rehearsal. It must refuse a code that comes back with the wrong `state` ([Agent Cloud sign-in](/docs/build/choosing-a-sign-in-shape.md#agent-cloud-sign-in-shape-2)).

## Step grammar

A step is one line in `steps`: an HTTP request, `SIGN IN`, or `MAIL`. A step that doesn't parse fails its flow with `can't read step "…": expected METHOD /path [body] -> STATUS[|STATUS] [as name] [xN]`.

### HTTP step

```text
METHOD /path [body] -> STATUS[|STATUS] [as name] [xN]
^(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\s+(\S+)(?:\s+(.*?))?\s*->\s*(\d{3}(?:\|\d{3})*)(?:\s+as\s+([A-Za-z_]\w*))?(?:\s+x(\d+))?\s*$
```

| Part | Rule |
|---|---|
| `METHOD` | `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD` or `OPTIONS`, upper case. Lower case doesn't parse. `GET` and `HEAD` take no body: a step written with one is refused before it's sent (below) |
| `/path` | No spaces, `@` or `\`. Starts with `/` (not `//`) or with a `{name.field}` template. Query strings are fine. Checked again after templating; a path that leaves the app fails with `the filled-in path doesn't start with / or would leave the app` |
| `body` | Everything between the path and `->`, read as loose JSON (below). Sent with `content-type: application/json` |
| `->` | Spaces around it are optional |
| `STATUS` | Exact three-digit codes, alternatives joined by `\|`: `201\|409`. No `2xx` |
| `as name` | Keeps the parsed JSON response (or the raw text if it isn't JSON) for later steps. Name is `[A-Za-z_]\w*`, and not `i` or `run` ([below](#templates)) |
| `xN` | Sends N copies at the same moment. Capped at 100. Must come after `as` |
| Every request | Sends `accept: application/json`, the flow's cookies (including those set after `SIGN IN`), and the flow's `headers`. Redirects aren't followed: a `302` is the status you match. Times out after 15 seconds |

For an `xN` step, every copy must return one of the listed statuses, and `as name` keeps a list of the N bodies in copy order: `{name.0.id}` is the first copy's `id`. For token auth, set flow-level `headers = { Authorization = "Bearer {login.token}" }`. It fills in once a step has kept `login`, such as `"POST /api/login {email: 'pat@example.test'} -> 200 as login"`.

### Loose JSON bodies

Bodies are written like JavaScript object literals and converted to JSON, which must then be valid, or the step fails with the parser's error.

| Written | Becomes |
|---|---|
| A bare identifier followed by `:` (`{name: 'x'}`) | A quoted key, except `true`, `false` and `null` |
| Single- or double-quoted strings | JSON strings |
| A backslash | JSON's escapes inside strings: `\n`, `\t`, `\r`, `\b`, `\f`, `\uXXXX` (`\u0000` sends a NUL). Any other character is kept literally: `'it\'s'` is `it's` |
| Numbers, `true`, `false`, `null`, arrays, nesting | Passed through |

### Templates

`{expr}` is replaced in the path, in body strings, and in header values; `expr` matches `[A-Za-z_][\w.]*`.

| Template | Value |
|---|---|
| `{run}` | Six random characters, the same for every step of one run of the flow; new on each run |
| `{i}` | The copy's number in an `xN` step, from `0` to one less than N; `0` in a single request |
| `{name.path}` | A field of a response kept with `as name`, by dotted path. List indexes are numbers: `{camp.items.0.id}` |

A body string that is exactly `"{expr}"` keeps the value's type, so `{camp_id: '{camp.id}'}` sends a number if the id is one. Anywhere else the value is inserted as text. A template that resolves to nothing (an unknown name or a missing field) is left as written, which usually fails the request visibly.

**Don't name a kept response `i` or `run`.** Inside every request `{i}` is the copy's number, so after `-> 201 as i`, `POST /api/docs/{i.id}/pay` sends `{i.id}` unfilled and gets a 404. `as run` replaces `{run}` for every later step. Use another name: `as doc`, then `/api/docs/{doc.id}/pay`.

### SIGN IN

`SIGN IN <who>`, in upper case. `<who>` is an email or a user id from the data, 1 to 200 characters, no spaces; templates work (`SIGN IN p{run}@example.test`).

| App's sign-in | What the step does |
|---|---|
| Agent Cloud sign-in | Requests `GET /auth/sign-in`, expects a redirect to `$AGC_AUTH_URL/authorize?…`, has the test sign-in mint a code for `<who>`, and calls the app's callback with it, keeping the cookies it sets; later steps run signed in. Fails if the redirect goes elsewhere or the callback answers 400 or more |
| Behind the edge (`[auth] provider = "edge"`) | Makes no request. Later steps carry `x-agc-user-email` and `x-agc-user-name`, both `<who>` in lower case; earlier ones reach the app as nobody, and a rehearsal failure says so |

### MAIL

```text
MAIL <address> -> N [xM] [within Ss]
^MAIL\s+(\S+)\s*->\s*(\d+)(?:\s+x(\d+))?(?:\s+within\s+(\d+)s)?\s*$
```

| Part | Rule |
|---|---|
| `<address>` | Templated, lower-cased. With `xM`, `{i}` runs from `0` to one less than M |
| `N` | Exactly this many messages captured to each address, counted only from the start of this run of the flow |
| `xM` | M addresses at once; capped at 100. Must come before `within` |
| `within Ss` | How long to wait, in whole seconds with the `s`; default 30, capped at 120 |

The step polls the platform outbox once a second and passes only when every address has exactly N messages and the counts have held still for 3 seconds, so a duplicate sent late by a retried job fails it. It needs platform email: under `agc check` the mirror's outbox, in a rehearsal the rehearsal's. Use it only for addresses whose requests should each send a message, not for the nineteen the race above rejects.

## Reference

A flow takes `name` (defaults to `smoke <n>`, and is a held check's identity), `steps` (run in order) and `headers` (sent with every HTTP step, templated per request). Any other key (`replay`, `external`) is ignored with a lint warning ([manifest reference](/docs/reference/agentcloud-toml-reference.md)).

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md). A failing step puts its detail inside a failed `check.failed` result (exit 11); these aren't envelope codes:

| Detail | Cause | Fix |
|---|---|---|
| `20 at once: 19×201, 1×500; unexpected: …` | A copy of an `xN` step got an unlisted status | Fix the race in the app |
| `step "HEAD /x {a: 1} -> 200": a HEAD step takes no body. Remove the body …` | A `HEAD` step with a body | Remove the body, or use `POST` |
| `step "GET /x {a: 1} -> 200": a GET step takes no body. Move its values into the query string …` | A `GET` step with a body | Use the query string, or `POST` |
| `SIGN IN steps need agent.cloud sign-in: run the app with agc up` | No test sign-in | Run under `agc up` |
| `MAIL steps need platform email: run the app with agc up` | No outbox | Run under `agc up` |
| `expected 1 to each; got 2 to p3@example.test` | Duplicate or missing mail | Make the send idempotent with a stable `key` |

```json
{"ok": false, "command": "check", "error": {"code": "app.not_linked"}}
```

## Agent notes

- A step checks the status; check what the data looks like afterwards with an [invariant](/docs/checks/invariants.md).
- Don't widen the accepted statuses to make a flow pass; that's changing the check, not fixing the change.
- A space inside the path splits it: everything after the space is read as the body.
- Never use `as i` or `as run`: `{i.id}` stays unfilled and the request fails with no hint. Use `as doc`.

## Related

- [Invariants](/docs/checks/invariants.md)
- [Checks overview](/docs/checks/checks-overview.md)
- [Background work and workers](/docs/build/background-work-and-workers.md)
- [Sending email](/docs/build/sending-email.md)
- [Agent Cloud sign-in](/docs/build/choosing-a-sign-in-shape.md#agent-cloud-sign-in-shape-2)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "checks/smoke-flows: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
