Audit a codebase for refactoring opportunities across all packages/sections. Spawns parallel refactor subagents per section, collects suggestions without executing, then presents a ranked plan for approval. Use when the user asks to "audit for refactors", "refactoring suggestions", "code quality review", "refactor plan", "find refactoring opportunities", or "what should we clean up".
Analyze a codebase for refactoring opportunities across all packages. Designed to be non-destructive — collects and ranks suggestions without executing any changes.
Scan the repository to identify distinct sections of the codebase. Each section should be a coherent unit (package, service, module) with its own domain.
package.json, workspace configs (pnpm-workspace.yaml, lerna.json), Cargo.toml workspaces, monorepo packages/ or apps/ directories, top-level folders with their own build configs.| Section | Root path | Language | Focus areas |
|---------|-----------|----------|-------------|
| ... | ... | ... | ... |
If the user provided an explicit section breakdown in their prompt, use that instead of auto-discovering.
Spawn one refactor subagent per section in parallel. Each subagent receives:
Subagent instructions (include verbatim in each subagent prompt):
Analyze the code in
{root_path}for refactoring opportunities. Do NOT make any changes — analysis only.For each suggestion, provide:
- What: File(s) and specific code to change (include line numbers)
- Why: How this improves the code (readability, performance, maintainability, correctness)
- Size:
quick-win(< 30 min),moderate(1-2 hrs), orsignificant(half-day+)- Risk:
low(safe rename/extract),medium(logic restructure, needs tests),high(behavioral change, cross-cutting)- Category: One of:
dead-code,duplication,extraction,simplification,naming,type-safety,performance,error-handling,organizationFocus areas for this section: {focus_areas}
Prioritize suggestions that:
- Remove dead code or unused imports
- Eliminate duplication (especially copy-pasted logic)
- Extract reusable functions from repeated patterns
- Simplify overly complex conditionals or nested logic
- Improve type safety (remove
any, add missing types)Flag any cross-package opportunities — shared logic that could move to a common package, inconsistent patterns across packages, or duplicated utilities.
Output your suggestions as a structured list grouped by category.
After all subagents complete:
Present the consolidated suggestions to the user:
## Refactor Audit Report
### Quick Wins
- [ ] **[category]** `path/to/file.ts:L42` — [what to change]. *Why:* [reason]. Risk: [low/medium].
### Moderate
- [ ] **[category]** `path/to/file.ts:L100-150` — [what to change]. *Why:* [reason]. Risk: [low/medium/high].
### Significant
- [ ] **[category]** `path/to/file.ts` + `other/file.ts` — [what to change]. *Why:* [reason]. Risk: [medium/high].
### Cross-Package Opportunities
- [ ] [description of shared pattern or utility that spans packages]
### Skipped
- [suggestion]: [why it was dropped]
Wait for user approval. The user may:
Do not execute any refactors until the user explicitly approves.
After approval, for each approved suggestion:
refactor subagent (or general-purpose for cross-package changes) to implement the change.Commit approved changes in logical groups (one commit per category or per section, not one per suggestion).
significant size minimum.