skills/refactoring/SKILL.md
Systematic, behavior-preserving code refactoring with safety gates. Dispatches refactoring-agent. Triggers: "refactor", "clean up code", "reduce tech debt", "extract method", "rename". NOT for reactive PR feedback — use code-review for that.
npx skillsauth add Wilder1222/superomni refactoringInstall 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.
Status protocol — end every session with one of: DONE (evidence provided) · DONE_WITH_CONCERNS (list each) · BLOCKED (state what blocks you) · NEEDS_CONTEXT (state what you need).
Auto-advance — pipeline: THINK → PLAN → REVIEW → BUILD → VERIFY → RELEASE. Only human gate is spec approval at THINK. On DONE at other stages, print [STAGE] DONE -> advancing to [NEXT-STAGE] and invoke the next skill. On any non-DONE status at any stage, STOP.
Output directory — all artifacts go in docs/superomni/<kind>/<kind>-[branch]-[session]-[date].md. See CLAUDE.md for the full directory map.
TACIT-DENSE — before high-tacit decisions, classify D1 (domain expertise) · D2 (user-facing UX) · D3 (team culture) · D4 (novel pattern). On hit, output TACIT-DENSE [D#]: [question] — My default: [recommendation]. See reference for actions.
Anti-sycophancy — take a position on every significant question. Name flaws directly. No filler ("that's interesting", "you might consider", "that could work").
Telemetry (local only) — at session end, log bin/analytics-log. Nothing leaves the machine.
See preamble-ref.md for detailed protocols.
Goal: Improve the structure and readability of existing code without changing its observable behavior, using systematic refactoring patterns with safety gates at every step.
Never start refactoring if tests are failing. Refactoring broken code creates two problems from one. Run the full test suite first and get it green, then refactor.
Refactoring: extract calculateTax() from 45-line processOrder() function
BEFORE: npm test → 47 passing, 0 failing
Refactor: extract 12 lines into calculateTax(), update all call sites
AFTER: npm test → 47 passing, 0 failing
Result: DONE — behavior preserved, code cleaner
Refactoring: "clean up the order processing module"
BEFORE: (didn't run tests)
Refactored: moved logic, renamed variables, fixed a bug I noticed
AFTER: npm test → 44 passing, 3 failing
[VIOLATED: Mixed refactoring with bug fix; no baseline established]
Identify what needs refactoring and why:
# Review the code to be refactored
git diff main...HEAD --stat 2>/dev/null | head -20
git log --oneline -5
# Find complexity hotspots
find . -name "*.js" -o -name "*.ts" -o -name "*.py" | \
xargs wc -l 2>/dev/null | sort -rn | head -15
# Find code smells
grep -rn "TODO\|FIXME\|HACK\|SMELL\|DUPLICATE" \
--include="*.js" --include="*.ts" --include="*.py" . \
| grep -v "node_modules\|.git" | head -15
Define scope:
REFACTORING SCOPE
────────────────────────────────────────
Target files: [list of files to refactor]
Reason: [why this code needs improvement]
Boundaries: [what files/modules are out of scope]
Behavior contract: [what must remain unchanged]
────────────────────────────────────────
This phase is a hard gate — do not proceed until tests pass.
# Run the full test suite and save results
npm test 2>&1 | tee /tmp/refactor-baseline.txt
# or: pytest -v 2>&1 | tee /tmp/refactor-baseline.txt
# or: go test ./... 2>&1 | tee /tmp/refactor-baseline.txt
# Extract the test count
grep -cE "pass|PASS|✓|ok " /tmp/refactor-baseline.txt 2>/dev/null || \
tail -5 /tmp/refactor-baseline.txt
Record:
SAFETY BASELINE
────────────────────────────────────────
Tests passing: [N]
Tests failing: [N] ← must be 0 to proceed
Test command: [npm test | pytest | go test | etc.]
Baseline saved: /tmp/refactor-baseline.txt
────────────────────────────────────────
Gate: If Tests failing > 0 → report BLOCKED. Do not proceed.
Identify refactoring targets using the standard smell catalog:
| Smell | Detection | Refactoring |
|-------|----------|-------------|
| Long function | > 30 lines with multiple concerns | Extract method |
| Duplicate code | Same logic in 2+ places | Extract and DRY |
| Magic numbers/strings | if (code === 42) | Extract named constant |
| God object | Class handling too many responsibilities | Extract class |
| Deep nesting | > 3 levels of if/for/try | Guard clauses, early returns |
| Long parameter list | > 4 parameters | Introduce parameter object |
| Feature envy | Method uses another class's data more than its own | Move method |
| Data clump | 3+ params always passed together | Extract into type/struct |
| Dead code | Unused variables, functions, imports | Delete safely |
| Inconsistent naming | Mixed conventions in same module | Systematic rename |
Produce:
SMELL INVENTORY
────────────────────────────────────────
[file:line] — [smell name] — [priority: P0|P1|P2]
[file:line] — [smell name] — [priority: P0|P1|P2]
────────────────────────────────────────
Total smells found: [N]
refactoring-agentDispatch the refactoring-agent with:
The agent will:
Handoff:
DONE → proceed to Phase 5 verificationDONE_WITH_CONCERNS → review concerns, proceed if only style-level (not behavior-level)BLOCKED → test failure during refactoring — agent will have reverted; review what went wrongAfter the agent completes:
# Re-run the full test suite
npm test 2>&1 | tee /tmp/refactor-after.txt
# Compare to baseline
echo "Before: $(grep -cE 'pass|PASS|✓' /tmp/refactor-baseline.txt 2>/dev/null) passing"
echo "After: $(grep -cE 'pass|PASS|✓' /tmp/refactor-after.txt 2>/dev/null) passing"
# Confirm the diff is structural, not behavioral
git diff HEAD | grep "^[+-]" | grep -v "^---\|^+++" | \
grep -vE "^[+-][[:space:]]*$" | head -30
Verification checklist:
REFACTORING COMPLETE
════════════════════════════════════════
Target: [files/modules refactored]
Smells fixed: [N of N found]
Changes:
[file] — [smell] → [refactoring applied]
[file] — [smell] → [refactoring applied]
Tests: [N] before → [N] after (must be equal)
Behavior: PRESERVED | CHANGES_NEEDED
Code delta: [shorter by N lines | flatter by N indentation levels]
Skipped (out of scope or too risky):
[file] — [reason]
Status: DONE | DONE_WITH_CONCERNS | BLOCKED
════════════════════════════════════════
mkdir -p docs/superomni/executions
_BRANCH=$(git branch --show-current 2>/dev/null | tr '/' '-' || echo "unknown")
_DATE=$(date +%Y%m%d)
_REF_FILE="docs/superomni/executions/refactoring-${_BRANCH}-${_DATE}.md"
echo "Refactoring record saved to ${_REF_FILE}"
development
Meta-skill: create, install, list, and manage skills and agents within the superomni framework. Merges writing-skills + agent-management into one unified workflow. Triggers: "create skill", "write a skill", "install skill", "list skills", "create agent", "write an agent", "install agent", "list agents", "new skill", "new agent", "add skill", "add agent", "manage framework".
testing
Dependency security, license, and freshness audit. Dispatches dependency-auditor agent to scan all package managers. Triggers: "dependency audit", "check dependencies", "npm audit", "security scan", "check for vulnerabilities", "outdated packages", "license check".
development
Meta-skill: use when creating a new skill for the superomni framework. Guides through the process of designing and writing a well-structured skill. Triggers: "create a new skill", "write a skill for", "add a skill that".
documentation
Use when turning a spec or idea into a concrete, executable implementation plan. Triggers: "write a plan", "plan this out", "create implementation plan", "how should we implement".