Lookahead Preferences: Training Code Agents to Write Code That Survives the Next Change

When a language model edits a program over many rounds, adding features as the specification changes, the code tends to decay even while it keeps passing its tests. It becomes longer, more duplicated, and harder to extend. Today's training signals cannot see this decay. Unit tests only check whether the current version works. The alternative, an LLM judge (a strong model asked to grade code), is known to reward longer and more decorated outputs. We propose a third signal: judge each intermediate version of a program by what happens next. Concretely, we fork the refinement process from two candidate versions and ask which one lets the agent pass more of the future specification checkpoints. We then use these consequence-based comparisons as training pairs for direct preference optimization (DPO), a method that fine-tunes a model directly on "A is better than B" pairs without fitting a separate reward model.

The idea in two sentences: code quality in iterative refinement is mostly about how cheaply the code can be changed later, so we label preference pairs by measured downstream success rather than by current tests or a biased judge. We also use the same machinery as a measuring instrument. It lets us test directly whether training on LLM-judge preferences speeds up code erosion, a causal link that the literature suspects but has never checked.

Core Claim

SlopCodeBench follows coding agents through multi-checkpoint specification changes. Structural erosion rises in 77% of agent trajectories and verbosity in 75.5%. Agent code ends up 2.3x more verbose and 2.0x more eroded than human code, and it accumulates verbosity about 7x faster. The best agents pass only about 14.8% of checkpoints end to end. The same paper reports that quality-aware system prompts do not stop this iterative degradation.

A separate literature shows that LLM judges prefer verbose outputs. Verbosity Bias in Preference Labeling reports bias values of 0.328 for GPT-4 and 0.428 for GPT-3.5 on HH-RLHF, with judgments diverging from humans exactly when humans prefer concise answers. Don't Judge Code by Its Cover finds that superficial cues in code, such as authority claims, # correct code comments, and illusory complexity, shift judge accuracy by up to 26.7 points, and that prompt-based mitigations help only marginally.

Our claim has two parts. First, when judge-labelled preferences are used to train a refinement policy, they measurably speed up erosion relative to test-only labels. Second, preferences labelled by downstream checkpoint success reduce erosion and raise later-checkpoint pass rates. They also beat a simple static penalty on length or complexity, because they reward abstractions that pay off later instead of penalizing all added structure uniformly.

For a task at checkpoint $k$ with candidate programs $y_a$ and $y_b$, we define the lookahead value

$$V_H(y) = \frac{1}{H}\sum_{j=1}^{H} \Pr\left[\text{rollout from } y \text{ passes checkpoint } k+j\right],$$

where $H$ is the lookahead horizon and the probability is estimated from $n$ independent continuations by a fixed rollout policy. The pair $(y_w, y_l)$ is formed when $V_H(y_w) - V_H(y_l)$ exceeds a confidence margin. Pairs inside the margin are kept as explicit ties, following the tie-training remedy for spurious correlations in Spurious Correlation Learning in Preference Optimization. Setting $H=0$ recovers ordinary current-checkpoint test labels, so the horizon itself serves as the ablation knob.

Relation to Prior Work

The closest line of work builds DPO pairs from execution. CodeLutra separates passing from failing candidates with tests and trains with DPO plus supervised regularization. IterPref mines pairs from debugging traces and masks the loss to the differing tokens. Focused-DPO reweights the loss onto error-prone points. Function-Level Execution Feedback applies stepwise preference loss to decomposed functions.

These papers supply our training method, and we reuse IterPref's diff-restricted loss as a variant. All four, however, label pairs by whether the code passes now, on single-function tasks. None of them can distinguish two passing programs, one lean and one bloated. Our labels come from future checkpoints, and our setting is multi-turn specification change.

Two further papers motivate specific design choices. Function-Level Execution Feedback found that GPT-4o-mini as a judge agreed with execution on only about 52% of passing functions, which is part of why we distrust judge labels. ARIES shows that iterative DPO can teach self-refinement to an 8B model, but its reward signals are open to length bias and it never evaluates code, so it does not tell us whether refinement training helps or harms maintainability.

Response-conditioned Bradley–Terry disentangles length from quality by pairing responses against modified prompts. It is the most principled existing length-debiasing method, so we include it as a baseline. Its limitation for our purposes is that it treats length as a nuisance variable, whereas in code, some added length (a helper that later absorbs three features) is exactly what we want to reward.

Rethinking Code Refinement trains static judges of efficiency and warns that "refinements" often make code worse. That finding supports measuring consequences rather than trusting surface judgments. Anchored Self-Play constrains us in a different way. It shows that training only on unit-test feedback with synthetic tasks drifts away from realistic data. Because we must synthesize evolving-specification training tasks, we evaluate only on held-out, human-built SlopCodeBench problems.

Research Program

Training corpus. SlopCodeBench is too small to train on (36 problems, 196 checkpoints), and we hold it out entirely. We build a training corpus of evolving-specification tasks by chaining related BigCodeBench tasks into sequences of three to six checkpoints. A strong model drafts each extension, and a human-checked test suite covers each checkpoint. Following SlopCodeBench, the agent must extend its own previous code without a prescribed interface.

Collecting pairs. A base policy, Qwen2.5-Coder-7B for comparability with IterPref and Focused-DPO, produces several candidates at each checkpoint. We keep only candidates that pass the current tests. This choice isolates non-functional quality: current tests cannot separate these candidates, so the lookahead signal carries all the information. From each surviving candidate, a frozen rollout policy runs $n$ continuations through the next $H$ checkpoints. The rollout policy is the base model, which keeps it from co-adapting with the model being trained. The rollouts give us $V_H$.

