From 1d7629213bd2c5ec672beff9c3b35cb9e338e21d Mon Sep 17 00:00:00 2001 From: Jim Wordelman Date: Sat, 15 Aug 2026 01:22:47 -0700 Subject: [PATCH] fix(init): refuse to recreate a missing server-mode database (be-5up5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bd init's server-mode "already initialized" check treated a missing or unreachable database the same as a genuine fresh clone, silently returning nil and letting init proceed to create an empty database — even when metadata.json's project_id proved this was an existing project whose database had gone missing (the 2026-08-11 fleet-wide data loss). Guard on cfg.ProjectID != "" to distinguish the two cases. An existing project now gets a refusal (initGuardMissingServerDBMessage) that names bd doctor / bd dolt status / bd backup restore as the recovery path and explicitly does not recommend bd bootstrap, since bootstrap has its own related mode-blind create bug (tracked separately). A new --recreate-missing flag is required to opt into creating fresh in this case — never implied by --force, config, or env. Genuine fresh clones (empty project_id) are unaffected. Also: - doctor/gitignore.go: drop the "bd init (safe to re-run)" fix hint, since re-running bd init is exactly the unsafe action this fix prevents in the case that matters. - storage/dolt/errors.go: the general "database not found" hint no longer claims bd bootstrap is a safe first step for "an existing project" — only for genuine fresh-clone / shared-server-recovery, which is what the existing tests actually cover. --- cmd/bd/doctor/gitignore.go | 8 +++--- cmd/bd/doctor/gitignore_test.go | 12 ++++----- cmd/bd/init.go | 34 ++++++++++++++++++------ cmd/bd/init_guard.go | 46 +++++++++++++++++++++++++++++++++ internal/storage/dolt/errors.go | 10 ++++++- 5 files changed, 91 insertions(+), 19 deletions(-) diff --git a/cmd/bd/doctor/gitignore.go b/cmd/bd/doctor/gitignore.go index ebafa5dc8..c332ec652 100644 --- a/cmd/bd/doctor/gitignore.go +++ b/cmd/bd/doctor/gitignore.go @@ -154,7 +154,7 @@ func CheckGitignore(repoPath string) DoctorCheck { Name: "Gitignore", Status: "warning", Message: ".beads/.gitignore not found", - Fix: "Run: bd init (safe to re-run) or bd doctor --fix", + Fix: "Run: bd doctor --fix", } } @@ -167,7 +167,7 @@ func CheckGitignore(repoPath string) DoctorCheck { Status: "warning", Message: "Outdated .beads/.gitignore (missing required patterns)", Detail: "Missing: " + strings.Join(missing, ", "), - Fix: "Run: bd doctor --fix or bd init (safe to re-run)", + Fix: "Run: bd doctor --fix", } } @@ -727,7 +727,7 @@ func CheckProjectGitignore(repoPath string) DoctorCheck { Name: "Project Gitignore", Status: StatusWarning, Message: "No project .gitignore found — Dolt/credential files may be committed accidentally", - Fix: "Run: bd init (safe to re-run) or bd doctor --fix", + Fix: "Run: bd doctor --fix", } } return DoctorCheck{ @@ -751,7 +751,7 @@ func CheckProjectGitignore(repoPath string) DoctorCheck { Status: StatusWarning, Message: "Project .gitignore missing required exclusion patterns", Detail: "Missing: " + strings.Join(missing, ", "), - Fix: "Run: bd doctor --fix or bd init (safe to re-run)", + Fix: "Run: bd doctor --fix", } } diff --git a/cmd/bd/doctor/gitignore_test.go b/cmd/bd/doctor/gitignore_test.go index e123d53c7..ab28e41f0 100644 --- a/cmd/bd/doctor/gitignore_test.go +++ b/cmd/bd/doctor/gitignore_test.go @@ -951,7 +951,7 @@ func TestCheckGitignore_VariousStatuses(t *testing.T) { // Don't create .beads directory }, expectedStatus: StatusWarning, - expectedFix: "Run: bd init (safe to re-run) or bd doctor --fix", + expectedFix: "Run: bd doctor --fix", description: "returns warning when .beads directory doesn't exist", }, { @@ -963,7 +963,7 @@ func TestCheckGitignore_VariousStatuses(t *testing.T) { } }, expectedStatus: StatusWarning, - expectedFix: "Run: bd init (safe to re-run) or bd doctor --fix", + expectedFix: "Run: bd doctor --fix", description: "returns warning when .gitignore doesn't exist", }, { @@ -999,7 +999,7 @@ daemon.log } }, expectedStatus: StatusWarning, - expectedFix: "Run: bd doctor --fix or bd init (safe to re-run)", + expectedFix: "Run: bd doctor --fix", description: "returns warning when missing required patterns like dolt/ and redirect", }, { @@ -1018,7 +1018,7 @@ daemon.log } }, expectedStatus: StatusWarning, - expectedFix: "Run: bd doctor --fix or bd init (safe to re-run)", + expectedFix: "Run: bd doctor --fix", description: "returns warning when missing multiple patterns", }, { @@ -1034,7 +1034,7 @@ daemon.log } }, expectedStatus: StatusWarning, - expectedFix: "Run: bd doctor --fix or bd init (safe to re-run)", + expectedFix: "Run: bd doctor --fix", description: "returns warning for empty file", }, { @@ -1054,7 +1054,7 @@ daemon.log } }, expectedStatus: StatusWarning, - expectedFix: "Run: bd doctor --fix or bd init (safe to re-run)", + expectedFix: "Run: bd doctor --fix", description: "returns warning for comments-only file", }, { diff --git a/cmd/bd/init.go b/cmd/bd/init.go index 5064509d2..4296bb1f9 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -373,6 +373,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): reinitLocal, _ := cmd.Flags().GetBool("reinit-local") initIfMissing, _ := cmd.Flags().GetBool("init-if-missing") discardRemote, _ := cmd.Flags().GetBool("discard-remote") + recreateMissing, _ := cmd.Flags().GetBool("recreate-missing") nonInteractiveFlag, _ := cmd.Flags().GetBool("non-interactive") roleFlag, _ := cmd.Flags().GetString("role") fromJSONL, _ := cmd.Flags().GetBool("from-jsonl") @@ -800,6 +801,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): // CheckRemoteSafety at cmd/bd/init_safety.go and // engdocs/adr/0002-init-safety-invariants.md). if !reinitLocal { + initAllowRecreateMissing = recreateMissing if err := checkExistingBeadsData(prefix); err != nil { // --init-if-missing makes init idempotent, but ONLY for the // benign "workspace already initialized" case. An operational @@ -2273,6 +2275,7 @@ func init() { initCmd.Flags().Bool("force", false, "Deprecated alias for --reinit-local. Bypasses only the LOCAL data-safety guard; does NOT authorize remote divergence (see 'bd help init-safety').") initCmd.Flags().Bool("reinit-local", false, "Re-initialize local .beads/ over existing local data. Does NOT authorize remote divergence; see --discard-remote.") initCmd.Flags().Bool("discard-remote", false, "Authorize discarding the configured remote's Dolt history when re-initializing. Requires --destroy-token in non-interactive mode; see 'bd help init-safety'.") + initCmd.Flags().Bool("recreate-missing", false, "Explicitly authorize creating a fresh, empty database when this project's configured server-mode database is missing or unreachable. Opt-in per invocation only; never implied by --force, config, or env (see 'bd help init-safety').") initCmd.Flags().Bool("from-jsonl", false, "Import issues from configured import.path; refuses remote history unless --discard-remote authorizes replacement") initCmd.Flags().Bool("init-if-missing", false, "If the workspace is already initialized, skip init and exit 0 instead of failing (idempotent init for scaffolds)") initCmd.Flags().String("destroy-token", "", "Explicit confirmation token for destructive re-init in non-interactive mode (format: 'DESTROY-')") @@ -2529,21 +2532,36 @@ Aborting.`, ui.RenderWarn("⚠"), location, ui.RenderAccent("bd list"), prefix) password := cfg.GetDoltServerPassword() user := cfg.GetDoltServerUser() + // doltDirExists==false is ambiguous in server mode: it's the normal + // state both for a genuine fresh clone (GH#2433) and for an existing + // project whose server-side database was lost or is unreachable + // (be-5up5: 2026-08-11 fleet-wide data loss). project_id is only + // written by a real prior `bd init`, so a non-empty value here + // proves the latter — recovery, not a fresh clone. + existingProject := cfg.ProjectID != "" + result := checkDatabaseOnServer(host, port, user, password, dbName, cfg.GetDoltServerTLS()) if result.Reachable && !result.Exists && result.Err == nil { - // Server is up but DB doesn't exist. Since we also know - // doltDirExists==false, this is a fresh clone — there's no - // local database to protect. Allow init to proceed so the - // user can bootstrap (e.g. via --from-jsonl). (GH#2433) + // Server is up but DB doesn't exist. + if existingProject && !initAllowRecreateMissing { + return initGuardMissingServerDBMessage(dbName, host, port, prefix) + } + // Fresh clone (GH#2433) or explicit --recreate-missing opt-in — + // there's no local database to protect. Allow init to proceed so + // the user can bootstrap (e.g. via --from-jsonl). return nil } if result.Reachable && result.Exists { // Server up and DB exists — fall through to "already initialized" error. } else { - // Server unreachable or error during check: this is a fresh clone - // with committed metadata.json but no local dolt/ directory. - // Allow init to proceed so the user can bootstrap the database - // (e.g. via --from-jsonl). (GH#2433) + // Server unreachable or error during check. + if existingProject && !initAllowRecreateMissing { + return initGuardMissingServerDBMessage(dbName, host, port, prefix) + } + // Fresh clone with committed metadata.json but no local dolt/ + // directory, or explicit --recreate-missing opt-in — allow init + // to proceed so the user can bootstrap the database (e.g. via + // --from-jsonl). return nil } } diff --git a/cmd/bd/init_guard.go b/cmd/bd/init_guard.go index febc23452..7245f943b 100644 --- a/cmd/bd/init_guard.go +++ b/cmd/bd/init_guard.go @@ -113,3 +113,49 @@ func initGuardServerMessage(dbName, host string, port int, prefix, syncRemote st b.WriteString("\nAborting.") return errors.New(b.String()) } + +// initAllowRecreateMissing is threaded from --recreate-missing for the +// duration of a single `bd init` invocation (be-5up5). It must only ever be +// set from that explicit per-invocation flag — never from a config key, env +// var, or implied by --force/--reinit-local — because it authorizes the one +// thing this file's guard exists to prevent: creating a fresh database when +// an existing project's configured one is missing or unreachable. +var initAllowRecreateMissing bool + +// initGuardMissingServerDBMessage builds the refusal error for be-5up5: an +// existing project (metadata.json carries a project_id, written only by a +// real prior `bd init`) whose configured server-mode database is absent or +// the server could not be reached to confirm. This differs from +// initGuardServerMessage above (used when there is no evidence either way +// about prior initialization, where `bd bootstrap` is a reasonable next +// step): here metadata.json already proves the project was previously +// initialized, so recommending bootstrap would route straight back into the +// same class of silent-empty-database creation this guard exists to stop +// (2026-08-11 fleet-wide data loss). Point at restore-from-export instead, +// and require the explicit --recreate-missing opt-in before bd init will +// create anything. +func initGuardMissingServerDBMessage(dbName, host string, port int, prefix string) error { + var b strings.Builder + fmt.Fprintf(&b, "\n%s Database %q not found on server at %s:%d (or the server could not be reached to confirm).\n", ui.RenderWarn("⚠"), dbName, host, port) + b.WriteString("This workspace was already initialized (metadata.json has a project_id from a prior\n") + b.WriteString("bd init), so this looks like a recovery situation, not a fresh clone: the configured\n") + b.WriteString("database is missing from the server, or the server could not be reached to check.\n") + + b.WriteString("\nbd init will NOT create an empty database here — that would strand any existing\n") + b.WriteString("issue data behind a new, empty database of the same name.\n") + + b.WriteString("\nDiagnose with:\n") + b.WriteString(" bd doctor # check project health\n") + b.WriteString(" bd dolt status # inspect Dolt server state\n") + + b.WriteString("\nTo recover existing data, restore from an export rather than creating fresh:\n") + b.WriteString(" bd backup restore # if a local backup snapshot exists\n") + b.WriteString(" Check .beads/backup/ for a JSONL export you can import manually.\n") + + b.WriteString("\nIf you are certain no recoverable data exists and want a fresh, empty database at\n") + b.WriteString("this name, opt in explicitly for this one invocation:\n") + fmt.Fprintf(&b, " bd init --recreate-missing --prefix %s\n", prefix) + + b.WriteString("\nAborting.") + return errors.New(b.String()) +} diff --git a/internal/storage/dolt/errors.go b/internal/storage/dolt/errors.go index de359dd92..23cccfb92 100644 --- a/internal/storage/dolt/errors.go +++ b/internal/storage/dolt/errors.go @@ -189,9 +189,17 @@ func databaseNotFoundError(cfg *Config) error { if cfg.SyncRemote != "" { fmt.Fprintf(&b, "\n\nTip: sync.remote is configured (%s).\nRun bd bootstrap to recover from the remote or confirm what bootstrap will do with --dry-run.", cfg.SyncRemote) } else { - b.WriteString("\n\nTip: If this is an existing project, fresh clone, or shared-server recovery, run bd bootstrap first.\n") + // be-5up5: no longer claims bootstrap is a safe first step "if this is an + // existing project" — bootstrap can silently create an empty database in + // server mode (2026-08-11 fleet-wide loss), so that framing pointed + // existing projects with a missing database at the same danger this guard + // exists to stop. Fresh-clone / shared-server-recovery guidance (the + // common, safe case) is unchanged. + b.WriteString("\n\nTip: If this is a fresh clone or shared-server recovery, run bd bootstrap first.\n") b.WriteString("If bootstrap cannot find the expected remote automatically, set sync.remote\nin .beads/config.yaml and re-run bd bootstrap.\n") b.WriteString("Use bd bootstrap --dry-run if you need to confirm the plan before it initializes anything.\n") + b.WriteString("If this is an existing project whose database went missing, run bd doctor before\n") + b.WriteString("bootstrap or init — neither is guaranteed to recover it safely.\n") b.WriteString("Use bd init only when creating a brand-new project with no existing .beads data.") }