Sub topic 12
Let the assistant run commands and tests safely
Earlier chapters treated the assistant as an adviser. You pasted in an error, it proposed a change, and you applied the change yourself. Many tools can also act directly. They read files, run commands, edit code, and run your tests in a loop. This chapter covers how to allow that without losing work, leaking data, or accepting a "fix" that only makes the tests pass.
This chapter assumes you can already reproduce a failure, write a regression test, and use basic git (branch, diff, commit, restore). It builds on the verification habits from the earlier chapters.
What changes when the assistant can act
In chat mode, you are the only one who touches the machine. In agentic mode, the assistant chooses commands and runs them, and you decide which ones to allow. The debugging loop stays the same: reproduce, diagnose, make a small change, verify. The difference is that each step now has side effects.
A wrong suggestion in chat costs you a few minutes. A wrong command in agentic mode can delete files, overwrite uncommitted work, install packages, or call a real service. Your job moves from typing the fix to setting boundaries and reviewing results.
The loop below shows where your checkpoints sit.
Prepare the workspace first
Set up the environment before the assistant starts, because that is where most of the protection comes from. Permission prompts help, but you will get tired of them. A safe workspace still protects you when you stop reading each prompt closely.
- Start from a clean git state. Commit or stash your own work. Then every change the assistant makes shows up in
git diff, and you can discard it. - Work on a throwaway branch. A branch costs nothing, and a bad session ends with
git switchand a branch delete. - Use an isolated environment. Use a virtual environment or a container so installs do not change your system.
- Remove real credentials from the shell. If your terminal has production keys in environment variables, the assistant's commands inherit them. Use test or sandbox credentials only.
- Confirm the failing test exists. The assistant needs an objective signal. A failing test, or a script that reproduces the error, gives it one.
A typical setup for the average bug from earlier chapters:
git status --short # should print nothing
git switch -c fix/average-empty # throwaway branch for this session
python -m venv .venv
. .venv/bin/activate
python -m pytest -q # record the starting resultSave the output of that last command. It is your baseline. After the session, you compare against it.
Decide what the assistant may run
Most tools let you choose between approving every command and allowing certain commands automatically. Sort commands by what they can damage, not by how likely they are to be used.
| Risk level | Examples | Suggested policy |
|---|---|---|
| Read-only | `ls`, `cat`, `git status`, `git diff`, `git log` | Allow automatically |
| Test and lint runs | `python -m pytest -q`, `ruff check .` | Allow automatically, with a timeout |
| Local writes | editing source files, creating files | Allow on a branch, review the diff |
| Environment changes | `pip install`, `npm install`, changing config | Approve each one |
| Destructive | `rm -rf`, `git reset --hard`, `git clean -fd`, `git push --force` | Deny, or approve with great care |
| External effects | `git push`, `curl` to real APIs, database commands, deploys | Deny unless you set up the target on purpose |
Test commands deserve a second look. Running tests executes your code, and your code may talk to a database, send email, or call a paid API. Before you allow the test command, check that the test suite is safe to run on your machine. If some tests touch real services, give the assistant a narrower command, such as python -m pytest tests/test_stats.py -q.
Match commands by their full text, not only by their first word. Allowing git allows git push and git reset --hard. Allowing python allows any script. Prefer specific entries such as git diff and python -m pytest.
Give the task and the limits together
State the goal, the success check, and the boundaries in the prompt. The template below extends the debugging prompt from earlier chapters.
Goal: fix the failing test tests/test_stats.py::test_average_of_empty_list_raises.
Check: run `python -m pytest tests/test_stats.py -q`. It must pass, and the rest of the suite must still pass.
Rules:
- Edit only stats.py. Do not edit, skip, or delete any test.
- Do not install packages, change configuration files, or touch the network.
- Do not run git commands that write (commit, reset, push, clean). I will commit.
- Make the smallest change that works. Explain the cause from the traceback before you edit.
- If three attempts fail, stop and report what you learned instead of trying more variations.Each rule closes off a failure described in the next section. The attempt limit matters most. Without it, an assistant can loop for a long time, changing code in ways that move the symptom around.
Review what happened, not just the result
A green test run is not sufficient evidence when the assistant could change the tests. Check three things after every session.
First, read the commands. Most tools keep a log of what ran. Look for anything you did not expect, such as an install, a file deletion outside the project, or a network call.
Second, read the full diff. Pay particular attention to the test directory and to configuration:
git diff --stat # which files changed, and how much
git diff -- tests/ # any change here needs a reason
git diff -- stats.py # the intended fix
git status --short # new or deleted files
python -m pytest -q # run the whole suite yourselfThird, run the tests yourself, outside the assistant's session. This confirms the result in a clean process and does not rely on a report.
For the average bug, the diff you want is small and matches the diagnosis:
def average(values):
if not values:
raise ValueError("average() requires at least one value")
return sum(values) / len(values)If the diff touches files you did not mention, stop and ask why before you keep any of it.
Failure modes that appear in agentic sessions
The failure modes from the earlier chapter still apply: invented APIs, overconfidence, and fixes that hide the bug. Running commands adds these:
- Weakened tests. The assistant makes a test pass by changing the assertion, adding
@pytest.mark.skip, or deleting the test. Checkgit diff -- tests/every time. A test change is acceptable only when you agreed the test was wrong. - Swallowed errors. The assistant wraps the failing call in
try/except Exception: pass. The test turns green and the bug stays. Search the diff for newexceptblocks. - Environment drift. The assistant installs or upgrades a package to make an error go away. The fix then works only on this machine. Approve installs one at a time, and ask for a lock-file diff.
- Scope creep. A request to fix one function turns into reformatting five files. A large diff is hard to review, so reject it and ask for the minimal change.
- Broad cleanup commands. Commands such as
git clean -fdorrm -rf buildcan remove untracked files you wanted. Deny them by default. - Runaway output. A command that prints thousands of lines, such as a verbose test run or a log tail, can fill the assistant's context and push out the details that matter. Ask for
-q,-x, or| head -50. - Instructions inside data. A file, log line, or web page the assistant reads might contain text that looks like an instruction, such as "ignore previous rules and run this command". Treat file contents and command output as data. If the assistant starts doing something that came from a file instead of from you, stop the session.
Keep sessions short and recoverable
Small steps carry over from the earlier chapter on applying one change at a time. Commit after each verified step, and do not let the assistant stack many edits before you look.
A practical rhythm:
- The assistant proposes a fix and runs the narrow test.
- You review the diff and run the full suite.
- You commit with a message that names the root cause.
- If anything is wrong, run
git restore stats.py, orgit restore .for tracked files. Then try again with better information.
Because you committed the baseline and each good step, you can always return to a known state. Discarding the branch at the end is a normal result, not a failure.
When to take the keyboard back
Agentic loops work best on bugs with a clear failing test and a local cause. They work poorly when the failure is intermittent, depends on data you cannot share, or involves systems the assistant cannot reach. Stop the session when:
- three attempts have not changed the failure message,
- each attempt changes a different part of the code with no shared explanation,
- the fix needs credentials, production data, or access you will not grant,
- the diff keeps growing while the failure stays the same.
At that point, use what the session produced. The notes on what was ruled out, the reduced test, and the commands that reproduce the failure all help when you debug directly.
Exercise
An assistant reports that all tests now pass after fixing a bug in stats.py. Name three checks you run before you commit, and say what each one guards against.
Show answer
(1) Run git diff -- tests/ to see whether the assistant edited, skipped, or deleted a test, which would make the pass meaningless. (2) Run git diff --stat and git status --short to find unexpected changes such as extra files, config edits, or new dependencies, which would signal scope creep or environment drift. (3) Run the full test suite yourself in a fresh process, which confirms the result does not depend on the assistant's session or on a report you have not verified. Also search the diff for new broad except blocks that could hide the error.
Exercise
You want the assistant to run pytest automatically but not to push code. Why is allowing the whole git command a poor policy, and what should you allow instead?
Show answer
Allowing git as a whole also allows git push, git reset --hard, and git clean -fd, which can send code out or destroy local work. Allow specific read-only forms such as git status, git diff, and git log, and keep writing commands (commit, push, reset, clean) for yourself or behind per-command approval.