* feat(doltversion): own dolt CLI discovery, hardened probe, and version policy in a leaf package
PR-1 of the dolt CLI install/version contract (design:
mybd reports/2026-07-25-psxg4-dolt-cli-contract-design.md). The dolt
executable managed proxied-server mode spawns was discovered by a bare
exec.LookPath with no override, probed nowhere, and version-checked only
by one advisory archive_level probe; a missing binary erred after
.beads/ creation and an incompatible one died opaquely.
internal/doltversion (stdlib-only) now owns:
- resolution order BEADS_DOLT_BIN env > clone-local sidecar (hook point;
the sidecar itself lands with identity threading in PR-2) > PATH.
Explicit values are absolutized before validation (a bare name would
otherwise validate the cwd file but exec a PATH lookup) and an
explicitly-named broken binary is an error, never a silent
fall-through;
- a hardened probe: timeout + process kill, 64KiB output caps,
unconditional symlink resolution recording given and real paths,
errno-based exec-format diagnostics naming the likely arch mismatch,
and file identity captured for later change detection;
- version parsing generalized from the archive_level probe, tolerating
prerelease/dev suffixes;
- policy: NO hard version floor (adversarial review rejected 2.2.0 as
policy-without-evidence); RecommendedMin 2.0.0 warns only, backed by
the 2026-07-25 cross-version matrix run (cross-read of module-written
storage requires >= 2.0.0). An unparseable version string is likewise
a warning, not a refusal - the only hard failures are a missing,
non-executable, or probe-failing binary.
Wired at the two entry points: the managed proxied UOW factory replaces
its bare LookPath and derives archive-level support from the probe it
already ran (no second dolt-version fork on the per-command hot path;
warning is printed once, suppressed under --quiet/--json), and bd init's
proxied preflight fails before any .beads/ write when the binary is
missing or broken (external proxied config is exempt - bd never spawns
that server). doltserver.Start's own bare LookPath is deliberately
untouched here; identity threading through every spawn site is PR-2.
Agent-Signature: claude-fable-5-medium on behalf of matt wilkie
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AQHiBWVPSXDCcUSG9EGw9d
* fix(doltversion): fail closed on prerelease archive_level; dedupe dolt advisory
Two review findings on this PR (see reports/2026-07-26-reviewer-dispatch-sweep.md
#5092 for the adversarial-review reasoning):
1. archive_level fail-open regression (major, re gastownhall/beads#4986).
newManagedProxiedServerUOWProvider derived archiveLevelSupported straight
from doltID.Version.AtLeast(MinDoltVersionForArchiveLevelConfig), but
AtLeast deliberately ignores Version.Prerelease (by design, for the
separate warn-only RecommendedMin check). That meant a prerelease build
at or above the floor, e.g. "1.52.1-rc1", satisfied the >= 1.52.1 gate
and had auto_gc_behavior.archive_level written into the YAML — even
though the old doltVersionAtLeast (gc_config.go) already failed closed
for any prerelease-suffixed string via strconv.Atoi. If that prerelease
build predates the field, dolt sql-server's yaml.UnmarshalStrict refuses
to start: the exact #4986 regression. Extracted the derivation into
doltArchiveLevelSupported(id) and added a Prerelease == "" gate, plus
TestDoltArchiveLevelSupported (table test covering above/at/below the
floor and prerelease-at/-above-floor) and
TestDoltArchiveLevelSupported_UnparsedVersionFailsClosed.
2. Duplicate dolt-version advisory (major, UX). `bd init --proxied-server`
probed and printed the version-recommendation warning twice: once in its
own preflight (init_proxied_server.go) and again ~90 lines later when
newProxiedServerUOWProvider reaches newManagedProxiedServerUOWProvider,
which resolved+probed again. The existing doltVersionWarnOnce didn't
cover init's own Fprintf. Extracted one shared resolveAndProbeDolt(ctx,
errPrefix) helper (uow_factory.go) that does resolve + probe +
once-gated warn, called from both sites; init_proxied_server.go no
longer needs its own copy of the Resolve/ProbeWithPolicy/error-wrapping
sequence. This also folds the two call sites' warning gates (previously
!in.quiet vs !quietFlag && !jsonOutput) into one JSON-mode-safe gate.
Verified: CGO_ENABLED=1 go build -tags gms_pure_go ./... clean;
CGO_ENABLED=1 go vet -tags gms_pure_go ./cmd/bd/... clean; gofmt -l on all
changed files empty; targeted tests pass (TestDoltArchiveLevelSupported,
TestDoltArchiveLevelSupported_UnparsedVersionFailsClosed,
TestNewProxiedServerUOWProvider_RoutesExternalConfigToExternalProvider,
TestNewExternalProxiedServerUOWProvider_*, TestBuildProxiedServerClientInfo,
and internal/doltversion's own suite).
Agent-Signature: builder-unknown-model-unknown-reasoning on behalf of maphew
* test(doltversion): resolve symlinks in RealPath expectations (macOS fix)
TestProbeNormalOutput and TestProbeSymlink failed on macOS CI because
t.TempDir lives under /var/folders, itself a symlink to
/private/var/folders. Probe derives RealPath via filepath.EvalSymlinks,
so it correctly returned the /private/var form while the tests compared
against the raw TempDir path.
Map the expected path through the same EvalSymlinks resolution (new
resolveSymlinks test helper). Verified against a symlinked TMPDIR on
Linux, which reproduced the failure before this change.
Agent-Signature: claude-fable-5-high on behalf of maphew
* refactor(cmd/bd): narrow resolveAndProbeDolt to drop the unused identity return
Upstream removed the archive-level gate from proxied-server mode
(68afa3d51), which was the only consumer of the probed identity at these
call sites; returning it now trips unparam on the PR-introduced line.
PR-2's revalidation consumer re-widens the signature when it lands.
Agent-Signature: claude-fable-5-high on behalf of maphew
* fix(doltversion): cross-process probe cache, PATHEXT completion, daily warning dedup
Review fixes (all three MERGE-AFTER-FIXES items from the 2026-08-10 review):
1. The per-command `dolt version` fork on the store-open hot path is gone
for the steady state: ProbeWithPolicyCached fronts the fork with a JSON
cache in the user cache dir, keyed by the binary's (real path, size,
mtime) fingerprint - the exact fields Identity documents for caching.
A fingerprint hit costs a stat; any change to the binary re-probes.
Hard probe errors are never cached, and every cache failure direction
fails open to the uncached behavior.
2. Windows: an explicit BEADS_DOLT_BIN spelled without its extension now
completes via exec.LookPath's PATHEXT handling (exact spelled path wins
when both exist), and the behavioral tests run on Windows through .cmd
stubs instead of skipping wholesale - plus a focused windows-latest CI
job (test-windows-liveness pattern) so those branches actually execute
at PR time. skipOnWindows remains only where the subject genuinely
doesn't exist there (exec-bit check, symlink creation).
3. The old/unverifiable-version advisory now repeats at most once per day
(warn stamp in the same cache file) instead of on every bd invocation;
troubleshooting docs state the cadence.
Nits from the same review: Ctrl-C during the probe no longer prints the
misleading install hint (call site branches on context.Canceled); Probe's
stat branch now uses validateExplicitPath's not-found/not-executable
taxonomy split; ParseVersion documents its trailing-integer laxity and the
tightening required before part-2 enforcement.
Agent-Signature: claude-fable-5-high on behalf of maphew
* fix(doltversion): honor init's local --quiet for the advisory; defer sidecar claim in docs
Cross-vendor review follow-ups: bd init defines a local --quiet that
shadows the persistent flag, so resolveAndProbeDolt now takes quiet as a
parameter (init passes in.quiet || quietFlag) instead of reading the
global; and the troubleshooting entry no longer advertises the clone-local
sidecar resolution source, which is a part-2 hook not yet wired
(SidecarValue is always empty in this PR).
Agent-Signature: claude-fable-5-high on behalf of maphew
* test: stop the Windows counting stub double-counting forks
cmd.exe includes the space before a mid-line '>>' in the written text,
so the batch stub's 'set /p =x >> file' appended two bytes ('x ') per
invocation and forkCount — which counts bytes — read every fork as two.
All four Windows failures were exactly doubled expectations; the cache
itself hit and skipped forks correctly. Lead with the redirection so
exactly one byte lands per invocation.
Agent-Signature: claude-fable-5-high on behalf of maphew
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019CwiLhLbAGZdJtKYT76CPp
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
241 lines
8.2 KiB
Go
241 lines
8.2 KiB
Go
package doltversion
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// writeCountingVersionStub writes a stub that prints a version line AND
|
|
// appends one byte to countFile per invocation, so tests can prove whether
|
|
// the binary was actually forked or the result came from the cache.
|
|
//
|
|
// The batch variant leads with the redirection: cmd.exe includes any space
|
|
// before a mid-line `>>` in the text being written (`set /p =x >> f`
|
|
// appends "x ", two bytes), which silently doubled forkCount on Windows.
|
|
func writeCountingVersionStub(t *testing.T, dir, name, line, countFile string) string {
|
|
t.Helper()
|
|
return writeExecStub(t, dir, name,
|
|
"#!/bin/sh\nprintf x >> '"+countFile+"'\necho '"+line+"'\n",
|
|
"@>>\""+countFile+"\" <nul set /p =x\r\n@echo "+line+"\r\n")
|
|
}
|
|
|
|
func forkCount(t *testing.T, countFile string) int {
|
|
t.Helper()
|
|
data, err := os.ReadFile(countFile)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return 0
|
|
}
|
|
t.Fatalf("read count file: %v", err)
|
|
}
|
|
return len(data)
|
|
}
|
|
|
|
func TestCachedProbeHitSkipsFork(t *testing.T) {
|
|
dir := t.TempDir()
|
|
countFile := filepath.Join(dir, "count")
|
|
stub := writeCountingVersionStub(t, dir, "dolt", "dolt version 2.1.0", countFile)
|
|
cachePath := filepath.Join(dir, "cache", "dolt-probe.json")
|
|
now := time.Date(2026, 8, 11, 12, 0, 0, 0, time.UTC)
|
|
|
|
first, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, now)
|
|
if err != nil {
|
|
t.Fatalf("first probe: %v", err)
|
|
}
|
|
if first.FromCache {
|
|
t.Error("first probe: FromCache = true, want false")
|
|
}
|
|
if got := forkCount(t, countFile); got != 1 {
|
|
t.Fatalf("after first probe: fork count = %d, want 1", got)
|
|
}
|
|
|
|
second, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, now)
|
|
if err != nil {
|
|
t.Fatalf("second probe: %v", err)
|
|
}
|
|
if !second.FromCache {
|
|
t.Error("second probe: FromCache = false, want true (fingerprint unchanged)")
|
|
}
|
|
if got := forkCount(t, countFile); got != 1 {
|
|
t.Errorf("after second probe: fork count = %d, want still 1 (cache hit must not fork)", got)
|
|
}
|
|
if second.Identity.Version.String() != "2.1.0" {
|
|
t.Errorf("cached Version = %v, want 2.1.0", second.Identity.Version)
|
|
}
|
|
if second.Warning != nil {
|
|
t.Errorf("cached Warning = %v, want nil for a recent version", second.Warning)
|
|
}
|
|
}
|
|
|
|
func TestCachedProbeMissOnBinaryChange(t *testing.T) {
|
|
dir := t.TempDir()
|
|
countFile := filepath.Join(dir, "count")
|
|
stub := writeCountingVersionStub(t, dir, "dolt", "dolt version 2.1.0", countFile)
|
|
cachePath := filepath.Join(dir, "dolt-probe.json")
|
|
now := time.Date(2026, 8, 11, 12, 0, 0, 0, time.UTC)
|
|
|
|
if _, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, now); err != nil {
|
|
t.Fatalf("first probe: %v", err)
|
|
}
|
|
|
|
// Replace the stub with different content (size changes) — the
|
|
// fingerprint must miss and the new binary must be re-probed.
|
|
stub2 := writeCountingVersionStub(t, dir, "dolt", "dolt version 2.2.0-different", countFile)
|
|
if stub2 != stub {
|
|
t.Fatalf("rewrite landed at %q, want same path %q", stub2, stub)
|
|
}
|
|
|
|
res, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, now)
|
|
if err != nil {
|
|
t.Fatalf("probe after binary change: %v", err)
|
|
}
|
|
if res.FromCache {
|
|
t.Error("probe after binary change: FromCache = true, want false")
|
|
}
|
|
if got := forkCount(t, countFile); got != 2 {
|
|
t.Errorf("fork count = %d, want 2 (changed binary must be re-probed)", got)
|
|
}
|
|
if res.Identity.Version.String() != "2.2.0-different" {
|
|
t.Errorf("Version = %v, want 2.2.0-different (from the NEW binary)", res.Identity.Version)
|
|
}
|
|
}
|
|
|
|
func TestCachedProbeCorruptCacheFailsOpen(t *testing.T) {
|
|
dir := t.TempDir()
|
|
countFile := filepath.Join(dir, "count")
|
|
stub := writeCountingVersionStub(t, dir, "dolt", "dolt version 2.1.0", countFile)
|
|
cachePath := filepath.Join(dir, "dolt-probe.json")
|
|
if err := os.WriteFile(cachePath, []byte("{not json"), 0o600); err != nil {
|
|
t.Fatalf("write corrupt cache: %v", err)
|
|
}
|
|
|
|
res, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, time.Now())
|
|
if err != nil {
|
|
t.Fatalf("probe with corrupt cache: %v", err)
|
|
}
|
|
if res.FromCache {
|
|
t.Error("FromCache = true, want false (corrupt cache must be ignored)")
|
|
}
|
|
if res.Identity.Version.String() != "2.1.0" {
|
|
t.Errorf("Version = %v, want 2.1.0", res.Identity.Version)
|
|
}
|
|
// The successful probe must have repaired the cache file.
|
|
if _, ok := loadCacheEntry(cachePath); !ok {
|
|
t.Error("cache file not rewritten after probe over corrupt cache")
|
|
}
|
|
}
|
|
|
|
func TestCachedProbeHardErrorNotCached(t *testing.T) {
|
|
dir := t.TempDir()
|
|
stub := writeExecStub(t, dir, "dolt-fails", "#!/bin/sh\nexit 1\n", "@exit /b 1\r\n")
|
|
cachePath := filepath.Join(dir, "dolt-probe.json")
|
|
|
|
for i := 0; i < 2; i++ {
|
|
_, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, time.Now())
|
|
if err == nil {
|
|
t.Fatalf("call %d: want error from failing binary, got nil", i+1)
|
|
}
|
|
if !errors.Is(err, ErrProbeFailed) {
|
|
t.Errorf("call %d: error = %v, want wrapping ErrProbeFailed", i+1, err)
|
|
}
|
|
}
|
|
if _, err := os.Stat(cachePath); !os.IsNotExist(err) {
|
|
t.Errorf("cache file exists after hard probe errors; hard errors must not be cached (stat err: %v)", err)
|
|
}
|
|
}
|
|
|
|
func TestCachedProbeUnverifiableIsCachedWithWarning(t *testing.T) {
|
|
dir := t.TempDir()
|
|
countFile := filepath.Join(dir, "count")
|
|
stub := writeCountingVersionStub(t, dir, "dolt", "definitely not a version", countFile)
|
|
cachePath := filepath.Join(dir, "dolt-probe.json")
|
|
now := time.Date(2026, 8, 11, 12, 0, 0, 0, time.UTC)
|
|
|
|
first, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, now)
|
|
if err != nil {
|
|
t.Fatalf("first probe: %v", err)
|
|
}
|
|
if first.Warning == nil {
|
|
t.Fatal("first probe: Warning = nil, want unverifiable warning")
|
|
}
|
|
|
|
second, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, now)
|
|
if err != nil {
|
|
t.Fatalf("second probe: %v", err)
|
|
}
|
|
if !second.FromCache {
|
|
t.Error("second probe: FromCache = false, want true")
|
|
}
|
|
if second.Warning == nil {
|
|
t.Fatal("second probe: Warning = nil, want unverifiable warning replayed from cache")
|
|
}
|
|
if second.Warning.RawOutput != "definitely not a version" {
|
|
t.Errorf("cached Warning.RawOutput = %q", second.Warning.RawOutput)
|
|
}
|
|
if got := forkCount(t, countFile); got != 1 {
|
|
t.Errorf("fork count = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestCachedProbeWarnStampDedupes(t *testing.T) {
|
|
dir := t.TempDir()
|
|
stub := writeVersionEchoStub(t, dir, "dolt-old", "dolt version 1.52.1")
|
|
cachePath := filepath.Join(dir, "dolt-probe.json")
|
|
base := time.Date(2026, 8, 11, 12, 0, 0, 0, time.UTC)
|
|
|
|
first, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, base)
|
|
if err != nil {
|
|
t.Fatalf("first probe: %v", err)
|
|
}
|
|
if first.Warning == nil || !first.WarnDue {
|
|
t.Fatalf("first probe: Warning=%v WarnDue=%v, want warning due (never warned)", first.Warning, first.WarnDue)
|
|
}
|
|
MarkWarned(cachePath, base)
|
|
|
|
soon, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, base.Add(time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("second probe: %v", err)
|
|
}
|
|
if soon.Warning == nil {
|
|
t.Fatal("second probe: Warning = nil, want warning still present")
|
|
}
|
|
if soon.WarnDue {
|
|
t.Error("second probe 1h later: WarnDue = true, want false (stamped within interval)")
|
|
}
|
|
|
|
later, err := ProbeWithPolicyCached(context.Background(), stub, cachePath, base.Add(25*time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("third probe: %v", err)
|
|
}
|
|
if !later.WarnDue {
|
|
t.Error("third probe 25h later: WarnDue = false, want true (interval elapsed)")
|
|
}
|
|
}
|
|
|
|
func TestCachedProbeEmptyCachePathUncached(t *testing.T) {
|
|
dir := t.TempDir()
|
|
countFile := filepath.Join(dir, "count")
|
|
stub := writeCountingVersionStub(t, dir, "dolt-old", "dolt version 1.52.1", countFile)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
res, err := ProbeWithPolicyCached(context.Background(), stub, "", time.Now())
|
|
if err != nil {
|
|
t.Fatalf("call %d: %v", i+1, err)
|
|
}
|
|
if res.FromCache {
|
|
t.Errorf("call %d: FromCache = true, want false with empty cachePath", i+1)
|
|
}
|
|
if res.Warning == nil || !res.WarnDue {
|
|
t.Errorf("call %d: Warning=%v WarnDue=%v, want warning always due without a cache", i+1, res.Warning, res.WarnDue)
|
|
}
|
|
}
|
|
if got := forkCount(t, countFile); got != 2 {
|
|
t.Errorf("fork count = %d, want 2 (no cache, every call forks)", got)
|
|
}
|
|
// MarkWarned with empty path must be a no-op, not a panic.
|
|
MarkWarned("", time.Now())
|
|
}
|