Artifact c74782d108f068c309c182b5f582d25fcc547c2789f40488afa25c49133d4a68:
- File docs/design/rewind.md — part of check-in [40edf1e438] at 2026-11-22 13:49:21 on branch rewind — rewind: check a fork's restore against the fork's own history; keep one file in memory at a time (user: dmestas, size: 5605) [annotate] [blame] [check-ins using] [more...]
Rewind: file history and restores
Status: built 2026-11-22 (ticket 29). Tier: core (src/filelog.c: the record and the restore) and
bundled (src/rewind.c: what to restore and when to refuse). Clock: durability. Record:
pic_file_change per write or edit and per restored file, pic_blob for kept bytes,
pic_restore per restore. Default: on, rewindMaxBytes 1 MiB.
What it is
Claude Code's checkpoints, over pic's journal: go back to before a user message and restore the conversation, the files, or both. The user-facing description is rewind; the commit points are in durability.
Core records and enforces
- Each write or edit call commits the file's prior state with its "started": exists or not, mode,
SHA3-256, and the bytes (content-addressed in
pic_blob, deflated by the sqlar rule) when no larger thanrewindMaxBytes; past it,pre_keptis 0 and the hash is still kept. The file is read before the transaction begins. A replay finds the row and keeps it: the first attempt saw the file before any write. The file's state after the call (hash, mode) commits with the result. - A restore is journaled whole before any file is touched: one
pic_restorerow and onepic_file_changeper file, holding the file as found (so an undo can put it back) and the state to restore. Applying it writes only a file still as found; a file already in the target state counts as restored, anything else is left (changed). Outcomes, the conversation fork it asked for (fork_at) anddonecommit in one transaction, so a crash leaves both or neither. A started restore is finished by whoever next takes the conversation's lease outside a transaction; open's recovery does, so the next pic of any kind finishes it.
What is left to chance
- Before a write or edit. The file is read for its pre-image in the moment before "started" commits, and the tool writes after that commit: for write at once, for edit after nothing more than the commit (edit computes its new text before). A change another process makes inside that window (one fsync long) is in neither the pre-image nor the tool's view, and a rewind puts back the state from before it. Closing it would need a compare-and-replace the filesystem does not have; re-reading just before the rename only narrows it.
- During a restore. The component checks each file, core reads it again as it journals the
restore and leaves it (
changed) unless it is what the component saw, and applies only over that journaled state, reading the file once more after the bytes and directories are ready. What remains is the write of the temporary file before the rename: a change landing there is overwritten. - Replacing a file renames a new one over it, as the write tool does: hard links to the old
file keep the old bytes. Paths are kept resolved (
pic_tool_path_key), so a symlink in the workspace still points at the restored file. - The chain across a rewind. A conversation's own restore was checked against the same chain, so the check starts again after it. The restore that made a fork found the file in the conversation it came from, whose changes are not on the fork's path; it must have put back what the fork's path last left there (the earliest state after the fork point is the last before it), so its after-state is compared with the previous change's, and a hand edit stays refused.
- Memory. A restore reads one file at a time, storing each one's bytes in a transaction of its own before the restore is journaled; a crash can leave such bytes unreferenced, never missing.
- Undo keeps, at restore time, every replaced file's bytes whatever
rewindMaxBytessays (up to 256 MiB; a larger file is refused), since the user's file is at stake and restores are rare.
The component decides
rewind.c picks the files (every write and edit on the conversation's path after the message),
the target of each (the earliest such call's pre-image), the refusals (the file is not what the
conversation last left there, counting its own restores; one of those changes found the file other
than the change before left it, so something outside pic wrote in between; its earlier bytes were
not kept; its call never finished), and whether to go on past them (--force-partial, or a yes in the terminal). The
conversation half is pic_fork at the message's parent, as /fork does. Undo restores the
pre-images of the latest restore's restored files.
Decisions
- Esc Esc opens
/rewind, Claude Code's binding, where Pi opens/tree. Pi'sdoubleEscapeActionsetting is honoured withrewindadded, so a Pi user can taketreeback. - A new seam: rpc.c is core and may not call
rewind.c, sopic_rpc_hostcarries the components' commands (pic_rpc_command), filled by main.c. It is the command registry the stubs inci/tiers.stubsalready point at; the model, skill and template commands could move onto it. rewindMaxBytesis read once per process; the terminal runs each request as its own process, so a change applies from the next request.- Hashing reads a file into memory only to keep it (at most
rewindMaxBytes); others are hashed as a stream, on the host executor. - Retention:
pic_blobis not pruned yet;pic prunecould drop bytes no conversation can rewind to (older than a setting), as it does for images. - Not tracked: bash,
!commands, and anything outside pic, as in Claude Code. Directories a write created are not removed.