Feature: Plan Lifecycle Service coverage boost round 4
  As a developer
  I want to exercise remaining uncovered code paths in PlanLifecycleService
  So that code coverage improves to meet the 97% threshold

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

  # ---------------------------------------------------------------
  # _run_estimation — event_bus.emit PLAN_ESTIMATION_COMPLETE
  # coverage (via execute_plan when estimation_actor is configured)
  # ---------------------------------------------------------------

  Scenario: execute_plan with estimation_actor succeeds and emits PLAN_ESTIMATION_COMPLETE
    Given an action with estimation_actor configured for r4
    And a plan in strategize-complete state for r4
    And the plan lifecycle service has a recording event bus for r4
    When I execute the plan for r4
    Then the event bus should have recorded PLAN_ESTIMATION_COMPLETE event
    And the plan should be in execute phase

  Scenario: execute_plan with estimation_actor catches event_bus emit failure for PLAN_ESTIMATION_COMPLETE
    Given an action with estimation_actor configured for r4
    And a plan in strategize-complete state for r4
    And the plan lifecycle service has a failing event bus for r4
    When I execute the plan for r4
    Then the plan should be in execute phase despite estimation emit failure

  # ---------------------------------------------------------------
  # _run_estimation — cost_estimate_usd assignment (line 376)
  # ---------------------------------------------------------------

  Scenario: execute_plan populates cost_estimate_usd when estimation returns cost
    Given an action with estimation_actor returning cost for r4
    And a plan in strategize-complete state for r4
    And the plan lifecycle service has a recording event bus for r4
    When I execute the plan for r4 with mocked estimation
    Then the plan cost_estimate_usd should be populated

  # ---------------------------------------------------------------
  # execute_plan — lock acquire/release when lock_service is configured
  # Lines 1616-1621 (acquire) and 1684-1688 (finally release)
  # ---------------------------------------------------------------

  Scenario: execute_plan acquires and releases plan lock when lock_service is configured
    Given an action for execute with lock service for r4
    And a plan in strategize-complete state for r4
    When I execute the plan with lock service for r4
    Then the plan should be in execute phase
    And the lock service should have acquired and released the plan lock

  # ---------------------------------------------------------------
  # apply_plan — lock acquire/release when lock_service is configured
  # Lines 1830-1835 (acquire) and 1872-1877 (finally release)
  # ---------------------------------------------------------------

  Scenario: apply_plan acquires and releases plan lock when lock_service is configured
    Given an action for apply with lock service for r4
    And a plan in execute-complete state for r4
    When I apply the plan with lock service for r4
    Then the plan should be in apply phase
    And the lock service should have acquired and released the plan lock

  # ---------------------------------------------------------------
  # _subscribe_correction_reconciliation — event_bus.subscribe failure
  # Lines 562-566
  # ---------------------------------------------------------------

  Scenario: lifecycle service handles event_bus subscribe failure gracefully
    Given I have a plan lifecycle service with event_bus that fails on subscribe for r4
    When the service is created for r4
    Then no exception should be raised for r4

  # ---------------------------------------------------------------
  # prompt_plan — validation scenarios
  # Lines 2082 (blank guidance) and 2086-2097 (wrong phase/state)
  # ---------------------------------------------------------------

  Scenario: prompt_plan rejects blank guidance with ValidationError
    Given a plan in execute phase with processing state for prompt_plan r4
    When I prompt the plan with blank guidance for r4
    Then a ValidationError should be raised for blank guidance

  Scenario: prompt_plan rejects plan not in execute phase
    Given a plan in strategize phase for prompt_plan r4
    When I prompt the plan with guidance for wrong phase for r4
    Then a PlanError should be raised for wrong phase

  Scenario: prompt_plan rejects plan in complete state
    Given a plan in execute-complete state for prompt_plan r4
    When I prompt the plan with guidance for complete state for r4
    Then a PlanError should be raised for wrong state

  # ---------------------------------------------------------------
  # prompt_plan — state transition and decision_service interaction
  # Lines 2100-2154
  # ---------------------------------------------------------------

  Scenario: prompt_plan transitions from errored to processing and clears error_message
    Given a plan in execute phase with errored state for prompt_plan r4
    When I prompt the plan with guidance to resume from errored state for r4
    Then the plan processing_state should be processing
    And the plan error_message should be cleared

  Scenario: prompt_plan handles decision_service exception gracefully
    Given a plan in execute phase with processing state for prompt_plan r4
    And the plan lifecycle service has a failing decision service for r4
    When I prompt the plan with guidance for r4
    Then the plan processing_state should be processing
    And no exception should propagate

  # ---------------------------------------------------------------
  # _run_invariant_reconciliation — event_bus.emit exceptions
  # Lines 499-505 (success emit failure) and 530-536 (failure emit failure)
  # ---------------------------------------------------------------

  Scenario: _run_invariant_reconciliation success path catches event_bus emit failure
    Given an action with invariant_actor for reconciliation success r4
    And a plan in strategize-complete state with project links for r4
    And the plan lifecycle service has a failing event bus for r4
    When I execute the plan for invariant reconciliation emit failure r4
    Then the plan should be in execute phase
    And the event_bus should have been called for INVARIANT_RECONCILED emit failure

  Scenario: _run_invariant_reconciliation failure path catches event_bus emit failure
    Given an action with invariant_actor causing reconciliation failure for r4
    And a plan in strategize-complete state with project links for r4
    And the plan lifecycle service has a failing event bus for r4
    When I execute the plan expecting reconciliation failure for r4
    Then ReconciliationBlockedError should be raised
    And the event_bus should have been called for INVARIANT_VIOLATED emit failure

  # ---------------------------------------------------------------
  # Unknown automation profile validation
  # Lines 1229-1233
  # ---------------------------------------------------------------

  Scenario: use_action rejects unknown automation profile with ValidationError
    Given an action with unknown automation profile for r4
    When I use the action to create a plan for r4
    Then a ValidationError should be raised for unknown profile

  # ---------------------------------------------------------------
  # _handle_correction_applied — event handler scenarios
  # Lines 577 (plan_id None guard), 584-595 (exception handlers)
  # ---------------------------------------------------------------

