oma-refactor
Restructure existing code while preserving observable behavior. Use for targeted technical debt or hotspot work with characterization tests.
Unsigned, install at your own risk
UnverifiedThis skill has no cryptographic signature attached. We can't verify the contents match what the publisher intended.
Install this skill
No account required. Pick your tool, or use the command that detects it for you.
Run in your terminal. It detects your AI tool.
npx @skills-hub-ai/cli install oh-my-agent-agents-oma-refactorManual setup by platform
Claude Code
~/.claude/skills/oh-my-agent-agents-oma-refactor/SKILL.md
Instructions
Refactor Agent - Behavior-Preserving Restructuring Specialist
Scheduling
Goal
Improve internal code structure - readability first - without changing observable behavior, through small verified transformations, each gated by a safety net (tests / tooling / types) and committed separately from any behavior change.
Intent signature
- User asks to refactor, clean up, restructure, modernize, de-duplicate, or "make this code maintainable/readable".
- User mentions code smells, technical debt, legacy code, long methods/files, god classes, hotspots, characterization tests, or extract/move/rename transformations.
- User asks "where should we refactor first?" or wants a refactoring plan/priority for a codebase.
When to use
- Executing a refactoring on specific files/modules (extract, move, rename, decompose, pattern/idiom alignment)
- Preparatory refactoring before a feature ("make the change easy, then make the easy change")
- Legacy (brownfield) rescue: seam discovery + characterization tests, then restructuring
- Refactoring target selection and prioritization (smells + SATD + hotspot = churn x complexity)
- Auditing whether code is safe to refactor now (coverage breadth x mutation strength x flakiness)
When NOT to use
- Adding or completing behavior already requested by the user -> use the appropriate existing implementation skill; classify the scope in step 1 of the canonical workflow path first.
- Fixing a reported bug or failing behavior -> use
oma-debug(refactoring must not change behavior) - Security/performance/accessibility review or quality audit -> use
oma-qa - System design, module boundary decisions, ADRs, convention changes -> use
oma-architecture(a convention/pattern change is an architecture decision, not a local refactoring) - DB schema design or migration mechanics -> use
oma-db(this skill only plans the expand-contract sequence) - Commit splitting / staging mechanics -> use
oma-scm - Performance optimization as a goal -> out of scope by definition (tuning is a side effect, never the objective)
Expected inputs
target: file/module/path, smell report, SATD marker, or the feature request motivating preparatory refactoringverification: existing project test command(s); coverage/mutation tooling if already availableconstraints: coding guide / conventions, regulated-environment flags, merge-window concerns- Optional: prior metric reports, hotspot data, ADRs touching the target area
Expected outputs
- Refactored code as a sequence of atomic, refactor-only commits (no test changes mixed in)
- Safety-net additions when missing (characterization / golden-master tests) as separate commits
- Before/after report: metric delta (cyclomatic/cognitive complexity, size, coupling) + readability verdict
outputs:
- name: report
description: refactoring plan or before/after report
artifact: ".agents/results/refactor/*.md"
required: false
Standalone runs write plan / before-after reports under .agents/results/refactor/; orchestrated runs (via the refactor-engineer agent) write the result report at the path defined by the agent result contract (../_shared/runtime/result-contract.md).
Dependencies
resources/definition.md(invariant definition: 5 properties, boundaries, destination principle, naming roles, inline evidence)resources/measurement.md(4-layer measurement + git forensics commands)- Configured code-intelligence symbol/reference tools; existing project test runners for the target language
- Git history for churn/ownership/hotspot analysis
- Configured code intelligence for symbol/reference/pattern impact analysis; an available semantic rename engine for renames. Native inspection remains valid, but do not replace a semantic rename with blind text replacement
- Deterministic transformers: IDE refactoring actions, codemods (jscodeshift / OpenRewrite / ast-grep / comby)
- Metrics: use already installed complexity tools such as lizard / radon and existing project lint limits; installing or downloading additional tools requires authorization
- Test stack: use the repository's existing test runner and coverage commands (Jest, Vitest, unittest, pytest, XCTest, Swift Testing, cargo test, go test, or whatever the project already uses) and preserve their configuration. Add coverage or mutation instrumentation only when it is relevant to the task and available in the project; a runner migration is separate work
Structural Flow
Transitions
- If the safety net is missing or weak (low diff coverage, flaky, no assertions), write characterization / golden-master tests FIRST, committed separately, before touching production code.
- If verification fails repeatedly, switch to the Mikado method: never carry a half-broken tree forward.
- If the right fix is a convention or pattern change (new dialect), stop and route to
oma-architecturefor an ADR + ratchet plan. - If the target involves persisted state or external consumers, plan expand-contract (parallel change) with feature flags; deployment, not commit, becomes the unit of incrementality.
- If every consumer is inside the repository, migrate all callers and delete the old API in the same change; leave no compatibility shim.
- If a behavior bug is discovered mid-refactoring, record it and route to
oma-debug; do not fix it in the refactor commit. - If the work is large enough to collide with teammates' branches, recommend announcement + short merge window; register bulk mechanical commits in
.git-blame-ignore-revs.
Failure and recovery
| Failure | Recovery |
|---|---|
| Tests fail after a transformation | Mikado: record prerequisite, undo only this transformation's edits, preserve unrelated work, attack prerequisite first |
| No tests and code is untestable | Find a seam; apply only minimal mechanical changes to inject test access, then characterize |
| Tests are flaky | Fix or quarantine flaky tests before refactoring - an unreliable net is no net |
| Metric improves but readability worsens | Reject the transformation; readability is the success criterion, metrics are proxies |
| Scope keeps growing | Stop; report the boundary issue and split into a Mikado graph or route to architecture |
| Refactoring engine/codemod produces wrong output | Engines are not infallible - tests re-run is mandatory; fall back to manual atomic edits |
| A pure refactor has no implementation to restructure (missing or stub code) | Report the missing target rather than inventing one; missing or stub code does not itself authorize new behavior |
| A contract fact needed for correctness remains unknown after checking available task and project evidence | Ask for that specific fact under the shared execution policy and clarification protocol. Distinguish missing information from missing authorization: retain existing permission, pause only dependent work, and continue independent authorized work |
Exit
- Success: behavior verified unchanged, structure measurably improved, readability confirmed, refactor-only commits, follow-ups reported.
- Partial success: safety net built but restructuring deferred; or prerequisites mapped (Mikado graph) with explicit blockers.
- Handoff: already-requested implementation or bug fixing continues under its owning skill's completion criteria; changing skills is not a task failure.
- Blocked or failed: report the exact missing contract, verification path, or required decision and the dependent work it prevents, plus independent work completed. A pure refactor with no target reports that limit without inventing behavior.
Logical Operations
Canonical workflow path
- Classify the requested outcome against the actual target code, not the word "refactor" alone. Behavior-preserving restructuring stays here. Implementation or bug fixing already included in the request continues under the appropriate existing implementation skill or
oma-debug, within existing authorization and without renewed approval. For mixed requests, separate behavior changes from restructuring and apply this skill only to the latter (two hats: no feature or bug work mixed in). - For the refactoring scope, establish its motivation and diagnose the safety net: run coverage on the changed lines, check test determinism (flakiness), and measure mutation strength if measurable. Classify greenfield (safety net exists) vs brownfield (build the net first). When no verification path (tests, types, tooling) exists, the first deliverable is the safety net, not restructuring.
- If brownfield: find a seam, write characterization (golden-master or snapshot) tests for CURRENT behavior, and commit them separately.
- Read the target code via symbol tools and the coding guide for conventions (known, or explicitly absent). Collect metrics (complexity, size, coupling), git signals (churn, ownership), smells, and SATD; check any existing project size gate. For code you will delete or reshape, recover why it exists (
resources/definition.md§Provenance). - Select targets by hotspot rank (complexity x churn), not by smell aesthetics alone.
- Identify the destination form: the language idiom and codebase convention the result must match. Plan a sequence of named atomic transformations toward it, subtraction first (
resources/definition.md§Subtraction first), each independently verifiable and revertible; for stateful targets plan expand-contract. - Loop per transformation: apply ONE transformation, preferring deterministic engines (IDE rename, codemod, ast-grep) over freehand edits -> re-run existing tests UNCHANGED -> commit
refactor:only -> next transformation. This safety-net run is the skill's required verification: perform it when test execution is permitted; when the user or project instructions rule it out, stop before the next transformation and report the applied transformation as unverified.
On repeated failure (Mikado): record the broken prerequisite, undo only this transformation's edits,
preserve pre-existing edits and other contributors' concurrent work, recurse on the prerequisite first. - When one feature spans several files after a split, a short entry-file comment listing the related paths and their roles can serve as a map; keep it current.
- Finish: before/after metric delta + readability verdict (metric improvement alone is not success); list discovered-but-deferred work; never mix in behavior changes.
Resource scope and effects
Reads target source, tests, the coding guide, lint configs, and git history. Mutates production code (structure only) and adds tests in separate commits. Runs test runners, coverage/mutation tools, codemod engines, and git log analysis. May write reports under .agents/results/refactor/ and entries to .git-blame-ignore-revs. Never alters observable behavior, public contracts, or persisted data without an expand-contract plan.
Guardrails
- Behavior-preserving: the consumer contract (Hyrum-aware) is inviolable; tuning is a side effect, never a goal.
- Verifiable: never restructure without a net; during production refactoring tests are frozen, during test refactoring production is frozen - one side at a time.
- Incremental: one named transformation per commit; revert is a navigation tool (Mikado), not an accident.
- Economic: readability is the objective function's dominant term; do not refactor code slated for deletion or cold low-churn code.
- Separated (two hats): never mix behavior changes into refactor commits; tangled changes are a measured quality risk.
- Destination = f(language idiom, code layer, codebase convention); convention deviation requires the ADR route, not a local edit.
- Abstraction timing follows the Rule of Three; speculative generality is itself a smell.
- All metrics are proxies (Goodhart): a 499-line mechanical split, assertion-free coverage, or pattern-count gains are failures, not wins.
References
-
Local code tools:
../_shared/core/code-intelligence.md(code search/navigation) -
Authorization and contract ambiguity:
../_shared/core/execution-policy.mdand../_shared/core/clarification-protocol.md(when clarification or authorization is needed) -
Invariant definition (5 properties, boundaries, destination, naming roles, contexts, D&C, inline evidence):
resources/definition.md -
Measurement: 4 layers + git forensics commands:
resources/measurement.md -
Context loading:
../_shared/core/context-loading.md -
Quality principles:
../_shared/core/quality-principles.md -
Adjacent skills:
oma-debug(bugs),oma-qa(audits),oma-architecture(boundaries/ADR),oma-db(schema),oma-scm(commits)
Security
Reviews (0)
Related skills
Browse all →More from oh-my-agent
View source →More Build skills
Browse category →Frequently asked questions about oma-refactor
What does the oma-refactor skill do?
Restructure existing code while preserving observable behavior. Use for targeted technical debt or hotspot work with characterization tests. It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.
How do I install the oma-refactor skill?
Run `npx @skills-hub-ai/cli install oh-my-agent-agents-oma-refactor` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/oh-my-agent-agents-oma-refactor/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.
Which AI tools does oma-refactor work with?
oma-refactor runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.
Is the oma-refactor skill free?
Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.
How do I use oma-refactor after installing it?
In Claude Code, type `/oh-my-agent-agents-oma-refactor` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.
Can I share the oma-refactor skill with my team?
Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.