Refactor
Perform structured refactoring operations guided by user intent. Supports common refactoring patterns across any language.
Instructions
Perform the refactoring described in $ARGUMENTS. If no specific operation is given, analyze the target code and suggest the most impactful refactoring.
Step 1: Understand the target
Before making any changes, gather context:
- Read the target code — the file, module, or symbol the user wants to refactor
- Identify the refactoring type from
$ARGUMENTS (or infer it):
- extract — Pull code out into a new module/file/function/class
- split — Break a large file into multiple focused files
- inline — Remove an unnecessary abstraction by inlining its contents
- rename — Rename a symbol across the entire codebase
- simplify — Reduce complexity (flatten nesting, remove dead branches, simplify control flow)
- decouple — Reduce coupling between modules (extract interfaces, inject dependencies)
- reorganize — Move files/modules to a better location in the project structure
- Map the blast radius — find all files that import, reference, or depend on the target code
Step 2: Plan the refactoring
Present a brief plan to the user before executing:
## Refactoring Plan
**Type:** [extract | split | inline | rename | simplify | decouple | reorganize]
**Target:** [file or symbol being refactored]
### Changes
- [ ] [Description of each change, in order]
### Files affected
- [List every file that will be modified]
### Risks
- [Any behavioral changes, import path changes, or public API impacts]
Wait for user confirmation before proceeding. If the refactoring is trivial (single-file, no public API change), proceed directly.
Step 3: Execute the refactoring
Apply changes methodically:
- Make structural changes first — create new files, move code
- Update all references — imports, re-exports, barrel files, config entries
- Clean up the source — remove extracted code from the original location, remove unused imports
- Preserve behavior — do not change logic, types, or public interfaces unless explicitly asked
Guidelines per refactoring type
Extract:
- Give the new module a clear, descriptive name
- Define a minimal public interface — only export what the original callers need
- Move related types, constants, and helpers together with the extracted code
- Update barrel/index files if the project uses them
Split:
- Identify natural seams — group by feature, responsibility, or domain concept
- Each resulting file should have one clear responsibility
- Create an index/barrel file if the original was imported by many consumers
- Aim for files that are scannable in a minute or two
Inline:
- Verify the abstraction is used in only a few places (or adds no value despite wide use)
- Replace each call site with the inlined implementation
- Remove the now-empty module and clean up imports
- Simplify the inlined code in context if it was over-generalized
Rename:
- Use the Agent tool (subagent_type: Explore) to find ALL references across the codebase — source code, tests, config, docs, comments, strings
- Apply the rename in all locations
- Check for string-based references (e.g., dynamic imports, reflection, serialization keys) that grep might miss — flag these to the user
Simplify:
- Flatten deeply nested conditionals (early returns, guard clauses)
- Replace complex logic with clearer alternatives
- Remove unnecessary abstractions or indirection
- Reduce function length by extracting well-named helpers only if they represent distinct concepts
Decouple:
- Identify the coupling (direct imports, shared mutable state, circular dependencies)
- Introduce interfaces or dependency injection where appropriate
- Avoid over-engineering — only add abstraction if it reduces coupling meaningfully
Reorganize:
- Move files to their new locations
- Update ALL import paths across the codebase
- Update any path references in config files, build scripts, or documentation
Step 4: Verify
After applying changes:
- Search for broken references — any remaining imports of old paths or old symbol names
- Check for circular dependencies introduced by the refactoring
- List all files modified for the user to review
- If tests exist, suggest running them to verify behavior is preserved
1---2name: refactor3description: Perform structured refactoring operations: extract module, split file, inline abstraction, rename symbol across codebase, simplify complex code, or reduce coupling. Use when the user wants to refactor code, extract a module, split a large file, or reorganize code structure.4---56# Refactor78Perform structured refactoring operations guided by user intent. Supports common refactoring patterns across any language.910## Instructions1112Perform the refactoring described in `$ARGUMENTS`. If no specific operation is given, analyze the target code and suggest the most impactful refactoring.1314### Step 1: Understand the target1516Before making any changes, gather context:17181. **Read the target code** — the file, module, or symbol the user wants to refactor192. **Identify the refactoring type** from `$ARGUMENTS` (or infer it):20 - **extract** — Pull code out into a new module/file/function/class21 - **split** — Break a large file into multiple focused files22 - **inline** — Remove an unnecessary abstraction by inlining its contents23 - **rename** — Rename a symbol across the entire codebase24 - **simplify** — Reduce complexity (flatten nesting, remove dead branches, simplify control flow)25 - **decouple** — Reduce coupling between modules (extract interfaces, inject dependencies)26 - **reorganize** — Move files/modules to a better location in the project structure273. **Map the blast radius** — find all files that import, reference, or depend on the target code2829### Step 2: Plan the refactoring3031Present a brief plan to the user before executing:3233```markdown34## Refactoring Plan3536**Type:** [extract | split | inline | rename | simplify | decouple | reorganize]37**Target:** [file or symbol being refactored]3839### Changes40- [ ] [Description of each change, in order]4142### Files affected43- [List every file that will be modified]4445### Risks46- [Any behavioral changes, import path changes, or public API impacts]47```4849Wait for user confirmation before proceeding. If the refactoring is trivial (single-file, no public API change), proceed directly.5051### Step 3: Execute the refactoring5253Apply changes methodically:54551. **Make structural changes first** — create new files, move code562. **Update all references** — imports, re-exports, barrel files, config entries573. **Clean up the source** — remove extracted code from the original location, remove unused imports584. **Preserve behavior** — do not change logic, types, or public interfaces unless explicitly asked5960#### Guidelines per refactoring type6162**Extract:**63- Give the new module a clear, descriptive name64- Define a minimal public interface — only export what the original callers need65- Move related types, constants, and helpers together with the extracted code66- Update barrel/index files if the project uses them6768**Split:**69- Identify natural seams — group by feature, responsibility, or domain concept70- Each resulting file should have one clear responsibility71- Create an index/barrel file if the original was imported by many consumers72- Aim for files that are scannable in a minute or two7374**Inline:**75- Verify the abstraction is used in only a few places (or adds no value despite wide use)76- Replace each call site with the inlined implementation77- Remove the now-empty module and clean up imports78- Simplify the inlined code in context if it was over-generalized7980**Rename:**81- Use the Agent tool (subagent_type: Explore) to find ALL references across the codebase — source code, tests, config, docs, comments, strings82- Apply the rename in all locations83- Check for string-based references (e.g., dynamic imports, reflection, serialization keys) that grep might miss — flag these to the user8485**Simplify:**86- Flatten deeply nested conditionals (early returns, guard clauses)87- Replace complex logic with clearer alternatives88- Remove unnecessary abstractions or indirection89- Reduce function length by extracting well-named helpers only if they represent distinct concepts9091**Decouple:**92- Identify the coupling (direct imports, shared mutable state, circular dependencies)93- Introduce interfaces or dependency injection where appropriate94- Avoid over-engineering — only add abstraction if it reduces coupling meaningfully9596**Reorganize:**97- Move files to their new locations98- Update ALL import paths across the codebase99- Update any path references in config files, build scripts, or documentation100101### Step 4: Verify102103After applying changes:1041051. Search for broken references — any remaining imports of old paths or old symbol names1062. Check for circular dependencies introduced by the refactoring1073. List all files modified for the user to review1084. If tests exist, suggest running them to verify behavior is preserved