How Reinforcement Learning Breaks the Long-Horizon Bottleneck in Code Models
This article explains why code models can score far higher after reinforcement learning post-training without any base model change, and what that gap actually reveals. It covers reward design that separates correctness from token, tool-call, and wall-time cost, length-weighted baselines that remove trajectory-length bias in advantage estimation, and the rollout length oscillation that stalls long-horizon training along with practical damping techniques. It also identifies three sources of incomparable benchmark scores and closes with a practical checklist for building reproducible evaluation and stable long-horizon training.
1. The Phenomenon: Same Base Model, Double the Score
A pattern shows up often in this field: a code model, post-trained with reinforcement learning on top of an unchanged base, jumps well past that base on several coding benchmarks, sometimes even past later releases in the same family. It is tempting to read this as “the base model was weak,” but from an engineering standpoint the more reasonable explanation is this: the base model sets the capability ceiling; reinforcement learning decides whether that capability can be triggered reliably.
Code tasks make this especially clear. Base models usually already have syntax knowledge, familiarity with common libraries, and basic reasoning. In a real repository, though, the job is a long chain of behavior: understand the request, locate files, edit across files, run tests, and back off and retry based on errors. Most failures on that chain are not “cannot write code” but unstable policy: no self-correction after drifting off course, conclusions delivered too early, and large numbers of steps burned on irrelevant files. The value of reinforcement learning is compressing that latent capability into a policy that is more stable and cheaper to run.
2. Reward Design: Decomposing “Correct” into Optimizable Signals
The naive approach uses test pass/fail as the only reward, and in practice two problems appear. First, it is sparse: in long tasks, most intermediate steps get no signal. Second, the policy learns to “retry indefinitely,” trading step count for success rate. A more mature approach adds a cost term to the reward:
# Illustrative: single-step reward with cost penaltiesreward = correctness_gain \- alpha * token_cost \- beta * tool_call_cost \- gamma * wall_time_cost
Here correctness_gain can be a terminal reward from passing tests, or a shaped reward for effective intermediate progress. What matters is calibrating the coefficients. Penalties that are too heavy make the policy conservative, willing only to make “small patches” and unable to complete cross-module refactors. Penalties that are too light degrade the search into brute force. In engineering practice, curriculum-style scheduling is common: weaken the cost terms early in training and increase them gradually later, so the policy first learns to get it done, then learns to do it cheaply.
3. Baselines and Advantage Estimation: Why Length Weighting Works
In policy gradient methods, most of the variance comes from advantage estimation. In code tasks, trajectory length varies enormously across samples: some tasks finish in three steps, others need dozens of tool calls. A single global mean as the baseline gives long trajectories a naturally larger return, which introduces systematic bias and pushes the policy toward stretching tasks out.
Length-weighted baselines match the reference to the trajectory scale:
# Illustrative: length-bucketed baselinebaseline = {bucket: mean_return_of_bucketfor bucket, group in group_by_length(trajectories)}advantage = return_i - baseline[length_bucket(i)]
Bucket granularity needs a balance: coarse buckets leave residual bias, while overly fine buckets leave too few samples per bucket and a noisy baseline. In practice this is combined with in-group normalization, advantage clipping, and gradient accumulation to push variance down further.
4. The Core Difficulty in Long-Horizon Training: Rollout Length Oscillation
The most typical instability in long-horizon code training is rollout length swinging back and forth. At one step the policy learns to try a few more times and average length rises; the cost penalty immediately pushes it back and length drops sharply; the policy then rediscovers that trying less fails, and length climbs again. This oscillation makes the training curve look converged while it is actually being pulled in both directions, and the final metric stalls.
Available damping measures include:
- Soft penalties on length: apply a smooth penalty to trajectories beyond a given quantile instead of a hard cutoff, avoiding the signal loss caused by discarding many samples.
- Reference policy constraints: limit how far the new policy can drift, preventing a single batch update from overturning learned long-horizon behavior wholesale.
- Advantage smoothing and update throttling: clip unusually large advantage values to reduce the impact of extreme samples in a single batch.
- Stratified sampling: sample by task complexity and estimated step count so the proportion of long tasks in each batch stays stable.
- Reproducible execution environments: containerized sandboxes, pre-installed dependencies, and restricted network access reduce environment jitter being mistaken for policy noise.
5. Do Not Ignore the Evaluation Setup: Three Sources of Incomparable Scores
When you see a conclusion like “doubled,” confirm three things first, or the conclusion does not hold.
- Benchmark and training share the same lineage. When the benchmark and the training method come from the same system, the scoring rules are inherently more familiar, and comparability drops.
- The strictness of the scoring standard has changed. Newer benchmarks often move from “unit tests pass” to a maintainer’s veto: fixing only the specified path without handling the same class of problem, breaking compatibility with input types the repository already supports, or omitting necessary documentation can all score zero outright. Under such standards, models without targeted adaptation generally lose points.
- Execution framework differences. The same model can differ by several percentage points in pass rate across agent frameworks, because the framework determines the toolset, how context is organized, the failure retry strategy, and the prompt templates. Comparing scores across frameworks is essentially comparing two systems.
The more credible approach is therefore a longitudinal comparison with the framework and scoring version fixed, while keeping at least one general third-party setup as a horizontal reference.
6. A Practical Checklist
- Build a stable evaluation pipeline first: pin dependency versions, fix random seeds, and log the full trajectory of every run along with a classification of failure causes.
- Keep reward design simple before making it complex: get it working with a terminal reward, then add cost terms, and only then consider process shaping.
- Treat length oscillation as a first-class monitoring metric: track length quantiles, truncation rate, and retry rate, and read them alongside pass rate.
- On the data side, stratify by difficulty and deduplicate, so a large volume of homogeneous easy samples does not dilute the long-horizon learning signal.
- Keep training and inference frameworks decoupled as much as possible, and validate model-side changes separately from framework-side optimizations to make attribution easier.
- Before rollout, run a small-sample manual review on real repository tasks; leaderboard scores cannot cover implicit requirements such as maintainability and control over the scope of changes.
7. Conclusion
“Same base model, double the score” is not mysterious. It usually comes from the combination of reward shaping, baseline correction, and training stability engineering, not from a sudden jump in base capability. Likewise, huge gaps across leaderboards are often caused by scoring rules and execution frameworks rather than model capability itself. For engineering teams, what is worth investing in is a reproducible evaluation system and a stable long-horizon training method. When elastic compute and unified operations are needed, a cloud platform’s general capabilities, such as container services, object storage, log services, and monitoring and alerting, can serve as the foundation for training and evaluation, leaving attention for the algorithms and the data themselves.
