From 65cc81ab4b53582af08194683ef2b8bd74d4bd4d Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Thu, 3 Sep 2026 01:10:14 -0700 Subject: [PATCH] fix(config): a metadata-less server-mode workspace must not open a phantom embedded database (#6120) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A workspace that declares `dolt.mode: server` in .beads/config.yaml but has no metadata.json got no substituted config, so bd fell through to the embedded branch, created .beads/embeddeddolt/beads, and answered every query out of it: exit 0, "No issues found." — indistinguishable from real emptiness. BEADS_DOLT_SERVER_MODE=1 did not help. The metadata-less substitution gated on HostImpliesServerMode, which answers a narrower question than the one being asked: it returns false as soon as config.yaml names any `dolt.mode` (correct when inferring FROM a host) and never consults BEADS_DOLT_SERVER_MODE. Gate on IsDoltServerMode instead — the resolver the very next branch uses to set doltCfg.ServerMode, and the one normalizeLoadedConfig already gives store_factory.go and loadServerModeFromBeadsDir. It is a superset of HostImpliesServerMode, so no workspace that reached server mode before stops reaching it now. An unreachable server now produces a connection error naming the endpoint. That is the right answer: a reader can act on it. Co-authored-by: Claude Fable 5 --- cmd/bd/main.go | 27 +- cmd/bd/metadataless_server_workspace_test.go | 259 +++++++++++++++++++ 2 files changed, 280 insertions(+), 6 deletions(-) create mode 100644 cmd/bd/metadataless_server_workspace_test.go diff --git a/cmd/bd/main.go b/cmd/bd/main.go index 5b109c08a..9d627ec63 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -1592,12 +1592,27 @@ var rootCmd = &cobra.Command{ // deployments that empty relic answers every query with an empty // result set and exit 0 (false-empty), which readers misinterpret as // "no work". Absent metadata.json (cfg == nil, cfgErr == nil) keeps - // the fresh-repo embedded default below — unless env/config.yaml - // supply a remote host (GH#3545): host inference must not depend - // on metadata existing, so substitute the default config and let - // the normal mode/connection resolution run. - if cfg == nil && configfile.DefaultConfig().HostImpliesServerMode() { - logConfigDiscovery(beadsDir, "no metadata.json; host inference (GH#3545) selects server mode") + // the fresh-repo embedded default below — unless the env/config.yaml + // layers already select server mode: that decision must not depend on + // metadata existing, so substitute the default config and let the + // normal mode/connection resolution run. + // + // The gate asks IsDoltServerMode — the same resolver the branch below + // uses to set doltCfg.ServerMode — rather than HostImpliesServerMode. + // Host inference (GH#3545) answers only one layer of that question and + // deliberately returns false as soon as config.yaml names a + // `dolt.mode`: correct for inferring FROM a host, wrong as the whole + // gate. A workspace declaring `dolt.mode: server` in .beads/config.yaml + // with no metadata.json therefore kept cfg nil, fell through to the + // embedded branch, and answered every query out of a phantom + // .beads/embeddeddolt database that same run had just created — + // exit 0, no rows, nothing to distinguish it from real emptiness. + // BEADS_DOLT_SERVER_MODE=1 did not rescue it either; the old gate + // never consulted it. IsDoltServerMode is a superset of + // HostImpliesServerMode, so nothing that reached server mode before + // stops reaching it now. + if cfg == nil && configfile.DefaultConfig().IsDoltServerMode() { + logConfigDiscovery(beadsDir, "no metadata.json; env/config.yaml select server mode") cfg = configfile.DefaultConfig() } if cfg != nil { diff --git a/cmd/bd/metadataless_server_workspace_test.go b/cmd/bd/metadataless_server_workspace_test.go new file mode 100644 index 000000000..ef402a516 --- /dev/null +++ b/cmd/bd/metadataless_server_workspace_test.go @@ -0,0 +1,259 @@ +//go:build cgo + +package main + +import ( + "bytes" + "context" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/steveyegge/beads/internal/testutil" +) + +// A workspace can declare server mode without ever writing metadata.json: +// `dolt.mode: server` in .beads/config.yaml, or BEADS_DOLT_SERVER_MODE=1 in the +// environment. Both are resolved by configfile.IsDoltServerMode, and gc +// provisions the config.yaml shape on every `gc-beads-bd start`. +// +// Before the fix, the metadata-less branch in the root PersistentPreRunE gated +// its config substitution on HostImpliesServerMode alone. That predicate +// answers a narrower question — "does a configured HOST imply server mode?" — +// and deliberately returns false as soon as config.yaml names a dolt.mode; it +// never consults BEADS_DOLT_SERVER_MODE at all. So cfg stayed nil, ServerMode +// stayed false, and bd created and then read a brand-new +// .beads/embeddeddolt/beads database: every query answered out of an empty +// relic, exit 0, "No issues found." A reader cannot tell that from real +// emptiness, which is why these tests assert on the phantom directory and on +// the false-empty separately. +// +// The invariant under test is one-directional and does not depend on a server +// being reachable: a workspace that selects server mode must reach the server +// or fail loudly. It must never quietly become an embedded workspace. + +const phantomEmbeddedDir = "embeddeddolt" + +// TestMetadatalessServerModeWorkspaceNeverOpensPhantomEmbeddedDatabase covers +// the two ways a workspace with no metadata.json selects server mode. Both +// shapes are admitted by the legacy-upgrade guard as-is, so this test exercises +// the store-selection gate directly on any branch. +func TestMetadatalessServerModeWorkspaceNeverOpensPhantomEmbeddedDatabase(t *testing.T) { + bd := buildBDUnderTest(t) + + cases := []struct { + name string + setup func(t *testing.T, beadsDir string, port int) []string + }{ + { + name: "config.yaml dolt.mode server", + setup: func(t *testing.T, beadsDir string, port int) []string { + t.Helper() + writeFile(t, filepath.Join(beadsDir, "config.yaml"), + []byte(fmt.Sprintf("dolt:\n mode: server\n port: %d\n", port))) + return nil + }, + }, + { + name: "BEADS_DOLT_SERVER_MODE env var", + setup: func(t *testing.T, beadsDir string, port int) []string { + t.Helper() + return []string{ + "BEADS_DOLT_SERVER_MODE=1", + fmt.Sprintf("BEADS_DOLT_SERVER_PORT=%d", port), + } + }, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + repoDir := t.TempDir() + initGitRepo(t, repoDir) + beadsDir := filepath.Join(repoDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o700); err != nil { + t.Fatal(err) + } + port := freeLoopbackPort(t) + env := tc.setup(t, beadsDir, port) + writeFile(t, filepath.Join(beadsDir, localVersionFile), []byte("1.3.0\n")) + if err := os.Chmod(beadsDir, 0o700); err != nil { + t.Fatal(err) + } + + // --db names the local Dolt path bd itself computes for this + // workspace. It is the shortest route to store selection for a + // metadata-less workspace that has no local database directory + // yet, which is exactly the state a server-mode workspace is + // supposed to stay in. + run := runBDInWorkspace(t, bd, repoDir, + []string{"list", "--json", "--limit", "0", "--all"}, env, + filepath.Join(beadsDir, "dolt")) + + assertNoPhantomEmbeddedDatabase(t, beadsDir) + assertNotFalseEmpty(t, run) + if !strings.Contains(run.output, fmt.Sprintf("127.0.0.1:%d", port)) { + t.Fatalf("bd did not report the configured server endpoint 127.0.0.1:%d:\n%s", port, run.output) + } + }) + } +} + +// TestGCProvisionedServerWorkspaceNeverOpensPhantomEmbeddedDatabase pins the +// byte shape gc creates: config.yaml declaring server mode, a .beads/dolt data +// root owned by the sql-server, the gitignored port file, and a current-era +// version witness — and still no metadata.json. +// +// Two different mechanisms keep this shape safe depending on what else has +// landed, and the assertions deliberately cover only what both guarantee. On a +// branch without gastownhall/beads#6119 the legacy-upgrade guard refuses the +// workspace outright (over-broadly — that is what #6119 fixes). Once #6119 +// removes that refusal, the store-selection gate fixed here is the only thing +// standing between this shape and a phantom embedded database, so the test +// stays meaningful in exactly the configuration that ships. +func TestGCProvisionedServerWorkspaceNeverOpensPhantomEmbeddedDatabase(t *testing.T) { + bd := buildBDUnderTest(t) + + repoDir := t.TempDir() + initGitRepo(t, repoDir) + beadsDir := filepath.Join(repoDir, ".beads") + if err := os.MkdirAll(filepath.Join(beadsDir, "dolt"), 0o700); err != nil { + t.Fatal(err) + } + port := freeLoopbackPort(t) + writeFile(t, filepath.Join(beadsDir, "config.yaml"), + []byte(fmt.Sprintf("dolt:\n mode: server\n port: %d\n", port))) + writeFile(t, filepath.Join(beadsDir, "dolt-server.port"), []byte(fmt.Sprintf("%d\n", port))) + writeFile(t, filepath.Join(beadsDir, localVersionFile), []byte("1.3.0\n")) + if err := os.Chmod(beadsDir, 0o700); err != nil { + t.Fatal(err) + } + + for _, args := range [][]string{ + {"list", "--json", "--limit", "0", "--all"}, + {"ready", "--json"}, + } { + t.Run(args[0], func(t *testing.T) { + run := runBDInWorkspace(t, bd, repoDir, args, nil, "") + assertNoPhantomEmbeddedDatabase(t, beadsDir) + assertNotFalseEmpty(t, run) + }) + } +} + +type bdRun struct { + args []string + output string + stdout string + exitCode int + err error +} + +// runBDInWorkspace runs bd as a subprocess with auto-start disabled, so a +// server-mode workspace cannot mask a misrouted open by starting a server of +// its own. dbPath, when non-empty, is passed as --db. +// +// Every ambient BEADS_DOLT_* and BEADS_DIR is stripped first. This package's +// TestMain starts a shared Dolt server and exports its endpoint, and env beats +// config.yaml in every layer of the precedence chain being tested here — a +// leaked BEADS_DOLT_SERVER_PORT would retarget the workspace at that server +// and quietly change what the assertions mean. +func runBDInWorkspace(t *testing.T, bd, repoDir string, args, extraEnv []string, dbPath string) bdRun { + t.Helper() + if dbPath != "" { + args = append([]string{"--db", dbPath}, args...) + } + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, bd, args...) + cmd.Dir = repoDir + cmd.Env = append(envWithoutBeadsStorageSettings(), + "BD_DISABLE_METRICS=1", + "BEADS_DOLT_AUTO_START=0", + ) + cmd.Env = append(cmd.Env, extraEnv...) + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + err := cmd.Run() + if ctxErr := ctx.Err(); ctxErr != nil { + t.Fatalf("bd %s did not finish before the deadline: %v\n%s%s", + strings.Join(args, " "), ctxErr, stdout.String(), stderr.String()) + } + run := bdRun{ + args: args, + output: stdout.String() + stderr.String(), + stdout: stdout.String(), + err: err, + } + run.exitCode = cmd.ProcessState.ExitCode() + return run +} + +// assertNoPhantomEmbeddedDatabase is the load-bearing assertion: bd must not +// have created .beads/embeddeddolt for a workspace that selects server mode. +// Once that directory exists it is self-perpetuating — discovery prefers it +// over .beads/dolt on every later run — so the damage outlives the invocation +// that caused it. +func assertNoPhantomEmbeddedDatabase(t *testing.T, beadsDir string) { + t.Helper() + path := filepath.Join(beadsDir, phantomEmbeddedDir) + if info, err := os.Stat(path); err == nil && info.IsDir() { + entries, _ := os.ReadDir(path) + names := make([]string, 0, len(entries)) + for _, entry := range entries { + names = append(names, entry.Name()) + } + t.Fatalf("bd created a phantom embedded database at %s (contains %v) for a server-mode workspace", path, names) + } +} + +// assertNotFalseEmpty rejects the failure this bug produced: a successful, +// empty answer sourced from somewhere other than the configured server. An +// error is a fine outcome here — a reader can act on it. +func assertNotFalseEmpty(t *testing.T, run bdRun) { + t.Helper() + if run.err == nil { + t.Fatalf("bd %s exited 0 for an unreachable server-mode workspace; a silent empty result is indistinguishable from real emptiness:\n%s", + strings.Join(run.args, " "), run.output) + } + if strings.Contains(run.output, "no beads configuration found") { + t.Fatalf("bd %s fell through to the embedded default instead of the configured server:\n%s", + strings.Join(run.args, " "), run.output) + } + for _, empty := range []string{"No issues found.", "No ready work"} { + if strings.Contains(run.stdout, empty) { + t.Fatalf("bd %s reported %q instead of reaching the configured server:\n%s", + strings.Join(run.args, " "), empty, run.output) + } + } + if strings.TrimSpace(run.stdout) == "[]" { + t.Fatalf("bd %s printed an empty JSON result instead of reaching the configured server:\n%s", + strings.Join(run.args, " "), run.output) + } +} + +func envWithoutBeadsStorageSettings() []string { + kept := make([]string, 0, len(os.Environ())) + for _, entry := range os.Environ() { + name, _, _ := strings.Cut(entry, "=") + if strings.HasPrefix(name, "BEADS_DOLT_") || name == "BEADS_DIR" { + continue + } + kept = append(kept, entry) + } + return kept +} + +func freeLoopbackPort(t *testing.T) int { + t.Helper() + port, err := testutil.FindFreePort() + if err != nil { + t.Fatalf("find free port: %v", err) + } + return port +}