LEGO-RL: Harness-Native Reinforcement Learning for Coding Agents

Yiming Du, Yuxin Jiang, Tao Yuan, Jianbo Dai, Shaowei Wang, Jierun Chen, Chaofan Tao, Xianzhi Yu, Lifeng Shang, Kam-Fai Wong, Xiaohui Li, Haoli BaiarXiv 2026

TL;DR

LEGO-RL is a novel framework that enables reinforcement learning (RL) for coding agents directly within their native execution harnesses (such as OpenHands or Claude Code) without modifying their internal control flow. By combining an in-process LLM proxy, robust sandbox defenses, and a live diagnostic UI, it achieves substantial performance gains on SWE-bench Verified while ensuring perfect mathematical alignment between rollout behavior and policy updates.

What Problem Does This Paper Address?

Building AI agents that can autonomously write, test, and debug code in real-world software repositories is a major goal in AI engineering. To do this, agents rely on complex software "harnesses" that manage prompts, tool interactions, and file-system states.

Training these agents using reinforcement learning—specifically policy-gradient methods—presents a massive challenge. Policy-gradient methods require the training engine to recompute the exact probability of the tokens generated during the agent's "rollout" (its attempt to solve a problem). However, native agent harnesses frequently rewrite interaction histories, compact contexts, or re-serialize data between steps. This creates a severe train-inference discrepancy. Furthermore, executing code at scale introduces environmental crashes, timeouts, and "reward hacking" (where agents exploit the grading environment rather than actually solving the task), which corrupts the RL training signal.

Methods

LEGO-RL bridges native coding-agent harnesses with scalable policy-gradient optimization via three core pillars:

  1. Faithful Optimization (In-Process Proxy): Instead of trying to reconstruct trajectories post-hoc from modified text transcripts, LEGO-RL deploys an in-process proxy at the LLM provider API boundary. It captures raw token IDs, log-probabilities, and response masks at generation time. For sparse Mixture-of-Experts (MoE) models, it records and replays rollout-time expert routing decisions (via the R3 mechanism), ensuring flawless trainer-side log-probability recomputation.
  2. Reliable Execution & Sandbox Orchestration: Built on top of the Harbor framework, LEGO-RL orchestrates isolated sandboxes using Docker or Kubernetes. It utilizes a Nydus lazy-pull snapshotter to stream container image chunks on demand, drastically reducing startup overhead. Crucially, it implements stage-wise defenses against reward hacking, such as hiding repository history during execution, enforcing egress firewalls via a privileged sidecar, and withholding test files until the final grading phase.
  3. Observable Training: The framework includes an integrated agent plugin and a Live UI that provides real-time, granular diagnostics. It tracks trajectory-level failures, tool-use distributions, and train-inference consistency metrics, allowing researchers to quickly differentiate between policy degradation and infrastructure failures.

The infrastructure utilizes Group Sequence Policy Optimization (GSPO) and decouples trajectory generation from optimization using fully asynchronous rollout scheduling to maximize GPU utilization.

Figure 1: Overview of the LEGO-RL training infrastructure.

Datasets

  • OpenSWE Candidate Pools: Used to construct a filtered, 2,699-task training index. Tasks were selected using a rollout-based difficulty screening process, retaining instances that a baseline model solved 1–3 times out of 4 trials to ensure a high density of usable learning signals.
  • SWE-bench Verified: Used as the primary evaluation benchmark. The training set was designed to be strictly disjoint from SWE-bench Verified at both the repository and instance levels to prevent data contamination.

Key Insights and Ideas

  • Harness Dependence is Critical: A policy optimization gain achieved under one agent control flow does not necessarily translate to another. RL must optimize the policy behavior induced by the specific harness's prompts and tools.
  • The Necessity of Generation-Time Capture: Because harnesses dynamically alter context between turns, capturing tokens at the model-serving boundary is the only way to maintain mathematically sound policy gradients.
  • Task Difficulty is Policy-Relative: As an agent improves during training, a fixed task pool gradually yields more "zero-variation" groups (where the agent either succeeds on all rollouts or fails on all of them). This means the informativeness of the dataset decays over time, highlighting the need for dynamic or difficulty-aware task selection.
  • RL Shifts Verification, Not Just Recovery: Detailed trajectory analysis revealed that RL training primarily changes the agent's self-checking behavior (e.g., re-reading a file after modifying it) rather than its ability to recover after an explicit command failure.

Key Results

The authors evaluated LEGO-RL by training the sparse MoE model Qwen3.5-35B-A3B across three distinct native harnesses, achieving substantial absolute improvements in resolve rates on SWE-bench Verified:

  • OpenHands SDK: 64.0% $\rightarrow$ 70.4% (+6.4%)
  • Claude Code: 62.4% $\rightarrow$ 68.2% (+5.8%)
  • OpenCode: 57.2% $\rightarrow$ 66.6% (+9.4%)

The framework maintained a rollout-to-training log-probability Pearson correlation above 0.99 across all configurations. From an efficiency standpoint, asynchronous scheduling achieved a 2.5$\times$ reduction in step time compared to synchronous execution by eliminating batch-boundary stalls caused by long-tail trajectories. Sandbox optimizations (like lazy image pulling and prebuilt task images) delivered up to a 33.2$\times$ median speedup in environment setup.

Comparison of representative agentic RL frameworks.

Limitations and Future Work

  • Single Model Architecture: The evaluation was exclusively performed using Qwen3.5-35B-A3B; generalization to other model architectures or mixed-harness training remains to be verified.
  • Coarse Binary Rewards: Executable verification provides a coarse binary (pass/fail) reward, which lacks the granularity to assign intermediate credit for partial progress or clever error recovery.
  • Compute Constraints: Due to the high cost of production-scale training, main configurations were limited to a single run, leaving run-to-run variance unquantified.

Future Work: The authors are currently extending LEGO-RL to support mixed training (combining repository repair with other verifiable software tasks) and multi-harness co-training (optimizing a single policy across multiple agent harnesses simultaneously).

Summary updated

BibTeX
@misc{du2026lego,
  author = {Yiming Du and Yuxin Jiang and Tao Yuan and Jianbo Dai and Shaowei Wang and Jierun Chen and Chaofan Tao and Xianzhi Yu and Lifeng Shang and Kam-Fai Wong and Xiaohui Li and Haoli Bai},
  title = {{LEGO-RL: Harness-Native Reinforcement Learning for Coding Agents}},
  year = {2026},
  eprint = {2608.17393},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2608.17393},
}
Figure