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:
Steve Yegge
2025-11-23 23:11:08 -08:00
parent bee0e44187
commit d1641c7d2e
9 changed files with 365 additions and 9 deletions

View File

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