@mock_only @subplan @plan_executor
Feature: PlanExecutor subplan spawning integration
  As a system executing hierarchical plans
  I want PlanExecutor to realise subplan_spawn and subplan_parallel_spawn decisions
  So that child plans are actually spawned and executed during the Execute phase

  # ------------------------------------------------------------------
  # subplan_spawn decision realisation
  # ------------------------------------------------------------------

  Scenario: subplan_spawn decisions are realised as child plan executions (stub mode)
    Given a PlanExecutor configured with SubplanService and SubplanExecutionService
    And a parent plan in Execute phase with a subplan_spawn decision
    When I call run_execute on the parent plan
    Then SubplanService.get_spawn_decisions should have been called
    And SubplanService.spawn should have been called with the spawn entries
    And SubplanExecutionService.execute_all should have been called
    And the parent plan subplan_statuses should be updated

  Scenario: subplan_spawn decisions are realised in runtime mode
    Given a PlanExecutor configured with SubplanService and SubplanExecutionService in runtime mode
    And a parent plan in Execute phase with a subplan_spawn decision
    When I call run_execute on the parent plan in runtime mode
    Then SubplanService.get_spawn_decisions should have been called
    And SubplanService.spawn should have been called with the spawn entries
    And SubplanExecutionService.execute_all should have been called

  # ------------------------------------------------------------------
  # subplan_parallel_spawn concurrent execution
  # ------------------------------------------------------------------

  Scenario: subplan_parallel_spawn decisions trigger parallel execution
    Given a PlanExecutor configured with SubplanService and SubplanExecutionService
    And a parent plan in Execute phase with a subplan_parallel_spawn decision
    When I call run_execute on the parent plan
    Then SubplanService.get_spawn_decisions should have been called
    And SubplanService.spawn should have been called with the spawn entries
    And SubplanExecutionService.execute_all should have been called

  # ------------------------------------------------------------------
  # No-op when no spawn decisions are recorded
  # ------------------------------------------------------------------

  Scenario: No-op when no spawn decisions are recorded
    Given a PlanExecutor configured with SubplanService and SubplanExecutionService
    And a parent plan in Execute phase with no spawn decisions
    When I call run_execute on the parent plan
    Then SubplanService.get_spawn_decisions should have been called
    And SubplanService.spawn should NOT have been called
    And SubplanExecutionService.execute_all should NOT have been called

  # ------------------------------------------------------------------
  # No-op when SubplanService is not configured
  # ------------------------------------------------------------------

  Scenario: No-op when SubplanService is not injected
    Given a PlanExecutor with no SubplanService configured
    And a parent plan in Execute phase with a subplan_spawn decision
    When I call run_execute on the parent plan
    Then the execute phase completes without error
    And no subplan spawning occurs

  # ------------------------------------------------------------------
  # Parent plan status tracking for child subplan failures
  # ------------------------------------------------------------------

  Scenario: Parent plan error_details records failed subplan IDs
    Given a PlanExecutor configured with SubplanService and SubplanExecutionService
    And a parent plan in Execute phase with a subplan_spawn decision
    And the SubplanExecutionService will report a failed subplan
    When I call run_execute on the parent plan
    Then the parent plan error_details should contain failed_subplan_ids
    And the parent plan error_details should contain subplan_execution_failed true
