beads/internal/doltserver
Julian Knutsen 2f1e8be3ca
feat(backends): registry Remote + OpenWith options; beads.OpenBestAvailableWith for embedders (#7188)
* feat(backends): registry Remote + optional OpenWith(ctx, dir, OpenOptions)

Slice S1: backends.Backend gains Remote bool and an optional
OpenWith(ctx, beadsDir, OpenOptions{Credential, HTTPClient, UserAgent}) so a
registered extension backend can honor a per-open credential instead of
dialing with a process-global client — the motivating case is one embedder
process serving many workspaces with distinct credentials. Open keeps
working unmodified for every existing backend: Backend.OpenWithOptions calls
OpenWith when present, otherwise falls back to Open and ignores
HTTPClient/UserAgent, but refuses (ErrCredentialWithoutOpenWith) rather than
silently dropping a non-nil Credential.

Public beads.OpenBestAvailableWith(ctx, beadsDir, opts) carries this through
the SDK surface (mirrored in beads_nocgo.go and aliased in backend/backend.go
and beads.go); OpenBestAvailable is now that call with a zero OpenOptions,
identical to its prior behavior for Dolt, embedded Dolt, and every registered
backend. The plain-Dolt path (no registered backend) applies the same
Credential refusal, since Dolt has no per-open credential seam either.

doltserver.ResolvePhysicalRoots now checks backends.IsRemote(cfg.GetBackend())
before every Dolt-mode branch, including the shared-server check (which reads
only env/config.yaml and never consults cfg) — without this ordering, a global
BEADS_DOLT_SHARED_SERVER=1 would hijack a remote-backend workspace into
reporting a bogus local Dolt root it will never open.

Tests: backends_test new file open_with_test.go covers OpenWithOptions
dispatch/back-compat/Credential refusal and Remote/IsRemote; root package
open_backend_selection_test.go adds a fake OpenWith registrant proving
dispatch and option pass-through through OpenBestAvailableWith, zero-options
back-compat for every path (registered w/ and w/o OpenWith, plain Dolt), and
the Credential-without-OpenWith refusal on both paths; doltserver
physical_root_test.go adds the registered-remote-backend case including the
shared-server-override ordering regression test. Verified under both cgo and
nocgo builds. Live-mutated each new guard (the Credential refusal, the
OpenWith dispatch, the ResolvePhysicalRoots remote-backend case, and the
plain-Dolt Credential refusal) one at a time and confirmed RED before
reverting to GREEN.

No HTTP client code added; that is a later slice.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fixup(backends): address Opus review findings on OpenWith (M1-M3, L1-L2)

- M1: frame cmd/bd store-open errors for remote backends as unreachable
  server failures instead of Dolt-shaped "failed to open database"
  advice (openStoreErrorMessage, backendNameForErrorFraming), with tests.
- M2: rewrite the OpenBestAvailable/OpenBestAvailableWith back-compat
  subtest to assert fixed expected outcomes (error identity/message,
  returned store behavior) now that the two functions are identical,
  instead of comparing them to each other.
- M3: document and enforce the OpenWith contract: an implementation that
  receives a non-nil Credential it cannot type-assert to its expected
  type must refuse with the new ErrUnsupportedCredential sentinel,
  verified with a fake registrant.
- L1: document that a nil OpenOptions.Credential means the backend's
  default/ambient auth, and that multi-tenant embedders must pass a
  non-nil Credential (RequireCredential deferred to S2).
- L2: fail closed on HTTPClient/UserAgent passed to a backend without
  OpenWith, mirroring the existing Credential refusal, via new
  ErrHTTPClientWithoutOpenWith/ErrUserAgentWithoutOpenWith sentinels.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* build(bazel): auto-sync BUILD files

Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/37195230844
(`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy).
Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see
scripts/bazel-autofix-push.sh.

* fix(backends): keep the store-open error chain through the remote framing

Maintainer review fixes for the adopted PR (iteration 1).

openStoreErrorMessage returned a string, so ensureStoreActiveWithContext
rebuilt its error from text (%s) where base wrapped the open error with %w,
and the chain ended at the CLI's framing. Prime tells a store-open timeout
apart from a generic "storage unavailable" only through
errors.Is(err, context.DeadlineExceeded) on what ensureStoreActiveWithContext
returns (cmd/bd/prime.go), so the flattening silently downgraded that
diagnostic for every backend, Dolt included, not just the new Remote ones.

Replace the helper with openStoreError, which returns an error wrapping err
with %w on both arms. The message text is unchanged: "failed to open
database: <err>" for Dolt and any registered backend that is not Remote,
"failed to reach remote backend %q: <err>" for a Remote one. direct_mode.go
wraps the result with %w ahead of its Hint line; main.go keeps printing it
through HandleError("%v", ...), which only formats.

Tests:

- TestOpenStoreErrorFramesRemoteBackends (renamed from
  TestOpenStoreErrorMessageFramesRemoteBackends) now also asserts errors.Is
  on every arm, and the exact original text for the non-Remote arms.
- TestEnsureStoreActiveKeepsOpenErrorChain drives the real
  ensureStoreActiveWithContext against a registered backend whose open
  outlives its context, for a local and a Remote backend, and requires
  errors.Is(err, context.DeadlineExceeded) plus the framing prefix and the
  Hint line.
- TestFormatMemoriesForPrimeTimesOutOpeningRegisteredBackend is the
  unstubbed twin of TestFormatMemoriesForPrimeTimesOutOpeningStore and
  requires the timeout banner, not the "storage unavailable" one.

Measured at this head: turning direct_mode.go's outer %w into %v reddens
both chain-test rows and the prime test; turning the helper's %w into %v
reddens the framing test as well.

Non-gating cleanup in the same area:

- beads_cgo.go, beads_nocgo.go: OpenBestAvailableWith's doc and dispatch
  comments said only a non-nil Credential is refused. The code also refuses
  a non-nil HTTPClient and a non-empty UserAgent, each with its own
  ErrXWithoutOpenWith sentinel.
- internal/doltserver/physical_root.go: the Mode doc lists "remote-backend",
  and the RemoteBackend doc covers a registered Remote backend as well as a
  Dolt server on a non-local host.
- open_backend_selection_test.go: the plain-Dolt refusal test is a table
  over Credential, HTTPClient and UserAgent (renamed
  TestOpenBestAvailableWithRefusesPerOpenOptionsOnPlainDolt). Disabling the
  nocgo HTTPClient arm or the cgo UserAgent arm reddens only that row. The
  zero-options test's comment had the delegation backwards and pointed at
  beads.go; OpenBestAvailable delegates to OpenBestAvailableWith in
  beads_cgo.go and beads_nocgo.go.

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

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
2026-10-04 09:34:36 -07:00
..
BUILD.bazel feat(backends): registry Remote + OpenWith options; beads.OpenBestAvailableWith for embedders (#7188) 2026-10-04 09:34:36 -07:00
configport_test.go /{cmd,internal}: migration commands for server to proxied server 2026-07-13 13:23:47 -07:00
dirty_state_test.go test: add multi-process concurrency and server lifecycle integration tests 2026-03-22 15:01:45 -07:00
doltserver.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
doltserver_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
doltserver_unix.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
doltserver_unix_test.go perf: snapshot Dolt processes in one pass (bd-1rh.15) (#5326) 2026-08-03 20:49:27 -07:00
doltserver_windows.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
doltserver_windows_test.go fix(windows): detect exited processes in liveness helpers (#4980) (#4981) 2026-07-23 18:24:49 -07:00
fakedolt_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
fdleak_integration_test.go fix(doltserver): stop detached children inheriting the caller's fds (#4634) (#5184) 2026-07-31 08:41:41 -07:00
gc_config.go Use Snappy output for Dolt garbage collection (#4986) 2026-07-23 08:55:29 -07:00
gc_config_test.go Use Snappy output for Dolt garbage collection (#4986) 2026-07-23 08:55:29 -07:00
lifecycle_integration_test.go feat(doltserver): add fenced restart and remotesapi listener support (#6020) 2026-10-01 00:57:54 -07:00
listenerowner_linux.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
listenerowner_linux_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
listenerowner_other.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
logrotate.go fix(doltserver): rotate dolt-server.log at startup to cap size (#3161) 2026-04-10 21:52:37 -07:00
logrotate_test.go fix(doltserver): rotate dolt-server.log at startup to cap size (#3161) 2026-04-10 21:52:37 -07:00
managed_server_proof_test.go fix(storage): an env-pointed server is shared, not owned — close the migrate-gate bypass (#6118) 2026-09-04 23:37:32 -07:00
manifest_recovery.go fix(doltserver): gate corrupt-manifest .dolt reinit behind explicit bd doctor --fix (bd-6dnrw.6) 2026-06-11 00:30:13 -04:00
manifest_recovery_test.go fix(doltserver): gate corrupt-manifest .dolt reinit behind explicit bd doctor --fix (bd-6dnrw.6) 2026-06-11 00:30:13 -04:00
physical_root.go feat(backends): registry Remote + OpenWith options; beads.OpenBestAvailableWith for embedders (#7188) 2026-10-04 09:34:36 -07:00
physical_root_test.go feat(backends): registry Remote + OpenWith options; beads.OpenBestAvailableWith for embedders (#7188) 2026-10-04 09:34:36 -07:00
port_race_test.go test: add multi-process concurrency and server lifecycle integration tests 2026-03-22 15:01:45 -07:00
portsource_invariant_test.go fix(dolt): explicit ServerPort must outrank ambient BEADS_DOLT_SERVER_PORT env var (#5835) 2026-08-20 05:43:57 -07:00
portsources_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
probe.go fix(doltserver): drain MySQL handshake before closing probe connections (#5277) 2026-08-10 12:51:12 -07:00
probe_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
procattr_linux.go fix(doltserver): reap leaked test dolt sql-servers (test-gated Pdeathsig + suite-exit sweep) (#4592) 2026-07-07 12:59:07 -07:00
procattr_linux_test.go fix(doltserver): reap leaked test dolt sql-servers (test-gated Pdeathsig + suite-exit sweep) (#4592) 2026-07-07 12:59:07 -07:00
procattr_other_unix.go fix(doltserver): reap leaked test dolt sql-servers (test-gated Pdeathsig + suite-exit sweep) (#4592) 2026-07-07 12:59:07 -07:00
servermode.go feat(doltserver): add fenced restart and remotesapi listener support (#6020) 2026-10-01 00:57:54 -07:00
socket_integration_test.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
startowned.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
startowned_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
startupwatch.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
startupwatch_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
sweep.go fix(doltserver): recognize /var/tmp as a fixed temp root on Linux for sandbox-home detection (#6729) 2026-09-26 08:33:28 -07:00
sweep_darwin.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_darwin_test.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_linux.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_other.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_reap_unix.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_reap_unix_test.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_suite.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_suite_linux_test.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_suite_other.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_suite_test.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_suite_unix.go fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441) 2026-09-22 15:08:40 -07:00
sweep_test.go fix(doltserver): recognize /var/tmp as a fixed temp root on Linux for sandbox-home detection (#6729) 2026-09-26 08:33:28 -07:00
testmain_integration_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00
testmain_test.go fix(doltserver): auto-started dolt must own its port; move a taken bd-chosen port (#7201) 2026-10-04 09:22:03 -07:00