@sandbox-cleanup-conditional
Feature: Sandbox cleanup only on execute failure (#10872)
  Verifies that the execute_plan command's finally block only calls
  sandbox_obj.cleanup() when execution fails. On success the worktree
  branch survives until ``plan apply`` merges it (spec §13256-13260).

  Scenario: Execute success prevents sandbox cleanup for scco
    Given a mocked lifecycle service with a strat-complete plan for scco
    And the executor completes successfully for scco
    And sandbox_infos contains two active sandboxes for scco
    When I invoke plan execute for plan "01TESTSCCPASS0000000000" for scco
    Then the CLI exit code should be zero for scco
    And sandbox cleanup should NOT have been called for scco

  Scenario: Execute exception triggers sandbox cleanup for scef
    Given a mocked lifecycle service with a strat-complete plan for scef
    And the executor raises RuntimeError during run_execute for scef
    And sandbox_infos contains two active sandboxes for scef
    When I invoke plan execute for plan "01TESTSCCFAIL0000000000" for scef
    Then the CLI exit code should be non-zero for scef
    And sandbox cleanup should have been called for scef

  Scenario: Error recovery reversion triggers sandbox cleanup for sceef
    Given a mocked lifecycle service with an execute-errored plan for scef-er
    And the executor raises ValueError during error recovery for scef-er
    And sandbox_infos contains two active sandboxes for scef-er
    When I invoke plan execute for plan "01TESTSCEFERR000000000" for scef-er
    Then the CLI exit code should be non-zero for scef-er
    And sandbox cleanup should have been called for scef-er

  Scenario: Execute with empty sandbox_infos skips cleanup for sccs
    Given a mocked lifecycle service with a strat-complete plan for sccs
    And the executor completes successfully for sccs
    And sandbox_infos is empty for sccs
    When I invoke plan execute for plan "01TESTSCCSKIP000000000000" for sccs
    Then the CLI exit code should be zero for sccs
    And sandbox cleanup should NOT have been called for sccs
