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

Closed
opened 2026-04-10 14:21:11 +00:00 by hamza.khyari · 3 comments
Member

Summary

When a user runs plan execute a second time on the same plan (e.g. after reviewing the diff and wanting a fresh attempt), _create_sandbox_for_plan crashes with:

fatal: a branch named 'cleveragents/plan-<PLAN_ID>' already exists

The worktree branch from the first execute was never cleaned up, so GitWorktreeSandbox.create() fails trying to create a branch that already exists.

Metadata

  • Commit Message: fix(plan): clean up stale worktree branch before re-creating sandbox
  • Branch: bugfix/sandbox-reexecute-cleanup

Current Behavior

  1. plan execute creates worktree branch cleveragents/plan-<id>
  2. Execute completes, plan reaches execute/complete
  3. User runs plan execute again
  4. _create_sandbox_for_plan calls GitWorktreeSandbox.create()
  5. git worktree add fails: branch already exists
  6. CLI crashes with "Unexpected error"

Expected Behavior

  1. _create_sandbox_for_plan detects the existing branch
  2. Cleans up the stale worktree and branch
  3. Creates a fresh worktree for the new execute attempt
  4. Re-execute succeeds

Subtasks

  • In _create_sandbox_for_plan, check if branch cleveragents/plan-<id> already exists before calling sandbox.create()
  • If it exists, remove the stale worktree (git worktree remove) and delete the branch (git branch -D)
  • Then proceed with sandbox.create() as normal
  • Add scenario-8 (re-execute) to agents-test suite

Definition of Done

  • Re-executing a plan does not crash
  • Stale worktree and branch are cleaned up before re-creation
  • Second execute produces fresh LLM output
  • Scenario-8 end-to-end test passes

Spec Reference

specification.md §18323 (Phase reversion — Execute may revert to Strategize)

## Summary When a user runs `plan execute` a second time on the same plan (e.g. after reviewing the diff and wanting a fresh attempt), `_create_sandbox_for_plan` crashes with: ``` fatal: a branch named 'cleveragents/plan-<PLAN_ID>' already exists ``` The worktree branch from the first execute was never cleaned up, so `GitWorktreeSandbox.create()` fails trying to create a branch that already exists. ## Metadata - **Commit Message**: `fix(plan): clean up stale worktree branch before re-creating sandbox` - **Branch**: `bugfix/sandbox-reexecute-cleanup` ## Current Behavior 1. `plan execute` creates worktree branch `cleveragents/plan-<id>` 2. Execute completes, plan reaches `execute/complete` 3. User runs `plan execute` again 4. `_create_sandbox_for_plan` calls `GitWorktreeSandbox.create()` 5. `git worktree add` fails: branch already exists 6. CLI crashes with "Unexpected error" ## Expected Behavior 1. `_create_sandbox_for_plan` detects the existing branch 2. Cleans up the stale worktree and branch 3. Creates a fresh worktree for the new execute attempt 4. Re-execute succeeds ## Subtasks - [ ] In `_create_sandbox_for_plan`, check if branch `cleveragents/plan-<id>` already exists before calling `sandbox.create()` - [ ] If it exists, remove the stale worktree (`git worktree remove`) and delete the branch (`git branch -D`) - [ ] Then proceed with `sandbox.create()` as normal - [ ] Add scenario-8 (re-execute) to agents-test suite ## Definition of Done - Re-executing a plan does not crash - Stale worktree and branch are cleaned up before re-creation - Second execute produces fresh LLM output - Scenario-8 end-to-end test passes ## Spec Reference `specification.md` §18323 (Phase reversion — Execute may revert to Strategize)
hamza.khyari added this to the v3.5.0 milestone 2026-04-10 14:21:11 +00:00
Owner

Verified — Bug fix: clean up stale worktree branch before re-creating sandbox. MoSCoW: Should-have. Priority: Medium.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Bug fix: clean up stale worktree branch before re-creating sandbox. MoSCoW: Should-have. Priority: Medium. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Owner

Verified — Bug fix: clean up stale worktree branch before re-creating sandbox. MoSCoW: Should-have. Priority: Medium.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Bug fix: clean up stale worktree branch before re-creating sandbox. MoSCoW: Should-have. Priority: Medium. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Owner

Verified — Bug fix: clean up stale worktree branch before re-creating sandbox. MoSCoW: Should-have. Priority: Medium.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Bug fix: clean up stale worktree branch before re-creating sandbox. MoSCoW: Should-have. Priority: Medium. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#7271
No description provided.