Skip to content

TRIM: Reducing AI-Generated CodeSlop via Agent Trajectory Minimization

🕒 Published (v1): 2026-07-20 17:06 UTC · Source: Arxiv · link

Why this paper was selected

Google Research; trajectory minimization cuts agent-generated code bloat — practical coding agent fix

Ask a follow-up

Open an assistant pre-loaded with this paper's context.

💬 Ask ChatGPT✦ Ask Claude

TL;DR

Coding agents accumulate speculative, abandoned, and temporary edits during iterative repair that persist into final patches even after tests pass—a phenomenon the authors formalize as CODESLOP. TRIM (Trajectory-guided Redundancy Identification and Minimization) exploits the temporal structure of agent repair trajectories to perform hierarchical counterfactual search, removing functionally unnecessary changes without re-synthesizing. Across four agent scaffolds and two benchmarks, TRIM reduces CODESLOP by 17.8%–32.9% at roughly half the validation cost of Delta Debugging.

Problem

Coding agents are evaluated purely on test pass/fail, but agent-generated patches routinely contain residual search artifacts—speculative edits, abandoned hypotheses, reverted-then-reintroduced changes—that survive into the final patch unchanged. These bloat patch size, increase reviewer cognitive load, and compound over time as agents handle larger codebases. No prior work provides a functional (behavioral) definition of this redundancy or an efficient algorithm to remove it.

Method

Formal definition. Let \(AP\) be an agent-generated patch and \(AP^*\) the shortest patch derivable from \(AP\) that still passes the task's test suite. CODESLOP is defined as: $\(\text{CODESLOP}(AP) = \text{len}(AP) - \text{len}(AP^*)\)$ where \(\text{len}(\cdot)\) counts modified lines. This is a behavioral, not static, criterion: a change is slop iff it can be removed while preserving observable behavior under execution.

TRIM algorithm. Given the raw agent execution trajectory \(\text{Traj}\), TRIM operates in three stages:

  1. Trajectory reconstruction. Filter \(\text{Traj}\) to retain only edit actions and feedback requests (test executions). Group edits between consecutive feedback requests into edit sequences: $\(\text{TrajR} = \langle (E_1, \text{FR}_1), (E_2, \text{FR}_2), \ldots, (E_k, \text{FR}_k) \rangle\)$ Replay the trajectory in temporal order to handle reverts and overwrites, normalizing heterogeneous editing primitives (sed, structured replace, etc.) into a unified before/after-text representation.

  2. Hierarchical counterfactual search. Coarse-to-fine removal at three granularities: (a) entire edit sequences \(E_i\), (b) files within surviving sequences, (c) individual atomic edits \(e_{ij}\). At each level, TRIM asks: can this group be removed while the remaining patch still passes validation?

  3. Validation. A candidate removal is accepted only if: (i) the resulting patch passes all task test files and (ii) \(\text{len}(\text{MP}) < \text{len}(\text{AP})\).

Two variants: TRIM-G (enforces 1-minimality—no single unit can be further removed) and TRIM-NG (no such guarantee, faster).

Key Contributions

  • First formal, behavioral definition of CODESLOP as removable functional redundancy in agent-generated patches (distinct from static verbosity metrics).
  • TRIM: a trajectory-aware, coarse-to-fine patch minimization algorithm that exploits edit-sequence hierarchy rather than treating the patch as an unstructured hunk set.
  • Empirical demonstration that agent-based self-minimization (prompting agents to shrink their own patches) fails or enlarges the patch in 3.8%–44.9% of cases—establishing that this is a hard problem for the agents themselves.
  • Evidence that minimized patches sometimes recover exact developer-written fixes.

Results

  • CODESLOP reduction: 17.8%–32.9% across CRASHFIXER, SWEAGENT, MINISWEAGENT, and OPENHANDS on Live-kBench and SWE-Bench.
  • Versus agent-based minimization baselines: 1.6×–3.1Ă— more CODESLOP eliminated.
  • Validation efficiency: TRIM requires roughly half the validation calls of Delta Debugging.
  • Correctness regression: negligible across all evaluated configurations.
  • Human-patch recovery: in some cases the minimized patch achieves exact agreement with the developer-written fix (e.g., SWE-Bench sympy__sympy-15017 reduced from 3-file, 5-hunk patch to a single corrected line).

Limitations

  • Behavior preservation is operationalized via the available test suite, not true behavioral equivalence; incomplete test suites may cause over-minimization or fail to expose regressions.
  • The search is restricted to patches derivable from \(AP\) by deletion—TRIM cannot synthesize a cleaner solution from scratch, so \(AP^*\) is an upper bound on quality, not a global minimum.
  • Requires access to the agent's execution trajectory, which is not always logged or exposed by agent scaffolds.
  • Coarse-to-fine ordering affects convergence speed; adversarial orderings (essential edits early, CODESLOP late) could slow convergence.
  • Evaluated only on program repair; generalization to other agentic coding tasks (feature addition, refactoring) is not demonstrated.

Relevance to Agentic AI / LLM Agents

TRIM addresses a fundamental quality gap in agentic software engineering: test-passing is a necessary but insufficient signal for patch quality, and agents have no intrinsic incentive to prune their own search artifacts. The trajectory-as-structure insight is broadly applicable—any multi-turn tool-using agent produces an execution history that encodes dependency information richer than the final artifact alone, and TRIM shows this history can be exploited for post-hoc optimization. This connects directly to work on agent scaffolding, multi-turn RL for code agents (e.g., CacheRL), and the growing concern about maintainability of AI-generated codebases at scale. The behavioral definition of CODESLOP also provides a clean evaluation criterion that could supplement pass@k metrics in coding agent benchmarks.