fix(schema,uow): cover the proxied data-behind path and make its consent step runnable (post-merge #6659) (#6695)

* fix(schema,uow): cover the proxied data-behind path and make its consent step runnable (post-merge #6659)

Post-merge review of the landed range d0063ac..e1e1158 (PR #6659, the
release/1.3.0 backport of #6658) raised two majors. Neither is a regression
from that PR; both are gaps its surface left open.

F-A — the uow/proxied path never reached the #6575 stop, and its blunt body
prescribed the wedge. `doltSQLProvider.initSchema` passed a nil adopter to
`CheckSharedStoreMigrateGate`, so `localDataBehind` short-circuited and routing
could never emit `smartDataBehind`. A proxied store that was level on schema
and behind in data reported `shared-store` instead, whose body is the
designated-migrator recipe — `bd migrate --force` then `bd dolt push` — which
in that state is exactly the #6368 wedge the stop exists to prevent. The
refusal was always safe (the shared arm suppresses the auto-migrate either
way); the guidance was not.

Only `AheadBehind` is wired. It is the complete data-behind predicate, while
leaving `IsStrictAncestor`/`WorkingSetClean` nil keeps `routeAdoptFastForward`
false, so the remote-AHEAD arm on this path keeps the plain adopt directive it
has today. `FastForward` stays nil: it is a write that would promote the schema
for every co-resident client.

The remedy also had to be topology-correct before it could be printed.
`bd dolt pull` is refused at the proxied front door
(`proxy.dolt_pull.unsupported`), as are `bd dolt push`, `bd migrate` and
`bd conflicts` — so the body, agent directive and `options` now name the
machine the pull runs on, and the option id becomes
`pull-first-on-server-host`. The don't-force warning gains
`BD_ALLOW_REMOTE_MIGRATE=1`, the one consent surface the proxy does not refuse.
Read-only proxied opens print the pull-first block instead of the shared
consent template.

The proxied fact is the caller's, not the provider's: `newSQLServerUOWProvider`
is also `bd serve`'s provider for SERVER-mode workspaces, where that pull
works. A new `uow.WithProxiedServerMode()` is set only by
`openProxiedServerUOWProvider`.

F-B — the shared data-behind consent step named a verb that cannot unlock a
remote-backed shared store. `sharedMigrateConsent` has one reader,
`sharedNoRemoteGate`, reachable only on the `!hasRemote` branch; a data-behind
stop has a remote by construction. The four surfaces now name
`SharedConsentCommandForced` (`bd migrate schema --force`) with its `--global`
twin, and the human/JSON `--global` retargets follow. Widening the verb consent
to the remote-backed arm was rejected: it would drop the designated-migrator
confirmation #4259 deliberately requires whenever a remote exists. This mirrors
PR #6692, the main-line remediation for the same defect on #6658, so the two
lines converge rather than diverge.

Tests: the consent step is proven by EXECUTING its path (a text assertion on
`Options()` is how the dead command shipped), with the bare verb as the
counterfactual; the proxied route is driven through `initSchema` against a
mocked behind-clone and asserts the body does not prescribe
`bd migrate --force`; a server-mode open pins that it keeps the runnable-here
remedy; a level clone pins that reading ancestry does not turn a genuine
first-mover into a stop.

Scope: the scorecard's two Required Changes only. Its non-gating items — F-C
(`noticeSharedMigrateRefusal` lacks an `IsDataBehind()` branch), F-D
(`warnRemoteSyncOpenRefusal` verbosity), F-E (`docs` not retargeted), F-F/F-G
(stale comments) — are left for separate follow-ups.

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

* fix(schema,cli): the proxied consent step is not refused — say so (post-merge #6659 iter 2)

Post-merge review iteration 2 of PR #6695 found one major: the proxied
data-behind guidance this PR added asserts that its own prescribed consent
step, `bd migrate schema --force`, is refused at the proxied front door. It
is not.

The refusal table keys on the full command path. `proxyMaintenanceRefusals`
registers the bare `migrate` verb and its hooks/issues/sync children, but has
no `migrate schema` row, so the two-word path misses the lookup and
`validateProxyMaintenanceBeforeProvider` falls through to nil. `bd migrate
schema` then runs its proxied arm, which reports the migration the provider
open already applied under that verb's consent.

That makes the claim wrong in the unsafe direction: `forceOrEnvConsent` is
read BEFORE the data-behind stop is routed, so an operator told "it is refused
here anyway" who types it to confirm applies the migration to a data-behind
shared store — the #6368 wedge, reachable from the exact topology this
remediation was written for. The don't-force warning made it worse by
presenting BD_ALLOW_REMOTE_MIGRATE=1 as the only consent surface this
workspace can reach.

Reworded to the truth on all five surfaces, and pinned so it cannot drift
again:

- dataBehindBody's proxied consent clause now says the step CAN be run here
  and that the precondition is temporal, not locational: run it only after
  the pull lands on the server host.
- the proxied don't-force warning names `bd migrate schema --force` alongside
  BD_ALLOW_REMOTE_MIGRATE=1 as the two reachable consent surfaces, both
  honored before this stop is reached.
- the consent option's When/Risk drop "bd migrate is refused in
  proxied-server mode" and say the pull-first half is load-bearing here
  because this one does NOT fail closed the way the pull does.
- CHANGELOG drops "the one consent surface the proxy does not refuse".
- the same false premise is corrected in the uow test comment.
- TestProxyMaintenanceAllowsTheSharedConsentVerb resolves the command from
  SharedConsentCommandForced through the same cobra lookup the root pre-run
  uses, asserts validateProxyMaintenanceBeforeProvider returns nil for it,
  and keeps the bare `bd migrate` refusal (proxy.migrate.unsupported) as the
  counterfactual. No test called that function with this command before,
  which is why the false wording shipped green.

Also the two non-gating consistency findings from the same iteration:

- `--json` gate["expected"] carries the location qualifier when Proxied, so
  an expected-only reader is not handed a locally-refused command while
  options/hint carry it. Extracted to dataBehindExpectedField so the
  qualifier does not add a branch to handleRemoteMigrateGateJSON (cognitive
  stays 33, cyclomatic 18).
- fallbackReasonNote's data-behind arm gates its remedy on Proxied; the note
  prints after the body, so unqualified it made the last paragraph prescribe
  what the paragraph above called refused.
- smart_remote_migrate_gate.go's injection-site comment no longer says the
  shared UOW provider wires neither callback; it wires AheadBehind only.

Tests: ./internal/storage/schema (full, ok), ./cmd/bd -run
'Migrate|Consent|Proxy|RemoteGate|DataBehind|SharedStore|RemoteSyncOpen'
(ok), ./internal/storage/uow -run 'DataBehind|Proxied|MigrateGate' (ok).

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

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Julian Knutsen 2026-09-24 03:46:42 -07:00 • committed by GitHub
commit bb03a310a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 959 additions and 44 deletions

View file

@ -228,8 +228,34 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
operator who can confirm every co-resident client is upgraded). On a shared
Dolt sql-server the guidance carries #5920's consequence — migrating promotes
the schema for every co-resident client, and clients still on an older bd
refuse the database until upgraded — and names the `bd migrate schema`
consent step the retry needs there.
refuse the database until upgraded — and names the consent step the retry
needs there: `bd migrate schema --force`. It is the forced form because this
stop always has a remote configured (behind-ness is read from the
remote-tracking ref), and the bare `bd migrate schema` consent is only read
for a shared database with *no* remote; the flag consents to migrating a
remote-backed shared store, and by then the pull has landed the commits the
clone was missing.
**Proxied-server mode reaches the stop too, and is told where to run the
remedy.** The store-open gate on that path supplied no branch-position
callback, so a proxied clone that was level on schema and behind in data
could never be classified data-behind: it got the blunt shared-store refusal,
whose body is the designated-migrator recipe — `bd migrate --force` then
`bd dolt push` — which in this state is the wedge the stop exists to prevent.
It now routes to the same data-behind stop as every other topology. Because
`bd dolt pull` is refused at the proxied front door
(`proxy.dolt_pull.unsupported`), along with `bd dolt push`, the bare
`bd migrate` and `bd conflicts`, the guidance there names the machine the pull
has to run on rather than printing a command this binary rejects; the `--json`
option is `pull-first-on-server-host`, and `expected` carries the same
qualifier so a single-field reader is not handed a locally-refused command.
The shared-store consent step is the other way round: `bd migrate schema
--force` is *not* refused here — the refusal table keys on the command path
and has no `migrate schema` row — so the guidance says it can be run from this
workspace but only after the pull lands on the server host, and the warning
against forcing past the stop names it alongside `BD_ALLOW_REMOTE_MIGRATE=1`
as the two consent surfaces this topology can still reach. Read-only proxied
opens print the same pull-first block instead of the shared-consent template.
Both existing workarounds keep working unchanged: `BD_SMART_GATE=0` opts out
of the smart gate entirely (which means the blunt gate applies to `bd dolt

View file

@ -367,15 +367,27 @@ func isForcedMigrate(cmd *cobra.Command) bool {
// open targets `beads_global`, so the block's `bd migrate schema` would
// migrate the PROJECT database and leave the refusal in place — the working
// remedy is the same verb with the same flag.
func printGlobalDatabaseConsentHint(w io.Writer) {
//
// It takes the refusal because "the same verb" is not the same verb on every
// arm: the #6575 data-behind stop on a shared store is remote-backed by
// construction, and there the bare verb's consent is never read (see
// schema.SharedConsentCommandForced), so retargeting the bare form would hand
// the operator a global-scoped command that still cannot succeed. This mirrors
// the retarget in handleRemoteMigrateGateJSON. A nil error keeps the
// pre-existing bare-verb wording.
func printGlobalDatabaseConsentHint(w io.Writer, e *schema.RemoteMigrateGateError) {
if !globalFlag {
return
}
consent := schema.SharedConsentCommandGlobal
if e != nil && e.IsDataBehind() && e.Shared {
consent = schema.SharedConsentCommandForcedGlobal
}
fmt.Fprintf(w,
"\n This command targeted the global database (--global), so run the\n"+
" migrate step with the same flag:\n"+
" %s\n",
schema.SharedConsentCommandGlobal)
consent)
}
// renderTypedOpenError prints the actionable block for the store-open failures
@ -405,7 +417,7 @@ func renderTypedOpenError(err error) bool {
handleRemoteMigrateGateJSON(gateErr)
} else {
fmt.Fprint(os.Stderr, gateErr.UserMessage())
printGlobalDatabaseConsentHint(os.Stderr)
printGlobalDatabaseConsentHint(os.Stderr, gateErr)
}
return true
}

View file

@ -9,8 +9,78 @@ import (
"testing"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/storage/schema"
)
// TestProxyMaintenanceAllowsTheSharedConsentVerb pins the capability fact the
// #6575 proxied data-behind guidance is written against: the consent command
// that guidance prescribes is NOT refused at the proxied front door.
//
// The refusal table keys on the full command path. It has rows for the bare
// `migrate` verb and for `migrate hooks` / `migrate issues` / `migrate sync`,
// but none for `migrate schema` — so that path falls through
// validateProxyMaintenanceBeforeProvider's multi-word arm to `bd migrate
// schema`'s own proxied arm, which reports the migration the provider open
// already applied under this verb's consent.
//
// An earlier revision of that guidance asserted the opposite on three runtime
// surfaces and nothing failed, because no test called this function with this
// command. The wording is not cosmetic: forceOrEnvConsent honors `--force`
// BEFORE the data-behind stop is routed, so an operator told "it is refused
// here anyway" who types it to confirm takes the exact wedge the stop exists
// to prevent.
//
// The command is resolved from the constant the gate prescribes rather than
// retyped here, so drift on either side has to face this test.
func TestProxyMaintenanceAllowsTheSharedConsentVerb(t *testing.T) {
oldJSON := jsonOutput
jsonOutput = true
t.Cleanup(func() { jsonOutput = oldJSON })
args := strings.Fields(strings.TrimPrefix(schema.SharedConsentCommandForced, "bd "))
target, _, err := rootCmd.Find(args)
if err != nil || target == rootCmd {
t.Fatalf("the gate prescribes %q, which does not resolve to a subcommand: %v",
schema.SharedConsentCommandForced, err)
}
path := strings.TrimSpace(strings.TrimPrefix(target.CommandPath(), rootCmd.Name()))
if path != "migrate schema" {
t.Fatalf("prescribed consent command resolves to path %q, want %q", path, "migrate schema")
}
var allowErr error
out := captureStdout(t, func() error {
allowErr = validateProxyMaintenanceBeforeProvider(target)
return nil
})
if allowErr != nil {
t.Errorf("%q is refused before the provider (%v), but the #6575 proxied data-behind guidance tells the operator it can be run from this workspace",
path, allowErr)
}
if out != "" {
t.Errorf("a non-refusal still emitted a typed error: %s", out)
}
// The counterfactual, so "only the bare `bd migrate` verb is refused here"
// is a claim and not a vacuous one.
bare, _, err := rootCmd.Find([]string{"migrate"})
if err != nil || bare == rootCmd {
t.Fatalf("bd migrate does not resolve: %v", err)
}
var bareErr error
refusal := captureStdout(t, func() error {
bareErr = validateProxyMaintenanceBeforeProvider(bare)
return nil
})
if bareErr == nil {
t.Fatal("bare `bd migrate` is no longer refused in proxied-server mode; the data-behind guidance names it as the one form that is")
}
if !strings.Contains(refusal, `"code": "proxy.migrate.unsupported"`) {
t.Errorf("bare migrate refusal = %q, want proxy.migrate.unsupported", refusal)
}
}
func TestProxyCapabilityMatrix(t *testing.T) {
for _, cap := range []ProxyCapability{ProxyCapReadonly, ProxyCapMaxRows} {
err := AssertProxyCapability(ProxyModeProxied, cap)

View file

@ -61,6 +61,22 @@ func humanDecisionRequired(e *schema.RemoteMigrateGateError) bool {
return !(e.IsDataBehind() && !e.DataDiverged && !e.Shared)
}
// dataBehindExpectedField renders the data-behind stop's `expected` field.
//
// On a proxied workspace the remedy is the same command in a different PLACE,
// and `expected` is what a single-field agent reader keys on. The option id,
// its when/risk and the hint all carry that qualifier, so leaving it off here
// alone would hand such a reader a command its own front door refuses
// (proxy.dolt_pull.unsupported). Kept out of handleRemoteMigrateGateJSON so
// the qualifier does not add another branch to that already-deep switch.
func dataBehindExpectedField(proxied bool) string {
const tail = "; the migration is only allowed once this clone has nothing left to pull"
if proxied {
return "run `" + schema.DataBehindRemedyCommand + "` on the machine that hosts this database first — this workspace is in proxied-server mode, where it is refused (proxy.dolt_pull.unsupported)" + tail
}
return "run `" + schema.DataBehindRemedyCommand + "` first" + tail
}
// handleRemoteMigrateGateJSON renders the #4259 remote-migrate gate error as a
// structured JSON error block for agent consumption.
//
@ -87,12 +103,13 @@ func handleRemoteMigrateGateJSON(e *schema.RemoteMigrateGateError) {
if globalFlag {
sharedConsent = schema.SharedConsentCommandGlobal
}
// The #6575 data-behind stop on a SHARED store carries the same consent
// verb in its second option (migrate-shared-after-pulling), so it needs
// the same retarget: under --global the project-scoped verb would
// consent the wrong database and leave the refusal in place. It reaches
// here through the default arm (empty Decision), so the Decision test
// alone would miss it.
// The #6575 data-behind stop on a SHARED store carries a consent verb in
// its second option (migrate-shared-after-pulling) too — the forced one,
// since that stop is always remote-backed — so it needs the same
// retarget: under --global the project-scoped verb would consent the
// wrong database and leave the refusal in place. It reaches here through
// the default arm (empty Decision), so the Decision test alone would
// miss it.
retargetShared := globalFlag && (e.Decision == "shared-no-remote" || (e.IsDataBehind() && e.Shared))
opts := make([]map[string]interface{}, 0, len(e.Options()))
@ -105,9 +122,18 @@ func handleRemoteMigrateGateJSON(e *schema.RemoteMigrateGateError) {
// "expected" would still hand it the wrong-target command.
retargeted := make([]string, len(commands))
for i, c := range commands {
if c == schema.SharedConsentCommand {
switch c {
case schema.SharedConsentCommand:
retargeted[i] = schema.SharedConsentCommandGlobal
} else {
case schema.SharedConsentCommandForced:
// The data-behind arm's consent step is the FORCED
// verb (that stop is remote-backed by construction,
// where the bare verb's consent is never read), so it
// needs its own global form — matching only the bare
// verb would leave this option pointing at the
// project database.
retargeted[i] = schema.SharedConsentCommandForcedGlobal
default:
retargeted[i] = c
}
}
@ -192,7 +218,7 @@ func handleRemoteMigrateGateJSON(e *schema.RemoteMigrateGateError) {
gate["data_behind_shape"] = "fast-forward"
}
gate["observed"] = observed
gate["expected"] = "run `" + schema.DataBehindRemedyCommand + "` first; the migration is only allowed once this clone has nothing left to pull"
gate["expected"] = dataBehindExpectedField(e.Proxied)
}
}
m["remote_migrate_gate"] = gate

View file

@ -101,18 +101,27 @@ func TestHandleRemoteMigrateGateJSON_DataBehind(t *testing.T) {
}
for _, tc := range []struct {
name string
diverged bool
wantShape string
name string
diverged bool
proxied bool
wantShape string
wantOptionID string
}{
{name: "fast-forward shape", diverged: false, wantShape: "fast-forward"},
{name: "diverged shape", diverged: true, wantShape: "diverged"},
{name: "fast-forward shape", diverged: false, wantShape: "fast-forward", wantOptionID: "pull-first"},
{name: "diverged shape", diverged: true, wantShape: "diverged", wantOptionID: "pull-first"},
// On a proxied workspace the remedy is the same command in a different
// PLACE, and observed/expected are what a single-field reader keys on.
// The qualifier reached the option id and the hint an iteration before
// it reached expected, which is the divergence this case exists to
// catch: all three are asserted together below.
{name: "proxied qualifies every remedy field", proxied: true, wantShape: "fast-forward", wantOptionID: "pull-first-on-server-host"},
} {
t.Run(tc.name, func(t *testing.T) {
gate := &schema.RemoteMigrateGateError{
CurrentVersion: 66, LatestVersion: 67, Pending: 1,
FallbackReason: "data-behind",
DataDiverged: tc.diverged,
Proxied: tc.proxied,
}
parsed := capture(t, gate)
obj, ok := parsed["remote_migrate_gate"].(map[string]interface{})
@ -149,8 +158,8 @@ func TestHandleRemoteMigrateGateJSON_DataBehind(t *testing.T) {
t.Fatalf("options = %v, want exactly one (the pull)", obj["options"])
}
o, _ := rawOpts[0].(map[string]interface{})
if id, _ := o["id"].(string); id != "pull-first" {
t.Errorf("option id = %v, want \"pull-first\"", o["id"])
if id, _ := o["id"].(string); id != tc.wantOptionID {
t.Errorf("option id = %v, want %q", o["id"], tc.wantOptionID)
}
cmds, _ := o["commands"].([]interface{})
if len(cmds) != 1 || cmds[0] != schema.DataBehindRemedyCommand {
@ -171,6 +180,21 @@ func TestHandleRemoteMigrateGateJSON_DataBehind(t *testing.T) {
if hint == gate.EscapeHint() {
t.Errorf("hint must not be the runnable escape command %q", gate.EscapeHint())
}
// The location qualifier is all-or-nothing across the three fields
// an agent may read on their own: an `expected`-only reader on this
// topology would otherwise be handed a command its own front door
// refuses, while `options` and `hint` said where to run it.
if tc.proxied {
for name, surface := range map[string]string{"expected": expected, "hint": hint} {
for _, want := range []string{"proxied-server mode", "proxy.dolt_pull.unsupported"} {
if !strings.Contains(surface, want) {
t.Errorf("proxied %s = %q, want it to carry %q the way options[0] does", name, surface, want)
}
}
}
} else if strings.Contains(expected, "proxied-server mode") {
t.Errorf("expected = %q carries the proxied qualifier on a direct workspace, where the pull is runnable here", expected)
}
// Nothing anywhere in the block may offer the migrate/push pair or
// the no-op bootstrap for this state.
blob, err := json.Marshal(parsed)
@ -212,8 +236,10 @@ func TestHandleRemoteMigrateGateJSON_DataBehind(t *testing.T) {
}
consent, _ := rawOpts[1].(map[string]interface{})
cmds, _ := consent["commands"].([]interface{})
if len(cmds) != 1 || cmds[0] != schema.SharedConsentCommandGlobal {
t.Errorf("consent option commands = %v, want [%q] under --global", cmds, schema.SharedConsentCommandGlobal)
// The forced form, not the bare one: this stop is remote-backed by
// construction and the bare verb's consent is never read there.
if len(cmds) != 1 || cmds[0] != schema.SharedConsentCommandForcedGlobal {
t.Errorf("consent option commands = %v, want [%q] under --global", cmds, schema.SharedConsentCommandForcedGlobal)
}
// The pull is target-agnostic and must NOT be rewritten.
pull, _ := rawOpts[0].(map[string]interface{})

View file

@ -2,12 +2,15 @@ package main
import (
"bytes"
"context"
"io"
"os"
"strings"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/steveyegge/beads/internal/storage/schema"
)
@ -189,17 +192,173 @@ func TestPrintGlobalDatabaseConsentHint(t *testing.T) {
orig := globalFlag
defer func() { globalFlag = orig }()
sharedNoRemote := &schema.RemoteMigrateGateError{
CurrentVersion: 65, LatestVersion: 66, Pending: 1,
Decision: "shared-no-remote", Shared: true,
}
globalFlag = false
var off bytes.Buffer
printGlobalDatabaseConsentHint(&off)
printGlobalDatabaseConsentHint(&off, sharedNoRemote)
if off.Len() != 0 {
t.Errorf("no hint without --global, got:\n%s", off.String())
}
globalFlag = true
var on bytes.Buffer
printGlobalDatabaseConsentHint(&on)
printGlobalDatabaseConsentHint(&on, sharedNoRemote)
if !strings.Contains(on.String(), schema.SharedConsentCommandGlobal) {
t.Errorf("hint must name %q:\n%s", schema.SharedConsentCommandGlobal, on.String())
}
// The data-behind stop on a shared store is the arm where "the same verb
// with the same flag" is not enough: it is remote-backed by construction,
// so the bare verb's consent is never read and a --global rewrite of it is
// still a command that cannot succeed. Mirrors the JSON retarget.
var behind bytes.Buffer
printGlobalDatabaseConsentHint(&behind, &schema.RemoteMigrateGateError{
CurrentVersion: 66, LatestVersion: 67, Pending: 1,
FallbackReason: "data-behind", Shared: true,
})
if !strings.Contains(behind.String(), schema.SharedConsentCommandForcedGlobal) {
t.Errorf("the shared data-behind hint must name %q:\n%s",
schema.SharedConsentCommandForcedGlobal, behind.String())
}
}
// expectRemoteBackedSharedProbe mocks the probe sequence a shared,
// remote-backed store makes before the gate decides: CurrentVersion, then
// PendingVersions (both a cursor probe plus a max-version read), then the
// remote count. With the smart router pinned off, no further query follows
// either outcome, so an unmet expectation means the gate took a path this test
// does not describe.
func expectRemoteBackedSharedProbe(mock sqlmock.Sqlmock) {
for i := 0; i < 2; i++ {
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM information_schema\.tables`).
WithArgs("schema_migrations").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
mock.ExpectQuery(`SELECT COALESCE\(MAX\(version\), 0\) FROM schema_migrations`).
WillReturnRows(sqlmock.NewRows([]string{"version"}).AddRow(1))
}
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM dolt_remotes`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
}
// runPrescribedConsent replays the root PersistentPreRunE for one command line
// and reports what the real shared gate does afterwards on a remote-backed
// store. It resolves the command through the same cobra lookup the pre-run
// uses and feeds it to the same two predicates, so a command string that is
// not actually the consenting verb cannot pass by looking like one.
func runPrescribedConsent(t *testing.T, cmdline string) error {
t.Helper()
t.Setenv(schema.SmartGateEnv, "0")
t.Setenv(schema.AllowRemoteMigrateEnv, "0")
args := strings.Fields(strings.TrimPrefix(cmdline, "bd "))
target, rest, err := rootCmd.Find(args)
if err != nil {
t.Fatalf("the gate prescribes %q, which does not resolve to a command: %v", cmdline, err)
}
if target == rootCmd {
t.Fatalf("the gate prescribes %q, which resolves to bare bd", cmdline)
}
if err := target.ParseFlags(rest); err != nil {
t.Fatalf("the gate prescribes %q, whose flags do not parse: %v", cmdline, err)
}
t.Cleanup(func() {
// These cobra commands are package-level singletons, so a parsed flag
// outlives the test that parsed it and would silently consent for the
// next one.
target.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed {
return
}
_ = f.Value.Set(f.DefValue)
f.Changed = false
})
schema.SetForceAllowRemoteMigrate(false)
schema.SetSharedMigrateConsent(false)
})
// The two lines the root pre-run runs, verbatim in effect.
schema.SetForceAllowRemoteMigrate(isForcedMigrate(target))
schema.SetSharedMigrateConsent(isSchemaMigrateVerb(target) && !isPreviewCommand(target))
db, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer db.Close()
expectRemoteBackedSharedProbe(mock)
var gateErr error
_ = captureNoticeStderr(t, func() {
gateErr = schema.CheckSharedStoreMigrateGate(context.Background(), db, "", nil, nil)
})
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet expectations for %q: %v", cmdline, err)
}
return gateErr
}
// TestDataBehindSharedConsentCommandUnlocks is the executing twin of
// TestSharedRefusalPrescribesAWorkingVerb, for the #6575 data-behind stop on a
// shared store — the one arm where the structural pairing that test checks is
// not enough.
//
// That stop always has a remote configured (behind-ness is read from the
// remote-tracking ref), and on the remote-backed arm the bare verb's consent
// is never read: schema.sharedMigrateConsent has a single reader, inside
// sharedNoRemoteGate, which is reachable only when no remote is configured.
// So the option this refusal carries has to be checked by RUNNING its consent
// path. Asserting its wording is exactly what let a dead command ship: the
// pre-existing tests pinned the option's text and passed throughout.
//
// The command under test is taken from Options() rather than written out here,
// so this stays a test of whatever the gate actually prescribes.
func TestDataBehindSharedConsentCommandUnlocks(t *testing.T) {
behindShared := &schema.RemoteMigrateGateError{
CurrentVersion: 66, LatestVersion: 67, Pending: 1,
FallbackReason: "data-behind", Shared: true,
}
opts := behindShared.Options()
if len(opts) != 2 || opts[1].ID != "migrate-shared-after-pulling" || len(opts[1].Commands) != 1 {
t.Fatalf("Options() = %+v, want pull-first then a single-command consent step", opts)
}
prescribed := opts[1].Commands[0]
t.Run("the prescribed command unlocks the post-pull retry", func(t *testing.T) {
if err := runPrescribedConsent(t, prescribed); err != nil {
t.Fatalf("the gate prescribes %q for this state, but running its consent path still refuses: %v",
prescribed, err)
}
})
// The counterfactual, and the regression pin: the command that shipped.
// If this ever passes, the consent read has moved and the option above can
// go back to the bare verb.
t.Run("the bare verb does not, which is why the option carries --force", func(t *testing.T) {
err := runPrescribedConsent(t, schema.SharedConsentCommand)
if err == nil {
t.Fatal("the bare verb now unlocks a remote-backed shared store; revisit SharedConsentCommandForced")
}
if !schema.IsRemoteMigrateGateError(err) {
t.Fatalf("want the gate refusal, got %T: %v", err, err)
}
})
// Every surface that names a consent command for this stop must name the
// one just executed — the finding was four surfaces agreeing on a dead
// command, so agreement alone is not the property worth pinning.
t.Run("the prose surfaces name the executed command", func(t *testing.T) {
for name, surface := range map[string]string{
"UserMessage": behindShared.UserMessage(),
"AgentDirective": behindShared.AgentDirective(),
} {
if !strings.Contains(surface, prescribed) {
t.Errorf("%s does not name the prescribed consent command %q:\n%s", name, prescribed, surface)
}
}
})
}

View file

@ -197,6 +197,13 @@ func openProxiedServerUOWProvider(ctx context.Context, beadsDir, databaseOverrid
if err != nil {
return nil, err
}
// This is the one entry point that is proxied-server mode by definition;
// the funnel below is shared with `bd serve`'s SERVER-mode provider, which
// must not be told it is proxied. The distinction reaches exactly one
// thing: a migration-gate refusal whose remedy is `bd dolt pull` has to
// say where that pull can be run, because proxied mode refuses it at the
// front door (proxy.dolt_pull.unsupported) and server mode does not.
opts = append(append([]uow.ProviderOption{}, opts...), uow.WithProxiedServerMode())
return newSQLServerUOWProvider(ctx, beadsDir, topology, opts...)
}

View file

@ -127,6 +127,20 @@ type RemoteMigrateGateError struct {
// can report conflicts to resolve first. Set only when
// FallbackReason == fallbackReasonDataBehind; meaningless otherwise.
DataDiverged bool
// Proxied marks a refusal raised on a proxied-server-mode open — the unit
// of work provider, which reaches its database only through the dbproxy.
// It changes WHERE the data-behind remedy can be run, not what it is: in
// that mode `bd dolt pull` is refused at the front door
// (proxy.dolt_pull.unsupported, cmd/bd/proxy_capability.go), along with
// `bd dolt push`, `bd migrate` and `bd conflicts`. Printing the bare
// command there would hand the operator a remedy their own binary rejects,
// so the surfaces that name it say to run it where the database lives.
//
// Deliberately set by the provider after the gate returns rather than
// plumbed into the gate: the topology is the caller's fact, and every
// other injection site would have to pass a constant false.
Proxied bool
}
const (
@ -268,7 +282,16 @@ func (e *RemoteMigrateGateError) fallbackReasonNote() string {
case fallbackReasonSharedStore:
why = "this store is shared (dolt sql-server); the first-mover auto-migrate is disabled here because it would lock out co-resident clients (#5920)"
case fallbackReasonDataBehind:
why = "this clone is behind the remote in data commits it has not pulled, so it is not the first-mover it looks like on schema alone — migrating in place would diverge from those commits, and can leave every later `bd dolt pull` refusing to merge (#6575, #6368). Run `" + DataBehindRemedyCommand + "` first, then retry: this clone is then the true first-mover it looked like"
// UserMessage prints this note AFTER userBody, so on a proxied
// workspace an unqualified remedy makes the LAST paragraph tell the
// operator to type here the command the paragraph above just said the
// front door refuses. Gate it the same way dataBehindBody and
// Options() already do.
remedy := "Run `" + DataBehindRemedyCommand + "` first, then retry"
if e.Proxied {
remedy = "Run `" + DataBehindRemedyCommand + "` on the server host first (this workspace is in proxied-server mode, which refuses it — proxy.dolt_pull.unsupported), then retry here"
}
why = "this clone is behind the remote in data commits it has not pulled, so it is not the first-mover it looks like on schema alone — migrating in place would diverge from those commits, and can leave every later `bd dolt pull` refusing to merge (#6575, #6368). " + remedy + ": this clone is then the true first-mover it looked like"
// Only promise the retry resolves itself where it actually does. On a
// shared store the first-mover auto-migrate is suppressed regardless
// (fallbackReasonSharedStore, #5920), so pulling clears THIS stop but
@ -283,7 +306,7 @@ func (e *RemoteMigrateGateError) fallbackReasonNote() string {
// lockout; data-behind now outranks it, so without this clause the
// operator loses the warning that promoting the schema on a shared
// server locks out every co-resident client still on an older bd.
why += " — but on this shared store the migrate still needs explicit consent (`" + SharedConsentCommand + "`), because it promotes the schema for every co-resident bd client at once and clients still on an older binary will refuse this database until they are upgraded (#5920)"
why += " — but on this shared store the migrate still needs explicit consent (`" + SharedConsentCommandForced + "`), because it promotes the schema for every co-resident bd client at once and clients still on an older binary will refuse this database until they are upgraded (#5920)"
}
default:
return ""
@ -322,20 +345,60 @@ func (e *RemoteMigrateGateError) dataBehindBody() string {
" This clone has no commits of its own, so the pull is a pure fast-forward:\n" +
" nothing local is merged or discarded.\n"
}
if e.Proxied {
// The remedy is the same command; what changes is where it can be
// typed. This workspace reaches the database through the dbproxy, and
// the proxied front door refuses `bd dolt pull` outright — so leaving
// the line above unqualified would name a command this very binary
// rejects (gastownhall/beads#6575 follow-up). It sits after the shape
// clause because the conflict handling that clause describes moves to
// the same machine.
body += "" +
"\n" +
" That pull cannot be run from here: this workspace is in proxied-server mode,\n" +
" which refuses `" + DataBehindRemedyCommand + "` at the front door (proxy.dolt_pull.unsupported),\n" +
" along with `bd dolt push`, the bare `bd migrate` and `bd conflicts`. Run\n" +
" it — and any conflict resolution it needs — where the database lives: on\n" +
" the server host, from a workspace with direct access to that database.\n"
}
body += "" +
"\n" +
" Then re-run what you just ran.\n"
if e.Shared {
// #5920: on a shared store the first-mover auto-migrate stays
// suppressed after the pull, so the retry needs the consent verb.
// suppressed after the pull, so the retry needs explicit consent.
// Saying "it proceeds on its own" here would loop the operator.
// The command is the FORCED verb, not the bare one: this stop always
// has a remote (that is where behind-ness is read from), and the bare
// verb's consent is only read on the no-remote arm — see
// SharedConsentCommandForced.
body += "" +
" This database is served to co-resident bd clients, so the migration itself\n" +
" still needs explicit consent afterwards — it promotes the schema for EVERY\n" +
" client at once, and clients still on an older bd will refuse this database\n" +
" until they are upgraded (#5920):\n" +
" " + SharedConsentCommand + "\n" +
" (" + SharedConsentCommandGlobal + " for the shared global database)\n"
" until they are upgraded (#5920). Once the pull above has completed:\n" +
" " + SharedConsentCommandForced + "\n" +
" (" + SharedConsentCommandForcedGlobal + " for the shared global database)\n"
if e.Proxied {
// Unlike the pull, this consent step is NOT refused here, and
// saying otherwise would be wrong in the unsafe direction. The
// proxied refusal table keys on the command path
// (cmd/bd.proxyMaintenanceRefusals): it registers the bare
// `migrate` verb and its hooks/issues/sync children, but has no
// `migrate schema` row, so the two-word path falls through
// validateProxyMaintenanceBeforeProvider to a proxied arm that
// reports the migration the provider open just applied under this
// verb's consent. A reader told it is blocked here tries it to
// confirm — and forceOrEnvConsent is honored BEFORE this stop is
// ever routed, so the wedge lands on the one topology that can
// still take it. State the real precondition instead: not where,
// but when.
body += "" +
" Unlike the pull, that consent step CAN be run from here: the proxied front\n" +
" door refuses the bare `bd migrate` verb, not `" + SharedConsentCommandForced + "`,\n" +
" and the provider open applies the migration under it. That is exactly why\n" +
" it has to wait — run it only after the pull on the server host completes.\n"
}
} else {
body += "" +
" The migration then runs on its own: with nothing left to pull, this clone\n" +
@ -347,6 +410,32 @@ func (e *RemoteMigrateGateError) dataBehindBody() string {
" while this clone is still behind — the exact state this stop exists to\n" +
" prevent — and the `bd dolt push` that would follow is rejected as a\n" +
" non-fast-forward, because the clone is still missing the remote's commits.\n"
if e.Shared {
// The consent command this arm prescribes is itself a forced migrate,
// so the warning above has to say what makes that one different: the
// pull having already landed, not the flag. Named in full rather than
// as "the command above", which is ambiguous once the proxied clause
// below is also printed.
body += "" +
" The same holds for `" + SharedConsentCommandForced + "` until the pull has\n" +
" finished: what makes it safe is that there is nothing left to pull by then.\n"
}
if e.Proxied {
// Only the BARE `bd migrate` the warning above names is refused here.
// Two other surfaces carry the same forced consent and are both
// reachable from this workspace: the two-word `migrate schema` path,
// which no proxy refusal row matches, and the env hatch, which no
// command dispatch touches at all. forceOrEnvConsent reads both before
// this stop is routed, so a proxied warning that implies the topology
// already blocks the wedge would leave it open on the one topology
// that can still take it.
body += "" +
" Being in proxied-server mode does not put that out of reach: the front door\n" +
" refuses the bare `bd migrate` verb, not `" + SharedConsentCommandForced + "`, and\n" +
" " + AllowRemoteMigrateEnv + "=1 grants the same consent to ANY command, including\n" +
" the one you just ran. Both are honored before this stop is even reached, so\n" +
" do not reach for either to get past it.\n"
}
return body
}
@ -472,10 +561,17 @@ func (e *RemoteMigrateGateError) AgentDirective() string {
} else {
d += " This clone has no commits of its own, so that pull is a pure fast-forward and discards nothing."
}
if e.Proxied {
d += " This workspace is in proxied-server mode, where `" + DataBehindRemedyCommand + "` is refused at the " +
"front door (proxy.dolt_pull.unsupported) — do NOT attempt it here. The pull has to be run where the " +
"database lives (the server host, from a workspace with direct access to it); surface that to the " +
"operator rather than retrying locally."
}
if e.Shared {
d += " After the pull the migration still needs the operator's explicit consent (" + SharedConsentCommand +
"): this database is shared with co-resident bd clients and migrating promotes the schema for all of " +
"them at once (#5920). Do NOT auto-run it."
d += " After the pull the migration still needs the operator's explicit consent (" + SharedConsentCommandForced +
" — this store is remote-backed, so the bare verb does not unlock it; see SharedConsentCommandForced): " +
"this database is shared with co-resident bd clients and migrating promotes the schema for all of " +
"them at once (#5920). Do NOT auto-run it, and do NOT run it before the pull has completed."
} else {
d += " After the pull, re-running the original command migrates on its own; nothing else is needed."
}
@ -555,8 +651,20 @@ func (e *RemoteMigrateGateError) Options() []GateOption {
if e.DataDiverged {
risk = "this clone also has commits of its own, so the pull MERGES; it can report conflicts that must be resolved before it completes (bd dolt pull --strategy ours|theirs on embedded storage, bd conflicts resolve on a server store). Nothing is discarded without that resolution"
}
id := "pull-first"
if e.Proxied {
// An option an agent can read as runnable-here is the harm on this
// topology: the proxied front door refuses the command. Say WHERE
// in both the id and the precondition, so neither a Commands-only
// reader nor an id-only reader is misled.
id = "pull-first-on-server-host"
when = "always, for this stop, but NOT from this workspace: it is in proxied-server mode, where " +
DataBehindRemedyCommand + " is refused (proxy.dolt_pull.unsupported). Run it on the machine that " +
"hosts this database, from a workspace with direct access to it, then retry here"
risk += "; running it from this proxied workspace fails with proxy.dolt_pull.unsupported and changes nothing"
}
pull := GateOption{
ID: "pull-first",
ID: id,
When: when,
Commands: []string{DataBehindRemedyCommand},
Risk: risk,
@ -564,12 +672,29 @@ func (e *RemoteMigrateGateError) Options() []GateOption {
if e.Shared {
// The pull itself is still safe; what the shared store changes is
// what happens AFTER it, so the consent step becomes a second
// option rather than a footnote on the first (#5920).
// option rather than a footnote on the first (#5920). It carries
// the FORCED verb because this stop is always remote-backed and
// the bare verb's consent is read only on the no-remote arm — an
// option whose command cannot succeed in the state that printed it
// is worse than no option (see SharedConsentCommandForced).
consentWhen := "the pull above has completed AND every co-resident bd client of this server is upgraded to this binary (confirmed with the operator)"
consentRisk := "co-resident clients still on an older bd will refuse this database until upgraded; running it before the pull completes is the wedge this stop prevents. Its --force consents to migrating a remote-backed shared store (the bare verb's consent is read only with no remote configured); it does not make migrating while behind safe"
if e.Proxied {
// Unlike the pull, this command is NOT refused here: the
// refusal table has no "migrate schema" row, so the two-word
// path falls through and the provider open applies the
// migration under its consent. The precondition is therefore
// temporal, not locational — and it is the only thing between
// an agent on this topology and the wedge, because forced
// consent is honored before this stop is routed at all.
consentWhen += ". Unlike the pull this one IS runnable from this proxied workspace — the front door refuses the bare `bd migrate`, not this two-word path — so the pull-first half of this precondition is load-bearing here, not a formality"
consentRisk += ". Running it from this proxied workspace does NOT fail closed the way the pull does: the consent is honored before this stop is routed, and the provider open applies the migration"
}
return []GateOption{pull, {
ID: "migrate-shared-after-pulling",
When: "the pull above has completed AND every co-resident bd client of this server is upgraded to this binary (confirmed with the operator)",
Commands: []string{SharedConsentCommand},
Risk: "co-resident clients still on an older bd will refuse this database until upgraded; running it before the pull completes is the wedge this stop prevents",
When: consentWhen,
Commands: []string{SharedConsentCommandForced},
Risk: consentRisk,
}}
}
return []GateOption{pull}
@ -974,6 +1099,32 @@ const SharedConsentCommand = "bd migrate schema"
// migrates the wrong one.
const SharedConsentCommandGlobal = SharedConsentCommand + " --global"
// SharedConsentCommandForced is the consent command for a shared database that
// IS remote-backed. Every #6575 data-behind stop is in that state by
// construction — behind-ness is read from `remotes/<name>/<branch>`, so there
// is always a remote — and there the bare verb above is a dead end:
// sharedMigrateConsent has exactly one reader, sharedNoRemoteGate, which
// checkRemoteMigrateGate reaches only on the !hasRemote branch. With a remote
// configured the retry routes through forceOrEnvConsent instead, so typing the
// bare verb sets a consent nothing reads and the operator lands on the blunt
// shared-store refusal, whose body prescribes the `bd migrate --force` this
// stop's own body tells them not to use.
//
// The fix is deliberately the command, not the gate: widening
// sharedNoRemoteGate's verb consent to the remote-backed arm would drop the
// designated-migrator confirmation that #4259's cross-clone fork risk demands
// whenever a remote exists, which is the very thing sharedNoRemoteGate's
// comment says is intentional. `--force` is what forceOrEnvConsent honors, and
// the CLI accepts it on this verb (cmd/bd.isForcedMigrate covers both
// `bd migrate` and `bd migrate schema`), so this is the narrowest command that
// still names the schema migration AND actually unlocks the retry.
const SharedConsentCommandForced = SharedConsentCommand + " --force"
// SharedConsentCommandForcedGlobal is SharedConsentCommandForced aimed at the
// shared global database, for the same reason SharedConsentCommandGlobal
// exists.
const SharedConsentCommandForcedGlobal = SharedConsentCommandGlobal + " --force"
// sharedNoRemoteGate decides the shared-store-without-a-remote arm
// (gastownhall/beads#5920). There is no remote, so none of the #4259
// migrate-vs-adopt machinery applies: the only question is whether the

View file

@ -246,9 +246,11 @@ func routeAdoptFastForward(ctx context.Context, db DBConn, ref string, adopt *Fa
// (behind == 0) and a purely-ahead clone (behind == 0, ahead >= 1) are still
// permitted — they are genuine first-movers with nothing to pull.
//
// adopt may be nil, or wire neither callback (the shared unit-of-work
// provider's injection site) — then there is no ancestry fact to read and
// routing stays exactly as it was before this check existed. With only
// adopt may be nil, or wire neither callback — then there is no ancestry fact
// to read and routing stays exactly as it was before this check existed. (The
// shared unit-of-work provider's injection site wires AheadBehind only; every
// other callback stays nil there deliberately, so no new auto-write path
// opens.) With only
// IsStrictAncestor wired, the fact narrows to the ahead == 0 shape; the
// diverged shape then reads "not behind", which is the pre-widening
// behavior rather than a new hazard.

View file

@ -1246,11 +1246,23 @@ func TestDataBehindRemedySurfaces(t *testing.T) {
// fallbackReasonSharedStore (whose note carried them) and Options()'
// sharedRisk only ever decorated the adopt option.
msg := base(true, false).UserMessage()
for _, want := range []string{"co-resident", "#5920", SharedConsentCommand} {
for _, want := range []string{"co-resident", "#5920", SharedConsentCommandForced} {
if !strings.Contains(msg, want) {
t.Errorf("shared data-behind message is missing %q:\n%s", want, msg)
}
}
// This stop always has a remote (that is where behind-ness is read
// from), so the bare verb is a dead end here: its consent is read only
// by sharedNoRemoteGate, on the !hasRemote branch. Asserting the forced
// form above is not enough — every forced form CONTAINS the bare form
// as a substring, so a regression to the bare verb would still satisfy
// it. Remove the forms that do work, then require nothing prescribing
// the bare one is left.
scrubbed := strings.ReplaceAll(msg, SharedConsentCommandForcedGlobal, "")
scrubbed = strings.ReplaceAll(scrubbed, SharedConsentCommandForced, "")
if strings.Contains(scrubbed, SharedConsentCommand) {
t.Errorf("shared data-behind message prescribes the bare verb, which cannot succeed with a remote configured:\n%s", msg)
}
if strings.Contains(msg, "proceeds on its own") {
t.Errorf("a shared store must not be promised an automatic retry:\n%s", msg)
}
@ -1300,6 +1312,63 @@ func TestDataBehindRemedySurfaces(t *testing.T) {
t.Error("IsDataBehind() = true on a nil receiver")
}
})
// Proxied-server mode is the one topology where the remedy command is
// correct and unrunnable at the same time: `bd dolt pull` is refused at the
// front door (proxy.dolt_pull.unsupported), as are `bd dolt push`,
// `bd migrate` and `bd conflicts`. Naming it unqualified there hands the
// operator a command their own binary rejects, which is how they end up
// reaching for the one hatch the proxy does NOT refuse.
t.Run("proxied surfaces say where the pull can be run", func(t *testing.T) {
proxied := func(shared bool) *RemoteMigrateGateError {
e := base(shared, false)
e.Proxied = true
return e
}
for _, shared := range []bool{false, true} {
msg := proxied(shared).UserMessage()
for _, want := range []string{
"proxied-server mode", "proxy.dolt_pull.unsupported", "server host",
DataBehindRemedyCommand, AllowRemoteMigrateEnv + "=1",
} {
if !strings.Contains(msg, want) {
t.Errorf("shared=%v: proxied body missing %q:\n%s", shared, want, msg)
}
}
d := proxied(shared).AgentDirective()
for _, want := range []string{"proxy.dolt_pull.unsupported", "do NOT attempt it here"} {
if !strings.Contains(d, want) {
t.Errorf("shared=%v: proxied AgentDirective missing %q:\n%s", shared, want, d)
}
}
opts := proxied(shared).Options()
if opts[0].ID != "pull-first-on-server-host" {
t.Errorf("shared=%v: pull option id = %q, want the host-qualified id", shared, opts[0].ID)
}
// The command itself is unchanged — it is the right command — so an
// agent that reads only Commands must find the qualification in the
// precondition it is required to check.
if len(opts[0].Commands) != 1 || opts[0].Commands[0] != DataBehindRemedyCommand {
t.Errorf("shared=%v: pull option commands = %v, want [%q]", shared, opts[0].Commands, DataBehindRemedyCommand)
}
if !strings.Contains(opts[0].When, "proxy.dolt_pull.unsupported") {
t.Errorf("shared=%v: pull option precondition must name the refusal:\n%s", shared, opts[0].When)
}
}
// Nothing above may leak into the ordinary embedded/server-mode stop,
// whose remedy IS runnable where it is printed.
direct := base(true, false).UserMessage()
for _, forbidden := range []string{"proxied-server mode", "server host"} {
if strings.Contains(direct, forbidden) {
t.Errorf("non-proxied body gained %q:\n%s", forbidden, direct)
}
}
if base(true, false).Options()[0].ID != "pull-first" {
t.Error("non-proxied pull option id changed")
}
})
}
// TestSmartGateEnabled pins the default-on contract: unset and unparseable

View file

@ -0,0 +1,288 @@
package uow
import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/steveyegge/beads/internal/storage/schema"
)
// dataBehindCurrent is a schema cursor at or above the convergence floor
// (schema.LastNonDeterministicMigration) and below this binary's latest, so the
// smart gate reaches the equal-version first-mover arm with migrations still
// pending — the only arm where the #6575 ancestry check runs.
const dataBehindCurrent = 65
// expectSmartGateDataBehindRoute mocks the smart router's reads for a clone
// that is LEVEL with the cached remote ref on schema and BEHIND it in data:
// the local hashes, the active branch, the remote ref's hashes (identical, so
// no skew and the same max version), and finally the ahead/behind commit
// counts the #6575 check reads through the injected adopter.
//
// The last query is the one that did not happen before this fix: with a nil
// adopter localDataBehind short-circuits, so the route ended at
// smartAutoMigrate and the refusal reported shared-store instead.
func expectSmartGateDataBehindRoute(mock sqlmock.Sqlmock, ahead, behind int) {
hashes := func() *sqlmock.Rows {
return sqlmock.NewRows([]string{"version", "content_hash"}).
AddRow(dataBehindCurrent-1, "hash-a").
AddRow(dataBehindCurrent, "hash-b")
}
ref := "remotes/origin/main"
mock.ExpectQuery(regexp.QuoteMeta("SELECT version, content_hash FROM schema_migrations")).
WillReturnRows(hashes())
mock.ExpectQuery(regexp.QuoteMeta("SELECT active_branch()")).
WillReturnRows(sqlmock.NewRows([]string{"branch"}).AddRow("main"))
mock.ExpectQuery(regexp.QuoteMeta(fmt.Sprintf("SHOW TABLES AS OF '%s' LIKE 'schema_migrations'", ref))).
WillReturnRows(sqlmock.NewRows([]string{"table"}).AddRow("schema_migrations"))
mock.ExpectQuery(regexp.QuoteMeta(fmt.Sprintf("SHOW COLUMNS FROM schema_migrations AS OF '%s' LIKE 'content_hash'", ref))).
WillReturnRows(sqlmock.NewRows([]string{"column"}).AddRow("content_hash"))
mock.ExpectQuery(regexp.QuoteMeta(fmt.Sprintf("SELECT version, content_hash FROM schema_migrations AS OF '%s'", ref))).
WillReturnRows(hashes())
// versioncontrolops.LocalAheadBehind — reached only through a wired
// AheadBehind callback.
mock.ExpectQuery(`FROM dolt_log AS OF`).
WillReturnRows(sqlmock.NewRows([]string{"ahead", "behind"}).AddRow(ahead, behind))
}
// TestInitSchemaProxiedDataBehind covers the proxied/`bd serve` open path's
// half of gastownhall/beads#6575.
//
// The store-open gate on this path passed a nil adopter, so localDataBehind
// had no ancestry fact to read and the route could never emit smartDataBehind.
// A proxied clone that was level on schema and behind in data therefore
// reported the blunt shared-store refusal — whose body is the designated-
// migrator recipe, `bd migrate --force` then `bd dolt push`. In this state that
// recipe IS the #6368 wedge the stop exists to prevent, so the refusal was safe
// (the shared arm suppresses the auto-migrate either way) while the guidance it
// printed was not.
func TestInitSchemaProxiedDataBehind(t *testing.T) {
// newProxiedProvider returns a provider over a fresh mock with the two
// escape hatches pinned off, so the gate is reached on its own merits.
newProxiedProvider := func(t *testing.T) (*doltSQLProvider, sqlmock.Sqlmock) {
t.Helper()
t.Setenv(schema.AllowRemoteMigrateEnv, "0")
t.Setenv(schema.SmartGateEnv, "1")
t.Cleanup(func() {
schema.SetSharedMigrateConsent(false)
schema.SetForceAllowRemoteMigrate(false)
})
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("create sql mock: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
return &doltSQLProvider{
defaultBranch: defaultBranch,
db: db,
serverEndpoint: "tcp:127.0.0.1:3306",
proxiedServerMode: true,
}, mock
}
t.Run("routes to the data-behind stop instead of the blunt shared-store one", func(t *testing.T) {
p, mock := newProxiedProvider(t)
lockName := expectBehindDatabaseThroughPreparation(mock, "beads", dataBehindCurrent)
expectSharedGateProbe(mock, dataBehindCurrent, 1)
expectSmartGateDataBehindRoute(mock, 0, 2)
// Nothing between the route and the lock release: the refusal must land
// before MigrateUp issues its first statement.
expectLockRelease(mock, lockName)
err := p.initSchema(context.Background(), "beads")
var gateErr *schema.RemoteMigrateGateError
if !errors.As(err, &gateErr) {
t.Fatalf("initSchema() error = %T (%v), want *schema.RemoteMigrateGateError", err, err)
}
if !gateErr.IsDataBehind() {
t.Fatalf("FallbackReason = %q, Decision = %q — want the #6575 data-behind stop",
gateErr.FallbackReason, gateErr.Decision)
}
if !gateErr.Shared {
t.Error("Shared = false; this provider only ever serves a shared database")
}
if !gateErr.Proxied {
t.Error("Proxied = false; every refusal from this provider is a proxied-server-mode refusal")
}
// The finding, stated as an assertion: the body an operator reads must
// not hand them the migrate-in-place recipe while they are still behind.
body := gateErr.UserMessage()
for _, forbidden := range []string{"bd migrate --force\n", "designated migrator"} {
if strings.Contains(body, forbidden) {
t.Errorf("proxied data-behind body still prescribes %q:\n%s", forbidden, body)
}
}
if !strings.Contains(body, schema.DataBehindRemedyCommand) {
t.Errorf("body must name the pull:\n%s", body)
}
// ...and because `bd dolt pull` is refused at the proxied front door,
// the body has to say where it can actually be run.
for _, want := range []string{"proxied-server mode", "proxy.dolt_pull.unsupported", "server host"} {
if !strings.Contains(body, want) {
t.Errorf("proxied body missing %q:\n%s", want, body)
}
}
// The env hatch is not the only consent surface that stays reachable
// here: the proxy refusal table keys on the command path and has no
// `migrate schema` row, so the forced schema verb falls through too
// (pinned in cmd/bd by TestProxyMaintenanceAllowsTheSharedConsentVerb).
// Both are read by forceOrEnvConsent before this stop is routed, so a
// warning that names only the refused bare `bd migrate --force` leaves
// the wedge reachable on exactly this topology.
for _, want := range []string{schema.AllowRemoteMigrateEnv + "=1", schema.SharedConsentCommandForced} {
if !strings.Contains(body, want) {
t.Errorf("proxied body must warn about the reachable consent surface %q:\n%s", want, body)
}
}
opts := gateErr.Options()
if len(opts) != 2 || opts[0].ID != "pull-first-on-server-host" {
t.Fatalf("Options() = %+v, want the host-qualified pull plus the consent step", opts)
}
for _, o := range opts {
for _, c := range o.Commands {
if c == "bd migrate --force" || c == "bd bootstrap" {
t.Errorf("option %q offers %q, measured not to work in this state", o.ID, c)
}
}
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
})
// The counterfactual for the topology flag. This same provider is the one
// `bd serve` builds for a SERVER-mode workspace, where `bd dolt pull` is
// not refused — telling that operator to go find another machine would be
// the same class of wrong guidance in the other direction.
t.Run("a server-mode open keeps the runnable-here remedy", func(t *testing.T) {
p, mock := newProxiedProvider(t)
p.proxiedServerMode = false
lockName := expectBehindDatabaseThroughPreparation(mock, "beads", dataBehindCurrent)
expectSharedGateProbe(mock, dataBehindCurrent, 1)
expectSmartGateDataBehindRoute(mock, 0, 2)
expectLockRelease(mock, lockName)
err := p.initSchema(context.Background(), "beads")
var gateErr *schema.RemoteMigrateGateError
if !errors.As(err, &gateErr) {
t.Fatalf("initSchema() error = %T (%v), want *schema.RemoteMigrateGateError", err, err)
}
if !gateErr.IsDataBehind() {
t.Fatalf("FallbackReason = %q — the stop itself is topology-independent", gateErr.FallbackReason)
}
if gateErr.Proxied {
t.Error("Proxied = true for a server-mode open")
}
if body := gateErr.UserMessage(); strings.Contains(body, "server host") {
t.Errorf("server-mode body must keep the runnable-here pull:\n%s", body)
}
if id := gateErr.Options()[0].ID; id != "pull-first" {
t.Errorf("server-mode pull option id = %q, want %q", id, "pull-first")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
})
t.Run("a level clone is unaffected", func(t *testing.T) {
// The counterfactual for the wiring: same route, behind == 0. Reading
// the ancestry must not turn a genuine first-mover into a data-behind
// stop — it stays the shared-store refusal it was before this fix.
p, mock := newProxiedProvider(t)
lockName := expectBehindDatabaseThroughPreparation(mock, "beads", dataBehindCurrent)
expectSharedGateProbe(mock, dataBehindCurrent, 1)
expectSmartGateDataBehindRoute(mock, 0, 0)
expectLockRelease(mock, lockName)
err := p.initSchema(context.Background(), "beads")
var gateErr *schema.RemoteMigrateGateError
if !errors.As(err, &gateErr) {
t.Fatalf("initSchema() error = %T (%v), want *schema.RemoteMigrateGateError", err, err)
}
if gateErr.IsDataBehind() {
t.Error("a level clone must not reach the data-behind stop")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
})
t.Run("read-only open prints the pull-first block, not the consent template", func(t *testing.T) {
// readThroughRefusedMigration's template names the shared consent verb
// for every refusal. For this stop that verb is doubly wrong: the
// remedy is the pull, and a remote-backed shared store never reads the
// bare verb's consent at all (schema.SharedConsentCommandForced).
p, mock := newProxiedProvider(t)
p.readOnly = true
lockName := expectBehindDatabaseThroughPreparation(mock, "beads", dataBehindCurrent)
expectSharedGateProbe(mock, dataBehindCurrent, 1)
expectSmartGateDataBehindRoute(mock, 1, 2) // diverged shape
expectLockRelease(mock, lockName)
mock.ExpectExec(regexp.QuoteMeta("USE `beads`")).
WillReturnResult(sqlmock.NewResult(0, 0))
var initErr error
stderr := captureStderr(t, func() {
initErr = p.initSchema(context.Background(), "beads")
})
if initErr != nil {
t.Fatalf("initSchema() error = %v, want nil — reads keep working on the old schema", initErr)
}
for _, want := range []string{
"Read-only command", "without migrating",
schema.DataBehindRemedyCommand, "MERGES", "server host",
} {
if !strings.Contains(stderr, want) {
t.Errorf("read-through warning missing %q:\n%s", want, stderr)
}
}
// Two-sided: the template's "run 'bd migrate schema'" line must be gone
// for this stop. Scrub the forms that DO work first — every forced form
// contains the bare verb as a substring.
scrubbed := strings.ReplaceAll(stderr, schema.SharedConsentCommandForcedGlobal, "")
scrubbed = strings.ReplaceAll(scrubbed, schema.SharedConsentCommandForced, "")
if strings.Contains(scrubbed, schema.SharedConsentCommand) {
t.Errorf("read-through still prescribes the bare consent verb:\n%s", stderr)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
})
}
// TestDataBehindAdopterWiring pins the shape of the injected adopter, not just
// its effect. Only AheadBehind is wired: it is the complete #6575 predicate,
// while leaving IsStrictAncestor/WorkingSetClean nil keeps routeAdoptFastForward
// returning false, so the remote-AHEAD arm on this path keeps the plain adopt
// directive it had before. FastForward must stay nil — it is a WRITE that would
// promote the schema for every co-resident client of this server.
func TestDataBehindAdopterWiring(t *testing.T) {
for _, readOnly := range []bool{false, true} {
a := dataBehindAdopter(readOnly)
if a.AheadBehind == nil {
t.Fatal("AheadBehind must be wired; it is the whole point of the injection")
}
if a.FastForward != nil {
t.Error("FastForward must stay nil: a fast-forward here promotes the schema for every co-resident client")
}
if a.IsStrictAncestor != nil || a.WorkingSetClean != nil {
t.Error("IsStrictAncestor/WorkingSetClean must stay nil so the remote-ahead arm is unchanged")
}
if a.ReadOnly != readOnly {
t.Errorf("ReadOnly = %v, want %v", a.ReadOnly, readOnly)
}
}
}

View file

@ -22,6 +22,7 @@ import (
db "github.com/steveyegge/beads/internal/storage/domain/db"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/storage/schema"
"github.com/steveyegge/beads/internal/storage/versioncontrolops"
)
const (
@ -52,6 +53,10 @@ type doltSQLProvider struct {
// preview it still bootstraps and migrates normally — it only changes what
// happens when the migration gate REFUSES; see providerOptions.readOnly.
readOnly bool
// proxiedServerMode: the workspace that opened this provider is in
// proxied-server mode, so the commands the proxied front door refuses are
// off the table for its operator; see providerOptions.proxiedServerMode.
proxiedServerMode bool
// eventsJournalEnabled activates the durable events journal for THIS
// provider instance only. See SetEventsJournalEnabled.
eventsJournalEnabled atomic.Bool
@ -122,6 +127,17 @@ type providerOptions struct {
// working through the upgrade window. That is the same warn-and-continue
// contract embeddeddolt gives its read-only-command intent.
readOnly bool
// proxiedServerMode records that the WORKSPACE is in proxied-server mode,
// which this provider cannot infer: it is the single funnel for the
// proxied CLI and for `bd serve`'s own provider on a SERVER-mode
// workspace, and both front a Dolt sql-server through the same dbproxy.
// The distinction matters to exactly one thing — which commands the front
// door refuses. In proxied mode `bd dolt pull` is rejected before it opens
// anything (proxy.dolt_pull.unsupported), so the #6575 data-behind stop
// has to name where that pull can be run; on a server-mode workspace the
// same command works, and saying otherwise would be the same class of
// wrong guidance in the other direction.
proxiedServerMode bool
}
// WithPreview opens the provider for a non-mutating preview command.
@ -134,6 +150,13 @@ func WithReadOnly() ProviderOption {
return func(o *providerOptions) { o.readOnly = true }
}
// WithProxiedServerMode marks the open as belonging to a proxied-server-mode
// workspace. See providerOptions.proxiedServerMode: only the refusal guidance
// depends on it, and only the proxied CLI open may set it.
func WithProxiedServerMode() ProviderOption {
return func(o *providerOptions) { o.proxiedServerMode = true }
}
func applyProviderOptions(opts []ProviderOption) providerOptions {
var resolved providerOptions
for _, opt := range opts {
@ -264,10 +287,18 @@ func (p *doltSQLProvider) initSchemaAttempt(ctx context.Context, database string
// database by the time it runs, so a fresh bootstrap reads version 0
// and passes.
schema.WithMigrationGate(func(ctx context.Context, c *sql.Conn) error {
return schema.CheckSharedStoreMigrateGate(ctx, c, "", nil, nil)
return schema.CheckSharedStoreMigrateGate(ctx, c, "", nil, dataBehindAdopter(p.readOnly))
})); err != nil {
var gateErr *schema.RemoteMigrateGateError
if errors.As(err, &gateErr) {
// The gate reads the database, never the workspace, so it cannot
// know which commands this operator's front door will refuse. Only
// a proxied-server-mode workspace refuses `bd dolt pull`, and this
// provider also serves `bd serve` on a server-mode workspace,
// where that pull works — so the flag is the caller's, not a
// property of being behind a proxy (gastownhall/beads#6575
// follow-up).
gateErr.Proxied = p.proxiedServerMode
if p.readOnly {
return readThroughRefusedMigration(ctx, conn, database, gateErr)
}
@ -281,6 +312,39 @@ func (p *doltSQLProvider) initSchemaAttempt(ctx context.Context, database string
return nil
}
// dataBehindAdopter injects the one ancestry fact the smart gate's #6575
// data-behind check needs on this path. Without it localDataBehind short-
// circuits on a nil adopter, so a proxied store that is level on schema and
// BEHIND the remote in data could never route to smartDataBehind: it reported
// the blunt shared-store refusal instead, whose body names `bd migrate --force`
// and `bd dolt push` — the designated-migrator recipe, which in this state is
// the #6368 wedge the stop exists to prevent. The refusal itself was always
// safe here (the shared arm suppresses the first-mover auto-migrate whatever
// the ancestry says); the guidance it printed was not.
//
// Only AheadBehind is wired, deliberately. It is the complete data-behind
// predicate (behind >= 1 whatever ahead is, plus which shape), so the stop is
// fully covered — while leaving IsStrictAncestor/WorkingSetClean nil keeps
// routeAdoptFastForward returning false, so the remote-AHEAD arm still routes
// to the same plain adopt directive it does today. Nothing but the data-behind
// state changes message. FastForward stays nil for a stronger reason: the
// fast-forward is a WRITE that would promote the schema for every co-resident
// client of this server, which is exactly what the shared gate refuses to do
// unattended (#5920), and CheckSharedStoreMigrateGate suppresses it on a
// shared store regardless.
func dataBehindAdopter(readOnly bool) *schema.FastForwardAdopter {
return &schema.FastForwardAdopter{
AheadBehind: func(ctx context.Context, db schema.DBConn, ref string) (int, int, error) {
return versioncontrolops.LocalAheadBehind(ctx, db, ref)
},
// No FastForward is wired above, so canAutoFastForward is already
// false and this flag changes nothing today. Set anyway so the
// adopter's safety invariant ("ReadOnly means cannot write here")
// does not come to depend on that coincidence.
ReadOnly: readOnly,
}
}
// readThroughRefusedMigration serves a read command from a database the
// migration gate refused to migrate: warn, attach at the current schema, and
// carry on. Writes remain refused (they open without WithReadOnly), so the
@ -306,6 +370,19 @@ func readThroughRefusedMigration(ctx context.Context, conn *sql.Conn, database s
// the refusal, which is the actionable half.
return backoff.Permanent(gateErr)
}
// #6575: the data-behind stop's remedy is the pull, and the consent verb
// this template names does not unlock a remote-backed shared store at all
// (schema.SharedConsentCommandForced). Print that stop's own block, which
// REPLACES the migrate-or-adopt framing rather than decorating it, and say
// only what read-through adds on top.
if gateErr.IsDataBehind() {
fmt.Fprintf(os.Stderr,
"Warning: %[1]s"+
" Read-only command: continuing on schema v%[2]d without migrating.\n"+
" Writes stay blocked until the steps above are done.\n",
gateErr.UserMessage(), gateErr.CurrentVersion)
return nil
}
fmt.Fprintf(os.Stderr,
"Warning: %[1]v\n"+
" Read-only command: continuing on schema v%[2]d without migrating.\n"+
@ -686,6 +763,7 @@ func openAndInitSchema(ctx context.Context, ep proxy.Endpoint, database, rootUse
expectedProjectID: expectedProjectID,
preview: opts.preview,
readOnly: opts.readOnly,
proxiedServerMode: opts.proxiedServerMode,
}
if err := initProvider.initSchema(ctx, database); err != nil {
@ -719,5 +797,6 @@ func openAndInitSchema(ctx context.Context, ep proxy.Endpoint, database, rootUse
expectedProjectID: expectedProjectID,
preview: opts.preview,
readOnly: opts.readOnly,
proxiedServerMode: opts.proxiedServerMode,
}, nil
}