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
16 changes: 14 additions & 2 deletions .beads/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 16 additions & 1 deletion .beads/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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"
7 changes: 0 additions & 7 deletions .beads/metadata.json

This file was deleted.

93 changes: 87 additions & 6 deletions bin/bd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: `$(<file)` is a bash
# builtin redirection and the rest is parameter expansion (bash 3.2 safe — no jq, no grep).
_read_beads_meta() {
_BD_META_MODE=""; _BD_META_DB=""
[ -n "$_BD_BEADS_DIR" ] && [ -r "$_BD_BEADS_DIR/metadata.json" ] || return 1
local raw t
raw=$(<"$_BD_BEADS_DIR/metadata.json")
case "$raw" in *'"dolt_mode"'*)
t=${raw#*\"dolt_mode\"}; t=${t#*:}; t=${t#*\"}; _BD_META_MODE=${t%%\"*} ;;
esac
case "$raw" in *'"dolt_database"'*)
t=${raw#*\"dolt_database\"}; t=${t#*:}; t=${t#*\"}; _BD_META_DB=${t%%\"*} ;;
esac
return 0
}

# 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. Key by
# the git toplevel (where the db lives) with a $PWD fallback; cksum keeps it POSIX.
# coordinator (holding the single-instance lock) swallow another repo's pending push.
#
# Keyed on the DATABASE, not the git worktree: on the shared Dolt server several worktrees of
# one repo resolve to a single database, and they must share one coordinator or two of them can
# commit/push concurrently. An embedded database lives inside the checkout, so there the .beads
# directory is the identity. cksum keeps the hashing POSIX.
_setup_state() {
local root key
root=$(git rev-parse --show-toplevel 2>/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}"
Expand Down Expand Up @@ -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
Expand Down
Loading