@unit
Feature: agents plan correct with revert and append correction modes
  As a user
  I want to correct plan decisions using revert and append modes
  So that I can fix suboptimal LLM choices without losing all downstream work

  Background:
    Given a plan correction service
    And a plan with ID "plan-001" and root decision "D1"
    And a decision tree with decisions "D1->D2,D3;D2->D4"

  Scenario: Revert mode prunes decision tree and re-executes from target
    When I invoke plan correct with plan_id "plan-001" decision_id "D2" mode "revert"
    Then the correction should succeed
    And decisions "D3,D4" should be pruned from the tree
    And the LLM should be re-executed from decision "D2"
    And the plan status should transition to "correcting" then "active"

  Scenario: Append mode adds guidance without re-executing
    Given I have guidance "Use FastAPI instead"
    When I invoke plan correct with plan_id "plan-001" decision_id "D2" mode "append"
    Then the correction should succeed
    And the guidance should be appended to decision "D2" context
    And no decisions should be pruned
    And the LLM should not be re-executed
    And the plan status should remain "active"

  Scenario: Revert mode shows confirmation prompt
    Given the --yes flag is not set
    When I invoke plan correct with plan_id "plan-001" decision_id "D2" mode "revert"
    Then a confirmation prompt should be displayed
    And the prompt should warn about pruning decisions "D3,D4"
    And the prompt should ask for user confirmation

  Scenario: Revert mode skips confirmation with --yes flag
    Given the --yes flag is set
    When I invoke plan correct with plan_id "plan-001" decision_id "D2" mode "revert"
    Then no confirmation prompt should be displayed
    And the correction should proceed immediately

  Scenario: Both modes validate plan and decision IDs
    When I invoke plan correct with plan_id "nonexistent" decision_id "D1" mode "revert"
    Then an error should be raised indicating plan not found
    When I invoke plan correct with plan_id "plan-001" decision_id "nonexistent" mode "revert"
    Then an error should be raised indicating decision not found

  Scenario: Both modes reject non-correctable plan states
    Given a plan with status "applying"
    When I invoke plan correct with plan_id "plan-001" decision_id "D1" mode "revert"
    Then an error should be raised indicating plan is not correctable
    Given a plan with status "applied"
    When I invoke plan correct with plan_id "plan-001" decision_id "D1" mode "append"
    Then an error should be raised indicating plan is not correctable

  Scenario: Updated decision tree is persisted to database
    When I invoke plan correct with plan_id "plan-001" decision_id "D2" mode "revert"
    Then the updated decision tree should be persisted to the database
    And agents plan tree should show the corrected tree

  Scenario: Dry-run mode analyzes impact without executing
    Given the --dry-run flag is set
    When I invoke plan correct with plan_id "plan-001" decision_id "D2" mode "revert"
    Then the impact analysis should be displayed
    And the correction should report affected decisions "D2,D3,D4"
    And the risk level should be calculated
    And no changes should be made to the plan
