Fix bd-afd: Auto-fix metadata.json jsonl_export mismatch
## Summary When metadata.json gets deleted (git clean, merge conflict, rebase), the version tracking code auto-recreates it using DefaultConfig() which hardcoded jsonl_export to 'issues.jsonl'. But many repos (including beads itself) use 'beads.jsonl', causing a mismatch between config and actual JSONL file. ## Changes 1. **bd doctor --fix auto-detection** (cmd/bd/doctor/fix/database_config.go) - New DatabaseConfig() fix function that auto-detects actual JSONL file - Prefers beads.jsonl over issues.jsonl (canonical name) - Skips backup files and merge artifacts - Wired into doctor.go applyFixes() 2. **Version tracking auto-detection** (cmd/bd/version_tracking.go) - trackBdVersion() now scans for existing JSONL files before defaulting - Prevents mismatches when metadata.json gets recreated - Added findActualJSONLFile() helper function 3. **Canonical default name** (internal/configfile/configfile.go) - DefaultConfig() changed from issues.jsonl to beads.jsonl - Aligns with canonical naming convention 4. **FindJSONLPath preference** (internal/beads/beads.go) - Now prefers beads.jsonl over issues.jsonl when scanning - Default changed from issues.jsonl to beads.jsonl 5. **Test coverage** - Added comprehensive tests for DatabaseConfig fix - Updated configfile tests for new default - Verified backup file skipping logic ## Testing - All existing tests pass - New tests verify auto-fix behavior - Integration tested with simulated mismatches Closes: bd-afd
This commit is contained in:
@@ -198,6 +198,8 @@ func applyFixes(result doctorResult) {
|
||||
err = fix.MergeDriver(result.Path)
|
||||
case "Sync Branch Config":
|
||||
err = fix.SyncBranchConfig(result.Path)
|
||||
case "Database Config":
|
||||
err = fix.DatabaseConfig(result.Path)
|
||||
default:
|
||||
fmt.Printf(" ⚠ No automatic fix available for %s\n", check.Name)
|
||||
fmt.Printf(" Manual fix: %s\n", check.Fix)
|
||||
|
||||
164
cmd/bd/doctor/fix/database_config.go
Normal file
164
cmd/bd/doctor/fix/database_config.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
// DatabaseConfig auto-detects and fixes metadata.json database/JSONL config mismatches.
|
||||
// This fixes the issue where metadata.json gets recreated with wrong JSONL filename.
|
||||
//
|
||||
// bd-afd: bd doctor --fix should auto-fix metadata.json jsonl_export mismatch
|
||||
func DatabaseConfig(path string) error {
|
||||
if err := validateBeadsWorkspace(path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(path, ".beads")
|
||||
|
||||
// Load existing config
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
// No config exists - nothing to fix
|
||||
return fmt.Errorf("no metadata.json found")
|
||||
}
|
||||
|
||||
fixed := false
|
||||
|
||||
// Check if configured JSONL exists
|
||||
if cfg.JSONLExport != "" {
|
||||
jsonlPath := cfg.JSONLPath(beadsDir)
|
||||
if _, err := os.Stat(jsonlPath); os.IsNotExist(err) {
|
||||
// Config points to non-existent file - try to find actual JSONL
|
||||
actualJSONL := findActualJSONLFile(beadsDir)
|
||||
if actualJSONL != "" {
|
||||
fmt.Printf(" Updating jsonl_export: %s → %s\n", cfg.JSONLExport, actualJSONL)
|
||||
cfg.JSONLExport = actualJSONL
|
||||
fixed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if configured database exists
|
||||
if cfg.Database != "" {
|
||||
dbPath := cfg.DatabasePath(beadsDir)
|
||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
||||
// Config points to non-existent file - try to find actual database
|
||||
actualDB := findActualDBFile(beadsDir)
|
||||
if actualDB != "" {
|
||||
fmt.Printf(" Updating database: %s → %s\n", cfg.Database, actualDB)
|
||||
cfg.Database = actualDB
|
||||
fixed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !fixed {
|
||||
return fmt.Errorf("no configuration mismatches detected")
|
||||
}
|
||||
|
||||
// Save updated config
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
return fmt.Errorf("failed to save config: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// findActualJSONLFile scans .beads/ for the actual JSONL file in use.
|
||||
// Prefers beads.jsonl over issues.jsonl, skips backups and merge artifacts.
|
||||
func findActualJSONLFile(beadsDir string) string {
|
||||
entries, err := os.ReadDir(beadsDir)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var candidates []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := entry.Name()
|
||||
|
||||
// Must end with .jsonl
|
||||
if !strings.HasSuffix(name, ".jsonl") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip merge artifacts and backups
|
||||
lowerName := strings.ToLower(name)
|
||||
if strings.Contains(lowerName, "backup") ||
|
||||
strings.Contains(lowerName, ".orig") ||
|
||||
strings.Contains(lowerName, ".bak") ||
|
||||
strings.Contains(lowerName, "~") ||
|
||||
strings.HasPrefix(lowerName, "backup_") {
|
||||
continue
|
||||
}
|
||||
|
||||
candidates = append(candidates, name)
|
||||
}
|
||||
|
||||
if len(candidates) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Prefer beads.jsonl over issues.jsonl (canonical name)
|
||||
for _, name := range candidates {
|
||||
if name == "beads.jsonl" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to first candidate
|
||||
return candidates[0]
|
||||
}
|
||||
|
||||
// findActualDBFile scans .beads/ for the actual database file in use.
|
||||
// Prefers beads.db (canonical name), skips backups and vc.db.
|
||||
func findActualDBFile(beadsDir string) string {
|
||||
entries, err := os.ReadDir(beadsDir)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var candidates []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := entry.Name()
|
||||
|
||||
// Must end with .db
|
||||
if !strings.HasSuffix(name, ".db") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip backups and vc.db
|
||||
if strings.Contains(name, "backup") || name == "vc.db" {
|
||||
continue
|
||||
}
|
||||
|
||||
candidates = append(candidates, name)
|
||||
}
|
||||
|
||||
if len(candidates) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Prefer beads.db (canonical name)
|
||||
for _, name := range candidates {
|
||||
if name == "beads.db" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to first candidate
|
||||
return candidates[0]
|
||||
}
|
||||
124
cmd/bd/doctor/fix/database_config_test.go
Normal file
124
cmd/bd/doctor/fix/database_config_test.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
// TestDatabaseConfigFix_JSONLMismatch tests that DatabaseConfig fixes JSONL mismatches.
|
||||
// bd-afd: Verify auto-fix for metadata.json jsonl_export mismatch
|
||||
func TestDatabaseConfigFix_JSONLMismatch(t *testing.T) {
|
||||
// Create temporary directory
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.Mkdir(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
// Create beads.jsonl file (actual JSONL)
|
||||
jsonlPath := filepath.Join(beadsDir, "beads.jsonl")
|
||||
if err := os.WriteFile(jsonlPath, []byte(`{"id":"test-123"}`), 0644); err != nil {
|
||||
t.Fatalf("Failed to create beads.jsonl: %v", err)
|
||||
}
|
||||
|
||||
// Create metadata.json with wrong JSONL filename (issues.jsonl)
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
JSONLExport: "issues.jsonl", // Wrong - should be beads.jsonl
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Failed to save config: %v", err)
|
||||
}
|
||||
|
||||
// Run the fix
|
||||
if err := DatabaseConfig(tmpDir); err != nil {
|
||||
t.Fatalf("DatabaseConfig failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify the config was updated
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load updated config: %v", err)
|
||||
}
|
||||
|
||||
if updatedCfg.JSONLExport != "beads.jsonl" {
|
||||
t.Errorf("Expected JSONLExport to be 'beads.jsonl', got %q", updatedCfg.JSONLExport)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDatabaseConfigFix_PrefersBeadsJSONL tests that DatabaseConfig prefers beads.jsonl over issues.jsonl.
|
||||
func TestDatabaseConfigFix_PrefersBeadsJSONL(t *testing.T) {
|
||||
// Create temporary directory
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.Mkdir(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
// Create both beads.jsonl and issues.jsonl
|
||||
beadsJSONL := filepath.Join(beadsDir, "beads.jsonl")
|
||||
if err := os.WriteFile(beadsJSONL, []byte(`{"id":"test-123"}`), 0644); err != nil {
|
||||
t.Fatalf("Failed to create beads.jsonl: %v", err)
|
||||
}
|
||||
|
||||
issuesJSONL := filepath.Join(beadsDir, "issues.jsonl")
|
||||
if err := os.WriteFile(issuesJSONL, []byte(`{"id":"test-456"}`), 0644); err != nil {
|
||||
t.Fatalf("Failed to create issues.jsonl: %v", err)
|
||||
}
|
||||
|
||||
// Create metadata.json with wrong JSONL filename (old.jsonl)
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
JSONLExport: "old.jsonl", // Wrong - should prefer beads.jsonl
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Failed to save config: %v", err)
|
||||
}
|
||||
|
||||
// Run the fix
|
||||
if err := DatabaseConfig(tmpDir); err != nil {
|
||||
t.Fatalf("DatabaseConfig failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify the config was updated to beads.jsonl (not issues.jsonl)
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load updated config: %v", err)
|
||||
}
|
||||
|
||||
if updatedCfg.JSONLExport != "beads.jsonl" {
|
||||
t.Errorf("Expected JSONLExport to be 'beads.jsonl', got %q", updatedCfg.JSONLExport)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindActualJSONLFile_SkipsBackups tests that backup files are skipped.
|
||||
func TestFindActualJSONLFile_SkipsBackups(t *testing.T) {
|
||||
// Create temporary directory
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create beads.jsonl and various backup files
|
||||
files := []string{
|
||||
"beads.jsonl",
|
||||
"beads.jsonl.backup",
|
||||
"backup_beads.jsonl",
|
||||
"beads.jsonl.orig",
|
||||
"beads.jsonl.bak",
|
||||
"beads.jsonl~",
|
||||
}
|
||||
|
||||
for _, name := range files {
|
||||
path := filepath.Join(tmpDir, name)
|
||||
if err := os.WriteFile(path, []byte(`{"id":"test"}`), 0644); err != nil {
|
||||
t.Fatalf("Failed to create %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// findActualJSONLFile should return beads.jsonl (not backups)
|
||||
result := findActualJSONLFile(tmpDir)
|
||||
if result != "beads.jsonl" {
|
||||
t.Errorf("Expected 'beads.jsonl', got %q", result)
|
||||
}
|
||||
}
|
||||
@@ -252,7 +252,7 @@ func CheckDatabaseConfig(repoPath string) DoctorCheck {
|
||||
Status: "warning",
|
||||
Message: "Configuration mismatch detected",
|
||||
Detail: strings.Join(issues, "\n "),
|
||||
Fix: "Update configuration in .beads/metadata.json:\n" +
|
||||
Fix: "Run 'bd doctor --fix' to auto-detect and fix mismatches, or manually:\n" +
|
||||
" 1. Check which files are actually being used\n" +
|
||||
" 2. Update metadata.json to match the actual filenames\n" +
|
||||
" 3. Or rename the files to match the configuration",
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
@@ -34,6 +35,13 @@ func trackBdVersion() {
|
||||
// No config file yet - create one with current version
|
||||
cfg = configfile.DefaultConfig()
|
||||
cfg.LastBdVersion = Version
|
||||
|
||||
// bd-afd: Auto-detect actual JSONL file instead of using hardcoded default
|
||||
// This prevents mismatches when metadata.json gets deleted (git clean, merge conflict, etc.)
|
||||
if actualJSONL := findActualJSONLFile(beadsDir); actualJSONL != "" {
|
||||
cfg.JSONLExport = actualJSONL
|
||||
}
|
||||
|
||||
_ = cfg.Save(beadsDir) // Best effort
|
||||
return
|
||||
}
|
||||
@@ -175,6 +183,57 @@ func findSubstring(haystack, needle string) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
// findActualJSONLFile scans .beads/ for the actual JSONL file in use.
|
||||
// Prefers beads.jsonl over issues.jsonl, skips backups and merge artifacts.
|
||||
// Returns empty string if no JSONL file is found.
|
||||
//
|
||||
// bd-afd: Auto-detect JSONL file to prevent metadata.json mismatches
|
||||
func findActualJSONLFile(beadsDir string) string {
|
||||
entries, err := os.ReadDir(beadsDir)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var candidates []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := entry.Name()
|
||||
|
||||
// Must end with .jsonl
|
||||
if !strings.HasSuffix(name, ".jsonl") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip merge artifacts and backups
|
||||
lowerName := strings.ToLower(name)
|
||||
if strings.Contains(lowerName, "backup") ||
|
||||
strings.Contains(lowerName, ".orig") ||
|
||||
strings.Contains(lowerName, ".bak") ||
|
||||
strings.Contains(lowerName, "~") ||
|
||||
strings.HasPrefix(lowerName, "backup_") {
|
||||
continue
|
||||
}
|
||||
|
||||
candidates = append(candidates, name)
|
||||
}
|
||||
|
||||
if len(candidates) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Prefer beads.jsonl over issues.jsonl (canonical name)
|
||||
for _, name := range candidates {
|
||||
if name == "beads.jsonl" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to first candidate (including issues.jsonl if present)
|
||||
return candidates[0]
|
||||
}
|
||||
|
||||
// autoMigrateOnVersionBump automatically migrates the database when CLI version changes.
|
||||
// This function is best-effort - failures are silent to avoid disrupting commands.
|
||||
// Called from PersistentPreRun after daemon check but before opening DB for main operation.
|
||||
|
||||
Reference in New Issue
Block a user