diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a0ba3976..884a0d7f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -883,6 +883,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 workspace below it that would take its metadata exit with code 16 and a hint to restore `metadata.json`, and routed and cross-repository opens fail without opening its database. +- **The Go library's opens refuse a graph workspace too.** + `beads.OpenBestAvailable`, `OpenBestAvailableWith`, `OpenFromConfig` and + `OpenGated` opened a workspace whose `metadata.json` declares a + `graph_mode` other than `dependency`, or whose `.beads` holds the + `graph-preview-format` marker, as an Issue store and ran Issue schema + migrations into the graph database. They now + return `*beads.UnsupportedWorkspaceFormatError` (match it with `errors.As`) + before opening, creating or changing anything, the same refusal the CLI + exits 16 with. - **`bd setup claude --remove` no longer leaves a blank-line hole.** Removing the beads section from `CLAUDE.md` (or, with the other setup recipes, from `AGENTS.md` or `GEMINI.md`) left the blank lines around it behind, three in diff --git a/beads.go b/beads.go index 3b7d03fde..235415c84 100644 --- a/beads.go +++ b/beads.go @@ -99,6 +99,18 @@ func configuredBackendUnavailable(backend, beadsDir string, cfg *configfile.Conf } } +// UnsupportedWorkspaceFormatError is returned by OpenBestAvailable, +// OpenBestAvailableWith, OpenFromConfig and OpenGated when the workspace +// declares a data model this build cannot open: a metadata.json graph_mode +// other than "dependency", such as the "link" workspaces graph-preview builds +// of bd write, or the .beads/graph-preview-format marker those builds write +// beside it (FormatMarker), whatever the metadata says. Nothing was opened, +// created or modified. Match it with errors.As to read BeadsDir, GraphMode and +// FormatMarker; the bd CLI refuses the same workspaces with exit code 16. +// Aliased here because internal/configfile is unimportable outside this +// module. +type UnsupportedWorkspaceFormatError = configfile.UnsupportedWorkspaceFormatError + // Transaction provides atomic multi-operation support within a database transaction. // Use Storage.RunInTransaction() to obtain a Transaction instance. type Transaction = beads.Transaction @@ -369,6 +381,9 @@ func Open(ctx context.Context, dbPath string) (Storage, error) { // name configuration. // beadsDir is the path to the .beads directory. func OpenFromConfig(ctx context.Context, beadsDir string) (Storage, error) { + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } return dolt.NewFromConfigWithOptions(ctx, beadsDir, &dolt.Config{CreateIfMissing: true}) } @@ -403,6 +418,11 @@ func OpenFromConfig(ctx context.Context, beadsDir string) (Storage, error) { // operation holds one exclusively; zero means fail fast. A busy gate is // reported as ErrGateBusy (match with errors.Is). func OpenGated(ctx context.Context, beadsDir string, wait time.Duration) (Storage, error) { + // Before the gates: taking them creates gate files in and beside a + // workspace this build must leave untouched. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } wsGate, err := workspacegate.ForWorkspace(beadsDir) if err != nil { return nil, err diff --git a/beads_cgo.go b/beads_cgo.go index d36297ce3..31b91c683 100644 --- a/beads_cgo.go +++ b/beads_cgo.go @@ -46,6 +46,11 @@ func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, error) { // typed refusal (the matching ErrXWithoutOpenWith sentinel), never a silent // open with the field ignored. func OpenBestAvailableWith(ctx context.Context, beadsDir string, opts OpenOptions) (Storage, error) { + // Before Load, which can migrate a legacy config.json, and before any + // backend is chosen, as the CLI store factories do. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } cfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("loading storage metadata: %w", err) diff --git a/beads_nocgo.go b/beads_nocgo.go index ba6853c4a..878a87d45 100644 --- a/beads_nocgo.go +++ b/beads_nocgo.go @@ -32,6 +32,11 @@ func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, error) { // rationale; behavior here is identical except that embedded Dolt is // unavailable without CGO. func OpenBestAvailableWith(ctx context.Context, beadsDir string, opts OpenOptions) (Storage, error) { + // Before Load, which can migrate a legacy config.json, and before any + // backend is chosen, as the CLI store factories do. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } cfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("loading storage metadata: %w", err) diff --git a/cmd/bd/bootstrap.go b/cmd/bd/bootstrap.go index 8d07611cf..c910aa300 100644 --- a/cmd/bd/bootstrap.go +++ b/cmd/bd/bootstrap.go @@ -1615,7 +1615,7 @@ func findParentConfig(beadsDir string) (*configfile.Config, error) { candidate := filepath.Join(dir, ".beads") cfg, err := configfile.LoadForDiscovery(candidate) if (err != nil || cfg == nil) && hasGraphFormatMarker(candidate) { - return nil, &unsupportedWorkspaceFormatError{beadsDir: candidate, formatMarker: true} + return nil, &configfile.UnsupportedWorkspaceFormatError{BeadsDir: candidate, FormatMarker: true} } if err != nil { return nil, fmt.Errorf("%s: %w", configfile.ConfigPath(candidate), err) diff --git a/cmd/bd/workspace_format_guard.go b/cmd/bd/workspace_format_guard.go index 1ae6c3995..2b7d35124 100644 --- a/cmd/bd/workspace_format_guard.go +++ b/cmd/bd/workspace_format_guard.go @@ -2,9 +2,6 @@ package main import ( "errors" - "fmt" - "os" - "path/filepath" "github.com/steveyegge/beads/internal/configfile" ) @@ -12,7 +9,7 @@ import ( // ExitUnsupportedWorkspaceFormat is the exit code for a command refused // because its workspace declares a format this bd build cannot open: a // metadata.json graph_mode other than "dependency", or a graph workspace -// format marker (graphFormatMarkerFile). Stable so a wrapper can +// format marker (configfile.GraphFormatMarkerFile). Stable so a wrapper can // tell "this workspace needs a different bd build" apart from a generic // failure (1) without grepping stderr. // @@ -21,86 +18,28 @@ import ( // §E3, which freezes 10/11/12/130. const ExitUnsupportedWorkspaceFormat = 16 -// graphFormatMarkerFile is the format sentinel graph-preview builds write in -// .beads before metadata.json, so that a graph workspace whose metadata was -// never written, was lost, or was rewritten without graph_mode by a bd that -// does not know the field still declares its format. -const graphFormatMarkerFile = "graph-preview-format" - -// unsupportedWorkspaceFormatError refuses a workspace whose metadata or format -// marker declares a data model other than the Issue store this build opens. -// Graph-preview builds persist graph_mode "link" plus graph_* binding fields -// beside an ordinary Dolt backend, and the graphFormatMarkerFile beside them, -// so nothing else tells this build that the database behind it is not an -// Issue store; opening it as one would run Issue migrations into it. -type unsupportedWorkspaceFormatError struct { - beadsDir string - graphMode string - // formatMarker reports that graphFormatMarkerFile, not graph_mode, - // declared the format. - formatMarker bool -} - -func (e *unsupportedWorkspaceFormatError) Error() string { - if e.formatMarker { - return fmt.Sprintf("workspace %s has a %s marker, which this bd build does not support; %s", - e.beadsDir, graphFormatMarkerFile, configfile.BackendNotOpenedGuarantee) - } - return fmt.Sprintf("workspace %s uses format graph_mode %q, which this bd build does not support; %s", - e.beadsDir, e.graphMode, configfile.BackendNotOpenedGuarantee) -} - -func (e *unsupportedWorkspaceFormatError) hint() string { - if e.formatMarker { - return fmt.Sprintf("restore metadata.json (graph_mode and the graph_* fields) from version control or a backup, then use a bd build that supports graph workspaces; do not remove .beads/%s", graphFormatMarkerFile) - } - return fmt.Sprintf("use a bd build that supports graph_mode %q workspaces; leave graph_mode and the graph_* fields in metadata.json unchanged", e.graphMode) -} - // checkWorkspaceFormat refuses metadata whose graph_mode marker names any -// model but the default. An unknown marker is refused too: it may describe a -// format newer than this build, so it must never fall back to the Issue store. -// It also refuses a beadsDir holding the graph format marker file, whatever -// the metadata says and whatever the file holds. cfg may be nil (no metadata); -// beadsDir may be "" (no workspace directory, so no marker to find). +// model but the default, and a beadsDir holding the graph format marker file, +// with a *configfile.UnsupportedWorkspaceFormatError. It is +// configfile.CheckWorkspaceFormat, the check the public beads package and the +// Dolt store's metadata-driven opens apply too, so the CLI and a library +// caller refuse the same workspaces with the same error. func checkWorkspaceFormat(cfg *configfile.Config, beadsDir string) error { - if mode, err := cfg.GetGraphMode(); err != nil || mode != configfile.GraphModeDependency { - return &unsupportedWorkspaceFormatError{beadsDir: beadsDir, graphMode: cfg.GraphMode} - } - if hasGraphFormatMarker(beadsDir) { - return &unsupportedWorkspaceFormatError{beadsDir: beadsDir, formatMarker: true} - } - return nil + return configfile.CheckWorkspaceFormat(cfg, beadsDir) } // loadAndCheckWorkspaceFormat reads beadsDir's discovery metadata without -// migrating it and refuses a workspace in a format this build cannot open -// (checkWorkspaceFormat). cfg is nil when beadsDir has no metadata. Metadata -// that cannot be read beside the graph format marker is refused as that -// format too: the marker alone declares it. +// migrating it and refuses a workspace in a format this build cannot open, +// including unreadable metadata beside the graph format marker. It is +// configfile.LoadAndCheckWorkspaceFormat. func loadAndCheckWorkspaceFormat(beadsDir string) (*configfile.Config, error) { - cfg, err := configfile.LoadForDiscovery(beadsDir) - if err != nil { - if hasGraphFormatMarker(beadsDir) { - return nil, &unsupportedWorkspaceFormatError{beadsDir: beadsDir, formatMarker: true} - } - return nil, fmt.Errorf("loading config: %w", err) - } - if err := checkWorkspaceFormat(cfg, beadsDir); err != nil { - return nil, err - } - return cfg, nil + return configfile.LoadAndCheckWorkspaceFormat(beadsDir) } // hasGraphFormatMarker reports whether anything exists at beadsDir's graph -// format marker path. Callers read beadsDir's metadata first, so a directory -// bd cannot search has already failed there. +// format marker path (configfile.HasGraphFormatMarker). func hasGraphFormatMarker(beadsDir string) bool { - if beadsDir == "" { - return false - } - _, err := os.Lstat(filepath.Join(beadsDir, graphFormatMarkerFile)) - return err == nil + return configfile.HasGraphFormatMarker(beadsDir) } // checkDiscoveredWorkspaceFormat runs checkWorkspaceFormat on beadsDir's @@ -110,15 +49,14 @@ func hasGraphFormatMarker(beadsDir string) bool { // config.json into metadata.json before the format check could refuse it. // Metadata that does not load is left to the caller's own load to report, // unless the graph format marker sits beside it (loadAndCheckWorkspaceFormat). +// It is configfile.CheckDiscoveredWorkspaceFormat, the pre-check the public +// beads package's opens run too. func checkDiscoveredWorkspaceFormat(beadsDir string) error { - if _, err := loadAndCheckWorkspaceFormat(beadsDir); isUnsupportedWorkspaceFormat(err) { - return err - } - return nil + return configfile.CheckDiscoveredWorkspaceFormat(beadsDir) } func isUnsupportedWorkspaceFormat(err error) bool { - var formatErr *unsupportedWorkspaceFormatError + var formatErr *configfile.UnsupportedWorkspaceFormatError return errors.As(err, &formatErr) } @@ -130,10 +68,10 @@ func handleWorkspaceGuardError(err error) error { if err == nil { return nil } - var formatErr *unsupportedWorkspaceFormatError + var formatErr *configfile.UnsupportedWorkspaceFormatError if !errors.As(err, &formatErr) { return HandleError("%v", err) } - _ = HandleErrorWithHint(formatErr.Error(), formatErr.hint()) + _ = HandleErrorWithHint(formatErr.Error(), formatErr.Hint()) return &exitError{Code: ExitUnsupportedWorkspaceFormat} } diff --git a/cmd/bd/workspace_format_guard_test.go b/cmd/bd/workspace_format_guard_test.go index d66f03f29..a19ae0d80 100644 --- a/cmd/bd/workspace_format_guard_test.go +++ b/cmd/bd/workspace_format_guard_test.go @@ -379,7 +379,7 @@ func TestHandleWorkspaceGuardErrorExitCodes(t *testing.T) { if err := handleWorkspaceGuardError(nil); err != nil { t.Fatalf("handleWorkspaceGuardError(nil) = %v", err) } - formatErr := &unsupportedWorkspaceFormatError{beadsDir: "/w/.beads", graphMode: "link"} + formatErr := &configfile.UnsupportedWorkspaceFormatError{BeadsDir: "/w/.beads", GraphMode: "link"} markerDir := t.TempDir() writeFile(t, filepath.Join(markerDir, "graph-preview-format"), []byte("link-preview-v9\n")) markerErr := checkWorkspaceFormat(nil, markerDir) diff --git a/internal/configfile/BUILD.bazel b/internal/configfile/BUILD.bazel index b56381c3c..44d63260a 100644 --- a/internal/configfile/BUILD.bazel +++ b/internal/configfile/BUILD.bazel @@ -12,6 +12,7 @@ go_library( "external_dolt_config.go", "graph_mode.go", "proxied_server_client_info.go", + "workspace_format.go", ], importpath = "github.com/steveyegge/beads/internal/configfile", visibility = ["//:__subpackages__"], @@ -30,6 +31,7 @@ go_test( "credentials_test.go", "external_dolt_config_test.go", "graph_mode_test.go", + "workspace_format_test.go", ], embed = [":configfile"], deps = ["//internal/config"], diff --git a/internal/configfile/workspace_format.go b/internal/configfile/workspace_format.go new file mode 100644 index 000000000..9da5f88b1 --- /dev/null +++ b/internal/configfile/workspace_format.go @@ -0,0 +1,118 @@ +package configfile + +import ( + "errors" + "fmt" + "os" + "path/filepath" +) + +// GraphFormatMarkerFile is the format sentinel graph-preview builds write in +// .beads before metadata.json, so that a graph workspace whose metadata was +// never written, was lost, or was rewritten without graph_mode by a bd that +// does not know the field still declares its format. +const GraphFormatMarkerFile = "graph-preview-format" + +// UnsupportedWorkspaceFormatError refuses a workspace whose metadata or format +// marker declares a data model other than the Issue store this build opens. +// Graph-preview builds persist graph_mode "link" plus graph_* binding fields +// beside an ordinary Dolt backend, and the GraphFormatMarkerFile beside them, +// so nothing else tells this build that the database behind it is not an +// Issue store; opening it as one would run Issue migrations into it. +// +// It is defined here, beside the metadata, so that every open path refuses +// with the same type and text: cmd/bd's admission guard and store factories, +// the public beads package, and the Dolt store's metadata-driven constructors. +type UnsupportedWorkspaceFormatError struct { + // BeadsDir is the workspace whose metadata or marker declared the format. + BeadsDir string + // GraphMode is the graph_mode marker exactly as the metadata wrote it. + GraphMode string + // FormatMarker reports that GraphFormatMarkerFile, not graph_mode, + // declared the format. + FormatMarker bool +} + +func (e *UnsupportedWorkspaceFormatError) Error() string { + if e.FormatMarker { + return fmt.Sprintf("workspace %s has a %s marker, which this bd build does not support; %s", + e.BeadsDir, GraphFormatMarkerFile, BackendNotOpenedGuarantee) + } + return fmt.Sprintf("workspace %s uses format graph_mode %q, which this bd build does not support; %s", + e.BeadsDir, e.GraphMode, BackendNotOpenedGuarantee) +} + +// Hint names the remedy: a build that supports the format, with the +// workspace's metadata and marker left as they are (or, for a marker without +// graph metadata, the metadata restored first). +func (e *UnsupportedWorkspaceFormatError) Hint() string { + if e.FormatMarker { + return fmt.Sprintf("restore metadata.json (graph_mode and the graph_* fields) from version control or a backup, then use a bd build that supports graph workspaces; do not remove .beads/%s", GraphFormatMarkerFile) + } + return fmt.Sprintf("use a bd build that supports graph_mode %q workspaces; leave graph_mode and the graph_* fields in metadata.json unchanged", e.GraphMode) +} + +// CheckWorkspaceFormat refuses metadata whose graph_mode marker names any +// model but the default, with an *UnsupportedWorkspaceFormatError. An unknown +// marker is refused too: it may describe a format newer than this build, so +// it must never fall back to the Issue store. It also refuses a beadsDir +// holding GraphFormatMarkerFile, whatever the metadata says and whatever the +// file holds. cfg may be nil (no metadata); beadsDir may be "" (no workspace +// directory, so no marker to find). +func CheckWorkspaceFormat(cfg *Config, beadsDir string) error { + if mode, err := cfg.GetGraphMode(); err != nil || mode != GraphModeDependency { + return &UnsupportedWorkspaceFormatError{BeadsDir: beadsDir, GraphMode: cfg.GraphMode} + } + if HasGraphFormatMarker(beadsDir) { + return &UnsupportedWorkspaceFormatError{BeadsDir: beadsDir, FormatMarker: true} + } + return nil +} + +// LoadAndCheckWorkspaceFormat reads beadsDir's discovery metadata without +// migrating it (LoadForDiscovery) and refuses a workspace in a format this +// build cannot open (CheckWorkspaceFormat). cfg is nil when beadsDir has no +// metadata. Metadata that cannot be read beside the graph format marker is +// refused as that format too: the marker alone declares it. Any other load +// failure is returned wrapped. +func LoadAndCheckWorkspaceFormat(beadsDir string) (*Config, error) { + cfg, err := LoadForDiscovery(beadsDir) + if err != nil { + if HasGraphFormatMarker(beadsDir) { + return nil, &UnsupportedWorkspaceFormatError{BeadsDir: beadsDir, FormatMarker: true} + } + return nil, fmt.Errorf("loading config: %w", err) + } + if err := CheckWorkspaceFormat(cfg, beadsDir); err != nil { + return nil, err + } + return cfg, nil +} + +// HasGraphFormatMarker reports whether anything exists at beadsDir's graph +// format marker path. Callers read beadsDir's metadata first, so a directory +// bd cannot search has already failed there. An empty beadsDir never probes +// the working directory. +func HasGraphFormatMarker(beadsDir string) bool { + if beadsDir == "" { + return false + } + _, err := os.Lstat(filepath.Join(beadsDir, GraphFormatMarkerFile)) + return err == nil +} + +// CheckDiscoveredWorkspaceFormat runs CheckWorkspaceFormat on beadsDir's +// metadata as LoadForDiscovery reads it, which never rewrites the workspace. +// An open that has not yet decided this build may touch the workspace runs it +// first: Load would otherwise migrate a legacy config.json into metadata.json +// before the format check could refuse it. Metadata that does not load is +// left to the caller's own load to report, unless the graph format marker +// sits beside it (LoadAndCheckWorkspaceFormat). +func CheckDiscoveredWorkspaceFormat(beadsDir string) error { + _, err := LoadAndCheckWorkspaceFormat(beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if errors.As(err, &formatErr) { + return err + } + return nil +} diff --git a/internal/configfile/workspace_format_test.go b/internal/configfile/workspace_format_test.go new file mode 100644 index 000000000..ddcf6f2ca --- /dev/null +++ b/internal/configfile/workspace_format_test.go @@ -0,0 +1,160 @@ +package configfile + +import ( + "errors" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestCheckWorkspaceFormatRefusesOtherModels(t *testing.T) { + const beadsDir = "/work/repo/.beads" + for _, mode := range []string{GraphModeLink, "future-format", "Dependency"} { + t.Run(mode, func(t *testing.T) { + err := CheckWorkspaceFormat(&Config{Backend: BackendDolt, GraphMode: mode}, beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) { + t.Fatalf("CheckWorkspaceFormat() = %v, want *UnsupportedWorkspaceFormatError", err) + } + if formatErr.BeadsDir != beadsDir || formatErr.GraphMode != mode { + t.Fatalf("refusal = %+v, want BeadsDir %q and GraphMode %q", formatErr, beadsDir, mode) + } + for _, want := range []string{beadsDir, `graph_mode "` + mode + `"`, "this bd build does not support", BackendNotOpenedGuarantee} { + if !strings.Contains(err.Error(), want) { + t.Errorf("refusal %q does not mention %q", err, want) + } + } + if hint := formatErr.Hint(); !strings.Contains(hint, `graph_mode "`+mode+`"`) || !strings.Contains(hint, "unchanged") { + t.Errorf("hint %q does not name the mode or ask to keep the marker", hint) + } + }) + } +} + +func TestCheckWorkspaceFormatAdmitsIssueWorkspaces(t *testing.T) { + for name, cfg := range map[string]*Config{ + "no metadata": nil, + "absent marker": {Backend: BackendDolt}, + "dependency marker": {Backend: BackendDolt, GraphMode: GraphModeDependency}, + } { + t.Run(name, func(t *testing.T) { + if err := CheckWorkspaceFormat(cfg, "/work/repo/.beads"); err != nil { + t.Fatalf("CheckWorkspaceFormat() = %v, want admission", err) + } + }) + } +} + +// TestCheckDiscoveredWorkspaceFormatLeavesWorkspaceUnchanged pins the +// pre-check every open runs before Load: it refuses a graph workspace marked +// in either metadata file without migrating a legacy config.json, and leaves +// a workspace with no metadata to the caller's own load. +func TestCheckDiscoveredWorkspaceFormatLeavesWorkspaceUnchanged(t *testing.T) { + const metadata = `{"backend":"dolt","graph_mode":"link"}` + for _, name := range []string{ConfigFileName, "config.json"} { + t.Run(name, func(t *testing.T) { + beadsDir := t.TempDir() + if err := os.WriteFile(filepath.Join(beadsDir, name), []byte(metadata), 0o600); err != nil { + t.Fatal(err) + } + err := CheckDiscoveredWorkspaceFormat(beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || formatErr.BeadsDir != beadsDir || formatErr.GraphMode != GraphModeLink { + t.Fatalf("CheckDiscoveredWorkspaceFormat() = %v, want the link refusal for %s", err, beadsDir) + } + entries, err := os.ReadDir(beadsDir) + if err != nil || len(entries) != 1 || entries[0].Name() != name { + t.Fatalf("pre-check changed the workspace: %v, %v", entries, err) + } + }) + } + if err := CheckDiscoveredWorkspaceFormat(t.TempDir()); err != nil { + t.Fatalf("CheckDiscoveredWorkspaceFormat(no metadata) = %v, want nil", err) + } +} + +// TestCheckWorkspaceFormatRefusesFormatMarker pins the marker half of the +// check: a beadsDir holding GraphFormatMarkerFile is refused whatever its +// metadata says, with its own text and hint, while graph_mode still wins when +// both declare the format. +func TestCheckWorkspaceFormatRefusesFormatMarker(t *testing.T) { + beadsDir := t.TempDir() + if err := os.WriteFile(filepath.Join(beadsDir, GraphFormatMarkerFile), []byte("anything"), 0o600); err != nil { + t.Fatal(err) + } + for name, cfg := range map[string]*Config{ + "no metadata": nil, + "stripped metadata": {Backend: BackendDolt}, + "dependency marker": {Backend: BackendDolt, GraphMode: GraphModeDependency}, + } { + t.Run(name, func(t *testing.T) { + err := CheckWorkspaceFormat(cfg, beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || !formatErr.FormatMarker || formatErr.BeadsDir != beadsDir || formatErr.GraphMode != "" { + t.Fatalf("CheckWorkspaceFormat() = %#v, want the format marker refusal", err) + } + wantErr := "workspace " + beadsDir + " has a graph-preview-format marker, which this bd build does not support; " + BackendNotOpenedGuarantee + if err.Error() != wantErr { + t.Errorf("refusal = %q, want %q", err, wantErr) + } + wantHint := "restore metadata.json (graph_mode and the graph_* fields) from version control or a backup, then use a bd build that supports graph workspaces; do not remove .beads/graph-preview-format" + if formatErr.Hint() != wantHint { + t.Errorf("hint = %q, want %q", formatErr.Hint(), wantHint) + } + }) + } + err := CheckWorkspaceFormat(&Config{Backend: BackendDolt, GraphMode: GraphModeLink}, beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || formatErr.FormatMarker || formatErr.GraphMode != GraphModeLink { + t.Fatalf("CheckWorkspaceFormat(link beside marker) = %#v, want the graph_mode refusal", err) + } + if err := CheckWorkspaceFormat(nil, ""); err != nil { + t.Fatalf("CheckWorkspaceFormat(nil, \"\") = %v, want admission without probing the working directory", err) + } +} + +// TestCheckDiscoveredWorkspaceFormatRefusesFormatMarker pins that the +// pre-check refuses a marked workspace with no, stripped or unparseable +// metadata, and still leaves unparseable metadata without the marker to the +// caller's own load. +func TestCheckDiscoveredWorkspaceFormatRefusesFormatMarker(t *testing.T) { + for name, files := range map[string]map[string]string{ + "marker only": {GraphFormatMarkerFile: ""}, + "stripped metadata.json": {GraphFormatMarkerFile: "x", ConfigFileName: `{"backend":"dolt"}`}, + "stripped legacy config.json": {GraphFormatMarkerFile: "x", "config.json": `{"backend":"dolt"}`}, + "unparseable metadata.json": {GraphFormatMarkerFile: "x", ConfigFileName: `{"backend":`}, + "unparseable legacy config.json": {GraphFormatMarkerFile: "x", "config.json": `{"backend":`}, + } { + t.Run(name, func(t *testing.T) { + beadsDir := t.TempDir() + for file, content := range files { + if err := os.WriteFile(filepath.Join(beadsDir, file), []byte(content), 0o600); err != nil { + t.Fatal(err) + } + } + err := CheckDiscoveredWorkspaceFormat(beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || !formatErr.FormatMarker || formatErr.BeadsDir != beadsDir { + t.Fatalf("CheckDiscoveredWorkspaceFormat() = %v, want the format marker refusal", err) + } + if _, err := LoadAndCheckWorkspaceFormat(beadsDir); !errors.As(err, &formatErr) || !formatErr.FormatMarker { + t.Fatalf("LoadAndCheckWorkspaceFormat() = %v, want the format marker refusal", err) + } + entries, err := os.ReadDir(beadsDir) + if err != nil || len(entries) != len(files) { + t.Fatalf("pre-check changed the workspace: %v, %v", entries, err) + } + }) + } + unparseable := t.TempDir() + if err := os.WriteFile(filepath.Join(unparseable, ConfigFileName), []byte(`{"backend":`), 0o600); err != nil { + t.Fatal(err) + } + if err := CheckDiscoveredWorkspaceFormat(unparseable); err != nil { + t.Fatalf("CheckDiscoveredWorkspaceFormat(unparseable, no marker) = %v, want nil", err) + } + if _, err := LoadAndCheckWorkspaceFormat(unparseable); err == nil || !strings.Contains(err.Error(), "loading config") { + t.Fatalf("LoadAndCheckWorkspaceFormat(unparseable, no marker) = %v, want the load failure", err) + } +} diff --git a/internal/storage/dolt/open.go b/internal/storage/dolt/open.go index 6de61bbff..9d55757c7 100644 --- a/internal/storage/dolt/open.go +++ b/internal/storage/dolt/open.go @@ -83,7 +83,15 @@ func ApplyResolvedServerPort(beadsDir string, cfg *Config) { // closed instead of opening a new, empty Dolt database. beadsDir is the workspace // the config came from, so the refusal can offer the metadata heal when that // workspace already holds a Dolt database. +// +// It first refuses a workspace whose metadata or graph format marker declares a +// data model this build cannot open (configfile.CheckWorkspaceFormat): such a +// workspace keeps another model's database behind an ordinary Dolt backend, +// and opening it here would run Issue migrations into that database. func requireDoltBackend(fileCfg *configfile.Config, beadsDir string) error { + if err := configfile.CheckWorkspaceFormat(fileCfg, beadsDir); err != nil { + return err + } switch fileCfg.Backend { case configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite: return fmt.Errorf("configured storage backend %q is no longer supported and cannot be opened as Dolt: %s", fileCfg.Backend, configfile.RemovedBackendDetailAt(fileCfg.Backend, beadsDir, fileCfg)) @@ -109,6 +117,11 @@ func NewFromConfig(ctx context.Context, beadsDir string) (*DoltStore, error) { // `bd doctor` that should behave the same way as normal top-level CLI commands // while still honoring externally managed server mode. func NewFromConfigWithCLIOptions(ctx context.Context, beadsDir string, cfg *Config) (*DoltStore, error) { + // Refuse a graph workspace before Load can migrate a legacy config.json + // into metadata.json; requireDoltBackend re-checks the loaded config. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } fileCfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("loading config: %w", err) @@ -139,6 +152,11 @@ func NewFromConfigWithCLIOptions(ctx context.Context, beadsDir string, cfg *Conf // NewFromConfigWithOptions creates a DoltStore with options from metadata.json. // Options in cfg override those from the config file. Pass nil for default options. func NewFromConfigWithOptions(ctx context.Context, beadsDir string, cfg *Config) (*DoltStore, error) { + // Refuse a graph workspace before Load can migrate a legacy config.json + // into metadata.json; requireDoltBackend re-checks the loaded config. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } fileCfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("loading config: %w", err) diff --git a/internal/storage/dolt/open_test.go b/internal/storage/dolt/open_test.go index c3d86077d..10a79f28b 100644 --- a/internal/storage/dolt/open_test.go +++ b/internal/storage/dolt/open_test.go @@ -3,6 +3,7 @@ package dolt import ( "bytes" "context" + "errors" "io" "os" "path/filepath" @@ -208,6 +209,138 @@ func TestConfigConstructorsRejectNonDoltBackends(t *testing.T) { } } +// TestConfigConstructorsRefuseGraphWorkspace pins that every metadata-driven +// constructor refuses a workspace whose metadata declares another data model, +// before it resolves a server or writes anything into the workspace (a port +// file, a Dolt root, or the metadata.json that Load would migrate a legacy +// config.json into). The CLI's own store open does not come through here; the +// public beads package's config opens and cmd/bd's direct NewFromConfig* +// callers do. +func TestConfigConstructorsRefuseGraphWorkspace(t *testing.T) { + // If the refusal were missing, the open must fail fast rather than reach + // the suite's shared test server. + t.Setenv("BEADS_DOLT_SERVER_HOST", "127.0.0.1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "1") + for _, mode := range []string{configfile.GraphModeLink, "future-format"} { + for _, doltMode := range []string{configfile.DoltModeEmbedded, configfile.DoltModeServer} { + // "config.json" is the legacy name Load migrates into metadata.json; + // a refused open must not run that migration either. + for _, fileName := range []string{configfile.ConfigFileName, "config.json"} { + t.Run(mode+"/"+doltMode+"/"+fileName, func(t *testing.T) { + beadsDir := t.TempDir() + metadata := `{"backend":"dolt","dolt_mode":"` + doltMode + `","dolt_database":"beads_graph_0123","graph_mode":"` + mode + `","graph_future_binding":"keep-me"}` + path := filepath.Join(beadsDir, fileName) + if err := os.WriteFile(path, []byte(metadata), 0o600); err != nil { + t.Fatal(err) + } + constructors := map[string]func() (*DoltStore, error){ + "default": func() (*DoltStore, error) { + return NewFromConfig(t.Context(), beadsDir) + }, + "standard": func() (*DoltStore, error) { + return NewFromConfigWithOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true, CreateIfMissing: true}) + }, + "cli": func() (*DoltStore, error) { + return NewFromConfigWithCLIOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true, ReadOnly: true}) + }, + } + for name, construct := range constructors { + t.Run(name, func(t *testing.T) { + store, err := construct() + if err == nil { + _ = store.Close() + t.Fatalf("graph_mode %q workspace opened as Dolt", mode) + } + var formatErr *configfile.UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || formatErr.BeadsDir != beadsDir || formatErr.GraphMode != mode { + t.Fatalf("constructor error = %v, want UnsupportedWorkspaceFormatError for %s", err, beadsDir) + } + entries, readErr := os.ReadDir(beadsDir) + if readErr != nil || len(entries) != 1 { + t.Fatalf("refused open changed the workspace: %v, %v", entries, readErr) + } + if got, readErr := os.ReadFile(path); readErr != nil || string(got) != metadata { + t.Fatalf("refused open rewrote %s: %q, %v", fileName, got, readErr) + } + }) + } + }) + } + } + } +} + +// TestConfigConstructorsRefuseGraphFormatMarker pins that every +// metadata-driven constructor refuses a workspace holding the +// graph-preview-format marker graph-preview builds write, whatever its +// metadata says (none, stripped of graph_mode in either metadata file, an +// explicit dependency, or unparseable), and leaves the workspace unchanged. +func TestConfigConstructorsRefuseGraphFormatMarker(t *testing.T) { + // If the refusal were missing, the open must fail fast rather than reach + // the suite's shared test server. + t.Setenv("BEADS_DOLT_SERVER_HOST", "127.0.0.1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "1") + layouts := []struct { + name string + fileName string // "" writes no metadata file + metadata string + }{ + {name: "marker only"}, + {name: "stripped embedded metadata", fileName: configfile.ConfigFileName, metadata: `{"backend":"dolt","dolt_mode":"embedded","dolt_database":"beads_graph_0123"}`}, + {name: "stripped server metadata", fileName: configfile.ConfigFileName, metadata: `{"backend":"dolt","dolt_mode":"server","dolt_database":"beads_graph_0123"}`}, + {name: "stripped legacy config.json", fileName: "config.json", metadata: `{"backend":"dolt","dolt_mode":"embedded","dolt_database":"beads_graph_0123"}`}, + {name: "dependency metadata", fileName: configfile.ConfigFileName, metadata: `{"backend":"dolt","dolt_mode":"server","graph_mode":"dependency"}`}, + {name: "unparseable metadata", fileName: configfile.ConfigFileName, metadata: `{"backend":"dolt",`}, + } + for _, layout := range layouts { + t.Run(layout.name, func(t *testing.T) { + beadsDir := t.TempDir() + want := map[string]string{configfile.GraphFormatMarkerFile: "link-preview-v9\n"} + if layout.fileName != "" { + want[layout.fileName] = layout.metadata + } + for name, content := range want { + if err := os.WriteFile(filepath.Join(beadsDir, name), []byte(content), 0o600); err != nil { + t.Fatal(err) + } + } + constructors := map[string]func() (*DoltStore, error){ + "default": func() (*DoltStore, error) { + return NewFromConfig(t.Context(), beadsDir) + }, + "standard": func() (*DoltStore, error) { + return NewFromConfigWithOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true, CreateIfMissing: true}) + }, + "cli": func() (*DoltStore, error) { + return NewFromConfigWithCLIOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true, ReadOnly: true}) + }, + } + for name, construct := range constructors { + t.Run(name, func(t *testing.T) { + store, err := construct() + if err == nil { + _ = store.Close() + t.Fatalf("workspace with a %s marker opened as Dolt", configfile.GraphFormatMarkerFile) + } + var formatErr *configfile.UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || formatErr.BeadsDir != beadsDir || !formatErr.FormatMarker { + t.Fatalf("constructor error = %v, want the format marker refusal for %s", err, beadsDir) + } + entries, readErr := os.ReadDir(beadsDir) + if readErr != nil || len(entries) != len(want) { + t.Fatalf("refused open changed the workspace: %v, %v", entries, readErr) + } + for fileName, content := range want { + if got, readErr := os.ReadFile(filepath.Join(beadsDir, fileName)); readErr != nil || string(got) != content { + t.Fatalf("refused open rewrote %s: %q, %v", fileName, got, readErr) + } + } + }) + } + }) + } +} + func TestCLIDirUsesSharedDoltRootInSharedServerMode(t *testing.T) { sharedRoot := t.TempDir() t.Setenv("BEADS_DOLT_SHARED_SERVER", "1") diff --git a/open_backend_selection_test.go b/open_backend_selection_test.go index dc79697d7..36ca6bb7e 100644 --- a/open_backend_selection_test.go +++ b/open_backend_selection_test.go @@ -3,9 +3,11 @@ package beads_test import ( "context" "errors" + "maps" "net/http" "os" "path/filepath" + "slices" "strings" "testing" @@ -475,3 +477,192 @@ func TestOpenBestAvailableWithRefusesPerOpenOptionsOnPlainDolt(t *testing.T) { }) } } + +// graphWorkspaceMetadata is the metadata.json a graph-preview build of bd +// writes for a graph_mode "link" workspace: the marker and its binding beside +// an ordinary embedded Dolt backend. +const graphWorkspaceMetadata = `{ + "database": "", + "backend": "dolt", + "graph_mode": "link", + "graph_scope_url": "https://example.test/scope", + "graph_authority_id": "0123456789abcdef0123456789abcdef", + "graph_workspace": "/work/repo/.beads", + "graph_schema_version": 7, + "graph_ready": true, + "dolt_mode": "embedded", + "dolt_database": "beads_graph_0123456789abcdef0123456789abcdef", + "project_id": "0123456789abcdef0123456789abcdef" +}` + +// graphWorkspaceMetadataStripped is graphWorkspaceMetadata as a bd that +// predates graph_mode rewrites it: the graph fields are gone, but the database +// name still points at the graph database. +const graphWorkspaceMetadataStripped = `{ + "database": "", + "backend": "dolt", + "dolt_mode": "embedded", + "dolt_database": "beads_graph_0123456789abcdef0123456789abcdef", + "project_id": "0123456789abcdef0123456789abcdef" +}` + +// publicOpens are the public entry points that open a workspace from its +// metadata. +var publicOpens = map[string]func(context.Context, string) (beads.Storage, error){ + "OpenBestAvailable": beads.OpenBestAvailable, + "OpenBestAvailableWith": func(ctx context.Context, beadsDir string) (beads.Storage, error) { + return beads.OpenBestAvailableWith(ctx, beadsDir, beads.OpenOptions{}) + }, + "OpenFromConfig": beads.OpenFromConfig, + "OpenGated": func(ctx context.Context, beadsDir string) (beads.Storage, error) { + return beads.OpenGated(ctx, beadsDir, 0) + }, +} + +// workspaceTreeDigest records every path under root with its kind and, for +// files, its content, so a test can prove an open left a workspace untouched. +func workspaceTreeDigest(t *testing.T, root string) map[string]string { + t.Helper() + digest := map[string]string{} + err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + rel, err := filepath.Rel(root, path) + if err != nil { + return err + } + if d.IsDir() { + digest[rel] = "dir" + return nil + } + content, err := os.ReadFile(path) + if err != nil { + return err + } + digest[rel] = "file:" + string(content) + return nil + }) + if err != nil { + t.Fatalf("digest %s: %v", root, err) + } + return digest +} + +// TestPublicOpensRefuseGraphWorkspace is the library half of the graph_mode +// fence the CLI applies at admission: every public open of a workspace whose +// metadata or graph-preview-format marker declares another data model refuses +// it with the typed error before it opens, creates or modifies anything, in +// the workspace or beside it (the workspace gate file OpenGated takes lives +// beside .beads). +func TestPublicOpensRefuseGraphWorkspace(t *testing.T) { + // If the fence were missing, a server-mode open must fail fast rather + // than reach TestMain's shared test server. + t.Setenv("BEADS_DOLT_SERVER_HOST", "127.0.0.1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "1") + layouts := []struct { + name string + filename string // "" writes no metadata file + metadata string + mode string + embedded bool + // marker writes the graph-preview-format marker graph-preview builds + // leave in .beads; the refusal then names the marker, not a mode. + marker bool + }{ + {name: "link embedded", filename: "metadata.json", metadata: graphWorkspaceMetadata, mode: "link", embedded: true}, + {name: "link server", filename: "metadata.json", metadata: `{"backend":"dolt","dolt_mode":"server","dolt_database":"beads_graph_0123","graph_mode":"link"}`, mode: "link"}, + {name: "unknown marker", filename: "metadata.json", metadata: `{"backend":"dolt","graph_mode":"future-format"}`, mode: "future-format"}, + // A refused open must not migrate a legacy config.json either. + {name: "link legacy config.json", filename: "config.json", metadata: graphWorkspaceMetadata, mode: "link", embedded: true}, + // A graph workspace whose metadata was never written, was lost, or + // was rewritten without graph_mode is still declared by the marker. + {name: "marker only", marker: true}, + {name: "marker beside stripped metadata", filename: "metadata.json", metadata: graphWorkspaceMetadataStripped, embedded: true, marker: true}, + {name: "marker beside stripped legacy config.json", filename: "config.json", metadata: graphWorkspaceMetadataStripped, embedded: true, marker: true}, + {name: "marker beside server metadata", filename: "metadata.json", metadata: `{"backend":"dolt","dolt_mode":"server","dolt_database":"beads_graph_0123"}`, marker: true}, + {name: "marker beside dependency metadata", filename: "metadata.json", metadata: `{"backend":"dolt","dolt_mode":"server","graph_mode":"dependency"}`, marker: true}, + {name: "marker beside unparseable metadata", filename: "metadata.json", metadata: `{"backend":"dolt",`, marker: true}, + } + for _, layout := range layouts { + for name, open := range publicOpens { + t.Run(layout.name+"/"+name, func(t *testing.T) { + root := t.TempDir() + beadsDir := filepath.Join(root, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + if layout.filename != "" { + if err := os.WriteFile(filepath.Join(beadsDir, layout.filename), []byte(layout.metadata), 0o600); err != nil { + t.Fatal(err) + } + } + if layout.marker { + if err := os.WriteFile(filepath.Join(beadsDir, "graph-preview-format"), []byte("link-preview-v9\n"), 0o600); err != nil { + t.Fatal(err) + } + } + if layout.embedded { + graphDB := filepath.Join(beadsDir, "embeddeddolt", "beads_graph_0123456789abcdef0123456789abcdef", ".dolt") + if err := os.MkdirAll(graphDB, 0o750); err != nil { + t.Fatal(err) + } + } + before := workspaceTreeDigest(t, root) + + // Check err, not store: a failed OpenFromConfig can return a + // typed-nil store. + store, err := open(t.Context(), beadsDir) + if err == nil { + _ = store.Close() + t.Fatalf("%s opened a graph_mode %q workspace", name, layout.mode) + } + var formatErr *beads.UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) { + t.Fatalf("%s error = %v, want *beads.UnsupportedWorkspaceFormatError", name, err) + } + if formatErr.BeadsDir != beadsDir || formatErr.GraphMode != layout.mode || formatErr.FormatMarker != layout.marker { + t.Fatalf("refusal = %+v, want BeadsDir %q, GraphMode %q and FormatMarker %v", formatErr, beadsDir, layout.mode, layout.marker) + } + if layout.marker && !strings.Contains(err.Error(), "graph-preview-format marker") { + t.Fatalf("refusal does not name the format marker: %v", err) + } + if !strings.Contains(err.Error(), "no storage database was opened or modified") { + t.Fatalf("refusal lacks the data-safety guarantee: %v", err) + } + if after := workspaceTreeDigest(t, root); !maps.Equal(after, before) { + t.Fatalf("%s changed the workspace: before %v, after %v", name, slices.Sorted(maps.Keys(before)), slices.Sorted(maps.Keys(after))) + } + }) + } + } +} + +// TestPublicOpensAdmitDependencyWorkspace pins the other side of the fence: a +// "dependency" marker is the ordinary Issue workspace, so the open proceeds to +// its backend (here, a server nothing listens on) instead of being refused. +func TestPublicOpensAdmitDependencyWorkspace(t *testing.T) { + t.Setenv("BEADS_DOLT_SERVER_HOST", "127.0.0.1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "1") + for name, open := range publicOpens { + t.Run(name, func(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + metadata := `{"backend":"dolt","dolt_mode":"server","graph_mode":"dependency"}` + if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0o600); err != nil { + t.Fatal(err) + } + store, err := open(t.Context(), beadsDir) + if err == nil { + _ = store.Close() + t.Fatalf("%s opened a store on an unreachable server", name) + } + var formatErr *beads.UnsupportedWorkspaceFormatError + if errors.As(err, &formatErr) || !strings.Contains(err.Error(), "unreachable") { + t.Fatalf("%s error = %v, want the backend's unreachable-server error", name, err) + } + }) + } +}