# Rewind, fork and checkpoints

> How zelk takes a session back to before an earlier prompt or branches it, why the log never loses anything by it, and how checkpoints put files back, and what they can't.

A session's log only grows, as [Durability and recovery](/concepts/durability/)
describes, so going back can't mean deleting. zelk gives you three moves,
and each adds to the log:

- **Rewind the conversation** to before an earlier prompt, in the same session.
- **Restore files** to how they stood before that prompt.
- **Fork** the conversation into a new session, and carry on there.

The first two combine, and a fork leaves the original as it was. They are
the user's moves: the model has no tool to rewind or restore and isn't told
that checkpoints exist.

## Points

You can go back to a **point**: a user prompt that started a run from idle,
whether a message or a skill invocation. A message you sent while a run was
going, queued or steering, is part of that run and isn't a point. A point is
named by the sequence number of its prompt in the log, and the list of points
is the conversation as it now stands: after a rewind, the points it dropped
are no longer listed, though the log still holds them.

Each point also says how many files a restore would change. At zero, or with
checkpoints off, only the conversation can go back.

## What a rewind does

A rewind adds two records to the session's log and removes none. The first,
`RewindRequested`, names the point and carries the conversation as it stood
just before that prompt, written out whole, so replaying the log never has
to read the earlier events again. The second, `RewindFinished`, says it is
done. When the fold reaches it, the conversation in the session's state is
replaced by the one in the first record. That covers the history, the goal,
the plan and the task list, the instructions and skills that were loaded, the
answers and denials you gave, and the points. The prompts after the point,
with their model calls and tool results, stop reaching the model. They stay
in the log, which is why the diagram dims them rather than removing them.

The rest of the state belongs to the world, not the conversation, so a
rewind keeps it: the tokens spent, the configuration, the processes and
subagents, the auto-mode breaker. What depends on what the model saw is
reset: the record of which files it has read, so every write needs a fresh
read, and the cached request sizes and reviewer verdicts.

The next model call also gets a note. It says the conversation was rewound to
before your message, that what came after is undone, whether files were
restored, and the working directory. Without it, a model would trust what it
remembers of files it can no longer see change.

A rewind is refused while a run is going, a request for you is open, the
session is suspended, a process is alive or a subagent hasn't finished.

## What a fork shares and copies

A fork is not a rewind in a new place. It is a new session with a log of
its own. It begins with two records, written in one batch, so a fork is
never seen without its conversation: the session's start, with the source's
current configuration, and `SessionForked`, which carries the conversation
up to the point you chose. If the source has a scratch directory, the fork
gets its own empty scratch space and the model is told its new path.

The fork **copies** the conversation, and the source's saved command output
and fetched pages, which the conversation may refer to. It never reads the
source again, so deleting the source later doesn't affect it. It **starts
empty** in everything else: no processes, no subagents, no checkpoints, no
spending, a fresh breaker, and no points until its first prompt. The source's
points are sequence numbers in the source's log and mean nothing in the
fork's. The catalog records which session it came from, and a note tells the
model.

It **shares** the workspace, because the files are on disk and there is only
one set of them. An edit made in the fork is an edit in the source's
directory, and neither session's checkpoints know about the other's changes.
The note says to read a file again before trusting it.

The source isn't changed, and it needn't be idle. Forking a running session
takes what the run has done so far: every finished step, and none of the
model turn or tool calls still under way, so no tool call is left without its
result. A note says how many items were left out.

A conversation is a tree of points, the prompts that started a run from idle, laid out in the order they were sent, and the workspace’s checkpoints sit beneath it, one snapshot for each point. A session keeps files for its last 100 points, and a checkpoint saves no file over 8 MiB. The log only grows: a rewind or a fork adds records.

- **Rewind the conversation**. Rewinding the conversation adds to the log and takes nothing out. The prompt you went back past, and everything it did, drops out of what the model sees and stays in the log, dimmed here. The next prompt grows a new branch from the point before it. The files weren’t asked to move, so they keep every edit.
   1. You send “Add a retry loop”. It starts a run from idle, so it is a point, P1. No file is tracked yet, so the run-start pass saves nothing.
   2. Edit changes a.go to v1. Just before it ran, zelk saved a.go’s old version, v0, with the session.
   3. You send “Rename the helper”. It starts a run from idle, so it is a point, P2. It follows P1. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (a.go): the files as the last run left them.
   4. Edit changes a.go to v2. This run saved a.go already, so nothing is saved again.
   5. You send “Log every attempt”. It starts a run from idle, so it is a point, P3. It follows P2. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (a.go): the files as the last run left them.
   6. Edit changes a.go to v3. This run saved a.go already, so nothing is saved again.
   7. You rewind the conversation to before P3. The log gets a rewind request and its finish, and P3 leaves the conversation but stays in the log. The files stay as they are, and the model is told it must read a file again before trusting it.
   8. You send “Use backoff instead”. It starts a run from idle, so it is a point, P4. It follows P2. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (a.go): the files as the last run left them.
