Code refactoring

From akillness/jeo-skills · View interactive version

Code refactoring is an AI agent skill from the GitHub repository akillness/jeo-skills. Turn a cleanup packet into one behavior-preserving refactor brief. Use when simplifying a messy function/component/service, freezing behavior before touching fragile legacy code, splitting a cleanup-heavy diff into reviewable slices, or planning a repeated migration / codemod without changing intend

How to use this skill

Skill file

Code Refactoring

Use this skill when the job is to improve structure without changing intended behavior.

The center of the skill should stay small and repeatable:

  1. identify the cleanup packet you actually have,
  2. choose one refactor mode,
  3. make the behavior guardrail explicit,
  4. stage the work in reviewable slices,
  5. verify and route remaining work honestly.

Read these support docs before handling unfamiliar cleanup work:

When to use this skill

When not to use this skill

Instructions

Step 1: Start from the cleanup packet

Use references/intake-packets-and-route-outs.md.

Choose the packet the user already has:

Output the intake briefly:

## Cleanup Packet
- Current artifact:
- Why it is enough (or not enough):
- Missing evidence to collect next:

Rule: do not force a giant refactor plan when the immediate need is only search, diagnosis, review, or performance evidence.

Step 2: Choose one primary refactor mode

Pick exactly one primary mode from references/refactor-modes.md.

Primary modes:

Quick selector:

| Signal | Mode |

|---|---|

| One file or narrow module, clear intent, at least one fast guardrail exists | local-safe-refactor |

| Fragile legacy area, hidden invariants, weak test trust | behavior-freeze-first |

| Same API or structure repeats across many files | repetitive-migration-codemod |

| Cleanup is mixed into a risky review diff and needs smaller slices | diff-shaping-cleanup |

Rule: one primary mode, optional secondary note. Do not mix every cleanup tactic into one answer.

Step 3: Freeze the behavior guardrail

Before broad edits, decide how you will prove intent stayed the same.

Guardrail sources can include:

Minimum rule:

If the user mainly needs help designing the entire validation program, route to testing-strategies.

Step 4: Build the smallest credible cleanup plan

Keep the plan reviewable.

Preferred slices:

  1. rename / move / extract work
  2. duplicated or dead-code cleanup
  3. mechanical migration or codemod rollout
  4. semantic follow-up only if still needed
  5. verification + handoff

For each slice, capture:

Rules:

Step 5: Use the right mode packet

Use the matching packet in references/intake-packets-and-route-outs.md:

Good moves by mode:

Step 6: Verify and route remaining work

Do not stop at “looks cleaner.”

Verification brief:

## Refactor Brief
- Primary mode:
- Behavior to preserve:
- Guardrail used:
- Smallest planned slices:
- Risks still open:
- Recommended next move:

Always call out:

Output format

## Cleanup Packet
- Current artifact:
- Primary mode:
- Why this mode fits:

## Behavior Guardrail
- Intended behavior to preserve:
- Evidence available:
- Missing evidence:

## Planned slices
1. ...
2. ...
3. ...

## Verification
- Fast checks:
- Higher-risk checks:

## Route-outs
- Use `debugging` for:
- Use `code-review` for:
- Use `testing-strategies` for:
- Use `performance-optimization` for:
- Use `codebase-search` for:

Examples

Example 1: Oversized service handler

Input: "Refactor this 180-line checkout handler into something readable without changing behavior."

Good response shape:

Example 2: Fragile legacy module

Input: "This reporting service is impossible to maintain, but we barely trust the tests. Help me refactor it safely."

Good response shape:

Example 3: Repeated API migration

Input: "We need to replace a deprecated client API across 220 TypeScript files before the framework upgrade."

Good response shape:

Example 4: Search-first route-out

Input: "Before we refactor anything, find every call site and wrapper around this old helper so we can see the blast radius."

Good response shape:

Best practices

  1. Start from the packet the user actually has, not an idealized cleanup workflow.
  2. Pick one primary mode before proposing actions.
  3. Make behavior preservation explicit; do not assume it.
  4. Prefer small, reviewable slices over one giant cleanup diff.
  5. Use codemods or structural rewrites only when repetition justifies the setup cost.
  6. Keep diagnosis, review judgment, test-policy design, performance tuning, and symbol inventory routed to neighboring skills instead of absorbing them.
  7. Preserve evidence of what was verified and what still remains risky.

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.