# Durability

What pic promises about a crash, how the journal keeps the promise, and how the tests prove it.

## The promise

A crash, a kill or a lost connection never loses a finished step and never runs a command twice. A
command that may have run when pic died is reported as such to the model, and is not run again.

## The journal

Each workspace has one journal, `WORKSPACE/.pic/journal.sqlite`, a SQLite database in WAL mode with
`synchronous=FULL` and an exclusive lock, so one process owns it at a time. pic commits to it before
and after every step:

- **A request** is admitted (committed) before anything is sent.
- **A reply request** (a generation) is committed as started before the provider is asked, and its
  reply is committed when it arrives.
- **A tool call** (an effect) is committed as an intent, then as started immediately before it
  touches anything outside the journal, then with its result.
- **A hook run** is committed before it starts and with its outcome.
- **A compaction** and its summary request commit together.

## What a crash leaves, and what the next pic does

The next pic that opens the journal (`pic run`, or the next request) recovers before it does
anything else:

| Left in the journal | Recovery |
|---|---|
| a generation started, no reply | failed; the turn is asked again |
| a replay-safe effect started (read, write, edit) | run again: write's bytes come from its input, and edit journals the new content as its plan before it starts, so a replay writes the same bytes |
| a replay-unsafe effect started (bash) | marked unknown, with the result "outcome unknown (interrupted before its result was recorded); not re-run" for the model |
| a hook run started | interrupted; never run again, its output not used; a tool call whose PreToolUse hook was cut off does not run |
| an effect with its result | nothing to do; the result is used |

## How it is tested

Dev builds (`make pic-dev`, `PIC_FAULTS`) have named fault points at every commit and effect
boundary. `PIC_CRASH_AT=point:n` exits abruptly (status 77) on the nth arrival at that point.
`test/pic/crash.sh` crashes at each point, reopens, finishes the run, and checks that the journal
and the outside world agree: a ledger file outside the workspace counts how often a replay-unsafe
command really ran. The release build has no fault points; its run of the suite takes `--no-faults`.
The same suites run under ASan and UBSan, in the release build, and under [Fil-C](filc.md).

The S0 studies of the reference systems' durability contracts are kept in [the
archive](../archive/2026-11-s0/durability-contracts.md).
