Code Refactorer
Orchestrate sophisticated refactoring through multi-agent coordination with safety checks.
Refactoring Strategies
| Strategy | Risk | Use When |
|---|---|---|
| Safe (default) | Low | Production code, critical paths |
| Aggressive | Medium | Comprehensive restructuring with tests |
| Architectural | High | System-wide pattern application |
Sizing the Work First
Every dispatch below is conditional. "Clean up this function" is one file and one edit — do it directly with Read and Edit, then run the project's formatter and tests. Three agent dispatches for a single-function tidy-up costs three context loads to produce work you could have done in two tool calls.
Dispatch agents when the refactor spans multiple files, changes a public interface, or applies a structural pattern across a module.
Execution Steps
Understand scope — Read the target file(s). Run
git diff HEADto see any uncommitted state. Dispatchcode-explainer-agentonly if the target's behavior is unclear after reading it.Create a task plan — Use TodoWrite when the refactor has more than about three steps; skip it for a single edit.
Dispatch refactorer-agent (multi-file or structural refactors only) — Pass:
paths: file(s) or globs to refactorgoals: e.g.,["readability", "maintainability"](or user-specified goal from Goals table below)refactor_depth:"surface"|"moderate"|"deep"based on strategyrisk_tolerance:"low"|"medium"|"high"matching the strategy
Apply patches — Write the refactored code to disk. Apply incrementally — one logical change at a time.
Enforce style — Run the project's own formatter and linter (the runner prefixes in
allowed-toolscover the common ones; readpackage.jsonscripts or the equivalent manifest to find the right command). Dispatchstyle-enforcer-agentonly when the project has no configured formatter, or the refactor touched enough files that a convention drift is plausible.Validate — mandatory, never skipped — A refactor is not done until behavior preservation has been checked by something other than reading your own diff. Exactly one of these must run and pass:
- Preferred: run the tests covering the touched code. If the suite is large, run the narrowest command that exercises the changed files.
- Fallback, when no test covers the touched code: dispatch
code-explainer-agenton the before/after pair and have it state, per changed function, whether observable behavior is identical. This is not optional — it is what replaces the test run, not an addition to it.
Then run
git diff HEADand review the final changes. Report which of the two checks ran and its result. If neither could run, say so explicitly and mark the refactor unverified rather than complete.Generate tests — Dispatch
test-writer-agentonly when the refactored code had no test coverage to begin with. Existing passing tests are the behavior-preservation check; adding more is not.
Goals
readability: Extract expressions, rename for clarityperformance: Algorithm improvements, cachingmaintainability: SOLID principles, reduce couplingtestability: Dependency injection, pure functionsall: Comprehensive improvement
Output Format
Provides:
- Summary with metrics and risk assessment
- Analysis of current issues and patterns
- Incremental patches with diffs
- Migration plan for architectural changes
- Validation results
Safety
- Small, reviewable patches
- Dependency ordering
- Rollback procedures
- Validation checkpoints