Patch Now, Consolidate Later: Two-Timescale Code Refinement Against Erosion

When a coding agent repeatedly edits its own program to meet changing requirements, the program rots. On SlopCodeBench, a benchmark where agents extend their own code across successive specification checkpoints, complexity piles into functions that are already the most complex in 77% of agent trajectories. Duplicated, bloated code grows in 75.5% of them. Agent code ends up 2.3x more verbose than human repositories, and it accumulates bloat about 7x faster. The best agent passes only 14.8% of checkpoints. Brains face a similar problem when learning motor skills and handle it differently. A fast, plastic circuit absorbs immediate corrections. A slow circuit later takes over the corrected behavior, using the fast circuit's output as its teacher. After that handover the fast circuit returns to baseline.

We propose to build a coding agent that works the same way. Quick fixes for failing tests go into a separate, disposable patch layer that sits on top of a frozen core codebase. Only after a patch has survived a later specification change does a separate consolidation step rewrite the core so that it reproduces the patched behavior. That step is checked by comparing the outputs of the old patched program and the new rewritten one, and the patch layer is then emptied. The bet is that erosion comes largely from committing every tactical fix directly into core code. On this view, delaying and batching structural integration removes both the fixes that later get superseded and the local hacks that accumulate.

Core Claim

The core claim is that separating where fixes are made from when they become permanent reduces code erosion under iterative specification change without lowering functional correctness, and that each of three ingredients matters. The first is the separation itself: fast edits cannot restructure the core. The second is a delay before a patch becomes permanent, which we call a consolidation window. The third is that the consolidation step uses the patched program's behavior as its reference, not only the test suite. This is a claim about agent architecture and editing discipline, not about model weights. That distinguishes it from the closest existing bridges between memory consolidation and agents.

Dual-Layer Agentic Memory applies the fast-buffer, slow-store split to factual knowledge. Facts are held in an external store and periodically fine-tuned into weights. That paper also reports "parametric interference", where fine-tuning corrupts facts the model already knew. It never touches executable code. MASkills refines, merges and prunes natural-language skill files with validation-based rollback, but its skills are prompts, not programs under test. Progress-Gated Dual-Process Routing separates tactical and strategic corrections within one episode and then discards them. TGPR amortizes refinement search into weights offline on single-shot problems. None of these separates a disposable behavioral patch from a permanent structural representation of the same program. None of them uses the patched behavior as the target for consolidation, and none is evaluated on erosion.

Where the Biology Actually Constrains the Design

Three results supply specific design choices rather than decoration.

The first is the internal-teacher result of Masuda and Amari. In their cerebellar model, the slow brainstem site learns from the fast cortical output rather than from the external error signal, and this makes the transfer robust without fine-tuning of learning rates. In code, this means the consolidation step should be judged by behavioral equivalence to the patched program, which gives a dense oracle, instead of only by the sparse test suite.

The second is the division of labor reported by Reinhold et al.. The striatum is needed for fast learning but becomes dispensable once behavior is consolidated downstream, which frees it for new learning. In code, this means the patch layer must actually be emptied after consolidation.

The third is the interference finding of Shadmehr and Mussa-Ivaldi. A fresh motor memory is disrupted by learning an opposing task five minutes later, but is protected after a 24-hour window. In code, this suggests that a fix should not be committed to core until it has survived at least one subsequent specification change. Fixes that are overwritten before then never touch the core.

We treat these as hypotheses to test, not as guarantees. The biological models are low-dimensional, as the file notes. Nothing in them establishes that the analogy transfers to discrete, compositional programs.

Research Program

The system has two stores inside the same repository. The core is ordinary source code and is read-only during the fast phase. The patch layer is a designated module of overrides: wrapper functions, dispatch entries and small added handlers that intercept core behavior, stored as an ordered stack of small diffs. Each diff is tagged with the checkpoint and the failing tests that motivated it.

In the fast phase, the agent sees test failures and execution traces and may only add or edit patches. This keeps each fix local and cheap to discard. When a new checkpoint arrives, patches whose motivating tests are removed or contradicted by the new specification are dropped without further work. That is the concrete analogue of an unconsolidated trace being overwritten.

Consolidation is triggered once a patch has persisted through one later checkpoint; the length of this delay is a swept parameter. A separate agent call then receives the core, the full surviving patch stack, and complexity and duplication measurements. It rewrites the core so that no patches are needed. The rewrite is accepted only under two conditions. First, the new core must match the old core-plus-patches on the full test suite. Second, it must match on inputs generated from the checkpoint's command-line or API contract, where outputs of the two versions are compared directly (differential testing). If the rewrite fails either check, it is rejected and the patches remain, similar to MEIC's rollback-to-best-version. On success, the patch layer is emptied.

