arXiv:2507.22853cs.SEcs.AI2025-07被引 6

先生成测试用例再修复代码,提升自动修复成功率。

Repair-R1: Better Test Before Repair

  • 训练时引入测试用例,让模型先生成能区分缺陷的测试。
  • 修复成功率提升2.68%至48.29%,测试覆盖率最高增53.96%。
  • 适合研究自动程序修复与大模型结合的开发者使用。

自动化程序修复(APR)旨在自动定位缺陷、生成补丁并验证修复效果。现有基于大语言模型(LLM)的APR方法通常仅在推理阶段使用测试用例,采用先修复后验证的迭代方式。这一传统范式忽略了测试用例在训练阶段的潜力以及修复前进行测试的可能性。为此,我们提出Repair-R1,将测试用例引入模型训练阶段,并将测试生成前置到修复之前。模型需先生成具有区分性的测试用例以识别缺陷行为,再基于这些测试进行修复。这使模型更精准定位缺陷并理解其根本原因,从而提升修复效果。我们在三种不同主干模型上实现Repair-R1,采用强化学习(RL)协同优化测试生成与缺陷修复。在四个广泛使用的基准测试上,实验结果表明其优越性:相较于基线模型,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。

原文摘要 · Abstract (English)

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.

程序修复大模型强化学习测试生成

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。