Abstract
Automated Program Repair (APR) is a task to automatically generate patches for the buggy code. However, most research focuses on generating correct patches while ignoring the consistency between the fixed code and the original buggy code. How to conduct adaptive bug fixing and generate patches with minimal modifications have seldom been investigated. To bridge this gap, we first introduce a novel task, namely AdaPR (Adaptive Program Repair). We then propose a two-stage approach AdaPatcher (Adaptive Patch Generator) to enhance program repair while maintaining the consistency. In the first stage, we utilize a Bug Locator with self-debug learning to accurately pinpoint bug locations. In the second stage, we train a Program Modifier to ensure consistency between the post-modified fixed code and the pre-modified buggy code. The Program Modifier is enhanced with a location-aware repair learning strategy to generate patches based on identified buggy lines, a hybrid training strategy for selective reference and an adaptive preference learning to prioritize fewer changes. The experimental results show that our approach outperforms a set of baselines by a large margin, validating the effectiveness of our two-stage framework for the newly proposed AdaPR task.
Introduction
Automated Program Repair (APR) has seen significant advancements with the integration of Large Language Models (LLMs). While these models excel at generating code that passes test cases, they often fail to maintain consistency with the original source. In many instances, an LLM might rewrite an entire function to fix a minor logic error, resulting in "noisy" patches that are difficult for developers to review and maintain.
To address this, we define a new challenge: Adaptive Program Repair (AdaPR). The goal of AdaPR is to produce patches that are not only correct but also consistent with the original code, minimizing unnecessary modifications. As illustrated in Figure 1, standard LLM repairs often involve excessive changes, whereas an adaptive approach targets only the specific buggy lines.
Methodology: The AdaPatcher Framework
We propose AdaPatcher, a two-stage framework designed to decouple bug localization from patch generation to achieve adaptive repair.
Stage I: Bug Locator with Self-Debug Learning
The first stage focuses on identifying the precise location of the bug. Rather than relying solely on static code analysis, we employ Self-Debug Learning. The model analyzes execution traces—capturing variable states at each line during failed test runs—and compares them against expected outputs. This runtime information allows the Bug Locator to output a "Code Diff" that pinpoints the exact lines requiring modification.
Stage II: Program Modifier
The second stage generates the final patch based on the identified locations. The Program Modifier incorporates three key strategies to ensure consistency:
- Location-Aware Repair Learning: The model is specifically trained to generate fixes for the lines flagged in the first stage.
- Hybrid Training for Selective Reference: To mitigate the impact of potential errors in bug localization, the modifier is trained on a mixture of ground-truth and model-predicted bug locations. This teaches the model to selectively trust the locator's output.
- Adaptive Preference Learning: We utilize Direct Preference Optimization (DPO) to align the model with the "less is more" principle. By training on pairs of correct patches, the model learns to prefer the one with fewer modifications.
Experimental Evaluation
We evaluated AdaPatcher on a newly curated benchmark, ACPR (Accuracy-Consistency Program Repair), which contains over 52,000 real-world code triplets (buggy code, failed test, and correct code) from CodeNet.
Key Results
- Accuracy: AdaPatcher achieved a 31.95% accuracy rate, significantly outperforming the vanilla CodeLlama baseline (10.67%) and rivaling much larger models.
- Consistency: The framework substantially improved the Code Consistency Rate (CCR). For GPT-4o, the CCR rose from 27.40% to 50.08% when integrated into the AdaPatcher pipeline.
- Localization Performance: Human evaluations indicated that AdaPatcher’s bug localization was rated as "Accurate" 55% of the time, compared to just 20% for GPT-4o.
Conclusion
AdaPatcher addresses the critical need for consistency in automated program repair. By combining execution-trace-based localization with preference learning for minimal modifications, our framework generates patches that are both functionally correct and developer-friendly. Future work will focus on extending this approach to multi-module bugs and other programming languages beyond Python.
Summary updated