c/agents › command
0votes

Claude Code subagent: refactorer

TemplateLow risk Tested
submitted by CommandVault editors to c/agents · 2 uses

A .claude/agents/refactorer.md that simplifies code without changing behavior.

Template · 2 variables
---
name: refactorer
description: Simplifies and restructures code without changing behavior. Use when a file is hard to read, duplicated or too large.
tools: Read, Grep, Glob, Edit, Bash
---

You refactor code in {{project_name}} with zero behavior change.

1. Run `{{test_command}}` first and note the results. If coverage of the target code is thin, add characterization tests before refactoring.
2. Make small, separate steps: rename, extract function, remove duplication, simplify conditionals. Run tests after each.
3. Don't change public interfaces, outputs or error messages unless asked.
4. Stop if a step makes tests fail and you can't explain why.
5. Report each step, the before/after line counts and the test results.
0 commentsreport

Guide

What you get: A refactorer that removes duplication in small steps, runs your checks after each step, and keeps public functions and outputs the same. Best for Developers with a long or duplicated file they want simpler without changing behaviour..

Before you start

  • A test or check command that covers the code (fill in test_command)
  • Permission for the agent to run that command and edit files
  • Fill in project_name

Worked example (Oct 17, 2026, Claude Code 2.1.296; Codex 0.151.0 (gpt-5.6-sol))

Input

Planted src/lib/report.ts: customerReport and adminReport are two near-identical 26-30 line loops (59 lines total). scripts/check-report.mjs compares both outputs (normal and empty input) against fixed expected strings and passes. test_command = `node scripts/check-report.mjs`. Request: "Use the refactorer subagent to simplify src/lib/report.ts: customerReport and adminReport are long and duplicated."

Result

Claude Code: the refactorer subagent ran the check first, added a temporary old-vs-new comparison over 3,005 inputs, then extracted orderTotal, orderLine, summarize and renderReport in two steps; file went from 59 to 47 lines, each export to 4 lines, signatures unchanged. Codex: the refactorer agent extracted shared helpers into one private buildReport; 59 to 52 lines. Verification: I re-ran `node scripts/check-report.mjs` in both copies: "all checks passed" (4/4); `npx tsc --noEmit` shows no errors in report.ts (only stale .next type errors that the untouched base project also has).

Test project: fresh create-next-app (Next.js 16.4.2, TypeScript), Node 24.14.1. Blanks filled with project_name = "demo-app (Next.js 16 + TypeScript)". Claude Code: body saved as .claude/agents/<name>.md, run headless with `claude -p "Use the <name> subagent to ..." --permission-mode acceptEdits`. Codex: converted to .codex/agents/<name>.toml (name, description, developer_instructions) and run with `codex exec -m gpt-5.6-sol -s workspace-write "Use the <name> agent to ..."`. Subagent use was confirmed from Claude Code's stream-json log (Agent call with subagent_type) and from Codex session logs (spawn_agent with agent_type, and the agent's instructions present in the child session). Claude Code allowed tools: Bash(node *), Bash(git diff *), Bash(git status *); `npx tsc` was therefore blocked for the subagent. Claude's subagent wrote its comparison script and a copy of the original file in its own Claude Code session scratchpad under /private/tmp, outside the repo.

How to check it worked

  • Run the test command yourself before and after and compare
  • Read the diff: exported names and signatures should be unchanged
  • Run your type checker (for example `npx tsc --noEmit`)

Common problems

  • If the checks only cover a few cases, behaviour changes in untested paths can slip through; add characterization tests first
  • Small intentional differences can appear (here, underline length now follows the title length); read its notes
  • It needs permission to run the test command, or it cannot check its own steps

Add this subagent to your AI tool

Converted for each tool's agent format. Fill in the blanks above first.

One command
npx commandvault add claude-code-subagent-refactorer-3a6be62c --tool claude
Save as .claude/agents/refactorer.md
---
name: refactorer
description: Simplifies and restructures code without changing behavior. Use when a file is hard to read, duplicated or too large.
tools: Read, Grep, Glob, Edit, Bash
---

You refactor code in {{project_name}} with zero behavior change.

1. Run `{{test_command}}` first and note the results. If coverage of the target code is thin, add characterization tests before refactoring.
2. Make small, separate steps: rename, extract function, remove duplication, simplify conditionals. Run tests after each.
3. Don't change public interfaces, outputs or error messages unless asked.
4. Stop if a step makes tests fail and you can't explain why.
5. Report each step, the before/after line counts and the test results.

Use it: Claude delegates to it when a task matches, or ask: "Use the refactorer subagent to …".

From Claude Code's docs , checked Oct 17, 2026.

More from c/agents

See all of c/agents →
0 comments

Log in to join the discussion.