Skip to content

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.sh

Or ask your AI agent:

@[/cm-start] I need to refactor this project. Start by analyzing the codebase.

What happens:

  1. cm-codeintell runs the Skeleton Indexer โ†’ .cm/skeleton.md
  2. Agent reads the skeleton (~5K tokens) and understands every function, class, route
  3. 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:

  1. Read .cm/skeleton.md for structural overview
  2. Identify dependency clusters and tight coupling
  3. Run the 9 Windows Analysis (current state โ†’ ideal future)
  4. 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 incrementally

Step 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 message

Critical: 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 intact

Step 6: Deploy Incrementally โ€‹

@[/cm-safe-deploy] Deploy to staging for verification

Skills Involved โ€‹

Tips & Gotchas โ€‹

TipWhy
Always index firstWithout skeleton, agent wastes 50+ tool calls scanning files
Characterization tests before refactoringThe only way to prove you didn't break anything
Refactor in small commitsEach commit should independently pass all tests
Never refactor and add features simultaneouslyOne purpose per commit โ€” refactor OR feature, never both
Use cm-continuity across sessionsLarge 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

Open Source AI Agent Skills Framework