fix: SetupRedirect now works with tracked beads architecture

The SetupRedirect function was failing for rigs that use the tracked
beads architecture where the canonical beads location is mayor/rig/.beads
and there is no rig-level .beads directory.

This fix now checks for both locations:
1. rig/.beads (with optional redirect to mayor/rig/.beads)
2. mayor/rig/.beads directly (if no rig/.beads exists)

This ensures crew and polecat workspaces get the correct redirect file
pointing to the shared beads database in all configurations.

Closes: gt-jy77g

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
max
2026-01-07 22:09:53 -08:00
committed by Steve Yegge
parent c4d956ebe7
commit f4cbcb4ce9
2 changed files with 85 additions and 26 deletions

View File

@@ -161,9 +161,10 @@ func cleanBeadsRuntimeFiles(beadsDir string) {
// - worktreePath: the worktree directory (e.g., <rig>/crew/<name> or <rig>/refinery/rig)
//
// The function:
// 1. Computes the relative path from worktree to rig-level .beads
// 2. Cleans up runtime files (preserving tracked files like formulas/)
// 3. Creates the redirect file
// 1. Finds the canonical beads location (rig/.beads or mayor/rig/.beads)
// 2. Computes the relative path from worktree to that location
// 3. Cleans up runtime files (preserving tracked files like formulas/)
// 4. Creates the redirect file
//
// Safety: This function refuses to create redirects in the canonical beads location
// (mayor/rig) to prevent circular redirect chains.
@@ -186,10 +187,47 @@ func SetupRedirect(townRoot, worktreePath string) error {
}
rigRoot := filepath.Join(townRoot, parts[0])
rigBeadsPath := filepath.Join(rigRoot, ".beads")
if _, err := os.Stat(rigBeadsPath); os.IsNotExist(err) {
return fmt.Errorf("no rig .beads found at %s", rigBeadsPath)
// Find the canonical beads location. In order of preference:
// 1. rig/.beads (if it exists and has content or a redirect)
// 2. mayor/rig/.beads (tracked beads architecture)
//
// The tracked beads architecture stores the actual database in mayor/rig/.beads
// and may not have a rig/.beads directory at all.
rigBeadsPath := filepath.Join(rigRoot, ".beads")
mayorBeadsPath := filepath.Join(rigRoot, "mayor", "rig", ".beads")
// Compute depth for relative paths
// e.g., crew/<name> (depth 2) -> ../../
// refinery/rig (depth 2) -> ../../
depth := len(parts) - 1 // subtract 1 for rig name itself
upPath := strings.Repeat("../", depth)
var redirectPath string
// Check if rig-level .beads exists
if _, err := os.Stat(rigBeadsPath); err == nil {
// rig/.beads exists - check if it has a redirect to follow
rigRedirectPath := filepath.Join(rigBeadsPath, "redirect")
if data, err := os.ReadFile(rigRedirectPath); err == nil {
rigRedirectTarget := strings.TrimSpace(string(data))
if rigRedirectTarget != "" {
// Rig has redirect (e.g., "mayor/rig/.beads" for tracked beads).
// Redirect worktree directly to the final destination.
redirectPath = upPath + rigRedirectTarget
}
}
// If no redirect in rig/.beads, point directly to rig/.beads
if redirectPath == "" {
redirectPath = upPath + ".beads"
}
} else if _, err := os.Stat(mayorBeadsPath); err == nil {
// No rig/.beads but mayor/rig/.beads exists (tracked beads architecture).
// Point directly to mayor/rig/.beads.
redirectPath = upPath + "mayor/rig/.beads"
} else {
// Neither location exists - this is an error
return fmt.Errorf("no beads found at %s or %s", rigBeadsPath, mayorBeadsPath)
}
// Clean up runtime files in .beads/ but preserve tracked files (formulas/, README.md, etc.)
@@ -201,25 +239,6 @@ func SetupRedirect(townRoot, worktreePath string) error {
return fmt.Errorf("creating .beads dir: %w", err)
}
// Compute relative path from worktree to rig root
// e.g., crew/<name> (depth 2) -> ../../.beads
// refinery/rig (depth 2) -> ../../.beads
depth := len(parts) - 1 // subtract 1 for rig name itself
redirectPath := strings.Repeat("../", depth) + ".beads"
// Check if rig-level beads has a redirect (tracked beads case).
// If so, redirect directly to the final destination to avoid chains.
// The bd CLI doesn't support redirect chains, so we must skip intermediate hops.
rigRedirectPath := filepath.Join(rigBeadsPath, "redirect")
if data, err := os.ReadFile(rigRedirectPath); err == nil {
rigRedirectTarget := strings.TrimSpace(string(data))
if rigRedirectTarget != "" {
// Rig has redirect (e.g., "mayor/rig/.beads" for tracked beads).
// Redirect worktree directly to the final destination.
redirectPath = strings.Repeat("../", depth) + rigRedirectTarget
}
}
// Create redirect file
redirectFile := filepath.Join(worktreeBeadsDir, "redirect")
if err := os.WriteFile(redirectFile, []byte(redirectPath+"\n"), 0644); err != nil {