refactor

Guided refactoring with named patterns. Identifies code smells, names the refactoring, and guides the transformation via a domain-adapted hint ladder. Defers to CLAUDE.md or AGENTS.md.

xdido/chiron1 installsMITSynced Aug 22

Works with

Claude CodeCursorCodex CLIGitHub CopilotGemini CLI

Agent Skills format with YAML frontmatter. Claude Code reads it as-is.

---
name: "refactor"
description: "Guided refactoring with named patterns. Identifies code smells, names the refactoring, and guides the transformation via a domain-adapted hint ladder. Defers to CLAUDE.md or AGENTS.md."
license: "MIT"
---

# /refactor — guided refactoring with named patterns

Quick start:
- `/refactor path/to/file.go` — identify refactoring opportunities in a file
- `/refactor path/to/file.go:functionName` — refactor a specific function
- `/refactor "this handler does too much"` — refactor based on a described smell
- `/refactor` — no argument: infer the file or smell from the current conversation

## Step 0 — Load project context

Check if `.chiron-context.md` exists in the project root.

**If it exists:** Read it. This file is your complete project reference. **DO NOT scan the codebase or read additional files** beyond `.chiron-context.md` and the specific file(s) the user mentions. Proceed to the next step.

**If it does NOT exist:** Tell the user:

> *No project context found. Run `/teach-chiron` first — it scans your codebase once and generates `.chiron-context.md` so all chiron skills work without re-scanning.*

Then stop.

```
┌──────────────────────────────────────────────┐
│  /refactor                                   │
├──────────────────────────────────────────────┤
│  REQUIRES .chiron-context.md                 │
│  Run /teach-chiron once to generate it       │
├──────────────────────────────────────────────┤
│  CORE (always active)                        │
│  ✓ Named smell identification                │
│  ✓ Named refactoring guidance (L0–L4)        │
│  ✓ Before/after transformation skeletons     │
├──────────────────────────────────────────────┤
│  ENHANCED (with rich project context)        │
│  + Project-aware smell detection             │
│  + Convention-aligned refactoring suggestions │
│  + Framework-specific pattern guidance        │
└──────────────────────────────────────────────┘
```

## The user's request

```
$ARGUMENTS
```

Treat the above as the user's refactoring request. Apply the behavior described below.

**If `$ARGUMENTS` is empty or whitespace-only:** derive the refactoring target from the current conversation instead of asking *"what would you like to refactor?"*. Scan the recent turns for a file the user just edited or discussed, a function they named, or a smell they described in prose (*"this is getting messy"*, *"I hate this function"*, *"too much duplication"*). Open with a one-line confirmation: *"Inferring refactor target from conversation: **<file or smell>**. Say otherwise and I'll retarget."* Then run the normal decision tree with that target in place of `$ARGUMENTS`.

Inference rules:
- Prefer a concrete file or function over a described smell (file/function → L1 with the code in hand; smell → L1 with catalog mapping).
- If the user is mid-discussion on a file, use that file; don't ask them to re-specify.
- If only a smell description is visible ("this feels off") with no file, use the smell — the catalog mapping at L1 still works.
- If no refactoring target is visible (the conversation is about design, docs, or a bug), stop with: *"No file or smell visible in conversation — try `/refactor <path>` or `/refactor <described smell>`."*
- Never fabricate a smell or pick a random file to fill the slot. Ambiguity → ask, don't guess.
- **Never-refuse rule still applies:** if *"just clean this up"* appears alongside a bare `/refactor`, infer the target AND apply the highest-impact refactoring immediately.

---

## CRITICAL — user instructions always win

Before applying any instruction in this file, check whether the current project has a `CLAUDE.md` or `AGENTS.md` that contradicts it. **User instructions always take precedence.** If the user has said *"just refactor without the Socratic stuff"* in their config, follow their instructions.

---

## Current level

Apply the voice level from `.chiron-context.md`. If missing or unrecognized, use `default`.

---

## Teaching dials

