Skip to main content
skill CX Codex

refactor · boshu2 — Claude Code skill: what it does, how to install

refactor — a Claude Code skill for restructuring code without changing observable behavior. Runs on requests to clean up, extract functions, remove dead code, or simplify logic.

Apache-2.0 123 installs ★ 458 updated 2026-11-13

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add boshu2/agentops --skill refactor -a claude-code
Open on GitHub

What refactor does

The skill applies a single isolated structural change such as extracting a function, inlining, or removing dead code. It treats behavior preservation as a testable claim: exit codes, error messages, and output hashes must match between runs.

Execution follows a fixed sequence. The agent runs targeted baseline checks or captures output hashes, applies the edit, and reruns the exact same checks. Any defect discovered during the transformation is noted in the report and left untouched, preventing unapproved logic changes.

If no runtime or test harness is present, the skill lists the exact commands to verify neutrality manually and marks uncovered surfaces under not checked. The tool does not commit changes or retry failures automatically.

What it helps with

  • extract shared logic without breaking public interfaces
  • simplify conditionals while verifying output hashes match
  • delete dead code while ensuring regression checks pass
  • report existing bugs separately without modifying behavior

Example prompts for the agent

  • Refactor parse_payload to simplify conditionals and verify output hashes
  • Extract repeated token validation in auth.ts without changing signatures
  • Clean up unused helper methods in export.py and run baseline checks

How to install refactor

1Skills CLI (Vercel)

npx skills add boshu2/agentops --skill refactor -a claude-code

2Manually: put the files in place

~/.claude/skills/refactor/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/refactor/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about refactor

How to install refactor in Claude Code?

Run npx skills add boshu2/agentops --skill refactor -a claude-code to add the skill. You can also place the skill file directly at ~/.claude/skills/refactor/SKILL.md.

What does refactor do?

It performs one bounded code restructuring step and verifies that observable behavior, return codes, and output hashes remain identical before and after.

What happens when refactor discovers an existing bug?

It records the file, line number, and reason in a suspected bugs section without fixing it. This prevents undocumented behavior changes during refactoring.

Source text

SKILL.md · Apache-2.0
---
name: refactor
description: 'Restructure or clean up code with no behavior change, proved by before-and-after checks. Use when: asked to clean up, extract, dedupe or simplify, even one function.'
practices:
- refactoring
- legacy-code-seams
- design-patterns
hexagonal_role: supporting
consumes:
- repo-context
produces:
- code-changes
context_rel: []
skill_api_version: 1
user-invocable: true
metadata:
  capabilities: [refactor]
  effects: [modify_source_files]
  canonical_status: canonical
  disposition: keep_specialist
  tier: execution
  dependencies: []
output_contract: code changes with regression evidence
---
# Refactor — one structural experiment

Refactor changes structure while preserving observable behavior. It performs one
caller-selected transformation and reports the result. "Behavior-preserving" is
a claim to prove with before/after checks, never to assert. A user sees it
worked when the report shows the same checks run before and after with
identical results, and any suspected bug listed unfixed.

## What counts as behavior

Unless the caller explicitly excluded a surface, all of these must survive:

- **Messages and exit codes.** Error and output text compares byte-for-byte;
  exit codes, error types and which input raises which error stay the same.
  Scripts and callers parse them. Preserve an inconsistent message and report
  it; normalizing it is a behavior change.
- **Differences between near-duplicates.** When merging duplicated branches,
  carry every difference (constants, comparisons, messages, extra steps) as a
  parameter or a branch. Do not unify a difference the caller has not declared
  accidental.
- **Interfaces.** Public signatures, defaults, return types, persisted field
  names, protocol values and CLI flags. Renaming one is a compatibility change
  unless the accepted scope provides for it.
- **Order and coverage.** Branch priority, default handling, evaluation count,
  side-effect order, and the set of tests that run. A pre-existing red that
  vanishes, or a test that stops running, is a behavior change.
- **Protective checks.** Trust-boundary validation, data-loss guards, security
  controls and accessibility affordances stay, even when another layer,
  upstream or downstream, duplicates one or no test reaches it. The neutrality
  gates cannot see their removal, so looking redundant is no reason to delete
  one.

## Procedure

1. Name the preserved behavior, the focused acceptance surface and the concrete
   structural problem for its callers, in the caller's domain terms.
2. Run the focused check and the smallest regression check the changed surface
   justifies, error paths included, and record that honest baseline, including
   reproducible ambient failures. Add any characterization test the
   transformation needs now, before the edit, so it is part of the baseline.
   For output-producing surfaces (generators, serializers, formatters,
   reports), hash the output over fixed inputs. When the caller's evaluation
   compares the built program before and after the change, pin the starting
   source, build its baseline before edits and keep that binary and the
   comparison inputs.
