diff --git a/.github/scripts/embedded-cmd-test-shards.txt b/.github/scripts/embedded-cmd-test-shards.txt index 18583376a..21e6f181d 100644 --- a/.github/scripts/embedded-cmd-test-shards.txt +++ b/.github/scripts/embedded-cmd-test-shards.txt @@ -198,6 +198,7 @@ 100 54 TestEmbeddedDiscoveryPinsEnvFileReadonly 100 54 TestEmbeddedFormatJSONAliasMatchesJSON +100 54 TestEmbeddedGraphModeDependencyWorkspaceUnaffected 100 54 TestEmbeddedInitAgentArtifactsGolden 100 54 TestEmbeddedSearchConcurrent 100 54 TestEmbeddedShowAsOfFailureExitCodes diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index 03a769e7f..b319ab300 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -1085,7 +1085,7 @@ jobs: outputs: result: ${{ steps.result.outputs.result }} env: - PURE_CMD_BD_TESTS: '^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir|NocgoFactoriesHonorSharedServer|NocgoNewDoltStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoNewReadOnlyStoreFromConfig_ErrorSuggestsCorrectFlag)' + PURE_CMD_BD_TESTS: '^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir|NocgoFactoriesHonorSharedServer|NocgoNewDoltStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoNewReadOnlyStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoStoreFactoriesRefuseGraphWorkspace)' # The only pre-merge run of these boundaries: where it executes # remotely --config=sole-run fails on a remote cache eviction instead # of re-running the whole invocation (whose retry could turn a failed diff --git a/CHANGELOG.md b/CHANGELOG.md index 1485ad043..a54cb677a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -850,6 +850,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 with no `.local_version` witness. A pre-1.0 witness, or a `.beads/dolt` that holds anything, is still refused, and commands other than `bd init` are decided as before. +- **A graph-mode workspace is refused instead of opened as an Issue store.** + Graph-preview builds mark their workspaces with `"graph_mode": "link"` and + `graph_*` binding fields in `metadata.json`, beside an ordinary Dolt + backend. bd opened such a workspace as an Issue store and ran Issue schema + initialization against the graph database, and every `metadata.json` rewrite + (including the `config.json` to `metadata.json` migration) dropped the + marker and the binding. Now any `graph_mode` other than `dependency`, + including a value bd does not recognize, makes every command that runs in + the workspace or names it with `--db` or `BEADS_DB` (`bd init`, `bd doctor` + and `bd bootstrap` included), and `bd create --repo` aimed at it, refuse it + before touching anything. The refusal names the format and exits with code + 16, so installed beads git hooks refuse commits and pushes there too. + Commands that reach such a workspace only through a route from another + one, such as a routed `bd show`, fail without touching it, but not with + code 16. Store-less commands such as `bd version` and `bd help` still run. + A `metadata.json` rewrite keeps `graph_mode` and the `graph_*` fields + graph-preview builds write, and like those builds it omits a zero + `graph_schema_version` and a false `graph_ready`. - **`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/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 37c4abfd2..6178f6900 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -381,6 +381,7 @@ go_library( "where.go", "wisp.go", "wisp_proxied_server.go", + "workspace_format_guard.go", "workspace_gate.go", "worktree.go", "worktree_cmd.go", @@ -843,6 +844,8 @@ go_test( "graph_apply_thread_spawner_embedded_test.go", "graph_embedded_test.go", "graph_export_test.go", + "graph_mode_admission_e2e_test.go", + "graph_mode_dependency_embedded_test.go", "graph_proxied_integration_test.go", "graph_test.go", "graph_visual_test.go", @@ -1326,6 +1329,7 @@ go_test( "wisp_gc_purge_closed_embedded_test.go", "wisp_proxied_integration_test.go", "wisp_test.go", + "workspace_format_guard_test.go", "workspace_gate_test.go", "workspace_identity_test.go", "worktree_cmd_test.go", diff --git a/cmd/bd/backend_support.go b/cmd/bd/backend_support.go index 2f28c48c6..60d256cd1 100644 --- a/cmd/bd/backend_support.go +++ b/cmd/bd/backend_support.go @@ -12,10 +12,19 @@ import ( // 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. 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 } @@ -80,14 +89,16 @@ func openStoreError(backendName string, err error) error { return fmt.Errorf("failed to open database: %w", err) } -// backendNameForErrorFraming best-effort loads metadata.json to learn which +// 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. +// 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.Load(beadsDir) + cfg, err := configfile.LoadForDiscovery(beadsDir) if err != nil || cfg == nil { return "" } @@ -95,6 +106,12 @@ func backendNameForErrorFraming(beadsDir string) string { } 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) diff --git a/cmd/bd/bootstrap.go b/cmd/bd/bootstrap.go index 1fa9c3cff..b7c28aded 100644 --- a/cmd/bd/bootstrap.go +++ b/cmd/bd/bootstrap.go @@ -207,6 +207,12 @@ Examples: } if beadsDir == "" { + // Discovery passes over a .beads marked only by a legacy + // config.json. One in a format this build cannot open is refused + // as that, not reported as no workspace. + if err := checkUndiscoveredWorkspaceFormat(); err != nil { + return handleWorkspaceGuardError(err) + } if jsonOutput { if err := outputJSON(noWorkspaceBootstrapPayload()); err != nil { return err @@ -219,7 +225,7 @@ Examples: } if err := guardLegacyUpgradeWorkspace(beadsDir); err != nil { - return HandleError("%v", err) + return handleWorkspaceGuardError(err) } // Load config from .beads/metadata.json. When the beadsDir was @@ -234,7 +240,9 @@ Examples: } if cfg == nil { cfg, err = findParentConfig(beadsDir) - if err != nil { + if isUnsupportedWorkspaceFormat(err) { + return handleWorkspaceGuardError(err) + } else if err != nil { return HandleError("failed to load ancestor storage metadata: %v; no storage database was opened or modified; fix or restore metadata.json and retry", err) } } diff --git a/cmd/bd/create.go b/cmd/bd/create.go index 64aeada9c..c54ce7fb8 100644 --- a/cmd/bd/create.go +++ b/cmd/bd/create.go @@ -373,6 +373,11 @@ var createCmd = &cobra.Command{ } if dryRun && parentID == "" { + // The preview opens no target, but it refuses a local one in a + // format this build cannot open, as the create it previews would. + if err := checkRepoTargetFormat(repoPath); err != nil { + return handleWorkspaceGuardError(err) + } return renderDryRun() } @@ -402,6 +407,9 @@ var createCmd = &cobra.Command{ // flag value can be ambiguous. allowCreate := !isAmbiguousRepoTarget(cmd.Flags().Changed("repo"), repoOverride) if err := ensureBeadsDirForPath(rootCtx, targetBeadsDir, store, allowCreate); err != nil { + if isUnsupportedWorkspaceFormat(err) { + return handleWorkspaceGuardError(err) + } return HandleError("failed to initialize target repo: %v", err) } @@ -969,8 +977,12 @@ func init() { } // handleCreateTargetStoreError preserves the existing text path for ordinary -// store failures while rendering capability refusals as typed JSON when asked. +// store failures while rendering capability refusals as typed JSON when asked, +// and a target in a format this build cannot open as the workspace refusal. func handleCreateTargetStoreError(err error, targetPath, fallbackFormat string) error { + if isUnsupportedWorkspaceFormat(err) { + return handleWorkspaceGuardError(err) + } var capErr *ProxyCapabilityError if !errors.As(err, &capErr) { return HandleError(fallbackFormat, err) @@ -1020,6 +1032,12 @@ func openDryRunTargetStore(ctx context.Context, repoPath string) (storage.DoltSt targetPath := routing.ExpandPath(repoPath) beadsDir := filepath.Join(targetPath, ".beads") + // A target in a format this build cannot open is refused as that, even + // one marked only by a legacy config.json, which the check below would + // report as not initialized. + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } metadataPath := filepath.Join(beadsDir, "metadata.json") if _, err := os.Stat(metadataPath); err != nil { if os.IsNotExist(err) { @@ -1035,6 +1053,17 @@ func openDryRunTargetStore(ctx context.Context, repoPath string) (storage.DoltSt return store, nil } +// checkRepoTargetFormat refuses a local create target in a format this build +// cannot open, reading its metadata without opening or migrating it. The +// command's own workspace (".") has passed admission already, and a remote +// target has no local metadata to read, so both pass. +func checkRepoTargetFormat(repoPath string) error { + if repoPath == "." || remotecache.IsRemoteURL(repoPath) { + return nil + } + return checkDiscoveredWorkspaceFormat(filepath.Join(routing.ExpandPath(repoPath), ".beads")) +} + // isAmbiguousRepoTarget reports whether an explicit --repo value is a // bare/relative filesystem path (not absolute, not "~/"-prefixed). Such a // value silently resolves against the current working directory (see @@ -1055,6 +1084,14 @@ func ensureBeadsDirForPath(ctx context.Context, targetPath string, sourceStore s beadsDir := filepath.Join(targetPath, ".beads") metadataPath := filepath.Join(beadsDir, "metadata.json") + // A target in a format this build cannot open is refused before anything + // below: one marked only by a legacy config.json has no metadata.json, so + // it would otherwise be initialized here and lose its marker to the + // metadata.json written beside it. + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return err + } + // Check if beads directory already exists with a Dolt database. // metadata.json is the canonical marker for an initialized beads dir. if _, err := os.Stat(metadataPath); err == nil { diff --git a/cmd/bd/doctor.go b/cmd/bd/doctor.go index 4740e6a32..8bac2674e 100644 --- a/cmd/bd/doctor.go +++ b/cmd/bd/doctor.go @@ -220,7 +220,7 @@ Examples: if err := validateDoctorWorkspaceBackend(absPath); isLegacyUpgradeRefusal(err) { return printLegacyUpgradeDiagnostic(err) } else if err != nil { - return HandleError("%v", err) + return handleWorkspaceGuardError(err) } if doctorFix && isOrchestratorRoot(absPath) { diff --git a/cmd/bd/dolt.go b/cmd/bd/dolt.go index b6764cb75..8ae3876d4 100644 --- a/cmd/bd/dolt.go +++ b/cmd/bd/dolt.go @@ -1124,6 +1124,11 @@ is shown as not running, and the next bd command launches it.`, if beadsDir == "" { return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint()) } + // A workspace in a format this build cannot open is refused before + // configfile.Load, which would first migrate a legacy config.json. + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return handleWorkspaceGuardError(err) + } cfg, cfgErr := configfile.Load(beadsDir) if cfgErr != nil { return HandleError("loading config: %v", cfgErr) @@ -2004,6 +2009,11 @@ func showDoltConfig(testConnection bool) error { return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint()) } + // A workspace in a format this build cannot open is refused before + // configfile.Load, which would first migrate a legacy config.json. + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return handleWorkspaceGuardError(err) + } cfg, err := configfile.Load(beadsDir) if err != nil { return HandleError("loading config: %v", err) diff --git a/cmd/bd/graph_mode_admission_e2e_test.go b/cmd/bd/graph_mode_admission_e2e_test.go new file mode 100644 index 000000000..c0c91187c --- /dev/null +++ b/cmd/bd/graph_mode_admission_e2e_test.go @@ -0,0 +1,259 @@ +//go:build cgo + +package main + +import ( + "bytes" + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" +) + +// graphModeAdmissionFixture is a workspace whose metadata declares a +// graph_mode this build does not open. +type graphModeAdmissionFixture struct { + name string + filename string + mode string + metadata string + // embedded adds what a real graph workspace holds beside its metadata: + // the embedded Dolt root of its graph database and the format sentinel + // graph-preview builds write. Without the guard, an ordinary bd opens + // that database as an Issue store and migrates it. + embedded bool +} + +// write lays the fixture down under beadsDir and returns the metadata path. +func (f graphModeAdmissionFixture) write(t *testing.T, beadsDir string) string { + t.Helper() + path := filepath.Join(beadsDir, f.filename) + writeFile(t, path, []byte(f.metadata)) + if f.embedded { + if err := os.MkdirAll(filepath.Join(beadsDir, "embeddeddolt", "beads_graph_0123456789abcdef0123456789abcdef", ".dolt"), 0o700); err != nil { + t.Fatal(err) + } + writeFile(t, filepath.Join(beadsDir, "graph-preview-format"), []byte("link-preview-v9\n")) + } + if err := os.Chmod(beadsDir, 0o700); err != nil { + t.Fatal(err) + } + return path +} + +func graphModeAdmissionFixtures() []graphModeAdmissionFixture { + var fixtures []graphModeAdmissionFixture + fixtures = append(fixtures, graphModeAdmissionFixture{ + name: "metadata.json/link/embedded", filename: "metadata.json", mode: "link", + metadata: graphLinkWorkspaceMetadata, embedded: true, + }) + for _, filename := range []string{"metadata.json", "config.json"} { + for _, mode := range []string{"link", "future-format"} { + fixtures = append(fixtures, graphModeAdmissionFixture{ + name: filename + "/" + mode + "/server", filename: filename, mode: mode, + metadata: `{"backend":"dolt","dolt_mode":"server","graph_mode":"` + mode + `"}`, + }) + } + } + return fixtures +} + +// runGraphModeAdmissionBD runs bd in repoDir with a private HOME and no +// ambient BEADS_* selection, and returns its combined output and exit code. +func runGraphModeAdmissionBD(t *testing.T, bd, repoDir string, args ...string) (string, int) { + t.Helper() + return runGraphModeAdmissionBDEnv(t, bd, repoDir, nil, args...) +} + +// runGraphModeAdmissionBDEnv is runGraphModeAdmissionBD run in dir with env +// added to bd's environment. +func runGraphModeAdmissionBDEnv(t *testing.T, bd, dir string, env []string, args ...string) (string, int) { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, bd, args...) + cmd.Dir = dir + cmd.Env = append(bdEnv(t.TempDir()), + "BEADS_DOLT_SERVER_MODE=0", + "BEADS_DOLT_SHARED_SERVER=0", + ) + cmd.Env = append(cmd.Env, env...) + var output bytes.Buffer + cmd.Stdout = &output + cmd.Stderr = &output + err := cmd.Run() + if ctx.Err() != nil { + t.Fatalf("bd %s did not finish promptly: %v\n%s", strings.Join(args, " "), ctx.Err(), output.String()) + } + var exitErr *exec.ExitError + switch { + case err == nil: + return output.String(), 0 + case errors.As(err, &exitErr): + return output.String(), exitErr.ExitCode() + default: + t.Fatalf("bd %s: %v", strings.Join(args, " "), err) + return "", -1 + } +} + +// requireGraphModeRefusal fails unless bd args exited with the refusal of +// fixture's workspace format and its hint. +func requireGraphModeRefusal(t *testing.T, fixture graphModeAdmissionFixture, args []string, output string, code int) { + t.Helper() + if code != ExitUnsupportedWorkspaceFormat { + t.Fatalf("bd %s exit = %d, want %d (ExitUnsupportedWorkspaceFormat)\n%s", + strings.Join(args, " "), code, ExitUnsupportedWorkspaceFormat, output) + } + // --json renders the refusal as a JSON error, which escapes the quotes + // around the mode, so match its parts. + for _, want := range []string{"graph_mode", fixture.mode, "this bd build does not support", "use a bd build that supports graph_mode"} { + if !strings.Contains(output, want) { + t.Fatalf("bd %s refusal does not mention %q:\n%s", strings.Join(args, " "), want, output) + } + } +} + +// A refusal in a helper is insufficient: ordinary commands must reach it before +// metadata migration, version tracking, or an attempt to provision/open storage. +func TestGraphModeCLIRefusesLegacyStoreWithoutWorkspaceEffects(t *testing.T) { + bd := buildBDUnderTest(t) + commands := [][]string{ + {"show", "beads/plan"}, + {"list", "--json"}, + {"create", "graph fence probe"}, + {"context", "--json"}, + {"where"}, + {"dolt", "start"}, + {"doctor", "--json"}, + {"bootstrap", "--dry-run", "--yes"}, + {"init", "--force", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents"}, + // What the installed git hooks run; their shim passes exit 16 on. + {"hooks", "run", "pre-commit"}, + } + for _, fixture := range graphModeAdmissionFixtures() { + for _, args := range commands { + t.Run(fixture.name+"/"+args[0], func(t *testing.T) { + repoDir := t.TempDir() + initGitRepoAt(t, repoDir) + beadsDir := filepath.Join(repoDir, ".beads") + metadataPath := fixture.write(t, beadsDir) + before := legacyUpgradeTreeDigest(t, beadsDir) + + output, code := runGraphModeAdmissionBD(t, bd, repoDir, args...) + requireGraphModeRefusal(t, fixture, args, output, code) + if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { + t.Fatalf("refused bd %s changed the workspace\n%s", strings.Join(args, " "), output) + } + if got, err := os.ReadFile(metadataPath); err != nil || string(got) != fixture.metadata { + t.Fatalf("refused bd %s rewrote %s: %q, %v", strings.Join(args, " "), fixture.filename, got, err) + } + }) + } + } +} + +// Commands also reach a workspace they do not run in: create --repo, from +// another workspace or from none and as a dry run, --db and BEADS_DB, +// init --proxied-server, and bootstrap in a workspace with no metadata of its +// own, which takes the nearest ancestor's. Each must refuse a graph workspace +// the same way, before resolving it migrates a legacy config.json or anything +// opens its store. +func TestGraphModeOutsideEntryPointsRefuseWithoutWorkspaceEffects(t *testing.T) { + bd := buildBDUnderTest(t) + sourceDir, _, _ := bdInit(t, bd, "--prefix", "src") + outsideDir := t.TempDir() + entries := []struct { + name string + // run returns where bd runs, what it adds to bd's environment and + // bd's arguments, for a graph workspace in repoDir. + run func(t *testing.T, repoDir string) (dir string, env, args []string) + }{ + {"create --repo from a workspace", func(t *testing.T, repoDir string) (string, []string, []string) { + return sourceDir, nil, []string{"create", "--repo", repoDir, "graph fence probe"} + }}, + {"create --repo from outside", func(t *testing.T, repoDir string) (string, []string, []string) { + return outsideDir, nil, []string{"create", "--repo", repoDir, "graph fence probe"} + }}, + {"create --dry-run --repo", func(t *testing.T, repoDir string) (string, []string, []string) { + // Without --parent the preview opens no target at all. + return sourceDir, nil, []string{"create", "--dry-run", "--repo", repoDir, "graph fence probe"} + }}, + {"create --dry-run --parent --repo", func(t *testing.T, repoDir string) (string, []string, []string) { + // A dry run looks the parent up in the target's store. + return sourceDir, nil, []string{"create", "--dry-run", "--parent", "src-abc", "--repo", repoDir, "graph fence probe"} + }}, + {"--db", func(t *testing.T, repoDir string) (string, []string, []string) { + return outsideDir, nil, []string{"--db", filepath.Join(repoDir, ".beads"), "list", "--json"} + }}, + {"BEADS_DB", func(t *testing.T, repoDir string) (string, []string, []string) { + return outsideDir, []string{"BEADS_DB=" + filepath.Join(repoDir, ".beads")}, []string{"list", "--json"} + }}, + {"init --proxied-server", func(t *testing.T, repoDir string) (string, []string, []string) { + return repoDir, nil, []string{"init", "--proxied-server", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents"} + }}, + {"bootstrap below it", func(t *testing.T, repoDir string) (string, []string, []string) { + innerDir := filepath.Join(repoDir, "inner") + writeFile(t, filepath.Join(innerDir, ".beads", "config.yaml"), []byte("# bd workspace config\n")) + initGitRepoAt(t, innerDir) + return innerDir, nil, []string{"bootstrap", "--dry-run", "--yes"} + }}, + } + for _, fixture := range graphModeAdmissionFixtures() { + for _, entry := range entries { + t.Run(fixture.name+"/"+entry.name, func(t *testing.T) { + repoDir := t.TempDir() + initGitRepoAt(t, repoDir) + beadsDir := filepath.Join(repoDir, ".beads") + metadataPath := fixture.write(t, beadsDir) + dir, env, args := entry.run(t, repoDir) + before := legacyUpgradeTreeDigest(t, beadsDir) + + output, code := runGraphModeAdmissionBDEnv(t, bd, dir, env, args...) + requireGraphModeRefusal(t, fixture, args, output, code) + if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { + t.Fatalf("refused bd %s changed the workspace\n%s", strings.Join(args, " "), output) + } + if got, err := os.ReadFile(metadataPath); err != nil || string(got) != fixture.metadata { + t.Fatalf("refused bd %s rewrote %s: %q, %v", strings.Join(args, " "), fixture.filename, got, err) + } + }) + } + } +} + +// Commands that never open a store keep working in a workspace this build +// refuses, and leave it untouched. +func TestGraphModeWorkspaceAdmitsStorelessCommands(t *testing.T) { + bd := buildBDUnderTest(t) + fixture := graphModeAdmissionFixtures()[0] + for _, args := range [][]string{ + {"version"}, + {"--version"}, + {"help"}, + {"completion", "bash"}, + } { + t.Run(strings.Join(args, " "), func(t *testing.T) { + repoDir := t.TempDir() + initGitRepoAt(t, repoDir) + beadsDir := filepath.Join(repoDir, ".beads") + fixture.write(t, beadsDir) + before := legacyUpgradeTreeDigest(t, beadsDir) + + output, code := runGraphModeAdmissionBD(t, bd, repoDir, args...) + if code != 0 { + t.Fatalf("bd %s exit = %d, want 0\n%s", strings.Join(args, " "), code, output) + } + if strings.Contains(output, "graph_mode") { + t.Fatalf("store-less bd %s reported the workspace format:\n%s", strings.Join(args, " "), output) + } + if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { + t.Fatalf("bd %s changed the workspace", strings.Join(args, " ")) + } + }) + } +} diff --git a/cmd/bd/graph_mode_dependency_embedded_test.go b/cmd/bd/graph_mode_dependency_embedded_test.go new file mode 100644 index 000000000..1e2561a64 --- /dev/null +++ b/cmd/bd/graph_mode_dependency_embedded_test.go @@ -0,0 +1,90 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" +) + +// readGraphKeys returns the graph_* keys of a metadata.json and their values. +func readGraphKeys(t *testing.T, path string) map[string]any { + t.Helper() + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var doc map[string]any + if err := json.Unmarshal(data, &doc); err != nil { + t.Fatalf("parse %s: %v\n%s", path, err, data) + } + keys := map[string]any{} + for key, value := range doc { + if strings.HasPrefix(key, "graph_") { + keys[key] = value + } + } + return keys +} + +// The graph-mode fence must not change ordinary workspaces: one without the +// marker, and one whose metadata names the default "dependency" model +// explicitly, open and write as before, and a command that rewrites +// metadata.json keeps the marker exactly as it found it. +func TestEmbeddedGraphModeDependencyWorkspaceUnaffected(t *testing.T) { + if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { + t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") + } + t.Parallel() + bd := buildEmbeddedBD(t) + + for _, marker := range []string{"absent", "dependency"} { + t.Run(marker, func(t *testing.T) { + dir, beadsDir, _ := bdInit(t, bd, "--prefix", "gm") + metadataPath := filepath.Join(beadsDir, "metadata.json") + if marker != "absent" { + data, err := os.ReadFile(metadataPath) + if err != nil { + t.Fatal(err) + } + var doc map[string]any + if err := json.Unmarshal(data, &doc); err != nil { + t.Fatal(err) + } + doc["graph_mode"] = marker + data, err = json.MarshalIndent(doc, "", " ") + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(metadataPath, data, 0o600); err != nil { + t.Fatal(err) + } + } + wantKeys := readGraphKeys(t, metadataPath) + before, err := os.ReadFile(metadataPath) + if err != nil { + t.Fatal(err) + } + + issue := bdCreate(t, bd, dir, "ordinary workspace issue", "--type", "task") + if out := bdList(t, bd, dir, "--json"); !strings.Contains(out, issue.ID) { + t.Fatalf("bd list --json does not show %s:\n%s", issue.ID, out) + } + if after, err := os.ReadFile(metadataPath); err != nil || string(after) != string(before) { + t.Fatalf("create/list changed metadata.json:\nbefore:\n%s\nafter:\n%s\n%v", before, after, err) + } + + // upgrade ack rewrites metadata.json through configfile.Save. + if out, err := bdRunWithFlockRetry(t, bd, dir, "upgrade", "ack"); err != nil { + t.Fatalf("bd upgrade ack: %v\n%s", err, out) + } + if got := readGraphKeys(t, metadataPath); len(got) != len(wantKeys) || got["graph_mode"] != wantKeys["graph_mode"] { + data, _ := os.ReadFile(metadataPath) + t.Fatalf("metadata rewrite changed graph keys: got %v, want %v\n%s", got, wantKeys, data) + } + }) + } +} diff --git a/cmd/bd/init.go b/cmd/bd/init.go index bca3e336e..2f8133983 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -855,7 +855,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): if initProxiedServer { if beadsDir := resolveInitBeadsDir(); beadsDir != "" { if err := guardLegacyUpgradeWorkspace(beadsDir); err != nil { - return err + return handleWorkspaceGuardError(err) } } if err := runInitProxiedServer(cmd, rootCtx, initProxiedServerInput{ @@ -1006,7 +1006,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): // or rewrite state beside a source that has not been preserved. if beadsDir := resolveInitBeadsDir(); beadsDir != "" { if err := guardLegacyUpgradeWorkspaceForInit(beadsDir, initServerMode); err != nil { - return err + return handleWorkspaceGuardError(err) } } diff --git a/cmd/bd/legacy_upgrade_guard.go b/cmd/bd/legacy_upgrade_guard.go index 71ced0205..bbaf45f08 100644 --- a/cmd/bd/legacy_upgrade_guard.go +++ b/cmd/bd/legacy_upgrade_guard.go @@ -19,10 +19,12 @@ import ( ) // guardLegacyUpgradeWorkspace rejects the reviewed pre-Dolt and legacy-Dolt -// workspace shapes before command setup can track a version, migrate metadata, -// or construct a store. It intentionally classifies only metadata, regular -// SQLite files, and the bounded local version witness; storage internals stay -// behind the driver boundary. +// workspace shapes, and workspaces whose metadata declares a format this build +// cannot open (checkWorkspaceFormat), before command setup can track a version, +// migrate metadata, or construct a store. It intentionally classifies only +// metadata, regular SQLite files, and the bounded local version witness; +// storage internals stay behind the driver boundary. Render its refusals with +// handleWorkspaceGuardError. func guardLegacyUpgradeWorkspace(beadsDir string) error { return guardLegacyUpgradeWorkspaceForInit(beadsDir, false) } @@ -44,6 +46,12 @@ func guardLegacyUpgradeWorkspaceForInit(beadsDir string, initSelectsServer bool) if err != nil { return fmt.Errorf("loading config: %w", err) } + // This guard admits only the ordinary Issue store. A workspace in another + // format is refused before any legacy classification, which could + // otherwise admit it, or report it as a legacy store to migrate. + if err := checkWorkspaceFormat(cfg, beadsDir); err != nil { + return err + } if isHistoricalSQLiteWorkspace(beadsDir, cfg) { return legacyUpgradeRefusal("historical SQLite workspace") } @@ -271,9 +279,34 @@ func onlyLegacySQLiteEntries(beadsDir string, entries []os.DirEntry, databaseNam // excludes vc.db, so this bounded fallback runs only after that discovery found // no current workspace. func guardUndiscoveredLegacyWorkspace() error { + for _, beadsDir := range undiscoveredWorkspaceCandidates() { + if err := guardLegacyUpgradeWorkspace(beadsDir); err != nil { + return err + } + } + return nil +} + +// checkUndiscoveredWorkspaceFormat refuses a workspace in a format this build +// cannot open (checkWorkspaceFormat) that discovery passed over, such as one +// marked only by a legacy config.json, and nothing else. It reads metadata +// without migrating it. +func checkUndiscoveredWorkspaceFormat() error { + for _, beadsDir := range undiscoveredWorkspaceCandidates() { + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return err + } + } + return nil +} + +// undiscoveredWorkspaceCandidates lists, nearest first, the .beads directories +// the undiscovered-workspace checks read: the one BEADS_DIR names or, without +// it, each .beads from the working directory up to the repository root and +// below BEADS_CEILING_DIRECTORIES. +func undiscoveredWorkspaceCandidates() []string { if explicit := os.Getenv("BEADS_DIR"); explicit != "" { - beadsDir := beads.FollowRedirect(utils.CanonicalizePath(explicit)) - return guardLegacyUpgradeWorkspace(beadsDir) + return []string{beads.FollowRedirect(utils.CanonicalizePath(explicit))} } cwd, err := os.Getwd() @@ -283,19 +316,18 @@ func guardUndiscoveredLegacyWorkspace() error { dir := utils.CanonicalizePath(cwd) boundary := utils.CanonicalizePath(git.GetRepoRoot()) bound := ceiling.For(dir) + var candidates []string for { if bound.Excludes(dir) { - return nil - } - if err := guardLegacyUpgradeWorkspace(filepath.Join(dir, ".beads")); err != nil { - return err + return candidates } + candidates = append(candidates, filepath.Join(dir, ".beads")) if boundary != "" && utils.PathsEqual(dir, boundary) { - return nil + return candidates } parent := filepath.Dir(dir) if parent == dir { - return nil + return candidates } dir = parent } diff --git a/cmd/bd/main.go b/cmd/bd/main.go index a9db9392c..68c9f89b7 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -1579,6 +1579,9 @@ var rootCmd = &cobra.Command{ } } if err := guardLegacyNoStoreCommand(cmd, beadsDir); err != nil { + if isUnsupportedWorkspaceFormat(err) { + return handleWorkspaceGuardError(err) + } isMigrationCommand := false for current := cmd; current != nil; current = current.Parent() { if current.Name() == "migrate" { @@ -1638,7 +1641,7 @@ var rootCmd = &cobra.Command{ prepareSelectedCommandContext(bd, true) refreshBoundCommandConfig(cmd) if guardErr := guardLegacyUpgradeWorkspace(bd); guardErr != nil { - return HandleError("%v", guardErr) + return handleWorkspaceGuardError(guardErr) } cfg, cfgErr := configfile.LoadForDiscovery(bd) if cfgErr != nil || cfg != nil && (cfg.IsDoltProxiedServerMode() || @@ -1659,7 +1662,7 @@ var rootCmd = &cobra.Command{ dbPath = bd } } else if guardErr := guardUndiscoveredLegacyWorkspace(); guardErr != nil { - return HandleError("%v", guardErr) + return handleWorkspaceGuardError(guardErr) } } @@ -1744,7 +1747,7 @@ var rootCmd = &cobra.Command{ prepareSelectedCommandContext(beadsDir, true) refreshBoundCommandConfig(cmd) if guardErr := guardLegacyUpgradeWorkspace(beadsDir); guardErr != nil { - return HandleError("%v", guardErr) + return handleWorkspaceGuardError(guardErr) } // Workspace operation gate: every command that reaches this point diff --git a/cmd/bd/store_factory.go b/cmd/bd/store_factory.go index 22ff0edb2..e41e88bd9 100644 --- a/cmd/bd/store_factory.go +++ b/cmd/bd/store_factory.go @@ -180,6 +180,9 @@ func newExistingDoltStoreFromConfig(ctx context.Context, beadsDir string) (stora func openDoltStoreFromConfig(ctx context.Context, beadsDir string, requireExisting bool) (s storage.DoltStorage, err error) { defer func() { s, err = activateVersionedHistoryStore(ctx, s, err) }() defer func() { s, err = activateEventsJournalStore(beadsDir, s, err) }() + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } cfg, err := configfile.Load(beadsDir) if err != nil { // A present-but-unloadable metadata.json must not degrade to the @@ -299,6 +302,9 @@ func newPreviewStoreFromConfig(ctx context.Context, beadsDir string) (storage.Do // OpenNonMutating hook on backends.Backend, which this function would call // instead, is the follow-up that closes the exception. func openNonMutatingStoreFromConfig(ctx context.Context, beadsDir string, preview bool) (storage.DoltStorage, error) { + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } cfg, err := configfile.Load(beadsDir) if err != nil { // Same contract as newDoltStoreFromConfig: a present-but-unloadable diff --git a/cmd/bd/store_factory_nocgo.go b/cmd/bd/store_factory_nocgo.go index 48dc24981..b5e882750 100644 --- a/cmd/bd/store_factory_nocgo.go +++ b/cmd/bd/store_factory_nocgo.go @@ -69,6 +69,9 @@ func acquireEmbeddedLock(_ string, _ bool) (util.Unlocker, error) { func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (s storage.DoltStorage, err error) { defer func() { s, err = activateVersionedHistoryStore(ctx, s, err) }() defer func() { s, err = activateEventsJournalStore(beadsDir, s, err) }() + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } cfg, err := configfile.Load(beadsDir) if err != nil { // Name the real cause: without this, a present-but-unloadable @@ -106,6 +109,9 @@ func newExistingDoltStoreFromConfig(ctx context.Context, beadsDir string) (stora // in the construction guard). The registry arm is the exception the cgo // twin, openNonMutatingStoreFromConfig, describes. func newReadOnlyStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) { + if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } cfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("load %s: %w", configfile.ConfigPath(beadsDir), err) diff --git a/cmd/bd/store_factory_nocgo_test.go b/cmd/bd/store_factory_nocgo_test.go index b20a8cacc..acf430c12 100644 --- a/cmd/bd/store_factory_nocgo_test.go +++ b/cmd/bd/store_factory_nocgo_test.go @@ -13,6 +13,7 @@ import ( "time" "github.com/steveyegge/beads/internal/config" + "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/dolt" ) @@ -198,3 +199,47 @@ func TestNocgoFactoriesHonorSharedServer(t *testing.T) { }) } } + +// TestNocgoStoreFactoriesRefuseGraphWorkspace is the !cgo half of +// TestStoreFactoriesRefuseGraphWorkspace. The CGO_ENABLED=0 binaries +// .goreleaser.yml ships open routed, cross-repository and create --repo +// targets through their own copies of these factories, so each must refuse a +// graph workspace before its configfile.Load, which would otherwise migrate +// one marked only by a legacy config.json into metadata.json first. It runs in +// CI only because bazel.yml's PURE_CMD_BD_TESTS allowlists it by name. +func TestNocgoStoreFactoriesRefuseGraphWorkspace(t *testing.T) { + isolateSharedServerSignals(t) + pinLegacyUpgradeGuardEnv(t) + const metadata = `{"backend":"dolt","graph_mode":"link"}` + factories := map[string]func(context.Context, string) (storage.DoltStorage, error){ + "read-write": newDoltStoreFromConfig, + "routed": newExistingDoltStoreFromConfig, + "read-only": newReadOnlyStoreFromConfig, + "preview": newPreviewStoreFromConfig, + } + for _, filename := range []string{"metadata.json", "config.json"} { + for name, open := range factories { + t.Run(filename+"/"+name, func(t *testing.T) { + beadsDir := t.TempDir() + path := filepath.Join(beadsDir, filename) + if err := os.WriteFile(path, []byte(metadata), 0o600); err != nil { + t.Fatal(err) + } + store, err := open(t.Context(), beadsDir) + if err == nil { + _ = store.Close() + } + if !isUnsupportedWorkspaceFormat(err) { + t.Fatalf("%s open = %v, want unsupported workspace format refusal", name, err) + } + if got, readErr := os.ReadFile(path); readErr != nil || string(got) != metadata { + t.Fatalf("%s open rewrote %s: %q, %v", name, filename, got, readErr) + } + entries, readErr := os.ReadDir(beadsDir) + if readErr != nil || len(entries) != 1 { + t.Fatalf("%s open left %d entries in the workspace, want only %s (%v)", name, len(entries), filename, readErr) + } + }) + } + } +} diff --git a/cmd/bd/store_reopen.go b/cmd/bd/store_reopen.go index f65f69255..4a774a7fe 100644 --- a/cmd/bd/store_reopen.go +++ b/cmd/bd/store_reopen.go @@ -91,7 +91,10 @@ func resolveBeadsDirForDBPath(dbPath string) string { } for _, beadsDir := range candidates { - cfg, err := configfile.Load(beadsDir) + // Read-only: --db, BEADS_DB and create --repo resolve their target + // here before command admission classifies it, and configfile.Load + // would migrate a candidate's legacy config.json on the way. + cfg, err := configfile.LoadForDiscovery(beadsDir) if err != nil || cfg == nil { continue } diff --git a/cmd/bd/workspace_format_guard.go b/cmd/bd/workspace_format_guard.go new file mode 100644 index 000000000..4e9daa0a4 --- /dev/null +++ b/cmd/bd/workspace_format_guard.go @@ -0,0 +1,85 @@ +package main + +import ( + "errors" + "fmt" + + "github.com/steveyegge/beads/internal/configfile" +) + +// 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". Stable so a wrapper can +// tell "this workspace needs a different bd build" apart from a generic +// failure (1) without grepping stderr. +// +// 16 because 13, 14 and 15 are taken (ExitGuardMismatch, ExitMigrationFrozen, +// ExitHomeDirRefused). Like ExitHomeDirRefused, it is not part of protocol v0 +// §E3, which freezes 10/11/12/130. +const ExitUnsupportedWorkspaceFormat = 16 + +// unsupportedWorkspaceFormatError refuses a workspace whose metadata 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, so nothing else in the metadata 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 +} + +func (e *unsupportedWorkspaceFormatError) Error() string { + 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 { + 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. +// cfg may be nil (no metadata), which is an ordinary workspace. +func checkWorkspaceFormat(cfg *configfile.Config, beadsDir string) error { + if mode, err := cfg.GetGraphMode(); err == nil && mode == configfile.GraphModeDependency { + return nil + } + return &unsupportedWorkspaceFormatError{beadsDir: beadsDir, graphMode: cfg.GraphMode} +} + +// checkDiscoveredWorkspaceFormat runs checkWorkspaceFormat on beadsDir's +// metadata as LoadForDiscovery reads it, which never rewrites the workspace. +// A path that opens or initializes a workspace without passing through command +// admission runs it first: configfile.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. +func checkDiscoveredWorkspaceFormat(beadsDir string) error { + cfg, err := configfile.LoadForDiscovery(beadsDir) + if err != nil { + return nil + } + return checkWorkspaceFormat(cfg, beadsDir) +} + +func isUnsupportedWorkspaceFormat(err error) bool { + var formatErr *unsupportedWorkspaceFormatError + return errors.As(err, &formatErr) +} + +// handleWorkspaceGuardError renders a workspace admission refusal from +// guardLegacyUpgradeWorkspace. An unsupported workspace format gets a hint and +// ExitUnsupportedWorkspaceFormat; every other refusal keeps HandleError's +// rendering and exit 1. +func handleWorkspaceGuardError(err error) error { + if err == nil { + return nil + } + var formatErr *unsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) { + return HandleError("%v", err) + } + _ = 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 new file mode 100644 index 000000000..a0609ed82 --- /dev/null +++ b/cmd/bd/workspace_format_guard_test.go @@ -0,0 +1,259 @@ +//go:build cgo + +package main + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/configfile" + "github.com/steveyegge/beads/internal/storage" +) + +// graphLinkWorkspaceMetadata is the metadata.json a graph_mode link workspace +// carries: the marker, its binding, and an ordinary embedded Dolt backend. +const graphLinkWorkspaceMetadata = `{ + "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" +}` + +// graphWorkspaceLayout is a .beads directory whose metadata declares a +// graph_mode, plus whatever else the case lays down beside it. +type graphWorkspaceLayout struct { + filename string + metadata string + // embedded adds the embedded Dolt root a graph database lives in. + embedded bool + // doltRoot adds a .beads/dolt root without a version witness, which + // the legacy classification reads as a pre-1.0 Dolt workspace. + doltRoot bool + // sqlite adds a beads.db, which the legacy classification reads as a + // historical SQLite workspace when nothing else selects Dolt. + sqlite bool +} + +// write lays the layout down under beadsDir and returns the metadata path. +func (l graphWorkspaceLayout) write(t *testing.T, beadsDir string) string { + t.Helper() + path := filepath.Join(beadsDir, l.filename) + writeFile(t, path, []byte(l.metadata)) + if l.embedded { + if err := os.MkdirAll(filepath.Join(beadsDir, "embeddeddolt", "beads_graph_0123456789abcdef0123456789abcdef", ".dolt"), 0o700); err != nil { + t.Fatal(err) + } + } + if l.doltRoot { + if err := os.Mkdir(filepath.Join(beadsDir, "dolt"), 0o700); err != nil { + t.Fatal(err) + } + } + if l.sqlite { + writeFile(t, filepath.Join(beadsDir, "beads.db"), []byte("SQLite format 3\x00")) + } + return path +} + +func TestWorkspaceFormatGuardRefusesGraphWorkspaces(t *testing.T) { + pinLegacyUpgradeGuardEnv(t) + tests := []struct { + name string + layout graphWorkspaceLayout + mode string + }{ + {name: "link embedded", mode: "link", + layout: graphWorkspaceLayout{filename: "metadata.json", metadata: graphLinkWorkspaceMetadata, embedded: true}}, + // The legacy classification admits this shape: a server workspace + // with no local Dolt root. + {name: "link server", mode: "link", + layout: graphWorkspaceLayout{filename: "metadata.json", metadata: `{"backend":"dolt","dolt_mode":"server","graph_mode":"link"}`}}, + {name: "link legacy config.json", mode: "link", + layout: graphWorkspaceLayout{filename: "config.json", metadata: graphLinkWorkspaceMetadata, embedded: true}}, + {name: "unknown marker", mode: "future-format", + layout: graphWorkspaceLayout{filename: "metadata.json", metadata: `{"backend":"dolt","graph_mode":"future-format"}`, embedded: true}}, + {name: "marker is case-sensitive", mode: "Dependency", + layout: graphWorkspaceLayout{filename: "metadata.json", metadata: `{"backend":"dolt","graph_mode":"Dependency"}`, embedded: true}}, + // Without the format check first, these two shapes are reported as + // legacy workspaces to migrate. + {name: "link over legacy dolt root", mode: "link", + layout: graphWorkspaceLayout{filename: "metadata.json", metadata: `{"backend":"dolt","graph_mode":"link"}`, doltRoot: true}}, + {name: "link beside historical sqlite", mode: "link", + layout: graphWorkspaceLayout{filename: "metadata.json", metadata: `{"graph_mode":"link"}`, sqlite: true}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + beadsDir := t.TempDir() + path := tt.layout.write(t, beadsDir) + before := legacyUpgradeTreeDigest(t, beadsDir) + + err := guardLegacyUpgradeWorkspace(beadsDir) + if !isUnsupportedWorkspaceFormat(err) { + t.Fatalf("guardLegacyUpgradeWorkspace() = %v, want unsupported workspace format refusal", err) + } + for _, want := range []string{beadsDir, `graph_mode "` + tt.mode + `"`, "this bd build does not support", "no storage database was opened or modified"} { + if !strings.Contains(err.Error(), want) { + t.Fatalf("refusal %q does not mention %q", err, want) + } + } + if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { + t.Fatalf("guard changed the workspace") + } + if got, readErr := os.ReadFile(path); readErr != nil || string(got) != tt.layout.metadata { + t.Fatalf("guard rewrote %s: %q, %v", tt.layout.filename, got, readErr) + } + }) + } +} + +func TestWorkspaceFormatGuardAdmitsDependencyWorkspaces(t *testing.T) { + pinLegacyUpgradeGuardEnv(t) + for _, metadata := range []string{ + `{"backend":"dolt"}`, + `{"backend":"dolt","graph_mode":""}`, + `{"backend":"dolt","graph_mode":"dependency"}`, + } { + t.Run(metadata, func(t *testing.T) { + beadsDir := t.TempDir() + writeFile(t, filepath.Join(beadsDir, "metadata.json"), []byte(metadata)) + if err := guardLegacyUpgradeWorkspace(beadsDir); err != nil { + t.Fatalf("guardLegacyUpgradeWorkspace() = %v, want admission", err) + } + }) + } + if err := guardLegacyUpgradeWorkspace(t.TempDir()); err != nil { + t.Fatalf("guardLegacyUpgradeWorkspace(no metadata) = %v, want admission", err) + } +} + +// Routed writes, cross-repository reads and create --repo open another +// workspace's store without passing through command admission, so the store +// factories must refuse a graph workspace themselves, before any open. That +// includes one marked only by a legacy config.json, which configfile.Load +// would migrate into metadata.json before a check of the loaded metadata ran. +func TestStoreFactoriesRefuseGraphWorkspace(t *testing.T) { + pinLegacyUpgradeGuardEnv(t) + factories := map[string]func(context.Context, string) (storage.DoltStorage, error){ + "read-write": newDoltStoreFromConfig, + "routed": newExistingDoltStoreFromConfig, + "read-only": newReadOnlyStoreFromConfig, + "preview": newPreviewStoreFromConfig, + } + for _, filename := range []string{"metadata.json", "config.json"} { + for name, open := range factories { + t.Run(filename+"/"+name, func(t *testing.T) { + beadsDir := t.TempDir() + path := graphWorkspaceLayout{filename: filename, metadata: graphLinkWorkspaceMetadata, embedded: true}.write(t, beadsDir) + before := legacyUpgradeTreeDigest(t, beadsDir) + store, err := open(t.Context(), beadsDir) + if err == nil { + _ = store.Close() + } + if !isUnsupportedWorkspaceFormat(err) { + t.Fatalf("%s open = %v, want unsupported workspace format refusal", name, err) + } + if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { + t.Fatalf("%s open changed the workspace", name) + } + if got, readErr := os.ReadFile(path); readErr != nil || string(got) != graphLinkWorkspaceMetadata { + t.Fatalf("%s open rewrote %s: %q, %v", name, filename, got, readErr) + } + }) + } + } +} + +// Error framing and the bd dolt diagnostics read a workspace's metadata outside +// the store factories: framing names the backend of an open that may have just +// been refused, and bd dolt show and status, which admission guards already, +// load and validate it again. None of them may migrate a graph workspace's +// legacy config.json into metadata.json, and the diagnostics refuse it the way +// admission does. +func TestWorkspaceMetadataReadersLeaveGraphWorkspaceUnchanged(t *testing.T) { + pinLegacyUpgradeGuardEnv(t) + requireFormatExit := func(t *testing.T, name string, run func() error) { + t.Helper() + var err error + captureStderr(t, func() { err = run() }) + if code, ok := exitCodeFromError(err); !ok || code != ExitUnsupportedWorkspaceFormat { + t.Fatalf("%s exit code = %d (%v), want %d", name, code, ok, ExitUnsupportedWorkspaceFormat) + } + } + readers := map[string]func(t *testing.T, beadsDir string){ + "error framing": func(t *testing.T, beadsDir string) { + if got := backendNameForErrorFraming(beadsDir); got != configfile.BackendDolt { + t.Fatalf("backendNameForErrorFraming = %q, want %q", got, configfile.BackendDolt) + } + }, + "dolt backend config": func(t *testing.T, beadsDir string) { + if _, err := loadDoltBackendConfig(beadsDir); !isUnsupportedWorkspaceFormat(err) { + t.Fatalf("loadDoltBackendConfig = %v, want unsupported workspace format refusal", err) + } + }, + "dolt show": func(t *testing.T, _ string) { + requireFormatExit(t, "bd dolt show", func() error { return showDoltConfig(false) }) + }, + "dolt status": func(t *testing.T, _ string) { + requireFormatExit(t, "bd dolt status", func() error { return doltStatusCmd.RunE(doltStatusCmd, nil) }) + }, + } + for _, filename := range []string{"metadata.json", "config.json"} { + for name, read := range readers { + t.Run(filename+"/"+name, func(t *testing.T) { + beadsDir := t.TempDir() + // The embedded root lets workspace discovery select a + // workspace marked only by config.json. + path := graphWorkspaceLayout{filename: filename, metadata: graphLinkWorkspaceMetadata, embedded: true}.write(t, beadsDir) + t.Setenv("BEADS_DIR", beadsDir) + before := legacyUpgradeTreeDigest(t, beadsDir) + read(t, beadsDir) + if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { + t.Fatalf("%s changed the workspace", name) + } + if got, err := os.ReadFile(path); err != nil || string(got) != graphLinkWorkspaceMetadata { + t.Fatalf("%s rewrote %s: %q, %v", name, filename, got, err) + } + }) + } + } +} + +func TestHandleWorkspaceGuardErrorExitCodes(t *testing.T) { + if err := handleWorkspaceGuardError(nil); err != nil { + t.Fatalf("handleWorkspaceGuardError(nil) = %v", err) + } + formatErr := &unsupportedWorkspaceFormatError{beadsDir: "/w/.beads", graphMode: "link"} + tests := []struct { + name string + err error + want int + }{ + {name: "unsupported format", err: formatErr, want: ExitUnsupportedWorkspaceFormat}, + {name: "legacy refusal", err: legacyUpgradeRefusal("legacy Dolt workspace"), want: 1}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var got error + stderr := captureStderr(t, func() { got = handleWorkspaceGuardError(tt.err) }) + if code, ok := exitCodeFromError(got); !ok || code != tt.want { + t.Fatalf("exit code = %d (%v), want %d", code, ok, tt.want) + } + if !strings.Contains(stderr, "Error: "+tt.err.Error()) { + t.Fatalf("stderr does not carry the refusal:\n%s", stderr) + } + }) + } + if ExitUnsupportedWorkspaceFormat != 16 { + t.Fatalf("ExitUnsupportedWorkspaceFormat = %d; it is a stable scripting contract", ExitUnsupportedWorkspaceFormat) + } +} diff --git a/cmd/bd/workspace_gate.go b/cmd/bd/workspace_gate.go index 70e5ed492..442bff099 100644 --- a/cmd/bd/workspace_gate.go +++ b/cmd/bd/workspace_gate.go @@ -60,12 +60,14 @@ import ( // and --dry-run/--inspect previews also open the store read-only, but // they keep creating the files, so they stay visible too. // -// Known residual (PR-B2 scope, documented honestly): the pre-chokepoint -// DISCOVERY code paths (configfile.Load at main.go's early config probe and -// internal/beads.findDatabaseInBeadsDir) can perform the legacy -// config.json→metadata.json migration write BEFORE this gate is acquired — -// the chokepoint necessarily runs after workspace selection. Deferring that -// legacy write behind the gate is out of scope here. +// The legacy config.json→metadata.json migration write happens behind this +// gate. The pre-chokepoint DISCOVERY code paths (main.go's early config probe, +// the --db/BEADS_DB resolution in resolveBeadsDirForDBPath, and +// internal/beads.findDatabaseInBeadsDir) read metadata with +// configfile.LoadForDiscovery, which never migrates, and the internal/doltserver +// loaders that plan the gate call configfile.Load only when metadata.json +// already exists. The command workspace's first migrating configfile.Load is +// the backend resolution PersistentPreRunE runs after acquiring the gate. // workspaceGateHandle is the gate set held for the current command, stored // beside `store` because their lifetimes are paired: acquired just before diff --git a/internal/beads/beads.go b/internal/beads/beads.go index 2629ba508..03a303def 100644 --- a/internal/beads/beads.go +++ b/internal/beads/beads.go @@ -547,8 +547,10 @@ func findBeadsDirUpward(start string) string { // Embedded Dolt checks both embeddeddolt/ and the legacy dolt/ path. Returns // empty string if no database is configured or found. func findDatabaseInBeadsDir(beadsDir string, _ bool) string { - // Check for metadata.json first (single source of truth) - cfg, err := configfile.Load(beadsDir) + // Check for metadata.json first (single source of truth). Discovery reads + // it without migrating a legacy config.json: callers locate a workspace + // here before admission has decided whether this build may touch it. + cfg, err := configfile.LoadForDiscovery(beadsDir) if err != nil { // A present but unreadable/invalid metadata file is authoritative. Do // not bypass it by discovering a leftover local Dolt directory; callers diff --git a/internal/configfile/BUILD.bazel b/internal/configfile/BUILD.bazel index 8e3dc50ba..b56381c3c 100644 --- a/internal/configfile/BUILD.bazel +++ b/internal/configfile/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "credentials_unix.go", "credentials_windows.go", "external_dolt_config.go", + "graph_mode.go", "proxied_server_client_info.go", ], importpath = "github.com/steveyegge/beads/internal/configfile", @@ -28,6 +29,7 @@ go_test( "configfile_test.go", "credentials_test.go", "external_dolt_config_test.go", + "graph_mode_test.go", ], embed = [":configfile"], deps = ["//internal/config"], diff --git a/internal/configfile/configfile.go b/internal/configfile/configfile.go index 9a832354f..92e371820 100644 --- a/internal/configfile/configfile.go +++ b/internal/configfile/configfile.go @@ -20,6 +20,25 @@ type Config struct { Database string `json:"database"` Backend string `json:"backend,omitempty"` // Storage backend: "dolt" (default), a registered extension, or a legacy rejection tombstone. Read via GetBackend(). + // GraphMode identifies the workspace's data model, independently of its + // storage backend. An absent marker preserves existing Issue workspaces. + // This build opens only GraphModeDependency workspaces (see GetGraphMode); + // the marker is carried so a Save never strips it from a workspace this + // build must refuse. + GraphMode string `json:"graph_mode,omitempty"` + + // Graph workspace binding, written by builds that create graph_mode + // workspaces. This build never reads them, and missing or mismatched + // values never authorize implicit bootstrap or migration. A Save carries + // these known fields only, with the tags graph-preview builds declare: a + // zero graph_schema_version and a false graph_ready are omitted, as those + // builds omit them, and a graph_* key outside this set is not kept. + GraphScopeURL string `json:"graph_scope_url,omitempty"` + GraphAuthorityID string `json:"graph_authority_id,omitempty"` + GraphWorkspace string `json:"graph_workspace,omitempty"` + GraphSchemaVersion int `json:"graph_schema_version,omitempty"` + GraphReady bool `json:"graph_ready,omitempty"` + // Deletions configuration DeletionsRetentionDays int `json:"deletions_retention_days,omitempty"` // 0 means use default (3 days) diff --git a/internal/configfile/graph_mode.go b/internal/configfile/graph_mode.go new file mode 100644 index 000000000..ef2b24857 --- /dev/null +++ b/internal/configfile/graph_mode.go @@ -0,0 +1,30 @@ +package configfile + +import "fmt" + +// Workspace data models a metadata.json graph_mode marker can name. +// GraphModeDependency is the Issue-and-dependency workspace every bd opens; +// an absent marker means the same thing. GraphModeLink is the persisted +// graph workspace format written by graph-preview builds. +const ( + GraphModeDependency = "dependency" + GraphModeLink = "link" +) + +// GetGraphMode resolves the persisted workspace model. Unknown values must not +// fall back to the Issue store: they may describe a format this client cannot +// safely open. A missing config or marker retains the historical default. +// +// A recognized mode is not necessarily one this build can open: callers that +// open storage must still refuse anything other than GraphModeDependency. +func (c *Config) GetGraphMode() (string, error) { + if c == nil || c.GraphMode == "" { + return GraphModeDependency, nil + } + switch c.GraphMode { + case GraphModeDependency, GraphModeLink: + return c.GraphMode, nil + default: + return "", fmt.Errorf("unsupported workspace graph_mode %q; use a compatible bd build without changing the workspace marker", c.GraphMode) + } +} diff --git a/internal/configfile/graph_mode_test.go b/internal/configfile/graph_mode_test.go new file mode 100644 index 000000000..27f2e4e78 --- /dev/null +++ b/internal/configfile/graph_mode_test.go @@ -0,0 +1,170 @@ +package configfile + +import ( + "encoding/json" + "os" + "path/filepath" + "reflect" + "strings" + "testing" +) + +func TestGraphModeDiscoveryAndRoundTrip(t *testing.T) { + for _, filename := range []string{ConfigFileName, "config.json"} { + for _, mode := range []string{"", GraphModeDependency, GraphModeLink, "future-format"} { + t.Run(filename+"/"+mode, func(t *testing.T) { + dir := t.TempDir() + input := `{"backend":"dolt","graph_mode":"` + mode + `"}` + path := filepath.Join(dir, filename) + if err := os.WriteFile(path, []byte(input), 0o600); err != nil { + t.Fatal(err) + } + cfg, err := LoadForDiscovery(dir) + if err != nil { + t.Fatal(err) + } + got, err := cfg.GetGraphMode() + if mode == "future-format" { + if err == nil || got != "" { + t.Fatalf("unknown marker resolved to %q, %v", got, err) + } + } else { + want := mode + if want == "" { + want = GraphModeDependency + } + if err != nil || got != want { + t.Fatalf("mode = %q, %v; want %q", got, err, want) + } + } + entries, err := os.ReadDir(dir) + if err != nil || len(entries) != 1 { + t.Fatalf("discovery changed directory: %v, %v", entries, err) + } + after, err := os.ReadFile(path) + if err != nil || string(after) != input { + t.Fatalf("discovery changed metadata: %q, %v", after, err) + } + // Saving otherwise unrelated config must retain the marker, + // including one this client does not understand. + if err := cfg.Save(dir); err != nil { + t.Fatal(err) + } + loaded, err := LoadForDiscovery(dir) + if err != nil || loaded == nil || loaded.GraphMode != mode { + t.Fatalf("marker lost during save: %#v, %v", loaded, err) + } + }) + } + } +} + +func TestGraphModeAbsentConfigPreservesLegacyDefault(t *testing.T) { + cfg, err := LoadForDiscovery(t.TempDir()) + if err != nil || cfg != nil { + t.Fatalf("absent config = %#v, %v", cfg, err) + } + mode, err := cfg.GetGraphMode() + if err != nil || mode != GraphModeDependency { + t.Fatalf("absent config mode = %q, %v", mode, err) + } +} + +// graphWorkspaceMetadata is the metadata.json shape a graph_mode link +// workspace carries: the marker, its binding, and ordinary Dolt settings. +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" +}` + +// graphKeys returns every graph_* key of a metadata document with its value. +func graphKeys(t *testing.T, data []byte) map[string]any { + t.Helper() + var doc map[string]any + if err := json.Unmarshal(data, &doc); err != nil { + t.Fatalf("parse metadata %q: %v", data, err) + } + keys := map[string]any{} + for key, value := range doc { + if strings.HasPrefix(key, "graph_") { + keys[key] = value + } + } + return keys +} + +// A bd that does not open graph workspaces still rewrites metadata on paths +// that change unrelated settings, and Load itself rewrites a legacy +// config.json as metadata.json. Neither may strip the graph marker or its +// binding: a workspace that lost them would read as an ordinary Issue store. +func TestGraphWorkspaceBindingSurvivesSave(t *testing.T) { + want := graphKeys(t, []byte(graphWorkspaceMetadata)) + if len(want) != 6 { + t.Fatalf("fixture carries %d graph_* keys, want 6: %v", len(want), want) + } + + t.Run("metadata.json", func(t *testing.T) { + dir := t.TempDir() + if err := os.WriteFile(ConfigPath(dir), []byte(graphWorkspaceMetadata), 0o600); err != nil { + t.Fatal(err) + } + cfg, err := Load(dir) + if err != nil || cfg == nil { + t.Fatalf("Load = %#v, %v", cfg, err) + } + cfg.StaleClosedIssuesDays = 30 + if err := cfg.Save(dir); err != nil { + t.Fatal(err) + } + saved, err := os.ReadFile(ConfigPath(dir)) + if err != nil { + t.Fatal(err) + } + if got := graphKeys(t, saved); !reflect.DeepEqual(got, want) { + t.Fatalf("Save changed the graph binding:\n got %v\nwant %v\nsaved:\n%s", got, want, saved) + } + }) + + t.Run("legacy config.json migration", func(t *testing.T) { + dir := t.TempDir() + legacy := filepath.Join(dir, "config.json") + if err := os.WriteFile(legacy, []byte(graphWorkspaceMetadata), 0o600); err != nil { + t.Fatal(err) + } + if _, err := Load(dir); err != nil { + t.Fatal(err) + } + migrated, err := os.ReadFile(ConfigPath(dir)) + if err != nil { + t.Fatalf("Load did not migrate config.json: %v", err) + } + if got := graphKeys(t, migrated); !reflect.DeepEqual(got, want) { + t.Fatalf("config.json migration changed the graph binding:\n got %v\nwant %v\nmigrated:\n%s", got, want, migrated) + } + }) +} + +// The marker and binding are omitempty, so metadata.json written for an +// ordinary workspace is byte-for-byte what it was before they existed. +func TestGraphFieldsOmittedFromOrdinaryMetadata(t *testing.T) { + dir := t.TempDir() + if err := DefaultConfig().Save(dir); err != nil { + t.Fatal(err) + } + data, err := os.ReadFile(ConfigPath(dir)) + if err != nil { + t.Fatal(err) + } + if got := graphKeys(t, data); len(got) != 0 { + t.Fatalf("ordinary metadata.json gained graph keys %v:\n%s", got, data) + } +} diff --git a/scripts/ci_workflow_test.go b/scripts/ci_workflow_test.go index 80a8ca14b..5f9842d90 100644 --- a/scripts/ci_workflow_test.go +++ b/scripts/ci_workflow_test.go @@ -4571,18 +4571,20 @@ func TestBazelEmbeddedJobRunsEmbeddedTier(t *testing.T) { // group, only the split (core gets slower again). var conformanceCoreSlowGroups = []string{"ReadyCountsPageChunking", "Portable"} -// bazel-pure replaces pr.yml's check-cmd-bd-puregeo-tests job: the same pure +// bazel-pure replaces pr.yml's check-cmd-bd-puregeo-tests job: its pure // cmd/bd test selector, the same pure build set, and the js/wasm hook test // under the same exact-count guard. // bazel.yml's pure-Go and js/wasm lane is the only run of the CGO_ENABLED=0 // portability boundaries pr.yml's retired check-cmd-bd-puregeo-tests job // owned (ga-96smfk.22): the same -short cmd/bd subset, the same cmd/bd and // test-binary builds, and the exact js/wasm refusal contract with its count -// guard. The selectors below are that job's, frozen. +// guard. The selectors below are that job's, frozen, except that the cmd/bd +// subset also runs TestNocgoStoreFactoriesRefuseGraphWorkspace, the only run +// of the CGO_ENABLED=0 store factories' graph_mode refusal. func TestBazelPureJobMirrorsPureGoJob(t *testing.T) { job := readCIWorkflow(t, bazelWorkflowName).job(t, bazelPureJobName) - const pureCmdBDTests = "^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir|NocgoFactoriesHonorSharedServer|NocgoNewDoltStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoNewReadOnlyStoreFromConfig_ErrorSuggestsCorrectFlag)" + const pureCmdBDTests = "^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir|NocgoFactoriesHonorSharedServer|NocgoNewDoltStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoNewReadOnlyStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoStoreFactoriesRefuseGraphWorkspace)" if job.Env["PURE_CMD_BD_TESTS"] != pureCmdBDTests { t.Errorf("%s PURE_CMD_BD_TESTS = %q, want the pure cmd/bd -run selector %q", bazelPureJobName, job.Env["PURE_CMD_BD_TESTS"], pureCmdBDTests) }