diff --git a/internal/cmd/spawn.go b/internal/cmd/spawn.go index 788905dd..2a901b75 100644 --- a/internal/cmd/spawn.go +++ b/internal/cmd/spawn.go @@ -141,6 +141,9 @@ func runSpawn(cmd *cobra.Command, args []string) error { polecatGit := git.NewGit(r.Path) polecatMgr := polecat.NewManager(r, polecatGit) + // Router for mail operations (used for checking inbox and sending assignments) + router := mail.NewRouter(r.Path) + // Auto-select polecat if not specified if polecatName == "" { polecatName, err = selectIdlePolecat(polecatMgr, r) @@ -160,6 +163,9 @@ func runSpawn(cmd *cobra.Command, args []string) error { } } + // Address for this polecat (used for mail operations) + polecatAddress := fmt.Sprintf("%s/%s", rigName, polecatName) + // Check if polecat exists existingPolecat, err := polecatMgr.Get(polecatName) polecatExists := err == nil @@ -196,8 +202,6 @@ func runSpawn(cmd *cobra.Command, args []string) error { } // Check for unread mail (indicates existing unstarted work) - polecatAddress := fmt.Sprintf("%s/%s", rigName, polecatName) - router := mail.NewRouter(r.Path) mailbox, mailErr := router.GetMailbox(polecatAddress) if mailErr == nil { _, unread, _ := mailbox.Count() @@ -229,10 +233,6 @@ func runSpawn(cmd *cobra.Command, args []string) error { return fmt.Errorf("getting polecat: %w", err) } - // Define polecatAddress and router for later use (mail sending) - polecatAddress := fmt.Sprintf("%s/%s", rigName, polecatName) - router := mail.NewRouter(r.Path) - // Beads operations use rig-level beads (at rig root, not mayor/rig) beadsPath := r.Path diff --git a/internal/polecat/manager.go b/internal/polecat/manager.go index b375a4f7..f3485fb7 100644 --- a/internal/polecat/manager.go +++ b/internal/polecat/manager.go @@ -281,11 +281,27 @@ func (m *Manager) Recreate(name string, force bool) (*Polecat, error) { _ = mayorGit.WorktreePrune() // Delete the old branch so worktree starts fresh from current HEAD - _ = mayorGit.DeleteBranch(branchName, true) // force delete + // Ignore error - branch may not exist (first recreate) or may fail to delete + _ = mayorGit.DeleteBranch(branchName, true) - // Create fresh worktree with new branch from current HEAD - if err := mayorGit.WorktreeAdd(polecatPath, branchName); err != nil { - return nil, fmt.Errorf("creating fresh worktree: %w", err) + // Check if branch still exists (deletion may have failed or branch was protected) + branchExists, err := mayorGit.BranchExists(branchName) + if err != nil { + return nil, fmt.Errorf("checking branch existence: %w", err) + } + + // Create worktree - handle both cases like Add() does + if branchExists { + // Branch still exists, create worktree using existing branch + // This happens if delete failed (e.g., protected branch) + if err := mayorGit.WorktreeAddExisting(polecatPath, branchName); err != nil { + return nil, fmt.Errorf("creating worktree with existing branch: %w", err) + } + } else { + // Branch was deleted, create fresh worktree with new branch from HEAD + if err := mayorGit.WorktreeAdd(polecatPath, branchName); err != nil { + return nil, fmt.Errorf("creating fresh worktree: %w", err) + } } // Set up shared beads