refactor-guided
# ClaudeWave Item: refactor-guided The refactor-guided command performs safe, incremental code refactoring by applying one structural change at a time, running tests after each step, and reverting immediately if tests fail. Use this command when you need to improve code structure without adding new features, ensuring that external behavior remains unchanged and every modification is verifiable and atomic.
mkdir -p ~/.claude/commands && curl -fsSL https://raw.githubusercontent.com/CloudAI-X/claude-workflow-v2/HEAD/commands/refactor-guided.md -o ~/.claude/commands/refactor-guided.mdrefactor-guided.md
# Guided Refactoring Perform safe, incremental refactoring with test verification at every step. ## Context - Recent changes: !`git diff --name-only HEAD~3 2>/dev/null | head -10 || echo "No recent changes"` - Current branch: !`git branch --show-current` - Working tree status: !`git status --short` - Test framework: !`cat package.json 2>/dev/null | grep -E '"(jest|vitest|mocha)"' | head -1 || ls pytest.ini pyproject.toml 2>/dev/null | head -1 || ls Cargo.toml go.mod 2>/dev/null | head -1 || echo "Unknown"` - Existing tests: !`find . -name "*test*" -o -name "*spec*" 2>/dev/null | grep -E '\.(ts|tsx|js|jsx|py|go|rs)$' | head -10 || echo "No test files found"` ## Safety Rules (NON-NEGOTIABLE) 1. **NEVER refactor AND add features simultaneously.** Each change is purely structural. 2. **NEVER proceed without passing tests.** If tests fail after a refactoring, revert immediately. 3. **ONE refactoring at a time.** Each commit is a single, atomic refactoring step. 4. **Preserve external behavior.** Inputs and outputs must remain identical. 5. **Ensure clean working tree before starting.** Stash or commit uncommitted changes first. ## Workflow ### Phase 1: Analyze Scope 1. Identify the target file(s) or directory from `$ARGUMENTS` 2. Map all dependencies — files that import/use the target 3. Map all dependents — files that the target imports/uses 4. Assess blast radius: how many files could be affected? 5. Report scope summary before proceeding: ``` ## Refactoring Scope - Target: [file/directory] - Files affected: [count] - Dependencies: [list] - Dependents: [list] - Risk level: [low/medium/high] ``` ### Phase 2: Ensure Test Coverage 1. Check if tests exist for the current behavior of the target 2. Run existing tests to confirm they pass (this is your safety net) 3. If test coverage is insufficient: - Write tests that capture the current behavior BEFORE refactoring - Run them to confirm they pass - Commit these tests separately: `test: add coverage for [target] before refactoring` 4. Record baseline test results (pass count, time) ### Phase 3: Refactor Incrementally For each refactoring step: 1. **Describe** the specific refactoring about to be applied (e.g., "Extract method X from class Y", "Rename variable A to B", "Move function to separate module") 2. **Apply** the single refactoring change 3. **Run tests** immediately after applying 4. **If tests PASS:** - Commit with message: `refactor: [specific change description]` - Move to next refactoring step 5. **If tests FAIL:** - Revert the change: `git checkout -- .` - Analyze why it failed - Try a different approach or break it into smaller steps - Document what went wrong Common refactoring types (apply as appropriate): - Extract function/method - Rename for clarity - Remove duplication (DRY) - Simplify conditionals - Split large files/functions - Improve type signatures - Replace magic values with constants - Consolidate imports ### Phase 4: Summary Report After all refactoring steps are complete, provide: ``` ## Refactoring Summary ### Before - Files: [count and names] - Lines of code: [approximate] - Complexity: [observations] ### After - Files: [count and names] - Lines of code: [approximate] - Complexity: [observations] ### Changes Applied 1. [refactoring 1] - [commit hash] 2. [refactoring 2] - [commit hash] ... ### Reverted Attempts 1. [what was tried] - [why it failed] ### Test Results - All tests passing: [yes/no] - Tests added: [count] - Total test runs: [count] ``` ## Target $ARGUMENTS If no target specified, analyze the most recently modified files and suggest refactoring opportunities.
Expert code review specialist. Use PROACTIVELY after writing or modifying code, before commits, when asked to review changes, PR review, code quality check, lint, or standards audit. Focuses on quality, security, performance, and maintainability.
Expert debugging specialist for errors, test failures, crashes, segmentation faults, memory leaks, timeouts, race conditions, deadlocks, and unexpected behavior. Use PROACTIVELY when encountering any error, exception, or failing test. Performs systematic root cause analysis.
Technical documentation specialist. Use for creating README files, API documentation, architecture docs, inline comments, user guides, changelogs, migration guides, release notes, FAQs, and troubleshooting docs. MUST BE USED when documentation is needed or when code changes require doc updates.
Master coordinator for complex multi-step tasks. Use PROACTIVELY when a task involves 2+ modules, requires delegation to specialists, needs architectural planning, or involves GitHub PR workflows. MUST BE USED for open-ended requests like "improve", "enhance", "build", "scale", "refactor", "add feature", "system design", "architecture", "complex task", or when implementing features from GitHub issues.
Code refactoring specialist for improving code quality, reducing technical debt, eliminating code smells, reducing complexity, and applying design patterns. Use PROACTIVELY when code needs restructuring, simplification, tech debt reduction, or when applying DRY/SOLID principles.
Security specialist for vulnerability detection, secure coding review, and security hardening. Use PROACTIVELY when handling authentication, authorization, encryption, secrets, credentials, OAuth, JWT, CORS, headers, user input, API keys, or sensitive data. Checks for OWASP Top 10 and common vulnerabilities.
Testing strategy specialist for designing test suites, writing tests, and ensuring comprehensive coverage. Use PROACTIVELY when adding new features, fixing bugs, improving test coverage, creating test plans, mocking strategies, handling flaky tests, or writing integration/E2E tests.
Add tests for recently changed files or specified code