Call EnterPlanMode immediately before doing anything else.
You are performing a comprehensive, multi-dimensional refactoring analysis. Examine code through Correctness & Security, Performance & Efficiency, and Structure & Maintainability lenses simultaneously, synthesize cross-cutting insights, and — after user approval — execute behavior-preserving changes incrementally.
ARGUMENTS: The user may provide an optional target argument — a file path, directory, function/class name, branch name, commit range, or natural language description of what to refactor. If no argument is provided, auto-detect the scope from git state.
IMPORTANT: Always quote the user-supplied argument in double quotes when passing it to shell commands.
Step 1: Resolve Refactoring Target
Determine what code to refactor based on the argument and project state.
If an argument was provided, resolve it in this order:
File path — if the path exists on disk as a file, refactor that file:
test -f "<path>" && echo "file"
Read the file in full and identify all functions, classes, and modules within it.
Directory path — if the path is a directory, find all source files in it:
test -d "<path>" && echo "directory"
Find source files (exclude test files, node_modules, vendor, build artifacts):
find "<path>" -type f \( -name '*.ts' -o -name '*.js' -o -name '*.py' -o -name '*.go' -o -name '*.rb' -o -name '*.rs' -o -name '*.java' -o -name '*.tsx' -o -name '*.jsx' -o -name '*.php' -o -name '*.cs' -o -name '*.kt' -o -name '*.swift' -o -name '*.c' -o -name '*.cpp' -o -name '*.h' \) ! -path '*/node_modules/*' ! -path '*/vendor/*' ! -path '*/__pycache__/*' ! -path '*/dist/*' ! -path '*/build/*' ! -path '*/target/*' ! -name '*.test.*' ! -name '*.spec.*' ! -name '*_test.*' | head -20
If the directory contains more than 20 source files, list them and ask the user to narrow the scope or confirm they want to proceed (up to 30 files maximum).
Function, class, or method name — if the argument is not a valid path, search the codebase for it:
grep -rn --include='*.ts' --include='*.js' --include='*.py' --include='*.go' --include='*.rb' --include='*.rs' --include='*.java' --include='*.tsx' --include='*.jsx' --include='*.php' --include='*.cs' --include='*.kt' -E "(function|def|func|class|fn|pub fn|export|interface|struct|enum|trait|impl)\s+<arg>" . 2>/dev/null | grep -v node_modules | grep -v vendor | head -10
If found in multiple files, list them and ask the user to confirm which one. Read the full file(s) containing the match.
Git ref (branch or tag) — if git rev-parse --verify <arg> succeeds and it is not a file path, identify files changed on that ref compared to the default branch:
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
[ -z "$default_branch" ] && git rev-parse --verify main >/dev/null 2>&1 && default_branch=main
[ -z "$default_branch" ] && git rev-parse --verify master >/dev/null 2>&1 && default_branch=master
git diff "$default_branch"..."<arg>" --name-only --diff-filter=ACMR 2>/dev/null
Read those files in full for refactoring analysis.
Commit range — if the argument contains .., use it directly:
git diff "<range>" --name-only --diff-filter=ACMR 2>/dev/null
Read those files in full.
Natural language description — if none of the above match, interpret the argument as a description of what to refactor (e.g., "the authentication module", "error handling in the API layer"). Search for relevant code by extracting keywords and scanning the codebase. Present found files and ask the user to confirm scope.
If none of the above produce results, inform the user and stop:
Could not resolve the argument as a file path, directory, code identifier, git ref, or code area description. Try: /refactor src/auth/handler.ts (file), /refactor src/utils/ (directory), /refactor handleLogin (function), /refactor feature-branch (branch), /refactor HEAD~3..HEAD (range), or /refactor "the database layer" (description).
If no argument was provided, auto-detect in this priority order:
- Staged changes — check for staged files:
git diff --cached --name-only --diff-filter=ACMR 2>/dev/null
- Unstaged changes — check for modified files:
git diff --name-only --diff-filter=ACMR 2>/dev/null
- Branch diff — if on a non-default branch, find files changed on this branch:
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
[ -z "$default_branch" ] && git rev-parse --verify main >/dev/null 2>&1 && default_branch=main
[ -z "$default_branch" ] && git rev-parse --verify master >/dev/null 2>&1 && default_branch=master
git diff "$default_branch"...HEAD --name-only --diff-filter=ACMR 2>/dev/null
- If no changes are found, inform the user and stop:
No changed files detected. Working tree is clean. Specify a target: /refactor src/auth/handler.ts or /refactor src/utils/
Filter detected files to source files only (exclude test files, configs, docs, generated files). If more than 20 source files are detected, list them and ask the user to confirm or narrow the scope.
After resolving the target, gather project context by reading these files if they exist:
CLAUDE.md — project conventions
package.json, pyproject.toml, Cargo.toml, go.mod, Gemfile, pom.xml, build.gradle, composer.json — project manifest
.eslintrc*, biome.json, .prettierrc*, .rubocop.yml, clippy.toml — linting/style configuration
tsconfig.json, .editorconfig — code style settings
Detect test infrastructure by checking:
- Existence of test files matching common patterns (
*.test.*, *.spec.*, *_test.*, test_*.*)
- Test configuration files (
jest.config.*, vitest.config.*, pytest.ini, conftest.py, etc.)
- Test runner command (look at
package.json scripts, Makefile, CI workflows)
Record the test runner command for use in Step 4.
If the resolved scope covers more than 20 files or the total lines of code across all target files exceeds 1500, note this in the output so the user knows the analysis covers a large scope.
State the resolved target, detected project context, and test coverage status clearly before proceeding.
Step 2: Multi-Dimensional Analysis
Run the analysis as a Workflow of exactly 3 read-only Explore agents in parallel — one per lens below. Call the Workflow tool with a script along these lines, substituting the context resolved in Step 1 and each agent's brief verbatim from its ### Agent N section:
export const meta = {
name: 'refactor-analysis',
description: 'Three-lens refactoring analysis: correctness & security, performance, structure',
phases: [{ title: 'Analyze' }],
}
const CONTEXT = `<the target files, project context, and language/framework resolved in Step 1>`
// Mirrors the structured format below — the harness validates each agent's return against it.
// `rating` carries the lens-specific field: Severity (Agent 1), Impact (Agent 2), Category (Agent 3).
const FINDINGS = {
type: 'object', additionalProperties: false,
properties: {
findings: { type: 'array', items: {
type: 'object', additionalProperties: false,
properties: {
id: { type: 'string' }, file: { type: 'string' }, line: { type: 'string' },
title: { type: 'string', maxLength: 80 },
current: { type: 'string' }, proposed: { type: 'string' }, rationale: { type: 'string' },
confidence: { type: 'string', enum: ['high', 'medium', 'low'] },
risk: { type: 'string', enum: ['safe', 'moderate', 'breaking'] },
rating: { type: 'string' },
},
required: ['id', 'file', 'line', 'title', 'current', 'proposed', 'rationale', 'confidence', 'risk', 'rating'],
} },
looks_good: { type: 'array', minItems: 2, maxItems: 3, items: { type: 'string' } },
},
required: ['findings', 'looks_good'],
}
const LENSES = [
{ key: 'correctness-security', brief: `<Agent 1 brief, verbatim>` },
{ key: 'performance', brief: `<Agent 2 brief, verbatim>` },
{ key: 'structure', brief: `<Agent 3 brief, verbatim>` },
]
const reports = await parallel(LENSES.map(l => () =>
agent(`Analyze the target code through the ${l.key} lens. Read the FULL target files, not just snippets.\n${CONTEXT}\n\n${l.brief}`,
{ label: `analyze:${l.key}`, phase: 'Analyze', agentType: 'Explore', schema: FINDINGS })))
return { lenses: LENSES.map((l, i) => ({ key: l.key, report: reports[i] })) }
Wait for the Workflow's completion notification before continuing — never synthesize from partial results. Each report is a validated { findings, looks_good } object; a null report means that agent was skipped or failed — say so in the plan header rather than silently dropping the lens.
Fallback. If the Workflow tool is not available in this session, launch the same three briefs as 3 Explore subagents in parallel via the Agent tool (subagent_type: "Explore", model: "opus").
Provide each agent with:
- The resolved target files from Step 1
- The project context (manifest, linting config, conventions)
- The language and framework detected
IMPORTANT: All subagents MUST be launched with agentType: 'Explore' inside the Workflow script (omit model — each agent inherits the session model), or, on the Agent-tool fallback, with subagent_type: "Explore" and model: "opus" (resolves to the latest Claude Opus, the most capable model). The Explore agent is read-only by design (Edit and Write are denied at the agent level). This ensures no subagent can accidentally modify the project during analysis. The explicit model: "opus" on the Agent path pins the fan-out to the latest Opus even when a cheaper default subagent model is configured, so the analysis never silently runs on a smaller model. Never use general-purpose subagents in this skill.
IMPORTANT: Instruct each agent to read the full target files (not just snippets) so they understand the complete code structure, how functions relate to each other, and whether a proposed change would break callers or dependents.
Each agent must return findings in this structured format (the FINDINGS schema above enforces it on the Workflow path; on the Agent-tool fallback, include the list in each prompt):
- ID: agent-local identifier (e.g., C1, P1, S1)
- File: exact file path and line number(s)
- Title: short description (under 80 characters)
- Current pattern: what the code does now (include the relevant code snippet)
- Proposed change: what the code should do instead (include the replacement code snippet)
- Rationale: why this change improves the code
- Confidence: High / Medium / Low (how certain the agent is that this is an actual issue)
- Risk: Safe (behavior-preserving, no regression possible) / Moderate (behavior-preserving but context-dependent) / Breaking (intentionally changes behavior for correctness or security)
Each agent must also return 2-3 "Looks Good" callouts — things the code already does well in their analysis dimension that should NOT be changed. This prevents unnecessary refactoring and acknowledges good practices.
Agent 1: Correctness & Security
Review the target code for correctness issues, security vulnerabilities, and hardening opportunities:
Correctness:
- Logic errors: off-by-one errors, boundary conditions, incorrect comparisons, wrong operator precedence, short-circuit evaluation mistakes
- Null / undefined safety: potential null dereferences, optional chaining gaps, missing nil checks, unsafe type assertions or casts
- Error handling: swallowed exceptions, missing error propagation, catch blocks that hide failures, inconsistent error handling across similar code paths, unhandled promise rejections
- Type safety: implicit type coercions that cause bugs, unchecked type assertions, missing generic constraints, stringly-typed APIs that should use enums or unions
- API contract violations: wrong return types, missing required fields, incorrect parameter usage, broken interface contracts, violated pre/postconditions
- Concurrency correctness: race conditions, deadlock risks, shared mutable state without synchronization, non-atomic read-modify-write sequences, missing locks or semaphores
- Edge cases: empty inputs not handled, boundary values not considered, missing default cases in switch/match, unreachable code that should be reachable
Security:
- Injection vulnerabilities: SQL injection, XSS, command injection, LDAP injection, template injection, header injection
- Input validation: missing or insufficient validation at trust boundaries, unsanitized user input passed to sensitive operations
- Authentication & authorization: auth bypasses, privilege escalation paths, missing permission checks, session management weaknesses, insecure token handling
- Secrets in code: hardcoded API keys, credentials, tokens, connection strings, private keys, encryption keys
- Error information leaks: stack traces exposed to users, internal paths or identifiers in error messages, verbose error logging with sensitive data
- Unsafe deserialization: untrusted data parsed without validation (JSON.parse of user input with prototype pollution risk, pickle.loads, YAML.load, eval, Function constructor)
- Cryptographic weaknesses: weak algorithms (MD5, SHA1 for security purposes), hardcoded IVs/salts, predictable random (Math.random for tokens), custom crypto implementations
- TOCTOU: time-of-check-to-time-of-use vulnerabilities in file operations, permission checks, or state validation
- Access control: missing authorization on routes/endpoints, insecure direct object references, path traversal, directory traversal
- Resource safety: unbounded allocations from user input, missing timeouts on network calls, denial-of-service vectors, regex backtracking (ReDoS)
- SSRF: user-controlled URLs passed to HTTP clients without allowlist validation
For each finding, assign:
- Severity: Critical / High / Medium / Low
Return findings and strengths in the structured format described above.
Agent 2: Performance & Efficiency
Review the target code for performance issues, resource efficiency, and optimization opportunities:
- Algorithm complexity: O(n^2) or worse where O(n) or O(n log n) is possible, unnecessary nested loops, quadratic string concatenation
- N+1 queries: database queries inside loops, repeated network calls that could be batched, sequential API calls that could be parallelized
- Unnecessary allocations: objects or arrays created in hot paths that could be reused, string concatenation in loops instead of builders/join, creating closures inside loops
- Missing caching: expensive pure computations repeated with the same inputs, redundant filesystem or network reads, repeated regex compilation
- Sync-to-async opportunities: blocking I/O operations that could be non-blocking, sequential independent operations that could be parallelized (Promise.all, asyncio.gather, goroutines)
- Redundant computation: values calculated multiple times when they could be computed once and stored, unnecessary re-renders (React), duplicate processing in middleware chains
- Memory and resource leaks: unclosed file handles, database connections, event listeners not removed, subscriptions not unsubscribed, timers not cleared, streams not drained
- Resource lifecycle: missing cleanup in destructors/finalizers/defer, connections not returned to pools, temporary files not deleted, acquired locks not released in error paths
- Inefficient data structures: arrays used where sets or maps would provide O(1) lookup, linear searches through sorted data, unnecessary copying of large structures, using objects as lookup tables without considering Map
- Unindexed queries: database queries on columns without indexes, missing composite indexes for multi-column WHERE clauses, full table scans
- Scalability bottlenecks: single-threaded processing where parallelism is possible, unbounded queues, missing backpressure, global locks that serialize concurrent operations
- Bundle and payload size: unused imports, large dependencies where lighter alternatives exist, missing tree-shaking, uncompressed responses, oversized payloads without pagination
For each finding, assign:
- Impact: High / Medium / Low (estimated performance improvement)
Return findings and strengths in the structured format described above.
Agent 3: Structure & Maintainability
Review the target code for clarity, consistency, architecture, and maintainability:
Readability:
- Naming clarity: vague or misleading variable/function/class names (e.g.,
data, temp, result, handle), inconsistent naming conventions within the file, abbreviations that hurt readability
- Function length and complexity: functions over 40 lines, cyclomatic complexity above 10, functions doing more than one thing, too many parameters (5+)
- Dead code: unreachable code, unused imports, unused variables, commented-out code blocks, feature flags for long-removed features, functions with no callers
- Magic numbers and strings: unexplained numeric constants, hardcoded string values that should be named constants, repeated literal values
- Complex conditionals: deeply nested if/else chains that could be guard clauses, boolean expressions with more than 3 conditions, negated conditions that could be simplified, conditional chains that could be lookup tables
- Deep nesting: more than 3 levels of indentation, arrow code, early return patterns that could flatten logic, nested callbacks that could be async/await
- Missing or misleading comments: complex algorithms without explanation, comments that contradict the code, TODO/FIXME without context or ticket reference
Architecture & Design:
- Separation of concerns: business logic mixed with I/O, presentation mixed with data access, configuration scattered through application code
- Module boundaries: circular dependencies, modules with too many responsibilities, god classes/files, unclear public API surfaces
- API ergonomics: confusing function signatures, inconsistent parameter ordering, boolean parameters that should be enums or option objects, missing builder/fluent patterns for complex construction
- Testability: tightly coupled dependencies that prevent unit testing, hidden dependencies on global state, side effects in constructors, untestable private logic that should be extracted
- Code duplication: 3 or more occurrences of substantially similar logic (not minor repetition — only flag when extraction genuinely improves clarity), copy-paste patterns with slight variations
- Inconsistent patterns: different error handling approaches in the same module, mixed sync/async styles without reason, inconsistent logging or validation patterns
- Unclear control flow: complex state machines without documentation, non-obvious side effects, action-at-a-distance patterns, implicit ordering dependencies
- Overly complex abstractions: indirection that adds complexity without value, premature generalization, unnecessary design patterns, wrapper classes that only delegate
For each finding, assign:
- Category: naming / complexity / dead-code / magic-values / conditionals / nesting / separation / modules / api-design / testability / duplication / inconsistency / control-flow / abstraction / comments
Return findings and strengths in the structured format described above.
Step 3: Synthesize Refactoring Plan
Collect all findings from the 3 agents and produce a single, structured refactoring plan.
Synthesis rules:
Deduplicate: If two agents flagged the same line or function for related reasons, merge into one finding with combined context and note all applicable pillars.
Identify cross-cutting improvements: Scan every finding's file path and line range. If two findings from different dimensions touch the same function or overlap within a 10-line span, flag them as cross-cutting. Also detect semantic overlaps (e.g., "remove dead code" from Structure that also eliminates an "unused crypto import with a known CVE" from Correctness). Cross-cutting findings get bracket notation: [C+P] (Correctness + Performance), [C+S] (Correctness + Structure), [P+S] (Performance + Structure), [C+P+S] (all three).
Priority order: Cross-cutting improvements first (highest value — one change, multiple benefits), then Correctness & Security (by severity: Critical > High > Medium > Low), then Performance & Efficiency (by impact: High > Medium > Low), then Structure & Maintainability (by category importance).
Track dependencies: If one change is a prerequisite for another (e.g., "extract validation function" enables "add input sanitization"), note the dependency with "depends on R3" notation.
Assign IDs: Number findings sequentially across the entire plan: [R1], [R2], [R3], etc. (R for Refactoring).
Be specific: Every finding must have a file path and line number. Never say "consider improving" without pointing to exact code.
Be actionable: Every finding must include a concrete proposed change with code showing the transformation.
Omit empty sections: If there are no cross-cutting findings, do not include the cross-cutting heading. Same for individual pillar sections with no findings.
Use this report format:
## Refactoring Plan: <target description>
**Scope**: <N files, M total lines> | **Findings**: <X total> (<A cross-cutting, B correctness/security, C performance, D structure/maintainability>)
### Test Coverage
<Status: "Tests found — runner: `<command>`" or "No test coverage detected. Consider running `/test-gen` before applying changes to establish a regression baseline.">
---
### Cross-Cutting Improvements (one change, multiple benefits)
| ID | File:Line | Change | Pillars | Confidence | Risk |
|----|-----------|--------|---------|------------|------|
| [R1] | `path:42` | <description> | [C+S] | High | Safe |
**[R1]** `path/to/file.ext:42` — <Title>
**Current**: <what the code does now — include code snippet>
**Proposed**: <what it should do — include replacement code snippet>
**Why**: <benefits across the noted pillars>
---
### Correctness & Security
| ID | File:Line | Change | Severity | Confidence | Risk |
|----|-----------|--------|----------|------------|------|
| [R3] | `path:15` | <description> | High | High | Moderate |
**[R3]** `path/to/file.ext:15` — <Title>
**Current**: <pattern>
**Proposed**: <change>
**Why**: <rationale>
---
### Performance & Efficiency
| ID | File:Line | Change | Impact | Confidence | Risk |
|----|-----------|--------|--------|------------|------|
(same detail format)
---
### Structure & Maintainability
| ID | File:Line | Change | Category | Confidence | Risk |
|----|-----------|--------|----------|------------|------|
(same detail format)
---
### Dependencies
- [R5] depends on [R2] (extraction must happen before the security fix)
### Looks Good (do not change)
- <Positive observation from Correctness & Security agent>
- <Positive observation from Performance & Efficiency agent>
- <Positive observation from Structure & Maintainability agent>
---
### Recommendation
<Brief assessment: how many changes are safe to apply immediately, how many need review, overall code quality impression, suggested approach (e.g., "apply all Safe changes first, then review the 2 Moderate-risk changes individually")>
After presenting the refactoring plan, call ExitPlanMode, then ask:
Ready to apply these changes? (e.g., "apply all", "apply R1 through R4", "apply all safe changes", "apply security only", "skip R7")
Step 4: Execute Refactoring
After the user approves (or modifies) the plan, apply the changes.
Before making any changes, capture a backup stash that you can identify reliably later. git stash push exits 0 even when there's nothing to stash, so use git stash create + git stash store to capture an explicit SHA instead:
backup_sha=$(git stash create "refactor-backup: before /refactor changes" 2>/dev/null)
if [ -n "$backup_sha" ]; then
git stash store -m "refactor-backup: before /refactor changes" "$backup_sha"
fi
If $backup_sha is non-empty, a backup exists at that SHA — record it so a later "revert all" can use git stash apply "$backup_sha" (or git checkout "$backup_sha" -- .) to restore exactly that snapshot rather than popping whatever stash happens to be on top. If $backup_sha is empty, the working tree was clean — proceed without a backup.
If the repository has uncommitted changes outside the refactoring scope, warn the user before proceeding.
Track progress with tasks. Before applying the first change, call TaskCreate once per selected refactoring (one task per [R<n>] finding). The subject should be the finding ID + title (e.g., [R3] Extract input validation). Mark in_progress when you start the Edit for that finding and completed after it's applied. Refactoring plans regularly have 10-30 findings; this keeps the user oriented through the execution phase.
Execution rules:
Apply in priority order: Cross-cutting first, then Correctness & Security, then Performance & Efficiency, then Structure & Maintainability. Within each group, respect dependency ordering.
Respect the user's selection: If the user said "apply R1 through R4", only apply those. If "apply all safe changes", filter to Risk=Safe only. If "apply security only", filter to Correctness & Security findings and cross-cutting findings that include the Correctness pillar.
Use Edit for modifications to existing files. Use Write only when creating genuinely new files (e.g., extracting a module into a new file).
Show each change: After applying each finding, briefly state what was changed:
[R1] Applied: Extracted input validation into validateUserInput() and added sanitization. (src/auth/handler.ts:42-58)
After all changes are applied, if a test runner was detected, run the test suite:
<test_runner_command> 2>&1
If all tests pass:
All changes applied successfully. refactorings across files. All tests pass.
Run git diff to review the changes before committing.
Skill handoff. After a successful refactor, offer to refresh tests for the touched modules — particularly when the refactoring renamed or restructured public surfaces:
Next: Want me to hand off to /test-gen for the modified files to refresh test coverage and pick up any new branches the refactor introduced?
Use the Skill tool to invoke /test-gen if the user agrees. Skip the offer when the refactor was purely internal (e.g., a constant rename inside one function) and existing tests already exercise the surface.
If tests fail, report which tests failed and diagnose the likely cause:
tests passed, failed. The failure appears related to [R4] ().
Options:
- "revert R4" — undo just that change
- "revert all" — restore to pre-refactoring state (
git stash apply "$backup_sha" against the SHA captured before Step 4)
- "fix it" — attempt to fix the failing test while preserving the refactoring intent
If no test runner is available:
All changes applied. No test runner detected — review changes manually with git diff before committing.
Skill handoff. Offer to bootstrap a test infrastructure via /test-gen (which detects no-framework repos and proposes setup):
Next: No test framework detected. Want me to hand off to /test-gen to scaffold one and write tests for the refactored modules?
Use the Skill tool to invoke /test-gen if the user agrees.
1---2name: refactor3description: Comprehensive code refactoring across correctness, security, performance, and maintainability with behavior-preserving, incremental changes.4---56Call `EnterPlanMode` immediately before doing anything else.78You are performing a comprehensive, multi-dimensional refactoring analysis. Examine code through Correctness & Security, Performance & Efficiency, and Structure & Maintainability lenses simultaneously, synthesize cross-cutting insights, and — after user approval — execute behavior-preserving changes incrementally.910**ARGUMENTS:** The user may provide an optional target argument — a file path, directory, function/class name, branch name, commit range, or natural language description of what to refactor. If no argument is provided, auto-detect the scope from git state.1112**IMPORTANT:** Always quote the user-supplied argument in double quotes when passing it to shell commands.1314---1516## Step 1: Resolve Refactoring Target1718Determine what code to refactor based on the argument and project state.1920**If an argument was provided**, resolve it in this order:21221. **File path** — if the path exists on disk as a file, refactor that file:23 ```bash24 test -f "<path>" && echo "file"25 ```26 Read the file in full and identify all functions, classes, and modules within it.27282. **Directory path** — if the path is a directory, find all source files in it:29 ```bash30 test -d "<path>" && echo "directory"31 ```32 Find source files (exclude test files, node_modules, vendor, build artifacts):33 ```bash34 find "<path>" -type f \( -name '*.ts' -o -name '*.js' -o -name '*.py' -o -name '*.go' -o -name '*.rb' -o -name '*.rs' -o -name '*.java' -o -name '*.tsx' -o -name '*.jsx' -o -name '*.php' -o -name '*.cs' -o -name '*.kt' -o -name '*.swift' -o -name '*.c' -o -name '*.cpp' -o -name '*.h' \) ! -path '*/node_modules/*' ! -path '*/vendor/*' ! -path '*/__pycache__/*' ! -path '*/dist/*' ! -path '*/build/*' ! -path '*/target/*' ! -name '*.test.*' ! -name '*.spec.*' ! -name '*_test.*' | head -2035 ```36 If the directory contains more than 20 source files, list them and ask the user to narrow the scope or confirm they want to proceed (up to 30 files maximum).37383. **Function, class, or method name** — if the argument is not a valid path, search the codebase for it:39 ```bash40 grep -rn --include='*.ts' --include='*.js' --include='*.py' --include='*.go' --include='*.rb' --include='*.rs' --include='*.java' --include='*.tsx' --include='*.jsx' --include='*.php' --include='*.cs' --include='*.kt' -E "(function|def|func|class|fn|pub fn|export|interface|struct|enum|trait|impl)\s+<arg>" . 2>/dev/null | grep -v node_modules | grep -v vendor | head -1041 ```42 If found in multiple files, list them and ask the user to confirm which one. Read the full file(s) containing the match.43444. **Git ref** (branch or tag) — if `git rev-parse --verify <arg>` succeeds and it is not a file path, identify files changed on that ref compared to the default branch:45 ```bash46 default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')47 [ -z "$default_branch" ] && git rev-parse --verify main >/dev/null 2>&1 && default_branch=main48 [ -z "$default_branch" ] && git rev-parse --verify master >/dev/null 2>&1 && default_branch=master49 ```50 ```bash51 git diff "$default_branch"..."<arg>" --name-only --diff-filter=ACMR 2>/dev/null52 ```53 Read those files in full for refactoring analysis.54555. **Commit range** — if the argument contains `..`, use it directly:56 ```bash57 git diff "<range>" --name-only --diff-filter=ACMR 2>/dev/null58 ```59 Read those files in full.60616. **Natural language description** — if none of the above match, interpret the argument as a description of what to refactor (e.g., "the authentication module", "error handling in the API layer"). Search for relevant code by extracting keywords and scanning the codebase. Present found files and ask the user to confirm scope.62637. If none of the above produce results, inform the user and stop:64 > Could not resolve the argument as a file path, directory, code identifier, git ref, or code area description. Try: `/refactor src/auth/handler.ts` (file), `/refactor src/utils/` (directory), `/refactor handleLogin` (function), `/refactor feature-branch` (branch), `/refactor HEAD~3..HEAD` (range), or `/refactor "the database layer"` (description).6566**If no argument was provided**, auto-detect in this priority order:67681. **Staged changes** — check for staged files:69 ```bash70 git diff --cached --name-only --diff-filter=ACMR 2>/dev/null71 ```722. **Unstaged changes** — check for modified files:73 ```bash74 git diff --name-only --diff-filter=ACMR 2>/dev/null75 ```763. **Branch diff** — if on a non-default branch, find files changed on this branch:77 ```bash78 default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')79 [ -z "$default_branch" ] && git rev-parse --verify main >/dev/null 2>&1 && default_branch=main80 [ -z "$default_branch" ] && git rev-parse --verify master >/dev/null 2>&1 && default_branch=master81 ```82 ```bash83 git diff "$default_branch"...HEAD --name-only --diff-filter=ACMR 2>/dev/null84 ```854. If no changes are found, inform the user and stop:86 > No changed files detected. Working tree is clean. Specify a target: `/refactor src/auth/handler.ts` or `/refactor src/utils/`8788Filter detected files to source files only (exclude test files, configs, docs, generated files). If more than 20 source files are detected, list them and ask the user to confirm or narrow the scope.8990**After resolving the target**, gather project context by reading these files if they exist:91- `CLAUDE.md` — project conventions92- `package.json`, `pyproject.toml`, `Cargo.toml`, `go.mod`, `Gemfile`, `pom.xml`, `build.gradle`, `composer.json` — project manifest93- `.eslintrc*`, `biome.json`, `.prettierrc*`, `.rubocop.yml`, `clippy.toml` — linting/style configuration94- `tsconfig.json`, `.editorconfig` — code style settings9596**Detect test infrastructure** by checking:97- Existence of test files matching common patterns (`*.test.*`, `*.spec.*`, `*_test.*`, `test_*.*`)98- Test configuration files (`jest.config.*`, `vitest.config.*`, `pytest.ini`, `conftest.py`, etc.)99- Test runner command (look at `package.json` scripts, `Makefile`, CI workflows)100101Record the test runner command for use in Step 4.102103If the resolved scope covers more than 20 files or the total lines of code across all target files exceeds 1500, note this in the output so the user knows the analysis covers a large scope.104105State the resolved target, detected project context, and test coverage status clearly before proceeding.106107---108109## Step 2: Multi-Dimensional Analysis110111Run the analysis as a **`Workflow` of exactly 3 read-only Explore agents in parallel** — one per lens below. Call the `Workflow` tool with a script along these lines, substituting the context resolved in Step 1 and each agent's brief verbatim from its `### Agent N` section:112113```js114export const meta = {115 name: 'refactor-analysis',116 description: 'Three-lens refactoring analysis: correctness & security, performance, structure',117 phases: [{ title: 'Analyze' }],118}119120const CONTEXT = `<the target files, project context, and language/framework resolved in Step 1>`121122// Mirrors the structured format below — the harness validates each agent's return against it.123// `rating` carries the lens-specific field: Severity (Agent 1), Impact (Agent 2), Category (Agent 3).124const FINDINGS = {125 type: 'object', additionalProperties: false,126 properties: {127 findings: { type: 'array', items: {128 type: 'object', additionalProperties: false,129 properties: {130 id: { type: 'string' }, file: { type: 'string' }, line: { type: 'string' },131 title: { type: 'string', maxLength: 80 },132 current: { type: 'string' }, proposed: { type: 'string' }, rationale: { type: 'string' },133 confidence: { type: 'string', enum: ['high', 'medium', 'low'] },134 risk: { type: 'string', enum: ['safe', 'moderate', 'breaking'] },135 rating: { type: 'string' },136 },137 required: ['id', 'file', 'line', 'title', 'current', 'proposed', 'rationale', 'confidence', 'risk', 'rating'],138 } },139 looks_good: { type: 'array', minItems: 2, maxItems: 3, items: { type: 'string' } },140 },141 required: ['findings', 'looks_good'],142}143144const LENSES = [145 { key: 'correctness-security', brief: `<Agent 1 brief, verbatim>` },146 { key: 'performance', brief: `<Agent 2 brief, verbatim>` },147 { key: 'structure', brief: `<Agent 3 brief, verbatim>` },148]149150const reports = await parallel(LENSES.map(l => () =>151 agent(`Analyze the target code through the ${l.key} lens. Read the FULL target files, not just snippets.\n${CONTEXT}\n\n${l.brief}`,152 { label: `analyze:${l.key}`, phase: 'Analyze', agentType: 'Explore', schema: FINDINGS })))153154return { lenses: LENSES.map((l, i) => ({ key: l.key, report: reports[i] })) }155```156157Wait for the Workflow's completion notification before continuing — never synthesize from partial results. Each `report` is a validated `{ findings, looks_good }` object; a `null` report means that agent was skipped or failed — say so in the plan header rather than silently dropping the lens.158159**Fallback.** If the `Workflow` tool is not available in this session, launch the same three briefs as **3 Explore subagents in parallel** via the `Agent` tool (`subagent_type: "Explore"`, `model: "opus"`).160161Provide each agent with:162- The resolved target files from Step 1163- The project context (manifest, linting config, conventions)164- The language and framework detected165166**IMPORTANT:** All subagents MUST be launched with `agentType: 'Explore'` inside the `Workflow` script (omit `model` — each agent inherits the session model), or, on the `Agent`-tool fallback, with `subagent_type: "Explore"` and `model: "opus"` (resolves to the latest Claude Opus, the most capable model). The Explore agent is read-only by design (Edit and Write are denied at the agent level). This ensures no subagent can accidentally modify the project during analysis. The explicit `model: "opus"` on the `Agent` path pins the fan-out to the latest Opus even when a cheaper default subagent model is configured, so the analysis never silently runs on a smaller model. Never use general-purpose subagents in this skill.167168**IMPORTANT:** Instruct each agent to read the **full target files** (not just snippets) so they understand the complete code structure, how functions relate to each other, and whether a proposed change would break callers or dependents.169170Each agent must return findings in this structured format (the `FINDINGS` schema above enforces it on the Workflow path; on the Agent-tool fallback, include the list in each prompt):171- **ID**: agent-local identifier (e.g., C1, P1, S1)172- **File**: exact file path and line number(s)173- **Title**: short description (under 80 characters)174- **Current pattern**: what the code does now (include the relevant code snippet)175- **Proposed change**: what the code should do instead (include the replacement code snippet)176- **Rationale**: why this change improves the code177- **Confidence**: High / Medium / Low (how certain the agent is that this is an actual issue)178- **Risk**: Safe (behavior-preserving, no regression possible) / Moderate (behavior-preserving but context-dependent) / Breaking (intentionally changes behavior for correctness or security)179180Each agent must also return 2-3 **"Looks Good"** callouts — things the code already does well in their analysis dimension that should NOT be changed. This prevents unnecessary refactoring and acknowledges good practices.181182---183184### Agent 1: Correctness & Security185186Review the target code for correctness issues, security vulnerabilities, and hardening opportunities:187188**Correctness:**189- **Logic errors**: off-by-one errors, boundary conditions, incorrect comparisons, wrong operator precedence, short-circuit evaluation mistakes190- **Null / undefined safety**: potential null dereferences, optional chaining gaps, missing nil checks, unsafe type assertions or casts191- **Error handling**: swallowed exceptions, missing error propagation, catch blocks that hide failures, inconsistent error handling across similar code paths, unhandled promise rejections192- **Type safety**: implicit type coercions that cause bugs, unchecked type assertions, missing generic constraints, stringly-typed APIs that should use enums or unions193- **API contract violations**: wrong return types, missing required fields, incorrect parameter usage, broken interface contracts, violated pre/postconditions194- **Concurrency correctness**: race conditions, deadlock risks, shared mutable state without synchronization, non-atomic read-modify-write sequences, missing locks or semaphores195- **Edge cases**: empty inputs not handled, boundary values not considered, missing default cases in switch/match, unreachable code that should be reachable196197**Security:**198- **Injection vulnerabilities**: SQL injection, XSS, command injection, LDAP injection, template injection, header injection199- **Input validation**: missing or insufficient validation at trust boundaries, unsanitized user input passed to sensitive operations200- **Authentication & authorization**: auth bypasses, privilege escalation paths, missing permission checks, session management weaknesses, insecure token handling201- **Secrets in code**: hardcoded API keys, credentials, tokens, connection strings, private keys, encryption keys202- **Error information leaks**: stack traces exposed to users, internal paths or identifiers in error messages, verbose error logging with sensitive data203- **Unsafe deserialization**: untrusted data parsed without validation (JSON.parse of user input with prototype pollution risk, pickle.loads, YAML.load, eval, Function constructor)204- **Cryptographic weaknesses**: weak algorithms (MD5, SHA1 for security purposes), hardcoded IVs/salts, predictable random (Math.random for tokens), custom crypto implementations205- **TOCTOU**: time-of-check-to-time-of-use vulnerabilities in file operations, permission checks, or state validation206- **Access control**: missing authorization on routes/endpoints, insecure direct object references, path traversal, directory traversal207- **Resource safety**: unbounded allocations from user input, missing timeouts on network calls, denial-of-service vectors, regex backtracking (ReDoS)208- **SSRF**: user-controlled URLs passed to HTTP clients without allowlist validation209210For each finding, assign:211- **Severity**: Critical / High / Medium / Low212213Return findings and strengths in the structured format described above.214215---216217### Agent 2: Performance & Efficiency218219Review the target code for performance issues, resource efficiency, and optimization opportunities:220221- **Algorithm complexity**: O(n^2) or worse where O(n) or O(n log n) is possible, unnecessary nested loops, quadratic string concatenation222- **N+1 queries**: database queries inside loops, repeated network calls that could be batched, sequential API calls that could be parallelized223- **Unnecessary allocations**: objects or arrays created in hot paths that could be reused, string concatenation in loops instead of builders/join, creating closures inside loops224- **Missing caching**: expensive pure computations repeated with the same inputs, redundant filesystem or network reads, repeated regex compilation225- **Sync-to-async opportunities**: blocking I/O operations that could be non-blocking, sequential independent operations that could be parallelized (Promise.all, asyncio.gather, goroutines)226- **Redundant computation**: values calculated multiple times when they could be computed once and stored, unnecessary re-renders (React), duplicate processing in middleware chains227- **Memory and resource leaks**: unclosed file handles, database connections, event listeners not removed, subscriptions not unsubscribed, timers not cleared, streams not drained228- **Resource lifecycle**: missing cleanup in destructors/finalizers/defer, connections not returned to pools, temporary files not deleted, acquired locks not released in error paths229- **Inefficient data structures**: arrays used where sets or maps would provide O(1) lookup, linear searches through sorted data, unnecessary copying of large structures, using objects as lookup tables without considering Map230- **Unindexed queries**: database queries on columns without indexes, missing composite indexes for multi-column WHERE clauses, full table scans231- **Scalability bottlenecks**: single-threaded processing where parallelism is possible, unbounded queues, missing backpressure, global locks that serialize concurrent operations232- **Bundle and payload size**: unused imports, large dependencies where lighter alternatives exist, missing tree-shaking, uncompressed responses, oversized payloads without pagination233234For each finding, assign:235- **Impact**: High / Medium / Low (estimated performance improvement)236237Return findings and strengths in the structured format described above.238239---240241### Agent 3: Structure & Maintainability242243Review the target code for clarity, consistency, architecture, and maintainability:244245**Readability:**246- **Naming clarity**: vague or misleading variable/function/class names (e.g., `data`, `temp`, `result`, `handle`), inconsistent naming conventions within the file, abbreviations that hurt readability247- **Function length and complexity**: functions over 40 lines, cyclomatic complexity above 10, functions doing more than one thing, too many parameters (5+)248- **Dead code**: unreachable code, unused imports, unused variables, commented-out code blocks, feature flags for long-removed features, functions with no callers249- **Magic numbers and strings**: unexplained numeric constants, hardcoded string values that should be named constants, repeated literal values250- **Complex conditionals**: deeply nested if/else chains that could be guard clauses, boolean expressions with more than 3 conditions, negated conditions that could be simplified, conditional chains that could be lookup tables251- **Deep nesting**: more than 3 levels of indentation, arrow code, early return patterns that could flatten logic, nested callbacks that could be async/await252- **Missing or misleading comments**: complex algorithms without explanation, comments that contradict the code, TODO/FIXME without context or ticket reference253254**Architecture & Design:**255- **Separation of concerns**: business logic mixed with I/O, presentation mixed with data access, configuration scattered through application code256- **Module boundaries**: circular dependencies, modules with too many responsibilities, god classes/files, unclear public API surfaces257- **API ergonomics**: confusing function signatures, inconsistent parameter ordering, boolean parameters that should be enums or option objects, missing builder/fluent patterns for complex construction258- **Testability**: tightly coupled dependencies that prevent unit testing, hidden dependencies on global state, side effects in constructors, untestable private logic that should be extracted259- **Code duplication**: 3 or more occurrences of substantially similar logic (not minor repetition — only flag when extraction genuinely improves clarity), copy-paste patterns with slight variations260- **Inconsistent patterns**: different error handling approaches in the same module, mixed sync/async styles without reason, inconsistent logging or validation patterns261- **Unclear control flow**: complex state machines without documentation, non-obvious side effects, action-at-a-distance patterns, implicit ordering dependencies262- **Overly complex abstractions**: indirection that adds complexity without value, premature generalization, unnecessary design patterns, wrapper classes that only delegate263264For each finding, assign:265- **Category**: naming / complexity / dead-code / magic-values / conditionals / nesting / separation / modules / api-design / testability / duplication / inconsistency / control-flow / abstraction / comments266267Return findings and strengths in the structured format described above.268269---270271## Step 3: Synthesize Refactoring Plan272273Collect all findings from the 3 agents and produce a single, structured refactoring plan.274275**Synthesis rules:**2762771. **Deduplicate**: If two agents flagged the same line or function for related reasons, merge into one finding with combined context and note all applicable pillars.2782792. **Identify cross-cutting improvements**: Scan every finding's file path and line range. If two findings from different dimensions touch the same function or overlap within a 10-line span, flag them as cross-cutting. Also detect semantic overlaps (e.g., "remove dead code" from Structure that also eliminates an "unused crypto import with a known CVE" from Correctness). Cross-cutting findings get bracket notation: `[C+P]` (Correctness + Performance), `[C+S]` (Correctness + Structure), `[P+S]` (Performance + Structure), `[C+P+S]` (all three).2802813. **Priority order**: Cross-cutting improvements first (highest value — one change, multiple benefits), then Correctness & Security (by severity: Critical > High > Medium > Low), then Performance & Efficiency (by impact: High > Medium > Low), then Structure & Maintainability (by category importance).2822834. **Track dependencies**: If one change is a prerequisite for another (e.g., "extract validation function" enables "add input sanitization"), note the dependency with "depends on R3" notation.2842855. **Assign IDs**: Number findings sequentially across the entire plan: `[R1]`, `[R2]`, `[R3]`, etc. (R for Refactoring).2862876. **Be specific**: Every finding must have a file path and line number. Never say "consider improving" without pointing to exact code.2882897. **Be actionable**: Every finding must include a concrete proposed change with code showing the transformation.2902918. **Omit empty sections**: If there are no cross-cutting findings, do not include the cross-cutting heading. Same for individual pillar sections with no findings.292293**Use this report format:**294295```296## Refactoring Plan: <target description>297298**Scope**: <N files, M total lines> | **Findings**: <X total> (<A cross-cutting, B correctness/security, C performance, D structure/maintainability>)299300### Test Coverage301302<Status: "Tests found — runner: `<command>`" or "No test coverage detected. Consider running `/test-gen` before applying changes to establish a regression baseline.">303304---305306### Cross-Cutting Improvements (one change, multiple benefits)307308| ID | File:Line | Change | Pillars | Confidence | Risk |309|----|-----------|--------|---------|------------|------|310| [R1] | `path:42` | <description> | [C+S] | High | Safe |311312**[R1]** `path/to/file.ext:42` — <Title>313**Current**: <what the code does now — include code snippet>314**Proposed**: <what it should do — include replacement code snippet>315**Why**: <benefits across the noted pillars>316317---318319### Correctness & Security320321| ID | File:Line | Change | Severity | Confidence | Risk |322|----|-----------|--------|----------|------------|------|323| [R3] | `path:15` | <description> | High | High | Moderate |324325**[R3]** `path/to/file.ext:15` — <Title>326**Current**: <pattern>327**Proposed**: <change>328**Why**: <rationale>329330---331332### Performance & Efficiency333334| ID | File:Line | Change | Impact | Confidence | Risk |335|----|-----------|--------|--------|------------|------|336337(same detail format)338339---340341### Structure & Maintainability342343| ID | File:Line | Change | Category | Confidence | Risk |344|----|-----------|--------|----------|------------|------|345346(same detail format)347348---349350### Dependencies351352- [R5] depends on [R2] (extraction must happen before the security fix)353354### Looks Good (do not change)355356- <Positive observation from Correctness & Security agent>357- <Positive observation from Performance & Efficiency agent>358- <Positive observation from Structure & Maintainability agent>359360---361362### Recommendation363364<Brief assessment: how many changes are safe to apply immediately, how many need review, overall code quality impression, suggested approach (e.g., "apply all Safe changes first, then review the 2 Moderate-risk changes individually")>365```366367After presenting the refactoring plan, call `ExitPlanMode`, then ask:368369> **Ready to apply these changes?** (e.g., "apply all", "apply R1 through R4", "apply all safe changes", "apply security only", "skip R7")370371---372373## Step 4: Execute Refactoring374375After the user approves (or modifies) the plan, apply the changes.376377**Before making any changes**, capture a backup stash that you can identify reliably later. `git stash push` exits 0 even when there's nothing to stash, so use `git stash create` + `git stash store` to capture an explicit SHA instead:378379```bash380backup_sha=$(git stash create "refactor-backup: before /refactor changes" 2>/dev/null)381if [ -n "$backup_sha" ]; then382 git stash store -m "refactor-backup: before /refactor changes" "$backup_sha"383fi384```385386If `$backup_sha` is non-empty, a backup exists at that SHA — record it so a later "revert all" can use `git stash apply "$backup_sha"` (or `git checkout "$backup_sha" -- .`) to restore exactly that snapshot rather than popping whatever stash happens to be on top. If `$backup_sha` is empty, the working tree was clean — proceed without a backup.387388If the repository has uncommitted changes outside the refactoring scope, warn the user before proceeding.389390**Track progress with tasks.** Before applying the first change, call `TaskCreate` once per selected refactoring (one task per `[R<n>]` finding). The subject should be the finding ID + title (e.g., `[R3] Extract input validation`). Mark `in_progress` when you start the Edit for that finding and `completed` after it's applied. Refactoring plans regularly have 10-30 findings; this keeps the user oriented through the execution phase.391392**Execution rules:**3933941. **Apply in priority order**: Cross-cutting first, then Correctness & Security, then Performance & Efficiency, then Structure & Maintainability. Within each group, respect dependency ordering.3953962. **Respect the user's selection**: If the user said "apply R1 through R4", only apply those. If "apply all safe changes", filter to Risk=Safe only. If "apply security only", filter to Correctness & Security findings and cross-cutting findings that include the Correctness pillar.3973983. **Use Edit for modifications** to existing files. Use Write only when creating genuinely new files (e.g., extracting a module into a new file).3994004. **Show each change**: After applying each finding, briefly state what was changed:401 > **[R1]** Applied: Extracted input validation into `validateUserInput()` and added sanitization. (`src/auth/handler.ts:42-58`)4024035. **After all changes are applied**, if a test runner was detected, run the test suite:404 ```bash405 <test_runner_command> 2>&1406 ```4074086. **If all tests pass:**409 > **All changes applied successfully.** <N> refactorings across <M> files. All tests pass.410 >411 > Run `git diff` to review the changes before committing.412413 **Skill handoff.** After a successful refactor, offer to refresh tests for the touched modules — particularly when the refactoring renamed or restructured public surfaces:414415 > **Next:** Want me to hand off to `/test-gen` for the modified files to refresh test coverage and pick up any new branches the refactor introduced?416417 Use the `Skill` tool to invoke `/test-gen` if the user agrees. Skip the offer when the refactor was purely internal (e.g., a constant rename inside one function) and existing tests already exercise the surface.4184197. **If tests fail**, report which tests failed and diagnose the likely cause:420 > **<P> tests passed, <F> failed.** The failure appears related to [R4] (<brief diagnosis>).421 >422 > Options:423 > - "revert R4" — undo just that change424 > - "revert all" — restore to pre-refactoring state (`git stash apply "$backup_sha"` against the SHA captured before Step 4)425 > - "fix it" — attempt to fix the failing test while preserving the refactoring intent4264278. **If no test runner is available:**428 > **All changes applied.** No test runner detected — review changes manually with `git diff` before committing.429430 **Skill handoff.** Offer to bootstrap a test infrastructure via `/test-gen` (which detects no-framework repos and proposes setup):431432 > **Next:** No test framework detected. Want me to hand off to `/test-gen` to scaffold one and write tests for the refactored modules?433434 Use the `Skill` tool to invoke `/test-gen` if the user agrees.