← Latest papers
💻 computer science

RefactorAssist: Agentic Refinement for Reliable Code Refactoring

This paper introduces RefactorAssist, an agentic framework that combines static repair with iterative, test-guided refinement using error logs and context retrieval to significantly improve the functional correctness and reliability of LLM-generated code refactorings.

Original authors: Jonathan Cordeiro, Shayan Noei, Ying Zou

Published 2026-08-04
📖 7 min read🧠 Deep dive

Original authors: Jonathan Cordeiro, Shayan Noei, Ying Zou

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you are a master architect who has spent years designing a magnificent, complex castle. Now, imagine hiring a super-smart, incredibly fast robot assistant to help you renovate it. Your goal isn't to change how the castle works—people still need to get from the kitchen to the bedroom without falling through the floor—but to make the hallways wider, the rooms brighter, and the structure easier to maintain. This is the world of code refactoring: the process of cleaning up and reorganizing computer code to make it better, without breaking the software it runs.

For a long time, we've had tools that act like basic spell-checkers for code, pointing out messy formatting or obvious mistakes. But recently, a new kind of "robot" has arrived: Large Language Models (LLMs). Think of these as AI assistants that have read almost every book, manual, and instruction sheet ever written. They are amazing at writing new stories or fixing broken sentences. However, when you ask them to renovate a complex castle (a software project), they sometimes get a little too creative. They might move a wall that supports the roof, or rename a door in a way that confuses everyone trying to find the kitchen. The big question for scientists and engineers is: Can we trust these AI robots to do the renovation safely, or do they need a human supervisor to double-check their work?

This is exactly what the paper "RefactorAssist: Agentic Refinement for Reliable Code Refactoring" investigates. The researchers, Jonathan Cordeiro, Shayan Noei, and Ying Zou, wanted to see if these AI robots could fix up code without breaking it, and if they did break it, could a smart "repair agent" fix the robot's mistakes?

The Robot's First Attempt: A Mixed Bag

The team started by asking several different AI models (including some open-source ones like StarCoder2 and powerful commercial ones like GPT-4o) to perform renovations on real-world Java software projects. They treated the software like a living thing with a built-in "health check" system called unit tests. These tests are like a series of checkpoints: if the code is changed correctly, the robot passes the test; if the robot accidentally breaks a function, the test fails.

The results were a bit of a shock. Even the best AI models, like GPT-4o, only managed to get the renovation right about 80.8% of the time. That means nearly 1 in 5 times, the AI tried to improve the code but accidentally broke something, causing the software to fail its health checks. The researchers found that simply giving the AI more examples of how to renovate (a technique called "few-shot prompting") didn't really help much. The problem wasn't that the AI didn't know how to renovate; it was that it didn't understand the context well enough to avoid making subtle, dangerous mistakes.

Why Did the Robots Fail?

To figure out what went wrong, the researchers acted like detectives, examining the "crime scenes" of the failed code. They discovered that the AI's mistakes fell into eight main categories, with the most common being:

  1. Hallucinations and Context Confusion (24.3%): The AI would invent new features or change things that didn't need changing because it misunderstood the story of the code.
  2. Inconsistent Renaming (15.3%): The AI would rename a variable (like calling a "door" a "gate") in one place but forget to update the name everywhere else, leaving the code confused.
  3. Adding New Stuff (13.7%): The AI would accidentally add new variables or functions that weren't supposed to be there.
  4. Incomplete Code (11.3%): The AI would stop writing the code halfway through, leaving missing pieces.
  5. Syntax and Structural Errors (9.7%): Basic mistakes like forgetting a closing bracket or a semicolon.
  6. Edge Cases (9%): The AI forgot to handle rare situations, like what happens if a user enters zero instead of a number.
  7. Type Handling (8.7%): Mixing up different kinds of data, like trying to put a text message into a number box.
  8. Scope Issues (8%): Trying to use a variable in a place where it doesn't exist.

Enter RefactorAssist: The Super-Inspector

The researchers realized that just asking the AI to try again wasn't enough. They needed a system that could catch these mistakes and fix them automatically. So, they built RefactorAssist, a smart "repair agent" that acts like a two-stage quality control inspector.

Stage 1: The Quick Fix (Static Repair)
Before even asking the AI to think again, RefactorAssist runs a fast, rule-based check. It's like a spell-checker that doesn't need to understand the story, just the grammar. It fixes obvious things like missing imports, unbalanced brackets, and simple type errors. This step is cheap and fast because it doesn't use the heavy AI brain. Surprisingly, this simple step fixed a huge chunk of the problems, boosting the success rate from 66.1% to 74.4% (and up to 80.3% in their best configuration) just by cleaning up the syntax.

Stage 2: The Detective Work (Agentic Repair)
For the remaining broken code (the ones that still failed after the quick fix), RefactorAssist kicks into "agent mode." It gathers all the evidence: the error messages from the failed tests, the specific changes the AI made (the "diff"), and the surrounding code context. It then asks a powerful AI (like GPT-4o) to act as a detective, explaining why the code failed and suggesting a fix. This detective doesn't just guess; it uses the evidence to guide the repair.

The results of this two-stage process were impressive. After the initial AI attempt, the static fix, and then the iterative detective work, the success rate climbed all the way to 94.2%. Crucially, the system was able to fix 70.8% of the failures that remained after the initial static repair step. This means that for the subset of renovations the AI botched and the grammar check couldn't fix, the RefactorAssist detective agent successfully recovered the vast majority, bringing the total success rate to nearly perfect.

What Didn't Work?

The researchers also tested some ideas that they thought might help but turned out to be less useful. For instance, they tried adding a "retrieval" system that would search the entire project for extra context to give the AI more background information. While this helped a little bit in the very first few seconds of fixing, it didn't actually help the AI solve the hardest problems in the long run. The paper suggests that having a clear, accurate explanation of why something broke (the "diagnosis") is far more important than having a massive amount of extra context.

The Takeaway

This paper doesn't claim that AI can perfectly refactor code on its own yet. In fact, it proves that without help, AI makes mistakes about 20% of the time. However, it shows that by combining a simple, fast "grammar check" with a smart, evidence-based "detective agent," we can fix most of those mistakes.

The main finding is that RefactorAssist can take a messy, broken AI-generated renovation and turn it into a safe, working code update with a success rate of 94.2%. It suggests that the future of software engineering isn't just about having a smarter AI, but about building a team where the AI does the heavy lifting, and a specialized repair agent catches the errors before they reach the user. It's a reminder that even super-smart robots need a good quality control team to do their best work.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →