# 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.

## 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; 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.
- **Retention**: `pic_blob` is not pruned yet; `pic prune` could 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.
