Merge origin/main into fix/standalone-hooks-selected-context-6384

Resolves the re-conflict opened when main advanced 25 commits past the
previous merge's P2 (cefcabb1e), via #6321 (13150ba360) and #6457
(6a4b4aa14e). Six files conflicted / 14 hunks.

Dispositions, each measured rather than assumed:

- cmd/bd/hooks.go (union, per-hunk 8-way split). #6457 absorbed the PR's
  env plumbing and hardened it into a proof-string API
  (gitTrackedFileContext, trackedProofContainingIndex/InheritedIndex), so
  the probe hunks take main. main still passes nil from
  installHooksWithOptions and has no resolveStandaloneHooksContext, so the
  standalone machinery hunks take ours; ours already adopted main's
  shared-mode guard and routes around it with two entry points.

- cmd/bd/hooks_guard_test.go, cmd/bd/init_git_hooks.go,
  cmd/bd/init_proxied_server_test.go: main is a strict superset (the last
  adds "bare"/"git_dir_cwd" cases plus workTreeless handling; its
  excludeRoot defaults to common, so the three ours-only lines are the
  extended form of the same assertions). Byte-identical to origin/main.

- cmd/bd/hooks_worktree_test.go: ours is the superset here. All five
  shared helpers are byte-identical across both sides and main has no
  function ours lacks, so ours adds six standalone-hooks functions
  (+206/-0) with no deletion.

- internal/storage/domain/git/git_test.go: main relocated this block into
  the else-if arm above it and added
  TestConfig_BeadsRoleScrubsInheritedRoutingEnv, which asserts the
  stronger NoError/found/"maintainer" form. Of the PR's delta over base,
  s.False(useCaseFound) is already verbatim on main; only s.Empty(role)
  was uncovered, so it is carried into main's arm rather than duplicating
  the BeadsRole call.

Validation on the merged tree: go build ./... and go vet clean; the
36-name hooks/init selector over the PR's changed test files is 36 PASS /
0 FAIL / 0 SKIP (129 subtests). Mutating uninstallStandaloneHooks back to
the context-free resetHooksPathIfBeadsManaged (the negation of the PR's
thesis) gives 35 PASS / 1 FAIL, sole casualty
TestStandaloneHookCommandsUseSelectedContext red in all 9 subtests, so
the hooks.go union is load-bearing. The six cmd/bd/doctor failures are
base-preexisting ambient-environment reds: pristine origin/main and the
merged tree return identical verdicts on all six.
This commit is contained in:
Gas City Adopt-PR 2026-09-28 23:08:54 +00:00
commit 86defb4143
203 changed files with 11101 additions and 537 deletions

162
.bazelrc
View file

@ -26,12 +26,168 @@ test --test_env=BD_DISABLE_EVENT_FLUSH=1
# -short, skips, tag filters); the environment comes from the wrapper above.
test:prcore --@rules_go//go/config:race
test:prcore --test_arg=-test.short
# pr-core.sh's -parallel 4. Without it -test.parallel defaults to GOMAXPROCS,
# the executor's core count (192 on a farm worker), not a runner's 4.
test:prcore --test_arg=-test.parallel=4
test:prcore --test_arg=-test.skip=^TestEmbedded
test:prcore --test_env=BEADS_TEST_SKIP=dolt
test:prcore --test_tag_filters=-requires-docker,-embedded,-manual
# As in pr.yml's PR Core step: a root or non-POSIX executor fails
# TestAddExcludePatternsRefusesReadErrors instead of skipping it.
test:prcore --test_env=BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION=1
# Every tag that belongs to another lane (scripts/bazel_policy_test.go
# bazelPRCoreExcludedTags). integration-only targets run zero tests outside
# --config=integration.
test:prcore --test_tag_filters=-requires-docker,-dolt-server,-embedded,-manual,-integration-only
test:ci --config=prcore
test:ci --keep_going --test_summary=terse
# tools/bazel/equivalence.py compares the Go tests Bazel ran with the ones
# `go test` would run, from each target's test.xml. rules_go only lists passing
# tests in test.xml under -test.v, and --remote_download_minimal would leave
# test.xml on the remote side, so ask for both.
test:ci --test_env=GO_TEST_WRAP_TESTV=1
test:ci --remote_download_regex=.*/test\.xml$
# bazel.yml publishes the non-race bd as pr.yml's ci-build-artifacts.
test:ci --remote_download_regex=.*/bin/cmd/bd/bd_for_tests/bd$
# Docker lane: only the requires-docker targets (pr.yml's container-backed
# jobs "Test (storage domain + uow)" and "Contract corpus"), on a host with a
# docker daemon. Race like the domain+uow job (//cmd/bd/protocol's variant
# opts out, like the non-race Contract corpus job), but no -short and no
# BEADS_TEST_SKIP=dolt, which would keep the containers from starting. Those
# targets are also tagged no-remote-exec, so they run on the host even with
# --config=remote-exec; their builds may still run remotely. -test.v in
# test.xml, as in test:ci, lets equivalence.py see which tests ran.
test:docker --@rules_go//go/config:race
test:docker --test_tag_filters=requires-docker
test:docker --build_tests_only
test:docker --keep_going
test:docker --test_env=GO_TEST_WRAP_TESTV=1
# Those jobs pass no -p/-parallel, so go test uses GOMAXPROCS, ubuntu-latest's
# 4 vCPUs, for both. Match that on any host: 4 test processes (hence at most
# 4 dolt containers starting at once) and -test.parallel=4.
test:docker --local_test_jobs=4
test:docker --test_arg=-test.parallel=4
# The variants depend on host state no action key sees (the docker daemon,
# the pulled dolt image, the network), so a cached result proves nothing about
# this host: always run them, like the jobs' `go test -count=1`.
test:docker --nocache_test_results
# Embedded-Dolt tier: PR Risk's embedded jobs ("Test (Embedded Dolt Cmd
# N/20)", "Test (Embedded Dolt Storage N/5)", "Test (Embedded Dolt
# Conformance - core/audit)"), as the `embedded`-tagged variants of bd_test
# and embeddeddolt_test. Race like the jobs' binaries, no -short, no
# BEADS_TEST_SKIP; each variant sets BEADS_TEST_EMBEDDED_DOLT=1. The shard
# variants run the jobs' shard scripts, so every Bazel shard runs exactly the
# tests of the job with the same number.
# Only the variants and what they need are built (the same race configuration
# as --config=ci, so the two lanes share every compile).
test:embedded --@rules_go//go/config:race
test:embedded --test_tag_filters=embedded
test:embedded --build_tests_only
test:embedded --keep_going
test:embedded --test_summary=terse
# The variants are timeout = "eternal" because the jobs' Go timeouts are past
# "long"; rbe-west rejects any action timeout over 1200s, so eternal means
# 1200s here, and the effective limits are: storage shards, the job's
# -test.timeout=15m; cmd shards and conformance core/audit, -test.timeout=19m
# (the jobs pass 20m and 30m), so that Go's timeout, which dumps every
# goroutine, fires before Bazel's 1200s kill, which does not (see the BUILD
# targets).
test:embedded --test_timeout=-1,-1,-1,1200
# The jobs pass no -parallel: GOMAXPROCS on a 4-vCPU runner.
test:embedded --test_arg=-test.parallel=4
test:embedded --test_env=GO_TEST_WRAP_TESTV=1
test:embedded --remote_download_regex=.*/test\.xml$
# Dolt-server lane: the same jobs as the docker lane ("Test (storage domain +
# uow)" and "Contract corpus"), but every dolt sql-server is a child of the
# test process, started from the pinned dolt in the test wrapper's runfiles
# (BEADS_TEST_DOLT_SERVER=local, set by each dolt-server target), so the
# targets run on remote workers and need no docker. Same selection rules as
# test:docker: race (//cmd/bd/protocol's variant opts out, like its job), no
# -short, no BEADS_TEST_SKIP=dolt, -test.parallel=4, and the no--short twins
# of the job's other packages. Results are cacheable: every input (test
# binary, dolt, env) is in the action key, and a server that cannot start
# fails the target (BEADS_TEST_REQUIRE_DOLT_CONTAINER=1) instead of skipping.
# --config=docker stays as the A/B control. test.xml is downloaded for
# tools/bazel/equivalence.py --job, test.log for bazel.yml's failure artifact.
test:doltserver --@rules_go//go/config:race
test:doltserver --test_tag_filters=dolt-server
test:doltserver --build_tests_only
test:doltserver --keep_going
test:doltserver --test_env=GO_TEST_WRAP_TESTV=1
test:doltserver --test_arg=-test.parallel=4
# Locally (fork PRs, rbe=off, no executor): at most 4 test processes, as in
# test:docker; remote workers are not limited by it.
test:doltserver --local_test_jobs=4
test:doltserver --remote_download_regex=.*/test\.(log|xml)$
# Integration lane: main.yml's "Main Linux integration packages (N/6)" and
# "Main Linux integration cmd/bd (N/8)" jobs (and nightly.yml's Full Test
# Suite, which runs the same `go test` over ./... in one job): every package
# built with -tags=integration,gms_pure_go, race, no -short, no -skip,
# BEADS_TEST_SKIP=dolt. The tag is set for the whole build, as `go test -tags`
# does, not per target: gazelle generates srcs/deps with integration files
# included (root BUILD.bazel `gazelle:build_tags`), rules_go drops them from
# every other configuration, and //internal/testutil/integration (integration
# files only) compiles to its real contents only here. A per-target `gotags`
# would not save the compile either: it transitions the whole dep graph.
build:integration --@rules_go//go/config:tags=gms_pure_go,integration
test:integration --@rules_go//go/config:race
test:integration --test_env=BEADS_TEST_SKIP=dolt
# The variants the PR-core lane leaves out stay out, except integration-only
# (targets with tests only under the tag): the jobs' BEADS_TEST_SKIP=dolt
# keeps every container- or server-backed test skipped, which is what the
# untagged base targets already do. scripts/bazel_policy_test.go derives the
# required exclusions from the PR-core list, so a new lane tag lands here too.
test:integration --test_tag_filters=-requires-docker,-dolt-server,-embedded,-manual
test:integration --build_tests_only
test:integration --keep_going
test:integration --test_summary=terse
# The jobs pass no -p/-parallel: GOMAXPROCS on a 4-vCPU ubuntu-latest runner.
test:integration --test_arg=-test.parallel=4
# The jobs give each package binary -timeout=30m. rbe-west rejects an action
# timeout over 1200s, so every size gets 1200s, and Go's own -test.timeout (19m,
# after the 1200s rules_go derives from TEST_TIMEOUT: the last flag wins) fires
# first and dumps every goroutine to test.log, which Bazel's kill does not.
test:integration --test_timeout=1200
test:integration --test_arg=-test.timeout=19m
# The runner is not root, so TestAddExcludePatternsRefusesReadErrors runs
# there; make a root executor fail it rather than skip (as test:prcore does).
test:integration --test_env=BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION=1
# -test.v in test.xml, as in test:ci, for tools/bazel/equivalence.py.
test:integration --test_env=GO_TEST_WRAP_TESTV=1
# ci and integration build into the same output directory (the tags flag
# does not change it), so they share bazel-testlogs: a lane's test.xml is
# overwritten by the other lane's next run of the same target. Run
# equivalence.py / check_testcases.py right after each lane's own
# `bazel test`, before the other lane runs (or give the lanes separate jobs).
test:integration --remote_download_regex=.*/test\.xml$
# Pure-Go lane: pr.yml's "Check pure-Go and js/wasm boundaries
# (CGO_ENABLED=0)" job. CGO is off for every Go target (rules_go drops
# cgo-constrained files like go build does; third_party/patches/
# gozstd_nocgo.patch lets dolt's store/nbs, an unused but declared dep,
# compile; its package init panics, so a pure binary that links it fails when
# run). bazel.yml builds bd and the job's pure test binaries, runs each of
# them once (the contamination check `go build` does at compile time) and runs
# its -short cmd/bd subset.
build:pure --@rules_go//go/config:pure
test:pure --test_arg=-test.short
# The job passes no -parallel: GOMAXPROCS on a 4-vCPU runner.
test:pure --test_arg=-test.parallel=4
test:pure --test_env=GO_TEST_WRAP_TESTV=1
test:pure --remote_download_regex=.*/test\.xml$
# js/wasm hook boundary (same job): build for GOOS=js GOARCH=wasm. Bazel
# cannot run the wasm test binary itself; bazel.yml runs it with the Go SDK's
# go_js_wasm_exec (node), like `go test -exec`. The test toolchain needs an
# exec platform matching the target platform, hence
# --use_target_platform_for_tests (no test action runs under this config).
build:js-wasm --platforms=@rules_go//go/toolchain:js_wasm
build:js-wasm --use_target_platform_for_tests
build:js-wasm --remote_download_outputs=toplevel
# Remote execution against any REAPI-compatible service. Only generic tuning
# lives here; the executor endpoint and TLS credentials are never committed.
@ -41,6 +197,10 @@ build:remote-exec --jobs=64
build:remote-exec --remote_download_minimal
build:remote-exec --remote_timeout=600
build:remote-exec --remote_retries=3
# Upload only remotely executed results. A locally executed action (a
# no-remote-exec test, a host-dependent docker-lane result) must not seed the
# shared cache that CI and every other developer read.
build:remote-exec --noremote_upload_local_results
try-import %workspace%/.bazelrc.local
try-import %workspace%/user.bazelrc

70
.github/actions/setup-bazel/action.yml vendored Normal file
View file

@ -0,0 +1,70 @@
name: setup-bazel
description: >
Install a sha256-pinned Bazelisk (Bazel itself comes from .bazelversion),
restore the runner Bazel cache (--repository_cache and Bazelisk only),
and write a CI bazelrc outside the workspace. When the caller passes all of
BAZEL_REMOTE_EXECUTOR, RBE_TLS_CERT and RBE_TLS_KEY in the step env, that rc
enables --config=remote-exec for every bazel command; with none of them the
job runs locally (fork PRs); a partial set fails. `bazel` on PATH is a
wrapper that always passes the generated rc.
outputs:
remote:
description: '"true" when remote execution is configured'
value: ${{ steps.rc.outputs.remote }}
cache-restored:
description: '"true" when a runner Bazel cache was restored (any prefix match)'
value: ${{ steps.restore.outputs.cache-matched-key != '' }}
cache-hit:
description: '"true" when the cache for this .bazelversion and MODULE.bazel.lock was restored exactly'
value: ${{ steps.restore.outputs.cache-hit }}
runs:
using: composite
steps:
# Keyed on what determines its content; saved only by a push to main that
# missed the exact key (see the caller's "Save Bazel runner cache" step).
# A lock change restores the previous lock's entry, which still holds most
# of the downloads.
- name: Restore Bazel runner cache
id: restore
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ runner.temp }}/bazel-ci-cache
key: bazel-repo-v2-${{ runner.os }}-${{ hashFiles('.bazelversion', 'MODULE.bazel.lock') }}
restore-keys: |
bazel-repo-v2-${{ runner.os }}-
- name: Install Bazelisk
shell: bash
run: |
set -euo pipefail
version=v1.29.0
case "$(uname -m)" in
x86_64) arch=amd64; sha=5a408715e932c0250d28bd84555f12edbf70117de42f9181691c736eacc4a992 ;;
aarch64 | arm64) arch=arm64; sha=e20e8b0f4f240091b7a55bf17b9398bd4f40ee70ae0208dff95dd4c445fb4010 ;;
*) echo "setup-bazel: unsupported arch $(uname -m)" >&2; exit 1 ;;
esac
curl -fsSL --retry 3 --max-time 120 -o "$RUNNER_TEMP/bazelisk" \
"https://github.com/bazelbuild/bazelisk/releases/download/${version}/bazelisk-linux-${arch}"
echo "${sha} $RUNNER_TEMP/bazelisk" | sha256sum -c -
sudo install -m 755 "$RUNNER_TEMP/bazelisk" /usr/local/bin/bazelisk
rm -f "$RUNNER_TEMP/bazelisk"
echo "BAZELISK_HOME=$RUNNER_TEMP/bazel-ci-cache/bazelisk" >> "$GITHUB_ENV"
- name: Write CI bazelrc
id: rc
shell: bash
env:
BAZEL_CI_CACHE_DIR: ${{ runner.temp }}/bazel-ci-cache
BAZEL_CI_SECRET_DIR: ${{ runner.temp }}/bazel-ci-secret
run: bash "$GITHUB_ACTION_PATH/write-bazelrc.sh"
- name: Install bazel wrapper
shell: bash
env:
RC: ${{ steps.rc.outputs.rc }}
run: |
set -euo pipefail
printf '%s\n' '#!/usr/bin/env bash' "exec /usr/local/bin/bazelisk --bazelrc=\"$RC\" \"\$@\"" > "$RUNNER_TEMP/bazel-wrapper"
sudo install -m 755 "$RUNNER_TEMP/bazel-wrapper" /usr/local/bin/bazel
rm -f "$RUNNER_TEMP/bazel-wrapper"
/usr/local/bin/bazelisk --version

View file

