Sub topic 1
How an Agent Changes Many Files at Once
A coding agent does not produce a multi-file change in one step. It builds the change through a loop of small actions. Each action reads or writes part of your working tree. To review the result well, you need to know what that loop looks like, where it can go wrong, and what evidence it leaves behind.
Prerequisites
This chapter assumes you have:
- Basic Git use: branches, commits, diffs, and staging.
- Experience reading code in at least one language.
- The ability to run a project's tests, linter, and build from the command line.
- Used a coding agent or AI assistant to edit code at least once.
The agent is a loop around a language model
A coding agent is a program that repeatedly asks a language model what to do next, runs the tool the model chooses, and feeds the result back. The model only produces text. The surrounding program turns some of that text into real actions, called tool calls.
Typical tools are:
- Search and read: list files, search for text, open a file or a line range.
- Edit: replace a string in a file, insert lines, create a file, or overwrite a file.
- Run: execute a shell command such as a test suite, a formatter, or
git.
A multi-file change is the sum of many iterations of this loop.
Three consequences follow, and the rest of the tutorial builds on them.
- The agent decides what to look at as it goes. It does not load your whole repository.
- Edits happen directly on disk, one at a time.
- The final summary is more text from the model. It is not a measurement of what changed.
How the agent finds the files to change
Suppose you ask: "Rename load_config to read_settings everywhere." The agent usually works like this:
- It searches for the old name, often with a text search similar to
grep. - It opens the files that matched, or parts of them.
- It edits each one.
- It searches again to check for leftovers.
- It may run tests or a type checker.
This works well when the target is a distinctive string. It works poorly when the target is a concept that has no single spelling. Examples are "every place that assumes prices are integers" and "every caller that depends on the old error type". Text search finds only what the agent thought to search for.
Typical gaps follow from this:
- Dynamic references. A name built at runtime, such as
getattr(module, "load_" + kind), never appears in a search for the full name. - Non-code files. Documentation, CI configuration, shell scripts, and templates that mention the name may not be searched.
- Generated or vendored files. The agent may edit a generated file directly. The next build then overwrites the edit.
- Files it never opened. The agent can only reason about code that is in its context. If a caller was never read, the agent has no information about what that caller expects.
Context is limited, so the agent works from a partial view
The model sees a limited amount of text at once, called its context window. Everything the agent reads, runs, and writes competes for that space. In a long run, early material may be dropped or summarized, or it may simply be given less weight.
This shapes the diff you receive:
- The agent may edit file 12 in a style or with an assumption that conflicts with a decision it made in file 3.
- It may repeat a helper function in two files because it no longer "remembers" it wrote the first one.
- It may fix a failing test by editing the code under test in a way that undoes an earlier fix.
When you see inconsistencies between files in the same diff, this is often the cause. The inconsistency is a signal to check the interfaces between those files, not only each file alone.
Edits are sequential and not transactional
A database transaction either applies fully or not at all. An agent run has no such guarantee. Each edit lands immediately in your working tree. If the run stops halfway, because of an error, a timeout, or a cancel, you may have:
- A function renamed in its definition but not in half of its callers.
- A new file created but not imported anywhere.
- A test updated for behavior that the code does not yet have.
The tree can be broken at any moment during the run, and it may still be broken when the agent reports finishing. Git is the only transaction boundary you have, and you create it yourself. Start from a known commit on a dedicated branch, so that every change since then belongs to the agent.
git switch -c agent/rename-config-loader
git status --short # no output means the working tree is clean
git log --oneline -1 # note the commit the agent starts fromIf the working tree is not clean when the agent starts, you cannot tell your edits from the agent's. Later chapters cover this baseline in detail. For now, keep the point that the diff is only meaningful relative to a known starting commit.
What the agent can touch besides the files you named
An agent can use any tool you have allowed. That includes more than file edits:
| Action | Where the effect appears | Visible in `git diff`? |
|---|---|---|
| Edit a tracked file | Working tree | Yes |
| Create a new file | Working tree | No, until staged (shown by `git status`) |
| Delete a file | Working tree | Yes |
| Run a formatter across the repo | Many files | Yes, as noise |
| Install a dependency | Lockfile, `node_modules`, virtual environment | Lockfile only; installed packages are usually ignored |
| Edit ignored files, such as `.env` | Working tree | No |
| Run a command with side effects, such as a database migration | Outside the repo | No |
Two rows matter most for review. git diff does not list untracked files, so a new file can be missing from your view. Effects outside Git, such as a migration run against a development database, leave no diff at all. The permissions you give the agent set the limit of what you must check.
Why changes grow wider than the task
Agents tend to produce more edits than requested. Common reasons:
- Ripple effects. Changing a function signature requires updating callers. This is legitimate, and it is how a one-line request becomes ten files.
- Helpful extras. The model has seen many pull requests that include cleanup, so it may reformat nearby code or rename variables.
- Fixing failures. If a test fails after the change, the agent edits whatever makes it pass. That may be the code, the test, or the test configuration.
- Vague scope. "Improve error handling in the loader" invites edits wherever the agent finds an error.
Not every extra edit is wrong. Your job is to tell required ripple from optional extras and from changes that should not exist. That is the work of the review, and it is why the task statement matters before the run.
What the final summary is, and is not
At the end, the agent writes a summary such as "Renamed the function in 9 files and updated all tests. All tests pass." The summary is a good map of what the agent intended. It is unreliable as evidence:
- It describes the agent's belief, which may differ from the actual state of the files.
- "Tests pass" may refer to a subset of the tests, or to a run made before a later edit.
- It tends to omit things the agent found awkward, such as a test it skipped.
Treat the summary as a list of claims to verify. Compare each claim to the diff and to your own test run.
A small worked example of the loop's footprint
Here is how the evidence of one run looks in Git. Assume the agent finished the rename task.
git status --shortA plausible output:
M src/config/loader.py
M src/app/main.py
M tests/test_loader.py
M src/billing/invoice.py
?? src/config/settings_compat.pyYou can read several things from these five lines before opening any code:
- The first three modified files match the task.
src/billing/invoice.pyhas no obvious link to a config loader rename. It needs a closer look.settings_compat.pyis new and untracked. A plaingit diffwould not show it. The agent created something you did not ask for.
This kind of triage is the subject of a later chapter. Here the point is that the structure of the agent loop leaves a footprint, and the footprint is visible if you look for it.
Key points
- An agent works in a loop of reading, editing, and running. A multi-file change is many small steps, not one operation.
- It finds files by searching, so references it did not search for can be missed.
- It sees only part of the repository at a time, which produces inconsistencies between files.
- Edits land immediately and are not transactional. Your branch and starting commit are the only rollback point.
- Extra files, extra edits, and side effects outside Git are normal outcomes of the loop and must be checked.
- The agent's summary is a set of claims, not proof.
Exercise
An agent finishes a task to rename load_config to read_settings. It reports "Renamed in all 9 files, all tests pass." Name three reasons this report might still be incomplete or wrong, and one Git command that helps you check part of it.
Show answer
Possible reasons: (1) the agent found references by text search, so dynamic references such as getattr with a built string, or mentions in docs, CI files, and scripts, may remain unchanged; (2) "all tests pass" may describe a subset of tests or a run before the last edit, and the agent may have skipped or weakened a test; (3) the agent may have created new untracked files or edited unrelated files that its summary does not mention, and effects outside Git such as migrations leave no diff. Useful commands: git status --short lists modified and untracked files, so you can compare them to the nine files claimed, and git diff --stat shows the size of the change per file.
Exercise
Why does starting the agent on a dedicated branch from a clean working tree matter, given that agent edits are not transactional?
Show answer
Each edit lands in the working tree immediately, and a run can stop or finish with the tree half-changed. If the tree started clean on a known commit, every difference from that commit is the agent's work. You can then review it as one unit, keep parts of it, or discard it by returning to the starting commit. If your own uncommitted edits were mixed in, you could not separate them from the agent's changes.