TL;DR
Training AI software engineers using only pass/fail test results at the end of a long coding session leaves them guessing which specific actions helped or hurt. Counterfactual Rollout Replay (CRR) turns code sandboxes into time machines during training: by rewinding to pivotal moments and playing out alternative choices, agents pinpoint the exact value of intermediate steps without needing human labels or learned reward models. Even after accounting for the extra compute spent exploring these parallel timelines, CRR significantly outperforms standard reinforcement learning.
What Problem Does This Paper Address?
Imagine writing a 30-step bug fix—searching files, running shell commands, editing code—only to be told at the very end: "Tests failed." Was your initial file search brilliant and your final edit a typo, or were you barking up the wrong tree from step one?
This is the credit-assignment bottleneck in reinforcement learning (RL) for software engineering (SWE) agents. Standard algorithms like PPO and GRPO rely on outcome-only rewards (did the final patch pass held-out tests?), applying a single broad brushstroke of credit or blame across every action in a trajectory. To get step-by-step guidance, researchers typically pay for expensive human annotations or train complex Process Reward Models (PRMs) to judge intermediate steps. The authors ask a simpler question: since code repositories live in restorable sandboxes, can we use the executable environment itself to generate step-level credit for "free" (meaning zero supervision labels or learned reward models)?
Methods
CRR builds directly on standard policy-gradient machinery (specifically GRPO, which scores trajectories relative to a group of sibling attempts), changing only how individual steps are scored.
Here is how CRR works during training:
- Run an on-policy rollout: The agent attempts to solve a GitHub issue in up to 50 turns, receiving a binary terminal reward ($1$ if tests pass, $0$ if they fail).
- Select pivotal decision points: Rather than branching everywhere, CRR picks up to $k=4$ moments per trajectory where the model was uncertain (high token entropy) and taking consequential actions—weighting file edits ($1.0$) and shell commands ($0.75$) much higher than passive file reads ($0$).
- Fork and replay: Using a snapshot-and-restore sandbox (
SWE-MiniSandbox), CRR rewinds the environment to each selected state, forces the agent to sample a different action, and plays the alternate timeline out to completion. - Contrastive scoring: Crucially, CRR retains the original trajectory for training rather than swapping in a better branch. At the forked steps, it replaces the generic trajectory-wide advantage with the exact difference between the original outcome and the counterfactual outcome ($R(\tau) - R(\tilde{\tau}_t)$). Unselected steps keep their standard GRPO outcome advantage.
Datasets
No new benchmarks are introduced; instead, the authors curate clean, deterministic splits from existing executable SWE environments:
- Training: 3,472 deterministic Python tasks (filtered via three-rerun consistency checks to eliminate flaky tests), combining 2,272 SWE-Gym instances and 1,200 SWE-rebench instances.
- Evaluation: Strictly decontaminated, held-out benchmarks testing repository-level issue resolution: SWE-bench Verified (human-validated tasks), SWE-bench Live (1,565 fresh instances created after Jan 2024), and the held-out SWE-rebench leaderboard split (860 tasks collected after April 2025).
Key Insights and Ideas
- Scoring actions beats replacing trajectories: When you fork an environment and find a working solution, it is tempting to throw away the original attempt and train on the winning branch. The authors test this exact baseline (Search-Select-GRPO) under an equal compute budget and find CRR wins ($36.2\%$ vs. $34.4\%$). Using forks to assign credit to what the policy actually did is more effective than simply cherry-picking better rollouts.
- Replay overhead pays for itself: Forking isn't free in terms of GPU hours—CRR spends roughly $41\%$ of its total compute running counterfactuals. Yet when given the exact same wall-clock time on identical hardware, CRR still decisively beats an extended vanilla GRPO run that gets to execute nearly $58\%$ more updates.
- Orthogonal to learned rewards and search: Because CRR grounds credit directly in real execution rather than text-based heuristics, stacking it on top of rubric-based PRMs (
SWE-Trace) and trajectory search (TSR) yields additive gains rather than redundant signals.
Key Results
Using a Qwen3-14B backbone trained with LoRA on a single $8\times\text{H100}$ node:
- Across-the-board improvements: On the 24,000-trajectory full budget, CRR achieves 41.7% greedy
pass@1on SWE-bench Verified (+5.3 points over vanilla GRPO and +2.6 overSWE-Trace), 35.9% on SWE-bench Live (+4.8 over GRPO), and 32.6% on SWE-rebench (+4.7 over GRPO). - Equal-compute victory: At an equal 40.1 hours of node wall-clock time (all fork overhead included), CRR hits 41.7% on Verified compared to 36.7% for extended vanilla GRPO.
- Composability & Consistency: Combining CRR +
TSR+SWE-Tracepushes Verifiedpass@1to 45.6%. Under multi-sample evaluation, CRR also boostspass@8by 3.9 points (to $57.7\%$) andall-8consistency (solving the bug on all 8 tries) by 5.2 points.
Limitations and Future Work
CRR relies heavily on cheap, reliable state restoration and deterministic test suites. In diagnostic sweeps where the authors artificially multiplied snapshot/replay costs, CRR's wall-clock advantage over GRPO vanished once replay became $2\times$ to $4\times$ more expensive—meaning heavy VM-based or non-deterministic networked environments remain out of reach. Furthermore, single counterfactual rollouts still carry variance from future policy sampling, and data-dependent step selection alters the training objective without formal convergence guarantees. Future work points toward handling noisy/imperfect replay, analyzing selector-weighted objectives, and managing heterogeneous trajectory lengths.
Summary updated