agentleFS
heliohqheliohq/ship/skills/refactor/SKILL.md

refactor

Skill93 starsChanged 5 months agoMITView on GitHub

Improve existing code without changing behavior: scan smells, simplify, dedupe, reuse utilities, and verify after edits. Use for refactor, cleanup, simplify, reduce duplication, extract method, dead code, or code smells. No PR.

111 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use modern-go, to-design and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • jidechaoSkill

    modern-go

    Modernize Go code by applying version-appropriate idioms and APIs (gofix-style transformations). Scans go.mod for the Go version, then transforms Go source files to use modern patterns—from Go 1.0 through 1.26+. Use when the user says "现代化","现代Go语言", "地道的", "idiomatic", "modernize", "modern-go", "update Go code", "gofix", or wants to upgrade Go idioms.

    Also in 3 projects that use this

  • jidechaoSkill

    to-design

    Generate a design document (design proposal) from a PRD, in the style of Go's official design proposals — Abstract / Background / Design / Rationale / Compatibility / Implementation, heavy on the 'why' and tradeoffs. Triggers on: to-design, prd-to-design, prd转设计文档, 生成设计文档, 写设计文档, design doc, design proposal, 设计提案, 技术设计文档.

    Also in 3 projects that use this

  • chidekinaSkill

    test-gen

    Generate test cases from type constraints and specs. Creates boundary tests, edge cases, and fixtures automatically. Pact-lang inspired. Triggers on: generate tests, test from spec, create fixtures, boundary tests.

    Also in 5 projects that use this

  • pvnarpSkill

    incident

    Guides incident response and post-mortem analysis. Covers triage, mitigation, root cause analysis, and prevention. Use when responding to active incidents, writing post-mortems, or improving incident processes.

    Also in 6 projects that use this

  • platformplatformSkill

    db-query

    Query the local Postgres database of the active Aspire worktree via psql.

    Also in 3 projects that use this

  • JetBrainsSkill

    fix-bug

    End-to-end bug fix workflow for the Exposed project. Accepts a GitHub issue (#NUMBER), YouTrack issue (EXPOSED-NUMBER), or YouTrack URL (https://youtrack.jetbrains.com/issue/EXPOSED-NUMBER). Fetches the issue, creates a failing reproducer test, commits on a new branch, implements the fix, validates, and opens a PR. Use this skill whenever the user wants to fix a bug from an issue tracker, mentions a EXPOSED issue number, or references a GitHub issue to fix.

    Also in 6 projects that use this

  • +2 more from jidechao

The file

We split this skill into the rules it gives an agent

11 rules. 0 are common, 9 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 94 edited versions we read keep these.

  • 9

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Read the target (file, directory, or codebase as indicated by user).

    line 59
  2. Distinctive:

    Search the codebase for existing utilities and helpers that could replace newly written code.

    line 78
  3. Distinctive:

    Wait for all four agents. Aggregate findings into a single list, then

    line 90
  4. Distinctive:

    deduplicate: if two agents flagged the same code location for overlapping reasons, keep the finding from the lens that owns it per the …

    line 91
  5. Very specific:

    Verify command for this repo (test/typecheck/lint)

    line 136
  6. Distinctive:

    After each batch: run verify.

    line 141
  7. Distinctive:

    After all smells: run full verify.

    line 143
  8. Very specific:

    Read references/structural-card.md for the template (45-60 lines).

    line 150
  9. Distinctive:

    If no test coverage for the code being changed: write characterization tests first.

    line 156
  10. Distinctive:

    Run tests after each step.

    line 159
  11. Distinctive:

    After all changes: run full verify.

    line 162

“Common” is measured: kept by at least 6 in 10 of the 94 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Ship: Refactor

You are a staff engineer who makes code better. Not later. Now.

Users say "refactor this" and expect fewer lines, less duplication, clearer logic, better structure. They don't want a document — they want the code to improve. Diagnose, fix, verify. In that order.

Principal Contradiction

The code's current structure vs the change patterns it actually faces.

Code that fit its original change pattern becomes a liability when the pattern shifts — functions grow, logic duplicates, modules accrete concerns. Resolve by matching technique to smell: simplify, extract, consolidate, delete.

Core Principle

MAKE THE CODE BETTER, NOT JUST DIFFERENT.
SIMPLIFY FIRST. RESTRUCTURE ONLY WHEN NEEDED.
VERIFY AFTER EVERY CHANGE.

Red Flag

Never:

  • Change external behavior — same inputs must produce same outputs, status codes, return shapes, validation rules. Most important constraint.
  • Rewrite a function's internal logic — extract, rename, simplify conditionals, add guard clauses are fine, but the function must produce identical output. "Improving" logic (changing format, tightening validation, renaming return fields) is a behavior change.
  • Diagnose without reading the code — every smell must cite file:line
  • Skip verification ("tests are probably fine")
  • Force a change after verification fails twice — revert and skip it
  • Claim "no tests" without checking for test files
  • Refactor and add features in the same session
  • Move code between files without improving anything — reorganization alone is not refactoring. (Exception: replacing new code with an existing utility IS an improvement — the Reuse lens handles this.)
  • Disguise architectural redesign as refactoring
  • Skip running existing tests before AND after changes to establish baseline

Phase 1: Scan

Read the target (file, directory, or codebase as indicated by user). Determine the diff or file set to review.

Small target shortcut: single file under ~200 lines — scan all four lenses yourself in one pass (no agent dispatch; round-trip overhead outweighs parallelism). Same smell catalog. The Reuse lens still searches the broader codebase, not just the target.

Standard scan (multiple files, directories, or codebase):

Launch four review agents in parallel using the Agent tool — send all four in a single message. Pass each agent the target files/diff so each has full context. Each agent scans through one lens as defined in references/smell-catalog.md:

Agent 1: Structure Review

Scan the Surgical + Structural sections of the smell catalog.

Agent 2: Reuse Review

Search the codebase for existing utilities and helpers that could replace newly written code. Flag any new function that duplicates existing functionality. Flag inline logic that could use an existing utility.

Agent 3: Quality Review

Scan the Quality section of the smell catalog.

Agent 4: Efficiency Review

Scan the Efficiency section of the smell catalog.

Deduplication

Wait for all four agents. Aggregate findings into a single list, then deduplicate: if two agents flagged the same code location for overlapping reasons, keep the finding from the lens that owns it per the smell catalog's ownership notes. Drop the duplicate.

For each finding, record: lens (structure/reuse/quality/efficiency), smell name, file:line, severity (how much it hurts the next change or the runtime).

Phase 2: Classify

Decide the approach based on risk, not file count or lens:

SignalClassificationWhy
Findings are within-file, tests exist, changes are localQuickLow risk — fix directly, verify as you go
Cross-file dependencies change, no test coverage, large blast radius, or user says "refactor this module/codebase"PlannedHigh risk — write an execution card so user can review before you start
Not a code smell (algorithmic problem, runtime bug, feature request)RedirectWrong tool — suggest /ship:dev or /ship:auto

Lens-specific classification guidance (classify determines quick vs planned path — NOT execution order within a path. Execution order is always structure → reuse → quality → efficiency regardless of classification):

  • Structure: surgical smells → quick; structural smells → planned (as before)
  • Reuse: replacing code with existing utility → quick (it's a deletion, low risk even if cross-file)
  • Quality: almost always quick — these are local, low-risk fixes
  • Efficiency: quick if the fix is local (add projection, hoist a resource); planned if it changes call patterns across files (batching N+1 across a call chain)

Output: [Refactor] Scope: <files>. Classification: <quick|planned|redirect>. Findings: <N> (structure: <n>, reuse: <n>, quality: <n>, efficiency: <n>).

Phase 3: Execute

Execution order across lenses

Fix in this order — each leaves the code better for the next:

  1. Structure — changes code shape, do first to avoid rework.
  2. Reuse — duplication is now clear vs what was tangled.
  3. Quality — polish (stringly-typed, comments, naming).
  4. Efficiency — last; structural changes may already fix some.

Within each category, order smells simplest first.

Quick path

Low-risk findings with existing test coverage. No spec file. Direct edits.

  1. Form micro-plan (in memory):
    • Findings grouped by lens, ordered per execution order above
    • Verify command for this repo (test/typecheck/lint)
    • Abort rule: revert + skip if verify fails twice on same smell
  2. Fix one smell family at a time. Apply the technique from references/smell-catalog.md.
  3. After each batch: run verify. If fail: revert, skip to next smell.
  4. After all smells: run full verify. Report results.

Planned path

High-risk changes. Write an execution card first, get alignment, then execute.

  1. Write execution card:
    • Read references/structural-card.md for the template (45-60 lines).
    • For codebase-level work, read references/rescue-playbook.md for the full 8-step process.
    • Include findings from ALL lenses in the Evidence section, grouped by lens.
    • In /ship:auto mode: save to .ship/tasks/<task_id>/refactor/spec.md and proceed.
    • In standalone mode: save to .ship/refactor-card.md (no task_id needed) and show the card to the user via AskUserQuestion before executing.
  2. If no test coverage for the code being changed: write characterization tests first.
  3. Execute in order: Structure → Reuse → Quality → Efficiency. Run tests after each step. If tests fail twice on the same step: revert to last passing state, report what failed.
  4. After all changes: run full verify. Report results.

Execution Handoff

Output the report card — read the format from ../shared/report-card.md (resolved against this skill's base directory, not the working directory):

## [Refactor] Report Card

| Field | Value |
|-------|-------|
| Status | <DONE / BLOCKED> |
| Summary | <N> smells fixed across <L> lenses, <M> lines saved |

### Metrics
| Metric | Value |
|--------|-------|
| Structure fixes | <N> (extracted: <n>, consolidated: <n>, dead code: <n> lines) |
| Reuse fixes | <N> (replaced with existing utility) |
| Quality fixes | <N> (strings→constants: <n>, comments removed: <n>, naming: <n>, other: <n>) |
| Efficiency fixes | <N> (batched: <n>, hoisted: <n>, projected: <n>, other: <n>) |
| Lines before/after | <N> → <M> |
| Files touched | <N> |
| Tests | <passed / failed / none> |
| Deferred | <smells outside scope> |

### Artifacts
| File | Purpose |
|------|---------|
| .ship/tasks/<task_id>/refactor/spec.md | Execution card (planned path only) |

### Next Steps
1. **Ship** — /ship:handoff to create the PR (workflow continues here after refactor)
2. **Review** — /ship:review to verify no behavior changed (recommended for large refactors)
3. **Continue** — /ship:refactor on remaining deferred smells

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.