Agentic Refactoring
Test-Driven Tech-Debt Cleanup
11 / 12
Before the agent cleans up old code, make it prove the behavior stays the same.
The Shift
Every codebase has a module nobody wants to open. It was written years ago, or rushed last quarter under deadline. It works, sort of, so it sits in the backlog. The fear is rational: without tests, any cleanup can break behavior you did not know existed.
Agents make cleanup cheaper, but blind rewrites are still dangerous. A clean rewrite that changes behavior is still a bug. The move is test-first: have the agent read the code, write a behavior spec, generate characterization tests, get a green baseline, then refactor in small steps.
Your job is to decide what must stay true. The tests make the scary part safer.
Characterize
pin behavior with tests first
Change
small, reversible edits
Verify
re-run the safety net
Commit
merge and iterate
Repeat — each pass stays green
Tests are the contract
Characterization tests freeze observable behavior. Change the structure only while that contract stays green.