3. Apply one bounded transformation: extract, move, rename, inline, simplify a
   conditional, reduce parameters, encapsulate, substitute existing code, or
   delete dead code; the
   [refactoring catalog](references/behavior-preserving-simplification.md#refactoring-catalog)
   holds per-pattern safety checks. Judge it by what callers must
   understand and where a domain rule must change, not by file size.
4. A bug or suspicious inconsistency found on the way is reported separately
   (location, why it looks wrong) and left unfixed. Fixing it inside the
   refactor hides a behavior change the caller did not authorize.
5. Rerun exactly the step-2 checks over the same inputs and compare any
   output hashes byte-for-byte.
6. Report, then stop. A red result is evidence for the caller; this skill does
   not revert, narrow, retry, commit, validate, or route subsequent work.

When nothing can be executed (no runtime, no tests, code supplied in a
message), neutrality is unproven: give the exact before/after commands and
inputs the caller must run, error paths included, and list every surface under
`not checked`.

```text
transformation: <the one change>
preserved:      <behavior and surfaces from step 1>
checks:         <command>: before -> <result>; after -> <result>   (or "not run")
outputs:        <before/after hashes when the surface produces output>
abstraction:    <a new one and the second use or contract that justifies it, or "none">
diff:           <files touched>; only those the transformation names, plus tests added before the change
suspected bugs: <file:line, why>; reported, not fixed
not checked:    <surfaces no check covered>; present even when empty
```

## Neutrality gates

The checks from step 2 give the same result after as before: no new red, no
vanished red, identical output hashes. A mismatch is a behavior diff to surface
and explain, never to shrug at; the caller decides whether to keep, narrow, or
reverse it. A gate skipped or narrowed after the fact is the **post-hoc
neutrality** failure mode — the diff decides what got tested. Name any surface
the gates did not cover under `not checked`.

## Boundaries and tests

Refactoring after a check goes green is the step of test-driven development
that held for agents
([practice ledger](https://github.com/boshu2/agentops/blob/main/docs/practice-ledger.md#holds-in-a-different-shape)).

- Use [Implement](../implement/SKILL.md) to change behavior; it tidies its own
  fresh change under its acceptance checks.
- Use Refactor when restructuring is the request, or reaches code the behavior
  change did not touch; that green result is the baseline.
- Use [Architecture](../architecture/SKILL.md#procedure) to choose where to
  restructure or a new module boundary; this skill executes one transformation
  in the area the caller selected.

Reject a wrapper that only adds another name or pushes the same coordination into its callers. Keep
internal test seams private when callers need no substitution. Exercise the
preserved behavior through the caller-facing boundary; retain existing tests
until equivalent coverage of their distinct cases is demonstrated. Moving a
test is not permission to delete the behavior it protected.

Use the caller's vocabulary for extracted operations and types. A naming
ambiguity that changes behavior belongs with the existing domain definition;
consult [Domain](../domain/SKILL.md) only when that distinction needs work.

When the caller-chosen transformation can be cut more than one way (which
lines an extraction takes, where a chosen interface sits), probe before you
cut. A choice between module boundaries the caller has not made goes to
Architecture first.
Run the probe in disposable isolation (a scratch branch, worktree, or copied
tree the caller's policy allows): rough in the seam, see what it forces —
signature churn, import cycles, test rewrites — then discard the probe and
keep only the knowledge. Stop condition: at most two probes; if the second
candidate seam also fights back, report both findings to the caller instead of
trying a third. Cutting the first imaginable seam directly into the working
tree is the **premature seam** failure mode: the wrong boundary calcifies
because reverting it now costs more than living with it.

## References

- [Behavior-preserving simplification](references/behavior-preserving-simplification.md) — refactoring catalog and per-pattern safety checks
- [Codebase design](https://github.com/mattpocock/skills/blob/main/skills/engineering/codebase-design/SKILL.md) — Matt Pocock; the wrapper and test-seam rules under Boundaries and tests adapt it.

© boshu2 · Apache-2.0 · boshu2/agentops

Similar skills

All skills for Code review and refactoring →

refactor by other authors comes first.

refactor official github The refactor skill reshapes existing code without changing its external behavior. Use it for gradual cleanup of large functions, duplication, weak types, and code smells. CX Codex +1 22,321installs refactor parcadei refactor is a Claude Code skill that guides code changes through analysis, planning, implementation, review, and test validation. It activates for refactoring requests that should preserve existing behavior. CX Codex +1 625installs refactor smallnest refactor restructures code safely based on Martin Fowler's catalog without altering external behavior. Fixes code smells through small, verifiable steps. CX Codex +1 538installs refactor pproenca A skill with 43 refactoring rules drawn from Martin Fowler's catalog and Clean Code. Useful when you want the agent to restructure code by a fixed set of rules instead of guessing. CX Codex +1 661installs refactor FradSer Runs automated code refactoring on specified files, directories, or recent git modifications. CX Codex +1 113installs oma-refactor First Fluke oma-refactor — a skill for Claude Code and Codex restructures existing code while preserving observable behavior. Executes targeted technical debt cleanup through small test-gated transformations. CX Codex +1 ★ 1,472repo stars refactor-expert alirezarezvani The refactor-expert subagent reviews code structure and plans incremental refactoring. Use it to find duplication, architectural issues, and gaps in test coverage. CX Codex ★ 788repo stars code-refactoring akillness The code-refactoring skill turns a refactoring request into a behavior-aware plan with clear guardrails, reviewable slices, and a defined next step. CX Codex +1 552installs refactor-code davila7 refactor-code — a slash command that walks Claude through 17 refactoring steps, from analysis and tests to performance checks and review prep. CX Codex +1 4,890installs safe-refactor JuliusBrussee safe-refactor restructures code while preserving existing behavior, enforcing verification boundaries before structural edits. CX Codex +1 130,700installs refactor-project FradSer refactor-project — a skill for Claude Code, Codex, Cursor and other agents that runs automated refactoring across every file in the project at once. Cuts duplication, normalises patterns, and strips legacy code in a single pass. CX Codex +1 122installs

From the same set7

Project sponsors

Companies that keep this analytics open