beads/cmd/bd/prime_shared_server_test.go
Rongjun GENG 686bc798cb
fix(prime): honor shared-server mode consistently across bd prime's store resolution paths (#6589)
* fix(store-factory): honor shared-server mode in prime's store resolution

newDoltStoreFromConfig and its read-only sibling
openNonMutatingStoreFromConfig checked only cfg.IsDoltServerMode(),
which deliberately does not read dolt.shared-server from config.yaml
(to avoid a circular import with the doltserver package). A workspace
with config.yaml tracked but metadata.json gitignored as machine-local
state — the common shape of a linked git worktree — left
configfile.Load returning (nil, nil), so both paths silently fell
through to the embedded backend and created a phantom database named
"beads" instead of reaching the real shared server.

cmd/bd/main.go's own top-level command resolution already compensates
for this exact gap (GH#3817), but bd prime resolves its store through
a separate path (formatMemoriesForPrime -> ensureStoreActiveForPrime
-> ensureStoreActiveWithContext -> newDoltStoreFromConfig) that never
got the same fix. Centralized the compensation into
effectiveServerMode so the two internal paths and main.go's own
resolution cannot drift from each other again.

Fixes #6551.

* fix(store-factory): route read-only shared mode

* fix(store-factory): reach the !cgo twins and scope shared-server per workspace

Maintainer review fixes for PR #6589 (GH#6551). Addresses the three majors in
the review synthesis, plus the two non-gating items, and corrects two false
claims in the PR's own new prose.

1. major — the !cgo twins kept the unfixed gate, and "centralizing the check
   here keeps them from drifting again" was structurally false.
   effectiveServerMode sat behind //go:build cgo, so store_factory_nocgo.go's
   own copies of newDoltStoreFromConfig / newReadOnlyStoreFromConfig could not
   call it: the released CGO_ENABLED=0 binaries (.goreleaser.yml ships three)
   still ignored shared-server in the exact GH#6551 shape, and there the
   fallthrough is the "reinstall with embedded support" refusal rather than a
   phantom database — actively misleading, in the one build where a shared
   server is the only usable backend. Moved the helper to a new untagged file,
   store_factory_server_mode.go, and called it at all four sites. The
   centralization claim is now true rather than reworded, and the new file's
   header records why it must stay untagged.

2. major — TestPrimeCfgNilHonorsSharedServer was vacuous. Two separate causes,
   both fixed:
   - the beads dir had no files at all, so hasBeadsProjectFiles was false,
     FindBeadsDir() returned "" and ensureStoreActiveWithContext refused with
     ErrNoBeadsDatabase before the factory was reached. It now gets the
     config.yaml the reported shape actually has, which is simultaneously
     discoverable and still metadata.json-less.
   - it exported BEADS_DOLT_SHARED_SERVER=1, which the OLD gate already
     honored, so the A/B stayed green on unfixed code even after the first
     fix. The env var is now deliberately empty: the config.yaml layer is the
     whole of the base gap. Renamed to TestPrimeHonorsSharedServerConfigYaml,
     because the "cfg == nil" mechanism the old name asserts does not happen at
     this call site — normalizeLoadedConfig substitutes DefaultConfig() before
     the gate, so the gate's `cfg != nil` conjunct never short-circuited here.
     The same false mechanism is corrected in TestEffectiveServerMode's comment.
   Also added the positive assertion the test lacked: it now requires the
   captured output to name the port it pinned, so a refusal before store
   resolution can no longer pass. Confirmed by A/B against the reverted gate.

3. major — cross-workspace opens resolved activation from the launcher's
   process-global config, contradicting the invariant three lines above the
   gate. doltserver.IsSharedServerMode() reads process-global state, but
   routed.go:214, create.go:408 and init_contributor.go:330 pass a foreign
   beadsDir, so a launcher enabling shared-server in its own config.yaml could
   retarget a workspace that explicitly asked for embedded — a connect failure
   or a same-named database holding someone else's rows.
   effectiveServerMode now takes beadsDir and resolves the yaml layer against
   it via config.WorkspaceYamlValueStrictWithLocal (config.yaml only in the
   first iteration; see item 4 below). The env arm stays machine-global
   (configfile.IsDoltServerMode already honors it above an explicit dolt_mode,
   so that is base-equivalent), and the process-wide merge is still consulted
   when the target declares no dolt_mode of its own — which preserves both the
   GH#6551 headline fix and base behavior, since IsDoltServerMode falls through
   to config.yaml only when c.DoltMode == "". Purely workspace-scoping the read
   as prescribed would have dropped the user-global layer, which viper merges,
   and regressed the fix it was meant to protect.
   Shared-server mode still wins over a stale metadata.json dolt_mode=embedded
   for the bound workspace (main.go:1710 / shouldUseExternalDoltStatus,
   GH#2946); TestEffectiveServerModeResolvesYamlPerWorkspace pins both halves
   of that pair, and each half was mutation-proved to fail independently.

Non-gating, taken because they were adjacent:
- TestEffectiveServerMode's false-case assertions are now hermetic
  (config.ResetForTesting + HOME/BEADS_DIR scoping), matching its sibling.
- the new assertion no longer matches on internal/storage/dolt's connection
  prose; it uses errors.Is(err, syscall.ECONNREFUSED) and the port the test
  itself pins, both owned outside the package this PR does not own.

Required by fix 1 rather than optional: routing the !cgo factories through
effectiveServerMode makes them consult ambient config, which reds the two
pre-existing TestNocgoNew*FromConfig flag-suggestion tests on any machine with
dolt.shared-server enabled. Both now isolate those signals first, verified by
running them with BEADS_DOLT_SHARED_SERVER=1.

Tests: TestNocgoFactoriesHonorSharedServer is new and is the missing behavioral
half of fix 1 — the cgo-tagged tests cannot observe the !cgo twins and the
pure-Go CI job only checks compilation. Each of its two arms was proved by
reverting one !cgo call site at a time.

Validated: gofmt, git diff --check, go build and go vet under both CGO_ENABLED
settings, and every touched test individually in both lanes under a private
temp root (the shared /tmp suite-root sweeper races sibling agent sessions; see
the pre-review CI note).

Iteration 2 of the maintainer review found one new major, introduced by fix 3
itself, plus four non-gating items. All five are addressed here.

4. major -- the workspace-scoped read dropped the config.local.yaml layer, so
   the four factories resolved dolt.shared-server differently from main.go,
   bootstrap, doctor and migrate-dolt-mode: the same resolver-divergence class
   GH#6551 is an instance of, newly introduced by scoping the read. Initialize
   merges .beads/config.local.yaml LAST (the file-loading block at the end of
   Initialize), i.e. at the
   highest priority, and it is the documented place for machine-specific
   settings that must not be committed (docs/reference/configuration.md).
   dolt.shared-server is exactly that kind of state while .beads/config.yaml is
   git-tracked, so "the repo tracks it, this machine opts out" is the ordinary
   use of the escape hatch -- this repo is itself that shape. The divergence ran
   both ways and the enabling direction was the silent one: config.yaml false
   (what `bd dolt shared-server off` persists) plus a local true fell through to
   embeddeddolt.Open and re-created the GH#6551 phantom database with no error.
   Added config.WorkspaceYamlValueStrictWithLocal beside WorkspaceYamlValueStrict
   so the layering lives in the package that owns it, and pointed the helper at
   it. Each layer keeps the strict contract -- absent file or absent key falls
   through, unreadable/unparseable/non-scalar is an error -- which matches
   Initialize, since it fails outright on a config.local.yaml it cannot merge.
   config.local.yaml's name is now a config constant used by both readers, so
   the merged and workspace-scoped paths cannot drift onto different files.
   TestEffectiveServerModeHonorsWorkspaceLocalYaml pins both directions and uses
   the merged config.GetBool as its oracle, so the assertion cannot drift from
   the layer order it pins; reverting to the config.yaml-only reader fails both
   arms independently. The config package's own contract test additionally pins
   the order, the fallthrough when the local file is silent on the key, and the
   error on a malformed local file; inverting the two layers fails it.
   The prescription asked for cast.ToBool parsing. Used strconv.ParseBool
   instead: spf13/cast is an indirect dependency, ParseBool is exactly what
   cast.ToBool calls for a string (so "1"/"t"/"T"/"TRUE" resolve here as they do
   everywhere else, which was the point), and it matches migrationSharedTopology,
   the in-tree precedent for parsing this key from this reader. An unparseable
   value is viper's false too, and now says so.
   Corrected the parity claim the finding named: for the bound workspace these
   are the same two files the process-wide merge reads, which is true only now.

Non-gating items from iteration 2, taken because the iteration was already
forced and all four live in code this fixup owns:
- the read-only factory's positive discriminator was a near-tautology: the
  pinned port was "1", OR'd with errors.Is(ECONNREFUSED), so the string half
  matched any errno, timestamp or port containing that digit and held up
  wherever the dial error was not ECONNREFUSED. Pinned 59998 instead, five
  distinctive digits like the prime sibling. Verified both halves now fire
  independently, so neither is carrying the other.
- the new !cgo behavioral pin ran in no CI lane: the pure-Go cmd/bd test step is
  a name allowlist and omitted it, so the exact single-call-site regression this
  commit mutation-simulated would have passed every job. Added the three Nocgo
  test names to that -run group in both pr.yml and main.yml. The three pass
  under -tags gms_pure_go, CGO_ENABLED=0, -short in 0.4s with no server, and
  reverting store_factory_nocgo.go's first call site now reds that lane --
  failing only the mutated arm, so per-site discrimination is preserved. (This
  also retires the "the pure-Go CI job only checks compilation" note above: that
  job does run a named subset, and now it runs these.)
- the unreadable-config.yaml arm degraded silently and its comment claimed the
  opposite: it said the configfile.Load error path "refuses to degrade to the
  embedded default", but returning false IS the degrade to embedded here. Kept
  the conservative false (main.go degrades identically on the same corrupt
  input, so it is parity, and a target whose own config is unreadable must not
  be retargeted by the LAUNCHER's config), added a debug.Logf so the discarded
  parse error is recoverable, and corrected the comment to describe what the
  code does.
- TestNocgoFactoriesHonorSharedServer's discriminator was negative-only, the
  one-sidedness class this commit fixed twice elsewhere: any future pre-gate
  refusal would have passed it without the shared-server arm running. Added the
  positive half the siblings use, against its own distinctive pinned port.

Iteration 2 validation: gofmt, git diff --check, go build ./... and go vet under
both CGO_ENABLED settings, the full internal/config suite, the cmd/bd
store-factory and prime shared-server tests (including the 30s flagship binary
test), and the exact pure-Go CI step command with the widened allowlist. Every
fix above was mutation-proved, each arm separately.

Iteration 3 of the maintainer review returned approve with no blocker or major
finding: one minor and five nits. All but one are addressed here, amended into
this same fixup rather than stacked.

5. minor -- the layered reader honored a lone config.local.yaml, so the fixed
   divergence survived at the edge of the fix. Initialize hangs its local merge
   off the project config.yaml it actually found (primaryConfigPath, assigned
   only where an os.Stat on a config.yaml succeeded), so a .beads holding
   config.local.yaml ALONE is never merged: config.GetBool answers from defaults
   while the four patched factories answered from the local file. Same
   resolver-divergence class as item 4, one-directional instead of two, and loud
   (a split-brain connect failure) rather than a phantom database.
   Took the "require the pair" arm rather than only narrowing the prose. A
   reader whose entire contract is "resolve the way Initialize's merged reader
   does" should satisfy that contract instead of documenting where it does not,
   and the permissive read left it the single resolver in the tree that honors a
   file nothing else reads -- the divergence pointed the other way. Two lines in
   WorkspaceYamlValueStrictWithLocal; a stat error other than not-exist drops the
   layer too, matching Initialize, whose own probes for a project config.yaml
   fail the same way, and the tracked read then surfaces that error instead of
   swallowing it.
   Added the missing table arm (local only, no tracked config.yaml) and mutation-
   proved the guard in both directions: neutralizing it reds that arm alone, with
   the divergent ("true", true) answer, while all seven incumbent arms stay green;
   always dropping the local layer reds the two override arms and the malformed-
   local arm while the new one stays green. The guard fires on exactly the
   population it was written for.
   Both parity claims now state Initialize's condition as well as its order
   (store_factory_server_mode.go, yaml_config.go).

Nits, taken because amending this fixup costs nothing extra:
- the !cgo test's own comment still told the reader the pure-Go job "only checks
  that the package compiles" -- in the commit that allowlisted that test by name
  in both workflows. It reads as license to drop the name, which would retire
  exactly the coverage iteration 2 bought. It now says the job runs this test by
  name in pr.yml and main.yml, and that the test name and both allowlists must
  move together.
- three new doc comments cited loadConfigFiles, which does not exist; the merge
  is inline at the end of Initialize. All three now cite Initialize, as does
  item 4 of this message.
- the new constants' justification claimed they keep the merged reader and the
  workspace-scoped reader from drifting onto different files, but only the local
  path was switched -- Initialize still spells config.yaml literally everywhere
  it probes for one. Narrowed the comment to what the constants actually bind,
  rather than rewriting config-path literals this PR has no other reason to move.
- the three pinned "nothing listens here" ports (59997/59998/59999) sat inside
  the kernel's ephemeral range (32768-60999 here and on the GitHub runners).
  That only became load-bearing because the two discriminator fixes worked: the
  assertions now mean "we dialed THIS port and were refused", so a process that
  bound :0 and landed there would answer the dial and red a correct
  implementation. Moved all three to 19997/19998/19999. Each move is proved live
  by the assertion that names its constant -- the prime test still finds its port
  in the captured output.

Declined: switching migrationSharedTopology (migrate_dolt_mode.go:1238) to the
layered reader. Both lanes graded it optional and explicitly non-regressing --
migrate has always read config.yaml only -- and it is a production behavior
change to `bd migrate dolt-mode` that no further review pass would see, on an
iteration that is otherwise approve. It belongs with the five other unlayered
IsDoltServerMode resolvers iteration 2 recorded (where.go:143,
context_cmd.go:140, doctor/database.go:311, doctor/shared_store.go:45,
doctor/server.go:73) as follow-up.

Iteration 3 validation: gofmt, git diff --check, go vet in the default,
CGO_ENABLED=0/gms_pure_go and GOOS=windows modes, the full internal/config
suite, the five cmd/bd resolver and call-site tests including the 33s prime
binary test at its new port, the migrate-dolt-mode topology test, and the exact
pure-Go CI step command (ok, 0.44s).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-27 10:32:56 -07:00

128 lines
5.9 KiB
Go

//go:build cgo
package main
import (
"context"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
// sharedServerPrimePort is pinned so the env var the run is given and the
// assertion on the captured output cannot drift apart. BEADS_DOLT_SERVER_PORT is
// the highest-priority port source (internal/doltserver), so it wins over
// DefaultSharedServerPort, and nothing listens here.
//
// Below 32768, i.e. outside the kernel's ephemeral range (32768-60999 here and
// on the GitHub runners), so a process that bound :0 cannot be handed this port
// and answer the dial the assertion below expects to fail.
const sharedServerPrimePort = "19999"
// TestPrimeHonorsSharedServerConfigYaml is a regression test for GH#6551.
//
// bd prime resolves its store through ensureStoreActiveForPrime ->
// ensureStoreActiveWithContext -> newDoltStoreFromConfig, a different path
// from every other command's resolution in cmd/bd/main.go. That path did not
// compensate for the gap configfile.IsDoltServerMode() leaves on purpose: it
// does not read dolt.shared-server from config.yaml, to avoid a circular
// import with the doltserver package. A linked git worktree commonly has
// config.yaml tracked but metadata.json gitignored as machine-local, so the
// workspace's only statement of shared-server mode lives in the one file this
// resolution ignored, and prime silently fell through to embeddeddolt.Open,
// creating a phantom embedded database named "beads" instead of reaching the
// real shared server. Same shape as the GH#3817 fix already applied to
// main.go's own resolution (see TestSharedServerCfgNilHonorsSharedServer) and
// the same centralizing fix (effectiveServerMode).
//
// The config.yaml layer is the ONLY thing this test may rely on, which is why
// BEADS_DOLT_SHARED_SERVER is deliberately absent from the env below.
// configfile.IsDoltServerMode honors that env var itself (step 2 of its
// precedence chain), and newDoltStoreFromConfig runs normalizeLoadedConfig
// before the gate, so an absent metadata.json arrives as a non-nil
// DefaultConfig() rather than nil: exporting the env var made the OLD gate
// select server mode too, and the A/B went green on unfixed code. This test
// was named for a `cfg == nil` short-circuit that does not happen at this call
// site.
//
// Hermetic: no container required. Auto-start is disabled and the server
// port points nowhere, so the honored path fails fast instead of silently
// creating the phantom store — the observable signal this test checks for.
func TestPrimeHonorsSharedServerConfigYaml(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("not supported on Windows")
}
bdBinary := buildSharedServerTestBinary(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// metadata.json-less beads dir — the configfile.Load -> (nil, nil) case
// that left the compensation gap.
beadsDir := filepath.Join(t.TempDir(), ".beads", "shared-server-prime")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatalf("mkdir metadata-less beads dir: %v", err)
}
// The dir needs a config.yaml, and not only for realism. It is
// simultaneously the reported GH#6551 shape — config.yaml tracked,
// metadata.json gitignored as machine-local, so configfile.Load still
// returns (nil, nil) because it reads only metadata.json — and what makes
// the workspace DISCOVERABLE. With an entirely empty dir,
// internal/beads.hasBeadsProjectFiles is false, so FindBeadsDir() ignores
// BEADS_DIR and returns "", and ensureStoreActiveWithContext refuses with
// ErrNoBeadsDatabase before newDoltStoreFromConfig is ever reached. This
// test then passed identically with and without the fix: no phantom store
// can be created on either, so the assertion below proved nothing.
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"),
[]byte("dolt:\n shared-server: true\n auto-start: false\n"), 0o600); err != nil {
t.Fatalf("write config.yaml: %v", err)
}
env := []string{
"PATH=" + os.Getenv("PATH"),
"HOME=" + t.TempDir(),
"BEADS_DIR=" + beadsDir,
// BEADS_DOLT_SHARED_SERVER is intentionally NOT set here; see the note
// on the test. The workspace's config.yaml is the signal under test.
"BEADS_DOLT_SHARED_SERVER=",
// Disable auto-start and point at a port nothing listens on so the
// shared-server path fails fast instead of spinning up a server.
"BEADS_DOLT_AUTO_START=0",
"BEADS_DOLT_SERVER_PORT=" + sharedServerPrimePort,
"BD_DISABLE_EVENT_FLUSH=1",
"BEADS_TEST_MODE=1",
"GIT_TERMINAL_PROMPT=0",
"GIT_ASKPASS=",
"SSH_ASKPASS=",
"GT_ROOT=",
}
neutralCwd := t.TempDir()
// A connection error from the honored shared-server attempt is expected and
// fine — formatMemoriesForPrime degrades gracefully to a "memory
// unavailable" banner rather than failing the command — so the exit status
// carries no signal. The OUTPUT does, and it is checked below.
out, _ := ssExec(ctx, bdBinary, neutralCwd, env, "prime", "--memories-only")
// Positive half: prime must have REACHED shared-server resolution. The
// phantom-directory check below is one-sided on its own — any earlier
// failure (a rename of `prime --memories-only`, a bad env pin, any refusal
// before store resolution) also leaves no embeddeddolt directory, so the
// test would go green without ever exercising the honored path. Asserting
// the pinned endpoint appears is what makes it able to fail for the reason
// it was written.
if !strings.Contains(out, sharedServerPrimePort) {
t.Fatalf("bd prime never reached shared-server resolution: output does not name the pinned endpoint %s.\n"+
"Output:\n%s", sharedServerPrimePort, out)
}
phantom := filepath.Join(beadsDir, "embeddeddolt")
if info, statErr := os.Stat(phantom); statErr == nil && info.IsDir() {
t.Fatalf("bd prime created a phantom embedded database at %s for a workspace with "+
"dolt.shared-server: true in its config.yaml (GH#6551 regression)", phantom)
}
}