* feat(workspacegate): wire the gate through a shared physical-root resolver, OpenGated, and the CLI (part 2) Part 2 of the workspace-gate work started in #5046, which landed the internal/workspacegate primitive and deliberately held back OpenGated because its private physical-root derivation was wrong in exactly the ways review found: it skipped the store's real mode resolution and silently skipped the physical gate on metadata-less workspaces. internal/doltserver.ResolvePhysicalRoots is now the one shared answer to 'which local directory will this workspace's store actually open': side-effect free (never triggers configfile.Load's legacy-migration write, never mkdirs the shared-server tree), central-config aware (the dolt_mode inheritance that previously existed only inside storage/dolt/open.go), correct for embedded (embeddeddolt, where ResolveDoltDir answers dolt/), proxied (env > client-info sidecar > default, hoisted out of package main so it cannot diverge), shared (no-mkdir path), remote backends (workspace gate only), and the supported no-metadata layouts (embeddeddolt-then-dolt stat probe). beads.OpenGated gives library consumers a store that holds SHARED workspace+physical gates for its lifetime. The CLI acquires at one chokepoint after workspace resolution - SHARED for store-opening commands, EXCLUSIVE for backup restore - and in-command for the noDbCommands that replace state: init, migrate from-*-to-* (both source and destination roots, ahead of migrate.lock per the normative lock order), and bootstrap. Posture is deliberate and asymmetric: SHARED acquisition failures that are not contention warn once and continue ungated (the gate is cooperative; normal commands must not brick existing deployments), while contention aborts with the holder's identity, and EXCLUSIVE failures of any kind refuse - maintenance must not pretend to a fence it does not hold. Known residual: discovery-path config loads before the chokepoint can still fire the legacy-migration write; deferring that is follow-up scope alongside doctor, secondary-target opens, and bd dolt set. 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(workspacegate): nil store on failed factory open before gate-release cleanup newDoltStore's concrete factory branches (e.g. server-mode dolt.New) return a typed-nil pointer on error; assigning it to the storage.DoltStorage interface makes 'store != nil' true in closeStoreBeforeGateRelease, which then calls Close on a nil receiver and panics (SIGSEGV observed in TestPersistentPreRunHonorsSkipStoreAnnotation, verify run 20260727T000727Z). No store was opened on that path, so drop the value before the error return. 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_01P3JtQ3ukoVmfg6LCKcKBBh * fix(workspacegate): address review — unparam, gate-busy lock classification, bootstrap dolt-dir gating Three fixes from the adversarial review of gastownhall/beads#5093 (see reports/2026-07-26-reviewer-dispatch-sweep.md, "#5093" section): - unparam (CI lint fail): drop the dead PhysicalRoots return value from buildWorkspaceGateSet; both call sites already discarded it, and the code comments already document that provenance is deliberately not surfaced in user-facing errors. Updated both callers. - TestEmbeddedInitConcurrent regression (CI test fail): the new EXCLUSIVE workspace gate in cmd/bd/init.go short-circuits before the embedded Dolt flock is ever attempted, so a losing concurrent `bd init` now reports gate-busy output that isEmbeddedLockOutput did not recognize. Taught isEmbeddedLockOutput to also match workspacegate.ErrBusy's "workspace gate busy" text, and added a deterministic TestInitGateBusyClassifiedAsLockContention that holds the gate in-process and exercises the exact classification path, independent of subprocess race timing. - Bootstrap dolt-dir gating gap: executeBootstrapPlan gated plan.BeadsDir's resolved physical roots but not doltserver.ResolveDoltDir(plan.BeadsDir), the directory executeInitAction/executeRestoreAction/executeJSONLImportAction actually open. Pass it as an extraRoot, mirroring how bd init passes its own resolved db path. Also fixes a locally-discovered flake in TestChokepointSharedExcludesMigrateExclusive: acquireMigrateGates now threads the package-global rootCtx into acquireExclusiveWorkspaceGates, so a prior in-process test that ran the full command path (which sets rootCtx via setupGracefulShutdown and cancels it via rootCancel() without resetting the var) can leak an already-canceled context into this test. Pin rootCtx to nil for the test's duration, matching the isolation pattern already used elsewhere in this file. Reproduced deterministically by injecting a canceled context before the fix, confirmed neutralized after. TestEmbeddedInitConcurrent itself still does not reproduce the CI gate-busy symptom on this dev machine (10/10 processes succeed within the 5s exclusive-gate wait, so the test's own "at least 1 lock error" assertion still fails locally) — that is a machine-speed-dependent property of the test's contention window, not the classification bug; the classification fix is independently verified by the new deterministic test and by code inspection of workspacegate.ErrBusy's text. Agent-Signature: claude-fable-5-high on behalf of matt wilkie --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
176 lines
6.2 KiB
Go
176 lines
6.2 KiB
Go
package beads_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads"
|
|
"github.com/steveyegge/beads/internal/workspacegate"
|
|
)
|
|
|
|
// OpenGated must fail fast (never reaching the storage open) while a
|
|
// maintenance operation holds the workspace gate exclusively, and must
|
|
// not leak a shared gate handle when the underlying storage open fails.
|
|
// Both paths are environment-independent: neither needs a working store.
|
|
func TestOpenGatedGateSemantics(t *testing.T) {
|
|
dir := t.TempDir()
|
|
beadsDir := filepath.Join(dir, ".beads")
|
|
if err := os.Mkdir(beadsDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
gate, err := workspacegate.ForWorkspace(beadsDir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Exclusive holder (simulating a mode migration) blocks OpenGated,
|
|
// matchable through the exported alias.
|
|
h, err := gate.Acquire(context.Background(), workspacegate.Exclusive,
|
|
workspacegate.Options{Reason: "test migration"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := beads.OpenGated(context.Background(), beadsDir, 0); !errors.Is(err, beads.ErrGateBusy) {
|
|
t.Fatalf("OpenGated under exclusive gate: err = %v, want ErrGateBusy", err)
|
|
}
|
|
if err := h.Release(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Error path: a storage-open failure must release the shared gates
|
|
// taken on the way in. A beadsDir that is a regular file cannot open
|
|
// in any mode or environment.
|
|
badDir := filepath.Join(dir, "bad")
|
|
if err := os.Mkdir(badDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
badBeads := filepath.Join(badDir, ".beads")
|
|
if err := os.WriteFile(badBeads, []byte("not a dir"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := beads.OpenGated(context.Background(), badBeads, 0); err == nil {
|
|
t.Fatal("OpenGated on a file-as-.beads unexpectedly succeeded")
|
|
}
|
|
badGate, err := workspacegate.ForWorkspace(badBeads)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h3, err := badGate.Acquire(context.Background(), workspacegate.Exclusive, workspacegate.Options{})
|
|
if err != nil {
|
|
t.Fatalf("gate still held after failed OpenGated: %v", err)
|
|
}
|
|
_ = h3.Release()
|
|
}
|
|
|
|
// OpenGated must gate the LIBRARY open path's root, not only the
|
|
// CLI-parity resolver's: an embedded-metadata workspace is opened by the
|
|
// library path (server-only, DatabasePath-derived) at .beads/dolt, so an
|
|
// exclusive holder on that root must block OpenGated even though the
|
|
// resolver alone would only gate .beads/embeddeddolt. Environment
|
|
// independent: gate acquisition happens before the storage open.
|
|
func TestOpenGatedGatesLibraryRoot(t *testing.T) {
|
|
dir := t.TempDir()
|
|
beadsDir := filepath.Join(dir, ".beads")
|
|
if err := os.Mkdir(beadsDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"),
|
|
[]byte(`{"backend":"dolt","database":"beads.db","dolt_mode":"embedded"}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Neutralize any host central config: it must not be able to move the
|
|
// library root out from under this assertion.
|
|
t.Setenv("BEADS_CENTRAL_CONFIG", filepath.Join(dir, "no-central.json"))
|
|
t.Setenv("BEADS_DOLT_DATA_DIR", "")
|
|
|
|
libGate, err := workspacegate.ForPhysicalRoot(filepath.Join(beadsDir, "dolt"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h, err := libGate.Acquire(context.Background(), workspacegate.Exclusive,
|
|
workspacegate.Options{Reason: "test holder on library-path root"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = h.Release() }()
|
|
|
|
if _, err := beads.OpenGated(context.Background(), beadsDir, 0); !errors.Is(err, beads.ErrGateBusy) {
|
|
t.Fatalf("OpenGated with EX holder on the library-path root: err = %v, want ErrGateBusy", err)
|
|
}
|
|
}
|
|
|
|
// With a working store, OpenGated holds SHARED gates for the storage
|
|
// lifetime: other shared holders (a second cooperating consumer) coexist,
|
|
// an exclusive acquirer is excluded until Close, and the decorator must
|
|
// not amputate extended capabilities (the contract on AsIssueClaimer).
|
|
func TestOpenGatedLifecycle(t *testing.T) {
|
|
skipIfNoDoltServer(t)
|
|
|
|
tmpDir := t.TempDir()
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.Mkdir(beadsDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
metadata := fmt.Sprintf(`{"backend":"dolt","database":"dolt","dolt_mode":"server","dolt_server_host":"127.0.0.1","dolt_server_port":%d}`, testServerPort)
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gate, err := workspacegate.ForWorkspace(beadsDir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
st, err := beads.OpenGated(context.Background(), beadsDir, 0)
|
|
if err != nil {
|
|
t.Fatalf("OpenGated: %v", err)
|
|
}
|
|
|
|
// Decorator contract: an embedder switching from OpenFromConfig to
|
|
// OpenGated keeps every As*/interface assertion working.
|
|
if _, ok := beads.AsIssueClaimer(st); !ok {
|
|
t.Error("OpenGated store lost IssueClaimer")
|
|
}
|
|
if _, ok := beads.AsEventQuerier(st); !ok {
|
|
t.Error("OpenGated store lost EventQuerier")
|
|
}
|
|
if _, ok := beads.AsBlockedQuerier(st); !ok {
|
|
t.Error("OpenGated store lost BlockedQuerier")
|
|
}
|
|
if _, ok := st.(beads.RemoteStore); !ok {
|
|
t.Error("OpenGated store lost RemoteStore")
|
|
}
|
|
if _, ok := st.(beads.SyncStore); !ok {
|
|
t.Error("OpenGated store lost SyncStore")
|
|
}
|
|
if _, ok := st.(beads.VersionControlReader); !ok {
|
|
t.Error("OpenGated store lost VersionControlReader")
|
|
}
|
|
|
|
// Shared holders coexist: a second cooperating consumer (equivalent to
|
|
// a concurrent OpenGated) acquires the same workspace gate SHARED
|
|
// while the first store is open.
|
|
shared2, err := gate.Acquire(context.Background(), workspacegate.Shared, workspacegate.Options{})
|
|
if err != nil {
|
|
t.Fatalf("second shared holder blocked while OpenGated storage open: %v", err)
|
|
}
|
|
_ = shared2.Release()
|
|
|
|
// The shared gate is held for the storage lifetime: exclusive
|
|
// acquisition fails while open, succeeds after a clean Close.
|
|
if _, err := gate.Acquire(context.Background(), workspacegate.Exclusive, workspacegate.Options{}); !errors.Is(err, beads.ErrGateBusy) {
|
|
t.Fatalf("exclusive while OpenGated storage open: err = %v, want ErrGateBusy", err)
|
|
}
|
|
if err := st.Close(); err != nil {
|
|
t.Fatalf("Close: %v", err)
|
|
}
|
|
h2, err := gate.Acquire(context.Background(), workspacegate.Exclusive, workspacegate.Options{})
|
|
if err != nil {
|
|
t.Fatalf("gate still held after Close: %v", err)
|
|
}
|
|
_ = h2.Release()
|
|
}
|