* feat(backends): registry Remote + optional OpenWith(ctx, dir, OpenOptions)
Slice S1: backends.Backend gains Remote bool and an optional
OpenWith(ctx, beadsDir, OpenOptions{Credential, HTTPClient, UserAgent}) so a
registered extension backend can honor a per-open credential instead of
dialing with a process-global client — the motivating case is one embedder
process serving many workspaces with distinct credentials. Open keeps
working unmodified for every existing backend: Backend.OpenWithOptions calls
OpenWith when present, otherwise falls back to Open and ignores
HTTPClient/UserAgent, but refuses (ErrCredentialWithoutOpenWith) rather than
silently dropping a non-nil Credential.
Public beads.OpenBestAvailableWith(ctx, beadsDir, opts) carries this through
the SDK surface (mirrored in beads_nocgo.go and aliased in backend/backend.go
and beads.go); OpenBestAvailable is now that call with a zero OpenOptions,
identical to its prior behavior for Dolt, embedded Dolt, and every registered
backend. The plain-Dolt path (no registered backend) applies the same
Credential refusal, since Dolt has no per-open credential seam either.
doltserver.ResolvePhysicalRoots now checks backends.IsRemote(cfg.GetBackend())
before every Dolt-mode branch, including the shared-server check (which reads
only env/config.yaml and never consults cfg) — without this ordering, a global
BEADS_DOLT_SHARED_SERVER=1 would hijack a remote-backend workspace into
reporting a bogus local Dolt root it will never open.
Tests: backends_test new file open_with_test.go covers OpenWithOptions
dispatch/back-compat/Credential refusal and Remote/IsRemote; root package
open_backend_selection_test.go adds a fake OpenWith registrant proving
dispatch and option pass-through through OpenBestAvailableWith, zero-options
back-compat for every path (registered w/ and w/o OpenWith, plain Dolt), and
the Credential-without-OpenWith refusal on both paths; doltserver
physical_root_test.go adds the registered-remote-backend case including the
shared-server-override ordering regression test. Verified under both cgo and
nocgo builds. Live-mutated each new guard (the Credential refusal, the
OpenWith dispatch, the ResolvePhysicalRoots remote-backend case, and the
plain-Dolt Credential refusal) one at a time and confirmed RED before
reverting to GREEN.
No HTTP client code added; that is a later slice.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fixup(backends): address Opus review findings on OpenWith (M1-M3, L1-L2)
- M1: frame cmd/bd store-open errors for remote backends as unreachable
server failures instead of Dolt-shaped "failed to open database"
advice (openStoreErrorMessage, backendNameForErrorFraming), with tests.
- M2: rewrite the OpenBestAvailable/OpenBestAvailableWith back-compat
subtest to assert fixed expected outcomes (error identity/message,
returned store behavior) now that the two functions are identical,
instead of comparing them to each other.
- M3: document and enforce the OpenWith contract: an implementation that
receives a non-nil Credential it cannot type-assert to its expected
type must refuse with the new ErrUnsupportedCredential sentinel,
verified with a fake registrant.
- L1: document that a nil OpenOptions.Credential means the backend's
default/ambient auth, and that multi-tenant embedders must pass a
non-nil Credential (RequireCredential deferred to S2).
- L2: fail closed on HTTPClient/UserAgent passed to a backend without
OpenWith, mirroring the existing Credential refusal, via new
ErrHTTPClientWithoutOpenWith/ErrUserAgentWithoutOpenWith sentinels.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* build(bazel): auto-sync BUILD files
Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/37195230844
(`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy).
Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see
scripts/bazel-autofix-push.sh.
* fix(backends): keep the store-open error chain through the remote framing
Maintainer review fixes for the adopted PR (iteration 1).
openStoreErrorMessage returned a string, so ensureStoreActiveWithContext
rebuilt its error from text (%s) where base wrapped the open error with %w,
and the chain ended at the CLI's framing. Prime tells a store-open timeout
apart from a generic "storage unavailable" only through
errors.Is(err, context.DeadlineExceeded) on what ensureStoreActiveWithContext
returns (cmd/bd/prime.go), so the flattening silently downgraded that
diagnostic for every backend, Dolt included, not just the new Remote ones.
Replace the helper with openStoreError, which returns an error wrapping err
with %w on both arms. The message text is unchanged: "failed to open
database: <err>" for Dolt and any registered backend that is not Remote,
"failed to reach remote backend %q: <err>" for a Remote one. direct_mode.go
wraps the result with %w ahead of its Hint line; main.go keeps printing it
through HandleError("%v", ...), which only formats.
Tests:
- TestOpenStoreErrorFramesRemoteBackends (renamed from
TestOpenStoreErrorMessageFramesRemoteBackends) now also asserts errors.Is
on every arm, and the exact original text for the non-Remote arms.
- TestEnsureStoreActiveKeepsOpenErrorChain drives the real
ensureStoreActiveWithContext against a registered backend whose open
outlives its context, for a local and a Remote backend, and requires
errors.Is(err, context.DeadlineExceeded) plus the framing prefix and the
Hint line.
- TestFormatMemoriesForPrimeTimesOutOpeningRegisteredBackend is the
unstubbed twin of TestFormatMemoriesForPrimeTimesOutOpeningStore and
requires the timeout banner, not the "storage unavailable" one.
Measured at this head: turning direct_mode.go's outer %w into %v reddens
both chain-test rows and the prime test; turning the helper's %w into %v
reddens the framing test as well.
Non-gating cleanup in the same area:
- beads_cgo.go, beads_nocgo.go: OpenBestAvailableWith's doc and dispatch
comments said only a non-nil Credential is refused. The code also refuses
a non-nil HTTPClient and a non-empty UserAgent, each with its own
ErrXWithoutOpenWith sentinel.
- internal/doltserver/physical_root.go: the Mode doc lists "remote-backend",
and the RemoteBackend doc covers a registered Remote backend as well as a
Dolt server on a non-local host.
- open_backend_selection_test.go: the plain-Dolt refusal test is a table
over Credential, HTTPClient and UserAgent (renamed
TestOpenBestAvailableWithRefusesPerOpenOptionsOnPlainDolt). Disabling the
nocgo HTTPClient arm or the cgo UserAgent arm reddens only that row. The
zero-options test's comment had the delegation backwards and pointed at
beads.go; OpenBestAvailable delegates to OpenBestAvailableWith in
beads_cgo.go and beads_nocgo.go.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
109 lines
4.1 KiB
Go
109 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/storage/backends"
|
|
)
|
|
|
|
// validateConfiguredBackend fails closed on metadata that selects a removed or
|
|
// unrecognized backend. beadsDir is the workspace whose metadata cfg came from;
|
|
// the rejection inspects it (read-only) so a workspace that already holds a Dolt
|
|
// database is told to fix the stale "backend" value rather than to export and
|
|
// reinitialize. Pass "" only where there is genuinely no workspace directory.
|
|
func validateConfiguredBackend(cfg *configfile.Config, beadsDir string) error {
|
|
if cfg == nil {
|
|
return nil
|
|
}
|
|
if backends.Registered(cfg.Backend) {
|
|
return nil
|
|
}
|
|
switch cfg.Backend {
|
|
case configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite:
|
|
return configfile.RemovedBackendErrorAt(cfg.Backend, beadsDir, cfg)
|
|
case "", configfile.BackendDolt:
|
|
return nil
|
|
default:
|
|
return configfile.UnknownBackendErrorAt(cfg.Backend, beadsDir, cfg)
|
|
}
|
|
}
|
|
|
|
// registeredBackendWorkspaceIsBeadsDir reports whether metadata selects a
|
|
// backend that has no separately discoverable local database.
|
|
func registeredBackendWorkspaceIsBeadsDir(cfg *configfile.Config) bool {
|
|
if cfg == nil {
|
|
return false
|
|
}
|
|
return backends.WorkspaceIsBeadsDir(cfg.GetBackend())
|
|
}
|
|
|
|
func requireDoltBackend(cfg *configfile.Config, beadsDir string) error {
|
|
if err := validateConfiguredBackend(cfg, beadsDir); err != nil {
|
|
return err
|
|
}
|
|
if cfg != nil && cfg.GetBackend() != configfile.BackendDolt {
|
|
return fmt.Errorf("not using Dolt backend (configured backend %q)", cfg.GetBackend())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// normalizeLoadedConfig substitutes the default config for an absent
|
|
// metadata.json (cfg == nil) so mode inference still runs: a remote host
|
|
// supplied via BEADS_DOLT_SERVER_HOST or config.yaml dolt.host (GH#3545)
|
|
// must select server mode even when no metadata.json exists — otherwise
|
|
// the CLI silently opens the embedded store against a remote-host
|
|
// configuration.
|
|
func normalizeLoadedConfig(cfg *configfile.Config) *configfile.Config {
|
|
if cfg == nil {
|
|
return configfile.DefaultConfig()
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// openStoreError frames a failed store-open for the operator. A registered
|
|
// remote backend (backends.IsRemote) is a pure network client of a bd serve
|
|
// with no local database: "failed to open database" misnames the failure, and
|
|
// the "database"-shaped diagnostics the generic path otherwise invites
|
|
// (checking .dolt/ files, a wrong data directory, ...) are the wrong advice
|
|
// for what is really a dial, auth, or protocol failure against a remote
|
|
// server. Every other backend — Dolt, or a registered backend that isn't
|
|
// Remote — keeps the original, unqualified wording. Both framings wrap err
|
|
// with %w, so errors.Is/errors.As consumers (prime's context.DeadlineExceeded
|
|
// check, for one) see the same chain as before: only a Remote backend's
|
|
// message text changes, which is what keeps this additive rather than a
|
|
// behavior change for any existing workspace.
|
|
func openStoreError(backendName string, err error) error {
|
|
if backends.IsRemote(backendName) {
|
|
return fmt.Errorf("failed to reach remote backend %q: %w", backendName, err)
|
|
}
|
|
return fmt.Errorf("failed to open database: %w", err)
|
|
}
|
|
|
|
// backendNameForErrorFraming best-effort loads metadata.json to learn which
|
|
// backend a failed open was for, so a caller that does not already have cfg
|
|
// in scope (for example direct mode's single-shot store-open) can still give
|
|
// openStoreError the name it needs. A failure to even read the config
|
|
// degrades to "" — openStoreError's default, Dolt-shaped framing — rather
|
|
// than compounding one error with another.
|
|
func backendNameForErrorFraming(beadsDir string) string {
|
|
cfg, err := configfile.Load(beadsDir)
|
|
if err != nil || cfg == nil {
|
|
return ""
|
|
}
|
|
return cfg.GetBackend()
|
|
}
|
|
|
|
func loadDoltBackendConfig(beadsDir string) (*configfile.Config, error) {
|
|
cfg, err := configfile.Load(beadsDir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("loading config: %w", err)
|
|
}
|
|
if cfg == nil {
|
|
cfg = configfile.DefaultConfig()
|
|
}
|
|
if err := requireDoltBackend(cfg, beadsDir); err != nil {
|
|
return nil, err
|
|
}
|
|
return cfg, nil
|
|
}
|