Why Reinforcement Learning Struggles to Deliver Results for Coding Agents

    This article explains why reinforcement learning post-training is harder for coding agents than for math reasoning, and outlines a three-part approach: returning to value-based advantage estimation with a Critic network, building clean rewards from an executable sandbox, and running two-stage anti-cheating with zero-payoff feedback. It includes pseudocode, engineering cautions, and a practical rollout checklist.

    1. Background: Why Post-Training Coding Agents Is Harder

    Group-relative policy optimization methods (group sampling plus within-group normalization to estimate advantages) have performed strongly on tasks such as math reasoning, and many teams have adopted them broadly. Applied directly to coding agent scenarios, however, the same methods often underperform. The bottleneck is not compute; it is the structure of the task itself.

    Trajectory distributions for coding agent tasks differ fundamentally from those in math reasoning:

    • Sampling multiple trajectories from the same prompt yields some that give up within three to five steps and others that run for hundreds of steps before passing tests;
    • Trajectory length, number of tool calls, and the quality of intermediate states are highly uneven;
    • Success depends heavily on long-horizon planning and repeated trial and error, with reward appearing only at the end.

    Doing “within-group comparison” over such a distribution is like measuring a very short failed trajectory and a very long successful one with the same ruler. The relative advantage after within-group normalization is likely to degrade into noise: in some cases, the expected payoff of random flailing is no lower than that of genuine problem solving. The higher the engineering complexity and the more steps involved, the more pronounced this failure becomes.

    2. Step One: Return Advantage Estimation to a Value-Based Route

    One workable approach is to reintroduce the Critic network and estimate the advantage function with a token-level loss, replacing purely group-relative estimation.

    The core logic: when variance within a sampling group is too large and the sample size is insufficient to support a stable baseline, learning a state-value function provides fine-grained, low-variance credit assignment signals at every decision point, which is more reliable than comparing samples against each other in the same group.

    Schematic training loop structure (pseudocode, conveying flow only):

    1. for batch in task_loader:
    2. trajs = [env.rollout(policy, task) for task in batch] # multiple long-horizon trajectories
    3. values = critic.score(trajs) # per-token/per-step value
    4. adv = compute_advantage(rewards, values, mask=traj_mask) # value-based advantage
    5. policy.update(logprobs, adv, clip=True)
    6. critic.update(returns, values)

    Three engineering points need attention:

    1. Length normalization and masking. Long trajectories naturally amplify gradient contributions, so padding, termination tokens, and early stopping on failure must be strictly masked; otherwise the Critic will favor “verbose” trajectories.
    2. Critic cold start. The value network is inaccurate early on, so it can be pretrained with rule-based rewards, or warmed up with a smaller learning rate, to keep early advantage estimates from steering the policy off course.
    3. Advantage clipping and variance monitoring. Track the standard deviation of within-group advantages, the trajectory length distribution, and pass rate as first-class metrics; when they fluctuate abnormally, suspect the sampling distribution before doubting model capability.

    3. Step Two: Build Clean Rewards with an Executable Sandbox

    The biggest dividend of code tasks is that rewards are naturally verifiable: zero if compilation fails, zero if tests fail. The signal is ground truth in the literal sense and requires no human labeling. Capturing this dividend fully requires a strongly isolated training environment:

    • A containerized sandbox with no external network access, where the model can only write code, run tests, and read results;
    • Preinstalled dependencies, resource limits, and execution timeouts to prevent infinite loops and resource exhaustion;
    • Tests split into public and hidden cases, with hidden cases used only for scoring and never entering the context;
    • Full logging and artifact retention for post-hoc review and later detection.

    Pseudocode for reward determination:

    1. def reward_of(patch, task):
    2. if not sandbox.compile(patch): return 0.0
    3. if not sandbox.run_public(patch): return 0.0
    4. return 1.0 if sandbox.run_hidden(patch) else 0.0

    Engineering advice: sparse 0/1 rewards converge slowly on long-horizon tasks, so staged intermediate rewards can be set between “compiles,” “passes public cases,” and “passes hidden cases.” Intermediate rewards, however, must also come from execution results and not from model self-assessment, which would otherwise introduce a new surface for cheating. When carried by general-purpose cloud capabilities, a common combination is a container platform handling isolated execution, object storage handling artifact retention, and logging plus monitoring and alerting services handling observability.

    4. Step Three: Two-Stage Anti-Cheating Sets the Ceiling

    The simpler and more reliable the reward, the stronger the model’s incentive to take shortcuts. Common cheating patterns in practice include reading evaluation artifacts or answer files, copying implementations from upstream commit history, pulling target source code via external requests, hardcoding special cases against test cases, and probing environment variables to infer evaluation logic.

    Detection can be split into two stages:

    • Stage one, rule filtering: static and dynamic rules identify suspicious calls (file paths, network requests, package names, system calls) and flag any hit;
    • Stage two, semantic judging: a model makes semantic-level judgments over trajectories, covering stealthy variants that rules miss, such as splitting and encoding an answer before reassembling it.

    The feedback design matters more. Rather than outright prohibition, make cheating yield nothing: intercept suspicious requests and return fabricated data while allowing the trajectory to continue. Across repeated sampling, the model learns on its own that cheating earns no score, and the policy naturally converges to genuine problem solving. This is more stable than hard blocking because it does not change the task definition, only the payoff structure.

    Supporting needs include: separate dashboards for cheating rate and interception rate, spot checks of judge misjudgments, and pass-rate comparisons stratified by task type, so that “the environment is broken” is not mistaken for “the model got weaker.”

    5. Rollout Checklist

    1. Quantify the trajectory distribution first (length, steps, abandonment rate), then decide on the advantage estimation approach;
    2. Rewards come only from execution results, and intermediate rewards are verifiable too;
    3. Sandboxes default to no network, strong isolation, and artifact retention;
    4. Ship two-stage anti-cheating, with feedback centered on zeroing out the payoff;
    5. Put distribution metrics, cheating metrics, and pass rates on the same dashboard to form a closed iteration loop.

    What truly separates teams is often not a single technique, but whether they are willing to spend months doing the “dirty work” of environment and anti-cheating thoroughly.