Skip to content
Development
Skill

/refactor

Refactors code toward project-guideline compliance and testability (so /optimus:unit-test can safely increase coverage) across four analysis lenses, fanned out to parallel agents when the scope warrants it. Supports "testability" or "guidelines" focus plus flexible scoping.

From plugin
optimus
7519 skills2 agents1 hook

Claude Code

$ npx -y skills add oprogramadorreal/optimus-claude --skill refactor --agent claude-code

SKILL.md

refactor.SKILL.md
description: Refactors code toward project-guideline compliance and testability (so /optimus:unit-test can safely increase coverage) across four analysis lenses, fanned out to parallel agents when the scope warrants it. Supports "testability" or "guidelines" focus plus flexible scoping. Read-only until the user approves the plan; applied changes stay local. Use after /optimus:init, before /optimus:unit-test; for an iterative loop, use /optimus:deep refactor.
disable-model-invocation: true
argument-hint: "[testability|guidelines] [scope]"

Project-Wide Code Refactoring

Analyze existing source code against the project's own guidelines across four lenses — inline for a small scope, fanned out to parallel agents otherwise — present a prioritized refactoring plan, then apply only user-approved changes with test verification. Two goals:

1. **Guideline compliance** — align code with coding-guidelines.md, architecture.md, styling.md, and testing.md 2. **Testability** — restructure code so `/optimus:unit-test` can safely increase coverage without risky refactoring

Step 1: Prerequisites and scope

If `git rev-parse --is-inside-work-tree` does not return `true`, read `$CLAUDE_PLUGIN_ROOT/skills/init/references/multi-repo-detection.md` and apply it. When it returns `true`, resolve the repository root with `git rev-parse --show-toplevel`, including in a linked worktree or subdirectory. In a multi-repo workspace, load each targeted repo's `.claude/CLAUDE.md` and `.claude/docs/` (not the workspace root's) and apply that repo's context to its files; if the scope doesn't determine a repo, ask which one.

If `.claude/CLAUDE.md` or `.claude/docs/coding-guidelines.md` is missing, recommend `/optimus:init` first. On the user's choice to continue, fall back to the bundled baseline: read `$CLAUDE_PLUGIN_ROOT/skills/init/templates/docs/coding-guidelines.md` and work against it plus general best practices, and note in the report that findings are generic, not project-specific.

**Focus:** a bare `testability` or `guidelines` argument sets the focus and is consumed from the scope text — a keyword inside a quoted string is scope, not focus (`"improve testability in auth"` → no focus). If both appear, take the first and say the other needs its own pass. Everything remaining is natural-language scope.

**Scope:** if the arguments describe a scope, map it to directory paths by scanning the project structure — no question needed. Otherwise use `AskUserQuestion` (header "Scope"):

  • **Full project** — all source directories (default when the user just says "refactor")
  • **Directory** — specific path(s) for targeted cleanup
  • **Changed since** — files modified since a commit, tag, or date

For changed-since, use `git diff --name-only <ref>...HEAD` for commits, branches, and tags; for relative dates use `git log --no-merges --since="2 weeks ago" --format= --name-only` instead (`--since` is a `git log` flag, not `git diff`). Apply Step 3's exclusions to the result. In a monorepo with full-project scope, ask which subprojects to include (default: all).

Step 2: Harness mode

If your invocation prompt contains `HARNESS_MODE_INLINE`, you are a single iteration inside the `/optimus:deep` orchestrator: read `$CLAUDE_PLUGIN_ROOT/references/harness-mode.md` and follow its single-iteration protocol, which overrides the interactive steps. Before interpreting the progress file's iteration and findings fields, inspect its `harness` field: only when it equals `"test-coverage"`, also load the **Refactor Phase Execution** section of `$CLAUDE_PLUGIN_ROOT/references/coverage-harness-mode.md` and apply its field mapping and overrides (both dispatches use `Phase: refactor`).

Step 3: Load project context and map analysis areas

