Feature: Plan CLI coverage round 2 – remaining uncovered lines
  As a developer
  I want to exercise the remaining uncovered lines in plan.py
  So that coverage gaps at lines 724, 737-741, 832-833, 879-883,
  930-931, 1007-1008, 1094-1095, 1108-1115, 1223-1245,
  1469-1481, 1565-1578, 2588-2598, 2616-2618, 2691-2762 are closed

  # ===================================================================
  # Legacy build command — >5 changes truncation (line 724)
  # ===================================================================

  Scenario: Legacy build shows truncation message when more than 5 changes
    Given a r2cov CLI runner
    And a mocked legacy container for r2cov build returning 8 changes
    When I invoke r2cov legacy build
    Then the r2cov command should exit normally
    And the r2cov output should contain "and 3 more"

  # ===================================================================
  # Legacy build command — PlanError handler (lines 737-738)
  # ===================================================================

  Scenario: Legacy build raises PlanError
    Given a r2cov CLI runner
    And a mocked legacy container for r2cov build that raises PlanError
    When I invoke r2cov legacy build
    Then the r2cov command should abort
    And the r2cov output should contain "Build Error"

  # ===================================================================
  # Legacy build command — CleverAgentsError handler (lines 740-741)
  # ===================================================================

  Scenario: Legacy build raises CleverAgentsError
    Given a r2cov CLI runner
    And a mocked legacy container for r2cov build that raises CleverAgentsError
    When I invoke r2cov legacy build
    Then the r2cov command should abort
    And the r2cov output should contain "Error"

  # ===================================================================
  # Legacy new command — ValidationError handler (lines 879-880)
  # ===================================================================

  Scenario: Legacy new raises ValidationError
    Given a r2cov CLI runner
    And a mocked legacy container for r2cov new that raises ValidationError
    When I invoke r2cov legacy new "bad-name"
    Then the r2cov command should abort
    And the r2cov output should contain "Validation Error"

  # ===================================================================
  # Legacy new command — CleverAgentsError handler (lines 882-883)
  # ===================================================================

  Scenario: Legacy new raises CleverAgentsError
    Given a r2cov CLI runner
    And a mocked legacy container for r2cov new that raises CleverAgentsError
    When I invoke r2cov legacy new "bad-name"
    Then the r2cov command should abort
    And the r2cov output should contain "Error"

  # ===================================================================
  # Legacy current command — CleverAgentsError handler (lines 930-931)
  # ===================================================================

  Scenario: Legacy current raises CleverAgentsError
    Given a r2cov CLI runner
    And a mocked legacy container for r2cov current that raises CleverAgentsError
    When I invoke r2cov legacy current
    Then the r2cov command should abort
    And the r2cov output should contain "Error"

  # ===================================================================
  # Legacy continue command — CleverAgentsError handler (lines 1094-1095)
  # ===================================================================

  Scenario: Legacy continue raises CleverAgentsError
    Given a r2cov CLI runner
    And a mocked legacy container for r2cov continue that raises CleverAgentsError
    When I invoke r2cov legacy continue
    Then the r2cov command should abort
    And the r2cov output should contain "Error"

  # ===================================================================
  # _get_lifecycle_service body (lines 1108-1115)
  # ===================================================================

  Scenario: _get_lifecycle_service creates a PlanLifecycleService
    Given a r2cov CLI runner
    And a mocked container for r2cov lifecycle service construction
    When I call r2cov _get_lifecycle_service
    Then the r2cov lifecycle service should be returned

  # ===================================================================
  # Multi-project scopes in _print_lifecycle_plan (lines 1223-1245)
  # ===================================================================

  Scenario: Lifecycle plan display with multi-project scopes and changeset summaries
    Given a r2cov CLI runner
    And a lifecycle plan with multi-project scopes for r2cov
    When I invoke r2cov _print_lifecycle_plan on the plan
    Then the r2cov output should contain "Multi-Project Scopes"
    And the r2cov output should contain "proj-alpha"
    And the r2cov output should contain "Changed:"

  Scenario: Multi-project scopes with validation passed and failed
    Given a r2cov CLI runner
    And a lifecycle plan with multi-project scopes with validation for r2cov
    When I invoke r2cov _print_lifecycle_plan on the plan
    Then the r2cov output should contain "Validation"
    And the r2cov output should contain "PASSED"

  # ===================================================================
  # use_action — execution_environment validation (lines 1469-1481)
  # ===================================================================

  Scenario: use_action rejects invalid execution_environment
    Given a r2cov CLI runner
    And a mocked lifecycle service for r2cov use action
    When I invoke r2cov use with execution-environment "invalid_env"
    Then the r2cov command should abort
    And the r2cov output should contain "Invalid execution environment"

  Scenario: use_action accepts valid execution_environment
    Given a r2cov CLI runner
    And a mocked lifecycle service for r2cov use action
    When I invoke r2cov use with execution-environment "container"
    Then the r2cov command should exit normally

  # ===================================================================
  # execute_plan — execution_environment validation (lines 1565-1578)
  # ===================================================================

  Scenario: execute_plan rejects invalid execution_environment
    Given a r2cov CLI runner
    And a mocked lifecycle service for r2cov execute with a valid plan
    When I invoke r2cov execute with execution-environment "badenv"
    Then the r2cov command should abort
    And the r2cov output should contain "Invalid execution environment"

  Scenario: execute_plan accepts valid execution_environment
    Given a r2cov CLI runner
    And a mocked lifecycle service for r2cov execute with a valid plan
    When I invoke r2cov execute with execution-environment "host"
    Then the r2cov command should exit normally

  # ===================================================================
  # resume_plan rich output branches (lines 2588, 2593, 2598)
  # ===================================================================

  Scenario: Resume plan rich output shows decision_id, checkpoint, and sandbox_ref
    Given a r2cov CLI runner
    And a mocked resume service for r2cov with all optional fields
    When I invoke r2cov plan resume "PLAN123" in rich format
    Then the r2cov command should exit normally
    And the r2cov output should contain "Decision ID"
    And the r2cov output should contain "Checkpoint"
    And the r2cov output should contain "Sandbox"

  # ===================================================================
  # resume_plan — CleverAgentsError handler (lines 2616-2618)
  # ===================================================================

  Scenario: Resume plan raises CleverAgentsError
    Given a r2cov CLI runner
    And a mocked resume service for r2cov that raises CleverAgentsError
    When I invoke r2cov plan resume "PLAN123" in rich format
    Then the r2cov command should abort
    And the r2cov output should contain "Error"

  # ===================================================================
  # rollback_plan — full command body (lines 2691-2762)
  # ===================================================================

  Scenario: Rollback plan succeeds with --yes flag in rich format
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback that succeeds
    When I invoke r2cov plan rollback with --yes
    Then the r2cov command should exit normally
    And the r2cov output should contain "Rollback Summary"
    And the r2cov output should contain "Changes Reverted"
    And the r2cov output should contain "Impact"
    And the r2cov output should contain "Post-Rollback State"
    And the r2cov output should contain "Rollback complete"

  Scenario: Rollback plan succeeds in json format
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback that succeeds
    When I invoke r2cov plan rollback with --yes and --format json
    Then the r2cov command should exit normally
    And the r2cov output should contain "rollback_summary"

  Scenario: Rollback plan raises BusinessRuleViolation
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback that raises BusinessRuleViolation
    When I invoke r2cov plan rollback with --yes
    Then the r2cov command should abort
    And the r2cov output should contain "Rollback blocked"

  Scenario: Rollback plan raises ResourceNotFoundError
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback that raises ResourceNotFoundError
    When I invoke r2cov plan rollback with --yes
    Then the r2cov command should abort
    And the r2cov output should contain "Not found"

  Scenario: Rollback plan raises ValidationError
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback that raises ValidationError
    When I invoke r2cov plan rollback with --yes
    Then the r2cov command should abort
    And the r2cov output should contain "Validation Error"

  Scenario: Rollback plan raises CleverAgentsError
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback that raises CleverAgentsError
    When I invoke r2cov plan rollback with --yes
    Then the r2cov command should abort
    And the r2cov output should contain "Error"

  Scenario: Rollback plan cancelled when user declines confirmation
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback that succeeds
    When I invoke r2cov plan rollback without --yes and decline
    Then the r2cov command should abort
    And the r2cov output should contain "Rollback cancelled"

  # ===================================================================
  # rollback_plan — enriched confirmation prompt (issue #3443)
  # ===================================================================

  Scenario: Rollback confirmation prompt shows checkpoint label and creation time
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback with label "before-db-migration"
    When I invoke r2cov plan rollback without --yes and accept
    Then the r2cov command should exit normally
    And the r2cov output should contain "before-db-migration"
    And the r2cov output should contain "created"

  Scenario: Rollback confirmation prompt shows side effects when decisions exist
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback with 3 decisions and 1 child plan
    When I invoke r2cov plan rollback without --yes and accept
    Then the r2cov command should exit normally
    And the r2cov output should contain "invalidate 3 decisions"
    And the r2cov output should contain "cancel 1 child plan"

  Scenario: Rollback confirmation prompt falls back when checkpoint not found
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback where get_checkpoint raises not found
    When I invoke r2cov plan rollback without --yes and accept
    Then the r2cov command should exit normally
    And the r2cov output should contain "Rollback"

  Scenario: Rollback confirmation prompt shows only decisions when no child plans
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback with 4 decisions and no child plans
    When I invoke r2cov plan rollback without --yes and accept
    Then the r2cov command should exit normally
    And the r2cov output should contain "invalidate 4 decisions"

  Scenario: Rollback confirmation prompt counts parallel spawn decisions as child plans
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback with 2 parallel spawn child plans
    When I invoke r2cov plan rollback without --yes and accept
    Then the r2cov command should exit normally
    And the r2cov output should contain "cancel 2 child plans"

  Scenario: Rollback confirmation prompt handles future checkpoint timestamp gracefully
    Given a r2cov CLI runner
    And a mocked checkpoint service for r2cov rollback with a future checkpoint timestamp
    When I invoke r2cov plan rollback without --yes and accept
    Then the r2cov command should exit normally
    And the r2cov output should contain "just now"
