beads/cmd/bd/init_embedded_test.go
ecuthiell beed11c7f4
fix(init): use selected hook context in embedded initialization (#6464)
* fix(worktree): scrub inherited Git routing state

Establish CWD as the worktree repository boundary before discovery can consume inherited Git routing. Fence the startup config probe, clear routing in-process before root pre-run discovery, and retain command-owned subprocess scrubbing as defense in depth without changing PID or signal semantics.

Fixes #5531

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell

* refactor(worktree): explain owned filtering and remove an unused shim

Document why filtering the fresh routing slice in place is safe and call
the shared routing-key classifier directly from tests. Retain the two
forwarders used by production worktree subprocess/removal paths.

Review: https://github.com/gastownhall/beads/pull/5623#pullrequestreview-5098421671
Bead: saa-cgh.23
Agent-Signature: unknown-model/unknown-reasoning

* fix(config): isolate identity and tracked-file Git probes from routing

Reuse the worktree routing owner for GetIdentity and isGitTracked so
explicit cwd/path selection is not displaced by inherited repository,
index or injected config state. Preserve identity precedence and the
tracked-secret refusal contract, with real target/decoy regressions.

Follow-up: https://github.com/gastownhall/beads/issues/6222
Dependency: https://github.com/gastownhall/beads/pull/5623
Reported-by: bee-ghosttrack
Bead: saa-cgh.93
Agent-Signature: unknown-model/unknown-reasoning

* fix(config): share subprocess key identity for Git routing (saa-cgh.93)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(exec): match Windows environment key identity (#5618)

Centralize os/exec-compatible environment-key comparison and use it for credential, metrics, Git-hook, trace, and repro subprocess filters. Preserve exact Unix semantics and Unicode near-collisions.

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell
Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>

* test(execenv): exercise every environment operation under both host rules

Thread the host choice through narrow private helpers behind the existing
exported functions. Keep one normalization owner and retain native wrapper
tests; the additional table covers both host branches for equality, prefix
matching, last-wins lookup, filtering and input preservation.

Review: https://github.com/gastownhall/beads/pull/5672#pullrequestreview-5096948965
Bead: saa-cgh.78
Agent-Signature: unknown-model/unknown-reasoning

* test(execenv): exercise exported host policy wiring (saa-cgh.78)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* docs(worktree): explain startup Git environment scope (saa-cgh.23)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(config): isolate role commands from Git routing (saa-cgh.99)

Follow-up to bee-ghosttrack's review of gastownhall/beads#6322; refs #6357.

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* test(env): exercise shared key helpers on native Windows (saa-cgh.78)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* refactor(gitenv): share key parsing and normalize routing membership once (saa-cgh.93)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* docs(config): explain role command routing policy (saa-cgh.99)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): isolate role configuration from inherited Git routing (saa-cgh.109.3)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): use the selected origin in contributor setup (saa-cgh.109.7)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): isolate Git writes in planning repositories (saa-cgh.109.9)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* test(init): retain inherited planning commit author (saa-cgh.109.9)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): align embedded role gates with selected Git context (saa-cgh.109.12)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): scope embedded artifact Git operations to selected directory (saa-cgh.109.15)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* test(init): cover selected optional artifacts and explain refused commits (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* Honor role-probe cancellation and exercise actual fork planning (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): warn when automatic contributor role write fails (saa-cgh.109.3)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* test(init): cover successful nonquiet contributor role writes (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* Assert the automatic contributor setup banner alongside warnings (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* test(init): read persisted role routing from the store (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* fix(git): isolate domain role configuration from inherited routing (saa-cgh.109.5)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): probe the selected repository before configuring roles (saa-cgh.109.8)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* docs(init): explain role probe discovery ceiling behavior (saa-cgh.109.8)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): bind proxied tail Git operations to selected directory (saa-cgh.109.14)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(gitignore): preserve CRLF when appending project rules (saa-cgh.96)

Reported by bee-ghosttrack in gastownhall/beads#6343.

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* test(gitignore): pin detector edge cases and clarify append behavior (saa-cgh.96)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(gitignore): preserve line endings in Git exclude appends (saa-cgh.104)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* test(gitignore): clarify exclude fixture path variable (saa-cgh.104)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(gitignore): refuse exclude writes after read errors (saa-cgh.110)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* refactor(gitignore): share append byte composition (saa-cgh.105)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* docs(gitignore): explain append aliasing and trailing CR completion (saa-cgh.105)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* refactor(gitignore): use shared composition for exclude rules (saa-cgh.111)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(gitignore): omit the empty exclude leading separator (saa-cgh.112)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): bind exclude writers to the selected Git directory (saa-cgh.109.16)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(hooks): isolate tracked-file guard from Git routing (saa-cgh.98)

Follow-up to bee-ghosttrack's review of gastownhall/beads#6322; refs #6356.

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(hooks): retain legitimate inherited tracking context (saa-cgh.98)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* test(hooks): isolate trusted global configuration fixture (saa-cgh.98)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* docs(hooks): explain conservative tracked-file guard (saa-cgh.98)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* refactor(git): resolve hooks paths in an explicit read-only context (saa-cgh.109.17.1)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* Bind init hook operations to the selected repository (saa-cgh.109.17)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* Give the hooks tail fixture an explicit remote context (saa-cgh.109.17)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* test(git): canonicalize the hooks fixture repository root (saa-cgh.109.17.1)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): complete selected hook options and alias handling (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* Clarify hooks resolver ownership and preserve work-tree diagnostics (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* Explain restoration of absent routing variables in selected exclude tests (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* Keep init role operations on the captured selected Git context (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* Align selected-tail fixtures with captured role authority (saa-cgh.120)

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* fix(init): keep embedded hook setup in the selected project (saa-cgh.120.12)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(init,config): maintainer fixup for #6464

Pre-review CI repair:
- Annotate #nosec G702 on the selected hooks-path git invocation. No shell
  is involved, every path is passed as its own argv element, and both values
  reach that line absolute via resolveInitHooksContext.

Review iteration 1, blocking:
- internal/config: give the secret-config guard the inherited-env fallback
  that the structurally identical hook guard in cmd/bd/hooks.go already has.
  isGitTracked is the fail-open half of "refuse to write a secret key into a
  git-tracked config.yaml", and the scrub alone narrowed it to upward
  discovery from the config file's directory: under a detached work tree
  (GIT_DIR + GIT_WORK_TREE with no in-tree .git) the probe reported
  "untracked" and the secret was written into the tracked file. Probe the
  scrubbed environment first so inherited routing cannot hide a tracked
  file, then the inherited one; either probe reporting the path as tracked
  refuses the write. Adds the legitimate-routing subtest that the
  poisoned-routing matrix could not express.
- cmd/bd/config.go: the new comment claimed "All role commands ignore
  inherited Git routing". Only the writers were hardened -
  routing.DetectUserRole, beads.RepoContext.Role, `bd config show` and
  `bd doctor` still resolve beads.role through the inherited environment, so
  `bd config get beads.role` and `bd config show` can disagree under one
  environment. Narrow the comment to the four verbs it covers; widening the
  boundary to the readers is a separate change.

Review iteration 1, non-blocking:
- cmd/bd/worktree_cmd.go: disclose that ClearRouting unsets the variables on
  the bd process itself, and state what clearing the discovery-scope
  controls does to repository selection. The comment previously claimed the
  opposite of what the init path's own note admits.
- cmd/bd/init_proxied_server.go: drop isRoleGitRepo. With workDir set, gitUC
  is NewInitGitRepository(workDir), whose IsGitRepo runs the same
  `git rev-parse --git-dir` with the same Dir and scrubbed env; with workDir
  empty the helper returned its fallback. It was isRepo in both arms, plus a
  second git subprocess per `bd init --proxied`.
- cmd/bd/doctor/gitignore.go: omit the leading blank line when creating a
  fresh .gitignore, matching the two sibling writers this PR already
  conditionalized in cff94aa43.

Review iteration 2, non-blocking:
- cmd/bd/worktree_cmd.go: iteration 1's own rewrite lumped
  GIT_DISCOVERY_ACROSS_FILESYSTEM in with GIT_CEILING_DIRECTORIES as
  "removes an inherited stop". That is backwards. git(1) documents that
  discovery "does not cross filesystem boundaries" by default and that this
  variable "can be set to true to tell Git not to stop at filesystem
  boundaries" - it only ever permits, so clearing it *adds* a stop and
  narrows the walk. Split the clause per control: the ceiling widens,
  GIT_DISCOVERY_ACROSS_FILESYSTEM narrows (a repository reachable only
  across a mount point is no longer found by any `bd worktree` verb, with no
  opt-out), and ClearRouting's GIT_CONFIG prefix match drops the whole
  GIT_CONFIG* family for the command's lifetime rather than for one child
  probe. This comment is the only in-tree assertion about the variable's
  direction; export_auto.go:1083-1085 groups the two without claiming one.
- cmd/bd/config.go: "bd config's own beads.role reads and writes" covered
  `bd config show`, which the very next sentence lists as an exception. Name
  the four verbs that do scrub (get/set/unset/set-many, at config.go:209,
  :355, :609 and :902 - the only four git invocations in the file).
- internal/config: make the guard's fail-open default unspellable. The
  mirrored helper took `envs ...[]string`, so a future zero-probe call would
  fall through to "untracked" and write the secret with no compiler
  backstop. Promote the first environment to a required parameter. The tail
  stays variadic rather than matching the twin's fixed (clean, inherited)
  arity because the fallback's fixture guard has to probe one environment
  alone, and exec treats a nil Env as "inherit the current process" rather
  than as a disabled probe.

Pre-approval CI repair:
- Annotate the four gosec taint findings this PR newly introduces in package
  cmd/bd: G704 on the two connectivity probes in dolt.go, G702 on the
  staged-deletion probe in hooks.go and on the ls-remote probe in
  sync_git.go. None of the four is reported by the PR lane, whose lint is
  scoped to the diff (only-new-issues / --new-from-merge-base), and all four
  sit on lines this PR does not otherwise touch - but main.yml runs the same
  wrapper with BD_LINT_NEW_FROM_MERGE_BASE unset and sweeps the whole tree,
  so merging as-is turns main's Lint and PR Lint jobs red. Measured
  whole-tree at pr.yml's pinned golangci-lint v2.10.1 with
  --build-tags=gms_pure_go: origin/main 0 issues, this head 4, this head
  with the annotations 0. The findings are taint over-approximation rather
  than new exposure: gosec's analysis is not field-sensitive, so routing
  config discovery through env-derived paths marks the whole store value
  tainted and every remote URL read back off it inherits that. Each
  annotation states why its own call site is safe; the additions are
  comment-only, with no deletions and no behaviour change.

  The hooks.go annotation sits on the isExportFileStagedForDeletion
  declaration rather than on the exec.Command call. main has since rewritten
  that body to address the export file through nearestExistingDir +
  filepath.Rel, so a call-site comment both conflicted with the merge and
  described an argument the merged body no longer builds. On the declaration
  it merges cleanly and its justification holds for either body.

Pre-approval base refresh (rebased onto 511849496):
- Reconcile this PR with gastownhall/beads#6477, which landed on main after
  this branch was first rebased and which changes the same reader. The two
  changes collide in both directions, and neither side's CI can see it: the
  PR lane builds this branch against its own base, main's lane never has the
  PR, and only the merged tree fails.
- #6477 direction: its new subtest
  TestConfig_ReadFailuresAreNotMissing/invalid_routing_boolean poisons
  GIT_CONFIG_NOSYSTEM and expects the beads.role read to exit 128. This PR
  scrubs GIT_CONFIG* for exactly that key, so git never sees the poison and
  the read succeeds. Replaced that row with
  TestConfig_BeadsRoleScrubsInheritedRoutingEnv, which pins both halves of
  the new contract: beads.role reads through the scrub, and a key outside it
  (user.name) still surfaces the same poison as an exit-128 read error
  rather than as an absent key. #6477's general guarantee is untouched -
  malformed_config still proves it for beads.role through the repository's
  own config file.
- Opposite direction: this PR's own
  TestInitGitRepositoryUsesSelectedDirectory/captured_home asserted
  require.NoError on a generic-constructor read, commented "The existing
  reader maps Git exit errors to absence" - the precise behaviour #6477
  removed. Expect the propagated exit error instead. The test's intent is
  unchanged and in fact better served: only the current caller environment
  can see the changed HOME's broken .gitconfig, so that error is itself the
  proof that generic role reads still use it.

* test(init): retarget the bare row to the work-tree-less install policy

This PR's TestInitEmbeddedHooksSelectedTail/bare row (authored
2026-09-09) asserted that a bare repository fails to resolve hooks
and installs nothing. #6457 (6a4b4aa14e, 2026-09-27) added
git.ResolveWorkTreelessHooksContext specifically to end that
behavior -- its own comment calls out "turning a supported
repository into a warning and no install at all" as the defect it
fixes -- so the row pinned a policy main had deliberately
superseded by the time the two met.

Retargeted in place rather than deleted, so the bare shape keeps
coverage through the embedded-init entry point (main's new
bare/git_dir_cwd rows cover only the proxied path): bare now
installs like any other selected repository, and being its own
common directory, its hooks path lands in its own config.

Mutation-proved load-bearing: removing the work-tree-less fallback
reds this row alone (the other eight rows stay green) with
".../hooks/pre-commit: no such file or directory" -- i.e. it
reproduces the original expectation exactly.

---------

Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>
Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
2026-09-29 16:54:33 -07:00

1774 lines
69 KiB
Go

//go:build cgo
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/gitenv"
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
"github.com/steveyegge/beads/internal/storage/schema"
"github.com/steveyegge/beads/internal/types"
"github.com/stretchr/testify/require"
)
func TestEmbeddedInitHooksRouting(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt init tests")
}
bd := buildEmbeddedBD(t)
target, decoy, home := newInitRoleFixture(t)
global := filepath.Join(home, ".gitconfig")
initRoleFixtureGit(t, target, "config", "--file", global, "user.name", "Hook Fixture")
initRoleFixtureGit(t, target, "config", "--file", global, "user.email", "hooks@example.invalid")
require.NoError(t, os.WriteFile(filepath.Join(decoy, "seed"), []byte("decoy\n"), 0600))
initRoleFixtureGit(t, decoy, "add", "seed")
t.Setenv("GIT_DIR", filepath.Join(home, "missing.git"))
t.Setenv("GIT_WORK_TREE", decoy)
t.Setenv("GIT_INDEX_FILE", filepath.Join(decoy, ".git", "index"))
preserveInitRoleInputs(t, filepath.Join(decoy, ".git", "config"), filepath.Join(decoy, ".git", "index"), global)
storage := filepath.Join(home, "separate storage", ".beads")
require.NoError(t, os.MkdirAll(filepath.Dir(storage), 0750))
cmd := exec.Command(bd, "init", "--prefix", "hookfixture", "--quiet", "--non-interactive", "--skip-agents", "--role", "maintainer")
env := bdEnv(home)
for _, key := range []string{"BEADS_DIR", "BEADS_DB", "BD_DB", "BD_DOLT_HOST", "BD_DOLT_PORT"} {
env = envWithout(env, key)
}
cmd.Dir, cmd.Env = target, append(env, "BEADS_DIR="+storage, "DOLT_ROOT_PATH="+home, "BD_DOLT_MODE=embedded", "BD_EVENTS_JOURNAL=false")
out, err := cmd.CombinedOutput()
require.NoError(t, err, "embedded hook init: %s", out)
cfg, err := configfile.Load(storage)
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, configfile.DoltModeEmbedded, cfg.DoltMode)
info, err := os.Stat(filepath.Join(storage, "embeddeddolt", "hookfixture", ".dolt"))
require.NoError(t, err)
require.True(t, info.IsDir())
for _, name := range managedHookNames {
require.Contains(t, string(readInitHooksFile(t, filepath.Join(storage, "hooks", name))), hookSectionBeginPrefix)
}
got := initRoleFixtureGit(t, target, "config", "--local", "--get", "core.hooksPath")
require.Equal(t, filepath.Clean(filepath.Join(storage, "hooks")), filepath.Clean(got))
}
var (
embeddedBDOnce sync.Once
embeddedBD string
embeddedBDErr error
)
// buildEmbeddedBD returns the path to an embedded bd binary for subprocess tests.
// If BEADS_TEST_BD_BINARY is set, uses that pre-built binary (skipping the ~45s build).
// CI can pre-build once and pass the path to all test invocations.
func buildEmbeddedBD(t *testing.T) string {
t.Helper()
embeddedBDOnce.Do(func() {
prebuilt, err := findPrebuiltBDBinary()
if err != nil {
embeddedBDErr = err
return
}
if prebuilt != "" {
embeddedBD = prebuilt
return
}
tmpDir, err := testTempDir("bd-embedded-init-test-*")
if err != nil {
embeddedBDErr = fmt.Errorf("failed to create temp dir: %w", err)
return
}
name := "bd"
if runtime.GOOS == "windows" {
name = "bd.exe"
}
embeddedBD = filepath.Join(tmpDir, name)
cmd := exec.Command("go", "build", "-tags", "gms_pure_go", "-o", embeddedBD, ".")
if out, err := cmd.CombinedOutput(); err != nil {
embeddedBDErr = fmt.Errorf("go build failed: %v\n%s", err, out)
}
})
if embeddedBDErr != nil {
t.Fatalf("Failed to build embedded bd binary: %v", embeddedBDErr)
}
return embeddedBD
}
func bdEnv(dir string) []string {
var env []string
for _, e := range os.Environ() {
if strings.HasPrefix(e, "BEADS_") {
continue
}
env = append(env, e)
}
return append(env,
"HOME="+dir,
"BEADS_DOLT_AUTO_START=0",
"BEADS_NO_DAEMON=1",
"BD_DISABLE_METRICS=1",
"BD_DISABLE_EVENT_FLUSH=1",
)
}
// envWithout returns env minus any entries for the named variable.
func envWithout(env []string, name string) []string {
out := make([]string, 0, len(env))
for _, e := range env {
if strings.HasPrefix(e, name+"=") {
continue
}
out = append(out, e)
}
return out
}
// isEmbeddedLockOutput recognizes every "another process holds the lock"
// outcome for concurrent bd commands against the same embedded workspace:
// the embedded Dolt flock's own messages, and the workspacegate EXCLUSIVE
// contention message (workspacegate.ErrBusy = "workspace gate busy"). The
// gate is acquired BEFORE the embedded flock is ever attempted, so a losing
// concurrent `bd init` today reports gate contention rather than a flock
// error — both are the same class of outcome from the caller's point of
// view: another bd process holds the lock, retry later.
func isEmbeddedLockOutput(out string) bool {
out = strings.ToLower(out)
return strings.Contains(out, "one writer at a time") ||
strings.Contains(out, "database is locked") ||
strings.Contains(out, "locked by another dolt process") ||
strings.Contains(out, "workspace gate busy")
}
func runCommandBuffers(t *testing.T, cmd *exec.Cmd) (stdout, stderr bytes.Buffer, err error) {
t.Helper()
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
return stdout, stderr, err
}
// bdRunWithFlockRetry runs a bd command with retry on flock contention.
// Returns stdout and nil on success, or combined stdout/stderr and the last
// error after retries are exhausted or a non-flock error occurs.
func bdRunWithFlockRetry(t *testing.T, bd, dir string, args ...string) ([]byte, error) {
t.Helper()
var out []byte
var err error
for attempt := 0; attempt < 10; attempt++ {
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err == nil {
return stdout.Bytes(), nil
}
out = append(stdout.Bytes(), stderr.Bytes()...)
if !isEmbeddedLockOutput(string(out)) {
return out, err
}
t.Logf("bd %s: flock contention (attempt %d/10), retrying...", args[0], attempt+1)
time.Sleep(time.Duration(500*(1<<min(attempt, 4))) * time.Millisecond)
}
return out, err
}
// bdInit creates a temp dir with a git repo, runs bd init --quiet with the
// given extra args, and returns (dir, beadsDir, combined output).
// Fatals if bd init fails.
func bdInit(t *testing.T, bd string, extraArgs ...string) (dir, beadsDir string, out string) {
t.Helper()
dir = t.TempDir()
initGitRepoAt(t, dir)
out = runBDInit(t, bd, dir, extraArgs...)
beadsDir = filepath.Join(dir, ".beads")
return
}
// bdInitInDir runs bd init --quiet in an existing dir. Fatals on failure.
func runBDInit(t *testing.T, bd, dir string, extraArgs ...string) string {
t.Helper()
args := append([]string{"init", "--quiet"}, extraArgs...)
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd init %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(extraArgs, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdInitFail runs bd init --quiet expecting failure. Returns combined output.
func bdInitFail(t *testing.T, bd string, extraArgs ...string) string {
t.Helper()
dir := t.TempDir()
initGitRepoAt(t, dir)
args := append([]string{"init", "--quiet"}, extraArgs...)
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("bd init should have failed")
}
return string(out)
}
func readBack(t *testing.T, beadsDir, database, key string, metadata bool) string {
t.Helper()
// The embedded dolt driver holds a process-level lock, so concurrent
// test functions in the same shard can transiently block each other.
// Retry a few times before giving up.
const maxAttempts = 5
var lastErr error
for attempt := 0; attempt < maxAttempts; attempt++ {
if attempt > 0 {
time.Sleep(time.Duration(attempt) * 500 * time.Millisecond)
}
val, err := readBackOnce(t, beadsDir, database, key, metadata)
if err == nil {
return val
}
lastErr = err
if !strings.Contains(err.Error(), "locked") {
break // non-lock error, don't retry
}
t.Logf("readBack: attempt %d/%d got lock error, retrying: %v", attempt+1, maxAttempts, err)
}
t.Fatalf("readBack: %v", lastErr)
return "" // unreachable
}
func readBackOnce(t *testing.T, beadsDir, database, key string, metadata bool) (string, error) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
store, err := embeddeddolt.Open(ctx, beadsDir, database, "main")
if err != nil {
return "", fmt.Errorf("New failed: %w", err)
}
defer store.Close()
if metadata {
val, err := store.GetMetadata(ctx, key)
if err != nil {
return "", fmt.Errorf("GetMetadata(%q) failed: %w", key, err)
}
return val, nil
}
val, err := store.GetConfig(ctx, key)
if err != nil {
return "", fmt.Errorf("GetConfig(%q) failed: %w", key, err)
}
return val, nil
}
func stripANSI(s string) string {
var out strings.Builder
for i := 0; i < len(s); i++ {
if s[i] == '\033' && i+1 < len(s) && s[i+1] == '[' {
for i += 2; i < len(s); i++ {
if (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z') {
break
}
}
continue
}
out.WriteByte(s[i])
}
return out.String()
}
func runDolt(t *testing.T, doltBin, dir string, args ...string) string {
t.Helper()
cmd := exec.Command(doltBin, args...)
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
t.Fatalf("dolt %s failed: %v", strings.Join(args, " "), err)
}
return stripANSI(string(out))
}
func doltHeadHash(t *testing.T, doltBin, dir string) string {
t.Helper()
line := strings.TrimSpace(runDolt(t, doltBin, dir, "log", "-n", "1", "--oneline"))
if idx := strings.IndexByte(line, ' '); idx > 0 {
return line[:idx]
}
t.Fatalf("unexpected dolt log --oneline output: %q", line)
return ""
}
func requireFile(t *testing.T, path string) {
t.Helper()
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Errorf("expected file to exist: %s", path)
}
}
func requireNoFile(t *testing.T, path string) {
t.Helper()
if _, err := os.Stat(path); err == nil {
t.Errorf("expected file not to exist: %s", path)
}
}
func TestEmbeddedInit(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt init tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("basic", func(t *testing.T) {
dir, beadsDir, out := bdInit(t, bd, "--prefix", "basic")
embeddedDir := filepath.Join(beadsDir, "embeddeddolt")
requireFile(t, beadsDir)
requireFile(t, embeddedDir)
requireFile(t, filepath.Join(embeddedDir, "basic", ".dolt"))
if doltBin, err := exec.LookPath("dolt"); err == nil {
dbDir := filepath.Join(embeddedDir, "basic")
statusOut := runDolt(t, doltBin, dbDir, "status")
if !strings.Contains(statusOut, "nothing to commit") {
t.Errorf("expected clean working set, got:\n%s", statusOut)
}
logOut := runDolt(t, doltBin, dbDir, "log", "--oneline")
for _, want := range []string{"schema: apply migrations", "bd init"} {
if !strings.Contains(logOut, want) {
t.Errorf("dolt log missing %q commit:\n%s", want, logOut)
}
}
}
if val := readBack(t, beadsDir, "basic", "issue_prefix", false); val != "basic" {
t.Errorf("issue_prefix: got %q, want %q", val, "basic")
}
if strings.Contains(out, "bd initialized") {
t.Error("--quiet should suppress success message")
}
// bd_version is in local_metadata (dolt-ignored), not metadata
func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
store, err := embeddeddolt.Open(ctx, beadsDir, "basic", "main")
if err != nil {
t.Fatalf("failed to open store for bd_version check: %v", err)
}
defer store.Close()
if val, err := store.GetLocalMetadata(ctx, "bd_version"); err != nil || val == "" {
t.Error("bd_version local metadata not set")
}
}()
importTime := readBack(t, beadsDir, "basic", "last_import_time", true)
if importTime == "" {
t.Error("last_import_time metadata not set")
}
if _, err := time.Parse(time.RFC3339, importTime); err != nil {
t.Errorf("last_import_time not valid RFC3339: %q", importTime)
}
cfg, err := configfile.Load(beadsDir)
if err != nil {
t.Fatalf("failed to load metadata.json: %v", err)
}
if cfg.Backend != configfile.BackendDolt {
t.Errorf("Backend: got %q, want %q", cfg.Backend, configfile.BackendDolt)
}
if cfg.ProjectID == "" {
t.Error("ProjectID should be set")
}
requireFile(t, filepath.Join(beadsDir, "config.yaml"))
if _, err := os.Stat(filepath.Join(beadsDir, "interactions.jsonl")); !os.IsNotExist(err) {
t.Fatalf("interactions.jsonl should be created only when audit.enabled is true, got stat err %v", err)
}
requireFile(t, filepath.Join(dir, "AGENTS.md"))
requireFile(t, filepath.Join(dir, ".agents", "skills", "beads", "SKILL.md"))
requireFile(t, filepath.Join(dir, ".agents", "skills", "beads", "agents", "openai.yaml"))
requireFile(t, filepath.Join(dir, ".codex", "config.toml"))
requireFile(t, filepath.Join(dir, ".codex", "hooks.json"))
// Cursor integration is auto-installed by bd init too (rules + hooks).
requireFile(t, filepath.Join(dir, ".cursor", "rules", "beads.mdc"))
requireFile(t, filepath.Join(dir, ".cursor", "hooks.json"))
content, err := os.ReadFile(filepath.Join(beadsDir, ".gitignore"))
if err != nil {
t.Fatalf("failed to read .beads/.gitignore: %v", err)
}
for _, pattern := range []string{"*.db", "dolt/", "bd.sock"} {
if !strings.Contains(string(content), pattern) {
t.Errorf(".gitignore missing pattern: %s", pattern)
}
}
{
out := bdDolt(t, bd, dir, "remote", "list")
if strings.Contains(out, "origin") {
t.Fatalf("init without git origin should not configure a Dolt remote; remote list:\n%s", out)
}
// Asserted by reading it back, not by matching a spelling. The
// writer nests (bd-zj95), so "the substring `sync.remote:` is
// absent" is now satisfied by a config.yaml that DOES persist a
// remote — this guard would have sailed past the regression it
// exists to catch.
beadsDir := filepath.Join(dir, ".beads")
configYAML, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml"))
if err != nil {
t.Fatalf("read config.yaml: %v", err)
}
if got := config.GetStringFromDir(beadsDir, "sync.remote"); got != "" {
t.Fatalf("init without git origin persisted sync.remote = %q; config.yaml:\n%s", got, configYAML)
}
}
})
t.Run("database_with_prefix", func(t *testing.T) {
_, beadsDir, _ := bdInit(t, bd, "--database", "shared_db", "--prefix", "alpha")
cfg, err := configfile.Load(beadsDir)
if err != nil {
t.Fatalf("failed to load metadata.json: %v", err)
}
if cfg.DoltDatabase != "shared_db" {
t.Errorf("DoltDatabase: got %q, want %q", cfg.DoltDatabase, "shared_db")
}
requireFile(t, filepath.Join(beadsDir, "embeddeddolt", "shared_db", ".dolt"))
if val := readBack(t, beadsDir, "shared_db", "issue_prefix", false); val != "alpha" {
t.Errorf("issue_prefix: got %q, want %q", val, "alpha")
}
})
t.Run("fork_auto_contributor", func(t *testing.T) {
dir := t.TempDir()
initGitRepoAt(t, dir)
origin := filepath.Join(dir, "origin.git")
upstream := filepath.Join(dir, "upstream.git")
for _, bareRepo := range []string{origin, upstream} {
if err := os.MkdirAll(bareRepo, 0755); err != nil {
t.Fatalf("mkdir %s: %v", bareRepo, err)
}
cmd := exec.Command("git", "init", "--bare")
cmd.Dir = bareRepo
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git init --bare %s failed: %v\n%s", bareRepo, err, out)
}
}
for name, url := range map[string]string{"origin": origin, "upstream": upstream} {
cmd := exec.Command("git", "remote", "add", name, url)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git remote add %s failed: %v\n%s", name, err, out)
}
}
out := runBDInit(t, bd, dir, "--prefix", "forkauto")
if strings.Contains(out, "Fork detected") {
t.Errorf("--quiet should suppress fork auto-routing output, got:\n%s", out)
}
beadsDir := filepath.Join(dir, ".beads")
planningDir := filepath.Join(dir, ".beads-planning")
if val := readBack(t, beadsDir, "forkauto", "routing.mode", false); val != "auto" {
t.Errorf("routing.mode: got %q, want %q", val, "auto")
}
if val := readBack(t, beadsDir, "forkauto", "routing.contributor", false); val != planningDir {
t.Errorf("routing.contributor: got %q, want %q", val, planningDir)
}
if val := readBack(t, beadsDir, "forkauto", "sync.remote", false); val != "upstream" {
t.Errorf("sync.remote: got %q, want %q", val, "upstream")
}
if _, err := os.Stat(filepath.Join(planningDir, ".beads")); err != nil {
t.Errorf("planning .beads missing: %v", err)
}
// Regression: autoConfigureForkContributor must initialize the planning
// Dolt schema, not just create the .beads directory. An uninitialized
// store causes "Dolt server unreachable" on first use (e.g. bd migrate-personal).
planningEmbeddedDir := filepath.Join(planningDir, ".beads", "embeddeddolt")
if _, err := os.Stat(planningEmbeddedDir); err != nil {
t.Errorf("planning embeddeddolt dir missing (planning store not pre-initialized): %v", err)
}
roleCmd := exec.Command("git", "config", "--get", "beads.role")
roleCmd.Dir = dir
roleOut, err := roleCmd.Output()
if err != nil {
t.Fatalf("git config --get beads.role failed: %v", err)
}
if role := strings.TrimSpace(string(roleOut)); role != "contributor" {
t.Errorf("beads.role: got %q, want %q", role, "contributor")
}
})
t.Run("git_origin_registered_as_dolt_remote", func(t *testing.T) {
bareDir := filepath.Join(t.TempDir(), "plain.git")
runGitForBootstrapTest(t, "", "init", "--bare", "-b", "main", bareDir)
seedDir := t.TempDir()
initGitRepoAt(t, seedDir)
runGitForBootstrapTest(t, seedDir, "branch", "-M", "main")
runGitForBootstrapTest(t, seedDir, "commit", "--allow-empty", "-m", "init")
runGitForBootstrapTest(t, seedDir, "remote", "add", "origin", "file://"+bareDir)
runGitForBootstrapTest(t, seedDir, "push", "-u", "origin", "main")
dir := t.TempDir()
initGitRepoAt(t, dir)
remoteURL := "file://" + bareDir
runGitForBootstrapTest(t, dir, "remote", "add", "origin", remoteURL)
runBDInit(t, bd, dir, "--prefix", "pg", "--skip-hooks", "--skip-agents")
out := bdDolt(t, bd, dir, "remote", "list")
if !strings.Contains(out, "origin") || !strings.Contains(out, remoteURL) {
t.Fatalf("git origin should be registered as a Dolt remote %q; remote list:\n%s", remoteURL, out)
}
configYAML, err := os.ReadFile(filepath.Join(dir, ".beads", "config.yaml"))
if err != nil {
t.Fatalf("read config.yaml: %v", err)
}
if !strings.Contains(string(configYAML), remoteURL) {
t.Fatalf("git origin should be persisted as sync.remote; config.yaml:\n%s", configYAML)
}
bdDolt(t, bd, dir, "push")
ls := exec.Command("git", "ls-remote", remoteURL, "refs/dolt/data")
lsOut, err := ls.CombinedOutput()
if err != nil {
t.Fatalf("git ls-remote refs/dolt/data failed: %v\n%s", err, lsOut)
}
if !strings.Contains(string(lsOut), "refs/dolt/data") {
t.Fatalf("bd dolt push did not publish refs/dolt/data:\n%s", lsOut)
}
})
// The #5068 refusal and consent paths, end to end.
t.Run("dolt_push_consent_and_lazy_adoption", func(t *testing.T) {
bareDir := filepath.Join(t.TempDir(), "later-origin.git")
runGitForBootstrapTest(t, "", "init", "--bare", "-b", "main", bareDir)
remoteURL := "file://" + bareDir
dir := t.TempDir()
initGitRepoAt(t, dir)
runGitForBootstrapTest(t, dir, "branch", "-M", "main")
runGitForBootstrapTest(t, dir, "commit", "--allow-empty", "-m", "init")
runBDInit(t, bd, dir, "--prefix", "late", "--skip-hooks", "--skip-agents")
bdCreate(t, bd, dir, "Lazy remote adoption", "--type", "task")
runGitForBootstrapTest(t, dir, "remote", "add", "origin", remoteURL)
runGitForBootstrapTest(t, dir, "push", "-u", "origin", "main")
// No TTY and no --yes: bd must refuse rather than derive a remote and
// upload to it.
{
out := bdDoltFail(t, bd, dir, "push")
if !strings.Contains(out, remoteURL) {
t.Errorf("refusal did not name the remote it would have adopted; output:\n%s", out)
}
if list := bdDolt(t, bd, dir, "remote", "list"); strings.Contains(list, remoteURL) {
t.Errorf("refused push still added the remote; remote list:\n%s", list)
}
configYAML, readErr := os.ReadFile(filepath.Join(dir, ".beads", "config.yaml"))
if readErr == nil && strings.Contains(string(configYAML), remoteURL) {
t.Errorf("refused push still persisted sync.remote; config.yaml:\n%s", configYAML)
}
if lsOut, lsErr := exec.Command("git", "ls-remote", remoteURL, "refs/dolt/data").Output(); lsErr == nil && len(strings.TrimSpace(string(lsOut))) != 0 {
t.Errorf("refused push still uploaded issue history: %s", lsOut)
}
}
// --yes is the scripted consent for git-origin adoption (#5068). The
// capability this subtest covers is unchanged; only the consent is
// new, and a test process has no TTY so adoption now fails closed
// without it.
ambientBare := filepath.Join(t.TempDir(), "ambient-origin.git")
runGitForBootstrapTest(t, "", "init", "--bare", "-b", "main", ambientBare)
ambientURL := "file://" + ambientBare
ambientDir := t.TempDir()
initGitRepoAt(t, ambientDir)
runGitForBootstrapTest(t, ambientDir, "remote", "add", "origin", ambientURL)
pushCmd := exec.Command(bd, "-C", dir, "dolt", "push", "--yes")
pushCmd.Dir = ambientDir
pushCmd.Env = bdEnv(ambientDir)
if out, err := pushCmd.CombinedOutput(); err != nil {
t.Fatalf("bd -C target dolt push failed: %v\n%s", err, out)
}
out := bdDolt(t, bd, dir, "remote", "list")
if !strings.Contains(out, "origin") || !strings.Contains(out, remoteURL) {
t.Fatalf("bd dolt push --yes should adopt later git origin %q; remote list:\n%s", remoteURL, out)
}
if strings.Contains(out, ambientURL) {
t.Fatalf("bd -C target dolt push adopted ambient origin %q; remote list:\n%s", ambientURL, out)
}
configYAML, err := os.ReadFile(filepath.Join(dir, ".beads", "config.yaml"))
if err != nil {
t.Fatalf("read config.yaml: %v", err)
}
if !strings.Contains(string(configYAML), remoteURL) {
t.Fatalf("bd dolt push should persist sync.remote; config.yaml:\n%s", configYAML)
}
ls := exec.Command("git", "ls-remote", remoteURL, "refs/dolt/data")
lsOut, err := ls.CombinedOutput()
if err != nil {
t.Fatalf("git ls-remote refs/dolt/data failed: %v\n%s", err, lsOut)
}
if !strings.Contains(string(lsOut), "refs/dolt/data") {
t.Fatalf("bd dolt push did not publish refs/dolt/data:\n%s", lsOut)
}
})
t.Run("remote_bootstraps_existing_dolt_data", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("uses os.Symlink to mask dolt off PATH; symlink semantics differ on Windows")
}
remoteDir := filepath.Join(t.TempDir(), "remote")
remoteURL := "file://" + remoteDir
sourceDir, _, _ := bdInit(t, bd, "--prefix", "src", "--skip-hooks", "--skip-agents")
sourceCfg, err := configfile.Load(filepath.Join(sourceDir, ".beads"))
if err != nil {
t.Fatalf("load source metadata.json: %v", err)
}
if sourceCfg.ProjectID == "" {
t.Fatal("source project ID is empty")
}
cmd := exec.Command(bd, "create", "Remote issue", "--type", "task")
cmd.Dir = sourceDir
cmd.Env = bdEnv(sourceDir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd create failed: %v\n%s", err, out)
}
bdDolt(t, bd, sourceDir, "commit")
bdDolt(t, bd, sourceDir, "remote", "add", "origin", remoteURL)
bdDolt(t, bd, sourceDir, "push", "--force")
cloneDir := t.TempDir()
initGitRepoAt(t, cloneDir)
gitBin, err := exec.LookPath("git")
if err != nil {
t.Fatalf("git not found: %v", err)
}
pathDir := filepath.Join(t.TempDir(), "path")
if err := os.MkdirAll(pathDir, 0o750); err != nil {
t.Fatal(err)
}
if err := os.Symlink(gitBin, filepath.Join(pathDir, "git")); err != nil {
t.Fatalf("symlink git into PATH: %v", err)
}
noDoltEnv := bdEnv(cloneDir)
replacedPath := false
for i, entry := range noDoltEnv {
if strings.HasPrefix(entry, "PATH=") {
noDoltEnv[i] = "PATH=" + pathDir
replacedPath = true
break
}
}
if !replacedPath {
noDoltEnv = append(noDoltEnv, "PATH="+pathDir)
}
cmd = exec.Command(bd, "init", "--quiet", "--prefix", "clone", "--remote", remoteURL, "--skip-hooks", "--skip-agents")
cmd.Dir = cloneDir
cmd.Env = noDoltEnv
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd init --remote without dolt CLI failed: %v\n%s", err, out)
}
cloneCfg, err := configfile.Load(filepath.Join(cloneDir, ".beads"))
if err != nil {
t.Fatalf("load clone metadata.json: %v", err)
}
if cloneCfg.ProjectID != sourceCfg.ProjectID {
t.Fatalf("clone ProjectID = %q, want source ProjectID %q", cloneCfg.ProjectID, sourceCfg.ProjectID)
}
if val := readBack(t, filepath.Join(cloneDir, ".beads"), "clone", "_project_id", true); val != sourceCfg.ProjectID {
t.Fatalf("clone database _project_id = %q, want source ProjectID %q", val, sourceCfg.ProjectID)
}
cmd = exec.Command(bd, "list")
cmd.Dir = cloneDir
cmd.Env = bdEnv(cloneDir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd list failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "Remote issue") {
t.Fatalf("cloned database missing remote issue:\n%s", stdout.String())
}
cloneBeadsDir := filepath.Join(cloneDir, ".beads")
requireNoFile(t, filepath.Join(cloneBeadsDir, "hooks"))
requireNoFile(t, filepath.Join(cloneDir, "AGENTS.md"))
requireNoFile(t, filepath.Join(cloneDir, "CLAUDE.md"))
requireNoFile(t, filepath.Join(cloneDir, ".claude"))
requireNoFile(t, filepath.Join(cloneDir, ".agents"))
requireNoFile(t, filepath.Join(cloneDir, ".codex"))
out := bdDolt(t, bd, cloneDir, "remote", "list")
if !strings.Contains(out, "origin") || !strings.Contains(out, remoteURL) {
t.Fatalf("expected origin remote %q in remote list:\n%s", remoteURL, out)
}
configYAML, err := os.ReadFile(filepath.Join(cloneBeadsDir, "config.yaml"))
if err != nil {
t.Fatalf("read config.yaml: %v", err)
}
if !strings.Contains(string(configYAML), remoteURL) {
t.Fatalf("config.yaml should persist --remote URL %q:\n%s", remoteURL, configYAML)
}
})
t.Run("remote_behind_schema_gate", func(t *testing.T) {
// bd-4mpy7 / #4516: bootstrapping from a remote whose database is
// behind this binary's schema. Shared fixture: a published remote
// regressed one migration below LatestVersion. Two paths against it:
// the default smart gate auto-migrates the clone as a safe
// first-mover (remote at the same version — no one has migrated),
// while the BD_SMART_GATE=0 opt-out must fail with
// designated-migrator guidance and leave a finalized workspace where
// the guidance commands can run — not a half-initialized directory
// with a raw gate error.
remoteDir := filepath.Join(t.TempDir(), "behind-remote")
remoteURL := "file://" + remoteDir
sourceDir, sourceBeads, _ := bdInit(t, bd, "--prefix", "bsrc", "--skip-hooks", "--skip-agents")
bdCreate(t, bd, sourceDir, "Behind remote issue", "--type", "task")
bdDolt(t, bd, sourceDir, "commit")
// Regress the source database one migration and publish it, all in
// one raw SQL session — running bd against the regressed database
// would just auto-migrate it back (it has no remote registered yet).
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
db, cleanupSQL, err := embeddeddolt.OpenSQL(ctx, filepath.Join(sourceBeads, "embeddeddolt"), "bsrc", "main")
if err != nil {
t.Fatalf("OpenSQL: %v", err)
}
for _, q := range []string{
fmt.Sprintf("DELETE FROM schema_migrations WHERE version = %d", schema.LatestVersion()),
"CALL DOLT_COMMIT('-am', 'regress schema one version')",
fmt.Sprintf("CALL DOLT_REMOTE('add', 'origin', '%s')", remoteURL),
"CALL DOLT_PUSH('--force', 'origin', 'main')",
} {
if _, err := db.ExecContext(ctx, q); err != nil {
_ = cleanupSQL()
t.Fatalf("%s: %v", q, err)
}
}
_ = cleanupSQL()
t.Run("default_smart_gate_auto_migrates_first_mover", func(t *testing.T) {
cloneDir := t.TempDir()
initGitRepoAt(t, cloneDir)
cmd := exec.Command(bd, "init", "--quiet", "--prefix", "bclone", "--remote", remoteURL, "--skip-hooks", "--skip-agents")
cmd.Dir = cloneDir
// Exercise the true default: strip any ambient opt-out so
// BD_SMART_GATE is genuinely unset.
cmd.Env = envWithout(append(bdEnv(cloneDir), schema.AllowRemoteMigrateEnv+"=0"), schema.SmartGateEnv)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("default smart gate should auto-migrate the safe first-mover during init: %v\n%s", err, out)
}
if !strings.Contains(string(out), "Smart gate") || !strings.Contains(string(out), "bd dolt push") {
t.Fatalf("smart auto-migrate should announce itself and direct a follow-up push:\n%s", out)
}
// The clone is migrated and immediately usable, no unlock needed.
cmd = exec.Command(bd, "list")
cmd.Dir = cloneDir
cmd.Env = bdEnv(cloneDir)
listOut, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd list after smart auto-migrate failed: %v\n%s", err, listOut)
}
if !strings.Contains(string(listOut), "Behind remote issue") {
t.Fatalf("auto-migrated clone missing source issue:\n%s", listOut)
}
})
t.Run("opt_out_gates_with_guidance", func(t *testing.T) {
cloneDir := t.TempDir()
initGitRepoAt(t, cloneDir)
cmd := exec.Command(bd, "init", "--quiet", "--prefix", "bclone", "--remote", remoteURL, "--skip-hooks", "--skip-agents")
cmd.Dir = cloneDir
cmd.Env = append(bdEnv(cloneDir), schema.AllowRemoteMigrateEnv+"=0", schema.SmartGateEnv+"=0")
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd init --remote against a behind-schema remote should fail; output:\n%s", out)
}
for _, want := range []string{
"Re-running `bd init` will NOT fix this",
"bd migrate --force",
"bd dolt push",
} {
if !strings.Contains(string(out), want) {
t.Fatalf("init output missing %q:\n%s", want, out)
}
}
// The failed init must leave a finalized workspace (metadata.json,
// config.yaml) so the guidance commands can open the cloned database.
cloneBeads := filepath.Join(cloneDir, ".beads")
for _, f := range []string{"metadata.json", "config.yaml"} {
if _, err := os.Stat(filepath.Join(cloneBeads, f)); err != nil {
t.Fatalf("failed init should leave %s behind: %v", f, err)
}
}
// Recovery per the guidance: the designated migrator unlocks,
// migrates, and the workspace is usable.
cmd = exec.Command(bd, "migrate")
cmd.Dir = cloneDir
cmd.Env = append(bdEnv(cloneDir), schema.AllowRemoteMigrateEnv+"=1")
if migOut, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%s=1 bd migrate failed: %v\n%s", schema.AllowRemoteMigrateEnv, err, migOut)
}
cmd = exec.Command(bd, "list")
cmd.Dir = cloneDir
cmd.Env = bdEnv(cloneDir)
listOut, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd list after migrate failed: %v\n%s", err, listOut)
}
if !strings.Contains(string(listOut), "Behind remote issue") {
t.Fatalf("migrated clone missing source issue:\n%s", listOut)
}
})
})
t.Run("remote_clone_failure_emits_url_and_hint", func(t *testing.T) {
// remotesapi:// is rejected by dolt as an unknown scheme almost
// instantly, so this exercises the non-empty-remote clone failure
// path without depending on TCP timeouts. Verifies (a) init exits
// non-zero rather than silently bootstrapping fresh, (b) the wrap
// from cmd/bd/init.go echoes the URL the user typed in %q form,
// and (c) the Hint: line is present.
remoteURL := "remotesapi://127.0.0.1:1/no-such-db"
dir := t.TempDir()
initGitRepoAt(t, dir)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, bd, "init", "--quiet", "--prefix", "fail", "--remote", remoteURL, "--skip-hooks", "--skip-agents")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd init --remote with bogus URL to fail; got success:\n%s", out)
}
wantWrap := fmt.Sprintf("failed to clone remote %q", remoteURL)
if !strings.Contains(string(out), wantWrap) {
t.Fatalf("expected init.go wrap %q in output; got:\n%s", wantWrap, out)
}
if !strings.Contains(string(out), "Hint:") {
t.Fatalf("expected error output to include a Hint: about reachability/credentials; got:\n%s", out)
}
if _, statErr := os.Stat(filepath.Join(dir, ".beads", "config.yaml")); statErr == nil {
t.Fatalf(".beads/config.yaml should not exist after a failed clone; init must not silently fall through to fresh init")
}
})
// Regression: bd init --stealth must not touch any git-visible files. Previously it
// created/modified the tracked project-root .gitignore via doctor.EnsureProjectGitignore, which
// showed up in `git status` and defeated stealth. Everything beads adds must be excluded
// (.beads/) or live in .git/info/exclude, leaving the working tree clean from git's view.
t.Run("stealth_leaves_worktree_clean", func(t *testing.T) {
bareDir := filepath.Join(t.TempDir(), "stealth.git")
runGitForBootstrapTest(t, "", "init", "--bare", "-b", "main", bareDir)
remoteURL := "file://" + bareDir
dir := t.TempDir()
initGitRepoAt(t, dir)
runGitForBootstrapTest(t, dir, "remote", "add", "origin", remoteURL)
// Commit a baseline so the repo has a clean, non-empty starting state.
gitignorePath := filepath.Join(dir, ".gitignore")
if err := os.WriteFile(gitignorePath, []byte("node_modules/\n"), 0644); err != nil {
t.Fatalf("seed .gitignore: %v", err)
}
for _, args := range [][]string{
{"add", "-A"},
{"commit", "-m", "baseline"},
} {
cmd := exec.Command("git", args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %s failed: %v\n%s", args[0], err, out)
}
}
runBDInit(t, bd, dir, "--prefix", "stc", "--stealth")
beadsDir := filepath.Join(dir, ".beads")
requireNoFile(t, filepath.Join(dir, "AGENTS.md"))
requireNoFile(t, filepath.Join(dir, "CLAUDE.md"))
requireNoFile(t, filepath.Join(dir, ".claude"))
requireNoFile(t, filepath.Join(dir, ".agents"))
requireNoFile(t, filepath.Join(dir, ".codex"))
// Stealth must stay invisible: it should create .beads/ but route everything else into
// .git/info/exclude so the database lives there without git seeing it.
requireFile(t, beadsDir)
excludeContent, err := os.ReadFile(filepath.Join(dir, ".git", "info", "exclude"))
if err != nil {
t.Fatalf("failed to read .git/info/exclude: %v", err)
}
for _, want := range []string{".beads/", ".dolt/", "*.db"} {
if !strings.Contains(string(excludeContent), want) {
t.Errorf(".git/info/exclude missing %q:\n%s", want, excludeContent)
}
}
// git status --porcelain must be empty: stealth touched no visible files.
cmd := exec.Command("git", "-c", "core.hooksPath=", "status", "--porcelain")
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git status failed: %v\n%s", err, out)
}
if strings.TrimSpace(string(out)) != "" {
t.Errorf("bd init --stealth left git-visible changes (should be invisible):\n%s", out)
}
// And the seeded .gitignore must be byte-for-byte unchanged.
got, err := os.ReadFile(gitignorePath)
if err != nil {
t.Fatalf("read .gitignore: %v", err)
}
if string(got) != "node_modules/\n" {
t.Errorf("stealth modified project .gitignore:\ngot: %q", string(got))
}
{
out := bdDolt(t, bd, dir, "remote", "list")
if strings.Contains(out, "origin") {
t.Fatalf("stealth init should not synthesize a Dolt remote; remote list:\n%s", out)
}
// Read back rather than grep for a spelling: see the same guard in
// the no-git-origin case above.
beadsDir := filepath.Join(dir, ".beads")
configYAML, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml"))
if err != nil {
t.Fatalf("read config.yaml: %v", err)
}
if got := config.GetStringFromDir(beadsDir, "sync.remote"); got != "" {
t.Fatalf("stealth init persisted sync.remote = %q; config.yaml:\n%s", got, configYAML)
}
}
// bd doctor --fix may exit non-zero for unrelated checks; we only care that it does not
// introduce git-visible changes on a stealth repo.
{
fixCmd := exec.Command(bd, "doctor", "--fix", "--yes")
fixCmd.Dir = dir
fixCmd.Env = bdEnv(dir)
if out, err := fixCmd.CombinedOutput(); err != nil {
t.Logf("bd doctor --fix exited non-zero (tolerated): %v\n%s", err, out)
}
statusCmd := exec.Command("git", "-c", "core.hooksPath=", "status", "--porcelain")
statusCmd.Dir = dir
out, err := statusCmd.CombinedOutput()
if err != nil {
t.Fatalf("git status failed: %v\n%s", err, out)
}
if strings.TrimSpace(string(out)) != "" {
t.Errorf("bd doctor --fix left git-visible changes on a stealth repo:\n%s", out)
}
if got, _ := os.ReadFile(gitignorePath); string(got) != "node_modules/\n" {
t.Errorf("bd doctor --fix modified project .gitignore on a stealth repo:\ngot: %q", string(got))
}
}
})
t.Run("force_reinit", func(t *testing.T) {
doltBin, err := exec.LookPath("dolt")
if err != nil {
t.Skip("dolt CLI not on PATH")
}
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "fi")
dbDir := filepath.Join(beadsDir, "embeddeddolt", "fi")
statusOut := runDolt(t, doltBin, dbDir, "status")
if !strings.Contains(statusOut, "nothing to commit") {
t.Errorf("after first init: expected clean working set, got:\n%s", statusOut)
}
logOut1 := runDolt(t, doltBin, dbDir, "log", "--oneline")
for _, want := range []string{"schema: apply migrations", "bd init"} {
if !strings.Contains(logOut1, want) {
t.Errorf("after first init: missing %q commit:\n%s", want, logOut1)
}
}
headAfterFirst := doltHeadHash(t, doltBin, dbDir)
t.Logf("HEAD after first init: %s", headAfterFirst)
t.Logf("log after first init:\n%s", logOut1)
// Second init with --force
runBDInit(t, bd, dir, "--prefix", "fi", "--force")
statusOut = runDolt(t, doltBin, dbDir, "status")
if !strings.Contains(statusOut, "nothing to commit") {
t.Errorf("after force reinit: expected clean working set, got:\n%s", statusOut)
}
logOut2 := runDolt(t, doltBin, dbDir, "log", "--oneline")
t.Logf("HEAD after force reinit: %s", doltHeadHash(t, doltBin, dbDir))
t.Logf("log after force reinit:\n%s", logOut2)
for _, want := range []string{"schema: apply migrations", "bd init"} {
if !strings.Contains(logOut2, want) {
t.Errorf("after force reinit: missing %q commit:\n%s", want, logOut2)
}
}
commitCount1 := strings.Count(strings.TrimSpace(logOut1), "\n") + 1
commitCount2 := strings.Count(strings.TrimSpace(logOut2), "\n") + 1
if commitCount2 < commitCount1 {
t.Errorf("commit count decreased after force reinit: before=%d after=%d", commitCount1, commitCount2)
}
if val := readBack(t, beadsDir, "fi", "issue_prefix", false); val != "fi" {
t.Errorf("issue_prefix after --force: got %q, want %q", val, "fi")
}
})
t.Run("auto_commit_bypasses_hooks", func(t *testing.T) {
dir := t.TempDir()
initGitRepoAt(t, dir)
templatePath := filepath.Join(dir, "custom-agents.md")
if err := os.WriteFile(templatePath, []byte("# Custom Agents\nThis is custom.\n"), 0644); err != nil {
t.Fatal(err)
}
hookPath := filepath.Join(dir, ".git", "hooks", "prepare-commit-msg")
hook := "#!/bin/sh\necho hook-fired >> .hook-ran\nexit 1\n"
if err := os.WriteFile(hookPath, []byte(hook), 0755); err != nil {
t.Fatal(err)
}
unsetHooksPath := exec.Command("git", "config", "--unset", "core.hooksPath")
unsetHooksPath.Dir = dir
if out, err := unsetHooksPath.CombinedOutput(); err != nil {
t.Fatalf("git config --unset core.hooksPath failed: %v\n%s", err, out)
}
{
cmd := exec.Command(bd, "init", "--prefix", "hook", "--agents-template", templatePath)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd init failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "bd initialized successfully") {
t.Errorf("expected success message, got: %s", stdout.String())
}
}
content, err := os.ReadFile(filepath.Join(dir, "AGENTS.md"))
if err != nil {
t.Fatalf("failed to read AGENTS.md: %v", err)
}
if !strings.Contains(string(content), "Custom Agents") {
t.Error("AGENTS.md should contain custom template content")
}
if _, err := os.Stat(filepath.Join(dir, ".hook-ran")); err == nil {
t.Fatal("expected init auto-commit to bypass git hooks")
}
logCmd := exec.Command("git", "log", "--oneline", "-n", "1")
logCmd.Dir = dir
stdout, stderr, err := runCommandBuffers(t, logCmd)
if err != nil {
t.Fatalf("git log failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "bd init: initialize beads issue tracking") {
t.Fatalf("expected init commit to succeed, got log: %s", stdout.String())
}
})
t.Run("from_jsonl_with_remote_data_requires_discard_and_skips_clone", func(t *testing.T) {
bareDir := filepath.Join(t.TempDir(), "remote.git")
runGitForBootstrapTest(t, "", "init", "--bare", bareDir)
sourceDir := t.TempDir()
runGitForBootstrapTest(t, sourceDir, "init", "-b", "main")
runGitForBootstrapTest(t, sourceDir, "config", "user.email", "test@test.com")
runGitForBootstrapTest(t, sourceDir, "config", "user.name", "Test User")
runGitForBootstrapTest(t, sourceDir, "commit", "--allow-empty", "-m", "init")
runGitForBootstrapTest(t, sourceDir, "remote", "add", "origin", bareDir)
runGitForBootstrapTest(t, sourceDir, "push", "origin", "main")
runGitForBootstrapTest(t, sourceDir, "push", "origin", "HEAD:refs/dolt/data")
dir := t.TempDir()
initGitRepoAt(t, dir)
runGitForBootstrapTest(t, dir, "remote", "add", "origin", bareDir)
beadsDir := filepath.Join(dir, ".beads")
if err := os.MkdirAll(beadsDir, 0750); err != nil {
t.Fatal(err)
}
issue := types.Issue{
ID: "jlremote-abc123",
Title: "JSONL authoritative",
Status: types.StatusOpen,
Priority: 2,
IssueType: types.TypeTask,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
line, _ := json.Marshal(issue)
if err := os.WriteFile(filepath.Join(beadsDir, "issues.jsonl"), append(line, '\n'), 0644); err != nil {
t.Fatal(err)
}
cmd := exec.Command(bd, "init", "--prefix", "jlremote", "--from-jsonl", "--discard-remote", "--destroy-token=DESTROY-jlremote", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("--from-jsonl with authorized remote discard should import without cloning: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
showCmd := exec.Command(bd, "show", "jlremote-abc123", "--json")
showCmd.Dir = dir
showCmd.Env = bdEnv(dir)
out, err := showCmd.CombinedOutput()
if err != nil {
t.Fatalf("imported issue not found: %v\n%s", err, out)
}
if !strings.Contains(string(out), "JSONL authoritative") {
t.Fatalf("imported issue title missing from show output:\n%s", out)
}
})
t.Run("server_flags_ignored", func(t *testing.T) {
_, beadsDir, _ := bdInit(t, bd, "--prefix", "sv",
"--server-host", "10.0.0.1", "--server-port", "4444", "--server-user", "alice")
cfg, err := configfile.Load(beadsDir)
if err != nil {
t.Fatalf("failed to load metadata.json: %v", err)
}
if cfg.DoltServerHost != "10.0.0.1" {
t.Errorf("DoltServerHost: got %q, want %q", cfg.DoltServerHost, "10.0.0.1")
}
if cfg.DoltServerPort != 4444 {
t.Errorf("DoltServerPort: got %d, want %d", cfg.DoltServerPort, 4444)
}
if cfg.DoltServerUser != "alice" {
t.Errorf("DoltServerUser: got %q, want %q", cfg.DoltServerUser, "alice")
}
})
t.Run("invalid_dirname_errors_early", func(t *testing.T) {
// A directory name like "my project" (space) survives hyphen/dot sanitization
// and produces an invalid Dolt database name. The init command should exit
// non-zero with a human-readable error rather than a cryptic storage failure.
parent := t.TempDir()
dir := filepath.Join(parent, "my project")
if err := os.MkdirAll(dir, 0750); err != nil {
t.Fatal(err)
}
initGitRepoAt(t, dir)
cmd := exec.Command(bd, "init", "--quiet")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("bd init should have failed for directory with invalid name")
}
outStr := string(out)
if !strings.Contains(outStr, "invalid database name") && !strings.Contains(outStr, "produces an invalid") {
t.Errorf("expected actionable error message, got: %s", outStr)
}
})
t.Run("remote_host_without_server_mode_fails", func(t *testing.T) {
// When dolt.host is set to a remote address but server mode is not
// enabled, bd init must hard-fail (not fall through to embedded).
dir := t.TempDir()
initGitRepoAt(t, dir)
xdgDir := filepath.Join(dir, ".config", "bd")
if err := os.MkdirAll(xdgDir, 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
if err := os.WriteFile(filepath.Join(xdgDir, "config.yaml"),
[]byte("dolt.host: 100.111.197.110\ndolt.port: 3306\n"), 0o600); err != nil {
t.Fatalf("write config.yaml: %v", err)
}
cmd := exec.Command(bd, "init", "--prefix", "ambi", "--non-interactive")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd init to fail with remote host and no server mode, but it succeeded:\n%s", out)
}
output := string(out)
if !strings.Contains(output, "server mode is not enabled") {
t.Errorf("expected error about server mode not enabled, got:\n%s", output)
}
if !strings.Contains(output, "100.111.197.110") {
t.Errorf("error should mention the configured host, got:\n%s", output)
}
})
t.Run("config_yaml_dolt_mode_server_metadata", func(t *testing.T) {
// When dolt.mode: server is set in config.yaml and init runs in
// embedded mode (no server available), the metadata.json should
// still reflect that server mode was requested. We verify by
// checking that the init process attempted server mode.
dir := t.TempDir()
initGitRepoAt(t, dir)
xdgDir := filepath.Join(dir, ".config", "bd")
if err := os.MkdirAll(xdgDir, 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
if err := os.WriteFile(filepath.Join(xdgDir, "config.yaml"),
[]byte("dolt.mode: server\n"), 0o600); err != nil {
t.Fatalf("write config.yaml: %v", err)
}
// With dolt.mode: server and no actual server, init should fail
// with a connection error — proving that config.yaml triggered
// server mode.
cmd := exec.Command(bd, "init", "--prefix", "srvmode", "--non-interactive", "--quiet")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
// We expect failure because there's no server to connect to.
// The key assertion is that it tried server mode at all.
if err == nil {
// If it succeeded, it created an embedded DB — meaning
// config.yaml dolt.mode was ignored.
beadsDir := filepath.Join(dir, ".beads")
cfg, loadErr := configfile.Load(beadsDir)
if loadErr != nil {
t.Fatalf("bd init succeeded but cannot load metadata: %v", loadErr)
}
if strings.ToLower(cfg.DoltMode) != "server" {
t.Errorf("expected DoltMode=server in metadata, got %q (config.yaml dolt.mode: server was ignored)", cfg.DoltMode)
}
} else {
// Init failed — check that the error is connection-related,
// which proves server mode was attempted.
output := string(out)
if !strings.Contains(output, "connect") && !strings.Contains(output, "server") &&
!strings.Contains(output, "dial") && !strings.Contains(output, "refused") {
t.Errorf("expected server connection error, got:\n%s", output)
}
}
})
t.Run("config_yaml_server_mode_allows_hyphenated_database_name", func(t *testing.T) {
// Server mode allows hyphens in database names. dolt.mode: server from
// config.yaml must be applied before embedded-mode database validation.
dir := t.TempDir()
initGitRepoAt(t, dir)
xdgDir := filepath.Join(dir, ".config", "bd")
if err := os.MkdirAll(xdgDir, 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
if err := os.WriteFile(filepath.Join(xdgDir, "config.yaml"),
[]byte("dolt.mode: server\n"), 0o600); err != nil {
t.Fatalf("write config.yaml: %v", err)
}
cmd := exec.Command(bd, "init", "--prefix", "hyphendb", "--database", "server-db", "--non-interactive", "--quiet")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected server init to fail without a server, but it succeeded:\n%s", out)
}
output := string(out)
if strings.Contains(output, "hyphens which are invalid in embedded mode") {
t.Fatalf("config.yaml dolt.mode: server was applied too late:\n%s", output)
}
if !strings.Contains(output, "connect") && !strings.Contains(output, "server") &&
!strings.Contains(output, "dial") && !strings.Contains(output, "refused") {
t.Errorf("expected server connection error, got:\n%s", output)
}
})
}
// TestEmbeddedInitConcurrent verifies concurrent `bd init` writers are
// serialized rather than corrupting the workspace. The EXCLUSIVE workspace
// gate (see acquireExclusiveWorkspaceGates in cmd/bd/init.go) is acquired
// before the embedded Dolt flock is ever attempted, so contention here
// normally surfaces as gate-busy output rather than a flock error; either is
// classified by isEmbeddedLockOutput as the same "another process holds the
// lock" outcome. At least one process must succeed; unexpected errors still
// fail the test.
//
// It deliberately does NOT require that any racer *observed* contention.
// Whether a waiter blocks and then succeeds or gives up and reports the gate
// busy depends on how long the winner holds the gate versus the waiter's wait
// budget — a property of the machine, not of the lock. On a runner fast enough
// that all ten inits serialize inside that budget, zero lock errors is the
// correct outcome: it means serialization worked and nobody had to give up.
// Requiring one made this test fail on exactly the hardware where the gate was
// working best (GH#4914), and the EXCLUSIVE gate added in #5093 — acquired
// before the embedded flock is ever attempted — makes the serialize-and-succeed
// outcome more likely, not less. Every other concurrency test in this package
// already treats contention as tolerated rather than required; this one was the
// outlier.
//
// The contention path itself is not left uncovered:
// TestInitGateBusyClassifiedAsLockContention (added alongside the gate in
// #5093) exercises it deterministically by holding the gate in-process, which
// is what this test was approximating by racing.
func TestEmbeddedInitConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt init tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir := t.TempDir()
initGitRepoAt(t, dir)
const N = 10
env := bdEnv(dir)
type result struct {
idx int
out string
err error
timedOut bool
}
results := make([]result, N)
var wg sync.WaitGroup
wg.Add(N)
for i := 0; i < N; i++ {
go func(idx int) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, bd, "init", "--prefix", "conc", "--force", "--quiet", "--skip-agents")
cmd.Dir = dir
cmd.Env = env
out, err := cmd.CombinedOutput()
results[idx] = result{idx: idx, out: string(out), err: err, timedOut: ctx.Err() == context.DeadlineExceeded}
}(i)
}
wg.Wait()
successes, lockErrors, timeoutKills := 0, 0, 0
for _, r := range results {
if r.timedOut {
t.Logf("process %d timed out after 90s running concurrent bd init: %v\n%s", r.idx, r.err, r.out)
timeoutKills++
continue
}
if strings.Contains(r.out, "panic") {
t.Errorf("process %d panicked:\n%s", r.idx, r.out)
}
if r.err == nil {
successes++
} else if isEmbeddedLockOutput(r.out) {
lockErrors++
} else {
t.Errorf("process %d failed with unexpected error: %v\n%s", r.idx, r.err, r.out)
}
}
if successes < 1 {
t.Errorf("expected at least 1 success, got %d", successes)
}
// No assertion on lockErrors: see the doc comment. 0 is a valid outcome.
// timeoutKills > 2 (i.e. > N/5) indicates a systemic runner problem, not normal load variance.
if timeoutKills > 2 {
t.Errorf("too many timeout-killed processes: %d/%d (cap is 2)", timeoutKills, N)
}
if successes+lockErrors+timeoutKills != N {
t.Errorf("expected successes (%d) + lock errors (%d) + timeout kills (%d) = %d, got %d",
successes, lockErrors, timeoutKills, N, successes+lockErrors+timeoutKills)
}
t.Logf("%d/%d succeeded, %d/%d got lock error, %d/%d timed out", successes, N, lockErrors, N, timeoutKills, N)
beadsDir := filepath.Join(dir, ".beads")
embeddedDir := filepath.Join(beadsDir, "embeddeddolt")
requireFile(t, embeddedDir)
requireFile(t, filepath.Join(embeddedDir, "conc", ".dolt"))
if val := readBack(t, beadsDir, "conc", "issue_prefix", false); val != "conc" {
t.Errorf("issue_prefix: got %q, want %q", val, "conc")
}
cfg, err := configfile.Load(beadsDir)
if err != nil {
t.Fatalf("failed to load metadata.json: %v", err)
}
if cfg.Backend != configfile.BackendDolt {
t.Errorf("Backend: got %q, want %q", cfg.Backend, configfile.BackendDolt)
}
if doltBin, err := exec.LookPath("dolt"); err == nil {
dbDir := filepath.Join(embeddedDir, "conc")
statusOut := runDolt(t, doltBin, dbDir, "status")
if !strings.Contains(statusOut, "nothing to commit") {
t.Errorf("expected clean working set after concurrent init, got:\n%s", statusOut)
}
logOut := runDolt(t, doltBin, dbDir, "log", "--oneline")
if !strings.Contains(logOut, "schema: apply migrations") {
t.Errorf("missing 'schema: apply migrations' commit:\n%s", logOut)
}
}
}
// TestInitGateBusyClassifiedAsLockContention pins the fix for
// TestEmbeddedInitConcurrent's regression: a losing concurrent `bd init`
// that fails because another process holds the workspace gate EXCLUSIVELY
// (rather than hitting the embedded Dolt flock, which the gate now
// short-circuits before it is ever attempted) must still be classified as
// lock contention by isEmbeddedLockOutput, not as an unexpected failure.
// Deterministic and fast: it holds the gate in-process instead of racing
// subprocesses against a wall-clock deadline, so unlike
// TestEmbeddedInitConcurrent it does not depend on the winner's init being
// slow enough to exhaust another process's wait budget.
func TestInitGateBusyClassifiedAsLockContention(t *testing.T) {
resetGateTestEnv(t)
t.Cleanup(releaseWorkspaceGates)
beadsDir := newGateTestWorkspace(t)
oldWait := exclusiveGateWait
exclusiveGateWait = 10 * time.Millisecond
t.Cleanup(func() { exclusiveGateWait = oldWait })
// Simulate the winner: hold the workspace gate EXCLUSIVELY for the
// duration of its init, exactly as cmd/bd/init.go does.
winner, err := acquireExclusiveWorkspaceGates(context.Background(), beadsDir, "test winner init")
if err != nil {
t.Fatalf("winner acquisition: %v", err)
}
defer func() { _ = winner.Release() }()
// Simulate the loser: bd init's own acquisition call, and its own
// error-wrapping (cmd/bd/init.go: "bd init refuses to run over live bd
// activity on this workspace: %w").
_, gateErr := acquireExclusiveWorkspaceGates(context.Background(), beadsDir, "bd init")
if gateErr == nil {
t.Fatal("loser acquisition under a live exclusive holder must fail")
}
loserOutput := fmt.Errorf("bd init refuses to run over live bd activity on this workspace: %w", gateErr).Error()
if !isEmbeddedLockOutput(loserOutput) {
t.Fatalf("gate-busy loser output not classified as lock contention: %q", loserOutput)
}
}
func TestEmbeddedInitRoleRouting(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt init tests")
}
bd := buildEmbeddedBD(t)
// Serial: newInitRoleFixture owns CWD and environment for each child.
for _, tc := range []struct{ name, initial, flag, want string }{
{"explicit", "maintainer", "contributor", "contributor"},
{"default", "", "", "maintainer"},
{"retained", "contributor", "", "contributor"},
{"fork", "", "", "contributor"},
} {
t.Run(tc.name, func(t *testing.T) {
target, decoy, home := newInitRoleFixture(t)
if tc.initial != "" {
initRoleFixtureGit(t, target, "config", "--local", "beads.role", tc.initial)
}
global := filepath.Join(home, ".gitconfig")
initRoleFixtureGit(t, target, "config", "--file", global, "user.name", "Embedded Init Fixture")
initRoleFixtureGit(t, target, "config", "--file", global, "user.email", "embedded@example.invalid")
t.Setenv("GIT_DIR", filepath.Join(home, "missing.git"))
if isGitRepo() {
t.Fatal("invalid routing must refuse the inherited repository probe")
}
preserveInitRoleInputs(t, filepath.Join(decoy, ".git", "config"), global)
beadsDir := filepath.Join(target, ".beads")
if tc.name == "fork" {
initRoleFixtureGit(t, target, "remote", "add", "upstream", filepath.Join(home, "upstream.git"))
if err := os.MkdirAll(beadsDir, 0750); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("# owned fork configuration\n"), 0600); err != nil {
t.Fatal(err)
}
}
args := []string{"init", "--prefix", "rolefixture", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents"}
if tc.flag != "" {
args = append(args, "--role", tc.flag)
}
cmd := exec.Command(bd, args...)
cmd.Dir = target
env := bdEnv(home)
for _, key := range []string{"BD_DB", "BD_DOLT_HOST", "BD_DOLT_PORT"} {
env = envWithout(env, key)
}
// Explicit storage selection bypasses the separate early git-init guard.
cmd.Env = append(env, "BEADS_DIR="+beadsDir, "DOLT_ROOT_PATH="+home, "BD_DOLT_MODE=embedded", "BD_EVENTS_JOURNAL=false")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("embedded init failed: %v\n%s", err, out)
}
cfg, err := configfile.Load(beadsDir)
if err != nil || cfg == nil || cfg.GetBackend() != configfile.BackendDolt || cfg.DoltMode != configfile.DoltModeEmbedded {
t.Fatalf("init must persist embedded metadata: %+v, %v", cfg, err)
}
if info, err := os.Stat(filepath.Join(beadsDir, "embeddeddolt", "rolefixture", ".dolt")); err != nil || !info.IsDir() {
t.Fatalf("embedded database directory missing: %v", err)
}
if got := initRoleFixtureGit(t, target, "config", "--local", "--get", "beads.role"); got != tc.want {
t.Errorf("embedded init target role = %q, want %q", got, tc.want)
}
if tc.name == "fork" {
planning := filepath.Join(home, ".beads-planning")
if got := initRoleFixtureGit(t, planning, "rev-parse", "--is-inside-work-tree"); got != "true" {
t.Errorf("planning Git repository missing: %q", got)
}
// Read the stored value; config get reads this key's YAML/default source.
routing := readBack(t, beadsDir, "rolefixture", "routing.contributor", false)
if filepath.Clean(routing) != filepath.Clean(planning) {
t.Errorf("persisted contributor routing = %q; want %q", routing, planning)
}
repos, err := config.GetReposFromYAML(filepath.Join(beadsDir, "config.yaml"))
if err != nil || len(repos.Additional) != 1 || filepath.Clean(repos.Additional[0]) != filepath.Clean(planning) {
t.Errorf("planning repo in config.yaml = %+v, %v", repos, err)
}
}
})
}
}
func TestEmbeddedInitSelectedExcludeRouting(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt init tests")
}
bd := buildEmbeddedBD(t)
for _, name := range []string{"stealth_decoy", "stealth_invalid", "fork_decoy", "fork_invalid", "quiet_stealth", "quiet_fork", "fork_auto_invalid"} {
t.Run(name, func(t *testing.T) {
target, decoy, home := newInitRoleFixture(t)
global := filepath.Join(home, ".gitconfig")
initRoleFixtureGit(t, target, "config", "--file", global, "user.name", "Exclude Fixture")
initRoleFixtureGit(t, target, "config", "--file", global, "user.email", "exclude@example.invalid")
exclude := filepath.Join(target, ".git", "info", "exclude")
const existing = "# owned project exclude\nkeep-me/\n"
if err := os.WriteFile(exclude, []byte(existing), 0600); err != nil {
t.Fatal(err)
}
preserveInitRoleInputs(t, filepath.Join(decoy, ".git", "config"), filepath.Join(decoy, ".git", "info", "exclude"), global)
missing := filepath.Join(home, "missing-inherited.git")
routing := filepath.Join(decoy, ".git")
if strings.HasSuffix(name, "invalid") {
routing = missing
}
t.Setenv("GIT_DIR", routing)
t.Setenv("GIT_WORK_TREE", decoy)
storage := filepath.Join(home, "separate storage", ".beads")
if err := os.MkdirAll(filepath.Dir(storage), 0750); err != nil {
t.Fatal(err)
}
args := []string{"init", "--prefix", "excludefixture", "--non-interactive", "--skip-hooks", "--skip-agents", "--role", "maintainer"}
stealth, quiet := strings.Contains(name, "stealth"), strings.HasPrefix(name, "quiet")
want := "**/RECOVERY*.md"
banner := "Added to .git/info/exclude:"
if stealth {
args = append(args, "--stealth")
want, banner = ".claude/settings.local.json", "Stealth mode configured successfully!"
} else if strings.Contains(name, "auto") {
// Auto-detect arm: without --setup-exclude the exclude write is
// reached only through the fork auto-detect gate, so this row
// covers that gate's own repository probe rather than the
// already-hardened writer. An upstream remote makes the scrubbed
// detectForkSetup report a fork; --role=maintainer keeps
// autoConfigureForkContributor an early return, so the gate is
// the only path that can add the pattern.
initRoleFixtureGit(t, target, "remote", "add", "upstream", filepath.Join(home, "upstream.git"))
if isGitRepo() {
t.Fatal("invalid routing must refuse the inherited repository probe")
}
} else {
args = append(args, "--setup-exclude")
}
if quiet {
args = append(args, "--quiet")
}
env := bdEnv(home)
for _, key := range []string{"BEADS_DIR", "BEADS_DB", "BD_DB", "BD_DOLT_HOST", "BD_DOLT_PORT"} {
env = envWithout(env, key)
}
cmd := exec.Command(bd, args...)
cmd.Dir, cmd.Env = target, append(env, "BEADS_DIR="+storage, "DOLT_ROOT_PATH="+home, "BD_DOLT_MODE=embedded", "BD_EVENTS_JOURNAL=false")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("embedded exclude init failed: %v\n%s", err, out)
}
data, err := os.ReadFile(exclude)
if err != nil || !strings.HasPrefix(string(data), existing) || !containsExactPattern(string(data), want) || !containsExactPattern(string(data), ".beads/") {
t.Fatalf("selected exclude = %q (%v); want original lines and %q", data, err, want)
}
if strings.Contains(string(out), banner) == quiet {
t.Errorf("exclude banner does not match quiet=%v: %s", quiet, out)
}
cfg, err := configfile.Load(storage)
if err != nil || cfg == nil || cfg.DoltMode != configfile.DoltModeEmbedded {
t.Fatalf("selected storage metadata = %+v, %v", cfg, err)
}
if info, err := os.Stat(filepath.Join(storage, "embeddeddolt", "excludefixture", ".dolt")); err != nil || !info.IsDir() {
t.Fatalf("embedded database missing from selected storage: %v", err)
}
if _, err := os.Stat(missing); !os.IsNotExist(err) {
t.Errorf("inherited Git directory was created: %v", err)
}
})
}
}
func TestEmbeddedInitArtifactRouting(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt init tests")
}
bd := buildEmbeddedBD(t)
target, decoy, home := newInitRoleFixture(t)
global := filepath.Join(home, ".gitconfig")
initRoleFixtureGit(t, target, "config", "--file", global, "user.name", "Artifact Fixture")
initRoleFixtureGit(t, target, "config", "--file", global, "user.email", "artifact@example.invalid")
t.Setenv("GIT_DIR", filepath.Join(home, "missing.git"))
preserveInitRoleInputs(t, filepath.Join(decoy, ".git", "config"), global)
beadsDir := filepath.Join(target, ".beads")
cmd := exec.Command(bd, "init", "--prefix", "artifactfixture", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents", "--role", "contributor")
cmd.Dir = target
env := bdEnv(home)
for _, key := range []string{"BD_DB", "BD_DOLT_HOST", "BD_DOLT_PORT"} {
env = envWithout(env, key)
}
cmd.Env = append(env, "BEADS_DIR="+beadsDir, "DOLT_ROOT_PATH="+home, "BD_DOLT_MODE=embedded", "BD_EVENTS_JOURNAL=false")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("embedded artifact init failed: %v\n%s", err, out)
}
cfg, err := configfile.Load(beadsDir)
if err != nil || cfg == nil || cfg.GetBackend() != configfile.BackendDolt || cfg.DoltMode != configfile.DoltModeEmbedded {
t.Fatalf("artifact init must persist embedded metadata: %+v, %v", cfg, err)
}
if info, err := os.Stat(filepath.Join(beadsDir, "embeddeddolt", "artifactfixture", ".dolt")); err != nil || !info.IsDir() {
t.Fatalf("embedded artifact database directory missing: %v", err)
}
if got := initRoleFixtureGit(t, target, "config", "--local", "--get", "beads.role"); got != "contributor" {
t.Errorf("embedded role/artifact composition lost explicit role: %q", got)
}
want, err := os.ReadFile(filepath.Join(beadsDir, "metadata.json"))
if err != nil {
t.Fatal(err)
}
show := exec.Command("git", "show", "HEAD:.beads/metadata.json")
show.Dir, show.Env = target, gitenv.ScrubRouting(os.Environ())
if got, err := show.CombinedOutput(); err != nil || !bytes.Equal(got, want) {
t.Errorf("embedded init did not commit target metadata: %v: %s", err, got)
}
}
func TestEmbeddedInitGitBootstrapRouting(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt init tests")
}
bd := buildEmbeddedBD(t)
for _, name := range []string{"fresh", "decoy", "invalid", "existing_invalid", "explicit_storage", "quiet"} {
t.Run(name, func(t *testing.T) {
existing, decoy, home := newInitRoleFixture(t)
target := t.TempDir()
if name == "existing_invalid" {
target = existing
}
global := filepath.Join(home, ".gitconfig")
initRoleFixtureGit(t, existing, "config", "--file", global, "user.name", "Bootstrap Fixture")
initRoleFixtureGit(t, existing, "config", "--file", global, "user.email", "bootstrap@example.invalid")
missingGit := filepath.Join(home, "missing git dir")
if name == "invalid" || name == "existing_invalid" {
t.Setenv("GIT_DIR", missingGit)
} else if name == "decoy" || name == "explicit_storage" {
t.Setenv("GIT_DIR", filepath.Join(decoy, ".git"))
t.Setenv("GIT_WORK_TREE", decoy)
}
preserveInitRoleInputs(t, global, filepath.Join(decoy, ".git", "config"), filepath.Join(decoy, ".git", "HEAD"))
beadsDir := filepath.Join(target, ".beads")
args := []string{"init", "--prefix", "bootstrapfixture", "--non-interactive", "--skip-hooks", "--skip-agents", "--role", "maintainer"}
if name == "quiet" {
args = append(args, "--quiet")
}
cmd := exec.Command(bd, args...)
cmd.Dir = target
env := bdEnv(home)
for _, key := range []string{"BEADS_DIR", "BEADS_DB", "BD_DB", "BD_DOLT_HOST", "BD_DOLT_PORT"} {
env = envWithout(env, key)
}
if name == "explicit_storage" {
beadsDir = filepath.Join(home, "separate storage", ".beads")
if err := os.MkdirAll(filepath.Dir(beadsDir), 0750); err != nil {
t.Fatal(err)
}
env = append(env, "BEADS_DIR="+beadsDir)
}
cmd.Env = append(env, "DOLT_ROOT_PATH="+home, "BD_DOLT_MODE=embedded", "BD_EVENTS_JOURNAL=false")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("embedded bootstrap failed: %v\n%s", err, out)
}
cfg, err := configfile.Load(beadsDir)
if err != nil || cfg == nil || cfg.DoltMode != configfile.DoltModeEmbedded {
t.Fatalf("embedded metadata missing: %+v (%v)", cfg, err)
}
if info, err := os.Stat(filepath.Join(beadsDir, "embeddeddolt", "bootstrapfixture", ".dolt")); err != nil || !info.IsDir() {
t.Fatalf("embedded database missing: %v", err)
}
gitDir := filepath.Join(target, ".git")
if name == "explicit_storage" {
if _, err := os.Stat(gitDir); !os.IsNotExist(err) {
t.Fatalf("explicit storage must skip repository creation: %v", err)
}
} else {
actualGit := initRoleFixtureGit(t, target, "rev-parse", "--absolute-git-dir")
wantInfo, err := os.Stat(gitDir)
if err != nil {
t.Fatal(err)
}
actualInfo, err := os.Stat(actualGit)
if err != nil || !os.SameFile(wantInfo, actualInfo) {
t.Fatalf("selected Git directory = %q (%v)", actualGit, err)
}
}
wantBanner := name != "quiet" && name != "existing_invalid" && name != "explicit_storage"
if got := strings.Count(string(out), "Initialized git repository"); (wantBanner && got != 1) || (!wantBanner && got != 0) {
t.Errorf("initialization banner count = %d, want visible=%v: %s", got, wantBanner, out)
}
if _, err := os.Stat(missingGit); !os.IsNotExist(err) {
t.Errorf("init created the inherited invalid Git directory: %v", err)
}
})
}
}