Skip to main content
ClaudeWave
Skill81 repo starsupdated today

refactor

>

Install in Claude Code
Copy
git clone --depth 1 https://github.com/testdouble/han /tmp/refactor && cp -r /tmp/refactor/han.coding/skills/refactor ~/.claude/skills/refactor
Then start a new Claude Code session; the skill loads automatically.

SKILL.md

## Project Context

- git installed: !`git --version 2>/dev/null`
- current branch: !`git branch --show-current 2>/dev/null`
- working tree: !`git status --porcelain 2>/dev/null | head -5`
- CLAUDE.md: !`find . -maxdepth 1 -name "CLAUDE.md" -type f`
- project-discovery.md: !`find . -maxdepth 3 -name "project-discovery.md" -type f`

## Constraints (read before anything else)

This skill restructures existing production and test code in your working
tree. It is an execution skill, not a document generator. These constraints
shape every step and override any instinct to move faster. The canon they
derive from, with provenance, is in
[references/refactoring-discipline.md](./references/refactoring-discipline.md);
pull that reference when a step needs the full rule or a step feels off.

- **Behavior preservation is the definition.** A refactoring changes internal
  structure without changing observable behavior. A change that alters
  behavior is not a refactoring done badly; it is not a refactoring at all.
  When a planned step turns out to require a behavior change, it leaves this
  skill's scope: defer it with a note naming the behavior change it needs.
- **Tests are the license to refactor.** No edit until the full suite has
  been run, observed green, and the target's behavior is covered. If coverage
  of the target cannot be established, stop and offer the characterization
  path in the reference; never refactor blind.
- **Small named steps, green to green.** Each step is one named refactoring
  with a bounded mechanic (extract function, rename, move, inline, and so
  on), and the suite runs after every step. A red suite after a step means
  revert the step, not patch forward.
- **The declared scope is a contract.** The target named in Step 1 bounds
  every edit. When a step starts pulling in files outside that scope, stop,
  report the spread, and let the user re-scope. Spreading edits are how a
  refactoring silently becomes a rewrite.
- **Never alongside an active tdd loop.** If the working tree shows a
  red-green cycle in flight (failing tests, a half-implemented behavior), do
  not run: the refactor step of `/tdd` owns cleanup inside the loop, and
  restructuring while a test is red violates the two-hats rule both skills
  share.
- **Refactor-only changes.** No behavior fixes, no features, no drive-by bug
  fixes, even when you spot one. Record what you found and leave it. If the
  user asks for commits, each commit contains refactoring only.
- **YAGNI governs the plan.** Apply
  [../../references/yagni-rule.md](../../references/yagni-rule.md): every
  refactoring in the plan needs evidence the code has a reason to change (a
  review finding, named duplication, a confusing read documented by the user,
  upcoming work in that area). Removing duplication is the job; adding
  speculative abstraction, configuration, or indirection is not. Defer
  evidence-free items with a reopen trigger.

# Refactor

## Step 1: Bind the Target and Resolve Project Config

**Bind the target.** Resolve the request to a named target: specific files or
directories, a named code smell in a named place, or the refactoring findings
in a provided document (a `/code-review` report, an `/architectural-analysis`
report, or equivalent). When a findings document is given, extract only the
refactoring-shaped findings (structural suggestions, duplication, naming,
coupling) and record each finding's ID so the summary can trace back to it.
If the request is open-ended ("clean up the codebase", "improve quality")
with no named target, ask the user for one before doing anything: open-ended
refactoring runs are the documented failure mode this skill exists to avoid,
and a wrong guess here burns the whole run.

**Resolve commands.** Read CLAUDE.md's `## Project Discovery` section for the
test command (under `### Commands and Tests`), the lint command, the build
command, language, and framework. If absent, fall back to
`project-discovery.md`. If still absent, run
`${CLAUDE_SKILL_DIR}/scripts/detect-refactor-context.sh` and parse its
output for git state and manifest-inferred commands. A missing test command is
a hard blocker: exhaust inference, then ask the user, because this skill
cannot run without a way to verify behavior. Also note any type-check command
the project has; where one exists it runs alongside the tests as a second
behavior-preservation check.

**Resolve standards and decisions.** Resolve the coding-standards directory
and ADR directory the same way: CLAUDE.md's `## Project Discovery` section,
then `project-discovery.md`, then Glob defaults (`docs/`, `docs/adr/`,
`docs/coding-standards/`, `docs/decisions/`). Also check `CLAUDE.md` and
`AGENTS.md` for inline standards. Read the standards and ADRs whose titles,
paths, or one-line summaries indicate they govern the target area; cap at
five documents. The target's restructured form must conform to these. If none
exist, state that plainly and infer conventions from the surrounding code.

## Step 2: Establish the Safety Net

**Check the tree.** If `working tree` in Project Context shows failing
mid-cycle work or the user describes an in-flight tdd loop on this code,
stop and say why (the never-alongside-tdd constraint). Uncommitted but
complete work is fine; recommend committing it first so refactoring diffs
stay clean, then proceed.

**Run the full suite.** Run the resolved test command with Bash. **Paste the
runner's summary line.** If anything is red, stop: a red suite is not a
license to refactor. Report the failures and recommend fixing them first
(via `/investigate` or `/tdd`), or re-scoping the target away from the
broken area.

**Establish coverage of the target.** Confirm the target's observable
behavior is exercised by the suite: find the tests that drive the target
(Glob and Grep for the target's public symbols in test files), and run them
scoped if the runner supports it. State plainly which behaviors are covered
and which are not. If the target has n