Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions bin/bd
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,55 @@ _read_beads_meta() {
return 0
}

# Is THIS HOST provisioned to run the shared beads Dolt server? Tested by the presence of
# ~/.beads/shared-server/beads-server.yaml — the config file homelab's `roles/beads_dolt`
# ansible role writes (never bd itself; see homelab BEADS.md "Where each piece lives"). Three
# candidates were considered:
# - a live probe (connect to 127.0.0.1:3308, or `systemctl --user is-active
# beads-dolt.service`): rejected outright — both fork a subprocess/socket, which this file's
# portability bar forbids outside the already-subprocess-heavy mutating path, and this check
# runs on `bootstrap`, which isn't that path.
# - `~/.beads/shared-server/` (the parent dir) or its `dolt/` data dir: both are weaker
# signals than the config file — the data dir in particular is where EVERY database's Dolt
# files live (`<data_dir>/<db>`, see BEADS.md landmine #8), so it would keep reporting "yes"
# forever after a decommission, long after the directory stopped meaning "there is a server
# here". The config file is the one artifact the role's own install step (not steady-state
# operation) is responsible for, making it the closer match for "is this host SET UP to run
# the server" rather than "is data present" or "was the server ever started".
# A false positive (file present, server actually down or never started) is still safe: the
# caller below only ever turns it into env vars for `bd bootstrap`, and bootstrap against a dead
# server fails LOUD ("connection refused" against a real host:port) instead of today's silent
# wrong-database success — strictly better, which is the whole reason this guard exists.
_bd_shim_is_shared_server_host() {
[ -r "$HOME/.beads/shared-server/beads-server.yaml" ]
}

