diff --git a/cmd/bd/bootstrap.go b/cmd/bd/bootstrap.go index 772e2f446..0c9f4e05f 100644 --- a/cmd/bd/bootstrap.go +++ b/cmd/bd/bootstrap.go @@ -29,6 +29,7 @@ import ( "github.com/steveyegge/beads/internal/storage/embeddeddolt" "github.com/steveyegge/beads/internal/storage/schema" "github.com/steveyegge/beads/internal/storage/versioncontrolops" + "github.com/steveyegge/beads/internal/ui" "github.com/steveyegge/beads/internal/utils" "golang.org/x/term" ) @@ -119,7 +120,9 @@ Bootstrap auto-detects the right action: • If git origin has Dolt data (refs/dolt/data): clones from git and wires origin for future push/pull • If .beads/backup/*.jsonl exists: restores from backup • If .beads/issues.jsonl exists: imports from git-tracked JSONL - • If no database exists: creates a fresh one + • If no database exists: creates a fresh one — unless the workspace was already + initialized (metadata.json has a project_id) and its server-mode database is + missing, in which case bootstrap refuses rather than strand the existing data • If database already exists: validates and reports status If sync.remote points at a git repository, bootstrap verifies refs/dolt/data @@ -294,7 +297,7 @@ func applyBootstrapMetadataRepair(beadsDir string, cfg *configfile.Config, apply // BootstrapPlan describes what bootstrap will do. type BootstrapPlan struct { - Action string `json:"action"` // "sync", "restore", "jsonl-import", "init", "none" + Action string `json:"action"` // "sync", "restore", "jsonl-import", "init", "none", "refuse" Reason string `json:"reason"` // Human-readable explanation BeadsDir string `json:"beads_dir"` Database string `json:"database"` @@ -315,14 +318,28 @@ type BootstrapPlan struct { // Blocked marks a plan that took no action and found no database. It is // derived in detectBootstrapAction from Action+HasExisting, never set by a // detection branch, and it is the single discriminator the exit code and - // the printed output both switch on. Additive on purpose: JSON consumers - // switch on "action", so a new action value would break them, while - // "none" + "blocked" + a non-zero exit code is backwards compatible. + // the printed output both switch on. + // + // "blocked" is the stable, script-facing signal and must be set for EVERY + // no-database outcome, so a consumer that only understands "none" is never + // told a run succeeded when it did not. A new "action" value is acceptable + // only for a state that must not be conflated with an existing one — the + // refusal is one, because "none" also covers the benign "a database + // already exists, nothing to do" — and only when it is accompanied by + // "blocked" and, outside --dry-run, a non-zero exit code. That is the + // contract to preserve when extending this enum: not "never add an action", + // but "never add an action that is the only signal of what happened". Blocked bool `json:"blocked,omitempty"` // BlockedRemote is the credential-free remote URL a Blocked plan could not // verify, carried only so the diagnostic can name it. BlockedRemote string `json:"blocked_remote,omitempty"` HasExisting bool `json:"has_existing"` + + // refusalDetail is the full operator-facing explanation for + // Action=="refuse". It is unexported so the ANSI styling it carries never + // leaks into --json output; JSON consumers read the plain-text one-liner + // in Reason instead. + refusalDetail string } // MarshalJSON renders the plan for `bd bootstrap --json` (and `--dry-run --json`) @@ -394,8 +411,24 @@ func requireBootstrapDoltBackend(cfg *configfile.Config, beadsDir string) error // branch can leave a stale flag behind for a later branch to inherit. func detectBootstrapAction(beadsDir string, cfg *configfile.Config) BootstrapPlan { plan := detectBootstrapPlan(beadsDir, cfg) - plan.Blocked = plan.Action == "none" && !plan.HasExisting - if !plan.Blocked { + // "refuse" is a no-database outcome too, so it carries the same flag as + // "none": the action string says WHY bootstrap stopped, Blocked says THAT + // it stopped, and only the latter is the contract scripts switch on. This + // changes no exit code — RunE routes Action=="refuse" through + // executeBootstrapPlan, which errors, and --dry-run is contractually + // exit 0 either way (see bootstrapPlanOutcome) — it only stops a refusal + // from being the one bootstrap outcome that produced nothing yet reported + // "blocked" absent. + plan.Blocked = (plan.Action == "none" || plan.Action == "refuse") && !plan.HasExisting + if plan.Blocked { + // Keep SyncRemote's documented "empty on a Blocked plan" invariant + // true now that refusals are Blocked: a refusal can be reached with a + // forge SyncRemote already recorded (planConfiguredSyncRemote keeps it + // for the workspace it expected to create), and no Blocked plan wires + // up, clones from, or pushes to a remote. Inert for execution — every + // consumer of plan.SyncRemote runs on a sync/provisioning action. + plan.SyncRemote = "" + } else { plan.BlockedRemote = "" } return plan @@ -436,8 +469,22 @@ func detectBootstrapPlan(beadsDir string, cfg *configfile.Config) BootstrapPlan // probe-error branch, which then inherited HasExisting=true and printed the // #5743 success tick again. var deferredExistingPlan *BootstrapPlan - if dbAction, ok := existingBootstrapDBPlan(beadsDir, cfg, isServer, isSharedServer); ok { - if beadsDirExists || dbAction.Action != "none" { + dbAction, dbProbe, hasExistingDB := existingBootstrapDBPlan(beadsDir, cfg, isServer, isSharedServer) + // The same masking hazard applies to a server-side hit with no local + // evidence. Probing on ProjectID alone (see existingBootstrapDBPlan) means + // a shadow-dir-less workspace now reaches the Exists arm, where the only + // thing matched is the database NAME — nothing checks that the database on + // the server holds this project's data. Such a workspace previously + // skipped the probe entirely and always fell through to sync.remote, so + // settling here would let a stale or unrelated same-named database (including + // one left behind by the very bug this guard fixes) silently suppress the + // configured recovery path. Defer it exactly as a missing beadsDir is + // deferred: sync.remote / refs/dolt/data / local files get their turn, and + // the name match survives as the fallback verdict rather than becoming the + // verdict. + settledOnNameAlone := dbProbe.Ran && !dbProbe.LocalEvidence + if hasExistingDB { + if dbAction.Action != "none" || (beadsDirExists && !settledOnNameAlone) { return dbAction } deferredExistingPlan = &dbAction @@ -484,6 +531,16 @@ func detectBootstrapPlan(beadsDir string, cfg *configfile.Config) BootstrapPlan return plan } + // Every recovery source has now been ruled out, so the fall-through below + // is a fresh, empty database. Decide the refusal HERE rather than inside + // executeInitAction: at plan time the answer reaches --dry-run and --json, + // printBootstrapPlan never advertises "will create fresh database" for a + // plan that cannot run, and confirmPrompt never asks the operator to + // approve one. The probe result is reused, so no second dial is needed. + if refusal, refused := refuseServerModeInitPlan(plan, cfg, isServer, isSharedServer, dbProbe); refused { + return refusal + } + // Fresh setup plan.Action = "init" plan.Reason = "No existing database, remote, or backup — will create fresh database" @@ -600,77 +657,176 @@ func localFileRecoveryPlan(plan BootstrapPlan, beadsDir string) (BootstrapPlan, return BootstrapPlan{}, false } -func existingBootstrapDBPlan(beadsDir string, cfg *configfile.Config, isServer, isSharedServer bool) (BootstrapPlan, bool) { +// refuseServerModeInitPlan turns a would-be fresh "init" into an explicit +// "refuse" plan when metadata.json proves this workspace was already +// initialized (ProjectID set) and the configured server-mode database could +// not be confirmed to exist. Creating one here would strand the existing issue +// data behind a new, empty database of the same name — the same root cause +// PR #5791 (open, not yet merged) addresses for bd init. +// +// Reaching the init fall-through already implies the database was not +// confirmed: existingBootstrapDBPlan returns a settled "none" plan whenever a +// probe found it. The probe is passed in rather than re-run so the refusal +// costs no extra round-trip and can quote why the check failed. +func refuseServerModeInitPlan(plan BootstrapPlan, cfg *configfile.Config, isServer, isSharedServer bool, probe serverDBProbe) (BootstrapPlan, bool) { + if !isServer || cfg.ProjectID == "" || !probe.Ran || probe.Exists { + return BootstrapPlan{}, false + } + host := cfg.GetDoltServerHost() + port := bootstrapServerPort(plan.BeadsDir, cfg, isSharedServer) + plan.Action = "refuse" + plan.Reason = bootstrapMissingServerDBSummary(plan.Database, host, port, probe.Err) + plan.refusalDetail = bootstrapMissingServerDBRefusal(plan.Database, host, port, probe.Err).Error() + return plan, true +} + +// serverDBProbe records what a server-mode existence probe concluded, so the +// answer can be reused by the init fall-through instead of dialing the server +// a second time. +type serverDBProbe struct { + Ran bool // a probe was actually performed + Exists bool // the server confirmed the database exists + Err error // the probe could not reach or question the server + // LocalEvidence records whether anything on this machine ties this + // workspace to the database the probe asked about — in practice a + // populated local shadow directory. Without it the probe can only match + // on the database NAME, which is why the caller refuses to let an + // Exists verdict settle the plan ahead of the real recovery sources. + LocalEvidence bool +} + +// existingBootstrapDBPlan reports whether an existing database already settles +// the bootstrap plan. The returned serverDBProbe carries the server-mode +// probe result — including on the false path, where the caller needs it to +// decide whether a fall-through to "init" is safe. +func existingBootstrapDBPlan(beadsDir string, cfg *configfile.Config, isServer, isSharedServer bool) (BootstrapPlan, serverDBProbe, bool) { plan := BootstrapPlan{ BeadsDir: beadsDir, Database: cfg.GetDoltDatabase(), } - var dbPath string if isServer { - dbPath = bootstrapServerDoltDir(beadsDir, cfg, isSharedServer) - } else { - dbPath = filepath.Join(beadsDir, "embeddeddolt") + // Scope note: this is per-workspace evidence in locally-managed server + // mode only. In shared-server mode bootstrapServerDoltDir resolves to + // doltserver.SharedDoltDir() — ~/.beads/shared-server/dolt/, one + // directory for EVERY shared-server database on the machine — so a + // fresh clone of project B reads a populated directory belonging to + // projects A and C. Everything below that calls this "local evidence" + // is therefore only sound for the locally-managed case; the + // shared-server granularity gap is pre-existing (main probes on the + // same signal) and is tracked separately rather than narrowed here, + // because keying on the database-named subdirectory would also change + // the locally-managed verdict for a data dir holding only .doltcfg. + dbPath := bootstrapServerDoltDir(beadsDir, cfg, isSharedServer) + hasLocalShadowDir := false + if info, err := os.Stat(dbPath); err == nil && info.IsDir() { + if entries, _ := os.ReadDir(dbPath); len(entries) > 0 { + hasLocalShadowDir = true + } + } + // A missing/empty local shadow directory normally means no database + // exists yet. But when metadata.json carries a ProjectID, this + // workspace was already initialized somewhere — most likely a fresh + // clone of an already-bootstrapped server-mode project, which has no + // local shadow directory by design. In that case, skip the local-dir + // short-circuit and live-probe the server directly rather than + // concluding "no database" from local-disk state alone. + if !hasLocalShadowDir && cfg.ProjectID == "" { + return BootstrapPlan{}, serverDBProbe{}, false + } + + // Retry the reachable-but-absent window only when the local shadow + // directory says this database really does live here; see + // probeBootstrapServerDB for why a fresh clone must not pay 70s. + result := probeBootstrapServerDB(beadsDir, cfg, isSharedServer, hasLocalShadowDir) + probe := serverDBProbe{Ran: true, Exists: result.Exists, Err: result.Err, LocalEvidence: hasLocalShadowDir} + + if result.Exists { + plan.HasExisting = true + plan.Action = "none" + plan.Reason = fmt.Sprintf("Database %s already exists on server at %s:%d", cfg.GetDoltDatabase(), cfg.GetDoltServerHost(), bootstrapServerPort(beadsDir, cfg, isSharedServer)) + return plan, probe, true + } + // An unverifiable probe means UNKNOWN, never "exists". Answering + // "nothing to do" for it is only defensible with local evidence that + // the database lives here — a populated shadow directory (in + // locally-managed server mode; see the scope note above, it is not + // per-database in shared-server mode), where a down server is a live + // database we simply cannot see right now. + // Without that evidence the very same error describes a fresh clone + // whose server has not been started yet, which is the normal state of + // every locally-managed server-mode clone before its first sync. Such + // a clone must stay eligible for sync.remote, refs/dolt/data, + // .beads/backup/ and issues.jsonl, so report "not found" and let + // detectBootstrapAction consult them. + // + // When it does settle here, Blocked is still not hand-set: + // detectBootstrapAction derives it from Action=="none" && !HasExisting + // (HasExisting is false on this branch), keeping one owner for the flag + // and the #5743 non-zero exit rather than a false success tick. + if result.Err != nil && hasLocalShadowDir { + plan.Action = "none" + plan.Reason = fmt.Sprintf("Could not verify existing server database %s: %v", cfg.GetDoltDatabase(), result.Err) + return plan, probe, true + } + return BootstrapPlan{}, probe, false } + + dbPath := filepath.Join(beadsDir, "embeddeddolt") if info, err := os.Stat(dbPath); err != nil || !info.IsDir() { - return BootstrapPlan{}, false + return BootstrapPlan{}, serverDBProbe{}, false } entries, _ := os.ReadDir(dbPath) if len(entries) == 0 { - return BootstrapPlan{}, false - } - - if isServer { - probeCfg := bootstrapServerProbeConfig{ - host: cfg.GetDoltServerHost(), - port: bootstrapServerPort(beadsDir, cfg, isSharedServer), - user: cfg.GetDoltServerUser(), - pass: cfg.GetDoltServerPassword(), - database: cfg.GetDoltDatabase(), - tls: cfg.GetDoltServerTLS(), - } - // When the server is reachable but the DB appears absent, retry with - // exponential backoff before concluding the DB is genuinely missing. - // A managed Dolt restart completes in <30 s; three retries over 70 s - // cover all observed restart windows. - retryDelays := []time.Duration{10 * time.Second, 20 * time.Second, 40 * time.Second} - var result bootstrapServerDBCheck - for attempt := 0; ; attempt++ { - result = checkBootstrapServerDB(probeCfg) - if result.Err != nil || result.Exists || !result.Reachable { - break - } - if attempt >= len(retryDelays) { - break - } - fmt.Fprintf(os.Stderr, "Database %s not found on reachable server (attempt %d/%d), retrying in %v (possible transient restart)\n", - cfg.GetDoltDatabase(), attempt+1, len(retryDelays), retryDelays[attempt]) - bootstrapRetryDelay(retryDelays[attempt]) - } - if result.Err != nil { - // Same "declined, and no database was confirmed" class as a - // failed remote probe: report it and exit non-zero rather than - // printing a success tick (#5743). Blocked is not hand-set here — - // detectBootstrapAction derives it from Action=="none" && !HasExisting - // (HasExisting is false on this branch), keeping one owner for the flag. - plan.Action = "none" - plan.Reason = fmt.Sprintf("Could not verify existing server database %s: %v", cfg.GetDoltDatabase(), result.Err) - return plan, true - } - if result.Exists { - plan.HasExisting = true - plan.Action = "none" - plan.Reason = fmt.Sprintf("Database %s already exists on server at %s:%d", probeCfg.database, probeCfg.host, probeCfg.port) - return plan, true - } - return BootstrapPlan{}, false + return BootstrapPlan{}, serverDBProbe{}, false } plan.HasExisting = true plan.Action = "none" plan.Reason = "Database already exists at " + dbPath - return plan, true + return plan, serverDBProbe{}, true +} + +// probeBootstrapServerDB checks whether the configured database exists on +// the Dolt server. +// +// When retryTransient is set, a reachable server that reports the database +// absent is retried with backoff, because a managed Dolt restart can +// transiently look like a missing database for up to ~30s. That budget costs +// 70s, so it is only worth paying when something independent says the +// database really is supposed to be here — in practice, a populated local +// shadow directory. A caller with no such evidence (a fresh clone, which by +// design has no shadow directory) is not watching a database disappear; it is +// simply ahead of its first sync, and must pass retryTransient=false so the +// answer costs one round-trip instead of 70s. +func probeBootstrapServerDB(beadsDir string, cfg *configfile.Config, isSharedServer, retryTransient bool) bootstrapServerDBCheck { + probeCfg := bootstrapServerProbeConfig{ + host: cfg.GetDoltServerHost(), + port: bootstrapServerPort(beadsDir, cfg, isSharedServer), + user: cfg.GetDoltServerUser(), + pass: cfg.GetDoltServerPassword(), + database: cfg.GetDoltDatabase(), + tls: cfg.GetDoltServerTLS(), + } + if !retryTransient { + return checkBootstrapServerDB(probeCfg) + } + retryDelays := []time.Duration{10 * time.Second, 20 * time.Second, 40 * time.Second} + var result bootstrapServerDBCheck + for attempt := 0; ; attempt++ { + result = checkBootstrapServerDB(probeCfg) + if result.Err != nil || result.Exists || !result.Reachable { + break + } + if attempt >= len(retryDelays) { + break + } + fmt.Fprintf(os.Stderr, "Database %s not found on reachable server (attempt %d/%d), retrying in %v (possible transient restart)\n", + cfg.GetDoltDatabase(), attempt+1, len(retryDelays), retryDelays[attempt]) + bootstrapRetryDelay(retryDelays[attempt]) + } + return result } func bootstrapSharedServerMode(beadsDir string) bool { @@ -769,6 +925,21 @@ func printBootstrapPlan(plan BootstrapPlan) { case "init": fmt.Printf("Bootstrap plan: create fresh database\n") fmt.Printf(" Database: %s\n", plan.Database) + case "refuse": + // Deliberately the one-line summary, not refusalDetail: the full + // explanation is the error executeBootstrapPlan returns, and printing + // both would render it twice. + // + // Deliberately stdout, unlike the "none"+Blocked arm above: this is the + // plan the command was asked to compute, and the one-line Reason rides + // with it (pinned by TestPrintBootstrapPlan_RefusePrintsSummaryNotDetail) + // so a stdout-only consumer is never left with a bare "refuse". It is + // not the #5743 false-success class either — a real refuse run exits + // non-zero through executeBootstrapPlan, so nothing reads this as + // success. + fmt.Printf("Bootstrap plan: refuse — will NOT create a database\n") + fmt.Printf(" Database: %s\n", plan.Database) + fmt.Printf(" Reason: %s\n", plan.Reason) } } @@ -792,6 +963,16 @@ func executeBootstrapPlan(plan BootstrapPlan, cfg *configfile.Config, nonInterac if err := requireBootstrapDoltBackend(cfg, plan.BeadsDir); err != nil { return err } + // A refusal is a decision, not an action awaiting approval: surface it + // before confirmPrompt so the operator is never asked to approve it, and + // before the workspace gates below so no state is touched. + if plan.Action == "refuse" { + detail := plan.refusalDetail + if detail == "" { + detail = plan.Reason + } + return errors.New(detail) + } if !confirmPrompt("Proceed?", nonInteractive) { fmt.Fprintf(os.Stderr, "Aborted.\n") return nil @@ -836,6 +1017,10 @@ func executeBootstrapPlan(plan BootstrapPlan, cfg *configfile.Config, nonInterac } func executeInitAction(ctx context.Context, plan BootstrapPlan, cfg *configfile.Config) error { + if err := refuseServerModeInitForExistingProject(plan, cfg); err != nil { + return err + } + prefix := inferPrefix(cfg) dbName := cfg.GetDoltDatabase() @@ -884,6 +1069,86 @@ func wireBootstrapDoltRemote(ctx context.Context, s storage.DoltStorage, plan Bo configureInitDoltRemote(ctx, s, doltRemoteURL(plan.SyncRemote), false) } +// refuseServerModeInitForExistingProject is the belt-and-braces half of the +// guard whose real decision is made at plan time by refuseServerModeInitPlan. +// It re-checks, at the last moment before a database would actually be +// created, that this workspace is not an already-initialized project +// (ProjectID set) whose server-mode database cannot be confirmed to exist — +// covering any caller that reaches executeInitAction with a plan that did not +// come from detectBootstrapAction. +// +// It deliberately probes ONCE. The transient-restart backoff belongs to the +// plan-time probe; repeating it here would charge a second 70s to a decision +// that has already been made, and refusing is the safe direction anyway. +func refuseServerModeInitForExistingProject(plan BootstrapPlan, cfg *configfile.Config) error { + isSharedServer := bootstrapSharedServerMode(plan.BeadsDir) + if (!cfg.IsDoltServerMode() && !isSharedServer) || cfg.ProjectID == "" { + return nil + } + result := probeBootstrapServerDB(plan.BeadsDir, cfg, isSharedServer, false) + if result.Exists { + return nil + } + host := cfg.GetDoltServerHost() + port := bootstrapServerPort(plan.BeadsDir, cfg, isSharedServer) + return bootstrapMissingServerDBRefusal(cfg.GetDoltDatabase(), host, port, result.Err) +} + +// bootstrapMissingServerDBSummary is the single-line, unstyled form of +// bootstrapMissingServerDBRefusal, used as BootstrapPlan.Reason so --json +// consumers get the explanation without ANSI escapes. +func bootstrapMissingServerDBSummary(dbName, host string, port int, probeErr error) string { + if probeErr != nil { + return fmt.Sprintf("Could not verify database %q on Dolt server at %s:%d (%v); workspace already initialized (project_id set) — refusing to create an empty database", dbName, host, port, probeErr) + } + return fmt.Sprintf("Database %q not found on server at %s:%d; workspace already initialized (project_id set) — refusing to create an empty database", dbName, host, port) +} + +// bootstrapMissingServerDBRefusal builds the refusal error shown when bd +// bootstrap would otherwise create a fresh, empty server-mode database for a +// workspace that was already initialized elsewhere. Per GH#2363, this must +// never suggest a destructive "recreate" action as an actionable next step. +// +// The equivalent guard for `bd init` is proposed in PR #5791, which is open +// and not yet merged; this wording is kept deliberately close to it so the +// two can share one message once that lands. +func bootstrapMissingServerDBRefusal(dbName, host string, port int, probeErr error) error { + var b strings.Builder + if probeErr != nil { + fmt.Fprintf(&b, "\n%s Could not verify database %q on Dolt server at %s:%d: %v\n", ui.RenderWarn("⚠"), dbName, host, port, probeErr) + } else { + fmt.Fprintf(&b, "\n%s Database %q not found on server at %s:%d.\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/bootstrap), so this looks like a recovery situation, not a fresh clone.\n") + + b.WriteString("\nbd bootstrap 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") + + // Every route here must be followable FROM this state. `bd backup restore` + // alone is not: it restores a Dolt-native snapshot, not a `bd export` + // JSONL, and it requires an initialized database — so it silently routes + // the operator back through `bd init`, the very unguarded door this + // refusal exists to keep shut (PR #5791). Name that precondition instead + // of implying it, and lead with the routes that need no init at all. + b.WriteString("\nTo recover existing data, repopulate the database rather than creating fresh.\n") + b.WriteString("Routes that work from this state:\n") + b.WriteString(" bd dolt push # from another clone that still has the data\n") + b.WriteString(" bd config set sync.remote # then re-run bd bootstrap to clone from it\n") + b.WriteString(" bd init && bd backup restore # ONLY with a Dolt-native backup snapshot:\n") + b.WriteString(" # bd backup restore needs an initialized\n") + b.WriteString(" # database and will not read a bd export JSONL\n") + b.WriteString("\nbd bootstrap only auto-restores .beads/backup/issues.jsonl, so a timestamped or\n") + b.WriteString("otherwise-named export under .beads/backup/ is still yours to import by hand.\n") + + b.WriteString("\nAborting.") + return errors.New(b.String()) +} + func executeRestoreAction(ctx context.Context, plan BootstrapPlan, cfg *configfile.Config) error { prefix := inferPrefix(cfg) dbName := cfg.GetDoltDatabase() diff --git a/cmd/bd/bootstrap_test.go b/cmd/bd/bootstrap_test.go index 2ab190820..981243e92 100644 --- a/cmd/bd/bootstrap_test.go +++ b/cmd/bd/bootstrap_test.go @@ -3,6 +3,9 @@ package main import ( + "context" + "encoding/json" + "errors" "fmt" "os" "path/filepath" @@ -1839,3 +1842,545 @@ func TestFinalizeSyncedBootstrapSharedServerSetsServerMode(t *testing.T) { t.Errorf("dolt_mode = %q, want %q — shared server should set server mode", loaded.GetDoltMode(), configfile.DoltModeServer) } } + +// TestDetectBootstrapAction_NoLocalShadowDirStillLiveChecksExisting reproduces +// be-cy41 bug #1: existingBootstrapDBPlan returned BootstrapPlan{}, false (and +// detectBootstrapAction fell through toward Action="init") purely because no +// local shadow dolt-data directory existed — but in server mode that is the +// NORMAL state for a fresh clone of an already-initialized project. When +// metadata.json carries a ProjectID (proving this workspace was previously +// initialized), the server-side existence check must still run instead of +// being short-circuited by local filesystem state that a legitimate clone +// will never have (the same ambiguity PR #5791 addresses for bd init; that +// PR is open and not yet merged, so nothing it adds is referenced here). +func TestDetectBootstrapAction_NoLocalShadowDirStillLiveChecksExisting(t *testing.T) { + t.Setenv("BEADS_DOLT_DATA_DIR", "") + t.Setenv("BEADS_DOLT_SHARED_SERVER", "") + t.Setenv("BEADS_DOLT_SERVER_DATABASE", "") + t.Setenv("BEADS_DOLT_SERVER_HOST", "") + t.Setenv("BEADS_DOLT_SERVER_PORT", "") + tmpDir := t.TempDir() + beadsDir := filepath.Join(tmpDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + // Deliberately do NOT create doltDataDir at all — no local shadow + // directory, which is the normal state for a fresh clone of a + // pre-existing server-mode project. + doltDataDir := filepath.Join(tmpDir, "dolt-data") + + oldWd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + defer func() { _ = os.Chdir(oldWd) }() + if err := os.Chdir(tmpDir); err != nil { + t.Fatal(err) + } + + cfg := configfile.DefaultConfig() + cfg.DoltMode = configfile.DoltModeServer + cfg.DoltDatabase = "project_exists" + cfg.DoltDataDir = doltDataDir + cfg.ProjectID = "proj-already-initialized-456" + t.Setenv("BEADS_DOLT_DATA_DIR", doltDataDir) + + probed := false + origCheck := checkBootstrapServerDB + checkBootstrapServerDB = func(probeCfg bootstrapServerProbeConfig) bootstrapServerDBCheck { + probed = true + if probeCfg.database != "project_exists" { + t.Fatalf("unexpected dbName: %s", probeCfg.database) + } + return bootstrapServerDBCheck{Exists: true, Reachable: true} + } + defer func() { checkBootstrapServerDB = origCheck }() + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(time.Duration) {} + defer func() { bootstrapRetryDelay = origDelay }() + + plan := detectBootstrapAction(beadsDir, cfg) + + if !probed { + t.Fatal("checkBootstrapServerDB was never called — a local-shadow-directory gate short-circuited the real server-side existence check before it could run") + } + if plan.Action != "none" { + t.Fatalf("action = %q, want %q — bootstrap must not plan to recreate a database that already exists on the server just because no local shadow directory is present", plan.Action, "none") + } + if !plan.HasExisting { + t.Error("HasExisting = false, want true") + } +} + +// TestExecuteInitAction_ServerModeExistingProjectMissingDBRefuses reproduces +// be-cy41 bug #2: executeInitAction is mode-blind — it always builds a +// dolt.Config with CreateIfMissing:true, AutoStart:true, and no server +// host/port/mode fields, so it silently creates an embedded-style database +// even for a server-mode workspace. When the workspace was already +// initialized (metadata.json has a project_id — the same signal PR #5791 uses) but +// the configured database is missing on the server, this silently strands any +// existing issue data behind a new, empty database of the same name. +// executeInitAction must refuse instead (the same root cause as PR #5791's +// proposed bd init guard, which is still open). +func TestExecuteInitAction_ServerModeExistingProjectMissingDBRefuses(t *testing.T) { + t.Setenv("BEADS_DOLT_DATA_DIR", "") + t.Setenv("BEADS_DOLT_SHARED_SERVER", "") + t.Setenv("BEADS_DOLT_SERVER_DATABASE", "") + t.Setenv("BEADS_DOLT_SERVER_HOST", "") + t.Setenv("BEADS_DOLT_SERVER_PORT", "") + tmpDir := t.TempDir() + beadsDir := filepath.Join(tmpDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + + oldWd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + defer func() { _ = os.Chdir(oldWd) }() + if err := os.Chdir(tmpDir); err != nil { + t.Fatal(err) + } + + cfg := configfile.DefaultConfig() + cfg.DoltMode = configfile.DoltModeServer + cfg.DoltDatabase = "myproj" + cfg.ProjectID = "proj-existing-123" + + origCheck := checkBootstrapServerDB + checkBootstrapServerDB = func(probeCfg bootstrapServerProbeConfig) bootstrapServerDBCheck { + return bootstrapServerDBCheck{Exists: false, Reachable: true} + } + defer func() { checkBootstrapServerDB = origCheck }() + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(time.Duration) {} + defer func() { bootstrapRetryDelay = origDelay }() + + plan := BootstrapPlan{BeadsDir: beadsDir, Database: "myproj", Action: "init"} + err = executeInitAction(context.Background(), plan, cfg) + + if err == nil { + t.Fatal("executeInitAction succeeded silently for an existing project with a missing server-mode database — want a refusal error") + } + if !strings.Contains(err.Error(), "myproj") { + t.Errorf("error %q does not mention the missing database name %q", err.Error(), "myproj") + } + if !strings.Contains(err.Error(), "will NOT create") && !strings.Contains(err.Error(), "not found on server") { + t.Errorf("error %q does not read as a refusal to auto-create", err.Error()) + } + if _, statErr := os.Stat(filepath.Join(beadsDir, "embeddeddolt")); statErr == nil { + t.Error("executeInitAction created an embedded database as a side effect of a refused server-mode init — no database should have been created at all") + } +} + +// freshServerModeCloneFixture builds the state a fresh clone of an +// already-bootstrapped server-mode project is actually in: .beads/ present +// with a configured sync.remote, metadata.json carrying a ProjectID, and no +// local shadow dolt-data directory (a clone never has one). +func freshServerModeCloneFixture(t *testing.T) (beadsDir string, cfg *configfile.Config) { + t.Helper() + snapshotBootstrapEnv(t) + config.ResetForTesting() + t.Cleanup(config.ResetForTesting) + t.Setenv("BEADS_DOLT_SHARED_SERVER", "") + t.Setenv("BEADS_DOLT_SERVER_HOST", "") + t.Setenv("BEADS_DOLT_SERVER_PORT", "") + tmpDir := t.TempDir() + beadsDir = filepath.Join(tmpDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("sync.remote: http://myserver:7007/mydb\n"), 0o644); err != nil { + t.Fatal(err) + } + t.Setenv("BEADS_DIR", beadsDir) + t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1") + if err := config.Initialize(); err != nil { + t.Fatalf("config.Initialize failed: %v", err) + } + t.Chdir(tmpDir) + cfg = configfile.DefaultConfig() + cfg.DoltMode = configfile.DoltModeServer + cfg.DoltDatabase = "mydb" + cfg.DoltDataDir = filepath.Join(tmpDir, "dolt-data") // never created: fresh clone + cfg.ProjectID = "proj-fresh-clone-789" + t.Setenv("BEADS_DOLT_DATA_DIR", cfg.DoltDataDir) + return beadsDir, cfg +} + +// TestDetectBootstrapAction_FreshCloneUnreachableServerStillSyncs pins the +// rule that an unverifiable probe means UNKNOWN, not "exists". A fresh clone +// of a locally-managed server-mode project has nothing running yet, so +// "connection refused" is its NORMAL state — it must not be converted into +// Action="none" ("nothing to do"), which would hide sync.remote, +// refs/dolt/data, .beads/backup/ and issues.jsonl behind a dead server. +func TestDetectBootstrapAction_FreshCloneUnreachableServerStillSyncs(t *testing.T) { + beadsDir, cfg := freshServerModeCloneFixture(t) + orig := checkBootstrapServerDB + checkBootstrapServerDB = func(bootstrapServerProbeConfig) bootstrapServerDBCheck { + return bootstrapServerDBCheck{Reachable: false, Err: errors.New("dial tcp 127.0.0.1:3307: connect: connection refused")} + } + defer func() { checkBootstrapServerDB = orig }() + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(time.Duration) {} + defer func() { bootstrapRetryDelay = origDelay }() + + plan := detectBootstrapAction(beadsDir, cfg) + if plan.Action != "sync" { + t.Errorf("action = %q (reason %q), want sync: an unreachable server must not veto sync.remote recovery on a fresh clone", plan.Action, plan.Reason) + } +} + +// TestDetectBootstrapAction_FreshCloneReachableAbsentSkipsTransientBackoff +// pins that the 10/20/40s transient-restart budget is only spent when a local +// shadow directory says the database really does live here. A fresh clone has +// no shadow directory by design, so "reachable but absent" is simply "not +// synced yet" and must cost one probe, not 70 seconds. +func TestDetectBootstrapAction_FreshCloneReachableAbsentSkipsTransientBackoff(t *testing.T) { + beadsDir, cfg := freshServerModeCloneFixture(t) + probes := 0 + orig := checkBootstrapServerDB + checkBootstrapServerDB = func(bootstrapServerProbeConfig) bootstrapServerDBCheck { + probes++ + return bootstrapServerDBCheck{Reachable: true, Exists: false} + } + defer func() { checkBootstrapServerDB = orig }() + var slept time.Duration + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(d time.Duration) { slept += d } + defer func() { bootstrapRetryDelay = origDelay }() + + plan := detectBootstrapAction(beadsDir, cfg) + if plan.Action != "sync" { + t.Errorf("action = %q, want sync", plan.Action) + } + if slept > 0 { + t.Errorf("fresh clone with no local DB paid %v of transient-restart backoff before sync", slept) + } + if probes != 1 { + t.Errorf("probes = %d, want exactly 1 — a clone with no local shadow dir has nothing to be transiently missing", probes) + } +} + +// TestDetectBootstrapAction_ShadowDirUnreachableServerStillReportsNone pins +// the other side of that rule: with a populated local shadow directory there +// IS local evidence the database lives here, so an unreachable server is a +// live database we cannot see right now and "nothing to do" remains the +// correct answer. This is main's long-standing behavior and must survive the +// narrowing above. +func TestDetectBootstrapAction_ShadowDirUnreachableServerStillReportsNone(t *testing.T) { + beadsDir, cfg := freshServerModeCloneFixture(t) + if err := os.MkdirAll(filepath.Join(cfg.DoltDataDir, "mydb"), 0o750); err != nil { + t.Fatal(err) + } + orig := checkBootstrapServerDB + checkBootstrapServerDB = func(bootstrapServerProbeConfig) bootstrapServerDBCheck { + return bootstrapServerDBCheck{Reachable: false, Err: errors.New("dial tcp 127.0.0.1:3307: connect: connection refused")} + } + defer func() { checkBootstrapServerDB = orig }() + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(time.Duration) {} + defer func() { bootstrapRetryDelay = origDelay }() + + plan := detectBootstrapAction(beadsDir, cfg) + if plan.Action != "none" { + t.Errorf("action = %q, want none — a populated shadow dir is local evidence the DB exists, so a down server means 'do nothing', not 'recover'", plan.Action) + } + if !strings.Contains(plan.Reason, "Could not verify") { + t.Errorf("reason = %q, want the 'Could not verify' wording", plan.Reason) + } +} + +// TestDetectBootstrapAction_ExistingProjectMissingServerDBRefusesAtPlanTime +// pins that the refusal is a PLAN, not an execute-time surprise. With every +// recovery source absent the fall-through would be a fresh, empty database; +// because metadata.json proves the workspace was already initialized, the +// plan itself must say "refuse" so --dry-run and --json report it and +// printBootstrapPlan never advertises "will create fresh database". +func TestDetectBootstrapAction_ExistingProjectMissingServerDBRefusesAtPlanTime(t *testing.T) { + snapshotBootstrapEnv(t) + config.ResetForTesting() + t.Cleanup(config.ResetForTesting) + t.Setenv("BEADS_DOLT_SHARED_SERVER", "") + t.Setenv("BEADS_DOLT_SERVER_HOST", "") + t.Setenv("BEADS_DOLT_SERVER_PORT", "") + tmpDir := t.TempDir() + beadsDir := filepath.Join(tmpDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + // No sync.remote, no backup, no issues.jsonl: nothing to recover from. + t.Setenv("BEADS_DIR", beadsDir) + t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1") + if err := config.Initialize(); err != nil { + t.Fatalf("config.Initialize failed: %v", err) + } + t.Chdir(tmpDir) + cfg := configfile.DefaultConfig() + cfg.DoltMode = configfile.DoltModeServer + cfg.DoltDatabase = "mydb" + cfg.DoltDataDir = filepath.Join(tmpDir, "dolt-data") + cfg.ProjectID = "proj-already-initialized-456" + t.Setenv("BEADS_DOLT_DATA_DIR", cfg.DoltDataDir) + + probes := 0 + orig := checkBootstrapServerDB + checkBootstrapServerDB = func(bootstrapServerProbeConfig) bootstrapServerDBCheck { + probes++ + return bootstrapServerDBCheck{Reachable: true, Exists: false} + } + defer func() { checkBootstrapServerDB = orig }() + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(time.Duration) {} + defer func() { bootstrapRetryDelay = origDelay }() + + plan := detectBootstrapAction(beadsDir, cfg) + + if plan.Action != "refuse" { + t.Fatalf("action = %q (reason %q), want refuse — an already-initialized workspace must never plan a fresh empty database", plan.Action, plan.Reason) + } + if probes != 1 { + t.Errorf("probes = %d, want exactly 1 — the refusal must reuse the plan-time probe, not dial again", probes) + } + if !strings.Contains(plan.Reason, "mydb") { + t.Errorf("reason = %q does not name the database", plan.Reason) + } + if strings.Contains(plan.Reason, "\x1b[") { + t.Errorf("reason = %q carries ANSI escapes; --json consumers must get plain text", plan.Reason) + } + if plan.refusalDetail == "" { + t.Error("refusalDetail is empty — the operator-facing explanation is lost") + } + if !strings.Contains(plan.refusalDetail, "will NOT create") { + t.Errorf("refusalDetail = %q does not read as a refusal", plan.refusalDetail) + } + + // The machine-readable surface is what provisioning scripts consume, and + // "blocked" — not the action string — is the discriminator they are + // documented to switch on (see the BootstrapPlan.Blocked comment). A + // refusal produced no database, so it must carry the flag; otherwise it is + // the one no-database outcome a "blocked"-keyed consumer reads as success. + if !plan.Blocked { + t.Error("Blocked = false on a refuse plan — a run that produced no database must set the flag scripts key on") + } + raw, err := json.Marshal(plan) + if err != nil { + t.Fatalf("json.Marshal(plan): %v", err) + } + var decoded map[string]any + if err := json.Unmarshal(raw, &decoded); err != nil { + t.Fatalf("unmarshal plan JSON %s: %v", raw, err) + } + if decoded["action"] != "refuse" { + t.Errorf("json action = %v, want refuse", decoded["action"]) + } + // "blocked" is omitempty, so a false flag does not render as false — the + // key vanishes entirely and the consumer cannot tell this run from a + // successful one. + if decoded["blocked"] != true { + t.Errorf("json blocked = %v, want true; full plan JSON: %s", decoded["blocked"], raw) + } + if strings.Contains(string(raw), "\x1b[") { + t.Errorf("plan JSON carries ANSI escapes, which --json consumers must never receive: %s", raw) + } +} + +// TestDetectBootstrapAction_RefusePlanDropsUnverifiedSyncRemote pins that a +// refusal honours SyncRemote's documented "empty on a Blocked plan" contract. +// A forge sync.remote with no Dolt data yet is recorded on the plan for the +// workspace bootstrap expected to create (planConfiguredSyncRemote keeps it +// and returns done=false), and the refusal is downstream of that — so without +// explicit clearing the credential-bearing URL rides out in --json for a run +// that wired up nothing, cloned nothing and pushed nothing. +func TestDetectBootstrapAction_RefusePlanDropsUnverifiedSyncRemote(t *testing.T) { + beadsDir := newForgeSyncRemoteWorkspace(t, "https://github.com/org/repo.git") + // Forge repo reachable, but no refs/dolt/data yet: sync.remote is recorded + // and bootstrap keeps looking rather than cloning. + stubProbeGitRemoteDoltData(t, func(string) (bool, error) { return false, nil }) + + cfg := configfile.DefaultConfig() + cfg.DoltMode = configfile.DoltModeServer + cfg.DoltDatabase = "mydb" + // Never created: a fresh clone has no local shadow directory. + cfg.DoltDataDir = filepath.Join(filepath.Dir(beadsDir), "dolt-data") + cfg.ProjectID = "proj-already-initialized-456" + t.Setenv("BEADS_DOLT_DATA_DIR", cfg.DoltDataDir) + + orig := checkBootstrapServerDB + checkBootstrapServerDB = func(bootstrapServerProbeConfig) bootstrapServerDBCheck { + return bootstrapServerDBCheck{Reachable: true, Exists: false} + } + defer func() { checkBootstrapServerDB = orig }() + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(time.Duration) {} + defer func() { bootstrapRetryDelay = origDelay }() + + plan := detectBootstrapAction(beadsDir, cfg) + + if plan.Action != "refuse" { + t.Fatalf("action = %q (reason %q), want refuse", plan.Action, plan.Reason) + } + if !plan.Blocked { + t.Fatal("Blocked = false on a refuse plan") + } + if plan.SyncRemote != "" { + t.Errorf("SyncRemote = %q on a Blocked plan, want empty — the refusal creates no workspace, so nothing may be cloned from or pushed to that remote", plan.SyncRemote) + } + raw, err := json.Marshal(plan) + if err != nil { + t.Fatalf("json.Marshal(plan): %v", err) + } + var decoded map[string]any + if err := json.Unmarshal(raw, &decoded); err != nil { + t.Fatalf("unmarshal plan JSON %s: %v", raw, err) + } + if got, ok := decoded["sync_remote"]; ok { + t.Errorf("plan JSON carries sync_remote = %v on a Blocked plan; full JSON: %s", got, raw) + } +} + +// TestPrintBootstrapPlan_RefusePrintsSummaryNotDetail covers the refuse arm of +// printBootstrapPlan, which no test reached. The full explanation is the error +// executeBootstrapPlan returns, so printing refusalDetail here too would render +// it twice — a regression a future edit could reintroduce silently. +func TestPrintBootstrapPlan_RefusePrintsSummaryNotDetail(t *testing.T) { + plan := BootstrapPlan{ + Action: "refuse", + Database: "mydb", + Reason: `Database "mydb" not found on server at 127.0.0.1:3307; workspace already initialized (project_id set) — refusing to create an empty database`, + // A marker no production string can produce, so a match is proof the + // detail field itself was printed rather than wording that overlaps. + refusalDetail: "REFUSAL-DETAIL-MARKER: bd bootstrap will NOT create an empty database here", + Blocked: true, + } + + out := captureStdout(t, func() error { + printBootstrapPlan(plan) + return nil + }) + + if !strings.Contains(out, "refuse — will NOT create a database") { + t.Errorf("output does not announce the refusal:\n%s", out) + } + if !strings.Contains(out, plan.Reason) { + t.Errorf("output omits the one-line reason:\n%s", out) + } + if strings.Contains(out, "REFUSAL-DETAIL-MARKER") { + t.Errorf("printBootstrapPlan printed refusalDetail; executeBootstrapPlan already returns it, so the operator would read the whole explanation twice:\n%s", out) + } +} + +// TestExecuteBootstrapPlan_RefuseErrsWithoutPrompting pins that a refuse plan +// never reaches confirmPrompt or the workspace gates: it is a decision, not an +// action awaiting approval. Before this, the refusal was discovered inside +// executeInitAction — i.e. after the operator had already answered "Proceed?" +// to a plan that said the opposite. +func TestExecuteBootstrapPlan_RefuseErrsWithoutPrompting(t *testing.T) { + snapshotBootstrapEnv(t) + config.ResetForTesting() + t.Cleanup(config.ResetForTesting) + tmpDir := t.TempDir() + beadsDir := filepath.Join(tmpDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + cfg := configfile.DefaultConfig() + cfg.DoltMode = configfile.DoltModeServer + cfg.DoltDatabase = "mydb" + cfg.ProjectID = "proj-existing-123" + + probes := 0 + orig := checkBootstrapServerDB + checkBootstrapServerDB = func(bootstrapServerProbeConfig) bootstrapServerDBCheck { + probes++ + return bootstrapServerDBCheck{Reachable: true, Exists: false} + } + defer func() { checkBootstrapServerDB = orig }() + + plan := BootstrapPlan{ + BeadsDir: beadsDir, + Database: "mydb", + Action: "refuse", + Reason: "short summary", + refusalDetail: "long refusal: bd bootstrap will NOT create an empty database here", + } + // nonInteractive=false so a confirmPrompt would be reached if one ran. + err := executeBootstrapPlan(plan, cfg, false) + if err == nil { + t.Fatal("executeBootstrapPlan returned nil for a refuse plan — bootstrap would exit 0 having done nothing, hiding the refusal") + } + if !strings.Contains(err.Error(), "will NOT create") { + t.Errorf("error = %q, want the refusalDetail text", err.Error()) + } + if probes != 0 { + t.Errorf("probes = %d, want 0 — the decision was already made at plan time", probes) + } +} + +// TestDetectBootstrapAction_FreshCloneServerNameMatchStillSyncs pins the +// POSITIVE half of the widened probe. Unlocking the server-side existence +// check for ProjectID-bearing workspaces also unlocked its Exists==true arm, +// and a reachable server merely holding a database of the configured NAME is +// not proof that this workspace is bound to it — nothing checks project_id or +// content. Before the widening such a clone never probed at all and always +// fell through to sync.remote, so a stale or unrelated same-named database +// (including one left behind by the very bug this guard fixes) must not +// silently settle the plan as "nothing to do" and suppress the configured +// recovery clone. The name match survives only as the fallback verdict, which +// TestDetectBootstrapAction_NoLocalShadowDirStillLiveChecksExisting pins. +func TestDetectBootstrapAction_FreshCloneServerNameMatchStillSyncs(t *testing.T) { + beadsDir, cfg := freshServerModeCloneFixture(t) + orig := checkBootstrapServerDB + checkBootstrapServerDB = func(bootstrapServerProbeConfig) bootstrapServerDBCheck { + return bootstrapServerDBCheck{Reachable: true, Exists: true} + } + defer func() { checkBootstrapServerDB = orig }() + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(time.Duration) {} + defer func() { bootstrapRetryDelay = origDelay }() + + plan := detectBootstrapAction(beadsDir, cfg) + if plan.Action != "sync" { + t.Errorf("action = %q (reason %q), want sync — a server-side name match with no local evidence binding this workspace to that database must not mask configured sync.remote recovery", plan.Action, plan.Reason) + } +} + +// TestDetectBootstrapAction_ShadowDirServerNameMatchSettlesNone pins the other +// half of the same gate: a populated local shadow directory IS evidence this +// workspace is bound to the database the server confirmed, so the Exists +// verdict must settle the plan immediately instead of being deferred behind +// sync.remote. Deferring it would re-clone over a database that already exists +// — GH#5037's Error 1007, which the comment above the settle gate exists to +// prevent — and the fixture configures a sync.remote precisely so the +// assertion discriminates: without the local-evidence half of +// settledOnNameAlone, sync.remote wins and the action becomes "sync". The +// Err-arm twin is TestDetectBootstrapAction_ShadowDirUnreachableServerStillReportsNone; +// this is the Exists-arm one, which nothing covered. +func TestDetectBootstrapAction_ShadowDirServerNameMatchSettlesNone(t *testing.T) { + beadsDir, cfg := freshServerModeCloneFixture(t) + if err := os.MkdirAll(filepath.Join(cfg.DoltDataDir, "mydb"), 0o750); err != nil { + t.Fatal(err) + } + orig := checkBootstrapServerDB + checkBootstrapServerDB = func(bootstrapServerProbeConfig) bootstrapServerDBCheck { + return bootstrapServerDBCheck{Reachable: true, Exists: true} + } + defer func() { checkBootstrapServerDB = orig }() + origDelay := bootstrapRetryDelay + bootstrapRetryDelay = func(time.Duration) {} + defer func() { bootstrapRetryDelay = origDelay }() + + plan := detectBootstrapAction(beadsDir, cfg) + if plan.Action != "none" { + t.Errorf("action = %q (reason %q), want none — with local evidence binding this workspace to the database the server reports, re-cloning it is GH#5037's Error 1007", plan.Action, plan.Reason) + } + if !plan.HasExisting { + t.Error("HasExisting = false on a confirmed server-side database with local evidence") + } + // Discriminates the Exists arm from the Err arm's "Could not verify" + // settle, which the same populated shadow directory also unlocks. + if !strings.Contains(plan.Reason, "already exists on server") { + t.Errorf("reason = %q, want the Exists-arm wording — a 'Could not verify' reason here means the probe result, not the local evidence, is what settled the plan", plan.Reason) + } +}