From 1854dbac7f07f6d94766d5fa322ef594a0cab985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kova=C4=BE?= Date: Sun, 26 Jul 2026 21:08:59 +0200 Subject: [PATCH] bd shim: derive BEADS_DOLT_SERVER_DATABASE for bootstrap on shared-server hosts The fresh-clone guard correctly refuses non-setup verbs on a checkout with config.yaml but no metadata.json and tells the user to run `bd bootstrap` -- but that advice is wrong on a shared-server host (devbox): bootstrap has no --database flag, and bd 1.0.4 only lands on the right database when BOTH BEADS_DOLT_SHARED_SERVER=1 and BEADS_DOLT_SERVER_DATABASE= are set, failing every partial combination silently onto database "beads" (homelab BEADS.md landmine #10, closing its "Known gaps" entry for this shim). Detect a shared-server host via the ansible role's own config file (~/.beads/shared-server/beads-server.yaml) rather than a liveness probe or the data dir -- see the in-code comment for why. Derive the database name from the checkout's own tracked .beads/config.yaml (dolt.database:, a flat key) rather than guessing, scoped to `bootstrap` alone and only when the caller has not already set either variable. Adds bin/bd.test.sh, a hermetic companion test (ccstatusline's .test.sh convention) for the two new helpers and the end-to-end bootstrap --dry-run behavior via a stub real-bd. --- bin/bd | 94 +++++++++++++++++++++++++++++++ bin/bd.test.sh | 146 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100755 bin/bd.test.sh diff --git a/bin/bd b/bin/bd index c52f629..51ae459 100755 --- a/bin/bd +++ b/bin/bd @@ -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 (`/`, 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: ""` 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 `$( 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/ 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* diff --git a/bin/bd.test.sh b/bin/bd.test.sh new file mode 100755 index 0000000..c1af36e --- /dev/null +++ b/bin/bd.test.sh @@ -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-). 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:-}" +printf 'DATABASE:%s\n' "${BEADS_DOLT_SERVER_DATABASE:-}" +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:" "$(printf '%s\n' "$out" | grep '^SHARED:')" +check "non-shared-server host (the Mac): DATABASE left unset" "DATABASE:" "$(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:" "$(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