The graph-mode fence decides a workspace's format from graph_mode in its metadata alone. Graph-preview builds also write a .beads/graph-preview-format marker before metadata.json (donnabox fork commit deea8d57df, Donna Box), so that an interrupted or metadata-lost graph workspace stays fenced. bd ignored the marker. When a bd that predates graph_mode had rewritten metadata.json without the field, bd opened the graph database by its recorded name and `bd list` ran Issue schema initialization against it. When metadata.json was never written or was lost, bd fell back to the default database name and created an Issue database inside the graph workspace, and `bd init` initialized an Issue workspace there. checkWorkspaceFormat now also refuses a beadsDir that holds anything at graph-preview-format, whatever its content and whatever the metadata says (absent, stripped, or an explicit "dependency"). The graph_mode check still runs first, so a normal graph workspace is still reported by its graph_mode. The refusal is the same typed error with its own message and a hint to restore metadata.json, so every call site, the --json rendering and exit code 16 (ExitUnsupportedWorkspaceFormat) are unchanged. An empty beadsDir never probes the working directory. loadAndCheckWorkspaceFormat reads the metadata with LoadForDiscovery and refuses metadata that cannot be parsed beside the marker as the marker's format: the marker alone declares it. Command admission (guardLegacyUpgradeWorkspaceForInit) uses it, and checkDiscoveredWorkspaceFormat now delegates to it, so the paths that check before a migrating configfile.Load (the four store factories, create --repo's ensureBeadsDirForPath and dry-run target open, the bd dolt loaders, bootstrap's undiscovered walk) refuse a marked workspace too, with or without readable metadata. Other metadata load failures are still left to the caller's own load, so a malformed legacy config.json without the marker keeps create --repo's auto-initialization. bootstrap's ancestor walk (findParentConfig) refuses an ancestor that holds the marker without metadata it can read, as it refuses one whose metadata declares graph_mode. It used to skip a marker-only ancestor and take a more distant workspace's metadata or the defaults, and to report a marked ancestor with unparseable metadata as a load failure (exit 1). With the check in checkWorkspaceFormat, bootstrap in a .beads holding only the marker refuses it (exit 16) instead of reporting no workspace. validateConfiguredBackend returns early for nil metadata, so its backstop covers the marker only when metadata was loaded; every caller runs admission or checkDiscoveredWorkspaceFormat on the same beadsDir first, and its comment now says so. Tests: guard unit tests for marker-only, metadata lost, stripped (metadata.json and legacy config.json), server, explicit dependency, unparseable metadata, marker over a legacy Dolt root, and arbitrary marker content; the four store factories (cgo, and the CGO_ENABLED=0 copies goreleaser ships) with stripped, missing and, for cgo, unparseable metadata beside the marker; findParentConfig with a marked ancestor below an ordinary workspace; the exit-code mapping; ensureBeadsDirForPath with a malformed config.json with and without the marker; and the subprocess e2e matrices (in-workspace commands, plain `bd init`, and the outside entry points: create --repo, create --dry-run, --db, BEADS_DB, init --proxied-server, bootstrap below it) extended with four marker fixtures that must exit 16 with the marker refusal and leave .beads byte-identical, while store-less commands still run. The e2e subprocess timeout goes from 10s to 20s: refused runs finish in well under a second, but an unrefused one creates or opens a database and can approach 10s under load, so without the fence the rows fail on their assertion rather than on the timeout. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
130 lines
5.3 KiB
Go
130 lines
5.3 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.
|
|
//
|
|
// It also refuses metadata that declares a workspace format this build cannot
|
|
// open (checkWorkspaceFormat). Command admission refuses those first, and the
|
|
// store factories check before their migrating configfile.Load
|
|
// (checkDiscoveredWorkspaceFormat), so here it backs up any other caller that
|
|
// validates metadata it loaded itself. It returns early for a nil cfg, so that
|
|
// backstop covers the graph format marker only when metadata was loaded; a
|
|
// caller that may have none must check beadsDir with
|
|
// checkDiscoveredWorkspaceFormat or command admission first, as every caller
|
|
// does today.
|
|
func validateConfiguredBackend(cfg *configfile.Config, beadsDir string) error {
|
|
if cfg == nil {
|
|
return nil
|
|
}
|
|
if err := checkWorkspaceFormat(cfg, beadsDir); err != nil {
|
|
return err
|
|
}
|
|
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 reads 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. It reads with LoadForDiscovery:
|
|
// the open it frames may have just refused the workspace, and
|
|
// configfile.Load would migrate a legacy config.json into metadata.json.
|
|
func backendNameForErrorFraming(beadsDir string) string {
|
|
cfg, err := configfile.LoadForDiscovery(beadsDir)
|
|
if err != nil || cfg == nil {
|
|
return ""
|
|
}
|
|
return cfg.GetBackend()
|
|
}
|
|
|
|
func loadDoltBackendConfig(beadsDir string) (*configfile.Config, error) {
|
|
// Refuse a workspace format this build cannot open before configfile.Load,
|
|
// which would otherwise migrate a legacy config.json into metadata.json
|
|
// before requireDoltBackend's own format check could refuse it.
|
|
if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil {
|
|
return nil, err
|
|
}
|
|
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
|
|
}
|