<img src="https://secure.leadforensics.com/794635.png" style="display:none;">

Announcement: Deterministic PR Refactoring Agents

Reviewers can now trigger guided agentic refactorings directly from pull requests. Centralized workflow, no separate process or debate about what good code looks like.

Copied!

CodeScene’s new PR Refactoring Agent analyzes the branch using deterministic Code Health data, performs targeted refactorings, and commits improvements back to the PR.

The result is code that’s easier to review, AI-friendly, and more token efficient. And yes, we support all those promises with data. Your code deserves that.

PR_Delta_Analysis_Example_Refactoring_Agent

Fix Code Health degradations via a single click in the PR.

 

Why we built it

Refactoring rarely wins against feature pressure. And even when teams want to improve the code, refactoring is a specialized skill that takes time and experience to develop. That’s why many maintainability issues remain in codebases long after they start hurting delivery speed and increasing defect rates. 

This becomes even more important with AI-assisted development.

Agents depend on explicit structure and intention-revealing code. Unhealthy code increases defect risk, wastes tokens, and makes AI-generated changes hard to verify. As our research shows, unhealthy code will become a bottleneck for organizations looking to scale agentic development.

That’s why we built the PR Refactoring Agent.

Instead of relying on custom prompts or subjective preferences, the agent refactors based on deterministic Code Health signals and measurable targets.


How to use the refactoring agent 

Code_health_uplift_agentThe PR refactoring agent at work, offering a live feedback loop on its progress.

Under the hood, the PR Refactoring Agent combines CodeScene’s MCP server with specialized workflows focused specifically on Code Health improvements.

The agent currently supports two such workflows:

Workflow 1: Fix Code Health degradations

Automatically refactor the maintainability issues introduced in the current PR.

The agent limits itself to new degradations and avoids modifying previously existing issues elsewhere in the codebase. This keeps the workflow focused, reviewable, and safe to adopt incrementally.

Workflow 2: Uplift any code

Target a specific Code Health score and let the agent guide the refactoring process toward that structural goal.

By default, the agent aims for the next meaningful improvement step, for example from 5.2 to 6.0. But teams can raise the bar further and target AI-friendly code at 9.5 or even an optimal 10.0.

 

A centralized workflow for maintainability governance

The Code Health review and refactoring functionality is available through CodeScene’s MCP Server, too. The PR Refactoring Agent offers a centralized workflow that integrates directly into the review process where teams already collaborate.

A typical workflow looks like this:

1.  A reviewer triggers the agent from a pull request or merge request comment.

2. The workflow validates the requested action and model.

3. The agent analyzes the branch using Code Health data and performs the requested refactoring workflow.

4. Progress updates are reported back to the PR.

5. Generated improvements are committed directly back to the same branch.


Everything happens inside the existing review flow, where the whole team can inspect the changes, evaluate the outcome, and decide whether to accept the improvements.

 

Why deterministic guidance matters

AI coding tools are probabilistic by nature. Code Health is not.

The PR Refactoring Agent uses Code Health as a deterministic ground truth to guide and validate its behavior. Rather than guessing what “better code” means, the agent works toward explicit structural improvements that are measurable, reviewable, and outcome focused:

  • Healthy code significantly reduces AI-induced defect risk.

  • A Code Health above 9.5 dramatically lowers break rates during AI-assisted change.

  • Healthy code also reduces unnecessary token consumption.

token_waste_unhealthy_code

Token waste due to unhealthy code as a function of programming language.

Read more about how unhealthy code is burning your token usage

Get started 

The PR Refactoring Agent is available as part of CodeScene’s AI-powered Code Health workflows. To get started:

Existing CodeScene users: activate the refactoring agent as documented here.
New to CodeScene? Start your free trial, then activate as described in the documentation. 

Watch the 1-min demo 

 

Adam Tornhill

Adam Tornhill is a programmer who combines degrees in engineering and psychology. He’s the founder and CTO of CodeScene where he designs tools for code analysis. Adam is also a recognized international speaker and the author of multiple technical books, including the best selling Your Code as a Crime Scene and Software Design X-Rays. Adam’s other interests include modern history, music, retro computing, and martial arts.

Keep reading

See more

Unhealthy code is burning your token usage - here's the data

New research shows agents consume up to 50% more tokens on unhealthy code. Here's what the data reveals and how to mitig...

Making Legacy Code AI-Ready: Benchmarks on Agentic Refactoring

Benchmarks and use cases for agentic refactoring with the CodeScene CodeHealth MCP Server.

Agentic AI Coding: Best Practice Patterns for Speed with Quality

How CodeScene’s Code Health and MCP tools turn agentic speed into sustainable engineering practices.