# Applied progress must match recovered state

These four constructed controls illustrate a recovery bookkeeping error.
They are not a Raft implementation, crash-injection test, snapshot protocol or
empirical observation about any storage engine.

## Source

Diego Ongaro's [Chapter 3 erratum](https://github.com/ongardie/dissertation/blob/0c10ea1b53d73bf6d163bdde8e4141a1cbd8f36a/README.md#chapter-3-basic-raft-algorithm)
clarifies that lastApplied must share the state machine's persistence lifetime.
The [Raft paper](https://raft.github.io/raft.pdf), Figure 2, supplies the
ordered-application context. Source accessed 2026-11-20.

## Exact setup and result

A counter starts at zero. Its retained log contains one committed increment.
After recovering the supplied state/index pair, replay starts after that index.

| Recovered counter | Recovered applied index | Replayed entries | Final counter |
| ---: | ---: | --- | ---: |
| 0 | 0 | 1 | 1 |
| 1 | 1 | none | 1 |
| 1 | 0 | 1 | 2 |
| 0 | 1 | none | 0 |

The first row rebuilds lost volatile state; the second retains a consistent
durable pair. The other rows supply inconsistent recovery state: duplicate
application and skipped effects respectively. Neither error requires a second
client request or a second committed log entry.

## Reproduce

Use Node.js, with all files in one directory:

```sh
node model.mjs --check
node --test model.test.mjs
```

[Model](./model.mjs), [tests](./model.test.mjs), [generated results](./results.json).
Results are deterministic integer arithmetic with input/overflow checks; no
statistical uncertainty applies to these selected cases. This is not a measured
failure frequency, exhaustive state space, or general exactly-once guarantee.
The model assumes a known committed log and omits elections, storage flushes,
transaction atomicity, snapshots, retries and external side effects. A real
implementation must establish its own consistent recovery contract.

See [crash-recovery](https://hesela.dev/terms/crash-recovery/),
[Raft](https://hesela.dev/terms/raft/) and the
[Observatory explanation](https://hesela.com/analyses/storage-consistency-ordering/#crash-recovery-state).
