Fix critical P0 database reinitialization bug (bd-130)

Fixes silent data loss when .beads/ directory removed and daemon auto-starts.

Root cause: checkGitForIssues() hardcoded 'issues.jsonl' but git tracks 'beads.jsonl'

Changes:
- Fix A (bd-131): checkGitForIssues() tries beads.jsonl first, then issues.jsonl
- Fix B (bd-132): Immediate export after import in bd init to prevent daemon race
- Fix C (bd-133): Safety check that fails loudly if import fails
- Fix D (bd-134): Daemon startup auto-import when DB empty but git has issues
- Tests (bd-135): Comprehensive integration test suite

Oracle-recommended improvements:
- Export to exact git-relative path (prevents path drift)
- filepath.ToSlash for Windows git compatibility
- 64MB scanner buffer for large JSONL lines
- Improved safety check messages (only suggest local file if exists)

All tests passing. No regressions.

Amp-Thread-ID: https://ampcode.com/threads/T-0e31dc6a-a0d9-46c6-87b2-cfdebe829a52
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-24 14:52:24 -07:00
parent 3b15b5b259
commit 14895bf97a
5 changed files with 515 additions and 24 deletions

View File

@@ -926,6 +926,21 @@ func runDaemonLoop(interval time.Duration, autoCommit, autoPush bool, logPath, p
defer func() { _ = store.Close() }()
log("Database opened: %s", daemonDBPath)
// Check for empty DB with issues in git - auto-recovery
ctx := context.Background()
stats, err := store.GetStatistics(ctx)
if err == nil && stats.TotalIssues == 0 {
issueCount, jsonlPath := checkGitForIssues()
if issueCount > 0 {
log("Empty database but git has %d issues, importing...", issueCount)
if err := importFromGit(ctx, daemonDBPath, store, jsonlPath); err != nil {
log("Warning: startup import failed: %v", err)
} else {
log("Successfully imported %d issues from git", issueCount)
}
}
}
// Start RPC server
socketPath := filepath.Join(filepath.Dir(daemonDBPath), "bd.sock")
server := rpc.NewServer(socketPath, store)