@sandbox-reexecute-cleanup
Feature: Stale worktree branch cleanup before re-execute (#7271)
  Verifies that re-executing a plan cleans up the stale worktree
  branch from the previous execution before creating a fresh sandbox.

  Scenario: cleanup_stale removes existing branch and worktree for srec
    Given a temp git repo with a worktree branch for plan "01TESTREEXEC000000000000" for srec
    When I call cleanup_stale for plan "01TESTREEXEC000000000000" for srec
    Then the branch "cleveragents/plan-01TESTREEXEC000000000000" should not exist for srec
    And the worktree directory should not exist for srec

  Scenario: cleanup_stale is idempotent when no branch exists for srec
    Given a temp git repo without any worktree branches for srec
    When I call cleanup_stale for plan "01TESTNOEXIST00000000000" for srec
    Then cleanup_stale should return False for srec

  Scenario: create succeeds after cleanup_stale removes stale branch for srec
    Given a temp git repo with a worktree branch for plan "01TESTRECREATE0000000000" for srec
    When I call cleanup_stale for plan "01TESTRECREATE0000000000" for srec
    And I create a fresh sandbox for plan "01TESTRECREATE0000000000" for srec
    Then the fresh sandbox should be a directory for srec
    And the branch "cleveragents/plan-01TESTRECREATE0000000000" should exist for srec
