# Data access and the personal-data read ledger

Who may read which data, and the record that they did. Two things live here: **data access**
(who is allowed to read a restricted slice of a dataset) and the **read ledger** (one audit row for
every read of personal data). Both are enforced by the server; the dashboard only shows them.

## Data classes

| Class | What it is | Default | Needs `pii:read` |
| --- | --- | --- | --- |
| `person_level` | Who did what: subjects, their events, the identity ledger | **restricted** | yes |
| `replay` | Recorded sessions of a person | **restricted** | yes |
| `replay_moment_outline` | The text outline of one moment of a recorded session, for agents (`replay_moment_get`) | **restricted** | no: judged by the scope policy, see below |
| `survey_response` | An answer someone gave, with the identity they gave | **restricted** | yes |
| `aggregate` | Counts and rates over a cohort at or above the project's floor | unrestricted | no |
| `telemetry` | Spans, logs, metrics and error events: about the software | unrestricted | no |

`replay_moment_outline` is the person-level class behind the agent tool `replay_moment_get`, and the only class an agent
can be admitted to (see "An agent that acts for a named person" below). Every served call writes one `data.read` row.
Nothing is served at all while Moment replay is off, which it is on every deployment today.

Person-level product data is restricted by default, until an organization says who may read it. An organization changes the **mode** of a class; it
cannot add a class or change what a class is.

## Who may read a person

Reading a person is a person's act. A call is refused **before the organization's policy is read**
when the caller is an agent, a service, an OAuth client (even for the person who authorized it), a
delegated call, or an organization CI token. A person whose token lacks the human-only `pii:read`
scope is refused with that scope named. After that, in order: the caller must be able to read the
project, product analytics must be enabled for the project (otherwise the answer is `422
analytics_not_enabled`), and the organization's policy must admit them.

There is no MCP tool for person-level data except `replay_moment_get` (below). An agent that needs any other person-level
class asks the person to read it in the dashboard.

### An agent that acts for a named person

One class, `replay_moment_outline`, and one tool, `replay_moment_get`, may be read by an agent. For that class only,
the refusal above is replaced by the question "whom is this call for?", answered from the token alone:

- an **agent** whose token's `act.pid` names a person other than the agent itself acts for that person. An agent with no
  `act`, or whose `act.pid` is itself (the incident carrier is not a person), has no delegating person and is refused,
  whatever the class's mode;
- an **OAuth client's token** (a signed-in person's token with a `client_id`) acts for the person who authorized it;
  one that also carries `act`, or a `credential`, is ambiguous and is refused;
- a service, a guest and an organization token are refused as before.

A call that has a person is then judged, in order, and every step only refuses: the **agent's own** reach (`read_telemetry`
on the project, so its pins, scopes and network rules still bind it); then the **person**, as they would be on their own: an
active human principal who holds `pii:read` through their memberships and `read_pii` on the project; then the PA-01
gate; then the class policy, with the person as the subject. A person the organization has not named is refused while
the class is restricted, and the agent is never admitted by its own name or by the person's name alone. A delegator
that is missing, is another agent, or is not an active account is a refusal, and a lookup that fails is never read as
"admitted". The ledger row written before the answer records the person (`onBehalfOfPrincipalId` for an agent, and
`actingForPrincipalId` with `delegationBasis` in the metadata for either kind).

Every other call is unchanged: for any other class, an agent, a service, an OAuth client, a delegated call and an
organization token are refused exactly as in the first paragraph. Moment replay is switched off today, so this path
serves nothing yet.

## Modes and scopes

A **scope** names a slice of one class, by one attribute, and the people who may read it:

| Attribute | Example |
| --- | --- |
| `project` | `project = *` (every project) |
| `environment` | `environment = production` |
| `service.name` | `service.name = payments` (telemetry only) |
| `event_property:<name>` | `event_property:plan = enterprise` |
| `host_tag:<name>` | `host_tag:tier = db` (telemetry only) |

