Condition-Based Restarts for Iterative Code Repair

Coding agents fix bugs in a loop. They edit the code, run the tests, read the error, and edit again. Most of the benefit arrives in the first few turns, and after three to five turns improvement typically stalls (Yan et al.). Past that point agents tend to make small, futile edits instead of abandoning a flawed approach (USEagent). Today's agents handle this in one of two ways. They keep patching until the tests pass or the budget runs out, or they restart on a fixed schedule. Industrial maintenance has a name for this pair of habits. The first is reactive "fix it when it breaks" maintenance. The second is calendar-based overhaul, which replaces parts on a fixed timetable whether or not they are worn. That field's main lesson is that both lose to condition-based maintenance: measure how worn the asset actually is, and choose an intervention from that measurement (Jian Kang et al., Michele Berno et al.).

This note proposes treating a coding agent's refinement attempt as a degrading asset. A cheap "health index" is computed from execution signals alone, such as tests that used to pass and now fail, the same traceback repeating, or edits that undo earlier edits. A small learned policy reads this index after every turn and picks one of four actions. It can submit, keep patching, roll back to the best checkpoint so far, or restart from scratch. The claim is testable: at a matched token budget, this condition-aware policy resolves more issues than a restart schedule that is tuned but fixed. The argument is the one maintenance research makes against calendar schedules. Tasks "wear out" at very different rates, so any single schedule services easy tasks too often and hard tasks too late.

Core Claim and What Is New

The mechanism comes from maintenance planning; the application is iterative code repair. The closest maintenance result is a maintenance planning framework using online and offline deep reinforcement learning. It learned do-nothing / maintain / replace policies for water pipes. Its learned policies favoured replacement over maintenance, because maintenance only temporarily lowered failure probability while replacement reset the risk entirely. The authors also found that a greedy threshold rule was the most expensive and least reliable policy they tested. Our hypothesis is that coding agents behave the same way. Late-stage patching resembles maintenance, which lowers failure only briefly. Rolling back or restarting resembles replacement, and after a trajectory has "degraded" it is often worth its higher up-front token cost.

The closest code-side work is the rollback architecture of Fang et al.. It reverts to the last stable checkpoint whenever a fix breaks previously passing tests. That design is a reactive threshold rule, the analogue of the greedy baseline, and it never decides to start over or to stop. USEagent lets a large model pick the next action dynamically, but it does not measure trajectory health and it documents poor backtracking. Adversarial Review improves the quality of each refinement step through structured critique between agents. It does not decide when to stop refining. None of these learns an intervention policy from trajectory degradation, and none compares it against schedule-based restarts at equal cost.

There is also a constraint on the design. Perry et al. found that models asked to fix their own code without an external verifier made security bugs worse, and that users trusted flawed code highly. For that reason the health index deliberately excludes the model's own judgment of its progress. It uses only signals from executing the code and from the diff history.

Research Program

The first component is the health index. After each turn $t$ we record features that cost no extra model calls. These are the number of passing tests and how that number has changed, the count of regressions (tests that passed before and fail now), and whether the exception type and location match an earlier turn. We also record the fraction of changed lines that revert earlier changes, the number of distinct files touched, and the current context length. The last feature matters because SWE-bench documents that localization degrades as context fills (SWE-bench).

The second component is data for learning the policy, collected by branching. We run a base agent on training tasks and snapshot its state every turn. From sampled snapshots we replay each of the four actions to the end of the budget and record whether the task was eventually resolved and how many tokens it used. Because we can branch from saved states, we can estimate the value of each action directly. This avoids learning purely from logs generated by a single policy.

Branching also makes the learning problem mostly supervised. For that reason we start with the simplest learner that fits: gradient-boosted classifiers that predict the probability of success for each action, $\hat p(\text{success} \mid h_t, a)$, from the health features $h_t$. The policy then picks

$$a_t = \arg\max_{a} \frac{\hat p(\text{success} \mid h_t, a)}{\hat c(a \mid h_t)},$$

where $\hat c$ is the predicted token cost of finishing under action $a$.

A conservative offline Q-learning variant, following the pipe-maintenance study, is a secondary arm. We run it only if the supervised version shows the one-step value estimates are myopic, meaning that a greedy ratio misses long-horizon value.

The third component is analysis. We fit hazard curves: the probability that continuing to patch eventually succeeds, as a function of turn and of health state. The central question is whether the turn at which intervention becomes optimal varies widely across tasks. If it does, a condition-based policy should win. If it is nearly constant, a fixed schedule suffices, and we will report that as the finding.

Baselines

