# Checkpoints

Checkpoints provide the file half of
[rewind](/internals/rewind/): what **Restore code** puts back.

## Why

Checkpoints follow Claude Code's checkpointing. The purpose is to undo what
the agent did, quickly; version control covers the rest.

- Save a file's old version just before zelk's file tools first change it
  in a run. Cost follows the edits, not the repository.
- A restore changes only paths the session tracked. The user's edits to
  other files, and other sessions' edits to them, are left alone.
- Each session keeps its versions in its own directory. They go when it is
  deleted or expires: no shared quota, no eviction, no collector.
- On by default.

## Tracked paths and versions

A **version** is a path's content at one moment: its digest, size and
executable bit, or that the path was absent. Windows has no executable bit, so
there every version records it unset, and a restore brings a file back
with its content and never executable. A version that can't be saved
is recorded as **skipped** with a reason: larger than the limit (8 MiB, or
the environment's file bound if smaller), a symlink (at the path or any
directory above it), a hard link, not a regular file, unreadable, zelk's
own cache, or not a workspace path. A skipped path
is never restored; a restore preview lists it as excluded. Files in the
session's scratch directory are none of these: the file tools record no change
of them, so they are never tracked (docs/design/tools.md, *Staleness*).

A path becomes **tracked** when a version of it is first saved. Versions are
saved at two moments:

1. **Before an edit.** Before the driver starts a trusted tool call that
   declares the path it writes (`tool.WithWritesPath`: Write and Edit, on
   `file_path`), it saves that path's current version, unless this run has
   already saved it. The path is resolved exactly as the tool resolves it
   (`workspace.Paths.Resolve` with the call's working directory).
2. **At a point's run start.** When a run that a point started begins, the
   driver re-reads every tracked path and saves those whose content differs
   from their latest saved version. This catches Bash and outside edits to
   tracked files. A file that only Bash ever changed is never tracked: that
   gap is documented, as Claude Code documents it.

A path no record can hold (Git metadata, or a name that isn't a portable
workspace path, such as one with a backslash) is never tracked; its call runs
as it would without checkpoints.

Tools from MCP servers or clients are never trusted to name their writes.
Subagents are their own sessions with their own checkpoints; a parent's
restore doesn't reach their edits.

## Events

The checkpoint events are:

- `FileVersionsSaved{Run, Start, More, Calls, Files}`: versions saved in run
  `Run`. `Start` marks a run-start pass. A pass too large for one batch is
  recorded in consecutive parts: `More` marks every part but the last, the
  pass is the last part's, and nothing else is saved between them. A crash
  between parts leaves the pass owed; the next attempt records only what
  still differs. `Calls` are the tool call IDs the versions were saved for.
  The content is already in the session's file history when this is
  appended.
- `FileVersionsDropped{Before, All}`: versions no longer kept. `All` drops
  every version (expiry); otherwise records before sequence number `Before`
  are compacted to the latest version of each path at or before it.

`Config.Checkpoints` (recorded, default on when the store and environment
support it) turns saving on. It is the session's own: resuming it with
checkpoints off for new sessions changes nothing. While it is on, the reducer
refuses to start (`EffectStarted`) a trusted write-declaring tool call whose ID no
`FileVersionsSaved` of the current run lists. So no edit runs without its
old version. Resolution of the path stays in the driver; the reducer checks
only call IDs, so replay never depends on path rules.

These are plain records, not effects. Saving a version changes nothing but
the session's own history directory, so a crash before the record leaves
only an unreferenced file, removed by the next prune, and the call hasn't
started. Recovery simply saves again.

## Content at a point

For a point P whose run had a run-start pass S, a path's content at P is:

- if the path was tracked before S: its latest version saved at or before
  S. The pass guarantees this equals the file as it was at S;
- otherwise: its first version saved after S. That is the file just before
  the agent first changed it. A Bash change between S and that save is the
  documented gap.

A point whose run had no pass (checkpoints were off then) has no code restore.

A restore to P covers every path tracked at or after S, in log order. That
includes paths changed in runs a later rewind dropped from the conversation,
since those files really changed. Preview reads each path's current content
(`env.FileObserver`) and plans a change only where it differs from the
target. `RewindPoint.Files` is that count. Zero hides the code options.

## Restore

The plan becomes the existing `FileRestorePlan`: `Before` is the current
version, `After` the target, nil for absent. Excluded lists skipped paths.
Confirmation, `FileRestoreRequested`/`Applied`/`Finished`, the
conditional per-file write through `env.FileRestorer.RestoreFile`, the
recovery rules and `Session.Rewind`'s use of it are unchanged.
`FileRestoreRequested.Boundary` is `point:<seq>`.

Every target version is read from the history, and its digest checked,
before intent. A missing one refuses with "files from that point are no
longer kept".

## Storage, limits and retention

`store.FileHistory` stores versions content-addressed per session. The file
store keeps them under `<dir>/history/<hex session id>/`, beside the logs and
with the same protection.

- **100 points.** Past 100 points with a pass, the driver appends
  `FileVersionsDropped{Before}` for the oldest kept pass, then prunes files no
  remaining version references. Content at the oldest kept point stays
  restorable.
- **8 MiB per file.** No total cap; only edited files are saved.
- **Expiry and deletion.** Checkpoints go with their session: a deleted
  session, or one idle past the host's cutoff, is removed whole
  ([retention](/internals/retention/)), as Claude Code removes a session's file
  history with its transcript.
- **Fork.** A fork's points start empty, so it needs none of the source's
  history.
- **Move.** A session that moves to another root drops its checkpoints as
  the move commits: they describe the old root's files, and restoring them
  in the new one would change files the session never touched there. The
  current run owes a new pass, which tracks the new root. The driver prunes
  the old content right after the commit; a crash first leaves it for
  expiry, which drops a log's history even when it names no versions.

The agent has no tool to rewind or restore and isn't told about checkpoints:
both stay with the user, as in Claude Code.

## Tests that gate it

- Each recording boundary crashed before and after it lands.
- The first edit in a run saves once; a created file saves as absent.
- Edit then Bash on the same file restores; Bash alone doesn't.
- Two points back, the earliest version wins.
- A rewind's dropped runs still restore to an earlier point.
- Pruning at 101 points keeps the oldest kept point restorable.
- Expiry and deletion drop history; a held session is skipped.
- More than 2,048 files in the workspace makes no difference.
