beads/cmd/bd/dolt.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

2504 lines
91 KiB
Go

package main
import (
"bufio"
"context"
"database/sql"
"errors"
"fmt"
"io"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/doltserver"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dberrors"
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
"github.com/steveyegge/beads/internal/storage/doltutil"
"github.com/steveyegge/beads/internal/storage/versioncontrolops"
"github.com/steveyegge/beads/internal/ui"
"golang.org/x/term"
)
var doltCmd = &cobra.Command{
Use: "dolt",
GroupID: "setup",
Short: "Configure Dolt database settings",
Long: `Configure and manage Dolt database settings and server lifecycle.
Beads runs Dolt embedded (in-process) by default: there is no sql-server and
nothing is auto-started. A database only uses a dolt sql-server when it is
configured for one: shared-server mode, an explicit server mode, or a
non-localhost dolt_server_host. The server-only commands below fail with
"not supported in embedded mode (no Dolt server)" on an embedded database.
Server lifecycle (server mode only):
bd dolt start Start the Dolt server for this project
bd dolt stop Stop the Dolt server for this project
Diagnostics (both modes):
bd dolt status Show Dolt engine status (embedded: in-process, data dir)
bd dolt show Show current Dolt configuration with connection test
Configuration (server mode only):
bd dolt set <k> <v> Set a configuration value
bd dolt test Test server connection
Version control:
bd dolt commit Commit pending changes
bd dolt push Push commits to Dolt remote
bd dolt pull Pull commits from Dolt remote
Remote management:
bd dolt remote add <name> <url> Add a Dolt remote
bd dolt remote list List configured remotes
bd dolt remote remove <name> Remove a Dolt remote
Configuration keys for 'bd dolt set':
database Database name (default: issue prefix or "beads")
host Server host (default: 127.0.0.1)
port Server port (auto-detected; override with bd dolt set port <N>)
user MySQL user (default: root)
data-dir Custom dolt data directory (absolute path; default: .beads/dolt)
Remote server authentication (password + TLS) is NOT stored via 'bd dolt set'
(keeps secrets out of metadata.json). Configure them with:
BEADS_DOLT_PASSWORD Server password (highest priority)
BEADS_DOLT_SERVER_TLS Enable TLS (set to "1" or "true")
BEADS_DOLT_SERVER_USER MySQL user override (else use 'bd dolt set user')
BEADS_CREDENTIALS_FILE Optional path to credentials file
Default credentials file: ~/.config/beads/credentials (Linux/macOS)
%APPDATA%\beads\credentials (Windows)
Format (INI, section = host:port of the resolved connection):
[127.0.0.1:3307]
password = secret
Password resolution: BEADS_DOLT_PASSWORD → credentials [host:port] → empty.
Full reference: docs/architecture/dolt.md (Environment Variables / Credentials).
Flags for 'bd dolt set':
--update-config Also write to config.yaml for team-wide defaults
Examples:
bd dolt set database myproject
bd dolt set host 192.168.1.100 --update-config
bd dolt set data-dir /home/user/.beads-dolt/myproject
export BEADS_DOLT_PASSWORD=... BEADS_DOLT_SERVER_TLS=1
bd dolt test`,
}
var doltShowCmd = &cobra.Command{
Use: "show",
SilenceUsage: true,
SilenceErrors: true,
Short: "Show current Dolt configuration with connection status",
RunE: func(cmd *cobra.Command, args []string) error {
return showDoltConfig(true)
},
}
var doltSetCmd = &cobra.Command{
Use: "set <key> <value>",
SilenceUsage: true,
SilenceErrors: true,
Short: "Set a Dolt configuration value",
Long: `Set a Dolt configuration value in metadata.json.
Keys:
database Database name (default: issue prefix or "beads")
host Server host (default: 127.0.0.1)
port Server port (auto-detected; override with bd dolt set port <N>)
user MySQL user (default: root)
data-dir Custom dolt data directory (absolute path; default: .beads/dolt)
There is no 'password' or 'tls' key here on purpose — secrets and TLS must
not land in metadata.json. Use environment variables or the credentials file:
BEADS_DOLT_PASSWORD Server password (highest priority)
BEADS_DOLT_SERVER_TLS Enable TLS ("1" or "true")
BEADS_CREDENTIALS_FILE Optional override path for credentials
Default credentials file: ~/.config/beads/credentials
Format:
[host:port]
password = secret
See: bd dolt --help and docs/architecture/dolt.md
Use --update-config to also write to config.yaml for team-wide defaults.
Examples:
bd dolt set database myproject
bd dolt set host 192.168.1.100
bd dolt set port 3307 --update-config
bd dolt set data-dir /home/user/.beads-dolt/myproject
export BEADS_DOLT_PASSWORD=... BEADS_DOLT_SERVER_TLS=1`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
return HandleError("%v", err)
}
if !usesSQLServer() {
return HandleError("'bd dolt set' is not supported in embedded mode (no Dolt server)")
}
key := args[0]
value := args[1]
updateConfig, _ := cmd.Flags().GetBool("update-config")
return setDoltConfig(key, value, updateConfig)
},
}
var doltTestCmd = &cobra.Command{
Use: "test",
SilenceUsage: true,
SilenceErrors: true,
Short: "Test connection to Dolt server",
Long: `Test the connection to the configured Dolt server.
This verifies that:
1. The server is reachable at the configured host:port
2. The connection can be established
Use this before switching to server mode to ensure the server is running.`,
RunE: func(cmd *cobra.Command, args []string) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
return HandleError("%v", err)
}
if !usesSQLServer() {
return HandleError("'bd dolt test' is not supported in embedded mode (no Dolt server)")
}
return testDoltConnection()
},
}
// isRemoteNotFoundErr checks whether the error is a Dolt "remote not found"
// error. This typically happens when the remote was added via `dolt remote add`
// (filesystem config) but not via `bd dolt remote add` (which also registers it
// in the SQL server's dolt_remotes table).
func isRemoteNotFoundErr(err error) bool {
if err == nil {
return false
}
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "remote") && strings.Contains(msg, "not found")
}
// remoteLister is the narrow store surface needed to confirm the structured
// no-remote-configured state.
type remoteLister interface {
ListRemotes(ctx context.Context) ([]storage.RemoteInfo, error)
}
// persistedRemoteProber is implemented by stores that can check on-disk
// remote persistence (.dolt/repo_state.json) independently of the SQL
// server's dolt_remotes table (server-mode DoltStore).
type persistedRemoteProber interface {
HasPersistedRemote() bool
}
// isConfirmedNoRemote reports whether a push/pull failure is the benign
// "no remote configured" case that may exit 0. isRemoteNotFoundErr alone is a
// loose string match that also fires on deleted/renamed remote-side repos,
// missing remote branches, and typoed remote names — real sync failures that
// must keep a non-zero exit so agents and CI notice (bd-6dnrw.7). Only an
// actually-empty dolt_remotes table makes the skip safe; if the remotes can't
// be listed, treat the failure as real. An empty table alone is still not
// proof in server mode: a freshly auto-started sql-server can report empty
// dolt_remotes at cold start even though remotes are persisted on disk
// (GH#2118) — the same reason the remote-migrate gate reads repo_state.json
// directly — so the on-disk probe must agree before the skip fires
// (bd-578h9.10).
func isConfirmedNoRemote(ctx context.Context, st remoteLister, err error) bool {
if !isRemoteNotFoundErr(err) {
return false
}
return hasNoRemoteConfigured(ctx, st)
}
// hasNoRemoteConfigured is the structural half of isConfirmedNoRemote: the
// positive proof that this rig really has no remote, independent of how the
// failure worded itself. It is what actually makes an exit-0 skip safe, so a
// caller with a different (broader) error classification — `bd sync`, which
// runs on a timer and must not fail every tick on a solo rig — can reuse the
// proof without loosening it.
func hasNoRemoteConfigured(ctx context.Context, st remoteLister) bool {
configured, listErr := hasConfiguredRemote(ctx, st)
return listErr == nil && !configured
}
// hasConfiguredRemote decides, from evidence, whether a rig has a Dolt remote
// at all. It is the one decider for the push/sync no-remote question: both
// hasNoRemoteConfigured (the `bd sync` / `bd dolt push|pull` exit-0 gate) and
// adoptGitOriginRemoteForPush go through it. The MUTATING siblings —
// `bd config apply`, ensureDoltRemote, and the git-protocol CLI push route —
// consult the same persisted evidence via PersistedRemoteInfos (wy-6k7f7);
// the read-only doctor/drift diagnostics still judge from len(ListRemotes)
// alone and can report a false "no origin remote" during the cold-start
// window. Do not assume a new remote-related decision is covered by this
// function: route it here.
//
// Two sources of evidence, because dolt_remotes alone is not enough: a
// server-mode rig whose sql-server has just cold-started can report an EMPTY
// dolt_remotes while the remote IS persisted on disk in .dolt/repo_state.json
// (GH#2118), so the on-disk probe gets a veto. The probe has to be reached
// through the storage decorator chain, since HasPersistedRemote is not part of
// storage.DoltStorage and the store bd holds is all but always decorated —
// that is wy-xtv17.
//
// A failed listing is neither evidence: it returns the error, and callers must
// not read it as "no remote". Having two sibling functions decide this from
// different evidence is what left `bd dolt push` still trusting an empty
// dolt_remotes after wy-xtv17 hardened the no-remote gate (wy-82hc5), so the
// rule lives here once.
func hasConfiguredRemote(ctx context.Context, st remoteLister) (bool, error) {
remotes, listErr := st.ListRemotes(ctx)
if listErr != nil {
return false, listErr
}
if len(remotes) > 0 {
return true, nil
}
if prober, ok := persistedRemoteProberFor(st); ok && prober.HasPersistedRemote() {
return true, nil
}
return false, nil
}
// persistedRemoteProberFor finds the on-disk remote probe behind any chain of
// storage decorators.
//
// HasPersistedRemote is not part of storage.DoltStorage — only the concrete
// *dolt.DoltStore implements it — while the store bd actually holds is the
// composed chain caller → HookFiringStore → InstrumentedStorage → DoltStore
// (wireStorageDecorators). The hook layer is present on essentially every rig:
// main.go builds a hook runner whenever there is a dbPath, whether or not any
// hook scripts exist, so only no-hooks:true / BD_NO_HOOKS=1 leaves it off.
// Asserting straight on the passed store therefore all but always failed,
// silently skipping the GH#2118 cold-start probe and letting `bd sync` /
// `bd dolt push|pull` report "no remote configured" and exit 0 forever on a rig
// whose remote is persisted in .dolt/repo_state.json (wy-xtv17).
//
// It peels via storage.Unwrapper, the same contract storage.UnwrapStore uses,
// rather than calling UnwrapStore itself: this helper takes the narrow
// remoteLister, not a storage.DoltStorage. A store that implements the probe
// directly is honored before any peeling, so test doubles and any future
// decorator that forwards HasPersistedRemote keep working.
func persistedRemoteProberFor(st remoteLister) (persistedRemoteProber, bool) {
for {
if prober, ok := st.(persistedRemoteProber); ok {
return prober, true
}
u, ok := st.(storage.Unwrapper)
if !ok {
return nil, false
}
inner := u.Unwrap()
if inner == nil {
return nil, false
}
st = inner
}
}
// persistedRemoteInfoLister is the recovery-grade sibling of
// persistedRemoteProber: stores that can enumerate the on-disk remotes
// (name AND url) rather than only report their existence (server-mode
// DoltStore). Callers use it in the GH#2118 cold-start window to act on the
// invisible remote's actual URL instead of merely refusing (wy-6k7f7).
type persistedRemoteInfoLister interface {
PersistedRemoteInfos() []storage.RemoteInfo
}
// persistedRemoteInfosFor finds the on-disk remote enumeration behind any
// chain of storage decorators, peeling exactly like persistedRemoteProberFor
// (a direct implementer is honored before any peeling, so test doubles work).
// Returns nil when no store in the chain can enumerate persisted remotes —
// embedded rigs, where GH#2118 cannot occur.
func persistedRemoteInfosFor(st any) []storage.RemoteInfo {
for {
if lister, ok := st.(persistedRemoteInfoLister); ok {
return lister.PersistedRemoteInfos()
}
u, ok := st.(storage.Unwrapper)
if !ok {
return nil
}
inner := u.Unwrap()
if inner == nil {
return nil
}
st = inner
}
}
// isDivergedHistoryErr checks whether the error indicates that local and remote
// Dolt histories have diverged. This happens when independent pushes create
// separate commit histories with no common merge base (e.g., two agents
// bootstrapping from scratch and pushing to the same remote, or a local
// database being re-initialized while the remote retains the old history).
func isDivergedHistoryErr(err error) bool {
if err == nil {
return false
}
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "no common ancestor") ||
strings.Contains(msg, "can't find common ancestor") ||
strings.Contains(msg, "cannot find common ancestor")
}
// isAncestorPKMismatchErr reports Dolt's hard refusal to merge a table whose
// primary key set differs across the merging histories or in their common
// ancestor. The classification lives in dberrors so the cross-upgrade merge
// test (internal/storage/dolt) can pin it against a real Dolt refusal; see
// dberrors.IsAncestorPKMismatch for the full background (#4259).
func isAncestorPKMismatchErr(err error) bool {
return dberrors.IsAncestorPKMismatch(err)
}
// ancestorPKMismatchTable extracts the table name from a Dolt
// different-primary-keys merge refusal, or "" if it cannot be determined.
func ancestorPKMismatchTable(err error) string {
return dberrors.AncestorPKMismatchTable(err)
}
// printAncestorPKMismatchGuidance prints recovery guidance when a Dolt merge
// is refused because a table's primary key set differs across the merging
// histories or in their common ancestor. Unlike row conflicts, this cannot be
// auto-resolved and does not converge on retry; the clones must be
// re-converged through one canonical clone.
func printAncestorPKMismatchGuidance(err error) {
w := os.Stderr
table := ancestorPKMismatchTable(err)
fmt.Fprintln(w, "")
if table != "" {
fmt.Fprintf(w, "Dolt refused to merge: table %q has different primary keys across\n", table)
} else {
fmt.Fprintln(w, "Dolt refused to merge: a table has different primary keys across")
}
fmt.Fprintln(w, "the local and remote histories (or in their common ancestor).")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "This is a schema fork: two clones reshaped the table's primary key")
fmt.Fprintln(w, "independently, usually by upgrading bd (and so running schema migrations)")
fmt.Fprintln(w, "separately on each clone while un-synced changes existed on both sides.")
fmt.Fprintln(w, "Retrying will not help — these histories can no longer be merged.")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "Recovery (bootstrap from one canonical clone):")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " 1. Pick ONE clone as canonical (usually the most complete/up-to-date),")
fmt.Fprintln(w, " upgrade bd there, and make the remote authoritative:")
fmt.Fprintln(w, " bd dolt push --force")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " 2. On EVERY other clone, save local-only work, re-clone, re-apply:")
fmt.Fprintln(w, " bd export --all -o /tmp/beads-local.jsonl")
fmt.Fprintln(w, " rm -rf .beads/dolt")
fmt.Fprintln(w, " bd bootstrap")
fmt.Fprintln(w, " bd import /tmp/beads-local.jsonl")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "Full playbook (and how to prevent this during upgrades):")
fmt.Fprintln(w, " https://github.com/gastownhall/beads/blob/main/docs/recovery/init-safety.md#pk-fork-refused")
}
// printNoRemoteGuidance prints an informational message (to stdout) when
// push or pull is attempted but no Dolt remote is configured. Exits 0 because
// the absence of a remote is a valid configuration — not an error.
func printNoRemoteGuidance() {
fmt.Println("No remote is configured — skipping.")
fmt.Println("")
fmt.Println("For solo use, pushing is optional — your issues are stored locally")
fmt.Println("in .beads/ and versioned by Dolt automatically.")
fmt.Println("")
fmt.Println("To set up remote sync (for backup or team sharing):")
fmt.Println(" bd dolt remote add origin <url>")
fmt.Println(" bd dolt push")
fmt.Println("")
fmt.Println("Supported remote URLs:")
fmt.Println(" • GitHub (via git): git+ssh://git@github.com/org/repo.git")
fmt.Println(" • DoltHub: https://doltremoteapi.dolthub.com/org/repo")
fmt.Println(" • Azure Blob Storage: az://account.blob.core.windows.net/container/path")
}
// adoptGitOriginRemoteForPush gives a rig with no Dolt remote the one its git
// origin implies, so `bd dolt push` works out of the box.
//
// It asks hasConfiguredRemote rather than reading len(ListRemotes) itself: an
// empty dolt_remotes is not proof of a remote-less rig during the GH#2118
// cold-start window, and adopting there re-derives the remote from git.
// Usually that is the same URL and the AddRemote is a harmless re-add, but
// when the persisted remote and the git origin disagree — a Dolt remote
// deliberately pointed elsewhere, or a renamed/redirected origin — the rig
// starts pushing somewhere else on the strength of a stale listing (wy-82hc5).
//
// Adoption requires consent (#5068). The policy is decided by
// decideRemoteAdoption in dolt_remote_adopt.go and applied here, after the URL
// is known and before anything is written: nothing below this point is
// reachable without either --yes or an interactive confirmation.
func adoptGitOriginRemoteForPush(ctx context.Context, st storage.DoltStorage, policy adoptPolicy, optIn adoptOptIn) (bool, error) {
configured, err := hasConfiguredRemote(ctx, st)
if err != nil || configured {
return false, err
}
// Deriving the URL comes first and is read-only (`git remote get-url`).
// Workspace resolution is deliberately NOT done yet: selectedDoltBeadsDir
// calls prepareSelectedNoDBContext, which mutates process and on-disk
// workspace state, and nothing may mutate before consent is established.
originURL, err := gitOriginGetURLForActiveRepo(ctx)
if err != nil || originURL == "" {
return false, nil
}
remoteURL := normalizeRemoteURL(originURL)
if proceed, err := applyAdoptionConsent(remoteURL, policy, optIn); err != nil || !proceed {
return false, err
}
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return false, fmt.Errorf("no active beads workspace")
}
if err := st.AddRemote(ctx, "origin", remoteURL); err != nil {
return false, err
}
if err := config.SetYamlConfigInDir(beadsDir, "sync.remote", remoteURL); err != nil {
return false, fmt.Errorf("failed to persist sync.remote to config.yaml: %w", err)
}
fmt.Fprintln(os.Stderr, "Committing .beads/config.yaml (sync.remote) under your git identity.")
commitBeadsConfigForActiveRepo(ctx, "bd: update sync.remote")
return true, nil
}
// printDivergedHistoryGuidance prints recovery guidance when push/pull fails
// due to diverged local and remote histories.
func printDivergedHistoryGuidance(operation string) {
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Local and remote Dolt histories have diverged.")
fmt.Fprintln(os.Stderr, "This means the local database and the remote have independent commit")
fmt.Fprintln(os.Stderr, "histories with no common merge base.")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Recovery options:")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, " 1. Keep remote, discard local (recommended if remote is authoritative):")
fmt.Fprintln(os.Stderr, " bd bootstrap # re-clone from remote")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, " 2. Keep local, overwrite remote (if local is authoritative):")
fmt.Fprintln(os.Stderr, " bd dolt push --force # force-push local history to remote")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, " 3. Manual recovery (re-initialize local database):")
fmt.Fprintln(os.Stderr, " rm -rf .beads/dolt # delete local Dolt database")
fmt.Fprintln(os.Stderr, " bd bootstrap # re-clone from remote")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Tip: This usually happens when multiple agents independently initialize")
fmt.Fprintln(os.Stderr, "databases and push to the same remote. Use 'bd bootstrap' to clone an")
fmt.Fprintln(os.Stderr, "existing remote instead of 'bd init' to avoid divergent histories.")
}
var doltPushCmd = &cobra.Command{
Use: "push",
SilenceUsage: true,
SilenceErrors: true,
Short: "Push commits to Dolt remote",
Long: `Push local Dolt commits to the configured remote.
Requires a Dolt remote to be configured in the database directory. With no
remote configured, bd can adopt one derived from git origin — only with
consent: interactively, or via --yes; --no-adopt or BD_NO_REMOTE_ADOPT=1
disables adoption entirely.
For Hosted Dolt, set DOLT_REMOTE_USER and DOLT_REMOTE_PASSWORD environment
variables for authentication.
Use --force to overwrite remote changes (e.g., when the remote has
uncommitted changes in its working set).
Use --remote to push to a specific named remote instead of the default.
The remote must already exist (see 'bd dolt remote add').`,
RunE: func(cmd *cobra.Command, args []string) error {
if config.GetBool("no-push") {
fmt.Println("skipping push: rig is local-only (no-push: true)")
return nil
}
if isDoltLocalOnly() {
if jsonOutput {
if err := outputJSONRaw(map[string]string{"status": "disabled", "reason": "dolt.local-only=true"}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return nil
}
fmt.Println("Remote sync is disabled for this project (dolt.local-only=true).")
fmt.Println("Your issues are stored locally in .beads/.")
fmt.Println("To re-enable remote sync: bd config unset dolt.local-only")
return nil
}
ctx := context.Background()
st := getStore()
if st == nil {
return HandleError("no store available")
}
force, _ := cmd.Flags().GetBool("force")
remote, _ := cmd.Flags().GetString("remote")
if remote != "" {
fmt.Printf("Pushing to Dolt remote %q...\n", remote)
if err := st.PushRemote(ctx, remote, force); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
if isRemoteNotFoundErr(err) {
fmt.Fprintf(os.Stderr, "\nRemote %q is not configured.\n", remote)
fmt.Fprintln(os.Stderr, "Use 'bd dolt remote add <name> <url>' to add it.")
fmt.Fprintln(os.Stderr, "Use 'bd dolt remote list' to see configured remotes.")
} else if isAncestorPKMismatchErr(err) {
printAncestorPKMismatchGuidance(err)
} else if isDivergedHistoryErr(err) {
printDivergedHistoryGuidance("push --force")
}
return SilentExit()
}
fmt.Println("Push complete.")
return nil
}
assumeYes, _ := cmd.Flags().GetBool("yes")
noAdopt, _ := cmd.Flags().GetBool("no-adopt")
policy := currentAdoptPolicy(assumeYes, noAdopt, stdinIsTerminal(), jsonOutput)
if adopted, err := adoptGitOriginRemoteForPush(ctx, st, policy, pushAdoptOptIn); err != nil {
return HandleError("%v", err)
} else if adopted {
fmt.Println("Configured Dolt remote origin from git origin.")
}
fmt.Println("Pushing to Dolt remote...")
var pushErr error
if force {
pushErr = st.ForcePush(ctx)
} else {
pushErr = st.Push(ctx)
}
if pushErr != nil {
if isConfirmedNoRemote(ctx, st, pushErr) {
printNoRemoteGuidance()
return nil
}
fmt.Fprintf(os.Stderr, "Error: %v\n", pushErr)
if isAncestorPKMismatchErr(pushErr) {
printAncestorPKMismatchGuidance(pushErr)
} else if isDivergedHistoryErr(pushErr) {
op := "push"
if force {
op = "push --force"
}
printDivergedHistoryGuidance(op)
}
return SilentExit()
}
fmt.Println("Push complete.")
return nil
},
}
var doltPullCmd = &cobra.Command{
Use: "pull",
SilenceUsage: true,
SilenceErrors: true,
Short: "Pull commits from Dolt remote",
Long: `Pull commits from the configured Dolt remote into the local database.
Requires a Dolt remote to be configured in the database directory.
For Hosted Dolt, set DOLT_REMOTE_USER and DOLT_REMOTE_PASSWORD environment
variables for authentication.
Use --remote to pull from a specific named remote instead of the default.
The remote must already exist (see 'bd dolt remote add').
Use --strategy ours|theirs to resolve conflicts the auto-resolver declines
(e.g. both sides edited the same issue since the last sync) instead of
aborting the pull for manual resolution. Embedded storage only (#4992); on
server-mode/sql-server storage use 'bd conflicts resolve' after a pull that
reports conflicts.`,
RunE: func(cmd *cobra.Command, args []string) error {
if isDoltLocalOnly() {
if jsonOutput {
if err := outputJSONRaw(map[string]string{"status": "disabled", "reason": "dolt.local-only=true"}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return nil
}
fmt.Println("Remote sync is disabled for this project (dolt.local-only=true).")
fmt.Println("Nothing to pull.")
fmt.Println("To re-enable remote sync: bd config unset dolt.local-only")
return nil
}
ctx := context.Background()
st := getStore()
if st == nil {
return HandleError("no store available")
}
remote, _ := cmd.Flags().GetString("remote")
strategy, _ := cmd.Flags().GetString("strategy")
if strategy != "" {
if err := versioncontrolops.ValidateConflictStrategy(strategy); err != nil {
return HandleError("%v", err)
}
}
var puller storage.StrategicPuller
if strategy != "" {
var ok bool
puller, ok = storage.UnwrapStore(st).(storage.StrategicPuller)
if !ok {
return HandleError("storage backend %T does not support --strategy pulls (#4992): only embedded storage does; on server-mode/sql-server storage, resolve conflicts with 'bd conflicts resolve' or the raw dolt CLI", storage.UnwrapStore(st))
}
}
if remote != "" {
fmt.Printf("Pulling from Dolt remote %q...\n", remote)
var err error
if strategy != "" {
err = puller.PullRemoteWithStrategy(ctx, remote, strategy)
} else {
err = st.PullRemote(ctx, remote)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
if isRemoteNotFoundErr(err) {
fmt.Fprintf(os.Stderr, "\nRemote %q is not configured.\n", remote)
fmt.Fprintln(os.Stderr, "Use 'bd dolt remote add <name> <url>' to add it.")
fmt.Fprintln(os.Stderr, "Use 'bd dolt remote list' to see configured remotes.")
} else if isAncestorPKMismatchErr(err) {
printAncestorPKMismatchGuidance(err)
} else if isDivergedHistoryErr(err) {
printDivergedHistoryGuidance("pull")
}
return SilentExit()
}
fmt.Println("Pull complete.")
return nil
}
fmt.Println("Pulling from Dolt remote...")
var err error
if strategy != "" {
err = puller.PullWithStrategy(ctx, strategy)
} else {
err = st.Pull(ctx)
}
if err != nil {
if isConfirmedNoRemote(ctx, st, err) {
printNoRemoteGuidance()
return nil
}
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
if isAncestorPKMismatchErr(err) {
printAncestorPKMismatchGuidance(err)
} else if isDivergedHistoryErr(err) {
printDivergedHistoryGuidance("pull")
}
return SilentExit()
}
fmt.Println("Pull complete.")
return nil
},
}
var doltCommitCmd = &cobra.Command{
Use: "commit",
Short: "Create a Dolt commit from pending changes",
Long: `Create a Dolt commit from any uncommitted changes in the working set.
This is the primary commit point for batch mode. When auto-commit is set to
"batch", changes accumulate in the working set across multiple bd commands and
are committed together here with a descriptive summary message.
Also useful before push operations that require a clean working set, or when
auto-commit was off or changes were made externally.
For more options (--stdin, custom messages), see: bd vc commit`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
msg, _ := cmd.Flags().GetString("message")
if msg == "" {
msg = fmt.Sprintf("bd: dolt commit (auto-commit) by %s", getActor())
}
var (
committed bool
err error
)
if usesProxiedServer() {
// Proxied mode never opens a store — the root pre-run returns
// before newDoltStore — so getStore() is nil here and the UOW
// provider is the only handle on the server. This is the flush
// point dolt.auto-commit=batch/off defers to on that route
// (GH#4995). The message is built above so both routes name the
// same default.
committed, err = runDoltCommitProxiedServer(ctx, msg)
} else {
st := getStore()
if st == nil {
return HandleError("no store available")
}
// CommitAll, not Commit: this command's contract is "any uncommitted
// changes in the working set", including changes made externally and
// the config table — which server-mode Commit excludes (GH#2455), so
// out-of-band config dirt used to survive this command forever. Its
// committed bool also replaces the HEAD-before/HEAD-after comparison
// this command used to detect tolerated no-ops with, which cost two
// extra HEAD reads and raced against concurrent writers.
committed, err = st.CommitAll(ctx, msg)
}
if err != nil {
switch {
case isDoltNothingToCommit(err):
committed = false
case isReportedExit(err):
// The proxied route reports provider failures itself, through
// HandleErrorRespectJSON, so the JSON envelope is already on
// stdout; re-wrapping would print a second, plain Error line.
return err
default:
return HandleError("%v", err)
}
}
if !committed {
fmt.Println("Nothing to commit.")
return nil
}
commandDidExplicitDoltCommit = true
fmt.Println("Committed.")
return nil
},
}
var doltStartCmd = &cobra.Command{
Use: "start",
SilenceUsage: true,
SilenceErrors: true,
Short: "Start the Dolt SQL server for this project",
Long: `Start a dolt sql-server for the current beads project.
The server runs in the background on a per-project port derived from the
project path. PID and logs are stored in .beads/.
The server auto-starts transparently when needed, so manual start is rarely
required. Use this command for explicit control or diagnostics.
Not available in proxied-server mode: there the proxy owns its dolt backend's
lifecycle and starts it on demand, so a server started here would be a second,
unsupervised one over the same data directory. Use 'bd dolt status' to see what
is running and 'bd dolt stop' to shut it down.`,
RunE: func(cmd *cobra.Command, args []string) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
fileCfg, err := loadDoltBackendConfig(beadsDir)
if err != nil {
return HandleError("%v", err)
}
// A proxied workspace already has a server manager. The proxy spawns
// its dolt child on demand and reaps the whole tree when idle, so
// there is no state for bd to add here — only a second one to
// collide with. With the tree down, doltserver.Start resolves the
// proxied root's own configured port and launches an unsupervised
// sql-server directly over .beads/dolt, and the next ordinary bd
// command then fails because the relaunched proxy finds a foreign
// server on its port; with the tree up, Start instead adopts the
// proxy's child into bd's classic PID/port records, leaving two
// managers for one process. Refused rather than routed: the proxy
// owns its child's lifecycle, and an idle reaper means a "started"
// postcondition bd cannot honor for more than the idle window.
//
// Both the process-wide mode and the selected workspace's own
// metadata are consulted, and this runs before the embedded guard:
// over-refusing costs an operator one error message, under-refusing
// costs them the datastore.
if usesProxiedServer() || fileCfg.IsDoltProxiedServerMode() {
return HandleProxyCapabilityError(proxiedDoltStartRefusal())
}
if !usesSQLServer() {
return HandleError("'bd dolt start' is not supported in embedded mode (no Dolt server)")
}
// A remote (non-localhost) server host means bd does not own the
// server lifecycle (GH#3545/GH#3518): starting a repo-local
// server here would write local PID/port state that shadows the
// configured remote endpoint.
if host := fileCfg.GetDoltServerHost(); !configfile.IsLocalHostString(host) {
return HandleError("the configured Dolt server host is remote (%s); 'bd dolt start' only manages a local server.\nStart the server on that host, or clear dolt_server_host / dolt.host / BEADS_DOLT_SERVER_HOST to run one locally", host)
}
serverDir := doltserver.ResolveServerDir(beadsDir)
state, err := doltserver.Start(serverDir)
if err != nil {
if strings.Contains(err.Error(), "already running") {
fmt.Println(err)
return nil
}
return HandleError("%v", err)
}
fmt.Printf("Dolt server started (PID %d, port %d)\n", state.PID, state.Port)
fmt.Printf(" Data: %s\n", state.DataDir)
fmt.Printf(" Logs: %s\n", doltserver.LogPath(serverDir))
if doltserver.IsSharedServerMode() {
fmt.Println(" Mode: shared server")
}
if doltserver.IsDebugMode() {
fmt.Println(" Debug: on (loglevel=debug, --prof cpu)")
fmt.Printf(" Profile dir: %s\n", doltserver.DebugProfileDir(beadsDir))
fmt.Println(" Note: cpu.pprof is written when the server exits cleanly (bd dolt stop).")
}
return nil
},
}
var doltStopCmd = &cobra.Command{
Use: "stop",
SilenceUsage: true,
SilenceErrors: true,
Short: "Stop the Dolt SQL server for this project",
Long: `Stop the dolt sql-server managed by beads for the current project.
This sends a graceful shutdown signal. The server will restart automatically
on the next bd command unless auto-start is disabled.
For a managed proxied server, --force can recover unverifiable or legacy
process records (both the proxy and its backend) only after each live process
executable is matched to bd or dolt and its command line ties it to this
workspace. In that recovery path, force still refuses to signal a process
whose executable identity cannot be matched to bd or dolt, or whose workspace
scope cannot be established.`,
RunE: func(cmd *cobra.Command, args []string) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
fileCfg, err := loadDoltBackendConfig(beadsDir)
if err != nil {
return HandleError("%v", err)
}
if !usesSQLServer() {
return HandleError("'bd dolt stop' is not supported in embedded mode (no Dolt server)")
}
// Same remote-host ownership guard as 'bd dolt start': with a
// remote server host, the repo-local PID state (if any) is a
// leftover, and stopping it would report success while the
// configured external server keeps running (GH#3545/GH#3518).
if host := fileCfg.GetDoltServerHost(); !usesProxiedServer() && !configfile.IsLocalHostString(host) {
return HandleError("the configured Dolt server host is remote (%s); 'bd dolt stop' only manages a local server.\nStop the server on that host, or clear dolt_server_host / dolt.host / BEADS_DOLT_SERVER_HOST to manage one locally", host)
}
force, _ := cmd.Flags().GetBool("force")
if usesProxiedServer() {
rootDir, err := resolveProxiedServerRootPath(beadsDir)
if err != nil {
return HandleError("%v", err)
}
shutdownErr := proxy.Shutdown(rootDir)
if shutdownErr == nil {
return renderDoltStopResult(doltStopResult{
Stopped: true,
Force: force,
Verified: boolPointer(true),
})
}
if !force || !proxy.CanForceStopUnverified(shutdownErr) {
return HandleErrorRespectJSON("%v", shutdownErr)
}
report, forceErr := proxy.ForceStopUnverified(rootDir)
return renderDoltStopResult(newForcedDoltStopResult(shutdownErr, report, forceErr))
}
serverDir := doltserver.ResolveServerDir(beadsDir)
if err := doltserver.StopWithForce(serverDir, force); err != nil {
return HandleError("%v", err)
}
return renderDoltStopResult(doltStopResult{
Stopped: true,
Force: force,
})
},
}
// doltStopResult is the shared JSON object for successful and refused stop
// operations. Force-stop recovery deliberately exposes each irreversible
// action so automation can distinguish a matched executable from a signaled
// process and a quarantined record from one left in place.
type doltStopResult struct {
Stopped bool `json:"stopped"`
Force bool `json:"force"`
ForcedRecovery bool `json:"forced_recovery,omitempty"`
Verified *bool `json:"verified,omitempty"`
VerifiedShutdownError string `json:"verified_shutdown_error,omitempty"`
RecordFound bool `json:"record_found,omitempty"`
RecordPath string `json:"record_path,omitempty"`
RecordLeftAlone bool `json:"record_left_alone,omitempty"`
LockWasHeld bool `json:"lock_was_held,omitempty"`
PID int `json:"pid,omitempty"`
Executable string `json:"executable,omitempty"`
ExecutableVerified *bool `json:"executable_verified,omitempty"`
ProcessWasGone bool `json:"process_was_gone,omitempty"`
SignalSent bool `json:"signal_sent,omitempty"`
ProcessLeftAlone bool `json:"process_left_alone,omitempty"`
QuarantinedPath string `json:"quarantined_path,omitempty"`
Backend *doltStopRecordResult `json:"backend,omitempty"`
Error string `json:"error,omitempty"`
}
// doltStopRecordResult mirrors the per-record force-stop fields for the
// backend (proxy-child) record.
type doltStopRecordResult struct {
RecordFound bool `json:"record_found,omitempty"`
RecordPath string `json:"record_path,omitempty"`
LockWasHeld bool `json:"lock_was_held,omitempty"`
PID int `json:"pid,omitempty"`
Executable string `json:"executable,omitempty"`
ProcessWasGone bool `json:"process_was_gone,omitempty"`
SignalSent bool `json:"signal_sent,omitempty"`
QuarantinedPath string `json:"quarantined_path,omitempty"`
}
func newForcedDoltStopResult(
shutdownErr error,
report proxy.ForceStopReport,
forceErr error,
) doltStopResult {
result := doltStopResult{
Stopped: forceErr == nil,
Force: true,
ForcedRecovery: true,
Verified: boolPointer(false),
VerifiedShutdownError: shutdownErr.Error(),
RecordFound: report.RecordFound,
RecordPath: report.RecordPath,
LockWasHeld: report.LockWasHeld,
PID: report.PID,
Executable: report.Executable,
ProcessWasGone: report.ProcessWasGone,
SignalSent: report.SignalSent,
QuarantinedPath: report.QuarantinedPath,
}
if report.Executable != "" {
result.ExecutableVerified = boolPointer(
report.Executable == "bd" || report.Executable == "dolt",
)
}
result.ProcessLeftAlone = report.RecordFound &&
!report.ProcessWasGone &&
!report.SignalSent
result.RecordLeftAlone = report.RecordFound && report.QuarantinedPath == ""
if report.Backend != nil {
result.Backend = &doltStopRecordResult{
RecordFound: report.Backend.RecordFound,
RecordPath: report.Backend.RecordPath,
LockWasHeld: report.Backend.LockWasHeld,
PID: report.Backend.PID,
Executable: report.Backend.Executable,
ProcessWasGone: report.Backend.ProcessWasGone,
SignalSent: report.Backend.SignalSent,
QuarantinedPath: report.Backend.QuarantinedPath,
}
}
if forceErr != nil {
result.Error = forceErr.Error()
}
return result
}
func boolPointer(value bool) *bool {
return &value
}
func renderDoltStopResult(result doltStopResult) error {
if jsonOutput {
if err := outputJSON(result); err != nil {
return HandleError("encode dolt stop result: %v", err)
}
if result.Error != "" {
return SilentExit()
}
return nil
}
if !result.ForcedRecovery {
fmt.Println("Dolt server stopped.")
return nil
}
fmt.Printf("Verified shutdown refused: %s\n", result.VerifiedShutdownError)
if result.Error == "" {
fmt.Println("Dolt server stopped with --force.")
} else if result.ProcessLeftAlone {
fmt.Println("Force stop refused; the recorded process was left alone.")
} else {
fmt.Println("Force stop incomplete; completed actions are reported below.")
}
if result.RecordFound {
fmt.Printf(" Record: %s\n", result.RecordPath)
}
if result.PID != 0 {
fmt.Printf(" PID: %d\n", result.PID)
}
if result.Executable != "" {
if result.ExecutableVerified != nil && *result.ExecutableVerified {
fmt.Printf(" Executable: %s (matched bd/dolt)\n", result.Executable)
} else {
fmt.Printf(" Executable: %s (not bd/dolt)\n", result.Executable)
}
}
switch {
case result.SignalSent:
fmt.Println(" Process: signal sent")
case result.ProcessWasGone:
fmt.Println(" Process: already gone; no signal sent")
case result.ProcessLeftAlone:
fmt.Println(" Process: left alone; no signal sent")
}
switch {
case result.QuarantinedPath != "":
fmt.Printf(" Record quarantined: %s\n", result.QuarantinedPath)
case result.RecordLeftAlone:
fmt.Println(" Record: left unchanged")
}
if backend := result.Backend; backend != nil {
fmt.Printf(" Backend record: %s\n", backend.RecordPath)
if backend.PID != 0 {
fmt.Printf(" Backend PID: %d\n", backend.PID)
}
switch {
case backend.SignalSent:
fmt.Println(" Backend process: signal sent")
case backend.ProcessWasGone:
fmt.Println(" Backend process: already gone; no signal sent")
default:
fmt.Println(" Backend process: left alone; no signal sent")
}
if backend.QuarantinedPath != "" {
fmt.Printf(" Backend record quarantined: %s\n", backend.QuarantinedPath)
} else {
fmt.Println(" Backend record: left unchanged")
}
}
if result.Error != "" {
return HandleError("%s", result.Error)
}
return nil
}
var doltStatusCmd = &cobra.Command{
Use: "status",
SilenceUsage: true,
SilenceErrors: true,
Short: "Show Dolt engine status",
Long: `Show the status of the Dolt engine for the current project.
In embedded mode, reports that the Dolt engine runs in-process and shows
the on-disk data directory. For beads-managed (local) servers, displays
PID, port, and data directory from the local PID file. For externally-
managed servers — a shared server (dolt.shared-server: true), a remote
dolt_server_host, or a local server managed outside bd (dolt.auto-start:
false, e.g. an orchestrator-shared sql-server) — pings the configured
endpoint via SQL and reports reachability, server version, and database.
In proxied-server mode, reports the proxy (the endpoint every bd command
connects through) and the dolt backend behind it separately, read from the
proxy's own process records. Reporting starts nothing: a quiesced workspace
is shown as not running, and the next bd command launches it.`,
RunE: func(cmd *cobra.Command, args []string) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
cfg, cfgErr := configfile.Load(beadsDir)
if cfgErr != nil {
return HandleError("loading config: %v", cfgErr)
}
if cfg == nil {
cfg = configfile.DefaultConfig()
}
if err := validateConfiguredBackend(cfg, beadsDir); err != nil {
return HandleError("%v", err)
}
// A non-Dolt backend (SQLite or a removed-backend tombstone) has no Dolt engine;
// report the backend rather than misdescribing an embedded Dolt server
// (parity with `bd dolt show`, which already special-cases this).
if cfg.GetBackend() != configfile.BackendDolt {
fmt.Printf("Backend: %s (no Dolt engine)\n", cfg.GetBackend())
return nil
}
// Neither branch below describes a proxied workspace. It writes no
// classic PID file, so the bd-managed path reports "not running"
// while the proxy is serving CRUD, and shouldUseExternalDoltStatus
// deliberately excludes proxied so the SQL-probe path never sees it
// either. Keyed on the loaded config rather than the process-wide
// mode so `bd --db <proxied workspace> dolt status` describes the
// workspace it was pointed at.
if cfg.IsDoltProxiedServerMode() {
status, err := collectProxiedDoltStatus(beadsDir)
if err != nil {
return HandleError("%v", err)
}
renderProxiedDoltStatus(status)
return nil
}
if !usesSQLServer() {
showEmbeddedDoltStatus(beadsDir)
return nil
}
// For externally-managed Dolt servers, the local PID file is
// meaningless or absent — ping the configured endpoint via SQL
// instead. Two flavors qualify:
// - non-local host (Hosted Dolt, remote shared sql-server, bd-q35w)
// - local host with auto-start disabled (an orchestrator or
// systemd manages the server lifecycle, be-0eyj)
//
// IsAutoStartDisabled reads the active (globally-bound) config and
// BEADS_DOLT_AUTO_START env, not the per-beadsDir cfg loaded above.
// That coupling is intentional and consistent with every
// other call site of IsAutoStartDisabled in this package — both
// resolve against the same active workspace at command time.
if cfg != nil && shouldUseExternalDoltStatus(cfg, doltserver.IsAutoStartDisabled(), doltserver.IsSharedServerMode()) {
runExternalDoltStatus(beadsDir, cfg)
return nil
}
serverDir := doltserver.ResolveServerDir(beadsDir)
state, err := doltserver.IsRunning(serverDir)
if err != nil {
return HandleError("%v", err)
}
renderLocalDoltStatus(state, serverDir)
return nil
},
}
// renderLocalDoltStatus writes the bd-managed (local PID-file) status of
// the Dolt server to stdout, honoring jsonOutput. Extracted from the
// doltStatusCmd Run closure so the bd-managed output path is unit-testable
// without requiring a live dolt sql-server (the externally-managed path
// is exercised by TestRunExternalDoltStatus_Unreachable).
func renderLocalDoltStatus(state *doltserver.State, serverDir string) {
if jsonOutput {
if err := outputJSON(state); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return
}
if state == nil || !state.Running {
cfg := doltserver.DefaultConfig(serverDir)
fmt.Println("Dolt server: not running")
fmt.Printf(" Expected port: %d\n", cfg.Port)
return
}
fmt.Println("Dolt server: running")
fmt.Printf(" PID: %d\n", state.PID)
fmt.Printf(" Port: %d\n", state.Port)
fmt.Printf(" Data: %s\n", state.DataDir)
fmt.Printf(" Logs: %s\n", doltserver.LogPath(serverDir))
if doltserver.IsSharedServerMode() {
fmt.Println(" Mode: shared server")
}
if doltserver.IsDebugMode() {
fmt.Println(" Debug: on (loglevel=debug, --prof cpu)")
fmt.Printf(" Profile dir: %s\n", doltserver.DebugProfileDir(serverDir))
}
if isDoltLocalOnly() {
fmt.Println(" Remote sync: disabled (dolt.local-only=true)")
}
}
// shouldUseExternalDoltStatus reports whether bd dolt status should treat
// the server as externally-managed and probe via SQL instead of consulting
// the local PID file. Returns true when:
// - shared-server mode is enabled (and not proxied) — a shared server's
// lifecycle is owned by something other than bd (a Homebrew service,
// systemd/launchd unit, or a sibling clone), so bd has no PID file for
// it even when the host is local and auto-start is enabled. Without this
// branch, status reports "not running" while bd CRUD commands, bd dolt
// test, and bd dolt show all connect to the server fine (GH#3218). This
// is checked BEFORE the server-mode guard because shared-server mode
// wins over a stale metadata.json that still pins dolt_mode="embedded"
// — mirroring the loadServerMode override in main.go (GH#2946). That
// stale-metadata case (dolt.shared-server: true in config.yaml, which
// IsDoltServerMode does not consult) is exactly where the residual
// GH#3218 bug lived.
// - dolt_mode=server with a non-local host (Hosted Dolt, remote shared
// sql-server) — the PID file is on a different machine.
// - dolt_mode=server with a local host but bd auto-start is disabled —
// the server lifecycle is owned by something outside bd (e.g. an
// orchestrator or systemd unit), so no bd PID file exists. Without
// this branch, status reports "not running" even when bd CRUD
// commands successfully connect to the server (be-0eyj).
//
// When false, the caller falls back to the PID-file path that reports
// PID, port, log path, and data directory for bd-managed servers.
//
// autoStartDisabled and sharedServerMode are passed in (rather than read
// here) so the predicate is pure and unit-testable without manipulating
// package-level config or process env.
func shouldUseExternalDoltStatus(cfg *configfile.Config, autoStartDisabled, sharedServerMode bool) bool {
if cfg == nil {
return false
}
// Shared-server mode wins even over an explicit metadata.json
// dolt_mode="embedded" (loadServerMode override, main.go, GH#2946), so
// this must precede the IsDoltServerMode guard — otherwise a workspace
// with dolt.shared-server: true in config.yaml and stale embedded
// metadata still falls through to the PID-file "not running" path.
// Proxied-server mode is excluded, matching that override's !psm guard.
if sharedServerMode && !cfg.IsDoltProxiedServerMode() {
return true
}
if !cfg.IsDoltServerMode() {
return false
}
if !isLocalHost(cfg.GetDoltServerHost()) {
return true
}
return autoStartDisabled
}
// isLocalHost reports whether host refers to this machine. Used to
// distinguish beads-managed local servers from externally-hosted ones.
func isLocalHost(host string) bool {
h := strings.ToLower(strings.TrimSpace(host))
if h == "" {
return true // empty defaults to local
}
switch h {
case "localhost", "127.0.0.1", "::1", "0.0.0.0":
return true
}
return false
}
// runExternalDoltStatus queries an externally-hosted Dolt server and prints
// (or returns, for --json) status. Unlike the local path, there is no PID or
// log file — reachability, version, host/port/database, and TLS mode are the
// user-relevant signals.
func runExternalDoltStatus(beadsDir string, cfg *configfile.Config) {
host := cfg.GetDoltServerHost()
port := doltserver.DefaultConfig(beadsDir).Port
user := cfg.GetDoltServerUser()
database := cfg.GetDoltDatabase()
tls := cfg.GetDoltServerTLS()
password := cfg.GetDoltServerPasswordForPort(port)
dsn := doltutil.ServerDSN{
Host: host,
Port: port,
User: user,
Password: password,
TLS: tls,
Timeout: 5 * time.Second,
}.String()
result := map[string]interface{}{
"mode": "external",
"host": host,
"port": port,
"user": user,
"database": database,
"tls": tls,
}
db, openErr := sql.Open("mysql", dsn)
var running bool
var version string
var connErr error
if openErr == nil {
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if pingErr := db.PingContext(ctx); pingErr != nil {
connErr = pingErr
} else {
running = true
// Best-effort version lookup; don't treat errors as fatal.
_ = db.QueryRowContext(ctx, "SELECT @@version").Scan(&version)
}
} else {
connErr = openErr
}
result["running"] = running
if version != "" {
result["version"] = version
}
if connErr != nil {
result["error"] = connErr.Error()
}
if jsonOutput {
if err := outputJSON(result); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return
}
if running {
fmt.Println("Dolt server: running (external)")
} else {
fmt.Println("Dolt server: not reachable (external)")
}
fmt.Printf(" Host: %s\n", host)
fmt.Printf(" Port: %d\n", port)
fmt.Printf(" Database: %s\n", database)
fmt.Printf(" User: %s\n", user)
fmt.Printf(" TLS: %t\n", tls)
if version != "" {
fmt.Printf(" Version: %s\n", version)
}
if connErr != nil {
fmt.Printf(" Error: %v\n", connErr)
}
}
// showEmbeddedDoltStatus reports Dolt engine status when running in
// embedded mode. There is no separate server process; the engine runs
// in-process and data lives at .beads/embeddeddolt/.
func showEmbeddedDoltStatus(beadsDir string) {
dataDir := filepath.Join(beadsDir, "embeddeddolt")
dataDirExists := false
if info, err := os.Stat(dataDir); err == nil && info.IsDir() {
dataDirExists = true
}
if jsonOutput {
if err := outputJSON(map[string]interface{}{
"mode": "embedded",
// Embedded mode has an active in-process engine, but no
// separate server process. Use a server-specific field so
// clients do not read running=false as "Dolt is unavailable".
"server_running": false,
"data_dir": dataDir,
"data_dir_exists": dataDirExists,
}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return
}
fmt.Println("Dolt engine: embedded (in-process, no server)")
fmt.Printf(" Data: %s\n", dataDir)
if !dataDirExists {
fmt.Printf(" %s\n", ui.RenderWarn("Data directory does not exist — run 'bd init' to create it"))
}
if isDoltLocalOnly() {
fmt.Println(" Remote sync: disabled (dolt.local-only=true)")
}
}
var doltKillallCmd = &cobra.Command{
Use: "killall",
Short: "Kill all orphan Dolt server processes",
Long: `Find and kill orphan dolt sql-server processes not tracked by the
canonical PID file for the current repo's Dolt data directory.
Under an orchestrator, the canonical server lives at $GT_ROOT/.beads/. Any other
dolt sql-server processes using that shared data directory are considered
orphans and will be killed.
In standalone mode, only dolt sql-server processes using the current
project's Dolt data directory are eligible for cleanup. Other projects'
servers are preserved.`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir != "" {
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
return HandleError("%v", err)
}
}
if !usesSQLServer() {
return HandleError("'bd dolt killall' is not supported in embedded mode (no Dolt server)")
}
if beadsDir == "" {
beadsDir = "." // best effort
}
killed, err := doltserver.KillStaleServers(beadsDir)
if err != nil {
return HandleError("%v", err)
}
if len(killed) == 0 {
fmt.Println("No orphan dolt servers found.")
} else {
fmt.Printf("Killed %d orphan dolt server(s): %v\n", len(killed), killed)
}
return nil
},
}
// staleDatabasePrefixes lists database name prefixes that
// `bd dolt clean-databases` will drop. This is the cleanup side of the
// test/prod split. Two sibling lists must converge with it (be-avn):
// - internal/storage/dolt/store.go:testDatabasePrefixes (firewall side)
// - .gc/system/packs/dolt/formulas/mol-dog-stale-db.toml (city formula)
//
// The firewall list, this cleanup list, and the formula list MUST
// converge — operators rely on consistent semantics across `bd dolt
// clean-databases`, the SQL-side firewall, and `gc dolt cleanup`.
//
// Origin of each prefix:
// - testdb_ : applyConfigDefaults derives this for BEADS_TEST_MODE=1
// without an explicit Database (FNV hash of cfg.Path).
// - beads_test : convention for hand-written integration tests.
// - beads_pt : property-test fixtures.
// - beads_vr : version-roundtrip / migration fixtures.
// - doctest_ : `bd doctor` self-check fixtures.
// - doctortest_ : older `bd doctor` fixture name (kept for back-compat).
// - benchdb_ : per-bench scratch DBs (cmd/bd/template_test.go
// newTemplateBenchmarkStore, format `benchdb_<unixnano>`).
var staleDatabasePrefixes = []string{
"testdb_",
"beads_test",
"beads_pt",
"beads_vr",
"doctest_",
"doctortest_",
"benchdb_",
}
var doltCleanDatabasesCmd = &cobra.Command{
Use: "clean-databases",
SilenceUsage: true,
SilenceErrors: true,
Short: "Drop stale test databases from the Dolt server",
Long: `Identify and drop leftover test and agent databases that accumulate
on the shared Dolt server from interrupted test runs and terminated agents.
Stale database prefixes: testdb_*, beads_test*, beads_pt*, beads_vr*, doctest_*, doctortest_*, benchdb_*
These waste server memory and can degrade performance under concurrent load.
Use --dry-run to see what would be dropped without actually dropping.
DROP DATABASE only marks a database as dropped; Dolt keeps its directory
under .dolt_dropped_databases/ so it can be restored with
CALL DOLT_UNDROP(name) until an explicit purge — disk is not reclaimed
until then. Pass --purge-dropped to run CALL DOLT_PURGE_DROPPED_DATABASES()
after cleanup.
--purge-dropped is SERVER-GLOBAL and IRREVERSIBLE. Dolt has no way to scope
the purge to only the databases this run dropped: it permanently deletes
every dropped-but-not-yet-purged database on the server, including ones
dropped by something else entirely (e.g. an operator's accidental
DROP DATABASE on an unrelated database that was still recoverable via
DOLT_UNDROP). It also purges pre-existing residue from earlier
clean-databases runs even if this run finds no stale databases to drop.
Only pass it when nothing else on the server may be relying on DOLT_UNDROP
recovery.`,
RunE: func(cmd *cobra.Command, args []string) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
return HandleError("%v", err)
}
if !usesSQLServer() {
return HandleError("'bd dolt clean-databases' is not supported in embedded mode (no Dolt server)")
}
dryRun, _ := cmd.Flags().GetBool("dry-run")
purgeDropped, _ := cmd.Flags().GetBool("purge-dropped")
opts := cleanDatabasesOptions{dryRun: dryRun, purgeDropped: purgeDropped}
if usesProxiedServer() {
return runDoltCleanDatabasesProxied(rootCtx, beadsDir, opts)
}
// Connect directly to the Dolt server via config instead of getStore(),
// which isn't initialized for dolt subcommands (beads-9vt).
db, cleanup, err := openDoltServerConnection()
if err != nil {
return err
}
defer cleanup()
return cleanDatabases(rootCtx, db, opts)
},
}
// shouldPurgeDroppedDatabases reports whether clean-databases should invoke
// the (server-global, irreversible) purge. It gates purely on the
// --purge-dropped flag and deliberately ignores droppedCount: a prior
// clean-databases run may have left dropped-but-unpurged residue that this
// run's SHOW DATABASES scan never sees (the residue is already gone from
// SHOW DATABASES the moment it was dropped), so --purge-dropped must still
// fire the purge even when this run drops nothing. Extracted as a pure
// function so the gating contract itself — not just the SQL-level purge
// mechanism — has direct unit coverage.
func shouldPurgeDroppedDatabases(purgeDropped bool, droppedCount int) bool {
_ = droppedCount // deliberately unused — see doc comment above
return purgeDropped
}
// purgeDroppedDatabases issues Dolt's DOLT_PURGE_DROPPED_DATABASES() stored
// procedure, which permanently deletes database directories that DROP
// DATABASE only moved into .dolt_dropped_databases/. This is server-global:
// Dolt has no way to scope it to a particular set of databases, so it
// purges every dropped-but-not-yet-purged database on the server, not just
// ones this process dropped. Extracted so tests can drive it directly
// against a live test server without going through the full
// clean-databases command wiring (config loading, SHOW DATABASES scan,
// batching/backoff).
func purgeDroppedDatabases(ctx context.Context, conn versioncontrolops.DBConn) error {
purgeCtx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
_, err := conn.ExecContext(purgeCtx, "CALL DOLT_PURGE_DROPPED_DATABASES()")
return err
}
// --- Dolt remote management commands ---
type doltRemoteAddStore interface {
ListRemotes(ctx context.Context) ([]storage.RemoteInfo, error)
AddRemote(ctx context.Context, name, url string) error
RemoveRemote(ctx context.Context, name string) error
}
type doltRemoteAddResult struct {
Canceled bool
}
type doltRemoteOverwriteConfirmer func(surface, name, existingURL, newURL string) bool
func confirmDoltRemoteOverwrite(surface, name, existingURL, newURL string) bool {
if !term.IsTerminal(int(os.Stdin.Fd())) {
return true
}
fmt.Printf(" Remote %q already exists on %s: %s\n", name, surface, existingURL)
fmt.Printf(" Overwrite with: %s\n", newURL)
fmt.Print(" Overwrite? (y/N): ")
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
return false
}
response = strings.TrimSpace(strings.ToLower(response))
return response == "y" || response == "yes"
}
func findDoltRemoteURL(remotes []storage.RemoteInfo, name string) string {
for _, remote := range remotes {
if remote.Name == name {
return remote.URL
}
}
return ""
}
func ensureDoltRemote(ctx context.Context, st doltRemoteAddStore, name, url string, confirm doltRemoteOverwriteConfirmer) (doltRemoteAddResult, error) {
remotes, err := st.ListRemotes(ctx)
if err != nil {
return doltRemoteAddResult{}, fmt.Errorf("list existing remotes: %w", err)
}
existingURL := findDoltRemoteURL(remotes, name)
existingFromDiskOnly := false
if existingURL == "" {
// An empty listing is not proof the remote is absent: a freshly
// (auto-)started sql-server can report empty dolt_remotes while the
// remote is persisted on disk (GH#2118, wy-6k7f7). Recover the
// persisted URL so the add gets the same match/confirm treatment it
// would after the window, instead of silently writing over an
// invisible remote.
existingURL = findDoltRemoteURL(persistedRemoteInfosFor(st), name)
existingFromDiskOnly = existingURL != ""
}
if existingURL == "" {
if err := st.AddRemote(ctx, name, url); err != nil {
return doltRemoteAddResult{}, fmt.Errorf("add remote %s: %w", name, err)
}
return doltRemoteAddResult{}, nil
}
if doltutil.RemoteURLsMatch(existingURL, url) {
return doltRemoteAddResult{}, nil
}
if !confirm("SQL server", name, existingURL, url) {
return doltRemoteAddResult{Canceled: true}, nil
}
if err := st.RemoveRemote(ctx, name); err != nil {
// A remote known only from disk may not be removable through a
// cold-started server that doesn't see it yet; the confirmed add
// below is what establishes the new URL either way.
if !existingFromDiskOnly {
return doltRemoteAddResult{}, fmt.Errorf("remove existing remote %s: %w", name, err)
}
}
if err := st.AddRemote(ctx, name, url); err != nil {
return doltRemoteAddResult{}, fmt.Errorf("add remote %s: %w", name, err)
}
return doltRemoteAddResult{}, nil
}
var doltRemoteCmd = &cobra.Command{
Use: "remote",
Short: "Manage Dolt remotes",
Long: `Manage Dolt remotes for push/pull replication.
Subcommands:
add <name> <url> Add a new remote
list List all configured remotes
remove <name> Remove a remote
reset-data <name> Replace a remote's data plane after a history squash`,
}
var doltRemoteAddCmd = &cobra.Command{
Use: "add <name> <url>",
SilenceUsage: true,
SilenceErrors: true,
Short: "Add a Dolt remote",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if isDoltLocalOnly() {
fmt.Fprintln(os.Stderr, "Error: cannot add Dolt remote: remote sync is disabled (dolt.local-only=true).")
fmt.Fprintln(os.Stderr, "To re-enable remote sync: bd config unset dolt.local-only")
return SilentExit()
}
allowGitOrigin, _ := cmd.Flags().GetBool("allow-git-origin")
if doltRemoteMatchesGitOrigin(args[1]) {
if !allowGitOrigin {
fmt.Fprintf(os.Stderr, "Error: refusing to add %q as a Dolt remote — this URL matches the git origin.\n", args[1])
fmt.Fprintln(os.Stderr, " Hint: use --allow-git-origin to proceed anyway (e.g. monorepo layout).")
fmt.Fprintln(os.Stderr, " Hint: or set dolt.local-only=true to disable remote sync entirely.")
return SilentExit()
}
fmt.Fprintf(os.Stderr, "Warning: %q matches the git origin — proceeding because --allow-git-origin is set.\n", args[1])
}
ctx := context.Background()
st := getStore()
if st == nil {
return HandleError("no store available")
}
name, url := args[0], args[1]
result, err := ensureDoltRemote(ctx, st, name, url, confirmDoltRemoteOverwrite)
if err != nil {
if jsonOutput {
_ = outputJSONError(err, "remote_add_failed")
} else {
fmt.Fprintf(os.Stderr, "Error adding remote: %v\n", err)
}
return SilentExit()
}
if result.Canceled {
fmt.Println("Canceled.")
return nil
}
if name == "origin" {
if err := config.SetYamlConfig("sync.remote", url); err != nil {
return HandleError("failed to persist sync.remote to config.yaml: %v", err)
}
if isGitRepo() {
commitBeadsConfig("bd: update sync.remote")
}
}
if jsonOutput {
if err := outputJSON(map[string]interface{}{
"name": name,
"url": url,
}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
} else {
fmt.Printf("Added remote %q → %s\n", name, url)
}
return nil
},
}
var doltRemoteListCmd = &cobra.Command{
Use: "list",
SilenceUsage: true,
SilenceErrors: true,
Short: "List configured Dolt remotes",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
st := getStore()
if st == nil {
return HandleError("no store available")
}
remotes, err := st.ListRemotes(ctx)
if err != nil {
if jsonOutput {
_ = outputJSONError(err, "remote_list_failed")
} else {
fmt.Fprintf(os.Stderr, "Error listing remotes: %v\n", err)
}
return SilentExit()
}
if jsonOutput {
if err := outputJSON(formatDoltRemoteListJSON(remotes)); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return nil
}
if len(remotes) == 0 {
fmt.Println("No remotes configured.")
return nil
}
for _, r := range remotes {
fmt.Printf("%-20s %s\n", r.Name, r.URL)
}
return nil
},
}
type doltRemoteListJSON struct {
Name string `json:"name"`
URL string `json:"url"`
SQLURL string `json:"sql_url,omitempty"`
CLIURL string `json:"cli_url,omitempty"`
Status string `json:"status"`
}
func formatDoltRemoteListJSON(remotes []storage.RemoteInfo) []doltRemoteListJSON {
out := make([]doltRemoteListJSON, 0, len(remotes))
for _, r := range remotes {
out = append(out, doltRemoteListJSON{
Name: r.Name,
URL: r.URL,
SQLURL: r.URL,
Status: "ok",
})
}
return out
}
var doltRemoteRemoveCmd = &cobra.Command{
Use: "remove <name>",
Short: "Remove a Dolt remote",
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
name := args[0]
if usesProxiedServer() {
return runDoltRemoteRemoveProxied(ctx, name)
}
st := getStore()
if st == nil {
return HandleError("no store available")
}
if err := st.RemoveRemote(ctx, name); err != nil {
if jsonOutput {
_ = outputJSONError(err, "remote_remove_failed")
} else {
fmt.Fprintf(os.Stderr, "Error removing remote: %v\n", err)
}
return SilentExit()
}
if name == "origin" {
if current := config.GetYamlConfig("sync.remote"); current != "" {
if err := config.UnsetYamlConfig("sync.remote"); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to clear sync.remote from config.yaml: %v\n", err)
}
if isGitRepo() {
commitBeadsConfig("bd: clear sync.remote")
}
}
}
if jsonOutput {
if err := outputJSON(map[string]interface{}{
"name": name,
"removed": true,
}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
} else {
fmt.Printf("Removed remote %q\n", name)
}
return nil
},
}
// isTimeoutError checks if an error is a context deadline exceeded or timeout.
func isTimeoutError(err error) bool {
if err == nil {
return false
}
if err == context.DeadlineExceeded {
return true
}
// Check for net.Error timeout (covers TCP and MySQL driver timeouts)
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
return true
}
// Also catch wrapped context.DeadlineExceeded
return errors.Is(err, context.DeadlineExceeded)
}
func init() {
doltSetCmd.Flags().Bool("update-config", false, "Also write to config.yaml for team-wide defaults")
doltStopCmd.Flags().Bool("force", false, "Force stop (proxied recovery still requires a bd/dolt executable match)")
doltPushCmd.Flags().Bool("force", false, "Force push (overwrite remote changes)")
doltPushCmd.Flags().String("remote", "", "Push to a specific named remote instead of the default")
doltPushCmd.Flags().BoolP("yes", "y", false, "Consent to adopting a Dolt remote derived from git origin when none is configured")
doltPushCmd.Flags().Bool("no-adopt", false, "Never derive a Dolt remote from git origin (also BD_NO_REMOTE_ADOPT=1)")
doltPullCmd.Flags().String("remote", "", "Pull from a specific named remote instead of the default")
doltPullCmd.Flags().String("strategy", "", "Conflict resolution strategy for conflicts the auto-resolver declines: 'ours' or 'theirs' (embedded storage only, #4992)")
doltCommitCmd.Flags().StringP("message", "m", "", "Commit message (default: auto-generated)")
doltCleanDatabasesCmd.Flags().Bool("dry-run", false, "Show what would be dropped without dropping")
doltCleanDatabasesCmd.Flags().Bool("purge-dropped", false, "After dropping, also run CALL DOLT_PURGE_DROPPED_DATABASES() — server-global and irreversible, see --help")
doltRemoteAddCmd.Flags().Bool("allow-git-origin", false, "Allow adding a Dolt remote whose URL matches the git origin (proceed with a warning instead of aborting)")
doltRemoteResetDataCmd.Flags().BoolVarP(&doltRemoteResetDataYes, "yes", "y", false, "Skip the confirmation prompt (required in non-interactive use)")
doltRemoteCmd.AddCommand(doltRemoteAddCmd)
doltRemoteCmd.AddCommand(doltRemoteListCmd)
doltRemoteCmd.AddCommand(doltRemoteRemoveCmd)
doltRemoteCmd.AddCommand(doltRemoteResetDataCmd)
doltCmd.AddCommand(doltShowCmd)
doltCmd.AddCommand(doltSetCmd)
doltCmd.AddCommand(doltTestCmd)
doltCmd.AddCommand(doltCommitCmd)
doltCmd.AddCommand(doltPushCmd)
doltCmd.AddCommand(doltPullCmd)
doltCmd.AddCommand(doltStartCmd)
doltCmd.AddCommand(doltStopCmd)
doltCmd.AddCommand(doltStatusCmd)
doltCmd.AddCommand(doltKillallCmd)
doltCmd.AddCommand(doltCleanDatabasesCmd)
doltCmd.AddCommand(doltRemoteCmd)
rootCmd.AddCommand(doltCmd)
}
func selectedDoltBeadsDir() string {
beadsDir := ""
if os.Getenv("BEADS_DIR") != "" {
beadsDir = beads.FindBeadsDir()
}
if beadsDir == "" {
beadsDir = selectedNoDBBeadsDir(nil)
}
if beadsDir == "" {
return ""
}
prepareSelectedNoDBContext(beadsDir)
return beadsDir
}
// resolveDoltShowRemotes returns remotes for `bd dolt show`.
// `show` is a no-store diagnostic command, so getStore() is usually nil and
// ListRemotes is unavailable. Fall back to on-disk repo_state.json (same
// source as the remote-migrate gate) so remotes match `bd dolt remote list`
// (GH#4619).
//
// The physical root is resolved through the same mode-aware path selection
// used by store opening; a repo in one mode must not surface stale remotes
// persisted under another mode's data directory. Within that root, the active
// database directory is checked before the root-level cold-start layout. The
// first repo_state.json found on disk is authoritative: an empty
// remotes list there means "no remotes", not "keep looking" — this stops
// an authoritative-but-empty active database from falling through to a
// stale candidate. A corrupt or unreadable repo_state.json is surfaced as a
// warning rather than silently rendered as "(none)".
//
// Two known limits of that delegation, both narrower than the stale-mode leak
// it removes:
//
// - show/open parity does NOT hold for a workspace with no metadata.json.
// ResolvePhysicalRoots' discovery fallback picks the mode from what is on
// disk (embeddeddolt/, else dolt/), while the CLI open path additionally
// promotes a nil config to server mode via the
// `cfg == nil && configfile.DefaultConfig().IsDoltServerMode()` rescue in
// main.go (BEADS_DOLT_SERVER_MODE=1 / config.yaml dolt.mode). So a
// workspace with no metadata.json, that env set, and a leftover
// embeddeddolt/ tree reports the embedded root's remotes under a
// server-mode header. Mirroring the rescue inside the resolver is the real
// fix but changes every ResolvePhysicalRoots caller, so it is a follow-up
// rather than part of this command's fix.
// - a remote-host server workspace has no local root to read, so this
// returns nil and `bd dolt show` renders "(none)". Read that as "not
// locally knowable" rather than "no remotes configured"; `bd dolt remote
// list` against the server is the authoritative answer.
func resolveDoltShowRemotes(beadsDir string, cfg *configfile.Config) []storage.RemoteInfo {
ctx := context.Background()
if st := getStore(); st != nil {
if remotes, err := st.ListRemotes(ctx); err == nil && len(remotes) > 0 {
return remotes
}
}
dbName := ""
if cfg != nil {
dbName = cfg.GetDoltDatabase()
}
physical, err := doltserver.ResolvePhysicalRoots(beadsDir)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", ui.RenderWarn(fmt.Sprintf("could not resolve Dolt data directory: %v", err)))
return nil
}
if physical.RemoteBackend || len(physical.Roots) == 0 {
return nil
}
// Every ResolvePhysicalRoots branch appends exactly one root today; Roots
// is a slice because other callers (beads.OpenGated) union several.
// Revisit this if the resolver ever does that here, or the extra
// candidates would vanish without a trace.
root := physical.Roots[0]
var candidates []string
if dbName != "" {
candidates = append(candidates, filepath.Join(root, dbName))
}
candidates = append(candidates, root)
for _, dir := range candidates {
if dir == "" {
continue
}
statePath := filepath.Join(dir, ".dolt", "repo_state.json")
if _, err := os.Stat(statePath); err != nil {
// No dolt repo state at this candidate; try the next
// mode-appropriate candidate.
continue
}
remotes, err := doltutil.PersistedRemotes(dir)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", ui.RenderWarn(fmt.Sprintf("could not read remotes from %s: %v", statePath, err)))
return nil
}
// repo_state.json exists at this candidate: its remotes (even if
// empty) are authoritative for the active mode.
return remotes
}
return nil
}
func showDoltConfig(testConnection bool) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
cfg, err := configfile.Load(beadsDir)
if err != nil {
return HandleError("loading config: %v", err)
}
if cfg == nil {
cfg = configfile.DefaultConfig()
}
if err := validateConfiguredBackend(cfg, beadsDir); err != nil {
return HandleError("%v", err)
}
backend := cfg.GetBackend()
embedded := !usesSQLServer()
// Resolve actual server port for connection testing
showHost := cfg.GetDoltServerHost()
dsCfg := doltserver.DefaultConfig(beadsDir)
showPort := dsCfg.Port
embeddedDataDir := filepath.Join(beadsDir, "embeddeddolt")
if jsonOutput {
result := map[string]interface{}{
"backend": backend,
}
if backend == configfile.BackendDolt {
result["database"] = cfg.GetDoltDatabase()
result["embedded"] = embedded
if embedded {
result["data_dir"] = embeddedDataDir
} else {
result["host"] = showHost
result["port"] = showPort
result["user"] = cfg.GetDoltServerUser()
result["tls"] = cfg.GetDoltServerTLS()
result["shared_server"] = doltserver.IsSharedServerMode()
if testConnection {
result["connection_ok"] = testServerConnection(showHost, showPort)
}
}
}
if err := outputJSON(result); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return nil
}
if backend != configfile.BackendDolt {
fmt.Printf("Backend: %s\n", backend)
return nil
}
fmt.Println("Dolt Configuration")
fmt.Println("==================")
fmt.Printf(" Database: %s\n", cfg.GetDoltDatabase())
if embedded {
fmt.Println(" Mode: embedded (in-process Dolt engine)")
fmt.Printf(" Data: %s\n", embeddedDataDir)
} else {
fmt.Printf(" Host: %s\n", showHost)
fmt.Printf(" Port: %d\n", showPort)
fmt.Printf(" User: %s\n", cfg.GetDoltServerUser())
fmt.Printf(" TLS: %t\n", cfg.GetDoltServerTLS())
if doltserver.IsSharedServerMode() {
fmt.Println(" Mode: shared server")
if sharedDir, err := doltserver.SharedServerDir(); err == nil {
fmt.Printf(" Server: %s\n", sharedDir)
}
} else {
fmt.Println(" Mode: per-project")
}
if testConnection {
fmt.Println()
if testServerConnection(showHost, showPort) {
fmt.Printf(" %s\n", ui.RenderPass("✓ Server connection OK"))
} else {
fmt.Printf(" %s\n", ui.RenderWarn("✗ Server not reachable"))
}
}
}
fmt.Println("\nRemotes:")
remotes := resolveDoltShowRemotes(beadsDir, cfg)
if len(remotes) == 0 {
fmt.Println(" (none)")
} else {
for _, r := range remotes {
fmt.Printf(" %-16s %s\n", r.Name, r.URL)
}
}
printDoltShowConfigSources(os.Stdout)
return nil
}
// printDoltShowConfigSources renders doltserver.PortSourceLabels(), the same
// slice DefaultConfig resolves against, so this list can't drift from actual
// resolution behavior (GH#4511).
func printDoltShowConfigSources(w io.Writer) {
fmt.Fprintln(w, "\nConfig sources for server port (priority order):")
for i, label := range doltserver.PortSourceLabels() {
fmt.Fprintf(w, " %d. %s\n", i+1, label)
}
}
func setDoltConfig(key, value string, updateConfig bool) error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
cfg, err := loadDoltBackendConfig(beadsDir)
if err != nil {
return HandleError("%v", err)
}
var yamlKey string
switch key {
case "mode":
// Mode will be configurable again when embedded Dolt support returns.
// For now, server mode is required (embedded driver not yet re-integrated).
return HandleError("mode is not yet configurable; embedded mode is coming soon")
case "database":
if value == "" {
return HandleError("database name cannot be empty")
}
cfg.DoltDatabase = value
yamlKey = "dolt.database"
case "host":
if value == "" {
return HandleError("host cannot be empty")
}
cfg.DoltServerHost = value
yamlKey = "dolt.host"
case "port":
port, err := strconv.Atoi(value)
if err != nil || port <= 0 || port > 65535 {
return HandleError("port must be a valid port number (1-65535)")
}
cfg.DoltServerPort = port
yamlKey = "dolt.port"
case "socket":
// Empty value clears the socket (reverts to TCP host/port).
cfg.DoltServerSocket = value
yamlKey = "dolt.socket"
case "user":
if value == "" {
return HandleError("user cannot be empty")
}
cfg.DoltServerUser = value
yamlKey = "dolt.user"
case "data-dir":
// GH#2438: In server mode, data-dir has no effect on which database
// the server connects to. Setting it silently switches the local
// resolution path without affecting the running server, causing
// commands to operate on the wrong (often empty) database.
if value != "" && cfg.IsDoltServerMode() {
fmt.Fprintf(os.Stderr, "Error: setting data-dir in server mode is not supported (GH#2438).\n")
fmt.Fprintf(os.Stderr, "In server mode, the database is determined by the 'database' config key,\n")
fmt.Fprintf(os.Stderr, "not the local data directory. Setting data-dir would silently disconnect\n")
fmt.Fprintf(os.Stderr, "from the configured database '%s'.\n", cfg.GetDoltDatabase())
fmt.Fprintf(os.Stderr, "\nTo change which database to use:\n")
fmt.Fprintf(os.Stderr, " bd dolt set database <name>\n")
return SilentExit()
}
if value == "" {
// Allow clearing the custom data dir (revert to default .beads/dolt)
cfg.DoltDataDir = ""
} else {
if !filepath.IsAbs(value) {
return HandleError("data-dir must be an absolute path")
}
cfg.DoltDataDir = value
// Absolute paths are machine-specific and won't be persisted to
// metadata.json (which is committed to git). Use the env var for
// persistence across sessions. (GH#2251)
fmt.Fprintf(os.Stderr, "Note: absolute paths are not saved to metadata.json (it propagates via git).\n")
fmt.Fprintf(os.Stderr, "For persistence, add to your shell profile:\n")
fmt.Fprintf(os.Stderr, " export BEADS_DOLT_DATA_DIR=%s\n", value)
}
yamlKey = "dolt.data-dir"
case "shared-server":
lower := strings.ToLower(value)
if lower != "true" && lower != "false" {
return HandleError("shared-server must be 'true' or 'false'")
}
// shared-server is yaml-only (not stored in metadata.json)
if err := config.SetYamlConfig("dolt.shared-server", lower); err != nil {
return HandleError("setting shared-server: %v", err)
}
if jsonOutput {
if err := outputJSON(map[string]interface{}{
"key": "shared-server",
"value": lower,
"location": "config.yaml",
}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return nil
}
if lower == "true" {
fmt.Println("Shared server mode enabled.")
fmt.Println("All projects will use a single Dolt server at ~/.beads/shared-server/.")
fmt.Println("Each project's data remains isolated in its own database.")
} else {
fmt.Println("Shared server mode disabled. Each project will use its own Dolt server.")
}
return nil
default:
fmt.Fprintf(os.Stderr, "Error: unknown key '%s'\n", key)
fmt.Fprintf(os.Stderr, "Valid keys: database, host, port, socket, user, data-dir, shared-server\n")
return SilentExit()
}
// Audit log: record who changed what
logDoltConfigChange(beadsDir, key, value)
// Save to metadata.json
if err := cfg.Save(beadsDir); err != nil {
return HandleError("saving config: %v", err)
}
if jsonOutput {
result := map[string]interface{}{
"key": key,
"value": value,
"location": "metadata.json",
}
if updateConfig {
result["config_yaml_updated"] = true
}
if err := outputJSON(result); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return nil
}
fmt.Printf("Set %s = %s (in metadata.json)\n", key, value)
// Also update config.yaml if requested
if updateConfig && yamlKey != "" {
if err := config.SetYamlConfig(yamlKey, value); err != nil {
fmt.Printf("%s\n", ui.RenderWarn(fmt.Sprintf("Warning: failed to update config.yaml: %v", err)))
} else {
fmt.Printf("Set %s = %s (in config.yaml)\n", yamlKey, value)
}
}
return nil
}
func testDoltConnection() error {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
cfg, err := loadDoltBackendConfig(beadsDir)
if err != nil {
return HandleError("%v", err)
}
host := cfg.GetDoltServerHost()
port := doltserver.DefaultConfig(beadsDir).Port
addr := fmt.Sprintf("%s:%d", host, port)
if jsonOutput {
ok := testServerConnection(host, port)
if err := outputJSON(map[string]interface{}{
"host": host,
"port": port,
"connection_ok": ok,
}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
if !ok {
return SilentExit()
}
return nil
}
fmt.Printf("Testing connection to %s...\n", addr)
if testServerConnection(host, port) {
fmt.Printf("%s\n", ui.RenderPass("✓ Connection successful"))
} else {
fmt.Printf("%s\n", ui.RenderWarn("✗ Connection failed"))
fmt.Println("\nStart the server with: bd dolt start")
return SilentExit()
}
// Test remote connectivity
st := getStore()
if st == nil {
return nil
}
ctx := context.Background()
remotes, err := st.ListRemotes(ctx)
if err != nil || len(remotes) == 0 {
return nil
}
fmt.Println("\nRemote connectivity:")
for _, r := range remotes {
if doltutil.IsSSHURL(r.URL) {
// Test SSH connectivity by parsing host from URL
sshHost := extractSSHHost(r.URL)
if sshHost != "" {
fmt.Printf(" %s (%s)... ", r.Name, r.URL)
if testSSHConnectivity(sshHost) {
fmt.Printf("%s\n", ui.RenderPass("✓ reachable"))
} else {
fmt.Printf("%s\n", ui.RenderWarn("✗ unreachable"))
}
}
} else if strings.HasPrefix(r.URL, "https://") || strings.HasPrefix(r.URL, "http://") {
fmt.Printf(" %s (%s)... ", r.Name, r.URL)
if testHTTPConnectivity(r.URL) {
fmt.Printf("%s\n", ui.RenderPass("✓ reachable"))
} else {
fmt.Printf("%s\n", ui.RenderWarn("✗ unreachable"))
}
} else {
fmt.Printf(" %s (%s)... skipped (no connectivity test for this scheme)\n", r.Name, r.URL)
}
}
return nil
}
// serverDialTimeout controls the TCP dial timeout for server connection tests.
// Tests may reduce this to avoid slow unreachable-host hangs in CI.
var serverDialTimeout = 3 * time.Second
func testServerConnection(host string, port int) bool {
addr := net.JoinHostPort(host, strconv.Itoa(port))
_, err := doltserver.ProbeSQLServer("tcp", addr, serverDialTimeout)
return err == nil
}
// extractSSHHost extracts the hostname from an SSH URL for connectivity testing.
func extractSSHHost(url string) string {
// git+ssh://git@github.com/org/repo.git → github.com
// ssh://git@github.com/org/repo.git → github.com
// git@github.com:org/repo.git → github.com
url = strings.TrimPrefix(url, "git+ssh://")
url = strings.TrimPrefix(url, "ssh://")
if idx := strings.Index(url, "@"); idx >= 0 {
url = url[idx+1:]
}
// Handle colon-separated (git@host:path) or slash-separated (ssh://host/path)
if idx := strings.Index(url, ":"); idx >= 0 && !strings.Contains(url[:idx], "/") {
return url[:idx]
}
if idx := strings.Index(url, "/"); idx >= 0 {
return url[:idx]
}
return url
}
// testSSHConnectivity tests if an SSH host is reachable on port 22.
// Bare dial+close (no doltserver.ProbeSQLServer): SSH, not MySQL — there is
// no handshake greeting to drain here.
func testSSHConnectivity(host string) bool {
// #nosec G704 -- host is extractSSHHost(r.URL) over the remotes this repo's
// own store returns, i.e. operator-configured local state, not request data.
// The probe dials and immediately closes: nothing is sent and no response is
// read, so there is no forgeable request. gosec only reaches this call after
// this PR routed config discovery through env-derived paths; its taint
// analysis is not field-sensitive, so one env-derived path marks the whole
// store value tainted and every URL read back off it inherits that.
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, "22"), 5*time.Second)
if err != nil {
return false
}
_ = conn.Close()
return true
}
// httpURLToTCPAddr extracts a TCP dial address (host:port) from an HTTP(S) URL.
// Handles IPv6 addresses correctly (e.g., https://[::1]:8080/path).
func httpURLToTCPAddr(url string) string {
host := url
host = strings.TrimPrefix(host, "https://")
host = strings.TrimPrefix(host, "http://")
if idx := strings.Index(host, "/"); idx >= 0 {
host = host[:idx]
}
defaultPort := "443"
if strings.HasPrefix(url, "http://") {
defaultPort = "80"
}
// Use net.SplitHostPort to correctly handle IPv6 addresses (which
// contain colons that would otherwise be confused with host:port).
if h, p, err := net.SplitHostPort(host); err == nil {
return net.JoinHostPort(h, p)
}
// No port in host string. Strip IPv6 brackets if present so
// JoinHostPort can re-add them correctly.
h := strings.TrimPrefix(host, "[")
h = strings.TrimSuffix(h, "]")
return net.JoinHostPort(h, defaultPort)
}
// testHTTPConnectivity tests if an HTTP(S) URL is reachable via TCP.
// Bare dial+close (no doltserver.ProbeSQLServer): HTTP(S), not MySQL — there
// is no handshake greeting to drain here.
func testHTTPConnectivity(url string) bool {
addr := httpURLToTCPAddr(url)
// #nosec G704 -- same probe shape and same taint path as testSSHConnectivity
// above: url is a remote URL from this repo's own store, and the dial is
// closed without sending or reading anything.
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
if err != nil {
return false
}
_ = conn.Close()
return true
}
// openDoltServerConnection opens a direct MySQL connection to the Dolt server
// using config from the beads directory. This bypasses getStore() which isn't
// initialized for dolt subcommands (beads-9vt). Connects without selecting a
// database so callers can operate on all databases (SHOW DATABASES, DROP DATABASE).
func openDoltServerConnection() (*sql.DB, func(), error) {
beadsDir := selectedDoltBeadsDir()
if beadsDir == "" {
return nil, nil, HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
}
cfg, err := loadDoltBackendConfig(beadsDir)
if err != nil {
return nil, nil, HandleError("%v", err)
}
host := cfg.GetDoltServerHost()
port := doltserver.DefaultConfig(beadsDir).Port
user := cfg.GetDoltServerUser()
password := os.Getenv("BEADS_DOLT_PASSWORD")
connStr := doltutil.ServerDSN{
Host: host,
Port: port,
User: user,
Password: password,
TLS: cfg.GetDoltServerTLS(),
}.String()
db, err := sql.Open("mysql", connStr)
if err != nil {
return nil, nil, HandleError("connecting to Dolt server: %v", err)
}
db.SetMaxOpenConns(2)
db.SetMaxIdleConns(1)
db.SetConnMaxLifetime(30 * time.Second)
// Verify connectivity
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := db.PingContext(ctx); err != nil {
_ = db.Close()
fmt.Fprintf(os.Stderr, "Error: cannot reach Dolt server at %s:%d: %v\n", host, port, err)
fmt.Fprintln(os.Stderr, "Start the server with: bd dolt start")
return nil, nil, SilentExit()
}
return db, func() { _ = db.Close() }, nil
}
// doltServerPidFile returns the path to the PID file for the managed dolt server.
// logDoltConfigChange appends an audit entry to .beads/dolt-config.log.
// Includes the beadsDir path for debugging worktree config pollution (bd-la2cl).
func logDoltConfigChange(beadsDir, key, value string) {
logPath := filepath.Join(beadsDir, "dolt-config.log")
actor := os.Getenv("BEADS_ACTOR")
if actor == "" {
actor = os.Getenv("BD_ACTOR") // deprecated fallback
}
if actor == "" {
actor = "unknown"
}
entry := fmt.Sprintf("%s actor=%s key=%s value=%s beads_dir=%s\n",
time.Now().UTC().Format(time.RFC3339), actor, key, value, beadsDir)
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return // best effort
}
defer f.Close()
_, _ = f.WriteString(entry)
}