| name | refactor |
| description | Safe, incremental code refactoring with verification checkpoints and rollback safety |
| layer | hub |
| category | workflow |
| triggers | ["/refactor","refactor this","clean up this code","restructure this","extract this","simplify this"] |
| inputs | [{"target":"Code, file(s), or module to refactor"},{"goal":"What the refactoring should achieve (readability, modularity, testability, etc.)"},{"constraints":"What must NOT change (public API, behavior, performance characteristics)"}] |
| outputs | [{"refactoringPlan":"Step-by-step plan with verification at each step"},{"changes":"List of all modifications made"},{"verificationResults":"Test results and behavioral checks at each checkpoint"}] |
| linksTo | ["test","code-review","scout","debug"] |
| linkedFrom | ["cook","team","ship","optimize"] |
| preferredNextSkills | ["test","code-review"] |
| fallbackSkills | ["debug","scout"] |
| riskLevel | medium |
| memoryReadPolicy | selective |
| memoryWritePolicy | selective |
| sideEffects | ["Modifies source code files","May modify or create test files","Runs tests between refactoring steps"] |
Refactor Skill
Purpose
Improve code structure, readability, and maintainability WITHOUT changing external behavior. Refactoring is the disciplined practice of restructuring existing code -- altering its internal structure without altering its external behavior.
The cardinal rule: After refactoring, the system does exactly what it did before. Only the code is different.
Workflow
Phase 1: Assessment
-
Read the target code thoroughly. Understand what it does, how it does it, and why it is structured the way it is.
-
Identify the specific smells -- What is wrong with the current structure?
- Long function/method: Does too many things (> 30 lines is a signal)
- Deep nesting: More than 3 levels of indentation
- Duplication: Same or similar code in multiple places
- God object/module: One file doing everything
- Primitive obsession: Using raw types instead of domain objects
- Feature envy: Code that uses another module's data more than its own
- Shotgun surgery: A single change requires editing many files
- Unclear naming: Variables/functions named
data, result, temp, handle
- Dead code: Unreachable or unused code
- Inappropriate coupling: Modules that know too much about each other
-
Define the refactoring goal -- What does the code look like when we are done?
- More readable? (clearer names, shorter functions)
- More modular? (better separation of concerns)
- More testable? (injectable dependencies, pure functions)
- More extensible? (easier to add new features)
-
Identify behavioral constraints -- What must NOT change?
- Public API signatures
- External behavior (what callers observe)
- Performance characteristics (within tolerance)
- Side effects (file writes, API calls, database operations)
-
Check test coverage -- What tests exist for this code?
- If tests exist: They are our safety net. Run them before and after each step.