Restructures existing code without changing behavior using behavior-locking tests and incremental verification after each move.
How this skill is triggered — by the user, by Claude, or both
Slash command
/superpowers-optimized:refactoringThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Change structure without changing behavior. Prove it at every step.
Change structure without changing behavior. Prove it at every step.
Refactoring feels safe — "I'm just moving things around." That's exactly why it's dangerous. Without a behavior lock, structural changes silently break contracts, reorder side effects, or drop edge cases. This skill enforces the discipline that makes refactoring actually safe.
Before touching any structure, lock the current behavior with tests.
Run the existing test suite. Detect the project's test runner (check package.json scripts, Makefile, pytest.ini, Cargo.toml, etc.) and run it. Every test must pass. If tests fail before you start, you're debugging, not refactoring — switch to systematic-debugging.
Identify the refactoring surface — which functions, modules, or files will be structurally changed.
Audit test coverage on that surface. For each function/module in scope:
Writing characterization tests: Call the function with representative inputs and assert on the exact output. For side-effectful code (writes files, sends requests, modifies state), wrap the side effect in a spy/mock and assert it was called with the expected arguments. For error cases, trigger the error and assert on the error type/message. If the function is too tightly coupled to test in isolation, that's useful information — note it as a reason the refactoring is needed, and test at the integration level (HTTP request → response, CLI input → output) instead.
Confirm: all tests green. This is your baseline. Any structural change that breaks a test has changed behavior — stop and investigate.
Explicitly state the refactoring boundary before writing any code.
What changes: List the specific structural moves (extract function X, move file Y to Z, inline helper W, rename A to B across the codebase).
What stays the same: List the public APIs, contracts, and behaviors that must not change. Be concrete — "the HTTP response shape stays identical" is better than "behavior stays the same."
Boundary check: For each structural move, ask:
If any answer reveals a behavior change is required, pause: that portion is not a refactor. Split it into a separate task that goes through brainstorming → writing-plans.
One structural change at a time. Tests green after each.
For each move in the scope definition:
Make exactly one structural change. Examples:
Run the full test suite. All tests must pass. If the suite takes more than 2 minutes, run the subset covering the modules in scope — but always run the full suite in Phase 4.
If a test breaks: The structural change altered behavior. Do not fix the test to match the new structure — that's adjusting the lock, not proving safety. Instead:
Verify the passing state before moving to the next structural change. If the user has asked for commits, commit each step independently so it's revertable. Otherwise, proceed to the next change — the test suite is the safety net.
Never batch multiple structural moves into one step. "Extract and rename and move" is three steps, not one. If one of them breaks behavior, you need to know which.
After all structural moves are done:
Full test suite green — no exceptions, no skipped tests, no "known failures."
Behavioral equivalence confirmed — the characterization tests from Phase 1 still pass with identical assertions.
Import/reference audit — for each renamed/moved symbol, run separate Grep searches for:
require() calls (string-based references the rename didn't catch)export { OldName }, index.ts files)A single Grep cannot prove absence — search each category separately.
Code review — if the refactoring touched 5+ files or crossed module boundaries, review for structural issues a test suite won't flag: circular dependencies, layering violations, naming inconsistencies. For large refactors, suggest the user invoke requesting-code-review for an independent second opinion.
test-driven-development — when the refactoring reveals missing test coveragesystematic-debugging — when pre-refactoring tests are already failingbrainstorming → writing-plans — when the "refactoring" actually requires behavior changesverification-before-completion — final gate after the refactoring is completenpx claudepluginhub repozy/superpowers-optimized --plugin superpowers-optimizedRefactors code safely by making test-preserving transformations in small steps, running tests between each change to keep behavior unchanged.
Enforces rigid pre-refactor checklist: read existing code, verify covering tests pass, state goal clearly, validate non-ego motivation, plan smallest first step. Use before restructures, renames, or cleanups.
Executes behavior-preserving code restructuring by first establishing test coverage, then applying small transformations one at a time with verification. Refuses refactors lacking coverage.