Artifact 6be9657d56183939f920b10bc5938fc773a7e026e811146bfc92d4285332522d:
- File docs/dev/durability.md — part of check-in [3d1eba80bc] at 2026-11-25 12:42:13 on branch remote-list — Phone lists only person-driven sessions; remote updates old journals (user: dmestas, size: 16479) [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. For write and edit, the file as it
was (its bytes up to
rewindMaxBytes, or that there was none) commits with "started", read just before that transaction, and its hash after the call with the result; a replay keeps the first attempt's. - A rewind's file restore commits with every file it will touch, each with what it is now and what it goes back to, before it writes any; then its outcomes, and the conversation's fork when it asked for one, in one transaction.
- A hook run is committed before it starts and with its outcome. Where a required PostToolUse
hook matches a call, the call's result and its
tool_resultentry are replaced by the withheld notice, and its whole output (pic_output) is removed, in a transaction of its own before the first hook run starts; the result (or a hook's replacement) is written back only after every required hook has finished cleanly. A user'spic bashoutput is hooked before it is journaled at all. - A verdict (
pic_verdict, the design) is committed as started before the verify command runs and with its outcome after, each in its own transaction, while the request is still admitted; the request is answered in a transaction after that. - A component's answer (PreRequest, below) is committed, as a done hook run, before it takes effect, in a transaction of its own; the component is asked outside any transaction.
- A loop's fire (the design) is one transaction: its
pic_wakerow's fire count, last fire and next due time, and its inbox rowwake:ID:N; the inbox delivers it once. Cancelling a request a loop started pauses the loop in the cancel's transaction. - A compaction and its summary request commit together; one with no request (lapseMode trim) commits its generation, entry and cleared request in one transaction.
The workspace folder
WORKSPACE/.pic/ is made 0700. The journal, -wal, -shm, the lock, holder, migrate and lease
files are 0600: pic_open creates journal.sqlite 0600 before SQLite opens it (SQLite gives -wal
and -shm the mode of the file they belong to) and tightens any of the three that an earlier pic
left wider, in pic_open and pic_open_narrow; the others come from the platform's lock.
journal_private in store.c does both, and does nothing without a filesystem.
default_db in main.c, which runs before every open of the default journal, asks folder.c for two
things. pic_folder_seal writes .pic/.gitignore (*, as Pi does for its install root) when it is
missing. pic_folder_vcs finds the checkout root above the workspace (.zit-checkout, .fslckout
or _FOSSIL_) and, when .zitignore, .fslignore or the versioned ignore-glob do not already
cover the folder, adds the folder's path from the root: PATH/ to the root .zitignore, PATH/*
to .fossil-settings/ignore-glob. Each tool reads only its own project-level ignore file, never
.gitignore. The addition is append-only (fld_add): a new line, or after the last entry of a list
kept on one comma-separated line; no other byte moves. It says so once on stderr. It does not write
when a fossil repository holds its own ignore-glob setting (fossil settings ignore-glob) and no
versioned file exists, because a new versioned file replaces that setting; nor when the file cannot
be written. Those cases give the plain warning with the exact line, on the run that wrote
.pic/.gitignore. These entries only keep the journal out of commits: src/ignore.c is unchanged
and pic's tools still read every ignore file. pic_folder_report is what pic doctor prints: one line
per version control present (a .git above the workspace counts for git) and the fix when
uncovered. All of it is a no-op on a platform with no filesystem.
Tier: bundled (folder.c). Clock: vendor. Record: none; the .gitignore is the record. Default: on.
Asking a component
Core records and enforces; a component decides (the keel note). Before each
reply request, compact_step in loop.c asks the context policy (src/policy.h) once through
pic_hooks_ask: core passes the facts it owns (model, context window, cache lifetime, idle
seconds, whether this is the submission's first request) and gets back nothing, or a reason and a
JSON context such as
{"compact":"trim","arm":"compact","mode":"trim","idle":400,"tokens":120000,"calibration":1.420}.
An answer is a
pic_hook_run row (event PreRequest, ref the turn, command the component's name) and the
first one for a request's turn stands: a retry, or the next pic after a crash, reads it and never
asks again, whatever its own settings. An answer with no compact (a held-out or small lapse) is
kept as the measurement and does nothing. Core then asks the component for the plan of the mode it
named, handing back the answer's context, and runs it; with no component (pic_store.policy NULL)
pic never compacts on its own. Whatever a plan depends on beyond the journal goes in the answer: a
trim measures with the answer's calibration (real tokens per chars/4 token, from the last reply's
reported input), so a recovered plan cuts where the first would have.
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 (the file is never removed; the lock on it is what counts), 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, or across reading input (every open
takes the write lock for a moment, so a held one stalls every pic that starts); 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.
Every open store also says who it is in journal.sqlite.pic-holders/PID.N, a file it holds locked
exclusive while it is open: its pid, its parent's, when it opened the journal, whether it yields,
the schema its build reads, and its role (pic remote, pic attach, pic ask). A file nobody holds is a dead process's,
removed by the next open once it is two seconds old. A pic that needs the lock exclusive and cannot
have it holds journal.sqlite.pic-migrate locked, naming the schema it would migrate to; the
holders that yield (pic remote, pic attach) look each second and, when that schema is newer than
theirs and nothing they started is under way, close the journal (pic remote opens it again once no
migration waits). The migrating pic waits up to 15 seconds while a yielding holder is there, two
otherwise, and then fails naming each holder with the command that ends it. A pic from before
holders said who they are is described instead, with the lsof of the lock that lists it.
pic remote updates a journal an older pic last used itself, with pic_open, only when
pic_journal_unheld says no process has it open; it never signals a holder, and looks again each
second. pic doctor (src/doctor.c) acts on the same records: it lets the yielding holders go, then sends
SIGTERM and after five seconds SIGKILL to the rest, each checked against the platform's
file_users (lsof) so a reused pid is never signalled, plus the pics file_users finds that keep
the lock without a holder file; never itself or its parent, never a process that is not a pic.
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 generation refused for its key, its request held (--hold-key-failures) |
the attempt is failed with its error and credential; the request stays admitted, and whatever runs it next asks the turn again with the key active then |
| 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 |
| a result withheld for a required PostToolUse hook, not given back | stays withheld: the model sees the notice; a firing that had not started runs on the notice |
| a verdict started | unknown; the verify command is never run again, and the request goes on to be answered |
| a verdict done, its request not answered | nothing to redo: the Stop hooks' journaled decision and the verdict stand, and the request is answered |
| a PreRequest answer to compact, no compaction for that turn | the compaction runs, from the answer; the component is not asked again |
| a PreRequest answer being made, nothing committed | the next pic asks again; the answer is drawn afresh (a holdout arm too) |
| an effect with its result | nothing to do; the result is used |
| a loop due, no fire committed | whoever looks next fires it, once, standing for every run that came due |
| a monitor's command started, its process not recorded | the next look adopts the process its pid file names; with none, after 30 seconds, the monitor ends as "did not start" |
| a monitor whose command is still running | reading resumes at the journaled offset: nothing lost, nothing sent twice |
| a monitor whose command is gone | it ends, sending what its spool still holds, once; its command is started again only with --restart |
| the person's OK to a loop or goal, not committed | it still waits for the OK; nothing was sent |
| the person's OK committed | it is approved (or declined) once, with its one message to the conversation |
| a goal's check started, no outcome | interrupted; never run again; the policy is asked with its status unknown, a stall |
| a goal's policy answer journaled, its request not answered | the answer stands: the next owner acts on it as the request is answered, and asks nothing again |
| a loop's fire committed, its inbox row pending | delivered once by the inbox; the next look starts a pic run for it when no pic works the conversation |
| an inbox abort delivered, its request still admitted | the request is cancelled, as its owner would have (the inbox) |
| a restore started, not done | finished by the conversation's next owner, outside any transaction: a file already as the restore leaves it counts as restored, one still as it found it is written, any other is left alone; then the folders its deleted files made, if empty, then its fork, once |
| a restore's bytes stored, no restore journaled | nothing to finish: the bytes are unnamed, and pic prune drops them once an hour has passed |
Deleted conversations
A deleted conversation is a pic_trash row, committed alone; restoring removes it. Its purge (30
days on, or pic sessions empty-trash) is one transaction under the conversation's lease, so a
crash leaves it whole or untouched; it keeps every row pic check counts and empties their text,
and touches no request another conversation's path reaches (the design).
A refreshed sign-in
A subscription's refresh (cred.c, under ~/.pic/credential.lock) is not journaled and cannot be
recovered. When the provider rotates refresh tokens, the old one is spent the moment the token
endpoint answers; if pic then fails to write the new pair (the database busy past its timeout, the
disk full) or dies before it writes, the stored pair is dead and the next refresh is refused: the
user signs in again (pic auth login, or /login). A failed write is tried once more after 200 ms
before pic gives up. The window is one row write after one HTTP reply; nothing else is lost.
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.
wake_fire is reached twice per fire of a loop or a monitor, just before and just after its commit
(test/pic/wake.sh, test/pic/monitor.sh); monitor_started between a monitor's command starting
and its process being recorded.
The restore has four of its own (restore-bytes-stored, restore-planned, restore-wrote,
restore-done; a crash at the first leaves only stored bytes that prune removes), which
test/pic/rewind.sh crashes at, with a write's own points, before it rewinds and compares the
tree; at restore-checked it changes a file with PIC_RUN_AT=point:COMMAND (dev builds run
COMMAND there) to show a file changed after the check is left alone. 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/guard.sh crashes before, during and after a required PostToolUse hook (after-result,
after-withhold, after-hook-run, after-hook-row) and checks that no request the fake provider
logged, the journal, nor the hook's transcript_path rows hold the output.
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.