1---2name: refactor3description: Review and refactor code within a specified scope. Use when the user asks to clean up, simplify, reduce duplication, remove dead code, or improve code quality in a file, module, or broader area of the codebase.4---56# Refactor78## Input910- A file, module, package, feature, diff, or project area to refactor.11- Use explicit input first; otherwise infer from context, recent edits, selected files, or branch.12- Safest default: ask before refactoring the whole project.1314## Workflow15161. **Confirm input**. If no input is provided or implied, confirm before refactoring the entire project.172. **Identify surface**. Find files and direct dependencies that may need review or change.183. **Run baseline checks**. Use available tests, typecheck, lint, or build commands for the scope. Note missing or failing tooling.194. **Identify refactors**:20 - Dead code: unused functions, branches, flags, adapters, and stale comments/docs.21 - Duplication: repeated logic, validation, mapping, and query patterns.22 - Unnecessary complexity: deep nesting, mixed responsibilities, ambiguous naming, and over-abstraction.23 - Evidence-backed optimization: hot loops, N+1 queries, repeated expensive work.245. **Plan changes**. Confirm when changes may affect behavior or remove intentional code.256. **Apply safe changes incrementally**. Prefer small, reviewable edits.267. **Validate behavior**:27 - Re-run available baseline checks plus targeted tests for touched areas.28 - Ensure no accidental API, schema, or side-effect regressions.2930## Output3132- Input reviewed33- Refactors applied34- Behavior preserved or intentionally changed35- Validation run36- Remaining risks or follow-up work3738## Guardrails3940- Do not change behavior to satisfy style preferences alone.41- Do not remove intentional features without confirmation.42- Do not introduce broad rewrites when focused refactors solve the problem.43- Keep type boundaries strict; avoid loose typing unless narrowly justified.