Limiting what the agent can break
Set up safeguards that bound the damage an agent can do: sandboxes or containers, no production credentials, separate branches or worktrees, and approval gates for destructive commands such as `rm -rf`, `git push --force`, `git reset --hard` and database migrations. Match the agent's permissions to how much you trust the task.
Builds on: A coding agent is a model in a loop, Untrusted text can steer the agent
What could it actually do right now?
You start a terminal agent in your main checkout. Edits and commands are auto-approved. You ask it to fix a failing migration test. Your shell exports `AWS_PROFILE=prod-admin` and a `DATABASE_URL` that points at production, and you have two hours of uncommitted work. Halfway through, the agent decides the database looks "out of sync" and wants to reset things. What is it able to do?
It can do anything you can do
Every command the agent runs, runs as you. It has your files, environment variables, SSH keys, git rights and cloud logins. Its intent does not limit it. Only its access does. A wandering loop or an injected instruction can produce `git reset --hard`, which throws away your uncommitted edits to tracked files. It can run a migration against production, dropping columns and losing rows. It can run `git push --force`, overwriting teammates' commits. You can't stop the model from erring, so limit what an error can reach.
Four layers, outermost first
1. **Isolate**: run the agent in a container or VM that mounts only the project, not `~/.ssh` or `~/.aws`.
2. **No production credentials**: give it a test database and scoped, short-lived tokens.
3. **Own branch or worktree**: its resets and commits land on its own copy, not on your uncommitted work or `main`. Only layer 1 stops it from reaching your checkout, and only layer 2 stops it from pushing to the remote.
4. **Approval gates**: require your yes for `rm -rf`, `git push --force`, `git reset --hard`, migrations and deploys.
Gates match command text, and `python -c` can do the same damage. Gates back up isolation; they never replace it.
Your shell is the agent's shell
Before launching, you give the agent its own worktree and branch, so a `git reset --hard` there can't touch your uncommitted work. Then you check what it would inherit, and swap production for test. Note the limits. Nothing stops the agent from running `cd ..` into your main checkout. And `unset AWS_PROFILE` only hides a variable: `~/.aws/credentials` can still hold a default profile, which AWS tools fall back to in any process you start. Only a container that mounts the project but not your home directory truly removes them.
// Shell
$ git worktree add ../wt-mig -b agent/mig
Preparing worktree (new branch 'agent/mig')
HEAD is now at 3f9c2e1 Add orders migration
$ cd ../wt-mig
$ env | grep -E 'DATABASE|AWS'
DATABASE_URL=postgres://prod-db/app
AWS_PROFILE=prod-admin
$ unset AWS_PROFILE
$ export DATABASE_URL=$TEST_DB_URL
$ agent "fix the failing migration test"
Match permissions to trust
**Exploring an unfamiliar repo or an outsider's issue**: untrusted text may steer the agent. Use read-only access in a container, no credentials, and auto-run nothing.
**A routine fix in your own repo**: auto-approve edits and tests inside its worktree, and ask for everything else.
**Migrations, force pushes, deploys**: the agent drafts them. You read them and run them yourself, against a target you chose.
Bound the worst command in advance
Assume the agent will eventually run the worst command its access allows, and decide beforehand what that command can reach. Isolate it, keep production credentials out, give it its own branch or worktree, and gate destructive commands. Loosen these only as far as the task deserves.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.