fix(plan): clean up stale worktree branch before re-creating sandbox #10797

Closed
hamza.khyari wants to merge 1 commits from bugfix/m6-sandbox-reexecute-cleanup into master

1 Commits

Author SHA1 Message Date
hamza.khyari a9c820610a fix(plan): clean up stale worktree branch before re-creating sandbox
CI / build (pull_request) Successful in 3m45s
CI / coverage (pull_request) Successful in 13m29s
CI / helm (pull_request) Successful in 55s
CI / push-validation (pull_request) Successful in 21s
CI / security (pull_request) Successful in 4m22s
CI / e2e_tests (pull_request) Successful in 6m47s
CI / lint (pull_request) Successful in 3m44s
CI / typecheck (pull_request) Successful in 4m15s
CI / quality (pull_request) Successful in 4m20s
CI / integration_tests (pull_request) Successful in 8m4s
CI / unit_tests (pull_request) Successful in 7m3s
CI / docker (pull_request) Successful in 1m29s
CI / status-check (pull_request) Successful in 3s
When a user re-executes a plan (e.g. after a failed attempt or after
reviewing the diff), _create_sandbox_for_plan crashes with 'fatal: a
branch named cleveragents/plan-<id> already exists' because the
worktree branch from the previous execute was never cleaned up.

Add GitWorktreeSandbox.cleanup_stale() classmethod in the
infrastructure layer that idempotently removes stale worktree
directories and branches.  Each git command has explicit error
handling with logging and fallback (manual rmtree if worktree remove
fails).  _create_sandbox_for_plan in the CLI layer delegates to
cleanup_stale before calling sandbox.create().

ISSUES CLOSED: #7271
2026-04-20 13:22:44 +00:00