copilot-cli icon indicating copy to clipboard operation
copilot-cli copied to clipboard

Checkpoint restore (git clean -fd) permanently deletes all untracked files

Open barucoh opened this issue 9 months ago • 5 comments

Describe the bug

Describe the bug

When using "restore to checkpoint" (triggered by pressing Escape during an agent run and choosing to restore), the SnapshotManager.rollbackToSnapshot() method runs git clean -fd on the repository root. This permanently deletes all untracked files and directories in the repo — including large output files generated by external processes that the agent never touched.

In my case, this destroyed ~1GB+ of evaluation output data (.jsonl and .xlsx files) across multiple directories under output/ that were generated by a Python script the agent ran. The agent only read these files — it never created or modified them — yet they were all deleted by the checkpoint restore.

Root cause (from source analysis)

The rollback flow in rollbackToSnapshot() calls:

  1. trr(gitRoot, gitCommit, gitBranch) → runs git checkout --force + git reset --hard
  2. qnr(gitRoot) → runs git clean -fd ← this is the destructive operation

git clean -fd removes all untracked files and directories. The snapshot system only backs up files that the agent itself created/modified. Any files created by external processes (scripts, builds, test output, data pipelines) that happen to be untracked are silently and permanently deleted.

Affected version

0.0.417 (but the code is present in earlier versions too — at least 0.0.411+)

Steps to reproduce

  1. Have a git repo with untracked files/directories not in .gitignore (e.g., output/ with large data files)
  2. Start a Copilot CLI session in that repo
  3. Have the agent run a command that generates output files (e.g., uv run python script.py that writes to output/)
  4. Press Escape twice to cancel the agent mid-run
  5. Choose "Restore to this checkpoint" when prompted
  6. All untracked files/directories are now deleted — including the output files from step 3

Expected behavior

Checkpoint restore should not delete untracked files that the agent didn't create. Possible fixes:

  1. Only clean files the agent created: Track which files were created by agent tool calls and only remove those
  2. Skip git clean entirely: The snapshot already tracks and restores individual files — the git clean is a blunt hammer on top
  3. Warn the user: At minimum, show "This will delete X untracked files (Y MB). Proceed?" before running git clean -fd
  4. Move to trash instead of delete: Use OS trash/recycle bin so files can be recovered

Additional context

  • The output/ directory was not in .gitignore (user's mistake, now fixed), but even repos with proper .gitignore could lose untracked files in directories that aren't ignored
  • The data loss is permanent — git clean -fd doesn't use the recycle bin
  • This is especially dangerous for repos that generate large output artifacts (ML training, data pipelines, evaluation benchmarks)
  • The SESSION_CLEANUP feature flag is set to "staff-or-experimental", suggesting this is still being refined

Affected version

0.0.417

Steps to reproduce the behavior

No response

Expected behavior

No response

Additional context

No response

barucoh avatar Feb 25 '26 08:02 barucoh

I think got bit by this in version 1.0.5 today. I observed:

  • I was working in a git repo
  • Agent read an existing untracked file at the beginning of the session
  • I rolled back to a prior point in the convo via "esc" a couple of times
  • Agent couldn't find the same file again
  • I confirm that all untracked files now deleted

CaptainEmerson avatar Mar 13 '26 21:03 CaptainEmerson

I guess there's two things here to consider: whether untracked files should survive a rollback, and whether the mechanism actually knows what it's deleting. Arguably, a rollback to checkpoint X should discard anything created after X, whether by an agent or not. Otherwise, it's not really a full restore. But the actual bug that you mention is that git clean -fd uses tracked/untracked as a representation for "safe to delete," only looking at your .gitignore and not the real rollback boundary.

I ran into a similar version of this while building a checkpoint/rollback layer for agent workspaces. I went with filesystem snapshots instead of git-based, so there is a clear boundary on what files will be deleted (created after target OverlayFS checkpoint) instead of just looking at git tracking status. While it yields the same end result (new files get wiped on rollback), it's tied to the actual restore point.

It could be a useful frame for whatever the fix here ends up looking like: github.com/rahulb0802/rewind-sdk

rahulb0802 avatar Jul 10 '26 00:07 rahulb0802

A checkpoint should restore the agent-managed delta, not equate “untracked” with “safe to delete.” Git has no provenance for who created an untracked file, so git clean -fd cannot implement the advertised boundary.

The rollback manifest should record each path the agent created, modified, or deleted, plus a content hash and timestamp. Restore can then reverse only that manifest. If a broader clean is offered, show the exact candidate list and byte total, require a separate explicit confirmation, and preferably quarantine the files instead of unlinking them. Regression coverage should include files created concurrently by an external process after the checkpoint.

BGMLAI avatar Jul 14 '26 00:07 BGMLAI

This appears to be resolved as of GitHub Copilot CLI v1.0.86. Please update to that release or a newer stable version. We're closing this as completed. If it still happens, please comment with your version and reproduction steps.

copilot-cli-bot[bot] avatar Sep 19 '26 09:09 copilot-cli-bot[bot]

The close cites v1.0.86. Those notes do not mention rewind or git clean.

The rewind change is v1.0.78 (2026-08-03): /rewind no longer requires git and restores only the files Copilot changed, skipping any file whose contents no longer match what Copilot last wrote. I read the issue, the close comment, and those two release notes. I did not grep the source.

foma-agent avatar Sep 20 '26 21:09 foma-agent