Oma refactor

From gracefullight/krds · View interactive version

Oma refactor is an AI agent skill from the GitHub repository gracefullight/krds. Restructure existing code while preserving observable behavior. Use for targeted technical debt or hotspot work with characterization tests.

How to use this skill

Skill file

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

When to use

When NOT to use

Expected inputs

Expected outputs

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 .agents/results/result-refactor[-{sessionId}].md per the agent execution protocol.

Dependencies

Control-flow features

Structural Flow

Entry

  1. 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.
  2. For the refactoring scope, establish its motivation and diagnose the safety net: coverage of changed lines, test determinism (flakiness), mutation strength if measurable.
  3. Identify the destination form: the language idiom and codebase convention the result must match.

Missing or stub code does not itself authorize new behavior. A pure refactor with

no implementation to restructure reports the missing target rather than inventing

one. If 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.

Scenes

  1. PREPARE: Within the refactoring scope, classify greenfield (safety net exists) vs brownfield (build net first); check size gates and hotspot rank; confirm two-hats scope (no feature/bug work mixed in).
  2. ACQUIRE: Read target code via symbol tools; collect metrics (complexity, size, coupling) and git signals (churn, ownership); read the coding guide for conventions.
  3. REASON: Decompose the goal into a sequence of named atomic transformations; for stateful targets plan expand-contract; verify each step is independently verifiable and revertible.
  4. ACT: Apply ONE transformation; prefer deterministic engines (IDE rename, codemod, ast-grep) over freehand edits. When one feature spans several files after a split, consider G-5's optional header map if it helps navigation.
  5. VERIFY: Re-run existing tests unchanged. Pass -> commit (refactor-only) -> next transformation. Repeated failure -> Mikado: record the broken prerequisite, undo only this transformation's edits, recurse on the prerequisite first. Preserve pre-existing edits and other contributors' work.
  6. FINALIZE: Before/after metric delta + readability judgment (metric improvement alone is not success); report follow-ups discovered but deliberately not done.

Transitions

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 |

Exit

Logical Operations

Actions

| Action | SSL primitive | Evidence |

|--------|---------------|----------|

| Diagnose safety net | VALIDATE | Coverage/flakiness/mutation state of target scope |

| Collect signals | READ | Metrics, git churn/ownership, smells, SATD |

| Rank targets | COMPARE | Hotspot = complexity x churn |

| Plan atomic sequence | INFER | Named transformations, Mikado graph |

| Write characterization tests | WRITE | Golden-master/snapshot tests (separate commit) |

| Apply transformation | WRITE / CALL_TOOL | One atomic refactor, engine-first |

| Verify preservation | VALIDATE | Existing tests re-run unchanged |

| Commit separately | UPDATE_STATE | refactor:-typed commits only |

| Report delta | NOTIFY | Metric + readability before/after |

Tools and instruments

Canonical workflow path

  1. Apply Entry's scope routing first. For the remaining refactoring scope, run coverage and check test determinism; classify green/brownfield.
  2. If brownfield: find a seam, write characterization (golden-master) tests for CURRENT behavior, commit.
  3. Select targets by hotspot rank (complexity x churn), not by smell aesthetics alone.
  4. Plan a sequence of named atomic transformations toward the language-idiomatic, convention-conforming form.
  5. Loop per transformation: apply (engine-first) -> re-run tests UNCHANGED -> commit refactor: only.

On repeated failure: record prerequisite, undo only this transformation's edits,

preserve pre-existing and concurrent work, recurse (Mikado).

  1. Finish: metric delta + readability verdict; list discovered-but-deferred work; never mix in behavior changes.

Resource scope

| Scope | Resource target |

|-------|-----------------|

| CODEBASE | Target source, tests, coding guide, lint configs |

| LOCAL_FS | Reports under .agents/results/refactor/, .git-blame-ignore-revs |

| PROCESS | Test runners, coverage/mutation tools, codemod engines, git log analysis |

| MEMORY | Mikado prerequisite graph, deferred follow-ups, metric baselines |

Preconditions

Effects and side effects

Guardrails

  1. Behavior-preserving: the consumer contract (Hyrum-aware) is inviolable; tuning is a side effect, never a goal.
  2. Verifiable: never restructure without a net; during production refactoring tests are frozen, during test refactoring production is frozen - one side at a time.
  3. Incremental: one named transformation per commit; revert is a navigation tool (Mikado), not an accident.
  4. Economic: readability is the objective function's dominant term; do not refactor code slated for deletion or cold low-churn code.
  5. Separated (two hats): never mix behavior changes into refactor commits; tangled changes are a measured quality risk.
  6. Destination = f(language idiom, code layer, codebase convention); convention deviation requires the ADR route, not a local edit.
  7. Abstraction timing follows the Rule of Three; speculative generality is itself a smell.
  8. All metrics are proxies (Goodhart): a 499-line mechanical split, assertion-free coverage, or pattern-count gains are failures, not wins.

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.