When a coding agent extends its own program across a series of changing requirements, the code tends to rot. The agent keeps piling new logic into functions that are already the most complicated, and it adds redundant lines faster than human developers do. This proposal gives the agent a running record, which we call a constraint ledger, of the structural commitments its codebase has made so far. Examples are a public function's signature, a cap on nesting depth inside an already-dense function, and a ban on re-defining a helper that already exists. Each commitment carries a persistence score. Commitments that have survived many requirement changes become hard rules, enforced while the model writes code: tokens that would break them are masked out of the model's next-token choices. Commitments that the new requirement explicitly touches, or that only appeared during a single debugging session, are released or allowed to expire.
The claim is that code erosion in multi-step development comes from two failures. The agent has no notion of which past decisions are load-bearing, and nothing stops it from violating them. Separating durable from disposable constraints, and enforcing only the durable ones, should slow erosion without lowering functional correctness. The test bed is a benchmark built to measure this erosion. The design deliberately puts the competing explanation on trial: perhaps enforcement doesn't matter and simply checking the output and retrying would do as well.
The problem and what prior work leaves open
SlopCodeBench is the motivating evidence. It runs agents through 36 problems and 196 checkpoints, where each checkpoint changes the command-line or API specification. The authors measure two forms of erosion. Structural erosion is complexity concentrating in already-complex functions, and it worsens in 77% of agent trajectories. Verbosity worsens in 75.5%. Compared with 473 human Python repositories, agent code builds up verbosity about 7x faster and erosion about 5x faster, and the best agents pass only about 14.8% of checkpoints. The paper is diagnostic only. Its agents decode with no constraints, and it reports that quality-focused prompting helps on the first turn but does not stop later degradation.
Grammar-constrained decoding is the second ingredient. It forces a model's output to stay inside a formal language by masking invalid next tokens. Three works establish it for code. Grammar-Based Patches Generation predicts syntax-tree production rules and produces zero uncompilable patches, though top-match accuracy is only 13.42%. GAMMA fills masked slots in fixed repair templates. GREATGRAMMA makes sound constrained decoding practical for Python, Java and Go, at 5–32 ms of masking overhead per token. All three treat the grammar as a fixed, single-turn object, and all three guarantee syntax rather than design quality. We reuse GREATGRAMMA as the enforcement engine. We do not expect plain language grammars to help with erosion, because frontier agents rarely fail at syntax. The constraints we enforce are project-specific and change from checkpoint to checkpoint.
The idea of keeping two timescales comes from recommender systems. HierTCN keeps a slow user state that updates once per session, which shields it from noise inside a session. CLSR splits long-term from short-term interest with separate encoders. We borrow the update pattern rather than the networks. The ledger's durable layer updates once per accepted checkpoint, and the disposable layer lives only inside one checkpoint's repair loop.
The decay rule is defined against NS-DPO. That method handles preference drift by discounting old preference data with a single factor $\gamma$. This works on its time-varying benchmarks, but one $\gamma$ cannot tell a stable interface apart from a passing instruction. P-ShareLoRA separates shared preferences from individual ones but assumes neither changes over time. Our ledger gives each constraint its own decay, driven by evidence.
Core Claim
Erosion under changing specifications should drop when two conditions hold together. The agent must be told which structural commitments are durable, as estimated from their survival history. Those commitments must also be enforced rather than merely suggested. Specifically, we predict three things. A ledger presented only in the prompt will help less than the same ledger enforced at decode time. A uniform decay rate will either over-constrain the agent and hurt pass rate, or under-constrain it and let erosion back in. Per-constraint persistence will avoid both outcomes.
Research Program
Extracting constraints. After each accepted checkpoint, a static analyzer proposes candidate constraints from the current repository. We restrict ourselves to constraints that can be stated as regular or context-free restrictions on the agent's edit output, so GREATGRAMMA can enforce them. There are four kinds:
- the signature text of public functions;
- a maximum indentation depth and line budget for each function that is already in the top complexity tier;
- a ban on new top-level definitions whose names collide with existing helpers;
- an edit format that confines changes to diff hunks outside frozen regions.
The depth and line caps are aimed squarely at the mechanism SlopCodeBench identifies. When an already-dense function hits its budget, the only legal way to add logic is to put it somewhere else, typically in a new function.
Scoring persistence. For each constraint $c$, let $s_c$ be the number of checkpoints it has survived unchanged and $r_c$ the number of times it had to be released. Its persistence is the smoothed survival rate
$$p_c = \frac{s_c + 1}{s_c + r_c + 2},$$
which is the posterior mean under a uniform prior. A constraint is enforced as a hard mask when $p_c > \tau$ and the current specification does not mention the entity it governs. Entity mentions are detected by matching identifiers and by an LLM judge that is audited on a held-out sample.
A constraint is released, and $r_c$ incremented, in two cases. Either the new specification touches it, or constrained generation fails the visible tests $k$ times while unconstrained generation for the same step would pass. That second test is a cheap counterfactual run on the same step.
Constraints that arise within a single checkpoint's repair turns form the disposable layer. An example is "keep this temporary debug helper." These are never promoted and are deleted when the checkpoint ends.
Offline validation. Before any agent runs, we check whether $p_c$ predicts stability in human code. We replay the commit histories of the 473 Python repositories that SlopCodeBench uses for calibration. For each function, we ask whether its persistence score at commit $n$ predicts that its signature survives to commit $n+m$. If persistence does not predict human stability, the ledger is encoding noise, and we would redesign extraction before running agents.
Baselines
The do-nothing reference is the SlopCodeBench agent harness as published: unconstrained decoding with the full history in context. Its reported performance is a checkpoint pass rate of about 14.8% for the best agents, with erosion increasing in 77% of trajectories and verbosity in 75.5% (SlopCodeBench). We rerun it with our base model, so all comparisons share the model, prompts and token budget.
Quality-aware prompting is the intervention SlopCodeBench itself tested. The file gives no numeric result for it beyond the qualitative finding that it fails after the first turn. We rerun it under the same budget.
Syntax-only constrained decoding uses GREATGRAMMA with the plain Python grammar and no project constraints. It isolates whether syntactic validity alone affects erosion. We expect it to match the reference.
Ledger-in-prompt writes the same durable constraints into the prompt as explicit instructions, with no masking. This is the key ablation for enforcement. It tests whether the agent simply did not know its commitments, or knew them and broke them anyway.
Lint-and-retry is the strongest competitor. The agent generates without constraints, a checker tests the same durable constraints, and violations are fed back for regeneration. We cap it at the same total generated tokens as the masked variant, including retries. It is strong because it enforces the same rules with no decoder changes, and it can use the violation message as feedback.
Two decay ablations isolate the timescale ingredient. Freeze-all enforces every extracted constraint forever, equivalent to $\gamma = 1$. Uniform decay discounts all constraints by one NS-DPO-style factor $\gamma$, tuned on the same development split with the same number of trials as $\tau$ and $k$.
Beating the field means two things on the held-out problems. First, a lower fraction of trajectories with rising erosion and verbosity than every baseline. Second, a checkpoint pass rate no worse than the do-nothing reference within the confidence interval computed over seeds. Beating lint-and-retry specifically requires either lower erosion or equal erosion at fewer tokens.
Why not just…?
Why not just prompt the agent to write clean, modular code? SlopCodeBench already ran this experiment. Quality prompts reduced first-turn complexity, did not prevent degradation across later checkpoints, and slightly lowered correctness at higher cost (SlopCodeBench). The ledger-in-prompt baseline goes a step further: it gives the agent the specific commitments rather than generic advice. If ledger-in-prompt matches the enforced version, the contribution is the ledger alone and decode-time masking is unnecessary. We would report that outcome as the finding.
Why not just check the output and retry? This is the real threat, and we do not argue it away. Masking and lint-and-retry enforce identical rules. Masking spends no tokens on rejected drafts, but retry can steer with a readable error message. The matched-token comparison settles it. If retry equals masking, the decoding component reduces to an efficiency detail, and the claim narrows to the persistence ledger, which the decay ablations test independently of how enforcement is done.
Why not just keep the whole history in a long context, or truncate it? Flat histories are what current agents already use. SWE-bench reports that models drown in large contexts: Claude 2 resolves only 1.96% of issues unassisted, rising to 4.8% with oracle file localization. The do-nothing reference is exactly the flat-history condition.
Why not just decay everything uniformly? That is NS-DPO's approach transplanted to constraints. The uniform-decay ablation tests it directly. Our prediction is that no single $\gamma$ is good on both pass rate and erosion at once, because public signatures and checkpoint-specific details need opposite treatment.
Proposed Experiments
The primary evaluation runs all variants on SlopCodeBench. We split the 36 problems into development and test sets at the problem level, and use three or more seeds per configuration. We report four outcomes:
- checkpoint pass rate;
- the fraction of trajectories with increasing structural erosion and with increasing verbosity;
- the agent-to-human verbosity and erosion ratios, against the published 2.3x and 2.0x;
- added decoding latency, against GREATGRAMMA's 5–32 ms per token.
Hard caps invite gaming. An agent can satisfy a line budget by cramming logic onto long lines or by spawning trivial wrapper functions. To catch this, we also measure quality on checks the constraints never see: duplicated-line counts, the size distribution of newly created functions, and blind human ratings of maintainability on a sampled set of final repositories.
Three checks decide the outcome. First, persistence must predict human signature survival in the 473-repository replay better than a recency-only predictor. Second, the enforced ledger must reduce erosion relative to ledger-in-prompt at equal pass rate. Third, per-constraint persistence must beat both freeze-all and the best uniform $\gamma$ on the combined pass-rate and erosion criterion. Lint-and-retry is reported throughout, so a reader can see exactly how much of any gain is due to decode-time masking.
References
- SlopCodeBench: Measuring Code Erosion Under Iterative Specification Refinement
- Grammar-Based Patches Generation for Automated Program Repair
- Gamma: Revisiting Template-Based Automated Program Repair Via Mask Prediction
- Flexible and Efficient Grammar-Constrained Decoding
- Hierarchical Temporal Convolutional Networks for Dynamic Recommender Systems
- Disentangling Long and Short-Term Interests for Recommendation
- Right Now, Wrong Then: Non-Stationary Direct Preference Optimization under Preference Drift
- A Shared Low-Rank Adaptation Approach to Personalized RLHF
- SWE-bench: Can Language Models Resolve Real-world Github Issues?