Designing Reward Signals for AI Agents Fixing Bugs: Beyond Final Test Results
Training AI agents to fix software bugs raises a critical question: how should reward signals be designed when tasks span dozens or hundreds of steps? This article examines why outcome-only rewards teach agents bad habits, explores the granularity dilemma of process rewards, and presents a practical stage-based reward design along with engineering implementation details and evaluation metrics for building effective RL training pipelines for bug-fixing agents.
Designing Reward Signals for AI Agents Fixing Bugs
1. The Starting Problem: Correct Results Don’t Mean Efficient Processes
Imagine an AI agent fixing a code defect. The whole process takes twenty minutes. For roughly fifteen of those minutes, it repeatedly runs the same test command without gaining any new information; only in the last five minutes does it locate the actual problem and complete the fix. If the training pipeline only checks whether the final patch passes the tests, this trajectory receives a full score—including those fifteen minutes of wasted actions. What the model learns from training is: “as long as it passes in the end, the process doesn’t matter.”
This example is a hypothetical scenario constructed to illustrate the problem, not measured data from an actual experiment. But it reveals a real training challenge: when a task requires dozens or even hundreds of steps, how reward signals are distributed across each step directly determines the behavioral patterns the model learns.
2. Outcome-Only Rewards: Simple and Effective, but Too Coarse
Reinforcement learning with verifiable rewards (RLVR) is a common approach for training reasoning models. Its strength lies in objective judgment: whether a math answer is correct or whether code passes tests can be determined automatically by programs, with no human annotation required. On tasks with short step counts, this outcome-oriented approach has already achieved good results.
Bug fixing is different. A single fix may involve reading code, searching call relationships, reproducing the problem, writing a patch, and running tests—each of which can involve detours or redundant work. An outcome-based reward can only tell the model “this trajectory succeeded or failed overall”; it cannot answer “which step was valuable.” This is the credit assignment problem: a sparse final signal must be reasonably distributed across dozens of preceding actions, and naive approaches tend to distribute credit and blame together.
In this situation, the model may learn two types of bad habits. First, wasting steps on unnecessary repeated operations, because those steps happened to appear in successful trajectories. Second, locking in accidentally successful detours as correct strategies, which harms the model’s generalization to new tasks.
3. The Difficulty of Process Rewards: The Granularity Dilemma
To address overly coarse signals, researchers typically introduce process rewards—scoring intermediate steps as well. The difficulty lies in choosing the right granularity.
If the granularity is too fine—for example, scoring every single tool call separately—two problems arise. First, many actions cannot be judged as good or bad on their own. Reading a file or listing a directory is information gathering; it only has meaning in the context of the overall task. Second, step-by-step scoring invites reward hacking. The model may learn to perform actions that boost local scores, such as repeatedly running tests to inflate pass counts, rather than genuinely progressing toward solving the problem.
If the granularity is too coarse—looking only at the final result—we return to the problem described in Section 2.
Therefore, a suitable process reward needs to find a balance between the two: reflecting progress without over-interpreting individual actions.
4. A More Robust Approach: Scoring by Stage, Not by Action
One feasible approach is to split the trajectory into several semantically well-defined stages, such as:
- Problem localization: finding the code location related to the defect;
- Problem reproduction: constructing a minimal set of steps that reliably triggers the defect;
- Patch: submitting a fix that addresses the root cause of the defect;
- Verification: running the relevant tests and confirming that no regressions were introduced.
The reward for each stage is computed based on whether the stage’s goal was achieved, rather than on the quality of any single command. This way, information-gathering actions like reading files are grouped into the “localization” stage, and their value is reflected in the stage outcome—no separate judgment is needed.
At the same time, final correctness still serves as a gate. If the patch fails verification, stage rewards are not counted or are heavily discounted. This prevents the model from completing seemingly progressive stages without actually solving the problem.
Below is an illustrative reward function, intended only to demonstrate the idea—it is not an actual implementation:
def trajectory_reward(traj, verifier):# Final correctness as a gateif not verifier.passes_all_tests(traj.final_patch):return 0.0# Accumulate scores based on stage completionstage_score = 0.0for stage in ["locate", "reproduce", "patch", "verify"]:if traj.stage_completed(stage):stage_score += STAGE_WEIGHTS[stage]# Penalize repeated identical actionsrepeat_penalty = 0.05 * traj.count_repeated_actions()return max(0.0, stage_score - repeat_penalty)
This code embodies three principles: a correctness gate, stage-based scoring, and a repetition penalty. The specific stage definitions, weight values, and penalty coefficients need to be iteratively tuned based on task type and experimental data—there is no one-size-fits-all set of numbers.
5. Key Engineering Implementation Details
- Log trajectories completely. The action type, parameter summary, and returned result of each step should be saved, making it possible to analyze afterward whether the rewards were reasonable.
- Normalize before detecting repetition. The same command with different parameter ordering or whitespace should still be recognized as a duplicate; otherwise the penalty is easy to bypass. Commands can be standardized first, then hashed for comparison.
- Isolate and tamper-proof the verification environment. If the model can modify test files or read the expected outputs of hidden tests, reward exploitation will occur. Test suites should be placed where the model has no write access, and patches should be regularly checked to ensure they don’t modify the tests themselves.
- Make stage-completion criteria auditable. Judgments about stage completion are best grounded in explicit evidence—for example, “the reproduction script successfully triggered the exception” or “the target function was modified”—rather than vague subjective assessment.
- Normalize reward scales. Different tasks vary greatly in step count and difficulty; directly comparing absolute scores lets long tasks dominate. Normalization can be done against a task’s baseline step count or difficulty level.
6. Evaluation Metrics: Pass Rate Alone Is Not Enough
Reporting only the fix success rate reveals nothing about process quality. It is recommended to also track the following metrics:
- Success rate: the proportion of final patches that pass verification;
- Average step count: the number of operations used in successful cases, compared against baseline approaches;
- Proportion of wasted actions: the share of actions judged to be repeated or contributing no incremental information;
- Stage completion rate: the attainment of each stage, used to identify weak points in training.
These metrics need to be used together. If the success rate rises while the proportion of wasted actions also rises, it usually indicates the model is exploiting reward loopholes, and the reward design should be reviewed promptly.
7. Practical Recommendations
- Validate the reward function on simple tasks first—confirm that it can distinguish efficient success from inefficient success—before extending it to complex tasks.
- Manually review a sample of trajectories after training. Compared with looking at metrics alone, manual review makes it easier to spot reward hacking.
- Keep version records of the reward function. Every change to weights or rules should be traceable to its effect on training outcomes.
- For security-sensitive code changes, keep a human review step in place; do not treat automated rewards as equivalent to approval for production deployment.
Process rewards are not better the finer they are. Good reward design teaches the model to make progress on solving the problem—and prevents it from learning to please the scoring rules.
