AI-assisted refactoring
2026-11-17en10 minhandwrittentalk
AI-assisted refactoring
Use Cursor to improve existing code while maintaining control over architecture and behavior.
Today we are talking about AI-assisted refactoring. The topic is a bit technical, but I’ll try to keep it high level, so we don’t go too deep into the technicalities.
The first, maybe trivial, question we can ask ourselves is: what is AI-assisted refactoring? The subtitle speaks for itself: use Cursor, or any other AI tool, to improve an existing codebase while keeping control over architecture and behavior.
Agenda
- Refactoring, before and after AI
- Setting up a refactor with AI
- A few extra tricks
- The human’s role
We start with refactoring before and after AI. Then we try to understand how to set up a refactor using AI, we go through a few extra tricks, and we close by trying to figure out the role of the human in this loop.
When I started researching this topic, the first thing I wanted to understand was the difference between now and then.
Refactoring has always been there. It’s part of the software development process: something we can’t hide from, something we have to do, because of the nature of software.
Software rots.
Complexity grows as a system changes, unless someone works to reduce it.
AI era more copy/paste, less moved code.
Like everything in the universe, software tends to entropy: software rots. It’s common sense. Even if AI writes the very best code for our use case today, in three months, six months, a year, even if we don’t touch it, it gets sloppier step by step. The use case may evolve, and even if it doesn’t, the world outside the code does: think about all the vulnerabilities AI is finding right now. They make our software old, and we need to go and update it. So, we refactor.
What changed after AI? Not much, but AI tends to copy and paste more. We noticed it in our repos too: the same utilities copied everywhere, where before we leaned on the DRY principle and grouped things into one class or a library.
So what changed before and after? Not much in terms of principles, best practices or techniques. The definition did not change, the typist did. What changed is who writes the code, and that has an impact on how fast we can deliver those changes.
The refactor fight.
Before: engineers vs product. How many days for the refactor?
Now: product builders ship features. Engineers make it right.
Something else changed too. Before AI we had this little fight between product and engineers: one day for refactoring, one for bug fixing, the rest for features. Or: we have to spend a week, freeze the features, and so on.
Now we don’t care about that anymore, because the speed has increased a lot: we can deliver a refactor in days instead of months. Product builders, or whatever you want to call them, can do their job and implement the features, while we focus on the more architectural and technical part.
How CodeTaste tested it.
You might think: nothing has changed but the speed, so it’s free. It’s not free. Before telling the model “refactor the code, make no mistakes”, we have to understand a couple of limitations LLMs still have with refactoring.
I started from CodeTaste, the only benchmark I found on refactoring. They analyzed a bunch of open-source repos and picked human refactors that improved the code in some way. Then they went back in history, to the code before each refactor, and asked agents to redo it in two ways.
One way was a very specific, closed task: improve the logging, or improve the mapping between two domains. The other was the “make no mistakes” way: here’s an area you’re responsible for, refactor it to improve maintainability and scalability, use your creativity.
With small, specific tasks, the best agent made almost 70% of the same refactors the human did; the other four made 11 to 34%. With a broad, unspecified area, they made under 8%, and stopped there. Big tasks versus small tasks: the result is much better when the task is specific and well defined.
Good at local cleanup.
Weak at structure.
2024 Developers still did better at modularization, encapsulation, abstraction.
2026 Over 95% of AI review comments were defects or code improvements.
The second finding is similar, but a bit different: agents are good at local cleanup and weak at structure. They’re good at spotting smells like magic numbers, function complexity, vulnerabilities, or an any type: details you can find in one line. They’re not that good at bigger things like modularization, encapsulation and abstraction.
That study is from 2024, when developers were still better at it, and things have changed, of course. But a study from this year compared human and agent comments across a lot of code reviews, and the agent comments were mostly about tiny details instead of the big picture. LLMs still tend to go for the details instead of thinking about the bigger picture.
One last thing before starting: choose your oracle, a fancy way to say define your source of truth. Before a refactor we must know what has to stay true before and after. Say we have to improve the performance of a hotel rates search engine: the rates we show must stay the same before and after, and what we want to improve is the speed. Or I refactor a backend and want the behavior and the API contracts to stay the same.
Usually the source of truth is the automated tests, and it should be. But tests can hide a lot, because it’s impossible to test every possible behavior. Another study found that LLM refactorings changed a lot of behavior under the hood, because the test suites missed some of the behavior that mattered: a green suite missed about 21% of the changes that were not the same.
Set it up.
Before the agent:
Name. Freeze. Check.
Each step:
One step. Read. Revert, back to Name.
What do people recommend for a refactor? The practices differ, but the concepts are more or less the same. Before telling the agent to go:
- Name what you want to do. Architectural decision records help here, before AI and now: they make the change easier to test and, if there’s a problem, easier to revert, because you know where to look.
- Freeze what you want to preserve: your oracle, your source of truth.
- Check it, better in a deterministic way: unit tests, API contracts, something the agent can run by itself to tell whether it’s doing well.
Then, for each step:
- One step: well defined, not mixed with other things, never a refactor inside a feature. Validation, QA and reverting all get easier with self-contained pull requests and commits.
- Read what the refactor is doing. Not every detail, but why we’re doing it and whether the high level is right.
- Revert. Agents aren’t good at going back on their own steps: tell them a solution is bad and they’ll keep pushing the same one. Sometimes we have to step back, revert, fix the plan, and choose another way.
A few extra tricks.
Ratchet. Gate loop. One policy.
- Ratchet. Say the agent keeps adding more and more
anytypes, and you know it’s a bad pattern. Count the bad patterns in your codebase and decide that from now on the number can’t go up. It’s easy for humans, and especially for agents, to see when they’re doing something wrong: if the number goes up, they can go back and fix their own implementation. - Gate loop. This is true for any feature, not only refactors: before doing something, plan it, and not only with plan mode; a ticket works too. I plan with the agent and end up with a bunch of tickets, and that’s my plan. A gate checks the plan before the code and the diff after. If there’s no plan, it asks you to make one first.
- One policy. Two services talk to each other, and timeouts and retries piled up endpoint by endpoint, each with its own: a mess. Sometimes an error is retriable, sometimes you should just wait longer. We picked a new source of truth: one library, one standard, one policy, an average of everything we had. A person chooses the budget. It was a stack of changes in many small pull requests: a big refactor done with AI, without bothering the people working on features.
The human owns both definitions.
The goal.
Same behavior.
Last part: where is the human in the loop? The human owns two definitions.
The goal: what we’re doing and why. A refactor, especially a big one, doesn’t have a clear outcome; it’s usually a trade-off. Think microservices versus monolith: both have pros and cons, and it really depends on what you want.
Same behavior: as I said, we have to decide the source of truth before and after the refactor, because it changes the outcome a lot.
One last thing for humans, about reviews. If the agent builds a whole new library, how do you review it? You don’t go for the details; you leave those to the agent. You want the bigger picture: a diagram, the KPIs, what failed. Our refactor was meant to fix a lot of errors, so after it we have to check that those errors are gone. That’s on us, not on the agent.
And if you do read the code, look for patterns. If you see lots of comments, lots of any types, a lot of duplication, a weird use of inheritance instead of composition, and so on, there’s probably a bad pattern: understand why it’s there, instead of focusing on that one line.
Keep it simple.
The practices we learned before AI still apply.
Should we keep agent code simple, or let it be as complex as it wants, as long as it works? Keep it simple.
Agents, still in 2026, stick to the solution they have in mind. It’s very hard for them to go back and rethink, so they pile code over code over code, patching every possible case. And an agent is like a human in this sense: the more it has to think about, the bigger its context, the worse it performs.
All the good practices we learned before AI still apply. Part of our job now is to spot this bad behavior in the bigger picture and steer the agent the other way.
References
- Fowler. Refactoring. 1999, 2nd ed. 2018.
- Lehman. Programs, life cycles, and laws of software evolution. Proc. IEEE, 1980.
- Harding. AI assistant code quality. GitClear, Feb 2025. Correlation, not an experiment.
- Fowler. Talk, Nov 2025, and fragment, 8 Jan 2026.
- Thillen, Mündler, Raychev, Vechev. CodeTaste. 2026.
- Cordeiro, Noei, Zou. arXiv 2411.02320. 2024.
- Zhong, Noei, Zou, Adams. arXiv 2603.15911. 2026.
- Dristi, Dwyer. arXiv 2602.15761.
- Lee Robinson. Agent best practices, 9 Jan 2026, and Plan mode.
- Anthropic. Claude Code best practices.
- Cursor rules.
- CLAUDE.md.
- Lauren Tan. All Code Is Slop (Except Mine). 11 Nov 2026.
Further reading
- Cordeiro, Noei, Zou. RefactorAssist. 2026.
- Ben Amor et al. RefactorPlatform. EMNLP 2026.
- Smolić et al. arXiv 2604.03135. 2026.
- DORA 2025. AI amplifies the system you already have.
- Böckeler. TDD in the agent loop. Small sample.
- Bauer. Does GitHub Copilot improve code quality? GitHub, 18 Nov 2024. New code, not a refactor.
- Márton et al. MDPI Software 5(2):19. 29 Apr 2026. Small model, not Cursor.
- AGENTS.md.
- Zhao et al. arXiv 2607.09510. 2026. Agents keep repairing the wrong problem.