Refactoring changes the structure of code and leaves its behaviour alone. Agents are good at moving code and not good at knowing what must stay the same. You supply that.
Steps
- Check that tests cover the code you will touch. If they do not, ask for characterization tests that record what the code does today, correct or not.
- Run them and commit them separately.
- Refactor in small steps, each one a commit that keeps the suite green.
- Do not mix a refactor with a feature or a bug fix.
- Review the diff for logic changes hiding inside moves. A renamed function with a changed condition is the classic case.
What to ask the agent for
- A plan with the steps in order, approved before any edit.
- A statement of what will not change: public interfaces, data formats, error messages.
- A summary at the end listing any behaviour it noticed and left alone.
Signs of trouble
- Tests were edited to pass.
- The diff is much larger than the plan.
- Files outside the area changed.
Related: small pull requests for agent work.