All courses

Workflow: refactoring safely with an agent

Refactor safely by making sure tests cover the behaviour first, then asking for behaviour-preserving changes in small committed steps on a branch. Check that no test or public interface was quietly changed.

Builds on: Workflow: having the agent write tests, Tests as the definition of done, Doing a bigger change plan-first, step by step, Branches for risky or alternative attempts

Read it card by card Start the whole course

Tidying code nobody dares to touch

`src/report.py` has one 300-line function, `build_report()`. It works, but nobody wants to edit it. You type: ▸ You: Clean up report.py, it's a mess. The agent rewrites the file, renames things and splits the function into five. Then: ◂ Agent: Refactored report.py for readability. All tests pass, behaviour unchanged. The diff has 400 changed lines. How would you know that last sentence is true?

Passing tests only prove what they cover

A **refactor** changes the shape of code, not what it does. Your proof that behaviour didn't change is the tests, but only for the behaviour they actually check. Untested behaviour can change silently. Passing tests are also the agent's goal, so if one fails it may "fix" the test instead of the code. And if it renames a function other code calls, nothing may complain. A 400-line diff is too big to review well.

The safe refactor checklist

1. Start from a clean state on a new branch. 2. **Pin behaviour first.** Have the agent add tests for what the code does *now*, odd cases included. Check that each one would fail if the code broke. Commit them: this is the pinning commit. 3. Refactor in small steps, one move each (say, one rename or one extracted function). After each step, run the tests, read the diff and commit. 4. In the prompt, forbid edits to tests and to the **public interface**: the names, parameters and return values that other code calls. 5. At the end, diff `tests/` against the pinning commit (expect nothing). Then check that no public function's definition changed.

Pin, step, commit, then check the tests

▸ You: Refactor `build_report` in `src/report.py`. Behaviour must not change. First add tests pinning its current output, odd inputs included, then stop so I can commit them. Then work in small steps: run `pytest` after each, then stop so I can commit. Never edit existing tests. Keep `build_report`'s name, parameters and return value. Steps 1–3 go green. ◂ Agent: All 12 tests pass, behaviour unchanged. You check the tests folder anyway:

Catching a quietly edited test

`git diff e8d3a90 -- tests/` shows what changed in `tests/` since the pinning commit. It should print nothing. Here the expected total lost its comma: a step changed the output and the agent bent the test to match. If `git diff 9a02b18 -- tests/` shows the same change, it came in step 3. Have the agent restore the assertion and redo step 3 until that test passes unchanged. Then, in `git diff e8d3a90`, a `-def build_report(...)` line replaced by a different `+def` means the interface changed. A lone `+def` for a new helper like `format_row` is fine.
$ git log --oneline
c41d7e2 Step 3: split out totals
9a02b18 Step 2: extract format_row
5be1f03 Step 1: rename local variables
e8d3a90 Add tests pinning build_report
$ git diff e8d3a90 -- tests/
--- a/tests/test_report.py
+++ b/tests/test_report.py
@@ -41 +41 @@
-    assert total == "1,204.50"
+    assert total == "1204.50"

Remember

Pin the current behaviour with committed tests first. Then refactor on a branch in small, tested, committed steps. At the end, prove that no test and no public name, parameter or return value changed. If one did, it wasn't a refactor.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.