# Gate a refactor on a parity check

To prove a rewritten engine still behaves like the one it replaces, record a run
off the old machine, re-fold that recording through the new one, and diff the two
final states after normalizing away record-time noise. `@demlik/tea/parity` is
thin packaging over `recorder` for capture, `replayTrace` for update-only replay
from the captured post-init State, `deepEqual` for comparison, and
`normalizeForParity` for normalization. What you get is a GO/NO-GO boolean per run.

## 1. Record the golden run

Attach `recordRun` to the synchronous `run()` handle, before `await runtime.ready`,
so the boot transition is captured as `loaded`. Drive the runtime as production
does, then snapshot with `trace()`:

```ts
import { run } from "@demlik/tea/promise";
import { recordRun } from "@demlik/tea/parity";

const runtime = run(auditMachine, { ctx });
const rec = recordRun(runtime);

await runtime.ready;
for (const page of pages) await runtime.dispatch({ _tag: "scan", page });

const golden = rec.trace();
rec.stop();
await runtime.stop();
```

Non-determinism is a *record-time* property: the ids and timestamps an effect
minted during this run are frozen into `golden.msgs`. Replay never re-mints them,
so the same recording reproduces byte-identically forever.

## 2. Keep the recording as a fixture

A trace is plain data — `loaded`, the ordered `msgs`, `finalState` — so JSON is
the whole fixture format. Write it once from a real production run and the gate no
longer needs the old engine present at all:

```ts
import type { Trace } from "@demlik/tea/parity";

await writeFile("fixtures/audit-golden.json", JSON.stringify(rec.trace()));

// later, in the test:
const golden = JSON.parse(
  await readFile("fixtures/audit-golden.json", "utf8"),
) as Trace<AuditState, AuditMsg>;
```

`rec.toJSONL()` also dumps the run, but that line format is the package's own
record log and nothing public re-hydrates it — reach for it as a diagnostic
artifact, not as the fixture the gate reads back.

## 3. Replay the same recording through both engines

`goldenReplay` starts from the captured post-init State in `loaded` and folds
`msgs` through `update` only. It calls no `init`, handlers, Store operations or
Sub runners. It takes a live `Recording` handle or a bare `Trace`; `ctx` is an
unused optional legacy argument, so omit it:

```ts
import { goldenReplay } from "@demlik/tea/parity";

const oldState = goldenReplay(auditMachine, golden);
const newState = goldenReplay(auditMachineV2, golden);
```

Both sides consume the *same* input sequence, so any difference in the two
states is a difference in the reducers — never in the world.

## 4. Normalize before you compare

Raw states differ on generated ids, timestamps, and intra-effect ordering — noise,
not behavior. `normalizeForParity` returns a pure `(value) => normalized` that
strips those keys, recursively sorts object keys, and stable-key-sorts arrays. The
defaults strip `id`, `runId`, `timestamp` and sort by `ruleId`, `selector`, `url`;
pass a `ParitySchema` when your domain names them differently:

```ts
import { type ParitySchema, normalizeForParity } from "@demlik/tea/parity";

const schema: ParitySchema = {
  stripKeys: ["id", "etag", "observedAt"],
  sortKeys: ["ruleId", "resourceId"],
};
const normalize = normalizeForParity(schema);
```

Build the normalizer once and apply it to both sides — it is a pure function, so
reusing it across runs costs nothing.

## 5. Take the verdict

`parityEqual` is the gate. It reuses `trace-replay`'s order-insensitive
`deepEqual` rather than adding a second comparator, so `true` means *behaviorally
equivalent under the schema*:

```ts
import { parityEqual } from "@demlik/tea/parity";

expect(parityEqual(normalize(oldState), normalize(newState))).toBe(true);
```

The former `diff` alias is gone — its name read inverted (it returned `true`
when the values were EQUAL, so `if (diff(old, new))` read backwards).

## 6. Localize a failure to the step that drifted

A red gate tells you the run diverged, not where. Record with `captureSteps: true`
and the trace carries a `(msg, post-state)` pair per transition, so you can walk
prefixes until parity breaks and name the exact message:

```ts
const rec = recordRun(runtime, { captureSteps: true });

// after a failing gate:
for (const [k, step] of (golden.steps ?? []).entries()) {
  const prefix = {
    loaded: golden.loaded,
    msgs: golden.msgs.slice(0, k + 1),
    finalState: step.state,
  };
  if (!parityEqual(normalize(goldenReplay(machineV2, prefix)), normalize(step.state))) {
    throw new Error(`parity broke at msg ${k}: ${step.msg._tag}`);
  }
}
```

That is the whole gate: one recording, two folds, one normalized boolean — and a
rewrite that cannot silently drop a finding. The capture and re-fold primitives
underneath are documented on their own terms in
[Replay a recorded run in a test](./replay-in-a-test.md), and a machine you intend
to record is usually one you have already made resumable — see
[Make a machine durable and crash-recoverable](./make-durable.md).
