
# Production data protection

An agent never connects to production's database. It works on a masked copy (a mirror), reads production with `agc db query --env prod` against a masked branch, and sees rehearsal results as pass or fail. On a customer's private target, production reads need an owner's setting.

A successful production read returns `"env": "prod"`, `"read_only": true` and `data` naming the masked branch's time. Logs are scrubbed of credentials, so keep personal data out of log lines.

## How it works

### Masking policy

`maskPolicy()` is an allowlist the platform decides, not the agent ([Masking](/docs/concepts/masking.md#how-it-works) has every rule):

- numbers, dates, UUIDs, enums and list-constrained text stay real;
- few-valued labels stay visible;
- names, emails, phones and addresses get pseudonyms salted per app;
- other text becomes lorem; other types are blanked.

`[data.db] mask` rules can only add masking, a `keep` request is ignored and reported, and only an owner can keep a column, in the console. If no safe masked value passes a NOT NULL column's CHECKs, no copy is made.

### Masked copies are checked before anyone gets them

Copies are anonymized database branches, deleted if they fail these checks for failed masking:

| Check | Rule |
|---|---|
| `verifyMasked()`, table with a primary key | Checks blanked values; samples up to 1,000 rows of masked columns against production, allowing pseudonym collisions up to 2% plus one |
| `verifyMasked()`, table without one | Rejects at 90% overlap between sampled distinct values |
| `assertNoSecrets()` | No exposed masking salts in the copy's readable catalog |
| Owner password | Reset on the copy only, so its URL can't open production |

### Production queries

`agc db query --env prod` reaches `POST /v1/apps/:app/query`, which reads `agc-prod-view`, a masked branch built with the same policy. It's at most an hour old and never older than the last release; `--fresh` waits for a new one. The result's `masked_columns` names each column that may hold a masked value, traced through views and subqueries; the CLI prints them after `masked:`. Each query is one prepared statement in a read-only session, at most 100 rows (`truncated: true` past that) and 5 seconds.

### Rehearsals

A rehearsal runs the new release against `rehearse/<op>`, an unmasked fork of production, because masking changes behavior. What the agent gets back is reduced: invariants report pass or fail with a fixed sentence, a database error becomes one of a fixed set of sentences (`safeDbError`), and smoke-flow response bodies read "(hidden: this ran on a branch of production)".

### Logs

`agc logs` lets Builder and Read-only agents read production and rehearsal lines, kept 30 days. Every line passes `scrub()` from `infra/scripts/redact.mjs`, which replaces credential shapes: `Bearer`/`Basic` values, credential-named keys, cookies, URL passwords, JWTs, known token formats, signed-URL parameters and long opaque strings. Lines are cut at 2,000 characters, and `--grep` searches the scrubbed text. Mirror-only and unclaimed agents get `logs.not_allowed`. The console shows the owner the unscrubbed line.

## Data residency on private targets

For an app on a customer's target, mirrors hold no production rows, masked or not; `agc status` shows `mirrors: empty`. The app's Postgres lives in Agent Cloud's account; your network keeps the running app and its traffic.

Only the relay (for the app's pods and the runner, over TLS) and Agent Cloud's control plane connect to production. Control does so for migrations, invariants on rehearsal forks, and the mirror, a `schema-only` branch that gets production's `agentcloud_migrations` rows before the seed runs. Rehearsals run on an unmasked fork of production, with their pods on your node.

Production's branch is marked protected. With `PRODUCTION_ALLOW_IPS` configured, control also applies the database provider's IP allowlist to protected branches, admitting only control's and the relay's addresses; a failure to apply it is logged rather than blocking the app's setup.

A task sent to your runner carries only that app's `DATABASE_URL` (production or the rehearsal fork), sign-in and email credentials, granted connections' credentials, image pull token and, for an allowed production read, that query's one-hour read-only login; never the database provider API key, GitHub key or Agent Cloud's own tokens.

Any target Agent Cloud doesn't run, or one control can't read, counts as a customer's; platform targets get masked mirrors. Owner-authorized [connection](/docs/owners/connections.md) read credentials on mirrors aren't masked by this policy.

| Path on a customer's target | Result ([every path](/docs/data/reading-production-safely.md#data-on-private-targets)) |
|---|---|
| `agc up`, `agc db reset` | Schema-only mirror, then the seed |
| `agc up --at`, production `agc logs` | Refused: `target.private_data`; log lines are production data too |
| `agc db query --env prod` | Refused with `target.private_data` unless an owner turned on production reads (below) |
| `agc import` | Local dump validation only; console completion is unavailable and egress-only targets refuse restore |
| `agc status` | `mirrors: empty`; `internal: false` only with a public ALB |

| Data | Where it lives |
|---|---|
| Production Postgres and its branches | Agent Cloud's account |
| Images and build sources | Agent Cloud |
| Running app, its memory and local disk | Your node |
| App logs and request metrics | Your node |
| Connections (Snowflake and others) | Where they already are; never forked |

### Can an agent read production on a private target?

Yes, read-only, once an organization owner turns on "Agents may read this organization's apps' production databases" in the organization's General settings. It's off by default, only an owner changes it, and each change is an `org.prod_db_reads` Activity entry.

Then `agc db query --env prod` from a connected Builder or Read-only agent becomes a `query` task for the target's runner, which reads through the relay with a login made for that query: only `pg_read_all_data`, read-only transactions, expiring in an hour and dropped once the runner reports. Any read runs (`SELECT`, `WITH`, `EXPLAIN`, `SHOW`); no write lands. The agent never gets the credential.

The answer is production itself, unmasked: `"masked_columns": []`, `"truncated": false`, and `data` reading `production itself, read-only and unmasked, as of <time>`. A result up to 32 MiB comes back whole; a larger one gets `query.result_too_large`, so add a `WHERE` or `LIMIT`, or select fewer columns. Control keeps only the SQL and the row count, never the rows. Mirror-only and unclaimed agents, deploy keys and task tokens still get `target.private_data`.

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md). On a customer's target, `target.private_data` (exit 12) means work on the seeded mirror, or have an owner turn on production reads with the link in `fix`:

```json
{ "ok": false, "product": "agent.cloud", "command": "db query",
  "error": { "code": "target.private_data", "message": "This app runs on a private target: production data never reaches an agent, not even masked.", "fix": "Query your mirror instead (`agc db query` without --env), or ask an owner of Example Co (owner@example.test) to turn on “Agents may read this organization’s apps’ production databases” in its General settings. Give them this link, exactly as printed: https://console.agent.cloud/…", "retryable": false, "docs": "agc help data" },
  "next": [{ "cmd": "agc up --detach", "why": "your mirror, from the seed" }] }
```
## Agent notes

- On Agent Cloud's cloud, `--env prod` is a masked copy up to an hour old. Use `--fresh` before drawing conclusions about recent writes.
- Numbers, dates (including birth dates) and IDs aren't masked. In a small data set they can identify a person. Don't paste query results into issues or chat.
- `target.private_data` is policy, not an outage. Don't retry it or look for another route to production; write seed data that reproduces the case, or pass the `fix` link to the person. A mirror with no rows there isn't broken; check `[data.db] seed`.
- Rows from an owner-allowed production read on a private target are real and unmasked: use them for the question asked, and keep them out of files, commits and chat.

## Related

- [Masking](/docs/concepts/masking.md)
- [Reading production safely](/docs/data/reading-production-safely.md)
- [Data on private targets](/docs/data/reading-production-safely.md#data-on-private-targets)
- [Architecture of a private target](/docs/targets/architecture-of-a-private-target.md)
- [Logs](/docs/ship/investigating-a-reported-problem.md#logs)

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