Triage and clean up an unreviewed automated commit stream on main (#4418)

* Revert "test(cmd/bd): Seam B cross-mode list parity — embedded vs proxied CLI (bd-6dnrw.45)"

This reverts commit 93b186dcf9.

* Revert "test(ci): restore test-proxied-server-cmd lane behind a test-only init ungate (bd-6dnrw.44)"

This reverts commit 1425e7338e.

* Revert "fix(cli): keep the autocommit sweep off read-only and inspection commands; attribute sweeps distinctly (bd-578h9.7)"

This reverts commit 81489b8c15.

* Revert "fix(autocommit): dirty-working-set safety net behind commandDidWrite (bd-6dnrw.11)"

This reverts commit d2b6cc5250.

* test: add proven born-failing regression tests for kept fixes

For kept fixes whose shipped tests were characterization-only or absent, add
born-failing regression tests (verified RED when the defect is reintroduced,
GREEN with the fix in place):

- internal/lockfile: IsLocked must detect a *wrapped* ErrLocked — guards the
  errors.Is fix (was pointer-equality). (orig 1a83cb08c)
- internal/storage/domain/db: GetDescendants with a level filter across edge
  and dotted-ID branches — guards the dolt 2.1.6 named-CTE analyzer workaround;
  the pre-existing dotted-orphans test used an empty filter and never built the
  predicate. (orig 341c7a5a4)
- cmd/bd: GOMODCACHE must be pinned outside the temp test HOME — guards the
  testMainInner module-cache fix. (orig bc909834c)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* revert(serverv2): restore the gated proxied-server layer to BASE

The proxied / external-dolt-server subsystem (serverV2) is gated off and not
running yet. This reverts the unreviewed changes to its schema-independent
layers back to BASE 9a1c88b63, while keeping the parts welded to the published
schema.

Reverted to BASE (26 files):
- internal/storage/dbproxy/*  (proxy/socket process mgmt: orphan-dolt reaping,
  pidfile discovery, endpoint/shutdown hardening)
- internal/storage/uow/*      (uow tx lifecycle, warmup-retry classification,
  provider changes)
- cmd/bd proxied glue: proxied_server, init_proxied_server, list_proxied_server,
  uow_factory, store_factory(+nocgo), and main.go proxied-mode dispatch hunks

Kept at HEAD (welded to the published migration 0051 / required by the data
layer, consistent with the keep-the-schema-stack decision):
- internal/storage/domain/db/* (mints app-side ids because 0051 dropped the DB
  default; queries the live migrated schema) -- left identical to HEAD
- the issueops Runner/DBTX widening + scan changes that HEAD domain/db requires

A strict serverV2->BASE revert is impossible without breaking main: domain/db is
welded to the published 0051 (BASE domain/db inserts without an id and relies on
the DEFAULT(UUID()) that 0051 removed). This reverts everything that is NOT so
welded.

Verified: full `make test` green (60 ok packages, 0 failures); domain/db, uow,
and dbproxy all ok.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: CI Bot <ci@beads.test>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Julian Knutsen 2026-06-15 20:24:57 -07:00 • committed by GitHub
commit a59e753258
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 417 additions and 1507 deletions

View file

@ -733,60 +733,60 @@ jobs:
retention-days: 7
if-no-files-found: ignore
# End-to-end coverage for proxied-server CLI subcommands. Each sub-test
# spawns a real dolt sql-server via the proxy and exercises the bd CLI
# subprocess against it (see cmd/bd/*_proxied_integration_test.go).
# # End-to-end coverage for proxied-server CLI subcommands. Each sub-test
# # spawns a real dolt sql-server via the proxy and exercises the bd CLI
# # subprocess against it (see cmd/bd/*_proxied_integration_test.go).
# #
# # The -run pattern intentionally matches the TestProxiedServer* prefix so
# # new proxied integration suites (TestProxiedServerCreate, ...Update,
# # ...Show, etc.) are auto-included without further CI edits. Adopt the
# # `TestProxiedServer<Subcommand>` naming convention for any new entry
# # points and they will be picked up here.
# #
# # Companion to test-domain-uow (which tests the storage/uow layer); this
# # job is the CLI-side gate that ensures the proxied path stays at parity
# # with the embedded path's behavior.
# #
# # The package-level testcontainers Dolt singleton (started by
# # cmd/bd/test_dolt_server_cgo_test.go) is automatically skipped when
# # BEADS_TEST_PROXIED_SERVER=1 is set, so this lane is ~50s faster than
# # it would otherwise be.
# test-proxied-server-cmd:
# name: Test (proxied-server cmd)
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
#
# The -run pattern intentionally matches the TestProxiedServer* prefix so
# new proxied integration suites (TestProxiedServerCreate, ...Update,
# ...Show, etc.) are auto-included without further CI edits. Adopt the
# `TestProxiedServer<Subcommand>` naming convention for any new entry
# points and they will be picked up here.
# - name: Set up Go
# uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6
# with:
# go-version-file: 'go.mod'
#
# Companion to test-domain-uow (which tests the storage/uow layer); this
# job is the CLI-side gate that ensures the proxied path stays at parity
# with the embedded path's behavior.
# - name: Install Dolt CLI
# run: curl -fsSL https://github.com/dolthub/dolt/releases/latest/download/install.sh | sudo bash
#
# The package-level testcontainers Dolt singleton (started by
# cmd/bd/test_dolt_server_cgo_test.go) is automatically skipped when
# BEADS_TEST_PROXIED_SERVER=1 is set, so this lane is ~50s faster than
# it would otherwise be.
test-proxied-server-cmd:
name: Test (proxied-server cmd)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: Set up Go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6
with:
go-version-file: 'go.mod'
- name: Install Dolt CLI
run: curl -fsSL https://github.com/dolthub/dolt/releases/latest/download/install.sh | sudo bash
- name: Verify dolt on PATH
run: dolt version
- name: Configure Git and Dolt identity
run: |
git config --global user.name "CI Bot"
git config --global user.email "ci@beads.test"
dolt config --global --add user.name "CI Bot"
dolt config --global --add user.email "ci@beads.test"
- name: Pre-build bd binary for proxied integration tests
# The test's buildEmbeddedBD helper picks up BEADS_TEST_BD_BINARY,
# skipping a per-test ~45s rebuild for each invocation.
run: go build -tags gms_pure_go -o /tmp/bd-proxied-test ./cmd/bd
- name: Test proxied-server bd subcommands
env:
BEADS_TEST_PROXIED_SERVER: "1"
BEADS_TEST_BD_BINARY: /tmp/bd-proxied-test
# ^TestProxiedServer matches every proxied-server top-level test
# (Create today; future Update/Show/etc. auto-join without edits).
run: go test -tags gms_pure_go -count=1 -timeout=10m -v -run '^TestProxiedServer' ./cmd/bd/
# - name: Verify dolt on PATH
# run: dolt version
#
# - name: Configure Git and Dolt identity
# run: |
# git config --global user.name "CI Bot"
# git config --global user.email "ci@beads.test"
# dolt config --global --add user.name "CI Bot"
# dolt config --global --add user.email "ci@beads.test"
#
# - name: Pre-build bd binary for proxied integration tests
# # The test's buildEmbeddedBD helper picks up BEADS_TEST_BD_BINARY,
# # skipping a per-test ~45s rebuild for each invocation.
# run: go build -tags gms_pure_go -o /tmp/bd-proxied-test ./cmd/bd
#
# - name: Test proxied-server bd subcommands
# env:
# BEADS_TEST_PROXIED_SERVER: "1"
# BEADS_TEST_BD_BINARY: /tmp/bd-proxied-test
# # ^TestProxiedServer matches every proxied-server top-level test
# # (Create today; future Update/Show/etc. auto-join without edits).
# run: go test -tags gms_pure_go -count=1 -timeout=10m -v -run '^TestProxiedServer' ./cmd/bd/
# Embedded Dolt tests - test the in-process Dolt engine (no server required)
#

View file

@ -28,60 +28,60 @@ jobs:
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: bash .github/scripts/ci-embedded-tier.sh
# End-to-end coverage for proxied-server CLI subcommands. Each sub-test
# spawns a real dolt sql-server via the proxy and exercises the bd CLI
# subprocess against it (see cmd/bd/*_proxied_integration_test.go).
# # End-to-end coverage for proxied-server CLI subcommands. Each sub-test
# # spawns a real dolt sql-server via the proxy and exercises the bd CLI
# # subprocess against it (see cmd/bd/*_proxied_integration_test.go).
# #
# # The -run pattern intentionally matches the TestProxiedServer* prefix so
# # new proxied integration suites (TestProxiedServerCreate, ...Update,
# # ...Show, etc.) are auto-included without further CI edits. Adopt the
# # `TestProxiedServer<Subcommand>` naming convention for any new entry
# # points and they will be picked up here.
# #
# # Companion to test-domain-uow (which tests the storage/uow layer); this
# # job is the CLI-side gate that ensures the proxied path stays at parity
# # with the embedded path's behavior.
# #
# # The package-level testcontainers Dolt singleton (started by
# # cmd/bd/test_dolt_server_cgo_test.go) is automatically skipped when
# # BEADS_TEST_PROXIED_SERVER=1 is set, so this lane is ~50s faster than
# # it would otherwise be.
# test-proxied-server-cmd:
# name: Test (proxied-server cmd)
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
#
# The -run pattern intentionally matches the TestProxiedServer* prefix so
# new proxied integration suites (TestProxiedServerCreate, ...Update,
# ...Show, etc.) are auto-included without further CI edits. Adopt the
# `TestProxiedServer<Subcommand>` naming convention for any new entry
# points and they will be picked up here.
# - name: Set up Go
# uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6
# with:
# go-version-file: 'go.mod'
#
# Companion to test-domain-uow (which tests the storage/uow layer); this
# job is the CLI-side gate that ensures the proxied path stays at parity
# with the embedded path's behavior.
# - name: Install Dolt CLI
# run: curl -fsSL https://github.com/dolthub/dolt/releases/latest/download/install.sh | sudo bash
#
# The package-level testcontainers Dolt singleton (started by
# cmd/bd/test_dolt_server_cgo_test.go) is automatically skipped when
# BEADS_TEST_PROXIED_SERVER=1 is set, so this lane is ~50s faster than
# it would otherwise be.
test-proxied-server-cmd:
name: Test (proxied-server cmd)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: Set up Go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6
with:
go-version-file: 'go.mod'
- name: Install Dolt CLI
run: curl -fsSL https://github.com/dolthub/dolt/releases/latest/download/install.sh | sudo bash
- name: Verify dolt on PATH
run: dolt version
- name: Configure Git and Dolt identity
run: |
git config --global user.name "CI Bot"
git config --global user.email "ci@beads.test"
dolt config --global --add user.name "CI Bot"
dolt config --global --add user.email "ci@beads.test"
- name: Pre-build bd binary for proxied integration tests
# The test's buildEmbeddedBD helper picks up BEADS_TEST_BD_BINARY,
# skipping a per-test ~45s rebuild for each invocation.
run: go build -tags gms_pure_go -o /tmp/bd-proxied-test ./cmd/bd
- name: Test proxied-server bd subcommands
env:
BEADS_TEST_PROXIED_SERVER: "1"
BEADS_TEST_BD_BINARY: /tmp/bd-proxied-test
# ^TestProxiedServer matches every proxied-server top-level test
# (Create today; future Update/Show/etc. auto-join without edits).
run: go test -tags gms_pure_go -count=1 -timeout=10m -v -run '^TestProxiedServer' ./cmd/bd/
# - name: Verify dolt on PATH
# run: dolt version
#
# - name: Configure Git and Dolt identity
# run: |
# git config --global user.name "CI Bot"
# git config --global user.email "ci@beads.test"
# dolt config --global --add user.name "CI Bot"
# dolt config --global --add user.email "ci@beads.test"
#
# - name: Pre-build bd binary for proxied integration tests
# # The test's buildEmbeddedBD helper picks up BEADS_TEST_BD_BINARY,
# # skipping a per-test ~45s rebuild for each invocation.
# run: go build -tags gms_pure_go -o /tmp/bd-proxied-test ./cmd/bd
#
# - name: Test proxied-server bd subcommands
# env:
# BEADS_TEST_PROXIED_SERVER: "1"
# BEADS_TEST_BD_BINARY: /tmp/bd-proxied-test
# # ^TestProxiedServer matches every proxied-server top-level test
# # (Create today; future Update/Show/etc. auto-join without edits).
# run: go test -tags gms_pure_go -count=1 -timeout=10m -v -run '^TestProxiedServer' ./cmd/bd/
# Embedded Dolt tests - test the in-process Dolt engine (no server required)
#

View file

@ -900,12 +900,6 @@ func TestEmbeddedCreateCommitPending(t *testing.T) {
store := openStore(t, beadsDir, "cp2")
ctx := t.Context()
if pending, err := store.HasPendingChanges(ctx); err != nil {
t.Fatalf("HasPendingChanges: %v", err)
} else if pending {
t.Error("expected clean working set before write")
}
// Create an issue (writes to working set, no dolt commit in embedded mode)
issue := &types.Issue{
Title: "Pending issue",
@ -917,12 +911,6 @@ func TestEmbeddedCreateCommitPending(t *testing.T) {
t.Fatalf("CreateIssue: %v", err)
}
if pending, err := store.HasPendingChanges(ctx); err != nil {
t.Fatalf("HasPendingChanges: %v", err)
} else if !pending {
t.Error("expected dirty working set after write")
}
committed, err := store.CommitPending(ctx, "test")
if err != nil {
t.Fatalf("CommitPending: %v", err)
@ -931,18 +919,6 @@ func TestEmbeddedCreateCommitPending(t *testing.T) {
t.Error("expected commit with pending changes")
}
// The batch commit message should describe what changed (bd-6dnrw.11).
commits, err := store.Log(ctx, 1)
if err != nil {
t.Fatalf("Log: %v", err)
}
if len(commits) == 0 {
t.Fatal("expected at least one commit")
}
if msg := commits[0].Message; !strings.Contains(msg, "1 created") {
t.Errorf("expected descriptive batch commit message, got %q", msg)
}
// Second call should be no-op
committed2, err := store.CommitPending(ctx, "test")
if err != nil {

View file

@ -6,8 +6,6 @@ import (
"slices"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/issueops"
)
@ -115,79 +113,6 @@ func maybeAutoCommitStore(ctx context.Context, st storage.DoltStorage, p doltAut
return nil
}
// autoCommitSweepExemptPaths are inspection commands that display version
// control or working-set state. The unflagged-writes sweep must never run
// after them: bd dolt status would commit the dirty state it just displayed,
// destroying the inspect-before-commit flow (bd-578h9.7). Keyed by full
// command path because leaf names like "status" collide across parents.
var autoCommitSweepExemptPaths = map[string]bool{
"bd dolt status": true,
"bd vc status": true,
"bd diff": true,
"bd history": true,
}
// autoCommitSweepExempt reports whether cmd must not trigger the
// dirty-working-set sweep (bd-578h9.7). Read-only commands are exempt for a
// second reason: they open the embedded store read-only, so the sweep's
// commit would fail with errReadOnly and turn a successful read into a fatal
// error. Explicitly flagged writes (commandDidWrite) still auto-commit.
func autoCommitSweepExempt(cmd *cobra.Command) bool {
return isReadOnlyCommand(cmd.Name()) || autoCommitSweepExemptPaths[cmd.CommandPath()]
}
// formatDoltSweepCommitMessage attributes a sweep commit distinctly from a
// normal auto-commit: the swept changes belong to an EARLIER command that
// failed (or forgot commandDidWrite) before its own auto-commit could run —
// blaming them on the command that merely triggered the sweep corrupts the
// audit trail (bd-578h9.7).
func formatDoltSweepCommitMessage(cmd, actor string) string {
cmd = strings.TrimSpace(cmd)
if cmd == "" {
cmd = "write"
}
actor = strings.TrimSpace(actor)
if actor == "" {
actor = "unknown"
}
return fmt.Sprintf("bd: autocommit sweep of earlier uncommitted changes (after %s by %s)", cmd, actor)
}
// workingSetHasUnflaggedWrites reports whether the embedded working set holds
// committable changes even though no write path set commandDidWrite. It is the
// safety net behind that flag: a mutating command that forgets to set it would
// otherwise leave its writes to be swept into the NEXT command's auto-commit
// with wrong attribution (bd-6dnrw.11). Only meaningful in embedded mode with
// auto-commit "on" — batch mode keeps the working set dirty by design.
func workingSetHasUnflaggedWrites(ctx context.Context, cmdName string) bool {
if !isEmbeddedMode() {
return false
}
if mode, err := getDoltAutoCommitMode(); err != nil || mode != doltAutoCommitOn {
return false
}
st := getStore()
if st == nil {
return false
}
unwrapped := storage.UnwrapStore(st)
if lm, ok := unwrapped.(storage.LifecycleManager); ok && lm.IsClosed() {
return false
}
checker, ok := unwrapped.(interface {
HasPendingChanges(ctx context.Context) (bool, error)
})
if !ok {
return false
}
dirty, err := checker.HasPendingChanges(ctx)
if err != nil || !dirty {
return false
}
debug.Logf("command %q left uncommitted changes without setting commandDidWrite; auto-committing anyway (bd-6dnrw.11)", cmdName)
return true
}
func isDoltNothingToCommit(err error) bool {
return issueops.IsNothingToCommitError(err)
}

View file

@ -2,10 +2,7 @@ package main
import (
"errors"
"strings"
"testing"
"github.com/spf13/cobra"
)
func TestFormatDoltAutoCommitMessage(t *testing.T) {
@ -27,69 +24,6 @@ func TestFormatDoltAutoCommitMessage(t *testing.T) {
}
}
// findTestCommand walks the REAL command tree so the exemption assertions
// break if a covered command is renamed or moved.
func findTestCommand(t *testing.T, path ...string) *cobra.Command {
t.Helper()
cmd := rootCmd
for _, name := range path {
var next *cobra.Command
for _, c := range cmd.Commands() {
if c.Name() == name {
next = c
break
}
}
if next == nil {
t.Fatalf("command %q not found under %q", name, cmd.CommandPath())
}
cmd = next
}
return cmd
}
// TestAutoCommitSweepExempt covers bd-578h9.7: the dirty-working-set sweep
// must not run after inspection commands (bd dolt status would commit the
// dirty state it just displayed) or read-only commands (their store is opened
// read-only, so the sweep's commit fails and turns a successful read fatal).
func TestAutoCommitSweepExempt(t *testing.T) {
cases := []struct {
path []string
exempt bool
}{
{[]string{"dolt", "status"}, true},
{[]string{"vc", "status"}, true},
{[]string{"diff"}, true},
{[]string{"history"}, true},
{[]string{"list"}, true}, // readOnlyCommands
{[]string{"ready"}, true}, // readOnlyCommands
{[]string{"create"}, false},
{[]string{"update"}, false},
{[]string{"dolt", "commit"}, false},
{[]string{"vc", "merge"}, false},
}
for _, tc := range cases {
cmd := findTestCommand(t, tc.path...)
if got := autoCommitSweepExempt(cmd); got != tc.exempt {
t.Errorf("autoCommitSweepExempt(%q) = %v, want %v", cmd.CommandPath(), got, tc.exempt)
}
}
}
func TestFormatDoltSweepCommitMessage(t *testing.T) {
msg := formatDoltSweepCommitMessage("update", "alice")
if !strings.Contains(msg, "sweep") || !strings.Contains(msg, "update") || !strings.Contains(msg, "alice") {
t.Fatalf("sweep message must name the sweep, the triggering command, and the actor: %q", msg)
}
if msg == formatDoltAutoCommitMessage("update", "alice", nil) {
t.Fatal("sweep commits must be distinguishable from normal auto-commits")
}
msg = formatDoltSweepCommitMessage("", "")
if !strings.Contains(msg, "write") || !strings.Contains(msg, "unknown") {
t.Fatalf("unexpected fallback message: %q", msg)
}
}
func TestIsDoltNothingToCommit(t *testing.T) {
if isDoltNothingToCommit(nil) {
t.Fatal("nil error should not be treated as nothing-to-commit")

View file

@ -0,0 +1,40 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
// TestGOMODCACHENotUnderTestHome guards the testMainInner fix that pins
// GOMODCACHE before re-homing HOME to a temp dir. The default module cache
// is $HOME/go/pkg/mod; if it is left to follow the fake test HOME, every
// in-test `go build` (buildEmbeddedBD) re-downloads the full module set —
// slow, and a hard failure when offline.
//
// By the time any test runs, TestMain -> testMainInner has already:
// - set HOME to a temp dir (testTempRoot), and
// - pinned GOMODCACHE to the real module cache.
//
// So GOMODCACHE must be set and must NOT resolve under the current HOME.
func TestGOMODCACHENotUnderTestHome(t *testing.T) {
home := os.Getenv("HOME")
if home == "" {
t.Skip("HOME not set; testMainInner did not run")
}
modcache := os.Getenv("GOMODCACHE")
if modcache == "" {
t.Fatalf("GOMODCACHE is empty after test setup: the module cache will default to $HOME/go/pkg/mod (%s), forcing buildEmbeddedBD to re-download every dependency into the temp HOME",
filepath.Join(home, "go", "pkg", "mod"))
}
absHome, _ := filepath.Abs(home)
absCache, _ := filepath.Abs(modcache)
rel, err := filepath.Rel(absHome, absCache)
if err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) && rel != "." {
t.Fatalf("GOMODCACHE (%s) resolves under the test HOME (%s): the module cache followed the fake HOME, so buildEmbeddedBD must re-download all modules",
absCache, absHome)
}
}

View file

@ -120,6 +120,12 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
if os.Getenv("BEADS_DOLT_PROXIED_SERVER") == "1" {
initProxiedServer = true
}
if initProxiedServer {
// Proxied-server mode has no local Dolt init lifecycle yet. When it
// is implemented, that path must mark any local .dolt/ it creates or
// acknowledges with doltserver.MarkDoltDirCompatible.
FatalError("--proxied-server is not yet implemented")
}
if initProxiedServer && initServerMode {
FatalError("--server and --proxied-server are mutually exclusive")
}
@ -129,13 +135,6 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
FatalError("--proxied-server cannot be combined with --shared-server, --external, or any --server-* flag")
}
}
if initProxiedServer && !proxiedServerInitUngated() {
// Dark-launch gate (bd-6dnrw.44): stays until the remaining P1
// decisions land (TLS, auth). The env escape exists so the proxied
// integration suites and their CI lane can bootstrap real
// workspaces without opening the user surface.
FatalError("--proxied-server is not yet implemented")
}
if serverConfigPath != "" {
if !initProxiedServer {
FatalError("--proxied-server-config-path requires --proxied-server")

View file

@ -144,15 +144,12 @@ func runInitProxiedServer(cmd *cobra.Command, ctx context.Context, in initProxie
fmt.Fprintf(os.Stderr, "Warning: failed to initialize version tracking: %v\n", fsResult.LocalVersionErr)
}
// Deliberately a local, not the package-global uowProvider: init owns this
// provider's whole lifecycle and closes it before the tail runs.
provider, err := newProxiedServerUOWProvider(ctx, beadsDir)
uowProvider, err := newProxiedServerUOWProvider(ctx, beadsDir)
if err != nil {
FatalError("failed to open uow provider: %v", err)
}
defer func() { _ = provider.Close(ctx) }()
uw, err := provider.NewUOW(ctx)
uw, err := uowProvider.NewUOW(ctx)
if err != nil {
FatalError("failed to open unit of work: %v", err)
}

View file

@ -1,274 +0,0 @@
//go:build cgo
package main
// Seam B of bd-6dnrw.45: CLI-level parity between direct (embedded) and
// proxied-server list semantics. One embedded project and one proxied
// project receive the same fixture (creates issued in the same order), and
// every list invocation must produce the same TITLE sequence — IDs differ
// across projects, titles are the stable key.
//
// Runs in the test-proxied-server-cmd CI lane via the ^TestProxiedServer
// naming convention.
import (
"bytes"
"encoding/json"
"os/exec"
"strings"
"testing"
"time"
"github.com/steveyegge/beads/internal/types"
)
type parityEnv struct {
name string
dir string
env []string
}
func (e parityEnv) run(bd string, args ...string) (string, string, error) {
cmd := exec.Command(bd, args...)
cmd.Dir = e.dir
cmd.Env = e.env
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
func (e parityEnv) create(t *testing.T, bd string, args ...string) {
t.Helper()
fullArgs := append([]string{"create"}, args...)
stdout, stderr, err := e.run(bd, fullArgs...)
if err != nil {
t.Fatalf("[%s] bd create %s failed: %v\nstdout:\n%s\nstderr:\n%s",
e.name, strings.Join(args, " "), err, stdout, stderr)
}
}
func (e parityEnv) listTitles(t *testing.T, bd string, args ...string) []string {
t.Helper()
fullArgs := append([]string{"list", "--json"}, args...)
stdout, stderr, err := e.run(bd, fullArgs...)
if err != nil {
t.Fatalf("[%s] bd list --json %s failed: %v\nstdout:\n%s\nstderr:\n%s",
e.name, strings.Join(args, " "), err, stdout, stderr)
}
return parseListTitles(t, e.name, stdout)
}
func parseListTitles(t *testing.T, envName, stdout string) []string {
t.Helper()
start := strings.Index(stdout, "[")
if start < 0 {
if strings.Contains(stdout, "null") || strings.TrimSpace(stdout) == "" {
return nil
}
t.Fatalf("[%s] no JSON array found in list output:\n%s", envName, stdout)
}
var issues []*types.IssueWithCounts
if err := json.Unmarshal([]byte(stdout[start:]), &issues); err != nil {
t.Fatalf("[%s] failed to parse list output: %v\nraw: %s", envName, err, stdout[start:])
}
titles := make([]string, len(issues))
for i, issue := range issues {
titles[i] = issue.Title
}
return titles
}
// sleepToNextSecond parks until just past the next wall-clock second.
// created_at is DATETIME (second resolution); per-project creates must land
// in strictly increasing seconds or default-sort tie-breaking (by random
// hash ID) would make the two projects' orderings legitimately diverge.
func sleepToNextSecond() {
now := time.Now()
time.Sleep(now.Truncate(time.Second).Add(time.Second + 50*time.Millisecond).Sub(now))
}
func TestProxiedServerListParity(t *testing.T) {
requireProxiedServerEnv(t)
bd := buildEmbeddedBD(t)
// Proxied project.
pp := bdProxiedInit(t, bd, "par")
proxied := parityEnv{name: "proxied", dir: pp.dir, env: bdProxiedEnv(pp.dir)}
// Embedded project.
eDir := t.TempDir()
initGitRepoAt(t, eDir)
embedded := parityEnv{name: "embedded", dir: eDir, env: bdEnv(eDir)}
if stdout, stderr, err := embedded.run(bd, "init", "--quiet", "--prefix", "par",
"--non-interactive", "--skip-hooks", "--skip-agents"); err != nil {
t.Fatalf("embedded bd init failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
}
seedParityFixture(t, bd, embedded, proxied)
// Same list invocation against both projects must yield the same title
// sequence. Mirrors the seedProxiedListData filter surface.
cases := []struct {
name string
args []string
}{
{"all", []string{"--all", "--limit", "0"}},
{"default", nil},
{"status_open", []string{"--status", "open", "--limit", "0"}},
{"type_task", []string{"--type", "task", "--all", "--limit", "0"}},
{"type_bug", []string{"--type", "bug", "--all", "--limit", "0"}},
{"priority_1", []string{"--priority", "1", "--all", "--limit", "0"}},
{"assignee_alice", []string{"--assignee", "alice", "--all", "--limit", "0"}},
{"label_and", []string{"--label", "backend", "--label", "urgent", "--limit", "0"}},
{"label_any", []string{"--label-any", "backend,frontend", "--limit", "0"}},
{"ready", []string{"--ready", "--limit", "0"}},
{"limit_3", []string{"--limit", "3"}},
{"limit_boundary", []string{"--limit", "12"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
eTitles := embedded.listTitles(t, bd, tc.args...)
pTitles := proxied.listTitles(t, bd, tc.args...)
if len(eTitles) == 0 && tc.name != "default" {
t.Errorf("embedded returned no rows for %v — fixture or filter broken", tc.args)
}
assertSameTitleSequence(t, tc.args, eTitles, pTitles)
})
}
// Documented asymmetry: --offset is rejected in direct mode and honored
// under --proxied-server (list.go FatalError vs SQL OFFSET).
t.Run("offset_asymmetry", func(t *testing.T) {
stdout, stderr, err := embedded.run(bd, "list", "--limit", "2", "--offset", "2")
if err == nil {
t.Errorf("embedded bd list --offset should be rejected, got:\n%s", stdout)
}
if !strings.Contains(stdout+stderr, "--offset is only supported under --proxied-server") {
t.Errorf("embedded --offset rejection should name the constraint, got:\nstdout: %s\nstderr: %s", stdout, stderr)
}
full := proxied.listTitles(t, bd, "--all", "--limit", "0")
if len(full) < 4 {
t.Fatalf("need >= 4 rows for the offset window, have %d", len(full))
}
page := proxied.listTitles(t, bd, "--all", "--limit", "2", "--offset", "2")
want := full[2:4]
if len(page) != 2 || page[0] != want[0] || page[1] != want[1] {
t.Errorf("proxied --limit 2 --offset 2 = %v, want %v (window of %v)", page, want, full)
}
})
// Same-on-both-sides behavior: truncation hint goes to stderr when more
// rows matched than --limit allowed.
t.Run("truncation_hint_parity", func(t *testing.T) {
for _, e := range []parityEnv{embedded, proxied} {
stdout, stderr, err := e.run(bd, "list", "--all", "--limit", "2")
if err != nil {
t.Fatalf("[%s] bd list --all --limit 2 failed: %v\nstderr:\n%s", e.name, err, stderr)
}
if !strings.Contains(stderr, "more results matched") {
t.Errorf("[%s] expected truncation hint on stderr, got stderr %q", e.name, stderr)
}
if strings.Contains(stdout, "more results matched") {
t.Errorf("[%s] truncation hint leaked into stdout:\n%s", e.name, stdout)
}
}
})
}
func assertSameTitleSequence(t *testing.T, args []string, eTitles, pTitles []string) {
t.Helper()
if len(eTitles) != len(pTitles) {
t.Errorf("list %v: embedded returned %d rows, proxied %d\nembedded: %v\nproxied: %v",
args, len(eTitles), len(pTitles), eTitles, pTitles)
return
}
for i := range eTitles {
if eTitles[i] != pTitles[i] {
t.Errorf("list %v: title sequences diverge at %d\nembedded: %v\nproxied: %v",
args, i, eTitles, pTitles)
return
}
}
}
// seedParityFixture creates the 12-issue seedProxiedListData shape in BOTH
// projects, interleaved so each project's creates land in strictly
// increasing created_at seconds (see sleepToNextSecond).
func seedParityFixture(t *testing.T, bd string, embedded, proxied parityEnv) {
t.Helper()
pastDue := time.Now().Add(-48 * time.Hour).Format("2006-01-02")
fixture := [][]string{
{"Open bug", "--type", "bug", "--priority", "0", "--assignee", "alice",
"--description", "This is a bug", "--label", "backend", "--label", "urgent"},
{"Feature request", "--type", "feature", "--priority", "1", "--assignee", "bob",
"--label", "frontend"},
{"Backend task", "--type", "task", "--priority", "2", "--assignee", "alice",
"--label", "backend"},
{"Deferred chore", "--type", "chore", "--priority", "3", "--defer", "+7d"},
{"Epic with deps", "--type", "epic", "--priority", "1", "--label", "planning"},
{"Architecture decision", "--type", "decision", "--priority", "4", "--label", "pinned-ref"},
// Parent-child edges ride dotted child IDs of "Epic with deps" in
// both projects; --parent needs per-project IDs, so child rows are
// created with --parent below via a per-project lookup.
{"No desc bug", "--type", "bug", "--priority", "1"},
{"Overdue task", "--type", "task", "--priority", "1", "--assignee", "alice",
"--label", "urgent", "--due", pastDue},
{"Metadata issue", "--type", "feature", "--priority", "1", "--metadata", `{"env":"prod"}`},
{"Ready task", "--type", "task", "--priority", "0", "--label", "backend"},
}
for _, args := range fixture {
embedded.create(t, bd, args...)
proxied.create(t, bd, args...)
sleepToNextSecond()
}
// Child rows need the per-project epic ID for --parent.
for _, e := range []parityEnv{embedded, proxied} {
epicID := findTitleID(t, bd, e, "Epic with deps")
e.create(t, bd, "Child task A", "--type", "task", "--priority", "2",
"--assignee", "bob", "--label", "backend", "--parent", epicID)
}
sleepToNextSecond()
for _, e := range []parityEnv{embedded, proxied} {
epicID := findTitleID(t, bd, e, "Epic with deps")
e.create(t, bd, "Child task B", "--type", "task", "--priority", "3",
"--label", "frontend", "--parent", epicID)
}
sleepToNextSecond()
for _, e := range []parityEnv{embedded, proxied} {
titles := e.listTitles(t, bd, "--all", "--limit", "0")
if len(titles) < 12 {
t.Fatalf("[%s] expected at least 12 seeded issues, got %d: %v", e.name, len(titles), titles)
}
}
}
func findTitleID(t *testing.T, bd string, e parityEnv, title string) string {
t.Helper()
fullArgs := []string{"list", "--json", "--all", "--limit", "0"}
stdout, stderr, err := e.run(bd, fullArgs...)
if err != nil {
t.Fatalf("[%s] bd list --json failed: %v\nstderr:\n%s", e.name, err, stderr)
}
start := strings.Index(stdout, "[")
if start < 0 {
t.Fatalf("[%s] no JSON array in list output:\n%s", e.name, stdout)
}
var issues []*types.IssueWithCounts
if err := json.Unmarshal([]byte(stdout[start:]), &issues); err != nil {
t.Fatalf("[%s] parse list output: %v", e.name, err)
}
for _, issue := range issues {
if issue.Title == title {
return issue.ID
}
}
t.Fatalf("[%s] no issue titled %q found", e.name, title)
return ""
}

View file

@ -618,25 +618,20 @@ func TestProxiedServerList(t *testing.T) {
// --- M. Pagination across the issues+wisps UNION ALL ---
t.Run("ready_returns_both_perm_and_wisp", func(t *testing.T) {
// The wisps table participates in --ready via the UNION path, but
// the ephemeral exclusion applies to it exactly like classic
// (bd-6dnrw.44 item 2): non-ephemeral NoHistory wisps surface by
// default, true ephemerals stay hidden.
noHistory := bdProxiedCreate(t, bd, p.dir, "NoHistory wisp ready", "--no-history")
var ephemeralIDs []string
// Create three ephemeral wisps as ready candidates. The proxied
// union path forces IncludeEphemeral=true on the wisp side, so
// these must surface in --ready alongside permanent issues.
var wispIDs []string
for i := 0; i < 3; i++ {
w := bdProxiedCreate(t, bd, p.dir, fmt.Sprintf("Wisp ready %d", i), "--ephemeral")
ephemeralIDs = append(ephemeralIDs, w.ID)
wispIDs = append(wispIDs, w.ID)
}
issues := bdProxiedListJSON(t, bd, p, "--ready", "--limit", "0")
ids := listIssueIDs(issues)
if !containsID(issues, noHistory.ID) {
t.Errorf("non-ephemeral wisp %s should appear in --ready (UNION path), got %v", noHistory.ID, ids)
}
for _, wid := range ephemeralIDs {
if containsID(issues, wid) {
t.Errorf("ephemeral wisp %s should be excluded from default --ready (classic parity), got %v", wid, ids)
for _, wid := range wispIDs {
if !containsID(issues, wid) {
t.Errorf("ephemeral wisp %s should appear in --ready (UNION path), got %v", wid, ids)
}
}
if !containsID(issues, seed.readyTask) {

View file

@ -2,7 +2,6 @@ package main
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
@ -13,7 +12,6 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/storage/dberrors"
"github.com/steveyegge/beads/internal/storage/uow"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/ui"
@ -117,14 +115,6 @@ func runListProxiedHierarchicalParent(ctx context.Context, uw uow.UnitOfWork, in
func gatherProxiedHierarchical(ctx context.Context, uw uow.UnitOfWork, parentID string, baseFilter types.IssueFilter) ([]*types.Issue, error) {
parent, err := uw.IssueUseCase().GetIssue(ctx, parentID)
if isProxiedIssueNotFound(err) {
// Classic GetIssue falls back to the wisps table; a wisp parent
// (e.g. a mol root) must be treeable here too.
parent, err = uw.IssueUseCase().GetWisp(ctx, parentID)
if isProxiedIssueNotFound(err) {
return nil, fmt.Errorf("parent issue %q not found", parentID)
}
}
if err != nil {
return nil, fmt.Errorf("error checking parent issue: %w", err)
}
@ -264,12 +254,6 @@ func runListProxiedWatch(_ *cobra.Command, ctx context.Context, in listInput) er
}
}
// isProxiedIssueNotFound reports whether a use-case Get failed because the
// row (or the whole wisps table) is absent, as opposed to a real query error.
func isProxiedIssueNotFound(err error) bool {
return errors.Is(err, sql.ErrNoRows) || dberrors.IsTableNotExist(err)
}
func emitProxiedListJSONResult(iwc []*types.IssueWithCounts, in listInput, hasMore bool) {
sortIssuesWithCounts(iwc, in.sortBy, in.reverse)
if iwc == nil {

View file

@ -1030,26 +1030,8 @@ var rootCmd = &cobra.Command{
dolt.ApplyCLIAutoStart(beadsDir, doltCfg)
if proxiedServerMode {
// Only commands with a proxied-server dispatch path may proceed:
// everything else reads the global store, which stays nil in this
// mode and would nil-panic mid-command (bd-6dnrw.44 item 1).
// Reject before spawning the proxy/dolt processes.
if !commandSupportsProxiedServer(cmd) {
FatalError("'bd %s' is not supported in proxied-server mode yet (supported: create, list, doctor, init; use 'bd list --ready' for ready work)", strings.TrimPrefix(cmd.CommandPath(), "bd "))
}
p, err := newProxiedServerUOWProvider(rootCtx, beadsDir)
if err != nil {
// #4259: same migrate-or-adopt UX as the dolt/embeddeddolt open
// paths when the remote-migrate gate refuses an in-place upgrade.
var gateErr *schema.RemoteMigrateGateError
if errors.As(err, &gateErr) {
if jsonOutput {
handleRemoteMigrateGateJSON(gateErr)
} else {
fmt.Fprint(os.Stderr, gateErr.UserMessage())
}
os.Exit(1)
}
FatalError("failed to open uow provider: %v", err)
}
uowProvider = p
@ -1186,26 +1168,9 @@ var rootCmd = &cobra.Command{
} else {
// Dolt auto-commit: after a successful write command (and after final flush),
// create a Dolt commit so changes don't remain only in the working set.
// commandDidWrite is a fast-path hint, not the sole trigger: a write path
// that forgets to set it would otherwise leak its writes into the NEXT
// command's auto-commit with wrong attribution, so a dirty working set
// also triggers the commit (bd-6dnrw.11) — except after read-only and
// inspection commands, where the sweep would commit the very state the
// command exists to display, or fail outright on a read-only store
// (bd-578h9.7). Sweep commits are attributed as sweeps: the changes
// belong to an earlier command, not this one.
if !commandDidExplicitDoltCommit {
didWrite := commandDidWrite.Load()
sweep := !didWrite && !autoCommitSweepExempt(cmd) &&
workingSetHasUnflaggedWrites(rootCtx, cmd.Name())
if didWrite || sweep {
params := doltAutoCommitParams{Command: cmd.Name()}
if sweep {
params.MessageOverride = formatDoltSweepCommitMessage(cmd.Name(), getActor())
}
if err := maybeAutoCommit(rootCtx, params); err != nil {
FatalError("dolt auto-commit failed: %v", err)
}
if commandDidWrite.Load() && !commandDidExplicitDoltCommit {
if err := maybeAutoCommit(rootCtx, doltAutoCommitParams{Command: cmd.Name()}); err != nil {
FatalError("dolt auto-commit failed: %v", err)
}
}

View file

@ -45,9 +45,6 @@ func bdProxiedEnv(dir string) []string {
"HOME="+dir,
"BEADS_DOLT_PROXIED_SERVER=1",
"BEADS_NO_DAEMON=1",
// Bypass the bd init --proxied-server dark-launch gate (bd-6dnrw.44)
// for the bd subprocesses these suites spawn.
"BEADS_TEST_PROXIED_SERVER_INIT=1",
)
}

View file

@ -3,11 +3,12 @@ package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/dolthub/dolt/go/libraries/doltcore/servercfg"
"github.com/dolthub/dolt/go/libraries/utils/filesys"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/steveyegge/beads/internal/config"
@ -21,36 +22,6 @@ const (
proxiedServerLogName = "server.log"
)
// proxiedServerCommands lists the top-level commands that have a
// proxied-server dispatch path (a usesProxiedServer() branch in their Run
// func). Every other store-backed command reads the global store, which
// stays nil in proxied-server mode — PersistentPreRun rejects them up front
// so they fail with a clear error instead of a nil-pointer panic.
// doctor and init also work in proxied-server mode, but they skip store
// init entirely and never reach the guard.
var proxiedServerCommands = map[string]bool{
"create": true,
"list": true,
}
// commandSupportsProxiedServer reports whether cmd can run in proxied-server
// mode. Subcommands (e.g. "dep add") are judged by their top-level ancestor.
func commandSupportsProxiedServer(cmd *cobra.Command) bool {
for cmd.Parent() != nil && cmd.Parent().Parent() != nil {
cmd = cmd.Parent()
}
return proxiedServerCommands[cmd.Name()]
}
// proxiedServerInitUngated reports whether the dark-launch gate on
// `bd init --proxied-server` is bypassed for this process. Test-only: the
// proxied integration suites set it so they can bootstrap real proxied
// workspaces while the user-facing init surface stays gated on the open
// bd-6dnrw.44 P1 decisions (TLS, auth).
func proxiedServerInitUngated() bool {
return os.Getenv("BEADS_TEST_PROXIED_SERVER_INIT") == "1"
}
func proxiedServerRoot(beadsDir string) string {
return filepath.Join(beadsDir, proxiedServerRootName)
}
@ -237,3 +208,84 @@ func renderProxiedServerConfig(port int) ([]byte, error) {
}
const proxiedServerListenerHost = "127.0.0.1"
// TODO: this needs to return a dolt server uow provider as the global
// uow provider used by all commands
// 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)
// }
//
// persisted, _ := configfile.Load(cfg.BeadsDir)
//
// rootPath, isCustomRoot := resolveProxiedServerRootPath(cfg.BeadsDir, persisted)
// if isCustomRoot {
// if err := validateProxiedServerRootPath(rootPath); err != nil {
// return nil, err
// }
// }
//
// configPath, err := ensureProxiedServerConfig(cfg.BeadsDir, persisted)
// if err != nil {
// return nil, err
// }
//
// logPath, isCustomLog := resolveProxiedServerLogPath(cfg.BeadsDir, persisted)
// if isCustomLog {
// if err := validateProxiedServerLogPath(logPath); 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 doltserver.NewDoltServerStore(
// ctx,
// rootPath,
// cfg.BeadsDir,
// cfg.Database,
// name, email,
// logPath,
// configPath,
// proxy.BackendLocalServer,
// false, // autoSyncToOriginRemote — wired in a future iteration
// "root",
// "", // rootPassword: proxy is loopback-only, no auth
// doltBin,
// )
// }
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
}

View file

@ -61,21 +61,6 @@ func TestProxiedServerPathHelpers(t *testing.T) {
assert.Equal(t, "/tmp/some/.beads/proxieddb/server.log", proxiedServerLogPath(bd))
}
// TestCommandSupportsProxiedServer pins the PersistentPreRun guard for
// bd-6dnrw.44 item 1: only commands with a proxied-server dispatch path may
// reach store init in proxied-server mode; everything else must be rejected
// up front (the global store stays nil and would panic mid-command).
func TestCommandSupportsProxiedServer(t *testing.T) {
assert.True(t, commandSupportsProxiedServer(createCmd))
assert.True(t, commandSupportsProxiedServer(listCmd))
assert.False(t, commandSupportsProxiedServer(readyCmd), "bd ready has no proxied dispatch; users get pointed at bd list --ready")
assert.False(t, commandSupportsProxiedServer(showCmd))
assert.False(t, commandSupportsProxiedServer(depCmd))
// Subcommands are judged by their top-level ancestor.
assert.False(t, commandSupportsProxiedServer(depAddCmd))
}
// TestInitCommandRegistersProxiedServerFlag verifies the --proxied-server flag
// is wired into initCmd. Flag-presence regression test.
func TestInitCommandRegistersProxiedServerFlag(t *testing.T) {

View file

@ -93,11 +93,13 @@ func acquireEmbeddedLock(beadsDir string, serverMode bool) (util.Unlocker, error
func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) {
cfg, err := configfile.Load(beadsDir)
if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() {
// Proxied-server workspaces have no classic store backend; they are
// served through the UOW provider (newProxiedServerUOWProvider) by
// commands with a proxied dispatch path. Reachable cross-repo, e.g.
// when hydration or routing opens a foreign proxied workspace.
return nil, fmt.Errorf("workspace %s uses dolt proxied-server mode, which cannot be opened as a classic store; only commands with proxied-server support can use it", beadsDir)
// TODO: this needs to be uow provider
return nil, fmt.Errorf("proxy server store should be uow provider")
// return newProxiedServerStore(ctx, &dolt.Config{
// BeadsDir: beadsDir,
// Database: cfg.GetDoltDatabase(),
// ProxiedServer: true,
// })
}
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
return dolt.NewFromConfig(ctx, beadsDir)
@ -164,9 +166,14 @@ func migrateHyphenatedDB(beadsDir string, cfg *configfile.Config, oldName, newNa
func newReadOnlyStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) {
cfg, err := configfile.Load(beadsDir)
if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() {
// Proxied-server workspaces have no classic store backend (see
// newDoltStoreFromConfig); read-only cross-repo opens hit this too.
return nil, fmt.Errorf("workspace %s uses dolt proxied-server mode, which cannot be opened as a classic store; only commands with proxied-server support can use it", beadsDir)
// TODO: this needs to be uow provider
return nil, fmt.Errorf("proxy server store needs to be uow provider")
// 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})

View file

@ -49,10 +49,13 @@ func acquireEmbeddedLock(_ string, _ bool) (util.Unlocker, error) {
func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) {
cfg, err := configfile.Load(beadsDir)
if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() {
// Proxied-server workspaces have no classic store backend; they are
// served through the UOW provider by commands with a proxied
// dispatch path.
return nil, fmt.Errorf("workspace %s uses dolt proxied-server mode, which cannot be opened as a classic store; only commands with proxied-server support can use it", beadsDir)
// TODO: this needs to be uow provider
return nil, fmt.Errorf("proxy server store should be uow provider")
// return newProxiedServerStore(ctx, &dolt.Config{
// BeadsDir: beadsDir,
// Database: cfg.GetDoltDatabase(),
// ProxiedServer: true,
// })
}
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
return dolt.NewFromConfig(ctx, beadsDir)
@ -64,9 +67,14 @@ func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltS
func newReadOnlyStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) {
cfg, err := configfile.Load(beadsDir)
if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() {
// Proxied-server workspaces have no classic store backend (see
// newDoltStoreFromConfig); read-only cross-repo opens hit this too.
return nil, fmt.Errorf("workspace %s uses dolt proxied-server mode, which cannot be opened as a classic store; only commands with proxied-server support can use it", beadsDir)
// TODO: this needs to be uow provider
return nil, fmt.Errorf("proxy server store needs to be uow provider")
// 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})

View file

@ -8,7 +8,6 @@ import (
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/doltserver"
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
"github.com/steveyegge/beads/internal/storage/uow"
)
@ -18,24 +17,13 @@ func newProxiedServerUOWProvider(ctx context.Context, beadsDir string) (uow.Unit
return nil, fmt.Errorf("newProxiedServerUOWProvider: beadsDir must be set")
}
// Both loads return (nil, nil) when the file is simply absent; a non-nil
// error means the file EXISTS but cannot be read or parsed. Swallowing
// either silently falls back to a fresh managed local database — reads
// return zero issues and writes land in the wrong database (split-brain,
// bd-6dnrw.44 item 6) — so refuse to proceed instead.
persisted, err := configfile.Load(beadsDir)
if err != nil {
return nil, fmt.Errorf("newProxiedServerUOWProvider: workspace config in %s is unreadable; fix or remove it rather than letting bd guess the database: %w", beadsDir, err)
}
persisted, _ := configfile.Load(beadsDir)
database := configfile.DefaultDoltDatabase
if persisted != nil {
database = persisted.GetDoltDatabase()
}
info, err := configfile.LoadProxiedServerClientInfo(beadsDir)
if err != nil {
return nil, fmt.Errorf("newProxiedServerUOWProvider: %s in %s is unreadable; refusing to fall back to a fresh managed database (fix or remove the file): %w", configfile.ProxiedServerClientInfoFileName, beadsDir, err)
}
info, _ := configfile.LoadProxiedServerClientInfo(beadsDir)
if info != nil && info.External != nil {
return newExternalProxiedServerUOWProvider(ctx, beadsDir, database, info.External)
}
@ -113,7 +101,7 @@ func newManagedProxiedServerUOWProvider(
}
}
provider, err := uow.NewDoltServerUOWProvider(
return uow.NewDoltServerUOWProvider(
ctx,
rootPath,
database,
@ -124,17 +112,4 @@ func newManagedProxiedServerUOWProvider(
"", // proxy is loopback-only, no auth
doltBin,
)
if err != nil {
return nil, err
}
// Provider warmup means the managed dolt is up, so its `dolt init` (run by
// the proxy child in rootPath) has already happened. Seed the .bd-dolt-ok
// compatibility marker so future bd versions don't mistake the database
// for a pre-0.56 embedded-mode leftover. No-op when .dolt/ is absent.
if err := doltserver.MarkDoltDirCompatible(rootPath); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to mark %s dolt-compatible: %v\n", rootPath, err)
}
return provider, nil
}

View file

@ -2,7 +2,6 @@ package main
import (
"context"
"os"
"path/filepath"
"testing"
@ -25,36 +24,6 @@ func TestNewProxiedServerUOWProvider_RoutesExternalConfigToExternalProvider(t *t
"expected external validation error proving the external code path was taken; got: %v", err)
}
// A corrupted sidecar must abort provider construction, not silently fall
// back to a fresh managed local database (bd-6dnrw.44 item 6, split-brain).
func TestNewProxiedServerUOWProvider_CorruptSidecarErrorsInsteadOfManagedFallback(t *testing.T) {
beadsDir := t.TempDir()
require.NoError(t, os.WriteFile(
filepath.Join(beadsDir, configfile.ProxiedServerClientInfoFileName),
[]byte("{not json"), 0o600))
_, err := newProxiedServerUOWProvider(context.Background(), beadsDir)
require.Error(t, err)
assert.Contains(t, err.Error(), configfile.ProxiedServerClientInfoFileName,
"error must name the unreadable sidecar; got: %v", err)
assert.Contains(t, err.Error(), "refusing to fall back",
"must refuse the managed-local fallback; got: %v", err)
}
// An unparseable workspace config must abort too — defaulting the database
// name sends writes to the wrong database.
func TestNewProxiedServerUOWProvider_CorruptWorkspaceConfigErrors(t *testing.T) {
beadsDir := t.TempDir()
require.NoError(t, os.WriteFile(
filepath.Join(beadsDir, "metadata.json"),
[]byte("{not json"), 0o600))
_, err := newProxiedServerUOWProvider(context.Background(), beadsDir)
require.Error(t, err)
assert.Contains(t, err.Error(), "workspace config",
"error must point at the workspace config; got: %v", err)
}
func TestNewExternalProxiedServerUOWProvider_CreatesRootDir(t *testing.T) {
beadsDir := t.TempDir()
external := &configfile.ExternalDoltConfig{Host: "db.invalid"}

View file

@ -162,4 +162,15 @@ func TestIsLocked(t *testing.T) {
t.Error("IsLocked should return false for non-lock errors")
}
})
// born-failing guard for the errors.Is fix (commit 1a83cb08c): pre-fix
// pointer-equality (err == errProcessLocked) returned false for a wrapped
// ErrLocked, so callers using fmt.Errorf("...: %w", ErrLocked) lost
// lock-contention detection.
t.Run("true for wrapped ErrLocked", func(t *testing.T) {
wrapped := fmt.Errorf("acquire lock: %w", ErrLocked)
if !IsLocked(wrapped) {
t.Errorf("IsLocked should return true for wrapped ErrLocked, got false for: %v", wrapped)
}
})
}

View file

@ -32,27 +32,13 @@ func IsUpstreamMismatch(err error) bool {
return errors.As(err, &m)
}
func intendedUpstreamID(rootDir string, opts OpenOpts) string {
switch opts.Backend {
case BackendExternal:
func intendedUpstreamID(opts OpenOpts) string {
if opts.Backend == BackendExternal {
return server.ExternalDoltServerID(opts.External)
case BackendLocalServer:
return server.LocalDoltServerID(rootDir)
}
return ""
}
func checkUpstream(rootDir, want string, pf *pidfile.PidFile) error {
if want != "" && pf.UpstreamID != "" && pf.UpstreamID != want {
return &ErrUpstreamMismatch{
RootDir: rootDir,
Want: want,
Have: pf.UpstreamID,
}
}
return nil
}
type Endpoint struct {
Host string
Port int
@ -119,13 +105,17 @@ func GetCreateDatabaseProxyServerEndpoint(rootDir string, opts OpenOpts) (Endpoi
poll := time.NewTicker(openPollInterval)
defer poll.Stop()
want := intendedUpstreamID(rootDir, opts)
want := intendedUpstreamID(opts)
var lastSpawnErr error
for {
if ep, pf, ok := readAndDial(rootDir); ok {
if err := checkUpstream(rootDir, want, pf); err != nil {
return Endpoint{}, err
if want != "" && pf.UpstreamID != "" && pf.UpstreamID != want {
return Endpoint{}, &ErrUpstreamMismatch{
RootDir: rootDir,
Want: want,
Have: pf.UpstreamID,
}
}
return ep, nil
}
@ -164,19 +154,19 @@ func spawnAndHandoff(rootDir string, opts OpenOpts, deadline time.Time, lock *ut
// readers into dialing a port that nobody is listening on.
_ = pidfile.Remove(rootDir, PIDFileName)
// Probe the proxy-child flock. Held: a previous proxy-child is still
// Probe the proxy-child flock: if held, a previous proxy-child is still
// alive and has an orphaned dolt sql-server we must kill before
// respawning. Acquired: no proxy-child survives, but a SIGKILLed one
// leaves its dolt sql-server orphaned (the flock dies with its holder;
// the grandchild process does not) — still holding the dolt data-dir
// lock, which would wedge every respawn. Either way, kill whatever live
// process the child pidfile names, then release the flock so the child
// we are about to spawn can take it.
// respawning. If we can acquire it, no proxy-child is running — release
// immediately so the child we are about to spawn can take it.
if l, err := util.TryLock(filepath.Join(rootDir, server.LockFileName)); err == nil {
reapPidfileProcess(rootDir, server.PIDFileName)
l.Unlock()
} else if lockfile.IsLocked(err) {
reapPidfileProcess(rootDir, server.PIDFileName)
if pf, perr := pidfile.Read(rootDir, server.PIDFileName); perr == nil && pf != nil {
if proc, ferr := os.FindProcess(pf.Pid); ferr == nil {
_ = proc.Kill()
}
_ = pidfile.Remove(rootDir, server.PIDFileName)
}
}
port, err := PickFreePort()
@ -195,15 +185,8 @@ func spawnAndHandoff(rootDir string, opts OpenOpts, deadline time.Time, lock *ut
poll := time.NewTicker(openPollInterval)
defer poll.Stop()
want := intendedUpstreamID(rootDir, opts)
for {
if ep, pf, ok := readAndDial(rootDir); ok {
// Our child can lose the spawn race to a proxy fronting a
// different upstream; the winner's endpoint must fail the same
// check the steady-state discovery path applies.
if err := checkUpstream(rootDir, want, pf); err != nil {
return Endpoint{}, err
}
if ep, _, ok := readAndDial(rootDir); ok {
return ep, nil
}
select {
@ -309,46 +292,11 @@ func forkExecChild(rootDir string, opts OpenOpts, port int, lock *util.Lock) (*e
return cmd, done, nil
}
// reapConfirmDeadline bounds how long reapPidfileProcess waits for the killed
// process to disappear. A SIGKILLed dolt that is still a child of a live
// proxy-child stays a zombie until that parent reaps it, so death is awaited
// best-effort, not to certainty.
const reapConfirmDeadline = 5 * time.Second
// reapPidfileProcess kills the process the pidfile names, waits (bounded) for
// it to exit so the respawned dolt sql-server doesn't race the dying one for
// the data-dir lock, and removes the pidfile. A pidfile whose pid is already
// dead is simply stale and is removed without a kill.
func reapPidfileProcess(rootDir, pidName string) {
pf, err := pidfile.Read(rootDir, pidName)
if err != nil || pf == nil {
return
}
if pf.Pid > 0 && pidAlive(pf.Pid) {
if proc, ferr := os.FindProcess(pf.Pid); ferr == nil {
_ = proc.Kill()
}
deadline := time.Now().Add(reapConfirmDeadline)
for pidAlive(pf.Pid) && time.Now().Before(deadline) {
time.Sleep(50 * time.Millisecond)
}
}
_ = pidfile.Remove(rootDir, pidName)
}
func readAndDial(rootDir string) (Endpoint, *pidfile.PidFile, bool) {
pf, err := pidfile.Read(rootDir, PIDFileName)
if err != nil || pf == nil {
return Endpoint{}, nil, false
}
// A dead writer means a stale pidfile: after port reuse an arbitrary
// process could be listening on the recorded port, so a bare TCP probe
// must never be trusted on the word of a dead proxy. (Stale files are
// removed under proxy.lock in spawnAndHandoff, not here, so a racing
// starter's freshly written pidfile can't be deleted out from under it.)
if pf.Pid <= 0 || !pidAlive(pf.Pid) {
return Endpoint{}, nil, false
}
ep := Endpoint{Host: "127.0.0.1", Port: pf.Port}
if !probePort(ep, 500*time.Millisecond) {
return Endpoint{}, nil, false

View file

@ -1,173 +0,0 @@
package proxy
import (
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage/dbproxy/pidfile"
"github.com/steveyegge/beads/internal/storage/dbproxy/server"
"github.com/steveyegge/beads/internal/storage/dbproxy/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// deadPid returns the pid of a process that has already exited and been
// reaped, i.e. a pid that pidAlive must report dead.
func deadPid(t *testing.T) int {
t.Helper()
cmd := exec.Command("true")
require.NoError(t, cmd.Start())
pid := cmd.Process.Pid
require.NoError(t, cmd.Wait())
return pid
}
// startSleeper starts a long-running child process and arranges for it to be
// reaped as soon as it dies, so pidAlive doesn't see a lingering zombie.
func startSleeper(t *testing.T) *exec.Cmd {
t.Helper()
cmd := exec.Command("sleep", "60")
require.NoError(t, cmd.Start())
done := make(chan struct{})
go func() {
defer close(done)
_ = cmd.Wait()
}()
t.Cleanup(func() {
_ = cmd.Process.Kill()
<-done
})
return cmd
}
func skipOnWindows(t *testing.T) {
t.Helper()
if runtime.GOOS == "windows" {
t.Skip("test scaffolding uses unix coreutils (true/sleep)")
}
}
func TestPidAlive(t *testing.T) {
skipOnWindows(t)
assert.True(t, pidAlive(os.Getpid()))
assert.False(t, pidAlive(deadPid(t)))
}
func TestReadAndDial_DeadPidfileWriterRejected(t *testing.T) {
skipOnWindows(t)
root := t.TempDir()
// A listener really is up on the recorded port — the bare TCP probe
// would succeed. The dead writer pid alone must disqualify the pidfile.
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(func() { _ = ln.Close() })
port := ln.Addr().(*net.TCPAddr).Port
require.NoError(t, pidfile.Write(root, PIDFileName, pidfile.PidFile{Pid: deadPid(t), Port: port}))
_, _, ok := readAndDial(root)
assert.False(t, ok, "stale pidfile (dead writer) must not be trusted even with a live listener")
require.NoError(t, pidfile.Write(root, PIDFileName, pidfile.PidFile{Pid: os.Getpid(), Port: port}))
ep, pf, ok := readAndDial(root)
require.True(t, ok)
assert.Equal(t, port, ep.Port)
assert.Equal(t, os.Getpid(), pf.Pid)
}
func TestReapPidfileProcess_KillsLiveOrphan(t *testing.T) {
skipOnWindows(t)
root := t.TempDir()
orphan := startSleeper(t)
require.NoError(t, pidfile.Write(root, server.PIDFileName, pidfile.PidFile{Pid: orphan.Process.Pid}))
reapPidfileProcess(root, server.PIDFileName)
assert.False(t, pidAlive(orphan.Process.Pid), "orphan should be dead after reap")
pf, err := pidfile.Read(root, server.PIDFileName)
require.NoError(t, err)
assert.Nil(t, pf, "pidfile should be removed after reap")
}
func TestReapPidfileProcess_StalePidfileJustRemoved(t *testing.T) {
skipOnWindows(t)
root := t.TempDir()
require.NoError(t, pidfile.Write(root, server.PIDFileName, pidfile.PidFile{Pid: deadPid(t)}))
start := time.Now()
reapPidfileProcess(root, server.PIDFileName)
assert.Less(t, time.Since(start), reapConfirmDeadline, "dead pid must not be waited on")
pf, err := pidfile.Read(root, server.PIDFileName)
require.NoError(t, err)
assert.Nil(t, pf)
}
func TestIntendedUpstreamID_PerBackend(t *testing.T) {
root := t.TempDir()
ext := configfile.ExternalDoltConfig{Host: "10.0.0.1", Port: 3306}
assert.Equal(t, server.LocalDoltServerID(root),
intendedUpstreamID(root, OpenOpts{Backend: BackendLocalServer}))
assert.Equal(t, server.ExternalDoltServerID(ext),
intendedUpstreamID(root, OpenOpts{Backend: BackendExternal, External: ext}))
assert.Empty(t, intendedUpstreamID(root, OpenOpts{Backend: BackendLocalSharedServer}))
}
// A SIGKILLed proxy-child releases proxy-child.lock (flocks die with their
// holder) but leaves its dolt sql-server running. spawnAndHandoff must reap
// that orphan even though the child flock is acquirable.
func TestSpawnAndHandoff_ReapsOrphanWhenChildLockFree(t *testing.T) {
skipOnWindows(t)
root := t.TempDir()
orphan := startSleeper(t)
require.NoError(t, pidfile.Write(root, server.PIDFileName, pidfile.PidFile{Pid: orphan.Process.Pid}))
// Spawn a child that exits immediately so spawnAndHandoff returns fast;
// the orphan reap happens before the fork.
falseBin, err := exec.LookPath("false")
require.NoError(t, err)
prev := ResolveExecutable
ResolveExecutable = func() (string, error) { return falseBin, nil }
t.Cleanup(func() { ResolveExecutable = prev })
lock, err := util.TryLock(filepath.Join(root, LockFileName))
require.NoError(t, err)
_, spawnErr := spawnAndHandoff(root, OpenOpts{
Backend: BackendLocalServer,
LogFilePath: filepath.Join(root, "proxy-spawn.log"),
}, time.Now().Add(2*time.Second), lock)
require.Error(t, spawnErr, "child exits immediately, so spawn must fail")
assert.False(t, pidAlive(orphan.Process.Pid), "orphaned dolt must be killed before respawn")
pf, err := pidfile.Read(root, server.PIDFileName)
require.NoError(t, err)
assert.Nil(t, pf, "orphan pidfile should be removed")
}
// Shutdown promises no surviving processes even when the lock holder died
// without cleaning up its child (the orphaned-dolt case).
func TestShutdown_ReapsOrphanWhenLockFree(t *testing.T) {
skipOnWindows(t)
root := t.TempDir()
orphan := startSleeper(t)
require.NoError(t, pidfile.Write(root, server.PIDFileName, pidfile.PidFile{Pid: orphan.Process.Pid}))
require.NoError(t, Shutdown(root))
assert.False(t, pidAlive(orphan.Process.Pid), "orphan should be dead after Shutdown")
pf, err := pidfile.Read(root, server.PIDFileName)
require.NoError(t, err)
assert.Nil(t, pf)
}

View file

@ -3,7 +3,6 @@ package proxy_test
import (
"errors"
"net"
"os"
"testing"
"github.com/steveyegge/beads/internal/configfile"
@ -24,7 +23,7 @@ func TestGetCreateDatabaseProxyServerEndpoint_RejectsUpstreamMismatch(t *testing
existingCfg := configfile.ExternalDoltConfig{Host: "10.0.0.1", Port: 3306}
require.NoError(t, pidfile.Write(root, proxy.PIDFileName, pidfile.PidFile{
Pid: os.Getpid(),
Pid: 12345,
Port: port,
UpstreamID: server.ExternalDoltServerID(existingCfg),
}))
@ -55,7 +54,7 @@ func TestGetCreateDatabaseProxyServerEndpoint_ReusesMatchingUpstream(t *testing.
cfg := configfile.ExternalDoltConfig{Host: "10.0.0.1", Port: 3306}
require.NoError(t, pidfile.Write(root, proxy.PIDFileName, pidfile.PidFile{
Pid: os.Getpid(),
Pid: 12345,
Port: port,
UpstreamID: server.ExternalDoltServerID(cfg),
}))
@ -79,7 +78,7 @@ func TestGetCreateDatabaseProxyServerEndpoint_LegacyPidfileWithoutIDReused(t *te
port := ln.Addr().(*net.TCPAddr).Port
require.NoError(t, pidfile.Write(root, proxy.PIDFileName, pidfile.PidFile{
Pid: os.Getpid(),
Pid: 12345,
Port: port,
}))
@ -92,61 +91,6 @@ func TestGetCreateDatabaseProxyServerEndpoint_LegacyPidfileWithoutIDReused(t *te
assert.Equal(t, port, ep.Port)
}
func TestGetCreateDatabaseProxyServerEndpoint_RejectsLocalUpstreamMismatch(t *testing.T) {
root := t.TempDir()
otherRoot := t.TempDir()
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(func() { _ = ln.Close() })
port := ln.Addr().(*net.TCPAddr).Port
// A live proxy fronting some OTHER workspace's managed dolt (e.g. a
// recycled pid/port) must not be handed out for this rootDir.
require.NoError(t, pidfile.Write(root, proxy.PIDFileName, pidfile.PidFile{
Pid: os.Getpid(),
Port: port,
UpstreamID: server.LocalDoltServerID(otherRoot),
}))
_, err = proxy.GetCreateDatabaseProxyServerEndpoint(root, proxy.OpenOpts{
Backend: proxy.BackendLocalServer,
ConfigFilePath: root + "/dolt-config.yaml",
LogFilePath: root + "/server.log",
DoltBinPath: "dolt",
})
require.Error(t, err)
var mismatch *proxy.ErrUpstreamMismatch
require.True(t, errors.As(err, &mismatch), "expected ErrUpstreamMismatch, got %T: %v", err, err)
assert.Equal(t, server.LocalDoltServerID(root), mismatch.Want)
assert.Equal(t, server.LocalDoltServerID(otherRoot), mismatch.Have)
}
func TestGetCreateDatabaseProxyServerEndpoint_ReusesMatchingLocalUpstream(t *testing.T) {
root := t.TempDir()
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(func() { _ = ln.Close() })
port := ln.Addr().(*net.TCPAddr).Port
require.NoError(t, pidfile.Write(root, proxy.PIDFileName, pidfile.PidFile{
Pid: os.Getpid(),
Port: port,
UpstreamID: server.LocalDoltServerID(root),
}))
ep, err := proxy.GetCreateDatabaseProxyServerEndpoint(root, proxy.OpenOpts{
Backend: proxy.BackendLocalServer,
ConfigFilePath: root + "/dolt-config.yaml",
LogFilePath: root + "/server.log",
DoltBinPath: "dolt",
})
require.NoError(t, err)
assert.Equal(t, port, ep.Port)
}
func TestErrUpstreamMismatch_Message(t *testing.T) {
e := &proxy.ErrUpstreamMismatch{
RootDir: "/tmp/myserver",

View file

@ -2,19 +2,8 @@
package proxy
import (
"errors"
"syscall"
)
import "syscall"
func procAttrDetached() *syscall.SysProcAttr {
return &syscall.SysProcAttr{Setsid: true}
}
// pidAlive reports whether a process with the given pid currently exists.
// EPERM means the process exists but belongs to another user — that still
// counts as alive.
func pidAlive(pid int) bool {
err := syscall.Kill(pid, 0)
return err == nil || errors.Is(err, syscall.EPERM)
}

View file

@ -13,21 +13,3 @@ func procAttrDetached() *syscall.SysProcAttr {
CreationFlags: windows.DETACHED_PROCESS | windows.CREATE_NEW_PROCESS_GROUP,
}
}
// stillActive is GetExitCodeProcess's STILL_ACTIVE (STATUS_PENDING, 0x103),
// which x/sys/windows does not export under that name.
const stillActive = 0x103
// pidAlive reports whether a process with the given pid currently exists.
func pidAlive(pid int) bool {
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid))
if err != nil {
return false
}
defer func() { _ = windows.CloseHandle(h) }()
var code uint32
if err := windows.GetExitCodeProcess(h, &code); err != nil {
return false
}
return code == stillActive
}

View file

@ -39,11 +39,8 @@ func shutdownPair(rootDir, lockName, pidName string) error {
lockPath := filepath.Join(rootDir, lockName)
if l, err := util.TryLock(lockPath); err == nil {
// No live lock holder — but a SIGKILLed holder can leave the process
// its pidfile names still running (the orphaned-dolt case). Kill it
// too, or the no-survivors guarantee above does not hold.
reapPidfileProcess(rootDir, pidName)
l.Unlock()
_ = pidfile.Remove(rootDir, pidName)
return nil
} else if !lockfile.IsLocked(err) {
return fmt.Errorf("probe %s: %w", lockName, err)

View file

@ -93,8 +93,9 @@ func NewDoltServer(doltBinExec, rootDir, configPath, logFilePath string, keepAli
if keepAlivePeriod == 0 {
keepAlivePeriod = defaultKeepAlivePeriod
}
sum := sha256.Sum256([]byte(absRootDir))
return &DoltServer{
id: LocalDoltServerID(absRootDir),
id: hex.EncodeToString(sum[:]),
doltBinExec: absDoltBinExec,
rootDir: absRootDir,
configPath: absConfigPath,
@ -108,19 +109,6 @@ func (s *DoltServer) ID(_ context.Context) string {
return s.id
}
// LocalDoltServerID is the upstream ID a managed local DoltServer advertises
// for rootDir. Discovery uses it to verify that a proxy found via pidfile
// actually fronts this workspace's dolt and not some other instance behind a
// recycled pid/port.
func LocalDoltServerID(rootDir string) string {
abs, err := filepath.Abs(rootDir)
if err != nil {
abs = rootDir
}
sum := sha256.Sum256([]byte(abs))
return hex.EncodeToString(sum[:])
}
func (s *DoltServer) DSN(_ context.Context, database, user, password string) string {
dsn := util.DoltServerDSN{
User: user,
@ -139,10 +127,12 @@ func (s *DoltServer) DSN(_ context.Context, database, user, password string) str
return dsn.String()
}
// committerIdentity resolves the name/email commits in this managed repo
// should carry: git config when available, beads defaults otherwise.
func committerIdentity(ctx context.Context) (name, email string) {
name, email = "beads", "beads@localhost"
func (s *DoltServer) doltConfigure(ctx context.Context) error {
probe := exec.CommandContext(ctx, s.doltBinExec, "config", "--global", "--get", "user.name")
if out, err := probe.Output(); err == nil && strings.TrimSpace(string(out)) != "" {
return nil
}
name, email := "beads", "beads@localhost"
if out, err := exec.CommandContext(ctx, "git", "config", "user.name").Output(); err == nil {
if v := strings.TrimSpace(string(out)); v != "" {
name = v
@ -153,25 +143,11 @@ func committerIdentity(ctx context.Context) (name, email string) {
email = v
}
}
return name, email
}
// doltConfigureLocal pins the committer identity in rootDir's repo-local
// config so sql-server commits work on machines with no global dolt config.
// The previous version of this step wrote --global, silently mutating the
// user's dolt identity from a background server spawn (bd-6dnrw.44 P3).
func (s *DoltServer) doltConfigureLocal(ctx context.Context, name, email string) error {
probe := exec.CommandContext(ctx, s.doltBinExec, "config", "--local", "--get", "user.name")
probe.Dir = s.rootDir
if out, err := probe.Output(); err == nil && strings.TrimSpace(string(out)) != "" {
return nil
if out, err := exec.CommandContext(ctx, s.doltBinExec, "config", "--global", "--add", "user.name", name).CombinedOutput(); err != nil {
return fmt.Errorf("server: DoltServer.doltConfigure: set user.name: %w\n%s", err, out)
}
for _, kv := range [][2]string{{"user.name", name}, {"user.email", email}} {
cmd := exec.CommandContext(ctx, s.doltBinExec, "config", "--local", "--add", kv[0], kv[1])
cmd.Dir = s.rootDir
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("server: DoltServer.doltConfigureLocal: set %s: %w\n%s", kv[0], err, out)
}
if out, err := exec.CommandContext(ctx, s.doltBinExec, "config", "--global", "--add", "user.email", email).CombinedOutput(); err != nil {
return fmt.Errorf("server: DoltServer.doltConfigure: set user.email: %w\n%s", err, out)
}
return nil
}
@ -181,18 +157,16 @@ func (s *DoltServer) doltInit(ctx context.Context) error {
return fmt.Errorf("server: DoltServer.doltInit: mkdir %q: %w", s.rootDir, err)
}
name, email := committerIdentity(ctx)
// --name/--email let init succeed without global dolt config.
cmd := exec.CommandContext(ctx, s.doltBinExec, "init", "--name", name, "--email", email)
cmd := exec.CommandContext(ctx, s.doltBinExec, "init")
cmd.Dir = s.rootDir
if out, err := cmd.CombinedOutput(); err != nil {
if strings.Contains(string(out), "already been initialized") {
return s.doltConfigureLocal(ctx, name, email)
return nil
}
return fmt.Errorf("server: DoltServer.doltInit: %w\n%s", err, out)
}
return s.doltConfigureLocal(ctx, name, email)
return nil
}
var retryableDoltInitErrSubstrings = []string{
@ -243,6 +217,11 @@ func (s *DoltServer) Start(ctx context.Context) error {
return fmt.Errorf("server: DoltServer.Start: acquire %s: %w", LockFileName, err)
}
if err := s.doltConfigure(ctx); err != nil {
lock.Unlock()
return err
}
if err := s.doltInitWithRetries(ctx); err != nil {
lock.Unlock()
return err

View file

@ -368,11 +368,12 @@ func TestDoltServer_ConcurrentStart_SameRootDir_OneWins(t *testing.T) {
bin := requireDolt(t)
t.Setenv("HOME", t.TempDir())
// No global dolt config on purpose: init identity comes from the
// --name/--email flags and repo-local config, both written under the
// rootDir lock, so concurrent Starts have no shared config file to race
// on (the old --global write needed pre-seeding here to avoid exactly
// that race).
// Pre-configure dolt's global user.name/email so doltConfigure is a
// no-op for every concurrent Start (no JSON-write race on
// ~/.dolt/config_global.json).
require.NoError(t, exec.Command(bin, "config", "--global", "--add", "user.name", "beads-test").Run())
require.NoError(t, exec.Command(bin, "config", "--global", "--add", "user.email", "beads@test").Run())
rootDir := t.TempDir()
const n = 10

View file

@ -39,13 +39,21 @@ func TryLock(lockPath string) (*Lock, error) {
return &Lock{f: f}, nil
}
// File returns the underlying *os.File. Useful for fork+exec lock-fd
// inheritance: pass it via cmd.ExtraFiles, then Close() the parent's fd —
// the child retains the lock through its inherited fd, which references the
// same open file description.
func (l *Lock) File() *os.File {
return l.f
}
// Unlock releases the flock and closes the underlying file. Panics on
// failure to prevent silent deadlocks.
//
// There is intentionally no accessor for the underlying fd: the fork+exec
// lock-fd handoff it would enable was never wired up — spawned children
// acquire their own lock and signal contention via proxy.LockHeldExitCode
// instead.
// Do NOT call Unlock in fork+exec handoff scenarios where the lock fd has
// been inherited by a child: flock's LOCK_UN releases the lock from the OFD,
// which would silently drop the child's lock too. In that case use
// l.File().Close() to drop only the parent's fd reference.
func (l *Lock) Unlock() {
if err := lockfile.FlockUnlock(l.f); err != nil {
panic(fmt.Sprintf("util: failed to release lock: %v", err))

View file

@ -1823,11 +1823,20 @@ func (s *DoltStore) doltAddAndCommit(ctx context.Context, tables []string, commi
// This is the primary commit mechanism for batch mode, where multiple bd commands
// accumulate changes in the working set before committing at a logical boundary.
func (s *DoltStore) CommitPending(ctx context.Context, actor string) (bool, error) {
pending, err := s.HasPendingChanges(ctx)
// Check if there are any committable changes (excluding dolt_ignore'd tables
// like wisp tables, which appear in dolt_status but can't be staged).
var count int
err := s.db.QueryRowContext(ctx, `
SELECT COUNT(*) FROM dolt_status s
WHERE NOT EXISTS (
SELECT 1 FROM dolt_ignore di
WHERE di.ignored = 1
AND s.table_name LIKE di.pattern
)`).Scan(&count)
if err != nil {
return false, err
return false, fmt.Errorf("failed to check status: %w", err)
}
if !pending {
if count == 0 {
return false, nil // Nothing to commit
}
@ -1847,13 +1856,6 @@ func (s *DoltStore) CommitPending(ctx context.Context, actor string) (bool, erro
return true, nil
}
// HasPendingChanges reports whether the working set has committable changes,
// excluding dolt_ignore'd tables (e.g. wisp tables, which can sit dirty in
// dolt_status indefinitely without being committable).
func (s *DoltStore) HasPendingChanges(ctx context.Context) (bool, error) {
return issueops.HasPendingChanges(ctx, s.db)
}
// buildBatchCommitMessage generates a descriptive commit message summarizing
// what changed since the last commit by querying dolt_diff against HEAD.
// It reports issue-level create/update/delete counts and lists any other

View file

@ -74,3 +74,47 @@ func (s *testSuite) TestGetDescendantsDottedOrphans() {
s.ElementsMatch([]string{"bd-tree-c", "bd-tree-c.7", "bd-tree-r.1", "bd-tree-r.1.2", "bd-tree-m"},
ids, "SkipWisps must drop the wisp rows but keep the dotted-ID issue walk")
}
// TestGetDescendantsFilteredByStatus guards the dolt 2.1.6 analyzer
// workaround (commit 341c7a5a4): when GetDescendants carries a level filter,
// each branch of the recursive descendants CTE references the same
// `id IN (SELECT id FROM <table> WHERE ...)` predicate. Inlining that
// subquery into 3+ branches trips the analyzer ("unable to find field with
// index N in row of M columns"); hoisting it into a named non-recursive CTE
// (issue_matches / wisp_matches) dodges it. The existing dotted-orphans test
// uses an empty filter and so never builds the predicate — this test does.
func (s *testSuite) TestGetDescendantsFilteredByStatus() {
r := s.issueRepo()
deps := s.depRepo()
mk := func(id string, st types.Status) {
iss := newTestIssue(id, "f "+id)
iss.Status = st
s.Require().NoError(r.Insert(s.Ctx(), iss, "tester", domain.InsertIssueOpts{}))
}
mk("bd-f-r", types.StatusOpen) // root
mk("bd-f-a", types.StatusOpen) // open edge child
mk("bd-f-b", types.StatusClosed) // closed edge child (must be filtered out)
mk("bd-f-r.1", types.StatusOpen) // open dotted orphan
mk("bd-f-r.2", types.StatusClosed) // closed dotted orphan (must be filtered out)
for _, e := range []struct{ child, parent string }{
{"bd-f-a", "bd-f-r"},
{"bd-f-b", "bd-f-r"},
} {
s.Require().NoError(deps.Insert(s.Ctx(),
&types.Dependency{IssueID: e.child, DependsOnID: e.parent, Type: types.DepParentChild},
"tester", domain.DepInsertOpts{}))
}
st := types.StatusOpen
got, err := r.GetDescendants(s.Ctx(), "bd-f-r", types.IssueFilter{Status: &st})
s.Require().NoError(err) // dolt analyzer bug surfaces here without the named-CTE hoist
ids := make([]string, len(got))
for i, g := range got {
ids[i] = g.ID
}
s.ElementsMatch([]string{"bd-f-a", "bd-f-r.1"}, ids,
"only open descendants must be returned; the per-level filter must apply across edge and dotted branches")
}

View file

@ -595,13 +595,7 @@ func (s *EmbeddedDoltStore) CLIDir() string {
// implemented in version_control.go via versioncontrolops.
func (s *EmbeddedDoltStore) CommitPending(ctx context.Context, actor string) (bool, error) {
// Best-effort descriptive message summarizing the accumulated working-set
// changes (bd-6dnrw.11); fall back to a generic one if the query fails.
msg := fmt.Sprintf("bd: commit pending changes by %s", actor)
_ = s.withConn(ctx, false, func(tx *sql.Tx) error {
msg = issueops.BuildBatchCommitMessage(ctx, tx, actor)
return nil
})
if err := s.Commit(ctx, msg); err != nil {
if issueops.IsNothingToCommitError(err) {
return false, nil
@ -611,19 +605,6 @@ func (s *EmbeddedDoltStore) CommitPending(ctx context.Context, actor string) (bo
return true, nil
}
// HasPendingChanges reports whether the working set has committable changes,
// excluding dolt_ignore'd tables (e.g. wisp tables, which can sit dirty in
// dolt_status indefinitely without being committable).
func (s *EmbeddedDoltStore) HasPendingChanges(ctx context.Context) (bool, error) {
var pending bool
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
var err error
pending, err = issueops.HasPendingChanges(ctx, tx)
return err
})
return pending, err
}
// CommitExists is implemented in version_control.go via versioncontrolops.
func (s *EmbeddedDoltStore) GetCurrentCommit(ctx context.Context) (string, error) {

View file

@ -52,7 +52,6 @@ func (p *doltSQLProvider) BeginTx(ctx context.Context) (Tx, error) {
_, err = conn.ExecContext(ctx, "START TRANSACTION;")
if err != nil {
_ = conn.Close()
return nil, fmt.Errorf("uow: failed to start transaction: %w", err)
}
@ -61,18 +60,14 @@ func (p *doltSQLProvider) BeginTx(ctx context.Context) (Tx, error) {
}, nil
}
func (p *doltSQLProvider) initSchema(ctx context.Context, database string, hasRemoteProbe func() bool) error {
func (p *doltSQLProvider) initSchema(ctx context.Context, database string) error {
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 25 * time.Millisecond
bo.MaxElapsedTime = 15 * time.Second
return backoff.Retry(func() error {
// Dial/handshake failures here are the NORMAL warmup case — the
// freshly spawned child server can accept TCP before the SQL engine
// answers — so only mark genuinely permanent errors Permanent
// (bd-6dnrw.44 item 8).
conn, err := p.db.Conn(ctx)
if err != nil {
if isRetryableWarmupError(err) {
if isSerializationError(err) {
return fmt.Errorf("uow: pin connection: %w", err)
}
return backoff.Permanent(fmt.Errorf("uow: pin connection: %w", err))
@ -81,35 +76,14 @@ func (p *doltSQLProvider) initSchema(ctx context.Context, database string, hasRe
ddl := db.NewDDLSQLRepository(conn)
if err := ddl.CreateDatabaseIfNotExists(ctx, database); err != nil {
if isRetryableWarmupError(err) {
return fmt.Errorf("uow: creating database: %w", err)
}
return backoff.Permanent(fmt.Errorf("uow: creating database: %w", err))
}
if err := ddl.UseDatabase(ctx, database); err != nil {
if isRetryableWarmupError(err) {
return fmt.Errorf("uow: switching to database: %w", err)
}
return backoff.Permanent(fmt.Errorf("uow: switching to database: %w", err))
}
// #4259: refuse to silently auto-apply pending migrations to a
// remote-backed database — the same gate the dolt and embeddeddolt
// store opens run (bd-6dnrw.28: this third store-open path used to
// bypass it). hasRemoteProbe is the on-disk fallback for a freshly
// started child server whose dolt_remotes table is still empty
// (GH#2315); nil disables it (e.g. external servers with no local
// data dir). A gate refusal is permanent — never retried into a
// migration.
if err := schema.CheckRemoteMigrateGateWithRemoteCheck(ctx, conn, hasRemoteProbe); err != nil {
if isRetryableWarmupError(err) {
return fmt.Errorf("uow: remote-migrate gate: %w", err)
}
return backoff.Permanent(fmt.Errorf("uow: remote-migrate gate: %w", err))
}
if _, err := schema.MigrateUpWithLock(ctx, conn, database); err != nil {
if isRetryableWarmupError(err) || schema.IsMigrationLockError(err) {
if isSerializationError(err) || schema.IsMigrationLockError(err) {
return fmt.Errorf("uow: migrate: %w", err)
}
return backoff.Permanent(fmt.Errorf("uow: migrate: %w", err))
@ -139,10 +113,7 @@ func openDB(ctx context.Context, dsn string) (*sql.DB, error) {
return conn, nil
}
// openAndInitSchema connects to the proxied server, initializes the schema,
// and returns a ready provider. hasRemoteProbe is the remote-migrate gate's
// on-disk remote probe (nil to rely on dolt_remotes alone — see initSchema).
func openAndInitSchema(ctx context.Context, ep proxy.Endpoint, database, rootUser, rootPassword string, hasRemoteProbe func() bool) (UnitOfWorkProvider, error) {
func openAndInitSchema(ctx context.Context, ep proxy.Endpoint, database, rootUser, rootPassword string) (UnitOfWorkProvider, error) {
initDB, err := openDB(ctx, buildDSN(ep, "", rootUser, rootPassword))
if err != nil {
return nil, err
@ -153,7 +124,7 @@ func openAndInitSchema(ctx context.Context, ep proxy.Endpoint, database, rootUse
db: initDB,
}
if err := initProvider.initSchema(ctx, database, hasRemoteProbe); err != nil {
if err := initProvider.initSchema(ctx, database); err != nil {
_ = initDB.Close()
return nil, fmt.Errorf("uow: init schema: %w", err)
}

View file

@ -10,7 +10,6 @@ import (
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
"github.com/steveyegge/beads/internal/storage/doltutil"
)
func NewDoltServerUOWProvider(
@ -61,27 +60,5 @@ func NewDoltServerUOWProvider(
return nil, fmt.Errorf("uow: get proxy endpoint: %w", err)
}
// On-disk remote probe for the remote-migrate gate: the child dolt
// sql-server stores the database at <serverRootDir>/<database>, and a
// freshly started server can report an empty dolt_remotes table even
// though a remote is persisted in .dolt (GH#2315). Reads
// repo_state.json directly; a read/parse failure fails open but is
// logged, never swallowed (bd-6dnrw.33).
hasRemoteProbe := func() bool {
for _, dir := range []string{filepath.Join(absServerRootDir, database), absServerRootDir} {
remotes, err := doltutil.PersistedRemotes(dir)
if err != nil {
fmt.Fprintf(os.Stderr,
"Warning: remote-migrate gate could not inspect %s for persisted remotes (assuming none): %v\n",
dir, err)
continue
}
if len(remotes) > 0 {
return true
}
}
return false
}
return openAndInitSchema(ctx, ep, database, rootUser, rootPassword, hasRemoteProbe)
return openAndInitSchema(ctx, ep, database, rootUser, rootPassword)
}

View file

@ -11,13 +11,10 @@ import (
"sync"
"syscall"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
"github.com/steveyegge/beads/internal/storage/doltutil"
"github.com/steveyegge/beads/internal/storage/schema"
"github.com/steveyegge/beads/internal/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -180,83 +177,6 @@ func TestNewDoltServerUOWProvider_ConcurrentInstantiation(t *testing.T) {
}
}
// TestNewDoltServerUOWProvider_RemoteMigrateGate_BlocksReopen mirrors
// TestDoltNew_RemoteMigrateGate_BlocksReopen for the proxied-server store-open
// path (bd-6dnrw.28): opening a proxied workspace whose database is behind the
// binary AND has a remote persisted on disk must refuse to auto-migrate with a
// *schema.RemoteMigrateGateError instead of silently forking the schema
// (gastownhall/beads#4259/#4268). The proxy/child server is shut down between
// opens so the reopen exercises the cold-start state where dolt_remotes can
// read empty and only the on-disk probe sees the remote (GH#2315).
func TestNewDoltServerUOWProvider_RemoteMigrateGate_BlocksReopen(t *testing.T) {
testutil.RequireDoltBinary(t)
bin, err := exec.LookPath("dolt")
require.NoError(t, err)
t.Setenv(schema.AllowRemoteMigrateEnv, "0")
bdBin := buildBDBinary(t)
prev := proxy.ResolveExecutable
proxy.ResolveExecutable = func() (string, error) { return bdBin, nil }
t.Cleanup(func() { proxy.ResolveExecutable = prev })
t.Setenv("HOME", t.TempDir())
port, err := proxy.PickFreePort()
require.NoError(t, err)
storeRootDir := t.TempDir()
shutdownOnInterrupt(t, storeRootDir)
t.Cleanup(func() {
if err := proxy.Shutdown(storeRootDir); err != nil {
t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err)
}
})
cfgPath := writeServerConfig(t, port)
logPath := filepath.Join(t.TempDir(), "server.log")
ctx := context.Background()
open := func() (UnitOfWorkProvider, error) {
return NewDoltServerUOWProvider(
ctx, storeRootDir, "beads", logPath, cfgPath,
proxy.BackendLocalServer, "root", "", bin,
)
}
// Fresh database: the gate must not block initial creation/migration.
provider, err := open()
require.NoError(t, err)
// Simulate a database one migration behind this binary by dropping the
// latest cursor row (the schema change itself stays applied; only the
// recorded version regresses, so the gate sees a pending migration).
p, ok := provider.(*doltSQLProvider)
require.True(t, ok, "provider type = %T", provider)
_, err = p.db.ExecContext(ctx,
"DELETE FROM schema_migrations WHERE version = ?", schema.LatestVersion())
require.NoError(t, err)
require.NoError(t, provider.Close(ctx))
// Stop the proxy + child server so the remote can be persisted on disk and
// the reopen starts from the cold-start state.
require.NoError(t, proxy.Shutdown(storeRootDir))
// Persist a remote in the database's .dolt config. The child server may
// still be releasing its directory lock, so retry briefly.
dbDir := filepath.Join(storeRootDir, "beads")
remoteURL := "file://" + filepath.Join(t.TempDir(), "remote")
require.Eventually(t, func() bool {
return doltutil.AddCLIRemote(dbDir, "origin", remoteURL) == nil
}, 10*time.Second, 200*time.Millisecond, "AddCLIRemote(%s)", dbDir)
// Reopen: behind + remote-backed must refuse to migrate in place.
blocked, err := open()
if err == nil {
_ = blocked.Close(ctx)
t.Fatal("reopen = nil error, want *schema.RemoteMigrateGateError")
}
require.True(t, schema.IsRemoteMigrateGateError(err),
"reopen error = %T (%v), want error wrapping *schema.RemoteMigrateGateError", err, err)
}
var (
bdBinaryOnce sync.Once
bdBinary string

View file

@ -3,7 +3,6 @@ package uow
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"github.com/steveyegge/beads/internal/storage/domain/db"
@ -27,18 +26,7 @@ func (t *doltServerTx) Commit(ctx context.Context, message string) error {
t.done = true
defer t.releaseConn()
_, err := t.conn.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', ?);", message)
if err != nil {
// The session still holds the open transaction; releasing the conn
// like this would hand the next borrower a session whose START
// TRANSACTION implicitly commits these orphaned writes
// (bd-6dnrw.44 item 7). Roll back first; if even that fails,
// poison the conn so the pool discards the session.
if _, rbErr := t.conn.ExecContext(ctx, "ROLLBACK;"); rbErr != nil {
t.poisonConn()
}
return err
}
return nil
return err
}
func (t *doltServerTx) Rollback(ctx context.Context) error {
@ -47,13 +35,8 @@ func (t *doltServerTx) Rollback(ctx context.Context) error {
}
t.done = true
defer t.releaseConn()
if _, err := t.conn.ExecContext(ctx, "ROLLBACK;"); err != nil {
// Same hazard as a failed commit: an open tx must not ride a pooled
// session back to the next borrower.
t.poisonConn()
return err
}
return nil
_, err := t.conn.ExecContext(ctx, "ROLLBACK;")
return err
}
func (t *doltServerTx) RollbackUnlessCommitted(ctx context.Context) {
@ -62,23 +45,9 @@ func (t *doltServerTx) RollbackUnlessCommitted(ctx context.Context) {
}
}
// poisonConn marks the underlying driver connection bad so the pool discards
// it on Close instead of recycling a session with an open transaction (same
// pattern as bd-578h9.17 item 4).
func (t *doltServerTx) poisonConn() {
if t.conn != nil {
_ = t.conn.Raw(func(any) error { return driver.ErrBadConn })
}
}
// releaseConn returns the pinned session to the pool. The conn reference is
// deliberately kept: a closed *sql.Conn answers every Runner method with
// sql.ErrConnDone (QueryRowContext surfaces it via Row.Err), so a
// use-after-commit Runner fails with an error instead of the nil-pointer
// panic that nil-ing the field caused. Close is idempotent for our purposes
// (a second call just returns ErrConnDone, which we ignore).
func (t *doltServerTx) releaseConn() {
if t.conn != nil {
_ = t.conn.Close()
t.conn = nil
}
}

View file

@ -1,98 +0,0 @@
package uow
// bd-6dnrw.44 item 7: a failed DOLT_COMMIT used to release the pinned conn
// with the transaction still open; the next borrower's START TRANSACTION
// would implicitly commit the orphaned writes. These tests pin the repair
// sequence: rollback on commit failure, and poison the conn (pool discard,
// observable via db.Stats) when even the rollback fails.
import (
"context"
"database/sql"
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newMockTxProvider(t *testing.T) (*doltSQLProvider, sqlmock.Sqlmock) {
t.Helper()
mockDB, mock, err := sqlmock.New()
require.NoError(t, err)
t.Cleanup(func() { _ = mockDB.Close() })
return &doltSQLProvider{defaultBranch: defaultBranch, db: mockDB}, mock
}
func TestDoltServerTxCommitFailureRollsBackBeforeRelease(t *testing.T) {
p, mock := newMockTxProvider(t)
mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec("DOLT_COMMIT").WillReturnError(errors.New("commit exploded"))
mock.ExpectExec("ROLLBACK").WillReturnResult(sqlmock.NewResult(0, 0))
tx, err := p.BeginTx(context.Background())
require.NoError(t, err)
err = tx.Commit(context.Background(), "msg")
require.ErrorContains(t, err, "commit exploded")
require.NoError(t, mock.ExpectationsWereMet(), "ROLLBACK must run on the session before the conn is released")
assert.Equal(t, 1, p.db.Stats().OpenConnections, "rolled-back session is clean and may return to the pool")
}
func TestDoltServerTxCommitAndRollbackFailurePoisonsConn(t *testing.T) {
p, mock := newMockTxProvider(t)
mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec("DOLT_COMMIT").WillReturnError(errors.New("commit exploded"))
mock.ExpectExec("ROLLBACK").WillReturnError(errors.New("rollback exploded too"))
tx, err := p.BeginTx(context.Background())
require.NoError(t, err)
err = tx.Commit(context.Background(), "msg")
require.ErrorContains(t, err, "commit exploded")
assert.Equal(t, 0, p.db.Stats().OpenConnections, "session with an open tx must be discarded, not pooled")
}
func TestDoltServerTxRollbackFailurePoisonsConn(t *testing.T) {
p, mock := newMockTxProvider(t)
mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec("ROLLBACK").WillReturnError(errors.New("rollback exploded"))
tx, err := p.BeginTx(context.Background())
require.NoError(t, err)
err = tx.Rollback(context.Background())
require.ErrorContains(t, err, "rollback exploded")
assert.Equal(t, 0, p.db.Stats().OpenConnections, "session with an open tx must be discarded, not pooled")
}
func TestBeginTxStartTransactionFailureReleasesConn(t *testing.T) {
p, mock := newMockTxProvider(t)
mock.ExpectExec("START TRANSACTION").WillReturnError(errors.New("no tx for you"))
_, err := p.BeginTx(context.Background())
require.ErrorContains(t, err, "no tx for you")
assert.Equal(t, 0, p.db.Stats().InUse, "pinned conn must not leak when START TRANSACTION fails")
}
func TestDoltServerTxRunnerAfterCommitErrorsInsteadOfPanicking(t *testing.T) {
p, mock := newMockTxProvider(t)
mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec("DOLT_COMMIT").WillReturnResult(sqlmock.NewResult(0, 0))
tx, err := p.BeginTx(context.Background())
require.NoError(t, err)
require.NoError(t, tx.Commit(context.Background(), "msg"))
r := tx.Runner()
require.NotNil(t, r, "Runner must stay usable for error reporting after commit")
_, err = r.ExecContext(context.Background(), "SELECT 1")
assert.ErrorIs(t, err, sql.ErrConnDone, "exec on a committed tx must error, not panic")
_, err = r.QueryContext(context.Background(), "SELECT 1")
assert.ErrorIs(t, err, sql.ErrConnDone, "query on a committed tx must error, not panic")
row := r.QueryRowContext(context.Background(), "SELECT 1")
require.NotNil(t, row)
assert.ErrorIs(t, row.Err(), sql.ErrConnDone, "row on a committed tx must carry the error, not panic")
}

View file

@ -1,11 +1,7 @@
package uow
import (
"database/sql/driver"
"errors"
"io"
"net"
"syscall"
mysql "github.com/go-sql-driver/mysql"
)
@ -21,28 +17,3 @@ func isSerializationError(err error) bool {
}
return mysqlErr.Number == 1213 || mysqlErr.Number == 1205
}
// isRetryableWarmupError reports whether an error during provider warmup is
// plausibly transient. The managed dolt child (or the proxy relaying to it)
// can accept TCP before the SQL engine is ready, so dial, reset, and
// handshake failures must retry within the warmup window rather than abort
// it (bd-6dnrw.44 item 8). Serialization failures are retryable everywhere;
// anything else (auth refusal, SQL errors, the remote-migrate gate) is
// genuinely permanent.
func isRetryableWarmupError(err error) bool {
if err == nil {
return false
}
if isSerializationError(err) {
return true
}
var netErr net.Error
if errors.As(err, &netErr) {
return true
}
return errors.Is(err, driver.ErrBadConn) ||
errors.Is(err, mysql.ErrInvalidConn) ||
errors.Is(err, io.EOF) ||
errors.Is(err, syscall.ECONNREFUSED) ||
errors.Is(err, syscall.ECONNRESET)
}

View file

@ -1,45 +0,0 @@
package uow
import (
"database/sql/driver"
"errors"
"fmt"
"io"
"net"
"syscall"
"testing"
mysql "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
"github.com/steveyegge/beads/internal/storage/schema"
)
// bd-6dnrw.44 item 8: warmup must retry dial/handshake transients (the child
// server accepts TCP before the SQL engine answers) and stay permanent on
// real refusals (auth, SQL errors, the remote-migrate gate).
func TestIsRetryableWarmupError(t *testing.T) {
retryable := map[string]error{
"dial refused": &net.OpError{Op: "dial", Err: syscall.ECONNREFUSED},
"wrapped net error": fmt.Errorf("uow: pin connection: %w", &net.OpError{Op: "read", Err: syscall.ECONNRESET}),
"bad conn": fmt.Errorf("exec: %w", driver.ErrBadConn),
"invalid conn": mysql.ErrInvalidConn,
"eof handshake": io.EOF,
"deadlock": &mysql.MySQLError{Number: 1213},
"lock wait": &mysql.MySQLError{Number: 1205},
}
for name, err := range retryable {
assert.True(t, isRetryableWarmupError(err), "%s must be retryable: %v", name, err)
}
permanent := map[string]error{
"nil": nil,
"access denied": &mysql.MySQLError{Number: 1045},
"unknown db": &mysql.MySQLError{Number: 1049},
"plain error": errors.New("something structural"),
"gate refusal": &schema.RemoteMigrateGateError{},
}
for name, err := range permanent {
assert.False(t, isRetryableWarmupError(err), "%s must be permanent: %v", name, err)
}
}

View file

@ -51,7 +51,5 @@ func NewExternalDoltServerUOWProvider(
return nil, fmt.Errorf("uow: get proxy endpoint: %w", err)
}
// No on-disk remote probe: the external dolt server's data dir may live on
// another host, so the remote-migrate gate relies on dolt_remotes alone.
return openAndInitSchema(ctx, ep, database, rootUser, rootPassword, nil)
return openAndInitSchema(ctx, ep, database, rootUser, rootPassword)
}