ワンクリックで
refactor
Refactor existing code for quality, performance, or maintainability. Enforces core quality principles with ratchet gate.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Refactor existing code for quality, performance, or maintainability. Enforces core quality principles with ratchet gate.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Discover and map an existing codebase before planning or changing it.
Change the behavior of existing code — story-driven by default, or --issue N for a GitHub bug fix. Test-first, full verification, code review.
Use when a planned change touches persisted data shape — ORM models, migration files, schema definitions, serialized formats, or message contracts — in /change, /refactor, or /implement on an existing codebase. Routes schema changes through expand-contract and proves reversibility before any deploy.
Generate production code and tests for a story group using agent teams for parallel execution.
Generate test plan, test cases, test data fixtures, and Playwright E2E tests mapped to acceptance criteria.
Use when bumping a dependency version — package.json, pyproject.toml, requirements, or lockfile changes — in an existing codebase. Classifies the bump, audits the usage surface from the code graph, and isolates the upgrade in its own proven commit.
| name | refactor |
| description | Refactor existing code for quality, performance, or maintainability. Enforces core quality principles with ratchet gate. |
| argument-hint | [file-or-module-path] |
| context | fork |
Ultracode tip: A whole-repo
--sweepis a broad "scan many files, report the conclusion" task — run/effort ultracodebefore it for wider coverage. A targeted/refactor <path>is narrow and deterministic; leave ultracode off (/effort high) for those.
/refactor src/service/extraction.py
/refactor src/repository/
/refactor --sweep # whole-repo entropy scan (formerly /lint-drift)
/refactor --sweep --auto-fix # sweep + auto-commit CLEANUP-class items
Provide a file path or directory for a targeted refactor. Use --sweep for a whole-repo entropy scan that reports accumulated drift and routes findings back into the per-principle fix flow. The skill analyzes the target against core quality principles, plans the changes, and executes them one principle at a time.
Refactoring improves the internal structure of existing code without changing its observable behavior. No new features. No behavior changes. Every change must trace to a violation of the core quality principles.
For tiny cleanup that is obviously safe and local (for example one unused import, one typo in a comment, one lint-only change), use /vibe instead. Use /refactor when the change affects structure, module boundaries, tests, or multiple files.
/refactor --sweep)/refactor <path> fixes a targeted area. /refactor --sweep runs the whole-repo entropy scan (this absorbs the former /lint-drift skill): it reports accumulated drift and routes the findings back into the per-principle fix flow below. Entropy control for agent-generated code — as agents replicate patterns, drift accumulates.
What the sweep scans:
code-graph.json, not grep): orphan/dead files (fan_in == 0), layer-violation import directions, unstable hubs, cycles. Run /code-map first if the graph is missing or stale (stale = .claude/state/graph-dirty.jsonl non-empty — the graph-refresh Stop hook normally drains it); prefer the graph over grep. Always grep for dynamic references (getattr, registries, importlib) before declaring anything dead.code-map nor a targeted refactor finds it).code-gen/SKILL.md (do not restate them); the length/type cases are also enforced live by the hooks — the sweep catches what predates them.Sweep workflow:
code-graph.json (/code-map) if missing or stale..claude/state/last-drift-scan.txt, a commit SHA); full scan if no marker.specs/reviews/drift-report.md — category, file:line, suggested fix, severity (CLEANUP / REFACTOR / DEBT).--auto-fix, CLEANUP-class items may be auto-committed — they must pass the full ratchet gate..claude/state/last-drift-scan.txt.When to sweep: after every ~5 /auto iterations, before a release, or when learned-rules.md grows past ~10 rules (pattern-accumulation signal). Do not refactor code outside the current change's scope without recording it as drift first.
Read .claude/skills/code-gen/SKILL.md in full. Its core quality principles are the refactoring standard. Every change planned in Step 4 must cite a specific principle.
If specs/brownfield/ exists, read architecture-map.md, test-map.md, risk-map.md, and change-strategy.md before analyzing the target. If this is a non-trivial existing codebase and those maps do not exist, recommend /brownfield before broad refactoring. Locate target symbols via symbol-map.md (Lstart-Lend anchors); for files flagged in skeletons/, read the .skel.md and then only the relevant symbol slice with Read(offset, limit) instead of the whole file.
Coverage preflight — REQUIRED SUB-SKILL: checking-coverage-before-change for every symbol in the target path before the first edit. COVERED symbols give you the regression oracle to run after each step; UNCOVERED symbols route to pinning-down-behavior (or sprouting-instead-of-editing) before any in-place edit.
Migration preflight — REQUIRED SUB-SKILL: checking-migration-safety if the refactor touches ORM models or schema files (e.g. renaming a model field). A behavior-preserving refactor that requires a schema migration is two deployables, not one commit.
For each file in the target path:
code-gen/references/architecture.md)any (TypeScript) or missing type hints (Python). Count unannotated parameters and return types.Record findings in a structured list before proceeding.
Map each finding from Step 2 to one of the core quality principles:
any, missing annotations, untyped domain concepts.except, untyped catches, swallowed errors.Only violations of these principles justify a change. Do not refactor code that complies with the principles.
Invoke superpowers:writing-plans to produce a structured refactoring plan. This ensures the plan is reviewed before execution and prevents ad-hoc changes that drift from the quality principles.
Produce a written plan before touching any code:
File: src/service/extraction.py
Change: Split extract_data() into extract_raw(), validate_schema(), transform_fields()
Principle: #3 — extract_data() is 87 lines
Risk: One caller in api/routes.py — update import after split
File: src/service/extraction.py
Change: Add return type annotation to all 4 functions
Principle: #2 — return types missing
Risk: None
List every file, what will change, which principle it violates, and any known call-site impact.
Apply changes for one principle across all affected files. Then run the test suite. Then proceed to the next principle.
Order of execution:
After each principle: run tests, run lint, run type checks. If anything breaks, fix it before moving to the next principle.
When committing, follow keeping-refactors-pure: commit with HARNESS_COMMIT_KIND=refactor git commit … — the pre-commit hook then blocks any staged test/snapshot edits (a pure refactor leaves them byte-identical). Any behavioral fix discovered en route goes in a separate behavior commit.
After all changes are complete, spawn the clean-code-reviewer agent (harness-provided: .claude/agents/clean-code-reviewer.md; recognized by the review-on-stop Stop hook) on the full diff.
The reviewer will return findings at three severity levels:
Address every BLOCK finding. Re-run the reviewer after each fix cycle. Maximum 3 retry cycles.
If BLOCK findings remain after 3 cycles, stop and report the unresolved issues. Do not ship code with unresolved BLOCK findings.
/change.The target path contains refactored code that:
getattr, decorator registries, and plugin systems reference symbols by string. Verify with a project-wide search before deleting.