
# Masking

Every copy of production an agent can read (your mirror, `agc db reset`, `agc up --at`, `agc db query --env prod`) is masked. Names, emails, phones, addresses and free text are replaced. Numbers, dates, booleans, UUIDs and labels stay real, so totals, joins, invariants and bugs still reproduce.

Agent Cloud decides the policy from the database's catalog. You can add masking in `agentcloud.toml`; only the app's owner or an organization owner can remove it, in the console, with a confirmation.

```toml
[data.db]
mask = ["bookings.notes:text", "parents.passport_no:blank"]
```

## Ask your agent

```text
Start a mirror and find out why some bookings show a total of zero. Check that it hides
parents' names and phone numbers, mask the bookings notes column too, and show me counts, not rows.
```

## Steps

```bash
agc up --detach
agc db query "SELECT count(*) FROM bookings WHERE total_cents = 0"
agc db query "SELECT status, count(*) FROM bookings GROUP BY status" --env prod
```

Without `--env`, `agc db query` runs any SQL on the mirror's branch and returns JSON. `agc db` opens `psql` for a person.

## How it works

| Copy | What it holds |
|---|---|
| Mirror (`agc up`, `agc up --at`), `agc db reset` | A masked branch of production. With no production data yet, your migrations and the made-up rows in `seed.sql` |
| `agc db query --env prod` | One read-only, row-limited statement against a masked view, refreshed every few minutes; `data.as_of` says when it was taken, `--fresh` waits for a newer one. It never touches production |
| Masked branches for one-hour logins on the **Database** page | The same policy |
| An organization's own AWS target | Mirrors get production's schema with no rows, then the seed. `--env prod` answers `target.private_data` unless an organization owner turned on "Agents may read this organization’s apps’ production databases"; then Builder and Read-only agents read production itself, read-only and unmasked |
| Rehearsal | An unmasked fork agents never read: pass or fail per check, but its log lines (`agc logs --release <op>`) are scrubbed of secrets, not masked |

```mermaid
flowchart TD
  C[Column in production] --> T{Type}
  T -- "number, date, time, bool, uuid, enum,<br/>or text a CHECK limits to a list" --> K[kept]
  T -- "array, json, bytea, inet, xml, other" --> B[blanked]
  T -- text --> O{Owner made it visible?}
  O -- yes --> K
  O -- no --> N{Name or shape}
  N -- "email / phone / address / name-like" --> P[pseudonym or partial]
  N -- "few distinct values, not named like free text or a credential" --> K
  N -- otherwise --> X[text: lorem or a UUID]
```

The manifest can add masking to any column, an allowlisted number, date or enum included, but never remove it. A rule on a text column masks it as the rule's type says; on any other type it blanks the column.

| Column | On copies |
|---|---|
| `int2`, `int4`, `int8`, `numeric`, `float4`, `float8`, `money`, `bool`, `date` (dates of birth too), `time`, `timetz`, `timestamp`, `timestamptz`, `interval`, `uuid`, `oid`, declared enums, text a `CHECK` limits to a list of literals | Kept as is |
| Text with at most 20 distinct values, each repeated across at least five times as many filled rows, not named like free text (`note`, `comment`, `message`, `description`, `reason`, `medical`, `allerg…` and the like) or a credential (`password`, `secret`, `token`, `api_key`, `pin`, `ssn` and the like) | Kept as a label, so `status = 'confirmed'` works |
| Other text | Masked by its name (below) |
| Every other type: arrays, JSON, `bytea`, `inet`, `tsvector`, XML, composites | Blanked to an empty value of its type: `'{}'` for arrays and JSON, `NULL` where nothing else fits |

| Text column name contains | Masked as | How |
|---|---|---|
| `email` | `email` | Salted pseudonymous address |
| `phone`, `mobile`, `tel` | `phone` | Keeps the first 2 and last 2 characters, `******` between |
| `address`, `street`, `city`, `postcode`, `zip` | `address` | Salted pseudonymous city |
| `name`, `first_name`, `child`, `parent`, `submitter`, `owner`, `user`, `customer` and similar | `person` | Salted pseudonymous first name |
| anything else | `text` | 12 characters of lorem, or a random UUID when a CHECK or unique index covers it |

A `NULL` stays `NULL` on every copy.

**Pseudonyms are consistent within a copy.** Each copy is masked with a fresh random key, so one real person gets the same pseudonym in every column of that copy, and `approver = submitter` still holds. The key is removed from the copy's catalog before anyone can connect, so nobody can reverse a pseudonym by guessing names, and a key from one copy reverses no other.

