Skip to content
pi-c Artifact [4789cce47d]
Login

Artifact [4789cce47d]

Artifact 4789cce47d6bbd6b43c0888e9cfc108b10d9905dc008e0e2be3287f3529f856e:


# 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](../manual/rewind.md);
the commit points are in [durability](../dev/durability.md).

## 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 than `rewindMaxBytes`; past it, `pre_kept` is 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_restore` row and one
  `pic_file_change` per 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`) and `done` commit 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
  (fault point `restore-bytes-stored`, between the last of them and the journal; `pic prune` removes
  them).
- **Undo** keeps, at restore time, every replaced file's bytes whatever `rewindMaxBytes` says (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's
  `doubleEscapeAction` setting is honoured with `rewind` added, so a Pi user can take `tree` back.
- **A new seam**: rpc.c is core and may not call `rewind.c`, so `pic_rpc_host` carries the
  components' commands (`pic_rpc_command`), filled by main.c. It is the command registry the stubs
  in `ci/tiers.stubs` already point at; the model, skill and template commands could move onto it.
- **`rewindMaxBytes`** is 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_prune_files`, with `pic prune` and its hourly run at open): a `pic_blob`
  row goes when no `pic_file_change` names it (as a pre-image, or as a restore's target); every change and
  restore row stays, so bytes a rewind, its undo or a recovery could need are never dropped. The
  one exception to "named" is time: a restore stores bytes in one transaction and names them in the
  next, so bytes unnamed only count once they were stored before the previous prune's mark, which
  is at least an hour old (`blob_mark`, `blob_mark_ms` in `pic_meta`; the mark is the highest
  `pic_blob` rowid then). A restore that finds its bytes already stored replaces the row, so it
  has a new rowid, past the mark. Bytes stay while any row names them; dropping the bytes of old
  changes that are still named (a setting, as for images) is not done.
- **Folders a write created.** The write's call, as it commits "started", records each folder
  above the file that does not exist then (`pic_file_dir`, keyed by the change). A rewind that
  deletes the file copies the folders of every write since the message to the restore's file row,
  and once all the restore's files are applied removes them, deepest first, each only if it is an
  empty folder (the executor's `rmdir`). A folder that existed, one holding anything else, and one
  that is no folder any more stay; a crash is finished the same way, since a folder already gone is
  fine. Changes from before this existed have none recorded, so their folders stay. An undo does not
  remove folders.
- **Not tracked**: bash, `!` commands, and anything outside pic, as in Claude Code. A folder a
  command made is not removed.