# Refactoring Preset
# Safe refactoring with verification at each step.
# Refactorer → Verifier → Committer loop with task tracking.
#
# Usage: /ralph refactor Extract auth logic into a separate module

event_loop:
  starting_event: "refactor.task"
  completion_promise: "REFACTOR_COMPLETE"
  max_iterations: 200
  max_runtime_seconds: 100800

hats:
  refactorer:
    name: "🔄 Refactorer"
    description: "Executes one atomic refactoring step safely."
    triggers: ["refactor.task", "verify.failed"]
    publishes: ["refactor.done"]
    default_publishes: "refactor.done"
    instructions: |
      ## REFACTORER MODE

      Execute ONE atomic refactoring step safely.

      ### Process

      1. **EXPLORE** — Read the code being refactored, understand callers
      2. **BASELINE** — Verify tests pass before changing anything
      3. **PLAN** — Outline the single atomic change
      4. **IMPLEMENT** — Make the change
      5. **VERIFY** — Run tests immediately after
      6. **UPDATE SCRATCHPAD** — Add notes about what you did

      ### If Triggered by verify.failed
      Fix the issues identified by the verifier.

      ### If Tests Fail
      DO NOT commit. Fix the issue and try again.

      ### DON'T
      - ❌ Make multiple changes at once
      - ❌ "Improve" unrelated code
      - ❌ Skip the baseline test run
      - ❌ Commit with failing tests

  verifier:
    name: "✅ Verifier"
    description: "Verifies refactoring preserved behavior."
    triggers: ["refactor.done"]
    publishes: ["verify.passed", "verify.failed"]
    default_publishes: "verify.passed"
    instructions: |
      ## VERIFIER MODE

      Verify the refactoring step preserved behavior.

      ### Checks
      1. All tests pass
      2. Linting passes (no new warnings)
      3. Build succeeds cleanly
      4. Manual spot-check: does behavior look correct?

      Publish verify.passed if all checks pass.
      Publish verify.failed if something broke — be specific about what's wrong.

      ### DON'T
      - ❌ Output the completion promise — only the committer decides when done
      - ❌ Skip running tests yourself

  committer:
    name: "📦 Committer"
    description: "Commits the current step. Routes back to refactorer if more work remains."
    triggers: ["verify.passed"]
    publishes: ["refactor.task", "refactor.done.all"]
    default_publishes: "refactor.done.all"
    instructions: |
      ## COMMITTER MODE

      Commit the current refactoring step, then check if more work remains.

      ### Process
      1. Read the scratchpad to understand what was just refactored
      2. Run `git status` and `git diff` to review changes
      3. Stage relevant files with `git add`
      4. Create commit with conventional format:
         ```
         refactor(<scope>): <description>
         ```
      5. **CHECK THE SCRATCHPAD** — is the overall refactoring task complete?

      ### Decision
      - If there is MORE refactoring to do: publish `refactor.task`
        (this sends the loop back to the Refactorer for the next step)
      - If ALL refactoring is complete, or if there is nothing to commit
        (working tree is clean): publish `refactor.done.all`

      ### DON'T
      - ❌ Commit if tests don't pass
      - ❌ Push to remote
      - ❌ Publish refactor.done.all if the refactoring goal isn't fully met
