Repair-R1: Better Test Before Repair
该论文提出了Repair-R1方法,通过在训练阶段引入测试用例并提前进行测试,提高了自动程序修复(APR)的效率和效果。
更新于 2025-09-07。作者:Haichuan Hu、Xiaochen Xie、Quanjun Zhang。
中文摘要
自动程序修复(APR)旨在自动定位程序缺陷、生成补丁并验证修复。现有的APR技术通常与大型语言模型(LLM)相结合,利用LLM对代码相关知识的掌握来提高修复效果。基于LLM的当前APR方法通常仅在推理阶段利用测试用例,采用先修复后通过测试执行进行验证的迭代方法。这种传统范式忽略了两个重要方面:测试用例在训练阶段的潜在贡献以及在修复前进行测试的可能性。为了解决这个问题,我们提出了Repair-R1,它将测试用例引入模型训练阶段,并将测试生成提前到修复之前。模型需要首先生成能够区分缺陷行为的判别性测试用例,然后基于这些测试进行修复。这使模型能够更好地定位缺陷并理解缺陷的潜在原因,从而提高修复效果。我们使用三种不同的骨干模型实现了Repair-R1,并使用强化学习(RL)协同优化测试生成和错误修复。在四个广泛采用的基准测试上的实验结果证明了Repair-R1的优越性。特别是,与普通模型相比,Repair-R1将修复成功率提高了2.68%至48.29%,将测试生成成功率提高了16.38%至53.28%,将测试覆盖率提高了0.78%至53.96%。我们将在以下网址发布代码和权重:https://github.com/Tomsawyerhu/APR-RL 和 https://huggingface.co/tomhu/Qwen3-4B-RL-5000-step。
英文摘要
APR (Automated Program Repair) aims to automatically locate program defects, generate patches and validate the repairs. Existing techniques for APR are often combined with LLMs (Large Language Models), which leverages the code-related knowledge of LLMs to improve repair effectiveness. Current LLM-based APR methods typically utilize test cases only during the inference stage, adopting an iterative approach that performs repair first and validates it through test execution afterward. This conventional paradigm neglects two important aspects: the potential contribution of test cases in the training phase, and the possibility of leveraging testing prior to repair. To address this, we propose Repair-R1, which introduces test cases into the model's training phase and shifts test generation to precede repair. The model is required to first generate discriminative test cases that can distinguish defective behaviors, and then perform repair based on these tests. This enables the model to better locate defects and understand the underlying causes of defects, thereby improving repair effectiveness. We implement Repair-R1 with three different backbone models, using RL (reinforcement learning) to co-optimize test generation and bug repair. Experimental results on four widely adopted benchmarks demonstrate the superiority of Repair-R1. Specially, compared to vanilla models, Repair-R1 improves repair success rate by 2.68\% to 48.29\%, test generation success rate by 16.38\% to 53.28\%, and test coverage by 0.78\% to 53.96\%. We publish the code and weights at https://github.com/Tomsawyerhu/APR-RL and https://huggingface.co/tomhu/Qwen3-4B-RL-5000-step.
解读
一句话总结
该论文提出了Repair-R1方法,通过在训练阶段引入测试用例并提前进行测试,提高了自动程序修复(APR)的效率和效果。
问题1:这篇论文想要解决什么具体问题?
- 问题背景:现有基于LLM的APR方法通常只在推理阶段使用测试用例,忽略了测试用例在训练阶段和修复前的潜在价值。
- 现有方案不足:现有方法依赖于相似的错误,可能生成表面相似但未真正理解或分析根本缺陷的补丁;同时,测试数据利用率低。
- 研究目标:提出Repair-R1,将测试用例引入训练阶段,并在修复前生成测试用例,以提高修复效果。
问题2:论文的核心创新点是什么?
- 技术创新:利用强化学习框架联合优化测试用例生成和缺陷修复。
- 方法改进:将测试用例生成作为指导机制,优化代码修复过程。
- 优势:相比现有方法,Repair-R1在修复成功率、测试用例生成成功率以及测试覆盖率方面均有显著提升。
问题3:实验结果如何验证了方法的有效性?
- 关键实验:在四个广泛使用的基准测试上进行实验。
- 性能提升:与未训练模型相比,Repair-R1的修复成功率提高了2.68%至48.29%,测试用例生成成功率提高了16.38%至53.28%,测试覆盖率提高了0.78%至53.96%。
- 对比结果:与SFT和单目标优化相比,Repair-R1表现更优。
问题4:这个研究的实际应用价值是什么?
- 应用场景:适用于自动程序修复领域,可提高程序可靠性和开发效率。
- 实施建议:在APR模型中引入测试用例,并采用强化学习进行联合优化。
- 局限与展望:未来可进一步研究不同类型错误和复杂场景下的Repair-R1应用,并探索其他优化算法和奖励机制。
来源
机器可读索引:llms.txt