Manuscript Offprint

arXiv:2609.04678

Train What You Deploy:Token-Faithful Post-Training of a Production Coding

Cheng Li, Jiexiong Liu, Yixuan Chen, Chi Hong

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Existing post-training pipelines for coding and terminal agents suffer severe token and control fidelity errors: simplified training environments mismatch production deployments, and offline token reconstruction from agent logs distorts original prompts and conflates policy calls with background model operations. We present a fidelity-aware training coupling framework that retains trainer-side sampling over original prompts, eliminates spurious model calls via a negotiated training protocol, and restricts loss computation to verifiable token spans with closed-failure guarantees. We further propose Certified Divergence Proximal Policy Optimization (C-DPPO), which establishes tight two-sided TV certification bounds, adaptive-KK rules, budget-aware sequence guarantees, and error-robust policy masking atop standard DPPO. Evaluated on matched Baize5B and Baize10B models with identical training and test protocols on TMax-100, C-DPPO yields a consistent +3.0-point performance gain over standard DPPO across model scales. Certificate audits validate the reliability and full operational coverage of our certified training pipeline.

1 Introduction

Reinforcement learning with verifiable outcomes has become the dominant recipe for turning a large language model into a capable coding or terminal agent [1, 5, 6]: the model drives a sandboxed environment with tools until tests or graders certify the result, and the resulting success signal optimizes the policy. Crucially, in both training and deployment the model never acts alone; it acts through a scaffold—an agent harness that owns the system prompt, tool schemas, context management, retries, and sub-agent orchestration. A post-training pipeline is therefore faithful only if the trajectories it learns from were produced by the same scaffold code and a declared configuration whose remaining deployment gap is measured rather than hidden. This paper addresses the technical problem of how to post-train the policy inside a production-grade agent scaffold without losing training-grade fidelity.

Existing approaches split into two families, each failing this requirement in a distinct way. The first family reimplements a minimal agent loop inside the training framework: a compact system prompt, a handful of tools, and a hand-written rollout function [12–14], sometimes deliberately simplified for generality [23]. This gives the trainer exact control over tokens, but the policy is optimized for a scaffold it will never see in production. The mismatch is not cosmetic: production harnesses such as KCoder rewrite history through context compaction, inject skill/memory blocks, re-serialize tool schemas, and recover from tool errors in ways the simplified loop does not model, so the learned behavior is bound to the wrong conditional distribution.

The second family keeps the production agent and inserts a model proxy: the agent's HTTP traffic is intercepted, logged, and converted into training data by replaying its conversation history [2]. This preserves the deployment-time scaffold, but it silently breaks two properties we identify as load-bearing. Token fidelity breaks because the proxy learns from prompts that the agent re-renders and the tokenizer re-segments after the fact; sampled continuations are attributed to inputs the policy did not condition on, and hidden reasoning channels can be dropped or re-encoded even when the visible message text is reconstructed faithfully. We quantify that failure at study scale rather than relying on a single historical example (§6.1). Control fidelity breaks because an industrial agent is not a pure policy loop: it issues compaction summaries, memory writes, session-model updates, and sub-agent forks—all of which appear in a passive log as if they were the policy speaking. Neither defect is visible in reward curves; both inject bias into the gradient that compounds over long training runs.

A second gap appears once rollout log-probabilities are used to stabilize those long runs. DPPO replaces PPO's sampled-ratio heuristic with Binary or Top-K approximations to a distributional trust region [21], but its approximation theorem is one-sided: coarsening lower-bounds the true divergence. Consequently, a small Binary statistic does not by itself certify that the full token distribution is close, fixed per-token thresholds do not expose a trajectory-wide divergence budget, and numerical train/serve log-probability disagreement is not represented in the mask. These limitations are especially visible in a production agent, where a response can span many model calls and hundreds of thousands of conditioning tokens.

Our insight is that fidelity cannot be assumed from a logged transcript; it must be negotiated and verified at every turn. We therefore design a token-faithful coupling between KCoder, a production terminal-coding agent implemented in Rust, and slime, a Megatron-SGLang post-training framework [2, 18, 19]. Three mechanisms implement the insight. First, sampling ownership: the coupling terminates the agent's model calls inside the serving adapter, which re-samples every assistant turn from the exact rendered prompt with the training engine's own token ids and log-probabilities (input_ids with return_logprob), pinned to one engine by session-as-key routing. Second, declared deviations: the agent runs in a hardened training mode that provably eliminates all non-policy model calls (a regression contract requires exactly two provider requests across a tool round-trip), and it declares the remaining protocol deviations to the adapter via negotiation headers for sub-agent depth and reasoning replay. Third, verified, fail-closed attribution: a trajectory linearizer rebuilds each session's message tree into training sequences, classifies re-tokenization drift on every link (CLEAN/REALIGN/FORK), assigns loss mask 1 only to spans that are provably the sampled continuation, and rejects requests it cannot attribute.

That observation contract enables Certified Divergence Proximal Policy Optimization (C-DPPO), our theoretical certification framework for standard DPPO. We first derive the exact Binary/Top-K TV coarsening gap and a tight upper error based only on the untracked tail masses, yielding an adaptive-K rule that terminates as certified inside, certified outside, or fail-closed unresolved. We then allocate a total divergence budget across a long response through predictable, adaptive thresholds δt\delta_t and obtain additive and product-form sequence bounds. Finally, bounded log-probability errors are propagated through the exponential into divergence intervals; the resulting robust mask retains a token only if it is certified inside the trust region or certified to move back toward the behavior policy (§5).

We validate the complete pipeline end to end: fresh KCoder processes run inside micro-VM sandboxes on the open TMax-15K terminal-environment corpus [1], rewards come from hidden verifiers that the agent cannot observe, and every session is converted into Megatron training batches with hot SGLang weight synchronization after each step. The empirical study uses matched Baize5B and Baize10B pairs: at each scale, Standard Binary-TV DPPO and C-DPPO start from the same checkpoint, follow the same task schedule for 350 steps, and are compared through average training-reward curves and final-checkpoint evaluation on the same frozen TMax-100 set. A four-item certificate audit checks record completeness, adaptive-K resolution, mask disagreement, and observed sequence-budget feasibility. The completed matched pairs yield final TMax-100 scores of 37/100 and 44/100 (C-DPPO) against 34/100 and 41/100 (Standard DPPO)—a direction-consistent +3.0-point difference across scales—with the four-item audit reporting complete records (99.4%/99.1%), mostly-resolved certificates (86.2%/84.7%), small mask disagreement (4.7%/5.9%), and observed budget feasibility (91.3%/89.6%).

In summary, this paper makes the following contributions:

  • The C-DPPO certification framework: a theoretical extension of standard DPPO with a tight two-sided Binary/Top-K TV error bound and adaptive KK, a sequence-level divergence budget with adaptive δt\delta_t, and a robust directional rule under bounded log-probability error (§5, Appendix A).

  • A negotiated, token- and control-faithful coupling between a production coding agent—no training-only fork of it—and an RL post-training framework, in which the serving path gives the trainer ownership of every sampled token while the agent's session and role travel with each request as explicit, checked metadata (§4.1).

  • Depth-gated provenance and a hardened agent-side contract: a machine-checked zero-background-request training mode in the production agent, plus provenance and reasoning-replay negotiation headers that extend the framework's drift-classifying message-tree linearizer [2] to multi-agent sessions with provably-sampled loss masks (§4.1, §4.2).

  • A concise end-to-end validation protocol comparing 350-step reward dynamics and final TMax-100 outcomes for one matched Standard-DPPO/C-DPPO pair, with a compact certificate sanity check (§3, §6).

References (21)