Safe Refactoring
Requires: Claude Code Agent Teams feature.
Architect-planned, reviewer-validated code restructuring with per-step compilation checks.
Arguments: $ARGUMENTS
Instructions
- •
Get the recipe: Call
mcp__mira__recipe(ormcp__plugin_mira_mira__recipe) tool:coderecipe(action="get", name="refactor")
- •
Parse arguments (optional):
- •Any text -> use as the refactoring goal/context for the architect
- •
Determine context: What code to refactor, the goal of the restructuring, and any constraints. If no context is obvious, ask the user what they'd like refactored.
Phase 1: Analysis
- •
Create the team:
codeTeamCreate(team_name="refactor-{timestamp}") - •
Spawn architect using
Tasktool:codeTask( subagent_type="general-purpose", name="architect", team_name="refactor-{timestamp}", prompt=architect_prompt + "\n\n## Refactoring Goal\n\n" + user_context, run_in_background=true )IMPORTANT: Do NOT use
mode="bypassPermissions"— the architect is read-only. - •
Create and assign the analysis task using
TaskCreate+TaskUpdate. - •
Wait for the architect to report via SendMessage.
Phase 2: Validation
- •
Spawn code-reviewer using
Tasktool (same params,run_in_background=true, nobypassPermissions). - •
Send the architect's plan to code-reviewer via
SendMessageso they have the specific plan to validate. - •
Create and assign the validation task.
- •
Wait for code-reviewer to report via SendMessage. Then shut down both analyst agents.
Phase 3: Synthesis
- •
Combine architect's plan with code-reviewer's feedback:
- •If code-reviewer found missing callers or risks, incorporate them
- •If rated "needs revision" or "too risky", revise accordingly
- •
Present the validated refactoring plan to the user and WAIT for approval.
Phase 4: Implementation
- •
Execute the refactoring steps yourself. For each step:
- •Make the changes
- •Run
cargo test --no-runto verify compilation (NEVER use --release) - •If it doesn't compile, fix before moving to the next step
- •
For large refactors (5+ files), spawn implementation agents with
mode="bypassPermissions":- •Group steps by file ownership to avoid conflicts
- •Max 3 steps per agent
- •Each agent verifies with
cargo test --no-run
Phase 5: Verification
- •Run
cargo testto verify all tests pass (NEVER use --release). - •For large refactors, optionally spawn test-runner for parallel clippy + fmt + dead code checks.
- •If tests fail: fix regressions or update test imports/paths.
Phase 6: Finalize
- •Report summary of structural changes to the user.
- •Cleanup:
TeamDelete
Examples
/mira:refactor -> Prompts for what to refactor, then runs the full analysis -> validation -> implementation cycle /mira:refactor Split the database module into separate files per concern -> Architect analyzes the DB module, plans the split, code-reviewer validates, then implements /mira:refactor Rename the "watcher" module to "file_monitor" across the codebase -> Safe rename with caller analysis and per-step compilation checks
Agent Roles
| Phase | Agent | Focus |
|---|---|---|
| Analysis | architect | Map current structure, design target, plan migration steps |
| Validation | code-reviewer | Verify completeness, check for hidden behavior changes |
| Verification | test-runner | Run tests, clippy, fmt, check for dead code (large refactors only) |