diff --git a/cmd/bd/import.go b/cmd/bd/import.go index 3bdd1b6b..0b69d5a4 100644 --- a/cmd/bd/import.go +++ b/cmd/bd/import.go @@ -200,20 +200,29 @@ NOTE: Import requires direct database access and does not work with daemon mode. } // Check if database needs initialization (prefix not set) - // Detect prefix from the imported issues + // Detect prefix from the imported issues (bd-8an fix) initCtx := rootCtx configuredPrefix, err2 := store.GetConfig(initCtx, "issue_prefix") if err2 != nil || strings.TrimSpace(configuredPrefix) == "" { // Database exists but not initialized - detect prefix from issues detectedPrefix := detectPrefixFromIssues(allIssues) + prefixSource := "issues" if detectedPrefix == "" { // No issues to import or couldn't detect prefix, use directory name + // But avoid using ".beads" as prefix - go up one level cwd, err := os.Getwd() if err != nil { fmt.Fprintf(os.Stderr, "Error: failed to get current directory: %v\n", err) os.Exit(1) } - detectedPrefix = filepath.Base(cwd) + dirName := filepath.Base(cwd) + if dirName == ".beads" || dirName == "beads" { + // Running from inside .beads/ - use parent directory + detectedPrefix = filepath.Base(filepath.Dir(cwd)) + } else { + detectedPrefix = dirName + } + prefixSource = "directory" } detectedPrefix = strings.TrimRight(detectedPrefix, "-") @@ -222,7 +231,7 @@ NOTE: Import requires direct database access and does not work with daemon mode. os.Exit(1) } - fmt.Fprintf(os.Stderr, "✓ Initialized database with prefix '%s' (detected from issues)\n", detectedPrefix) + fmt.Fprintf(os.Stderr, "✓ Initialized database with prefix '%s' (detected from %s)\n", detectedPrefix, prefixSource) } // Phase 2: Use shared import logic diff --git a/cmd/bd/init.go b/cmd/bd/init.go index aa485316..abd1df14 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -294,9 +294,30 @@ With --stealth: configures global git settings for invisible beads usage: } } - // Create metadata.json for database metadata + // Create or preserve metadata.json for database metadata (bd-zai fix) if useLocalBeads { - cfg := configfile.DefaultConfig() + // First, check if metadata.json already exists + existingCfg, err := configfile.Load(localBeadsDir) + if err != nil { + fmt.Fprintf(os.Stderr, "Warning: failed to load existing metadata.json: %v\n", err) + } + + var cfg *configfile.Config + if existingCfg != nil { + // Preserve existing config + cfg = existingCfg + } else { + // Create new config, detecting JSONL filename from existing files + cfg = configfile.DefaultConfig() + // Check if beads.jsonl exists but issues.jsonl doesn't (legacy) + issuesPath := filepath.Join(localBeadsDir, "issues.jsonl") + beadsPath := filepath.Join(localBeadsDir, "beads.jsonl") + if _, err := os.Stat(beadsPath); err == nil { + if _, err := os.Stat(issuesPath); os.IsNotExist(err) { + cfg.JSONLExport = "beads.jsonl" // Legacy filename + } + } + } if err := cfg.Save(localBeadsDir); err != nil { fmt.Fprintf(os.Stderr, "Warning: failed to create metadata.json: %v\n", err) // Non-fatal - continue anyway