Get the FREE Ultimate OpenClaw Setup Guide →

Refactor

npx machina-cli add skill ConaryLabs/Mira/refactor --openclaw
Files (1)
SKILL.md
5.3 KB
<!-- plugin/skills/refactor/SKILL.md -->

name: refactor description: This skill should be used when the user asks to "refactor this", "restructure code", "reorganize modules", "rename and move", "safe restructuring", "reorganize code", "move and rename", or wants to reorganize code without changing behavior. argument-hint: "[refactoring goal]" disable-model-invocation: true

Safe Refactoring

Requires: Claude Code Agent Teams feature.

Architect-planned, reviewer-validated code restructuring with per-step compilation checks.

Arguments: $ARGUMENTS

Instructions

  1. Parse arguments (optional):

    • Any text -> use as the refactoring goal/context for the architect
  2. 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

  1. Launch for analysis: Call the Mira launch MCP tool to get the architect agent spec:

    launch(team="refactor-team", scope=user_context, members="atlas")
    
  2. Create the team:

    TeamCreate(team_name=result.data.suggested_team_id)
    
  3. Create and assign the analysis task:

    TaskCreate(subject=atlas_agent.task_subject, description=atlas_agent.task_description)
    TaskUpdate(taskId=id, owner="atlas", status="in_progress")
    
  4. Spawn Atlas (architect) using Task tool:

    Task(
      subagent_type="general-purpose",
      name="atlas",
      model=atlas_agent.model,
      team_name=result.data.suggested_team_id,
      prompt=atlas_agent.prompt + "\n\n## Refactoring Goal\n\n" + user_context,
      run_in_background=true
    )
    

    IMPORTANT: Do NOT use mode="bypassPermissions" -- Atlas is read-only. IMPORTANT: Always pass model="sonnet" to the Task tool. This ensures read-only agents use a cost-efficient model.

  5. Wait for Atlas to report via SendMessage.


Phase 2: Validation

  1. Launch for validation: Call launch to get the reviewer agent spec:

    launch(team="refactor-team", scope=user_context, members="iris")
    
  2. Spawn Iris (safety reviewer) using Task tool:

    Task(
      subagent_type="general-purpose",
      name="iris",
      model=iris_agent.model,
      team_name=result.data.suggested_team_id,
      prompt=iris_agent.prompt + "\n\n## Refactoring Plan\n\n" + atlas_plan,
      run_in_background=true
    )
    

    IMPORTANT: Do NOT use mode="bypassPermissions" -- Iris is read-only. IMPORTANT: Always pass model="sonnet" to the Task tool. This ensures read-only agents use a cost-efficient model.

  3. Send Atlas's plan to Iris via SendMessage so she has the specific plan to validate.

  4. Create and assign the validation task.

  5. Wait for Iris to report via SendMessage. Then shut down both analyst agents.


Phase 3: Synthesis

  1. Combine Atlas's plan with Iris's feedback:

    • If Iris found missing callers or risks, incorporate them
    • If rated "needs revision" or "too risky", revise accordingly
  2. Present the validated refactoring plan to the user and WAIT for approval.


Phase 4: Implementation

  1. Execute the refactoring steps yourself. For each step:

    • Make the changes
    • Run cargo test --no-run to verify compilation (NEVER use --release)
    • If it doesn't compile, fix before moving to the next step
  2. For large refactors (5+ files), launch implementation agents:

    launch(team="refactor-team", scope=implementation_plan, members="ash")
    

    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

  1. Launch for verification: Call launch to get the verifier agent spec:

    launch(team="refactor-team", scope=summary_of_changes, members="ash")
    
  2. Spawn Ash (build verifier) using Task tool:

    Task(
      subagent_type="general-purpose",
      name="ash",
      model=ash_agent.model,
      team_name=result.data.suggested_team_id,
      prompt=ash_agent.prompt + "\n\n## Changes Made\n\n" + summary_of_changes,
      run_in_background=true,
      mode="bypassPermissions"
    )
    
  3. Wait for Ash to report. If tests fail: fix regressions or update test imports/paths.


Phase 6: Finalize

  1. Report summary of structural changes to the user.
  2. Cleanup: Send shutdown_request to remaining agents, then 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
-> Atlas analyzes the DB module, plans the split, Iris 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

PhaseAgentFocus
AnalysisAtlas (architect)Map current structure, design target, plan migration steps
ValidationIris (safety reviewer)Verify completeness, check for hidden behavior changes
VerificationAsh (build verifier)Run tests, compilation, linters between steps

Source

git clone https://github.com/ConaryLabs/Mira/blob/main/plugin/skills/refactor/SKILL.mdView on GitHub

Overview

Safe Refactoring orchestrates architect-planned code restructuring with per-step validation. It coordinates Atlas (architect) and Iris (safety reviewer) to ensure behavior remains intact while reorganizing the codebase.

How This Skill Works

Phase 1 analyzes the refactor goal and launches Atlas to produce a formal plan. Phase 2 validates the plan with Iris, incorporating reviewer feedback. Phase 3 implements changes step-by-step, verifying compilation after each step with cargo test --no-run, then final verification before completion.

When to Use It

  • You want to refactor or restructure code without changing behavior.
  • You need to rename or move modules/files safely while preserving API.
  • Refactoring spans multiple files (5+), requiring coordinated team effort.
  • You want a safety review and validation by Iris before finalizing.
  • You require per-step compilation checks after each change (cargo test --no-run).

Quick Start

  1. Step 1: Provide the refactoring goal/context (parse arguments) to guide the architect.
  2. Step 2: Launch Atlas to generate a plan: create team, tasks, and spawn Atlas with the goal.
  3. Step 3: Validate with Iris, synthesize feedback, implement changes, and verify with cargo test --no-run after each step.

Best Practices

  • Clearly define the refactoring goal/arguments at the start to guide Atlas.
  • Use Atlas for architected planning and Iris for independent validation.
  • Break changes into small, auditable steps grouped by file ownership.
  • After each change, run cargo test --no-run to verify compilation before moving on.
  • Do not bypass read-only constraints; ensure proper approvals and validations throughout.

Example Use Cases

  • Refactoring a monolith into modular components with Atlas/Iris oversight to preserve behavior.
  • Renaming and moving modules while maintaining public API compatibility.
  • Reorganizing a feature’s code paths to improve maintainability with staged validation.
  • Splitting a large module into smaller files and validating dependency changes incrementally.
  • Coordinating a multi-file refactor across teams with staged implementation and verification.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers