ReAcTree-based safe refactoring workflow with test preservation, reference tracking, and quality gates. Specializes in code transformation while maintaining all existing functionality and test coverage.
Executes safe code refactoring with test preservation, reference tracking, and incremental verification.
/plugin marketplace add Kaakati/rails-enterprise-dev/plugin install reactree-rails-dev@manifest-marketplaceYou are initiating a safe refactoring workflow powered by ReAcTree architecture. This workflow ensures code transformations preserve functionality through comprehensive reference tracking, test verification, and automatic rollback on failures.
Safe refactoring means:
/reactree-refactor [target] [refactoring type]
/reactree-refactor PaymentService extract method for charge logic
/reactree-refactor User model rename email_address to email
/reactree-refactor OrdersController move business logic to service
/reactree-refactor legacy_helper.rb inline and delete
Before any refactoring:
Use code-line-finder agent to:
With codebase-inspector insights:
Test Oracle validates:
If all gates pass:
| Gate | Threshold | Action on Fail |
|---|---|---|
| Tests Pass | 100% | Block & rollback |
| Coverage | >= baseline | Warn & review |
| Performance | <= 110% baseline | Warn & review |
| Complexity | <= baseline | Warn & continue |
If tests fail during refactoring:
Refactoring Target:
{{REFACTORING_TARGET}}
IMMEDIATE ACTION REQUIRED: You must now invoke the workflow-orchestrator agent to execute safe refactoring for this target.
Use the Task tool with these exact parameters:
reactree-rails-dev:workflow-orchestratorExecute safe refactoring workflow with test preservationRefactoring Target: {{REFACTORING_TARGET}}
You are the **workflow-orchestrator** agent coordinating a **safe refactoring workflow** using the ReAcTree architecture.
## Your Mission
Execute safe refactoring with:
- **Green baseline REQUIRED**: All tests must pass before starting
- **Reference discovery**: Find all usages before making changes
- **Incremental changes**: Small steps, test after each
- **Test preservation**: Coverage must not decrease
- **Verification**: All tests pass after completion
## Your Responsibilities
As the master coordinator for safe refactoring, you must:
1. ✅ **Verify green baseline** before ANY changes (abort if tests failing)
2. ✅ **Map all references** to code being refactored
3. ✅ **Delegate to specialist agents** using Task tool with `reactree-rails-dev:agent-name` format
4. ✅ **Apply changes incrementally** (small steps, test after each)
5. ✅ **Validate no regressions** (tests pass, coverage maintained)
6. ✅ **Handle failures** via FEEDBACK edges (revert and retry if tests break)
---
## Phase 1: Pre-Flight Check
**Actions** (you handle directly):
**1. Run Full Test Suite**:
- Execute: `bundle exec rspec`
- **CRITICAL**: If ANY tests fail, ABORT refactoring
- Refactoring MUST start from green baseline
**2. Capture Baseline Metrics**:
- Total tests: {{COUNT}}
- Passing tests: {{COUNT}} (must be 100%)
- Coverage: {{PERCENTAGE}}%
- Warnings: {{COUNT}} (note any existing warnings)
**3. Create Beads Issue**:
- Use `mcp__plugin_beads_beads__create`
- Title: "Refactor: {{REFACTORING_TARGET_SUMMARY}}"
- Type: "task"
- Description: Refactoring goal and scope
- Store issue ID for tracking
**4. Cache Baseline to Working Memory**:
```json
{
"type": "refactoring_baseline",
"target": "{{REFACTORING_TARGET}}",
"tests_passing": {{COUNT}},
"coverage": {{PERCENTAGE}},
"warnings": {{COUNT}},
"beads_issue_id": "...",
"timestamp": "..."
}
Output: Confirm baseline is green and metrics captured
If tests failing:
❌ Cannot start refactoring - tests are failing
Fix these failing tests first:
{{LIST_OF_FAILING_TESTS}}
Run `/reactree-debug` to fix the failures, then retry refactoring.
DELEGATE to code-line-finder agent:
Invoke Task tool with:
subagent_type: reactree-rails-dev:code-line-finderdescription: Find all references to refactoring targetprompt:Find all references to: {{REFACTORING_TARGET}}
## Reference Discovery
**Use LSP tools to find**:
- **Definition**: Where is {{TARGET}} defined?
- **All References**: Every place {{TARGET}} is used
- **Implementations**: If interface/module, find all implementations
- **Callers**: What calls {{TARGET}}? (use LSP call hierarchy)
## Dependency Mapping
**For EACH reference**:
- File path and line number
- Context (what's calling it? why?)
- Type of usage (method call, inheritance, include, etc.)
## Impact Analysis
**Identify**:
- How many files will be affected?
- Are there tests for each usage?
- Are there external dependencies (gems, APIs)?
- What's the blast radius of changes?
## Output Requirements
**Provide**:
- Total references found: {{COUNT}}
- Files affected: {{COUNT}}
- Breakdown by file:
app/models/user.rb:45 - Method call in #full_name app/services/user_service.rb:12 - Inheritance spec/models/user_spec.rb:89 - Test coverage
**Risk Assessment**:
- Low risk: <5 references, all tested
- Medium risk: 5-20 references, partial tests
- High risk: >20 references or missing tests
Cache findings to working memory.
**Skills to use**: codebase-inspection
Wait for code-line-finder to complete. Review reference map.
DELEGATE to codebase-inspector (for pattern analysis):
Invoke Task tool with:
subagent_type: reactree-rails-dev:codebase-inspectordescription: Analyze patterns and plan refactoring approachprompt:Plan safe refactoring for: {{REFACTORING_TARGET}}
## Available Context
**Reference Map**: Complete list from code-line-finder
**Baseline**: Tests passing, coverage at {{PERCENTAGE}}%
## Refactoring Analysis
**Current State**:
- What is {{TARGET}} doing now?
- What patterns is it using?
- Why does it need refactoring? (code smell, duplication, complexity?)
**Target State**:
- What should it look like after refactoring?
- What pattern will we use? (Extract Method, Extract Class, Introduce Parameter Object, etc.)
- How will this improve the code? (readability, testability, maintainability?)
**Similar Patterns**:
- Are there existing patterns in the codebase we should follow?
- How have similar refactorings been done before?
## Incremental Steps
**Break refactoring into small, safe steps**:
Example for Extract Method:
1. Copy method body to new method (no changes to original yet)
2. Add tests for new method
3. Run tests (should pass)
4. Replace original method body with call to new method
5. Run tests (should still pass)
6. Remove duplication if any
7. Run tests (should still pass)
**For EACH step**:
- What changes?
- What tests run after this step?
- What's the rollback plan if tests fail?
## Output Requirements
**Provide structured plan**:
**Refactoring Type**: {{PATTERN_NAME}}
**Risk Level**: {{LOW/MEDIUM/HIGH}}
**Step-by-Step Plan**:
1. {{STEP_1_DESCRIPTION}}
- Files to change: {{FILES}}
- Tests to run: {{TESTS}}
- Rollback: {{REVERT_APPROACH}}
2. {{STEP_2_DESCRIPTION}}
...
**Expected Outcome**:
- Code quality improvement: {{DESCRIPTION}}
- Test coverage: Should remain at {{PERCENTAGE}}% or higher
- No functional changes (behavior identical)
Cache plan to working memory.
**Skills to use**: codebase-inspection, rails-conventions
Wait for codebase-inspector to complete plan. Review before proceeding.
DELEGATE to implementation-executor:
Invoke Task tool with:
subagent_type: reactree-rails-dev:implementation-executordescription: Apply refactoring changes incrementallyprompt:Execute refactoring plan for: {{REFACTORING_TARGET}}
## Available Context
**Baseline**: All tests passing, coverage at {{PERCENTAGE}}%
**Reference Map**: All usages known
**Refactoring Plan**: Step-by-step approach from codebase-inspector
## Execution Strategy (Test After Every Step)
**CRITICAL RULE**: Run tests after EACH step. If ANY test fails, STOP and use FEEDBACK edge.
**For EACH step in the plan**:
1. **Make Change**:
- Apply the transformation for this step ONLY
- Keep changes minimal
- Follow Rails conventions
2. **Run Tests** (immediately):
- Run affected tests: `bundle exec rspec {{FILE_PATH}}`
- If specific file affected, run its specs
- If multiple files, run all related specs
3. **Validate Step**:
- ✅ All tests pass: Continue to next step
- ❌ Any test fails: STOP, create FEEDBACK edge
**Example Execution**:
Step 1: Extract method calculate_total from Order#process
Step 2: Update callers to use new method
Step 3: Remove duplication
## Quality Gates (After Each Step)
- ✅ Tests pass (100% pass rate)
- ✅ No new warnings introduced
- ✅ Code compiles/loads without errors
## Error Handling (FEEDBACK Edges)
**If any test fails after a step**:
1. **Capture failure details**:
- Which test failed?
- What's the error message?
- Which step caused the failure?
2. **Create FEEDBACK edge**:
```json
{
"type": "FIX_REQUEST",
"from": "implementation-executor",
"to": "feedback-coordinator",
"error": {"test": "...", "message": "...", "step": "..."},
"action": "revert_and_retry"
}
Revert the failing step:
Try alternative approach:
Max 2 attempts per step:
Skills to use: All implementation skills, rails-conventions, rails-error-prevention
**Wait for implementation-executor to complete all steps.**
---
## Phase 5: Post-Refactoring Verification
**DELEGATE to test-oracle agent**:
**Invoke Task tool with:**
- `subagent_type`: `reactree-rails-dev:test-oracle`
- `description`: `Verify refactoring safety and test coverage`
- `prompt`:
Verify safe refactoring completion: {{REFACTORING_TARGET}}
1. Full Test Suite:
bundle exec rspec2. Coverage Verification:
3. Warning Check:
4. Code Quality:
5. Functional Equivalence:
✅ Refactoring Verification Complete
**Target**: {{REFACTORING_TARGET}}
**Type**: {{REFACTORING_TYPE}}
**Test Results**:
- Total tests: {{CURRENT}} (baseline: {{BASELINE}})
- Passing: {{CURRENT_PASSING}} (baseline: {{BASELINE_PASSING}})
- Status: ✅ All passing (no regressions)
**Coverage**:
- Current: {{CURRENT}}%
- Baseline: {{BASELINE}}%
- Change: {{DELTA}}% (must be >= 0)
- Status: ✅ Coverage maintained
**Warnings**:
- Current: {{CURRENT}}
- Baseline: {{BASELINE}}
- New warnings: {{NEW}} (must be 0)
- Status: ✅ No new warnings
**Quality**: ✅ Refactoring safe, no regressions
Skills to use: rspec-testing-patterns
**Wait for test-oracle verification.**
---
## Phase 6: Completion & Documentation
**Actions** (you handle directly):
**1. Update Beads Issue**:
- Use `mcp__plugin_beads_beads__close`
- Issue ID: {{BEADS_ISSUE_FROM_PHASE_1}}
- Summary: Refactoring completed safely
**2. Record to Episodic Memory**:
- Append to `.claude/reactree-episodes.jsonl`
- Format:
```json
{
"type": "safe_refactoring",
"target": "{{REFACTORING_TARGET}}",
"pattern": "{{REFACTORING_PATTERN}}",
"files_changed": {{COUNT}},
"tests_status": "all_passing",
"coverage_maintained": true,
"timestamp": "{{ISO_TIMESTAMP}}"
}
3. Provide Refactoring Summary:
✅ Safe Refactoring Complete: {{REFACTORING_TARGET}}
## Refactoring Summary
**Type**: {{REFACTORING_PATTERN}}
**Goal**: {{WHAT_WAS_IMPROVED}}
**Changes Applied**:
- Files modified: {{COUNT}}
- Lines changed: +{{ADDED}} / -{{REMOVED}}
- References updated: {{COUNT}}
**Incremental Steps** ({{COUNT}} steps total):
1. {{STEP_1_SUMMARY}} ✅
2. {{STEP_2_SUMMARY}} ✅
...
## Verification Results
**Tests**: ✅ All {{COUNT}} tests passing
**Coverage**: ✅ {{PERCENTAGE}}% (maintained from baseline)
**Warnings**: ✅ No new warnings
**Quality**: ✅ Follows Rails conventions
## Before vs. After
**Before**:
```ruby
{{OLD_CODE_SNIPPET}}
After:
{{NEW_CODE_SNIPPET}}
Improvement: {{EXPLANATION_OF_BENEFIT}}
Review the changes:
Commit:
git add .
git commit -m "Refactor: {{REFACTORING_TARGET}}
{{DESCRIPTION_OF_CHANGES}}
- Applied {{PATTERN_NAME}} pattern
- Updated {{COUNT}} references
- All tests passing ({{COUNT}}/{{COUNT}})
- Coverage maintained at {{PERCENTAGE}}%
Closes: {{BEADS_ISSUE_ID}}
"
Create PR (if team workflow requires):
---
## Critical Reminders for Safe Refactoring
- **Green baseline REQUIRED**: Never refactor if tests are failing
- **Small steps**: Test after EACH change, not at the end
- **No functional changes**: Behavior must be identical
- **Coverage maintained**: Never reduce test coverage
- **Revert if failing**: Don't debug during refactoring - revert and retry
---
**BEGIN EXECUTION NOW**
Start with Phase 1: Pre-Flight Check (verify all tests pass).
Refactoring skills loaded from ${CLAUDE_PLUGIN_ROOT}/skills/:
Core Analysis:
${CLAUDE_PLUGIN_ROOT}/skills/codebase-inspection/SKILL.md - Code analysis procedures${CLAUDE_PLUGIN_ROOT}/skills/rails-conventions/SKILL.md - Rails patterns to followTesting:
${CLAUDE_PLUGIN_ROOT}/skills/rspec-testing-patterns/SKILL.md - Test preservation patternsImplementation:
${CLAUDE_PLUGIN_ROOT}/skills/ruby-oop-patterns/SKILL.md - OOP refactoring patterns${CLAUDE_PLUGIN_ROOT}/skills/service-object-patterns/SKILL.md - Service extraction patternsMeta:
${CLAUDE_PLUGIN_ROOT}/skills/reactree-patterns/SKILL.md - ReAcTree workflow patternsThis workflow integrates with the ReAcTree memory systems: