arXiv:2610.33343cs.SEOct 2026

Manuscript Offprint

arXiv:2610.33343

From Cosmetic to Structural: A Paired Experiment on Metric-Guided Agentic Refactoring

Nadim Hagatulah, Markus Borg, Emma Söderberg

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

The role of coding agents is expanding beyond writing new code toward maintaining existing systems as AI teammates. Yet recent studies show that agent refactorings favor superficial, consistency-oriented edits over structural transformations needed to resolve Technical Debt (TD). We investigate if explicit static-analysis feedback closes this gap. Through the CodeScene MCP server, we give an open-source agent (gptme) access to code smell reports and CodeHealth (CH) scores (1–10, higher is better), and compare guided against unguided refactorings in a paired experiment covering 24,620 single-file competitive programming solutions in C++, Java, and Python. We find that guidance shifts the agent from performing cosmetic edits toward structural operations, raising the rate of effective refactorings by 21–26 percentage points. The benefit depends on initial code quality, with language-dependent break-even points at CH 4.9–7.1, below where the structural transformations pushed by guidance increasingly risk regression, indicating where the agent needs more support to refactor. Guided runs also shift the failure profile toward logical test failures, introduce new coupling smells, and consume ∼3−5×\sim3-5\times more tokens over ∼2×\sim2\times as many interaction turns. Our results indicate that AI teammates can benefit from similar static-analysis feedback that human developers already use, enabling structural transformations toward more effective TD remediation, at a higher token cost and more interaction turns.

Keywords: agentic software engineering, large language models, refactoring, technical debt, static analysis, code smells, empirical software engineering

1 Introduction

Refactoring is the established practice for remediating code smells [1], which accumulate as Technical Debt (TD) when left unresolved. TD has been found to lead to inflated development costs [2, 3], increased defect and security risks [4, 5], while also increasing developer unhappiness [6, 7]. Consequently, managing TD is important for sustainable, long-term software development [8].

Despite the positive effects of refactoring [2, 9, 10], it remains an infrequent activity among developers [11]. Martini et al. [12] report that only 26% of developers use tools to track TD, and 7% of organizations systematically monitor it. One reason is that refactoring requires domain knowledge and a deep understanding of the existing code to anticipate the consequences of changes [13]. Moreover, improper refactoring risks altering semantic behavior or introducing bugs [14]. The activity is also frequently deprioritized in favor of other tasks such as feature development [15]. As a result, developers often leave design issues unresolved rather than risk breaking functionality.

Large Language Models (LLMs) offer an automated alternative, generating refactored versions of code on demand [15–19]. The advancement of LLMs has further enabled autonomous multi-turn agents that plan, act through tools, and iterate on complex Software Engineering (SE) tasks with reduced human intervention [20]. This signals a shift toward what Li et al. [21] describe as AI teammates in SE. If such agents are to act as teammates on projects, they must also take on code maintenance.

Large-scale studies of agentic refactoring by Horikawa et al. [22] and Ottenhof et al. [23] show that agent-authored refactoring commits are dominated by superficial, consistency-oriented edits (e.g., variable renaming and annotation changes) rather than the structural transformations that remediate design flaws (e.g., Extract Method). Cordeiro et al. [16] attribute such misaligned modifications to a missing explicit purpose. Without concrete refactoring goals, LLMs fall back on their latent notion of clean code, and the authors suggest enriching prompts with guided information about specific issues. Furthermore, on the concept of AI-Friendliness, introduced from Thoughtworks Technology Radar [24], Borg et al. [25] empirically investigated this direction, showing that code written for human comprehension is also easier for LLMs to refactor without breaking semantic behavior.

These observations raise the question of what happens when an autonomous coding agent is given access to the same static-analysis feedback that modern development infrastructure already provides. Quality gates in IDEs and continuous integration (CI) pipelines routinely surface maintainability issues to human developers 12^{12}, and the Model Context Protocol (MCP) makes it practical to expose the same tooling to agents.

