From optimus
Refactors project-wide code for guideline compliance and testability using 4 parallel analysis agents. Aligns with coding guidelines and removes barriers before unit testing to prevent tech debt.
npx claudepluginhub oprogramadorreal/optimus-claude --plugin optimusThis skill uses the workspace's default tool permissions.
Analyze existing source code against the project's coding guidelines using 4 parallel agents to find guideline violations, testability barriers, cross-file duplication, and pattern inconsistency. Present a prioritized refactoring plan, then apply only user-approved changes with test verification.
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Searches prompts.chat for AI prompt templates by keyword or category, retrieves by ID with variable handling, and improves prompts via AI. Use for discovering or enhancing prompts.
Checks Next.js compilation errors using a running Turbopack dev server after code edits. Fixes actionable issues before reporting complete. Replaces `next build`.
Analyze existing source code against the project's coding guidelines using 4 parallel agents to find guideline violations, testability barriers, cross-file duplication, and pattern inconsistency. Present a prioritized refactoring plan, then apply only user-approved changes with test verification.
Two primary goals:
/optimus:unit-test can safely increase coverage without risky refactoringThe code-simplifier agent guards new code after every edit — this skill is the on-demand complement for restructuring existing code across the project.
Read $CLAUDE_PLUGIN_ROOT/skills/init/references/multi-repo-detection.md for workspace detection. If a multi-repo workspace is detected, resolve prerequisites per-repo:
.claude/CLAUDE.md and .claude/docs/ as prerequisites (not the workspace root)Read $CLAUDE_PLUGIN_ROOT/skills/init/references/prerequisite-check.md and apply the prerequisite check (CLAUDE.md + coding-guidelines.md existence, fallback logic).
Extract from the user's arguments:
deep flag (present/absent)harness keyword after deep (present/absent)deep or deep harness → iteration cap (optional, default 8, hard cap 10)testability (case-insensitive) → set focus = "testability"guidelines (case-insensitive) → set focus = "guidelines"focus = null (balanced — current behavior)/optimus:refactor "improve testability in auth" → focus=null, scope="improve testability in auth" (inside quotes)/optimus:refactor "fix guidelines compliance" → focus=null, scope="fix guidelines compliance" (inside quotes)Examples:
/optimus:refactor → full project, normal mode/optimus:refactor "focus on auth module" → scope to auth, normal mode/optimus:refactor testability → full project, normal mode, focus=testability/optimus:refactor guidelines → full project, normal mode, focus=guidelines/optimus:refactor testability "focus on src/api" → scope to src/api, focus=testability/optimus:refactor deep → full project, deep (8 iterations)/optimus:refactor deep 10 backend → scope to backend, deep (10 iterations)/optimus:refactor deep guidelines → full project, deep, focus=guidelines/optimus:refactor deep harness → harness mode, 8 iterations, full project/optimus:refactor deep harness testability → harness mode, focus=testabilityIf the iteration cap exceeds 10, clamp it to 10 and warn: "Iteration cap clamped to 10 (maximum)." If the iteration cap is less than 1, clamp it to 1 and warn: "Iteration cap clamped to 1 (minimum)."
AskUserQuestion needed.AskUserQuestion — header "Scope", question "What scope would you like to refactor?":
Default to full project if the user just says "refactor" without specifying.
For changed since: use git diff --name-only <ref>...HEAD for commit SHAs, branch names, 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). Filter to source files only (apply the exclusion rules from Step 3).
For monorepos with full project scope: ask which subprojects to include (default: all). For directory scope: auto-detect which subproject the path belongs to.
If the system prompt contains HARNESS_MODE_ACTIVE, read $CLAUDE_PLUGIN_ROOT/references/harness-mode.md and follow its single-iteration execution protocol. The reference covers progress file reading, state initialization, scope and file-list rules, and step overrides (including the Step 8 apply/output protocol). Then proceed through Step 1's scope resolution (branch-diff path only — no AskUserQuestion), Step 3, and Step 4 — skip only the Step 2 deep-mode confirmation. If scope_files.current is non-empty in the progress file, treat it as the pre-resolved scope (the harness pre-populated it from the feature-branch diff) and skip the Step 1 scope resolution entirely.
If HARNESS_MODE_ACTIVE is NOT in the system prompt, continue with the standard interactive flow below.
If the harness keyword was detected in Step 1, read the Skill-Triggered Invocation section of $CLAUDE_PLUGIN_ROOT/references/harness-mode.md and follow its steps. Pass:
skill_name = refactorscope = scope text from Step 1 argument parsingmax_iterations = parsed iteration cap from Step 1 (if specified)focus = focus keyword from Step 1 (if detected)The reference protocol presents the command and stops. Do not proceed to Step 3 or any remaining steps.
If the deep flag was detected in Step 1, activate deep mode. Deep mode loops analysis-apply cycles (Steps 4–8) until clean or the iteration cap is reached.
Before proceeding, check whether a test command is available (from .claude/CLAUDE.md). If no test command exists, deep mode's auto-apply loop has no safety net — fall back to normal mode and warn: "Deep mode requires a test command for safe auto-apply. Falling back to normal mode — re-run /optimus:init to set up test infrastructure first." Set deep-mode to false. Then continue with the standard single-pass flow.
If a test command is available, warn the user:
Deep mode runs up to [cap] iterative refactoring passes. Each iteration is a full multi-agent analysis cycle — credit and time consumption multiplies with iteration count. Fixes are applied automatically at each iteration without per-change approval. Low test coverage increases the chance of undetected breakage; consider running
/optimus:unit-testfirst to strengthen the safety net. Each iteration also accumulates context — on large codebases, output quality may degrade in later iterations.Test command:
[test command from CLAUDE.md]
Then use AskUserQuestion — header "Deep mode", question "Proceed with deep mode?":
Tell the user: Tip: For large codebases or extended sessions, re-run with /optimus:refactor deep harness to launch the external harness with fresh context per iteration.
If the user did not invoke with deep, skip this step.
If the user selects Normal mode, continue with the standard single-pass flow. Record the user's choice as a deep-mode flag for subsequent steps. If deep mode is confirmed, initialize iteration-count to 1, total-applied to 0, total-reverted to 0, and accumulated-findings to an empty list. Each entry in accumulated-findings tracks: file (with line), category, guideline (the specific project rule, barrier type, or quality concern from the agent finding), summary (one-sentence description of the issue), fix description (brief description of the applied or attempted change), iteration (which iteration discovered it), and status (updated through apply/test phases).
Read $CLAUDE_PLUGIN_ROOT/skills/init/references/constraint-doc-loading.md for the full document loading procedure (single project and monorepo layouts, scoping rules).
These files define the rules. Every suggestion must be justified by what these docs establish — never impose external preferences.
Apply the "Submodule Exclusion" rule from $CLAUDE_PLUGIN_ROOT/skills/init/references/constraint-doc-loading.md — exclude submodule directories from the analysis.
Within the chosen scope (Step 1), identify source directories. Skip non-source:
.git, .github, .vscode, .idea, .claude, .huskynode_modules, vendor, .venv, venv, envdist, build, out, target, bin, obj, coverage.next, .nuxt, __pycache__, .cache, .tox, .turbo
Also skip non-source file types: *.min.js, *.min.css, lock files (package-lock.json, yarn.lock, pnpm-lock.yaml, etc.), *.d.ts (unless hand-written), binary and data files.Also skip generated source files that should never be manually edited: *.g.dart, *.freezed.dart, *.mocks.dart (Dart/Flutter build_runner output), *.Designer.cs (Visual Studio generated), and any file inside a directory named Migrations/ (database migration files — EF Core, Django, Alembic, etc.).
Single project: Group files by top-level source directory. Monorepo: Organize by subproject, then by source directory within each.
Harness mode: If scope_files.current from the progress file is non-empty, derive analysis areas from the unique parent directories of those files instead of scanning the whole project — this keeps the refactor pass focused on the feature branch's actual footprint.
Rank directories by recent change frequency:
git log --no-merges --since="3 months" --format= --name-only -- <scope-path> | sort | uniq -c | sort -rn
Analyze highest-churn directories first. For full-project scope on large codebases, start with the top 10 most active areas.
Before proceeding to analysis, present a brief summary: docs loaded (with paths), docs missing (with fallback status), project type (single/monorepo/multi-repo workspace), and analysis areas identified with their git activity rank. Proceed immediately to Step 4 — do not wait for user confirmation.
Launch all 4 agents as general-purpose Agent tool calls in a single message so they run in parallel. The full fan-out is the design — do not reduce the count to save tokens or time.
Each agent receives the list of source files/directories from Step 3.
Read the agent prompt files from $CLAUDE_PLUGIN_ROOT/skills/refactor/agents/ for individual agent prompts. Read $CLAUDE_PLUGIN_ROOT/skills/refactor/agents/shared-constraints.md for the shared quality bar, exclusion rules, and false positive guidance.
If deep mode is active and iteration-count > 1, prepend the iteration context block to every agent prompt before the file list. Read $CLAUDE_PLUGIN_ROOT/skills/refactor/agents/context-blocks.md for the template and format.
| Agent | Role | Runs when |
|---|---|---|
| 1 — Guideline Compliance | Explicit violations of project docs with exact rule citations | Always |
| 2 — Testability Analyzer | Structural barriers to unit testing — hardcoded deps, tight coupling, global state | Always |
| 3 — Consistency Analyzer | Cross-file duplication, pattern inconsistency, missing abstractions, architectural drift | Always |
| 4 — Code Simplifier | Unnecessary complexity, naming, dead code, pattern violations | Always |
Each agent returns a structured list of findings, bounded by the Finding Cap rule in $CLAUDE_PLUGIN_ROOT/references/shared-agent-constraints.md. The Guideline Compliance agent (Agent 1) is constructed dynamically based on Step 3's doc loading results (single project vs monorepo paths).
Wait for all launched agents to complete before proceeding to Step 5.
Independently verify each finding to filter false positives. Apply the verification protocol from $CLAUDE_PLUGIN_ROOT/skills/init/references/verification-protocol.md — treat agent-reported findings as claims that require independent evidence, not as ground truth.
For each finding from Step 4:
For each unique file that has findings, check recent git history for deliberate changes:
git log --no-merges --format="%h %s" -5 -- <file>
If a recent commit message clearly indicates deliberate code introduction (e.g., "add dependency injection", "extract service layer") and a finding suggests removing or reverting that code → reduce the finding's confidence by one level (High → Medium, Medium → Low → drop).
For uninformative commit messages (fewer than 15 characters, or generic like "fix", "update", "changes"), run git show <sha> -- <file> to examine the actual diff for intent patterns: added abstractions, validation logic, or architectural changes. Apply the same confidence reduction if the diff shows deliberate structural code that a finding wants to undo.
Skip gracefully if git log fails or returns no results (e.g., shallow clone, newly created file, or file outside the repository).
Assign confidence:
Only findings with High or Medium confidence proceed to Step 6.
Merge validated findings from Steps 4–5. Deduplicate: if two agents flagged the same file and line range for the same category, keep the more detailed version. For findings flagged by both Agents 1 and 3, merge into one finding and note "confirmed by independent review".
After deduplication, check for cross-agent contradictions — findings that target the same code region but recommend opposite directions (e.g., "extract this to reduce duplication" vs. "inline this for clarity"). Keep the higher-severity finding and drop the other. When severities are equal, keep the finding that matches the active focus (testability or guidelines). When no focus is active, keep the testability finding — testability is a primary goal of this skill.
Maximum 15 findings per run — only include findings that represent distinct root causes with supporting evidence. Do NOT pad to reach the cap: a focused plan of 3 strong findings is preferred over 15 weak ones.
When focus is active (testability or guidelines):
testability focus: Testability Barrier findings + any finding with a "Testability impact" line → reserved slots. All others → other slots.guidelines focus: Guideline Violation, Inconsistency, Duplication, Missing Abstraction, Architectural Drift, Code Quality findings → reserved slots. Testability Barrier findings → other slots.When no focus is active (default balanced mode):
If more issues exist, note the count (e.g., "15 of ~24 findings shown") and suggest re-running with a narrower scope or /optimus:refactor deep.
Deep mode: Instead of presenting the output format below, append this iteration's validated findings to accumulated-findings. For each appended finding, record the current iteration-count as the finding's iteration number, and preserve the agent's guideline citation (or barrier type for testability findings) and issue description as the finding's guideline and summary fields. Deduplicate against previous iterations: if a finding matches an existing entry by file + line range + category, skip it if the existing entry is marked "(fixed)". If the existing entry is marked "(persistent — fix failed)", annotate the new entry as "(persistent — fix failed)". If the existing entry is marked "(reverted — test failure)", keep the new entry as "(reverted — attempt 2)" so Step 8 retries the fix once more; only promote to "(persistent — fix failed)" if it is reverted again. Then proceed directly to Step 8.
Normal mode: Present findings using the output format below, then proceed to Step 7.
## Refactoring Plan
### Summary
- Scope: [full project / directory / changed since X]
- Focus: [testability / guidelines / balanced (default)]
- Areas analyzed: [N]
- Total findings: [N] shown (of ~[M] detected) — Critical: [N], Warning: [N], Suggestion: [N]
- Cross-cutting findings: [N]
- Testability improvements: [N] findings will make code testable for /optimus:unit-test
- Top recommendation: [one-sentence summary of highest-impact finding]
### Cross-Cutting Findings
**[N]. [Finding title]** (Critical/Warning/Suggestion)
- **Files:** `file1:line`, `file2:line`, ...
- **Category:** [Guideline Violation | Testability Barrier | Duplication | Inconsistency | Missing Abstraction | Architectural Drift]
- **Guideline:** [which project guideline this addresses]
- **Pattern:** [brief description of the cross-file issue]
- **Suggested:** [brief description of the fix approach]
- **Testability impact:** [what becomes testable after this refactoring — omit if not applicable]
### Findings by Area
#### [Area/Module Name] — [path]
**[N]. [Finding title]** (Critical/Warning/Suggestion)
- **File:** `file:line`
- **Category:** [Guideline Violation | Testability Barrier | Code Quality | Duplication | Inconsistency | Missing Abstraction | Architectural Drift]
- **Guideline:** [which project guideline this addresses]
- **Current:**
[code sketch — max 5 lines]
- **Suggested:**
[code sketch — max 5 lines]
- **Testability impact:** [what becomes testable after this refactoring — omit if not applicable]
### Areas with No Findings
- [Area name] — code follows project guidelines
Prioritize by severity:
Include "Areas with No Findings" to confirm coverage — the user should know those areas were reviewed, not skipped.
No findings at all:
Deep mode: this is the convergence signal — report "Deep mode complete — no findings on iteration [N]", then skip to the cumulative summary and recommendation in Step 8 (bypass the apply phase).
Normal mode: Report as a positive result ("code follows project guidelines and is well-structured for testing"). Suggest tightening guidelines or broadening scope if the user expected issues. Skip Steps 7–8 and proceed directly to the recommendation in the Important section below.
Deep mode: Skip this step — auto-select "Apply all" and proceed directly to Step 8.
Normal mode:
Use AskUserQuestion — header "Action", question "How would you like to proceed with the refactoring findings?":
If the user selects Selective, ask which finding numbers to apply (e.g., "1, 3, 5"). Remember the user's choice and approved finding numbers for Step 8.
For each approved finding (skipping any annotated "(persistent — fix failed)" — these have already failed in a prior iteration):
After applying all approved changes, run the project's test command (from .claude/CLAUDE.md) if available. Follow the verification protocol from $CLAUDE_PLUGIN_ROOT/skills/init/references/verification-protocol.md — run tests fresh, read complete output, report actual results with evidence:
deep-mode is true, annotate each applied finding as "(fixed)" in accumulated-findings and add the count of applied fixes to total-applied.deep-mode is true: annotate kept changes as "(fixed)" in accumulated-findings (add to total-applied); for failed changes, if already annotated "(reverted — attempt 2)" promote to "(persistent — fix failed)", otherwise annotate as "(reverted — test failure)" (add to total-reverted).If no test command is available, warn the user that changes were applied without automated verification and carry higher risk.
Deep mode (non-final iteration): Skip this subsection — the iteration progress summary in the deep mode loop below replaces it.
Deep mode (final iteration) and Normal mode:
/optimus:unit-test to cover the restructured codeNormal mode: Skip this subsection — proceed to the recommendation below.
Deep mode: If arriving here from convergence (zero findings in Step 6), skip the termination checks below and go directly to the cumulative summary.
After applying changes and running tests, check termination conditions (the iteration's counts were already added to total-applied and total-reverted in the apply phase above):
iteration-count >= the cap → cap reached. Report: "Deep mode reached the iteration cap ([cap]). Remaining findings may exist — continue in a fresh conversation: re-run /optimus:refactor deep, increase the cap with /optimus:refactor deep [higher-cap], or narrow scope with /optimus:refactor deep \"focus on <area>\"."For all four conditions above, present the iteration report immediately after the termination/continuation message. This report is informational and non-blocking — no user prompt follows:
#### Iteration [N] — Report
| # | File | What Changed | Reason | Guideline / Category | Status |
|---|------|-------------|--------|---------------------|--------|
[one row per finding attempted in THIS iteration from accumulated-findings where iteration == current]
Column definitions:
file:linefixed, reverted — test failure, reverted — attempt 2, or persistent — fix failedFor condition 4 (continue), after presenting the iteration report also show the progress summary: "Iteration [N] of up to [cap] — [total-applied] findings applied so far, [total-reverted] reverted. Starting next pass..." If the next iteration will be 3 or higher, append to the progress summary: "Note: context is accumulating — if output quality degrades, consider finishing remaining findings in a fresh conversation." Then increment iteration-count and return to Step 4 for the next analysis pass. Keep the same scope from Step 1.
After the loop ends, present a cumulative report across all iterations:
## Deep Mode — Cumulative Report
**Summary:**
- Total iterations: [N]
- Total findings applied: [N]
- Total findings reverted (test failures): [N]
- Total findings persistent (fix failed): [N]
- Final test status: pass / fail / not available
- Testability improvements: [N] changes made code testable for /optimus:unit-test
**All Changes:**
| # | Iter | File | What Changed | Reason | Guideline / Category | Status |
|---|------|------|-------------|--------|---------------------|--------|
[one row per finding from accumulated-findings, across all iterations, ordered by iteration then sequence]
Column definitions match the per-iteration report table, plus:
The summary statistics provide a quick overview; the detailed table provides full auditability of every change attempted across all iterations.
git diff before committing)After the refactoring is complete, recommend the next step based on the outcome:
/optimus:commit to commit the fixes, then /optimus:unit-test to cover the restructured code/optimus:commit to commit the accumulated fixes, then /optimus:unit-test to strengthen test coverage on the newly-testable code/optimus:unit-test directly if coverage needs improvementTell the user:
/optimus:refactor deep to iterate automatically — it applies, tests, and repeats until clean (max 8 passes by default, configurable up to 10). Requires a test command in .claude/CLAUDE.md.