@ -0,0 +1,152 @@
#!/usr/bin/env bash
# Write the CI bazelrc for .github/actions/setup-bazel.
#
# Always: --repository_cache under BAZEL_CI_CACHE_DIR (the directory the
# runner cache saves and restores). Never --disk_cache: remote runs have the
# remote cache, and a local run's disk cache (about 7 GB) would not fit next to
# its output base on a hosted runner.
#
# Remote execution only when all three of BAZEL_REMOTE_EXECUTOR, RBE_TLS_CERT
# and RBE_TLS_KEY are set (repo secrets; empty on fork PRs, so those runs stay
# local). Exactly one or two of them is a misconfiguration and fails. Optional:
# RBE_TLS_CA (base64 PEM CA bundle) and RBE_INSTANCE (instance name; a warning
# when empty, because the default instance may share its action cache with
# other repos on the same farm). Cert and key are base64-encoded PEM (raw PEM
# is accepted too). Every line of the decoded PEM is masked in the job log.
#
# CI uploads only remotely executed results (--noremote_upload_local_results):
# a job's locally executed actions (PR code included) never enter the shared
# action cache from here. Developer rcs keep Bazel's default.
#
# The rc and the key material go to BAZEL_CI_SECRET_DIR, which must be outside
# the workspace (tests that walk the checkout must never see an endpoint or a
# cert path) and outside BAZEL_CI_CACHE_DIR (the runner cache must never store
# credentials). The endpoint is masked in the job log.
#
# Outputs (to $GITHUB_OUTPUT when set, else stdout): rc=<path>, remote=true|false.
set -euo pipefail
cache_dir="${BAZEL_CI_CACHE_DIR:?BAZEL_CI_CACHE_DIR is required}"
secret_dir="${BAZEL_CI_SECRET_DIR:?BAZEL_CI_SECRET_DIR is required}"
workspace="${GITHUB_WORKSPACE:-$PWD}"
abs() { mkdir -p "$1" && (cd "$1" && pwd -P); }
cache_dir="$(abs "$cache_dir")"
secret_dir="$(abs "$secret_dir")"
workspace="$(cd "$workspace" && pwd -P)"
case "$secret_dir/" in
"$workspace"/*)
echo "setup-bazel: BAZEL_CI_SECRET_DIR ($secret_dir) must be outside the workspace" >&2
exit 1
;;
"$cache_dir"/*)
echo "setup-bazel: BAZEL_CI_SECRET_DIR must not be inside the cached BAZEL_CI_CACHE_DIR" >&2
exit 1
;;
esac
chmod 700 "$secret_dir"
mkdir -p "$cache_dir/repo"
mask() {
local v="${1:-}"
[[ -z "$v" ]] && return 0
printf '::add-mask::%s\n' "$v"
}
# Mask the endpoint and its bare host[:port]: Bazel prints bytestream:// URIs
# that carry the host without the scheme.
executor="${BAZEL_REMOTE_EXECUTOR:-}"
if [[ -n "$executor" ]]; then
mask "$executor"
hostport="${executor#*://}"
hostport="${hostport%%/*}"
mask "$hostport"
mask "${hostport%:*}"
# Bazel's connection errors print the resolved address ("host/1.2.3.4:443").
if command -v getent >/dev/null 2>&1; then
while read -r addr _; do
mask "$addr"
done < <(getent ahosts "${hostport%:*}" 2>/dev/null | sort -u -k1,1)
fi
fi
cert="${RBE_TLS_CERT:-}"
key="${RBE_TLS_KEY:-}"
ca="${RBE_TLS_CA:-}"
instance="${RBE_INSTANCE:-}"
rc="$secret_dir/ci.bazelrc"
umask 077
{
echo "# Generated by .github/actions/setup-bazel; not a workspace file."
echo "common --repository_cache=$cache_dir/repo"
} >"$rc"
# write_pem DEST VALUE: decode base64 (or accept raw PEM) and check it is PEM.
write_pem() {
local dest="$1" body="$2"
body="$(printf '%s' "$body" | tr -d '\r')"
if [[ "$body" != *"-----BEGIN "* ]]; then
if ! body="$(printf '%s' "$body" | tr -d '\n ' | base64 -d 2>/dev/null)"; then
echo "setup-bazel: $(basename "$dest") secret is neither PEM nor base64 PEM" >&2
return 1
fi
fi
if [[ "$body" != *"-----BEGIN "* ]]; then
echo "setup-bazel: $(basename "$dest") secret does not decode to PEM" >&2
return 1
fi
# GitHub masks the secret as stored (base64), not what it decodes to.
local line
while IFS= read -r line; do
[[ -z "$line" || "$line" == -----* ]] || mask "$line"
done <<<"$body"
printf '%s\n' "$body" >"$dest"
chmod 600 "$dest"
}
set_fields=0
for v in "$executor" "$cert" "$key"; do
[[ -n "$v" ]] && set_fields=$((set_fields + 1))
done
remote=false
if [[ "$set_fields" -eq 3 ]]; then
write_pem "$secret_dir/client.crt" "$cert"
write_pem "$secret_dir/client.key" "$key"
# Outside the rc block below: write_pem's mask commands go to the log.
if [[ -n "$ca" ]]; then
write_pem "$secret_dir/ca.crt" "$ca"
fi
{
echo "build:remote-exec --remote_executor=$executor"
echo "build:remote-exec --tls_client_certificate=$secret_dir/client.crt"
echo "build:remote-exec --tls_client_key=$secret_dir/client.key"
if [[ -n "$ca" ]]; then
echo "build:remote-exec --tls_certificate=$secret_dir/ca.crt"
fi
if [[ -n "$instance" ]]; then
echo "build:remote-exec --remote_instance_name=$instance"
fi
echo "build:remote-exec --noremote_upload_local_results"
# Every command in this job (test, run //:gazelle, query) uses the
# executor; the committed .bazelrc holds the generic remote-exec tuning.
echo "build --config=remote-exec"
} >>"$rc"
if [[ -z "$instance" ]]; then
echo "::warning title=RBE_INSTANCE not set::remote execution uses the executor's default instance, whose action cache other repos on the farm may share; set RBE_INSTANCE to a beads-only instance"
fi
remote=true
echo "setup-bazel: remote execution enabled"
elif [[ "$set_fields" -ne 0 ]]; then
echo "setup-bazel: remote execution is partially configured; BAZEL_REMOTE_EXECUTOR, RBE_TLS_CERT and RBE_TLS_KEY must be set together (or none, for local execution)" >&2
exit 1
else
echo "setup-bazel: no remote execution secrets; running locally"
fi
out="${GITHUB_OUTPUT:-/dev/stdout}"
{
echo "rc=$rc"
echo "remote=$remote"
} >>"$out"

524
.github/workflows/bazel.yml vendored Normal file
View file

@ -0,0 +1,524 @@
# Bazel side-by-side lane (advisory). Runs `bazel test //... --config=ci`, the
# PR Core-parity selection (race, -short, dolt skip, -requires-docker), on the
# same commit as pr.yml's go test lanes, plus one job per mirrored CI job
# (pure-Go/js-wasm, docker lane, pr-risk.yml's embedded-Dolt tier). It is NOT
# in ci-gate's CI_GATE_REQUIRED; promotion needs the equivalence record
# described in tools/bazel/equivalence.py and a human decision.
#
# Execution mode, passed to .github/actions/setup-bazel:
# repo variable RBE_WEST_WORKERS=true plus secrets RBE_WEST_EXECUTOR (grpcs://
# endpoint), RBE_TLS_CERT, RBE_TLS_KEY (base64; RBE_TLS_CA optional):
# remote execution on rbe-west, instance "oss" (shared with gascity).
# fork PRs and rbe=off dispatches: local execution, slower but the same
# results, for every job but bazel-embedded, which runs remotely only.
# Same-repo runs are skipped while RBE_WEST_WORKERS is unset.
# workflow_dispatch with rbe=off simulates a fork run.
#
# Accepted risk (same model as gascity's bazel-test.yml): on push, merge_group
# and same-repo pull_request runs, code from the PR head runs with the RBE
# client certificate. setup-bazel is itself PR code, the key file is readable
# by every later step and by locally executed Bazel actions, and a .bazelrc
# edit can point the BEP or the executor anywhere. Anyone who can push a
# branch here can therefore use or copy the certificate and write to the
# farm's cache. Fork PRs never get it: pull_request from a fork receives no
# secrets, this workflow has no pull_request_target trigger, and forks cannot
# write the runner cache. In place: secrets are referenced by the setup step
# alone and never by a workflow- or job-level env (policy-tested), decoded key
# material is masked in logs, and CI uploads only remotely executed results
# (--noremote_upload_local_results in .bazelrc's remote-exec config and in
# the generated rc), and the certificate
# is issued to beads CI alone, so it can be revoked without affecting other
# clients. Maintainers decided (2026-09-28) that same-repo PRs keep remote
# execution under this model. Still open: a beads-only RBE_INSTANCE and
# farm-side denial of client action-cache writes. The lane is advisory and
# nothing downstream consumes artifacts built here.
#
# Runner cache: one entry per (.bazelversion, MODULE.bazel.lock) pair, about
# 2 GB (Bazel's --repository_cache plus Bazelisk's Bazel download), written
# only by a push to main that missed it. No --disk_cache: remote runs use the
# remote cache, and local runs would need about 7 GB more.
#
# Workflow-file changes suppress pull_request runs under the org approval
# policy; validate edits to this file with workflow_dispatch on the branch.
name: Bazel
on:
pull_request:
branches: [ main, 'release/**' ]
merge_group:
push:
branches: [ main ]
workflow_dispatch:
inputs:
rbe:
description: "Remote execution: on (use secrets) or off (fork simulation)"
required: true
type: choice
default: "on"
options: [ "on", "off" ]
details:
description: "List every equivalence divergence"
required: false
type: boolean
default: false
dolt-lane:
description: "Dolt-backed lane: doltserver (hermetic, remote) or docker (testcontainers A/B control)"
required: false
type: choice
default: "doltserver"
options: [ "doltserver", "docker" ]
workflow_call:
inputs:
rbe:
type: string
required: false
default: "on"
details:
type: boolean
required: false
default: true
go-test-json-artifact:
description: >
Artifact (from the calling run) holding go-test.json, PR Core's
`go-test -json` on this commit; enables the equivalence skip-parity
check. nightly.yml sets it.
type: string
required: false
default: ""
# Main runs always finish (they may save the runner cache); PR runs supersede.
concurrency:
group: bazel-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
env:
BD_DISABLE_METRICS: "1"
BD_DISABLE_EVENT_FLUSH: "1"
# Phase B report-only budget for T1/T2 (tools/bazel/critpath.py).
BAZEL_BUDGET_SECONDS: "420"
jobs:
bazel-test:
name: Bazel / test (advisory)
# Remote-execution runs use the org's Blacksmith pool (the farm's edge
# admits it; GitHub-hosted runners are refused) and do not count against
# GitHub's concurrent-job cap. Fork PRs and rbe=off have no remote
# executor and build everything locally, so they get the larger
# GitHub-hosted runner.
runs-on: ${{ (inputs.rbe || 'on') != 'off' && github.event.pull_request.head.repo.fork != true && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }}
# Remote execution goes to rbe-west (instance "oss", the Blacksmith-backed
# worker pool shared with gascity), switched on by the repo variable
# RBE_WEST_WORKERS. Until it is set, only fork PRs and rbe=off dispatches
# run (locally); same-repo runs are skipped rather than failing on an
# unreachable executor.
if: ${{ vars.RBE_WEST_WORKERS == 'true' || inputs.rbe == 'off' || github.event.pull_request.head.repo.fork == true }}
timeout-minutes: 100
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
# Go is for tools/bazel/equivalence.py's `go list` and for make; Bazel
# uses its own hermetic SDK.
- name: Set up Go
id: setup-go
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: 'go.mod'
cache: false
# Read-only: main.yml's build-artifacts job owns this cache.
- name: Restore Go module cache
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/go/pkg/mod
key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }}
restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-
- name: Set up Bazel
id: bazel
uses: ./.github/actions/setup-bazel
env:
BAZEL_REMOTE_EXECUTOR: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_WEST_EXECUTOR || '' }}
RBE_TLS_CERT: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_CERT || '' }}
RBE_TLS_KEY: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_KEY || '' }}
RBE_TLS_CA: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_CA || '' }}
RBE_INSTANCE: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && 'oss' || '' }}
# T1: remote + restored repository cache; T2: remote, cold runner; local:
# no remote execution (reported, never budgeted).
- name: Measurement tier
id: tier
env:
REMOTE: ${{ steps.bazel.outputs.remote }}
RESTORED: ${{ steps.bazel.outputs.cache-restored }}
run: |
if [ "$REMOTE" = "true" ] && [ "$RESTORED" = "true" ]; then tier=T1
elif [ "$REMOTE" = "true" ]; then tier=T2
else tier=local; fi
echo "tier=$tier" >> "$GITHUB_OUTPUT"
{
echo "### Bazel / test (advisory)"
echo
echo "Tier **$tier**: remote execution $REMOTE, runner cache restored $RESTORED."
echo
} >> "$GITHUB_STEP_SUMMARY"
# --profile and --build_event_json_file are command options, so they go
# after `test` (a startup position fails).
- name: bazel test //... --config=ci
id: test
timeout-minutes: 90
run: |
set -o pipefail
start=$(date +%s)
rc=0
bazel test //... --config=ci \
--profile="$RUNNER_TEMP/bazel-profile.json" \
--build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \
2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$?
echo "bazel test: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY"
echo >> "$GITHUB_STEP_SUMMARY"
exit "$rc"
# pr.yml's build-artifacts job, built by Bazel: the same artifact name
# and layout (bd-linux-gms-pure, SHA256SUMS, build-manifest.txt), so its
# consumers (PR Core, domain+uow, the package gates) can switch to it.
# None does yet. The script documents which binary and how it differs
# from `go build`'s.
- name: Package bd (ci-build-artifacts layout)
id: bd-artifact
if: ${{ always() && steps.test.outcome != 'skipped' }}
run: ./scripts/ci/package-bazel-bd.sh "$RUNNER_TEMP/bd-artifacts"
- name: Upload build artifacts
if: ${{ always() && steps.bd-artifact.outcome == 'success' }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: ci-build-artifacts
path: ${{ runner.temp }}/bd-artifacts/
retention-days: 1
if-no-files-found: error
- name: Critical-path report
if: ${{ always() && steps.test.outcome != 'skipped' }}
env:
TIER: ${{ steps.tier.outputs.tier }}
run: |
budget=()
if [ "$TIER" != "local" ]; then budget=(--budget "$BAZEL_BUDGET_SECONDS"); fi
python3 tools/bazel/critpath.py "$RUNNER_TEMP/bazel-profile.json" \
--tier "$TIER" --log "$RUNNER_TEMP/bazel-test.log" "${budget[@]}"
# nightly.yml's PR Core `go test -json` on the same commit.
- name: Download go-test results
if: ${{ always() && inputs.go-test-json-artifact != '' && steps.test.outcome != 'skipped' }}
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
name: ${{ inputs.go-test-json-artifact }}
path: ${{ runner.temp }}/go-test-json
# Fails on any Go test that `go test` runs and Bazel did not (tag filter,
# manual, missing target, empty shard) unless it is allowlisted. With
# go test results (nightly) it also fails on tests that passed under go
# test and were skipped under Bazel; a PR run cannot see those.
# It reads test.xml from bazel-testlogs, which --config=integration
# shares with --config=ci (same output directory): if a job ever runs
# both lanes, run each lane's equivalence right after its own
# `bazel test`, before the other lane overwrites the logs.
- name: Go test equivalence
if: ${{ always() && steps.test.outcome != 'skipped' }}
timeout-minutes: 15
env:
DETAILS: ${{ inputs.details && '--details' || '' }}
GO_TEST_JSON: ${{ inputs.go-test-json-artifact != '' && format('{0}/go-test-json/go-test.json', runner.temp) || '' }}
run: |
args=()
if [ -n "$DETAILS" ]; then args+=("$DETAILS"); fi
if [ -n "$GO_TEST_JSON" ]; then args+=(--go-test-json "$GO_TEST_JSON"); fi
python3 tools/bazel/equivalence.py --bep "$RUNNER_TEMP/bazel-bep.json" "${args[@]}"
# Runs last: it rewrites BUILD files in the checkout. main moves BUILD
# inputs (imports, go.mod) without touching Bazel, so drift shows up here
# first.
- name: BUILD files in sync (gazelle, go_srcs, MODULE.bazel)
if: ${{ always() && steps.bazel.outcome == 'success' }}
timeout-minutes: 15
run: |
start=$(date +%s)
make bazel-sync-check
make bazel-sync
drift="$(git status --porcelain --untracked-files=all)"
echo "BUILD sync check: $(( $(date +%s) - start ))s" >> "$GITHUB_STEP_SUMMARY"
if [ -n "$drift" ]; then
echo "::error title=Bazel BUILD files out of sync::run 'make bazel-sync' and commit the result"
echo "$drift"
git diff | head -200
echo "BUILD files **out of sync**; run \`make bazel-sync\` and commit the result." >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
- name: Upload test logs
if: ${{ failure() && steps.test.outcome == 'failure' }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: bazel-testlogs
path: |
bazel-testlogs/**/test.log
bazel-testlogs/**/test.xml
if-no-files-found: ignore
retention-days: 7
# Only a push to main that missed the exact key writes the runner cache.
# Its content (--repository_cache and Bazelisk's download, never the rc
# or credentials) is fixed by .bazelversion and MODULE.bazel.lock, so one
# entry per lock is enough and later pushes do not churn the quota.
- name: Save Bazel runner cache
if: ${{ always() && github.event_name == 'push' && github.ref == 'refs/heads/main' && steps.bazel.outcome == 'success' && steps.bazel.outputs.cache-hit != 'true' }}
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ runner.temp }}/bazel-ci-cache
key: bazel-repo-v2-${{ runner.os }}-${{ hashFiles('.bazelversion', 'MODULE.bazel.lock') }}
# pr.yml's check-cmd-bd-puregeo-tests job ("Check pure-Go and js/wasm
# boundaries (CGO_ENABLED=0)") with Bazel-built binaries: --config=pure
# builds bd and the job's three pure test binaries and runs its -short
# cmd/bd subset (PURE_CMD_BD_TESTS, kept equal to the job's -run selector by
# scripts/ci_workflow_test.go); --config=js-wasm builds the hooks test for
# js/wasm, which the Go SDK's go_js_wasm_exec runs under node with the job's
# exact pass-count guard.
bazel-pure:
name: Bazel / pure-Go and js/wasm (advisory)
# Same runner, rbe-west gate and skip rule as bazel-test.
runs-on: ${{ (inputs.rbe || 'on') != 'off' && github.event.pull_request.head.repo.fork != true && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }}
if: ${{ vars.RBE_WEST_WORKERS == 'true' || inputs.rbe == 'off' || github.event.pull_request.head.repo.fork == true }}
timeout-minutes: 90
env:
PURE_CMD_BD_TESTS: '^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir|NocgoFactoriesHonorSharedServer|NocgoNewDoltStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoNewReadOnlyStoreFromConfig_ErrorSuggestsCorrectFlag)'
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
# Only for go_js_wasm_exec and wasm_exec_node.js, which must match the
# Go version Bazel compiles with (go.mod's; policy-tested).
- name: Set up Go
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: 'go.mod'
cache: false
- name: Set up Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6
with:
node-version: '24'
- name: Set up Bazel
id: bazel
uses: ./.github/actions/setup-bazel
env:
BAZEL_REMOTE_EXECUTOR: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_WEST_EXECUTOR || '' }}
RBE_TLS_CERT: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_CERT || '' }}
RBE_TLS_KEY: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_KEY || '' }}
RBE_TLS_CA: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_CA || '' }}
RBE_INSTANCE: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && 'oss' || '' }}
- name: Run js/wasm hook boundary
run: |
set -euo pipefail
bazel build --config=js-wasm //internal/hooks:hooks_test
bin="$(bazel cquery --config=js-wasm --output=files //internal/hooks:hooks_test)"
cd internal/hooks
# Same selector and count guard as pr.yml's job.
test_status=0
# wasm_exec.js caps argv+env size; the Blacksmith runner's environment
# exceeds it, so run with only what the hook test needs.
wasm_exec="$(go env GOROOT)/lib/wasm/go_js_wasm_exec"
test_output="$(env -i PATH="$PATH" HOME="$HOME" TMPDIR="$RUNNER_TEMP" \
BD_DISABLE_METRICS=1 BD_DISABLE_EVENT_FLUSH=1 \
"$wasm_exec" "$GITHUB_WORKSPACE/$bin" \
-test.run '^TestRunHookReportsUnsupportedExecution$' \
-test.v 2>&1)" || test_status=$?
printf '%s\n' "$test_output"
(( test_status == 0 )) || exit "$test_status"
run_count=0
pass_count=0
nonpass_count=0
pass_pattern='^--- PASS: TestRunHookReportsUnsupportedExecution \([0-9.]+s\)$'
nonpass_pattern='^[[:space:]]*--- (FAIL|SKIP): '
while IFS= read -r line; do
if [[ "$line" == "=== RUN TestRunHookReportsUnsupportedExecution" ]]; then
((run_count += 1))
fi
if [[ "$line" =~ $pass_pattern ]]; then
((pass_count += 1))
fi
if [[ "$line" =~ $nonpass_pattern ]]; then
((nonpass_count += 1))
fi
done <<< "$test_output"
if (( run_count != 1 || pass_count != 1 || nonpass_count != 0 )); then
printf 'expected one exact js/wasm hook test run and pass with no failures or skips; got runs=%s passes=%s nonpasses=%s\n' \
"$run_count" "$pass_count" "$nonpass_count" >&2
exit 1
fi
- name: Build cmd/bd and pure-Go test binaries (--config=pure)
run: >-
bazel build --config=pure
//cmd/bd:bd
//cmd/bd:bd_test
//internal/storage/embeddeddolt:embeddeddolt_test
//internal/tracker:tracker_test
# `go build` with CGO_ENABLED=0 rejects a pure binary whose imports reach
# dolt's cgo-only store/nbs (gozstd). Bazel compiles gozstd's pure stubs
# instead, whose init panics (third_party/patches/gozstd_nocgo.patch), so
# the check happens here: every pure artifact is started once. bd_test
# starts in the next step. -test.run=^$ runs no test (TestMain still
# runs; BEADS_TEST_SKIP=dolt keeps tracker's from starting a container).
- name: Start every pure-Go artifact (gozstd contamination check)
run: |
set -euo pipefail
bazel run --config=pure //cmd/bd:bd -- version
bazel test --config=pure \
//internal/storage/embeddeddolt:embeddeddolt_test \
//internal/tracker:tracker_test \
--test_sharding_strategy=disabled \
'--test_arg=-test.run=^$' \
--test_env=BEADS_TEST_SKIP=dolt
# One unsharded process, like `go test -run`; the test.xml check keeps a
# selector that matches nothing from passing.
- name: Run pure-Go cmd/bd test subset (--config=pure)
run: |
set -euo pipefail
bazel test --config=pure //cmd/bd:bd_test \
--test_sharding_strategy=disabled \
"--test_arg=-test.run=$PURE_CMD_BD_TESTS"
n="$(grep -c '<testcase ' bazel-testlogs/cmd/bd/bd_test/test.xml || true)"
echo "pure cmd/bd subset: $n test cases"
(( n > 0 ))
# pr-risk.yml's embedded-Dolt tier ("Test (Embedded Dolt Cmd N/20)",
# "Test (Embedded Dolt Storage N/5)", "Test (Embedded Dolt Conformance -
# core/audit)") as --config=embedded: the embedded-tagged variants of
# //cmd/bd:bd_test and //internal/storage/embeddeddolt:embeddeddolt_test.
# The shard variants run the jobs' own shard scripts and manifests, so Bazel
# shard k runs exactly the tests of job k. A job of its own rather than a
# second invocation in bazel-test: the tier's long race shards stay off
# bazel-test's critical path and budget, and it compiles nothing bazel-test
# does not (same race configuration, shared through the remote cache).
bazel-embedded:
name: Bazel / embedded Dolt tier (advisory)
# Remote execution only: unlike the other jobs, no fork PR or rbe=off
# run. Locally the tier is 27 race test processes (~7-10k test-seconds)
# on one GitHub-hosted runner, an hour or more per run on the pool whose
# job cap this track exists to relieve, and it proves nothing pr-risk.yml's
# embedded jobs (which do run for forks) do not. The runs-on expression is
# the shared one and always picks the Blacksmith runner under this if.
runs-on: ${{ (inputs.rbe || 'on') != 'off' && github.event.pull_request.head.repo.fork != true && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }}
if: ${{ vars.RBE_WEST_WORKERS == 'true' && (inputs.rbe || 'on') != 'off' && github.event.pull_request.head.repo.fork != true }}
timeout-minutes: 60
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
- name: Set up Bazel
id: bazel
uses: ./.github/actions/setup-bazel
env:
BAZEL_REMOTE_EXECUTOR: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_WEST_EXECUTOR || '' }}
RBE_TLS_CERT: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_CERT || '' }}
RBE_TLS_KEY: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_KEY || '' }}
RBE_TLS_CA: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_CA || '' }}
RBE_INSTANCE: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && 'oss' || '' }}
# --build_event_json_file is a command option, so it goes after `test`.
- name: bazel test //... --config=embedded
id: test
timeout-minutes: 50
run: |
set -o pipefail
start=$(date +%s)
rc=0
bazel test //... --config=embedded \
--build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \
2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$?
echo "bazel test --config=embedded: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY"
exit "$rc"
# A shard the manifest assigns nothing, or a selector that matches
# nothing, exits 0: fail if any target's or shard's test.xml lists no
# tests (or is missing).
- name: Every target and shard ran tests
if: ${{ always() && steps.test.outcome != 'skipped' }}
run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json"
- name: Upload test logs
if: ${{ failure() && steps.test.outcome != 'skipped' }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: bazel-embedded-testlogs
path: |
bazel-testlogs/**/test.log
bazel-testlogs/**/test.xml
if-no-files-found: ignore
retention-days: 7
# pr.yml's container-backed jobs, "Test (storage domain + uow)" and
# "Contract corpus", as Bazel's dolt-server lane: --config=doltserver runs
# the dolt-server variants (domain/..., uow in 8 shards, tracker/...,
# doctor/fix, protocol in 8 shards), each of which starts its own dolt
# sql-server from the pinned dolt, so they execute on the remote workers
# like any other test (locally for forks and rbe=off). The docker lane
# (--config=docker: the same suites against testcontainers on this runner,
# no-remote-exec) stays the A/B control: dispatch with dolt-lane=docker.
bazel-doltserver:
name: Bazel / dolt-server lane (advisory)
# Same runner, rbe-west gate and skip rule as bazel-test.
runs-on: ${{ (inputs.rbe || 'on') != 'off' && github.event.pull_request.head.repo.fork != true && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }}
if: ${{ vars.RBE_WEST_WORKERS == 'true' || inputs.rbe == 'off' || github.event.pull_request.head.repo.fork == true }}
timeout-minutes: 90
env:
BAZEL_DOLT_LANE: ${{ inputs.dolt-lane || 'doltserver' }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
- name: Set up Bazel
id: bazel
uses: ./.github/actions/setup-bazel
env:
BAZEL_REMOTE_EXECUTOR: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_WEST_EXECUTOR || '' }}
RBE_TLS_CERT: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_CERT || '' }}
RBE_TLS_KEY: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_KEY || '' }}
RBE_TLS_CA: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && secrets.RBE_TLS_CA || '' }}
RBE_INSTANCE: ${{ (inputs.rbe || 'on') != 'off' && vars.RBE_WEST_WORKERS == 'true' && 'oss' || '' }}
# Docker lane only (the A/B control), as in the mirrored jobs; its
# tests fail rather than skip without the image.
- name: Pull Dolt sql-server image
if: ${{ env.BAZEL_DOLT_LANE == 'docker' }}
run: ./scripts/ci/pull-dolt-image.sh
- name: bazel test //... --config=doltserver
id: test
timeout-minutes: 80
run: |
set -o pipefail
start=$(date +%s)
rc=0
bazel test //... "--config=$BAZEL_DOLT_LANE" 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$?
echo "bazel test --config=$BAZEL_DOLT_LANE: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY"
exit "$rc"
- name: Upload test logs
if: ${{ failure() && steps.test.outcome == 'failure' }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: bazel-${{ env.BAZEL_DOLT_LANE }}-testlogs
path: |
bazel-testlogs/**/test.log
bazel-testlogs/**/test.xml
if-no-files-found: ignore
retention-days: 7

View file

@ -445,8 +445,8 @@ jobs:
run: go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1
# No BD_LINT_NEW_FROM_MERGE_BASE here: the push lane sweeps the whole
# tree, including the GOOS=windows cross-lint. pr.yml scopes the same
# wrapper to the PR's own diff.
# tree, including the GOOS=windows and GOOS=darwin cross-lints. pr.yml
# scopes the same wrapper to the PR's own diff.
- name: Run PR lint wrapper
run: make ci-pr-lint

View file

@ -26,6 +26,7 @@ on:
- nix
- mcp-package
- npm-package
- bazel
smoke_version:
description: "Optional previous release for cross-version-smoke"
required: false
@ -38,12 +39,75 @@ env:
jobs:
measure:
name: Measure selected CI suite
if: ${{ github.event_name == 'workflow_dispatch' && inputs.suite != 'full-test' }}
if: ${{ github.event_name == 'workflow_dispatch' && inputs.suite != 'full-test' && inputs.suite != 'bazel' }}
uses: ./.github/workflows/ci-measurements.yml
with:
suite: ${{ inputs.suite }}
smoke_version: ${{ inputs.smoke_version }}
# PR Core's go test (scripts/ci/pr-core.sh) with -json output, so the Bazel
# job below can compare per-test status: a test that passes here and skips
# under Bazel fails the equivalence check unless allowlisted.
bazel-go-test-json:
name: Bazel equivalence (go-test side)
if: ${{ github.event_name == 'schedule' || inputs.suite == 'bazel' }}
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
- name: Set up Go
id: setup-go
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version-file: 'go.mod'
cache: false
- name: Restore Go module cache
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/go/pkg/mod
key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }}
restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-
- name: Install Dolt
run: ./scripts/ci/install-dolt.sh
- name: Configure Git and Dolt identity
run: |
git config --global user.name "CI Bot"
git config --global user.email "ci@beads.test"
dolt config --global --add user.name "CI Bot"
dolt config --global --add user.email "ci@beads.test"
- name: Run PR core go-test (JSON)
env:
BEADS_PR_CORE_GO_TEST_JSON: ${{ runner.temp }}/go-test.json
run: make ci-pr-core
# Uploaded even when tests fail: the status comparison still applies.
- name: Upload go-test results
if: ${{ always() }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
name: bazel-equivalence-go-test-json
path: ${{ runner.temp }}/go-test.json
if-no-files-found: error
retention-days: 7
# Bazel lane with the full go test equivalence listing and the skip-parity
# check (advisory, like the PR run). Remote execution when the repo secrets
# are present.
bazel:
name: Bazel equivalence (full detail)
needs: bazel-go-test-json
if: ${{ !cancelled() && (github.event_name == 'schedule' || inputs.suite == 'bazel') }}
uses: ./.github/workflows/bazel.yml
secrets: inherit
with:
details: true
go-test-json-artifact: bazel-equivalence-go-test-json
full-test:
name: Full Test Suite
if: ${{ github.event_name == 'schedule' || inputs.suite == 'full-test' }}

View file

@ -486,6 +486,13 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- os: ubuntu-latest
expected_goos: linux
- os: macos-latest
expected_goos: darwin
- os: windows-latest
expected_goos: windows
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
@ -507,6 +514,12 @@ jobs:
shell: bash
run: go test '-tags=integration,gms_pure_go' -count=1 -run '^TestPRPreflight' ./scripts
- name: Check native user config diagnostics
shell: bash
env:
CGO_ENABLED: "0"
run: bash scripts/ci/test-user-config-diagnostic.sh '${{ matrix.expected_goos }}'
- name: Exercise test.sh prebuilt binary path
shell: bash
run: go test '-tags=gms_pure_go' -count=1 -run '^TestTestScriptPrebuiltBinaryContract$' ./scripts
@ -877,6 +890,15 @@ jobs:
sha256sum -c SHA256SUMS
chmod +x bd-linux-gms-pure
# The only lane with both the image and the pinned dolt CLI: compares
# the container and the local test server backend against the one
# checked-in fingerprint, so an image bump cannot drift the container
# side unnoticed. REQUIRE makes the container half fail, not skip.
- name: Test Dolt server fingerprint (container + local)
env:
BEADS_TEST_REQUIRE_DOLT_CONTAINER: "1"
run: go test -tags gms_pure_go -count=1 -timeout 5m -v -run '^TestDoltServerFingerprint$' ./internal/testutil/
- name: Test domain + uow + tracker
env:
BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure
@ -1207,14 +1229,63 @@ jobs:
$proofRoot = Join-Path $env:RUNNER_TEMP ("make install proof " + [guid]::NewGuid().ToString('N'))
$fixtureRepo = Join-Path $proofRoot 'fixture repo'
$buildDir = Join-Path $fixtureRepo 'build output'
$fakeGoDir = Join-Path $proofRoot 'fake go bin'
$installRoot = Join-Path $proofRoot 'install sandbox'
$installProfile = Join-Path $installRoot 'profile with spaces'
New-Item -ItemType Directory -Force -Path $buildDir, $installProfile | Out-Null
New-Item -ItemType Directory -Force -Path $buildDir, $fakeGoDir, $installProfile | Out-Null
Copy-Item -LiteralPath (Join-Path $env:GITHUB_WORKSPACE 'Makefile') -Destination $fixtureRepo
Copy-Item -LiteralPath (Join-Path $env:GITHUB_WORKSPACE 'go.mod') -Destination $fixtureRepo
$sentinel = Join-Path $buildDir 'bd.exe'
Set-Content -LiteralPath $sentinel -Encoding ascii -NoNewline -Value 'native-install-sentinel'
$fakeGo = Join-Path $fakeGoDir 'go'
$fakeGoScript = @'
#!/usr/bin/env bash
set -euo pipefail
[[ "${1-}" == build ]] || exit 91
shift
output=
while (($#)); do
case "$1" in
-o)
[[ $# -ge 2 ]] || exit 92
output=$2
shift 2
;;
*) shift ;;
esac
done
[[ "$(cygpath -aw "$output")" == "$(cygpath -aw "$BEADS_FAKE_GO_EXPECTED_OUTPUT")" ]] || {
printf 'fake go received output %q, expected %q\n' "$output" "$BEADS_FAKE_GO_EXPECTED_OUTPUT" >&2
exit 93
}
printf native-build-sentinel > "$(cygpath -u "$output")"
'@
if (-not $fakeGoScript.StartsWith('#!', [StringComparison]::Ordinal)) {
throw 'Fake Go shebang must start at byte zero after YAML extraction'
}
[IO.File]::WriteAllText(
$fakeGo,
($fakeGoScript -replace "`r`n", "`n"),
[Text.UTF8Encoding]::new($false)
)
$priorPath = $env:PATH
Push-Location $fixtureRepo
try {
$env:BEADS_FAKE_GO_EXPECTED_OUTPUT = $sentinel
$env:PATH = "$fakeGoDir;$priorPath"
& $make --no-print-directory "BUILD_DIR=$buildDir" GIT_BUILD=build-proof CC=proof build
if ($LASTEXITCODE -ne 0) {
throw 'Native Make build failed for a spaced BUILD_DIR'
}
} finally {
$env:PATH = $priorPath
Remove-Item -LiteralPath Env:BEADS_FAKE_GO_EXPECTED_OUTPUT
Pop-Location
}
if (-not (Test-Path -LiteralPath $sentinel -PathType Leaf)) {
throw "Native Make did not build bd.exe at the exact spaced destination '$sentinel'"
}
$sentinelHash = (Get-FileHash -LiteralPath $sentinel -Algorithm SHA256).Hash
& $git -C $fixtureRepo init --quiet

View file

@ -23,6 +23,8 @@ on:
- 'cmd/bd/proxied_server*.go'
- 'cmd/bd/dolt*.go'
- 'cmd/bd/*_proxied_server.go'
- 'cmd/bd/backup*.go'
- 'cmd/bd/purge*.go'
- 'cmd/bd/*managed_local*'
- 'cmd/bd/proxied_outage_reconnect_integration_test.go'
- 'cmd/bd/db_proxy_child.go'
@ -49,6 +51,8 @@ on:
- 'cmd/bd/proxied_server*.go'
- 'cmd/bd/dolt*.go'
- 'cmd/bd/*_proxied_server.go'
- 'cmd/bd/backup*.go'
- 'cmd/bd/purge*.go'
- 'cmd/bd/*managed_local*'
- 'cmd/bd/proxied_outage_reconnect_integration_test.go'
- 'cmd/bd/db_proxy_child.go'
@ -125,7 +129,11 @@ jobs:
TestManagedLocalProxiedDoltStartRefusesOverLiveProxy \
TestManagedLocalProxiedDoltStartRefusesWithProxyDown \
TestManagedLocalProxiedDoltLifecycleLeavesOtherTopologiesAlone \
TestManagedLocalProxiedTrackerUOWConformance
TestManagedLocalProxiedTrackerUOWConformance \
TestManagedLocalProxiedBackupRoundTrip \
TestManagedLocalProxiedAutoBackup \
TestManagedLocalProxiedBackupRestoreRefusedWhileAttached \
TestManagedLocalProxiedPurgeWispsPlaneRetention
do
if ! grep -qx "$test_name" <<<"$listed"; then
echo "managed-local lifecycle test $test_name is missing; got: '$listed'" >&2

View file

@ -77,7 +77,7 @@ jobs:
needs: detect-regression-need
if: needs.detect-regression-need.outputs.run_regression == 'true'
runs-on: ubuntu-latest
timeout-minutes: 25
timeout-minutes: 45
steps:
- name: Show regression trigger reason
run: echo "${{ needs.detect-regression-need.outputs.reason }}"
@ -117,5 +117,17 @@ jobs:
path: ~/.cache/beads-regression
key: regression-baseline-${{ hashFiles('tests/regression/BASELINE_VERSION') }}
# Passing-side wall time already overlapped the go deadline below, so
# ordinary GitHub-runner speed variance -- not a real hang -- was
# tripping this step. Measured over the 100 most recent Regression
# Tests runs (2026-09-03/04, ~29.5h): p50 593s (~10m), max passing
# 1189s (~19.8m) against the prior 1200s (20m) cap, and 5 runs in that
# window hit the identical `panic: test timed out after 20m0s` with
# zero `--- FAIL` lines -- a budget defect, not a regression. Match
# main.yml's Test job fix for the same failure mode: raise the go
# deadline to cover the observed slowdown and keep it inside a larger
# finite job budget (see timeout-minutes above), so the go deadline
# fires first and prints goroutine stacks -- a REAL hang stays
# diagnosable instead of being cut off by a bare job kill.
- name: Run regression tests
run: go test -tags=regression,gms_pure_go -timeout=20m -v ./tests/regression/...
run: go test -tags=regression,gms_pure_go -timeout=40m -v ./tests/regression/...

View file

@ -2,7 +2,11 @@ load("@gazelle//:def.bzl", "gazelle")
load("@rules_go//go:def.bzl", "go_library", "go_test")
# gazelle:prefix github.com/steveyegge/beads
# gazelle:build_tags gms_pure_go
# gazelle drops files whose build constraints name a tag it does not know.
# integration is listed so the `//go:build integration` test files (and
# //internal/testutil/integration) are in srcs and deps; rules_go filters them
# out of every build except --config=integration (.bazelrc), which sets the tag.
# gazelle:build_tags gms_pure_go,integration
# integrations/ holds no Go; it stays visible to Bazel (not in .bazelignore)
# only so //test/docsync can declare the markdown its link checks resolve.
# gazelle:exclude integrations
@ -56,11 +60,41 @@ filegroup(
visibility = ["//test/docsync:__pkg__"],
)
# //scripts:scripts_test dolt pin tests (ci_workflow_test.go): the workflows
# must install the pinned dolt.
# //scripts:scripts_test workflow-policy and CI-script tests: the whole
# .github tree (workflows, composite actions, CI helper scripts) they parse
# and run. The one whole-tree input the plan allows (it is small).
filegroup(
name = "workflow_files",
srcs = glob([".github/workflows/*.yml"]),
srcs = glob([".github/**"]),
visibility = ["//scripts:__pkg__"],
)
# PR Risk's embedded-Dolt shard scripts and their committed manifests. The
# embedded variants (//cmd/bd:bd_embedded_test,
# //internal/storage/embeddeddolt:embeddeddolt_embedded_test) run them through
# tools/bazel/go_test_manifest_shard.sh, so each Bazel shard runs exactly the
# tests of the CI job with the same shard number.
exports_files(
[
".github/scripts/embedded-cmd-test-shards.txt",
".github/scripts/embedded-storage-test-shard.sh",
".github/scripts/embedded-storage-test-shards.txt",
".github/scripts/embedded-test-shard.sh",
],
visibility = [
"//cmd/bd:__pkg__",
"//internal/storage/embeddeddolt:__pkg__",
],
)
# //scripts:scripts_test runs scripts/test.sh, which sources the canonical
# build flags and reads the known-broken-test skip list.
filegroup(
name = "test_sh_files",
srcs = [
".buildflags",
".test-skip",
],
visibility = ["//scripts:__pkg__"],
)

View file

@ -190,6 +190,59 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- **Auto-backup runs on a managed-local proxied-server workspace.** The
proxied arm of the post-command hook now calls auto-backup, so an explicit
`backup.enabled=true` (or `BD_BACKUP_ENABLED=1`) takes the same throttled,
change-detected Dolt-native backup into `.beads/backup` it takes in direct
mode, through the proxied provider's non-transactional seam. Before this the
opt-in was inert on every proxied workspace and `bd backup status` said so.
The default stays OFF, as on every server-mode workspace: many bd clients
share one server. It is honored exactly where `bd backup sync` is — a
proxied server bd started itself — and stays inert on an external or
team-server topology, strict `--readonly`, `bd serve`, a preview
(`--dry-run`/`--inspect`) and a migration freeze. A command that opened no
provider (e.g. `bd dolt stop`) backs nothing up rather than relaunching the
server to do so. `bd backup status` now reports the proxied default as
`auto: off in proxied-server mode; set backup.enabled=true to opt in`.
- **Auto-backup and `bd backup sync` no longer overlap.** A per-workspace
backup lock (`.beads/backup.lock`) serializes them in every mode:
auto-backup skips when another backup holds it, and `bd backup sync` waits
up to 5 seconds (the bound restore uses for the workspace gate) and then
fails with "another backup is running for this workspace".
- **`bd purge` covers an orchestrator's wisp retention sweep.** Three changes
that together let `bd purge --wisps-plane --older-than 168h --force` replace
a raw `DELETE FROM wisps` retention step:
- **Live-dependent protection, always on.** `bd purge` no longer deletes a
closed bead that a live bead depends on through a `parent-child`,
`tracks` or `blocks` edge — a closed molecule root whose step is still
open, a closed wisp a live convoy tracks, a closed blocker of live work.
"Live" is any status that is not done, including custom statuses. The
held-back count is reported as `live_dependent_skipped` in `--json` (and
on its own line in text output). The protection is on the role
(`issueops.SweepRequest.ProtectLiveDependents`); `bd prune` does not ask
for it.
- **`--wisps-plane`** selects every closed row stored in the wisps table,
including `--no-history` beads, which the default ephemeral selection
leaves to `bd prune`. It works on embedded, server and proxied
workspaces, and because it reaches durable-tier rows it requires
`--older-than` or `--pattern`, like `bd prune`
(`issueops.SweepWispsPlane`).
- **`--older-than` accepts hour and finer durations** (`36h`, `168h`,
`90m`, down to `1s`) on both `bd purge` and `bd prune`. Day values (`7`,
`7d`, `2w`) are unchanged. An `Nh` value used to be converted to whole
days: floored above a day (`36h` swept rows only 24 hours old) and
rounded UP to one day below it (`12h` kept everything younger than 24
hours). Both are now taken exactly, so `bd prune --older-than 12h` now
deletes rows closed 12–24 hours ago that it used to keep. A value too
large to represent (e.g. `213504d`) is now refused instead of silently
wrapping to a tiny age.
- **`--limit N`** caps one `bd purge` run at N beads, oldest-closed first
(`issueops.SweepRequest.Limit`), so a large backlog drains in bounded
transactions; `--json` then adds `remaining` and `has_more`. Loop while
`has_more` is true.
- **`bd backup` works on a proxied-server workspace bd runs the Dolt server
for.** `bd backup init`, `sync`, `remove`, `status` and `restore` are routed
over the proxied provider; before this, a proxied workspace — the default
@ -220,6 +273,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
server topology. Wiring an auto-backup hook through the new routes is left to
a later slice.
- **`bd` says when it resolved a partial ID instead of matching one exactly.**
([#6302](https://github.com/gastownhall/beads/pull/6302)) ID resolution tries an exact match first and only then falls back to
leading-prefix abbreviation (`a3f8` -> `a3f8e9`). Ambiguity was already a hard
error naming every candidate, but a *sole* abbreviation match was returned
silently, so a caller could act on an issue it had not named without any
signal. bd now writes a one-line note to stderr identifying both the input and
what it resolved to. The case that motivates it: a hierarchical child is a
valid leading-prefix match for its own parent's ID, so after a parent is
renamed or deleted the vacated ID quietly resolves to the child. Note this
covers **every** leading-prefix abbreviation, not only that shape, so the
ordinary documented `a3f8` -> `bd-a3f8e9` path now emits a line too, once per
resolved argument. That breadth is deliberate: a `.`-boundary rule would fix
parent/child but leave `bd-x.1` -> `bd-x.11` just as silent. stdout and
`--json` are unchanged; silence it with `--quiet` or
`BD_NO_PARTIAL_ID_NOTICE=1`.
- **`bd count` supports repeatable `--metadata-field key=value` filters**
([#6023](https://github.com/gastownhall/beads/issues/6023)), so callers can
count the same metadata-scoped set `bd list` returns without fetching every
@ -310,6 +379,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- **`bd purge` keeps closed beads a live bead depends on.** A closed bead
that a not-done bead depends on through `parent-child`, `tracks` or
`blocks` is no longer purged (reported as `live_dependent_skipped`), so a
purge that used to delete a closed molecule root under a live step now
leaves it. See the `--wisps-plane` entry under Added.
- **Proxied-server refusals now say *why* they refuse.** The JSON a refused
command prints gains a `reason` field next to the existing `code`, `error`
and `mutates`: `design` for a refusal that is expected to stay (shared

View file

@ -43,6 +43,13 @@ go_deps.module_override(
patches = ["//third_party/patches:dolt_go_serial.patch"],
path = "github.com/dolthub/dolt/go",
)
# Pure-Go stubs so --config=pure can compile dolt's store/nbs (see the patch).
go_deps.module_override(
patch_strip = 1,
patches = ["//third_party/patches:gozstd_nocgo.patch"],
path = "github.com/dolthub/gozstd",
)
use_repo(go_deps, "com_github_anthropics_anthropic_sdk_go", "com_github_burntsushi_toml", "com_github_cenkalti_backoff_v4", "com_github_charmbracelet_x_ansi", "com_github_data_dog_go_sqlmock", "com_github_dolthub_dolt_go", "com_github_dolthub_driver_v2", "com_github_dolthub_eventkit", "com_github_dolthub_go_mysql_server", "com_github_go_sql_driver_mysql", "com_github_google_uuid", "com_github_gowebpki_jcs", "com_github_invopop_jsonschema", "com_github_johanneskaufmann_html_to_markdown_v2", "com_github_mattn_go_sqlite3", "com_github_microcosm_cc_bluemonday", "com_github_oapi_codegen_oapi_codegen_v2", "com_github_olebedev_when", "com_github_spf13_cobra", "com_github_spf13_pflag", "com_github_spf13_viper", "com_github_stretchr_testify", "com_github_subosito_gotenv", "com_github_testcontainers_testcontainers_go", "com_github_testcontainers_testcontainers_go_modules_dolt", "com_github_yuin_goldmark", "in_gopkg_yaml_v3", "io_opentelemetry_go_otel", "io_opentelemetry_go_otel_exporters_otlp_otlpmetric_otlpmetrichttp", "io_opentelemetry_go_otel_exporters_stdout_stdoutmetric", "io_opentelemetry_go_otel_exporters_stdout_stdouttrace", "io_opentelemetry_go_otel_metric", "io_opentelemetry_go_otel_sdk", "io_opentelemetry_go_otel_sdk_metric", "io_opentelemetry_go_otel_trace", "io_rsc_script", "land_charm_glamour_v2", "land_charm_huh_v2", "land_charm_lipgloss_v2", "org_golang_x_mod", "org_golang_x_net", "org_golang_x_sync", "org_golang_x_sys", "org_golang_x_term")
# Hermetic Dolt CLI for tests (tools/bazel/dolt.bzl pins the release to

View file

@ -52,6 +52,7 @@ endif
all: build
BUILD_DIR := .
BD_BUILD_OUTPUT := $(BUILD_DIR)/bd$(if $(filter Windows_NT,$(OS)),.exe)
GIT_BUILD := $(shell git rev-parse --short HEAD)
ifeq ($(OS),Windows_NT)
INSTALL_DIR := $(USERPROFILE)/.local/bin
@ -104,21 +105,21 @@ build:
ifeq ($(OS),Windows_NT)
@if [ -n "$$CC" ]; then \
echo "Using CC=$$CC"; \
go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \
go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \
elif command -v gcc >/dev/null 2>&1; then \
CC=gcc go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \
CC=gcc go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \
elif command -v clang >/dev/null 2>&1 && clang -dumpmachine 2>/dev/null | grep -qi 'windows.*gnu'; then \
CC=clang go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \
CC=clang go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \
else \
for bin in $(WINDOWS_CGO_BINS); do \
if [ -x "$$bin/gcc.exe" ]; then \
echo "Using Windows CGO gcc from $$bin"; \
PATH="$$bin:$$PATH" CC=gcc go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \
PATH="$$bin:$$PATH" CC=gcc go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \
exit $$?; \
fi; \
if [ -x "$$bin/clang.exe" ] && "$$bin/clang.exe" -dumpmachine 2>/dev/null | grep -qi 'windows.*gnu'; then \
echo "Using Windows CGO clang from $$bin"; \
PATH="$$bin:$$PATH" CC=clang go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \
PATH="$$bin:$$PATH" CC=clang go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \
exit $$?; \
fi; \
done; \
@ -128,9 +129,9 @@ ifeq ($(OS),Windows_NT)
exit 1; \
fi
else
go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd ./cmd/bd
go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd
ifeq ($(shell uname),Darwin)
@codesign -s - -f $(BUILD_DIR)/bd 2>/dev/null || true
@codesign -s - -f "$(BD_BUILD_OUTPUT)" 2>/dev/null || true
@echo "Signed bd for macOS"
endif
endif
@ -264,7 +265,7 @@ test-regression:
# Override version: ./scripts/upgrade-smoke-test.sh v0.62.0
test-upgrade: build
@echo "Running upgrade smoke tests..."
@CANDIDATE_BIN=./bd ./scripts/upgrade-smoke-test.sh
@CANDIDATE_BIN="$(BD_BUILD_OUTPUT)" ./scripts/upgrade-smoke-test.sh
# Run cross-version smoke tests (last 30 tags → candidate).
@ -273,14 +274,14 @@ test-upgrade: build
# All from v0.30.0: ./scripts/cross-version-smoke-test.sh --from v0.30.0
test-cross-version: build
@echo "Running cross-version smoke tests..."
@CANDIDATE_BIN=./bd ./scripts/cross-version-smoke-test.sh
@CANDIDATE_BIN="$(BD_BUILD_OUTPUT)" ./scripts/cross-version-smoke-test.sh
# Run the authenticated historical upgrade corpus with strict fidelity checks.
# All qualified versions: ./scripts/migration-test/run.sh
# Single version: ./scripts/migration-test/run.sh --version v0.49.6
test-migration: build
@echo "Running migration test harness..."
@CANDIDATE_BIN=./bd ./scripts/migration-test/run.sh
@CANDIDATE_BIN="$(BD_BUILD_OUTPUT)" ./scripts/migration-test/run.sh
# Regenerate the golden-JSON contract corpus (cmd/bd/protocol/testdata/corpus/).
# Run after any deliberate bd --json wire change; review the diff, then commit.
@ -351,10 +352,10 @@ install install-force: build
@mkdir -p "$(INSTALL_DIR)"
ifeq ($(OS),Windows_NT)
@rm -f "$(INSTALL_DIR)/bd" "$(INSTALL_DIR)/bd.exe"
@cp "$(BUILD_DIR)/bd.exe" "$(INSTALL_DIR)/bd.exe"
@cp "$(BD_BUILD_OUTPUT)" "$(INSTALL_DIR)/bd.exe"
@echo "Installed bd.exe to $(INSTALL_DIR)/bd.exe"
else
@cp "$(BUILD_DIR)/bd" "$(INSTALL_DIR)/.bd.install.tmp.$$$$" && mv -f "$(INSTALL_DIR)/.bd.install.tmp.$$$$" "$(INSTALL_DIR)/bd"
@cp "$(BD_BUILD_OUTPUT)" "$(INSTALL_DIR)/.bd.install.tmp.$$$$" && mv -f "$(INSTALL_DIR)/.bd.install.tmp.$$$$" "$(INSTALL_DIR)/bd"
@echo "Installed bd to $(INSTALL_DIR)/bd"
@ln -sfn bd "$(INSTALL_DIR)/.beads.install.tmp.$$$$" && mv -f "$(INSTALL_DIR)/.beads.install.tmp.$$$$" "$(INSTALL_DIR)/beads"
@echo "Created 'beads' alias -> bd"
@ -376,7 +377,7 @@ fmt-check:
# Validate documentation references against actual CLI flags
check-docs:
@echo "Building bd for docs checks..."
@CGO_ENABLED=0 go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd ./cmd/bd
@CGO_ENABLED=0 go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BUILD_DIR)/bd" ./cmd/bd
@./scripts/check-doc-flags.sh ./bd
@./scripts/check-doc-freshness.sh
@go test -tags=gms_pure_go ./test/docsync

View file

@ -657,6 +657,11 @@ var roleContractCases = []roleContract{
RunSweeperEmptyMatchIsZeroAndNil,
RunSweeperRecordsExactlyOneHistoryEntry,
RunSweeperDoesNotMutateTheCallerRequest,
RunSweeperWispsPlaneClearsTheWholeWispsTable,
RunSweeperWispsPlaneRequiresAFilter,
RunSweeperProtectsLiveDependents,
RunSweeperProtectsLiveDependentsAcrossPlanes,
RunSweeperLimitTakesTheOldestClosedFirst,
),
roleCases("TreeWalker", "TreeWalker()", oncePerRole,

View file

@ -315,7 +315,7 @@ func TestRoleCasesFailsWhenAFactoryReturnsNil(t *testing.T) {
func runProbeChild(t *testing.T, mode string) (string, error) {
t.Helper()
cmd := exec.Command(os.Args[0], "-test.run=^"+t.Name()+"$", "-test.v")
cmd.Env = append(os.Environ(), roleBundleProbeEnv+"="+mode)
cmd.Env = append(bazeltest.ShardFreeEnv(os.Environ()), roleBundleProbeEnv+"="+mode)
out, err := cmd.CombinedOutput()
return string(out), err
}

View file

@ -89,6 +89,11 @@ type SweeperFixture struct {
// by hand. A nil Exec means "this backend cannot seed raw rows", and the
// case that needs it SKIPS with that reason rather than passing quietly.
Exec func(ctx context.Context, statements []SQLStatement) error
// AddDependencies wires edges between seeded rows, filled from the
// backend's DependencyEditor role so each adapter routes an edge to the
// dependency table of its source's plane itself. The live-dependent case
// needs it; a nil hook SKIPS that case with the reason.
AddDependencies func(ctx context.Context, req publicops.AddDependenciesRequest) error
}
// sweeperClosedAt is the stamp every seeded candidate carries, and
@ -345,7 +350,7 @@ func RunSweeperDryRunChangesNothing(t *testing.T, ctx context.Context, fixture S
IDPattern: sweeperPattern(fixture, "dry"),
}
preview := sweeperSweep(t, ctx, fixture, sweeperWithDryRun(request, true))
preview := sweeperSweep(t, ctx, fixture, sweeperAsDryRun(request))
if !preview.DryRun {
t.Error("preview.DryRun = false; the result must echo the request it answered")
}
@ -356,7 +361,7 @@ func RunSweeperDryRunChangesNothing(t *testing.T, ctx context.Context, fixture S
// A second preview: a dry run that had deleted anything reports a smaller
// number here.
if again := sweeperSweep(t, ctx, fixture, sweeperWithDryRun(request, true)); again.Swept != preview.Swept {
if again := sweeperSweep(t, ctx, fixture, sweeperAsDryRun(request)); again.Swept != preview.Swept {
t.Fatalf("second preview Swept = %d, first = %d: the first preview changed the store", again.Swept, preview.Swept)
}
@ -556,7 +561,7 @@ func RunSweeperRecordsExactlyOneHistoryEntry(t *testing.T, ctx context.Context,
}
before := sweeperHistory(t, ctx, fixture)
sweeperSweep(t, ctx, fixture, sweeperWithDryRun(request, true))
sweeperSweep(t, ctx, fixture, sweeperAsDryRun(request))
if after := sweeperHistory(t, ctx, fixture); after != before {
t.Errorf("history went %d -> %d across a DRY RUN, want no entry at all", before, after)
}
@ -609,6 +614,230 @@ func RunSweeperDoesNotMutateTheCallerRequest(t *testing.T, ctx context.Context,
}
}
// RunSweeperWispsPlaneClearsTheWholeWispsTable pins issueops.SweepWispsPlane:
// the selection is the wisps TABLE, so a closed no-history bead — durable-tier,
// and left alone by SweepEphemeral (RunSweeperLeavesNoHistoryBeadsToTheDurableTier)
// — goes with the closed ephemeral wisp, while a closed issues-plane row the
// same pattern admits stays.
func RunSweeperWispsPlaneClearsTheWholeWispsTable(t *testing.T, ctx context.Context, fixture SweeperFixture) {
t.Helper()
wisp := sweeperSeedClosedIssue(t, ctx, fixture, "plane", true)
durable := sweeperSeedClosedIssue(t, ctx, fixture, "plane", false)
noHistory := sweeperIssue(fixture, "plane", "nohist", false)
noHistory.NoHistory = true
if err := fixture.CreateWisp(ctx, noHistory, "sweeper-seed"); err != nil {
t.Fatalf("seeding %s: %v", noHistory.ID, err)
}
pinned := sweeperSeed(t, ctx, fixture, sweeperIssue(fixture, "plane", "pinned", true), func(issue *types.Issue) {
issue.Pinned = true
})
request := publicops.SweepRequest{
Tier: publicops.SweepWispsPlane,
IDPattern: sweeperPattern(fixture, "plane"),
ClosedBefore: &sweeperCutoff,
}
preview := sweeperSweep(t, ctx, fixture, sweeperAsDryRun(request))
if preview.Swept != 2 {
t.Fatalf("dry-run Swept = %d, want 2 (the ephemeral wisp and the no-history bead)", preview.Swept)
}
sweeperAssertWispRows(t, ctx, fixture, 3, wisp, noHistory.ID, pinned)
result := sweeperSweep(t, ctx, fixture, request)
if result.Swept != 2 {
t.Errorf("Swept = %d, want 2 (the ephemeral wisp and the no-history bead)", result.Swept)
}
if result.Skipped.Pinned != 1 {
t.Errorf("Skipped.Pinned = %d, want 1 — the plane selector does not override pinning", result.Skipped.Pinned)
}
sweeperAssertWispRows(t, ctx, fixture, 0, wisp, noHistory.ID)
sweeperAssertWispRows(t, ctx, fixture, 1, pinned)
sweeperAssertIssueRows(t, ctx, fixture, 1, durable)
}
// RunSweeperWispsPlaneRequiresAFilter pins that the wisps-plane tier carries
// the durable tier's require-a-filter gate: it reaches no-history beads, which
// are durable-tier. The refusal is asserted with its effect, as the durable
// case does.
func RunSweeperWispsPlaneRequiresAFilter(t *testing.T, ctx context.Context, fixture SweeperFixture) {
t.Helper()
id := sweeperSeedClosedIssue(t, ctx, fixture, "planegate", true)
result, err := fixture.Sweeper.Sweep(ctx, publicops.SweepRequest{Tier: publicops.SweepWispsPlane})
if !errors.Is(err, publicops.ErrValidation) {
t.Fatalf("Sweep(wisps-plane, no cutoff, no pattern) error = %v, want ErrValidation", err)
}
if result.Swept != 0 {
t.Errorf("refused sweep reported Swept = %d, want 0", result.Swept)
}
sweeperAssertWispRows(t, ctx, fixture, 1, id)
}
// RunSweeperProtectsLiveDependents pins issueops.SweepRequest.ProtectLiveDependents:
// a closed row a LIVE row depends on through parent-child, tracks or blocks is
// held back and counted, while a closed row reached only by a non-structural
// edge, or only by a dependent that is itself closed, is swept.
//
// The live sources carry three different not-done statuses (open,
// in_progress, hooked), because "live" is the not-done set and not "open".
// It runs on the wisps-plane tier with a cutoff — the request an
// orchestrator's retention sweep sends — after proving on a dry run that the
// same rows ARE candidates without the protection.
func RunSweeperProtectsLiveDependents(t *testing.T, ctx context.Context, fixture SweeperFixture) {
t.Helper()
if fixture.AddDependencies == nil {
t.Skip("fixture has no AddDependencies hook; cannot wire the edges this case needs")
}
live := func(name string, status types.Status) string {
issue := sweeperIssue(fixture, "live", name, true)
issue.Status = status
issue.ClosedAt = nil
return sweeperSeed(t, ctx, fixture, issue, nil)
}
closed := func(name string) string {
return sweeperSeed(t, ctx, fixture, sweeperIssue(fixture, "live", name, true), nil)
}
parent := closed("parent")
openChild := live("child-open", types.StatusOpen)
tracked := closed("tracked")
convoy := live("convoy", types.StatusInProgress)
blocker := closed("blocker")
hooked := live("hooked", types.StatusHooked)
related := closed("related")
relater := live("relater", types.StatusOpen)
doneParent := closed("done-parent")
doneChild := closed("done-child")
if err := fixture.AddDependencies(ctx, publicops.AddDependenciesRequest{
Actor: "sweeper-seed",
Edges: []publicops.DependencyEdge{
{IssueID: openChild, DependsOnID: parent, Type: publicops.DepParentChild},
{IssueID: convoy, DependsOnID: tracked, Type: publicops.DepTracks},
{IssueID: hooked, DependsOnID: blocker, Type: publicops.DepBlocks},
{IssueID: relater, DependsOnID: related, Type: publicops.DepRelated},
{IssueID: doneChild, DependsOnID: doneParent, Type: publicops.DepParentChild},
},
}); err != nil {
t.Fatalf("wiring dependencies: %v", err)
}
request := publicops.SweepRequest{
Tier: publicops.SweepWispsPlane,
IDPattern: sweeperPattern(fixture, "live"),
ClosedBefore: &sweeperCutoff,
}
unprotected := sweeperSweep(t, ctx, fixture, sweeperAsDryRun(request))
if unprotected.Swept != 6 || unprotected.Skipped.LiveDependent != 0 {
t.Fatalf("unprotected dry run: Swept = %d, LiveDependent = %d; want 6 and 0 — "+
"the protection must be asked for", unprotected.Swept, unprotected.Skipped.LiveDependent)
}
request.ProtectLiveDependents = true
result := sweeperSweep(t, ctx, fixture, request)
if result.Skipped.LiveDependent != 3 {
t.Errorf("Skipped.LiveDependent = %d, want 3 (the parent, the tracked bead and the blocker)",
result.Skipped.LiveDependent)
}
if result.Swept != 3 {
t.Errorf("Swept = %d, want 3 (the related-only bead and the closed parent/child pair)", result.Swept)
}
sweeperAssertWispRows(t, ctx, fixture, 3, parent, tracked, blocker)
sweeperAssertWispRows(t, ctx, fixture, 0, related, doneParent, doneChild)
sweeperAssertWispRows(t, ctx, fixture, 4, openChild, convoy, hooked, relater)
}
// RunSweeperProtectsLiveDependentsAcrossPlanes pins that the live-dependent
// protection reads BOTH dependency tables: a closed wisp that a live DURABLE
// (issues-table) bead depends on through parent-child, tracks or blocks is
// kept, whatever not-done status the durable source carries.
func RunSweeperProtectsLiveDependentsAcrossPlanes(t *testing.T, ctx context.Context, fixture SweeperFixture) {
t.Helper()
if fixture.AddDependencies == nil {
t.Skip("fixture has no AddDependencies hook; cannot wire the edges this case needs")
}
durableLive := func(name string, status types.Status) string {
issue := sweeperIssue(fixture, "xplane", name, false)
issue.Status = status
issue.ClosedAt = nil
return sweeperSeed(t, ctx, fixture, issue, nil)
}
closedWisp := func(name string) string {
return sweeperSeed(t, ctx, fixture, sweeperIssue(fixture, "xplane", name, true), nil)
}
parent := closedWisp("parent")
tracked := closedWisp("tracked")
blocker := closedWisp("blocker")
loose := closedWisp("loose")
child := durableLive("child", types.StatusOpen)
convoy := durableLive("convoy", types.StatusInProgress)
waiter := durableLive("waiter", types.StatusDeferred)
if err := fixture.AddDependencies(ctx, publicops.AddDependenciesRequest{
Actor: "sweeper-seed",
Edges: []publicops.DependencyEdge{
{IssueID: child, DependsOnID: parent, Type: publicops.DepParentChild},
{IssueID: convoy, DependsOnID: tracked, Type: publicops.DepTracks},
{IssueID: waiter, DependsOnID: blocker, Type: publicops.DepBlocks},
},
}); err != nil {
t.Fatalf("wiring cross-plane dependencies: %v", err)
}
result := sweeperSweep(t, ctx, fixture, publicops.SweepRequest{
Tier: publicops.SweepWispsPlane,
IDPattern: sweeperPattern(fixture, "xplane"),
ClosedBefore: &sweeperCutoff,
ProtectLiveDependents: true,
})
if result.Skipped.LiveDependent != 3 || result.Swept != 1 {
t.Errorf("Swept = %d, LiveDependent = %d; want 1 (the unlinked wisp) and 3", result.Swept, result.Skipped.LiveDependent)
}
sweeperAssertWispRows(t, ctx, fixture, 3, parent, tracked, blocker)
sweeperAssertWispRows(t, ctx, fixture, 0, loose)
sweeperAssertIssueRows(t, ctx, fixture, 3, child, convoy, waiter)
}
// RunSweeperLimitTakesTheOldestClosedFirst pins issueops.SweepRequest.Limit:
// one call deletes at most Limit rows, oldest closed_at first, and reports the
// rest as Remaining; the next call picks up where it stopped.
func RunSweeperLimitTakesTheOldestClosedFirst(t *testing.T, ctx context.Context, fixture SweeperFixture) {
t.Helper()
seed := func(name string, closed time.Time) string {
return sweeperSeed(t, ctx, fixture, sweeperIssue(fixture, "limit", name, true), func(issue *types.Issue) {
issue.ClosedAt = &closed
})
}
newest := seed("newest", sweeperClosedAt)
middle := seed("middle", sweeperClosedAt.Add(-24*time.Hour))
oldest := seed("oldest", sweeperClosedAt.Add(-48*time.Hour))
request := publicops.SweepRequest{
Tier: publicops.SweepEphemeral,
IDPattern: sweeperPattern(fixture, "limit"),
Limit: 2,
}
first := sweeperSweep(t, ctx, fixture, request)
if first.Swept != 2 || first.Remaining != 1 {
t.Fatalf("first limited sweep: Swept = %d, Remaining = %d; want 2 and 1", first.Swept, first.Remaining)
}
sweeperAssertWispRows(t, ctx, fixture, 0, oldest, middle)
sweeperAssertWispRows(t, ctx, fixture, 1, newest)
second := sweeperSweep(t, ctx, fixture, request)
if second.Swept != 1 || second.Remaining != 0 {
t.Fatalf("second limited sweep: Swept = %d, Remaining = %d; want 1 and 0", second.Swept, second.Remaining)
}
sweeperAssertWispRows(t, ctx, fixture, 0, newest)
if _, err := fixture.Sweeper.Sweep(ctx, publicops.SweepRequest{
Tier: publicops.SweepEphemeral, IDPattern: sweeperPattern(fixture, "limit"), Limit: -1,
}); !errors.Is(err, publicops.ErrValidation) {
t.Fatalf("negative Limit error = %v, want ErrValidation", err)
}
}
// --- fixture helpers -------------------------------------------------------
// sweeperPattern is the glob that scopes one case's ids: a sweep is asked of a
@ -664,8 +893,8 @@ func sweeperSeedOpen(t *testing.T, ctx context.Context, fixture SweeperFixture,
return sweeperSeed(t, ctx, fixture, issue, nil)
}
func sweeperWithDryRun(request publicops.SweepRequest, dryRun bool) publicops.SweepRequest {
request.DryRun = dryRun
func sweeperAsDryRun(request publicops.SweepRequest) publicops.SweepRequest {
request.DryRun = true
return request
}

View file

@ -29,7 +29,9 @@ func testMainInner(m *testing.M) int {
// AD-01 (be-c5p): allow root tests to connect to the test container.
os.Setenv("BEADS_TEST_SERVER", "1")
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
if testutil.DoltUnavailableForTestMain(err) {
return 1
}
} else {
defer testutil.TerminateDoltContainer()
testServerPort = testutil.DoltContainerPortInt()

View file

@ -1,4 +1,5 @@
load("@rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
go_library(
name = "bd_lib",
@ -14,6 +15,7 @@ go_library(
"backup_auto.go",
"backup_dolt.go",
"backup_export.go",
"backup_lock.go",
"backup_proxied_server.go",
"backup_restore.go",
"batch.go",
@ -351,6 +353,7 @@ go_library(
"//internal/atomicfile",
"//internal/audit",
"//internal/beads",
"//internal/ceiling",
"//internal/compact",
"//internal/config",
"//internal/configfile",
@ -393,6 +396,7 @@ go_library(
"//internal/storage/doltutil",
"//internal/storage/domain",
"//internal/storage/domain/db",
"//internal/storage/domain/git",
"//internal/storage/embeddeddolt",
"//internal/storage/externaldeps",
"//internal/storage/fs",
@ -475,23 +479,30 @@ go_binary(
go_test(
name = "bd_test",
# -race -short runs ~10 min (pr-core gives go test -timeout=30m).
# -race -short runs ~4-10 min unsharded (pr-core gives go test
# -timeout=30m). Bazel shards by test count, not duration; test re-execs
# strip the shard variables (bazeltest.ShardFreeEnv).
size = "large",
timeout = "long",
srcs = [
"aaa_stdio_baseline_test.go",
"actor_test.go",
"ado_roundtrip_test.go",
"ado_test.go",
"agent_hook_test.go",
"assign_fence_embedded_test.go",
"assign_fence_proxied_test.go",
"auto_import_upgrade_test.go",
"auto_import_upgrade_unit_test.go",
"autocommit_embedded_test.go",
"backend_registry_contract_test.go",
"backup_auto_proxied_local_integration_test.go",
"backup_auto_proxied_test.go",
"backup_auto_test.go",
"backup_dolt_test.go",
"backup_embedded_test.go",
"backup_export_failure_test.go",
"backup_lock_test.go",
"backup_proxied_external_integration_test.go",
"backup_proxied_local_integration_test.go",
"backup_proxied_policy_test.go",
@ -518,6 +529,7 @@ go_test(
"children_embedded_test.go",
"children_test.go",
"claim_pools_embedded_test.go",
"cli_fast_test.go",
"close_embedded_test.go",
"close_failure_report_test.go",
"close_gate_test.go",
@ -546,6 +558,7 @@ go_test(
"config_get_backup_enabled_test.go",
"config_guard_test.go",
"config_isolation_regression_test.go",
"config_leaked_secret_test.go",
"config_list_test.go",
"config_nodb_embedded_test.go",
"config_proxied_integration_test.go",
@ -567,6 +580,7 @@ go_test(
"context_proxied_integration_test.go",
"context_test.go",
"contributor_embedded_test.go",
"contributor_routing_e2e_test.go",
"cook_qnt_test.go",
"cook_test.go",
"count_embedded_test.go",
@ -607,6 +621,7 @@ go_test(
"delete_input_test.go",
"delete_proxied_integration_test.go",
"delete_proxied_server_output_test.go",
"delete_test.go",
"dep_add_target_test.go",
"dep_batch_test.go",
"dep_bulk_role_test.go",
@ -628,6 +643,7 @@ go_test(
"doctor_embedded_test.go",
"doctor_fix_order_test.go",
"doctor_fix_pending_migrations_test.go",
"doctor_health_integration_test.go",
"doctor_health_smoke_test.go",
"doctor_health_test.go",
"doctor_init_diagnostics_cgo_test.go",
@ -639,19 +655,23 @@ go_test(
"doctor_test.go",
"doctor_validate_test.go",
"dolt_autocommit_exemption_wiring_test.go",
"dolt_autocommit_integration_test.go",
"dolt_autocommit_server_test.go",
"dolt_autocommit_test.go",
"dolt_autopush_test.go",
"dolt_autostart_lifecycle_integration_test.go",
"dolt_clean_databases_purge_proxied_integration_test.go",
"dolt_clean_databases_purge_test.go",
"dolt_clean_databases_quote_test.go",
"dolt_commit_proxied_integration_test.go",
"dolt_commit_proxied_server_test.go",
"dolt_doctor_integration_test.go",
"dolt_embedded_test.go",
"dolt_help_auth_test.go",
"dolt_lifecycle_proxied_server_test.go",
"dolt_local_only_embedded_test.go",
"dolt_local_only_test.go",
"dolt_metadata_e2e_test.go",
"dolt_proxied_lifecycle_test.go",
"dolt_remote_add_test.go",
"dolt_remote_adopt_test.go",
@ -715,6 +735,7 @@ go_test(
"gc_embedded_test.go",
"gc_prune_test.go",
"git_repo_template_test.go",
"gitlab_integration_test.go",
"gitlab_test.go",
"global_identity_integration_test.go",
"gomodcache_rehome_test.go",
@ -733,6 +754,7 @@ go_test(
"history_full_hash_test.go",
"history_proxied_integration_test.go",
"history_remote_frontdoor_matrix_test.go",
"hook_env_test.go",
"hooks_embedded_test.go",
"hooks_exec_composition_test.go",
"hooks_export_staged_deletion_test.go",
@ -754,7 +776,9 @@ go_test(
"import_embedded_test.go",
"import_from_jsonl_test.go",
"import_global_prefix_isolation_test.go",
"import_missing_config_prefix_test.go",
"import_orphan_embedded_test.go",
"import_prefix_test.go",
"import_promoted_wisp_embedded_test.go",
"import_proxied_integration_test.go",
"import_shared_unit_test.go",
@ -763,9 +787,11 @@ go_test(
"info_kv_exclusion_test.go",
"info_proxied_integration_test.go",
"info_test.go",
"init_cancel_e2e_test.go",
"init_commit_test.go",
"init_embedded_test.go",
"init_gateway_test.go",
"init_git_bootstrap_routing_test.go",
"init_guard_test.go",
"init_hooks_test.go",
"init_metrics_test.go",
@ -777,6 +803,8 @@ go_test(
"init_proxied_server_test.go",
"init_registered_backend_test.go",
"init_remote_test.go",
"init_role_environment_test.go",
"init_role_routing_test.go",
"init_safety_test.go",
"init_server_flags_test.go",
"init_stealth_test.go",
@ -800,6 +828,7 @@ go_test(
"legacy_upgrade_guard_e2e_test.go",
"legacy_upgrade_guard_test.go",
"legacy_upgrade_guard_witness_test.go",
"linear_roundtrip_test.go",
"linear_test.go",
"lint_embedded_test.go",
"lint_stale_proxied_integration_test.go",
@ -837,6 +866,7 @@ go_test(
"metadata_ready_test.go",
"metadataless_server_workspace_test.go",
"metrics_test.go",
"migrate_dolt_mode_frontdoor_integration_test.go",
"migrate_dolt_mode_test.go",
"migrate_embedded_test.go",
"migrate_force_test.go",
@ -913,6 +943,8 @@ go_test(
"prune_proxied_integration_test.go",
"purge_embedded_test.go",
"purge_test.go",
"purge_wisps_plane_integration_test.go",
"purge_wisps_plane_managed_local_test.go",
"query_embedded_test.go",
"query_limit_policy_test.go",
"query_proxied_integration_test.go",
@ -945,10 +977,12 @@ go_test(
"rename_prefix_rewrite_test.go",
"rename_prefix_test.go",
"rename_prefix_worktree_test.go",
"reopen_defer_test.go",
"reopen_embedded_test.go",
"reopen_proxied_integration_test.go",
"reopen_test.go",
"repair_deps_test.go",
"reparent_test.go",
"reset_global_guard_test.go",
"reset_hook_ownership_test.go",
"reset_worktree_test.go",
@ -966,6 +1000,7 @@ go_test(
"search_embedded_test.go",
"search_proxied_integration_test.go",
"search_test.go",
"send_metrics_diagnostics_test.go",
"serve_batch_close_proxied_integration_test.go",
"serve_claim_next_proxied_integration_test.go",
"serve_claim_proxied_integration_test.go",
@ -1007,6 +1042,7 @@ go_test(
"show_not_found_hint_test.go",
"show_proxied_integration_test.go",
"show_proxied_reader_test.go",
"show_test.go",
"show_unit_helpers_test.go",
"show_watch_test.go",
"simple_helpers_test.go",
@ -1052,6 +1088,7 @@ go_test(
"telemetry_scrub_test.go",
"template_test.go",
"test_dolt_server_cgo_test.go",
"test_helpers_integration_test.go",
"test_helpers_pure_test.go",
"test_helpers_test.go",
"test_repo_beads_guard_darwin_test.go",
@ -1130,6 +1167,7 @@ go_test(
"BEADS_TEST_BD_BINARY": "$(rlocationpath :bd_for_tests)",
"BEADS_TEST_GOFMT": "$(rlocationpath @go_sdk//:bin/gofmt)",
},
shard_count = 16,
deps = [
"//:beads",
"//cmd/bd/doctor",
@ -1169,6 +1207,8 @@ go_test(
"//internal/storage/domain",
"//internal/storage/embeddeddolt",
"//internal/storage/externaldeps",
"//internal/storage/fs",
"//internal/storage/git",
"//internal/storage/issueops",
"//internal/storage/journalscan",
"//internal/storage/kvkeys",
@ -1242,6 +1282,50 @@ go_test(
}),
)
# bd_test as PR Risk's "Test (Embedded Dolt Cmd N/20)" jobs run it: shard k
# runs .github/scripts/embedded-test-shard.sh k 20 (the TestEmbedded* functions
# of *_embedded_test.go, assigned by the committed manifest) against this
# configuration's bd_test, with BEADS_TEST_EMBEDDED_DOLT=1 and the race bd
# (//cmd/bd:bd under --config=embedded) as the subprocess binary, like the
# jobs' /tmp/bd-embedded-test. It repeats bd_test's `env` (the wrapper cannot
# inherit it). shard_count must stay equal to the manifest's shard total.
#
# Tags:
# embedded: the embedded-Dolt tier; excluded from --config=prcore/ci,
# selected by --config=embedded.
sh_test(
name = "bd_embedded_test",
size = "large",
# The script gives the binary the job's -test.timeout=20m, as long as
# Bazel's 1200s action limit (eternal under --config=embedded), which
# starts earlier and kills without a goroutine dump (rules_go's wrapper
# ignores the SIGTERM). The trailing -test.timeout=19m overrides it (the
# last flag wins) so Go's own timeout fires first and dumps to test.log.
timeout = "eternal",
srcs = ["//tools/bazel:go_test_manifest_shard.sh"],
args = [
"$(rootpath //:.github/scripts/embedded-test-shard.sh)",
"BEADS_TEST_CMD_BINARY",
"$(rootpath :bd_test)",
"-test.timeout=19m",
],
data = [
":bd",
":bd_test",
":go_srcs",
"//:.github/scripts/embedded-cmd-test-shards.txt",
"//:.github/scripts/embedded-test-shard.sh",
"@go_sdk//:bin/gofmt",
],
env = {
"BEADS_TEST_BD_BINARY": "$(rlocationpath :bd)",
"BEADS_TEST_EMBEDDED_DOLT": "1",
"BEADS_TEST_GOFMT": "$(rlocationpath @go_sdk//:bin/gofmt)",
},
shard_count = 20,
tags = ["embedded"],
)
# CLI flag registrations pinned by //internal/httpapi spec parity tests.
exports_files(
[

View file

@ -4,11 +4,13 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// Shared helpers for the agent lifecycle hook commands (codex-hook, cursor-hook).
@ -16,17 +18,113 @@ import (
// the prime-runner and one-shot refresh-marker mechanics are identical and live
// here to avoid divergence.
// allowedPrimeArgs is the closed set of flags the agent hooks may pass to the
// re-executed bd binary. The hooks only ever run the fixed `bd prime`
// invocation (optionally --memories-only); anything else is a programming
// error and is refused before the subprocess is built.
var allowedPrimeArgs = map[string]bool{"--memories-only": true}
// errPrimeTestBinary is returned by resolvePrimeExecutable when the running
// binary is a test binary. Mirrors doctor/fix's ErrTestBinary: re-execing a
// test binary with `prime` fork-bombs the suite.
var errPrimeTestBinary = errors.New("running as test binary - cannot re-exec bd")
// primeExecutable resolves the re-exec target for runBdPrime. A var so tests
// can stub resolution and prove no subprocess is ever built from unexpected
// input.
var primeExecutable = resolvePrimeExecutable
// resolvePrimeExecutable is the production re-exec resolver. It restates
// getBdBinary (cmd/bd/doctor/fix/common.go), the repo's existing answer to "bd
// re-invokes bd": prefer the running binary, resolve symlinks so every re-exec
// site agrees on one path, refuse a test binary so an unstubbed test call
// fails loudly instead of fork-bombing, and fall back to a validated PATH
// lookup when the running binary cannot be resolved. getBdBinary is
// package-private to doctor/fix, so this restates the precedent rather than
// reusing it.
//
// One trade-off is kept deliberately, and it is narrower than it looks. Linux
// reports an unlinked image with a " (deleted)" suffix, but os.Executable
// strips that suffix (os/executable_procfs.go), so no "(deleted)" path ever
// reaches this resolver: a bd replaced in place mid-session (an ordinary
// `go install ./cmd/bd`) still resolves to its own path, EvalSymlinks succeeds
// against the new file, and the re-exec runs the replacement. The residual gap
// is a bd deleted without being replaced: EvalSymlinks fails, that failure is
// tolerated so the stale path is kept, and the PATH fallback is correctly not
// taken because os.Executable itself returned no error. Accepted: the target is
// PATH-independent because it contains a path separator (exec.Command calls
// LookPath only when filepath.Base(name) == name), and both hook handlers treat
// a prime failure as non-fatal, so the effect is a session primed without
// context.
func resolvePrimeExecutable() (string, error) {
exe, err := os.Executable()
if err == nil {
if resolved, linkErr := filepath.EvalSymlinks(exe); linkErr == nil {
exe = resolved
}
// testing.Testing() is the authoritative signal and must come first:
// rules_go links test binaries as <name>_test (cmd/bd's is bd_test),
// which no ".test" name check can see, so under the Bazel lane the
// name checks alone would let an unstubbed call re-exec the test
// binary. The name checks remain for binaries built with `go test -c`
// and run outside the harness. Mirrors doctor/fix's isTestBinary.
base := filepath.Base(exe)
if testing.Testing() || strings.HasSuffix(base, ".test") || strings.Contains(base, ".test.") {
return "", errPrimeTestBinary
}
return exe, nil
}
bdPath, lookErr := exec.LookPath("bd")
if lookErr != nil {
return "", fmt.Errorf("bd binary not found in PATH: %w", lookErr)
}
return bdPath, nil
}
// validatePrimeArgs rejects any argument outside allowedPrimeArgs so the
// re-exec below can never be steered by caller-supplied strings.
func validatePrimeArgs(args []string) error {
for _, arg := range args {
if !allowedPrimeArgs[arg] {
return fmt.Errorf("bd prime: unsupported hook argument %q", arg)
}
}
return nil
}
// primeCommand validates args, resolves the re-exec target, and builds the
// `bd prime [args...]` command without running it. Split out from runBdPrime
// so a test can assert the exec target and argv — the thing this seam exists
// to guarantee — without launching a subprocess.
func primeCommand(ctx context.Context, args ...string) (*exec.Cmd, error) {
if err := validatePrimeArgs(args); err != nil {
return nil, err
}
exe, err := primeExecutable()
if err != nil {
return nil, fmt.Errorf("bd prime: resolve executable: %w", err)
}
cmdArgs := append([]string{"prime"}, args...)
// #nosec G204 - exe comes from primeExecutable (resolvePrimeExecutable in
// production: this bd binary re-invoking itself); cmdArgs is the fixed
// "prime" subcommand plus allowlisted internal flags, never
// attacker-controlled input. Documentation only: .golangci.yml excludes
// G204 repo-wide, so this annotation is not what keeps lint green.
return exec.CommandContext(ctx, exe, cmdArgs...), nil
}
// runBdPrime shells out to `bd prime [args...]` and returns its combined output.
// The hooks exec a subprocess (rather than calling prime in process) to avoid
// re-entrant store initialization.
func runBdPrime(ctx context.Context, args ...string) (string, error) {
cmdArgs := append([]string{"prime"}, args...)
// #nosec G702 - os.Args[0] is this bd binary re-invoking itself; cmdArgs is the
// fixed "prime" subcommand plus internal flags, never attacker-controlled input.
cmd := exec.CommandContext(ctx, os.Args[0], cmdArgs...)
cmd, err := primeCommand(ctx, args...)
if err != nil {
return "", err
}
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("bd %s: %w: %s", strings.Join(cmdArgs, " "), err, strings.TrimSpace(string(out)))
return "", fmt.Errorf("bd %s: %w: %s", strings.Join(cmd.Args[1:], " "), err, strings.TrimSpace(string(out)))
}
return string(out), nil
}

131
cmd/bd/agent_hook_test.go Normal file
View file

@ -0,0 +1,131 @@
package main
import (
"context"
"errors"
"slices"
"strings"
"testing"
)
func TestValidatePrimeArgsAcceptsHookFlags(t *testing.T) {
if err := validatePrimeArgs(nil); err != nil {
t.Fatalf("no args should be allowed, got %v", err)
}
if err := validatePrimeArgs([]string{"--memories-only"}); err != nil {
t.Fatalf("--memories-only should be allowed, got %v", err)
}
}
func TestValidatePrimeArgsRejectsUnknownFlag(t *testing.T) {
err := validatePrimeArgs([]string{"--config"})
if err == nil {
t.Fatal("expected unknown flag to be rejected")
}
if !strings.Contains(err.Error(), `"--config"`) {
t.Fatalf("rejection should name the offending argument, got: %v", err)
}
}
// runBdPrime must refuse non-allowlisted arguments before it resolves the
// executable or builds a subprocess. The resolver is stubbed so the test also
// proves validation runs first — if a future change drops or reorders the
// validatePrimeArgs call, the resolver (and with it the subprocess) would be
// reached and this test fails instead of live-exec'ing anything.
func TestRunBdPrimeRejectsUnknownArgsBeforeExec(t *testing.T) {
called := false
orig := primeExecutable
primeExecutable = func() (string, error) {
called = true
return "", errors.New("resolver must not run for rejected args")
}
t.Cleanup(func() { primeExecutable = orig })
_, err := runBdPrime(context.Background(), "--config")
if err == nil {
t.Fatal("expected runBdPrime to reject unknown args")
}
if !strings.Contains(err.Error(), `"--config"`) {
t.Fatalf("rejection should name the offending argument, got: %v", err)
}
if called {
t.Fatal("executable resolver ran before argument validation")
}
}
// With allowlisted args, a resolver failure surfaces as the wrapped
// resolve-executable error and no subprocess is built.
func TestRunBdPrimeExecutableResolutionError(t *testing.T) {
orig := primeExecutable
primeExecutable = func() (string, error) {
return "", errors.New("no executable")
}
t.Cleanup(func() { primeExecutable = orig })
_, err := runBdPrime(context.Background())
if err == nil || !strings.Contains(err.Error(), "resolve executable") {
t.Fatalf("want resolve-executable error, got: %v", err)
}
}
// The point of the seam: the command must be built from the *resolved*
// executable, not from os.Args[0]. primeCommand builds without running, so
// this asserts the exec target and the full argv directly. The sentinel
// deliberately contains a path separator — exec.Command only skips LookPath
// for names that do, so a bare name would leave cmd.Path host-PATH-dependent
// and set cmd.Err, making the assertion vacuous.
func TestPrimeCommandUsesResolvedExecutable(t *testing.T) {
const sentinel = "/tmp/beads-prime-sentinel-bd"
orig := primeExecutable
primeExecutable = func() (string, error) { return sentinel, nil }
t.Cleanup(func() { primeExecutable = orig })
cmd, err := primeCommand(context.Background(), "--memories-only")
if err != nil {
t.Fatalf("primeCommand with allowlisted args: %v", err)
}
if cmd.Err != nil {
t.Fatalf("separator-containing sentinel should build cleanly, got cmd.Err=%v", cmd.Err)
}
if cmd.Path != sentinel {
t.Fatalf("exec target should be the resolved executable %q, got %q", sentinel, cmd.Path)
}
want := []string{sentinel, "prime", "--memories-only"}
if !slices.Equal(cmd.Args, want) {
t.Fatalf("argv should be %q, got %q", want, cmd.Args)
}
}
// primeCommand refuses non-allowlisted args before resolving, and returns no
// command to run.
func TestPrimeCommandRejectsUnknownArgs(t *testing.T) {
orig := primeExecutable
primeExecutable = func() (string, error) {
return "", errors.New("resolver must not run for rejected args")
}
t.Cleanup(func() { primeExecutable = orig })
cmd, err := primeCommand(context.Background(), "--config")
if err == nil {
t.Fatal("expected primeCommand to reject unknown args")
}
if cmd != nil {
t.Fatalf("rejected args must not yield a command, got %v", cmd.Args)
}
}
// The production resolver refuses a test binary rather than handing back a
// path whose re-exec would fork-bomb the suite — the guard doctor/fix's
// getBdBinary pairs with os.Executable. Under `go test` the running binary is
// the package test binary, so this exercises the refusal directly, the same
// way cmd/bd/doctor/fix's own suites assert ErrTestBinary.
func TestResolvePrimeExecutableRefusesTestBinary(t *testing.T) {
path, err := resolvePrimeExecutable()
if !errors.Is(err, errPrimeTestBinary) {
t.Fatalf("want errPrimeTestBinary under go test, got path=%q err=%v", path, err)
}
if path != "" {
t.Fatalf("refusal must not return a path, got %q", path)
}
}

View file

@ -42,10 +42,10 @@ Auto-backup default:
coordinate destinations). 'bd config get backup.enabled' shows the effective
value and its source.
On a proxied-server workspace that opt-in does not apply: auto-backup has
no post-command hook on that path, so backup.enabled=true is inert there.
The five backup verbs above are honored on a managed-local proxied server —
run 'bd backup sync' explicitly to back one up.`,
A proxied-server workspace is server mode too: auto-backup is OFF by
default and backup.enabled=true opts in. Both the opt-in and the five
backup verbs above are honored only on a managed-local proxied server —
one bd started itself; on a Dolt server bd does not own they are refused.`,
GroupID: "sync",
}

View file

@ -2,6 +2,7 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
@ -46,28 +47,21 @@ func isBackupAutoEnabled() bool {
// already computed so the note cannot disagree with the number beside
// it — and so status does not re-run the git-remote probe.
//
// The note narrates the reason the decision actually used. Proxied-server
// is checked first and ignores the config source, because on that topology
// auto-backup cannot run at all whatever backup.enabled says: the proxied
// arm of PersistentPostRunE never calls runPostRunAutoBackup (main.go), so
// an explicit backup.enabled=true is inert and reporting a bare
// "enabled=true" would send the operator away believing backups happen.
// The five backup verbs are honored on managed-local (the only proxied
// shape that reaches this line — every other one is refused by
// requireLocalProxiedBackup), so `bd backup sync` is the real answer there.
//
// Every other sql-server shape keeps its post-run hook, so there the
// default is merely off — the reason `bd config get backup.enabled`
// already reports for the same condition. Before this arm existed, status
// attributed that OFF to a missing git remote, which is untrue: server
// mode short-circuits isBackupAutoEnabled whether or not a remote exists.
// The note narrates the reason the decision actually used. An explicit
// backup.enabled needs no note: the source explains it, and every shape
// that reaches `bd backup status` honors it — including a managed-local
// proxied server, whose post-run arm runs auto-backup (every other proxied
// shape is refused by requireLocalProxiedBackup before status renders).
// A server-mode default is OFF whether or not a git remote exists, so it
// must not be attributed to the remote; before these arms existed, status
// did exactly that.
func backupAutoStatusNote(enabled bool) string {
if usesProxiedServer() {
return "auto-backup does not run on proxied-server; use 'bd backup sync'"
}
if config.GetValueSource("backup.enabled") != config.SourceDefault {
return ""
}
if usesProxiedServer() {
return "auto: off in proxied-server mode; set backup.enabled=true to opt in"
}
if usesSQLServer() {
return "auto: off in sql-server mode"
}
@ -125,6 +119,21 @@ func clientServerShareFilesystem() bool {
// bd sessions don't need a chatty repeat on every command.
var autoBackupSkipNoticeOnce sync.Once
// autoBackupBackendForCommand returns the storage this command's auto-backup
// runs against, or ok=false when there is nothing it may back up.
func autoBackupBackendForCommand() (localBackupBackend, bool) {
if usesProxiedServer() {
return proxiedAutoBackupBackend()
}
if store == nil {
return nil, false
}
if lm, ok := storage.UnwrapStore(store).(storage.LifecycleManager); ok && lm.IsClosed() {
return nil, false
}
return directLocalBackup{store: store}, true
}
// maybeAutoBackup runs a Dolt-native backup if enabled and the throttle interval has passed.
// Called from PersistentPostRun after auto-commit.
func maybeAutoBackup(ctx context.Context) {
@ -139,10 +148,8 @@ func maybeAutoBackup(ctx context.Context) {
if !isBackupAutoEnabled() {
return
}
if store == nil {
return
}
if lm, ok := storage.UnwrapStore(store).(storage.LifecycleManager); ok && lm.IsClosed() {
backend, ok := autoBackupBackendForCommand()
if !ok {
return
}
@ -152,7 +159,11 @@ func maybeAutoBackup(ctx context.Context) {
// constructs is meaningless to the server — register fails on
// every command. Skip cleanly with a one-time INFO so operators
// know auto-backup is silent on purpose.
if !clientServerShareFilesystem() {
//
// Proxied workspaces do not consult the host: proxiedAutoBackupBackend
// has already required managed-local, where bd spawned the server on
// this filesystem itself.
if !usesProxiedServer() && !clientServerShareFilesystem() {
autoBackupSkipNoticeOnce.Do(func() {
if !isQuiet() && !jsonOutput {
fmt.Fprintln(os.Stderr,
@ -190,7 +201,7 @@ func maybeAutoBackup(ctx context.Context) {
}
// Change detection: skip if nothing changed
currentCommit, err := store.GetCurrentCommit(ctx)
currentCommit, err := backend.CurrentCommit(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: auto-backup skipped: failed to get current commit: %v\n", err)
return
@ -200,8 +211,21 @@ func maybeAutoBackup(ctx context.Context) {
return
}
// One backup at a time per workspace (backup_lock.go). Auto-backup never
// waits: a held lock means a backup is already running.
release, err := acquireBackupLock(0)
if err != nil {
if errors.Is(err, errBackupBusy) {
debug.Logf("backup: skipping — another backup is running\n")
return
}
fmt.Fprintf(os.Stderr, "Warning: auto-backup skipped: %v\n", err)
return
}
defer release()
// Run the backup (force=true since we already checked change detection above)
if _, err := runBackupExport(ctx, true); err != nil {
if _, err := runBackupExport(ctx, backend, true); err != nil {
if !isQuiet() && !jsonOutput {
fmt.Fprintf(os.Stderr, "Warning: auto-backup failed: %v\n", err)
}

View file

@ -0,0 +1,240 @@
//go:build cgo && unix
package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
"github.com/steveyegge/beads/internal/storage/dbproxy/server"
"github.com/steveyegge/beads/internal/workspacegate"
)
// autoBackupOptIn is the explicit opt-in a server-mode workspace needs, with
// the throttle collapsed so every command in a test is due.
var autoBackupOptIn = []string{"BD_BACKUP_ENABLED=true", "BD_BACKUP_INTERVAL=1ms"}
// TestManagedLocalProxiedAutoBackup is the claim this slice makes: on a
// managed-local proxied workspace, an explicit backup.enabled=true makes the
// post-command hook take a Dolt-native backup into .beads/backup, exactly as
// it does in direct mode — and that backup restores.
//
// Like the explicit-verb round trip next door, the proof is a restore, not an
// exit code: an auto-backup that wrote a manifest nobody can restore from would
// pass every other assertion here.
//
// Named TestManagedLocalProxied* so the proxied-local smoke lane runs it.
func TestManagedLocalProxiedAutoBackup(t *testing.T) {
requireManagedLocalProxiedEnv(t)
bd := buildEmbeddedBD(t)
p := bdManagedLocalInit(t, bd, "bkauto", 5*time.Minute)
autoDir := filepath.Join(p.beadsDir, "backup")
manifest := filepath.Join(autoDir, "manifest")
// Server mode defaults auto-backup OFF: many clients share one server.
// Nothing may be written without the opt-in.
bdProxiedCreate(t, bd, p.dir, "no opt-in, no backup", "-p", "2")
if _, err := os.Stat(manifest); err == nil {
t.Fatalf("auto-backup ran without an opt-in: %s exists", manifest)
}
// With the opt-in, one ordinary write is enough.
created := proxiedAutoBackupCreate(t, bd, p, "captured by auto-backup")
if _, err := os.Stat(manifest); err != nil {
t.Fatalf("opted-in auto-backup produced no manifest at %s: %v", manifest, err)
}
state := readProxiedAutoBackupState(t, bd, p)
if state.LastDoltCommit == "" || state.Timestamp.IsZero() {
t.Fatalf("auto-backup did not record its watermark: %+v", state)
}
wantIDs := proxiedIssueIDs(t, bd, p)
if !slices.Contains(wantIDs, created) {
t.Fatalf("precondition: %s missing from %v", created, wantIDs)
}
// A read-only command with nothing new to capture must not advance the
// watermark: change detection runs before the backup, as in direct mode.
if _, stderr, err := bdProxiedRunBuffersWithEnv(t, bd, p.dir, autoBackupOptIn, "list", "--json"); err != nil {
t.Fatalf("bd list with auto-backup opted in: %v\n%s", err, stderr)
}
if again := readProxiedAutoBackupState(t, bd, p); again.LastDoltCommit != state.LastDoltCommit {
t.Fatalf("auto-backup advanced with no new commit: %q -> %q", state.LastDoltCommit, again.LastDoltCommit)
}
// `bd dolt stop` opens no provider, so its post-run hook has nothing to
// back up — and must not relaunch the server it just stopped to find out.
if _, stderr, err := bdProxiedRunBuffersWithEnv(t, bd, p.dir, autoBackupOptIn, "dolt", "stop"); err != nil {
t.Fatalf("bd dolt stop: %v\n%s", err, stderr)
}
assertManagedProxiedQuiescent(t, p, "bd dolt stop with auto-backup opted in")
// Diverge from the backup, then restore it and compare.
after := bdProxiedCreate(t, bd, p.dir, "created after the auto-backup", "-p", "2")
out, err := bdProxiedRun(t, bd, p.dir, "backup", "restore", autoDir, "--force", "--json")
if err != nil {
t.Fatalf("bd backup restore %s --force: %v\n%s", autoDir, err, out)
}
gotIDs := proxiedIssueIDs(t, bd, p)
if slices.Contains(gotIDs, after.ID) {
t.Fatalf("restore kept %s, created after the auto-backup; got %v", after.ID, gotIDs)
}
if !slices.Equal(gotIDs, wantIDs) {
t.Fatalf("restored issues differ from the auto-backed-up set:\n got %v\n want %v", gotIDs, wantIDs)
}
}
// TestManagedLocalProxiedBackupRestoreRefusedWhileAttached pins the restore
// design choice: restore REPLACES the database, so it refuses while any other
// bd command is attached to the workspace, and in that case touches nothing —
// the serving child keeps running and the data stays as it was.
//
// "Attached" is what another bd process holds for its lifetime: a SHARED
// workspace gate. The test holds exactly that, in-process, rather than racing
// a second process into position.
func TestManagedLocalProxiedBackupRestoreRefusedWhileAttached(t *testing.T) {
requireManagedLocalProxiedEnv(t)
bd := buildEmbeddedBD(t)
p := bdManagedLocalInit(t, bd, "bkatt", 5*time.Minute)
bdProxiedCreate(t, bd, p.dir, "in the backup", "-p", "1")
dest := filepath.Join(t.TempDir(), "dolt-backup")
if out, err := bdProxiedRun(t, bd, p.dir, "backup", "init", dest); err != nil {
t.Fatalf("bd backup init: %v\n%s", err, out)
}
if out, err := bdProxiedRun(t, bd, p.dir, "backup", "sync"); err != nil {
t.Fatalf("bd backup sync: %v\n%s", err, out)
}
after := bdProxiedCreate(t, bd, p.dir, "not in the backup", "-p", "1")
before := proxiedIssueIDs(t, bd, p)
proxyPID := readManagedProxyPidFile(t, p)
backendPID := readManagedBackendPidFile(t, p)
if proxyPID == nil || backendPID == nil {
t.Fatal("precondition: the managed topology should be serving")
}
gates, err := buildWorkspaceGateSet(p.beadsDir)
if err != nil {
t.Fatalf("resolve workspace gates: %v", err)
}
held, err := workspacegate.AcquireAll(context.Background(), workspacegate.Shared,
workspacegate.Options{Reason: "test: attached bd client"}, gates...)
if err != nil {
t.Fatalf("hold the workspace gate: %v", err)
}
released := false
release := func() {
if !released {
released = true
if err := held.Release(); err != nil {
t.Errorf("release workspace gate: %v", err)
}
}
}
t.Cleanup(release)
stdout, _, err := bdProxiedRunBuffers(t, bd, p.dir, "backup", "restore", dest, "--force", "--json")
if err == nil {
t.Fatalf("bd backup restore succeeded while another bd client was attached:\n%s", stdout)
}
var refusal struct {
Error string `json:"error"`
}
if jerr := json.Unmarshal([]byte(stdout[strings.Index(stdout, "{"):]), &refusal); jerr != nil {
t.Fatalf("restore refusal is not JSON on stdout: %v\n%s", jerr, stdout)
}
if !strings.Contains(refusal.Error, "other bd commands are using this workspace") {
t.Fatalf("restore refusal does not say why: %q", refusal.Error)
}
// Refused means untouched: the serving topology is the same processes.
if pf := readManagedProxyPidFile(t, p); pf == nil || pf.Pid != proxyPID.Pid || !processAlive(pf.Pid) {
t.Fatalf("refused restore disturbed the proxy: before %+v, now %+v", proxyPID, pf)
}
if pf := readManagedBackendPidFile(t, p); pf == nil || pf.Pid != backendPID.Pid || !processAlive(pf.Pid) {
t.Fatalf("refused restore disturbed the dolt child: before %+v, now %+v", backendPID, pf)
}
release()
if got := proxiedIssueIDs(t, bd, p); !slices.Equal(got, before) || !slices.Contains(got, after.ID) {
t.Fatalf("refused restore changed the data:\n got %v\n want %v", got, before)
}
// Once the other client detaches, the same restore goes through.
if out, err := bdProxiedRun(t, bd, p.dir, "backup", "restore", dest, "--force", "--json"); err != nil {
t.Fatalf("bd backup restore after the client detached: %v\n%s", err, out)
}
if slices.Contains(proxiedIssueIDs(t, bd, p), after.ID) {
t.Fatalf("restore after detach did not replace the database: %s is still here", after.ID)
}
}
// proxiedAutoBackupCreate creates an issue with auto-backup opted in and
// returns its ID.
func proxiedAutoBackupCreate(t *testing.T, bd string, p proxiedProject, title string) string {
t.Helper()
stdout, stderr, err := bdProxiedRunBuffersWithEnv(t, bd, p.dir, autoBackupOptIn, "create", title, "-p", "2", "--silent")
if err != nil {
t.Fatalf("bd create with auto-backup opted in: %v\n%s", err, stderr)
}
if strings.Contains(stderr, "auto-backup") {
t.Fatalf("auto-backup reported a problem:\n%s", stderr)
}
id := strings.TrimSpace(stdout)
if id == "" {
t.Fatalf("bd create --silent printed no ID; stderr:\n%s", stderr)
}
return id
}
// readProxiedAutoBackupState reads the auto-backup watermark through
// `bd backup status --json`, the surface an operator uses.
func readProxiedAutoBackupState(t *testing.T, bd string, p proxiedProject) backupState {
t.Helper()
out, err := bdProxiedRun(t, bd, p.dir, "backup", "status", "--json")
if err != nil {
t.Fatalf("bd backup status --json: %v\n%s", err, out)
}
start := strings.Index(string(out), "{")
if start < 0 {
t.Fatalf("bd backup status --json produced no JSON:\n%s", out)
}
var status struct {
Backup backupState `json:"backup"`
}
if err := json.Unmarshal(out[start:], &status); err != nil {
t.Fatalf("parse backup status JSON: %v\n%s", err, out)
}
return status.Backup
}
// proxiedIssueIDs lists every issue ID in the workspace, sorted.
func proxiedIssueIDs(t *testing.T, bd string, p proxiedProject) []string {
t.Helper()
var ids []string
for _, issue := range bdProxiedListJSON(t, bd, p, "--all") {
ids = append(ids, issue.ID)
}
slices.Sort(ids)
return ids
}
// assertManagedProxiedQuiescent fails when either managed-local pidfile is
// present, i.e. something relaunched the topology.
func assertManagedProxiedQuiescent(t *testing.T, p proxiedProject, after string) {
t.Helper()
for _, name := range []string{proxy.PIDFileName, server.PIDFileName} {
if _, err := os.Stat(filepath.Join(p.proxyRoot, name)); err == nil {
t.Fatalf("after %s, %s is present: the topology was relaunched", after, name)
}
}
}

View file

@ -0,0 +1,142 @@
package main
import (
"context"
"database/sql"
"os"
"testing"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage/uow"
)
// fakeMaintenanceProvider is a proxied provider stand-in exposing the
// non-transactional seam proxied auto-backup runs on. It never opens a
// connection; the tests here only assert whether it would be used.
type fakeMaintenanceProvider struct {
uow.UnitOfWorkProvider
runs int
}
func (f *fakeMaintenanceProvider) RunNonTx(context.Context, func(context.Context, *sql.Conn) error) error {
f.runs++
return nil
}
func (f *fakeMaintenanceProvider) Close(context.Context) error { return nil }
// TestProxiedAutoBackupBackendFollowsLocality pins where auto-backup may run on
// a proxied workspace: exactly where `bd backup sync` is honored. Auto-backup
// registers a server-side backup remote, which on a server bd does not own is
// global to every client of it — the storm the explicit verbs refuse by design
// — so an implicit trigger must not get further than the explicit one. An
// unreadable sidecar resolves to the unknown topology and fails closed.
//
// Cannot be parallel: mutates the proxiedServerMode and uowProvider globals.
func TestProxiedAutoBackupBackendFollowsLocality(t *testing.T) {
for _, tc := range []struct {
name string
sidecar string
noProvider bool
wantBackend bool
}{
{name: "managed-local with an open provider", sidecar: `{}`, wantBackend: true},
{name: "managed-local with no provider open", sidecar: `{}`, noProvider: true},
{name: "external tcp", sidecar: `{"external":{"host":"db.example.com","port":3306}}`},
{name: "external unix socket", sidecar: `{"external":{"socket":"/tmp/dolt.sock"}}`},
{name: "unreadable sidecar", sidecar: `{not json`},
} {
t.Run(tc.name, func(t *testing.T) {
beadsDir := prepareBackupStatusTest(t)
proxiedServerMode = true
if err := os.WriteFile(configfile.ProxiedServerClientInfoPath(beadsDir), []byte(tc.sidecar), 0o600); err != nil {
t.Fatalf("write proxied sidecar: %v", err)
}
oldProvider := uowProvider
t.Cleanup(func() { uowProvider = oldProvider })
fake := &fakeMaintenanceProvider{}
uowProvider = fake
if tc.noProvider {
uowProvider = nil
}
backend, ok := autoBackupBackendForCommand()
if ok != tc.wantBackend {
t.Fatalf("autoBackupBackendForCommand() ok = %v, want %v", ok, tc.wantBackend)
}
if !ok {
return
}
if _, isProxied := backend.(proxiedLocalBackup); !isProxied {
t.Fatalf("backend = %T, want proxiedLocalBackup", backend)
}
if fake.runs != 0 {
t.Fatalf("resolving the backend ran %d maintenance call(s); it must not touch the server", fake.runs)
}
})
}
}
// TestPersistentPostRunProxiedRunsAutoBackup pins the wiring: the proxied arm
// of PersistentPostRunE calls the auto-backup hook, under the same gate the
// direct arm's maintenance net uses, plus previews.
//
// Before this hook existed the proxied arm only pruned the journal and closed
// the provider, so backup.enabled=true was inert on every proxied workspace.
//
// Cannot be parallel: mutates PersistentPostRunE's package globals.
func TestPersistentPostRunProxiedRunsAutoBackup(t *testing.T) {
for _, tc := range []struct {
name string
cmd func() *cobra.Command
readonly bool
want int
}{
{name: "ordinary command", cmd: func() *cobra.Command { return &cobra.Command{Use: "list"} }, want: 1},
{name: "strict readonly", cmd: func() *cobra.Command { return &cobra.Command{Use: "list"} }, readonly: true},
{name: "bd serve", cmd: func() *cobra.Command { return &cobra.Command{Use: serveCmdName} }},
{name: "preview", cmd: func() *cobra.Command {
c := &cobra.Command{Use: "list"}
c.Flags().Bool("dry-run", false, "")
if err := c.Flags().Set("dry-run", "true"); err != nil {
t.Fatalf("set --dry-run: %v", err)
}
return c
}},
} {
t.Run(tc.name, func(t *testing.T) {
t.Chdir(t.TempDir())
t.Setenv("BEADS_DIR", "")
oldStore, oldProvider := store, uowProvider
oldReadonly, oldProxied := readonlyMode, proxiedServerMode
oldRootCtx, oldRootCancel := rootCtx, rootCancel
oldSpan, oldProfile, oldTrace := commandSpan, profileFile, traceFile
oldBackup := runPostRunAutoBackup
t.Cleanup(func() {
store, uowProvider = oldStore, oldProvider
readonlyMode, proxiedServerMode = oldReadonly, oldProxied
rootCtx, rootCancel = oldRootCtx, oldRootCancel
commandSpan, profileFile, traceFile = oldSpan, oldProfile, oldTrace
runPostRunAutoBackup = oldBackup
})
calls := 0
runPostRunAutoBackup = func(context.Context) { calls++ }
store, uowProvider = nil, nil
readonlyMode = tc.readonly
proxiedServerMode = true
rootCtx, rootCancel = context.Background(), nil
commandSpan, profileFile, traceFile = nil, nil, nil
if err := rootCmd.PersistentPostRunE(tc.cmd(), nil); err != nil {
t.Fatalf("PersistentPostRunE: %v", err)
}
if calls != tc.want {
t.Fatalf("auto-backup hook calls = %d, want %d", calls, tc.want)
}
})
}
}

View file

@ -133,9 +133,9 @@ func TestIsBackupAutoEnabled(t *testing.T) {
// status` reachable on a proxied server for the first time, where it rendered
// the embedded-mode footnote "(auto: no git remote)". On any sql-server shape
// the value is false because usesSQLServer() short-circuits, whether or not a
// remote exists, so that reason was simply untrue; and on proxied specifically
// even an explicit backup.enabled=true changes nothing, because the proxied
// arm of PersistentPostRunE never calls runPostRunAutoBackup.
// remote exists, so that reason was simply untrue. Since the proxied post-run
// arm gained its auto-backup hook, an explicit opt-in on proxied is honored
// and needs no caveat.
//
// Cannot be parallel: mutates the proxiedServerMode global, primeHasGitRemote
// and env vars.
@ -191,23 +191,25 @@ func TestBackupAutoStatusNote(t *testing.T) {
wantNote: "",
},
{
name: "proxied default → names the missing hook, not the remote",
// Proxied is server mode: the default is off whether or not a
// remote exists, and the note names the opt-in that now works.
name: "proxied default → off, names the opt-in, not the remote",
envVal: "\x00",
hasRemote: true,
proxied: true,
wantEnabled: false,
wantNote: "auto-backup does not run on proxied-server; use 'bd backup sync'",
wantNote: "auto: off in proxied-server mode; set backup.enabled=true to opt in",
},
{
// The dead opt-in: enabled=true with nothing to run it. The note
// must survive an explicit source here, or status reports a bare
// "enabled=true" and the operator believes backups are happening.
name: "proxied explicit opt-in → still inert, still annotated",
// The proxied post-run arm runs auto-backup on managed-local (the
// only proxied shape status renders on), so an explicit opt-in is
// real there and needs no caveat.
name: "proxied explicit opt-in → honored, no note",
envVal: "true",
hasRemote: false,
proxied: true,
wantEnabled: true,
wantNote: "auto-backup does not run on proxied-server; use 'bd backup sync'",
wantNote: "",
},
}

View file

@ -142,6 +142,20 @@ Run 'bd backup init <path>' first to configure a destination.`,
}()
ctx := rootCtx
if usesProxiedServer() {
if err := requireLocalProxiedBackup("backup sync"); err != nil {
return err
}
}
release, err := acquireBackupLock(backupLockWait)
if err != nil {
if errors.Is(err, errBackupBusy) {
return HandleErrorRespectJSON("%v; retry when it completes", err)
}
return HandleErrorRespectJSON("%v", err)
}
defer release()
if usesProxiedServer() {
return runBackupSyncProxied(ctx)
}
@ -453,7 +467,7 @@ backup configuration. The backup data at the destination is not deleted.`,
}
// Also remove backup_export if it exists (auto-export may have created it at same URL)
_ = bs.BackupRemove(ctx, "backup_export")
_ = bs.BackupRemove(ctx, versioncontrolops.ExportBackupName)
// Remove local config
if path, err := doltBackupConfigPath(); err == nil {

View file

@ -122,9 +122,35 @@ func atomicWriteFile(path string, data []byte) error {
return nil
}
// localBackupBackend is what a Dolt-native backup into a local directory needs
// from storage: the commit it would capture, and the backup itself. The direct
// store supplies one (directLocalBackup) and so does the proxied provider
// (proxiedLocalBackup, backup_proxied_server.go).
type localBackupBackend interface {
CurrentCommit(ctx context.Context) (string, error)
BackupToDir(ctx context.Context, dir string) error
}
// directLocalBackup is localBackupBackend over an embedded or sql-server store.
type directLocalBackup struct {
store storage.DoltStorage
}
func (b directLocalBackup) CurrentCommit(ctx context.Context) (string, error) {
return b.store.GetCurrentCommit(ctx)
}
func (b directLocalBackup) BackupToDir(ctx context.Context, dir string) error {
bs, ok := storage.UnwrapStore(b.store).(storage.BackupStore)
if !ok {
return fmt.Errorf("storage backend does not support backup operations")
}
return bs.BackupDatabase(ctx, dir)
}
// runBackupExport performs a Dolt-native backup to .beads/backup/.
// Returns the updated state.
func runBackupExport(ctx context.Context, force bool) (*backupState, error) {
func runBackupExport(ctx context.Context, backend localBackupBackend, force bool) (*backupState, error) {
dir, err := backupDir()
if err != nil {
return nil, err
@ -137,7 +163,7 @@ func runBackupExport(ctx context.Context, force bool) (*backupState, error) {
// Change detection: skip if nothing changed (unless forced)
if !force {
currentCommit, err := store.GetCurrentCommit(ctx)
currentCommit, err := backend.CurrentCommit(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get current commit: %w", err)
}
@ -147,12 +173,7 @@ func runBackupExport(ctx context.Context, force bool) (*backupState, error) {
}
}
bs, ok := storage.UnwrapStore(store).(storage.BackupStore)
if !ok {
return nil, fmt.Errorf("storage backend does not support backup operations")
}
if err := bs.BackupDatabase(ctx, dir); err != nil {
if err := backend.BackupToDir(ctx, dir); err != nil {
// Persist the attempt time even on failure so the throttle
// interval (checked by maybeAutoBackup via state.Timestamp)
// applies to the next command. Without this, a sync that keeps
@ -170,7 +191,7 @@ func runBackupExport(ctx context.Context, force bool) (*backupState, error) {
}
// Update watermarks
currentCommit, err := store.GetCurrentCommit(ctx)
currentCommit, err := backend.CurrentCommit(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get current commit for state: %w", err)
}

View file

@ -63,13 +63,10 @@ func TestRunBackupExport_PersistsThrottleOnFailure(t *testing.T) {
t.Fatalf("precondition: expected zero timestamp, got %v", st.Timestamp)
}
oldStore := store
fake := &failingBackupStore{commit: "deadbeef", backupErr: errors.New("sync to backup: server too busy")}
store = fake
t.Cleanup(func() { store = oldStore })
before := time.Now().UTC()
if _, err := runBackupExport(context.Background(), true); err == nil {
if _, err := runBackupExport(context.Background(), directLocalBackup{store: fake}, true); err == nil {
t.Fatal("expected runBackupExport to return the sync error, got nil")
}
if fake.backupCalls != 1 {

71
cmd/bd/backup_lock.go Normal file
View file

@ -0,0 +1,71 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/lockfile"
)
// The backup lock serializes the two writers of a workspace's Dolt backup:
// post-command auto-backup and an explicit `bd backup sync`. Both run under the
// command's SHARED workspace gate, so the gate alone lets them overlap, and the
// auto-backup body does remove → add → sync on one fixed remote name — two
// concurrent runs race each other's registration and full-sync the database
// twice. Restore needs no part in it: it holds the workspace gate EXCLUSIVELY,
// which already excludes every command that could take this lock.
//
// The two callers wait differently, and deliberately:
// - auto-backup does not wait. Another backup running means one is already
// happening; it skips, and the throttle picks it up on a later command.
// - `bd backup sync` waits up to backupLockWait — the same bound an exclusive
// workspace-gate acquisition (restore) uses — and then fails with an error
// naming the busy lock, so an operator is never told it synced when it
// did not.
const backupLockFileName = "backup.lock"
// backupLockWait bounds how long `bd backup sync` waits for another backup.
// A var so tests can shorten it.
var backupLockWait = exclusiveGateWait
// errBackupBusy is returned when another backup holds the lock past the wait.
var errBackupBusy = errors.New("another backup is running for this workspace")
const backupLockPoll = 50 * time.Millisecond
// acquireBackupLock takes the workspace's backup lock, polling up to wait
// (zero means one non-blocking attempt). The returned release must be called.
func acquireBackupLock(wait time.Duration) (release func(), err error) {
beadsDir := beads.FindBeadsDir()
if beadsDir == "" {
return nil, fmt.Errorf("backup lock: %s", activeWorkspaceNotFoundError())
}
path := filepath.Join(beadsDir, backupLockFileName)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600) //nolint:gosec // path is the workspace's own lock file
if err != nil {
return nil, fmt.Errorf("backup lock: open %s: %w", path, err)
}
deadline := time.Now().Add(wait)
for {
err := lockfile.FlockExclusiveNonBlocking(f)
if err == nil {
return func() {
_ = lockfile.FlockUnlock(f)
_ = f.Close()
}, nil
}
if !lockfile.IsLocked(err) {
_ = f.Close()
return nil, fmt.Errorf("backup lock: %s: %w", path, err)
}
if !time.Now().Before(deadline) {
_ = f.Close()
return nil, errBackupBusy
}
time.Sleep(backupLockPoll)
}
}

133
cmd/bd/backup_lock_test.go Normal file
View file

@ -0,0 +1,133 @@
package main
import (
"context"
"errors"
"os"
"strings"
"testing"
"time"
)
// TestBackupLockSerializesBackups pins the lock itself: a second holder is
// refused while the first holds it, a bounded wait gives up with errBackupBusy,
// and releasing makes it available again.
//
// Cannot be parallel: prepareBackupStatusTest mutates package globals.
func TestBackupLockSerializesBackups(t *testing.T) {
prepareBackupStatusTest(t)
release, err := acquireBackupLock(0)
if err != nil {
t.Fatalf("first acquisition: %v", err)
}
if _, err := acquireBackupLock(0); !errors.Is(err, errBackupBusy) {
t.Fatalf("non-blocking acquisition while held = %v, want errBackupBusy", err)
}
start := time.Now()
if _, err := acquireBackupLock(150 * time.Millisecond); !errors.Is(err, errBackupBusy) {
t.Fatalf("bounded wait while held = %v, want errBackupBusy", err)
}
if waited := time.Since(start); waited < 100*time.Millisecond {
t.Fatalf("bounded wait returned after %s; it must poll for the wait it was given", waited)
}
release()
again, err := acquireBackupLock(0)
if err != nil {
t.Fatalf("acquisition after release: %v", err)
}
again()
}
// TestAutoBackupSkipsWhileAnotherBackupRuns: auto-backup never waits on the
// lock. With it held the backup does not run and the throttle state is left
// alone, so the next command retries; once released, the same call backs up.
func TestAutoBackupSkipsWhileAnotherBackupRuns(t *testing.T) {
prepareBackupStatusTest(t)
t.Setenv("BD_BACKUP_ENABLED", "true")
t.Setenv("BD_BACKUP_INTERVAL", "1ms")
t.Setenv("BD_GIT_HOOK", "")
initConfigForTest(t)
oldStore := store
fake := &failingBackupStore{commit: "c1"}
store = fake
t.Cleanup(func() { store = oldStore })
release, err := acquireBackupLock(0)
if err != nil {
t.Fatalf("hold the backup lock: %v", err)
}
maybeAutoBackup(context.Background())
if fake.backupCalls != 0 {
t.Fatalf("auto-backup ran %d backup(s) while another backup held the lock", fake.backupCalls)
}
dir, err := backupDir()
if err != nil {
t.Fatal(err)
}
if st, _ := loadBackupState(dir); !st.Timestamp.IsZero() {
t.Fatalf("a skipped auto-backup advanced the throttle to %v", st.Timestamp)
}
release()
maybeAutoBackup(context.Background())
if fake.backupCalls != 1 {
t.Fatalf("auto-backup ran %d backup(s) after the lock was released, want 1", fake.backupCalls)
}
}
// TestBackupSyncFailsWhenAnotherBackupHoldsTheLock: an explicit sync waits
// backupLockWait for another backup and then fails loudly, before touching the
// store — it never reports a sync it did not do.
func TestBackupSyncFailsWhenAnotherBackupHoldsTheLock(t *testing.T) {
prepareBackupStatusTest(t)
oldWait := backupLockWait
backupLockWait = 100 * time.Millisecond
t.Cleanup(func() { backupLockWait = oldWait })
oldStore := store
fake := &failingBackupStore{commit: "c1"}
store = fake
t.Cleanup(func() { store = oldStore })
release, err := acquireBackupLock(0)
if err != nil {
t.Fatalf("hold the backup lock: %v", err)
}
defer release()
var runErr error
stderr := captureStderrForBackupLockTest(t, func() {
runErr = backupSyncCmd.RunE(backupSyncCmd, nil)
})
if runErr == nil {
t.Fatal("bd backup sync succeeded while another backup held the lock")
}
if !strings.Contains(stderr, "another backup is running") {
t.Fatalf("sync refusal does not say why; stderr:\n%s", stderr)
}
if fake.backupCalls != 0 {
t.Fatalf("refused sync still reached the store (%d backup calls)", fake.backupCalls)
}
}
func captureStderrForBackupLockTest(t *testing.T, fn func()) string {
t.Helper()
stdioMutex.Lock()
defer stdioMutex.Unlock()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
old := os.Stderr
os.Stderr = w
fn()
os.Stderr = old
_ = w.Close()
buf := make([]byte, 1<<16)
n, _ := r.Read(buf)
_ = r.Close()
return string(buf[:n])
}

View file

@ -12,6 +12,7 @@ import (
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
"github.com/steveyegge/beads/internal/storage/issueops"
@ -47,19 +48,83 @@ const proxiedBackupTargetName = defaultDoltBackupName
// Defense in depth, not a second policy: the rule and its wording come from the
// same registry row the pre-provider gate consults, so the two cannot drift.
func requireLocalProxiedBackup(path string) error {
row, ok := LookupCapabilityRow(path, "")
rule, ok := proxiedBackupRule(path)
if !ok {
// Unreachable: TestProxyCapabilityRegistryCoversCommandTree fails the
// build for a backup path with no row. Refuse rather than guess.
return HandleErrorRespectJSON("%s has no proxied-server capability row", path)
}
rule := row.ruleFor(resolveProxiedTopology(beads.FindBeadsDir()))
if rule.Outcome == ProxyOutcomeHonored {
return nil
}
return HandleProxyCapabilityError(proxyCapabilityErrorFor(rule))
}
// proxiedBackupRule resolves the registry rule for a backup path on this
// workspace's proxied topology. ok is false only for a path with no row.
func proxiedBackupRule(path string) (proxyCapabilityRule, bool) {
row, ok := LookupCapabilityRow(path, "")
if !ok {
return proxyCapabilityRule{}, false
}
return row.ruleFor(resolveProxiedTopology(beads.FindBeadsDir())), true
}
// proxiedAutoBackupBackend returns the backend post-command auto-backup runs
// against on a proxied-server workspace, or ok=false when it must not run.
//
// Auto-backup is a `bd backup sync` nobody typed, so it is honored exactly
// where that verb is — the "backup sync" registry row, i.e. managed-local
// only. Anywhere else the backup remote it registers would land on a server bd
// does not own, where it is global to every client; that is the storm the
// explicit verbs refuse by design, and an implicit trigger must not reopen it.
//
// It never opens a provider of its own. A command that did not open one (or
// closed it, as restore does) has no live topology to back up, and opening one
// here would relaunch a server the command may just have stopped.
func proxiedAutoBackupBackend() (localBackupBackend, bool) {
if rule, ok := proxiedBackupRule("backup sync"); !ok || rule.Outcome != ProxyOutcomeHonored {
debug.Logf("backup: skipping auto-backup — proxied topology does not honor backup sync\n")
return nil, false
}
if uowProvider == nil {
return nil, false
}
mp, ok := uowProvider.(uow.MaintenanceProvider)
if !ok {
debug.Logf("backup: skipping auto-backup — provider %T has no maintenance seam\n", uowProvider)
return nil, false
}
return proxiedLocalBackup{provider: mp}, true
}
// proxiedLocalBackup is localBackupBackend over the proxied provider's
// non-transactional seam, the one `bd backup sync` uses.
//
// No pre-backup commit: change detection compares HEAD, and every proxied
// write transaction already ends in DOLT_COMMIT('-Am') (uow doltServerTx), so
// HEAD is where the data is — the invariant direct mode gets from running
// auto-commit just before auto-backup.
type proxiedLocalBackup struct {
provider uow.MaintenanceProvider
}
func (b proxiedLocalBackup) CurrentCommit(ctx context.Context) (string, error) {
var hash string
err := b.provider.RunNonTx(ctx, func(ctx context.Context, conn *sql.Conn) error {
var err error
hash, err = versioncontrolops.CurrentCommit(ctx, conn)
return err
})
return hash, err
}
func (b proxiedLocalBackup) BackupToDir(ctx context.Context, dir string) error {
return b.provider.RunNonTx(ctx, func(ctx context.Context, conn *sql.Conn) error {
return versioncontrolops.BackupToDir(ctx, conn, conn, dir)
})
}
// proxiedActiveDatabase asks the server which database the provider opened,
// rather than re-deriving it from the workspace config. The provider already
// resolved that question (metadata.json, then --database/--db), and a second
@ -203,7 +268,7 @@ func runBackupRemoveProxied(ctx context.Context) error {
return err
}
// auto-export may have registered a second remote at the same URL.
_ = versioncontrolops.BackupRemove(ctx, conn, "backup_export")
_ = versioncontrolops.BackupRemove(ctx, conn, versioncontrolops.ExportBackupName)
return nil
})
if err != nil {

View file

@ -18,7 +18,8 @@ import (
// pollute parsed values (e.g. an issue ID from `bd q`).
func runBDStdout(t *testing.T, binPath, workDir string, args ...string) string {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
// 2m, not 30s: a -race bd under a loaded CI host has taken >30s for one init.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, binPath, args...)
cmd.Dir = workDir

View file

@ -13,6 +13,7 @@ import (
"github.com/spf13/viper"
"github.com/steveyegge/beads/cmd/bd/doctor"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/ceiling"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/gitenv"
@ -1022,7 +1023,8 @@ func isValidRemoteURL(rawURL string) bool {
// findBeadsRepoRoot walks up from the given path to find the repo root (containing .beads)
func findBeadsRepoRoot(startPath string) string {
path := startPath
for {
bound := ceiling.For(startPath)
for !bound.Excludes(path) {
beadsDir := filepath.Join(path, ".beads")
if info, err := os.Stat(beadsDir); err == nil && info.IsDir() {
return path

View file

@ -11,6 +11,7 @@ import (
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/backends"
"github.com/steveyegge/beads/internal/storage/contextinfo"
@ -83,8 +84,13 @@ func TestContextRoutesNameOneWorkspaceTheSameWay(t *testing.T) {
}
t.Chdir(dir)
t.Setenv("BEADS_DIR", beadsDir)
// git caches the cwd's repository context process-wide; a test
// that ran earlier from a non-repo directory would otherwise make
// this fresh repo look like "not a git repository".
git.ResetCaches()
beads.ResetCaches()
t.Cleanup(beads.ResetCaches)
t.Cleanup(git.ResetCaches)
provider, err := contextinfo.NewContextProvider(dir, Version).ContextUseCase().GetContextInfo(t.Context())
if err != nil {

View file

@ -6,6 +6,9 @@ import (
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/git"
)
// TestCreateRemoteRepoSkipsLocalDatabaseGuard is a regression test for a gap
@ -54,6 +57,18 @@ func TestCreateRemoteRepoSkipsLocalDatabaseGuard(t *testing.T) {
t.Fatalf("chdir(%q): %v", noBeadsCwd, err)
}
t.Cleanup(func() { _ = os.Chdir(originalWD) })
// git and beads cache the cwd's repository and workspace process-wide. Start
// from caches that describe noBeadsCwd: a cache an earlier test filled from
// the package directory (a checkout, or a worktree whose main repository has
// a .beads) would hand create that workspace instead. Reset again on the
// way out, before the cwd is restored, so the next test does not inherit
// this directory's answers.
git.ResetCaches()
beads.ResetCaches()
t.Cleanup(func() {
git.ResetCaches()
beads.ResetCaches()
})
// Hide the `dolt` CLI so remotecache.Ensure fails fast and
// deterministically in CGO-enabled builds, without attempting a real network

View file

@ -92,6 +92,37 @@ func TestProxiedServerDeferUndefer(t *testing.T) {
}
})
t.Run("undefer_clears_stray_defer_until_when_status_not_deferred", func(t *testing.T) {
// Mirrors TestEmbeddedUndefer's ga-bq3w5 coverage on the proxied-server
// route (runUndeferProxiedServer): a status that is neither "open" nor
// "deferred" makes the conditional status write observable — writing
// status=open here would be a real change, not a no-op.
j := bdProxiedCreate(t, bd, p.dir, "Stray future defer on in_progress issue", "--type", "task")
if out, err := bdProxiedRun(t, bd, p.dir, "update", j.ID, "--status", "in_progress", "--defer", "2099-01-01"); err != nil {
t.Fatalf("update --status in_progress --defer: %v\n%s", err, out)
}
status, deferUntil := showStatus(t, j.ID)
if status != string(types.StatusInProgress) || deferUntil == nil {
t.Fatalf("precondition: expected status=in_progress with defer_until set, got status=%q defer_until=%v", status, deferUntil)
}
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "undefer", j.ID)
if err != nil {
t.Fatalf("undefer: %v\nstdout=%s\nstderr=%s", err, stdout, stderr)
}
if strings.Contains(stdout+stderr, "is not deferred") {
t.Errorf("undefer refused instead of clearing the stray defer_until: stdout=%s stderr=%s", stdout, stderr)
}
status, deferUntil = showStatus(t, j.ID)
if status != string(types.StatusInProgress) {
t.Errorf("expected status to stay in_progress (not clobbered to open), got %q", status)
}
if deferUntil != nil {
t.Errorf("expected defer_until cleared after undefer, got %v", deferUntil)
}
})
t.Run("defer_is_idempotent", func(t *testing.T) {
d := bdProxiedCreate(t, bd, p.dir, "Idempotent defer", "--type", "task")
if out, err := bdProxiedRun(t, bd, p.dir, "defer", d.ID); err != nil {

View file

@ -17,6 +17,12 @@ import (
type deferProxiedResult struct {
issues []*types.Issue
errs []string
// wasDeferred tracks, in lockstep with issues, whether each undeferred
// issue was actually status=deferred (status flips to open) versus just
// carrying a stray defer_until on some other status (status untouched,
// only the timestamp cleared — ga-bq3w5). Unused by runDeferProxiedServer.
wasDeferred []bool
}
func proxiedUpdateByID(ctx context.Context, uw uow.UnitOfWork, id string, isWisp bool, updates map[string]any) error {
@ -125,21 +131,29 @@ func runUndeferProxiedServer(ctx context.Context, args []string) error {
continue
}
fullID := issue.ID
if issue.Status != types.StatusDeferred {
// Gate on defer_until, not status alone (ga-bq3w5) — mirrors the
// embedded-backend fix in undefer.go. See that file's comment for
// the full rationale.
wasDeferred := issue.Status == types.StatusDeferred
if !wasDeferred && issue.DeferUntil == nil {
r.errs = append(r.errs, fmt.Sprintf("%s is not deferred (status: %s)", fullID, string(issue.Status)))
continue
}
updates := map[string]interface{}{
"status": string(types.StatusOpen),
"defer_until": nil,
}
if wasDeferred {
updates["status"] = string(types.StatusOpen)
}
if uerr := proxiedUpdateByID(ctx, uw, fullID, isWisp, updates); uerr != nil {
r.errs = append(r.errs, fmt.Sprintf("Error undeferring %s: %v", fullID, uerr))
continue
}
if updated := proxiedGetByID(ctx, uw, fullID, isWisp); updated != nil {
r.issues = append(r.issues, updated)
r.wasDeferred = append(r.wasDeferred, wasDeferred)
}
}
if len(r.issues) == 0 {
@ -162,8 +176,12 @@ func runUndeferProxiedServer(ctx context.Context, args []string) error {
}
}
} else {
for _, iss := range res.issues {
fmt.Printf("%s Undeferred %s (now open)\n", ui.RenderPass("*"), iss.ID)
for i, iss := range res.issues {
if i < len(res.wasDeferred) && res.wasDeferred[i] {
fmt.Printf("%s Undeferred %s (now open)\n", ui.RenderPass("*"), iss.ID)
} else {
fmt.Printf("%s Cleared stale defer_until on %s (status unchanged: %s)\n", ui.RenderPass("*"), iss.ID, string(iss.Status))
}
}
}

View file

@ -62,6 +62,7 @@ go_library(
"//internal/doltremote",
"//internal/doltserver",
"//internal/git",
"//internal/gitenv",
"//internal/gitignore",
"//internal/storage",
"//internal/storage/dberrors",
@ -154,6 +155,7 @@ go_test(
"//internal/configfile",
"//internal/doltserver",
"//internal/git",
"//internal/gitenv",
"//internal/storage/dolt",
"//internal/storage/doltutil",
"//internal/storage/embeddeddolt",

View file

@ -89,7 +89,9 @@ func testMainInner(m *testing.M) int {
}
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
if testutil.DoltUnavailableForTestMain(err) {
return 1
}
} else {
defer testutil.TerminateDoltContainer()
port := testutil.DoltContainerPortInt()

View file

@ -1,4 +1,5 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
go_library(
name = "fix",
@ -96,6 +97,64 @@ go_test(
],
)
# The Dolt-backed half of fix_test, as pr.yml's "Test doctor/fix (Dolt-backed,
# hard-require container)" job runs it: TestMain starts a dolt sql-server
# container through testcontainers, and BEADS_FIX_REQUIRE_DOLT=1 turns a
# missing container into a failure instead of a silent skip. fix_test itself
# stays untagged: under --config=prcore (BEADS_TEST_SKIP=dolt, like pr-core's
# `go test`) it never touches docker and runs anywhere.
#
# Tags:
# requires-docker: needs a docker daemon; excluded from --config=prcore/ci,
# selected by --config=docker.
# no-remote-exec: the daemon is on the host, not on remote workers.
sh_test(
name = "fix_docker_test",
size = "large",
srcs = ["//tools/bazel:go_test_variant.sh"],
args = ["$(rootpath :fix_test)"],
data = [":fix_test"],
# Pinned to the container backend (the docker lane is the A/B control);
# rule env beats --test_env, so =local on the command line cannot flip it.
env = {
"BEADS_FIX_REQUIRE_DOLT": "1",
"BEADS_TEST_DOLT_SERVER": "container",
},
tags = [
"no-remote-exec",
"requires-docker",
],
)
# The Dolt-backed half of fix_test, as pr.yml's "Test doctor/fix (Dolt-backed,
# hard-require container)" step runs it, on the dolt-server lane: TestMain's
# server is a local `dolt sql-server` child of the test process, and
# BEADS_FIX_REQUIRE_DOLT=1 keeps the suite from skipping when it cannot start.
# fix_docker_test is the A/B control. 4 shards: unsharded it was the lane's
# critical path (~4 min remotely).
#
# Tags:
# dolt-server: starts hermetic dolt sql-servers from the pinned dolt in the
# test wrapper's runfiles (BEADS_TEST_DOLT_SERVER=local); excluded from
# --config=prcore/ci, selected by --config=doltserver. Runs remotely: no
# docker, and a missing server fails (REQUIRE=1) instead of skipping, so
# a cached pass is as good as a fresh one.
sh_test(
name = "fix_dolt_test",
size = "large",
srcs = ["//tools/bazel:go_test_variant.sh"],
args = ["$(rootpath :fix_test)"],
data = [":fix_test"],
env = {
"BEADS_FIX_REQUIRE_DOLT": "1",
"BEADS_TEST_DOLT_SERVER": "local",
"BEADS_TEST_REQUIRE_DOLT_CONTAINER": "1",
"GOMAXPROCS": "4",
},
shard_count = 4,
tags = ["dolt-server"],
)
# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) ---
filegroup(

View file

@ -6,6 +6,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/beads"
)
@ -38,9 +39,8 @@ func getBdBinary() (string, error) {
}
// Check if we're running as a test binary - this prevents fork bombs
// when tests call functions that execute bd subcommands
baseName := filepath.Base(exe)
if strings.HasSuffix(baseName, ".test") || strings.Contains(baseName, ".test.") {
// when tests call functions that execute bd subcommands.
if isTestBinary(exe) {
return "", ErrTestBinary
}
@ -56,6 +56,18 @@ func getBdBinary() (string, error) {
return bdPath, nil
}
// isTestBinary reports whether exe is a Go test binary. testing.Testing() is
// the authoritative signal: both `go test` and rules_go link test binaries with
// it set, whatever the file is called (Bazel names them <target>_test, not
// <pkg>.test). The name check is kept for binaries built by other means.
func isTestBinary(exe string) bool {
if testing.Testing() {
return true
}
baseName := filepath.Base(exe)
return strings.HasSuffix(baseName, ".test") || strings.Contains(baseName, ".test.")
}
// validateBeadsWorkspace ensures the path is a valid beads workspace before
// attempting any fix operations. This prevents path traversal attacks.
func validateBeadsWorkspace(path string) error {

View file

@ -103,6 +103,21 @@ func TestGetBdBinary_Errors(t *testing.T) {
})
}
// TestGetBdBinary_DetectsTestBinaryByAnyName guards against a fork bomb: a test
// binary must never be returned as "bd", whatever it is named. Bazel names
// test binaries <target>_test (no ".test" suffix); before the guard used
// testing.Testing(), each fix test re-ran the whole suite recursively there.
func TestGetBdBinary_DetectsTestBinaryByAnyName(t *testing.T) {
if _, err := getBdBinary(); !errors.Is(err, ErrTestBinary) {
t.Fatalf("getBdBinary() in a test binary: got err %v, want ErrTestBinary", err)
}
for _, name := range []string{"/x/fix.test", "/x/fix_test", "/x/bd"} {
if !isTestBinary(name) {
t.Errorf("isTestBinary(%q) = false inside a test binary", name)
}
}
}
// TestFilePermissionErrors tests handling of file permission issues
func TestFilePermissionErrors(t *testing.T) {
if os.Getuid() == 0 {

View file

@ -26,7 +26,9 @@ func testMainInner(m *testing.M) int {
// database-name firewall when they connect to the spawned test server.
os.Setenv("BEADS_TEST_SERVER", "1")
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
if testutil.DoltUnavailableForTestMain(err) {
return 1
}
} else {
defer testutil.TerminateDoltContainer()
}

View file

@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
@ -18,6 +19,7 @@ import (
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/execenv"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/types"
@ -1072,6 +1074,10 @@ func gitIndexLockPath(path string, env []string) (string, error) {
return filepath.Join(gitDir, "index.lock"), nil
}
// hookEnvGOOS lets both hook filters exercise either key policy on every test host.
// Process-wide seam: tests that swap it must not run in parallel.
var hookEnvGOOS = runtime.GOOS
// scrubGitHookEnv returns env with the GIT_* variables that can poison
// git's repo/worktree auto-discovery or object-store resolution removed,
// so git falls back to auto-discovery from cwd. The scrub is
@ -1090,34 +1096,28 @@ func gitIndexLockPath(path string, env []string) (string, error) {
// GIT_CONFIG namespace, which includes _COUNT, _KEY_n, _VALUE_n,
// _GLOBAL, _SYSTEM, _NOSYSTEM, and the legacy GIT_CONFIG itself.
func scrubGitHookEnv(env []string) []string {
return scrubGitHookEnvForOS(env, hookEnvGOOS)
}
func scrubGitHookEnvForOS(env []string, goos string) []string {
// The GIT_CONFIG prefix (no trailing "=") is intentional: it matches
// GIT_CONFIG=, GIT_CONFIG_COUNT=, GIT_CONFIG_KEY_n=, GIT_CONFIG_VALUE_n=,
// GIT_CONFIG_PARAMETERS=, GIT_CONFIG_GLOBAL=, GIT_CONFIG_SYSTEM=, and
// GIT_CONFIG_NOSYSTEM= — the whole family — in one entry. No standard
// git env var starts with GIT_CONFIG that we want to preserve.
prefixes := []string{
"GIT_DIR=",
"GIT_WORK_TREE=",
"GIT_INDEX_FILE=",
"GIT_COMMON_DIR=",
"GIT_PREFIX=",
"GIT_OBJECT_DIRECTORY=",
"GIT_ALTERNATE_OBJECT_DIRECTORIES=",
"GIT_CEILING_DIRECTORIES=",
"GIT_DISCOVERY_ACROSS_FILESYSTEM=",
"GIT_CONFIG",
}
out := make([]string, 0, len(env))
for _, e := range env {
skip := false
for _, p := range prefixes {
if strings.HasPrefix(e, p) {
skip = true
break
}
}
if !skip {
out = append(out, e)
cleaned := execenv.WithoutForOS(env, goos,
"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR",
"GIT_PREFIX", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES",
"GIT_CEILING_DIRECTORIES", "GIT_DISCOVERY_ACROSS_FILESYSTEM",
)
// Without owns a fresh slice, so this second filter cannot mutate env.
out := cleaned[:0]
for _, entry := range cleaned {
// Prefix policy also drops valueless GIT_CONFIG entries. Without
// intentionally preserves valueless exact keys such as GIT_DIR.
key := execenv.EntryKey(entry)
if !execenv.KeyHasPrefixForOS(key, "GIT_CONFIG", goos) {
out = append(out, entry)
}
}
return out

76
cmd/bd/hook_env_test.go Normal file
View file

@ -0,0 +1,76 @@
package main
import (
"slices"
"testing"
)
func TestFilterChainedHookEnvUsesHostKeyIdentity(t *testing.T) {
checkHookEnvForBothHosts(t, checkChainedHookEnv)
}
// Callers and subtests must remain serial while they swap the process-wide seam.
func checkHookEnvForBothHosts(t *testing.T, check func(*testing.T)) {
t.Helper()
for _, goos := range []string{"linux", "windows"} {
t.Run(goos, func(t *testing.T) {
previous := hookEnvGOOS
hookEnvGOOS = goos
t.Cleanup(func() { hookEnvGOOS = previous })
check(t)
})
}
}
func checkChainedHookEnv(t *testing.T) {
t.Helper()
env := []string{
"BD_GIT_HOOK=canonical", "bd_git_hook=mixed", "BD_GİT_HOOK=unicode-case",
"BD_GIT_HOOK=unicode-case", "BD_GIT_HOOK=",
"BD_GıT_HOOK=distinct", "KEEP=first", "KEEP=BD_GIT_HOOK=1",
"BD_GIT_HOOK", `=C:=C:\work`,
}
original := slices.Clone(env)
want := []string{"BD_GıT_HOOK=distinct", "KEEP=first", "KEEP=BD_GIT_HOOK=1", "BD_GIT_HOOK", `=C:=C:\work`}
if hookEnvGOOS != "windows" {
want = append([]string{"bd_git_hook=mixed", "BD_GİT_HOOK=unicode-case", "BD_GIT_HOOK=unicode-case"}, want...)
}
if got := filterEnv(env, "BD_GIT_HOOK"); !slices.Equal(got, want) {
t.Errorf("chained-hook environment = %q, want %q", got, want)
}
if !slices.Equal(env, original) {
t.Errorf("input environment mutated: %q", env)
}
}
func TestScrubGitHookEnvUsesHostKeyIdentity(t *testing.T) {
checkHookEnvForBothHosts(t, checkGitHookEnv)
}
func checkGitHookEnv(t *testing.T) {
t.Helper()
env := []string{
"GIT_DIR=canonical", "git_dir=mixed", "GİT_DİR=unicode-case", "GIT_DıR=distinct",
"GIT_WORK_TREE=unicode-case", "GIT_DIſCOVERY_ACROSS_FILESYSTEM=distinct", "GIT_CONFIG=",
"GIT_CONFIG_COUNT=1", "git_config_parameters=mixed", "gİt_config_count=unicode-case",
"GIT_CONFIG_UNSET", "git_config_unset", "KEEP=first", "KEEP=GIT_CONFIG_COUNT=1",
"GIT_AUTHOR_NAME=author", "GIT_OPTIONAL_LOCKS=1", "GIT_DIR", "MALFORMED", `=C:=C:\work`,
}
original := slices.Clone(env)
want := []string{"GIT_DıR=distinct"}
if hookEnvGOOS != "windows" {
want = append([]string{"git_dir=mixed", "GİT_DİR=unicode-case"}, want...)
want = append(want, "GIT_WORK_TREE=unicode-case")
}
want = append(want, "GIT_DIſCOVERY_ACROSS_FILESYSTEM=distinct")
if hookEnvGOOS != "windows" {
want = append(want, "git_config_parameters=mixed", "gİt_config_count=unicode-case", "git_config_unset")
}
want = append(want, "KEEP=first", "KEEP=GIT_CONFIG_COUNT=1", "GIT_AUTHOR_NAME=author", "GIT_OPTIONAL_LOCKS=1", "GIT_DIR", "MALFORMED", `=C:=C:\work`)
if got := scrubGitHookEnv(env); !slices.Equal(got, want) {
t.Errorf("Git-hook environment = %q, want %q", got, want)
}
if !slices.Equal(env, original) {
t.Errorf("input environment mutated: %q", env)
}
}

View file

@ -14,6 +14,7 @@ import (
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/execenv"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/gitenv"
"github.com/steveyegge/beads/internal/metrics"
@ -819,10 +820,10 @@ var hooksListCmd = &cobra.Command{
// allowTracked exempts shared installs (.beads-hooks/ is deliberately
// committed).
func guardHookWritePath(hookPath string, allowTracked bool) error {
return guardHookWritePathWithProbe(hookPath, allowTracked, isGitTrackedFile)
return guardHookWritePathWithProbe(hookPath, allowTracked, gitTrackedFileContext)
}
func guardHookWritePathWithProbe(hookPath string, allowTracked bool, tracked func(string) bool) error {
func guardHookWritePathWithProbe(hookPath string, allowTracked bool, tracked func(string) string) error {
fi, err := os.Lstat(hookPath)
if err != nil {
if os.IsNotExist(err) {
@ -846,8 +847,16 @@ func guardHookWritePathWithProbe(hookPath string, allowTracked bool, tracked fun
// foreign tracked file dirties every clone that shares it (the wy-81fnur
// incident). A bd-owned hook the user chose to commit (e.g. a team-shared
// .beads/hooks/) is bd's to maintain — same policy as shared installs.
if tracked(hookPath) && !isBdOwnedHookFile(hookPath) {
return fmt.Errorf("%s is tracked by git and not a bd-managed hook; bd will not modify committed files it does not own\nUntrack it (git rm --cached) or move hooks to an untracked directory and re-run", hookPath)
if proof := tracked(hookPath); proof != "" && !isBdOwnedHookFile(hookPath) {
// `git rm --cached` only aims at the index that supplied the proof if the
// operator's shell still carries the same routing bd saw, so the inherited
// case names clearing that routing as the remedy instead. Both remediations
// are fixed strings; neither exposes an inherited value.
remediation := "Untrack it (git rm --cached) or move hooks to an untracked directory and re-run"
if proof == trackedProofInheritedIndex {
remediation = "Clear the inherited Git routing environment and re-run, or untrack it (git rm --cached) in the index that routing selects"
}
return fmt.Errorf("%s is tracked by git (%s) and not a bd-managed hook; bd will not modify committed files it does not own\n%s", hookPath, proof, remediation)
}
return nil
}
@ -866,37 +875,65 @@ func isBdOwnedHookFile(path string) bool {
return err == nil && versionInfo.IsBdHook
}
// Fixed labels naming which index supplied a tracked-file proof. They are
// constants so the refusal can branch on the evidence source without
// reproducing the wording.
const (
trackedProofContainingIndex = "containing repository index"
trackedProofInheritedIndex = "inherited Git index"
)
// isGitTrackedFile reports whether either the containing repository or the
// inherited Git context tracks path. If neither probe succeeds, errors count
// as untracked — the guard only blocks writes it can prove are unsafe.
//
// This and isGitTrackedFileWithEnv are test-only conveniences. Production
// callers should use the Context variants, whose label feeds the refusal
// diagnostic; the boolean form discards it.
func isGitTrackedFile(path string) bool {
return gitTrackedFileContext(path) != ""
}
func gitTrackedFileContext(path string) string {
inherited := os.Environ()
return isGitTrackedFileWithEnv(path, gitenv.ScrubRouting(inherited), inherited)
return gitTrackedFileContextWithEnv(path, gitenv.ScrubRouting(inherited), inherited)
}
func isGitTrackedFileWithEnv(path string, clean, inherited []string) bool {
return gitTrackedFileContextWithEnv(path, clean, inherited) != ""
}
// gitTrackedFileContextWithEnv returns a fixed label for the first successful
// proof. It never includes inherited environment values or Git output.
func gitTrackedFileContextWithEnv(path string, clean, inherited []string) string {
dir := filepath.Dir(path)
base := filepath.Base(path)
// Check the containing repository first so inherited routing cannot hide a
// tracked hook. The fallback preserves bare work trees and trusted config.
// Exit 1 is git's "repository reached, path is not tracked" answer and is
// final, so the second probe runs only when the first environment failed
// for a configuration reason (no reachable repository) rather than spawning
// a second `git ls-files` on every ordinary miss.
for _, env := range [][]string{clean, inherited} {
// tracked hook. The fallback preserves bare work trees and trusted config,
// and whichever index answers first is sufficient to refuse the write, even
// if the other view does not track the path. The fallback is reached only
// when the clean probe failed for a configuration reason (no reachable
// repository): exit 1 is git's "repository reached, path is not tracked"
// answer and is final, so a repository that reports the path untracked ends
// the search instead of spawning a second `git ls-files` on every ordinary
// miss.
for index, env := range [][]string{clean, inherited} {
// #nosec G204 G702 - fixed "git" command; dir/base come from the hooks
// directory bd itself resolved, not user input
cmd := exec.Command("git", "-C", dir, "ls-files", "--error-unmatch", "--", base)
cmd.Env = env
err := cmd.Run()
if err == nil {
return true
if index == 0 {
return trackedProofContainingIndex
}
return trackedProofInheritedIndex
}
if exit, ok := err.(*exec.ExitError); ok && exit.ExitCode() == 1 {
return false
return ""
}
}
return false
return ""
}
func installHooksWithOptions(hookNames []string, force bool, shared bool, chain bool, beadsHooks bool) error {
@ -1036,9 +1073,11 @@ func installHooksAt(hookNames []string, force, shared, chain, beadsHooks bool, s
}
}
tracked := isGitTrackedFile
tracked := gitTrackedFileContext
if selected != nil {
tracked = func(path string) bool { return isGitTrackedFileWithEnv(path, selected.env, selected.inheritedEnv) }
tracked = func(path string) string {
return gitTrackedFileContextWithEnv(path, selected.env, selected.inheritedEnv)
}
}
// Refuse the whole install up front if any target is unsafe to write —
// stopping midway through the loop would leave hooks half-installed.
@ -1933,6 +1972,8 @@ func hookLinkedWorktreePrimaryRoot(hookRoot string) string {
if hookRoot == "" {
return ""
}
// #nosec G702 - fixed "git" command; args are constant subcommands plus an internal
// hookRoot derived from the hook's own beads dir, never attacker-controlled input.
cmd := exec.Command("git", "-C", hookRoot, "rev-parse", "--git-common-dir")
// Scrub the inherited GIT_* vars so git describes hookRoot itself rather
// than whatever repository GIT_DIR happens to name.
@ -2161,14 +2202,11 @@ func warnJSONLWithoutDoltRemote(reason string) {
// filterEnv returns a copy of env with entries matching the given key removed.
func filterEnv(env []string, key string) []string {
prefix := key + "="
out := make([]string, 0, len(env))
for _, e := range env {
if !strings.HasPrefix(e, prefix) {
out = append(out, e)
}
}
return out
return filterEnvForOS(env, key, hookEnvGOOS)
}
func filterEnvForOS(env []string, key, goos string) []string {
return execenv.WithoutForOS(env, goos, key)
}
// runPostMergeHook runs chained hooks after merge, then runs the legacy

View file

@ -14,36 +14,6 @@ import (
"github.com/stretchr/testify/require"
)
func TestStandaloneHookCommandKeepsTrackedGuard(t *testing.T) {
for _, mode := range []string{"default", "shared"} {
t.Run(mode, func(t *testing.T) {
_, _, commonExclude, _ := newInitExcludeRepos(t)
target := filepath.Dir(filepath.Dir(filepath.Dir(commonExclude)))
t.Chdir(target)
hooksDir := filepath.Join(target, ".beads-hooks")
require.NoError(t, os.Mkdir(hooksDir, 0755))
path := filepath.Join(hooksDir, "pre-commit")
const foreign = "#!/bin/sh\necho tracked owner\n"
require.NoError(t, os.WriteFile(path, []byte(foreign), 0755))
initExcludeGit(t, target, "add", ".beads-hooks/pre-commit")
initExcludeGit(t, target, "config", "--local", "core.hooksPath", hooksDir)
setStandaloneHookMode(t, mode)
var err error
stderr := captureStderr(t, func() { err = hooksInstallCmd.RunE(hooksInstallCmd, nil) })
if mode == "shared" {
require.NoError(t, err)
require.Contains(t, string(readInitHooksFile(t, path)), foreign)
require.Contains(t, string(readInitHooksFile(t, path)), hookSectionBeginPrefix)
} else {
require.Equal(t, &exitError{Code: 1}, err)
require.Contains(t, stderr, "tracked by git")
require.Equal(t, foreign, string(readInitHooksFile(t, path)))
require.NoFileExists(t, filepath.Join(hooksDir, "post-merge"))
}
})
}
}
func TestGuardHookWritePathIgnoresInheritedGitRouting(t *testing.T) {
runGit := func(repo string, args ...string) {
t.Helper()
@ -83,6 +53,7 @@ func TestGuardHookWritePathIgnoresInheritedGitRouting(t *testing.T) {
name string
env map[string]string
}{
{"both", map[string]string{"GIT_DIR": filepath.Join(target, ".git"), "GIT_WORK_TREE": target}},
{"repository", map[string]string{"GIT_DIR": filepath.Join(decoy, ".git")}},
{"worktree", map[string]string{"GIT_DIR": filepath.Join(decoy, ".git"), "GIT_WORK_TREE": decoy}},
{"index", map[string]string{"GIT_INDEX_FILE": filepath.Join(decoy, ".git", "index")}},
@ -92,9 +63,16 @@ func TestGuardHookWritePathIgnoresInheritedGitRouting(t *testing.T) {
for key, value := range tc.env {
t.Setenv(key, value)
}
if tc.name == "both" {
probe := exec.Command("git", "-C", hooksDir, "ls-files", "--error-unmatch", "--", "pre-commit")
out, err := probe.CombinedOutput()
require.NoError(t, err, "inherited index must also prove tracking: %s", out)
}
if !isGitTrackedFile(foreign) {
t.Error("inherited routing hid the tracked hook")
}
require.Equal(t, "containing repository index", gitTrackedFileContext(foreign), "clean proof takes precedence, including when both views track it")
require.ErrorContains(t, guardHookWritePath(foreign, false), "tracked by git (containing repository index)")
if err := guardHookWritePath(foreign, false); err == nil || !strings.Contains(err.Error(), "tracked by git") {
t.Errorf("expected tracked-file refusal, got %v", err)
}
@ -110,6 +88,7 @@ func TestGuardHookWritePathIgnoresInheritedGitRouting(t *testing.T) {
if isGitTrackedFile(untracked) {
t.Error("untracked hook reported as tracked")
}
require.Empty(t, gitTrackedFileContext(untracked))
if err := guardHookWritePath(untracked, false); err != nil {
t.Errorf("untracked hook refused: %v", err)
}
@ -554,6 +533,22 @@ func TestInitHooksContextGuardPreservesOwnership(t *testing.T) {
want = "symlink"
}
require.ErrorContains(t, err, want)
if name == "captured_fallback" {
require.ErrorContains(t, err, "tracked by git (inherited Git index)")
// The remediation follows the proof: `git rm --cached` from the
// operator's shell aims at whichever index that shell routes to,
// so the inherited case has to name clearing the routing instead.
require.ErrorContains(t, err, "Clear the inherited Git routing environment and re-run")
for _, entry := range hooks.inheritedEnv {
if strings.HasPrefix(entry, "GIT_DIR=") {
require.NotContains(t, err.Error(), strings.TrimPrefix(entry, "GIT_DIR="))
}
}
} else if name == "tracked" {
require.ErrorContains(t, err, "tracked by git (containing repository index)")
require.ErrorContains(t, err, "Untrack it (git rm --cached) or move hooks to an untracked directory")
require.NotContains(t, err.Error(), "Clear the inherited Git routing environment")
}
require.Equal(t, content, string(readInitHooksFile(t, hook)))
for _, other := range []string{"post-merge", "pre-commit.backup"} {
_, err := os.Lstat(filepath.Join(hooksDir, other))

View file

@ -340,7 +340,19 @@ func resolveInitHooksContext(workDir, beadsDir string) (*initHooksContext, error
env := gitenv.ScrubRouting(inherited)
paths, err := git.ResolveHooksContext(workDir, env)
if err != nil {
return nil, err
// A repository whose work tree does not resolve -- a bare repository, or
// a directory such as <repo>/.git that Git answers from -- has no work
// tree to anchor, but it does have a hooks directory, and bd installed
// into it before this resolver existed. Resolving that shape here keeps
// the selected, scrubbed context instead of turning a supported
// repository into a warning and no install at all. Any other failure is
// still a failure: the work-tree-less resolver rejects non-repositories
// and directories inside a work tree.
workTreeless, workTreelessErr := git.ResolveWorkTreelessHooksContext(workDir, env)
if workTreelessErr != nil {
return nil, err
}
paths = workTreeless
}
if beadsDir != "" && !filepath.IsAbs(beadsDir) {
beadsDir = filepath.Join(workDir, beadsDir)

View file

@ -585,7 +585,10 @@ func runInitProxiedServerTail(cmd *cobra.Command, ctx context.Context, in initPr
isColocated := gitUC.IsColocatedJJGit(ctx)
switch {
case isJJ && !isColocated:
if !in.quiet {
// The install arm below took over the hook-status gate that used to
// wrap this whole switch. Keep it here too, or a pure-JJ repo with
// current hooks starts re-printing the alias hint on every init.
if !in.quiet && (!hooksInstalled() || hooksNeedUpdate()) {
printJJAliasInstructions()
}
case isColocated || isRepo:

View file

@ -574,12 +574,27 @@ func TestProxiedInitExcludeUsesSelectedDirectory(t *testing.T) {
}
func TestProxiedInitHooksUseSelectedContext(t *testing.T) {
for _, name := range []string{"normal", "missing", "outdated", "current", "skip", "nonrepo", "pure_jj", "colocated", "config_lock", "quiet_config_lock"} {
for _, name := range []string{"normal", "missing", "outdated", "current", "skip", "nonrepo", "pure_jj", "colocated", "bare", "git_dir_cwd", "config_lock", "quiet_config_lock"} {
t.Run(name, func(t *testing.T) {
selected, decoy, storage, common := newInitHooksFixture(t)
// Repository shapes whose common directory resolves while no work tree
// does; both must still install, as bd did before the hooks resolver.
workTreeless := name == "bare" || name == "git_dir_cwd"
if name == "normal" {
selected = filepath.Dir(common)
}
if name == "bare" {
selected = t.TempDir()
initExcludeGit(t, selected, "init", "--bare", "--quiet")
}
if name == "git_dir_cwd" {
// Standing in a non-bare repository's own .git directory: rev-parse
// --git-dir answers, so the repository arm runs, but --show-toplevel
// fails and --is-bare-repository reports false.
repo := t.TempDir()
initExcludeGit(t, repo, "init", "--quiet")
selected = filepath.Join(repo, ".git")
}
current, ambient := t.TempDir(), t.TempDir()
initExcludeGit(t, selected, "config", "core.hooksPath", current)
initExcludeGit(t, decoy, "config", "core.hooksPath", ambient)
@ -636,7 +651,7 @@ func TestProxiedInitHooksUseSelectedContext(t *testing.T) {
require.Empty(t, stderr)
}
require.NotContains(t, stderr, "Failed to resolve git hooks")
wantInstall := name == "normal" || name == "missing" || name == "outdated" || name == "colocated" || locked || (name == "current" && !currentReady)
wantInstall := name == "normal" || name == "missing" || name == "outdated" || name == "colocated" || workTreeless || locked || (name == "current" && !currentReady)
path := filepath.Join(storage, "hooks", "pre-commit")
if name == "colocated" {
path = filepath.Join(current, "pre-commit")
@ -647,8 +662,18 @@ func TestProxiedInitHooksUseSelectedContext(t *testing.T) {
_, err := os.Stat(path)
require.ErrorIs(t, err, os.ErrNotExist, "ambient status must not trigger an install")
}
if workTreeless {
// These resolve through the work-tree-less hooks context, so the
// install must still land in the selected storage and configure the
// selected repository -- not the inherited one this fixture routes to.
require.Equal(t, filepath.Join(storage, "hooks"), initExcludeGit(t, selected, "config", "--local", "--get", "core.hooksPath"))
}
if name != "nonrepo" && name != "pure_jj" {
exclude := readInitHooksFile(t, filepath.Join(common, "info", "exclude"))
excludeRoot := common
if workTreeless {
excludeRoot = selected // a work-tree-less selection is its own common directory
}
exclude := readInitHooksFile(t, filepath.Join(excludeRoot, "info", "exclude"))
require.Contains(t, string(exclude), ".beads/", "selected exclude callback lost")
}
for path, before := range preserved {

View file

@ -9,6 +9,7 @@ import (
"strings"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/ceiling"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/doltserver"
"github.com/steveyegge/beads/internal/git"
@ -245,7 +246,11 @@ func guardUndiscoveredLegacyWorkspace() error {
}
dir := utils.CanonicalizePath(cwd)
boundary := utils.CanonicalizePath(git.GetRepoRoot())
bound := ceiling.For(dir)
for {
if bound.Excludes(dir) {
return nil
}
if err := guardLegacyUpgradeWorkspace(filepath.Join(dir, ".beads")); err != nil {
return err
}

View file

@ -420,7 +420,7 @@ func init() {
listCmd.Flags().String("title", "", "Filter by title text (case-insensitive substring match)")
listCmd.Flags().String("spec", "", "Filter by spec_id prefix")
listCmd.Flags().String("id", "", "Filter by specific issue IDs (comma-separated, e.g., bd-1,bd-5,bd-10)")
listCmd.Flags().IntP("limit", "n", workapi.DefaultListLimit, "Limit results (default 50, use 0 for unlimited)")
listCmd.Flags().IntP("limit", "n", workapi.DefaultListLimit, "Limit results (an explicit --limit always wins, 0 meaning unlimited; otherwise --all is unlimited; otherwise a configured list.limit applies; otherwise unlimited when piped, else 20 in agent mode at a terminal, else 50)")
listCmd.Flags().Int("offset", 0, "Skip the first N matching results (0-based). Only supported under --proxied-server.")
listCmd.Flags().String("format", "", "Output format: 'digraph' (for golang.org/x/tools/cmd/digraph), 'dot' (Graphviz), or Go template")
listCmd.Flags().Bool("all", false, "Show all issues including closed (overrides default filter)")

View file

@ -91,3 +91,62 @@ func TestListLimitPolicyIsResolvedBeforeTheRequest(t *testing.T) {
})
}
}
// GH#6069: the --limit flag help must describe the switch pinned above, in
// the switch's own order, not claim an unconditional "default 50". The whole
// usage string is pinned verbatim: a substring check would accept a clause
// scoped wider than its arm (an earlier draft's "--all is always unlimited"
// contained every needle and was still false, because an explicit --limit
// outranks --all). The cases below tie the three claims a reader is most
// likely to get wrong back to the switch, each with list.limit configured.
func TestListLimitHelpDescribesPipedBehavior(t *testing.T) {
flag := listCmd.Flags().Lookup("limit")
if flag == nil {
t.Fatal("listCmd has no --limit flag registered")
}
const want = "Limit results (an explicit --limit always wins, 0 meaning unlimited; otherwise --all is unlimited; otherwise a configured list.limit applies; otherwise unlimited when piped, else 20 in agent mode at a terminal, else 50)"
if flag.Usage != want {
t.Errorf("--limit help = %q\nwant %q", flag.Usage, want)
}
t.Chdir(t.TempDir())
config.ResetForTesting()
t.Cleanup(config.ResetForTesting)
if err := config.Initialize(); err != nil {
t.Fatalf("config.Initialize: %v", err)
}
config.Set("list.limit", 7)
if got := config.GetValueSource("list.limit"); got == config.SourceDefault {
t.Fatal("precondition: config.Set did not make list.limit count as configured")
}
for _, tc := range []struct {
name string
args []string
want int
needsPipe bool
}{
// limitChanged is the first arm, so --all does not make an explicit
// --limit unlimited. Holds at a terminal or a pipe alike.
{"an explicit --limit outranks --all", []string{"--all", "--limit", "5"}, 5, false},
// The AllFlag arm precedes listLimitConfigured and !IsTerminal, so
// this holds at a terminal too and never needs to skip.
{"--all is unlimited even with list.limit configured", []string{"--all"}, 0, false},
// listLimitConfigured precedes the !IsTerminal arm. Only a piped
// stdout can show that ordering; at a terminal the same 7 would
// prove nothing about the piped arm.
{"piped output takes a configured list.limit", nil, 7, true},
} {
t.Run(tc.name, func(t *testing.T) {
if tc.needsPipe && ui.IsTerminal() {
t.Skip("this case shows a configured list.limit outranking the piped-stdout arm, so stdout must be a pipe; go test's is, this run's is not")
}
in, err := gatherListInput(newListLimitCommand(t, tc.args...))
if err != nil {
t.Fatalf("gatherListInput(%v): %v", tc.args, err)
}
if in.effectiveLimit != tc.want {
t.Errorf("effectiveLimit = %d, want %d", in.effectiveLimit, tc.want)
}
})
}
}

View file

@ -228,16 +228,43 @@ func TestProxiedServerList(t *testing.T) {
})
t.Run("ready_parent_tree_excludes_blocked_descendants", func(t *testing.T) {
// TODO: re-enable once `bd dep add` is ported to proxied mode.
// Currently bd dep add fails with "storage is nil" under proxied
// since the dep subcommand has no usesProxiedServer() dispatch.
t.Skip("requires bd dep add proxied support")
parent := bdProxiedCreate(t, bd, p.dir, "Ready parent tree", "--type", "epic")
readyChild := bdProxiedCreate(t, bd, p.dir, "Ready child in tree", "--type", "task", "--parent", parent.ID)
blockedChild := bdProxiedCreate(t, bd, p.dir, "Blocked child in tree", "--type", "task", "--parent", parent.ID)
blocker := bdProxiedCreate(t, bd, p.dir, "Tree child blocker", "--type", "task")
bdProxiedDep(t, bd, p.dir, "add", blockedChild.ID, blocker.ID)
out := bdProxiedList(t, bd, p, "--ready", "--parent", parent.ID, "--no-pager")
if !strings.Contains(out, readyChild.ID) {
t.Errorf("ready child %s should appear in ready parent tree:\n%s", readyChild.ID, out)
}
if strings.Contains(out, blockedChild.ID) {
t.Errorf("blocked child %s should not appear in ready parent tree:\n%s", blockedChild.ID, out)
}
})
// Regression for gastownhall/beads#3936: relates-to between two epics
// must not nest them, and bidirectional relates-to must not drop them.
// must not nest them in `bd list` tree mode, and a bidirectional
// relates-to must not silently drop both epics from the output.
t.Run("tree_relates_to_does_not_nest_or_drop_epics", func(t *testing.T) {
t.Skip("requires bd dep add proxied support")
epicA := bdProxiedCreate(t, bd, p.dir, "Relates Epic A", "--type", "epic", "--priority", "2")
epicB := bdProxiedCreate(t, bd, p.dir, "Relates Epic B", "--type", "epic", "--priority", "2")
bdProxiedDep(t, bd, p.dir, "add", epicA.ID, epicB.ID, "--type", "relates-to")
out := bdProxiedList(t, bd, p, "--no-pager", "--type", "epic")
if !strings.Contains(out, epicA.ID) || !strings.Contains(out, epicB.ID) {
t.Fatalf("one-direction relates-to should keep both epics visible:\n%s", out)
}
if strings.Contains(out, "└── "+epicA.ID) || strings.Contains(out, "└── "+epicB.ID) ||
strings.Contains(out, "├── "+epicA.ID) || strings.Contains(out, "├── "+epicB.ID) {
t.Fatalf("relates-to must not nest epics under each other:\n%s", out)
}
bdProxiedDep(t, bd, p.dir, "add", epicB.ID, epicA.ID, "--type", "relates-to")
out = bdProxiedList(t, bd, p, "--no-pager", "--type", "epic")
if !strings.Contains(out, epicA.ID) || !strings.Contains(out, epicB.ID) {
t.Fatalf("bidirectional relates-to must not drop epics from tree output:\n%s", out)
}
})
t.Run("ready_parent_filter_includes_grandchildren", func(t *testing.T) {

View file

@ -24,6 +24,7 @@ import (
"github.com/subosito/gotenv"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/ceiling"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/debug"
@ -789,7 +790,8 @@ func resolveCommandBeadsDir(dbPath string) string {
return beadsDir
}
for dir := filepath.Dir(dbPath); dir != "" && dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
bound := ceiling.For(filepath.Dir(dbPath))
for dir := filepath.Dir(dbPath); dir != "" && dir != filepath.Dir(dir) && !bound.Excludes(dir); dir = filepath.Dir(dir) {
candidate := filepath.Join(dir, ".beads")
if info, err := os.Stat(candidate); err == nil && info.IsDir() {
return candidate
@ -2075,6 +2077,15 @@ var rootCmd = &cobra.Command{
if shouldAutoPruneEventsJournal(cmd) {
maybeAutoPruneEventsJournal(rootCtx, beads.FindBeadsDir())
}
// Auto-backup, through the provider this command opened. Same
// gate as the direct arm's maintenance net below (strict
// --readonly, `bd serve` and a migration freeze all skip it),
// plus previews: a --dry-run must not register a backup remote
// or write backup state. proxiedAutoBackupBackend decides the
// topology, so this stays inert off managed-local.
if runsPostCommandMaintenance(cmd.Name(), readonlyMode) && !isPreviewCommand(cmd) && !commandFreeze.Frozen() {
runPostRunAutoBackup(rootCtx)
}
if uowProvider != nil {
_ = uowProvider.Close(rootCtx)
uowProvider = nil
@ -2221,31 +2232,9 @@ var rootCmd = &cobra.Command{
_ = traceFile.Close() // Best effort cleanup
}
// Heap profiling: --mem-profile flag or BEADS_MEM_PROFILE env var.
// Runs a GC first by default; BEADS_MEM_PROFILE_NOGC=1 skips it to capture peak.
heapDest := memProfilePath
if heapDest == "" {
heapDest = os.Getenv("BEADS_MEM_PROFILE")
}
if heapDest != "" {
if os.Getenv("BEADS_MEM_PROFILE_NOGC") == "" {
runtime.GC()
}
if f, err := os.Create(heapDest); err == nil { // #nosec G304 -- user-supplied profiling path
_ = pprof.WriteHeapProfile(f)
_ = f.Close()
}
}
// Optional one-line MemStats summary: BEADS_MEM_STATS=/path/to/stats.txt
if statsDest := os.Getenv("BEADS_MEM_STATS"); statsDest != "" {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
if f, err := os.Create(statsDest); err == nil { // #nosec G304 -- user-supplied profiling path
fmt.Fprintf(f, "HeapAlloc=%d HeapSys=%d HeapInuse=%d HeapObjects=%d\n",
ms.HeapAlloc, ms.HeapSys, ms.HeapInuse, ms.HeapObjects)
_ = f.Close()
}
}
// Heap profiling / MemStats summary: --mem-profile flag or
// BEADS_MEM_PROFILE / BEADS_MEM_STATS env vars. See writeMemDiagnostics.
writeMemDiagnostics(memProfilePath)
// The signal context is canceled and cleared by the deferred hook
// registered at the top of this function, so that it also covers the
@ -2254,6 +2243,69 @@ var rootCmd = &cobra.Command{
},
}
// flusherDiagnosticsSuffix separates the detached send-metrics child's memory
// diagnostics from the parent command's. See memDiagnosticsDest.
const flusherDiagnosticsSuffix = ".send-metrics"
// memDiagnosticsDest returns the destination writeMemDiagnostics should write
// dest to, suffixed when this process is the detached flusher child.
//
// MaybeSpawnFlusher hands the child the parent's environment minus the endpoint
// (flusherChildEnv in internal/metrics/spawn.go), so BEADS_MEM_PROFILE and
// BEADS_MEM_STATS arrive holding the same absolute paths the parent resolved.
// The spawn happens on main()'s post-ExecuteC tail, i.e. after
// PersistentPostRunE already wrote them, so an unsuffixed child would silently
// replace the profile of the command the user actually asked about with a
// profile of the trivial flusher -- no error, no size anomaly. Error exits are
// worse: CheckReadonly and the pre-run gates call CloseAndFlush while
// PersistentPostRunE never runs, leaving the child's file as the only one.
//
// BD_IS_FLUSHER=1 is set only by flusherChildEnv, so a human running
// `bd send-metrics` directly still gets the plain path. The suffix is applied
// to the resolved destination, after the flag/env fallback, so both knobs and
// both sources follow one rule.
func memDiagnosticsDest(dest string) string {
if dest == "" || os.Getenv(metrics.EnvIsFlusher) != "1" {
return dest
}
return dest + flusherDiagnosticsSuffix
}
// writeMemDiagnostics honors the heap-profile and MemStats diagnostic knobs
// (--mem-profile / BEADS_MEM_PROFILE / BEADS_MEM_PROFILE_NOGC / BEADS_MEM_STATS).
// memProfileFlag is the --mem-profile flag value, which wins over
// BEADS_MEM_PROFILE. Both call sites pass memProfilePath: --mem-profile is
// registered on rootCmd.PersistentFlags(), so every subcommand inherits it,
// including the hidden send-metrics one -- which calls this directly because its
// Run exits before Cobra ever reaches PersistentPostRunE below.
func writeMemDiagnostics(memProfileFlag string) {
// Runs a GC first by default; BEADS_MEM_PROFILE_NOGC=1 skips it to capture peak.
heapDest := memProfileFlag
if heapDest == "" {
heapDest = os.Getenv("BEADS_MEM_PROFILE")
}
heapDest = memDiagnosticsDest(heapDest)
if heapDest != "" {
if os.Getenv("BEADS_MEM_PROFILE_NOGC") == "" {
runtime.GC()
}
if f, err := os.Create(heapDest); err == nil { // #nosec G304 -- user-supplied profiling path
_ = pprof.WriteHeapProfile(f)
_ = f.Close()
}
}
// Optional one-line MemStats summary: BEADS_MEM_STATS=/path/to/stats.txt
if statsDest := memDiagnosticsDest(os.Getenv("BEADS_MEM_STATS")); statsDest != "" {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
if f, err := os.Create(statsDest); err == nil { // #nosec G304 -- user-supplied profiling path
fmt.Fprintf(f, "HeapAlloc=%d HeapSys=%d HeapInuse=%d HeapObjects=%d\n",
ms.HeapAlloc, ms.HeapSys, ms.HeapInuse, ms.HeapObjects)
_ = f.Close()
}
}
}
func shouldRunPostCommandAutoExport(cmd *cobra.Command) bool {
if cmd == nil {
return true

View file

@ -50,7 +50,8 @@ func runPrimeBinary(t *testing.T, binPath, workDir string, args ...string) (stdo
// .beads/ directory so FindBeadsDir succeeds.
func initBeadsWorkspace(t *testing.T, binPath, workDir string) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
// 2m, not 30s: a -race bd under a loaded CI host has taken >30s for one init.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, binPath, "init", "--prefix", "test")
cmd.Dir = workDir

View file

@ -1,4 +1,6 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
load("//tools/bazel:race.bzl", "go_test_race_off")
go_library(
name = "protocol",
@ -49,3 +51,91 @@ go_test(
"//internal/testutil/bazeltest",
],
)
# protocol_test without the race detector, which --config=docker otherwise
# turns on for every target: pr.yml's "Contract corpus" job runs
# `go test ./cmd/bd/protocol` without -race (PR Core covers the race build).
# Matching it is cheap: tools/bazel/race.bzl lands protocol_test's deps in
# the configuration the non-race //cmd/bd:bd_for_tests already builds them in,
# so the extra work is protocol_test's test-only libraries and one link.
# Racing it instead would also be acceptable (a race-only failure is a real
# bug); then drop this target and point protocol_docker_test back at
# :protocol_test.
#
# Tags:
# manual: only protocol_docker_test's input, never a build target of its
# own. `bazel test //...` in every lane still builds it through
# protocol_docker_test (a test tag filter does not stop the build).
go_test_race_off(
name = "protocol_race_off",
tags = ["manual"],
test = ":protocol_test",
)
# protocol_test as pr.yml's "Contract corpus" job runs it: non-race, TestMain
# starts a dolt sql-server container and BEADS_PROTOCOL_REQUIRE_DOLT=1 turns a
# missing container into a failure instead of a skip. It repeats
# protocol_test's `env` (go_test_variant.sh cannot inherit it). protocol_test
# itself stays untagged: under --config=prcore (BEADS_TEST_SKIP=dolt) its
# Dolt-backed tests skip.
#
# Tags:
# requires-docker: needs a docker daemon; excluded from --config=prcore/ci,
# selected by --config=docker.
# no-remote-exec: the daemon is on the host, not on remote workers.
sh_test(
name = "protocol_docker_test",
size = "large",
srcs = ["//tools/bazel:go_test_variant.sh"],
args = ["$(rootpath :protocol_race_off)"],
data = [
":protocol_race_off",
"//cmd/bd:bd_for_tests",
],
# Pinned to the container backend (the docker lane is the A/B control);
# rule env beats --test_env, so =local on the command line cannot flip it.
env = {
"BEADS_PROTOCOL_REQUIRE_DOLT": "1",
"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)",
"BEADS_TEST_DOLT_SERVER": "container",
},
# ~10 min unsharded without -race in pr.yml.
shard_count = 8,
tags = [
"no-remote-exec",
"requires-docker",
],
)
# protocol_test as pr.yml's "Contract corpus" job runs it (non-race, like the
# job; see protocol_race_off), on the dolt-server lane: TestMain's server is a
# local `dolt sql-server` child of the test process, and
# BEADS_PROTOCOL_REQUIRE_DOLT=1 turns a server that cannot start into a
# failure, so the golden and double-run determinism tests cannot skip. It
# repeats protocol_test's `env`. protocol_docker_test is the A/B control.
#
# Tags:
# dolt-server: starts hermetic dolt sql-servers from the pinned dolt in the
# test wrapper's runfiles (BEADS_TEST_DOLT_SERVER=local); excluded from
# --config=prcore/ci, selected by --config=doltserver. Runs remotely: no
# docker, and a missing server fails (REQUIRE=1) instead of skipping, so
# a cached pass is as good as a fresh one.
sh_test(
name = "protocol_dolt_test",
size = "large",
srcs = ["//tools/bazel:go_test_variant.sh"],
args = ["$(rootpath :protocol_race_off)"],
data = [
":protocol_race_off",
"//cmd/bd:bd_for_tests",
],
env = {
"BEADS_PROTOCOL_REQUIRE_DOLT": "1",
"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)",
"BEADS_TEST_DOLT_SERVER": "local",
"BEADS_TEST_REQUIRE_DOLT_CONTAINER": "1",
"GOMAXPROCS": "4",
},
shard_count = 8,
tags = ["dolt-server"],
)

View file

@ -53,7 +53,9 @@ func testMainInner(m *testing.M) int {
// AD-01 (be-c5p): allow protocol tests to connect to the spawned test server.
os.Setenv("BEADS_TEST_SERVER", "1")
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
if testutil.DoltUnavailableForTestMain(err) {
return 1
}
} else {
defer testutil.TerminateDoltContainer()
testDoltPort = testutil.DoltContainerPortInt()

View file

@ -35,6 +35,9 @@ func sharedProxiedServerPort(t *testing.T) int {
sharedProxiedPort = port
})
if sharedProxiedErr != nil {
if os.Getenv(testutil.EnvRequireDoltContainer) == "1" {
t.Fatalf("shared proxied-server unavailable: %v, but %s=1; this lane must not skip", sharedProxiedErr, testutil.EnvRequireDoltContainer)
}
t.Skipf("shared proxied-server unavailable: %v", sharedProxiedErr)
}
return sharedProxiedPort

View file

@ -73,7 +73,7 @@ func init() {
pruneCmd.Flags().BoolP("force", "f", false, "Actually prune (without this, shows preview)")
pruneCmd.Flags().Bool("ignore-references", false, "Delete closed beads even when referenced by open beads (use with care; see --help for details)")
pruneCmd.Flags().Bool("dry-run", false, "Preview what would be pruned with stats")
pruneCmd.Flags().String("older-than", "", "Only prune beads closed more than N ago (e.g., 30d, 2w, 60)")
pruneCmd.Flags().String("older-than", "", "Only prune beads closed more than N ago (e.g., 30d, 2w, 60, 36h)")
pruneCmd.Flags().String("pattern", "", "Only prune beads matching ID glob pattern (e.g., 'gm-old-*')")
rootCmd.AddCommand(pruneCmd)
}

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"math"
"strconv"
"strings"
"time"
@ -36,6 +37,12 @@ type purgeScope struct {
// is not done. `bd prune` asks unless --ignore-references; `bd purge`
// never does, because a wisp's citations are as transient as the wisp.
protectReferenced bool
// protectLiveDependents asks the role to skip candidates a live bead
// depends on through a parent-child, tracks or blocks edge. `bd purge`
// always asks: deleting a closed wisp whose child, tracker or blocked bead
// is still live cuts that live work loose from its root, and there is no
// retention reason to want that — a deliberate removal is `bd delete`.
protectLiveDependents bool
// reportsReferences publishes the reference-skip keys under --json. It is
// separate from protectReferenced because `bd prune --ignore-references`
// still publishes them, as zeroes, which is the shipped shape.
@ -52,7 +59,21 @@ Closed ephemeral beads (wisps, transient molecules) accumulate rapidly and
have no value once closed. This command removes them to reclaim storage.
Deletes: issues, dependencies, labels, events, and comments for matching beads.
Skips: pinned beads (protected).
Skips: pinned beads, and closed beads a live bead still depends on through a
parent-child, tracks or blocks edge (a closed molecule root whose step is open,
a closed bead a live convoy tracks). Live means any status that is not done.
--wisps-plane selects by storage plane instead: every closed row stored in the
wisps table, including --no-history beads, which the default ephemeral
selection leaves to ` + "`bd prune`" + `. It reaches durable-tier rows, so it requires
--older-than or --pattern, like ` + "`bd prune`" + `.
--limit N caps one run at N beads, oldest-closed first, so a large backlog can
be drained in bounded transactions; --json then reports "remaining" and
"has_more". Loop while has_more is true.
--older-than takes days (7, 7d), weeks (2w) or a duration with hour precision
(36h, 168h, 90m).
To delete closed non-ephemeral beads (regular tasks, features, bugs, etc.)
use ` + "`bd prune`" + ` instead.
@ -65,6 +86,9 @@ EXAMPLES:
bd purge --force # Delete all closed ephemeral beads
bd purge --older-than 7d --force # Only purge items closed 7+ days ago
bd purge --pattern "*-wisp-*" # Only purge matching ID pattern
bd purge --older-than 36h --force # Hour precision: closed 36+ hours ago
bd purge --wisps-plane --older-than 168h --force
# Every closed wisps-table row, incl. no-history
bd purge --dry-run # Detailed preview with stats`,
SilenceUsage: true,
SilenceErrors: true,
@ -76,14 +100,20 @@ EXAMPLES:
}
}()
return runPurgeOrPrune(cmd, purgeScope{
cmdName: "purge",
pastTense: "purged",
countKey: "purged_count",
dryRunCountKey: "purge_count",
subjectNoun: "closed ephemeral bead",
tier: issueops.SweepEphemeral,
})
scope := purgeScope{
cmdName: "purge",
pastTense: "purged",
countKey: "purged_count",
dryRunCountKey: "purge_count",
subjectNoun: "closed ephemeral bead",
tier: issueops.SweepEphemeral,
protectLiveDependents: true,
}
if wispsPlane, _ := cmd.Flags().GetBool("wisps-plane"); wispsPlane {
scope.subjectNoun = "closed wisps-plane bead"
scope.tier = issueops.SweepWispsPlane
}
return runPurgeOrPrune(cmd, scope)
},
}
@ -110,6 +140,11 @@ func runPurgeOrPrune(cmd *cobra.Command, scope purgeScope) error {
dryRun, _ := cmd.Flags().GetBool("dry-run")
olderThan, _ := cmd.Flags().GetString("older-than")
pattern, _ := cmd.Flags().GetString("pattern")
// Only `bd purge` defines --limit; GetInt on an undefined flag is 0, "no cap".
limit, _ := cmd.Flags().GetInt("limit")
if limit < 0 {
return HandleErrorRespectJSON("invalid --limit value %d: must be 0 (no limit) or positive", limit)
}
// The ROLE refuses an unfiltered durable sweep — that guard is
// workapi.ValidateSweepRequest, below every front door. This branch is here
@ -117,30 +152,36 @@ func runPurgeOrPrune(cmd *cobra.Command, scope purgeScope) error {
// flags a person who typed `bd prune --force` has to reach for. The contract
// case RunSweeperRefusesAnUnfilteredDurableSweep proves the guard survives
// this branch being deleted.
if scope.tier == issueops.SweepDurable && olderThan == "" && pattern == "" {
if scope.tier != issueops.SweepEphemeral && olderThan == "" && pattern == "" {
command := scope.cmdName
if scope.tier == issueops.SweepWispsPlane {
command += " --wisps-plane"
}
return HandleErrorWithHint(
fmt.Sprintf("bd %s requires --older-than or --pattern", scope.cmdName),
fmt.Sprintf("bd %s requires --older-than or --pattern", command),
"Protects against accidental bulk deletion. Use `--pattern '*'` to\n"+
" include all closed beads in this scope, or `--older-than 1d`\n"+
" / `--pattern '<glob>'` to narrow the deletion.")
}
request := issueops.SweepRequest{
Actor: actor,
Tier: scope.tier,
IDPattern: pattern,
ProtectReferenced: scope.protectReferenced,
Actor: actor,
Tier: scope.tier,
IDPattern: pattern,
ProtectReferenced: scope.protectReferenced,
ProtectLiveDependents: scope.protectLiveDependents,
Limit: limit,
// A --dry-run and an UNCONFIRMED run ask the role the same question —
// "what would this do" — so both send DryRun. --force is this
// command's confirmation, not a request field.
DryRun: dryRun || !force,
}
if olderThan != "" {
days, err := parseHumanDuration(olderThan)
age, err := parseOlderThan(olderThan)
if err != nil {
return HandleErrorRespectJSON("invalid --older-than value %q: %v", olderThan, err)
}
cutoff := time.Now().UTC().AddDate(0, 0, -days)
cutoff := time.Now().UTC().Add(-age)
request.ClosedBefore = &cutoff
}
@ -165,16 +206,16 @@ func runPurgeOrPrune(cmd *cobra.Command, scope purgeScope) error {
switch {
case result.Swept == 0:
return emitSweepEmpty(scope, olderThan, pattern, result)
return emitSweepEmpty(scope, olderThan, pattern, limit, result)
case dryRun:
return emitSweepDryRun(scope, result)
return emitSweepDryRun(scope, limit, result)
case !force:
return emitSweepConfirm(scope, olderThan, pattern, result)
}
commandDidWrite.Store(true)
commandMayEmptyJSONLExport.Store(true)
return emitSweepResult(scope, result)
return emitSweepResult(scope, limit, result)
}
// warnSweepDefenseSkips reports the candidates the role's own recheck threw
@ -208,12 +249,40 @@ func addReferenceStats(scope purgeScope, stats map[string]interface{}, result is
}
}
func emitSweepEmpty(scope purgeScope, olderThan, pattern string, result issueops.SweepResult) error {
// addLimitStats reports what --limit left for the next run. It is attached
// only when the caller asked for a limit, so the unlimited shape is unchanged.
func addLimitStats(stats map[string]interface{}, limit int, result issueops.SweepResult) {
if limit <= 0 {
return
}
stats["remaining"] = result.Remaining
stats["has_more"] = result.Remaining > 0
}
// printLimitNote tells a text reader that --limit left rows behind.
func printLimitNote(result issueops.SweepResult) {
if result.Remaining > 0 {
fmt.Printf(" Remaining (over --limit): %d — run again to continue\n", result.Remaining)
}
}
// addLiveDependentStats attaches the live-dependent skip count to a --json
// payload when the protection held anything back, the way pinned_skipped is
// reported.
func addLiveDependentStats(stats map[string]interface{}, result issueops.SweepResult) {
if result.Skipped.LiveDependent > 0 {
stats["live_dependent_skipped"] = result.Skipped.LiveDependent
}
}
func emitSweepEmpty(scope purgeScope, olderThan, pattern string, limit int, result issueops.SweepResult) error {
if jsonOutput {
stats := map[string]interface{}{
scope.countKey: 0,
"message": fmt.Sprintf("No %ss to %s", scope.subjectNoun, scope.cmdName),
}
addLiveDependentStats(stats, result)
addLimitStats(stats, limit, result)
addReferenceStats(scope, stats, result)
return outputJSON(stats)
}
@ -225,6 +294,10 @@ func emitSweepEmpty(scope purgeScope, olderThan, pattern string, result issueops
msg += fmt.Sprintf(" (matching %q)", pattern)
}
fmt.Println(msg)
if result.Skipped.LiveDependent > 0 {
fmt.Println(ui.MutedStyle.Render(fmt.Sprintf(
" (%d closed bead(s) protected by live dependents)", result.Skipped.LiveDependent)))
}
if result.Skipped.Referenced > 0 {
fmt.Println(ui.MutedStyle.Render(fmt.Sprintf(
" (%d closed bead(s) protected by open-bead references — use --ignore-references to override)",
@ -233,7 +306,7 @@ func emitSweepEmpty(scope purgeScope, olderThan, pattern string, result issueops
return nil
}
func emitSweepDryRun(scope purgeScope, result issueops.SweepResult) error {
func emitSweepDryRun(scope purgeScope, limit int, result issueops.SweepResult) error {
if jsonOutput {
stats := map[string]interface{}{
"dry_run": true,
@ -245,6 +318,8 @@ func emitSweepDryRun(scope purgeScope, result issueops.SweepResult) error {
if result.Skipped.Pinned > 0 {
stats["pinned_skipped"] = result.Skipped.Pinned
}
addLiveDependentStats(stats, result)
addLimitStats(stats, limit, result)
addReferenceStats(scope, stats, result)
return outputJSON(stats)
}
@ -255,6 +330,9 @@ func emitSweepDryRun(scope purgeScope, result issueops.SweepResult) error {
if result.Skipped.Pinned > 0 {
fmt.Printf(" Pinned (skipped): %d\n", result.Skipped.Pinned)
}
if result.Skipped.LiveDependent > 0 {
fmt.Printf(" Live dependent (skipped): %d\n", result.Skipped.LiveDependent)
}
if result.Skipped.Referenced > 0 {
fmt.Printf(" %s %d\n", ui.MutedStyle.Render("Referenced (skipped):"), result.Skipped.Referenced)
sample := result.ReferencedIDs
@ -271,6 +349,7 @@ func emitSweepDryRun(scope purgeScope, result issueops.SweepResult) error {
}
fmt.Printf(" %s %s%s\n", ui.MutedStyle.Render("Referenced IDs (sample):"), strings.Join(idStrs, ", "), suffix)
}
printLimitNote(result)
fmt.Printf("\n(Dry-run mode — no changes made)\n")
return nil
}
@ -280,10 +359,16 @@ func emitSweepConfirm(scope purgeScope, olderThan, pattern string, result issueo
if result.Skipped.Pinned > 0 {
fmt.Printf("Skipping %d pinned bead(s)\n", result.Skipped.Pinned)
}
if result.Skipped.LiveDependent > 0 {
fmt.Printf("Skipping %d bead(s) a live bead depends on\n", result.Skipped.LiveDependent)
}
if result.Skipped.Referenced > 0 {
fmt.Println(ui.MutedStyle.Render(fmt.Sprintf("Skipping %d referenced bead(s)", result.Skipped.Referenced)))
}
hint := fmt.Sprintf("bd %s --force", scope.cmdName)
if scope.tier == issueops.SweepWispsPlane {
hint += " --wisps-plane"
}
if olderThan != "" {
hint += " --older-than " + olderThan
}
@ -295,7 +380,7 @@ func emitSweepConfirm(scope purgeScope, olderThan, pattern string, result issueo
fmt.Sprintf("Use --force to confirm or --dry-run to preview.\n %s", hint))
}
func emitSweepResult(scope purgeScope, result issueops.SweepResult) error {
func emitSweepResult(scope purgeScope, limit int, result issueops.SweepResult) error {
if jsonOutput {
stats := map[string]interface{}{
scope.countKey: result.Swept,
@ -306,6 +391,8 @@ func emitSweepResult(scope purgeScope, result issueops.SweepResult) error {
if result.Skipped.Pinned > 0 {
stats["pinned_skipped"] = result.Skipped.Pinned
}
addLiveDependentStats(stats, result)
addLimitStats(stats, limit, result)
addReferenceStats(scope, stats, result)
return outputJSON(stats)
}
@ -316,9 +403,13 @@ func emitSweepResult(scope purgeScope, result issueops.SweepResult) error {
if result.Skipped.Pinned > 0 {
fmt.Printf(" Pinned (skipped): %d\n", result.Skipped.Pinned)
}
if result.Skipped.LiveDependent > 0 {
fmt.Printf(" Live dependent (skipped): %d\n", result.Skipped.LiveDependent)
}
if result.Skipped.Referenced > 0 {
fmt.Printf(" %s %d\n", ui.MutedStyle.Render("Referenced (skipped):"), result.Skipped.Referenced)
}
printLimitNote(result)
return nil
}
@ -329,53 +420,77 @@ func capitalize(s string) string {
return strings.ToUpper(s[:1]) + s[1:]
}
// parseHumanDuration parses a human-friendly duration string into days.
// Accepts: "7d", "30d", "24h", "2w", or just a number (treated as days).
func parseHumanDuration(s string) (int, error) {
// parseOlderThan parses an --older-than value into an age.
//
// Day-denominated values keep the meaning they always had: a bare number is
// days ("7"), and "7d" / "2w" are days and weeks. Anything else is a Go
// duration with hour precision or finer — "36h", "168h", "90m", "1h30m" — and
// is taken exactly; "Nh" used to be floored to whole days, which made "36h"
// sweep rows only 24 hours old. The suffix letters are case-insensitive, as
// they always were. The age must be positive.
func parseOlderThan(s string) (time.Duration, error) {
s = strings.TrimSpace(s)
if s == "" {
return 0, fmt.Errorf("empty duration")
}
// Plain number = days
if days, err := strconv.Atoi(s); err == nil {
if days <= 0 {
return 0, fmt.Errorf("duration must be positive")
}
return days, nil
return daysAge(int64(days))
}
// Parse suffix
unit := s[len(s)-1]
numStr := s[:len(s)-1]
num, err := strconv.Atoi(numStr)
if err != nil {
return 0, fmt.Errorf("invalid number %q", numStr)
switch unit {
case 'd', 'D', 'w', 'W':
num, err := strconv.Atoi(s[:len(s)-1])
if err != nil {
return 0, fmt.Errorf("invalid number %q", s[:len(s)-1])
}
days := int64(num)
if unit == 'w' || unit == 'W' {
if days > maxOlderThanDays/7 || days < -maxOlderThanDays/7 {
return 0, errOlderThanRange
}
days *= 7
}
return daysAge(days)
}
if num <= 0 {
age, err := time.ParseDuration(strings.ToLower(s))
if err != nil {
// time.ParseDuration refuses an out-of-range value ("9999999h") the
// same way it refuses a malformed one, so neither can wrap around.
return 0, fmt.Errorf("invalid or out-of-range duration %q (use a number of days, Nd, Nw, or a duration such as 36h or 90m)", s)
}
return positiveAge(age)
}
// maxOlderThanDays is the largest day count a time.Duration can hold. A larger
// value used to overflow silently — 213504d wrapped to 25 minutes — which on a
// destructive command selected nearly everything instead of nothing.
const maxOlderThanDays = math.MaxInt64 / int64(24*time.Hour)
var errOlderThanRange = fmt.Errorf("duration out of range (at most %d days)", maxOlderThanDays)
func daysAge(days int64) (time.Duration, error) {
if days > maxOlderThanDays {
return 0, errOlderThanRange
}
return positiveAge(time.Duration(days) * 24 * time.Hour)
}
func positiveAge(age time.Duration) (time.Duration, error) {
if age <= 0 {
return 0, fmt.Errorf("duration must be positive")
}
switch unit {
case 'h', 'H':
days := num / 24
if days == 0 {
days = 1 // minimum 1 day
}
return days, nil
case 'd', 'D':
return num, nil
case 'w', 'W':
return num * 7, nil
default:
return 0, fmt.Errorf("unknown unit %q (use h, d, or w)", string(unit))
}
return age, nil
}
func init() {
purgeCmd.Flags().BoolP("force", "f", false, "Actually purge (without this, shows preview)")
purgeCmd.Flags().Bool("dry-run", false, "Preview what would be purged with stats")
purgeCmd.Flags().String("older-than", "", "Only purge beads closed more than N ago (e.g., 7d, 2w, 30)")
purgeCmd.Flags().String("older-than", "", "Only purge beads closed more than N ago (e.g., 7d, 2w, 30, 36h)")
purgeCmd.Flags().Int("limit", 0, "Purge at most N beads this run, oldest-closed first (0 = no limit); --json reports remaining/has_more")
purgeCmd.Flags().Bool("wisps-plane", false, "Select every closed row in the wisps table, including --no-history beads (requires --older-than or --pattern)")
purgeCmd.Flags().String("pattern", "", "Only purge beads matching ID glob pattern (e.g., *-wisp-*)")
rootCmd.AddCommand(purgeCmd)
}

View file

@ -1,40 +1,68 @@
package main
import "testing"
import (
"testing"
"time"
)
func TestParseHumanDuration(t *testing.T) {
// TestParseOlderThan pins --older-than on both `bd purge` and `bd prune`.
// Day-denominated values keep their meaning; hour (and finer) values are taken
// exactly rather than floored to whole days — "36h" used to become 24h and
// sweep rows younger than the caller asked for.
func TestParseOlderThan(t *testing.T) {
const day = 24 * time.Hour
tests := []struct {
input string
want int
want time.Duration
wantErr bool
}{
{"7", 7, false},
{"30", 30, false},
{"7d", 7, false},
{"30d", 30, false},
{"2w", 14, false},
{"1w", 7, false},
{"48h", 2, false},
{"12h", 1, false}, // rounds up to 1 day minimum
{"7D", 7, false},
{"2W", 14, false},
// Unchanged day semantics.
{"7", 7 * day, false},
{"30", 30 * day, false},
{"7d", 7 * day, false},
{"30d", 30 * day, false},
{"2w", 14 * day, false},
{"1w", 7 * day, false},
{"7D", 7 * day, false},
{"2W", 14 * day, false},
// Hour precision, no longer floored to days.
{"48h", 48 * time.Hour, false},
{"168h", 7 * day, false},
{"36h", 36 * time.Hour, false},
{"12h", 12 * time.Hour, false},
{"36H", 36 * time.Hour, false},
{"90m", 90 * time.Minute, false},
{"1h30m", 90 * time.Minute, false},
// Refusals.
{"", 0, true},
{"0", 0, true},
{"-1", 0, true},
{"0d", 0, true},
{"0h", 0, true},
{"-36h", 0, true},
{"abc", 0, true},
{"7x", 0, true},
{"d", 0, true},
// Out of range must be refused, never wrapped: 213504d used to
// overflow to 25m26s and select almost every closed row.
{"213504d", 0, true},
{"213504", 0, true},
{"30501w", 0, true},
{"106752d", 0, true},
{"9223372036854775807d", 0, true},
{"99999999999h", 0, true},
// The largest representable day count still parses.
{"106751d", 106751 * day, false},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got, err := parseHumanDuration(tt.input)
got, err := parseOlderThan(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("parseHumanDuration(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
return
t.Fatalf("parseOlderThan(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
}
if got != tt.want {
t.Errorf("parseHumanDuration(%q) = %d, want %d", tt.input, got, tt.want)
t.Errorf("parseOlderThan(%q) = %s, want %s", tt.input, got, tt.want)
}
})
}

View file

@ -0,0 +1,207 @@
//go:build cgo
package main
import (
"encoding/json"
"os"
"os/exec"
"strings"
"testing"
"time"
)
// purgeScenarioRunner drives one storage mode for the purge retention
// scenario. run never fatals: several steps assert a refusal.
type purgeScenarioRunner struct {
mode string
run func(t *testing.T, args ...string) (stdout, stderr string, err error)
}
func (r purgeScenarioRunner) must(t *testing.T, args ...string) string {
t.Helper()
stdout, stderr, err := r.run(t, args...)
if err != nil {
t.Fatalf("%s: bd %s failed: %v\nstdout:\n%s\nstderr:\n%s",
r.mode, strings.Join(args, " "), err, stdout, stderr)
}
return stdout
}
func (r purgeScenarioRunner) create(t *testing.T, title string, args ...string) string {
t.Helper()
id := strings.TrimSpace(r.must(t, append([]string{"create", title, "--silent"}, args...)...))
if id == "" {
t.Fatalf("%s: bd create %q printed no id", r.mode, title)
}
return id
}
func (r purgeScenarioRunner) exists(t *testing.T, id string) bool {
t.Helper()
_, _, err := r.run(t, "show", id)
return err == nil
}
// purgeJSON runs a purge that must succeed and decodes its --json object.
func (r purgeScenarioRunner) purgeJSON(t *testing.T, args ...string) map[string]any {
t.Helper()
out := r.must(t, append(append([]string{"purge"}, args...), "--json")...)
start := strings.Index(out, "{")
if start < 0 {
t.Fatalf("%s: bd purge %s --json printed no object:\n%s", r.mode, strings.Join(args, " "), out)
}
var result map[string]any
if err := json.Unmarshal([]byte(out[start:]), &result); err != nil {
t.Fatalf("%s: parse purge JSON: %v\n%s", r.mode, err, out)
}
return result
}
func jsonCount(result map[string]any, key string) int {
n, _ := result[key].(float64)
return int(n)
}
// runPurgeRetentionScenario is the retention sweep an orchestrator runs over
// the wisps plane — `bd purge --wisps-plane --older-than <age> --force` — and
// the three things it has to get right:
//
// - the wisps PLANE is the selection, so a closed --no-history bead goes
// with the closed ephemeral wisp while a closed durable bead stays;
// - a closed wisp a live wisp depends on (parent-child, tracks) is kept and
// counted, on the default purge as much as the plane one;
// - --older-than has sub-day precision: rows closed seconds ago are swept
// by "1s" and kept by "36h" (which used to be floored to 24h), and an
// out-of-range value is refused rather than wrapped;
// - --limit drains a backlog in bounded runs, reporting remaining/has_more.
func runPurgeRetentionScenario(t *testing.T, r purgeScenarioRunner) {
t.Helper()
wisp := r.create(t, "closed wisp", "--ephemeral")
noHistory := r.create(t, "closed no-history bead", "--no-history")
durable := r.create(t, "closed durable bead")
parent := r.create(t, "closed molecule root", "--ephemeral")
child := r.create(t, "open step", "--ephemeral")
r.must(t, "dep", "add", child, parent, "--type", "parent-child")
tracked := r.create(t, "closed tracked wisp", "--ephemeral")
convoy := r.create(t, "live convoy", "--ephemeral")
r.must(t, "dep", "add", convoy, tracked, "--type", "tracks")
r.must(t, "update", convoy, "--status", "in_progress")
r.must(t, "close", wisp, noHistory, durable, tracked)
r.must(t, "close", parent, "--force")
// Everything above closed within the last few seconds. closed_at is
// stored at second precision, so wait comfortably past one second before
// asking "--older-than 1s" for it.
time.Sleep(2500 * time.Millisecond)
// The plane selector reaches durable-tier rows, so it takes the durable
// tier's require-a-filter gate.
if stdout, stderr, err := r.run(t, "purge", "--wisps-plane", "--force"); err == nil ||
!strings.Contains(stdout+stderr, "requires --older-than or --pattern") {
t.Fatalf("%s: unfiltered purge --wisps-plane: err=%v\n%s%s", r.mode, err, stdout, stderr)
}
// Hour precision: nothing here is 36 hours old.
if got := r.purgeJSON(t, "--wisps-plane", "--older-than", "36h", "--dry-run"); jsonCount(got, "purge_count") != 0 {
t.Fatalf("%s: --older-than 36h selected rows closed seconds ago: %v", r.mode, got)
}
preview := r.purgeJSON(t, "--wisps-plane", "--older-than", "1s", "--dry-run")
if jsonCount(preview, "purge_count") != 2 || jsonCount(preview, "live_dependent_skipped") != 2 {
t.Fatalf("%s: dry run = %v; want purge_count 2 (wisp + no-history) and live_dependent_skipped 2 (root + tracked)",
r.mode, preview)
}
// --limit drains in bounded runs and says whether more remain.
first := r.purgeJSON(t, "--wisps-plane", "--older-than", "1s", "--limit", "1", "--force")
if jsonCount(first, "purged_count") != 1 || jsonCount(first, "remaining") != 1 || first["has_more"] != true {
t.Fatalf("%s: purge --limit 1 = %v; want purged_count 1, remaining 1, has_more true", r.mode, first)
}
result := r.purgeJSON(t, "--wisps-plane", "--older-than", "1s", "--limit", "1", "--force")
if jsonCount(result, "purged_count") != 1 || jsonCount(result, "live_dependent_skipped") != 2 ||
jsonCount(result, "remaining") != 0 || result["has_more"] != false {
t.Fatalf("%s: second purge --limit 1 = %v; want purged_count 1, live_dependent_skipped 2, remaining 0, has_more false",
r.mode, result)
}
if stdout, stderr, err := r.run(t, "purge", "--limit", "-1", "--force"); err == nil {
t.Fatalf("%s: purge --limit -1 succeeded:\n%s%s", r.mode, stdout, stderr)
}
if stdout, stderr, err := r.run(t, "purge", "--older-than", "213504d", "--force"); err == nil ||
!strings.Contains(stdout+stderr, "out of range") {
t.Fatalf("%s: purge --older-than 213504d must refuse as out of range: err=%v\n%s%s", r.mode, err, stdout, stderr)
}
for id, want := range map[string]bool{
wisp: false, noHistory: false,
parent: true, child: true, tracked: true, convoy: true, durable: true,
} {
if got := r.exists(t, id); got != want {
t.Errorf("%s: after purge --wisps-plane, %s exists = %v, want %v", r.mode, id, got, want)
}
}
// The default (ephemeral-tier) purge protects live dependents too, and
// still leaves no-history beads alone.
root2 := r.create(t, "second closed root", "--ephemeral")
step2 := r.create(t, "second open step", "--ephemeral")
r.must(t, "dep", "add", step2, root2, "--type", "parent-child")
r.must(t, "close", root2, "--force")
noHistory2 := r.create(t, "second no-history bead", "--no-history")
r.must(t, "close", noHistory2)
plain := r.purgeJSON(t, "--force")
if jsonCount(plain, "live_dependent_skipped") < 1 {
t.Errorf("%s: default purge reported no live-dependent skip: %v", r.mode, plain)
}
if !r.exists(t, root2) {
t.Errorf("%s: default purge deleted %s, whose step %s is still open", r.mode, root2, step2)
}
if !r.exists(t, noHistory2) {
t.Errorf("%s: default purge deleted no-history bead %s; only --wisps-plane selects it", r.mode, noHistory2)
}
}
func embeddedPurgeScenarioRunner(bd, dir string) purgeScenarioRunner {
return purgeScenarioRunner{
mode: "embedded",
run: func(t *testing.T, args ...string) (string, string, error) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
return stdout.String(), stderr.String(), err
},
}
}
func proxiedPurgeScenarioRunner(bd string, p proxiedProject) purgeScenarioRunner {
return purgeScenarioRunner{
mode: "proxied",
run: func(t *testing.T, args ...string) (string, string, error) {
t.Helper()
return bdProxiedRunBuffers(t, bd, p.dir, args...)
},
}
}
func TestEmbeddedPurgeWispsPlaneRetention(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "pwp")
runPurgeRetentionScenario(t, embeddedPurgeScenarioRunner(bd, dir))
}
func TestProxiedServerPurgeWispsPlaneRetention(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
p := newSharedProxiedProject(t, bd, "pwx")
runPurgeRetentionScenario(t, proxiedPurgeScenarioRunner(bd, p))
}

View file

@ -0,0 +1,19 @@
//go:build cgo && unix
package main
import (
"testing"
"time"
)
// TestManagedLocalProxiedPurgeWispsPlaneRetention runs the purge retention
// scenario on the managed-local proxied topology, the default for a proxied
// workspace bd starts the Dolt server for. Named TestManagedLocalProxied* so
// the proxied-local smoke lane runs it.
func TestManagedLocalProxiedPurgeWispsPlaneRetention(t *testing.T) {
requireManagedLocalProxiedEnv(t)
bd := buildEmbeddedBD(t)
p := bdManagedLocalInit(t, bd, "pwl", 5*time.Minute)
runPurgeRetentionScenario(t, proxiedPurgeScenarioRunner(bd, p))
}

View file

@ -18,7 +18,17 @@ var sendMetricsCmd = &cobra.Command{
// send-metrics. MaybeSpawnFlusher also refuses to spawn when EnvIsFlusher
// is set on this process, so the no-recursion guarantee is structural and
// not solely dependent on this exit.
os.Exit(metrics.RunSendMetrics())
//
// That same early exit means this Run returns before Cobra ever reaches
// PersistentPostRunE in main.go, so --mem-profile/BEADS_MEM_PROFILE/
// BEADS_MEM_STATS are silently inert for this subcommand unless honored
// here directly (be-wwy2.2). --mem-profile is registered on
// rootCmd.PersistentFlags(), so this hidden command inherits it and
// prints it in --help; pass memProfilePath exactly as PersistentPostRunE
// does, or the advertised flag would be parsed and then discarded.
code := metrics.RunSendMetrics()
writeMemDiagnostics(memProfilePath)
os.Exit(code)
},
}

View file

@ -0,0 +1,215 @@
package main
import (
"compress/gzip"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"testing"
"github.com/steveyegge/beads/internal/metrics"
)
// memStatsLine is the exact shape writeMemDiagnostics' MemStats summary writes.
var memStatsLine = regexp.MustCompile(`^HeapAlloc=\d+ HeapSys=\d+ HeapInuse=\d+ HeapObjects=\d+\n$`)
// requireHeapProfile fails unless path holds a real heap profile.
// runtime/pprof.WriteHeapProfile always gzips its protobuf output, so
// decompressing cleanly to a non-empty body is strong evidence a genuine profile
// was written -- without pulling in a pprof-parsing dependency just for this
// test. knob names the surface under test so a failure says which one broke.
func requireHeapProfile(t *testing.T, knob, path string) {
t.Helper()
f, err := os.Open(path)
if err != nil {
t.Fatalf("%s file was not written: %v", knob, err)
}
defer f.Close()
gz, err := gzip.NewReader(f)
if err != nil {
t.Fatalf("%s is not valid gzip: %v", knob, err)
}
body, err := io.ReadAll(gz)
if err != nil {
t.Fatalf("%s gzip stream corrupt: %v", knob, err)
}
if len(body) == 0 {
t.Fatalf("%s decompressed to 0 bytes, want a real heap profile", knob)
}
}
// TestSendMetricsHonorsMemDiagnostics is the be-wwy2.2 regression: send-metrics's
// Run calls os.Exit() directly, so it returns before Cobra ever reaches
// PersistentPostRunE (main.go) -- the one place --mem-profile / BEADS_MEM_PROFILE /
// BEADS_MEM_PROFILE_NOGC / BEADS_MEM_STATS are honored for every other bd
// subcommand. That makes the diagnostic tooling unreachable for the one child
// process that runs on the tail of nearly every bd invocation fleet-wide.
//
// This builds a real bd binary and exercises `bd send-metrics` as a subprocess
// (like MaybeSpawnFlusher does in production) rather than calling RunSendMetrics
// in-process, because the bug is specifically about the Cobra command-tree
// wiring around this subcommand, which only an actual subprocess run exercises.
func TestSendMetricsHonorsMemDiagnostics(t *testing.T) {
bdBin := buildBDForInitTests(t)
// send-metrics returns from PersistentPreRunE before any workspace/store
// setup (main.go: `if cmd.Name() == metrics.SendMetricsSubcommand { return nil }`),
// so no .beads workspace is needed here. BD_DISABLE_METRICS=1 makes
// RunSendMetrics's Enabled() check false, so it returns 0 right after a
// no-op prune of the (nonexistent) queue dir -- deterministic and offline,
// with no real network attempt.
baseEnv := func(home string) []string {
return []string{
"HOME=" + home,
"PATH=" + os.Getenv("PATH"),
"BD_DISABLE_METRICS=1",
}
}
t.Run("default with no diagnostics env vars is unaffected", func(t *testing.T) {
cmd := exec.Command(bdBin, metrics.SendMetricsSubcommand)
cmd.Env = baseEnv(t.TempDir())
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd send-metrics: %v\noutput: %s", err, out)
}
if len(out) != 0 {
t.Fatalf("bd send-metrics produced output with no diagnostics requested: %q", out)
}
})
t.Run("BEADS_MEM_STATS writes a one-line MemStats summary", func(t *testing.T) {
statsPath := filepath.Join(t.TempDir(), "stats.txt")
cmd := exec.Command(bdBin, metrics.SendMetricsSubcommand)
cmd.Env = append(baseEnv(t.TempDir()), "BEADS_MEM_STATS="+statsPath)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd send-metrics: %v\noutput: %s", err, out)
}
data, err := os.ReadFile(statsPath)
if err != nil {
t.Fatalf("BEADS_MEM_STATS file was not written: %v", err)
}
if !memStatsLine.Match(data) {
t.Fatalf("BEADS_MEM_STATS content = %q, want match of %s", data, memStatsLine)
}
})
t.Run("BEADS_MEM_PROFILE writes a valid gzipped heap profile", func(t *testing.T) {
profilePath := filepath.Join(t.TempDir(), "heap.pprof")
cmd := exec.Command(bdBin, metrics.SendMetricsSubcommand)
cmd.Env = append(baseEnv(t.TempDir()), "BEADS_MEM_PROFILE="+profilePath)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd send-metrics: %v\noutput: %s", err, out)
}
requireHeapProfile(t, "BEADS_MEM_PROFILE", profilePath)
})
// --mem-profile is registered on rootCmd.PersistentFlags() (main.go), so this
// hidden subcommand inherits it and advertises it in --help. The call site
// used to hardcode "", which parsed the flag and then discarded it -- the same
// "accepted but silently inert" state be-wwy2.2 exists to remove, one line
// below the fix. No BEADS_MEM_PROFILE here, so only the flag can produce the
// file.
t.Run("--mem-profile flag is honored, not just the env var", func(t *testing.T) {
profilePath := filepath.Join(t.TempDir(), "flag.pprof")
cmd := exec.Command(bdBin, metrics.SendMetricsSubcommand, "--mem-profile="+profilePath)
cmd.Env = baseEnv(t.TempDir())
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd send-metrics --mem-profile: %v\noutput: %s", err, out)
}
requireHeapProfile(t, "--mem-profile", profilePath)
})
// The detached flusher child inherits the parent's BEADS_MEM_* paths verbatim
// (flusherChildEnv strips only the endpoint and the flusher marker), and
// MaybeSpawnFlusher runs on main()'s post-ExecuteC tail -- after
// PersistentPostRunE already wrote them. Without a distinct destination the
// child's trivial profile would silently replace the profile of the command
// the user actually asked about. BD_IS_FLUSHER=1 is what production sets on
// the child, so driving it here is the real shape, not a contrivance.
t.Run("flusher child writes beside the parent's files instead of over them", func(t *testing.T) {
dir := t.TempDir()
profilePath := filepath.Join(dir, "heap.pprof")
statsPath := filepath.Join(dir, "stats.txt")
// Stand in for the parent command's own writeMemDiagnostics call, which
// has already happened by the time the flusher is spawned.
parentProfile := []byte("parent heap profile, must survive the spawn\n")
parentStats := []byte("parent memstats, must survive the spawn\n")
if err := os.WriteFile(profilePath, parentProfile, 0o600); err != nil {
t.Fatalf("seed parent profile: %v", err)
}
if err := os.WriteFile(statsPath, parentStats, 0o600); err != nil {
t.Fatalf("seed parent stats: %v", err)
}
cmd := exec.Command(bdBin, metrics.SendMetricsSubcommand)
cmd.Env = append(baseEnv(t.TempDir()),
"BEADS_MEM_PROFILE="+profilePath,
"BEADS_MEM_STATS="+statsPath,
metrics.EnvIsFlusher+"=1",
)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd send-metrics as flusher child: %v\noutput: %s", err, out)
}
// The parent's data is untouched, byte for byte.
if got, err := os.ReadFile(profilePath); err != nil {
t.Fatalf("parent profile disappeared: %v", err)
} else if string(got) != string(parentProfile) {
t.Fatalf("flusher child overwrote the parent's heap profile: got %q, want %q", got, parentProfile)
}
if got, err := os.ReadFile(statsPath); err != nil {
t.Fatalf("parent stats disappeared: %v", err)
} else if string(got) != string(parentStats) {
t.Fatalf("flusher child overwrote the parent's MemStats: got %q, want %q", got, parentStats)
}
// ...and the child's own diagnostics are still produced, just beside
// them. This is the half that distinguishes suffixing the destination
// from simply scrubbing the vars out of the child's environment, which
// would reinstate the inertness be-wwy2.2 removes.
requireHeapProfile(t, "flusher child BEADS_MEM_PROFILE", profilePath+".send-metrics")
childStats, err := os.ReadFile(statsPath + ".send-metrics")
if err != nil {
t.Fatalf("flusher child wrote no MemStats of its own: %v", err)
}
if !memStatsLine.Match(childStats) {
t.Fatalf("flusher child MemStats content = %q, want match of %s", childStats, memStatsLine)
}
})
t.Run("BEADS_MEM_PROFILE_NOGC is accepted and still writes the profile", func(t *testing.T) {
profilePath := filepath.Join(t.TempDir(), "heap.pprof")
cmd := exec.Command(bdBin, metrics.SendMetricsSubcommand)
cmd.Env = append(baseEnv(t.TempDir()), "BEADS_MEM_PROFILE="+profilePath, "BEADS_MEM_PROFILE_NOGC=1")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd send-metrics: %v\noutput: %s", err, out)
}
if _, err := os.Stat(profilePath); err != nil {
t.Fatalf("BEADS_MEM_PROFILE file was not written under NOGC: %v", err)
}
})
t.Run("diagnostics still run and exit code is preserved when RunSendMetrics fails", func(t *testing.T) {
statsPath := filepath.Join(t.TempDir(), "stats.txt")
cmd := exec.Command(bdBin, metrics.SendMetricsSubcommand)
// Deliberately no HOME: DataDir()'s os.UserHomeDir() fails
// deterministically and offline, so RunSendMetrics() returns 1 before
// ever reaching the enabled/network checks.
cmd.Env = []string{"PATH=" + os.Getenv("PATH"), "BEADS_MEM_STATS=" + statsPath}
out, err := cmd.CombinedOutput()
exitErr, ok := err.(*exec.ExitError)
if !ok {
t.Fatalf("bd send-metrics: want a non-zero *exec.ExitError, got %v (%T)\noutput: %s", err, err, out)
}
if code := exitErr.ExitCode(); code != 1 {
t.Fatalf("bd send-metrics exit code = %d, want 1 (RunSendMetrics's own failure code, preserved through the fix)", code)
}
if _, statErr := os.Stat(statsPath); statErr != nil {
t.Fatalf("BEADS_MEM_STATS file was not written even though the command failed: %v", statErr)
}
})
}

View file

@ -7,6 +7,7 @@ import (
"path/filepath"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/ceiling"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/utils"
@ -63,7 +64,8 @@ func resolveBeadsDirForDBPath(dbPath string) string {
}
addAncestorCandidates := func(path string) {
for dir := path; dir != "" && dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
bound := ceiling.For(path)
for dir := path; dir != "" && dir != filepath.Dir(dir) && !bound.Excludes(dir); dir = filepath.Dir(dir) {
addCandidate(filepath.Join(dir, ".beads"))
if filepath.Base(dir) == ".beads" {
addCandidate(dir)

View file

@ -38,7 +38,10 @@ func startTestDoltServer() func() {
return func() { testutil.TerminateDoltContainer() }
}
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
if testutil.DoltUnavailableForTestMain(err) {
// Nothing has been started yet; there is nothing to clean up.
os.Exit(1)
}
return func() {}
}

View file

@ -50,7 +50,9 @@ type testRunner interface {
}
func runTestsAndSweep(m testRunner) int {
stdout, stderr := os.Stdout, os.Stderr
code := m.Run()
code = checkStdioAfterRun(code, stdout, stderr)
swept := doltserver.SweepSuiteTestServers(testTempRoot)
return doltserver.ApplyLeakPolicy("cmd/bd", code, swept)
}

View file

@ -19,6 +19,10 @@ var undeferCmd = &cobra.Command{
This brings issues back from the icebox so they can be worked on again.
Issues will appear in 'bd ready' if they have no blockers.
If an issue carries a defer_until timestamp but its status isn't
"deferred" (e.g. after an explicit --status change), undefer clears
the stray timestamp without touching status.
Examples:
bd undefer bd-abc # Undefer a single issue
bd undefer bd-abc bd-def # Undefer multiple issues`,
@ -64,15 +68,26 @@ Examples:
fmt.Fprintf(os.Stderr, "Error getting %s: %v\n", fullID, err)
continue
}
if issue.Status != types.StatusDeferred {
// Gate on defer_until, not status alone (ga-bq3w5): bd ready hides
// any issue with a future defer_until regardless of status, so
// `bd update <id> --status open --defer <date>` leaves a status=open
// issue permanently invisible with no status-based signal anywhere.
// Mirrors GH#3233's `bd update --defer=""` gate (update.go): only
// flip status to open when it was actually "deferred" — other
// statuses shouldn't be clobbered just to clear a stray timestamp.
wasDeferred := issue.Status == types.StatusDeferred
if !wasDeferred && issue.DeferUntil == nil {
fmt.Fprintf(os.Stderr, "%s is not deferred (status: %s)\n", fullID, string(issue.Status))
continue
}
updates := map[string]interface{}{
"status": string(types.StatusOpen),
"defer_until": nil,
}
if wasDeferred {
updates["status"] = string(types.StatusOpen)
}
if err := store.UpdateIssue(ctx, fullID, updates, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error undeferring %s: %v\n", fullID, err)
@ -84,8 +99,10 @@ Examples:
if issue != nil {
undeferredIssues = append(undeferredIssues, issue)
}
} else {
} else if wasDeferred {
fmt.Printf("%s Undeferred %s (now open)\n", ui.RenderPass("*"), fullID)
} else {
fmt.Printf("%s Cleared stale defer_until on %s (status unchanged: %s)\n", ui.RenderPass("*"), fullID, string(issue.Status))
}
}

View file

@ -69,6 +69,79 @@ func TestEmbeddedUndefer(t *testing.T) {
t.Errorf("expected 'not deferred' message: %s", out)
}
})
// ===== Stray defer_until on a non-deferred status (ga-bq3w5) =====
t.Run("undefer_clears_stray_defer_until_when_status_not_deferred", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Stray future defer on open issue", "--type", "task")
// Reproduces ga-bq3w5: an explicit --status wins over --defer's own
// status=deferred default (update.go), so this single command leaves
// status=open with a live future defer_until — exactly the shape that
// silently starves an issue out of `bd ready` with no status-based
// signal anywhere.
cmd := exec.Command(bd, "update", issue.ID, "--status", "open", "--defer", "2099-01-01")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd update --status open --defer failed: %v\n%s", err, out)
}
status, deferUntil := showDeferState(t, bd, dir, issue.ID)
if status != "open" || deferUntil == nil {
t.Fatalf("precondition: expected status=open with defer_until set, got status=%q defer_until=%v", status, deferUntil)
}
if ids := wakeReadyIDs(t, bd, dir); ids[issue.ID] {
t.Fatalf("precondition: issue should be hidden from bd ready by its future defer_until")
}
out := bdUndefer(t, bd, dir, issue.ID)
if strings.Contains(out, "is not deferred") {
t.Errorf("undefer refused instead of clearing the stray defer_until: %s", out)
}
status, deferUntil = showDeferState(t, bd, dir, issue.ID)
if status != "open" {
t.Errorf("expected status to stay open, got %q", status)
}
if deferUntil != nil {
t.Errorf("expected defer_until cleared after undefer, got %v", deferUntil)
}
if ids := wakeReadyIDs(t, bd, dir); !ids[issue.ID] {
t.Errorf("expected issue back in bd ready after undefer cleared its defer_until")
}
})
// ===== Stray defer_until on a non-open status must not be clobbered =====
t.Run("undefer_clears_stray_defer_until_without_clobbering_in_progress", func(t *testing.T) {
// The "open" case above can't distinguish the conditional status
// write (only flip to open when wasDeferred) from an unconditional
// one: writing status=open to an already-open issue is a no-op.
// in_progress makes the distinction observable.
issue := bdCreate(t, bd, dir, "Stray future defer on in_progress issue", "--type", "task")
cmd := exec.Command(bd, "update", issue.ID, "--status", "in_progress", "--defer", "2099-01-01")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bd update --status in_progress --defer failed: %v\n%s", err, out)
}
status, deferUntil := showDeferState(t, bd, dir, issue.ID)
if status != "in_progress" || deferUntil == nil {
t.Fatalf("precondition: expected status=in_progress with defer_until set, got status=%q defer_until=%v", status, deferUntil)
}
out := bdUndefer(t, bd, dir, issue.ID)
if strings.Contains(out, "is not deferred") {
t.Errorf("undefer refused instead of clearing the stray defer_until: %s", out)
}
status, deferUntil = showDeferState(t, bd, dir, issue.ID)
if status != "in_progress" {
t.Errorf("expected status to stay in_progress (not clobbered to open), got %q", status)
}
if deferUntil != nil {
t.Errorf("expected defer_until cleared after undefer, got %v", deferUntil)
}
})
}
// TestEmbeddedUndeferConcurrent exercises undefer operations concurrently.

View file

@ -19,6 +19,8 @@ func isGitWorktree() bool {
// gitRevParse runs git rev-parse with the given flag and returns the trimmed output.
// This is a helper for CLI utilities that need git command execution.
func gitRevParse(flag string) string {
// #nosec G702 - fixed "git" command; flag is a constant rev-parse switch chosen by
// the caller in this package, never attacker-controlled input.
out, err := exec.Command("git", "rev-parse", flag).Output()
if err != nil {
return ""

View file

@ -688,12 +688,14 @@ func scrubWorktreeRemovalGitEnvForOS(env []string, goos string) []string {
// ScrubRoutingForOS returns a fresh slice, so filtering it in place is safe.
result := cleaned[:0]
for _, entry := range cleaned {
// One shared subprocess key identity: execenv mirrors os/exec's fold, so
// this drops exactly the entries a child process would treat as these two
// variables -- an independent fold here would diverge from that rule.
// ToUpper is not that rule; the two disagree in both directions for
// non-ASCII keys on Windows: ToUpper leaves GİT_OPTIONAL_LOCKS unmatched
// although Git honors it, and folds GIT_OPTIONAL_LOCKſ onto the literal
// although os/exec keeps it distinct.
key := worktreeGitEnvKey(entry)
// Key identity is the subprocess lookup rule (execenv), not ToUpper.
// The two disagree in both directions for non-ASCII keys on Windows:
// ToUpper leaves GİT_OPTIONAL_LOCKS unmatched although Git honors it,
// and folds GIT_OPTIONAL_LOCKſ onto the literal although os/exec keeps
// it distinct.
if execenv.KeyEqualForOS(key, "GIT_NO_REPLACE_OBJECTS", goos) ||
execenv.KeyEqualForOS(key, "GIT_OPTIONAL_LOCKS", goos) {
continue

View file

@ -11,6 +11,8 @@ import (
"reflect"
"strings"
"testing"
"github.com/steveyegge/beads/internal/testutil/bazeltest"
)
const (
@ -295,7 +297,7 @@ func runWorktreeRemoveProcess(t *testing.T, dir string, extraEnv []string, args
}
command := exec.Command(executable, "-test.run=^TestWorktreeRemoveProcessHelper$")
command.Dir = dir
command.Env = overrideWorktreeRemoveEnv(os.Environ(), append([]string{worktreeRemoveHelperEnv + "=1", worktreeRemoveHelperArgsEnv + "=" + string(encodedArgs), "BD_DISABLE_METRICS=1"}, extraEnv...))
command.Env = overrideWorktreeRemoveEnv(bazeltest.ShardFreeEnv(os.Environ()), append([]string{worktreeRemoveHelperEnv + "=1", worktreeRemoveHelperArgsEnv + "=" + string(encodedArgs), "BD_DISABLE_METRICS=1"}, extraEnv...))
var stdout, stderr bytes.Buffer
command.Stdout, command.Stderr = &stdout, &stderr
err = command.Run()

View file

@ -1,6 +1,7 @@
package main
import (
"fmt"
"os"
"testing"
)
@ -14,9 +15,16 @@ import (
// the var-init streams: under `go test -json` the testing framework swaps
// os.Stderr to os.Stdout inside M.Run (go.dev/issue/33419), after var-init
// and before any test, and that framework swap is not a leak (#5881).
//
// It only sees tests that ran before it in this process. TestMain repeats the
// check after m.Run (checkStdioAfterRun), which covers every test in the
// process, including each Bazel shard, where this test runs in one shard only.
func TestZZStdioNotLeaked(t *testing.T) {
if baselineStdout == nil || baselineStderr == nil {
t.Skip("stdio baseline not captured (TestAAAStdioBaseline filtered out); nothing to compare against")
// TestAAAStdioBaseline did not run in this process (-run filtered it
// out, or Bazel sharding put it in another shard); TestMain's check
// after m.Run still applies.
t.Skip("no baseline in this process; checkStdioAfterRun covers it")
}
if os.Stdout != baselineStdout {
t.Errorf("os.Stdout was leaked by an earlier test (now fd=%d name=%q); "+
@ -31,3 +39,29 @@ func TestZZStdioNotLeaked(t *testing.T) {
os.Stderr = baselineStderr
}
}
// checkStdioAfterRun fails the process (returning a non-zero code) when a test
// left os.Stdout or os.Stderr reassigned. stdout/stderr are the streams from
// before m.Run. The one reassignment allowed is the framework's own under
// -json, os.Stderr = os.Stdout (see TestZZStdioNotLeaked).
func checkStdioAfterRun(code int, stdout, stderr *os.File) int {
var leaked []string
if os.Stdout != stdout {
leaked = append(leaked, fmt.Sprintf("os.Stdout (now fd=%d name=%q)", os.Stdout.Fd(), os.Stdout.Name()))
}
if os.Stderr != stderr && os.Stderr != stdout {
leaked = append(leaked, fmt.Sprintf("os.Stderr (now fd=%d name=%q)", os.Stderr.Fd(), os.Stderr.Name()))
}
if len(leaked) == 0 {
return code
}
os.Stdout, os.Stderr = stdout, stderr
for _, l := range leaked {
fmt.Fprintf(stderr, "FAIL: %s was leaked by a test in this process; "+
"a capture helper restored it on the happy path only - move the restore into a defer\n", l)
}
if code == 0 {
code = 1
}
return code
}

View file

@ -85,6 +85,10 @@ Install with `uv tool install git+https://github.com/jklenk/thread`. Built by [@
- **[LoopTroop](https://github.com/looptroop-ai/LoopTroop)** - Local AI coding orchestrator for automated task planning, execution, and feedback loops. Uses a Beads-inspired methodology with LLM Council consensus and worktree isolation. Built by [@looptroop-ai](https://github.com/looptroop-ai). (Node.js/TypeScript)
## Mail Delegates
- **[aweb beads-mail](https://aweb.ai/docs/beads-mail/)** - Gives `bd mail` a mail provider in any beads repo: `npm i -g @awebai/aw`, `aw init`, `bd config set mail.delegate "aw beads-mail"`. [aweb](https://github.com/awebai/aweb) is an open-source communication layer for AI agents: each agent gets a stable identity and address, mail and chat are stored durably on a server so recipients can be offline, and a wake-up event tells the recipient's runtime (Claude Code, Pi, Codex, or any process reading the event stream) that work is waiting. Identities are either local, routable only inside one team, or global, addressable as `domain/name` across organizations and across independently run servers, which federate with each other. Messages are signed by the sender's own key and verified against a public registry, so `From:` is an identity rather than a caller-asserted name. MIT; run the whole stack yourself with Docker, or use the hosted service at app.aweb.ai. Built by [@juanre](https://github.com/juanre). (Go/Python)
## Coordination Servers
- **[BeadHub](https://github.com/beadhub/beadhub)** - Open-source coordination server for AI agent teams running beads. The `bdh` CLI is a transparent wrapper over `bd` that adds work claiming, file reservation, presence awareness, and inter-agent messaging (async mail and sync chat). Includes a web dashboard. Free hosted at beadhub.ai for open-source projects. Built by [@juanre](https://github.com/juanre). (Python/TypeScript)

View file

@ -499,6 +499,7 @@ Selected commonly-used variables:
| `BD_DEBUG` | Enable debug logging |
| `BD_MIGRATION_FREEZE_FILE` | Check this exact path for the freeze marker instead of walking ancestor directories; authoritative when set (see [Migration Freeze](#migration-freeze)) |
| `BEADS_DIR` | Force the active beads workspace directory |
| `BEADS_CEILING_DIRECTORIES` | Directories (separated like `PATH`) that `.beads` and `config.yaml` discovery never looks at or above, like git's `GIT_CEILING_DIRECTORIES`; the starting directory is always examined. For sandboxes such as `bazel test` that must not reach the user's own `~/.beads` |
| `BEADS_ACTOR` | Actor identity (preferred over `BD_ACTOR`, which is a deprecated alias) |
| `BEADS_IDENTITY` | Sender identity for `bd mail` |
| `BEADS_FSCK_TIMEOUT` | Runtime-only timeout for the pre-push `dolt fsck --quiet` integrity check (default `30s`) |

View file

@ -15,15 +15,21 @@ override discovery with `BEADS_DIR`.
Startup *config* discovery — the `rev-parse` probe in `internal/config`'s
`gitDirsForRepo`, which locates a linked worktree's shared `.beads` config —
scrubs inherited Git routing variables such as `GIT_DIR` and `GIT_WORK_TREE`
from its subprocess environment whenever it runs. The scrub is unconditional for
that probe, but the probe is a fallback, not a per-command step: it runs only
when nothing earlier in startup has already resolved a config path
(`internal/config/config.go`), so an ancestor `.beads/config.yaml` or an
explicit `BEADS_DIR` short-circuits it. Other startup Git probes still honor
inherited routing, including `internal/git`'s process-cached repository context
and the `.beads` database discovery probes in `internal/beads`, so those
variables can still select which workspace `bd` opens. Clearing those variables
from the `bd` process environment applies only to `bd worktree` commands.
from its subprocess environment whenever it runs, so a routed environment cannot
point that one lookup at another repository. The scrub is unconditional for that
probe, but the probe is a fallback, not a per-command step: it runs only when
nothing earlier in startup has already resolved a config path
(`internal/config/config.go`), so an ancestor `.beads/config.yaml` or an explicit
`BEADS_DIR` short-circuits it. It is a single probe, not a general guarantee:
other startup Git probes still honor inherited routing, including `internal/git`'s
process-cached repository context backing repository-root and hooks-directory
lookups and the `.beads` database discovery probes in `internal/beads`, so those
variables can still select which workspace `bd` opens. Scrubbing also drops any
inherited `GIT_CEILING_DIRECTORIES`, which *widens* upward discovery rather than
narrowing it: the explicit working directory fixes where that probe's search
starts, not where it stops, so the probe can select a containing parent
repository above that directory. Clearing those variables from the `bd` process
environment applies only to `bd worktree` commands.
`GIT_CEILING_DIRECTORIES` is scrubbed along with the redirects. That removes a
*bound* rather than a redirect, so discovery from the working directory is

View file

@ -1,6 +1,6 @@
# CI Cleanup Plan
Last reviewed: 2026-08-10
Last reviewed: 2026-09-03
Freshness source: `engdocs/CI_TEST_SURFACE_AUDIT.md`, `.github/workflows/*.yml`,
`.buildflags`, `.golangci.yml`, `scripts/ci/pr-lint.sh`, `Makefile`, package test
@ -167,8 +167,11 @@ easy to identify and rerun. It includes:
downloads, a five-minute timeout, and the `gms_pure_go` build tag.
- A second non-CGO Windows cross-lint pass when the native target does not
already cover that build tuple.
- A third non-CGO darwin (arm64) cross-lint pass under the same rule. Both
cross-lints are compile-only passes on the Linux runner; they add no macOS or
Windows runner and do not change the platform column above.
Both passes are SCOPED BY LANE. On a PR the wrapper takes
All three passes are SCOPED BY LANE. On a PR the wrapper takes
`BD_LINT_NEW_FROM_MERGE_BASE` and reports only the findings that PR introduces,
matching the `only-new-issues` setting on the sibling `lint` job; on a push to
`main`, and for anyone running `make ci-pr-lint` by hand, it is unset and the

View file

@ -1,6 +1,6 @@
# Linting Policy
Last reviewed: 2026-08-10
Last reviewed: 2026-09-03
Freshness source: `.golangci.yml`, `scripts/ci/pr-lint.sh`, `Makefile`,
`.github/workflows/pr.yml`, and `.github/workflows/main.yml`.
@ -42,7 +42,9 @@ The wrapper runs:
timeout, the `gms_pure_go` build tag, and `--new-from-merge-base` when
`BD_LINT_NEW_FROM_MERGE_BASE` names a ref; and
- a second non-CGO Windows cross-lint pass, on the same scope, when the native
host does not already cover that target.
host does not already cover that target; and
- a third non-CGO darwin (arm64) cross-lint pass, on the same scope, under the
same rule, so `//go:build darwin` files are linted from the Linux runner.
## Policy

View file

@ -81,6 +81,30 @@ To skip an optional service explicitly, use the existing skip mechanism:
BEADS_TEST_SKIP=dolt ./scripts/test.sh ./...
```
Tests that need a Dolt SQL server get one from `internal/testutil`
(`EnsureDoltContainerForTestMain`, `RequireDoltContainer`,
`StartIsolatedDoltContainer[Handle]`, `NewContainerProvider`). Two backends
sit behind that API, selected by `BEADS_TEST_DOLT_SERVER`:
- `container`: the `dolthub/dolt-sql-server` image through testcontainers
(needs docker and the pulled image). The default under plain `go test`.
- `local`: a `dolt sql-server` started by the test process from the pinned
dolt CLI (`BEADS_TEST_DOLT_BINARY`, else `dolt` on `PATH`; it must be the
image's version). No docker. Used only when explicitly selected, under
`go test` and `bazel test` alike (a Bazel target's `env`, or
`--test_env=BEADS_TEST_DOLT_SERVER=local`); unset means `container`, which
in a Bazel action without docker keeps the usual skip.
`BEADS_TEST_REQUIRE_DOLT_CONTAINER=1` turns an unavailable backend into a
failure (per test and in every `TestMain`) instead of a skip; lanes that
exist to run the Dolt suites set it.
Under Bazel, `bazel test //... --config=doltserver` runs the Dolt-backed
suites of pr.yml's "Test (storage domain + uow)" and "Contract corpus" jobs
on the `local` backend (the `dolt-server` targets); they need no docker and
execute remotely with `--config=remote-exec`. `--config=docker` runs the same
suites on the `container` backend (host docker) as the A/B control.
Tests that need a temporary repository or store should use `t.TempDir()` and
`t.Cleanup()`. Temporary repositories must set a repository-local hooks path;
do not inherit the developer's global hooks configuration.

View file

@ -10,8 +10,10 @@ go_library(
importpath = "github.com/steveyegge/beads/internal/beads",
visibility = ["//:__subpackages__"],
deps = [
"//internal/ceiling",
"//internal/configfile",
"//internal/git",
"//internal/gitenv",
"//internal/storage",
"//internal/storage/backends",
"//internal/utils",
@ -22,22 +24,34 @@ go_test(
name = "beads_test",
srcs = [
"beads_coverage_test.go",
"beads_hash_multiclone_test.go",
"beads_inherited_artifacts_test.go",
"beads_integration_test.go",
"beads_multidb_test.go",
"beads_symlink_test.go",
"beads_test.go",
"context_test.go",
"cwd_priority_test.go",
"fingerprint_test.go",
"routing_integration_test.go",
"sqlite_database_path_test.go",
"suite_temp_test.go",
"testmain_default_test.go",
"testmain_integration_test.go",
"testmain_test.go",
],
embed = [":beads"],
deps = [
"//internal/ceiling",
"//internal/configfile",
"//internal/doltserver",
"//internal/git",
"//internal/gitenv",
"//internal/routing",
"//internal/storage/dolt",
"//internal/testutil",
"//internal/testutil/bazeltest",
"//internal/types",
"//internal/utils",
],
)

View file

@ -19,6 +19,7 @@ import (
"sync"
"time"
"github.com/steveyegge/beads/internal/ceiling"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/gitenv"
@ -786,10 +787,14 @@ func hasBeadsProjectFiles(beadsDir string) bool {
// when they agree on which ancestors exist. The ceiling is not extended to "/"
// because it guards against ambient state in a world-writable shared root,
// which the filesystem root is not.
//
// BEADS_CEILING_DIRECTORIES (package ceiling) also ends the walk: no directory
// at or above a ceiling containing the origin is yielded.
type AncestorDirWalk struct {
next string
origin string
tempRoot string
bound *ceiling.Bound
done bool
}
@ -797,10 +802,12 @@ type AncestorDirWalk struct {
// caller's actual discovery start even when startDir begins a later segment of
// a bounded walk.
func NewAncestorDirWalk(startDir, originDir string) *AncestorDirWalk {
origin := canonicalizeAncestorWalkPath(originDir)
return &AncestorDirWalk{
next: canonicalizeAncestorWalkPath(startDir),
origin: canonicalizeAncestorWalkPath(originDir),
origin: origin,
tempRoot: canonicalizeAncestorWalkPath(os.TempDir()),
bound: ceiling.For(origin),
}
}
@ -832,12 +839,17 @@ func canonicalizeAncestorWalkPath(path string) string {
}
}
// Next returns the next directory permitted by the temp-root ceiling.
// Next returns the next directory permitted by the temp-root and
// BEADS_CEILING_DIRECTORIES ceilings.
func (w *AncestorDirWalk) Next() (string, bool) {
if w == nil || w.done || w.next == "" || w.next == "." {
return "", false
}
dir := w.next
if w.bound.Excludes(dir) {
w.done = true
return "", false
}
if w.tempRoot != "" && dir == w.tempRoot && dir != w.origin {
w.done = true
return "", false

View file

@ -12,6 +12,7 @@ import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/ceiling"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/gitenv"
@ -593,6 +594,33 @@ func TestAncestorDirWalkEndsAtOSTempRoot(t *testing.T) {
}
}
// TestAncestorDirWalkStopsBelowBeadsCeiling pins that BEADS_CEILING_DIRECTORIES
// ends every FindBeadsDir-style walk below the ceiling, so a .beads at or
// above it is never found.
func TestAncestorDirWalkStopsBelowBeadsCeiling(t *testing.T) {
sandbox := t.TempDir()
ceilingDir := filepath.Join(sandbox, "ceiling")
start := filepath.Join(ceilingDir, "project", "nested")
if err := os.MkdirAll(start, 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("TMPDIR", filepath.Join(sandbox, "elsewhere"))
t.Setenv(ceiling.EnvVar, ceilingDir)
var got []string
walk := NewAncestorDirWalk(start, start)
for dir, ok := walk.Next(); ok; dir, ok = walk.Next() {
got = append(got, dir)
}
want := []string{
canonicalizeAncestorWalkPath(start),
canonicalizeAncestorWalkPath(filepath.Join(ceilingDir, "project")),
}
if !slices.Equal(got, want) {
t.Fatalf("walk yielded %q, want %q", got, want)
}
}
// TestAncestorDirWalkYieldsFilesystemRoot pins the documented choice that the
// walk includes the filesystem root. Most of the hand-rolled loops this type
// replaced stopped before "/"; the unified walk does not, so the behavior is
@ -603,8 +631,10 @@ func TestAncestorDirWalkYieldsFilesystemRoot(t *testing.T) {
if err := os.MkdirAll(start, 0o755); err != nil {
t.Fatal(err)
}
// Point the ceiling somewhere off this walk so it can reach the root.
// Point the ceiling somewhere off this walk so it can reach the root, and
// lift any BEADS_CEILING_DIRECTORIES the test runner set.
t.Setenv("TMPDIR", filepath.Join(sandbox, "tmp"))
t.Setenv(ceiling.EnvVar, "")
fsRoot := canonicalizeAncestorWalkPath(start)
for {

View file

@ -27,7 +27,10 @@ func setupIntegrationTestMain(root string) (func(), error) {
os.Setenv("BEADS_TEST_SERVER", "1")
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
if testutil.DoltUnavailableForTestMain(err) {
cleanup()
return nil, err
}
return cleanup, nil
}

View file

@ -0,0 +1,14 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "ceiling",
srcs = ["ceiling.go"],
importpath = "github.com/steveyegge/beads/internal/ceiling",
visibility = ["//:__subpackages__"],
)
go_test(
name = "ceiling_test",
srcs = ["ceiling_test.go"],
embed = [":ceiling"],
)

131
internal/ceiling/ceiling.go Normal file
View file

@ -0,0 +1,131 @@
// Package ceiling bounds the upward directory searches bd uses to discover a
// .beads workspace or its config.yaml.
//
// BEADS_CEILING_DIRECTORIES is a list of absolute directories separated by the
// OS path-list separator (":" on POSIX, ";" on Windows), like git's
// GIT_CEILING_DIRECTORIES. A search that starts inside a ceiling never examines
// the ceiling itself or anything above it; the start directory is always
// examined. Ceilings that do not contain the start have no effect, and empty or
// relative entries are ignored. When the variable is unset nothing changes.
//
// It exists for sandboxes such as `bazel test`, whose working directory lives
// below the developer's home: without a ceiling, discovery walks out of the
// sandbox and reads or writes the developer's own ~/.beads.
package ceiling
import (
"os"
"path/filepath"
"strings"
)
// EnvVar names the ceiling list.
const EnvVar = "BEADS_CEILING_DIRECTORIES"
// Bound is the ceiling of one upward search. A nil Bound permits every
// directory, so callers can use it unconditionally.
type Bound struct {
start []string
ceilings []string
}
// For returns the bound for a search starting at start, or nil when
// BEADS_CEILING_DIRECTORIES names no ceiling containing start.
func For(start string) *Bound {
list := os.Getenv(EnvVar)
if list == "" {
return nil
}
startForms := forms(start)
var ceilings []string
for _, entry := range filepath.SplitList(list) {
if entry == "" || !filepath.IsAbs(entry) {
continue
}
for _, c := range forms(entry) {
if anyWithin(startForms, c) {
ceilings = append(ceilings, c)
}
}
}
if len(ceilings) == 0 {
return nil
}
return &Bound{start: startForms, ceilings: ceilings}
}
// Excludes reports whether the search must stop before examining dir: dir is
// not the start and is a ceiling or lies above one.
func (b *Bound) Excludes(dir string) bool {
if b == nil {
return false
}
dirForms := forms(dir)
for _, d := range dirForms {
for _, s := range b.start {
if d == s {
return false
}
}
}
for _, c := range b.ceilings {
for _, d := range dirForms {
if within(c, d) {
return true
}
}
}
return false
}
// GitEnv returns env with GIT_CEILING_DIRECTORIES set to the beads ceilings,
// for git probes that locate a workspace (and have scrubbed inherited git
// routing). env is returned unchanged when BEADS_CEILING_DIRECTORIES is unset.
func GitEnv(env []string) []string {
list := os.Getenv(EnvVar)
if list == "" {
return env
}
out := make([]string, 0, len(env)+1)
for _, e := range env {
if !strings.HasPrefix(e, "GIT_CEILING_DIRECTORIES=") {
out = append(out, e)
}
}
return append(out, "GIT_CEILING_DIRECTORIES="+list)
}
// forms returns the cleaned absolute path and, when it differs, its
// symlink-resolved form, so a ceiling and a walk that spell the same directory
// differently still meet.
func forms(path string) []string {
abs, err := filepath.Abs(path)
if err != nil {
return []string{filepath.Clean(path)}
}
abs = filepath.Clean(abs)
if resolved, err := filepath.EvalSymlinks(abs); err == nil && resolved != abs {
return []string{abs, resolved}
}
return []string{abs}
}
// anyWithin reports whether any of paths is parent or lies below it.
func anyWithin(paths []string, parent string) bool {
for _, p := range paths {
if within(p, parent) {
return true
}
}
return false
}
func within(path, parent string) bool {
if path == parent {
return true
}
if !strings.HasSuffix(parent, string(filepath.Separator)) {
parent += string(filepath.Separator)
}
return strings.HasPrefix(path, parent)
}

View file

@ -0,0 +1,102 @@
package ceiling
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// walk mimics a discovery loop: every directory from start upward that the
// bound lets it examine.
func walk(start string) []string {
b := For(start)
var seen []string
for dir := start; ; dir = filepath.Dir(dir) {
if b.Excludes(dir) {
break
}
seen = append(seen, dir)
if filepath.Dir(dir) == dir {
break
}
}
return seen
}
func TestUnsetExaminesEveryAncestor(t *testing.T) {
t.Setenv(EnvVar, "")
root := t.TempDir()
start := filepath.Join(root, "a", "b")
if For(start) != nil {
t.Fatal("For must return nil when the variable is unset")
}
got := walk(start)
if !slices.Contains(got, root) || got[len(got)-1] != filepath.Dir(got[len(got)-1]) {
t.Fatalf("unset ceiling must reach the filesystem root, got %v", got)
}
}
func TestCeilingStopsBelowItself(t *testing.T) {
root := t.TempDir()
start := filepath.Join(root, "a", "b")
if err := os.MkdirAll(start, 0o755); err != nil {
t.Fatal(err)
}
t.Setenv(EnvVar, strings.Join([]string{"relative/ignored", "", root}, string(os.PathListSeparator)))
got := walk(start)
want := []string{start, filepath.Join(root, "a")}
if !slices.Equal(got, want) {
t.Fatalf("walk = %v, want %v", got, want)
}
}
func TestStartAtCeilingIsExaminedButNothingAbove(t *testing.T) {
root := t.TempDir()
t.Setenv(EnvVar, root)
if got := walk(root); !slices.Equal(got, []string{root}) {
t.Fatalf("walk = %v, want only the start", got)
}
}
func TestUnrelatedCeilingHasNoEffect(t *testing.T) {
root := t.TempDir()
other := filepath.Join(root, "other")
start := filepath.Join(root, "a")
t.Setenv(EnvVar, other)
if For(start) != nil {
t.Fatal("a ceiling that does not contain the start must not bound it")
}
}
func TestCeilingMatchesThroughSymlink(t *testing.T) {
root := t.TempDir()
realDir := filepath.Join(root, "realDir")
start := filepath.Join(realDir, "a")
if err := os.MkdirAll(start, 0o755); err != nil {
t.Fatal(err)
}
link := filepath.Join(root, "link")
if err := os.Symlink(realDir, link); err != nil {
t.Skipf("symlinks unavailable: %v", err)
}
t.Setenv(EnvVar, link)
got := walk(start)
if !slices.Equal(got, []string{start}) {
t.Fatalf("walk = %v, want only %s (ceiling named through a symlink)", got, start)
}
}
func TestGitEnv(t *testing.T) {
env := []string{"A=1", "GIT_CEILING_DIRECTORIES=/old"}
t.Setenv(EnvVar, "")
if got := GitEnv(env); !slices.Equal(got, env) {
t.Fatalf("unset: GitEnv = %v, want unchanged", got)
}
t.Setenv(EnvVar, "/x")
want := []string{"A=1", "GIT_CEILING_DIRECTORIES=/x"}
if got := GitEnv(env); !slices.Equal(got, want) {
t.Fatalf("GitEnv = %v, want %v", got, want)
}
}

View file

@ -26,6 +26,7 @@ go_library(
go_test(
name = "compact_test",
srcs = [
"compactor_test.go",
"compactor_unit_test.go",
"git_test.go",
"haiku_test.go",
@ -33,6 +34,8 @@ go_test(
embed = [":compact"],
deps = [
"//internal/config",
"//internal/storage/dolt",
"//internal/testutil",
"//internal/types",
"@com_github_anthropics_anthropic_sdk_go//:anthropic-sdk-go",
],

View file

@ -17,6 +17,7 @@ go_library(
importpath = "github.com/steveyegge/beads/internal/config",
visibility = ["//:__subpackages__"],
deps = [
"//internal/ceiling",
"//internal/debug",
"//internal/gitenv",
"@com_github_spf13_viper//:viper",

View file

@ -9,6 +9,7 @@ import (
"time"
"github.com/spf13/viper"
"github.com/steveyegge/beads/internal/ceiling"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/gitenv"
"gopkg.in/yaml.v3"
@ -139,10 +140,12 @@ func Initialize() error {
cwd, err := os.Getwd()
if err == nil {
// BEADS_CEILING_DIRECTORIES bounds both upward walks below.
bound := ceiling.For(cwd)
var moduleRoot string
if ignoreRepoConfig {
// Find module root by walking up to go.mod.
for dir := cwd; dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
for dir := cwd; dir != filepath.Dir(dir) && !bound.Excludes(dir); dir = filepath.Dir(dir) {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
moduleRoot = dir
break
@ -172,7 +175,7 @@ func Initialize() error {
}
// Walk up parent directories to find .beads/config.yaml.
for dir := cwd; dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
for dir := cwd; dir != filepath.Dir(dir) && !bound.Excludes(dir); dir = filepath.Dir(dir) {
p := filepath.Join(dir, ".beads", "config.yaml")
if _, err := os.Stat(p); err == nil {
// When BEADS_DIR points at a different runtime workspace, do not
@ -436,8 +439,15 @@ func gitDirsForRepo(repoPath string) (gitDir, commonDir string, ok bool) {
cmd := exec.Command("git", "-C", repoPath, "rev-parse", "--git-dir", "--git-common-dir")
// repoPath is the authority for this probe. Inherited Git routing such as
// GIT_DIR overrides -C and can make startup read another repository's
// shared-worktree config before command dispatch has begun.
cmd.Env = gitenv.ScrubRouting(os.Environ())
// shared-worktree config before command dispatch has begun. Scrubbing also
// drops an inherited GIT_CEILING_DIRECTORIES, which widens rather than
// narrows discovery: -C repoPath fixes where the search starts, not where it
// stops, so dropping discovery ceilings can select a containing parent
// repository above repoPath. That is the intended trade, and it applies here
// too -- repoPath is the process working directory, not a proven repository
// root. An explicit BEADS_CEILING_DIRECTORIES is the exception: it is
// reapplied as the git ceiling (a no-op when unset).
cmd.Env = ceiling.GitEnv(gitenv.ScrubRouting(os.Environ()))
output, err := cmd.Output()
if err != nil {
return "", "", false

View file

@ -282,6 +282,46 @@ func TestInitialize_IgnoresModuleRootConfigWhenRequested(t *testing.T) {
}
}
// TestInitialize_StopsAtBeadsCeiling pins that BEADS_CEILING_DIRECTORIES keeps
// the config.yaml walk from reaching a .beads at or above the ceiling — here
// an unparseable one that would fail Initialize if it were read.
func TestInitialize_StopsAtBeadsCeiling(t *testing.T) {
restore := envSnapshot(t)
defer restore()
tmpDir := t.TempDir()
homeDir := filepath.Join(tmpDir, "home")
configDir := filepath.Join(tmpDir, "xdg-config")
ceilingDir := filepath.Join(tmpDir, "ceiling")
workDir := filepath.Join(ceilingDir, "work")
for _, dir := range []string{homeDir, configDir, workDir, filepath.Join(tmpDir, ".beads"), filepath.Join(ceilingDir, ".beads")} {
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
}
for _, dir := range []string{tmpDir, ceilingDir} {
if err := os.WriteFile(filepath.Join(dir, ".beads", "config.yaml"), []byte("json: [unclosed\n"), 0o644); err != nil {
t.Fatal(err)
}
}
t.Setenv("HOME", homeDir)
t.Setenv("XDG_CONFIG_HOME", configDir)
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "")
t.Setenv("BEADS_CEILING_DIRECTORIES", ceilingDir)
t.Chdir(workDir)
ResetForTesting()
defer ResetForTesting()
if err := Initialize(); err != nil {
t.Fatalf("Initialize() read config at or above the ceiling: %v", err)
}
if got := ConfigFileUsed(); got != "" {
t.Fatalf("ConfigFileUsed() = %q, want none below the ceiling", got)
}
}
func TestLocalConfigOverride(t *testing.T) {
// Isolate from environment variables
restore := envSnapshot(t)

View file

@ -129,6 +129,56 @@ func TestSecretGitTrackingIgnoresInheritedGitRouting(t *testing.T) {
}
}
// A checkout reachable only through inherited Git routing is the case a
// scrubbed-only probe cannot see at all: it finds no repository, reports
// "untracked", and the fail-open guard writes the secret into a tracked
// config.yaml. The inherited fallback is what keeps the refusal working there.
func TestSecretGitTrackingHonorsLegitimateGitRouting(t *testing.T) {
gitDir, work := t.TempDir(), t.TempDir()
routedGit := func(args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = work
cmd.Env = append(gitenv.ScrubRouting(os.Environ()), "GIT_DIR="+gitDir, "GIT_WORK_TREE="+work)
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("routed git %v: %v\n%s", args, err, output)
}
}
routedGit("init", "--quiet")
tracked := filepath.Join(work, "config.yaml")
untracked := filepath.Join(work, "untracked.yaml")
for _, path := range []string{tracked, untracked} {
if err := os.WriteFile(path, []byte("json: false\n"), 0o600); err != nil {
t.Fatal(err)
}
}
routedGit("add", "--", tracked)
// The work tree holds no .git entry, so the scrubbed probe must fail here.
// Without this precondition the inherited fallback could pass vacuously.
scrubbed := exec.Command("git", "ls-files", "--error-unmatch", tracked)
scrubbed.Dir = work
scrubbed.Env = gitenv.ScrubRouting(os.Environ())
if scrubbed.Run() == nil {
t.Fatal("scrubbed probe already resolves the routed checkout; fixture proves nothing")
}
t.Setenv("GIT_DIR", gitDir)
t.Setenv("GIT_WORK_TREE", work)
if !isGitTracked(tracked) {
t.Error("legitimate routing hid the tracked file from the secret guard")
}
if isGitTracked(untracked) {
t.Error("untracked file reported as tracked")
}
if err := checkSecretGitTracked(tracked, "linear.api_key"); err == nil || !strings.Contains(err.Error(), "refusing to write secret key") {
t.Errorf("tracked secret refusal = %v", err)
}
if err := checkSecretGitTracked(untracked, "linear.api_key"); err != nil {
t.Errorf("untracked secret refused: %v", err)
}
}
// A bare repository whose work tree is named only by GIT_DIR/GIT_WORK_TREE is
// invisible to the scrubbed probe — it is reachable through the inherited
// routing alone. The secret guard must still refuse a write into the tracked

View file

@ -8,5 +8,6 @@ import "os"
// caller's descriptor identity check prevents chmod unless it still refers to
// the directory inspected with Lstat.
func openBeadsDirHandle(path string) (beadsDirHandle, error) {
//nolint:gosec // G304: path is the .beads directory fixBeadsDirPermissions just inspected with Lstat; its descriptor identity check gates the chmod.
return os.Open(path)
}

View file

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
)
const userConfigYamlDisplayFallback = "~/.config/bd/config.yaml"
@ -27,6 +28,20 @@ func currentUserConfigYamlCandidates() userConfigYamlCandidates {
return buildUserConfigYamlCandidates(homeDir, homeErr, nativeConfigDir, nativeErr)
}
// nativeUserConfigValidationSource names the source of a relative directory
// returned without a resolver error. On Unix, os.UserConfigDir already rejects
// relative XDG_CONFIG_HOME values itself; only HOME can reach our validation.
func nativeUserConfigValidationSource() string {
switch runtime.GOOS {
case "windows":
return "APPDATA"
case "plan9":
return "home"
default:
return "HOME"
}
}
func buildUserConfigYamlCandidates(homeDir string, homeErr error, nativeConfigDir string, nativeErr error) userConfigYamlCandidates {
var candidates userConfigYamlCandidates
@ -37,7 +52,13 @@ func buildUserConfigYamlCandidates(homeDir string, homeErr error, nativeConfigDi
candidates.documented = filepath.Clean(filepath.Join(home, ".config", "bd", "config.yaml"))
}
if nativeDir, err := cleanAbsoluteUserDirectory("native user config directory", nativeConfigDir, nativeErr); err != nil {
nativeLabel := "native user config directory"
if nativeErr == nil {
nativeLabel += " (" + nativeUserConfigValidationSource() + ")"
}
// Resolver errors already identify their source; retain that diagnostic
// without inferring it from a later read of the process environment.
if nativeDir, err := cleanAbsoluteUserDirectory(nativeLabel, nativeConfigDir, nativeErr); err != nil {
candidates.nativeErr = err
} else {
candidates.native = filepath.Clean(filepath.Join(nativeDir, "bd", "config.yaml"))

View file

@ -4,9 +4,75 @@ import (
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestUserConfigYamlPathNamesNativeEnvironmentSource(t *testing.T) {
t.Setenv("HOME", "~")
t.Setenv("USERPROFILE", "~")
t.Setenv("home", "~")
t.Setenv("APPDATA", "relative-appdata")
for _, xdg := range []string{"relative-xdg", ""} {
name := "XDG configured"
wantSource := "XDG_CONFIG_HOME"
if xdg == "" {
name, wantSource = "XDG absent", "HOME"
}
switch runtime.GOOS {
case "windows":
wantSource = "APPDATA"
case "darwin", "ios":
wantSource = "HOME"
case "plan9":
wantSource = "home"
}
t.Run(name, func(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", xdg)
path, err := UserConfigYamlPath()
if err == nil || path != "" {
t.Fatalf("unsafe roots produced path %q, err=%v", path, err)
}
wantLabel := "native user config directory (" + wantSource + ")"
if wantSource == "XDG_CONFIG_HOME" {
// Go rejects relative XDG roots before returning a directory;
// preserve its source-bearing error under the generic label.
wantLabel = "native user config directory: "
}
if !strings.Contains(err.Error(), wantLabel) || !strings.Contains(err.Error(), wantSource) {
t.Errorf("native config error %q does not identify %s", err, wantSource)
}
})
}
}
func TestUserConfigYamlCandidatesOwnNativeDiagnostics(t *testing.T) {
wantSource := "HOME"
if runtime.GOOS == "windows" {
wantSource = "APPDATA"
} else if runtime.GOOS == "plan9" {
wantSource = "home"
}
t.Run("validation identifies its source", func(t *testing.T) {
candidates := buildUserConfigYamlCandidates(t.TempDir(), nil, "relative-native", nil)
want := "native user config directory (" + wantSource + ")"
if candidates.nativeErr == nil || !strings.Contains(candidates.nativeErr.Error(), want) {
t.Fatalf("builder native error = %v, want label %q", candidates.nativeErr, want)
}
})
t.Run("resolver diagnostic and identity are preserved", func(t *testing.T) {
resolverErr := errors.New("XDG_CONFIG_HOME resolver diagnostic")
candidates := buildUserConfigYamlCandidates(t.TempDir(), nil, "", resolverErr)
if !errors.Is(candidates.nativeErr, resolverErr) {
t.Fatalf("builder native error = %v, want original resolver error", candidates.nativeErr)
}
if got, want := candidates.nativeErr.Error(), "native user config directory: "+resolverErr.Error(); got != want {
t.Fatalf("builder native error = %q, want %q", got, want)
}
})
}
func TestSelectUserConfigYamlPathPrecedence(t *testing.T) {
t.Run("existing documented path wins", func(t *testing.T) {
home, native := userConfigTestRoots(t)

View file

@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"github.com/steveyegge/beads/internal/ceiling"
"github.com/steveyegge/beads/internal/gitenv"
"gopkg.in/yaml.v3"
)
@ -750,7 +751,8 @@ func findProjectBeadsDir() string {
return ""
}
for dir := cwd; dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
bound := ceiling.For(cwd)
for dir := cwd; dir != filepath.Dir(dir) && !bound.Excludes(dir); dir = filepath.Dir(dir) {
beadsDir := filepath.Join(dir, ".beads")
if info, err := os.Stat(beadsDir); err == nil && info.IsDir() {
return beadsDir

View file

@ -1157,6 +1157,7 @@ func TestEnvVarOverrides(t *testing.T) {
})
t.Run("database default", func(t *testing.T) {
t.Setenv("BEADS_DOLT_SERVER_DATABASE", "")
cfg := &Config{}
if got := cfg.GetDoltDatabase(); got != DefaultDoltDatabase {
t.Errorf("GetDoltDatabase() = %q, want %q", got, DefaultDoltDatabase)
@ -1164,6 +1165,7 @@ func TestEnvVarOverrides(t *testing.T) {
})
t.Run("database config value", func(t *testing.T) {
t.Setenv("BEADS_DOLT_SERVER_DATABASE", "")
cfg := &Config{DoltDatabase: "mydb"}
if got := cfg.GetDoltDatabase(); got != "mydb" {
t.Errorf("GetDoltDatabase() = %q, want mydb", got)

View file

@ -50,23 +50,29 @@ go_test(
name = "doltserver_test",
srcs = [
"configport_test.go",
"dirty_state_test.go",
"doltserver_test.go",
"doltserver_unix_test.go",
"doltserver_windows_test.go",
"fdleak_integration_test.go",
"gc_config_test.go",
"lifecycle_integration_test.go",
"logrotate_test.go",
"managed_server_proof_test.go",
"manifest_recovery_test.go",
"physical_root_test.go",
"port_race_test.go",
"portsource_invariant_test.go",
"portsources_test.go",
"probe_test.go",
"procattr_linux_test.go",
"socket_integration_test.go",
"sweep_darwin_test.go",
"sweep_reap_unix_test.go",
"sweep_suite_linux_test.go",
"sweep_suite_test.go",
"sweep_test.go",
"testmain_integration_test.go",
],
embed = [":doltserver"],
deps = [
@ -75,6 +81,98 @@ go_test(
"//internal/githooksenv",
"@com_github_dolthub_dolt_go//libraries/doltcore/servercfg",
] + select({
"@rules_go//go/platform:aix": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:android": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
"@org_golang_x_sys//unix",
],
"@rules_go//go/platform:darwin": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:dragonfly": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:freebsd": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:illumos": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:ios": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:js": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:linux": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
"@org_golang_x_sys//unix",
],
"@rules_go//go/platform:netbsd": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:openbsd": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:osx": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:plan9": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:qnx": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:solaris": [
"//internal/storage/doltutil",
"//internal/testutil",
"//internal/testutil/integration",
"@com_github_go_sql_driver_mysql//:mysql",
],
"@rules_go//go/platform:windows": [
"@org_golang_x_sys//windows",
],

View file

@ -120,6 +120,30 @@ type HooksContext struct {
HooksDir, CommonDir, RepoRoot, MainRepoRoot string
}
// normalizeHooksWorkDir anchors workDir and normalizes its symlinks and case
// once. Every HooksContext producer shares it so they cannot drift into emitting
// different spellings of the same directory: worktree code string-compares these
// paths (GH#880).
// The caller-supplied directory must resolve before it can select a repo:
// unlike the discovered repoRoot spelling in loadGitContext, it is an input
// to Git.
func normalizeHooksWorkDir(workDir string) (string, error) {
workDir, err := filepath.Abs(workDir)
if err != nil {
return "", err
}
// This caller-supplied directory must resolve before it can select a repo.
// Unlike the discovered repoRoot spelling, it is an input to Git.
workDir, err = filepath.EvalSymlinks(workDir)
if err != nil {
return "", fmt.Errorf("resolve hooks working directory: %w", err)
}
if canonical := canonicalizeCase(workDir); canonical != "" {
workDir = canonical
}
return workDir, nil
}
// ResolveHooksContext reads fresh paths without changing the legacy cache.
// workDir must be nonempty and is anchored and symlink/case-normalized once;
// bare/non-repositories error. Configured absolute hook paths retain their spelling.
@ -132,26 +156,15 @@ type HooksContext struct {
//
// This is the explicit-context sibling of GetGitHooksDir. It returns a struct
// instead of following this file's GetXFrom(startDir) convention because all
// four paths must come from one resolution of one directory. The first in-repo
// caller arrives with GH#6440, so at this commit only tests exercise the
// contract above.
// four paths must come from one resolution of one directory.
func ResolveHooksContext(workDir string, env []string) (HooksContext, error) {
if workDir == "" {
return HooksContext{}, fmt.Errorf("hooks context requires a working directory")
}
workDir, err := filepath.Abs(workDir)
workDir, err := normalizeHooksWorkDir(workDir)
if err != nil {
return HooksContext{}, err
}
// This caller-supplied directory must resolve before it can select a repo.
// Unlike the discovered repoRoot spelling above, it is an input to Git.
workDir, err = filepath.EvalSymlinks(workDir)
if err != nil {
return HooksContext{}, fmt.Errorf("resolve hooks working directory: %w", err)
}
if canonical := canonicalizeCase(workDir); canonical != "" {
workDir = canonical
}
ctx := loadGitContext(workDir, env)
if ctx.err != nil {
return HooksContext{}, ctx.err
@ -166,6 +179,58 @@ func ResolveHooksContext(workDir string, env []string) (HooksContext, error) {
RepoRoot: ctx.repoRoot, MainRepoRoot: ctx.mainRepoRoot()}, nil
}
// ResolveWorkTreelessHooksContext resolves hook paths for a repository whose
// common directory resolves but whose work tree does not: a bare repository, or
// a directory such as a repository's own .git that Git answers from while
// reporting no work tree. ResolveHooksContext requires one, because it cannot
// honor its RepoRoot contract without it; this is the explicit counterpart, so a
// caller opts into the weaker shape instead of silently receiving one. RepoRoot
// and MainRepoRoot are empty — there is no work-tree root — and a relative
// core.hooksPath is anchored to the common directory, which is where Git runs
// hooks without a work tree.
// It fails for non-repositories and for directories inside a work tree alike, so
// callers can use it as a fallback after ResolveHooksContext without widening
// that call's failure contract. workDir and env are handled exactly as
// ResolveHooksContext handles them, anchoring and normalization included.
func ResolveWorkTreelessHooksContext(workDir string, env []string) (HooksContext, error) {
if workDir == "" {
return HooksContext{}, fmt.Errorf("hooks context requires a working directory")
}
workDir, err := normalizeHooksWorkDir(workDir)
if err != nil {
return HooksContext{}, err
}
probe := exec.Command("git", "rev-parse", "--is-inside-work-tree", "--git-common-dir")
probe.Dir, probe.Env = workDir, env
output, err := probe.Output()
if err != nil {
return HooksContext{}, fmt.Errorf("resolve work-tree-less Git repository: %w", err)
}
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(lines) < 2 {
return HooksContext{}, fmt.Errorf("unexpected git rev-parse output: got %d lines, expected 2", len(lines))
}
// A work tree resolves here, so ResolveHooksContext's stronger contract is
// the one that applies and its error is the one the caller should surface.
if strings.TrimSpace(lines[0]) == "true" {
return HooksContext{}, fmt.Errorf("%s is inside a Git work tree", workDir)
}
commonDir, err := absoluteGitPath(workDir, strings.TrimSpace(lines[1]))
if err != nil {
return HooksContext{}, fmt.Errorf("failed to resolve common dir path: %w", err)
}
cmd := exec.Command("git", "config", "--get", "core.hooksPath")
cmd.Dir, cmd.Env = workDir, env
// Without a work tree Git runs hooks in the common directory, so it anchors a
// relative core.hooksPath in place of the absent work-tree root.
ctx := gitContext{gitDirRaw: commonDir, commonDir: commonDir, repoRoot: commonDir}
hooksDir, err := gitHooksDir(cmd, func() (*gitContext, error) { return &ctx, nil })
if err != nil {
return HooksContext{}, err
}
return HooksContext{HooksDir: hooksDir, CommonDir: commonDir}, nil
}
// getGitContext returns the cached git context, initializing it if needed.
func getGitContext() (*gitContext, error) {
gitCtxOnce.Do(initGitContext)

Some files were not shown because too many files have changed in this diff Show more