
# PR previews

> **Preview.** Available for apps on a qualified target, customer AWS targets included. An agent connected to a member of the app's organization can list and read previews; creating and stopping one needs a Builder agent. Details may change.

A PR preview runs an open pull request at its own address, on a branch of the app's database with its data, to try before release. `agc preview create` finds the PR from your current branch. It's ready when `agc preview get` shows `state: "ready"` and a `url`.

```bash
agc preview create
```

## Ask your agent

```text
Make a preview of my open pull request for camp-bookings and send me the link when it's ready.
```

The person gets an `open_url` that goes through the console's sign-in to the preview. The console's **Previews** page lists open PRs with **Create preview** on each.

## Steps

```bash
agc preview create
agc preview get
agc preview list
agc preview stop
```

Push your branch and open the PR first, or pass `--pr <number>` to skip the branch lookup. A create answers `disposition` `created` or `existing` and doesn't wait for the build, so poll `get`. New commits on the PR queue an update of the same preview (`update_queued: true`), on the same database branch.

```json
{
  "ok": true,
  "product": "agent.cloud",
  "command": "preview create",
  "data": {
    "app": "camp-bookings",
    "update_queued": false,
    "disposition": "created",
    "preview": { "id": "pv_…", "repo": "agentcloud-apps/camp-bookings", "pr": 7, "state": "queued", "url": null, "…": "…" },
    "message": "Created preview for PR #7."
  },
  "next": [{ "cmd": "agc preview get --pr 7", "why": "inspect status, link and expiry" }]
}
```

## How it works

**The PR.** agc reads the current branch and its upstream remote (else `origin`), which must point at the app's linked GitHub repo. Control then finds that open same-repository PR itself and refetches its head, so your local git is only a hint. Fork PRs are refused. You don't need a GitHub token or `gh`.

**The data.** The preview's database is a write-isolated child branch of the app's own database, schema and data. Writes and migrations there never reach the parent or other previews. This is deliberate: people who can open the preview, and the PR's code, see the app's data. Copied integration credentials are removed.

**Effects are captured, not sent.** The preview's app gets its own `AGC_AUTH_URL` and `AGC_EMAIL_URL` on the preview host, and `AGC_PREVIEW_EXPIRES_AT`. Email, webhook and payment effects go to the preview's capture, and no live provider is called. Sign-in maps the viewer's verified email to the matching copied app user, keeping their id and roles; no match, no access.

**The address.** A preview serves at `https://<app>-<number>.preview-agent.cloud`, such as `https://camp-bookings-7.preview-agent.cloud` for PR 7. On a customer AWS target it uses `<app>-<number>` in that target's own host pattern. Access goes through the console's sign-in. Each grant is for one incarnation, so a bookmarked host can't open a later recreation.

**Lifetime.** A preview expires 72 hours after its last distinct new head, and at most 7 days after it was created. Visits, lists and retries never extend it, and there's no manual renewal. After cleanup, creating again starts a fresh preview with new clocks.

**Logs.** `agc logs --release pv_…` (the preview's `id`) returns that preview's own output, scrubbed like production's, so you can see why its start-up failed. Its lines never count among the app's grouped errors.

**Quota.** Three allocated previews per app. Queued, building, ready, failed and deleting previews all take a slot, and a slot frees only once cleanup is verified, not when `stop` returns.

## Reference

| Command | Does |
|---|---|
| `agc preview create [--pr <n>]` | Create, or return the existing preview for that PR (`disposition: existing`) |
| `agc preview get [--pr <n>]` | The PR's latest preview; doesn't allocate or renew |
| `agc preview list` | Active and cleanup-pending previews, recent cleaned ones, and `quota` (`allocated`, `limit`) |
| `agc preview stop [--pr <n>]` | Request cleanup; repeating is safe |

| Field | Meaning |
|---|---|
| `state` | `queued`, `building`, `ready`, `failed`, `deleting`, `deleted` |
| `desired_sha` / `serving_sha` | The PR head to serve / the one being served |
| `preview_url` | The preview's address, always present |
| `url` / `open_url` | Set only while ready, serving the current head, and before expiry; `open_url` goes through the console's sign-in |
| `console_url` | The app's Previews page |
| `expires_at` / `hard_expires_at` | Idle expiry / the 7-day cap |
| `cleanup_pending` | Deleting, or expired and not yet cleaned up |
| `storage` / `analytics` | Whether the preview got its file storage / analytics branch, with `code`, `message` and `fix` when it didn't; null when the app has none |

## Errors

| Code | Exit | Cause | Fix |
|---|---|---|---|
| `preview.unavailable` | 1 | Previews are off in this environment, the app's target can't serve them, qualification is missing, or state could not be verified | Follow the error's `fix`; check the app's console Previews page |
| `preview.branch` / `preview.repository` | 1 | No usable branch or remote / remote differs from the linked repo | Pass `--pr <number>` |
| `preview.pr_not_found` / `preview.pr_ambiguous` | 1 | No open PR for this branch / more than one (`data.prs`) | Pass `--pr <number>` |
| `preview.quota` | 1 | Three previews already allocated (`data.previews`, `data.quota`) | `agc preview stop` one and wait for cleanup |
| `preview.not_found` | 1 | No preview for that PR | `agc preview create` |
| `preview.owner_required` | 12 | An agent nobody has connected to a person | Have the person claim or connect the agent |
| `app.unavailable` | 12 | The agent's person isn't in the app's organization | Use an agent connected to a member |
| `agent.purpose` | 12 | A Mirror-only or Read-only agent creating or stopping a preview | Use a Builder agent |
| `preview.usage` | 2 | Unknown action or flag | `agc preview create|get|list|stop [--pr <n>]` |
| `cli.not_available` | 1 | This control doesn't support the command | Use the agc built for this environment |

```json
{
  "ok": false, "product": "agent.cloud", "command": "preview create",
  "data": { "previews": ["…"], "quota": { "allocated": 3, "limit": 3 } },
  "error": { "code": "preview.quota", "message": "…", "fix": "Inspect agc preview list before retrying; stop a preview and wait for cleanup if all slots are allocated.", "retryable": false, "docs": "agc help preview" },
  "next": [{ "cmd": "agc preview list", "why": "inspect the existing preview state" }]
}
```

## Limits

- 3 allocated previews per app. 72 hours idle, 7 days total. One preview per PR.

## Agent notes

- A `url: null` with `state: "building"` is normal; don't create again.
- On a timeout, run `agc preview list` before retrying a create or stop.
- A preview contains the qualified app's real data. Keep it out of chat.

## Related

- [Environments](/docs/reference/agc-environment-variables.md#environments-production-and-staging)
- [The app's GitHub repo](/docs/ship/the-apps-github-repo.md)
- [Mirror vs production](/docs/concepts/mirror-vs-production.md)

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