How I built undo for my desktop AI companion: a backup before every destructive write
Description and use
Automatic backup before every destructive operation in the desktop AI companion Ankita enables undo without extra setup. Backups are stored in a hidden folder, limited by count and size, and restored via a dedicated restore_file command.
Problem
An AI companion that edits files can delete the wrong file; approval prompts alone are insufficient to prevent accidental data loss.
Outcome
Undo functionality is integrated as a precondition for all destructive tools, providing automatic backups and a restore mechanism without additional user configuration.
Workflow
- 1. Tool calls backupPath() before performing delete/write/edit/move/patch.
- 2. backupPath() creates a hashed directory for the file path and copies the original file if size ≤5 MB.
- 3. The destructive operation proceeds after successful backup.
- 4. User receives a message indicating a backup was kept and how to restore.
- 5. User can run restore_file to revert to the most recent backup or list older backups.
- 6. An audit log records each mutation with scrubbed secrets.
Evidence
Approval prompts were my first answer, and they are not enough. A prompt says "delete this file?" and a tired human clicks yes. The mistake is already made by the time anyone realises it.
Before delete_file, write_file, edit_file, edit_lines, move_file, or apply_patch touches anything, it calls backupPath(). Only after the copy succeeds does the mutation run.
Backups live inside the workspace, in .ankita_backups, so undo works with no config and never writes outside the workspace boundary.
Per path, the store keeps at most 20 backups and prunes the oldest; files over 5 MB are not copied at all. Symlinks are skipped too.
Restore is blunt. It deletes whatever is currently at the path and copies the backup over it. There is no check for whether you recreated or edited the file after the destructive operation — a restore will clobber that newer work without asking.
- Record date: 2026-10-27T11:11:12.130Z