- **Rewind conversation and files**. A restore puts back the files the session tracked, and only those. Each prompt’s snapshot is what its files were before the run: the file as it stood at the run-start pass, or as it was just before the agent first changed it. Restoring to P2 rewrote a.go, deleted the b.go that P2 created, and moved the conversation back with them.
   1. You send “Add a retry loop”. It starts a run from idle, so it is a point, P1. No file is tracked yet, so the run-start pass saves nothing.
   2. Edit changes a.go to v1. Just before it ran, zelk saved a.go’s old version, v0, with the session.
   3. You send “Add a config file”. It starts a run from idle, so it is a point, P2. It follows P1. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (a.go): the files as the last run left them.
   4. Write changes b.go to v1. Just before it ran, zelk saved b.go’s old version, that no file was there, with the session.
   5. Bash runs `sed -i s/old/new/ a.go`. It writes a.go, a file the session tracks. Nothing is saved now; the next run-start pass will notice.
   6. You send “Tidy up”. It starts a run from idle, so it is a point, P3. It follows P2. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (a.go, b.go): the files as the last run left them.
   7. Edit changes a.go to v3. This run saved a.go already, so nothing is saved again.
   8. You rewind the conversation and the files to before P2. The preview lists what it will do: puts a.go back to v1 (now v3); deletes b.go. zelk asks first. The conversation goes back only if every file does. P2 and P3 leave the conversation but stay in the log.
   9. You send “Use YAML instead”. It starts a run from idle, so it is a point, P4. It follows P1. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (a.go, b.go): the files as the last run left them.
- **Fork into a new session**. A fork is a new session with a copy of the conversation, and the same files. The source keeps its log, its points and its checkpoints. The fork starts with none of its own and shares the workspace: an edit in one is an edit in the other, which neither session’s checkpoints can tell the other about.
   1. You send “Add a retry loop”. It starts a run from idle, so it is a point, P1. No file is tracked yet, so the run-start pass saves nothing.
   2. Edit changes a.go to v1. Just before it ran, zelk saved a.go’s old version, v0, with the session.
   3. You send “Add a test”. It starts a run from idle, so it is a point, P2. It follows P1. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (a.go): the files as the last run left them.
   4. Write changes b.go to v1. Just before it ran, zelk saved b.go’s old version, that no file was there, with the session.
   5. You send “Use backoff”. It starts a run from idle, so it is a point, P3. It follows P2. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (b.go): the files as the last run left them.
   6. Edit changes a.go to v2. Just before it ran, zelk saved a.go’s old version, v1, with the session.
   7. You fork before P3. A new log, t, begins with its configuration and the conversation up to P2. s isn’t changed, and P3 stays in it. t has no points, no checkpoints and no spending yet, and it shares the workspace with s.
   8. You send “Use jitter instead”. It starts a run from idle, so it is a point, P4. It follows P2. No file is tracked yet, so the run-start pass saves nothing.
   9. Edit changes a.go to v3. Just before it ran, zelk saved a.go’s old version, v2, with the session.
- **What a restore can’t undo**. A checkpoint is the old version of a file the file tools changed, nothing more. The restore put a.go back. dist.tar, which only Bash wrote, was never tracked and stays. The push reached another system and stays too. Version control, and care with commands that reach beyond the workspace, cover what checkpoints can’t.
   1. You send “Fix the config loader”. It starts a run from idle, so it is a point, P1. No file is tracked yet, so the run-start pass saves nothing.
   2. Edit changes a.go to v1. Just before it ran, zelk saved a.go’s old version, v0, with the session.
   3. You send “Ship it”. It starts a run from idle, so it is a point, P2. It follows P1. The run-start pass re-reads every tracked file and saves the ones that changed since their last version (a.go): the files as the last run left them.
   4. Bash runs `make dist && git push origin main`. It writes dist.tar. No file tool did, so nothing is saved and dist.tar isn’t tracked. git push origin main reaches a system outside the workspace.
   5. You send “Bump the version”. It starts a run from idle, so it is a point, P3. It follows P2. The run-start pass re-reads every tracked file and finds each as its last version.
   6. Edit changes a.go to v2. Just before it ran, zelk saved a.go’s old version, v1, with the session.
   7. You rewind the conversation and the files to before P2. The preview lists what it will do: puts a.go back to v1 (now v2). zelk asks first. The conversation goes back only if every file does. P2 and P3 leave the conversation but stay in the log. It leaves alone dist.tar, which only Bash wrote and git push origin main, outside the workspace.

## What a checkpoint captures

A checkpoint is a set of file versions kept with the session. A **version** is
one path's content at one moment: its digest, size and executable bit, or the
fact that the path wasn't there. Versions are saved at two moments:

1. **Before an edit.** Before the driver starts a trusted tool call that
   declares the path it writes, which is Write and Edit, it saves that path's
   current version, unless this run saved it already. From then on the path is
   **tracked**. The reducer refuses to start such a call until its version is in
   the log, so no edit runs without its old one saved. A file the agent creates
   is saved as absent.
