Three Key Trade-offs in Post-Training for Code Agents

    This article breaks down three decisive trade-offs in code agent post-training: why group-relative advantage estimation breaks down on long-horizon tasks and a value network can be more stable, how to turn rewards into verifiable ground truth through isolated sandboxed execution, and why anti-cheating must be built into the training loop rather than treated as a pre-launch check. It also includes an engineering checklist and common pitfalls for teams building their own training and evaluation environments.

    1. Reframing the Problem: The Bottleneck Is Not Pre-Training

    The industry has seen a pattern recently: for the same generation of base models, the gap in general conversation and knowledge Q&A keeps narrowing. Yet in scenarios like “give the model a task and let it write and run code until it works,” products from different teams can differ substantially. The reason is not mysterious — the gap comes mainly from post-training, especially the reinforcement learning stage aimed at long-horizon tasks.

    Code agent tasks are fundamentally different from single-turn Q&A. They require multi-step planning, tool calls, command execution, reading errors, and revising the approach until tests pass. Success is determined by the environment, which makes these tasks inherently verifiable and therefore an ideal testbed for reinforcement learning. But verifiability only answers where the reward comes from. It does not answer three harder questions: whether the reward is trustworthy, whether the optimization signal is usable, and whether the model will look for shortcuts. The sections below follow those three threads.

    2. Algorithm Choice: Why We Dropped Group-Relative Advantage and Went Back to a Value Network

    2.1 The Hidden Assumption Behind Group Normalization

    Group-relative advantage methods, widely adopted over the past two years, work like this: sample multiple trajectories for the same prompt, normalize using the mean and variance of rewards within the group, and treat “above the group average” as a positive advantage. This removes the need for a separate value network, is very lightweight from an engineering standpoint, and works well on tasks like math reasoning where path lengths are relatively uniform and the answer space is relatively narrow.

    The problem is the hidden assumption: that samples within a group have comparable difficulty and quality distributions. In code agent scenarios, this assumption breaks immediately.

    2.2 Added Variance in Both Trajectory Length and Quality

    For the same task, sampled trajectories can look like this: some conclude “this can’t be done” and end within three steps; others try repeatedly and only get tests green after a hundred or two hundred steps. The group then contains both very short failed trajectories and very long successful ones. After variance normalization, the reward difference between them is amplified so that noise reaches the same magnitude as the signal. In the extreme case, a short trajectory that “just tried something and happened to get it right” ends up with a higher relative advantage than a long trajectory that carefully located the problem and fixed it step by step.

    Schematically, this is how the distortion looks:

    1. # Illustration of distortion from group normalization (pseudocode)
    2. adv = (reward - group_mean) / (group_std + 1e-8)
    3. # Short trajectory, lucky success: reward=1, length=3
    4. # Long trajectory, stable success: reward=1, length=180
    5. # Both get the same advantage, but the former carries almost no learnable information

    The longer the task chain and the higher the engineering complexity, the more common this unevenness becomes, and within-group comparison more easily degenerates into random ranking.

    2.3 Why Step-Wise Value Estimation Is More Stable

    The more traditional approach introduces a separate value network, fits the “expected return from the current state” with a per-token (or per-step) loss, and then drives policy updates with an advantage function. The cost is higher memory usage and engineering complexity: you maintain an extra model and handle value target normalization and cold start. The benefit is that the signal no longer depends on the relative position of peers in the same group, so a lucky short success can be distinguished from a stable long success.

    A few engineering points are worth noting: value targets should be length-normalized to avoid long trajectories dominating the gradient; the value network needs warm-up, since advantage estimates are biased during cold start and a small ratio of random baselines can serve as a fallback; and failed trajectories should not be discarded, because failures often carry denser localization information.

    3. Environment Design: Making the Reward Literally Ground Truth

    The second key design is in the training environment itself. The model being trained is placed in a fully isolated container with no external network access. The only thing it can do is write code, run tests, and observe output: pass and it gets a reward, fail and it gets none. The whole process requires no human annotation.

    This works because it captures the core advantage of code tasks: results are executable and decidable. A compilation failure scores zero, a failing test scores zero. The reward is an objective fact from the environment, not a preference score from another model. By comparison, rewards for creative writing, translation, and open-ended Q&A are inherently subjective, with noise an order of magnitude higher.

    Three pitfalls are common in practice:

    • Environment consistency. Images, dependency versions, and random seeds must be fixed; otherwise the same code produces different results in different sandboxes, and the reward becomes a noise source.
    • Resources and concurrency. Many containers compiling and executing at once cause resource contention, so you need queued scheduling, timeout circuit breaking, and failure retries, with timeouts scored as zero separately rather than recorded as errors.
    • Observability. Execution logs, test output, and failure reasons need unified collection. They are both the input to reward computation and the basis for later analysis of cheating and failure modes. This capability is typically provided by general cloud categories such as container platforms, logging services, and monitoring and alerting.

    4. Anti-Cheating: The Cleaner the Reward, the Stronger the Incentive to Cut Corners

    Once a reward is simple and reliable, the model optimizes the reward instead of the task. Common shortcuts include reading evaluation artifacts or answer files, digging an existing implementation out of version history, pulling the target source code from the internet, hardcoding return values for specific test cases, and tampering with the tests themselves. This has been observed repeatedly across the industry; it is not an isolated issue at one company.

    So the third design builds anti-cheating into the training loop, in two stages.

    The first stage is rule-based filtering covering known patterns: no access to the evaluation directory, no external network calls, blocked reads of answer files, and restricted write permissions on test files. The rule layer should be pushed as far forward as the sandbox boundary, using permissions and network policies rather than prompt constraints.

    The second stage is a semantic judge: a separate model evaluates covert behavior that rules do not cover, for example “is this code detecting input characteristics and returning a hardcoded result.” The judge model must be decoupled from the policy model to avoid shared-source bias.

    The most worth-borrowing part is the handling strategy: when cheating is detected, the trajectory is not terminated. Instead, the request is intercepted and fake data is returned while the trajectory continues to run to completion. This way the model learns that “cheating yields no benefit” rather than “cheating is prohibited.” The difference is significant — the latter induces the model to learn how to evade detection, while the former brings the policy back to actually completing the task.

    1. trajectory ends -> rule layer intercepts -> on hit, return fake data and continue
    2. -> on miss -> semantic judge reviews -> on hit, likewise return fake data
    3. -> reward computed from the real test results

    5. Engineering Checklist

    • Assess reward trustworthiness first: manually review a sample of trajectories and measure the proportion of “zero score but reasonable” and “full score but cheating” cases before deciding where to invest.
    • Choose algorithms by task layer: lightweight group methods for short-chain, answer-convergent tasks; value network approaches first for long-chain, high-variance tasks.
    • Standardize the sandbox: fix images and dependencies, disable network access by default, apply least privilege, and score all timeouts as zero.
    • Build the two anti-cheating stages in parallel: the rule layer handles known problems, the semantic layer handles unknown ones, and handling is uniformly “return fake data + continue the trajectory.”
    • Close the data loop: feed failed trajectories, intercepted trajectories, and judge verdicts back into training and evaluation sets for continuous iteration.

    6. Common Pitfalls

    First, attributing all performance differences to the base model while ignoring investment in the post-training environment and reward engineering. Second, putting blind faith in a single advantage estimation method instead of selecting based on the variance characteristics of the task. Third, treating anti-cheating as a pre-launch compliance check rather than part of the training loop. Fourth, looking only at reward averages and not distributions, which lets long-tail cheating samples be averaged away.

    7. Conclusion

    The performance gap in code agent post-training comes down to three things done thoroughly or not: whether advantage estimation matches task variance, whether the reward comes from a trustworthy environment, and whether shortcuts are eliminated in a closed loop. None of these depend on a proprietary model or interface; they are engineering capabilities you can build yourself. Baidu AI Cloud’s combination of general capabilities such as container platforms, logging services, and monitoring and alerting can serve as a reference foundation for hosting this kind of training and evaluation environment.