fix(spawn): resolve variable shadowing and branch deletion edge case

- Move router and polecatAddress definitions before if block to avoid
  shadowing (defining same vars inside and after the block)
- Handle branch deletion failure in Recreate() by checking if branch
  still exists and using WorktreeAddExisting like Add() does

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-21 10:29:32 -08:00
parent e07eb96144
commit ae6f4c8185
2 changed files with 26 additions and 10 deletions

View File

@@ -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

View File

@@ -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