In this study, we evaluate the impact of providing an agent with explicit static-analysis feedback through the CodeScene MCP Server 3^{3} (version 0.1.2, hereafter, the MCP server), exposing tools that report the occurrence, severity, and position of code smells, together with an aggregated CodeHealth (CH) score of the file. The CH metric has been shown to predict defect count and development velocity [4, 26, 27].

1^{1}https://www.sonarsource.com/products/sonarqube/

2^{2}https://codescene.com/product/automated-code-health-reviews

3^{3}https://github.com/codescene-oss/codescene-mcp-server

We run a paired experiment on 24,620 files across C++, Java, and Python (49,240 agent runs), using the open-source gptme agent harness powered by the open-weight Qwen3-Coder-30B-A3B model, comparing a Basic instruction (the agent relies on its intrinsic judgment) against a Guided instruction (the agent iterates with the MCP tool). Throughout the paper, metric guidance refers to the Guided configuration as a whole, which bundles the metric-referencing instruction, the availability of the tool, and the grounding of each iteration in external tool feedback (Section 3.3).

We evaluate the agents on single-file competitive programming solutions instead of production modules. While production codebases offer realism, they are more complex, often lacking complete test suites, controlled quality distributions, and reproducibility required for a strict, paired experimental design. By trading external realism for internal validity, our corpus isolates the agent's structural reasoning from complexities in enterprise, such as cross-file dependencies and build systems. Consequently, our findings measure how metric feedback alters agent behavior, rather than projecting end-to-end maintenance performance on production codebases.

The study is structured around the following Research Questions (RQs), moving from what guidance achieves, through what the agent does differently, to when it helps and what the overhead is:

• RQ 1_{1}: How does metric guidance affect the outcomes of agentic refactoring?

We compare the pass rate (proxy for behaviour preservation), CH improvement rate, and the combined effective refactoring rate between Basic and Guided runs to measure how guidance changes what the agent achieves.

• RQ 2_{2}: How does metric guidance change the refactoring operations applied and the code smells addressed?

The outcome does not necessarily distinguish structural work from superficial edits. We check the applied refactoring operations and per-smell remediation rates to characterize the work of each agent.

• RQ 3_{3}: How does the initial CH of a file moderate the effect of metric guidance?

Motivated by the AI-Friendliness hypothesis [25], we test if the potential benefit of guidance depends on how degraded the code is to begin with.

• RQ 4_{4}: How does metric guidance change the failure modes of agentic refactoring?

When guided refactorings fail, do they fail differently? We classify failures into compile errors, runtime errors, test failures, and exceptions, and map error messages to their causes.

• RQ 5_{5}: What computational overhead does metric-guided refactoring introduce?

We expect a tool-grounded feedback loop to bring increased tokens and interaction turns resulting in a trade-off that practitioners need to consider.

We present a large-scale, paired evaluation of static-analysis guidance for an agent performing refactoring operations, covering three languages, 24,620 files, and 49,240 agent runs. We provide evidence that metric guidance redirects agent behavior from superficial refactorings toward more meaningful structural transformations, raising the effective refactoring rate by 21 to 26 percentage points (pp) under the guiding metric, while also introducing some new coupling-related trade-off smells. Our moderation analysis extends the AI-Friendliness hypothesis to the agentic setting, finding that the effect of metric guided refactorings grows with the initial CH of the file, with language-specific break-even points, the initial CH at which Guided and Basic runs are equally likely to produce an effective refactoring. It ranges from CH 4.9 (C++) to 7.1 (Python). Above it, guidance helps by up to +34 pp, while below it, guidance lowers the effective refactoring rate (driven by a lower test pass rate). These break-even points are specific to our metric, LLM, harness, and languages, and should not be read as general CH thresholds for agentic refactoring. We further characterize how guidance shifts the failure profile toward test failures, and quantify the overhead of the iterative feedback loop ( ∼2×\sim2\times turns, 3−5×3-5\times tokens). All data, prompts, and analysis scripts are available in a replication package [28].

References (52)