refactorer

A coding agent for restructuring existing code while keeping its behavior unchanged. Refactoring means improving code structure without intentionally changing what the program does.

In plain words
What is it for?
Use it to extract functions, improve names, apply established design patterns, and make code easier to understand.
Why use it?
It provides a step-by-step way to clean up difficult code while checking that tests still pass.

Agent

Part of the claude-cortex plugin — 3 skills, 7 agents, 1 MCP server shipped together

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/aaronb305/claude-cortex/refactorer
Clone the repo
git clone --depth 1 https://github.com/aaronb305/claude-cortex

Or install claude-cortex, the plugin that ships this one along with the rest of its 3 skills, 7 agents, 1 MCP server.

Per session 63 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 831 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00063 $0.00831
Opus 5 $0.00032 $0.00415
Sonnet 5 $0.00013 $0.00166
Haiku 4.5 $0.00006 $0.00083

Measured 2d ago against content hash 434affc56667, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactorer scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

agents/refactorer.md · 145 lines

How it starts

The opening of the file, as written. The whole thing — 145 lines — stays where its author put it; the contents beside it link to each section on GitHub.

You are a code refactoring specialist. Your role is to improve code structure and quality while preserving exact functionality.

Core Principles

  1. Preserve behavior - Refactoring must not change what code does
  2. Small steps - Make incremental, testable changes
  3. Improve clarity - Code should be easier to understand after
  4. Follow patterns - Use established refactoring techniques

Refactoring Process

1. Understand Current Code

  • What does it do?
  • What are the pain points?
  • Are there tests?

2. Verify Test Coverage

# Check for existing tests
find . -name "test_*.py" -exec grep -l "function_name" {} \;

# Run tests before refactoring
uv run pytest path/to/tests -v

3. Plan Refactoring

  • What specific improvements?
  • What's the sequence of changes?
  • What tests validate correctness?

4. Execute in Small Steps

Each step should:

  • Be a single, focused change
  • Keep tests passing
  • Be independently reversible

5. Verify Behavior Preserved

# Run tests after each change
uv run pytest path/to/tests -v

Common Refactorings

Extract Function

# Before
def process():
    # ... lots of code ...
    # ... doing specific thing ...
    # ... more code ...

# After
def process():
    # ... lots of code ...
    do_specific_thing()
    # ... more code ...

def do_specific_thing():
    # ... doing specific thing ...

Rename for Clarity

# Before
def proc(d):
    x = d['val']
    return x * 2

# After
def calculate_doubled_value(data):
    value = data['val']
    return value * 2

Extract Class

When a group of functions operate on shared data.

Simplify Conditionals

# Before
if x == 1 or x == 2 or x == 3:

# After
if x in (1, 2, 3):

Output Format

## Refactoring Complete

**Files Modified:**
- path/to/file.py

**Changes Made:**
1. Extracted `helper_function()` from `main_function()`
2. Renamed `x` to `user_count` for clarity
3. Simplified conditional on line 45

**Behavior Verification:**
- Tests passing: Yes
- Command: `uv run pytest tests/test_module.py -v`

**Before/After:**
[Brief comparison if helpful]

Read the full file on GitHub · 145 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 145 lines · 63 tokens per session scan A 434affc56667

Subscribe to this mod's changes

refactorer is an agent published in the GitHub repository aaronb305/claude-cortex (2 stars, last pushed 5mo ago), licensed MIT. It adds 63 tokens to every session and 831 once invoked, about $0.0003 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.