@subplan @plan_executor @integration @phase4 @tdd_issue @tdd_issue_10270
Feature: Plan hierarchy 4-phase lifecycle execution
  As a system executing hierarchical plans
  I want child plans to independently complete all 4 phases — Strategize, Decompose, Execute, Validate
  So that hierarchical decomposition produces fully executed, verifiable, and aggregated results

  Background:
    Given a plan lifecycle service with in-memory storage
    And a DecisionService for recording spawn decisions
    And a SubplanService backed by the DecisionService

  # ------------------------------------------------------------------
  # Happy path — 2-level hierarchy (Forgejo #10270)
  # ------------------------------------------------------------------

  Scenario: Parent plan with 2-level hierarchy executes all phases for parent and children
    Given a parent plan "parent-happy" in Strategize phase with a spawn decision recorded
    When I create two child plans for "parent-happy" and pre-register them in the lifecycle
    And I strategize and execute the parent plan "parent-happy"
    Then the parent plan Strategize phase should be complete
    And the parent plan Execute phase should be complete
    And each child plan should have completed Strategize phase
    And each child plan should have completed Execute phase
    And the parent plan should have 1 subplan statuses
    And all child subplan statuses should indicate success

  # ------------------------------------------------------------------
  # Checkpoint triggers — on_subplan_spawn (Forgejo #10270)
  # ------------------------------------------------------------------

  Scenario: on_subplan_spawn checkpoint is created before first child execution
    Given a parent plan "parent-checkpoint" in Strategize phase with a spawn decision recorded
    And I create one child plan for "parent-checkpoint" and pre-register it in the lifecycle
    And a CheckpointService is configured for the parent plan "parent-checkpoint"
    When I strategize and execute the parent plan "parent-checkpoint"
    Then at least one on_subplan_spawn checkpoint should have been created

  # ------------------------------------------------------------------
  # Max-child-depth enforcement (Forgejo #10270)
  # ------------------------------------------------------------------

  Scenario: Decomposition respects max-child-depth during execution
    Given a decomposition service with max-child-depth 1
    And a project with 100 files across 5 directory levels
    When I decompose the project files with stored config
    Then the decomposition result should have max_depth_reached <= 1

  Scenario: Decomposition with default max-child-depth allows moderate nesting
    Given a decomposition service with default config
    And a project with 200 files across 5 directory levels
    When I decompose the project files with stored config
    Then the decomposition result should have max_depth_reached >= 1

  # ------------------------------------------------------------------
  # Child success aggregation (Forgejo #10270)
  # ------------------------------------------------------------------

  Scenario: Parent plan aggregates child subplan results after execution
    Given a parent plan "parent-aggregate" in Strategize phase with two spawn decisions recorded
    When I create two child plans for "parent-aggregate" and pre-register them in the lifecycle
    And I strategize and execute the parent plan "parent-aggregate"
    Then the parent plan should have subplan statuses with execution results
    And the parent plan error_details should not contain failed_subplan_ids

  # ------------------------------------------------------------------
  # Child failure handling (Forgejo #10270)
  # ------------------------------------------------------------------

  Scenario: Parent plan captures child plan failure in error_details
    Given a parent plan "parent-fail" in Strategize phase with a spawn decision recorded without action registration
    When I strategize and execute the parent plan "parent-fail"
    Then the parent plan error_details should have failed subplan ids

  # ------------------------------------------------------------------
  # Nested execution — 3-level hierarchy (Forgejo #10270)
  # ------------------------------------------------------------------

  Scenario: Grandchild plan in 3-level hierarchy executes and cascades results
    Given a parent plan "parent-nested" in Strategize phase with a spawn decision recorded
    And I create child plan "child-nested" for "parent-nested" with its own spawn decision recorded
    And I create a grandchild plan for "child-nested" and pre-register it in the lifecycle
    When I strategize and execute the parent plan "parent-nested"
    Then the parent plan should have 1 subplan statuses
    And the child plan should complete both Strategize and Execute phases
    And the grandchild plan should complete both Strategize and Execute phases
