refactor

A code-restructuring agent that changes how code is organized while keeping its external behavior the same. It can split large files, remove duplication, extract reusable parts, and simplify complicated functions.

In plain words
What is it for?
Use it to break up oversized files, share repeated logic, reduce complicated code, and improve structure when reliable tests already cover the code.
Why use it?
It makes code easier to read and maintain without intentionally changing what the software does. It uses existing tests before and after each change to check that behavior remains intact.

Agent

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/nxtg-ai/forge-plugin/refactor
Clone the repo
git clone --depth 1 https://github.com/nxtg-ai/forge-plugin
Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,993 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.00000 $0.01993
Opus 5 $0.00000 $0.00996
Sonnet 5 $0.00000 $0.00399
Haiku 4.5 $0.00000 $0.00199

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

Security

Grade A, and why

refactor 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.

docs/agents/refactor.md · 134 lines

How it starts

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

Refactor

Restructures code without changing behavior -- extracts functions, eliminates duplication, reduces complexity, and splits oversized files, all with a green test suite as the safety net.

Level L1 Vibe Coder
Category Domain Specialist
Model Sonnet

What It Does

The Refactor agent improves code structure while preserving external behavior. It takes an 800-line file and splits it into focused modules. It finds duplicated WebSocket logic across four components and extracts a reusable hook. It takes a function with cyclomatic complexity of 15 and simplifies it with early returns and extracted helpers. Every change it makes is protected by the test suite -- it runs tests before the refactoring to establish a green baseline, makes one structural change at a time, and runs tests after each step to verify behavior is preserved.

What makes this agent more disciplined than "just rename things" is its safety protocol. It will not refactor untested code. If a module lacks tests, it stops and tells you to generate them first (via the Testing agent). This is non-negotiable because refactoring without tests is not refactoring -- it is rewriting, and rewriting breaks things. The Refactor agent enforces the distinction: refactoring changes structure, never behavior, and tests prove it.

The agent encodes specific complexity thresholds: functions over 25 lines get extracted, cyclomatic complexity over 10 gets simplified, files over 300 lines get split, functions with more than 4 parameters get an options object, nesting deeper than 3 levels gets early returns. These are not arbitrary -- they are the thresholds where code comprehension drops sharply. The Refactor agent knows the catalog of refactoring patterns (Extract Function, Extract Hook, Replace Conditional with Polymorphism, Simplify with Early Returns) and applies the right one for each situation.

When to Use It

  • When a file has grown too large: When a service, component, or utility has accumulated responsibilities and needs decomposition into focused modules with clear boundaries.
  • When you see code duplication: When the same logic appears in multiple places and needs extraction into a shared function, hook, or utility.
  • When complexity is too high: When a function has deeply nested conditionals, long switch statements, or cyclomatic complexity that makes reasoning about behavior difficult.
  • When naming is misleading: When function names do not describe what they do, variable names are abbreviated, or class names do not reflect their single responsibility.

Read the full file on GitHub · 134 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 · 134 lines · 0 tokens per session scan A 838247b19e91

Subscribe to this mod's changes

refactor is an agent published in the GitHub repository nxtg-ai/forge-plugin (5 stars, last pushed 12d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,993 tokens. 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.