* fix(init): name --recreate-missing in the refusal without a paste-ready command The missing-server-database refusal added in #5791 ended with a complete `bd init --recreate-missing --prefix <prefix>` line. ADR 0002 Invariant 4 keeps complete destructive invocations out of bd error output, and this refusal also fires when the server merely could not be reached: a stopped server, a wrong port and a wrong data dir all look the same from here, and recreating in any of them strands the real data behind an empty database of the same name. The line also echoed the raw --prefix flag value, so a run without --prefix printed `--prefix ` with nothing after it. The refusal now names the flag and points to `bd help init-safety`, which keeps the full invocation behind the diagnosis it requires. The unused prefix parameter stays as `_` so the guard call sites are unchanged. Also: - --force / --reinit-local flag help, `bd help init-safety` and the init guard comment now say neither flag authorizes recreating a missing server-mode database; the --recreate-missing stanza says to rule out a stopped server or wrong port/data dir and restore first. - bootstrap.go comments no longer describe #5791 as open and unmerged. - TestInitGuardMissingServerDBMessage_RefusalTextNoEcho pins the wording for both evidence arms, with and without a prefix. - TestInitGuard_RecreateMissingE2ECreatesMissingServerDB runs the real binary against a live server lacking the database: refused without the flag, created with it. The unit tests flip initAllowRecreateMissing directly, so nothing exercised the documented command end to end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(recovery): init-missing-server-db covers both evidence arms and the flag limits The init-missing-server-db section described only the project_id arm of the guard. Under --reinit-local or --force a workspace that predates project_id is also refused when its local Dolt data directory survives, and the refusal then cites that directory instead of metadata.json. "What happened" now covers both arms. "Limits worth knowing" replaces the stale "fails open" bullet with a table of which local state meets the refusal under bd init and under --reinit-local / --force, and what --recreate-missing changes. The init-force-refused section now says --force / --reinit-local bypass only the local data-safety guard, not this refusal or the remote one, and step 3 says why the refusal names --recreate-missing without printing the command (ADR 0002 Invariant 4). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(adr): mark ADR 0004 Accepted now that #5791 has landed ADR 0004 (the missing-database guard signal) still read "Proposed" after the guards implementing it merged in #5791, and its Consequences said checkExistingBeadsDataAt "does not change" although the same PR gave it the Tier 1 probe ahead of project_id. Record the acceptance and say what the PR already changed; DOC_INVENTORY.md follows the status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Eddie the Engineer <adopt-pr@gascity.local> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
5.6 KiB
Go
134 lines
5.6 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestInitGuard_ForceE2EDoesNotRecreateMissingServerDB is the behavioural proof
|
|
// for review item 1 on PR #5791. The unit subtests above pin
|
|
// guardMissingServerDatabaseAt's semantics; only this one proves the reinit
|
|
// path actually CALLS it, which is where the bug lived — the guard existed and
|
|
// was simply never reached with --force.
|
|
//
|
|
// Runs the real binary, because that is the only way to exercise the
|
|
// --force -> reinitLocal -> "skip checkExistingBeadsData" path end to end.
|
|
// Without the wiring this test fails: init proceeds past the guard and reports
|
|
// a connection failure instead of the refusal.
|
|
func TestInitGuard_ForceE2EDoesNotRecreateMissingServerDB(t *testing.T) {
|
|
bd := buildBDUnderTest(t)
|
|
|
|
for _, flag := range []string{"--force", "--reinit-local"} {
|
|
t.Run(flag, func(t *testing.T) {
|
|
projectDir := t.TempDir()
|
|
beadsDir := filepath.Join(projectDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
metadata := map[string]interface{}{
|
|
"database": "dolt",
|
|
"backend": "dolt",
|
|
"dolt_mode": "server",
|
|
"dolt_database": "myproject",
|
|
"project_id": "existing-0000-1111-2222-333344445555",
|
|
}
|
|
data, _ := json.Marshal(metadata)
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Port 1 is unreachable by construction, standing in for a server
|
|
// whose database has been lost.
|
|
// #nosec G204 -- bd is a locally built binary, flag is a literal
|
|
cmd := exec.Command(bd, "init", flag, "--prefix", "myproject", "--skip-hooks")
|
|
cmd.Dir = projectDir
|
|
cmd.Env = initGuardE2EEnv(t, 1)
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err == nil {
|
|
t.Fatalf("bd init %s against a missing server-side database must fail, got success.\nOutput:\n%s", flag, out)
|
|
}
|
|
if !strings.Contains(string(out), "not found on server") {
|
|
t.Fatalf("bd init %s must be refused by the missing-database guard, not fail incidentally.\nWanted the guard's refusal; got:\n%s", flag, out)
|
|
}
|
|
if !strings.Contains(string(out), "--recreate-missing") {
|
|
t.Errorf("refusal must name the explicit opt-in flag, got:\n%s", out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestInitGuard_RecreateMissingE2ECreatesMissingServerDB is the success-side
|
|
// twin of the test above. The unit tests that permit --recreate-missing flip
|
|
// initAllowRecreateMissing directly, so nothing else runs the documented
|
|
// recovery command itself through what follows the guard (remote safety, the
|
|
// metadata rewrite, CREATE DATABASE). This runs the real binary against a live
|
|
// server that lacks the configured database: refused without the opt-in, and
|
|
// with it, init succeeds and the database exists on that server.
|
|
func TestInitGuard_RecreateMissingE2ECreatesMissingServerDB(t *testing.T) {
|
|
beadsDir := startProjectServerModeGuardFixture(t, "myproject")
|
|
port, err := strconv.Atoi(os.Getenv("BEADS_DOLT_SERVER_PORT"))
|
|
if err != nil {
|
|
t.Fatalf("fixture must pin BEADS_DOLT_SERVER_PORT: %v", err)
|
|
}
|
|
// The legacy-upgrade guard runs before init's own guards and refuses a
|
|
// server workspace whose local Dolt storage has no current-era version
|
|
// witness; a workspace bd initialized has one.
|
|
if err := writeLocalVersion(filepath.Join(beadsDir, localVersionFile), Version); err != nil {
|
|
t.Fatalf("write local version: %v", err)
|
|
}
|
|
if check := checkDatabaseOnServer("127.0.0.1", port, "root", "", "myproject", false); !check.Reachable || check.Exists {
|
|
t.Fatalf("precondition: server must be reachable and lack the database, got %+v", check)
|
|
}
|
|
|
|
projectDir := filepath.Dir(beadsDir)
|
|
env := initGuardE2EEnv(t, port)
|
|
args := []string{"init", "--prefix", "myproject", "--skip-hooks", "--skip-agents"}
|
|
|
|
// Without the opt-in this exact workspace is refused, so the success below
|
|
// is the flag's doing rather than a fixture init would accept anyway.
|
|
out, err := runExternalServerBD(t, projectDir, env, args...)
|
|
if err == nil || !strings.Contains(out, "not found on server") {
|
|
t.Fatalf("precondition: bd init without --recreate-missing must be refused by the missing-database guard, got err=%v:\n%s", err, out)
|
|
}
|
|
|
|
out, err = runExternalServerBD(t, projectDir, env, append(args, "--recreate-missing")...)
|
|
if err != nil {
|
|
t.Fatalf("bd init --recreate-missing must create the missing database, got %v:\n%s", err, out)
|
|
}
|
|
if check := checkDatabaseOnServer("127.0.0.1", port, "root", "", "myproject", false); !check.Reachable || !check.Exists {
|
|
t.Fatalf("bd init --recreate-missing reported success but the database is not on the server: %+v\n%s", check, out)
|
|
}
|
|
}
|
|
|
|
// initGuardE2EEnv builds a bd child environment in which only the fixture's
|
|
// files and the given port choose the server. Every BEADS_DOLT_* setting
|
|
// (shared-server mode, data dir, mode, the TestMain container's port) and the
|
|
// BEADS_DIR, BEADS_DB, BD_DB and BEADS_SHARED_SERVER_DIR overrides are
|
|
// dropped, which is what startProjectServerModeGuardFixture pins in-process.
|
|
// The mode variables are dropped rather than set to "0":
|
|
// initModeExplicitlyRequested treats any non-empty value as an explicit mode
|
|
// choice, and init would then stop inheriting server mode from metadata.json.
|
|
func initGuardE2EEnv(t *testing.T, port int) []string {
|
|
t.Helper()
|
|
env := []string{}
|
|
for _, kv := range externalServerTestEnv(t) {
|
|
name, _, _ := strings.Cut(kv, "=")
|
|
switch name {
|
|
case "BEADS_DB", "BD_DB", "BEADS_SHARED_SERVER_DIR":
|
|
continue
|
|
}
|
|
env = append(env, kv)
|
|
}
|
|
return append(env,
|
|
"BEADS_DOLT_SERVER_PORT="+strconv.Itoa(port),
|
|
"BD_NON_INTERACTIVE=1",
|
|
)
|
|
}
|