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
Log in to join the discussion.