Feature: Phase Reversion State Machine
  As a plan lifecycle manager
  I want to revert plans to earlier phases
  So that plans can be re-strategized when constraints prove unworkable

  Background:
    Given a plan lifecycle service is initialized

  Scenario: Auto-reversion from constrained Apply to Strategize
    Given an action "local/test-action" exists
    And a plan is created from action "local/test-action"
    And the plan is advanced to Apply phase with constrained state
    And the plan has automation profile "ci" allowing auto-reversion
    When auto-reversion from apply is attempted with reason "constraints too strict"
    Then the plan should be in Strategize phase
    And the plan processing state should be queued
    And the plan reversion count should be 1
    And a reversion decision should be recorded with source "apply" and target "strategize"

  Scenario: Manual reversion via revert_plan method
    Given an action "local/manual-revert" exists
    And a plan is created from action "local/manual-revert"
    And the plan is advanced to Execute phase
    When the plan is manually reverted to Strategize with reason "need different strategy"
    Then the plan should be in Strategize phase
    And the plan processing state should be queued
    And the plan reversion count should be 1
    And a reversion decision should be recorded with reason "need different strategy"

  Scenario: Loop guard prevents more than 3 reversions
    Given an action "local/loop-guard" exists
    And a plan is created from action "local/loop-guard"
    And the plan has been reverted 3 times
    When a reversion is attempted on the maxed-out plan
    Then a PlanError should be raised about max reversions

  Scenario: Auto-reversion blocked by manual profile threshold
    Given an action "local/profile-gate" exists
    And a plan is created from action "local/profile-gate"
    And the plan is advanced to Apply phase with constrained state
    And the plan has automation profile "manual" blocking auto-reversion
    When auto-reversion from apply is attempted with reason "profile blocks it"
    Then the plan should remain in Apply phase
    And the plan processing state should be constrained

  Scenario: Auto-reversion from Execute to Strategize
    Given an action "local/exec-revert" exists
    And a plan is created from action "local/exec-revert"
    And the plan is advanced to Execute phase
    And the plan has automation profile "ci" allowing auto-reversion
    When auto-reversion from execute is attempted with reason "validation failures"
    Then the plan should be in Strategize phase
    And the plan processing state should be queued
    And the plan reversion count should be 1

  Scenario: Invalid reversion on terminal plan (applied)
    Given an action "local/terminal-plan" exists
    And a plan is created from action "local/terminal-plan"
    And the plan has reached applied terminal state
    When a reversion is attempted on the terminal plan
    Then a PlanError should be raised about terminal state

  Scenario: Invalid reversion to wrong phase
    Given an action "local/wrong-phase" exists
    And a plan is created from action "local/wrong-phase"
    And the plan is in Strategize phase
    When a reversion to Apply phase is attempted
    Then an InvalidPhaseTransitionError should be raised

  Scenario: Reversion preserves plan context
    Given an action "local/context-preserve" exists
    And a plan is created from action "local/context-preserve" with arguments
    And the plan is advanced to Execute phase
    When the plan is manually reverted to Strategize with reason "context check"
    Then the plan arguments should be preserved
    And the plan project links should be preserved
    And the plan invariants should be preserved

  Scenario: Multiple reversions tracked in decisions list
    Given an action "local/multi-revert" exists
    And a plan is created from action "local/multi-revert"
    And the plan is advanced to Execute phase
    When the plan is manually reverted to Strategize with reason "first reversion"
    And the plan is re-advanced to Execute phase
    And the plan is manually reverted to Strategize with reason "second reversion"
    Then the plan reversion count should be 2
    And there should be 2 reversion decisions recorded

  Scenario: can_revert_to validates reversion paths
    Given an action "local/revert-validation" exists
    And a plan is created from action "local/revert-validation"
    Then the plan in Strategize phase cannot revert to Apply
    And the plan in Strategize phase cannot revert to Action
    And the plan in Execute phase can revert to Strategize
    And the plan in Apply phase can revert to Strategize

  Scenario: Auto-reversion from Execute blocked by profile threshold
    Given an action "local/exec-blocked" exists
    And a plan is created from action "local/exec-blocked"
    And the plan is advanced to Execute phase
    And the plan has automation profile "manual" blocking auto-reversion
    When auto-reversion from execute is attempted with reason "blocked by profile"
    Then the plan should remain in Execute phase

  Scenario: Cancelled plan cannot be reverted
    Given an action "local/cancelled-plan" exists
    And a plan is created from action "local/cancelled-plan"
    And the plan has been cancelled
    When a reversion is attempted on the cancelled plan
    Then a PlanError should be raised about terminal state
