Your agent needs a rollback command — here's how to build undo that actually works
Your agent just spent 20 minutes refactoring your entire authentication system. It's confident about the changes. You run the tests. Everything breaks.
Now what? You could dig through git history, figure out what it changed, manually revert pieces. Or you could have built rollback discipline from day one.
Here's the pattern that saves you from agent disasters:
Every agent session needs a rollback plan before it starts executing.
Not after it breaks something. Before it touches anything.
The simplest version is a git checkpoint pattern:
Before any code changes: 1. Create checkpoint: git stash push -m "pre-agent-$(date +%s)" 2. Create working branch: git checkout -b agent-session-$(date +%s) 3. Document the session goal in AGENT_SESSION.md 4. Give agent the rollback command: "Type ROLLBACK to undo everything"
But the real magic happens when you build rollback into the agent's instructions:
You have a rollback command available at all times. If I say "ROLLBACK" or if you detect that your changes broke something: 1. git checkout main 2. git branch -D [current-session-branch] 3. git stash pop (to restore pre-session state) 4. Report what you were attempting and why you rolled back Before making ANY code changes: - Create a session checkpoint - Document your plan in AGENT_SESSION.md - Test the rollback command to make sure it works
This isn't just about git. The pattern works for any stateful changes:
- Database changes: Transaction IDs and rollback scripts
- Config changes: Backup files with timestamps
- File system changes: Rsync snapshots or ZFS snapshots
- API changes: Rollback curl commands that undo the changes
The key insight: rollback needs to be faster than debugging. If it takes longer to rollback than to figure out what went wrong, you won't use it when you're frustrated.
I learned this the hard way when an agent "optimized" my database schema and dropped three columns of production data. The rollback took 30 seconds. The data recovery took three hours.
Warning: Don't make rollback so easy that your agent uses it instead of being careful. It's an escape hatch, not a development strategy.
Advanced pattern: Give your agent a "checkpoint and continue" command. It creates a rollback point mid-session, so if the next change breaks something, it only loses the last few steps instead of the entire session.
The agents that ship reliable code aren't the ones that never make mistakes. They're the ones that can undo mistakes faster than you can get frustrated.
Speaking of agents that ship reliable code — if you want an agent that can run persistent coding sessions with built-in safety rails and completion tracking, you need session discipline that goes way beyond rollback commands.