From 300d9deb30a3c76dd124ec98c66c377bf45b9987 Mon Sep 17 00:00:00 2001 From: Brett Eisenberg Date: Sat, 10 Oct 2026 04:25:38 +0000 Subject: [PATCH] fix(dolt): apply metadata socket on config opens applyResolvedConfig fills the connection config from metadata.json for every NewFromConfig* open. In the public package that is OpenFromConfig, OpenGated and the Dolt-server branches of OpenBestAvailable and OpenBestAvailableWith; in cmd/bd it is bd doctor and its fixes, the store factory behind routed and cross-repo opens, and the config apply, config drift, create and version-tracking helpers. It copies host, port, user, password and TLS but never dolt_server_socket. The top-level command path (resolveDoltServerConnection in cmd/bd/main.go) does read the socket, so the same workspace resolves to two different endpoints depending on how it is opened. Copy the socket through GetDoltServerSocket when the caller has not set one. Precedence is then caller, BEADS_DOLT_SERVER_SOCKET, metadata.json, as on the CLI path. Transport policy is unchanged: ResolveSocketTransport still prefers the socket and falls back to TCP when only TCP answers. - Cover metadata, env and caller precedence, and the no-socket case, in TestApplyResolvedConfig - Add a public test: with neither endpoint answering, OpenFromConfig reports the metadata socket rather than the TCP address - Document socket precedence and TCP fallback in docs/architecture/dolt.md --- beads_test.go | 62 ++++++++++++++++++++++++ docs/architecture/dolt.md | 6 +++ internal/storage/dolt/open.go | 5 ++ internal/storage/dolt/open_test.go | 77 ++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) diff --git a/beads_test.go b/beads_test.go index 2faaa3ce1..ef5bae37f 100644 --- a/beads_test.go +++ b/beads_test.go @@ -6,6 +6,7 @@ import ( "net" "os" "path/filepath" + "strconv" "strings" "testing" "time" @@ -192,6 +193,67 @@ func TestOpenFromConfig_ServerModeFailsWithoutServer(t *testing.T) { } } +func TestOpenFromConfig_MetadataSocketReportedWhenNoEndpointAnswers(t *testing.T) { + // A socket is a preference with TCP fallback, so with neither endpoint + // answering the error names the socket; a dropped socket names TCP. Every + // other source of an endpoint is pinned, and auto-start is off. + for _, name := range []string{ + "BEADS_DOLT_SERVER_SOCKET", + "BEADS_DOLT_SERVER_HOST", + "BEADS_DOLT_SERVER_PORT", + "BEADS_DOLT_PORT", + "BEADS_DOLT_SHARED_SERVER", + "BEADS_DOLT_SERVER_MODE", + "BEADS_CENTRAL_CONFIG", + "BEADS_DOLT_CREDENTIAL_COMMAND", + } { + t.Setenv(name, "") + } + t.Setenv("HOME", t.TempDir()) + t.Setenv("BEADS_DOLT_AUTO_START", "0") + + tmpDir := t.TempDir() + beadsDir := filepath.Join(tmpDir, ".beads") + if err := os.MkdirAll(beadsDir, 0755); err != nil { + t.Fatalf("failed to create .beads dir: %v", err) + } + + // The TCP endpoint: a port that was free a moment ago, now closed. + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatalf("failed to find free port: %v", err) + } + freePort := ln.Addr().(*net.TCPAddr).Port + ln.Close() + if conn, err := net.DialTimeout("tcp", fmt.Sprintf("127.0.0.1:%d", freePort), 200*time.Millisecond); err == nil { + conn.Close() + t.Fatalf("something is listening on 127.0.0.1:%d; the TCP endpoint must be unavailable", freePort) + } + + // The socket endpoint: a path that does not exist. + sockPath := filepath.Join(tmpDir, "dolt.sock") + if _, err := os.Stat(sockPath); !os.IsNotExist(err) { + t.Fatalf("socket path %s must not exist: %v", sockPath, err) + } + + if err := os.WriteFile(filepath.Join(beadsDir, "dolt-server.port"), []byte(strconv.Itoa(freePort)), 0644); err != nil { + t.Fatalf("failed to write dolt-server.port: %v", err) + } + metadata := fmt.Sprintf(`{"backend":"dolt","database":"dolt","dolt_mode":"server","dolt_database":"socketcheck","dolt_server_host":"127.0.0.1","dolt_server_socket":%q}`, sockPath) + if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0644); err != nil { + t.Fatalf("failed to write metadata.json: %v", err) + } + + store, openErr := beads.OpenFromConfig(context.Background(), beadsDir) + if openErr == nil { + store.Close() + t.Fatal("OpenFromConfig succeeded with neither the socket nor the TCP port answering") + } + if !strings.Contains(openErr.Error(), sockPath) { + t.Errorf("expected the configured socket %s in the error, got: %v", sockPath, openErr) + } +} + func TestOpenFromConfig_NoMetadata(t *testing.T) { skipIfNoDoltServer(t) // Missing metadata.json should use defaults (server mode) diff --git a/docs/architecture/dolt.md b/docs/architecture/dolt.md index 5e41e492f..b66c8d366 100644 --- a/docs/architecture/dolt.md +++ b/docs/architecture/dolt.md @@ -222,6 +222,12 @@ control is simpler than network allowlists. The Dolt server must be started with `dolt sql-server --socket `. Auto-start is not supported in socket mode. +`bd init --server-socket` records the path as `dolt_server_socket` in +`.beads/metadata.json`. A socket passed directly by a library caller wins, then +`BEADS_DOLT_SERVER_SOCKET`, then `dolt_server_socket`. The socket is a +preference, not a requirement: if it does not answer but the TCP host and port +do, bd connects over TCP. If neither answers, the error names the socket. + Switch to server mode when you need: - Multiple agents writing simultaneously - Orchestrator multi-rig setups diff --git a/internal/storage/dolt/open.go b/internal/storage/dolt/open.go index 6de61bbff..d64779f81 100644 --- a/internal/storage/dolt/open.go +++ b/internal/storage/dolt/open.go @@ -284,6 +284,11 @@ func applyResolvedConfig(ctx context.Context, beadsDir string, fileCfg *configfi // precedence chain and carries the source along with the port. ApplyResolvedServerPort(beadsDir, cfg) } + // Caller, then BEADS_DOLT_SERVER_SOCKET, then metadata.json, as on the CLI + // path. Still a preference: ResolveSocketTransport may fall back to TCP. + if cfg.ServerSocket == "" { + cfg.ServerSocket = fileCfg.GetDoltServerSocket() + } // Resolve the server-mode credential (the connection username). In server mode a // configured credential command takes precedence over the static user; it fails // closed (see ApplyGatewayCredential). The command runs only in server mode — an diff --git a/internal/storage/dolt/open_test.go b/internal/storage/dolt/open_test.go index c3d86077d..7b37f1232 100644 --- a/internal/storage/dolt/open_test.go +++ b/internal/storage/dolt/open_test.go @@ -448,6 +448,83 @@ func TestApplyResolvedConfig(t *testing.T) { } }) + // Socket precedence: caller, then BEADS_DOLT_SERVER_SOCKET, then + // metadata.json. Each case pins the env var and clears the credential + // command, which runs in server mode and could fail the resolve first. + socketFileCfg := func(t *testing.T, socket string) *configfile.Config { + t.Helper() + t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", "") + return &configfile.Config{ + Backend: configfile.BackendDolt, + DoltMode: configfile.DoltModeServer, + DoltDatabase: "beads_codex", + DoltServerSocket: socket, + } + } + + t.Run("applies socket from metadata", func(t *testing.T) { + t.Setenv("BEADS_DOLT_SERVER_SOCKET", "") + cfg := &Config{} + + if err := applyResolvedConfig(context.Background(), t.TempDir(), socketFileCfg(t, "/metadata/dolt.sock"), cfg); err != nil { + t.Fatalf("applyResolvedConfig: %v", err) + } + + if cfg.ServerSocket != "/metadata/dolt.sock" { + t.Fatalf("ServerSocket = %q, want the metadata socket %q", cfg.ServerSocket, "/metadata/dolt.sock") + } + }) + + t.Run("caller socket wins over env and metadata", func(t *testing.T) { + t.Setenv("BEADS_DOLT_SERVER_SOCKET", "/env/dolt.sock") + cfg := &Config{ServerSocket: "/caller/dolt.sock"} + + if err := applyResolvedConfig(context.Background(), t.TempDir(), socketFileCfg(t, "/metadata/dolt.sock"), cfg); err != nil { + t.Fatalf("applyResolvedConfig: %v", err) + } + + if cfg.ServerSocket != "/caller/dolt.sock" { + t.Fatalf("ServerSocket = %q, want the caller's socket %q", cfg.ServerSocket, "/caller/dolt.sock") + } + }) + + t.Run("env socket wins over metadata", func(t *testing.T) { + t.Setenv("BEADS_DOLT_SERVER_SOCKET", "/env/dolt.sock") + cfg := &Config{} + + if err := applyResolvedConfig(context.Background(), t.TempDir(), socketFileCfg(t, "/metadata/dolt.sock"), cfg); err != nil { + t.Fatalf("applyResolvedConfig: %v", err) + } + + if cfg.ServerSocket != "/env/dolt.sock" { + t.Fatalf("ServerSocket = %q, want the env socket %q", cfg.ServerSocket, "/env/dolt.sock") + } + }) + + t.Run("no socket anywhere leaves TCP config untouched", func(t *testing.T) { + t.Setenv("BEADS_DOLT_SERVER_SOCKET", "") + t.Setenv("BEADS_DOLT_SERVER_HOST", "") + t.Setenv("BEADS_DOLT_SERVER_PORT", "") + t.Setenv("BEADS_DOLT_PORT", "") + beadsDir := t.TempDir() + fileCfg := socketFileCfg(t, "") + cfg := &Config{} + + if err := applyResolvedConfig(context.Background(), beadsDir, fileCfg, cfg); err != nil { + t.Fatalf("applyResolvedConfig: %v", err) + } + + if cfg.ServerSocket != "" { + t.Fatalf("ServerSocket = %q, want empty (TCP)", cfg.ServerSocket) + } + if cfg.ServerHost != fileCfg.GetDoltServerHost() { + t.Fatalf("ServerHost = %q, want %q", cfg.ServerHost, fileCfg.GetDoltServerHost()) + } + if want := doltserver.DefaultConfig(beadsDir).Port; cfg.ServerPort != want { + t.Fatalf("ServerPort = %d, want %d", cfg.ServerPort, want) + } + }) + t.Run("pool timeouts populate from env vars (bd-vz0y9)", func(t *testing.T) { t.Setenv("BEADS_DOLT_POOL_READ_TIMEOUT", "90s") t.Setenv("BEADS_DOLT_POOL_WRITE_TIMEOUT", "45")