# Replay as policy testing

`evaluate(event, state)` is pure and `state` is a fold of `events.jsonl` ([events and state](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/events-and-state.html) §4), so replaying history tests a policy on real workloads with no mocks and no running agent. Research design: the command below is not implemented.

```
openprogram proactive replay --policy TestGapWatcher --sessions <ids|all>
```

- **Engineering**: Every new policy is replayed before activation, exposing poor precision offline (the failure associated with Clippy). The tool comes before the decision engine.
- **Paper**: The would-have-fired report supplies samples for [precision annotation](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/_research_archive/evaluation.html).

## Modes

A new policy's L2 inference never ran on history, so its `state.inferred` event is absent.

| Mode | State allowed | Determinism | For |
| --- | --- | --- | --- |
| strict | L0, L1, persisted `state.inferred` | Bit-for-bit | L0/L1 regression, paper numbers. DangerousCommandGuard; TestGapWatcher's L1 part |
| augmented | Strict plus newly computed L2 | Reproducible through the cache | First run of an L2 policy. UnvalidatedCompletionNudge; TestGapWatcher's completion signal |

## Augmented reproducibility

- Fix `model`, version and `temperature=0`.
- Persist new L2 results as a cache and publish the `model fingerprint` with the report. Reproduction means reproducing the annotation set, without calling a model that may have changed.
- A second replay of one policy on one session reads the cache and is deterministic.
- Prepare policies stop at would-have-prepared: the reviewer sub-agent cannot rerun deterministically. Label TestGapWatcher's report “Notify precision: online-only” and measure it by A/B.

## Three things replay must get right

| Detail | Without it | Rule |
| --- | --- | --- |
| Simulated cooldown | A new policy has no action history, so every match reports | Feed would-fire decisions back as virtual cooldown events |
| Clock injection | An old session's cooldown computed against today is wrong | “Now” is the event's `ts`. Policies must not call `time.time()`; the framework injects the clock into `evaluate` |
| Branch-aware folding | Rewound branches read as continuous history | Rebuild the DAG path from current node to root by `node_id`, then fold (§5 of [events and state](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/events-and-state.html)). Interruption budgets are global across branches |

## Recall

A would-have-fired report lists only fires, so precision comes from replay and recall cannot. Recall needs a separately annotated should-have-fired set. The report claims none.

## Report

```
replay-report {
  policy: "TestGapWatcher"
  mode: "augmented"                      # strict | augmented
  model_fingerprint: "claude-…"          # augmented only
  sessions_scanned: 214
  events_scanned: 51_320
  fired: 38
  per_session: [
    { session_id, node_path: [...],      # DAG path used for folding
      fires: [
        { event_ref, state_snapshot_ref, # triggering context after redaction
          action: "Prepare→Notify",
          cooldown_suppressed: false,
          notes: "Notify precision: online-only" }
      ] }
  ]
  invariant_checks: { loop_free: pass, breaker_exempt: pass, ... }
}
```

`event_ref` and `state_snapshot_ref` let annotators judge each fire; redaction is in [threat model](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/_research_archive/threat-model.html). The report also asserts the four [invariants](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/invariants.html) (§5).

## Implementation status

`openprogram proactive replay` is proposed and not built; the CLI has no `proactive` subcommand.
