
# The JSON envelope

Most agc commands write one final JSON object to stdout, the envelope, and exit with a code that says what kind of result it carries; three commands also stream NDJSON. A release is live only when the envelope has `"outcome": "live"` and the exit code is 0.

`ok` says whether the command did what it was asked; `next` says what to run now. For a person at a terminal agc renders the same envelope as text (see [Output modes](#output-modes)).

```json
{"ok":true,"product":"agent.cloud","command":"ship","data":{"op":"op_7f3c21a9","app":"camp-bookings","version":"v4","state":"live","url":"https://camp-bookings.ac0.app","release_url":"https://…/apps/camp-bookings/releases/op_7f3c21a9","migrations":["20261001120000_amount_cents"],"decision":{"human":false,"reason":"…"}},"next":[{"cmd":"agc status","why":"see production and your mirrors"}],"op":"op_7f3c21a9","env":"prod","outcome":"live","receipt":{"id":"rcp_9a1","url":"https://agent.cloud/r/rcp_9a1"}}
```

## Reference

### Fields

| Field | Type | Present | Meaning |
|---|---|---|---|
| `ok` | boolean | always | `true` when the command succeeded. A ship waiting for a person is `false` (exit 10). |
| `product` | `"agent.cloud"` | always | Identifies agc's output. Anything without it isn't agc. |
| `command` | string | always | The command as agc names it: `ship`, `db query`, `secret set`, `preview create`. |
| `op` | string | operations | The operation id (`op_…`) for `ship` and `wait`, including a ship that's still running. |
| `env` | string | operations | `"prod"` on a ship's or wait's outcome. |
| `outcome` | string | operations | On a ship or wait: `live`, `preview`, `waiting_approval`, `denied`, `failed` or `cancelled`; absent while the op is still running (exit 20). On `agc work show --follow`: `live`, `question`, `needs_person`, `ended` or `running`. |
| `receipt` | `{id, url}` | operations | Present only for a live release. Every other answer, a preview, a failed or a cancelled release included, has an op ID and no receipt. |
| `data.release_url` | string | ship operations | The release's page in the console, on every answer about a ship operation from `agc ship` or `agc wait`, whatever its state. A refusal before an operation exists (an unknown flag, `ship.bad_intent`, a snapshot problem) has no `op`, no `release_url` and no receipt. |
| `data` | object | usually | The command's result. Failures carry it when there's evidence to show (`checks`, `log_tail`, `warnings`). |
| `error` | `{code, message, fix, retryable, docs?}` | `ok: false` | `code` is stable (see the [catalog](/docs/reference/error-code-catalog.md)); `message` is for people; `fix` is the action; `retryable` is `true` only when the same command may succeed unchanged; `docs` is an `agc help` topic, when given. |
| `next` | `[{cmd, why}]` | always | Commands that make sense now, in order. Never empty on a failure. May contain placeholders like `<op>` or `<slug>`, or a non-command like `relay the console link`. |
| `decision` | – | never at the top level | Declared in `envelope.ts` but agc never sets it; a ship's decision is in `data.decision`. |

### Fields agc adds to `data` on any command

| Field | When |
|---|---|
| `claim_by`, `claim_warning` | The agent hasn't been connected to a person yet. `claim_by` is the deadline; the warning appears in its last day. |
| `agc_updated: {from, to}` | The first command after agc updated itself. |
| `agc_update_failed` | An automatic update failed; the command ran on the installed version, and `next` gains `agc update`. |
| `warnings` | Lint and setup warnings: unknown manifest keys, invariants with no racing smoke flow, money in float columns, wildcard egress grants. |
| `environment`, `control_url` | `agc status` and `agc doctor`, when `AGC_ENV` is set. |
| `live: {item, url}`, `tell_person` | `agc init`, `agc up`, `agc task start` and `agc task take`, when your credential owns an open Work item: `live.url` is its Live screen, and `tell_person` is the sentence to pass on to your person word for word. `agc up` gives it once per item. |

### Outcomes

`envelope.ts` declares eleven outcomes; agc sets ten.

| `outcome` | Exit | Set when |
|---|---|---|
| `live` | 0 | The release is serving. |
| `preview` | 10 | The first ship of an app nobody owns yet (its agent isn't connected to a person) runs privately at its preview until the person publishes it (`ship.preview`). Relay `data.relay` word for word: it carries `data.preview_url` and, when there is one, `data.publish_url`. Then `agc wait <op>` answers `live` once they publish. |
| `waiting_approval` | 10 | A person must decide. `data.approval.url` is the link to relay; `data.decision` carries `id`, `word` and `in_session` for deciding in your session. |
| `denied` | 12 | A person declined. |
| `failed` | 11 or 1 | 11 when a check failed in the rehearsal (`ship.check_failed`), 1 otherwise. A rehearsal failure leaves production unchanged; a later failure can leave production migrations applied. |
| `cancelled` | 1 | Agent Cloud cancelled the operation (a fleet gate's window, a stale task, a newer ship). Final: agc returns at once with the code control gave, or `ship.cancelled`. Nothing reached production. |
| `question` | 10 | `agc work show --follow`: the builder asked a question (`work.question`); it's in `data.question`. |
| `needs_person` | 10 | `agc work show --follow`: the item waits for a person (`work.needs_person`). |
| `ended` | 1 | `agc work show --follow`: the item ended with an exception (`work.ended`). |
| `running` | 20 | `agc work show --follow` reached its deadline before the item stopped (`work.still_running`). A ship still running has no outcome. |
| `rolled_back` | – | Never set by agc 0.3.0. |

### Streaming (NDJSON)

Three commands print more than one line, each a complete envelope:

- `agc wait`, when stdout isn't a terminal, prints one line per stage it waits through (`data.state`, `data.version`, `data.progress`; with an open approval, `data.approval` and `data.decision`), then the final envelope with the outcome.
- `agc logs --follow` prints `{"ok":true,…,"command":"logs","data":{"line":{…}}}` per log line until interrupted, then a final envelope with `data.followed` and `data.printed`.
- `agc work show <n> --follow` prints the item, then one line per new record entry, change of standing or change in what its owner says it's doing, each with `data.cursor`, then the final envelope when the item stops or `--timeout` (default 10 minutes) passes. At a terminal the lines are plain sentences.

Read the last line for `ok`, `outcome` and the exit code. `agc ship` doesn't stream: it prints only the final envelope (a person at a terminal sees a spinner on stderr).

## Output modes

agc prints the same envelope two ways: as one line of JSON for agents and scripts, or as a text view for a person at a terminal. The data, `next` steps and exit code are identical in both. Force a mode with a flag:

```bash
agc status --json
agc status --human
```

### How the mode is chosen

The first rule that matches wins (`outputMode` in agc's output code).

| # | Situation | Output |
|---|---|---|
| 1 | `--json` anywhere on the command line | JSON |
| 2 | `--human` anywhere on the command line | Text |
| 3 | `AGC_OUTPUT=json` or `AGC_OUTPUT=human` | That |
| 4 | Any of `AI_AGENT`, `CLAUDECODE`, `CURSOR_AGENT`, `CI`, `CODEX_SANDBOX`, `CODEX_THREAD_ID` is set (non-empty) | JSON, even inside a pseudo-terminal |
| 5 | stdout is not a terminal (a pipe, a file, a tool call) | JSON |
| 6 | stdout is a terminal | Text |

`--json` and `--human` together give JSON. `CODEX_HOME` alone doesn't count: it's a person's own Codex configuration, not a sign that Codex is running the shell.

### What text mode adds

| Feature | JSON mode | Text mode |
|---|---|---|
| Envelope | one JSON line on stdout | a rendered view on stdout: a dedicated view for `version`, `status`, `up`, `down`, `doctor`, `migrate`, `migration new`, `db query`, `db diff`, `db reset`, `init`, `import`, `exec`, `help`, `ship`, `wait`, `check`, `outbox`, `verify`, `events`, `agent install`, `agent uninstall` and `preview`; `key  value` lines for everything else |
| Progress | nothing on stderr | a spinner, then `✓`/`✗` with the duration, on stderr (only when stderr is a terminal) |
| Color | none | ANSI color when stdout is a terminal and `NO_COLOR` is unset |
| `agc wait` stages | one NDJSON envelope per stage | the spinner instead |
| Failures | `error` object | `✗ <message>`, then `fix` and `docs` lines, the check results, and the last log lines; a preview shows `!` and its `preview` and `publish` links instead |
| `next` | array | a `next` block, one command per line |

Text mode is for reading, not parsing: it drops fields that have no view. Scripts should pass `--json`. Mode never changes exit codes: a refused action is never exit 0 in either mode.

## Exit codes

agc's exit code says what kind of result the envelope carries, the same for a person and an agent. Branch on it, then read `error.code` for the specifics.

```bash
agc ship; echo "exit $?"
```

| Code | `EXIT` name | Meaning | What to do |
|---|---|---|---|
| 0 | `done` | The command did what it was asked. | Report the result; for a ship, the receipt. |
| 1 | `failed` | It didn't, for a reason in `error`. Includes most errors Agent Cloud's API answers. | Apply `fix` once. If it fails again the same way, report it (`agc feedback bug` when it's Agent Cloud's fault). |
| 2 | `usage` | The command was malformed: a missing argument, a bad value, a flag the command doesn't take (`usage.unknown_flag`), an invalid `agentcloud.toml`. | Read `agc help <command>`, fix the invocation or the manifest. |
| 10 | `approval` | A person must decide: a release, a first preview to publish (`ship.preview`), a domain, a builder change, a connection request, or a Work item's question or step (`work.question`, `work.needs_person` from `agc work show --follow`). | Relay `data.approval.url` exactly, or ask in your session when `data.decision.in_session` is true. Then `agc wait <op>`, or stop and report the op id. |
| 11 | `check` | A check failed: on your mirror (`check.failed`, `verify.failed`), in a ship's rehearsal (`ship.check_failed`), or `agc migrate` lost data (`migrate.not_preserved`). | Fix the change, not the check. Change a check only when this change makes it obsolete. |
| 12 | `denied` | Policy said no: a person declined, the app was deleted, the credential's purpose doesn't allow it, the organization hasn't granted it. | Report and stop. Retrying won't change it; a person has to. |
| 13 | `budget` | Over budget. | Never returned by agc 0.3.0. |
| 14 | `locked` | Another operation holds the environment. | Returned only as `context.changed`: the context doc changed since the `--base` you read; `agc context show <slug>`, then edit from its new `updated_at`. A ship blocked by another release gets `ship.locked` with exit 1. |
| 15 | `rolledBack` | Rolled back after release. | Never returned: there's no watch window or automatic rollback yet. |
| 20 | `running` | The operation is still running after `agc ship` or `agc wait` stopped following it (10 minutes), or a Work item hasn't stopped by `agc work show --follow`'s deadline. | `agc wait <op>`, or the `next` command for a Work item. Don't ship again. |

### Which codes agc emits

Counted from the `failure()` calls in agc's source (and `success()` calls for 0). A code with no call site can't be returned.

<!-- generated: exit-usage -->
| Code | `EXIT` name | Call sites in agc's source | Emitted |
|---|---|---|---|
| 0 | `done` | 142 | yes |
| 1 | `failed` | 185 | yes |
| 2 | `usage` | 176 | yes |
| 10 | `approval` | 10 | yes |
| 11 | `check` | 8 | yes |
| 12 | `denied` | 36 | yes |
| 13 | `budget` | 0 | **no: defined, never emitted** |
| 14 | `locked` | 1 | yes |
| 15 | `rolledBack` | 0 | **no: defined, never emitted** |
| 20 | `running` | 3 | yes |
<!-- /generated: exit-usage -->

### Exit codes outside the table

| Exit | When |
|---|---|
| any | `agc update` re-runs your command on the new version and exits with that run's code. |
| 1 | `agc devbox ports` in the foreground with no running Devbox. |
| 130 | `agc verify` interrupted with Ctrl-C. |
| ssh's | `agc devbox proxy` (SSH's ProxyCommand) exits with the tunnel's result. |

## Errors

A failure is the same envelope with `ok: false` and `error`:

```json
{"ok":false,"product":"agent.cloud","command":"exec","error":{"retryable":false,"code":"exec.no_command","message":"No command given.","fix":"Pass it after --, for example `agc exec -- npm test`."},"next":[{"cmd":"agc exec -- npm test","why":"run a command with the mirror database"}]}
```

An exit 2 example:

```json
{"ok":false,"product":"agent.cloud","command":"migration new","error":{"retryable":false,"code":"migration.no_slug","message":"No name given.","fix":"Pass a short name, for example `agc migration new amount_cents`."},"next":[{"cmd":"agc migration new <slug>","why":"create the file"}]}
```

Every code: [Error code catalog](/docs/reference/error-code-catalog.md).

## Agent notes

- Verify exit 0 and `outcome: live` before reporting deployment. Report failures with the op ID, outcome and error; do not invent a receipt.
- `ok: false` with exit 10 isn't an error to fix; it's the normal path for a first release. Relay `data.approval.url` exactly, or ask in session if `data.decision.in_session` is `true`; for `"outcome": "preview"`, relay `data.relay` word for word. Shipping again publishes nothing.
- `--no-wait` makes `agc ship` return at once with exit 20; that's expected, not an error.
- A cancelled operation is final: `agc ship` and `agc wait` exit 1 at once with `"outcome": "cancelled"` and the code control gave (`ship.cancelled` when it gave none).
- An error Agent Cloud answers keeps its own code but takes agc's exit: most are 1; approval, permission and deletion refusals are 12; `ship.idempotency_conflict` is 2. The [catalog](/docs/reference/error-code-catalog.md) lists each.
- Values from production (route names, error shapes from `agc capsule`) appear only in `data`, never in `next[].why`.
- agc scrubs every connection value and platform credential it holds from the printed envelope, so a value you see as `[set by agent.cloud]` is deliberate.
- Inside a pseudo-terminal that sets none of the agent variables (some harnesses do this), agc will render text. Set `AGC_OUTPUT=json` once in that environment, or pass `--json`.
- `agc devbox ports` in a terminal with no other argument runs in the foreground and prints plain lines, not an envelope. Agents get the envelope from `agc devbox ports --detach`, or from any run where stdout isn't a terminal.
- agc reads `--json` and `--human` anywhere on the command line, after `--` too, so `agc exec -- tool --json` switches agc itself to JSON as well as passing `--json` to `tool`.
- A ship blocked by another release is `ship.locked` with exit 1; read the decision from `data.decision`, never a top-level `decision`.

## Related

- [Outcomes and receipts](/docs/concepts/operations-and-waiting.md#outcomes-and-receipts)
- [Operations and waiting](/docs/concepts/operations-and-waiting.md)
- [Error code catalog](/docs/reference/error-code-catalog.md)
- [Approvals](/docs/concepts/approvals.md)
- [agc environment variables](/docs/reference/agc-environment-variables.md)

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