We will log three quantities per checkpoint. The first is the fraction of patches superseded before consolidation, which is direct evidence for or against the window mechanism. The second is the rate of consolidation rejections. The third is erosion measured separately on core and patch layer.

Baselines

The naive reference is a single-agent execution loop that edits code in place. It reads test output, patches the failing code and repeats, which is the standard SlopCodeBench protocol. We run it with the same backbone models, the same per-checkpoint call and token budget, and the same harness as every other condition. The target it sets is the SlopCodeBench number for the best agent reported there: a 14.8% checkpoint pass rate, with erosion rising in 77% and verbosity in 75.5% of trajectories. We will re-measure this baseline in our own harness rather than rely only on the reported figure, because our backbones may differ.

The strongest existing mitigation is the quality-aware prompting condition from SlopCodeBench. The authors report that it reduces initial verbosity and erosion but raises cost per checkpoint and slightly lowers correctness. We reproduce its prompts with the in-place agent, matched in budget. Beating it means lower erosion and verbosity slopes across checkpoints, not just lower starting values, at no loss in checkpoint pass rate.

A rollback baseline adds MEIC-style reversion to the in-place agent. If a checkpoint's edits cause regressions or worsen erosion, it reverts to the best prior version. This tests whether regression control alone explains any gain.

The key ablation is in-place periodic refactoring. It uses the same agent and the same consolidation prompt on the same schedule, with the same total compute. The difference is that fast edits go directly into the core and the refactor step is validated only against the test suite, not against a patched reference. This isolates what the separate layer and the behavioral-equivalence oracle contribute beyond "remember to clean up sometimes."

Two further ablations complete the picture. One removes differential testing and keeps tests only. The other sweeps the consolidation delay across three settings: immediate consolidation after every checkpoint, a one-checkpoint delay, and never consolidating so that patches accumulate. We count the proposal as succeeding only if it beats the periodic-refactor ablation on erosion slope at equal compute and equal or better pass rate.

Why not just…?

Why not just prompt the agent to write clean code? SlopCodeBench already tested this. Quality-aware prompts lowered initial erosion but did not stop degradation across iterations, and they slightly reduced correctness. We include this condition directly, so if prompting matches our system on erosion slope, the experiment will show it.

Why not just run a periodic refactor pass on the ordinary codebase? This is the real threat, and the proposal may reduce to it. If the gains come purely from spending calls on cleanup, the in-place periodic-refactor ablation will match the full system, and we will report that the biological separation adds nothing. Our reason to expect a difference is mechanistic and measurable. In the in-place condition, fixes later superseded by specification changes are already woven into the core, and refactoring must untangle them. In our system they are discarded unconsolidated. The fraction of superseded patches predicts the size of the gap: if that fraction is near zero on SlopCodeBench, we expect no gap.

Why not just use rollback, as MEIC does? Rollback protects against regressions in function, but erosion on SlopCodeBench accumulates in code that still passes tests. Reverting to the best passing version therefore keeps the bloat. The rollback baseline tests this directly.

Why not just train a better refining model, as TGPR or RefineCoder do? TGPR and RefineCoder improve single-problem pass rates (for example, TGPR's 77.6 on MBPP and 41.2 on LiveCodeBench). Neither is evaluated on multi-checkpoint erosion; there is no reported SlopCodeBench result for either on Lacuna. Trained models and our architecture are also complementary. We will run the architecture on two backbones of different strength. If the erosion benefit disappears on the stronger one, that suggests better models make the architecture unnecessary.

Proposed Experiments

The primary benchmark is SlopCodeBench: 36 problems and 196 checkpoints, scored with its own checkpoint pass rate, structural-erosion and verbosity metrics, and cost per checkpoint. We compute erosion and verbosity on the final core only. The core is what persists and what a human maintains, so the patch layer is excluded, and patch-layer size is reported separately so hidden bloat cannot hide there. All conditions share backbones, call budgets and three seeds per problem.

The decisive checks are these. First, the full system must show a lower per-checkpoint erosion slope than both quality prompting and in-place periodic refactoring at matched compute and non-inferior pass rate. Second, the delay sweep should show a U-shape in erosion. Immediate consolidation should behave like in-place editing, and never consolidating should bloat the patch layer. An intermediate window should be best, as the interference result predicts. Third, removing differential testing should increase silent behavioral drift, measured as disagreement between consolidated and patched versions on held-out contract-generated inputs.

As a secondary stress test of whether patch-then-consolidate helps multi-file work, we run the system on SWE-Bench ProMax. There the dominant failure is incomplete propagation of refactors across call sites, and GPT-5.2 resolves 41.2%. We will treat that benchmark as exploratory, since consolidation there must span an average of 11.4 files and our mechanism was designed around single evolving programs.