Case Study: Refactoring at Scale with Agents
codescene.com Β· article Β· 2026-09-24 source β Reader π₯οΈ
Behavioral Code AnalysisClosed-Loop Coding Agents (Compiler-Style vs. Chat-Style)Code QualityCoding AgentsLegacy & Organizational ForgettingTechnical DebtToken Efficiency
Large-scale legacy refactoring, once economically prohibitive, becomes tractable when a coding agent is paired with a deterministic code-quality feedback loop (CodeScene's Code Health metric) and automated correctness verification β demonstrated by uplifting a 300,000-line C codebase from Code Health 5.6 to a perfect 10.0 in three weeks for roughly $4,000 in tokens, work the author estimates would have taken 12-18 months of expert developer time before AI.
practical Β· narrative case study with argumentative framing β Written by Adam Tornhill (CodeScene founder/CTO) as a vendor case study: narrates a concrete large-scale refactoring project (credited to Daniel Webb and Dr. Markus Borg) and argues from it to a general claim about the economics of technical-debt remediation.
Structure
| Part | Role |
|---|---|
| Opening claim: code quality as a barrier to agentic coding | context |
| Case study setup: Street Fighter III: 3rd Strike, 300k lines of C, replay-trace correctness harness | evidence |
| The process in numbers (3 weeks, 252k lines modified, 726 files, 22 recipes/82 notes, model comparison) | evidence |
| An AI-designed playbook beyond human capacity | evidence / argument |
| Measuring engineering outcomes: why Code Health was trusted as the target metric | argument |
| Business value summary (defect reduction, token-waste reduction, cost) | conclusion |
| 'Is technical debt now a solved problem?' closing section | conclusion / caveat |
Key terms
- Code Health Behavioral Code Analysis
CodeScene's proprietary 0-10 code-quality score, used here as the deterministic target metric that refactoring agents optimize against and are judged by β chosen specifically because it was independently validated (in prior CodeScene research) as a proxy for productivity, defect density, AI correctness, and token efficiency, not merely a convenient automatable number. - Playbook
An iteratively-built, versioned set of named refactoring recipes (with documented preconditions, success/failure outcomes, and additional notes) that the agents accumulate as they work through a codebase, making each subsequent refactoring task more effective than the last. - AI-ready code
Code structured such that AI coding agents perform reliably and cost-efficiently on it β the stated end goal of the refactoring, and the reframing of 'code quality' as something done in service of AI performance, not only human maintainability.
Propositions
- explicitcorroboratedPoorly structured code and technical debt are a direct, measurable barrier to agentic coding performance, not only a human-maintainability concern.conf: highsupports βBrownfield Agentic Engineering Β· p1 β Both treat poor codebase structure/technical debt as a direct, measurable constraint on agent performance, not just a human-maintainability concern β motivating the zone/characterization-test discipline here and the Code-Health-first discipline there.
- explicitA 300,000-line C codebase (Street Fighter III: 3rd Strike) was refactored from Code Health 5.6 to a perfect 10.0 in three weeks at roughly $4,000 in token cost, versus an estimated 12-18 months of expert developer time in the pre-AI era β an order-of-magnitude reduction in elapsed project time and cost.conf: highsupportsIs AI coding on average 17.3x faster or 1.3x faster? Both. β LinkedIn post by Fabrice Bernhard Β· p10 β Empirically demonstrates, at real scale with real numbers, that legacy modernization is a tractable, worthwhile target β corroborating that it's a genuine bottleneck-aligned use case rather than an aspirational one.extendsForskning pΓ₯ digitalisering 2026 uke 19 Β· p9 β Where that document asserts LLMs 'can likely' refactor legacy code faster/better than humans, this case study supplies a concrete, measured instance (300k LOC, 3 weeks, $4,000) with explicit qualification of failure modes.
- explicitcorroboratedFunctional correctness throughout the refactor was verified by a replay-trace harness comparing rollback-state hashes frame by frame, not by human code review alone β automated, deterministic equivalence checking was the actual safety mechanism that made large-scale agentic refactoring trustworthy.conf: highextendsThis coding agent doesnβt want your feedback β it ships without it Β· p6 β Both describe a closed-loop agent pattern (deterministic feedback, bounded autonomous iteration), but this case study demonstrates it at far larger scale and with a richer verification oracle (frame-exact replay-trace equivalence) than a type-checker-and-retry loop.supports βBrownfield Agentic Engineering Β· p8 β Both use a shadow/replay-comparison oracle (diff old vs. new behavior on real traffic or recorded state) as the substitute for a trustworthy test suite β the same pattern applied to a web backend here and to a game-engine refactor there.
- explicitA larger, more capable model (Claude Opus) substantially outperformed smaller models (Sonnet, 'Terra'; also compared against Codex with 'Sol') on this task specifically because of its ability to capture and document discovered refactoring patterns into the emerging playbook, not just raw code-generation quality β smaller models plateaued at a local optimum on individual files and could not progress on remaining code smells.conf: high
- explicitThe agents discovered and named domain-specific refactoring recipes (e.g. 'Shared Index Range', 'Action Parameter', 'Uniform Step Table') that are structurally different from the generic refactoring catalogue (Fowler) a human expert would apply, suggesting generic refactorings are a foundation on which domain-specific, AI-discovered refactorings then build.conf: high
- explicitNot all refactoring attempts succeeded β some made Code Health worse β and documenting these failures in the playbook alongside successes was treated as essential to the process's effectiveness, not an embarrassing footnote.conf: high
- explicitcorroboratedCode Health was trusted as the agent's optimization target specifically because it had already been independently validated (in prior CodeScene research) as a proxy for real engineering outcomes β 10x faster evolution and 15x fewer defects in healthy code, improved AI correctness, and reduced token usage β which the author explicitly frames as the answer to the risk that 'give an agent a metric and it will happily optimize for any metric.'conf: highsupportsFragments: February 18 Β· p12 β Corroborates, from a different research lineage (CodeScene's AI-Ready-Code whitepaper cited in this article), the claim that LLMs behave more reliably on healthier codebases β the whole case study's premise is to engineer that healthiness first.supports βCode Red: The Business Impact of Code Quality -- A Quantitative Study of 39 Proprietary Production Codebases Β· p1 β This paper is the original quantitative study (Tornhill & Borg, 2022) behind the '15x fewer defects' figure that the later agentic-refactoring case study cites as the prior independent validation justifying trust in Code Health as an agent's optimization target.extends βPractices I Abandoned with Agents: An Ode to Test-Driven Development Β· p6 β Same author/company: Code Health is trusted as an agent optimization target precisely because it's an independently-validated, tooling-enforced proxy β the same 'enforce via tooling, not manual inspection' move this article makes for tests.
- explicitcorroboratedTechnical debt is 'a more urgent problem than ever, not less' after this case study, because organizations that adopt AI coding naively onto unhealthy codebases are likely to compound existing problems rather than see productivity gains materialize automatically.conf: highsupports βThomas Nys' Post Β· p4 β Both argue technical debt becomes more, not less, urgent once AI coding is adopted, because naive AI adoption onto unhealthy codebases compounds rather than resolves the underlying problem.
- implicitThe case study's favorable economics may not generalize to a typical enterprise legacy system: the codebase was an open-source game decompilation with deterministic, frame-perfect replayable state (enabling the correctness harness) and no external dependencies, databases, concurrency, or production constraints β conditions most enterprise codebases don't share.conf: medium
Arguments
Large-scale technical-debt remediation, previously economically unviable for most organizations, is now a viable investment given the right agent/feedback-loop infrastructure. strength: medium
Explicit premises:
- 300k lines of code were uplifted to perfect Code Health in three weeks for roughly $4,000, versus an estimated 12-18 months of expert developer time.
- Code Health improvements have been independently validated to deliver real productivity, defect-reduction, and token-efficiency benefits downstream.
Implicit premises:
- Organizations can obtain or build the necessary infrastructure (a deterministic quality-feedback loop, an automated correctness/equivalence check) cheaply relative to the refactoring itself β an assumption the article doesn't test outside its own tool and this one codebase.
Inferences:
- Technical-debt backlogs previously deprioritized as unaffordable should be re-evaluated against this changed cost structure.
Safe large-scale AI refactoring requires two specific, non-negotiable ingredients: deterministic quality feedback (not the agent's own unaided judgment) and automated correctness verification (not human code review at this pace and scale). strength: high
Explicit premises:
- The author states explicitly: 'the key... is ensuring that agents aren't working blindly,' citing deterministic quality feedback and automated tests/equivalence checks as 'absolutely essential' safeguards.
- Code Health was pre-validated against outcomes before being adopted as the target metric, addressing the Goodhart's-law risk directly.
Implicit premises:
- Human code review of 252,055 lines across 726 files in three weeks is not actually feasible at the depth needed to catch regressions β so the entire safety case has shifted from 'a human checked it' to 'an automated oracle checked it,' a premise the article assumes rather than argues.
Inferences:
- Codebases lacking any deterministic, automatable definition of correctness (the common case β most business logic isn't as cleanly checkable as a game's replayable state) may not be able to replicate this result regardless of agent capability or token budget.
Narratives
- The Street Fighter III: 3rd Strike case study itself: 300,000 lines of 1990s arcade-game C code, refactored under a replay-trace verification harness that confirmed 'we could still play the game as before.'
β Makes the correctness-verification claim viscerally concrete and credible β a deterministic, replayable game state is a case where 'did we break anything' has an unusually clean, automatable answer, which is exactly what makes the rest of the argument's safety claims land. - A quoted practitioner reaction: 'I had my agentic coding wins in the past, but this is the first time I'm experiencing a true superhuman AI performance at scale. And I say that as someone with three decades of experience in large-scale system development.'
β Supplies a credibility appeal from a named, experienced practitioner (implicitly Daniel Webb or Markus Borg) rather than the vendor's own marketing voice, supporting the magnitude claim about model capability (proposition 4).
Questions addressed
- β
Can coding agents remediate large-scale technical debt economically, in a domain where human-driven rewrites have historically failed or been deprioritized as too risky and expensive?
Yes, demonstrated at 300k LOC for roughly $4,000 and three weeks, given a deterministic quality-feedback loop and an automated correctness-verification harness. - β
Which refactorings should an agent apply at scale, and are they the same ones a human expert would reach for?
No β the agents discovered and productized domain-specific recipes structurally different from a human's generic catalogue (Fowler), which the author treats as a genuine, if 'obvious in retrospect,' insight: generic refactorings are the foundation, domain-specific ones the adaptation. - β
Does this result mean technical debt is now a solved problem industry-wide?
No β explicitly addressed and rejected: naive AI adoption onto unhealthy codebases, without this feedback/verification infrastructure, is argued to compound debt rather than resolve it. - β
Is the Code Health metric a legitimate optimization target for an agent, or just a convenient, gameable proxy?
Argued legitimate, because it was independently validated against productivity, defect-density, AI-correctness, and token-efficiency outcomes before being adopted here β directly answering the Goodhart's-law risk the author names himself.
Gaps
- incompleteThis is an n=1 case study, authored by the vendor of the Code Health tool being validated, on a codebase chosen specifically for having deterministic, frame-perfect replayable state β a correctness-verification advantage most enterprise codebases (external APIs, databases, UI, concurrency, production constraints) don't have. The article never addresses how correctness would be verified at this pace and scale without an equivalent deterministic oracle.
Read next: Look for a second, independent (ideally non-vendor-authored) case study on a typical enterprise codebase lacking a replay-trace-style oracle, to test whether the result generalizes. - incompleteThe headline $4,000-tokens-vs-12-18-months-of-engineers comparison discloses neither the loaded engineer cost assumption, the token pricing used, nor whether the 12-18 month estimate is the author's own guess or drawn from a cited prior project β leaving the ROI figure directionally persuasive but not independently checkable.
Read next: Request or look for the underlying cost model and a citation for the 12-18 month baseline estimate. - incompleteThe article states that 'many' refactoring attempts failed or made Code Health worse, and that documenting these failures mattered, but gives no quantitative failure rate β e.g. how many attempts were needed per successful recipe, or what fraction of the 22 recipes required multiple tries. This number would materially change how repeatable (versus lucky) the three-week headline figure looks.
Read next: A follow-up post or dataset disclosing attempt-level success/failure counts, not just the final playbook. - incompleteThe playbook and its named recipes were discovered on a single codebase (C, a 2D fighting-game engine). The article doesn't address how much of the discovered playbook is reusable across codebases/domains versus specific to this one β which bears directly on whether the $4,000/three-week economics would hold on a second, unrelated codebase rather than being a one-off artifact of this particular code's structure.
Read next: A second case study on a different language/domain (e.g. a typical enterprise CRUD service) testing playbook transfer.
Closes gaps elsewhere
- Forskning pΓ₯ digitalisering 2026 uke 19 Β· g2 β Supplies exactly the evidence and failure-mode qualification that gap asked for: a measured before/after Code Health transformation with cost and time figures, a model comparison showing weaker models plateau at local optima, and explicit acknowledgment that many refactoring attempts failed or degraded quality along the way.
Related documents
- Fragments: February 18
- Forskning pΓ₯ digitalisering 2026 uke 19
- Code maintainability plummets in the AI coding era
- Coding Models Are Doing Too Much
- The Economic Benefit of Refactoring
- Code Red: The Business Impact of Code Quality -- A Quantitative Study of 39 Proprietary Production Codebases
- Why did Martin Fowler invite 60 engineers to a ski resort?
- Agentic Engine Optimization (AEO)
- [FULL WORKSHOP] AI Coding For Real Engineers - Matt Pocock, AI Hero
- The Almedra Token-Efficiency Benchmark, Updated For GLM 5.2