Install any skill in seconds. Free to start, no credit card required.
Get Started Free →When refactoring, rewriting, or migrating critical code paths, orchestrate a safe cycle - assess risks, implement, verify, document. Chains pre-mortem and prove-it with built-in implementation phase. Use when the user says "refactor", "rewrite", "migrate", or "clean up" for non-trivial code.
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 6% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 162% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 443% | 0% |
| case-13 | ✗→✓ | ▲ Improved | 49% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 89% | 0% |
An elixir for risky code changes. Refactors feel safe ("I'm just cleaning up") but they're where bugs hide. This chains risk assessment, careful implementation, verification, and documentation. The ceremony slows you down just enough to catch problems before production does.
This skill works best with these skills installed:
| Skill | Purpose | If Missing | |-------|---------|------------| | pre-mortem | Risk assessment before starting | Falls back to built-in checklist | | prove-it | Verification enforcement | Falls back to built-in verification | | retrospective | Document learnings | Skipped (optional phase) |
If pre-mortem skill installed: Invoke it now.
If not installed, evaluate:
markdown## Refactor Risk Assessment **What's changing:** - [ ] Files affected: [list them] - [ ] Functions/classes modified: [list them] - [ ] Estimated lines changed: [number] **Risk factors:** - [ ] Touches shared/core code (used by multiple features) - [ ] Affects data persistence (database, files, cache) - [ ] Changes public API/interfaces - [ ] Lacks test coverage - [ ] Written by someone else / unfamiliar code - [ ] Production-critical path **Risk level:** [Low / Medium / High / Abort]
GATE: Do not proceed if:
If high risk: Consider breaking into smaller refactors.
Before touching code:
bash git stash # or commit current state git checkout -b refactor/[description]
Before: [How it works now] After: [How it should work - must be identical externally]
GATE: Do not proceed without:
Rules for safe refactoring:
bash # Good: multiple small commits git commit -m "Extract helper function" git commit -m "Rename variables for clarity" git commit -m "Move file to new location"
# Bad: one giant commit git commit -m "Refactor everything"
If prove-it skill installed: Invoke it now.
If not installed:
markdown## Refactor Verification **Automated checks:** - [ ] All existing tests pass - [ ] No new linter errors - [ ] Type checking passes (if applicable) **Manual verification:** - [ ] Tested the primary use case manually - [ ] Checked one edge case - [ ] Compared behavior before/after for critical paths **Regression check:** - [ ] git diff shows only intended changes - [ ] No accidental behavior changes - [ ] No debug code left in
GATE: Do not merge until all checks pass.
If verification fails:
bashgit revert HEAD # or reset to before refactor # Start over with smaller scope
If retrospective skill installed: Invoke for significant refactors.
For large refactors, capture:
markdown## Safe Refactor: [Description] ### Risk Assessment - Risk level: [Low/Medium/High] - Files affected: [count] - Risk factors: [list] ### Preparation - [x] Tests verified/written - [x] Branch created: refactor/[name] - [x] Current behavior documented ### Implementation - Commits: [count] - Approach: [brief description] ### Verification - [x] Tests pass - [x] Manual verification complete - [x] No regressions detected ### Learnings (if significant) [What you'd do differently]
User: "Refactor the authentication module to use the new token format"
Phase 1 - Risk Assessment: > Refactor Risk Assessment: > - Files affected: auth.ts, token.ts, middleware.ts, 12 test files > - Risk factors: Core code, Production-critical, Written by someone else > - Risk level: High > > Recommendation: Break into smaller refactors: > 1. First: Add new token format support alongside old > 2. Second: Migrate consumers one by one > 3. Third: Remove old format
Phase 2 - Prepare: > - x] Existing tests cover token generation and validation > - x] Created branch: refactor/token-format-v2 > - x] Documented: Old format is JWT with RS256, new format adds typ header
Phase 3 - Implement: > Making changes incrementally... > - Commit 1: "Add TokenV2 class with new format" > - Commit 2: "Add feature flag for token format" > - Commit 3: "Update token generation to use flag" > - Tests passing after each commit
Phase 4 - Verify: > - x] All 47 auth tests pass > - x] Manual test: login flow works with both formats > - x] Manual test: token refresh works > - x] git diff shows only intended changes > > Ready to merge.
Other measured skills in the registry, with their headline benchmark lift.