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

@@ -222,7 +222,7 @@ func FindBeadsDir() string {
// FindJSONLPath returns the expected JSONL file path for the given database path.
// It searches for existing *.jsonl files in the database directory and returns
// the first one found, or defaults to "beads.jsonl".
// the first one found, preferring beads.jsonl over issues.jsonl (bd-afd).
//
// This function does not create directories or files - it only discovers paths.
// Use this when you need to know where bd stores its JSONL export.
@@ -238,12 +238,18 @@ func FindJSONLPath(dbPath string) string {
pattern := filepath.Join(dbDir, "*.jsonl")
matches, err := filepath.Glob(pattern)
if err == nil && len(matches) > 0 {
// Return the first .jsonl file found
// bd-afd: Prefer beads.jsonl over issues.jsonl (canonical name)
for _, match := range matches {
if filepath.Base(match) == "beads.jsonl" {
return match
}
}
// Return the first .jsonl file found if beads.jsonl not present
return matches[0]
}
// Default to issues.jsonl
return filepath.Join(dbDir, "issues.jsonl")
// bd-afd: Default to beads.jsonl (was issues.jsonl)
return filepath.Join(dbDir, "beads.jsonl")
}
// DatabaseInfo contains information about a discovered beads database

View File

@@ -18,7 +18,7 @@ type Config struct {
func DefaultConfig() *Config {
return &Config{
Database: "beads.db",
JSONLExport: "issues.jsonl",
JSONLExport: "beads.jsonl", // Default to canonical name (was issues.jsonl)
}
}

View File

@@ -13,8 +13,9 @@ func TestDefaultConfig(t *testing.T) {
t.Errorf("Database = %q, want beads.db", cfg.Database)
}
if cfg.JSONLExport != "issues.jsonl" {
t.Errorf("JSONLExport = %q, want issues.jsonl", cfg.JSONLExport)
// bd-afd: Default changed from issues.jsonl to beads.jsonl (canonical name)
if cfg.JSONLExport != "beads.jsonl" {
t.Errorf("JSONLExport = %q, want beads.jsonl", cfg.JSONLExport)
}
}