diff --git a/cmd/bd/admin.go b/cmd/bd/admin.go index 1817de70c..8636c3265 100644 --- a/cmd/bd/admin.go +++ b/cmd/bd/admin.go @@ -9,11 +9,11 @@ import ( // requireServerMode returns an error if bd is running in embedded mode. // Used by admin subcommands to guard against embedded-mode execution. // This must be called inside RunE (after store init), not in PersistentPreRunE, -// because isEmbeddedMode() reads cmdCtx.ServerMode which is only populated +// because !usesSQLServer() reads cmdCtx.ServerMode which is only populated // after main.go's PersistentPreRun completes (cobra runs child PreRunE before // parent PreRun, so the check fires too early if placed on adminCmd itself). func requireServerMode(cmdName string) error { - if isEmbeddedMode() { + if !usesSQLServer() { return fmt.Errorf("'bd admin %s' is not yet supported in embedded mode", cmdName) } return nil diff --git a/cmd/bd/bootstrap.go b/cmd/bd/bootstrap.go index fe3f1ea50..c8dd65159 100644 --- a/cmd/bd/bootstrap.go +++ b/cmd/bd/bootstrap.go @@ -458,7 +458,7 @@ func printBootstrapPlan(plan BootstrapPlan) { switch plan.Action { case "none": fmt.Printf("✓ Database already exists: %s\n", plan.BeadsDir) - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Printf(" Nothing to do.\n") } else { fmt.Printf(" Nothing to do. Use 'bd doctor' to check health.\n") @@ -666,9 +666,15 @@ func finalizeSyncedBootstrap(beadsDir, syncRemote string, cfg *configfile.Config // required by configfile.Load consumers. cfg.Backend = configfile.BackendDolt cfg.DoltDatabase = dbName - if cfg.IsDoltServerMode() || doltserver.IsSharedServerMode() { + switch { + case cfg.IsDoltProxiedServerMode(): + // Preserve — the upstream cfg already carried proxied-server intent + // (e.g. propagated from a parent workspace or set during the + // proxied-server clone path). + cfg.DoltMode = configfile.DoltModeProxiedServer + case cfg.IsDoltServerMode() || doltserver.IsSharedServerMode(): cfg.DoltMode = configfile.DoltModeServer - } else { + default: cfg.DoltMode = configfile.DoltModeEmbedded } // Mirror init's convention: metadata.json database points at the Dolt diff --git a/cmd/bd/context.go b/cmd/bd/context.go index 1e0c08d32..3c8087199 100644 --- a/cmd/bd/context.go +++ b/cmd/bd/context.go @@ -32,6 +32,12 @@ type CommandContext struct { // Storage mode — true when connected to an external dolt sql-server. ServerMode bool + // ProxiedServer — true when this workspace uses the per-workspace + // proxied dolt sql-server (a parent proxy + child dolt sql-server, + // both rooted at /proxieddb). Mutually exclusive with + // embedded mode and ServerMode. + ProxiedServer bool + // Runtime state Store storage.DoltStorage RootCtx context.Context @@ -363,6 +369,10 @@ func syncCommandContext() { cmdCtx.Verbose = verboseFlag cmdCtx.Quiet = quietFlag + // Storage mode + cmdCtx.ServerMode = serverMode + cmdCtx.ProxiedServer = proxiedServerMode + // Runtime state cmdCtx.Store = store cmdCtx.RootCtx = rootCtx diff --git a/cmd/bd/context_cmd.go b/cmd/bd/context_cmd.go index e2f3dc3c4..189dc1cb5 100644 --- a/cmd/bd/context_cmd.go +++ b/cmd/bd/context_cmd.go @@ -21,6 +21,7 @@ type ContextInfo struct { DoltMode string `json:"dolt_mode"` ServerHost string `json:"server_host,omitempty"` ServerPort int `json:"server_port,omitempty"` + ProxiedDir string `json:"proxied_dir,omitempty"` Database string `json:"database"` DataDir string `json:"data_dir,omitempty"` ProjectID string `json:"project_id,omitempty"` @@ -97,6 +98,9 @@ Examples: dsCfg := doltserver.DefaultConfig(rc.BeadsDir) info.ServerPort = dsCfg.Port } + if cfg.IsDoltProxiedServerMode() { + info.ProxiedDir = proxiedServerRoot(rc.BeadsDir) + } if dataDir := cfg.GetDoltDataDir(); dataDir != "" { info.DataDir = dataDir @@ -146,6 +150,9 @@ func printContextText(info ContextInfo) { if info.ServerHost != "" { fmt.Printf(" server: %s:%d\n", info.ServerHost, info.ServerPort) } + if info.ProxiedDir != "" { + fmt.Printf(" proxied dir: %s\n", info.ProxiedDir) + } if info.DataDir != "" { fmt.Printf(" data dir: %s\n", info.DataDir) } diff --git a/cmd/bd/create.go b/cmd/bd/create.go index dd3b93852..cbf9a3b5a 100644 --- a/cmd/bd/create.go +++ b/cmd/bd/create.go @@ -671,7 +671,7 @@ var createCmd = &cobra.Command{ // a separate commit. In EmbeddedDoltStore mode, CreateIssue writes // to the working set without a Dolt commit, so we always commit // everything together at the end. - if isEmbeddedMode() || postCreateWrites { + if !usesSQLServer() || postCreateWrites { commitMsg := fmt.Sprintf("bd: create %s", issue.ID) if err := store.Commit(ctx, commitMsg); err != nil && !isDoltNothingToCommit(err) { WarnError("failed to commit: %v", err) diff --git a/cmd/bd/dep.go b/cmd/bd/dep.go index ac1c9ca55..673813cf4 100644 --- a/cmd/bd/dep.go +++ b/cmd/bd/dep.go @@ -158,7 +158,7 @@ Examples: warnIfCyclesExist(fromStore) } - if isEmbeddedMode() && fromStore != nil { + if !usesSQLServer() && fromStore != nil { if err := fromStore.Commit(ctx, fmt.Sprintf("bd: dep add (auto-commit) by %s", actor)); err != nil && !isDoltNothingToCommit(err) { FatalErrorRespectJSON("failed to commit: %v", err) } @@ -340,7 +340,7 @@ Examples: warnIfCyclesExist(fromStore) } - if isEmbeddedMode() && fromStore != nil { + if !usesSQLServer() && fromStore != nil { if err := fromStore.Commit(ctx, fmt.Sprintf("bd: dep add (auto-commit) by %s", actor)); err != nil && !isDoltNothingToCommit(err) { FatalErrorRespectJSON("failed to commit: %v", err) } @@ -862,7 +862,7 @@ var depRemoveCmd = &cobra.Command{ FatalErrorRespectJSON("%v", err) } - if isEmbeddedMode() && fromStore != nil { + if !usesSQLServer() && fromStore != nil { if err := fromStore.Commit(ctx, fmt.Sprintf("bd: dep remove (auto-commit) by %s", actor)); err != nil && !isDoltNothingToCommit(err) { FatalErrorRespectJSON("failed to commit: %v", err) } diff --git a/cmd/bd/doctor.go b/cmd/bd/doctor.go index 12b17b4b8..d02086d54 100644 --- a/cmd/bd/doctor.go +++ b/cmd/bd/doctor.go @@ -184,7 +184,7 @@ Examples: bd doctor --migration=post # Validate Dolt migration completed bd doctor --migration=pre --json # Machine-parseable migration validation`, Run: func(cmd *cobra.Command, args []string) { - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Fprintln(os.Stderr, "Note: 'bd doctor' is not yet supported in embedded mode.") fmt.Fprintln(os.Stderr, "") fmt.Fprintln(os.Stderr, "For embedded mode troubleshooting:") @@ -194,6 +194,10 @@ Examples: fmt.Fprintln(os.Stderr, " • Switch to server mode: bd init --server") os.Exit(0) } + if usesProxiedServer() { + fmt.Fprintln(os.Stderr, "Note: 'bd doctor' is not yet supported in proxied-server mode.") + os.Exit(0) + } // Use global jsonOutput set by PersistentPreRun // Determine path to check diff --git a/cmd/bd/doctor/gitignore.go b/cmd/bd/doctor/gitignore.go index fbf2ee8cc..51f0fc763 100644 --- a/cmd/bd/doctor/gitignore.go +++ b/cmd/bd/doctor/gitignore.go @@ -12,6 +12,9 @@ import ( const GitignoreTemplate = `# Dolt database (managed by Dolt, not git) dolt/ embeddeddolt/ +# Per-workspace proxied dolt sql-server (server config + log + child .dolt repo, +# all host-specific including the bound port baked into server_config.yaml). +proxieddb/ # Runtime files bd.sock @@ -87,6 +90,10 @@ var ProjectGitignorePatterns = []string{ ".dolt/", "*.db", ".beads-credential-key", + // Per-workspace proxied dolt sql-server. Belt-and-suspenders: .beads/.gitignore + // already ignores proxieddb/ in-tree, but pinning it at the project root + // keeps it ignored even if .beads/.gitignore is missing. + ".beads/proxieddb/", } // projectGitignoreComment is the section header added to the project .gitignore @@ -104,6 +111,7 @@ var requiredPatterns = []string{ "export-state.json", "dolt/", "embeddeddolt/", + "proxieddb/", "ephemeral.sqlite3", "dolt-server.pid", "dolt-server.log", diff --git a/cmd/bd/doctor/gitignore_test.go b/cmd/bd/doctor/gitignore_test.go index e5fb22029..b1191e203 100644 --- a/cmd/bd/doctor/gitignore_test.go +++ b/cmd/bd/doctor/gitignore_test.go @@ -1770,7 +1770,7 @@ func TestCheckProjectGitignore_AllPresent(t *testing.T) { } }() - content := "node_modules/\n.dolt/\n*.db\n.beads-credential-key\n" + content := "node_modules/\n.dolt/\n*.db\n.beads-credential-key\n.beads/proxieddb/\n" if err := os.WriteFile(".gitignore", []byte(content), 0644); err != nil { t.Fatal(err) } diff --git a/cmd/bd/dolt.go b/cmd/bd/dolt.go index 534ed61fd..ad957b929 100644 --- a/cmd/bd/dolt.go +++ b/cmd/bd/dolt.go @@ -100,7 +100,7 @@ Examples: bd dolt set data-dir /home/user/.beads-dolt/myproject`, Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Fprintln(os.Stderr, "Error: 'bd dolt set' is not supported in embedded mode (no Dolt server)") os.Exit(1) } @@ -122,7 +122,7 @@ This verifies that: Use this before switching to server mode to ensure the server is running.`, Run: func(cmd *cobra.Command, args []string) { - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Fprintln(os.Stderr, "Error: 'bd dolt test' is not supported in embedded mode (no Dolt server)") os.Exit(1) } @@ -366,7 +366,7 @@ project path. PID and logs are stored in .beads/. The server auto-starts transparently when needed, so manual start is rarely required. Use this command for explicit control or diagnostics.`, Run: func(cmd *cobra.Command, args []string) { - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Fprintln(os.Stderr, "Error: 'bd dolt start' is not supported in embedded mode (no Dolt server)") os.Exit(1) } @@ -403,7 +403,7 @@ var doltStopCmd = &cobra.Command{ This sends a graceful shutdown signal. The server will restart automatically on the next bd command unless auto-start is disabled.`, Run: func(cmd *cobra.Command, args []string) { - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Fprintln(os.Stderr, "Error: 'bd dolt stop' is not supported in embedded mode (no Dolt server)") os.Exit(1) } @@ -438,7 +438,7 @@ and database.`, if beadsDir == "" { FatalErrorWithHint(activeWorkspaceNotFoundError(), diagHint()) } - if isEmbeddedMode() { + if !usesSQLServer() { showEmbeddedDoltStatus(beadsDir) return } @@ -622,7 +622,7 @@ In standalone mode, only dolt sql-server processes using the current project's Dolt data directory are eligible for cleanup. Other projects' servers are preserved.`, Run: func(cmd *cobra.Command, args []string) { - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Fprintln(os.Stderr, "Error: 'bd dolt killall' is not supported in embedded mode (no Dolt server)") os.Exit(1) } @@ -667,7 +667,7 @@ Stale database prefixes: testdb_*, doctest_*, doctortest_*, beads_pt*, beads_vr* These waste server memory and can degrade performance under concurrent load. Use --dry-run to see what would be dropped without actually dropping.`, Run: func(cmd *cobra.Command, args []string) { - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Fprintln(os.Stderr, "Error: 'bd dolt clean-databases' is not supported in embedded mode (no Dolt server)") os.Exit(1) } @@ -856,7 +856,7 @@ var doltRemoteAddCmd = &cobra.Command{ os.Exit(1) } dbPath := locator.CLIDir() - embedded := isEmbeddedMode() + embedded := !usesSQLServer() // Check existing remotes on both surfaces sqlRemotes, _, cliRemotes, _ := listRemoteSurfaces(ctx, st, dbPath, embedded) @@ -961,7 +961,7 @@ var doltRemoteListCmd = &cobra.Command{ os.Exit(1) } dbPath := locator.CLIDir() - embedded := isEmbeddedMode() + embedded := !usesSQLServer() sqlRemotes, sqlErr, cliRemotes, cliErr := listRemoteSurfaces(ctx, st, dbPath, embedded) if sqlErr != nil { @@ -1080,7 +1080,7 @@ var doltRemoteRemoveCmd = &cobra.Command{ os.Exit(1) } dbPath := locator.CLIDir() - embedded := isEmbeddedMode() + embedded := !usesSQLServer() // Check both surfaces for conflicts sqlRemotes, _, cliRemotes, _ := listRemoteSurfaces(ctx, st, dbPath, embedded) @@ -1231,7 +1231,7 @@ func showDoltConfig(testConnection bool) { } backend := cfg.GetBackend() - embedded := isEmbeddedMode() + embedded := !usesSQLServer() // Resolve actual server port for connection testing showHost := cfg.GetDoltServerHost() diff --git a/cmd/bd/dolt_autostart_lifecycle_integration_test.go b/cmd/bd/dolt_autostart_lifecycle_integration_test.go index bd907f6f4..a9c24f7d4 100644 --- a/cmd/bd/dolt_autostart_lifecycle_integration_test.go +++ b/cmd/bd/dolt_autostart_lifecycle_integration_test.go @@ -18,7 +18,7 @@ func TestE2E_AutoStartedRepoLocalServerPersistsAcrossCommands(t *testing.T) { if testing.Short() { t.Skip("skipping slow integration test in short mode") } - if isEmbeddedMode() { + if !usesSQLServer() { t.Skip("skipping: bd dolt status not supported in embedded mode") } if runtime.GOOS == windowsOS { diff --git a/cmd/bd/errors.go b/cmd/bd/errors.go index 4e90af1f2..85de69255 100644 --- a/cmd/bd/errors.go +++ b/cmd/bd/errors.go @@ -27,12 +27,12 @@ func whereDiagHint() string { func workspaceDiagHint(includeWhere bool) string { if includeWhere { - if isEmbeddedMode() { + if !usesSQLServer() { return "run 'bd where' to inspect the resolved workspace, or 'bd init' to create a new database" } return "run 'bd where' to inspect the resolved workspace, run 'bd doctor' to diagnose, or 'bd init' to create a new database" } - if isEmbeddedMode() { + if !usesSQLServer() { return "check BEADS_DIR/worktree setup, or run 'bd init' to create a new database" } return "check BEADS_DIR/worktree setup, run 'bd doctor' to diagnose, or run 'bd init' to create a new database" diff --git a/cmd/bd/init.go b/cmd/bd/init.go index f86cee7f0..99ffb37cc 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -52,6 +52,11 @@ Pass --server to use an external dolt sql-server instead. In server mode, set connection details with --server-host, --server-port, and --server-user. Password should be set via BEADS_DOLT_PASSWORD environment variable. +Pass --proxied-server to use a per-workspace proxied dolt sql-server: bd +spawns a parent proxy + child dolt sql-server rooted at .beads/proxieddb/ +and manages their lifecycle for you. No host/port flags apply — bd picks a +free port and bakes it into .beads/proxieddb/server_config.yaml. + Auto-export is enabled by default. After every write command, bd exports issues to .beads/issues.jsonl (throttled to once per 60s). This keeps viewers (bv) and git-based workflows up to date without extra steps. @@ -101,6 +106,24 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): } sharedServer, _ := cmd.Flags().GetBool("shared-server") externalServer, _ := cmd.Flags().GetBool("external") + initProxiedServer, _ := cmd.Flags().GetBool("proxied-server") + // BEADS_DOLT_PROXIED_SERVER=1 mirrors BEADS_DOLT_SERVER_MODE=1 — lets + // orchestrators select the per-workspace proxied dolt sql-server + // without a CLI flag. + if os.Getenv("BEADS_DOLT_PROXIED_SERVER") == "1" { + initProxiedServer = true + } + // --proxied-server owns its own connection details (proxy + child dolt + // at /proxieddb, port baked into proxieddb-config.yaml). + // Combining it with any of the externally-managed server flags is + // ambiguous: the user's intent is unclear and we want to fail fast + // rather than silently pick one. + if initProxiedServer { + if initServerMode || sharedServer || externalServer || + serverHost != "" || serverPort != 0 || serverSocket != "" || serverUser != "" { + FatalError("--proxied-server cannot be combined with --server, --shared-server, --external, or any --server-* flag") + } + } // Handle --backend flag: "dolt" is the only supported backend. // "sqlite" is accepted for backward compatibility but prints a @@ -163,12 +186,14 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): initServerMode = true } - // Set serverMode so isEmbeddedMode() returns the correct value. + // Set serverMode so !usesSQLServer() returns the correct value. // Both the global and cmdCtx must be set because PersistentPreRun // creates a fresh cmdCtx (with ServerMode=false) before Run executes. serverMode = initServerMode + proxiedServerMode = initProxiedServer if cmdCtx != nil { cmdCtx.ServerMode = initServerMode + cmdCtx.ProxiedServer = initProxiedServer } // Propagate --shared-server flag to env so that IsSharedServerMode(), @@ -179,9 +204,9 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): } // Reject hyphens in --database for embedded mode. Must run AFTER - // serverMode is set above — otherwise isEmbeddedMode() always returns + // serverMode is set above — otherwise !usesSQLServer() always returns // true and incorrectly rejects server-mode names (GH#3231). - if database != "" && strings.ContainsRune(database, '-') && isEmbeddedMode() { + if database != "" && strings.ContainsRune(database, '-') && !usesSQLServer() { FatalError("database name %q contains hyphens which are invalid in embedded mode; use underscores instead (e.g. %q)", database, sanitizeDBName(database)) } @@ -694,6 +719,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): Database: dbName, ServerPort: initPort, ServerMode: initServerMode, + ProxiedServer: initProxiedServer, CreateIfMissing: true, // bd init is the only path that should create databases AutoStart: initServerMode && os.Getenv("BEADS_DOLT_AUTO_START") != "0", } @@ -710,7 +736,10 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): doltCfg.ServerUser = serverUser } - initLock, err := acquireEmbeddedLock(beadsDir, initServerMode) + // Proxied-server takes its own per-workspace lock under + // /proxieddb/proxy-child.lock; the embedded flock at + // .beads/embeddeddolt/.lock is irrelevant in that mode. + initLock, err := acquireEmbeddedLock(beadsDir, initServerMode || initProxiedServer) if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) @@ -770,6 +799,9 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): fmt.Fprintf(os.Stderr, "Error: failed to open Dolt store: %v\n", err) os.Exit(1) } + if initProxiedServer && !quiet { + fmt.Printf(" %s Proxied dolt sql-server at %s\n", ui.RenderPass("✓"), proxiedServerRoot(beadsDir)) + } // Initialize global database schema and config in shared-server mode. // Opens a separate store connection to beads_global with CreateIfMissing @@ -923,22 +955,31 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): } // Persist the connection mode matching this build. - if isEmbeddedMode() { - cfg.DoltMode = configfile.DoltModeEmbedded - } else { + switch { + case usesProxiedServer(): + cfg.DoltMode = configfile.DoltModeProxiedServer + case usesSQLServer(): cfg.DoltMode = configfile.DoltModeServer + default: + cfg.DoltMode = configfile.DoltModeEmbedded } - if serverHost != "" { - cfg.DoltServerHost = serverHost - } - if serverPort != 0 { - cfg.DoltServerPort = serverPort - } - if serverSocket != "" { - cfg.DoltServerSocket = serverSocket - } - if serverUser != "" { - cfg.DoltServerUser = serverUser + // Server connection fields are only meaningful for the + // externally-managed server backend (dolt_mode=server). + // Proxied-server owns its own connection details under + // /proxieddb; embedded has none. + if !usesProxiedServer() { + if serverHost != "" { + cfg.DoltServerHost = serverHost + } + if serverPort != 0 { + cfg.DoltServerPort = serverPort + } + if serverSocket != "" { + cfg.DoltServerSocket = serverSocket + } + if serverUser != "" { + cfg.DoltServerUser = serverUser + } } } @@ -1347,7 +1388,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): fmt.Printf("\n%s bd initialized successfully!\n\n", ui.RenderPass("✓")) } fmt.Printf(" Backend: %s\n", ui.RenderAccent(backend)) - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Printf(" Mode: %s\n", ui.RenderAccent("embedded")) } else { host := serverHost @@ -1390,7 +1431,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1): // Skipped in embedded mode: diagnostics use dolt.NewFromConfigWithOptions // which auto-starts a dolt sql-server. Embedded init already validates // the database via initSchema. - if !isEmbeddedMode() { + if usesSQLServer() { doctorResult := runInitDiagnostics(cwd) hasIssues := false for _, check := range doctorResult.Checks { @@ -1447,6 +1488,7 @@ func init() { initCmd.Flags().String("database", "", "Use existing server database name (overrides prefix-based naming)") initCmd.Flags().Bool("shared-server", false, "Enable shared Dolt server mode (all projects share one server at ~/.beads/shared-server/)") initCmd.Flags().Bool("external", false, "Server is externally managed (skip server startup); use with --shared-server or --server") + initCmd.Flags().Bool("proxied-server", false, "Use a per-workspace proxied dolt sql-server (proxy + child dolt) rooted at .beads/proxieddb") rootCmd.AddCommand(initCmd) } @@ -1521,6 +1563,35 @@ func checkExistingBeadsDataAt(beadsDir string, prefix string) error { // Check for existing Dolt database if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.GetBackend() == configfile.BackendDolt { + // Proxied-server mode stores its database under + // `.beads/proxieddb//`. Check that path first because + // IsDoltProxiedServerMode() and IsDoltServerMode() are mutually + // exclusive — we don't want to fall through into the embedded + // branch and miss an existing proxied database. + if cfg.IsDoltProxiedServerMode() { + dbName := cfg.GetDoltDatabase() + if dbName != "" { + dbDoltDir := filepath.Join(proxiedServerRoot(beadsDir), dbName, ".dolt") + if info, statErr := os.Stat(dbDoltDir); statErr == nil && info.IsDir() { + location := filepath.Join(proxiedServerRoot(beadsDir), dbName) + return fmt.Errorf(` +%s Found existing Dolt database: %s + +This workspace is already initialized. + +To use the existing database: + Just run bd commands normally (e.g., %s) + +If the database is genuinely corrupt and unrecoverable: + bd export > backup.jsonl # Back up first! + bd init --reinit-local --prefix %s # Then reinitialize + +Aborting.`, ui.RenderWarn("⚠"), location, ui.RenderAccent("bd list"), prefix) + } + } + return nil + } + // Embedded mode stores databases under `.beads/embeddeddolt//`. // Use the target workspace metadata rather than ambient process state so // init guards remain deterministic even when another test or earlier @@ -1953,7 +2024,7 @@ func isEmptyRemoteCloneError(err error) bool { func verifyMetadata(ctx context.Context, store storage.DoltStorage, key, value string) bool { if err := store.SetMetadata(ctx, key, value); err != nil { fmt.Fprintf(os.Stderr, "Warning: failed to write %s metadata: %v\n", key, err) - if !isEmbeddedMode() { + if usesSQLServer() { fmt.Fprintf(os.Stderr, " Run 'bd doctor --fix' to repair.\n") } return false @@ -1962,7 +2033,7 @@ func verifyMetadata(ctx context.Context, store storage.DoltStorage, key, value s readBack, err := store.GetMetadata(ctx, key) if err != nil || readBack != value { fmt.Fprintf(os.Stderr, "Warning: %s metadata write did not persist (wrote %q, read %q)\n", key, value, readBack) - if !isEmbeddedMode() { + if usesSQLServer() { fmt.Fprintf(os.Stderr, " Run 'bd doctor --fix' to repair.\n") } return false diff --git a/cmd/bd/init_git_hooks.go b/cmd/bd/init_git_hooks.go index 336046428..3223e4489 100644 --- a/cmd/bd/init_git_hooks.go +++ b/cmd/bd/init_git_hooks.go @@ -161,7 +161,7 @@ func installGitHooks() error { oldPath := hook.path + ".old" if err := os.Rename(hook.path, oldPath); err != nil { fmt.Fprintf(os.Stderr, "%s Failed to chain with existing %s hook: %v\n", ui.RenderWarn("⚠"), hook.name, err) - if !isEmbeddedMode() { + if usesSQLServer() { fmt.Fprintf(os.Stderr, "You can resolve this with: %s\n", ui.RenderAccent("bd doctor --fix")) } continue diff --git a/cmd/bd/link.go b/cmd/bd/link.go index 3eb76bde0..a87e7c9ed 100644 --- a/cmd/bd/link.go +++ b/cmd/bd/link.go @@ -68,7 +68,7 @@ Examples: // Check for cycles after adding dependency warnIfCyclesExist(fromStore) - if isEmbeddedMode() && fromStore != nil { + if !usesSQLServer() && fromStore != nil { if err := fromStore.Commit(ctx, fmt.Sprintf("bd: link (auto-commit) by %s", actor)); err != nil && !isDoltNothingToCommit(err) { FatalErrorRespectJSON("failed to commit: %v", err) } diff --git a/cmd/bd/main.go b/cmd/bd/main.go index 291524caa..ddefc90c1 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -66,17 +66,18 @@ type envSnapshotValue struct { var changeDirEnvSnapshot map[string]envSnapshotValue var ( - sandboxMode bool - globalFlag bool // Use the global shared-server database (beads_global) - serverMode bool // True when using external dolt sql-server (dolt_mode=server) - readonlyMode bool // Read-only mode: block write operations (for worker sandboxes) - storeIsReadOnly bool // Track if store was opened read-only (for staleness checks) - lockTimeout = 30 * time.Second // Dolt open timeout (fixed default) - profileEnabled bool - profileFile *os.File - traceFile *os.File - verboseFlag bool // Enable verbose/debug output - quietFlag bool // Suppress non-essential output + sandboxMode bool + globalFlag bool // Use the global shared-server database (beads_global) + serverMode bool // True when using external dolt sql-server (dolt_mode=server) + proxiedServerMode bool // True when using per-workspace proxied dolt sql-server (dolt_mode=proxied-server) + readonlyMode bool // Read-only mode: block write operations (for worker sandboxes) + storeIsReadOnly bool // Track if store was opened read-only (for staleness checks) + lockTimeout = 30 * time.Second // Dolt open timeout (fixed default) + profileEnabled bool + profileFile *os.File + traceFile *os.File + verboseFlag bool // Enable verbose/debug output + quietFlag bool // Suppress non-essential output // Dolt auto-commit policy (flag/config). Values: off | on doltAutoCommit string @@ -207,6 +208,11 @@ func repairSharedServerEmbeddedMismatch(beadsDir string, cfg *configfile.Config) if cfg == nil { return } + // This repair only applies when metadata.json explicitly says "embedded". + // Proxied-server (dolt_mode=proxied-server) and any other non-empty value + // fall through this guard untouched — we never rewrite a deliberately-set + // mode to "server" just because the shared-server env var happens to be + // active. if strings.ToLower(strings.TrimSpace(cfg.DoltMode)) != configfile.DoltModeEmbedded { return } @@ -223,9 +229,9 @@ func repairSharedServerEmbeddedMismatch(beadsDir string, cfg *configfile.Config) } } -// loadServerModeFromBeadsDir loads the storage mode (embedded vs server) from -// the given beads directory's metadata.json so that isEmbeddedMode() returns -// the correct value. +// loadServerModeFromBeadsDir loads the storage mode (embedded vs server vs +// proxied-server) from the given beads directory's metadata.json so that +// usesSQLServer() and usesProxiedServer() return the correct values. func loadServerModeFromBeadsDir(beadsDir string) { if beadsDir == "" { return @@ -235,20 +241,24 @@ func loadServerModeFromBeadsDir(beadsDir string) { return } repairSharedServerEmbeddedMismatch(beadsDir, cfg) + psm := cfg.IsDoltProxiedServerMode() sm := cfg.IsDoltServerMode() // GH#2946: shared-server override for stale metadata.json (no-db commands) - if !sm && doltserver.IsSharedServerMode() { + if !sm && !psm && doltserver.IsSharedServerMode() { sm = true } serverMode = sm + proxiedServerMode = psm if cmdCtx != nil { cmdCtx.ServerMode = sm + cmdCtx.ProxiedServer = psm } } -// loadServerModeFromConfig loads the storage mode (embedded vs server) from -// metadata.json so that isEmbeddedMode() returns the correct value. Called -// for commands that skip full DB init but still need to know the mode. +// loadServerModeFromConfig loads the storage mode (embedded vs server vs +// proxied-server) from metadata.json so that usesSQLServer() and +// usesProxiedServer() return the correct values. Called for commands that +// skip full DB init but still need to know the mode. func loadServerModeFromConfig() { loadServerModeFromBeadsDir(beads.FindBeadsDir()) } @@ -917,11 +927,23 @@ var rootCmd = &cobra.Command{ fmt.Fprintf(os.Stderr, "warning: failed to load beads config from %s: %v\n", beadsDir, cfgErr) } if cfg != nil { + // Proxied-server is mutually exclusive with the externally-managed + // server backend, so resolve it first and let the server-mode + // block below stay false when proxied is active. The store-factory + // dispatch (newDoltStore) keys off cfg.ProxiedServer ahead of + // cfg.ServerMode for the same reason. + doltCfg.ProxiedServer = cfg.IsDoltProxiedServerMode() + proxiedServerMode = doltCfg.ProxiedServer + if cmdCtx != nil { + cmdCtx.ProxiedServer = doltCfg.ProxiedServer + } + doltCfg.ServerMode = cfg.IsDoltServerMode() // Shared server mode (dolt.shared-server in config.yaml) is a // form of server mode. Override metadata.json if it still says - // embedded — handles installs created before GH#2946 fix. - if !doltCfg.ServerMode && doltserver.IsSharedServerMode() { + // embedded — handles installs created before GH#2946 fix. Skip + // this for proxied-server: it's its own backend, not server. + if !doltCfg.ServerMode && !doltCfg.ProxiedServer && doltserver.IsSharedServerMode() { doltCfg.ServerMode = true } serverMode = doltCfg.ServerMode @@ -982,7 +1004,7 @@ var rootCmd = &cobra.Command{ // - Embedded mode: ON — each command writes to the working set and needs // a Dolt commit in PersistentPostRun to persist changes to history. if strings.TrimSpace(doltAutoCommit) == "" { - if isEmbeddedMode() { + if !usesSQLServer() { doltAutoCommit = string(doltAutoCommitOn) } else { doltAutoCommit = string(doltAutoCommitOff) diff --git a/cmd/bd/proxied_server.go b/cmd/bd/proxied_server.go new file mode 100644 index 000000000..ca4228eda --- /dev/null +++ b/cmd/bd/proxied_server.go @@ -0,0 +1,196 @@ +package main + +import ( + "context" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + "github.com/dolthub/dolt/go/libraries/doltcore/servercfg" + "gopkg.in/yaml.v3" + + "github.com/steveyegge/beads/internal/config" + "github.com/steveyegge/beads/internal/storage" + "github.com/steveyegge/beads/internal/storage/db/proxy" + "github.com/steveyegge/beads/internal/storage/dolt" + proxieddolt "github.com/steveyegge/beads/internal/storage/doltserver" +) + +// Filesystem layout for --proxied-server (per workspace, all under one dir): +// +// /proxieddb/ proxy lockfile, pidfile, +// child dolt repository +// /proxieddb/server_config.yaml dolt sql-server YAML +// (host / port / log_level) +// /proxieddb/server.log daemon stdout/stderr +// +// The whole directory is owned by the per-workspace proxied dolt sql-server +// feature and is gitignored (the bound port is host-specific). +const ( + proxiedServerRootName = "proxieddb" + proxiedServerConfigName = "server_config.yaml" + proxiedServerLogName = "server.log" +) + +// proxiedServerRoot returns /proxieddb — the rootDir for the proxy +// lockfile/pidfile, the child dolt repository, and the server config + log. +func proxiedServerRoot(beadsDir string) string { + return filepath.Join(beadsDir, proxiedServerRootName) +} + +// proxiedServerConfigPath returns /proxieddb/server_config.yaml — +// the YAML config file consumed by dolt sql-server (parsed via +// servercfg.YamlConfigFromFile in internal/storage/db/server/doltserver.go). +func proxiedServerConfigPath(beadsDir string) string { + return filepath.Join(proxiedServerRoot(beadsDir), proxiedServerConfigName) +} + +// proxiedServerLogPath returns /proxieddb/server.log — where the +// dolt sql-server daemon writes stdout/stderr. +func proxiedServerLogPath(beadsDir string) string { + return filepath.Join(proxiedServerRoot(beadsDir), proxiedServerLogName) +} + +// ensureProxiedServerConfig returns the path to the proxied dolt sql-server +// YAML config, creating it (and the proxieddb root directory) with a freshly +// picked free port if it does not yet exist. +// +// Idempotent: existing files are returned untouched so the dolt sql-server +// keeps the port it was launched with across bd invocations. Re-picking the +// port on every call would race the already-running daemon (which is bound +// to the existing port) and require a restart on every bd command. +// +// Owned by the store-open path, not by `bd init`: the proxied-server store +// cannot run without this file, so every code path that opens the store is +// responsible for ensuring it. Init is just another caller. +func ensureProxiedServerConfig(beadsDir string) (string, error) { + root := proxiedServerRoot(beadsDir) + if err := os.MkdirAll(root, config.BeadsDirPerm); err != nil { + return "", fmt.Errorf("ensureProxiedServerConfig: mkdir %s: %w", root, err) + } + path := proxiedServerConfigPath(beadsDir) + + switch _, err := os.Stat(path); { + case err == nil: + return path, nil + case !os.IsNotExist(err): + return "", fmt.Errorf("ensureProxiedServerConfig: stat %s: %w", path, err) + } + + port, err := proxy.PickFreePort() + if err != nil { + return "", fmt.Errorf("ensureProxiedServerConfig: pick free port: %w", err) + } + + body, err := renderProxiedServerConfig(port) + if err != nil { + return "", fmt.Errorf("ensureProxiedServerConfig: render YAML: %w", err) + } + if err := os.WriteFile(path, body, 0o600); err != nil { + return "", fmt.Errorf("ensureProxiedServerConfig: write %s: %w", path, err) + } + return path, nil +} + +// renderProxiedServerConfig produces the dolt sql-server YAML body for the +// proxied-server backend. We construct dolt's typed servercfg.YAMLConfig and +// marshal it rather than hand-writing strings: every field carries its yaml +// tag from the upstream package, so the schema stays in lockstep with +// whatever version of dolt this binary links against. +// +// Only fields the proxied-server cares about are populated — listener +// host/port (forced to 127.0.0.1 + the picked port) and log_level. Everything +// else is left zero so the upstream `omitempty` tags suppress it. +func renderProxiedServerConfig(port int) ([]byte, error) { + host := proxiedServerListenerHost + logLevel := string(servercfg.LogLevel_Info) + yc := &servercfg.YAMLConfig{ + LogLevelStr: &logLevel, + ListenerConfig: servercfg.ListenerYAMLConfig{ + HostStr: &host, + PortNumber: &port, + }, + } + return yaml.Marshal(yc) +} + +// proxiedServerListenerHost is the host the child dolt sql-server binds. +// Always loopback — the proxy multiplexes external clients onto this socket +// and we don't want the child accidentally exposed on a routable interface. +const proxiedServerListenerHost = "127.0.0.1" + +// newProxiedServerStore constructs a storage.DoltStorage backed by a +// per-workspace proxied dolt sql-server. It is the cmd/bd-side glue that +// resolves all of the per-workspace dependencies — dolt binary, YAML config +// path, log path, committer identity — and hands them to +// proxieddolt.NewDoltServerStore. Called from newDoltStore (store_factory.go +// and store_factory_nocgo.go) when cfg.ProxiedServer is true. +func newProxiedServerStore(ctx context.Context, cfg *dolt.Config) (storage.DoltStorage, error) { + if cfg == nil { + return nil, fmt.Errorf("newProxiedServerStore: cfg is nil") + } + if cfg.BeadsDir == "" { + return nil, fmt.Errorf("newProxiedServerStore: cfg.BeadsDir must be set") + } + if cfg.Database == "" { + return nil, fmt.Errorf("newProxiedServerStore: cfg.Database must be set") + } + + doltBin, err := exec.LookPath("dolt") + if err != nil { + return nil, fmt.Errorf("newProxiedServerStore: dolt is not installed (not found in PATH); install from https://docs.dolthub.com/introduction/installation: %w", err) + } + + configPath, err := ensureProxiedServerConfig(cfg.BeadsDir) + if err != nil { + return nil, err + } + + name, email := cfg.CommitterName, cfg.CommitterEmail + if name == "" || email == "" { + fallbackName, fallbackEmail := proxiedServerCommitter() + if name == "" { + name = fallbackName + } + if email == "" { + email = fallbackEmail + } + } + + return proxieddolt.NewDoltServerStore( + ctx, + proxiedServerRoot(cfg.BeadsDir), + cfg.BeadsDir, + cfg.Database, + name, email, + proxiedServerLogPath(cfg.BeadsDir), + configPath, + proxy.BackendLocalServer, + false, // autoSyncToOriginRemote — wired in a future iteration + "root", + "", // rootPassword: proxy is loopback-only, no auth + doltBin, + ) +} + +// proxiedServerCommitter returns the (name, email) recorded on dolt commits +// made by the proxied-server store. Sourced from `git config user.name/email` +// when present, else falls back to ("beads", "beads@localhost"). Mirrors the +// behavior of the dolt sql-server's own auto-configure path +// (internal/storage/db/server/doltserver.go: doltConfigure). +func proxiedServerCommitter() (string, string) { + name, email := "beads", "beads@localhost" + if out, err := exec.Command("git", "config", "user.name").Output(); err == nil { + if v := strings.TrimSpace(string(out)); v != "" { + name = v + } + } + if out, err := exec.Command("git", "config", "user.email").Output(); err == nil { + if v := strings.TrimSpace(string(out)); v != "" { + email = v + } + } + return name, email +} diff --git a/cmd/bd/proxied_server_test.go b/cmd/bd/proxied_server_test.go new file mode 100644 index 000000000..ae37fed29 --- /dev/null +++ b/cmd/bd/proxied_server_test.go @@ -0,0 +1,121 @@ +package main + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/dolthub/dolt/go/libraries/doltcore/servercfg" + "github.com/dolthub/dolt/go/libraries/utils/filesys" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRenderProxiedServerConfig_RoundTrips(t *testing.T) { + body, err := renderProxiedServerConfig(54321) + require.NoError(t, err) + + cfg, err := servercfg.NewYamlConfig(body) + require.NoError(t, err) + + assert.Equal(t, proxiedServerListenerHost, cfg.Host(), "Host mismatch") + assert.Equal(t, 54321, cfg.Port(), "Port mismatch") + assert.Equal(t, servercfg.LogLevel_Info, cfg.LogLevel(), "LogLevel mismatch") +} + +func TestEnsureProxiedServerConfig_CreatesAndIsIdempotent(t *testing.T) { + beadsDir := t.TempDir() + + path1, err := ensureProxiedServerConfig(beadsDir) + require.NoError(t, err) + assert.Equal(t, filepath.Join(beadsDir, "proxieddb", "server_config.yaml"), path1) + + body1, err := os.ReadFile(path1) + require.NoError(t, err) + require.NotEmpty(t, body1) + require.True(t, strings.Contains(string(body1), proxiedServerListenerHost)) + + // Second call must NOT rewrite — running daemon is bound to the existing port. + path2, err := ensureProxiedServerConfig(beadsDir) + require.NoError(t, err) + assert.Equal(t, path1, path2) + + body2, err := os.ReadFile(path2) + require.NoError(t, err) + assert.Equal(t, body1, body2, "second call must not rewrite the file") + + // Round-trip: dolt's own loader must accept what we wrote. + loaded, err := servercfg.YamlConfigFromFile(filesys.LocalFS, path2) + require.NoError(t, err) + assert.Equal(t, proxiedServerListenerHost, loaded.Host()) + assert.Greater(t, loaded.Port(), 0) +} + +func TestProxiedServerPathHelpers(t *testing.T) { + bd := "/tmp/some/.beads" + assert.Equal(t, "/tmp/some/.beads/proxieddb", proxiedServerRoot(bd)) + assert.Equal(t, "/tmp/some/.beads/proxieddb/server_config.yaml", proxiedServerConfigPath(bd)) + assert.Equal(t, "/tmp/some/.beads/proxieddb/server.log", proxiedServerLogPath(bd)) +} + +// TestInitCommandRegistersProxiedServerFlag verifies the --proxied-server flag +// is wired into initCmd. Flag-presence regression test. +func TestInitCommandRegistersProxiedServerFlag(t *testing.T) { + flag := initCmd.Flags().Lookup("proxied-server") + require.NotNil(t, flag, "init command does not register --proxied-server") + assert.Equal(t, "false", flag.DefValue, "--proxied-server should default to false") +} + +// TestCheckExistingBeadsDataAt_ProxiedServerNoData asserts that a proxied +// workspace with metadata.json but no actual /proxieddb//.dolt +// directory is treated as a fresh clone — init is allowed to proceed so the +// caller can bootstrap. +func TestCheckExistingBeadsDataAt_ProxiedServerNoData(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + require.NoError(t, os.MkdirAll(beadsDir, 0o755)) + + metadata := map[string]interface{}{ + "database": "dolt", + "backend": "dolt", + "dolt_mode": "proxied-server", + "dolt_database": "myproj", + } + data, err := json.Marshal(metadata) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0o644)) + + // No /proxieddb/myproj/.dolt — fresh-clone scenario. + if err := checkExistingBeadsDataAt(beadsDir, "myproj"); err != nil { + t.Fatalf("fresh proxied workspace should allow init, got: %v", err) + } +} + +// TestCheckExistingBeadsDataAt_ProxiedServerWithExistingDB asserts that an +// existing proxied database (proxieddb//.dolt is present) blocks +// re-init with the standard "already initialized" error. +func TestCheckExistingBeadsDataAt_ProxiedServerWithExistingDB(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + require.NoError(t, os.MkdirAll(beadsDir, 0o755)) + + metadata := map[string]interface{}{ + "database": "dolt", + "backend": "dolt", + "dolt_mode": "proxied-server", + "dolt_database": "myproj", + } + data, err := json.Marshal(metadata) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0o644)) + + // Materialize /proxieddb/myproj/.dolt to look like a populated + // proxied database. + dbDoltDir := filepath.Join(beadsDir, "proxieddb", "myproj", ".dolt") + require.NoError(t, os.MkdirAll(dbDoltDir, 0o755)) + + err = checkExistingBeadsDataAt(beadsDir, "myproj") + require.Error(t, err, "existing proxied database should block init") + assert.Contains(t, err.Error(), "already initialized") + assert.Contains(t, err.Error(), filepath.Join(beadsDir, "proxieddb", "myproj")) +} diff --git a/cmd/bd/sql.go b/cmd/bd/sql.go index 60f2f51b7..517bec1eb 100644 --- a/cmd/bd/sql.go +++ b/cmd/bd/sql.go @@ -31,7 +31,7 @@ report the number of rows affected. WARNING: Direct database access bypasses the storage layer. Use with caution.`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - if isEmbeddedMode() { + if !usesSQLServer() { fmt.Fprintln(os.Stderr, "Error: 'bd sql' is not yet supported in embedded mode") os.Exit(1) } diff --git a/cmd/bd/store_factory.go b/cmd/bd/store_factory.go index 96abddaa3..f6c613b05 100644 --- a/cmd/bd/store_factory.go +++ b/cmd/bd/store_factory.go @@ -17,32 +17,56 @@ import ( "github.com/steveyegge/beads/internal/storage/embeddeddolt" ) -// isEmbeddedMode returns true when the current session is using the embedded -// Dolt engine (the default). Returns false in server mode (external dolt -// sql-server). Safe to call before store initialization — defaults to true -// (embedded) when the mode hasn't been set yet. -func isEmbeddedMode() bool { +// usesSQLServer returns true when the current session talks to a SQL server +// of any flavor — either an externally-managed dolt sql-server (server mode) +// or the per-workspace proxied dolt sql-server (proxied-server mode). Returns +// false for the embedded Dolt engine (the default). +// +// Safe to call before store initialization — defaults to false (embedded) +// when the mode hasn't been set yet. +// +// Use usesProxiedServer below to distinguish the proxied-server backend from +// the externally-managed one. +func usesSQLServer() bool { if shouldUseGlobals() { - if serverMode { - return false + if serverMode || proxiedServerMode { + return true } - } else if cmdCtx != nil && cmdCtx.ServerMode { - return false + } else if cmdCtx != nil && (cmdCtx.ServerMode || cmdCtx.ProxiedServer) { + return true } // Shared server mode is a form of server mode. This check covers // commands that skip DB init (dolt status, dolt start, etc.) where // serverMode hasn't been set from metadata.json yet (GH#2946). if doltserver.IsSharedServerMode() { - return false + return true } - return true // default: embedded + return false // default: embedded +} + +// usesProxiedServer reports whether the current session is using the +// per-workspace proxied dolt sql-server (dolt_mode=proxied-server). Used to +// branch off behavior that only applies to externally-managed dolt servers +// (host/port from cfg, hosted-Dolt remote auth, shared-server lifecycle). +func usesProxiedServer() bool { + if shouldUseGlobals() { + return proxiedServerMode + } + return cmdCtx != nil && cmdCtx.ProxiedServer } // newDoltStore creates a storage backend from an explicit config. -// When cfg.ServerMode is true, connects to an external dolt sql-server; -// otherwise uses the embedded Dolt engine (default). +// Dispatch order is mutually exclusive: +// - cfg.ProxiedServer: per-workspace proxied dolt sql-server +// (.beads/proxieddb/, the proxy + child dolt managed by bd itself). +// - cfg.ServerMode: externally-managed dolt sql-server (host/port from cfg). +// - otherwise: embedded Dolt engine (default). +// // Used by bd init and PersistentPreRun. func newDoltStore(ctx context.Context, cfg *dolt.Config) (storage.DoltStorage, error) { + if cfg.ProxiedServer { + return newProxiedServerStore(ctx, cfg) + } if cfg.ServerMode { return dolt.New(ctx, cfg) } @@ -78,6 +102,13 @@ func acquireEmbeddedLock(beadsDir string, serverMode bool) (util.Unlocker, error // auto-sanitized to underscores and the fix is persisted to metadata.json. func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) { cfg, err := configfile.Load(beadsDir) + if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() { + return newProxiedServerStore(ctx, &dolt.Config{ + BeadsDir: beadsDir, + Database: cfg.GetDoltDatabase(), + ProxiedServer: true, + }) + } if err == nil && cfg != nil && cfg.IsDoltServerMode() { return dolt.NewFromConfig(ctx, beadsDir) } @@ -142,6 +173,14 @@ func migrateHyphenatedDB(beadsDir string, cfg *configfile.Config, oldName, newNa // hydration from mutating foreign projects (GH#3231). func newReadOnlyStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) { cfg, err := configfile.Load(beadsDir) + if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() { + return newProxiedServerStore(ctx, &dolt.Config{ + BeadsDir: beadsDir, + Database: cfg.GetDoltDatabase(), + ProxiedServer: true, + ReadOnly: true, + }) + } if err == nil && cfg != nil && cfg.IsDoltServerMode() { return dolt.NewFromConfigWithOptions(ctx, beadsDir, &dolt.Config{ReadOnly: true}) } diff --git a/cmd/bd/store_factory_nocgo.go b/cmd/bd/store_factory_nocgo.go index 6a48e35f2..972c844b4 100644 --- a/cmd/bd/store_factory_nocgo.go +++ b/cmd/bd/store_factory_nocgo.go @@ -12,15 +12,31 @@ import ( "github.com/steveyegge/beads/internal/storage/dolt" ) -// isEmbeddedMode returns false in non-CGO builds since embedded Dolt -// requires CGO. Only server mode is available. -func isEmbeddedMode() bool { - return false +// usesSQLServer returns true in non-CGO builds since embedded Dolt requires +// CGO — the only options are the externally-managed dolt sql-server +// (ServerMode) and the per-workspace proxied dolt sql-server (ProxiedServer), +// both of which are SQL-server-shaped. +func usesSQLServer() bool { + return true } -// newDoltStore creates a server-mode storage backend. Embedded Dolt is not -// available without CGO. +// usesProxiedServer reports whether the current session is using the +// per-workspace proxied dolt sql-server (dolt_mode=proxied-server). +func usesProxiedServer() bool { + if shouldUseGlobals() { + return proxiedServerMode + } + return cmdCtx != nil && cmdCtx.ProxiedServer +} + +// newDoltStore creates a SQL-server-backed storage backend. Embedded Dolt is +// not available without CGO; the two server-shaped backends both work fine. +// - cfg.ProxiedServer: per-workspace proxied dolt sql-server. +// - cfg.ServerMode: externally-managed dolt sql-server. func newDoltStore(ctx context.Context, cfg *dolt.Config) (storage.DoltStorage, error) { + if cfg.ProxiedServer { + return newProxiedServerStore(ctx, cfg) + } if !cfg.ServerMode { return nil, fmt.Errorf("%s", nocgoEmbeddedErrMsg) } @@ -32,36 +48,57 @@ func acquireEmbeddedLock(_ string, _ bool) (util.Unlocker, error) { return util.NoopLock{}, nil } -// newDoltStoreFromConfig creates a server-mode storage backend from config. +// newDoltStoreFromConfig creates a SQL-server-backed storage backend from config. func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) { cfg, err := configfile.Load(beadsDir) + if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() { + return newProxiedServerStore(ctx, &dolt.Config{ + BeadsDir: beadsDir, + Database: cfg.GetDoltDatabase(), + ProxiedServer: true, + }) + } if err == nil && cfg != nil && cfg.IsDoltServerMode() { return dolt.NewFromConfig(ctx, beadsDir) } return nil, fmt.Errorf("%s", nocgoEmbeddedErrMsg) } -// newReadOnlyStoreFromConfig creates a read-only server-mode storage backend. +// newReadOnlyStoreFromConfig creates a read-only SQL-server-backed storage backend. func newReadOnlyStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) { cfg, err := configfile.Load(beadsDir) + if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() { + return newProxiedServerStore(ctx, &dolt.Config{ + BeadsDir: beadsDir, + Database: cfg.GetDoltDatabase(), + ProxiedServer: true, + ReadOnly: true, + }) + } if err == nil && cfg != nil && cfg.IsDoltServerMode() { return dolt.NewFromConfigWithOptions(ctx, beadsDir, &dolt.Config{ReadOnly: true}) } return nil, fmt.Errorf("%s", nocgoEmbeddedErrMsg) } -// nocgoEmbeddedErrMsg guides the user either to server mode (no rebuild -// needed) or to an embedded-capable install path. It intentionally enumerates -// the canonical install paths so users don't have to hunt through docs. +// nocgoEmbeddedErrMsg guides the user either to a SQL-server-shaped backend +// (no rebuild needed) or to an embedded-capable install path. It intentionally +// enumerates the canonical install paths so users don't have to hunt through +// docs. const nocgoEmbeddedErrMsg = `embedded Dolt requires a CGO build, but this bd binary was built with CGO_ENABLED=0. -Two options: +Three options: - 1. Use server mode (no reinstall needed): + 1. Use the proxied dolt sql-server (no external server, no reinstall): + bd init --proxied-server + bd spawns a per-workspace proxy + child dolt sql-server under + .beads/proxieddb/ and manages their lifecycle for you. + + 2. Use external server mode (no reinstall needed): bd init --server Requires a running 'dolt sql-server'. See docs/DOLT.md. - 2. Reinstall with embedded-mode support: + 3. Reinstall with embedded-mode support: brew install beads # macOS / Linux npm install -g @beads/bd # any platform with Node curl -fsSL https://raw.githubusercontent.com/steveyegge/beads/main/scripts/install.sh | bash diff --git a/cmd/bd/version_tracking.go b/cmd/bd/version_tracking.go index a254940d8..120a70367 100644 --- a/cmd/bd/version_tracking.go +++ b/cmd/bd/version_tracking.go @@ -128,7 +128,7 @@ func maybeShowUpgradeNotification() { // Display notification fmt.Printf("🔄 bd upgraded from v%s to v%s since last use\n", previousVersion, Version) fmt.Println("💡 Run 'bd upgrade review' to see what changed") - if !isEmbeddedMode() { + if usesSQLServer() { fmt.Println("💊 Run 'bd doctor' to verify upgrade completed cleanly") } diff --git a/cmd/bd/where.go b/cmd/bd/where.go index 9e815e31c..e6fdbf3a6 100644 --- a/cmd/bd/where.go +++ b/cmd/bd/where.go @@ -126,9 +126,10 @@ func shouldReadWherePrefixFromStore(beadsDir string) bool { return true } - // `bd where` should be able to report selected server-mode metadata without - // requiring a live Dolt server just to recover issue_prefix. - return !cfg.IsDoltServerMode() + // `bd where` should be able to report selected metadata without requiring + // a live Dolt server (or spawning the proxied-server daemon) just to + // recover issue_prefix. + return !cfg.IsDoltServerMode() && !cfg.IsDoltProxiedServerMode() } // findOriginalBeadsDir walks up from cwd looking for a .beads directory with a redirect file diff --git a/internal/configfile/configfile.go b/internal/configfile/configfile.go index ef83f8259..9df446915 100644 --- a/internal/configfile/configfile.go +++ b/internal/configfile/configfile.go @@ -201,11 +201,12 @@ func CapabilitiesForBackend(_ string) BackendCapabilities { // GetCapabilities returns the backend capabilities for this config. // Unlike CapabilitiesForBackend(string), this considers Dolt server mode -// which supports multi-process access. +// (and proxied-server mode) which support multi-process access. func (c *Config) GetCapabilities() BackendCapabilities { backend := c.GetBackend() - if backend == BackendDolt && c.IsDoltServerMode() { - // Server mode supports multi-writer, so NOT single-process-only + if backend == BackendDolt && (c.IsDoltServerMode() || c.IsDoltProxiedServerMode()) { + // Server-shaped backends multiplex writers through a sql-server, so + // NOT single-process-only. return BackendCapabilities{SingleProcessOnly: false} } return CapabilitiesForBackend(backend) @@ -218,8 +219,9 @@ func (c *Config) GetBackend() string { // Dolt mode constants const ( - DoltModeEmbedded = "embedded" - DoltModeServer = "server" + DoltModeEmbedded = "embedded" + DoltModeServer = "server" + DoltModeProxiedServer = "proxied-server" ) // Default Dolt server settings @@ -256,6 +258,20 @@ func (c *Config) IsDoltServerMode() bool { return strings.ToLower(c.DoltMode) == DoltModeServer } +// IsDoltProxiedServerMode reports whether this workspace is configured to use +// the per-workspace proxied dolt sql-server (a parent proxy + a child +// dolt sql-server, both rooted at /proxieddb). Proxied-server is a +// distinct backend kind from "server": IsDoltServerMode() returns false for it +// because the existing server-mode call sites assume user-supplied connection +// details (host/port/socket/user, BEADS_DOLT_PASSWORD, TLS) that proxied-server +// does not populate. +func (c *Config) IsDoltProxiedServerMode() bool { + if c.GetBackend() != BackendDolt { + return false + } + return strings.ToLower(c.DoltMode) == DoltModeProxiedServer +} + // GetDoltMode returns the Dolt connection mode, defaulting to server. func (c *Config) GetDoltMode() string { if c.DoltMode == "" { diff --git a/internal/configfile/configfile_test.go b/internal/configfile/configfile_test.go index 451c4e4ea..ab8e83ca5 100644 --- a/internal/configfile/configfile_test.go +++ b/internal/configfile/configfile_test.go @@ -392,6 +392,112 @@ func TestIsDoltServerModeEnvVar(t *testing.T) { }) } +// TestDoltProxiedServerMode covers the IsDoltProxiedServerMode predicate and +// the GetCapabilities branch that treats proxied-server as multi-process-safe +// (the proxy daemon serializes writers). +func TestDoltProxiedServerMode(t *testing.T) { + t.Run("IsDoltProxiedServerMode", func(t *testing.T) { + tests := []struct { + name string + cfg *Config + want bool + }{ + { + name: "empty backend", + cfg: &Config{Backend: ""}, + want: false, + }, + { + name: "embedded mode", + cfg: &Config{Backend: BackendDolt, DoltMode: DoltModeEmbedded}, + want: false, + }, + { + name: "server mode", + cfg: &Config{Backend: BackendDolt, DoltMode: DoltModeServer}, + want: false, + }, + { + name: "proxied-server mode", + cfg: &Config{Backend: BackendDolt, DoltMode: DoltModeProxiedServer}, + want: true, + }, + { + name: "proxied-server, mixed case", + cfg: &Config{Backend: BackendDolt, DoltMode: "Proxied-Server"}, + want: true, + }, + { + name: "default (no DoltMode)", + cfg: &Config{Backend: BackendDolt}, + want: false, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := tc.cfg.IsDoltProxiedServerMode(); got != tc.want { + t.Errorf("IsDoltProxiedServerMode() = %v, want %v", got, tc.want) + } + }) + } + }) + + t.Run("ServerAndProxiedAreMutuallyExclusive", func(t *testing.T) { + cfg := &Config{Backend: BackendDolt, DoltMode: DoltModeProxiedServer} + if cfg.IsDoltServerMode() { + t.Error("IsDoltServerMode() should be false for proxied-server mode") + } + if !cfg.IsDoltProxiedServerMode() { + t.Error("IsDoltProxiedServerMode() should be true for proxied-server mode") + } + }) + + t.Run("GetCapabilities_ProxiedServerNotSingleProcess", func(t *testing.T) { + cfg := &Config{Backend: BackendDolt, DoltMode: DoltModeProxiedServer} + caps := cfg.GetCapabilities() + if caps.SingleProcessOnly { + t.Error("proxied-server should report SingleProcessOnly=false (proxy multiplexes writers)") + } + }) + + t.Run("GetDoltModePreservesProxiedValue", func(t *testing.T) { + cfg := &Config{Backend: BackendDolt, DoltMode: DoltModeProxiedServer} + if got := cfg.GetDoltMode(); got != DoltModeProxiedServer { + t.Errorf("GetDoltMode() = %q, want %q", got, DoltModeProxiedServer) + } + }) + + t.Run("RoundtripPersistsProxiedMode", func(t *testing.T) { + dir := t.TempDir() + original := &Config{ + Database: "dolt", + Backend: BackendDolt, + DoltMode: DoltModeProxiedServer, + DoltDatabase: "myproj", + ProjectID: "abc-123", + } + if err := original.Save(dir); err != nil { + t.Fatalf("Save: %v", err) + } + loaded, err := Load(dir) + if err != nil { + t.Fatalf("Load: %v", err) + } + if loaded == nil { + t.Fatal("Load returned nil") + } + if loaded.DoltMode != DoltModeProxiedServer { + t.Errorf("DoltMode = %q, want %q", loaded.DoltMode, DoltModeProxiedServer) + } + if !loaded.IsDoltProxiedServerMode() { + t.Error("IsDoltProxiedServerMode() = false after roundtrip") + } + if loaded.IsDoltServerMode() { + t.Error("IsDoltServerMode() = true after roundtrip; should be false") + } + }) +} + // TestGetBackendAlwaysDolt tests that GetBackend always returns "dolt". func TestGetBackendAlwaysDolt(t *testing.T) { tests := []struct { diff --git a/internal/storage/dolt/store.go b/internal/storage/dolt/store.go index d03332a05..13b0679f7 100644 --- a/internal/storage/dolt/store.go +++ b/internal/storage/dolt/store.go @@ -243,6 +243,14 @@ type Config struct { // on metadata.json dolt_mode or BEADS_DOLT_SERVER_MODE env var. ServerMode bool + // ProxiedServer indicates this config targets a per-workspace proxied + // dolt sql-server (a parent proxy + a child dolt sql-server, both rooted + // at /proxieddb). Mutually exclusive with ServerMode: the + // proxied path owns its own connection details and does not consult + // ServerHost/Port/Socket/User. Set by the store factory based on + // metadata.json dolt_mode=proxied-server. + ProxiedServer bool + // AutoStart enables transparent server auto-start when connection fails. // When true and the host is localhost, bd will start a dolt sql-server // automatically if one isn't running. Disabled under orchestrator (GT_ROOT set). diff --git a/internal/storage/doltserver/store.go b/internal/storage/doltserver/store.go index 021988f07..0c36d2ba9 100644 --- a/internal/storage/doltserver/store.go +++ b/internal/storage/doltserver/store.go @@ -48,7 +48,18 @@ var ( _ storage.Compactor = (*DoltServerStore)(nil) ) -func newDoltServerStore( +// NewDoltServerStore constructs a *DoltServerStore against a per-workspace +// proxied dolt sql-server rooted at serverRootDir. It ensures the proxy + +// child dolt sql-server are running (spawning them if needed via +// proxy.GetCreateDatabaseProxyServerEndpoint), opens a *sql.DB connection, +// runs schema initialization, and returns a store implementing +// storage.DoltStorage. +// +// The serverConfigFilePath must point at an already-written dolt sql-server +// YAML config — callers are responsible for ensuring it exists before this +// function is invoked. See the cmd/bd glue layer (newProxiedServerStore / +// ensureProxiedServerConfig) for how this is wired up in bd. +func NewDoltServerStore( ctx context.Context, serverRootDir string, beadsDir string, diff --git a/internal/storage/doltserver/store_test.go b/internal/storage/doltserver/store_test.go index d31eb4303..5e51df1c6 100644 --- a/internal/storage/doltserver/store_test.go +++ b/internal/storage/doltserver/store_test.go @@ -59,7 +59,7 @@ func TestNewDoltServerStore_ValidationErrors(t *testing.T) { } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - s, err := newDoltServerStore( + s, err := NewDoltServerStore( context.Background(), t.TempDir(), t.TempDir(), tc.database, "Test", "test@example.com", @@ -97,7 +97,7 @@ func TestNewDoltServerStore_HappyPath(t *testing.T) { cfgPath := writeServerConfig(t, port) logPath := filepath.Join(t.TempDir(), "server.log") - store, err := newDoltServerStore( + store, err := NewDoltServerStore( context.Background(), storeRootDir, t.TempDir(),