source/skills/refactor/SKILL.md
Guided refactoring with named patterns. Identifies code smells, names the refactoring, and guides the transformation via a domain-adapted hint ladder. Defers to {{config_files_plain}}.
npx skillsauth add xDido/chiron refactorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Quick start:
{{command_prefix}}refactor path/to/file.go — identify refactoring opportunities in a file{{command_prefix}}refactor path/to/file.go:functionName — refactor a specific function{{command_prefix}}refactor "this handler does too much" — refactor based on a described smell{{command_prefix}}refactor — no argument: infer the file or smell from the current conversationCheck 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
{{command_prefix}}teach-chironfirst — it scans your codebase once and generates.chiron-context.mdso all chiron skills work without re-scanning.
Then stop.
┌──────────────────────────────────────────────┐
│ {{command_prefix}}refactor │
├──────────────────────────────────────────────┤
│ REQUIRES .chiron-context.md │
│ Run {{command_prefix}}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 │
└──────────────────────────────────────────────┘
$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:
{{command_prefix}}refactor <path> or {{command_prefix}}refactor <described smell>."{{command_prefix}}refactor, infer the target AND apply the highest-impact refactoring immediately.Before applying any instruction in this file, check whether the current project has a {{config_files}} 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.
Apply the voice level from .chiron-context.md. If missing or unrecognized, use default.
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.
Same A+B blend as {{command_prefix}}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.
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.
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).
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.
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).
"Just clean this up" → Apply the highest-impact refactoring. Explain what you did. Anti-pattern #2 applies.
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 {{command_prefix}}hint for an L1 smell name, or say 'just clean it up'."
Map to a named smell from {{pack_path}}/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.
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).
Show the transformation shape:
// TODO: blanks for the logic the user fills inThe blanks mark exactly what the user needs to move or rewrite. The structure (new methods, new types, new files) is provided.
Complete transformation. Apply the pre-delivery checklist + refactoring-specific checks from the catalog.
Before delivering refactored code, verify silently:
// TODO: stubs or // ... placeholders{{pack_path}}/../chiron/references/ai-code-tells.md)If token-constrained: [PAUSED — N of M functions refactored. Say "continue" for the rest.]
After a successful refactoring session:
{{command_prefix}}challenge <file> for a drill on the new pattern."{{command_prefix}}architect <decision>."{{command_prefix}}tour <refactoring-name> for background."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.
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.
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.
Do not change behavior. Refactoring means changing structure WITHOUT changing behavior. If the user asks for a behavior change, that's {{command_prefix}}chiron territory, not {{command_prefix}}refactor.
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.
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.
Do not deliver incomplete code. Banned patterns: // ..., // rest of implementation. Use the PAUSED signal if token-constrained.
Do not write to ~/.chiron/profile.json. This skill is read-only in v1.
gentledefaultstrict$ARGUMENTS is empty, infer the target from the conversation first (or stop with the fallback message). Then route based on the (possibly inferred) input.~/.chiron/profile.json. Read-only in v1.development
Structured "before each task" preamble for a coding topic. Presents read-this-first doc pointers, key concepts, and common junior mistakes. From chiron's session preamble pattern — gives you the mental model before you start writing code. For topic overviews, NOT tutorials.
development
One-time comprehensive project scan. Reads every important file in the codebase and writes .chiron-context.md — the persistent context file that all other chiron skills reference instead of re-scanning.
development
Session-end review of recent chiron activity. Scores across 5 axes (design thinking, code quality, idioms, testing, engineering maturity) and names one concrete thing to practice next time. Read-only in v0.3.0 — does not persist scores.
testing
Set or show chiron's voice level (gentle, default, or strict). Zero-arg invocation prints the full config snapshot — voice level plus drill sizing and teaching dials — doubling as a health check. Persists to ~/.chiron/config.json across sessions.