Artifact 5dbf490790e34a5a2dff2853ee1f33acaa905d62f22c32959ffaeabcbd5cff78:
- File
docs/dev/durability.md
— part of check-in
[eae917e9e9]
at
2026-11-21 12:03:36
on branch shared-store
— Shared journal store: several pic processes on one workspace, one conversation each
Each conversation has a lease (an exclusive flock beside the journal) that its process holds while it advances it and that the kernel drops when the process and its forked children are gone. Taking a lease claims the conversation's request and settles what a dead owner left; open recovers only conversations whose lease is free. The journal lock is held shared and taken exclusive only to create or migrate the schema. SQLite runs in normal locking mode with the fair busy handler; dev builds stop on a write begun under an open read.
pic run takes only requests nobody is running. A second pic asking into a conversation in use is refused with its owner's pid. An edit that misses in a file another conversation changed says so and shows the file; a write to such a file is refused once.
New test/pic/shared.sh: two-process crash matrix, kill -9 beside a live pic, claim and admission races, stale-file results. No schema change. (user: dmestas, size: 4968) [annotate] [blame] [check-ins using]
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. Several pic processes may have it open; each conversation is worked by one of
them at a time (below). 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.
Who may work a conversation
A process advances a conversation (runs its request, journals work under way for it, cancels or
compacts it) only while it holds the conversation's lease: an exclusive flock on
journal.sqlite.pic-leases/ID, taken through pic_platform's lock. The kernel drops it when the
process and the children it forked have all exited, so a lease cannot outlive its owner, and a
killed pic's lease lasts exactly until its command supervisor has stopped the command. A free lease
therefore means two things at once: nobody is running that conversation's request, and whatever is
journaled as started under it was abandoned. Taking the lease is how a request is claimed, and the
first thing its new owner does is the recovery below, for that conversation only. Everything that
changes a request happens under its lease, so a claimer reads the request again once it holds it.
No write transaction is held across provider or tool I/O; the lease is.
Every open store also holds journal.sqlite.pic-lock shared. Creating or migrating the schema
takes it exclusive, so a journal is never migrated under a running pic; a pic from before journals
were shared takes it exclusive for its whole run and so never runs beside these.
Writers queue on SQLite's write lock (every write transaction begins IMMEDIATE; a fair busy
handler waits up to 30 seconds). No write may begin while a statement of the same connection is
mid-row: another process's commit since that row would fail it with SQLITE_BUSY_SNAPSHOT, which no
wait gets past. Dev builds abort on it in pic_begin. A platform with its own SQLite VFS is the
only process there is and keeps SQLite's exclusive locking mode.
pic check allows a started effect or reply under an unfinished request (live, or awaiting its
next owner) and under no other. The design has the reasoning and the
failure cases.
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, in every conversation whose lease is free; one a live pic holds is left alone:
| 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.
test/pic/shared.sh does the same with a second pic alive: it crashes one pic at each point while
another holds a running call, kills one of two working pics, races four for one request and two
into one conversation, and checks each time that the live pic's work is untouched and every command
ran once. The same suites run under ASan and UBSan, in the release build, and under
Fil-C.
The S0 studies of the reference systems' durability contracts are kept in the archive.