Fix bd-l0r: Enforce ZFC (JSONL First Consistency) - refuse export when DB >> JSONL

This commit is contained in:
Steve Yegge
2025-11-23 22:34:19 -08:00
parent f5cc3ff85a
commit 1ba068fabd

View File

@@ -190,12 +190,24 @@ func validatePreExport(ctx context.Context, store storage.Storage, jsonlPath str
return fmt.Errorf("refusing to export empty DB over %d issues in JSONL (would cause data loss)", jsonlCount) return fmt.Errorf("refusing to export empty DB over %d issues in JSONL (would cause data loss)", jsonlCount)
} }
// Warning: large divergence suggests sync failure // Critical: refuse to export stale DB over fresh JSONL (bd-l0r)
// When DB has significantly more issues than JSONL, JSONL is the source of truth.
// Force import before allowing export to prevent stale DB from contaminating JSONL.
if jsonlCount > 0 { if jsonlCount > 0 {
divergencePercent := math.Abs(float64(dbCount-jsonlCount)) / float64(jsonlCount) * 100 divergencePercent := math.Abs(float64(dbCount-jsonlCount)) / float64(jsonlCount) * 100
if divergencePercent > 50 { if divergencePercent > 50 {
fmt.Fprintf(os.Stderr, "WARNING: DB has %d issues, JSONL has %d (%.1f%% divergence)\n", fmt.Fprintf(os.Stderr, "WARNING: DB has %d issues, JSONL has %d (%.1f%% divergence)\n",
dbCount, jsonlCount, divergencePercent) dbCount, jsonlCount, divergencePercent)
// If DB is much larger than JSONL, refuse export - force import first
// JSONL is always the source of truth (ZFC: JSONL First Consistency)
if dbCount > jsonlCount {
return fmt.Errorf("refusing to export: DB has %d more issues than JSONL (%.1f%% divergence). "+
"JSONL is the source of truth - import it first with 'bd import' or 'bd sync --import-only'",
dbCount-jsonlCount, divergencePercent)
}
// If JSONL is much larger than DB, just warn (could be legitimate new issues)
fmt.Fprintf(os.Stderr, "This suggests sync failure - investigate before proceeding\n") fmt.Fprintf(os.Stderr, "This suggests sync failure - investigate before proceeding\n")
} }
} }