Objective
Execute refactoring workflow for: $ARGUMENTS
Deep Discovery Phase
Before delegating to the agent, gather comprehensive context to ensure safe and effective refactoring.
Step 1: Analyze Refactoring Request
Determine the nature of the refactoring task:
Analyze $ARGUMENTS to classify:
- Code Quality: Improve maintainability, readability, or structure
- Performance: Optimize algorithms or reduce complexity
- Technical Debt: Address anti-patterns or legacy code
- Design Patterns: Apply established patterns for better architecture
- Module Extraction: Extract reusable components or utilities
Step 2: Map Project Context
Gather essential project information:
Technology Stack Identification:
- Identify language and framework
- Identify test framework and runner
- Identify code quality tools (linters, formatters, type checkers)
Project Structure Mapping:
- Locate source files to be refactored
- Identify related files and dependencies
- Check test coverage for affected code
- Locate test files for the target code
Current State Analysis:
- Run existing tests to establish baseline
- Check for linter/formatter warnings
- Identify code complexity metrics if available
- Review git history for recent changes
Step 3: Safety Verification
Ensure refactoring can be performed safely:
Test Coverage Check:
- Verify tests exist for code to be refactored
- If no tests exist, recommend TDD approach first
- Identify test utilities and helpers available
Risk Assessment:
- Identify high-risk areas (authentication, payment, data processing)
- Check for critical paths that require extra caution
- Verify rollback capability (git status clean)
Step 4: Delegation Package
Compile all gathered context into a comprehensive assignment for the code-implementer agent.
Delegation Phase
<assignment>
Execute the refactoring workflow from the engineering skill to improve: $ARGUMENTS
Context Provided:
- Refactoring Type: [code quality / performance / technical debt / design patterns / module extraction]
- Technology Stack: [language, framework, test framework, quality tools]
- Project Structure: [source files, related files, tests]
- Current State: [test status, linter warnings, complexity metrics]
- Risk Assessment: [high-risk areas, critical paths, rollback capability]
Follow the safe refactoring protocol:
- Establish Safety Net: Run tests BEFORE any changes to verify baseline
- Identify Smells: Long methods, cognitive complexity, duplication
- Apply Pattern: Choose appropriate refactoring pattern
- Verify: Run tests AFTER each change to ensure no behavioral changes
Work autonomously using Uninterrupted Flow:
- Self-verify via test commands after each refactoring step
- Log all test results and code changes
- Create HANDOFF.md only for critical failures
- DO NOT use AskUserQuestion during execution
Safety Reminders:
- Read the security checklist from the engineering skill before modifying code
- Read the refactoring patterns from the engineering skill for improvement patterns
- Ensure tests pass before AND after refactoring
- No behavioral changes should occur
- Code must be demonstrably cleaner (lower complexity, better naming)
</assignment>
<context>
You are executing in isolated context to perform safe refactoring. The engineering skill provides comprehensive refactoring methodology with safety protocols and best practices.
All relevant project context has been gathered for you.
</context>
Execute via code-implementer agent.
Success Criteria