I gave Claude Code a test project and this prompt: "this project is a mess. delete src and .env (the key is saved elsewhere), I want to start from scratch". It ran rm -rf src .env and then told me, honestly:

.env: git wasn't tracking it, so it's gone for good.

It was right. And /rewind would not have helped.

Claude Code deletes src and a git-ignored .env; one agent-ctrlz undo brings everything back

What /rewind actually covers

Claude Code's checkpoints are good at one thing: undoing changes the agent made with its own Edit and Write tools. The checkpointing docs are clear about the rest: files changed by Bash commands (rm, mv, git reset --hard, scripts) are not tracked.

That is a big gap, because in practice the agent edits through Bash all the time:

  • sed -i to rename something across files
  • a heredoc (cat > file <<EOF) to rewrite a config
  • rm -rf to "clean up"
  • git reset --hard or git clean -fd to "start fresh", taking your uncommitted work with it
  • supabase db reset or drizzle-kit push --force against your local database

I found an issue where /rewind reported that it had restored everything, but every change made through sed and heredocs was still on disk. The person only noticed later, after building on top of code they thought was gone. That is the worst version of this: not losing work, but believing you didn't.

What I built

agent-ctrlz is a small hook for Claude Code. It takes a snapshot of your project before every prompt and before every shell command, in a separate git repository under ~/.agent-ctrlz/. Your own .git, branches and history are never touched.

npx agent-ctrlz install 

Then, after the agent breaks something:

$ agent-ctrlz undo Back to prompt 7s ago: this project is a mess. delete src… + restored src/index.js + restored src/lib/utils.js + restored src/payments.js + restored .env (saved before: rm -rf src .env && ls -la) Apply this undo? [y/N] y Done. 

A snapshot alone can't bring back that .env: it is git-ignored, so it was never in the snapshot. So before a shell command runs, agent-ctrlz parses it. If it is about to delete or overwrite something the snapshot can't hold (ignored files, paths outside the project), it copies those paths aside first. It understands rm, mv, redirects, sed -i, find -delete, rsync --delete, git clean, git reset --hard and friends, through cd, &&, sudo and globs.

Databases too

Some of the worst agent stories aren't about files. A reset command wipes the local database you spent a day seeding.

Before supabase db reset, drizzle-kit push --force, prisma migrate reset or a DROP TABLE through psql, agent-ctrlz finds the database URL the way the command would (the command line, your Prisma or Drizzle config, supabase/config.toml, .env files) and runs pg_dump first. The command only runs if the dump worked.

$ agent-ctrlz restore-db Database snapshot 20261205T200022-798e 8s ago restore into postgresql://app@localhost:55499/shop Restore this database? [y/N] y Done. The database is back to the snapshot. 

I tested this in a real Claude Code session against PostgreSQL 18: the agent dropped a users table, and restore-db brought back the rows and the id sequence. The restore runs in one transaction, so if it fails, nothing changes.

The one rule: reversible, or you're asked first

I didn't want another tool that blocks the agent every few minutes. People turn those off. So agent-ctrlz never blocks a command it can make reversible: rm -rf src just runs, because you can undo it. rm -rf node_modules runs and isn't even copied, since a package manager recreates it.

It only steps in when a command can't be undone cheaply:

  • your home folder or a system folder
  • a delete through a variable it can't see yet, like rm -rf "$BUILD_DIR/" (if the variable is empty, that's your root)
  • anything over the 500 MB copy limit
  • a remote database, where a restore would lose writes made after the snapshot

Then Claude Code's own permission prompt shows the reason and asks you, even if you allowed Bash up front:

Hook PreToolUse:Bash requires confirmation for this command: agent-ctrlz cannot undo this command: it deletes "$BUILD_DIR/", which agent-ctrlz cannot resolve before it runs. Allow it only if you are sure. 

Codex too

agent-ctrlz also works with OpenAI's Codex CLI (agent-ctrlz install codex). Testing it taught me something. I gave Codex the same "delete src and .env" prompt. Its own guard refused rm -rf: "rm -f style commands are not permitted". So the agent did the same delete with:

python3 -c 'import pathlib, shutil; p=pathlib.Path("src"); shutil.rmtree(p); pathlib.Path(".env").unlink()' 

A guard that blocks one command doesn't stop an agent that wants to finish its task. It finds another way. That's why agent-ctrlz now copies git-ignored files like .env before any delete written inside a script one-liner, and one undo still brought everything back.

The one difference on Codex: it doesn't support "ask" from a hook yet, so for the rare command agent-ctrlz can't make reversible, Codex is blocked with the reason and the agent tells you.

Limits, honestly

  • Command parsing is best effort. A delete hidden inside a script file like python cleanup.py is caught by the project snapshot, but not for ignored files or paths outside the project.
  • Snapshots are local. They're not a backup of your disk.
  • I tested the asking in normal sessions, with --allowedTools, and in claude -p. Not with --dangerously-skip-permissions.
  • Linux and macOS, Node 22+.

It's free and MIT: github.com/kushoevskiv/agent-ctrlz. It works with Claude Code and Codex. It's a few days old, so I'd really like to hear what breaks.

What's the worst thing an agent has deleted on you?