* fix(configfile): carry graph_mode and graph_* binding through Save Graph-preview builds of bd persist a per-workspace format in metadata.json: "graph_mode": "link" plus graph_scope_url, graph_authority_id, graph_workspace, graph_schema_version and graph_ready, beside an ordinary Dolt backend. configfile.Config had no such fields, so every Save, and Load's config.json -> metadata.json migration, rewrote the file without them, turning a graph workspace's metadata into an ordinary Issue workspace's. Config now carries the marker and the binding as omitempty fields, so a Save round-trips them unchanged and metadata.json written for an ordinary workspace is byte-for-byte what it was. GetGraphMode resolves the marker: absent or "dependency" is the historical default, "link" is recognized, and any other value is an error rather than a fallback to the Issue store. Nothing in this build opens a non-default mode; the CLI refusal follows in the next commit. Ported from the donnabox fork commit deea8d57df (graph_mode.go, graph_mode_test.go and the configfile.go fields), without any graph code. The binding-survives-Save and omitted-from-ordinary-metadata tests are new. Co-authored-by: Donna Box <dbox@boxenllc.com> Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(cli): refuse graph_mode workspaces before opening them A workspace whose metadata.json declares a graph_mode other than "dependency" holds a graph database behind an ordinary Dolt backend, and nothing else in its metadata says so. bd admitted it like any current workspace: `bd list` ran the Issue schema initialization against the graph database (beads_graph_<id>), `bd init --force` wrote a .gitignore and a .beads/dolt root into it, `bd dolt start` started a server for it, and `bd doctor` reported on it as an Issue store. guardLegacyUpgradeWorkspace, the admission check every store-opening command, store-less command with workspace effects, init, doctor and bootstrap already run before touching a workspace, now refuses such a workspace first, before the legacy classifications that would admit it or report it as a legacy store to migrate. An unknown graph_mode value is refused the same way. The refusal is a typed error naming the workspace and the format, rendered with a hint (as JSON under --json) and exit code 16, ExitUnsupportedWorkspaceFormat, so a wrapper can tell "needs a different bd build" from a generic failure. `bd version`, `bd --version`, `bd help` and completion stay exempt, as they are for legacy workspaces. validateConfiguredBackend refuses the same metadata, which covers the store-open paths that skip command admission (routed writes, cross-repository reads, create --repo). Tests: guard and store-factory unit tests; a subprocess e2e test that show/list/create/context/where/dolt start/doctor/bootstrap/init are refused with exit 16 and leave .beads byte-identical (metadata.json and legacy config.json, link and unknown markers, embedded and server layouts), and that store-less commands still run; an embedded test that an ordinary workspace, with the marker absent or "dependency", still creates and lists issues and keeps the marker through a metadata.json rewrite. The guard hunk and graph_mode_admission_e2e_test.go are ported from the donnabox fork commit deea8d57df; the exit code, rendering, store factory backstop and remaining tests are new. Co-authored-by: Donna Box <dbox@boxenllc.com> Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(cli): refuse graph workspaces reached outside command admission Command admission refuses a graph_mode workspace a command runs in, but bd also reaches workspaces before or without admission: a create --repo target, the path --db or BEADS_DB names, and the stores the factories open for routed and cross-repository work. On those paths a workspace marked only by a legacy config.json was changed before anything refused it: configfile.Load migrates config.json into metadata.json before the format check sees the loaded metadata, and create --repo initialized such a target as a new workspace. Where the refusal did come, create reported it with exit 1 rather than 16. - checkDiscoveredWorkspaceFormat checks a workspace's metadata as configfile.LoadForDiscovery reads it, which never rewrites it. ensureBeadsDirForPath, the dry-run target open and the four store factories (cgo and nocgo builds) run it before their own load. - resolveBeadsDirForDBPath and findDatabaseInBeadsDir locate a workspace with LoadForDiscovery, so resolving --db, BEADS_DB or a create target no longer migrates it. - bd create renders a refused target as the workspace refusal, exit 16 with its hint, on the initializing and both dry-run paths. A dry run without --parent opens no target, so it now checks a local one before rendering the preview (checkRepoTargetFormat). - bd bootstrap, when discovery finds no workspace, reads each .beads from the working directory up to the repository root without migrating it and refuses one in a format this build cannot open, the way commands without a workspace already do. A .beads marked only by config.json is passed over by discovery, so bootstrap reported "no workspace" there instead of exit 16. The walk now returns its candidates (undiscoveredWorkspaceCandidates) to both checks: handing it the check as a func(string) error made gosec's G702 taint analysis connect BEADS_DIR to every func(string) error that runs git. - backendNameForErrorFraming reads with LoadForDiscovery, and loadDoltBackendConfig, bd dolt status and bd dolt show refuse a graph workspace before their migrating configfile.Load. Admission refuses first on every path that reaches them today; this keeps a new caller from migrating a legacy config.json before the refusal. - The CHANGELOG entry and the configfile comment now claim what holds: a routed read fails without touching a graph workspace but not with code 16, and a Save keeps only the known graph_* fields, with the tags graph-preview builds declare. workspace_gate.go no longer lists the pre-gate discovery reads as migrating. - New e2e rows run create --repo from a workspace and from outside one, create --dry-run with and without --parent, --db, BEADS_DB, init --proxied-server, bootstrap in a workspace nested in a graph one, and the hook shim's bd hooks run pre-commit against every fixture; each refusal must exit 16 and name the format and the hint. bootstrap in a workspace marked only by config.json now refuses like the other commands. The store factory test also runs on a config.json graph workspace, TestWorkspaceMetadataReadersLeaveGraphWorkspaceUnchanged covers the framing, bd dolt loader and diagnostic reads, and TestNocgoStoreFactoriesRefuseGraphWorkspace covers the CGO_ENABLED=0 factories; bazel.yml's pure lane runs it by name (PURE_CMD_BD_TESTS, pinned in scripts/ci_workflow_test.go). Measured with gocyclo/gocognit: every function this changes stays below the PR's worst changed function (guardLegacyUpgradeWorkspaceForInit 29/28, resolveBeadsDirForDBPath 26/36); showDoltConfig is 19/31 (was 18/30) and guardUndiscoveredLegacyWorkspace 3/3 (was 9/12). Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Donna Box <dbox@boxenllc.com> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com>
259 lines
10 KiB
Go
259 lines
10 KiB
Go
//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)
|
|
}
|
|
}
|