Feature: Plan Lifecycle Service coverage boost round 3
  As a developer
  I want to exercise remaining uncovered code paths in PlanLifecycleService
  So that code coverage improves beyond the current per-file threshold

  Background:
    Given I have a fresh plan lifecycle service for coverage boost r3

  # ---------------------------------------------------------------
  # complete_strategize — event_bus.emit PLAN_STATE_CHANGED success
  # ---------------------------------------------------------------

  Scenario: complete_strategize emits PLAN_STATE_CHANGED event via event bus
    Given a plan lifecycle service with a recording event bus for r3
    And an action "local/strat-complete-event" exists for coverage boost r3
    And a plan in strategize phase with processing state for r3
    When I complete strategize on the plan with event bus
    Then the event bus should have recorded a PLAN_STATE_CHANGED event

  # ---------------------------------------------------------------
  # complete_strategize — event_bus.emit PLAN_STATE_CHANGED exception
  # ---------------------------------------------------------------

  Scenario: complete_strategize catches event bus emission failure
    Given a plan lifecycle service with a failing event bus for r3
    And an action "local/strat-complete-fail" exists for coverage boost r3
    And a plan in strategize phase with processing state for r3
    When I complete strategize on the plan and event bus emit fails
    Then the plan phase should be "strategize" and state should be "complete"

  # ---------------------------------------------------------------
  # fail_strategize — event_bus.emit PLAN_ERRORED success
  # ---------------------------------------------------------------

  Scenario: fail_strategize emits PLAN_ERRORED event via event bus
    Given a plan lifecycle service with a recording event bus for r3
    And an action "local/strat-fail-event" exists for coverage boost r3
    And a plan in strategize phase with processing state for r3
    When I fail strategize on the plan with error "Test strategy error"
    Then the event bus should have recorded a PLAN_ERRORED event

  # ---------------------------------------------------------------
  # fail_strategize — event_bus.emit PLAN_ERRORED exception
  # ---------------------------------------------------------------

  Scenario: fail_strategize catches event bus emission failure
    Given a plan lifecycle service with a failing event bus for r3
    And an action "local/strat-fail-fail" exists for coverage boost r3
    And a plan in strategize phase with processing state for r3
    When I fail strategize on the plan and event bus emit fails
    Then the plan should be in errored state for r3

  # ---------------------------------------------------------------
  # complete_execute — event_bus.emit PLAN_STATE_CHANGED success
  # ---------------------------------------------------------------

  Scenario: complete_execute emits PLAN_STATE_CHANGED event via event bus
    Given a plan lifecycle service with a recording event bus for r3
    And an action "local/exec-complete-event" exists for coverage boost r3
    And a plan in execute phase with processing state for r3
    When I complete execute on the plan with event bus
    Then the event bus should have recorded a PLAN_STATE_CHANGED event

  # ---------------------------------------------------------------
  # complete_execute — event_bus.emit PLAN_STATE_CHANGED exception
  # ---------------------------------------------------------------

  Scenario: complete_execute catches event bus emission failure
    Given a plan lifecycle service with a failing event bus for r3
    And an action "local/exec-complete-fail" exists for coverage boost r3
    And a plan in execute phase with processing state for r3
    When I complete execute on the plan and event bus emit fails
    Then the plan phase should be "execute" and state should be "complete"

  # ---------------------------------------------------------------
  # fail_execute — event_bus.emit PLAN_ERRORED success
  # ---------------------------------------------------------------

  Scenario: fail_execute emits PLAN_ERRORED event via event bus
    Given a plan lifecycle service with a recording event bus for r3
    And an action "local/exec-fail-event" exists for coverage boost r3
    And a plan in execute phase with processing state for r3
    When I fail execute on the plan with error "Test execution error"
    Then the event bus should have recorded a PLAN_ERRORED event

  # ---------------------------------------------------------------
  # fail_execute — event_bus.emit PLAN_ERRORED exception
  # ---------------------------------------------------------------

  Scenario: fail_execute catches event bus emission failure
    Given a plan lifecycle service with a failing event bus for r3
    And an action "local/exec-fail-fail" exists for coverage boost r3
    And a plan in execute phase with processing state for r3
    When I fail execute on the plan and event bus emit fails
    Then the plan should be in errored state for r3

  # ---------------------------------------------------------------
  # fail_apply — event_bus.emit PLAN_ERRORED success
  # ---------------------------------------------------------------

  Scenario: fail_apply emits PLAN_ERRORED event via event bus
    Given a plan lifecycle service with a recording event bus for r3
    And an action "local/apply-fail-event" exists for coverage boost r3
    And a plan in apply phase with processing state for r3
    When I fail apply on the plan with error "Test apply error"
    Then the event bus should have recorded a PLAN_ERRORED event

  # ---------------------------------------------------------------
  # fail_apply — event_bus.emit PLAN_ERRORED exception
  # ---------------------------------------------------------------

  Scenario: fail_apply catches event bus emission failure
    Given a plan lifecycle service with a failing event bus for r3
    And an action "local/apply-fail-fail" exists for coverage boost r3
    And a plan in apply phase with processing state for r3
    When I fail apply on the plan and event bus emit fails
    Then the plan should be in errored state for r3

  # ---------------------------------------------------------------
  # fail_execute — _cleanup_devcontainers called on terminal failure
  # ---------------------------------------------------------------

  Scenario: fail_execute triggers devcontainer cleanup on terminal failure
    Given a plan lifecycle service with mocked devcontainer cleanup for r3
    And an action "local/exec-fail-cleanup" exists for coverage boost r3
    And a plan in execute phase with processing state for r3
    When I fail execute on the plan with error "Container cleanup test"
    Then the plan should be in errored state and cleanup should have been called

  # ---------------------------------------------------------------
  # fail_apply — _cleanup_devcontainers called on terminal failure
  # ---------------------------------------------------------------

  Scenario: fail_apply triggers devcontainer cleanup on terminal failure
    Given a plan lifecycle service with mocked devcontainer cleanup for r3
    And an action "local/apply-fail-cleanup" exists for coverage boost r3
    And a plan in apply phase with processing state for r3
    When I fail apply on the plan with error "Container cleanup test"
    Then the plan should be in errored state and cleanup should have been called