The naive reference point is the standard single-trajectory loop. The base agent keeps patching from test and traceback feedback until the tests pass or the token budget is exhausted. This corresponds to reactive run-to-failure maintenance and sets the floor.

The calendar baseline is the competitor we consider most dangerous. It restarts from scratch every $k$ turns, or rolls back to the best checkpoint every $k$ turns. Both $k$ and the choice between restart and rollback are tuned on the same development split, with the same tuning budget the learned policy receives. The reported plateau at three to five turns suggests this baseline will be strong. Beating it by a margin that is statistically significant under paired bootstrap over tasks is the core test of the proposal.

We also run matched-token best-of-N: independent parallel attempts, each capped at the tuned $k$ turns, with the first test-passing patch submitted. This separates the value of intelligent intervention from the value of simply spending tokens on diversity.

The threshold baseline reimplements the rollback-on-regression rule of Fang et al., adding a fixed restart after $m$ consecutive non-improving turns, with $m$ tuned on the development split. It stands in for the greedy heuristic that lost in the pipe-maintenance study.

The strongest existing method on the file's benchmarks is Adversarial Review. The file reports a 75.2% pass rate on SWE-bench Verified, against 72.6% for five-agent debate and 71.6% zero-shot, and 87% on LiveCodeBench (Adversarial Review). It uses roughly 4.5 times the tokens of zero-shot inference. We will not compare against these reported numbers across different models. Instead we will reimplement Adversarial Review on our base model. We then run our policy both as a replacement for its outer loop and on top of it, and plot resolution rate against tokens spent.

Beating Adversarial Review means one of two things. Either our policy reaches its resolution rate at a clearly lower token cost, or it adds resolution when layered on top at equal cost.

Two ablations isolate the key ingredient, trajectory-level condition awareness. The first gives the policy only a snapshot of the current turn's features, with no history. The second adds the model's own self-assessment ("are you making progress?") as a feature. Following Perry et al., we expect this addition to hurt calibration.

Why not just…?

Why not just restart every three turns? This is the real threat, and the experiment is built around it. If the tuned fixed schedule matches the learned policy within noise, the plateau is homogeneous across tasks, and the maintenance analogy adds nothing beyond "restart early." We report that outcome as a result. The heterogeneity analysis settles which case holds. The proposal survives only if the best intervention turn varies across tasks and the gains concentrate on the tasks furthest from the tuned $k$.

Why not just sample N independent attempts in parallel? Parallel sampling spends tokens on attempts that were never going to fail, as well as on those that were. Condition-based intervention aims to put tokens where the health index shows degradation. The matched-token best-of-N arm tests this directly. If best-of-N matches us, the win comes from diversity, not from monitoring.

Why not let the agent decide when to back off, as USEagent does? USEagent's own analysis reports futile micro-edits instead of rollback. Perry et al. found that model and user confidence are poorly aligned with correctness. The self-assessment ablation measures whether the model's own judgment adds anything once execution signals are available. We also compare against a USEagent-style prompted meta-controller at matched tokens.

Why not a simple regression-triggered rollback rule? The pipe-maintenance study found that the greedy threshold rule was the most expensive and least reliable policy it tested, because it reacted only to immediate risk. A rollback rule has the same blind spot: it never restarts and never stops early. The threshold baseline tests whether that finding carries over to code.

Proposed Experiments

The primary benchmark is SWE-bench Verified, with 500 issues. The policy is trained on SWE-bench instances disjoint from Verified. This excludes every Verified instance, and it excludes SWE-bench Lite as well to avoid overlap that we cannot check. The second benchmark is LiveCodeBench. There we train on problems released before a cutoff date and test on problems released after it, which limits contamination.

We use one open base model throughout and cap each benchmark at a fixed per-task token budget. The metrics are resolution rate at fixed tokens, the full curve of resolution rate against tokens, and the fraction of tokens spent on attempts that never succeed. That last metric is the analogue of maintenance labour wasted on healthy parts.

As a transfer check, we apply the same four-action policy to performance refinement on the six-project C/C++ suite from POLO. POLO reports speedups of 1.34x to 21.5x over -O3. In that setting the health index is built from measured speedup across turns instead of test outcomes.

The decisive checks are three. First, the learned policy must beat the tuned fixed schedule at matched tokens on SWE-bench Verified, with a paired-bootstrap confidence interval that excludes zero. Second, the gain must concentrate on tasks whose best intervention turn differs from the tuned $k$. Third, in the branched rollouts, the value of restart or rollback must exceed the value of continued patching once regressions or repeated tracebacks appear. That would be the code analogue of replacement beating maintenance in the pipe study. If the first check fails while the third holds, the practical recommendation becomes a simple fixed restart schedule, and we will say so.