2. **At a point's run start.** The driver re-reads every tracked path and saves
   those that differ from their latest version. This catches what Bash or an
   editor did to a tracked file between runs.

A file that only Bash ever changed is never tracked. Tools from MCP servers
aren't trusted to name what they write, so they track nothing either, and a
subagent is a session with checkpoints of its own, which its parent's restore
doesn't reach.

Some paths are recorded as **skipped**, with a reason: a file over 8 MiB, a
symlink, a hard link or something that isn't a regular file. A skipped path
is never restored, and a restore preview lists it as excluded. Git's own
metadata is never tracked at all.

### Content at a point

A path's content at a point is its latest version saved at or before that
point's run-start pass, if it was tracked by then. Otherwise it is its first
version saved afterwards, which is the file just before the agent first
changed it. That is what each snapshot cell shows. A restore covers every path
the session tracked, including those changed in runs that an earlier rewind
dropped from the conversation, because those files really did change.

### Restoring

zelk observes each tracked path, compares it with its content at the point,
and plans changes only where they differ. The preview lists what it will
create, rewrite and delete, and asks. If the log or the files change while
you decide, the preview is refused as stale and you are shown a fresh one.
Each write is conditional on the file being what the plan saw, and each is
recorded in the log. A restore never touches a path the session didn't track,
so your own edits to other files are left alone.

When files and conversation go back together, the conversation goes back only
if every file does. A crash in the middle is closed by the next execution
before it takes a command: if the restore had finished, the rewind finishes
too, and otherwise it finishes as failed with the conversation unchanged.
Nothing is repeated.

## Where checkpoints live, and for how long

Checkpoints are on by default. The file store keeps a session's versions in a
directory of its own, `history/`, beside the logs, keyed by the session and
content-addressed, with the same protection as the logs. No quota is shared
between sessions:

- **100 points.** A session keeps files for its last 100 points. Past that, the
  oldest are dropped, and the oldest point kept stays restorable. Asking for
  an older one fails with "files from that point are no longer kept".
- **8 MiB a file, no total cap.** Only edited files are saved, so cost follows
  the edits, not the size of the repository.
- **With the session.** Deleting a session, or expiry after `-cleanup-days` of
  inactivity, removes its checkpoints with it. A session that moves to another
  root drops them, because they describe the old root's files.

## What a restore can't undo

A checkpoint is the old version of a file the file tools changed. It isn't a
snapshot of the machine. A restore can't undo:

- **A command's effect outside the workspace.** A push, a deploy, a request, a
  package installed globally, a file in your home directory. The checkpoints
  never saw them, and they stay.
- **Files only Bash changed.** Nothing was saved first, so the file isn't tracked
  and stays as it is. The scene above shows one.
- **Skipped files, and anything the session never tracked.**
- **A subagent's edits.** They belong to its own session.
- **Tokens already spent.** The usage ledger isn't rewound.

Version control covers the rest.

## In the zelk command

In the terminal UI, open **Rewind…** from the palette, or press Esc twice on an
empty prompt while nothing runs. It lists your prompts, each with how many files
can go back too. Pick one and then an action. **Never mind** comes first, so a
stray Enter changes nothing.

| Choice | Does |
|---|---|
| Restore code and conversation | files and conversation as they were before that prompt |
| Restore conversation | the conversation goes back; files stay as they are |
| Restore code | files go back; the conversation stays |
| Fork from here | a new session from before that prompt; this one is kept |

The two choices that touch files show the preview and ask first. The prompt you
went back past returns to the prompt box, to edit and send again.
**Fork session** in the palette copies the whole conversation instead, and
`zelk exec resume --last --fork "task"` does the same for `exec`. A client of
[`zelk acp`](/cli/acp/) can fork too. `-checkpoints=false`, or
`"checkpoints": false` in `config.json`, turns checkpoints off for new
sessions. The [sessions guide](/cli/sessions/) has the commands.

In Go, `Session.RewindPoints`, `PreviewFileRestore`, `Rewind` and
`RestoreFiles`, and `Agent.Fork`, are the same moves ([reference](/reference/go/zelk/)).

## Where to go deeper

- [Sessions, rewind and checkpoints](/cli/sessions/) shows the menu and the flags.
- [The rewind contract](/internals/rewind/) lists every field of the state that a
  rewind takes from the point, keeps or resets, and
  [the checkpoint contract](/internals/checkpoints/) gives the rules for
  [which paths are tracked](/internals/checkpoints/#tracked-paths-and-versions)
  and [how long versions are kept](/internals/checkpoints/#storage-limits-and-retention).
- [Durability and recovery](/concepts/durability/) explains the log that makes
  this safe, and what a crash leaves.
- [The loop](/concepts/loop/) says which commands an idle session accepts and
  which a running one refuses.
- [Context and compaction](/concepts/context/): the log keeps everything a
  compaction drops from the model's view.
- [Subagents](/concepts/subagents/): each child is a session with checkpoints of
  its own.
- [Sandboxing](/concepts/sandboxing/) limits what a command can reach; checkpoints
  can't undo what it does inside that limit.
