Sub topic 12
Let the assistant run commands and tests safely
This chapter assumes you can run Python tests from a terminal, use git for commits and diffs, and have already written a debugging prompt by hand. Here you let the assistant run commands itself. This is called agentic debugging. The assistant proposes a command, runs it, reads the output, and decides what to try next.
What changes when the assistant can run commands
In a chat, you are the only link between the assistant and your code. You paste the traceback and you apply the fix. With command access, the assistant can run the failing test, read the traceback, edit a file, and run the test again without you in between.
This is faster, but the assistant can now change your machine. A wrong guess can delete files, overwrite work, or run a slow command against a real database. The workflow from earlier chapters still applies. You add guardrails around it.
Set up guardrails first
Do these before you start a session:
- Commit or stash your work. A clean git state lets you undo anything with
git diffandgit restore. - Work on a branch. If the session goes badly, you delete the branch.
- Use a test or dev environment. Never point the assistant at production data or real credentials.
- Check that no secrets are in files the assistant can read, such as
.env. The rules from the earlier chapter on sensitive data apply to anything the assistant can open.
Approve commands one at a time
Most assistants ask for permission before running a command. Read each request before you approve it. Treat the approval prompt as a code review.
| Command type | Examples | Approach |
|---|---|---|
| Read-only | `git status`, `cat stats.py`, `pytest -q` | Safe to approve. |
| Changes files | `sed -i`, formatters, `git checkout` | Check which files it touches. |
| Destructive or external | `rm -rf`, `git reset --hard`, `pip install`, `curl`, database commands | Deny by default. Ask why it is needed. |
Avoid "always allow" settings for broad commands such as rm or git. Allowing a narrow command such as pytest is reasonable.
Give the assistant a bounded task
An open request such as "fix my project" invites wide changes. Give the command, the scope, and the stop condition instead:
Run `pytest tests/test_stats.py -q`. One test fails.
Read the traceback and explain the cause before you edit anything.
Then change only stats.py, rerun the same test, and stop.
Do not install packages, touch other files, or commit.This keeps the earlier habits intact. The assistant explains first, makes a small change, and verifies with the test you named.
Review what it did
After the session, do not trust the assistant's summary alone. Check the evidence yourself:
git status
git diff
pytest -qLook for files you did not expect to change. Look for edited tests, since an assistant can make a test pass by weakening the assertion. This is the fix-that-hides-the-bug failure mode in a new form. Then run the full test suite yourself, not only the one test the assistant ran.
Keep the loop short
Agentic sessions can wander. Stop the session if the assistant reruns the same failing command with no new idea, or if it starts editing files unrelated to the error. Revert with git, then return to a manual prompt with the new information you learned.
Exercise
The assistant asks to run git reset --hard && pytest. Name two checks to make before approving, and one safer alternative.
Show answer
Check that you have no uncommitted work, since git reset --hard discards it, and ask why a reset is needed to diagnose a failing test. A safer alternative is to approve only pytest, or to commit or stash your work first so the reset cannot destroy anything.