---
name: refactor
description: Refactor code by finding files, grouping them, and launching parallel Snipper agents
argument-hint: <search-pattern-or-description>
disable-model-invocation: true
---

<objective>
Refactor code matching the given pattern across the codebase using parallel Snipper agents for maximum speed.

This skill finds all relevant files, creates ONE instruction file, then launches Snipper agents in parallel with batches of max 3 files each.
</objective>

<quick_start>
```bash
/refactor rename getUserData to fetchUserProfile
/refactor replace console.log with logger.info
/refactor convert class components to functional
/refactor update deprecated API calls
```
</quick_start>

<workflow>

## Phase 1: Discovery

### 1. Parse the Refactor Request

Understand what the pattern means:
- Could be: method name, component name, pattern, code smell, etc.
- Identify the search strategy (Grep for code patterns, Glob for file patterns)

### 2. Find All Affected Files

```
Use Grep to search for the pattern in the codebase
Use Glob if searching by file name patterns
Exclude: node_modules, .git, dist, build, bin, obj
```

### 3. Analyze Scope

- Count total files found
- If more than 15 files, ask user to confirm or narrow scope
- Show preview of files to refactor

## Phase 2: Create Instructions

### 4. Create Task Folder

```
Generate unique ID: refactor-{timestamp}
Create folder: .claude/tasks/refactor-{timestamp}/
```

### 5. Create ONE Instruction File

Create `.claude/tasks/refactor-{id}/instructions.md`:

```markdown
# Refactor: {title}

## Objective
{What needs to be refactored - derived from user request}

## Pattern to Find
{Exact code pattern, method name, or structure to locate}

## Transformation
{How to transform the found pattern - be specific and adaptive}

## Examples
Before:
{example of current code}

After:
{example of refactored code}

## Constraints
- Only modify code matching the pattern
- Preserve all existing functionality
- Follow codebase conventions
- No comments unless necessary
```

**IMPORTANT**: Make instructions adaptive - they should work for ANY file in the list.

## Phase 3: Group and Execute

### 6. Group Files into Batches

- Maximum 3 files per batch
- Group by related functionality when possible

### 7. Launch Snipper Agents in Parallel

For EACH batch, use Task tool with `subagent_type='Snipper'`:

```
Using the instructions in .claude/tasks/refactor-{id}/instructions.md, refactor these files:
- {file_1}
- {file_2}
- {file_3}
```

**CRITICAL**: Launch ALL batches in a SINGLE message with multiple Task calls.

### 8. Wait for Completion

- All Snipper agents run in parallel
- Collect results from each

## Phase 4: Verification

### 9. Validate Changes

```bash
# For TypeScript/JavaScript
pnpm lint || npm run lint
pnpm tsc || npx tsc

# For .NET
dotnet build
```

Fix any errors immediately.

### 10. Summary Report

```
Refactor Complete: {title}

Files modified: {count}
Batches executed: {count}
Errors: {count}

Modified files:
- {file1}
- {file2}
...

Next steps:
- Review changes: git diff
- Run tests: pnpm test
- Commit: /gitflow:commit
```

</workflow>

<instructions_template>

Create ONE file at `.claude/tasks/refactor-{id}/instructions.md`:

```markdown
# Refactor: {title}

## Objective
{What needs to be refactored - derived from user request}

## Pattern to Find
{Exact code pattern, method name, or structure to locate}

## Transformation
{How to transform the found pattern - be specific and adaptive}

## Examples
Before:
```
{example of current code}
```

After:
```
{example of refactored code}
```

## Constraints
- Only modify code matching the pattern
- Preserve all existing functionality
- Follow codebase conventions
- No comments unless necessary
```

</instructions_template>

<snipper_prompt_template>

For each batch, call Snipper with:

```
Using the instructions in .claude/tasks/refactor-{id}/instructions.md, refactor these files:
- {file_path_1}
- {file_path_2}
- {file_path_3}

Read the instructions file first, then apply the refactor to each file.
```

</snipper_prompt_template>

<execution_rules>

- **PARALLEL EXECUTION**: Launch all Snipper batches simultaneously
- **SINGLE INSTRUCTION FILE**: One instructions.md for all batches
- **MAX 3 FILES PER BATCH**: Keep Snipper agents focused
- **VALIDATE AFTER**: Run lint/typecheck after all batches complete
- **PRESERVE FUNCTIONALITY**: Never break existing behavior

</execution_rules>

<success_criteria>

- All target files identified
- ONE instruction file created in `.claude/tasks/refactor-{id}/instructions.md`
- Snipper agents launched in parallel (max 3 files per agent)
- All batches completed successfully
- Lint/type checks pass
- Summary provided to user

</success_criteria>
