with one click
refactor
// Systematic multi-file refactoring with pattern analysis, scope detection, batched execution, and progress tracking. Use for renames, pattern changes, API migrations, or any change affecting 6+ files.
// Systematic multi-file refactoring with pattern analysis, scope detection, batched execution, and progress tracking. Use for renames, pattern changes, API migrations, or any change affecting 6+ files.
[HINT] Download the complete skill directory including SKILL.md and all related files
| name | refactor |
| description | Systematic multi-file refactoring with pattern analysis, scope detection, batched execution, and progress tracking. Use for renames, pattern changes, API migrations, or any change affecting 6+ files. |
| category | process |
| triggers | ["rename","restructure","reorganize","pattern change","large-scale change","multi-file change"] |
Purpose: Systematic refactoring with planning, pattern detection, and batched execution Phases: Gather Context → Pattern Analysis → Plan → Execute → Validate → Commit Usage:
/refactor [scope flags] <refactor description>
/implement/debug/implement/hotfixNote: Command examples use
npmas default. Adapt to the project's package manager perai-assistant-protocol— Project Commands.
See ai-assistant-protocol for valid approval terms and invalid responses.
| Flag | Description |
|---|---|
--files=<paths> | Limit refactor to specific files/directories |
--project=<path> | Project root for monorepos |
Ask: (1) What pattern/code needs to change? (2) Target state? (3) Why? (4) Areas to exclude?
Wait for user response before exploring code.
Determine refactor type, file count, risk level:
| Scope | Files | Risk | Approach |
|---|---|---|---|
| Small | 1-5 | Low | Direct changes |
| Medium | 6-20 | Medium | Batched with testing |
| Large | 21-50 | High | Phased with checkpoints |
| Massive | 50+ | Critical | Multiple phases |
Wait for confirmation.
Search for all instances of the pattern. Present findings (see references/refactor-templates.md — Pattern Analysis).
Present edge cases for discussion. Wait for user guidance.
Present the refactor plan summary (see references/refactor-templates.md — Plan Summary).
For most refactors, direct changes are fine. However, when a direct rename or change is too risky (high coupling, external API surface, published package), use the Introduce Abstraction Layer pattern:
This pattern is safer for public APIs, published packages, and widely-used internal interfaces where a single atomic rename would be too disruptive or where the migration must be done incrementally across multiple PRs.
Present the chosen strategy (direct change or abstraction layer) as part of the plan summary.
GATE: Do NOT begin modifying files until user approves.
Before starting any changes:
git stash push -m "savepoint: before refactor" --include-untracked 2>/dev/null; git stash pop 2>/dev/null
Or ensure all current work is committed so you can revert cleanly.
Apply refactoring changes in a safe order to catch breakage early:
Run typecheck after each phase. If the typecheck fails, fix issues in the current phase before proceeding.
For large refactors, group changes into logical batches (max 5 files per batch). Present the batch grouping to the user for approval before executing.
Grouping strategies:
Each batch should be independently type-checkable — after applying a batch and running typecheck, there should be no new type errors introduced by that batch (existing errors from not-yet-migrated code are expected and acceptable).
See references/safe-refactoring-patterns.md for pattern-specific risk and batch safety guidance.
For each batch:
If tests fail after a batch: Revert the batch (git checkout -- [affected-files]), reassess the approach, and try again with a corrected strategy. Do not debug individual file changes within a broken batch.
If a file doesn't match expected patterns, present discrepancy report. Wait for user decision. Don't force the change.
npm run typecheck
npm run lint
npm run test -- {affected-pattern}
npm run test # Full suite for large refactors
Present verification report (see references/refactor-templates.md — Verification Report).
GATE: All tests must pass before proceeding.
Present commit message. Refactoring commits should use refactor: type and clearly describe what changed structurally.
Keep refactoring commits separate from feature commits. Never mix refactoring and behavior changes in the same commit.
GATE: Wait for explicit confirmation.
| Phase | Gate |
|---|---|
| 1. Gather Context | User confirms scope |
| 2. Pattern Analysis | User guides edge cases |
| 3. Plan | User approves |
| 4. Execute | Tests pass per batch |
| 5. Validate | All tests pass |
| 6. Commit | User confirms |
| ID | Type | Prompt / Condition | Expected |
|---|---|---|---|
| REF-T1 | Positive | "Rename all utils to helpers across the codebase" | Skill triggers |
| REF-T2 | Positive | "Restructure the components directory" | Skill triggers |
| REF-T3 | Positive | "Migrate from callbacks to async/await" | Skill triggers |
| REF-T4 | Negative | "Add a new helper function" | Does NOT trigger (→ /implement) |
| REF-T5 | Negative | "Fix the broken import" | Does NOT trigger (→ /debug) |
| REF-T6 | Negative | "Change 2 files to use new pattern" | Does NOT trigger (→ /implement, <6 files) |
| REF-T7 | Boundary | "Rename this function in 4 files" | Does NOT trigger (→ /implement, <6 files) |
| REF-T8 | Boundary | "Rename this function across 8 files" | Skill triggers (6+ files) |