# Versioning and diff

A segment is an audience and a broadcast is a message. Both change, and both
raise the same question later: who changed what, when, and what actually went
out. Every write to a segment or a broadcast keeps its own numbered state, so
that question has an answer instead of a guess.

A state is a copy, not a pointer. Changing a segment afterwards does not change
the states that were taken before it.

## What is archived

A segment state holds the fields that decide who is in the audience:

| Field | Meaning |
| --- | --- |
| `name`, `description` | the labels a reviewer recognises |
| `rules` | the rule tree the evaluator reads |
| `membership_mode` | `rules` or `static` |
| `exclude_segment_id` | the segment whose contacts are removed, if any |
| `created_at`, `created_by` | when and by whom the state was written |
| `content_hash` | fingerprint of the fields above, see below |

A broadcast state holds the fields that decide what goes out, as one JSON object
under `snapshot`:

`name`, `subject`, `preview_text`, `from_email`, `reply_to`, `text_body`,
`html_body`, `list_id`, `segment_id`, `topic_id`, `route_slug`, `scheduled_at`,
`track_opens`, `track_clicks`, `template_id`, and the `status` the broadcast was
in when the state was taken.

A broadcast state also carries a `reason`: `create`, `update`, `schedule` or
`start`. The state written on `start` is the record of what actually went out.

## When a state is written

| Write | Segment | Broadcast |
| --- | --- | --- |
| created | state 1 | state 1, reason `create` |
| edited | next state | next state, reason `update` |
| scheduled | not applicable | next state, reason `schedule` |
| started | not applicable | next state, reason `start` |
| restored | next state | next state, reason `update` |

A write that leaves every audited field as it was writes no state. The check
uses a fingerprint: the audited fields are put in a canonical order and hashed
with SHA-256, and the hash is compared with the newest state. Rule JSON is
decoded before hashing, so writing the same rules with the keys in another order
still hashes the same. This keeps a repeated save from filling the history with
identical copies.

Version numbers count per segment and per broadcast. They start at 1 and never
repeat, not even after a restore.

## What the diff shows

```
GET /api/practices/{id}/segments/{segmentID}/versions/{version}/diff?against=current
GET /api/practices/{id}/broadcasts/{campaignID}/versions/{version}/diff?against=3
```

`against` is a version number or `current` for the live segment or broadcast.
The answer has `from`, `to`, `against`, `identical` and a `changes` list.

```json
{
  "from": 1,
  "to": 3,
  "against": "3",
  "identical": false,
  "changes": [
    { "field": "name", "before": "Alle Kontakte", "after": "Aktive Kunden",
      "summary": "name: Alle Kontakte -> Aktive Kunden" },
    { "field": "rules.op", "before": "and", "after": "or",
      "summary": "rules.op: and -> or" },
    { "field": "rules",
      "before": [ { "field": "status", "operator": "eq", "value": "active" },
                  { "field": "city", "operator": "eq", "value": "Wien" } ],
      "after":  [ { "field": "status", "operator": "eq", "value": "active" },
                  { "field": "city", "operator": "in", "value": "Wien" } ],
      "summary": "rule 2: operator eq -> in" }
  ]
}
```

Two details matter for reading a diff:

- A rule tree is compared rule by rule, not as raw text. Added, removed and
  changed rules are named by their position, and a changed rule names the field
  and both values: `rule 2: operator eq -> in`. A rule that is unchanged is not
  reported. A changed combination operator is its own field, `rules.op`.
- A message body is reported by length and a short excerpt of each side, never
  in full. The whole body stays in the archive, where the version route returns
  it; it does not travel into a diff answer.

With `against=current` the live state is the second side and `to` is `0`, because
the live state has no number until it is archived.

## What restore does

```
POST /api/practices/{id}/segments/{segmentID}/versions/{version}/restore
POST /api/practices/{id}/broadcasts/{campaignID}/versions/{version}/restore
```

Restore writes the archived state onto the segment or broadcast and archives the
result as a new state. It never deletes anything: the state that was replaced
keeps its number and stays readable, and the restored content gets the next
number. A restore is therefore a change like any other, with its own entry in
the history.

Restore brings back the definition, not the outcome:

- A broadcast that is `running` or `finished` refuses the restore with `409` and
  the code `version_conflict`. Its record is closed: it either is on its way out
  or already went out, and its content must not change afterwards.
- `scheduled_at` and `status` are not restored. An old moment from the past is
  not a plan, and the status is not content. A restored draft stays a draft.
- Recipients are not restored. A contact who unsubscribed, was suppressed or was
  deleted after the state was taken stays out of the audience, and the recipients
  who already received a broadcast are not sent to again.
- The audience is recomputed on the next send from the restored rules, against
  the contacts as they are now.

For a segment, a restore that changes rules, membership mode or the exclusion
drops the cached membership as well, so the next send cannot keep using the
member list computed from the replaced definition.

## Reading the history

```
GET /api/practices/{id}/segments/{segmentID}/versions?limit=50
GET /api/practices/{id}/segments/{segmentID}/versions/{version}
```

The list is newest first. An unknown number answers `404` with the code
`version_not_found`, and the same `404` is answered for an id that belongs to
another workspace: no route confirms that an id exists somewhere else.

The dashboard shows the same history. A segment or a broadcast has a **History**
action that lists the states with their number, moment, author and reason, lets
you pick two states and shows the diff field by field, with **Restore this
version** behind a confirmation.

## Retention

States live exactly as long as the segment or the broadcast they belong to. They
are part of the workspace and are deleted with it. Deleting a segment or a
broadcast removes its states, because a state without its subject would be an
orphan record that no longer explains anything.

There is no separate retention window and no automatic pruning today. If your
workspace needs the history exported before a segment or broadcast is deleted,
export it first with the [audit and DSAR exports](/compliance-evidence).

## Errors

| Code | Status | Meaning |
| --- | --- | --- |
| `version_not_found` | 404 | that number does not exist for this segment or broadcast |
| `version_conflict` | 409 | the broadcast already went out, so a restore is refused |
| `validation` | 400 | `against` is neither a version number nor `current` |

Both codes carry a hint. The `version_not_found` hint points at the version list;
the `version_conflict` hint names the status and what is still possible.