`*` as a value means every value of that attribute. A scope's people are users, teams of this
organization or organization roles. **Rows inside a scope's slice are readable only by its
people**, in whatever mode the class is. **Rows outside every scope** follow the class's mode:
`unrestricted` lets anyone who holds the right permission read them; `restricted` lets nobody until
a scope admits them. Two scopes that contain the same row are an OR.

An agent is judged as the person it acts for, so it can never read a slice that person cannot. An
agent with no person behind it is on nobody's list, even though it holds an organization role of
its own: a scope naming `role: MEMBER` admits the members who are people, and a scope's `user`
entries can name only people.

A restricted dataset that is not about a person (telemetry by `service.name`, aggregates by an
event property) is checked against the scopes in the same way. Aggregates are product analytics, so
they are also checked against the project, which must belong to the organization, and are refused
with `analytics_not_enabled` until product analytics is enabled for that project.

A scope is versioned. Every change adds a version holding the state it produced, and
`restoreVersion` (on `PATCH`) writes an exact earlier state back as a **new** version; history is never
rewritten. `expectedVersion` makes a write a compare-and-set (409 `stale_version`).

## Changing it

Creating or changing a scope, and setting a class's mode, are **human-only** acts (`org:admin`,
so an organization **owner**): a person's own signed-in session, not a token, an agent or an OAuth
client. Each writes an audit row with `before` and `after`, in the same transaction as the change,
and tells the owners afterwards.

| Route | Does | Who |
| --- | --- | --- |
| `GET /orgs/:o/data-access?project=` | What you may read of each class, in which tier, and the cohort floor | any member |
| `GET /orgs/:o/data-scopes` | Every scope | owner |
| `POST /orgs/:o/data-scopes` | Create a scope (`data_scope.create`) | owner, in a session |
| `GET /orgs/:o/data-scopes/:id` | One scope and its versions | owner |
| `PATCH /orgs/:o/data-scopes/:id` | Change principals, archive, or `restoreVersion` (`data_scope.update`) | owner, in a session |
| `GET /orgs/:o/data-access-defaults` | The mode of each class | owner |
| `PUT /orgs/:o/data-access-defaults` | Set one or more modes (`data_access_default.update`) | owner, in a session |

`data_access_get` (MCP, read, `project:read`) answers the same question as the first route for an
agent: the mode of each class, the tier it will be served (`stored`, `sampled_agent`, `aggregate`
or `refused`), the scopes that bind it, and the project's cohort floor. For `replay_moment_outline` the tier is the gate's
own answer for the credential: `stored` for an agent whose delegating person would be admitted (and whose own token can
read the project), `refused` for an agent with no delegating person, whose person lacks access, or while PA-01 is shut.
Every other person-level class is `refused` to an agent, and `caller.personLevelEligible` stays `false` for one. Scopes that carve a slice
out for **other** people are counted, never described. A scope's attribute text is untrusted data.

## The read ledger

Every read that returns personal data writes one audit row, `data.read`, **after** the read, and
**the read fails if the row cannot be written**: data that was taken but not recorded is not
handed over.

A row holds the surface (`dashboard`, `api`, `mcp`, `worker` for Bucker's own background processing, `cli`), the tool or route, the data
class, the tier the data was held in, the **categories** of personal data that came back with
their counts (`email`, `phone`, `ip_address`, `name`, `user_id`, `device_id`, `location`,
`payment_card`, `government_id`, `credential`, `free_text`, `other`), how many rows, the approval
it ran under when one applied, where it went next (`client` or `model_provider`), and the **digest**
of the query. It never holds a value, an identifier or the query text. The actor fields are the
same as every other audit row: who, in which mode, through which transport and credential, for
which client.

**Volume.** A read of the stored tier, or one that returned any category, is one row per call.
A read that returned only aggregates and no category is one row per principal, session and hour,
recorded within about 60 seconds. It is never sampled.

The hourly [read counts](org-administration.md#read-counts) (`audit_read_counts`: how much each MCP
principal read, by hour) also carry the categories.
