TL;DR
Coding agents often produce "spaghetti code" that passes tests but is a nightmare to maintain. This paper introduces Metrics-in-the-Loop (MITL), a lightweight inference-time intervention that provides agents with real-time feedback on code complexity and test coverage, successfully steering them toward cleaner, more modular code without sacrificing functional correctness.
What Problem Does This Paper Address?
As AI coding agents move from writing snippets to building entire codebases, they face a "software aging" problem. Current agents are primarily guided by whether their code passes a test suite. While functional, this narrow focus often results in monolithic, poorly structured implementations that accumulate "erosion"—a concentration of high cyclomatic complexity and gaps in test coverage.
When an agent builds upon its own messy code in long autonomous runs, these weaknesses compound, making the codebase increasingly fragile and difficult to extend. Human developers can't keep pace with the volume of code generated, creating a critical need for automated "re-railing" mechanisms that ensure agent-written code remains maintainable over the long term.
Methods
The authors propose Metrics-in-the-Loop (MITL), a plugin for agent harnesses that requires no additional training or reference solutions. MITL operates during the agent's "trajectory" (its sequence of actions):
- Measurement: After any turn where the agent modifies code, MITL automatically calculates two key metrics:
- Cyclomatic Complexity (CC): A measure of the number of independent execution paths through a function. The authors set a target limit of 6.
- Test Coverage: Identifying specific lines of changed code that the agent's own tests failed to execute.
- Feedback Injection: If thresholds are exceeded, a short, actionable report is injected directly into the agent's context window before its next turn.
- Refactoring: The agent is given the opportunity to refactor the flagged functions or strengthen its tests before proceeding with the main task.
The researchers tested four state-of-the-art models (DeepSeek V4.1 Flash, GLM-5.3 Flash, Claude Haiku 5.5, and GPT-6 Luna) across various experimental "arms," ranging from no feedback to combined complexity and coverage reports.
Datasets
The approach was evaluated on three long-horizon coding benchmarks designed to simulate real-world development:
- Commit0: Agents must reconstruct entire Python libraries from scratch based on specifications.
- SlopCodeBench: Evaluates iterative program development over successive checkpoints and evolving requirements.
- SWE-EVO: A benchmark based on real-world software evolution, requiring agents to update published projects to their next release.
Key Insights and Ideas
- Agents are Responsive to Mechanical Feedback: One of the most significant findings is that agents actually listen. When flagged, agents re-edited 54–88% of complex functions on Commit0 and nearly all (87–94%) on SlopCodeBench.
- "Re-railing" Works: The intervention successfully prevents agents from building on a shaky foundation. By providing timely measurements, the system redirects the agent before complexity becomes entrenched.
- The Correctness-Maintainability Decoupling: Interestingly, writing "cleaner" code did not immediately lead to higher pass rates on held-out tests. The authors suggest this is because current benchmarks are either too small or too saturated to show the long-term benefits of maintainability, which typically pays off as codebases scale to millions of lines.
Key Results
- Significant Erosion Reduction: Complexity feedback (the 'C' and 'CT' arms) drastically reduced the erosion index ($E_{10}$) across all models and benchmarks. On SlopCodeBench, erosion was kept at or below 0.022 at every checkpoint.
- Stable Correctness: The intervention achieved these structural improvements with no detectable negative effect on functional correctness. As shown in Figure 1b, the "arrows" move vertically (improving structure) while remaining stable horizontally (maintaining pass rates).
- Cumulative Benefits: As seen in Figure 5, the gap in erosion between agents with feedback and those without grows over the course of a run, proving that MITL is particularly effective for long-horizon tasks.
Limitations and Future Work
- Language Scope: The current study is limited to Python. Future work is needed to see if these patterns hold for languages with different structural paradigms.
- Benchmark Saturation: Current benchmarks may not be "long" enough. The authors call for new benchmarks with even longer sequences of change requests to truly measure the relationship between code cleanliness and long-term bug rates.
- Semantic Complexity: Cyclomatic complexity is a "mechanical" metric. Future iterations of MITL could incorporate LLM-based semantic feedback to address logic-level maintainability issues that simple path-counting might miss.
Summary updated