| name | refactor |
| description | Code quality analysis and refactoring with validation |
| user-invocable | true |
| allowed-tools | Read, Grep, Glob, Bash(cargo check*), Bash(cargo clippy*), Bash(cargo test*), Bash(cargo fmt*), Bash(git status*), Bash(git diff*), Bash(git stash*), Bash(wc *), Edit, Write, Task |
Refactor
Code quality analysis and incremental refactoring with continuous validation.
Arguments (optional):
- Path or crate:
/refactor crates/app — limit scope
- No arguments — analyze the entire project
Process
Phase 1: Exploration
- Read CLAUDE.md and PRD.md for project rules and conventions
- Collect baseline metrics (in parallel):
cargo clippy --workspace --all-targets 2>&1 — warnings count
- Glob + wc — LOC per crate, files >400 lines
cargo test --workspace 2>&1 — baseline: all tests passing?
- If tests fail or code doesn't compile — report and stop
Phase 2: Diagnosis
Analyze code across 8 dimensions. For large scope, use parallel Task agents (subagent_type=Explore) — one per category or group of categories.
Analysis checklist:
Architecture (SOLID)
Legitimate exceptions to "1 file = 1 type": Error+ErrorKind pairs, Builder+Target, small private helpers (<30 LOC), related DTO families (Request/Response), typestate marker types, newtype wrapper collections.
Module coupling / cohesion
Duplication (DRY)
Dead code
Performance
Error boundaries
Security
Style and idioms
Phase 3: Report and prioritization
Output to terminal (do not create files):
## Analysis results
| Category | Found | Critical |
|----------------|-------|----------|
| Architecture | N | N |
| Coupling | N | N |
| Duplication | N | N |
| Dead code | N | N |
| Performance | N | N |
| Errors | N | N |
| Security | N | N |
| Style | N | N |
Baseline: X clippy warnings, Y total LOC
### Top issues (by impact)
1. ...
2. ...
Ask the user via AskUserQuestion:
- Which categories to fix?
- Budget: quick cleanup / full refactoring?
Phase 4: Execution
For each fix:
- Read the file, understand context
- Make the change (Edit)
cargo check — compiles?
- If logic is affected —
cargo test --workspace
- Move to next
Order: simple and safe first (dead code, style), then structural.
On compilation/test failure — revert the change and move to next.
Phase 5: Validation
cargo fmt --check
cargo clippy --workspace --all-targets -- -D warnings
cargo test --workspace
Compare with baseline: warnings removed, LOC deleted/changed.
Rules
- Do not create abstractions "for the future" — only when duplication already exists
- Do not change public API without explicit consent
- Do not touch files you haven't read
- Do not expand refactoring beyond the agreed scope
- Atomic changes: one fix at a time, verify after each
- Output to terminal, not to report files
- Communicate in the user's language