3.1 KiB
3.1 KiB
description
| description |
|---|
| Refactor workflow for safely restructuring existing code |
Refactor Workflow
Purpose
Safely restructure existing code while preserving behavior. Uses a different mindset than greenfield feature development: understand first, then change structure.
When to Use
- Code restructuring (moving, renaming, splitting modules)
- Pattern migration (e.g., switching from callbacks to async/await)
- Dependency upgrades with breaking changes
- Addressing tech debt or architectural improvements
Requires a specific goal. Invoke with a clear target, not an open directory:
- ✅
/refactor extract storage interface in task feature - ✅
/refactor split user handler into separate auth handler - ❌
/refactor apps/backend(too vague — use/auditfirst to find specific issues)
When NOT to Use
- New features (use
/orchestrator) - Small bug fixes (use
/quick-fix) - "Find what to improve" (use
/auditfirst, then/refactorfor structural findings)
Pre-Implementation Checklist
Before starting, you MUST:
- Scan
.agents/rules/directory for applicable rules - Read
architectural-pattern.mdandproject-structure.md - Read
rule-priority.mdfor conflict resolution
Phases
Phase 1: Impact Analysis
Set Mode: Use task_boundary to set mode to PLANNING
- Map the blast radius — what files, modules, and tests are affected?
- Document existing behavior — what tests currently pass? what contracts exist?
- Identify risks — can this be done incrementally or is a big-bang needed?
- Create refactoring plan in
task.mdwith incremental steps - If decision involves trade-offs, create an ADR using the ADR Skill
Skills to consider:
- Sequential Thinking — for multi-step refactoring with interdependencies
Phase 2: Incremental Change (TDD)
Set Mode: Use task_boundary to set mode to EXECUTION
For each step in the refactoring plan:
- Ensure existing tests pass before making any change
- Make one incremental change — move, rename, or restructure
- Run tests after each change — behavior must be preserved
- Add new tests if the refactoring exposes untested behavior
- Follow applicable rules:
- Architectural Patterns @architectural-pattern.md
- Code Organization Principles @code-organization-principles.md (includes circular dependency guidance)
Key principle: Never break the build for more than one step at a time.
Phase 3: Parity Verification
Set Mode: Use task_boundary to set mode to VERIFICATION
- Run the full validation suite (same as
/4-verify) - Compare test coverage — coverage should be equal to or better than before
- Verify no behavior changes — same inputs produce same outputs
- If applicable, run E2E tests (
/e2e-test)
Phase 4: Ship
Follow /5-commit with commit type refactor(<scope>): <description>
Completion Criteria
- Impact analysis documented
- All changes made incrementally with tests passing at each step
- Full verification suite passes
- Test coverage is equal to or better than before
- Committed with
refactortype