Use for significant refactoring — duplication detection, characterization tests, staged execution, simplicity review.
Resources
1Install
npx skillscat add mistakeknot/clavain/refactor-safely Install via the SkillsCat registry.
SKILL.md
Refactor Safely
Understand → Protect → Change → Verify → Simplify.
Announce: "I'm using the refactor-safely skill to guide this refactoring."
Step 1: Understand the Scope
- Identify the specific goal (not just "clean up" — extract module, remove duplication, simplify interface, etc.)
- Run
tldr-swinton:finding-duplicate-functionsto detect semantic duplication in the affected area - Run
fd-architectureto understand existing patterns and house style - Map blast radius: which files, tests, and consumers are affected?
Step 2: Protect with Tests
- Check existing test coverage for affected code paths
- Add characterization tests for any untested behavior you're about to change
- Use
test-driven-developmentskill to ensure tests exist for the target behavior - Run all tests — establish a green baseline
If tests are missing: write them first. Don't refactor untested code.
Step 3: Create a Refactor Plan
Use writing-plans to create a staged plan:
- Break into small, independent batches — each must leave tests green
- Order to minimize WIP — finish one before starting the next
- Flag any batch that changes public interfaces (higher risk)
Step 4: Execute in Batches
For each batch:
- Make the change
- Run affected tests
- Run
fd-quality— is the result actually simpler? - Commit with a descriptive message
After each batch verify: tests pass, code is objectively simpler (fewer lines, clearer intent, less coupling), no unintended behavior changes.
Step 5: Final Verification
- Run full test suite
- Run
fd-qualityon the complete change - Compare before/after: lines of code, file count, cyclomatic complexity
- Use
landing-a-changeto ship
Key Agents
| Agent | When |
|---|---|
tldr-swinton:finding-duplicate-functions |
Step 1 — duplication targets |
fd-architecture |
Step 1 — house patterns, module boundaries |
fd-quality |
Steps 4 & 5 — simplicity and idiom compliance |
Red Flags
Never:
- Refactor without running tests first
- Leave a batch with red tests
- Combine refactoring + feature work in the same commit
- Refactor code you don't understand yet
Always:
- Green baseline before starting
- Each batch small enough to review
- Verify simplicity after each batch
- Separate behavior changes from structural changes — different commits