From 1ba068fabdd20f64a5b8be8d898bc33d379baca8 Mon Sep 17 00:00:00 2001 From: Steve Yegge Date: Sun, 23 Nov 2025 22:34:19 -0800 Subject: [PATCH] Fix bd-l0r: Enforce ZFC (JSONL First Consistency) - refuse export when DB >> JSONL --- cmd/bd/integrity.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/bd/integrity.go b/cmd/bd/integrity.go index c18b3b7dd..7e1dd8477 100644 --- a/cmd/bd/integrity.go +++ b/cmd/bd/integrity.go @@ -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) } - // 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 { divergencePercent := math.Abs(float64(dbCount-jsonlCount)) / float64(jsonlCount) * 100 if divergencePercent > 50 { fmt.Fprintf(os.Stderr, "WARNING: DB has %d issues, JSONL has %d (%.1f%% divergence)\n", 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") } }