# Pull the flat `dolt.database: "<name>"` key out of config.yaml — NOT nested under a `dolt:`
# mapping; verified against the three tracked config.yaml files this shim actually serves
# (dotfiles/tuidriver/umbel), which all write it as a bare top-level key, double-quoted, alone
# on its line. A `while read` loop over shell input redirection is a bash builtin (like
# _read_beads_meta's `$(<file)`, no grep/sed/cat) — this path isn't the hot path (it only runs
# for `bd bootstrap` on a fresh, not-yet-bootstrapped clone), but there's no reason to reach for
# a subprocess when a builtin does the job just as simply. Sets _BD_CFG_DB="" and returns 1 if
# the key is missing or the file can't be read; the caller turns that into an explicit error
# ("add the key") rather than falling back to any guessed name.
_read_config_database() {
_BD_CFG_DB=""
[ -n "$_BD_BEADS_DIR" ] && [ -r "$_BD_BEADS_DIR/config.yaml" ] || return 1
local line
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
'dolt.database:'*)
line=${line#dolt.database:}
while [ "${line# }" != "$line" ]; do line=${line# }; done
line=${line#\"}; line=${line%\"}
_BD_CFG_DB="$line"
;;
esac
done < "$_BD_BEADS_DIR/config.yaml"
[ -n "$_BD_CFG_DB" ]
}

# Populate the coordinator's environment. State (lock/dirty/ts/log) is machine-local under
# $XDG_STATE_HOME and keyed per beads database — a global PATH shim must not let one repo's
# coordinator (holding the single-instance lock) swallow another repo's pending push.
Expand Down Expand Up @@ -269,6 +318,51 @@ _bd_shim_main() {
exit 69
;;
esac

# The guard above just exempted `bootstrap` — but on a shared-server host its own
# advice is wrong. `bd bootstrap` has no --database flag (verified from its help); the
# only way to name the target database is BEADS_DOLT_SHARED_SERVER=1 *and*
# BEADS_DOLT_SERVER_DATABASE=<name> TOGETHER — bd 1.0.4 fails every partial combination
# SILENTLY (neither -> embedded engine, database "beads"; shared-server var alone ->
# shared engine, database still "beads"; database var alone -> embedded engine, a
# throwaway local .beads/embeddeddolt/<name> nobody wants). See homelab BEADS.md
# landmine #10. And bootstrap does not read THIS project's own tracked config.yaml
# `dolt.database` to fill that gap (landmine #11) — despite config.yaml's own comment
# insisting it does — because there is no metadata.json yet to establish the workspace
# that reading would require. So: on a shared-server host, derive the name ourselves and
# hand bootstrap both variables it needs, scoped to this one exec (never exported past
# it, and this is a one-shot `exec` invocation with no parent shell to leak into anyway).
#
# Restricted to `bootstrap` alone, not the whole exemption list above:
# - it is the one verb landmine #10 is actually about, and the one this guard's exit-69
# message sends the user to run;
# - `init` looks similar but is a different code path for a BRAND-NEW project (flags,
# not env — see BEADS.md "Adding a new project"), and on this guard's own
# precondition (an EXISTING project's fresh clone) it fails on an already-existing
# database regardless (landmine #14) — these env vars would not rescue it;
# - `doctor`/`migrate`/`upgrade`/etc. are not documented anywhere as needing
# BEADS_DOLT_SERVER_DATABASE, and exporting BEADS_DOLT_SHARED_SERVER ambiently for a
# verb whose behaviour on a workspace-less checkout has never been verified is
# exactly the kind of guess this repo's landmines exist to warn against (landmine #1:
# the same var can auto-rewrite an unrelated embedded project's metadata.json to
# server mode).
#
# A caller who has already set EITHER var is left alone (both checked with -z below) —
# same precedent as the shared-server-selection block just past this one: an explicit
# override is deliberate, not a gap to paper over.
if [ "$sub" = "bootstrap" ] && [ -z "${BEADS_DOLT_SHARED_SERVER:-}" ] \
&& [ -z "${BEADS_DOLT_SERVER_DATABASE:-}" ] && _bd_shim_is_shared_server_host
then
if _read_config_database; then
export BEADS_DOLT_SHARED_SERVER=1
export BEADS_DOLT_SERVER_DATABASE="$_BD_CFG_DB"
else
printf 'bd-shim: %s/config.yaml has no dolt.database key.\n' "$_BD_BEADS_DIR" >&2
printf 'bd-shim: this host runs the shared beads server, so bootstrap needs a database name from config.yaml — add a flat key, e.g. dolt.database: "%s", then re-run.\n' \
"$(basename "$(dirname "$_BD_BEADS_DIR")")" >&2
exit 69
fi
fi
fi

# Shared-server selection, PER PROJECT. bd only treats dolt_mode=server as the *shared*
Expand Down
146 changes: 146 additions & 0 deletions bin/bd.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/usr/bin/env bash
# bin/bd.test.sh — hermetic tests for the bin/bd shim's shared-server bootstrap fix
# (dotfiles-<TBD>). Follows the ccstatusline/umbel-segment.test.sh convention: a standalone,
# non-bats companion script next to the file it tests (exit 0 = all pass), NOT currently wired
# into clerk/project-gate (which only runs `bats tests/clerk` — see that file's `bats
# tests/clerk && shellcheck -S error bin/*`). Run directly: ./bin/bd.test.sh
#
# Two layers, matching the two halves of the fix:
# 1. Direct unit tests of the two new helper functions, via SOURCING bin/bd — exactly what
# the `[ "${BASH_SOURCE[0]}" = "${0}" ]` guard at the bottom of bin/bd exists for: sourcing
# exposes the functions without running _bd_shim_main.
# 2. An end-to-end behavioural test of `bd bootstrap --dry-run` through the shim as an
# EXECUTED script (this is the only way to exercise _bd_shim_main itself), with BD_REAL_BIN
# pointed at a stub "real bd" that just echoes back the argv and env it received — no real
# bd/dolt/network involved, so this stays hermetic and can never touch a live database.
set -u

DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
SUT="$DIR/bd"

fails=0
check() {
desc=$1; expected=$2; actual=$3
if [ "$actual" = "$expected" ]; then
printf 'ok - %s\n' "$desc"
else
printf 'FAIL - %s\n expected: [%s]\n actual: [%s]\n' \
"$desc" "$(printf '%s' "$expected" | cat -v)" "$(printf '%s' "$actual" | cat -v)"
fails=$((fails + 1))
fi
}

# ---------------------------------------------------------------------------
# Layer 1: source the shim, call the two new helpers directly.
# ---------------------------------------------------------------------------
# shellcheck source=bd
. "$SUT"

work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT

# --- _bd_shim_is_shared_server_host ---

# No ~/.beads/shared-server/ at all (the Mac's shape) -> false.
HOME="$work/mac-home"
mkdir -p "$HOME"
if _bd_shim_is_shared_server_host; then res=yes; else res=no; fi
check "no shared-server dir at all -> not a shared-server host" "no" "$res"

# ~/.beads/shared-server/ exists but the config file doesn't (e.g. mid-decommission) -> false.
HOME="$work/half-provisioned-home"
mkdir -p "$HOME/.beads/shared-server/dolt/somedb"
if _bd_shim_is_shared_server_host; then res=yes; else res=no; fi
check "shared-server data dir present without beads-server.yaml -> not a shared-server host" "no" "$res"

# The actual marker file present (devbox's shape) -> true.
HOME="$work/devbox-home"
mkdir -p "$HOME/.beads/shared-server"
: > "$HOME/.beads/shared-server/beads-server.yaml"
if _bd_shim_is_shared_server_host; then res=yes; else res=no; fi
check "beads-server.yaml present -> is a shared-server host" "yes" "$res"

# --- _read_config_database ---

mkdir -p "$work/proj-ok/.beads"
cat > "$work/proj-ok/.beads/config.yaml" <<'EOF'
# comment mentioning dolt.database: "not-this-one" must not be picked up
sync.remote: "git+https://github.com/vessux/tuidriver.git"
dolt.database: "tuidriver"
EOF
_BD_BEADS_DIR="$work/proj-ok/.beads"
if _read_config_database; then res="ok:$_BD_CFG_DB"; else res="fail:$_BD_CFG_DB"; fi
check "flat dolt.database key, ignores a commented-out mention -> parses the real value" "ok:tuidriver" "$res"

mkdir -p "$work/proj-missing/.beads"
cat > "$work/proj-missing/.beads/config.yaml" <<'EOF'
sync.remote: "git+https://github.com/vessux/tuidriver.git"
# dolt.database: "tuidriver" (commented out, never added — the honest-error case)
EOF
_BD_BEADS_DIR="$work/proj-missing/.beads"
if _read_config_database; then res="ok:$_BD_CFG_DB"; else res="fail:$_BD_CFG_DB"; fi
check "dolt.database key only in a comment -> treated as absent" "fail:" "$res"

# ---------------------------------------------------------------------------
# Layer 2: execute the shim end-to-end against a stub "real bd", proving the actual bootstrap
# invocation gets (or doesn't get) the two env vars — the thing that was silently wrong before.
# ---------------------------------------------------------------------------
stub="$work/stub-bd"
cat > "$stub" <<'EOF'
#!/usr/bin/env bash
printf 'ARGV:%s\n' "$*"
printf 'SHARED:%s\n' "${BEADS_DOLT_SHARED_SERVER:-<unset>}"
printf 'DATABASE:%s\n' "${BEADS_DOLT_SERVER_DATABASE:-<unset>}"
EOF
chmod +x "$stub"

fresh_clone="$work/fresh-clone/.beads"
mkdir -p "$fresh_clone"
cat > "$fresh_clone/config.yaml" <<'EOF'
sync.remote: "git+https://github.com/vessux/tuidriver.git"
dolt.database: "tuidriver"
EOF
# Deliberately no metadata.json: this IS the fresh-clone shape (config.yaml tracked,
# metadata.json gitignored and absent until a real bootstrap runs).

# devbox-shaped host: shared-server marker present -> shim must supply both vars.
devbox_home="$work/devbox-home2"
mkdir -p "$devbox_home/.beads/shared-server"
: > "$devbox_home/.beads/shared-server/beads-server.yaml"

out=$(HOME="$devbox_home" BEADS_DIR="$fresh_clone" BD_REAL_BIN="$stub" bash "$SUT" bootstrap --dry-run)
check "shared-server host: SHARED is exported" "SHARED:1" "$(printf '%s\n' "$out" | grep '^SHARED:')"
check "shared-server host: DATABASE derived from config.yaml" "DATABASE:tuidriver" "$(printf '%s\n' "$out" | grep '^DATABASE:')"

# Mac-shaped host: no shared-server marker anywhere -> behaviour unchanged (neither var set,
# same as before this fix existed).
mac_home="$work/mac-home2"
mkdir -p "$mac_home"
out=$(HOME="$mac_home" BEADS_DIR="$fresh_clone" BD_REAL_BIN="$stub" bash "$SUT" bootstrap --dry-run)
check "non-shared-server host (the Mac): SHARED left unset" "SHARED:<unset>" "$(printf '%s\n' "$out" | grep '^SHARED:')"
check "non-shared-server host (the Mac): DATABASE left unset" "DATABASE:<unset>" "$(printf '%s\n' "$out" | grep '^DATABASE:')"

# Shared-server host but config.yaml has no dolt.database key -> loud error, not a guessed name.
fresh_clone_nokey="$work/fresh-clone-nokey/.beads"
mkdir -p "$fresh_clone_nokey"
cat > "$fresh_clone_nokey/config.yaml" <<'EOF'
sync.remote: "git+https://github.com/vessux/example.git"
EOF
err=$(HOME="$devbox_home" BEADS_DIR="$fresh_clone_nokey" BD_REAL_BIN="$stub" bash "$SUT" bootstrap --dry-run 2>&1 >/dev/null)
rc=0
HOME="$devbox_home" BEADS_DIR="$fresh_clone_nokey" BD_REAL_BIN="$stub" bash "$SUT" bootstrap --dry-run >/dev/null 2>/dev/null || rc=$?
check "missing dolt.database key -> exits 69 (not a guess)" "69" "$rc"
check "missing dolt.database key -> stderr names the fix" "yes" "$(printf '%s' "$err" | grep -q 'dolt.database' && echo yes || echo no)"

# A caller who already set one of the two vars is left alone (explicit override wins).
out=$(HOME="$devbox_home" BEADS_DIR="$fresh_clone" BD_REAL_BIN="$stub" BEADS_DOLT_SERVER_DATABASE=explicit-choice bash "$SUT" bootstrap --dry-run)
check "caller already set BEADS_DOLT_SERVER_DATABASE: shim does not also set SHARED" "SHARED:<unset>" "$(printf '%s\n' "$out" | grep '^SHARED:')"
check "caller already set BEADS_DOLT_SERVER_DATABASE: shim leaves the caller's value alone" "DATABASE:explicit-choice" "$(printf '%s\n' "$out" | grep '^DATABASE:')"

echo
if [ "$fails" -eq 0 ]; then
echo "All tests passed."
exit 0
fi
echo "$fails test(s) failed."
exit 1
Loading