
# Rehearsals

A rehearsal is a dry run of your release on a fresh copy of production's database. `agc ship` runs one for you: it migrates the copy, starts the new code there and runs your checks. A failure leaves production unchanged; a pass lets the release go on toward live.

A failed check exits 11 (`ship.check_failed`), and other failures exit 1. A release that goes live exits 0 with `"outcome": "live"`.

## What can rehearsal side effects reach?

A rehearsal gets the app's production secrets and `AGC_RUN=rehearsal`, so skip outside calls when `AGC_RUN` is `rehearsal`. Platform email is captured, but the app's own provider calls reach real services. The database is a fresh, unmasked branch of production, and the agent gets pass or fail per check, never its rows. Granted connections get restricted roles, described below.

## Ask your agent

```text
Add the cents column, and make sure Agent Cloud proves no booking total changes before it goes live.
```

The agent writes the migration and an invariant, ships, and reports which checks ran on the branch of production.

## How it works

```mermaid
sequenceDiagram
  participant A as agc ship
  participant C as control
  participant F as fork of production
  participant R as rehearsal pod + checker
  A->>C: snapshot
  C->>C: build image
  C->>F: fork production, set row keys aside
  C->>F: apply pending migrations
  C->>F: verify no existing row disappeared
  C->>R: start new image on the fork (no route)
  R->>R: smoke flows (calm run)
  R->>R: same flows while workers are killed (if a worker sends mail)
  C->>F: invariants, read-only
  C->>F: drop the fork
  C-->>A: pass/fail per check, then decide
```

The rehearsal runs end to end, then the fork is dropped, and only then is a person asked (if one is needed).

**When a release rehearses.** A release rehearses when it has smoke flows, invariants, pending migrations or a worker, when a person will be asked about it, or when the app serves its own `/mcp`. A code-only release with none of those skips it. Its only gate is that the new image answers healthy before traffic switches to it, so give the app at least one smoke flow to prove code-only releases against production data.

**Rehearse first, then ask.** A person only ever approves a release whose rehearsal passed, and a failed rehearsal asks nobody. If another release goes live before the approval, this one rehearses again against the new production before promoting, without asking again.

**What runs, in order:**

1. **Migrations, with row preservation.** Control records every table's keys, migrates, then checks that no existing row disappeared. A dropped table or column with data must be declared in the migration header (`-- agentcloud: drops bookings.child_name`, or `rekeys <table>`). This is the check `no existing row was deleted`. A table without a primary key fails if it has fewer rows. Otherwise its rows, like those of a table over 5,000,000 rows, are "not proven preserved", and the release waits for a person in the console instead of failing. Invariants with `hold = "unchanged"` or `hold = "equal:<check>"` must give the same result before and after.
2. **The new image on the fork**, with the fork's connection string and no public route.
3. **Smoke flows**, from a separate checker pod with no database, secrets or connections. If the code reads `AGC_AUTH_URL`, the checker also sends a valid sign-in code with the wrong `state` and expects the app to refuse it.
4. **A second run that kills the worker.** When the release has a worker and the app can send platform email, the flows run again while two workers race for the same jobs and are killed mid-job, twice. Each recipient's mail must match the calm run, or `background work survives crashes and duplicate workers` fails.
5. **Invariants**, in a `BEGIN READ ONLY` transaction, one statement each.

**What's captured or faked.** Platform email is captured and never sent. Sign-in runs in test mode and never reaches the identity provider, and an app behind the edge gets a test identity. External connections (Snowflake, DuckLake, Flight SQL) are never forked. Until a release that needs a person is approved, it gets only the connection roles verified read-only. The approval names the withheld connections, and approving rehearses again with full roles before anything goes live.

**What reaches you.** Each check's name, whether it passed, and a fixed sentence, never rows, counts, response bodies or status codes from the fork:

```json
{ "name": "cents agree with dollars", "kind": "invariant", "ok": false, "detail": "it found violations on a fresh branch of production; run agc check on your mirror to see them" }
```

Run `agc check` on your mirror to see the rows. If the app doesn't start, `error.message` gives a fixed cause (an unreadable file, a missing package, the wrong port) and `fix` says what to change.

**Restarts.** After a control-plane restart, a rehearsal quiet for two minutes starts again from a fresh fork, so migrations never apply twice.

## Reference

| Check that Agent Cloud adds | When |
|---|---|
| `no existing row was deleted` | The release has pending migrations |
| `background work survives crashes and duplicate workers` | The release has a worker and platform email |
| The sign-in state check | The code reads `AGC_AUTH_URL` |

## Errors

| `code` | exit | Cause | Fix |
|---|---|---|---|
| `ship.check_failed` | 11 | A check failed on the fork | Fix the change, not the check; `agc check` on the mirror |
| `ship.migration_failed` | 1 | A migration failed on the fork | Fix the migration and ship again |
| `ship.rehearsal_failed` | 1 | The new image didn't start on the fork, or the rehearsal couldn't start | Follow `fix`; the cause is in `error.message` |
| `ship.platform_error` | 1 | Agent Cloud couldn't run the rehearsal itself | Ship again in a few minutes; report it if it repeats |

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "ship",
  "op": "op_7f3c21aa90",
  "env": "prod",
  "outcome": "failed",
  "data": { "op": "op_7f3c21aa90", "app": "camp-bookings", "version": "v8", "state": "failed", "url": "https://camp-bookings.ac0.app", "migrations": ["20261020120000_amount_cents"], "decision": { "human": true, "reason": "Changes the production database (20261020120000_amount_cents)." }, "checks": [ { "name": "cents agree with dollars", "kind": "invariant", "ok": false, "detail": "it found violations on a fresh branch of production; run agc check on your mirror to see them" } ] },
  "error": { "code": "ship.check_failed", "message": "1 of 2 checks failed on a fresh branch of production: cents agree with dollars.", "fix": "Production is unchanged. Fix the change (not the check), run `agc check` on your mirror, and ship again.", "retryable": false },
  "next": [ { "cmd": "agc check", "why": "reproduce it on your mirror" }, { "cmd": "agc ship", "why": "ship again once fixed" } ]
}
```

## Limits

| What | Value |
|---|---|
| Invariant statement timeout | 30 s |
| Largest table the row check compares by key | 5,000,000 rows |
| Restart recovery picks up a quiet rehearsal after | 2 minutes |

## Agent notes

- A rehearsal's checks are the evidence; a green health check isn't. Every invariant needs a smoke flow that tries to break it.
- Don't try to read failure details out of a rehearsal. Reproduce on the mirror.
- Calls with the app's own keys (a payment provider, its own SMTP, a webhook) are real in a rehearsal. Skip them when `AGC_RUN` is `rehearsal`.
- `agc logs --release <op>` shows the rehearsal's own lines, scrubbed of secrets but not masked, and the fork is real data, so never log row contents.

## Related

- [Approvals](/docs/concepts/approvals.md)
- [Checks overview](/docs/checks/checks-overview.md)
- [Invariants](/docs/checks/invariants.md)
- [Smoke flows](/docs/checks/smoke-flows.md)
- [Safe schema changes](/docs/data/safe-schema-changes.md)
- [Logs](/docs/ship/investigating-a-reported-problem.md#logs)

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