Sub topic 8
Working in Small, Reversible Steps
This chapter assumes you can use Git branches and commits, run a test suite from the command line, and have already pinned down current behavior with tests. The goal is to keep every change small enough to check quickly and undo cheaply.
Why small steps matter
A coding agent can edit many files in one turn. When a large change fails, you can't tell which edit broke it. A small step has one purpose, a short diff, and one check. If the check fails, you discard that step and lose little work.
A reversible step has two properties:
- It leaves the code in a working state, with tests passing.
- It is saved as its own commit, so you can return to it.
Choosing the size of a step
A good step does one kind of thing. Extracting one function is a step. Renaming a symbol across the project is a separate step. Changing behavior is not part of a refactoring step at all.
A useful test: can you describe the step in one commit message without using "and"? If not, split it.
The step cycle
Each step follows the same loop.
Example: extracting a function
Start on a branch with passing tests, and let the agent make one step. The code below shows the change for the first step.
# Before
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = round(subtotal * 0.2, 2)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}
# After: same inputs and outputs, tax logic extracted
TAX_RATE = 0.2
def calculate_tax(subtotal):
return round(subtotal * TAX_RATE, 2)
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = calculate_tax(subtotal)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}The observable behavior is the same. Only the structure changed, so the existing tests should pass without edits. If a test must change, treat that as a warning that behavior drifted.
Using Git as the undo button
Commit after each verified step. These commands show the rhythm.
git switch -c refactor/extract-calculate-tax
pytest
# let the agent make one small step, then check it
pytest && git add -A && git commit -m "Extract calculate_tax from format_invoice"
# if a later step goes wrong, discard it and keep the checkpoint
git restore .The && means the commit only happens if pytest succeeds. The git restore . command discards uncommitted edits to tracked files, so it only removes work since the last commit. It does not remove new untracked files. Use git status to find any the agent created.
Telling the agent to stop
Agents tend to keep going. State the boundary in your prompt:
- "Do only this step, then stop and run the tests."
- "Do not start the next step until I confirm."
- "If a test fails, report it. Do not edit the test."
Common errors
| Error | Result | Fix |
|---|---|---|
| Committing without running tests | A broken checkpoint you can't trust | Chain the commit after the test command |
| Mixing a rename and a logic change | A diff that is hard to review | Make them separate commits |
| Letting the agent fix a failing test by editing it | Hidden behavior change | Forbid test edits during refactoring |
| Leaving work uncommitted across many steps | One failure discards everything | Commit after each passing step |
Exercise
You asked the agent to extract a function, and it also renamed two variables and reformatted the file. Tests pass. What should you do before committing, and why?
Show answer
Split or discard the extra edits. Keep only the extraction in this commit, and put the renames and formatting in separate commits, or restore them. A mixed diff is harder to review, and if a later problem appears, you can't revert one change without the others.
Exercise
Why does the commit command use pytest && git commit instead of running the two on separate lines?
Show answer
The && runs the commit only if the tests exit successfully. Separate lines would commit even when tests fail, which creates a broken checkpoint.