beads/cmd/bd/dolt.go
Harry Miller 29aba4f8b2
fix(config): bd config unset reported success while the key stayed set (#5760)
* fix(config): unset a nested key in config.yaml instead of reporting a no-op

commentOutYamlKey matched only a flat "a.b:" line, so unsetting a key
written in nested form left the file untouched while bd printed
"Unset <key> (in config.yaml)" and the value stayed effective. The write
side does not have this limitation: SetYamlConfig routes dotted keys
through updateNestedYamlKey, and readUserGlobalYamlValue accepts either
shape, so bd could produce and read a nested key it could not remove. The
shipped config.yaml template documents the nested form in its commented
examples, which is the likeliest way a user acquires one.

Locate the leaf through the yaml node tree and comment out its source
line rather than re-marshalling, so the file's comments and layout
survive. A leaf whose value is not a scalar on the key's own line is
refused: commenting one line of a multi-line value would leave the file
unparseable. The flat form is still tried first and still wins when a
file somehow carries both.

* fix(config): do not comment out a yaml key whose value is the block below it

commentOutYamlKey matched any line opening with the key, so unsetting a
key that names a mapping commented out only that line and left the block
beneath it indented under nothing. The result does not parse, and the
next bd command in that workspace fails to read its config rather than
losing one setting.

Skip a match whose value is empty and whose following content line is
indented deeper: that line is the head of a block, not a setting. A key
with a genuinely empty value is still commented out, including at end of
file, where there is no following line to judge.

* fix(config): clear the config.yaml layer on unset of a database-backed key

bd config unset routes a key IsYamlOnlyKey does not claim to
UnsetSetting, which deletes the database row and nothing else. A key that
also appears in .beads/config.yaml therefore survived its own unset:
"Unset <key>" was printed, and bd config show still resolved the value
from config.yaml on the next read. types.custom and types.infra are the
keys this bites, since neither is yaml-only and both are read back out of
the file.

Clear the file layer too when the key is set there, and name the
locations that were actually cleared so the message can be trusted. The
JSON form gains the same location field the yaml-only branch already
emits. Removing the key from config.yaml comments the line out rather
than deleting it, so the file keeps documenting the setting.

Stacked on the nested-key fix: without it this path would report
config.yaml as cleared while a nested key stayed effective.

* style(config): fix the misspell lint on the new doc comment

golangci-lint's misspell linter fails the PR on "re-marshalling" in the
commentOutNestedYamlKey doc comment: the enforced spelling is
"marshaling". Comment-only change.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(config): report the unset location from the write, not a merged pre-check

Two blocking items from review.

1. The location guard read viper's resolved value, not the config.yaml
   layer. config.GetYamlConfig is v.GetString on the merged viper, which
   serves SetDefault values and AutomaticEnv, so every non-yaml-only key
   with a non-empty default (no-hooks, no-db, json,
   events-journal-retain-days, events-journal-auto-prune) looked present in
   config.yaml even in a workspace with no config.yaml at all. That meant
   bd printed "Unset <key> (in database, config.yaml)" over a file it never
   touched - a more specific false claim than the message this PR set out
   to remove - and, with no project config.yaml, failed the command with
   "no .beads/config.yaml found" AFTER the database row was already gone,
   where main printed "Unset <key>" and exited 0.

   UnsetYamlConfig (and UnsetUserYamlConfig) now return whether the file
   was actually changed, threading out the found/ok that
   commentOutFlatYamlKey and commentOutNestedYamlKey already computed. A
   missing project config.yaml, and a key not present in a shape that can
   be safely commented, are both (false, nil) - "not in config.yaml" is an
   answer, not a failure. The command clears the layer unconditionally and
   derives the location from the result, so the string is a report of the
   write rather than a guess made before it. That also settles the
   non-blocking third point: a refusal path can no longer print "Unset ..."
   over a no-op.

2. commentOutNestedYamlKey over-deleted a flow mapping. It accepted any
   leaf whose key and scalar value shared a line, but in flow style the
   leaf's line IS the parent's, so `unset dolt.mode` commented out
   `dolt: {mode: server}` whole and took the entire mapping with it. On
   main the flat regex never matched this, so the loss is new. The leaf now
   has to start at the line's first non-space column.

Also from review: commentOutFlatYamlKey splits instead of scanning, so a
flat unset keeps the file's trailing newline like the nested and no-match
paths always did; and the "flat form wins when both are present" case says
why the nested copy survives, and what `changed` does and does not claim
when it does.

Regression coverage: four commentOutYamlKey cases (two flow-mapping, two
trailing-newline), a changed/content agreement assertion across the whole
table, and two embedded CLI subtests - one proving the location no longer
names config.yaml for a defaulted key that was never written there, one
proving an unset with no project config.yaml exits 0 and does not recreate
the file. All four reproduce red against the pre-fix code.

Reported-by: bee-ghosttrack
Agent-Signature: claude-code-opus-5-unknown-reasoning on behalf of Harry Miller

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(config): address #5760 review — top-level flat match, lists at the key's indent

Blockers from the re-review at d168df1841:

- The flat-key match accepted any indentation, so on the database-backed
  path `bd config unset enabled` commented out a nested backup.enabled and
  reported "in database, config.yaml". A flat key is a top-level key whose
  name contains the dots, so it now matches only at the document's
  top-level indentation, read from the first key line (yaml.v3 accepts a
  document whose whole mapping is indented, which main's "preserve
  indentation" case covers). The same change stops `mode` under
  `dolt: mode: x: 1` from triggering a refusal that did not apply.
- A list at the key's own indentation (`types.custom:` then `- step`)
  slipped past the refusal and left config.yaml unparseable. A `-` item at
  the key's indentation now counts as the key's value.

Smaller:

- Only a comment, an anchor or a tag after the colon (`b:  # note`,
  `base: &b`) still leaves the value to the lines beneath, and is refused.
- Blank-line detection uses TrimSpace, so a CRLF blank line no longer ends
  the value early.
- The message no longer calls a plain scalar on the next line "a mapping or
  list".

Each has a case in TestUnsetRefusesShapesItCannotEdit or
TestCommentOutYamlKey, and undoing each fix fails its case.

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(config): refuse a single-segment block scalar; pin the tag and dedent branches (#5760 review)

- unsupportedUnsetShape returned early for single-segment keys, so
  `notes: |` with an indented body plus `unset notes` commented the key and
  orphaned the body, and the file stopped parsing. The database-backed
  unset now reaches this path for every key, so single-segment keys are
  checked too.
- New pins: a tag alone after the colon (`m: !!map`) leaves the value
  below, and a dedent ends the value (`outer:` / `  k:` / `z: 1`, unset
  outer.k, is commented). Undoing either branch fails its case.
- The database-path error no longer claims a row was unset.
  UnsetSettingResult cannot say whether one existed.

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(config): restore CRLF normalization lost by the split-based unset rewrite

Base commentOutYamlKey scanned with bufio.Scanner, whose ScanLines drops a
trailing \r, so CRLF files came back LF-only. The PR switched to
strings.Split to preserve the file final newline, which carried every \r
through. Normalizing at entry keeps both properties: base own
TestCommentOutYamlKeyPreservesTheTrailingNewline now passes 9/9.

* fix(config): keep the no-config.yaml unset failure reportable for yaml-only keys

Base extracted the yaml-only unset into runConfigUnsetYamlOnly, which runs the
leaked-secret row cleanup independently of the YAML edit and folds the row's fate
into the returned error. This PR widened UnsetYamlConfig to (bool, error) and
swallowed the missing-config.yaml case as (false, nil) so its own new
database-backed call site would stop failing after the row was already gone.

Merging both turned base's report into a success, so
TestConfigUnsetCleansTheRowWhenTheYamlUnsetFails lost the error it asserts and
the leaked-credential rotation notice went to stdout instead of the error.

The two call sites need opposite answers, so report the case as a sentinel,
ErrNoProjectConfigYaml, and let each state its own contract. Resolution stays in
findProjectConfigYaml so neither caller re-derives it - a caller-side pre-check
is what produced the false "(in config.yaml)" claims this PR removes.

* fix(config): compare the unset against the normalized content, gate the hint

Maintainer review fixup for PR #5760 (adopt-pr iterations 1 and 2).
Iteration 1 addresses the synthesis Pre-Merge Checklist in full; iteration 2
takes both of its non-gating follow-ups.

New Finding 1 (major) — commentOutYamlKey rewrites CRLF to LF as its first
statement, but both public callers compared its result against the raw
os.ReadFile bytes. On any CRLF config.yaml those always differ, so the `changed`
bool meant "the bytes moved", not "the key was there": unsetting an ABSENT key
returned (true, nil), printed "Unset <key> (in config.yaml)" — the exact false
claim this PR exists to remove — rewrote the whole file to LF, and made the new
"was not set" branch unreachable on a supported platform. Both callers now
normalize once up front and compare like for like, so a no-op leaves the file
byte-identical. A real unset still normalizes to LF, which is base behavior; it
is pinned at the callee and, since iteration 2, through both callers. Note this
was introduced by the adopt lane's own CRLF-restoration repair (59463663e), not
by the contributor.

New Finding 1 test cell (required) — every change-report fixture in the package
was LF, so nothing in-tree could catch this. Added a CRLF absent-key cell for
BOTH callers asserting changed=false and byte-identity. Proven non-vacuous:
reverting one caller to the raw-byte compare reds that caller's subtest on both
assertions and leaves its sibling green.

New Finding 2 (minor) — printConfigSideEffects sat outside the if/else, so the
no-op branch printed "Backup config removed..." right after saying the key was
never set. All four side-effect keys are yaml-only by prefix, so 4/4 route
through the new branch. Gated on `changed`, which covers the --json arm too
since the hint goes to stderr and was never suppressed there.

New Finding 3 (minor) — the yaml-only --json payload rendered location as ""
both for an absent key and for an unpopulated field, with no way to tell them
apart; added a `changed` bool and pinned one --json cell per route.

New Finding 4 (nit) — the ErrNoProjectConfigYaml doc comment was written as a
caller census and was short by two. Re-censused: four non-test callers, taking
three different answers. Reworded to say so.

CHANGELOG: the honesty claim is no longer LF-only; recorded the no-op
byte-identity, the gated hint, and the new --json field.

Iteration 2, minor (test evidence) — the absent-key cell pins only the no-op
direction of the new compare. A caller that returned early whenever the
up-front normalization changed anything passed the whole package, and it turns
every unset on a CRLF config.yaml into a silent "<key> was not set" while the
key stays effective. Added TestUnsetOfAPresentKeyInACRLFFileReportsTheWrite for
both callers: changed=true, the key line commented out, and the file
LF-normalized. Proven non-vacuous: that early return in both callers reds only
this test, both subtests on both assertions; in one caller it reds only that
caller's subtest; and writing CRLF back reds only the file assertion. The rest
of the package stays green under each.

Iteration 2, nit (contract) — the CHANGELOG said `--json` carries a `changed`
boolean, but only the yaml-only route's payload does; the DB-backed route still
emits just key and location. Scoped the sentence to the yaml-only payload.

Validation (scrubbed env — ambient BEADS_* and BD_* variables fake reds here):
internal/config full package is 450 PASS / 0 FAIL; cmd/bd seam selector with
BEADS_TEST_EMBEDDED_DOLT=1 is 63 PASS / 0 FAIL / 2 SKIP over 65 runs, the two
skips being the cells that need a Dolt server. The new embedded cell executes
rather than skipping, and reverting the two config.go changes reds it on both
the payload and the hint assertions.

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

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
2026-10-01 05:07:04 -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)
}