perf(dolt): eliminate 12s slow path on bd commands when .local_version is stale (#3710)

* perf(dolt): be-1he eliminate 12s `dolt remote -v` against multi-db server roots

Three layers of defense for the bd 12s slow-path bug:

- Layer 1: migrateServerRootRemotes skips when rootDir/.dolt/repo_state.json
  is missing. Multi-database server roots have only sql-server.info; running
  `dolt remote -v` there takes ~12s to fail.
- Layer 2: ListCLIRemotes wraps the dolt subprocess in a 2s context timeout.
  Backstops Layer 1 against any future variant of the broken-parent-dir case.
- Layer 3: autoMigrateOnVersionBump does a read-only probe of bd_version
  before opening writeable. Read-only stores skip syncCLIRemotesToSQL, so a
  stale .local_version (e.g. from a multi-binary install) no longer triggers
  the slow path on every command.

End-user impact pre-fix: gc mail inbox 60-80s, gc status 30s+ hangs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore: release gate PASS for be-1he (3-layer slow-path fix)

* docs(repro): add be-1he slow-path repro script (be-vxngmn)

Adds scripts/repro-be-1he-slow-path/repro.sh + REPRO.md demonstrating
the 12-second `dolt remote -v` slow path on multi-DB server roots.

Satisfies coffeegoddd's review request: "We need a beads-only repro of
this to prove its a bug."

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(cli): regenerate CLI reference after slow-path fix rebase

Output of ./scripts/generate-cli-docs.sh ./bd and ./scripts/generate-llms-full.sh.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(cli): regenerate CLI reference + llms-full with pure-go bd

The previous regen (0aeb5934e) was built with a CGO-enabled bd, which
includes the CGO-only `bd federation` subcommands. CI builds the docs
binary with CGO_ENABLED=0 (scripts/ci/pr-policy.sh build_docs_binary),
so check-doc-flags rejected the extra federation subcommand pages.

Regenerated docs/CLI_REFERENCE.md, the federation cli-reference pages
(live + versioned 1.0.0/1.0.4/1.0.5), and website/static/llms-full.txt
with a CGO_ENABLED=0 binary. check-doc-flags + check-doc-freshness now
pass locally under LC_ALL=C.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(release-gate): correct be-1he layer description to match shipped diff

The PR body and this gate doc both described a "Layer 1" repo_state.json
sentinel in internal/storage/dolt/federation.go, but the actual diff never
touched that file - only cmd/bd/version_tracking.go (Layer 3) and
internal/storage/doltutil/remotes.go (Layer 2) are changed. Correct the
gate doc's commit table, acceptance-criteria row, and follow-up/out-of-scope
notes to describe only what ships. No code or test changes; Layer 2 and
Layer 3 remain independently verified as still-needed on current main
(ListCLIRemotes has no timeout, no equivalent sentinel exists).

Agent-Signature: claude-sonnet-medium on behalf of maphew

* fix(dolt): scope ListCLIRemotes timeout, fix repro.sh, purge Layer 1 narrative

Addresses the cross-vendor review's four should-fixes plus the comment nit
(gastownhall/beads#3710 review, 2026-07-24):

1. ListCLIRemotes' 2s cap could turn a slow-but-valid dolt remote -v into
   "remote absent": FindCLIRemote folds any error (including a timeout) into
   "", and EnsureCLIRemote then blind-adds on that signal, which hard-fails
   if the remote actually exists. Chose the "scope the cap" fix over
   "propagate DeadlineExceeded distinctly": it's fully contained inside
   ListCLIRemotes (no changes needed at the three call sites - remotes.go:192
   ensure-path, store.go:1958 URL match via hasMatchingCLIRemote, and
   doctor/federation.go:127, which already treats errors distinctly from "no
   remotes"), whereas threading a sentinel error type through FindCLIRemote's
   string-returning signature and its callers touches more surface for the
   same outcome. ListCLIRemotes now stats dbPath/.dolt/repo_state.json: 2s
   cap when absent (the known broken multi-DB server-root case - there's
   never a real answer coming from there), 30s cap otherwise (a real repo's
   `dolt remote -v` is ~130ms even loaded, so 30s only bites a genuinely hung
   subprocess). Added TestListCLIRemotesTimeout covering both branches,
   following the t.TempDir() pattern already used in
   persisted_remotes_test.go.

2. repro.sh's baseline `dolt remote -v` ran in the invocation directory, not
   $SERVER_ROOT, so the central "slow" measurement never exercised the
   constructed broken root. Fixed to `(cd "$SERVER_ROOT" && dolt remote -v
   2>&1) || true`, matching the correct subshell pattern already used later
   in the script. Also relabeled the "PARTIAL FIX ... Layer 2 timeout capped
   it" branch: the 2s cap lives in bd's Go code, not dolt, and this script
   calls the raw dolt binary directly - it's never capped. Relabeled to
   describe the underlying dolt slowness the script demonstrates instead of
   implying bd's timeout is in play here.

3. `dolt init --quiet` isn't supported by dolt 2.2.x and set -e killed the
   script before it reached the summary. Redirected output instead
   (`dolt init >/dev/null 2>&1`). Ran repro.sh end-to-end locally (dolt
   2.2.0): completes with exit 0, and the fixed subshell now shows real
   timing/output from inside $SERVER_ROOT.

4. Purged the unshipped "Layer 1" (federation.go sentinel) narrative from
   repro.sh and REPRO.md - same reconciliation already done for the release
   gate doc and PR body. Both files now describe only the two layers that
   ship (remotes.go timeout, version_tracking.go read-only probe), with a
   short note that an earlier draft described a Layer 1 that never landed.

5. cmd/bd/version_tracking.go:195-197 comment cited syncCLIRemotesToSQL,
   which doesn't exist anywhere in the tree, and overstated the read-only
   probe's benefit. On current main the writeable-open gate reads remotes
   via doltutil.PersistedRemotes (a fast on-disk repo_state.json probe, no
   dolt subprocess), so the probe saves an initSchema round-trip, not a 12s
   hang. Corrected.

Agent-Signature: claude-sonnet-medium on behalf of maphew

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
This commit is contained in:
Jim Wordelman 2026-07-24 00:53:06 -07:00 • committed by GitHub
commit ae48b0a090
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 365 additions and 2 deletions

View file

@ -201,6 +201,21 @@ func autoMigrateOnVersionBump(beadsDir string) {
ctx = context.Background()
}
// Read-only probe: if the DB is already at the current version, skip the
// writeable open. On current main the writeable-open gate reads remotes
// via doltutil.PersistedRemotes, a fast on-disk repo_state.json probe —
// not a `dolt remote -v` subprocess — so this doesn't save a 12s hang;
// it saves an unnecessary initSchema round-trip when no migration is
// needed. (be-1he)
if roStore, roErr := dolt.NewFromConfigWithOptions(ctx, beadsDir, &dolt.Config{ReadOnly: true}); roErr == nil {
dbVersion, _ := roStore.GetLocalMetadata(ctx, "bd_version")
_ = roStore.Close()
if dbVersion == Version {
debug.Logf("auto-migrate: database already at version %s (ro probe)", Version)
return
}
}
store, err := dolt.NewFromConfig(ctx, beadsDir)
if err != nil {
// Failed to open database - skip migration

View file

@ -1,6 +1,7 @@
package doltutil
import (
"context"
"encoding/json"
"fmt"
"os"
@ -9,6 +10,7 @@ import (
"sort"
"strings"
"sync"
"time"
"github.com/steveyegge/beads/internal/doltremote"
"github.com/steveyegge/beads/internal/remotecache"
@ -22,6 +24,36 @@ func cliRemoteLock(dbPath string) *sync.Mutex {
return lock.(*sync.Mutex)
}
// listCLIRemotesTimeoutBroken caps `dolt remote -v` wallclock for a database
// directory that lacks .dolt/repo_state.json — the known broken-parent-dir
// failure mode (e.g. a multi-DB server root) that otherwise takes ~12s to
// error out. There is never a real answer coming from a directory in this
// state, so failing fast here carries no risk of mistaking a slow-but-valid
// remote list for "absent". (be-1he)
const listCLIRemotesTimeoutBroken = 2 * time.Second
// listCLIRemotesTimeoutHealthy caps `dolt remote -v` wallclock for a
// directory that does have .dolt/repo_state.json — a real Dolt repo. This is
// deliberately generous: callers such as FindCLIRemote fold any
// ListCLIRemotes error (including a timeout) into "remote absent", and
// EnsureCLIRemote then blind-adds on that signal, which hard-fails if the
// remote in fact exists. A real repo's `dolt remote -v` is ~130ms even when
// under load, so 30s only ever bites a genuinely hung subprocess — it must
// not be tightened to a value a slow-but-valid call could plausibly cross
// (review should-fix, 2026-07-24).
const listCLIRemotesTimeoutHealthy = 30 * time.Second
// listCLIRemotesTimeout picks the wallclock cap for dbPath based on whether
// it looks like a real Dolt repo (has .dolt/repo_state.json) or the known
// broken-parent-dir case (doesn't). Pure and stat-only so it's cheap to call
// per-invocation and independently testable without shelling out to dolt.
func listCLIRemotesTimeout(dbPath string) time.Duration {
if _, err := os.Stat(filepath.Join(dbPath, ".dolt", "repo_state.json")); err != nil {
return listCLIRemotesTimeoutBroken
}
return listCLIRemotesTimeoutHealthy
}
// ShellQuote returns s wrapped in single quotes with any embedded single
// quotes escaped, making it safe to interpolate into a shell command string.
func ShellQuote(s string) string {
@ -83,7 +115,9 @@ func PersistedRemotes(dbPath string) ([]storage.RemoteInfo, error) {
// directory. This is a read-only guard for deciding whether CLI push/pull/fetch
// can safely run from that directory; remote mutation still goes through SQL.
func ListCLIRemotes(dbPath string) ([]storage.RemoteInfo, error) {
cmd := exec.Command("dolt", "remote", "-v") // #nosec G204 -- fixed command
ctx, cancel := context.WithTimeout(context.Background(), listCLIRemotesTimeout(dbPath))
defer cancel()
cmd := exec.CommandContext(ctx, "dolt", "remote", "-v") // #nosec G204 -- fixed command
cmd.Dir = dbPath
out, err := cmd.CombinedOutput()
if err != nil {

View file

@ -1,6 +1,10 @@
package doltutil
import "testing"
import (
"os"
"path/filepath"
"testing"
)
func TestShellQuote(t *testing.T) {
tests := []struct {
@ -122,3 +126,41 @@ func TestRemoteURLsMatchNormalizesGitURLs(t *testing.T) {
})
}
}
// listCLIRemotesTimeout must pick the aggressive 2s cap only for a directory
// that lacks .dolt/repo_state.json (the known broken multi-DB server-root
// case) and a generous 30s cap for anything that looks like a real Dolt
// repo, so a slow-but-valid `dolt remote -v` is never SIGKILLed and
// misread as "remote absent" by callers like FindCLIRemote (review
// should-fix, 2026-07-24).
func TestListCLIRemotesTimeout(t *testing.T) {
t.Run("missing .dolt directory entirely uses the broken-root cap", func(t *testing.T) {
if got := listCLIRemotesTimeout(t.TempDir()); got != listCLIRemotesTimeoutBroken {
t.Errorf("listCLIRemotesTimeout = %v, want %v", got, listCLIRemotesTimeoutBroken)
}
})
t.Run("has .dolt but no repo_state.json uses the broken-root cap", func(t *testing.T) {
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, ".dolt"), 0o755); err != nil {
t.Fatal(err)
}
if got := listCLIRemotesTimeout(dir); got != listCLIRemotesTimeoutBroken {
t.Errorf("listCLIRemotesTimeout = %v, want %v", got, listCLIRemotesTimeoutBroken)
}
})
t.Run("has repo_state.json uses the generous healthy-repo cap", func(t *testing.T) {
dir := t.TempDir()
doltDir := filepath.Join(dir, ".dolt")
if err := os.MkdirAll(doltDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(doltDir, "repo_state.json"), []byte(`{}`), 0o644); err != nil {
t.Fatal(err)
}
if got := listCLIRemotesTimeout(dir); got != listCLIRemotesTimeoutHealthy {
t.Errorf("listCLIRemotesTimeout = %v, want %v", got, listCLIRemotesTimeoutHealthy)
}
})
}

View file

@ -0,0 +1,51 @@
# Release gate: be-1he — eliminate 12s `dolt remote -v` slow path
**Verdict: PASS.**
**[maintainer edit 2026-07-23: corrected layer description to match shipped diff — see PR comment. This gate document (and the PR body) previously described a "Layer 1" `repo_state.json` sentinel in `internal/storage/dolt/federation.go`; the actual diff never touched that file. The doc below now reflects only what ships: Layer 2 (`internal/storage/doltutil/remotes.go`) and Layer 3 (`cmd/bd/version_tracking.go`).]**
Branch: `release/be-1he-slow-path-fix`
Base on origin: `origin/main` @ `7e3c7fbbe`
HEAD: `9b2d0b87a`
Source bead: `be-srb` (review of `be-1he`); investigator escalation `gm-cgjm3a`.
## Commit
| # | SHA on `release/be-1he-slow-path-fix` | Source on `quad341/beads:rebase/be-nx7-be-1n9-stack` | Subject |
|---|---------------------------------------|-------------------------------------------------------|---------|
| 1 | `9b2d0b87a` | `777dd60d2` | perf(dolt): be-1he eliminate 12s `dolt remote -v` against multi-db server roots |
Cherry-pick is clean: 2 files, +22/-1 lines (`cmd/bd/version_tracking.go`, `internal/storage/doltutil/remotes.go`). Diff content byte-equivalent to the reviewer-audited source SHA; only commit-graph offsets differ because the original sat on a 6-commit stack.
## Criteria
| # | Criterion | Verdict | Evidence |
|---|-----------|---------|----------|
| 1 | Review PASS present | PASS | Reviewer-1 (`beads/reviewer-1`, gm-vjcmu7) PASS verdict on `be-srb` at 2026-05-04T20:38Z. Layer-by-layer audit, OWASP walk, build/vet/lint clean, no request-changes findings. Gemini second-pass disabled per current policy. |
| 2 | Acceptance criteria met | PASS (2 of 3 layers) | Layer 2 2-second `context.WithTimeout` + named const `listCLIRemotesTimeout` at `internal/storage/doltutil/remotes.go:14-17,47-49`; Layer 3 read-only `bd_version` probe before writeable open at `cmd/bd/version_tracking.go:195-205`. **Layer 1 (`repo_state.json` sentinel in `internal/storage/dolt/federation.go`) is not part of this diff** — `federation.go` has no hunk on this branch; the earlier "3 layers" framing was stale from an upstream draft of the stacked branch. Layer 2 and Layer 3 stand on their own merits regardless: `ListCLIRemotes` still shells out to `dolt remote -v` with no timeout on `origin/main` (called from `cmd/bd/doctor/federation.go` and CLI push/pull/fetch remote routing), so Layer 2 still closes a real ~12s-hang risk; Layer 3 still avoids an unnecessary writeable store open when no migration is needed. Out-of-scope items (multi-binary `.local_version` hygiene, upstream dolt fix, `syncCLIRemotesToSQL` semantics, gascity-side fanout) untouched. |
| 3 | Tests pass | PASS | `go test -tags gms_pure_go -count 1 -short` on the cherry-picked branch vs the same command on `origin/main`: failure sets are identical. **No new regressions introduced.** Detail: `internal/storage/doltutil/...` clean (5.0s). `internal/storage/dolt/migrations/...` clean (0.06s). `internal/storage/dolt/...` 5 pre-existing `TestApplyConfigDefaults_*` failures (env leak: $GC_DOLT_PORT=28231 propagates through bash → Go test sees BEADS_DOLT_PORT default), reproduced byte-equal on `origin/main`. `cmd/bd/...` 17 pre-existing failures, reproduced byte-equal on `origin/main`. `cmd/bd/doctor/fix/...` 1 pre-existing `TestFixMissingMetadata_DoltRepair` failure, reproduced on `origin/main`. Version-tracking-targeted run (`-run 'TestAutoMigrate\|TestVersion\|TestCheckVersion'`) PASS in 0.107s. |
| 4 | No high-severity review findings open | PASS | Zero blocking findings. One `info` finding only: bead description doc-drift on line numbers (text says remotes.go:46-48 / const 14-19; actual is 47-49 / 14-17). Annotation only — no behavior change requested. |
| 5 | Final branch is clean | PASS | `git status` clean (untracked `.gc/`, `.gitkeep` are rig artifacts outside the tree). |
| 6 | Branch diverges cleanly from main | PASS | `git log origin/main..HEAD` is exactly 1 commit ahead. Cherry-pick of `777dd60d2` onto `7e3c7fbbe` was clean (no conflicts, no excludes needed). |
## Test environment
- Host: Linux 6.19.14-300.fc44.x86_64, Go from `make build` toolchain.
- `BEADS_DOLT_AUTO_START=0`; `GC_DOLT_PORT=28231` in env (rig's gc dolt server) — drives the 5 `TestApplyConfigDefaults_*` failures on both this branch and `origin/main`.
- TMPDIR/GOTMPDIR pinned to `~/.gotmp` (per /tmp tmpfs 12.5G per-user quota).
- `go vet -tags gms_pure_go ./internal/storage/dolt/... ./internal/storage/doltutil/... ./cmd/bd/...`: clean.
- `go build -tags gms_pure_go`: clean (`make build` succeeded).
## Push target
`PUSH_REMOTE=fork`. `git push --dry-run origin HEAD` returns `403 Permission to gastownhall/beads.git denied to quad341`. Cross-repo PR head: `quad341:release/be-1he-slow-path-fix`.
## Reviewer-deferred follow-ups (already filed)
- `be-bwd` — needs-tests bead routed to `beads/validator`, written and closed: unit tests for the new decision branches (Layer 2 timeout, Layer 3 RO probe — the branch's own history also includes tests for the since-dropped Layer 1, which do not apply here). Branch `tests/be-bwd-3layer-fix` @ `747caf121` carries the test commit; intentionally not folded into this PR per the deployer's "single bead, single commit" discipline. The validator's branch can ship as a separate follow-up PR once this lands, trimmed to match this PR's actual (2-layer) scope.
## Out of scope
- Stale `/home/jaword/go/bin/bd` v1.0.0 vs `~/.local/bin/bd` v1.0.3 multi-binary install — environment hygiene, not a code fix. The 3-layer fix makes the system resilient to stale `.local_version` regardless.
- Upstream dolt CLI bug (12s failure path on non-repo dirs) — lives in the dolt repo. Layer 2's timeout backstops it from this side; no routing-around fix (the earlier-described Layer 1 sentinel) is part of this PR.
- gascity-side `gc mail inbox` 8x bd fanout deduplication — separate handoff to `gascity/builder`, same family of bug as gascity PR #1546.

View file

@ -0,0 +1,102 @@
# be-1he: 12-second slow path on bd commands (multi-DB server root repro)
## Environment
- `bd` binary from any version before be-1he fix
- `dolt` CLI in PATH (required by `ListCLIRemotes`)
- Multi-DB Dolt server root: `.beads/dolt/` has `.dolt/sql-server.info` but
no `.dolt/repo_state.json`
## Running the repro
```bash
./scripts/repro-be-1he-slow-path/repro.sh
```
## What triggers the slow path
**Historical framing, as originally diagnosed:** when `.local_version` contained
a stale bd version string, `autoMigrateOnVersionBump` fired in `PersistentPreRun`
on every `bd` command until the file was updated, opening the store writeable
and (at the time) calling `syncCLIRemotesToSQL → ListCLIRemotes → dolt remote -v`
against the Dolt server root. On a multi-database server installation, the server
root has `.dolt/sql-server.info` (written by `dolt sql-server`) but no
`.dolt/repo_state.json`. The `dolt remote -v` subprocess takes ~12 seconds to
fail with:
```
fatal: The current directory's repository state is invalid.
open .dolt/repo_state.json: no such file or directory
```
`syncCLIRemotesToSQL` and `migrateServerRootRemotes` no longer exist on
current `main` — that writeable-open path was separately reworked around
`doltutil.PersistedRemotes`, a fast on-disk `repo_state.json` read with no
`dolt` subprocess. `ListCLIRemotes` itself is still present and still shells
out to `dolt remote -v`, though: it's called from `cmd/bd/doctor/federation.go`
(federation health checks) and from CLI push/pull/fetch remote routing in
`internal/storage/doltutil/remotes.go`. Those call sites are what be-1he's
Layer 2 timeout protects.
## Reproducing the 12-second hang manually
```bash
# 1. Create the broken server root structure
TMPDIR=$(mktemp -d)
mkdir -p "$TMPDIR/.dolt"
echo '[{"host":"127.0.0.1","port":3307}]' > "$TMPDIR/.dolt/sql-server.info"
# Note: no repo_state.json
# 2. Observe the raw, uncapped subprocess — the underlying dolt slowness
# this repro targets. This calls dolt directly, not through bd's
# ListCLIRemotes, so nothing here caps it.
cd "$TMPDIR"
time dolt remote -v # takes ~12 s to fail
cd -
# 3. Observe how bd scopes its Layer 2 cap: absence of repo_state.json is
# what selects the aggressive 2s timeout inside ListCLIRemotes (present
# → 30s, generous, so a slow-but-valid answer is never mistaken for
# "remote absent").
ls "$TMPDIR/.dolt/repo_state.json" 2>/dev/null || echo "absent → bd's ListCLIRemotes uses the 2s cap here"
```
## bd command sequence (to verify with a real workspace)
On a workspace running a multi-database Dolt server:
```bash
# Simulate stale .local_version (any different version string triggers it)
OLD_VERSION=$(cat .beads/.local_version)
echo "0.0.0" > .beads/.local_version
# Time a bd command (any command that goes through PersistentPreRun)
time bd version
# Restore
echo "$OLD_VERSION" > .beads/.local_version
```
Historically (pre-fix, and before the unrelated `PersistedRemotes` rework on
`main`): `bd version` could take ~12 s when the migration fired and called
`dolt remote -v` on the server root.
With be-1he's two shipped layers: `ListCLIRemotes` (still reachable from
doctor federation checks and CLI remote routing, not from the ordinary
auto-migrate path on current `main`) caps that same call at 2 s against a
directory like this one; `bd version` itself returns in < 1 s regardless,
since Layer 3's read-only probe avoids the writeable open when no migration
is needed.
## Two layers of the fix
| Layer | File | What it does |
|-------|------|-------------|
| 2 | `internal/storage/doltutil/remotes.go` | `ListCLIRemotes` wraps `dolt remote -v` in `context.WithTimeout`: 2 s when the target directory lacks `repo_state.json` (this repro's case), 30 s otherwise — a slow-but-valid answer from a real repo is never mistaken for "remote absent" |
| 3 | `cmd/bd/version_tracking.go` | `autoMigrateOnVersionBump` does a read-only `bd_version` probe before opening the store writeable, skipping an unnecessary `initSchema` round-trip when no migration is needed |
An earlier draft of this fix also described a "Layer 1" sentinel in
`internal/storage/dolt/federation.go` (`migrateServerRootRemotes` stat-checking
`repo_state.json` before calling `ListCLIRemotes`). That never shipped in
be-1he — `federation.go` has no hunk in this PR's diff — and is not part of
what this repro demonstrates.

View file

@ -0,0 +1,119 @@
#!/usr/bin/env bash
# Repro for be-1he: `dolt remote -v` against a multi-DB server root
# takes ~12 s when repo_state.json is absent.
#
# Usage:
# ./scripts/repro-be-1he-slow-path/repro.sh [--no-cleanup]
#
# Requirements:
# - dolt in PATH
# - time command (bash built-in)
#
# What this demonstrates:
# A multi-DB server root has .dolt/sql-server.info but no repo_state.json;
# `dolt remote -v` in such a dir takes ~12 s before failing with
# "not a valid dolt repository." That's raw upstream dolt CLI behavior,
# reproduced here directly, unmediated by bd.
#
# What be-1he ships against that slowness:
# - Layer 2 (internal/storage/doltutil/remotes.go): ListCLIRemotes caps
# its own `dolt remote -v` subprocess at 2 s when the target directory
# lacks repo_state.json (this script's SERVER_ROOT case) and at a
# generous 30 s otherwise, so a slow-but-valid remote list from a real
# repo is never mistaken for "remote absent". This script calls the
# raw `dolt` binary directly, not bd's ListCLIRemotes, so the timings
# below are never capped — they demonstrate the underlying dolt
# slowness the cap protects against.
# - Layer 3 (cmd/bd/version_tracking.go): a read-only bd_version probe
# before autoMigrateOnVersionBump opens the store writeable, saving an
# unnecessary initSchema round-trip when no migration is needed.
# (An earlier draft of this fix also described a "Layer 1" sentinel in
# internal/storage/dolt/federation.go; that never shipped in be-1he and is
# not part of this repro.)
set -euo pipefail
TMPDIR=$(mktemp -d)
cleanup() { rm -rf "$TMPDIR"; }
if [[ "${1:-}" != "--no-cleanup" ]]; then
trap cleanup EXIT
fi
echo "=== be-1he repro: dolt remote -v against multi-DB server root ==="
echo "Temp dir: $TMPDIR"
echo
# ── 1. Create a multi-DB server root (the broken structure) ──────────────────
SERVER_ROOT="$TMPDIR/server_root"
mkdir -p "$SERVER_ROOT/.dolt"
# sql-server.info is what a running dolt sql-server writes.
# A server root has this file but NOT repo_state.json.
cat > "$SERVER_ROOT/.dolt/sql-server.info" <<'EOF'
[{"host":"127.0.0.1","port":3307,"unix_socket":"","database":""}]
EOF
echo "--- Server root structure ---"
find "$SERVER_ROOT" -type f
echo
# ── 2. Baseline: dolt remote -v against the broken server root (slow) ────────
echo "--- Timing 'dolt remote -v' against broken server root (no repo_state.json) ---"
echo " Expected: ~12 s. This runs the raw dolt binary directly inside SERVER_ROOT,"
echo " not through bd's timeout-wrapped ListCLIRemotes, so nothing here caps it."
START=$(date +%s%3N)
(cd "$SERVER_ROOT" && dolt remote -v 2>&1) || true # expected to fail
END=$(date +%s%3N)
ELAPSED=$(( END - START ))
echo " Elapsed: ${ELAPSED} ms"
echo
if [[ $ELAPSED -gt 5000 ]]; then
echo "SLOW PATH CONFIRMED: elapsed ${ELAPSED}ms > 5000ms — matches the ~12s upstream"
echo "dolt CLI failure mode this repro targets. Raw dolt call, uncapped by bd."
elif [[ $ELAPSED -gt 1500 ]]; then
echo "SLOW PATH PARTIALLY OBSERVED: elapsed ${ELAPSED}ms — clearly slower than a"
echo "healthy repo's ~130ms, just short of the full ~12s this time. Still a raw,"
echo "uncapped dolt call: bd's own 2s cap (Layer 2) is not exercised by this script."
else
echo "FAST PATH: elapsed ${ELAPSED}ms — dolt exited quickly (this dolt version may"
echo "have fixed the underlying issue upstream)"
fi
# ── 3. Show how bd's Layer 2 cap is scoped ────────────────────────────────────
echo
echo "--- Layer 2 cap scoping: repo_state.json presence decides bd's timeout ---"
REPO_STATE="$SERVER_ROOT/.dolt/repo_state.json"
if [[ -f "$REPO_STATE" ]]; then
echo " repo_state.json EXISTS → bd's ListCLIRemotes uses the generous 30s cap"
echo " (this looks like a real repo; a slow-but-valid answer must not be"
echo " mistaken for 'remote absent' by callers like FindCLIRemote)."
else
echo " repo_state.json ABSENT → bd's ListCLIRemotes uses the aggressive 2s cap"
echo " (this is the broken multi-DB server-root case be-1he targets; there is"
echo " no real answer coming from here, so failing fast is safe)."
fi
# ── 4. Create a proper dolt repo for contrast ────────────────────────────────
echo
echo "--- Contrast: dolt remote -v against a proper dolt repo (fast) ---"
PROPER_REPO="$TMPDIR/proper_repo"
mkdir -p "$PROPER_REPO"
(cd "$PROPER_REPO" && dolt init >/dev/null 2>&1)
START=$(date +%s%3N)
(cd "$PROPER_REPO" && dolt remote -v 2>&1) || true
END=$(date +%s%3N)
ELAPSED=$(( END - START ))
echo " Elapsed: ${ELAPSED} ms (expected < 500 ms)"
echo
echo "=== Summary ==="
echo "The historical 12 s slow path (pre-be-1he, and before an unrelated main-branch"
echo "rework of remote migration around doltutil.PersistedRemotes) was:"
echo " bd command -> autoMigrateOnVersionBump -> writeable store open ->"
echo " ListCLIRemotes('.beads/dolt/') -> dolt remote -v in server root -> 12 s failure"
echo
echo "What be-1he ships against the remaining exposure:"
echo "Layer 2 fix (remotes.go): 2 s cap on ListCLIRemotes when repo_state.json"
echo " is absent, 30 s cap otherwise"
echo "Layer 3 fix (version_tracking.go): read-only bd_version probe avoids an"
echo " unnecessary writeable store open"