How an Agent Edits
The read, write, and run loop, and how to read the diff before you trust it.
Learning objectives
- Describe the loop an agent uses to change files
- Read a diff before you accept it
- Limit the files an agent is allowed to touch
An agent is a writer with a folder
A coding agent does not push a mystical “update” into your app. It does ordinary file operations, very quickly, with a model choosing the next step:
- Read the prompt and, usually, a few files it searched for.
- Decide which files to open.
- Insert, delete, or replace lines. Sometimes it creates a file. Sometimes it deletes one.
- Run a command, such as a test or
npm run dev, and read the output. - Edit again if the output looks wrong.
That loop is the whole trick. The model does not hold your repository in its weights. It holds a slice of text in the current conversation, then writes the slice back to disk. If the slice was incomplete, the edit can “fix” the function it saw and break the function it did not open.
The diff is the edit
After the agent stops, do not start with the summary it typed. Start with Git’s view of the working tree.
git status
git diff --stat
git diff -- app/reserve.php
--stat is the table of contents: which files, how many lines. The full diff is the edit itself. A line starting with - was removed. A line starting with + was added. For Workshop Co., a waitlist change should touch the reserve path, maybe a template, maybe a small migration. It should not rewrite the login system, the CSS framework, and the README in the same breath unless you asked for that.
| What you see | What to ask |
|---|---|
| One file, a few dozen lines | Does the new behavior match the prompt, and did a test run? |
| Twenty files, including lockfiles and config | Did the agent “upgrade the stack” while doing a one-line fix? Split or reject it. |
| A deleted file you did not mention | Stop. Deletion is a product decision, not a cleanup the model gets to make alone. |
.env or a key file in the diff | Do not commit. Remove the secret from the file and from the chat if it was pasted. Rotate the key. |
Give the agent a fence
Good agent instructions name the files in scope. “Add a waitlist in app/reserve.php and views/class.php. Do not change authentication, billing, or dependencies.” Scope is how a non-developer stays in charge. The model will still wander. The diff is how you catch the wandering before it becomes a commit.
If the tool offers an approval step on each file write, leave it on until you trust the repository’s review habit more than the model’s confidence.
Some tools will run git commit, git push, or even git reset --hard if you ask them to “clean this up.” Reset and force-push destroy the audit trail this book is about. Tell the agent it may edit and commit on a branch, and that it may not rewrite history or push to main.
Read this diff
You asked for a waitlist. git diff --stat shows app/reserve.php (+40), package-lock.json (hundreds of lines), and .env (+1). What do you do before any commit?
Answer
Inspect app/reserve.php. Refuse to stage .env. If the lockfile change was not requested, drop it unless you understand the dependency bump. Confirm .env is in .gitignore. Only the waitlist lines are a candidate for the next commit.