# Production approver

An agent that runs at night or in CI has nobody to ask. Without the admin
scope, a drop, a delete, a restore or a key rotation on **production** is
refused, so the work waits on a branch until someone with an admin token
shows up.

A production approver fixes that with one name: one email address per
project. The agent's request is stored, that person gets an email, and the
agent waits. Approve runs exactly the stored request. Deny, or no answer,
runs nothing.

## Turn it on

One field, in any of three places. It takes an owner or admin.

**Console.** Project > Settings > Policies > *Production approver*. Type the
email, Save.

**API.**

```sh
curl -X PUT https://api.scribase.com/v1/organizations/acme/projects/web/prod-approver \
  -H "Authorization: Bearer $SCRIBASE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"email":"lead@acme.com"}'
```

`GET` on the same path reads it, `DELETE` removes it (requests are refused
with `403` again). An agent token needs `project:<id>:admin` to change it: a
token that could name its own approver would approve itself.

**MCP.** `prod_approver_set` with `email` (an admin token). Without `email`
it shows the current approver; `remove: true` removes them.

The approver does not need a Scribase account. They get one short email when
they are named, so the first real request is not a surprise.

## What the agent sees

A token with `project:<id>:write` (plus `sql:write` or `migrations:write`)
sends a destructive request against production: `confirm: true` SQL
(`DROP`, `TRUNCATE`, `DELETE` or `UPDATE` without `WHERE`), a migration with
such SQL, a point-in-time restore, a backup restore.

Without an approver: `403 insufficient_scope`, as before.

With an approver the answer is `202`:

```json
{
  "status": "approval_required",
  "approval_id": "apr_5f0c9d3a7b1e4c2a9d8e6f01",
  "state": "pending",
  "approver": "lead@acme.com",
  "expires_at": "2026-11-26T11:40:12Z",
  "operation": {
    "method": "POST",
    "path": "/v1/organizations/acme/projects/web/environments/production/database/execute",
    "summary": "Run SQL: drop table legacy_orders"
  },
  "status_url": "/v1/organizations/acme/projects/web/approvals/apr_5f0c9d3a7b1e4c2a9d8e6f01",
  "wait_url": "/v1/organizations/acme/projects/web/approvals/apr_5f0c9d3a7b1e4c2a9d8e6f01?wait=25",
  "next": "Not run. ... Do not send the request again: on approve the stored request runs exactly as sent."
}
```

Nothing has run. The same token sending the same request again while it is
pending gets the same `approval_id` back and no second email.

## Wait for the answer

**MCP.** `approvals_wait` with the `approval_id`. It holds up to
`wait_seconds` (default 45) and returns the state. Call it again while the
state is `pending`.

**API.** `GET` the `status_url`. Add `?wait=25` to long-poll: the answer is
held until the request is decided or 25 seconds pass.

```sh
until state=$(curl -s "https://api.scribase.com$STATUS_URL?wait=25" \
    -H "Authorization: Bearer $SCRIBASE_TOKEN" | jq -r .state); \
  [ "$state" != pending ] && [ "$state" != approved ]; do :; done
echo "$state"
```

| `state` | Meaning | What the agent does |
|---|---|---|
| `pending` | Waiting for the approver | Keep waiting. Do not send the request again. |
| `approved` | Approved, the stored request is running | Ask again in a moment. |
| `executed` | Approved and run exactly as sent. `result` holds the HTTP status and body the original request would have had | Continue. |
| `failed` | Approved, but the request itself failed (`result` says why) | Fix the cause, send a new request. It needs a new approval. |
| `denied` | The approver said no. Nothing ran | Stop. Do the work on a branch or ask what to change. |
| `expired` | No answer in 24 hours. Nothing ran | Send the request again to ask again. |

`result.body` is shown to the token that asked and to members with at least
the developer role.

## What the approver sees

An email from `no-reply@scribase.com`: the project and environment, the token
that asked and whose it is, what would run (the SQL, or the route), and two
links, **Approve** and **Deny**.

Each link opens a page that shows the exact request and one button. The link
alone decides nothing, so a mail scanner that opens it cannot approve a
change. The token in the link works once and dies at expiry; a second click,
or a click after the other decision, gets "already used".

## What approve does

- It runs the stored method, path, query and body. Nothing else, and nothing
  the approver typed.
- It runs as the token that asked, with the admin role for that one request.
  The token's other limits still hold (its scopes, its organization, SQL never
  as a superuser). If the token was revoked or expired in the meantime, the
  request does not run.
- A token holds at most 5 pending requests and a project at most 20 (the
  next one gets `429`), and a stored body is at most 256 KB.

## Audit

Every step is in the token's audit log (`GET /v1/account/tokens/{id}/events`,
`scribase token events <id>`, or *Activity* on the tokens page), with `event`
and `detail` set:

| `event` | `detail` |
|---|---|
| `approval.requested` | the approval id and who was asked |
| `approval.approved` | the approval id and who approved |
| `approval.executed` | the approval id; `status` is the result of the run |
| `approval.denied` | the approval id and who denied |
| `approval.expired` | the approval id and who did not answer |

The organization audit log has the same steps (`prod_approval.requested`,
`.approved`, `.denied`, `.expired`) plus `prod_approver.set` and
`prod_approver.removed`.

## What it does not cover

A migration push that carries several migrations at once (`migrate_run`,
the full-set form of `POST .../migrations`) is refused with `403` for a
non-admin token when a pending migration is destructive and the target is
production. Send that migration on its own (`migrations_apply`) and it waits
for the approver.

Deleting the production environment itself or the whole project stays a
console action. Routes an agent token can never use (members, billing, keys
of the organization) are not approvable. A token without write access to the
project gets a plain `403`.
