skills-src/refactor/SKILL.md
Code improvement workflow with polish and overhaul tracks. Triggers: 'refactor', 'clean up', 'restructure', 'reorganize', or /refactor. Phases: explore, brief, implement, validate. Existing code only — Do NOT use for bug fixes (/debug) or new features (/ideate).
npx skillsauth add lvlup-sw/exarchos 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.
Two-track workflow for improving existing code. Polish track for small, contained refactors; overhaul track for architectural changes and migrations. Both tracks emphasize exploration before commitment and mandatory documentation updates.
Activate this skill when:
{{COMMAND_PREFIX}}refactor commandDisambiguation: If the user says "fix" or "clean up" — use {{COMMAND_PREFIX}}refactor when the code works but needs structural improvement. Use {{COMMAND_PREFIX}}debug when something is broken (error, crash, wrong behavior).
{{COMMAND_PREFIX}}refactor
|
+-----+-----+
| Explore |
+-----+-----+
|
+--------------+--------------+
| |
--polish (default)
| |
v v
+--------------+ +--------------+
| Polish | | Overhaul |
| Track | | Track |
+--------------+ +--------------+
# Default: overhaul track
{{COMMAND_PREFIX}}refactor "Description of what needs refactoring"
# Fast path: polish track
{{COMMAND_PREFIX}}refactor --polish "Small contained refactor description"
# Explore first, then decide track
{{COMMAND_PREFIX}}refactor --explore "Unsure of scope, explore first"
# Switch from polish to overhaul (during explore/brief)
{{COMMAND_PREFIX}}refactor --switch-overhaul
# Resume after context compaction
{{COMMAND_PREFIX}}rehydrate
| Aspect | Polish | Overhaul | |--------|--------|----------| | Scope | <=5 files, single concern | No limit | | Worktree | No (direct) | Yes (isolated) | | Delegation | No | Yes (full workflow) | | Documentation | Mandatory update phase | Mandatory update phase | | Human Checkpoints | 0 | 1 (merge) |
Before modifying any existing code behavior, capture current behavior as characterization tests. This is a mandatory pre-step for both tracks:
This aligns with Michael Feathers' approach in Working Effectively with Legacy Code — understand behavior before changing it.
At the brief phase, before committing to an approach, surface a Constraints section anchored to the architectural invariants relevant to the refactor. This is the refactor design-time equivalent of /ideate's Phase 0 and uses the same single shared source of truth for the selection rules: see @skills/brainstorming/references/constraint-anchoring.md. Load .exarchos/invariants.md (cost-of-load: always-load entries) and emit the Constraints section. devCatalog-gated: when .exarchos.yml: invariants.devCatalog: enabled is unset or disabled, surface no Constraints section and proceed directly. See @skills/refactor/references/brief-template.md for the brief-phase placement.
Fast path for small, contained refactors (<=5 files, single concern). Orchestrator may write code directly (exception to orchestrator constraints). No worktree, no delegation.
HSM phases: explore → brief → polish-implement → polish-validate → polish-update-docs → completed
For detailed phase instructions, state management, and auto-chain behavior, see @skills/refactor/references/polish-track.md.
Rigorous path for architectural changes, migrations, and multi-file restructuring. Uses full delegation model with worktree isolation.
HSM phases: explore → brief → overhaul-plan → overhaul-plan-review → overhaul-delegate → overhaul-review → overhaul-update-docs → synthesize → completed
For detailed phase instructions, skill invocations, and auto-chain behavior, see @skills/refactor/references/overhaul-track.md.
Initialize refactor workflow:
action: "init", featureId: "refactor-<slug>", workflowType: "refactor"
Use describe to discover the full state schema at runtime: exarchos_workflow({ action: "describe", actions: ["init"] }).
Sequential traversal required. Every phase MUST be traversed in order — you cannot skip phases, even if you have all the data for a later phase ready. For example,
exploremust transition tobriefbeforeoverhaul-plan— attemptingexplore→overhaul-plandirectly will be rejected by the HSM. Frombriefyou must go topolish-implementoroverhaul-plan, not directly tocompleted. Each transition requires its guard to be satisfied viaupdatessent alongside thephaseparameter in a singlesetcall. See@skills/refactor/references/polish-track.mdor@skills/refactor/references/overhaul-track.mdfor the exact tool call at each step.
Every phase transition has a guard that must be satisfied. Before transitioning, consult @skills/workflow-state/references/phase-transitions.md for the exact prerequisite for each guard.
The pattern for every transition: send the guard prerequisite in updates and the target in phase in a single set call.
Use exarchos_workflow({ action: "describe", actions: ["update", "init"] }) for
parameter schemas and exarchos_workflow({ action: "describe", playbook: "refactor" })
for phase transitions, guards, and playbook guidance.
For track-selection criteria at the explore phase, query the decision runbook:
exarchos_orchestrate({ action: "runbook", id: "scope-decision" })
This runbook provides structured criteria for choosing between polish and overhaul tracks based on file count, structural impact, and PR scope.
If scope expands beyond polish limits during explore or brief phase, use {{MCP_PREFIX}}exarchos_workflow with action: "update" to set track to "overhaul" and update explore.scopeAssessment.recommendedTrack.
Scope thresholds: If >5 files affected OR changes cross module boundaries -> recommend overhaul track.
Indicators to switch:
Output: "Scope expanded beyond polish limits. Switching to overhaul track."
CRITICAL: All skill invocations MUST use explicit Skill() tool calls:
| Skill | Invocation | Usage |
|-------|------------|-------|
| {{COMMAND_PREFIX}}plan | {{CHAIN next="plan" args="--refactor <state-file>"}} | Task extraction from brief |
| {{COMMAND_PREFIX}}delegate | {{CHAIN next="delegate" args="<state-file>"}} | Subagent dispatch for TDD |
| {{COMMAND_PREFIX}}review | {{CHAIN next="review" args="<state-file>"}} | Quality review |
| {{COMMAND_PREFIX}}synthesize | {{CHAIN next="synthesize" args="<feature>"}} | PR creation |
| Don't | Do Instead |
|-------|------------|
| Skip exploration | Always assess scope first (see references/explore-checklist.md) |
| Use polish for large changes | Switch to overhaul when scope expands |
| Skip doc updates | Documentation is mandatory (see references/doc-update-checklist.md) |
| Add features during refactor | Scope creep - stick to brief goals |
| Skip tests because "just moving code" | Refactors need test verification |
| Create design document for polish | Use brief in state file instead |
| Work in main for overhaul | Use worktree isolation |
When Exarchos MCP tools are available, emit events throughout the refactor workflow:
{{MCP_PREFIX}}exarchos_event with action: "append" → workflow.started with workflowType "refactor"exarchos_workflow set when phase is provided — emits workflow.transition with selected track (polish/overhaul)exarchos_workflow set when phase is provided — emits workflow.transition with from/to/trigger/featureId{{COMMAND_PREFIX}}delegate (subagents use git commit + git push per implementer prompt)git commit -m "refactor: <description>" + git push — no multi-branch stacking neededexarchos_workflow set when transitioning to terminal state — emits workflow.transition to "completed"tools
Land a subagent worktree branch onto an integration branch with preflight + recorded rollback. Triggers: operator (or `next_actions`) surfaces verb `merge_orchestrate` via Exarchos MCP. Local git operation — NOT remote PR merging (that is `merge_pr`).
tools
Land a subagent worktree branch onto an integration branch with preflight + recorded rollback. Triggers: operator (or `next_actions`) surfaces verb `merge_orchestrate` via Exarchos MCP. Local git operation — NOT remote PR merging (that is `merge_pr`).
tools
Land a subagent worktree branch onto an integration branch with preflight + recorded rollback. Triggers: operator (or `next_actions`) surfaces verb `merge_orchestrate` via Exarchos MCP. Local git operation — NOT remote PR merging (that is `merge_pr`).
tools
Land a subagent worktree branch onto an integration branch with preflight + recorded rollback. Triggers: operator (or `next_actions`) surfaces verb `merge_orchestrate` via Exarchos MCP. Local git operation — NOT remote PR merging (that is `merge_pr`).