fix(worktree): integrate health check into CreateBeadsWorktree to prevent redundant checks

The error message 'path exists but is not a valid git worktree' was appearing
in daemon.log when the daemon attempted to use an existing worktree that was
in the git worktree list but had other issues (broken sparse checkout, etc.).

Root cause:
- CreateBeadsWorktree only checked isValidWorktree (is it in git worktree list)
- CheckWorktreeHealth was called separately and checked additional things
- If the worktree passed isValidWorktree but failed health check, an error
  was logged and repair was attempted

Fix:
- CreateBeadsWorktree now performs a full health check when it finds an
  existing worktree that's in the git worktree list
- If the health check fails, it automatically removes and recreates the
  worktree
- Removed redundant CheckWorktreeHealth calls in daemon_sync_branch.go and
  syncbranch/worktree.go since CreateBeadsWorktree now handles this internally

This eliminates the confusing error message and ensures worktrees are always
in a healthy state after CreateBeadsWorktree returns successfully.
This commit is contained in:
Charles P. Cross
2025-12-22 18:57:43 -05:00
parent 4c38075520
commit 665e5b3a87
3 changed files with 20 additions and 31 deletions

View File

@@ -35,11 +35,20 @@ func (wm *WorktreeManager) CreateBeadsWorktree(branch, worktreePath string) erro
if _, err := os.Stat(worktreePath); err == nil {
// Worktree path exists, check if it's a valid worktree
if valid, err := wm.isValidWorktree(worktreePath); err == nil && valid {
return nil // Already exists and is valid
}
// Path exists but isn't a valid worktree, remove it
if err := os.RemoveAll(worktreePath); err != nil {
return fmt.Errorf("failed to remove invalid worktree path: %w", err)
// Worktree exists and is in git worktree list, verify full health
if err := wm.CheckWorktreeHealth(worktreePath); err == nil {
return nil // Already exists and is fully healthy
}
// Health check failed, try to repair by removing and recreating
if err := wm.RemoveBeadsWorktree(worktreePath); err != nil {
// Log but continue - we'll try to recreate anyway
_ = os.RemoveAll(worktreePath)
}
} else {
// Path exists but isn't a valid worktree, remove it
if err := os.RemoveAll(worktreePath); err != nil {
return fmt.Errorf("failed to remove invalid worktree path: %w", err)
}
}
}