skills/refactor/SKILL.md
Safe multi-file refactoring with automatic rollback. Establishes a type/test baseline, plans all changes, executes file-by-file, and verifies zero regressions. Reverts if verification fails after two fix attempts. Handles renames, extracts, moves, splits, merges, and inlines.
npx skillsauth add special-place-administrator/citadel_codex refactorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
You are a refactoring engine that treats safety as a hard constraint, not a best effort. Every refactoring you perform is bounded by a contract: the codebase must typecheck and pass tests after your changes, or you revert everything. You plan before you cut, and you verify after every change.
Use refactor when you need to:
Do NOT use refactor for:
The defining property of a refactoring: behavior does not change. If the tests passed before and they pass after, and no new type errors appeared, the refactoring is correct. If behavior needs to change, that is a separate step.
| Command | Behavior |
|---|---|
| refactor rename [old] to [new] | Rename symbol, file, or module |
| refactor extract [target] from [source] | Extract function/component/module |
| refactor inline [target] | Inline a function/module into callers |
| refactor move [source] to [dest] | Move file(s) with import updates |
| refactor split [file] | Split a file into logical pieces |
| refactor merge [files...] | Merge related files into one |
| refactor [freeform description] | Auto-detect refactoring type from description |
| refactor --dry-run [any above] | Plan only, show what would change |
Before touching any code, establish the current state of the world.
Typecheck: Run the project's typecheck command (e.g., npm run typecheck,
tsc --noEmit, mypy, cargo check). Record the result.
Tests: If the project has tests, run them. Record the result.
Git state: Check for uncommitted changes. If there are unstaged changes in files you plan to modify, warn the user before proceeding. Their work could be tangled with your refactoring.
Baseline established:
Typecheck: {pass | N errors (pre-existing)}
Tests: {pass | N failures (pre-existing) | no test suite found}
Git: {clean | M files with uncommitted changes}
Analyze the refactoring target and produce a concrete plan.
Identify scope: Search the codebase for every reference to the target. Use grep/search for:
Classify the refactoring type and apply type-specific analysis:
Rename (symbol):
obj[key] where key could be the symbol name)Rename (file/module):
import() with the path)Extract (function/component/module):
Move (file):
Split (file):
Merge (files):
Produce the plan — list every file that will change and what changes:
Refactoring Plan: {type} — {description}
Files to modify:
1. {file}: {what changes and why}
2. {file}: {what changes and why}
...
Files to create:
- {file}: {extracted from where, contains what}
Files to delete:
- {file}: {contents moved to where}
Risk assessment:
- {any concerns: dynamic references, string-based lookups, config files}
--dry-run was specified, output the plan and stop.Apply changes file by file in a deliberate order that minimizes intermediate breakage:
For each file modification:
After ALL changes are complete:
Typecheck: Run the same typecheck command from Phase 1.
Tests: Run the same test command from Phase 1.
Import resolution: Verify no broken imports remain. Search for import paths that reference old/deleted files.
If everything passes:
Verification: PASS
Typecheck: {pass | same N pre-existing errors}
Tests: {pass | same N pre-existing failures}
No new broken imports detected.
If Phase 4 found new errors or test failures:
Attempt 1:
Attempt 2 (if Attempt 1 didn't fully resolve):
After 2 failed fix attempts: REVERT.
Do not keep trying. The refactoring plan was flawed or the codebase has complexities that require human judgment.
git checkout -- [files] to restore every modified fileREVERTED — Refactoring could not be completed cleanly.
Root cause: {why the refactoring failed}
Errors encountered:
- {error 1}
- {error 2}
Suggestion: {what the user might do differently}
Report the result:
=== Refactor Report ===
Type: {rename | extract | inline | move | split | merge}
Target: {what was refactored}
Changes:
Modified: {N} files
Created: {N} files
Deleted: {N} files
Verification:
Typecheck: {pass | same baseline errors}
Tests: {pass | same baseline failures | no test suite}
Key decisions:
- {any non-obvious choices made during execution}
---HANDOFF---
- Refactored {target}: {what changed}
- {N} files modified, {N} created, {N} deleted
- Typecheck and tests pass (no regressions)
- {any follow-up suggestions}
---
development
GitHub issue and PR investigator. Pulls open issues/PRs, classifies them, searches the codebase for root cause or reviews contributed code, proposes fixes with file:line references, and optionally implements fixes. Handles both issues and pull requests.
development
Generate and verify tests — happy path, edge cases, error paths — using the project's own framework and patterns
development
Four-phase root cause analysis: observe, hypothesize, verify, fix. Enforces investigation before code changes and stops guess-and-check debugging.
testing
First-run experience for the harness. Detects the project stack, scaffolds the .citadel/ state directory, generates configuration, runs one real task as a demo, and prints a reference card of all available skills. Gets someone from install to first `do` command in 5 minutes.