# Compare recovery checkpoints

An operator comparing two agent snapshots needs to distinguish missing series,
new identities, changed totals, changed retry windows and explicit histogram
layout migrations. `compare-state` validates both core checkpoints and prints a
deterministic JSON difference without publishing or changing either file:

```sh
moon run cli -- compare-state before.json after.json
```

The portable API is `State.diff(other, max_changes=1024, max_paths=4096)` returning
`StateDiff`; `to_json()` returns a detached JSON report. Changes are sorted by
series identity, with `added`, `removed` or `changed`, complete before/after
logical record views and RFC 6901 paths relative to those views. The empty path
means the whole series was added/removed. Histogram paths include bucket indexes.
Int64 totals and timestamps remain decimal strings, including values above 2^53.
Capacity changes are separate from series changes. Unchanged identities are counted.

Views contain numeric kind, the last delta/retry window, cumulative origin and
total, including all distribution fields. Checkpoint version tags, derived indexes
and insertion order are excluded. Comparing a state with its restored checkpoint
produces no changes. Different numeric kinds remain visible. No timestamp-based
guess of lineage is made: a report is not an accumulation proof, merge instruction,
patch, authenticity check or delivery acknowledgement.

Both budgets accept 1..100000 and cause explicit refusal if exceeded; no partial
or truncated report is returned. The CLI retains its 4 MB UTF-8 input/output limits
and exits 2 on invalid input or oversized reports. A successful comparison exits
0 even if differences exist; callers inspect `changes` and the capacity fields.
Library budgets count changed series and paths rather than encoded bytes.

`checks/replay.mbtx` compares the saved first/second intervals from the credited
OTLP fixture, checks the exact total 12, identical snapshots, malformed input and
unchanged source files. Unit tests exercise all four accumulator kinds, capacity,
addition/removal, kind changes, explicit layout migration, reverse comparison,
ownership, exact large integers and recovery after budget errors.
