{"manifest":{"name":"Refactor Planner","version":"1.0.0","description":"Analyze messy code and produce a safe, incremental refactor plan with risk assessment, rollback strategy, and a test-first checklist.","tags":["refactoring","code-quality","architecture","workflow","agent"],"standard":"agentskills.io","standard_version":"1.0","content_checksum":"8e36e4b49a77171f9e9add12c4002facb82c72787010be079dc8eeeb9bb2da12","bundle_checksum":null,"metadata":{},"files":[]},"files":{"SKILL.md":"# Refactor Planner\n\n> **Purpose:** Analyze a code file, module, or system component and produce a safe, incremental refactor plan with risk assessment, rollback strategy, and a test-first validation checklist. Prevents big-bang rewrites that break production.\n\n---\n\n## Invocation\n\n```\n/refactor <file-or-description>\n```\n\n**Examples:**\n- `/refactor src/lib/auth.ts`\n- `/refactor The checkout flow uses a 400-line switch statement with no error handling`\n\n---\n\n## Analysis Steps\n\n### Step 1: Diagnose Current State\n\nIdentify code smells present:\n- **Long methods**: functions > 50 lines\n- **Deep nesting**: conditionals > 3 levels deep\n- **God object**: class or module doing too many things\n- **Duplicated logic**: same pattern repeated 3+ times\n- **Magic numbers/strings**: unnamed constants\n- **Missing error handling**: silent failures, swallowed exceptions\n- **Coupling**: this code is hard to test in isolation because it depends on X\n\nMap dependencies:\n- What calls this code?\n- What does it call?\n- What would break if its interface changed?\n\nAssess test coverage:\n- Is this code protected by tests?\n- If NO: tests must be written BEFORE refactoring begins\n\n---\n\n### Step 2: Define Target State\n\n- What will the code look like after refactoring?\n- What pattern or principle does this refactor apply?\n  - Extract Method, Extract Class, Strategy Pattern, Repository Pattern, etc.\n- What concrete metrics improve?\n  - Fewer lines, reduced cyclomatic complexity, better separation of concerns\n\n---\n\n### Step 3: Risk Assessment\n\n| Risk | Likelihood | Impact | Mitigation |\n|------|------------|--------|------------|\n| Breaking change to public API | Medium | High | Add adapter layer |\n| Missing test coverage | High | High | Write tests first (blocker) |\n| Concurrent modification by team | Low | Medium | Use a feature branch |\n| Performance regression | Low | High | Benchmark before and after |\n\n---\n\n### Step 4: Implementation Plan\n\nProduce a numbered task list — each step independently verifiable:\n\n```\n[ ] 0. PREREQUISITE: Write tests covering current behavior (if missing)\n[ ] 1. [Smallest safe change] — run tests, must pass\n[ ] 2. [Next change] — run tests, must pass\n[ ] 3. [Next change] — run tests, must pass\n...\n[ ] N. Delete dead code confirmed unreachable after refactor\n[ ] N+1. Update documentation and comments\n```\n\n---\n\n### Step 5: Rollback Plan\n\n- How to revert if something breaks in production?\n- Is this behind a feature flag? (recommended for risky refactors)\n- What is the blast radius if it fails? (1 endpoint, 1 user flow, entire app?)\n\n---\n\n## Output Format\n\n```\n## Refactor Plan: [Module/File Name]\n\n### Current State\n[Code smells identified, complexity metrics]\n\n### Target State\n[What it looks like after, patterns applied]\n\n### Risk Assessment\n[Table]\n\n### Prerequisites\n[Tests that must exist before starting]\n\n### Implementation Steps\n[Numbered checklist]\n\n### Rollback Strategy\n[How to revert]\n\n### Definition of Done\n- [ ] All pre-existing tests still pass\n- [ ] New tests cover the refactored logic\n- [ ] No public API surface changed (or migration provided)\n- [ ] Code reviewed by one other engineer\n```\n\n---\n\n## Rules\n\n- **Tests before refactoring** is non-negotiable — stop if coverage is missing and say so\n- Each step must pass tests independently — no \"it'll work when step 5 is done\"\n- Never rename a public API without a deprecation period\n- Prefer 10 small safe steps over 1 big clever change\n- If the refactor requires more than 500 lines changed: propose splitting into phases\n\n## Playground\n\n<!DOCTYPE html><html><head><meta charset='utf-8'><style>*{box-sizing:border-box;margin:0;padding:0}body{background:#0d1117;color:#e6edf3;font-family:monospace;font-size:12px;height:100vh;display:flex;flex-direction:column;overflow:hidden}.header{background:#161b22;border-bottom:1px solid #30363d;padding:8px 14px;font-size:11px;color:#8b949e;display:flex;justify-content:space-between;align-items:center;flex-shrink:0}.title{color:#58a6ff;font-weight:bold;font-size:13px}.panels{display:flex;flex:1;overflow:hidden}.panel{flex:1;overflow:auto;padding:12px;border-right:1px solid #30363d}.panel:last-child{border-right:none}.label{font-size:10px;color:#8b949e;text-transform:uppercase;letter-spacing:.08em;margin-bottom:6px}pre{white-space:pre-wrap;word-break:break-word;line-height:1.5}</style></head><body><div class='header'><span class='title'>Refactor Planner</span><span>Example · SkillSlap</span></div><div class='panels'><div class='panel'><div class='label'>Input: Code to refactor</div><pre><span style='color:#8b949e'>function processOrder(order) {</span>\n<span style='color:#8b949e'>  if (order.type === 'A') {</span>\n<span style='color:#8b949e'>    // 30 lines of logic</span>\n<span style='color:#8b949e'>  } else if (order.type === 'B') {</span>\n<span style='color:#8b949e'>    // 40 lines of logic</span>\n<span style='color:#8b949e'>  } else if (order.type === 'C') {</span>\n<span style='color:#8b949e'>    // 25 lines of logic</span>\n<span style='color:#8b949e'>  }</span>\n<span style='color:#8b949e'>}</span></pre></div><div class='panel'><div class='label'>Output: Refactor plan</div><pre><span style='color:#58a6ff'>Smell: Long if-else chain (strategy)</span>\n\n<span style='color:#58a6ff'>Plan (3 steps, no behaviour change)</span>\n\n<span style='color:#8b949e'>1. Extract handlers</span>\n<span style='color:#3fb950'>   processOrderA(order) { ... }</span>\n<span style='color:#3fb950'>   processOrderB(order) { ... }</span>\n<span style='color:#3fb950'>   processOrderC(order) { ... }</span>\n\n<span style='color:#8b949e'>2. Build dispatch map</span>\n<span style='color:#3fb950'>   const handlers = {</span>\n<span style='color:#3fb950'>     A: processOrderA,</span>\n<span style='color:#3fb950'>     B: processOrderB,</span>\n<span style='color:#3fb950'>     C: processOrderC</span>\n<span style='color:#3fb950'>   }</span>\n\n<span style='color:#8b949e'>3. Replace function body</span>\n<span style='color:#3fb950'>   const h = handlers[order.type]</span>\n<span style='color:#3fb950'>   if (!h) throw Error(order.type)</span>\n<span style='color:#3fb950'>   return h(order)</span></pre></div></div></body></html>"}}