Read `$CLAUDE_PLUGIN_ROOT/skills/init/references/constraint-doc-loading.md` and load the docs it lists, applying its skill-authoring lens, its monorepo scoping rule, and its submodule exclusion. These docs define the rules: every suggestion must be justified by what they establish — never impose external preferences.

Within the scope, identify source directories. Skip non-source directories (dependencies, build output, framework caches, dot-directories), minified/lock/binary files, and the generated source files listed under "All Agents Exclude" in `$CLAUDE_PLUGIN_ROOT/references/shared-agent-constraints.md`. Group areas by top-level source directory (monorepo: by subproject, then directory) and rank by recent churn:

git log --no-merges --since="3 months" --format= --name-only -- <scope-path> | sort | uniq -c | sort -rn

Analyze highest-churn areas first; for full-project scope on a large codebase, start with the top 10. Briefly summarize docs loaded (and any missing, with fallback status), project type, and ranked areas, then proceed immediately — do not wait for confirmation.

Step 4: Four-lens analysis

The four lenses below must all be covered. Size the fan-out to the scope: **for a handful of files, analyze them yourself in one pass** — four subagents over three files each re-read CLAUDE.md and the guideline docs to reach findings you can reach directly; read the prompt files below for their criteria and work through them. Fan out for a directory or wider, where the lenses genuinely read different parts of the tree.

When you do fan out, launch all 4 agents as `general-purpose` Agent tool calls in a **single** message so they run in parallel — separate messages serialize them for no benefit.

| Agent | Prompt file | Finds | |---|---|---| | 1 — Guideline Compliance | `agents/guideline-reviewer.md` | Explicit doc violations, each citing the rule it breaks | | 2 — Testability Analyzer | `agents/testability-analyzer.md` | Structural barriers to unit testing | | 3 — Consistency Analyzer | `agents/consistency-analyzer.md` | Cross-file duplication, inconsistency, drift | | 4 — Code Simplifier | `agents/code-simplifier.md` | Unnecessary complexity, naming, dead code |

Read the pr

Read more
@oprogramadorreal
Ships withoptimus

Primes your project for peak Claude Code performance

7519 skills2 agents
Get the whole plugin

Stars on GitHub

Momentum

stars in total
75
Star history of oprogramadorreal/optimus-claude on GitHub, a chart by star-history.com

Other skills on optimus.

brainstorm

brainstorm

Runs a structured design conversation — clarifies intent, proposes 2-3 approaches with trade-offs, iterates the design — and…

code-review

code-review

Reviews local changes, an open PR/MR, or a branch diff for bugs, security issues, and violations of the project's own coding…

commit

commit

Stages, commits, and optionally pushes local changes with a Conventional Commits message — always previews and confirms first,…

deep

deep

Runs an iterative auto-fix loop on a chosen target — review, refactor, or coverage — dispatching the base skill into fresh…

dream

dream

Prunes and consolidates the project's auto-memory to keep it minimal — deletes stale, wrong, or redundant memories, merges…

gauntlet

gauntlet

Runs a Gauntlet Loop: turns an ambitious goal into a builder/critic prompt judged against a concrete quality bar, confirms with…

More skills from other plugins.

brainstorming
Auto-invoked

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…

@obra@obraInstall Skill
ask-matt

ask-matt

Ask which skill or flow fits your situation. A router over the skills in this repo.

skills11d309k
@mattpocock@mattpocockInstall Skill
accessibility
Auto-invoked

accessibility

Design, implement, and audit accessible UI to WCAG 2.2 Level AA across Web, iOS, and Android — semantic ARIA roles and labels,…

ecc12d293k
@affaan-m@affaan-mInstall Skill
compiler-commit

compiler-commit

Use when you want to verify compiler changes and commit with the correct convention. Runs tests, lint, and format, then commits…

react2d252k
@facebook@facebookInstall Skill

Questions about refactor.

Which coding agents can use refactor?
refactor installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.