beads/cmd/bd/dolt_embedded_test.go
matt wilkie d3e35b8be4
fix(embeddeddolt): tolerate nothing-to-commit — bd bootstrap dies on a pristine store (#3886) (#5242)
* fix(embeddeddolt): tolerate nothing-to-commit in Commit/CommitWithConfig

bd bootstrap always builds an EmbeddedDoltStore (no ServerMode), and on a
pristine store calls SetConfig followed by CommitWithConfig with an
otherwise-clean working set. EmbeddedDoltStore.Commit wrapped ANY
DOLT_COMMIT error — including Dolt's benign "nothing to commit" — as a
hard failure, so bootstrap died even though the server store (DoltStore)
has always tolerated the identical case via isDoltNothingToCommit. Commit
now checks issueops.IsNothingToCommitError and returns nil for it;
CommitWithConfig inherits the fix since it aliases Commit (GH#3886).

CommitPending relied on Commit's error to decide whether it actually
committed anything ((bool, error) return), so making Commit tolerant would
have made CommitPending report "committed" on every clean-store call.
Switched it to compare HEAD before/after the Commit call instead, which is
correct regardless of how Commit reports a no-op.

CommitMergeResolution deliberately does NOT inherit the new tolerance: a
merge resolution that leaves the working set clean is the --ours case,
where resolving the conflict dirtied nothing but the merge must still be
concluded (see DoltStore.concludeOpenMerge / wy-36ilm F12, and
versioncontrolops.GetMergeBlockers's doc comment, which already flags this
exact gap for embedded). Silently swallowing the error here without also
concluding the merge would leave dolt_merge_status.is_merging true while
reporting success — worse than today's explicit failure. Whether embedded
DOLT_COMMIT('-Am') already concludes an open merge on a clean working set
was not established, so it keeps its own non-tolerant body rather than
guessing; that investigation is left for a follow-up scoped to merge
conclusion semantics.

Added the GH#3886 regression test (TestEmbeddedCommitToleratesNothingToCommit)
covering both Commit and CommitWithConfig on a clean working set.

Agent-Signature: claude-fable-5-high on behalf of maphew

* fix(embeddeddolt): close adversarial review findings on cb8a2edc6

Adversarial review of cb8a2edc6 (adding nothing-to-commit tolerance to
EmbeddedDoltStore.Commit/CommitWithConfig) found three issues:

1. MAJOR: Sync's --strategy conflict-resolution commit called the now-
   tolerant Commit instead of CommitMergeResolution. For the --ours case
   (resolution dirties nothing), Commit's tolerance would swallow the
   nothing-to-commit response as success, leaving
   dolt_merge_status.is_merging true while Sync reports Merged=true and
   pushes — the exact re-wedge CommitMergeResolution's own doc comment
   says must not be swallowed. Switched the call site to
   CommitMergeResolution, mirroring the server-mode twin
   (dolt/federation.go's Sync). No embedded Sync-with-strategy test seam
   exists in this package (a full test needs two real federated stores
   over a working Dolt remote with an induced conflict, which none of
   the existing embedded tests set up outside //go:build integration);
   added a WHY comment at the call site instead, per review guidance.

2. MINOR: CommitPending did three sequential engine opens (HEAD read,
   Commit, HEAD read) to detect whether a commit landed, and had a
   TOCTOU window where a concurrent HEAD move could misattribute the
   result. Extracted the shared commit body into a new commitAll(ctx,
   message, tolerateEmpty) helper that runs the one DOLT_COMMIT('-Am')
   on a single connection and returns whether it committed; Commit and
   CommitMergeResolution now call it with tolerateEmpty true/false
   respectively (discarding the bool), and CommitPending uses the bool
   directly instead of comparing HEAD before/after.

3. NIT: softened the comment claiming GetMergeBlockers's doc "already
   flags this exact gap for embedded" — that doc (conflicts.go) is
   generic, not embedded-specific.

Verified: gofmt clean; go vet -tags gms_pure_go
./internal/storage/embeddeddolt/ clean; go build -tags gms_pure_go ./...
clean; CGO_ENABLED=1 go test -tags gms_pure_go
./internal/storage/embeddeddolt/ -count=1 passes (including
TestEmbeddedSyncCommitsConfigBeforeMerge).

Agent-Signature: claude-fable-5-high on behalf of maphew

* fix(cli): report commit no-ops by HEAD movement, not by error

Cross-vendor review of 412cab2de (which made EmbeddedDoltStore.Commit
tolerate nothing-to-commit for gh 3886 parity) found that CLI callers keyed
their "did we commit" reporting off Commit's returned error. Server mode has
always tolerated this error internally, so it has ALWAYS misreported a
no-op commit on a clean working set; the embedded fix just widened the
window by making the same tolerance reachable there too.

- cmd/bd/vc.go (`bd vc commit`): capture GetCurrentCommit before Commit;
  after a nil-error Commit, if HEAD is unchanged, report the no-op
  ("Nothing to commit" / JSON committed:false) same as the existing
  isDoltNothingToCommit error path.
- cmd/bd/dolt.go (`bd dolt commit`): same before/after HEAD comparison,
  printing "Nothing to commit." instead of "Committed."
- cmd/bd/main.go (shutdown pending-batch flush): only print "Flushed
  pending batch commit on shutdown" when HEAD actually moved, so a clean
  shutdown stays quiet.

The existing isDoltNothingToCommit(err) fallback is kept at all three sites
for stores/engines that still surface the error directly.

Added embedded-mode regression tests (commit_clean_reports_nothing in both
vc_embedded_test.go and dolt_embedded_test.go) asserting the clean-working-
set case (immediately after `bd init`, which itself now commits cleanly)
reports the no-op rather than a fresh commit.

Verified: gofmt clean; go vet -tags gms_pure_go ./cmd/bd/ clean; go build
-tags gms_pure_go ./... clean; CGO_ENABLED=1 go test -tags gms_pure_go
-run 'TestEmbeddedVC$|TestEmbeddedDolt$' ./cmd/bd/ -count=1 passes
(BEADS_TEST_EMBEDDED_DOLT=1), including both new subtests.

Resolves the cross-vendor review finding on 412cab2de and absorbs bd
mybd-z9h7j.

Agent-Signature: claude-fable-5-high on behalf of maphew

* fix(shutdown): flush via CommitPending — don't spend the 5s budget on HEAD probes

Cross-vendor review of 5d6090e80 flagged two things about the HEAD-compare
no-op detection: (1) the shutdown flush pre-read shared the single 5s context
with the commit itself, so a slow engine open could starve the actual flush —
the one thing that path must not lose; (2) the before/after compare in
vc.go/dolt.go can misattribute a concurrent writer's commit.

The shutdown path now uses CommitPending, which reports the outcome
atomically from the single commit call — no probes, full budget, no race.
The interactive vc/dolt commit paths keep the compare (strictly better than
the prior misreport, pre-existing for server mode) with the race documented
and tracked as bd mybd-z9h7j, whose clean fix is threading CommitPending's
committed-bool shape through the VersionControl interface.

Agent-Signature: claude-fable-5-high on behalf of maphew
2026-08-01 12:17:00 -07:00

388 lines
12 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdDolt runs "bd dolt" with the given args and returns stdout.
func bdDolt(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"dolt"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd dolt %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdDoltFail runs "bd dolt" expecting failure and returns stderr+stdout.
func bdDoltFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"dolt"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd dolt %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
func TestEmbeddedDolt(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "td")
// ===== Server-only commands must fail in embedded mode =====
// status and show are intentionally NOT in this list: they report
// embedded-mode state rather than erroring out. See the
// embedded_status/embedded_show subtests below.
serverOnlyCmds := []struct {
name string
args []string
}{
{"start", []string{"start"}},
{"stop", []string{"stop"}},
{"test", []string{"test"}},
{"set", []string{"set", "host", "127.0.0.1"}},
{"killall", []string{"killall"}},
{"clean-databases", []string{"clean-databases"}},
}
for _, tc := range serverOnlyCmds {
t.Run("blocked_"+tc.name, func(t *testing.T) {
out := bdDoltFail(t, bd, dir, tc.args...)
if !strings.Contains(out, "not supported in embedded mode") {
t.Errorf("expected 'not supported in embedded mode' in output for dolt %s: %s", tc.name, out)
}
})
}
// ===== Embedded-mode inspection commands succeed with embedded-mode output =====
t.Run("embedded_status", func(t *testing.T) {
out := bdDolt(t, bd, dir, "status")
if !strings.Contains(out, "embedded") {
t.Errorf("expected embedded-mode status output to mention 'embedded': %s", out)
}
})
t.Run("embedded_status_json", func(t *testing.T) {
out := bdDolt(t, bd, dir, "status", "--json")
var result map[string]interface{}
if err := json.Unmarshal([]byte(out), &result); err != nil {
t.Fatalf("status --json returned invalid JSON: %v\n%s", err, out)
}
if result["mode"] != "embedded" {
t.Errorf("mode = %v, want embedded", result["mode"])
}
if result["server_running"] != false {
t.Errorf("server_running = %v, want false", result["server_running"])
}
if _, ok := result["running"]; ok {
t.Errorf("running should be omitted for embedded mode; use server_running instead: %v", result["running"])
}
})
t.Run("embedded_show", func(t *testing.T) {
out := bdDolt(t, bd, dir, "show")
if !strings.Contains(out, "Mode:") || !strings.Contains(out, "embedded") {
t.Errorf("expected embedded-mode show output to contain 'Mode:' and 'embedded': %s", out)
}
})
// ===== Working commands =====
// Create an issue so there are pending changes to commit
bdCreate(t, bd, dir, "Dolt commit test issue", "--type", "task")
t.Run("commit", func(t *testing.T) {
out := bdDolt(t, bd, dir, "commit")
// Should succeed — either commits or reports nothing to commit
_ = out
})
t.Run("commit_with_message", func(t *testing.T) {
bdCreate(t, bd, dir, "Another issue for commit", "--type", "task")
out := bdDolt(t, bd, dir, "commit", "-m", "test commit message")
_ = out
})
t.Run("commit_clean_reports_nothing", func(t *testing.T) {
// bd init leaves the working set clean (bootstrap's own commit
// tolerates nothing-to-commit, GH#3886). A second, truly no-op
// "bd dolt commit" here must print "Nothing to commit.", not
// "Committed." — this is the case EmbeddedDoltStore.Commit's new
// nothing-to-commit tolerance made possible to reach with a nil
// error.
cleanDir, _, _ := bdInit(t, bd, "--prefix", "tdcln")
out := bdDolt(t, bd, cleanDir, "commit")
if !strings.Contains(out, "Nothing to commit.") {
t.Errorf("expected 'Nothing to commit.' on a clean working set, got: %s", out)
}
})
// ===== Remote management =====
t.Run("remote_list_empty", func(t *testing.T) {
out := bdDolt(t, bd, dir, "remote", "list")
_ = out // Should succeed even with no remotes
})
t.Run("remote_add_and_list", func(t *testing.T) {
remoteDir := t.TempDir()
bdDolt(t, bd, dir, "remote", "add", "test-remote", "file://"+remoteDir)
out := bdDolt(t, bd, dir, "remote", "list")
if !strings.Contains(out, "test-remote") {
t.Errorf("expected 'test-remote' in remote list: %s", out)
}
})
t.Run("remote_remove", func(t *testing.T) {
bdDolt(t, bd, dir, "remote", "remove", "test-remote")
out := bdDolt(t, bd, dir, "remote", "list")
if strings.Contains(out, "test-remote") {
t.Errorf("expected 'test-remote' to be removed: %s", out)
}
})
// ===== Push/Pull with file remote =====
t.Run("push_to_file_remote", func(t *testing.T) {
ppDir, _, _ := bdInit(t, bd, "--prefix", "pp")
remoteDir := t.TempDir()
bdDolt(t, bd, ppDir, "remote", "add", "origin", "file://"+remoteDir)
// Create an issue and commit
bdCreate(t, bd, ppDir, "Push roundtrip issue", "--type", "task")
bdDolt(t, bd, ppDir, "commit", "-m", "roundtrip commit")
// Push to file remote
bdDolt(t, bd, ppDir, "push")
})
t.Run("push_force", func(t *testing.T) {
pfDir, _, _ := bdInit(t, bd, "--prefix", "pf")
remoteDir := t.TempDir()
bdDolt(t, bd, pfDir, "remote", "add", "origin", "file://"+remoteDir)
bdCreate(t, bd, pfDir, "Force push issue", "--type", "task")
bdDolt(t, bd, pfDir, "commit", "-m", "force push commit")
bdDolt(t, bd, pfDir, "push", "--force")
})
t.Run("push_no_remote", func(t *testing.T) {
// Push without a remote should fail gracefully, not panic
cmd := exec.Command(bd, "dolt", "push")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Log("push without remote succeeded unexpectedly")
}
_ = out
})
t.Run("pull_no_remote", func(t *testing.T) {
cmd := exec.Command(bd, "dolt", "pull")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Log("pull without remote succeeded unexpectedly")
}
_ = out
})
// ===== Push/Pull between two repos via shared file remote =====
t.Run("push_then_push_more", func(t *testing.T) {
// Push twice to verify incremental pushes work
ppDir, _, _ := bdInit(t, bd, "--prefix", "pm")
remoteDir := t.TempDir()
bdDolt(t, bd, ppDir, "remote", "add", "origin", "file://"+remoteDir)
// First push
bdCreate(t, bd, ppDir, "First push issue", "--type", "task")
bdDolt(t, bd, ppDir, "commit", "-m", "first commit")
bdDolt(t, bd, ppDir, "push")
// Second push
bdCreate(t, bd, ppDir, "Second push issue", "--type", "task")
bdDolt(t, bd, ppDir, "commit", "-m", "second commit")
bdDolt(t, bd, ppDir, "push")
})
}
// TestEmbeddedDoltConcurrent exercises dolt operations concurrently.
func TestEmbeddedDoltConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "dx")
// Pre-create issues so commits have something to work with
for i := 0; i < 4; i++ {
bdCreate(t, bd, dir, fmt.Sprintf("dolt-concurrent-%d", i), "--type", "task")
}
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
var args []string
expectFail := false
switch worker % 3 {
case 0:
args = []string{"dolt", "commit"}
case 1:
// Status is an embedded-mode inspection command.
args = []string{"dolt", "status"}
case 2:
// Server-only command should fail fast.
args = []string{"dolt", "start"}
expectFail = true
}
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if expectFail {
if err == nil {
r.err = fmt.Errorf("expected dolt %s to fail in embedded mode", args[1])
} else if !strings.Contains(string(out), "not supported in embedded mode") {
r.err = fmt.Errorf("unexpected error from dolt %s: %s", args[1], out)
}
} else {
if err != nil {
r.err = fmt.Errorf("dolt %s (worker %d): %v\n%s", strings.Join(args[1:], " "), worker, err, out)
}
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}
// TestAdminEmbeddedBlocked verifies that bd admin subcommands are blocked in
// embedded mode. The guard was previously on adminCmd.PersistentPreRunE which
// fired before cmdCtx.ServerMode was populated, causing a false "embedded mode"
// error even in server-mode projects. The guard is now inside each subcommand's
// Run func, after store init. This test ensures embedded mode still correctly
// rejects admin commands.
func TestEmbeddedAdminBlocked(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "ta")
blockedCmds := []struct {
name string
args []string
}{
{"compact --dolt", []string{"admin", "compact", "--dolt"}},
{"cleanup --dry-run", []string{"admin", "cleanup", "--dry-run"}},
{"reset", []string{"admin", "reset"}},
}
for _, tc := range blockedCmds {
tc := tc
t.Run("blocked_"+tc.name, func(t *testing.T) {
t.Parallel()
cmd := exec.Command(bd, tc.args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Errorf("bd %s should fail in embedded mode but succeeded: %s", tc.name, out)
return
}
if !strings.Contains(string(out), "not yet supported in embedded mode") {
t.Errorf("bd %s: expected 'not yet supported in embedded mode', got: %s", tc.name, out)
}
})
}
}
// TestAdminEmbeddedCompactReadOnlyAllowed verifies read-only compact modes are
// not blocked by the embedded-mode admin guard.
func TestEmbeddedAdminCompactReadOnlyAllowed(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "ta")
readOnlyCmds := []struct {
name string
args []string
}{
{"compact --stats", []string{"admin", "compact", "--stats"}},
{"compact --dolt --dry-run", []string{"admin", "compact", "--dolt", "--dry-run"}},
}
for _, tc := range readOnlyCmds {
tc := tc
t.Run("allowed_"+tc.name, func(t *testing.T) {
t.Parallel()
cmd := exec.Command(bd, tc.args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("bd %s should be allowed in embedded mode: %v\n%s", tc.name, err, out)
return
}
if strings.Contains(string(out), "not yet supported in embedded mode") {
t.Errorf("bd %s hit embedded-mode guard unexpectedly: %s", tc.name, out)
}
})
}
}