Abstract
Automated Program Repair (APR) aims to automatically generate patches for buggy code. While current Large Language Models (LLMs) excel at generating correct patches, they often ignore the consistency between the fixed code and the original buggy version, frequently rewriting entire functions unnecessarily. We introduce AdaPR (Adaptive Program Repair), a task focused on generating patches with minimal modifications. We propose AdaPatcher, a two-stage framework consisting of a Bug Locator with self-debug learning and a Program Modifier enhanced with location-aware repair and adaptive preference learning. Our results demonstrate that AdaPatcher significantly outperforms existing baselines in both accuracy and consistency.
Introduction
The primary goal of Automated Program Repair (APR) has traditionally been correctness—ensuring the generated patch passes all provided test cases. However, in real-world software development, a patch's utility is also determined by its consistency. Developers prefer patches that fix the specific error while preserving as much of the original code structure and logic as possible.
Current LLMs tend to be "over-fixers." When presented with a bug, they often discard the developer's implementation in favor of a complete rewrite. This behavior complicates code reviews and obscures the original intent. To bridge this gap, we propose the AdaPR task and the AdaPatcher framework, which prioritizes "minimalist" repairs that maintain high consistency with the original source.
The AdaPatcher Framework
AdaPatcher decouples the repair process into two specialized stages to ensure both precision and minimalism.
Stage I: Bug Localization with Self-Debug Learning
Identifying the exact location of a bug is the first step toward a minimal fix. Static analysis often fails to capture complex logic errors. To address this, AdaPatcher utilizes Self-Debug Learning. We augment the code with "Program Trace Information"—inline comments that detail real-time variable states and execution sequences during a failed test run. This "trace" acts as the model's "eyes," allowing it to pinpoint the root cause of a failure. The output of this stage is a "Code Diff" that explicitly marks the lines requiring modification.
Stage II: Program Modification and Preference Learning
Once the bug is localized, the Program Modifier generates the fix. This stage employs three key strategies:
- Location-Aware Repair: The model focuses its generation efforts strictly on the lines identified in Stage I.
- Hybrid Training: To ensure robustness, the modifier is trained on both ground-truth bug locations and the potentially noisy predictions from the Bug Locator.
- Adaptive Preference Learning: Using a strategy inspired by Direct Preference Optimization (DPO), we train the model to prefer patches that achieve correctness with the fewest changes. If multiple valid patches exist, the model is steered toward the one that maximizes consistency with the original code.
The ACPR Benchmark
To evaluate AdaPR, we introduced the Accuracy-Consistency Program Repair (ACPR) benchmark. Derived from CodeNet, ACPR contains 52,168 triplets: a buggy program, a failed test case, and a correct version written by the same developer. This unique structure provides a natural ground truth for consistency, as the reference solution reflects the original author's style and logic.
Experimental Results
We evaluated AdaPatcher across several LLM backbones, including GPT-4o, Claude-3.5, and CodeLlama-7B.
Performance Gains
AdaPatcher consistently improved both the Code Accuracy Rate (CAR) and the Code Consistency Rate (CCR):
- GPT-4o: The consistency rate improved dramatically from 27.40% to 48.69%, while accuracy also saw a boost from 62.77% to 67.57%.
- CodeLlama-7B: The two-stage approach proved particularly effective for smaller models, more than tripling the accuracy from 10.67% to 33.81%.
Insights
Our analysis confirms that providing execution traces is vital; static code analysis alone is often insufficient for pinpointing logic bugs. Furthermore, the two-stage decoupling prevents the model from being overwhelmed by the entire codebase, allowing for more targeted and less intrusive repairs.
Conclusion and Future Work
AdaPatcher demonstrates that "less is more" in program repair. By combining precise bug localization with preference learning for minimalism, we can generate patches that are both correct and developer-friendly.
Future research will focus on extending this framework to multi-file projects and languages beyond Python. Additionally, we aim to explore the trade-off between architectural improvements and minimalism, ensuring that the drive for consistency does not prevent necessary structural refactoring when a bug is deeply rooted in the code's design.
Summary updated