---
title: "Cleanup Timing for Code Slop in Long-Horizon Agent Runs"
description: "In one 42-task study, a final cleanup pass barely cut agent code slop; cleanup at natural breakpoints cut it more, with the gain in two of three models."
tags:
  - testing-verification
  - tool-agnostic
  - arxiv
aliases:
  - trajectory-time cleanup
  - monitor-triggered cleanup
last_reviewed: 2026-12-03
maturity: emerging
---

# Cleanup Timing for Code Slop in Long-Horizon Agent Runs

> Cleanup at natural breakpoints in an agent run cut code slop more than one final pass, for the two weaker models in a 42-task study.

In one study of 2,268 runs, the timing of a cleanup sub-agent inside a long agent run changed how much code slop the run left behind. "A single final cleanup pass barely reduces slop", while cleanup during the run reduced it ([Gandhi et al., arXiv:2612.01914v1](https://arxiv.org/abs/2612.01914v1)). The gain came from two of the three models tested. Claude Sonnet 4.6 got no slop reduction, and its strict resolve rate fell from 22.2% to 16.7% under breakpoint-triggered cleanup (Table 3). The authors say they "do not study cleanup as a solution to slop."

## What the study measured

The authors ran 42 SWE-EVO tasks, where each task asks an agent to evolve a real Python repository from one release to a later one. Each condition ran three models (Claude Sonnet 4.6, DeepSeek V3.2, Qwen3-Coder 480B) with three seeds. Every run used mini-swe-agent, a minimal harness where the agent issues one bash command per step, with a 100-step budget ([arXiv:2612.01914v1](https://arxiv.org/abs/2612.01914v1)).

The paper defines code slop as "the unnecessary code volume, control-flow complexity, or maintainability debt that an AI coding agent introduces beyond what the task requires for functional correctness." SonarQube scans the whole repository before and after the run. The paper reports the change in code volume (ΔLOC, non-comment lines), control-flow complexity (ΔCog, cognitive complexity), and maintainability debt (ΔRem, estimated minutes to fix all detected maintainability issues).

The [CodeSlop page](../patterns/anti-patterns/codeslop-trajectory-minimization.md) uses the same word for a different measure: the length gap between an agent's patch and its minimal behavior-preserving patch ([Mathai et al., arXiv:2607.18161v1](https://arxiv.org/abs/2607.18161v1)). Figures from the two papers do not compare.

## The four cleanup schedules

One-shot, Periodic-N, and Monitor run the same operation, a fresh sub-agent run with a 50-step budget and a $7.5 cost cap ([arXiv:2612.01914v1](https://arxiv.org/abs/2612.01914v1), Appendix E). Only the trigger differs, and Base runs no cleanup.

| Schedule | When cleanup fires |
|---|---|
| Base | Never |
| One-shot | Once, after the task attempt completes |
| Periodic-N | Every N steps and again at the end; the paper reports N of 10, 20, and 30 |
| Monitor | When a yes/no query to the agent's own model says the agent has reached a natural breakpoint |

At each step, the Monitor poses one binary question to the model: "has the agent just completed a meaningful subtask and reached a natural breakpoint where a cleanup pass would not interrupt mid-subtask work?" Two guards then suppress a pass. It skips if any of the last three steps ended in an execution or format error, and it skips if the diff has not changed since the previous cleanup (Appendix D).

The cleanup sub-agent receives the problem statement, the patch, and the trajectory. It removes five kinds of bloat: reproduction and debug scripts the agent created, formatting-only changes unrelated to the fix, patch-tool artifacts such as `.rej` files, clearly temporary debug prints or logging, and duplicate or near-duplicate files. Its prompt forbids new fix logic, changes to fix logic, mass deletion, and edits to files the patch did not touch, except deleting bloat files the agent created (Appendix E).

## Results across schedules

Table 1 of the paper reports these values. Resolve, fractional utility, and regression are percentages, and cost is mean dollars per instance. The three slop columns are model-balanced mid-90% means, which drop the most extreme 5% of runs at each tail. Lower is better, and a negative value means the repository ends up smaller than before the task ([arXiv:2612.01914v1](https://arxiv.org/abs/2612.01914v1)).

| Setting | Resolve | Frac. util. | Regr. | Cost | ΔLOC | ΔCog | ΔRem |
|---|---|---|---|---|---|---|---|
| Base | 13.5 | 73.2 | 35.4 | 0.60 | −20.3 | −1.0 | −0.8 |
| One-shot | 9.3 | 75.3 | 30.2 | 0.84 | −14.3 | −0.5 | −0.0 |
| Periodic-10 | 16.1 | 74.1 | 32.0 | 1.21 | −55.4 | −10.4 | −4.6 |
| Periodic-20 | 13.0 | 75.1 | 30.2 | 1.25 | −36.8 | −5.9 | −1.8 |
| Periodic-30 | 11.9 | 75.5 | 30.2 | 1.11 | −35.7 | −6.0 | −2.8 |
| Monitor | 13.5 | 70.7 | 36.0 | 0.91 | −79.3 | −13.4 | −4.8 |

- The final pass did little. One-shot reduced code volume less than Base (−14.3 against −20.3).
- Periodic cleanup reduced slop. Periodic-10 costs $1.21 per instance, "roughly double the cost of Base". The reductions are not monotone in N, so the authors call the period "a hyperparameter with no principled default."
- Monitor gave the largest reduction on all three measures. It costs $0.91 per instance, about 25% less than Periodic-10. Its debt lead over Periodic-10 is small (−4.8 against −4.6).
- Monitor also has the lowest fractional utility (70.7) and the highest regression rate (36.0) of the six settings. The authors treat the 70.7–75.5% utility span as within per-seed variation, and add: "We therefore do not claim cleanup is resolve-neutral".

## The gain depends on the model

Per-model results reverse the pooled picture for the strongest model. Table 2 reports raw per-model means, a different statistic from Table 1. Under Monitor, Claude Sonnet 4.6's code-volume delta rose from +22.1 to +28.7, with "the same pattern on complexity and debt" ([arXiv:2612.01914v1](https://arxiv.org/abs/2612.01914v1)). DeepSeek V3.2 moved from −126.5 to −683.4, and Qwen3-Coder 480B from −280.2 to −1211.5.

Claude's strict resolve fell under One-shot and Monitor, from 22.2% at Base to 11.9% and 16.7% (Table 3). The authors warn that Qwen3-Coder's strict resolve rate is low, "so per-model slop magnitudes are highly uncertain."

## Why it works

The paper reports the timing effect and does not establish its cause. It gives two partial explanations ([arXiv:2612.01914v1](https://arxiv.org/abs/2612.01914v1), §5.2). One-shot is "consistent with a conservative final pass that tidies a finished attempt rather than aggressively shrinking it". One reading, which is inference and not the paper's claim, is that the prompt rule "keep anything whose role is unclear" protects more code once the attempt is finished. Monitor saves cost without cleaning less often. It fires about as often as Periodic-10, but "each invocation acts on a settled, non-empty diff rather than running regardless of whether there is anything to clean." The authors say the result "suggests that this decision may require only a coarse trajectory signal rather than a learned reward model."

The paper's motivating claim, that a leaner intermediate state helps the next turn, is a hypothesis it "cannot test with this design" (§6). A separate 660-trial study with Claude Code found that code cleanliness did not change the agent's pass rate, though agents on cleaner code used 7 to 8% fewer tokens ([arXiv:2605.20049v1](https://arxiv.org/abs/2605.20049v1)). Linking that result to in-run cleanup is inference across two studies.

## When this backfires

- Under Monitor, the strongest model tested gained nothing. Claude Sonnet 4.6 had no slop reduction under Monitor, and its strict resolve fell from 22.2% to 16.7% (Table 3).
- A cleanup agent can break working code, and its limits here are prompt rules. When repair agents were prompted to minimize their own patches, 3.8% to 44.9% of results failed to preserve behavior or grew larger ([arXiv:2607.18161v1](https://arxiv.org/abs/2607.18161v1)). In RepoClean cleanup tasks, "All seven models cause previously passing files to fail" ([arXiv:2611.30350v1](https://arxiv.org/abs/2611.30350v1)).
- Each pass costs money. Monitor costs $0.91 per instance against $0.60 for Base, and it queries the model at every step.
- Wanted code can look like bloat. The cleanup prompt removes reproduction and debug scripts the agent created, which a project may want to keep.
- The evidence is narrow. "SWE-EVO is Python-only, and we use a single agent harness", no second turn was run, and the metrics miss "naming, architecture, or test coverage" ([arXiv:2612.01914v1](https://arxiv.org/abs/2612.01914v1), §8). The results "do not establish that a leaner end state is a better one".

## Key Takeaways

- Measure your own model's slop before adding in-run cleanup. In this study the strongest model already wrote compact code and changed little.
- If you add cleanup, trigger it at subtask breakpoints, and skip a pass when the diff is unchanged or any of the last three steps errored.
- Track strict resolve and regression rate next to the slop measures. Monitor's slop result came with the lowest utility and the highest regression rate in Table 1.
- Where a strong test oracle exists, consider trajectory minimization, which keeps each removal only if the tests still pass.

## Related

- [CodeSlop: Search-Trajectory Residue in Agent Patches](../patterns/anti-patterns/codeslop-trajectory-minimization.md) — post-run minimization that keeps a removal only if the tests still pass
- [Metrics-in-the-Loop: Complexity Feedback for Coding Agents](metrics-in-the-loop-complexity-feedback.md) — in-run feedback that sends measurements instead of running a cleanup agent
- [AI Slop as a Process Problem: Encoding Quality Standards as Pipeline Gates](../workflows/slop-as-process-problem.md) — a quality gate at merge time
- [Code Cleanliness as an Agent Cost Lever](../token-engineering/code-cleanliness-agent-cost-lever.md) — evidence that cleaner code changes agent cost more than pass rate