**When a masked value would break a constraint**, the copy is still made, never with the real value. A column whose CHECKs no mask passes is masked to `NULL` where it may be `NULL`; otherwise it's masked anyway and the CHECK comes back `NOT VALID` (still enforced on new writes). A table where a masked value would break a `NOT NULL`, a foreign key or a unique index is copied empty. `agc up` names both in `data.masking.not_valid` and `data.masking.emptied`. `agc db query --env prod` names each emptied table too, with the reason: an `emptied` list of `{table, reason}` in the JSON, and `emptied by masking: <table> (<reason>)` in text output.
## Making a column visible

1. On the app's **Settings** page, find the **Masked on mirrors** card.
2. Under **Also visible as they are**, type `table.column` (say `bookings.status`) and press **Add column** or **Save changes**.
3. "Show this column to agents as it is?" names it and warns that every mirror and branch will carry its real values, readable by any agent on the app. Press **Show <table.column> unmasked**, or **Cancel**.

The column then shows as a chip; **×** and **Save changes** mask it again, with no confirmation. The change applies to copies made after you save, and an existing mirror keeps what it had until reset. The **Database** page shows each table's masked-column count and each column's badge (**Masked · <type>** or **Visible**).

Keep only columns that hold nothing about a person and that masking breaks: a SKU, a country code, an external status with too many values to count as a label, or a non-sensitive column whose masking rule a copy refuses (`mirror.build_failed` names it). Never keep anything a person typed.

## Reference

| Mask type | In `mask` | Effect |
|---|---|---|
| `person` | yes | Pseudonymous first name |
| `email` | yes | Pseudonymous address |
| `phone` | yes | Partial: `+1******89` style |
| `address` | yes | Pseudonymous city |
| `text` | yes | Lorem, or a UUID under a CHECK or unique index |
| `blank` | yes | Empty value of the column's type |
| `keep` | ignored | Only an owner can keep a column, in the console |

Rules are `"table.column:type"`, or `"schema.table.column:type"` outside `public`. A `varchar(n)` shorter than a mask's longest output gets `n` characters of lorem instead, so the copy doesn't fail on length.

Console column format: `table.column`, lowercase letters, digits and `_`, each part up to 63 characters; anything else is dropped on save. Each change is recorded as app event `masking.changed`.

## Errors

| `code` | exit | Cause | Fix |
|---|---|---|---|
| `target.private_data` | 12 | The app is on an organization's own target, where production isn't copied, and production reads are off for this agent | Use the mirror's seed data, or relay the link in `fix` so an organization owner can turn production reads on |
| `mirror.build_failed` | 1 | The database engine refused one of Agent Cloud's masking rules, named by table and column | Report it with `agc feedback bug`, quoting the message; if the column isn't sensitive, an owner can make it visible, then run the command again |

An invalid rule is refused with: `Invalid mask rule "bookings.notes": use "table.column:type" (or "schema.table.column:type") with type person, email, phone, address, text, blank or keep.`

## Limits

| What | Value |
|---|---|
| Distinct values for a text column to stay as a label | 20 or fewer |
| Masked production view: fresh enough to serve directly | up to 10 minutes old |
| Older view served while a refresh starts | up to 1 hour old, with no release since its fork |
| Masked production view: never served | older than 1 hour, or older than the last release |
| Columns an owner can make visible per app | 100 |

## Agent notes

- Masking isn't permission to log or send data. Keep rows out of chat, logs, commits and migration-time code, masked or not; counts and totals are fine.
- Don't ask the person to unmask a column to debug. Ask only when the app can't work without it, naming the column and why. A `keep` rule does nothing.
- New text columns need no rule: the policy masks them by name and shape on every copy. `agc init` writes its inferred rules into `agentcloud.toml` as a record, with each reason as a comment.
- A table named in `data.masking.emptied` or in a query's `emptied` list has no rows on that copy, so a count of zero there says nothing about production.

## Related

- [Reading production safely](/docs/data/reading-production-safely.md)
- [What agents can and can't do](/docs/concepts/what-agents-can-and-cant-do.md)
- [Mirror vs production](/docs/concepts/mirror-vs-production.md)
- [Seed data](/docs/data/mirror-db-tools.md#seed-data)
- [Console database access](/docs/data/reading-production-safely.md#console-database-access)

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