Cost. The expense is roughly $n \times H$ agent episodes per candidate. We will report label quality as a function of $n$ and $H$ to find the cheapest setting that still works.

Measuring judge bias on the same pairs. On the same candidate pairs, we collect preferences from LLM judges: a frontier model and an open 70B model, each in both orders to control for position bias as in Pairwise or Pointwise?. We then measure three things: how often the judges agree with lookahead labels, how their preferences depend on the length difference and the difference in SlopCodeBench erosion metrics, and whether that dependence persists once lookahead value is controlled for. This part directly answers whether judges favor code that is worse for the future.

Training. We train otherwise-identical DPO models on four label sources: current tests, judge labels, static erosion and verbosity metrics, and lookahead labels. We then run each through SlopCodeBench as an agent in the same scaffold.

Baselines

The naive reference point is the base Qwen2.5-Coder-7B agent in the SlopCodeBench scaffold, with and without the benchmark's quality-aware system prompt. The prompted version is the no-training competitor that SlopCodeBench already found insufficient. Beating it means lower erosion and verbosity slopes across checkpoints at equal or better checkpoint pass rate.

The strongest existing training method for functional correctness is execution-labelled DPO in the style of CodeLutra and IterPref. IterPref reports 29.7% pass@1 on BigCodeBench-Hard for Qwen2.5-Coder-7B with Target-DPO. We run it on our corpus as the $H=0$ labelling: same candidates, same number of pairs, same DPO hyperparameter search budget. We also run a Target-DPO masked variant for both this baseline and our method, so the diff-restricted loss is held constant.

We found no reported result on Lacuna for any DPO-trained model on SlopCodeBench. The reference numbers there are therefore the benchmark's own 14.8% checkpoint pass rate for the best agents, and the 77% and 75.5% erosion and verbosity incidence rates. Beating the execution-only baseline means a higher pass rate at later checkpoints (four and beyond) and a lower fraction of trajectories with rising erosion. We also check that BigCodeBench-Hard does not regress, to confirm we have not traded away single-shot correctness.

The judge-labelled baseline uses the same candidate pairs, labelled by a frontier LLM judge with position swapping and a test-generation-first prompt. That is the prompt debiasing that Don't Judge Code by Its Cover evaluated. This baseline doubles as the causal test of whether judge labels accelerate erosion.

The static-metric baseline labels pairs by the lower SlopCodeBench erosion-plus-verbosity score among passing candidates. It is the most dangerous competitor, discussed below.

The response-conditioned DPO baseline from Rc-BT is applied to judge labels to test whether principled length disentangling is enough.

Three ablations isolate the key ingredient. We sweep the horizon $H \in \{0,1,2,3\}$. We run lookahead labels with ties discarded rather than trained on. We run lookahead labels generated with a different rollout model, to check that the labels encode code properties rather than the quirks of one policy.

Why not just…?

Why not just use unit tests, since they are unbiased? Unit tests are unbiased about the present and blind to the future. The file shows that passing code still erodes in 77% of SlopCodeBench trajectories. Anchored Self-Play also found that test-only training drifts toward unrealistic data. Our method keeps tests as the oracle and simply applies them to later checkpoints. The $H=0$ versus $H\ge1$ comparison shows directly whether looking ahead adds anything beyond current tests.

Why not just penalize length or erosion metrics directly? This is the real threat. It is cheap, and it may capture most of the benefit. Our reason to expect it to fall short is that a static penalty treats all added structure as bad, including helper functions and interfaces that later checkpoints reuse. It also invites a policy that games the metric, for example by cramming logic into dense one-liners. We settle this empirically. If static-metric DPO matches lookahead DPO on later-checkpoint pass rate and erosion slope, the lookahead labels are not worth their rollout cost, and we will report that. We will also measure how often lookahead winners are longer than losers. If that almost never happens, lookahead is just an expensive length proxy.

Why not just debias the judge by prompting, or use a debiased reward model? Don't Judge Code by Its Cover found that prompt mitigations, including generating tests first, give only marginal gains against superficial cues, and SlopCodeBench found the same for quality prompts on the agent side. Rc-BT is a stronger fix, and we include it. Even so, a perfectly length-neutral judge still judges a snapshot, and the question of which snapshot is easier to extend is not visible in the snapshot alone. The agreement analysis between judges and lookahead labels, controlling for length, quantifies how much of the gap debiasing could possibly close.

Why not just run SFT on the passing trajectories? CodeLutra shows that adding negative examples through DPO substantially outperforms training on positives alone (DS-1000 from 28.2% to 48.6%). Moreover, our filtering makes every candidate a passing one, so plain SFT on passing code would include the bloated candidates too. For completeness, we include SFT on lookahead winners only as an additional reference.

Decisive Checks

The first check concerns the bias measurement. If judges prefer the lookahead loser more often than chance on length-mismatched pairs, and if judge-labelled DPO produces a steeper erosion slope on SlopCodeBench than test-labelled DPO, we have the first causal evidence that evaluator verbosity bias feeds code erosion. If neither holds, that negative result is also informative for anyone labelling code preferences with judges.

The second check concerns the method. Lookahead DPO must beat both test-labelled and static-metric DPO on pass rate at checkpoint four and beyond, and on the fraction of trajectories with rising erosion, while staying within noise of IterPref-style training on BigCodeBench-Hard. The horizon sweep should show gains appearing at $H\ge1$ and saturating. If they grow without limit, the labels are probably tracking rollout-policy noise.

The third check concerns validity. Label agreement across different rollout policies must be well above chance. Otherwise the preferences describe one model's habits rather than the code, and the approach fails as stated.