Feature: Revert-mode re-execution from decision point
  As a plan correction user
  I want revert mode to perform real checkpoint restoration, actor state
  recovery, and re-execution signalling from the targeted decision point
  So that the plan can resume Strategize with the corrected guidance

  # ── Checkpoint restoration ─────────────────────────────────────

  Scenario: Revert with checkpoint service restores sandbox checkpoint
    Given revexec a correction service with a mock checkpoint service
    And revexec a checkpoint exists for plan "P1" decision "D1" with ref "abc123"
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction
    Then revexec the result checkpoint_restored should be true
    And revexec the result status should be "applied"

  Scenario: Revert without checkpoint service skips checkpoint restoration
    Given revexec a correction service without checkpoint service
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction
    Then revexec the result checkpoint_restored should be false
    And revexec the result status should be "applied"

  Scenario: Revert with no matching checkpoint skips restoration
    Given revexec a correction service with a mock checkpoint service
    And revexec no checkpoint exists for plan "P1" decision "D1"
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction
    Then revexec the result checkpoint_restored should be false

  # ── Actor state restoration ────────────────────────────────────

  Scenario: Revert extracts actor_state_ref from target decision
    Given revexec a correction service without checkpoint service
    And revexec a decision "D1" with actor_state_ref "lg-checkpoint-42"
    And revexec a correction request targeting decision "D1" in plan "P1" for revert with decisions
    When revexec I execute the revert correction with decisions
    Then revexec the result actor_state_ref should be "lg-checkpoint-42"

  Scenario: Revert without decisions mapping returns empty actor_state_ref
    Given revexec a correction service without checkpoint service
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction
    Then revexec the result actor_state_ref should be ""

  # ── Phase transition ───────────────────────────────────────────

  Scenario: Revert signals phase transition to strategize
    Given revexec a correction service without checkpoint service
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction
    Then revexec the result phase_transition_target should be "strategize"

  # ── User intervention decision ─────────────────────────────────

  Scenario: Revert creates a user_intervention decision ID
    Given revexec a correction service without checkpoint service
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction
    Then revexec the result user_intervention_decision_id should not be empty

  # ── Guidance injection ─────────────────────────────────────────

  Scenario: Revert with guidance propagates to attempt details
    Given revexec a correction service without checkpoint service
    And revexec a correction request targeting decision "D1" in plan "P1" for revert with guidance "Prioritize payments"
    When revexec I execute the revert correction
    Then revexec the result status should be "applied"
    And revexec the attempt details should contain phase_transition_target

  # ── Full re-execution flow ─────────────────────────────────────

  Scenario: Full revert flow with checkpoint and actor state
    Given revexec a correction service with a mock checkpoint service
    And revexec a checkpoint exists for plan "P1" decision "D1" with ref "commit-sha-1"
    And revexec a decision "D1" with actor_state_ref "lg-state-99"
    And revexec a decision tree where "D1" has children "D2,D3"
    And revexec a correction request targeting decision "D1" in plan "P1" for revert with decisions
    When revexec I execute the revert correction with decisions and tree
    Then revexec the result checkpoint_restored should be true
    And revexec the result actor_state_ref should be "lg-state-99"
    And revexec the result phase_transition_target should be "strategize"
    And revexec the result user_intervention_decision_id should not be empty
    And revexec the reverted decisions should contain "D1"
    And revexec the reverted decisions should contain "D2"
    And revexec the reverted decisions should contain "D3"

  # ── Subtree selectivity ────────────────────────────────────────

  Scenario: Revert only affects downstream decisions from target
    Given revexec a correction service without checkpoint service
    And revexec a two-level tree "ROOT" children "D1,D2" then "D1" children "D3"
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction with tree
    Then revexec the reverted decisions should contain "D1"
    And revexec the reverted decisions should contain "D3"
    And revexec the reverted decisions should not contain "ROOT"
    And revexec the reverted decisions should not contain "D2"

  # ── Dispatch integration ───────────────────────────────────────

  Scenario: Execute correction dispatch passes decisions to revert
    Given revexec a correction service without checkpoint service
    And revexec a decision "D1" with actor_state_ref "dispatch-state-ref"
    And revexec a correction request targeting decision "D1" in plan "P1" for revert with decisions
    When revexec I dispatch execute_correction with decisions
    Then revexec the result actor_state_ref should be "dispatch-state-ref"
    And revexec the result phase_transition_target should be "strategize"

  # ── Error paths ─────────────────────────────────────────────────

  Scenario: Revert handles checkpoint list failure gracefully
    Given revexec a correction service with a failing checkpoint list
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction
    Then revexec the result checkpoint_restored should be false
    And revexec the result status should be "applied"

  Scenario: Revert handles checkpoint rollback failure gracefully
    Given revexec a correction service with a failing checkpoint rollback
    And revexec a correction request targeting decision "D1" in plan "P1" for revert
    When revexec I execute the revert correction
    Then revexec the result checkpoint_restored should be false
    And revexec the result status should be "applied"

  Scenario: Revert with decision not in mapping returns empty actor state
    Given revexec a correction service without checkpoint service
    And revexec a decision "OTHER" with actor_state_ref "lg-state-miss"
    And revexec a correction request targeting decision "D1" in plan "P1" for revert with decisions
    When revexec I execute the revert correction with decisions
    Then revexec the result actor_state_ref should be ""

  # ── CorrectionResult model fields ──────────────────────────────

  Scenario: CorrectionResult default revert fields
    When revexec I create a CorrectionResult with only correction_id and status
    Then revexec the result checkpoint_restored should be false
    And revexec the result actor_state_ref should be ""
    And revexec the result user_intervention_decision_id should be ""
    And revexec the result phase_transition_target should be ""
