Refactor an Existing Project โ
Understand first. Plan surgically. Refactor safely. Use code intelligence to see the full impact of every change before making it.
Who This Is For โ
- Senior developers tasked with modernizing a legacy codebase
- Teams migrating from one framework/architecture to another
- Developers cleaning up accumulated tech debt
Prerequisites: CodyMaster installed, AI agent configured, Git initialized
What You'll Build โ
By the end of this workflow:
- โ Complete structural map of the existing codebase
- โ Refactoring plan with impact analysis
- โ Incremental refactored code with full test coverage
- โ Zero regressions, verified through quality gates
Step-by-Step Workflow โ
Step 1: Index the Codebase (2 minutes) โ
Start by building a structural understanding of the project:
bash
# From project root โ generates .cm/skeleton.md
bash scripts/index-codebase.shOr ask your AI agent:
@[/cm-start] I need to refactor this project. Start by analyzing the codebase.What happens:
cm-codeintellruns the Skeleton Indexer โ.cm/skeleton.md- Agent reads the skeleton (~5K tokens) and understands every function, class, route
- Working memory initialized via
cm-continuity
Output: Architecture map in ~5K tokens instead of reading 200+ files
Step 2: Identify Refactoring Targets โ
@[/cm-brainstorm-idea] Analyze this codebase for refactoring opportunities.
Focus on: tech debt, duplicated code, unclear architecture, and scalability issues.The agent will:
- Read
.cm/skeleton.mdfor structural overview - Identify dependency clusters and tight coupling
- Run the 9 Windows Analysis (current state โ ideal future)
- Produce 2-3 refactoring strategies with trade-offs
Example output:
Option A: Module Extraction (M effort, low risk)
โ Extract shared utilities into /lib, consolidate API routes
Option B: Architecture Migration (L effort, medium risk)
โ Move from monolithic to Domain-Driven folders
Option C: Full Rewrite (XL effort, high risk)
โ Start fresh with new design patterns
๐ฏ Recommendation: Option A first, then Option B incrementallyStep 3: Plan the Refactoring โ
@[/cm-planning] Execute Option A from the brainstorm analysis.
Create an incremental refactoring plan โ never break existing tests.The plan will include:
- File-by-file change list with expected impact
- Dependency tree showing what each change affects
- Test coverage requirements per change
- Git branch strategy (feature branches per refactoring unit)
Step 4: Execute with TDD Safety Net โ
@[/cm-execution] Execute the refactoring plan using Mode D (RARV batch)For each refactoring unit, the RARV cycle:
๐งช RED: Write test capturing CURRENT behavior
๐ข GREEN: Refactor the code โ test must still pass
๐ REFACTOR: Clean up the refactored code
โ
VERIFY: Run full test suite โ zero regressions
๐ COMMIT: Git checkpoint with clear messageCritical: Always write a characterization test BEFORE refactoring. This captures the existing behavior as the safety net.
Step 5: Verify with Quality Gates โ
@[/cm-quality-gate] Full 6-gate check on the refactored codeโ
Gate 1: Static analysis โ 0 errors
โ
Gate 2: All tests pass, coverage maintained
โ
Gate 3: Blind code review โ clean
โ
Gate 4: Anti-sycophancy โ no hidden issues
โ
Gate 5: Security scan โ no regressions
โ
Gate 6: i18n integrity โ keys intactStep 6: Deploy Incrementally โ
@[/cm-safe-deploy] Deploy to staging for verificationSkills Involved โ
Tips & Gotchas โ
| Tip | Why |
|---|---|
| Always index first | Without skeleton, agent wastes 50+ tool calls scanning files |
| Characterization tests before refactoring | The only way to prove you didn't break anything |
| Refactor in small commits | Each commit should independently pass all tests |
| Never refactor and add features simultaneously | One purpose per commit โ refactor OR feature, never both |
Use cm-continuity across sessions | Large refactoring spans multiple sessions โ memory prevents lost context |
When NOT to Refactor โ
- If the code works and won't be touched again โ leave it
- If you don't have tests and can't write them โ add tests first
- If the business needs a feature urgently โ ship feature first, refactor after