From 949ab4294c1840daff0fcee1b4d43f3ce1f068af Mon Sep 17 00:00:00 2001 From: Steve Yegge Date: Sun, 23 Nov 2025 22:39:44 -0800 Subject: [PATCH] Fix ZFC: only force import when DB >> JSONL (stale DB) Reverted 'always import' - that would overwrite local DB changes. Now: only import first if DB has >50% more issues than JSONL (stale DB). Preserves local uncommitted changes while catching sora scenario. --- cmd/bd/sync.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/cmd/bd/sync.go b/cmd/bd/sync.go index 48aae434..db029e86 100644 --- a/cmd/bd/sync.go +++ b/cmd/bd/sync.go @@ -123,16 +123,28 @@ Use --merge to merge the sync branch back to main branch.`, os.Exit(1) } - // Step 1: Import JSONL first (ZFC: JSONL First Consistency - bd-l0r) - // JSONL is always the source of truth. Import before export to ensure DB is synced. + // Step 1: Export pending changes (but check for stale DB first) if dryRun { - fmt.Println("→ [DRY RUN] Would import from JSONL (ZFC)") fmt.Println("→ [DRY RUN] Would export pending changes to JSONL") } else { - fmt.Println("→ Importing from JSONL (ZFC)...") - if err := importFromJSONL(ctx, jsonlPath, renameOnImport); err != nil { - fmt.Fprintf(os.Stderr, "Error importing (ZFC): %v\n", err) - os.Exit(1) + // ZFC safety check (bd-l0r): if DB significantly diverges from JSONL, + // force import first to sync with JSONL source of truth + if err := ensureStoreActive(); err == nil && store != nil { + dbCount, err := countDBIssuesFast(ctx, store) + if err == nil { + jsonlCount, err := countIssuesInJSONL(jsonlPath) + if err == nil && jsonlCount > 0 && dbCount > jsonlCount { + divergence := float64(dbCount-jsonlCount) / float64(jsonlCount) + if divergence > 0.5 { // >50% more issues in DB than JSONL + fmt.Printf("→ DB has %d issues but JSONL has %d (stale DB detected)\n", dbCount, jsonlCount) + fmt.Println("→ Importing JSONL first (ZFC)...") + if err := importFromJSONL(ctx, jsonlPath, renameOnImport); err != nil { + fmt.Fprintf(os.Stderr, "Error importing (ZFC): %v\n", err) + os.Exit(1) + } + } + } + } } // Pre-export integrity checks