Long-Horizon Coding Agent Training: From Outcome Rewards to Step-Level Credit Assignment

    This article explains why long-horizon coding agent tasks are difficult to train with outcome-only rewards and how step-level credit assignment can address sparse feedback and misattributed credit. It covers process reward design, adaptive credit redistribution, sandbox scaling requirements, cybersecurity evaluation principles, and practical engineering recommendations for building more reliable training and evaluation pipelines.

    1. Why Long-Horizon Tasks Are Hard

    A coding agent typically needs 30 to 80 interaction rounds to complete a real task: reading the codebase, locating the problem, modifying files, running tests, and adjusting based on feedback. The longer the trajectory, the more the final result depends on a small number of key decisions.

    Consider a typical scenario. The agent spends the first 70-plus steps correctly understanding the architecture and narrowing down the fault, but at step 79 it writes an incorrect variable name while modifying code, and the unit test at step 80 fails. The environment returns only a final score of 0. That result obscures the quality of most of the preceding work.

    2. The Cost of Outcome Rewards

    In common policy gradient methods such as PPO and GRPO, a trajectory-level reward is assigned to every action in that trajectory. When the result is 0, the correct exploration in the first 78 steps is also downweighted. This approach creates three problems.

    1. Sparse signal: A long trajectory receives feedback only once at the end, so the gradient signal is weak and noisy.
    2. Credit misassignment: The algorithm cannot distinguish effective exploration from a mistake.
    3. Behavior degradation: The model may learn risk-averse policies, such as taking fewer actions, trying fewer options, or becoming overly conservative. In some cases, it may repeatedly validate or pile up ineffective operations, increasing token consumption.

    3. The Basic Idea of Step-Level Scoring

    The intuitive solution is to assign a process reward to each step. Before putting this into practice, three questions need to be answered.

    What is the scoring basis? Verifiable intermediate signals can be used, such as whether static checks pass, local results of relevant tests, whether localization hits the real file, and whether modified code passes syntax and type checks. These signals can all be produced automatically by the environment without human judgment.

    Who performs the scoring? Human annotation is costly and difficult to cover long-tail tasks. Training a process reward model can expand coverage, but it introduces bias and requires ongoing validation.

    How can gaming be prevented? If the reward looks only at intermediate metrics, the model may learn to game the score, for example by repeatedly running meaningless checks to obtain positive feedback. Therefore, process signals should be tied to the final outcome. Their role is to reallocate credit, not to replace the final result.

    4. A General Approach to Adaptive Optimization

    The core of adaptive optimization is to estimate each step’s contribution to the final result and redistribute the terminal reward according to contribution rather than evenly. Common approaches fall into two categories.

    • Value estimation: Use a value network to score each state and use temporal-difference error to measure the local change introduced by a step.
    • Counterfactual comparison: Sample multiple candidate actions in the same state and compare differences in subsequent outcomes to determine which action is more favorable.

    Below is illustrative pseudocode that shows the idea rather than a complete implementation:

    1. # Illustrative: reassign advantages by per-step contribution
    2. def reassign_advantages(trajectory, final_reward, gamma=0.99):
    3. # 1. Estimate value for each state
    4. values = [critic(s) for s in trajectory.states]
    5. # 2. Use temporal-difference error as local credit for that step
    6. deltas = []
    7. for t, r_t in enumerate(trajectory.step_rewards):
    8. next_v = values[t + 1] if t + 1 < len(values) else 0.0
    9. deltas.append(r_t + gamma * next_v - values[t])
    10. # 3. Combine terminal reward with local credit and normalize as per-step advantages
    11. return normalize(deltas, anchor=final_reward)

    The key design principle is that good actions receive more positive updates, while erroneous steps are still penalized. However, the penalty is concentrated at the position that caused the failure rather than applied to every token in the entire trajectory.

    5. Scaling Sandbox Environments

    Training effectiveness depends on both task diversity and environment quality. Increasing the number of environments does not necessarily produce linear capability gains; the distribution of task difficulty also matters. When scaling, the following four requirements should be prioritized.

    • Reproducibility: Each environment should fix dependency versions, the initial code snapshot, and test cases.
    • Verifiability: Rewards should come from deterministic checks, such as whether tests pass or whether output matches expectations, reducing subjective judgment.
    • Isolation: Each sandbox should have an independent file system and network policy, with compute resources limited.
    • Decontamination: Training tasks and evaluation tasks should be strictly separated to prevent the model from memorizing answers instead of genuinely solving problems.

    The performance of coding agents in vulnerability analysis, remediation, and security testing should be understood and evaluated from a defensive perspective.

    • Evaluation should be conducted in controlled environments, targeting purpose-built test ranges or authorized test objects.
    • The focus should be on the ability to fix vulnerabilities, write security tests, and audit code, not on producing details that can be directly used for attacks.
    • When unexpected capabilities appear, they should be recorded, reproduced, and risk-classified, while coordinating with security teams to determine whether additional access controls are needed.

    7. Engineering Recommendations

    1. Start with outcome rewards to establish a stable baseline, then introduce process signals gradually.
    2. Begin with small process reward weights and regularly check whether they trigger reward gaming.
    3. Monitoring should include not only pass rate but also average interaction rounds, invalid operation ratio, and overconservative refusal rate.
    4. Regularly manually sample trajectories to confirm that credit assignment results match intuition.
    5. Fix the task set and random seeds during evaluation so that results across different versions are comparable.