用智能代理修复大模型代码重构的错误,让自动化重构更可靠。
RefactorAssist: Agentic Refinement for Reliable Code Refactoring

- 设计智能代理,结合测试日志与代码上下文迭代修正重构结果。
- 修复率达70.8%,整体通过率高达94.2%,显著提升重构正确性。
- 适合希望安全使用大模型重构代码的开发团队和工具开发者。
代码重构旨在不改变功能的前提下优化代码结构。尽管大语言模型(LLMs)在自动化软件工程任务方面展现出潜力,但其生成的重构常引入细微错误,导致功能变更和单元测试失败,限制了实际应用。为解决该问题,我们分析了10个开源Java项目及其原生测试套件中LLM重构失败的原因,并开发了RefactorAssist智能体以提升重构的功能正确性。研究发现,失败主因包括上下文误解/幻觉(24.3%)、重命名错误或不一致(15.3%)、新增功能或变量(13.7%)、代码不完整(11.3%)、语法与结构错误(9.7%)、未处理边界情况(9%)、类型处理不当(8.7%)及作用域外变量(8%)。为降低成本,RefactorAssist首先通过静态检查修复缺失导入、括号不匹配等编译错误,对剩余问题则结合错误日志与代码差异进行测试引导的智能修复,在最佳配置下实现70.8%的修复率与94.2%的累计通过率。结果表明,静态检查与测试引导的上下文感知智能体修复可显著提升大模型重构的可靠性,使其更接近实际开发工作流集成。
原文摘要 · Abstract (English)
Code refactoring aims to enhance the internal structure of source code without affecting its functional behavior. The recent advancements of Large Language Models (LLMs) have demonstrated potential for automating software engineering tasks, such as code refactoring. However, the refactorings produced by LLMs often introduce subtle errors, leading to functional behavior changes and failed unit tests, which limit their practical adoption. To address the limitations of LLM-generated refactorings, we analyze the root causes of their failures and develop the RefactorAssist agent to improve the functional correctness of LLM-generated refactorings. To this end, we use 10 open-source Java projects with their native test suites and manually evaluate why LLM-generated refactorings fail unit tests. We then design an agentic approach that leverages unit-test logs, error explanations, project context retrieval, and code diffs to guide the iterative refactoring. Our findings show that the main reasons for failure are context misunderstanding/hallucination (24.3%), incorrect or inconsistent renaming (15.3%), adding new functionality or variables (13.7%), code incompleteness (11.3%), syntax and structural errors (9.7%), edge cases not handled (9%), improper type handling (8.7%), and variables outside scope (8%). To make our approach cost-effective, RefactorAssist first applies a static repair step for missing imports, unbalanced brackets, and compilation errors without LLMs. For remaining failures, RefactorAssist incorporates error logs and code diffs, achieving up to a 70.8% repair rate on the remaining failures and a 94.2% cumulative pass rate under the best-performing configuration. These results indicate that static checks and test-guided, context-aware agentic repair can increase the reliability of LLM-generated refactorings, bringing them closer to practical integration within developer workflows.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。