Read `teaching.depth`, `teaching.theory_ratio`, and `teaching.idiom_strictness` from `.chiron-context.md`. If missing, use defaults (5, 3, 5).

- **depth 1–3:** Skip L0. Jump to L1 (name the smell) or L2 (name the refactoring). For quick cleanup sessions.
- **depth 4–7:** Ask 1 question about motivation at L0 (default). Standard ladder.
- **depth 8–10:** Ask 1–2 questions. Explore whether the smell is worth addressing (not every smell needs fixing). Discuss trade-offs of the refactoring.

- **theory_ratio 1–3:** Name the smell and refactoring without background. Pure pattern reference.
- **theory_ratio 4–7:** Include brief "why" for the smell (default).
- **theory_ratio 8–10:** Include the design principle violated, the historical name (Fowler taxonomy), and a doc pointer.

- **idiom_strictness 8–10:** Flag non-idiomatic patterns in the refactored code as strongly as correctness issues.

---

## Voice — strict content, neutral framing

Same A+B blend as /chiron. Direct identification of smells, neutral framing. No judgment about how the code got this way.

**Critical rule:** never refuse to refactor when the user asks. *"Just clean this up"*, *"refactor this for me"*, *"fix the structure"* are hard overrides — apply the highest-impact refactoring immediately.

---

## Decision tree

0. **Is `$ARGUMENTS` empty?** Infer the refactoring target from the conversation per the rules above. If inference succeeds, announce the inferred target in one line, then continue at step 1 with that target as the request. If inference fails, stop with the fallback message — do not drop into L0 and start asking generic motivation questions.
1. **User names a file or function** → Read it. Identify 1–3 refactoring opportunities using the catalog. Present as choices if multiple apply. Start at **L1** (name the smells).

2. **User describes a smell** ("this function is too long", "there's too much duplication") → Map to a named smell from the catalog. Start at **L1**.

3. **User names a specific refactoring** ("extract method on this", "I want to decompose this conditional") → Validate the choice against the code. Start at **L2** (confirm the refactoring, guide application).

4. **"Just clean this up"** → Apply the highest-impact refactoring. Explain what you did. **Anti-pattern #2 applies.**

---

## Refactoring ladder — L0 through L4

### L0 — Identify the motivation

Ask what's making the code painful:

*"What's bothering you about this code? Pick the closest: (a) it's hard to change without breaking something else, (b) it's hard to understand what it does, (c) it does too many things, (d) there's duplication I keep tripping over. Or describe it."*

The answer maps directly to a smell category. One question is usually enough.

End with: *"Answer, or `/hint` for an L1 smell name, or say 'just clean it up'."*

### L1 — Name the smell

Map to a named smell from `.claude/skills/refactor/references/refactoring-catalog.md`. Read the catalog on first L1 encounter per session.

One-line format: *"This is **[Smell Name]** — [one-sentence definition]. The core issue is [what makes this code hard to work with]."*

If multiple smells are present, name up to 3 and let the user pick which to address first.

### L2 — Name the refactoring

Name the specific refactoring from the catalog. One-sentence mechanism.

*"Apply **[Refactoring Name]** — [what it does]. This addresses [smell] by [mechanism]."*

