From 34e00ec58a97072393f394a3a28a172b04d15a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kova=C4=BE?= Date: Sun, 26 Jul 2026 00:47:23 +0200 Subject: [PATCH 1/2] chore(beads): untrack metadata.json, pin dolt.database in config.yaml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit metadata.json records dolt_mode, which must differ per host once devbox moves to the shared dolt sql-server (embedded on the Mac, server on devbox). It is git-tracked today and teleport syncs it between the two, so the hosts fight over one file — and bd rewrites it unprompted ("Auto-repaired: dolt_mode updated to server"), so discipline cannot avoid the conflict. Untrack it as host-local state; pin the host-independent bit (the database name) in config.yaml, which stays tracked. A fresh clone now needs `bd bootstrap` — which teleport's beads_fetch already runs for a new host, and which bin/bd now enforces rather than letting bd report an empty backlog. --- .beads/.gitignore | 16 ++++++++++++++-- .beads/config.yaml | 17 ++++++++++++++++- .beads/metadata.json | 7 ------- 3 files changed, 30 insertions(+), 10 deletions(-) delete mode 100644 .beads/metadata.json diff --git a/.beads/.gitignore b/.beads/.gitignore index 304f708..3c89677 100644 --- a/.beads/.gitignore +++ b/.beads/.gitignore @@ -66,5 +66,17 @@ db.sqlite bd.db # NOTE: Do NOT add negation patterns here. # They would override fork protection in .git/info/exclude. -# Config files (metadata.json, config.yaml) are tracked by git by default -# since no pattern above ignores them. +# config.yaml is tracked by git (shared, host-independent settings). +# metadata.json is deliberately NOT tracked — see below. + +# metadata.json is host-local state, not shared config. It records dolt_mode, +# which legitimately differs per machine: embedded on the Mac, server on devbox +# (against the shared dolt sql-server). Tracking it makes the two hosts fight +# over one file on every teleport git sync — and bd rewrites it unprompted +# ("Auto-repaired: dolt_mode updated to ..."), so the conflict is unavoidable. +# +# A fresh clone therefore has no metadata.json and must be bootstrapped, which +# is already what teleport's beads_fetch does for a new host. bin/bd refuses to +# run against an un-bootstrapped clone rather than let bd fall back to the +# default database and report an empty backlog. +metadata.json diff --git a/.beads/config.yaml b/.beads/config.yaml index 15faa05..be9c422 100644 --- a/.beads/config.yaml +++ b/.beads/config.yaml @@ -64,4 +64,19 @@ export.git-add: false # side now rides the `bd` shim (~/.config/bin/bd) — a debounced, crash-safe background # coordinator that flushes after every mutating `bd` — per ADR 0013. Leaving auto-push on would # just add a redundant, racy double-push. Pulls stay explicit (`bd dolt pull`). -dolt.auto-push: false \ No newline at end of file +dolt.auto-push: false + +# Dolt database name — the tracked, host-independent record of which database on +# the shared dolt sql-server belongs to this repo. metadata.json also carries it, +# but that file is host-local (gitignored, see .beads/.gitignore), so this is the +# copy that survives a fresh clone and tells provisioning/bootstrap the right name +# instead of leaving it to bd's directory-basename auto-detection. +# +# Note: bd only reads this file once metadata.json has established the workspace — +# config.yaml alone does NOT make a checkout usable. Run `bd bootstrap` on a fresh +# clone; teleport's beads_fetch already does this for a new host. +# +# Host-specific connection settings (dolt.shared-server / host / port) are +# deliberately NOT set here: they would follow the repo to the Mac, which stays +# embedded. bin/bd derives them per-project from metadata.json instead. +dolt.database: "dotfiles" \ No newline at end of file diff --git a/.beads/metadata.json b/.beads/metadata.json deleted file mode 100644 index b7db388..0000000 --- a/.beads/metadata.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "database": "dolt", - "backend": "dolt", - "dolt_mode": "embedded", - "dolt_database": "dotfiles", - "project_id": "36cb1b8c-7f21-4cfe-8547-a1fe64c239e2" -} \ No newline at end of file From fe10786453c158e0dd669a5252b29630de995a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kova=C4=BE?= Date: Sun, 26 Jul 2026 00:47:36 +0200 Subject: [PATCH 2/2] feat(bd-shim): per-project shared-server selection, database-keyed coordinator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three changes, all needed by the shared dolt sql-server on devbox. 1. Shared-server selection, per project. bd only reads dolt_mode=server as the *shared* server when BEADS_DOLT_SHARED_SERVER is set; without it the same metadata resolves to "per-project" mode on port 0 and bd spawns a stray server of its own. Exporting it globally is not viable — bd then auto- rewrites ANY embedded project's metadata.json to dolt_mode=server, breaking every project not yet migrated. The shim derives it from the project's own metadata.json, so migrations stay one-at-a-time and embedded repos are untouched. 2. Coordinator keyed on database identity, not the git worktree. Several worktrees of one repo resolve to a single database on the shared server and must share one coordinator lock, or two can commit/push concurrently. Embedded databases live inside the checkout, so there the .beads directory remains the identity. 3. Fresh-clone guard. metadata.json is now gitignored, so a clone that was never bootstrapped has config.yaml and no metadata.json. bd's behaviour there is to warn and fall back to the default database "beads" — reporting an empty backlog and carrying on. For an agent-facing tracker a silent "0 issues" is the worst failure mode, so the shim refuses, pointing at `bd bootstrap`. Setup/diagnostic verbs stay exempt. Metadata parsing is pure bash (builtin redirection + parameter expansion, no jq/grep fork) to keep the read fast-path a straight exec. Verified: embedded projects get no env; server projects get it; two worktrees of one database share a lock while a different database does not; the guard fires and exempts bootstrap; the Mac's tuidriver and dotfiles still resolve embedded unchanged. --- bin/bd | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 6 deletions(-) diff --git a/bin/bd b/bin/bd index c5785f2..c52f629 100755 --- a/bin/bd +++ b/bin/bd @@ -35,6 +35,14 @@ _BD_SHIM_COORD_ARG="__bd_shim_coordinator_loop" _BD_SHIM_SRC="${BASH_SOURCE[0]}" _BD_SHIM_SELF="" +# Resolved .beads directory and the two metadata.json fields we care about. +# Deliberately NOT named BEADS_DIR: that is bd's own env var, and assigning to it +# here would clobber a caller's export. Initialised so `set -u` is safe when this +# file is sourced (the test harness) without _bd_shim_main running. +_BD_BEADS_DIR="" +_BD_META_MODE="" +_BD_META_DB="" + _now() { date +%s; } # Absolute, symlink-resolved path of $1 without GNU `realpath`/`readlink -f` (macOS ships @@ -95,15 +103,55 @@ _is_mutating() { esac } +# Nearest .beads directory, walking up from $PWD. Honours an inherited BEADS_DIR (bd's own +# override). Pure bash — no subprocess — because this runs on every invocation including the +# read fast-path, which is otherwise a straight exec. +_locate_beads() { + if [ -n "${BEADS_DIR:-}" ]; then _BD_BEADS_DIR="$BEADS_DIR"; return 0; fi + local d="$PWD" + while : ; do + if [ -d "$d/.beads" ]; then _BD_BEADS_DIR="$d/.beads"; return 0; fi + if [ "$d" = "/" ] || [ -z "$d" ]; then break; fi + d=${d%/*}; [ -n "$d" ] || d="/" + done + _BD_BEADS_DIR="" + return 1 +} + +# Pull dolt_mode / dolt_database out of metadata.json without forking: `$(/dev/null) || root="" - [ -n "$root" ] || root="$PWD" - key=$(printf '%s' "$root" | cksum | cut -d' ' -f1) + local ident key + if [ "$_BD_META_MODE" = "server" ] && [ -n "$_BD_META_DB" ]; then + ident="server:$_BD_META_DB" + elif [ -n "$_BD_BEADS_DIR" ]; then + ident="embedded:$_BD_BEADS_DIR" + else + ident="cwd:$PWD" + fi + key=$(printf '%s' "$ident" | cksum | cut -d' ' -f1) STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/bd-shim/$key" mkdir -p "$STATE_DIR" 2>/dev/null || true QUIET="${BD_SHIM_QUIET:-2}" @@ -201,6 +249,39 @@ _bd_shim_main() { esac done + _locate_beads || true + _read_beads_meta || true + + # Fresh-clone guard. config.yaml is tracked; metadata.json is NOT (it records dolt_mode, + # which differs per host — embedded on the Mac, server on the devbox). A checkout with the + # former and not the latter is therefore a clone that was never bootstrapped, and bd's + # behaviour there is to warn and fall back to the default database "beads" — i.e. report an + # empty backlog and carry on. For an agent-facing tracker a silent "0 issues" is the worst + # possible failure, so turn it into a loud one. Setup/diagnostic verbs are exempt, above all + # `bootstrap`, which is the fix. + if [ -n "$_BD_BEADS_DIR" ] && [ -z "$_BD_META_MODE" ] && [ -r "$_BD_BEADS_DIR/config.yaml" ]; then + case "$sub" in + bootstrap|init|migrate|upgrade|doctor|where|help|version|completion|setup|hooks|config|"") ;; + *) + printf 'bd-shim: %s has config.yaml but no metadata.json — this clone has never been bootstrapped.\n' "$_BD_BEADS_DIR" >&2 + printf 'bd-shim: refusing "bd %s", which would silently use the default database and report an empty backlog.\n' "$sub" >&2 + printf 'bd-shim: run `bd bootstrap` (or `bd bootstrap --dry-run` to see the plan first).\n' >&2 + exit 69 + ;; + esac + fi + + # Shared-server selection, PER PROJECT. bd only treats dolt_mode=server as the *shared* + # server when BEADS_DOLT_SHARED_SERVER is set; without it the same metadata resolves to + # "per-project" mode on port 0 and bd will spawn a stray server of its own. Exporting it + # globally (e.g. from /etc/zsh/zshenv) is not an option: bd then auto-rewrites ANY embedded + # project's metadata.json to dolt_mode=server ("Auto-repaired: dolt_mode updated to + # server"), breaking every project not yet migrated. Deriving it from this project's own + # metadata keeps migrations one-at-a-time and leaves embedded repos untouched. + if [ "$_BD_META_MODE" = "server" ] && [ -z "${BEADS_DOLT_SHARED_SERVER:-}" ]; then + export BEADS_DOLT_SHARED_SERVER=1 + fi + # Reads / sync-plumbing / setup: exec the real bd — zero post-overhead and a perfect # passthrough of stdin/stdout/stderr/tty (matters for create-form and edit-in-$EDITOR). if ! _is_mutating "$sub"; then