Scenario: _handle_correction_applied skips event with no plan_id
    Given the plan lifecycle service has a recording event bus for correction handler r4
    When I handle a CORRECTION_APPLIED event with no plan_id for r4
    Then no exception should be raised for r4

 Scenario: _handle_correction_applied handles ReconciliationBlockedError gracefully
    Given a plan in strategize phase with processing state for r4
    And the invariant reconciliation raises ReconciliationBlockedError for r4
    When I handle a CORRECTION_APPLIED event for blocked reconciliation for r4
    Then no exception should be raised for r4

 Scenario: _handle_correction_applied handles general exception from get_plan gracefully
    Given the plan lifecycle service is configured for r4
    And get_plan will fail for the plan for r4
    When I handle a CORRECTION_APPLIED event when get_plan fails for r4
    Then no exception should be raised for r4

  # ---------------------------------------------------------------
  # resolve_actor_provider_model — namespace/name → provider/model
  # Lines 731-764 (factor resolution for #11254)
  # ---------------------------------------------------------------

  @tdd_issue @tdd_issue_11254
  Scenario: resolve_actor_provider_model returns provider/model for known provider
    Given the plan lifecycle service has a unit of work for actor resolution r4
    When I resolve actor provider model for "openai/gpt-4"
    Then the resolved actor provider model should be "openai/gpt-4"

  @tdd_issue @tdd_issue_11254
  Scenario: resolve_actor_provider_model resolves namespace/name via actor lookup
    Given the plan lifecycle service has a unit of work with a known actor "local/strategist" using "openai/gpt-4" for r4
    When I resolve actor provider model for "local/strategist"
    Then the resolved actor provider model should be "openai/gpt-4"

  @tdd_issue @tdd_issue_11254
  Scenario: resolve_actor_provider_model returns None when actor is not found
    Given the plan lifecycle service has a unit of work without actor "local/missing-actor" for r4
    When I resolve actor provider model for "local/missing-actor"
    Then the resolved actor provider model should be None

  @tdd_issue @tdd_issue_11254
  Scenario: resolve_actor_provider_model catches exception from actor lookup
    Given the plan lifecycle service has a unit of work that raises on actor lookup for r4
    When I resolve actor provider model for "local/crash-actor"
    Then the resolved actor provider model should be None

  @tdd_issue @tdd_issue_11254
  Scenario: resolve_actor_provider_model returns None when unit_of_work is absent
    Given the plan lifecycle service has no unit of work for r4
    When I resolve actor provider model for "local/strategist"
    Then the resolved actor provider model should be None

  @tdd_issue @tdd_issue_11254
  Scenario: resolve_actor_provider_model returns None for empty actor name
    Given the plan lifecycle service has a unit of work for actor resolution r4
    When I resolve actor provider model for ""
    Then the resolved actor provider model should be None