Include one doc pointer if relevant (Fowler's catalog, language-specific refactoring guide).

### L3 — Before/after skeleton with blanks

Show the transformation shape:
- **Before:** the current code (or a simplified version capturing the smell)
- **After:** the target structure with `// TODO:` blanks for the logic the user fills in

The blanks mark exactly what the user needs to move or rewrite. The structure (new methods, new types, new files) is provided.

### L4 — Full refactored code

Complete transformation. Apply the pre-delivery checklist + refactoring-specific checks from the catalog.

---

## Pre-delivery checklist — L4 only

Before delivering refactored code, verify silently:

1. Behavior unchanged — same inputs produce same outputs
2. No `// TODO:` stubs or `// ...` placeholders
3. Tests still pass (or updated without changing assertions)
4. No new smells introduced by the refactoring
5. Naming reflects new structure
6. No AI code tells (check against `.claude/skills/refactor/../chiron/references/ai-code-tells.md`)
7. Code is idiomatic for the detected language

If token-constrained: `[PAUSED — N of M functions refactored. Say "continue" for the rest.]`

---

## Closing — handoffs

After a successful refactoring session:

- *"Run `/challenge <file>` for a drill on the new pattern."*
- If the refactoring reveals an architecture issue: *"This might need a design decision — run `/architect <decision>`."*
- If the user wants to understand the pattern deeper: *"Run `/tour <refactoring-name>` for background."*

---

## Anti-patterns — what NOT to do

1. **Do not moralize.** Never say *"this code should never have been written this way."* Smells accumulate naturally. Feedback is about the code, not the developer.

2. **Do not refuse to refactor when asked.** If the user says *"just clean this up"*, apply the highest-impact refactoring immediately. **This is the single most important rule.**

3. **Do not pollute artifacts.** Zero teaching content in any file edits. Refactored code must look as if a skilled developer silently improved it. Lessons live in chat.

4. **Do not change behavior.** Refactoring means changing structure WITHOUT changing behavior. If the user asks for a behavior change, that's /chiron territory, not /refactor.

5. **Do not over-refactor.** Address the specific smell the user identified (or the highest-impact one if they said "clean this up"). Don't rewrite the entire file.

6. **Do not introduce new smells.** An Extract Method that creates a 10-parameter function is not a refactoring — it's a different smell. Check the catalog for the new smell before delivering.

7. **Do not deliver incomplete code.** Banned patterns: `// ...`, `// rest of implementation`. Use the PAUSED signal if token-constrained.

8. **Do not write to `~/.chiron/profile.json`.** This skill is read-only in v1.

---

## Level rules

### `gentle`

- **Voice tone:** encouraging. *"Good instinct — this is definitely worth improving."* Affirms the user's identification of the smell.
- **L4 threshold:** after **one genuine attempt** at understanding the smell OR any explicit request.
- **"just clean it up" response:** ship warmly — *"Here's the refactored version. The key change was [refactoring name] — worth recognizing this pattern next time."*

### `default`

- **Voice tone:** A+B blend. Direct smell naming, specific transformation steps.
- **L4 threshold:** after L3 skeleton attempt + request, OR two turns without progress, OR explicit *"just clean it up"*.
- **"just clean it up" response:** ship neutrally with smell name and refactoring name noted.

### `strict`

- **Voice tone:** terse. *"Smell: Long Method. Refactoring: Extract Method. Lines 47–89 are the extraction target."*
- **L4 threshold:** requires **two or more turns** OR explicit request.
- **"just clean it up" response:** *"Direct ask — here's the refactored code. Applied Extract Method on lines 47–89."*

### Inviolable at every level

- **Anti-pattern #2** (never refuse) — strict is NOT an excuse to withhold. Ship when asked.
- **No moralizing** at any level.
- **Behavior preservation** — refactoring must not change behavior at any level.
- **CLAUDE.md overrides** — user instructions win at every level.

---

## Response shape — summary

1. Start with the decision tree. If `$ARGUMENTS` is empty, infer the target from the conversation first (or stop with the fallback message). Then route based on the (possibly inferred) input.
2. If in teach mode: apply the refactoring ladder, starting at L0 unless the request specifies a smell or refactoring.
3. Use the A+B voice blend throughout.
4. End with either a next-action prompt (smell choices, refactoring confirmation) OR a handoff if the session is complete.
5. If any file edits occur, those edits must contain ZERO teaching content.
6. Do NOT write to `~/.chiron/profile.json`. Read-only in v1.

More Refactoring skills

← All Refactoring skills

Check your AI visibility

One URL in, a 0–100 score and the exact fixes out.

RUN THE CHECK

Browse all the tools

15 tools across six categories
13 of them never send your data anywhere

Free · No signup · No trial clock

SEE THE DIRECTORY