diff --git a/CHANGELOG.md b/CHANGELOG.md index f37674dbc..7a2a43a96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1128,6 +1128,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 section — plus the upgrade and init-safety ordering rules — now records the `bd dolt pull` exception instead of stating the pull is always refused. + **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 capability registry permits the + two-word `migrate schema` path — 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. + The *Clone behind the remote* docs section the payload links to, and the + upgrade and init-safety ordering rules that point at it, now give the same + proxied-server answer. + Both existing escape hatches are unchanged: `bd migrate --force` / `BD_ALLOW_REMOTE_MIGRATE=1` are still consulted before the smart gate, and `BD_SMART_GATE=0` still yields the blunt gate unconditionally (including for diff --git a/cmd/bd/proxy_capability_test.go b/cmd/bd/proxy_capability_test.go index 4bf83a8e3..b9bc35e93 100644 --- a/cmd/bd/proxy_capability_test.go +++ b/cmd/bd/proxy_capability_test.go @@ -11,8 +11,118 @@ import ( "testing" "github.com/spf13/cobra" + + "github.com/steveyegge/beads/internal/storage/schema" ) +// TestProxyMaintenanceAllowsTheSharedConsentVerb pins the capability facts the +// #6575 proxied data-behind guidance is written against: the consent command +// that guidance prescribes is NOT refused at the proxied front door, while the +// bare `bd migrate` verb, the `bd dolt pull` remedy and the other refusals the +// guidance names (`bd dolt push`, `bd conflicts`) are. +// +// The capability registry keys on the full command path. It refuses the bare +// `migrate` verb and `migrate hooks` / `migrate issues` / `migrate sync`, but +// permits `migrate schema` — so that path passes +// validateProxyRegistryBeforeProvider and reaches `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 the front door 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) + } + if path := commandRegistryPath(target); path != "migrate schema" { + t.Fatalf("prescribed consent command resolves to path %q, want %q", path, "migrate schema") + } + + find := func(t *testing.T, args ...string) *cobra.Command { + t.Helper() + cmd, _, err := rootCmd.Find(args) + if err != nil || cmd == rootCmd { + t.Fatalf("bd %s does not resolve: %v", strings.Join(args, " "), err) + } + return cmd + } + bareMigrate := find(t, "migrate") + doltPull := find(t, "dolt", "pull") + doltPush := find(t, "dolt", "push") + // `conflicts` itself is a parent group that cobra answers with help before + // any gate runs, so the refusal the guidance relies on lives on its leaves; + // resolve is the one a diverged pull's conflicts would need. + conflictsResolve := find(t, "conflicts", "resolve") + + for _, topology := range []ProxyTopology{ + ProxyTopologyManagedLocal, ProxyTopologyExternalTCP, + ProxyTopologyExternalUnix, ProxyTopologyTeamServer, + } { + t.Run(string(topology), func(t *testing.T) { + var allowErr error + out := captureStdout(t, func() error { + allowErr = validateProxyRegistryBeforeProvider(target, topology) + if allowErr == nil { + allowErr = validateProxyCapabilitiesBeforeProvider(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", + schema.SharedConsentCommandForced, allowErr) + } + if out != "" { + t.Errorf("a non-refusal still emitted a typed error: %s", out) + } + + // The counterfactuals, so "only the bare `bd migrate` verb is + // refused here" and "the pull cannot be run from here" are claims + // and not vacuous ones. The push and conflicts rows hold the rest + // of the refusal list the proxied body recites: `dolt push` is + // refused as unimplemented, the class of row that flips to allowed + // once a route lands (as `dolt commit`'s did), and the body would + // then name a refusal that no longer exists. + for _, tc := range []struct { + cmd *cobra.Command + code string + }{ + {bareMigrate, "proxy.migrate.unsupported"}, + {doltPull, "proxy.dolt_pull.unsupported"}, + {doltPush, "proxy.dolt_push.unsupported"}, + {conflictsResolve, "proxy.conflicts.unsupported"}, + } { + var refuseErr error + refusal := captureStdout(t, func() error { + refuseErr = validateProxyRegistryBeforeProvider(tc.cmd, topology) + return nil + }) + if refuseErr == nil { + t.Errorf("bd %s is no longer refused in proxied-server mode; the data-behind guidance says it is", + commandRegistryPath(tc.cmd)) + continue + } + if !strings.Contains(refusal, `"code": "`+tc.code+`"`) { + t.Errorf("bd %s refusal = %q, want %s", commandRegistryPath(tc.cmd), refusal, tc.code) + } + } + }) + } +} + func TestProxyCapabilityMatrix(t *testing.T) { for _, capability := range []ProxyCapability{ProxyCapReadonly, ProxyCapMaxRows} { err := AssertProxyCapability(ProxyModeProxied, capability) diff --git a/cmd/bd/remote_migrate_gate.go b/cmd/bd/remote_migrate_gate.go index 909466c41..52c0bb40e 100644 --- a/cmd/bd/remote_migrate_gate.go +++ b/cmd/bd/remote_migrate_gate.go @@ -68,6 +68,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. // @@ -208,7 +224,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) // The docs pointer is set once above alongside the blunt // observed/expected pair, and has to move with them: the section it // names tells the reader `bd dolt pull` is refused on every diff --git a/cmd/bd/remote_sync_open_test.go b/cmd/bd/remote_sync_open_test.go index 286858744..b3bda59ac 100644 --- a/cmd/bd/remote_sync_open_test.go +++ b/cmd/bd/remote_sync_open_test.go @@ -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{}) @@ -157,8 +166,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 { @@ -179,6 +188,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) diff --git a/cmd/bd/uow_factory.go b/cmd/bd/uow_factory.go index 9facce662..576cb5d99 100644 --- a/cmd/bd/uow_factory.go +++ b/cmd/bd/uow_factory.go @@ -210,7 +210,21 @@ func openProxiedServerUOWProvider(ctx context.Context, beadsDir, databaseOverrid if err != nil { return nil, err } - return newSQLServerUOWProvider(ctx, beadsDir, topology, opts...) + // 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. + return newSQLServerUOWProvider(ctx, beadsDir, topology, proxiedProviderOptions(opts)...) +} + +// proxiedProviderOptions is opts plus uow.WithProxiedServerMode(), built on a +// fresh slice so the caller's backing array is never written even when it has +// spare capacity. Extracted (rather than inlined at the call site) so the +// wiring has something to unit test; see TestProxiedProviderOptions. +func proxiedProviderOptions(opts []uow.ProviderOption) []uow.ProviderOption { + return append(append([]uow.ProviderOption{}, opts...), uow.WithProxiedServerMode()) } // newSQLServerUOWProvider is the single funnel every unit-of-work provider bd diff --git a/cmd/bd/uow_factory_test.go b/cmd/bd/uow_factory_test.go index 6eabc3e8b..032136b98 100644 --- a/cmd/bd/uow_factory_test.go +++ b/cmd/bd/uow_factory_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/steveyegge/beads/internal/configfile" + "github.com/steveyegge/beads/internal/storage/uow" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -47,6 +48,51 @@ func TestRootProviderOptions(t *testing.T) { } } +// TestProxiedProviderOptions pins the proxied-only half of the open posture: +// every proxied open gains exactly one option on top of the caller's, the +// proxied-server marker the data-behind refusal reads to say where `bd dolt +// pull` can be run. Dropping that append would put a proxied workspace back on +// the runnable-here remedy its own front door refuses. As with +// TestRootProviderOptions, the options are opaque from here: uow's +// TestApplyProviderOptions pins what WithProxiedServerMode sets, and +// TestNewDoltServerUOWProvider_CarriesProxiedServerMode that the open carries +// it to the gate. +func TestProxiedProviderOptions(t *testing.T) { + for _, tt := range []struct { + name string + in []uow.ProviderOption + }{ + {name: "no caller options"}, + {name: "preview", in: []uow.ProviderOption{uow.WithPreview()}}, + {name: "read-only", in: []uow.ProviderOption{uow.WithReadOnly()}}, + } { + t.Run(tt.name, func(t *testing.T) { + out := proxiedProviderOptions(tt.in) + if len(out) != len(tt.in)+1 { + t.Fatalf("proxiedProviderOptions(%d options) len = %d, want %d", len(tt.in), len(out), len(tt.in)+1) + } + for i, o := range out { + if o == nil { + t.Fatalf("proxiedProviderOptions(%d options)[%d] is nil", len(tt.in), i) + } + } + }) + } + + // The append must not land in the caller's backing array: with spare + // capacity an in-place append would write the marker into a slice the + // caller still owns. + in := make([]uow.ProviderOption, 1, 4) + in[0] = uow.WithPreview() + out := proxiedProviderOptions(in) + if &out[0] == &in[0] { + t.Fatal("proxiedProviderOptions returned the caller's backing array") + } + if in[:cap(in)][1] != nil { + t.Fatal("proxiedProviderOptions wrote into the caller's spare capacity") + } +} + func TestNewProxiedServerUOWProvider_RoutesExternalConfigToExternalProvider(t *testing.T) { beadsDir := t.TempDir() require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{ diff --git a/docs/getting-started/upgrading.md b/docs/getting-started/upgrading.md index 717dfbd91..6aaecc52b 100644 --- a/docs/getting-started/upgrading.md +++ b/docs/getting-started/upgrading.md @@ -284,7 +284,9 @@ There is one exception, and the gate names it when it applies: if the stop is the *data-behind* one — this clone is level with the remote on schema but is missing commits it has not pulled — then `bd dolt pull` is the remedy and is allowed through, because migrating before that pull is what wedges the clone. -See [Clone behind the remote](#clone-behind-the-remote). Every other +A proxied-server workspace refuses `bd dolt pull` outright, so there the pull +runs on the server host instead. See +[Clone behind the remote](#clone-behind-the-remote). Every other pending-migration refusal still blocks the pull, so the ordering rule above is what to plan for. @@ -384,6 +386,8 @@ It runs in both server and embedded modes. This is the one pending-migration stop whose remedy is a `bd dolt pull`, and the one the gate links here rather than to the migrate-or-adopt recipe above. +A proxied-server workspace refuses that pull, so it runs on the server host +instead; the last part of this section covers that case. It fires when the clone is **level with the remote on schema but behind it in data** — the remote has commits this clone has not pulled. On schema alone the @@ -430,6 +434,25 @@ bare `bd migrate schema` consent applies only to a shared database with *no* remote. It is safe at this point and only at this point: the pull has already landed the commits the clone was missing. +**In proxied-server mode** the database is shared too, so the same two steps +apply, but they run from two places. The workspace refuses `bd dolt pull` +(`proxy.dolt_pull.unsupported`) and `bd conflicts`, so the pull, and any +conflict resolution it needs, runs where the database lives: on the server +host, from a workspace with direct database access. The consent step can then +be run from the proxied workspace, but only once that pull has landed: + +```bash +bd dolt pull # on the server host, with direct database access +bd migrate schema --force # then, from the proxied workspace +``` + +Nothing in the proxied workspace enforces that order. It refuses the bare +`bd migrate` verb but not `bd migrate schema --force`, and +`BD_ALLOW_REMOTE_MIGRATE=1` grants the same consent to any command. Both are +honored before this stop is reached, so the stop never sees them: run either +one before the pull and it migrates the database while it is still behind. +Do not reach for either to get past the stop. + ### Shared servers A server-mode database is served to every `bd` client connected to that @@ -490,7 +513,7 @@ there: do step 2 from a client that is already set up, or join and migrate in one step with `BD_ALLOW_REMOTE_MIGRATE=1 bd init …`. The same rules apply in **proxied-server mode**, which is a shared server -reached through a local proxy. Two differences worth knowing: +reached through a local proxy. Three differences worth knowing: - read commands print a warning and keep serving the current schema, rather than the read simply not touching it; @@ -498,7 +521,10 @@ reached through a local proxy. Two differences worth knowing: because a daemon has no operator to consent for it. Reconcile the schema first with step 2, then start the daemon — or, for an unattended service, put `BD_ALLOW_REMOTE_MIGRATE=1` in its environment as an explicit, auditable - standing consent. + standing consent; +- the data-behind stop's remedy, `bd dolt pull`, cannot be run from the + proxied workspace: run it on the server host first, then consent from the + proxied workspace (see [Clone behind the remote](#clone-behind-the-remote)). ## Cross-era Upgrades diff --git a/docs/recovery/init-safety.md b/docs/recovery/init-safety.md index 1592552f7..ae66f6095 100644 --- a/docs/recovery/init-safety.md +++ b/docs/recovery/init-safety.md @@ -422,7 +422,8 @@ skipping straight to `bd list`) can hit either of two live gotchas — see binary is installed, `bd dolt push`/`bd dolt pull` are gated too, so this must happen first. The one exception is the *data-behind* stop — a clone level on schema but missing commits the remote has — where `bd dolt pull` **is** the - remedy and is allowed through; the gate says so when it fires, and + remedy and is allowed through (a proxied-server workspace refuses it, so + there the pull runs on the server host); the gate says so when it fires, and [Clone behind the remote](/getting-started/upgrading#clone-behind-the-remote) has the recipe. Plan for the rule, not the exception. - **One designated migrator**: upgrade one machine, let it migrate, then diff --git a/internal/storage/schema/remote_migrate_gate.go b/internal/storage/schema/remote_migrate_gate.go index 13630f4e9..0d6075dd6 100644 --- a/internal/storage/schema/remote_migrate_gate.go +++ b/internal/storage/schema/remote_migrate_gate.go @@ -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/capability_registry.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 @@ -322,6 +345,22 @@ 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" @@ -340,6 +379,26 @@ func (e *RemoteMigrateGateError) dataBehindBody() string { " 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 capability registry keys on the command path + // (cmd/bd/capability_registry.go): it refuses the bare `migrate` + // verb and its hooks/issues/sync children, but permits + // `migrate schema`, so the two-word path passes + // validateProxyRegistryBeforeProvider 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" + @@ -354,10 +413,28 @@ func (e *RemoteMigrateGateError) dataBehindBody() string { 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. + // 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 goes for the consent command above until the pull has finished:\n" + - " what makes it safe is that there is nothing left to pull by then.\n" + " 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 } @@ -484,6 +561,12 @@ 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 (" + SharedConsentCommandForced + " — this store is remote-backed, so the bare verb does not unlock it; see SharedConsentCommandForced): " + @@ -569,8 +652,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, @@ -583,11 +678,24 @@ func (e *RemoteMigrateGateError) Options() []GateOption { // 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 + // capability registry permits the two-word "migrate schema" + // path, so it gets 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)", + When: consentWhen, Commands: []string{SharedConsentCommandForced}, - 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. 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", + Risk: consentRisk, }} } return []GateOption{pull} diff --git a/internal/storage/schema/smart_remote_migrate_gate.go b/internal/storage/schema/smart_remote_migrate_gate.go index ab26d88a2..97dc5b121 100644 --- a/internal/storage/schema/smart_remote_migrate_gate.go +++ b/internal/storage/schema/smart_remote_migrate_gate.go @@ -246,11 +246,12 @@ 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 -// IsStrictAncestor wired, the fact narrows to the ahead == 0 shape; the -// diverged shape then reads "not behind", which is the pre-widening +// 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. // // Unlike routeAdoptFastForward, a query error is NOT folded into "false": diff --git a/internal/storage/schema/smart_remote_migrate_gate_test.go b/internal/storage/schema/smart_remote_migrate_gate_test.go index bc3bbc4b7..28093fc1c 100644 --- a/internal/storage/schema/smart_remote_migrate_gate_test.go +++ b/internal/storage/schema/smart_remote_migrate_gate_test.go @@ -1312,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 diff --git a/internal/storage/uow/BUILD.bazel b/internal/storage/uow/BUILD.bazel index fed8ccd9a..4eebb70b6 100644 --- a/internal/storage/uow/BUILD.bazel +++ b/internal/storage/uow/BUILD.bazel @@ -71,6 +71,7 @@ go_library( "//internal/storage/memoryops", "//internal/storage/schema", "//internal/storage/sqlbuild", + "//internal/storage/versioncontrolops", "//internal/types", "//internal/workapi", "//issueops", @@ -105,6 +106,7 @@ go_test( "counter_contract_test.go", "cross_record_invariant_contract_test.go", "cycle_detector_contract_test.go", + "data_behind_migrate_gate_test.go", "deleter_contract_test.go", "dependency_editor_contract_test.go", "dolt_sql_provider_lock_test.go", diff --git a/internal/storage/uow/data_behind_migrate_gate_test.go b/internal/storage/uow/data_behind_migrate_gate_test.go new file mode 100644 index 000000000..7cd51cba2 --- /dev/null +++ b/internal/storage/uow/data_behind_migrate_gate_test.go @@ -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 proxied capability registry keys on the command path and + // permits `migrate schema`, so the forced schema verb gets 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) + } + } +} diff --git a/internal/storage/uow/dolt_sql_provider.go b/internal/storage/uow/dolt_sql_provider.go index e7681d800..23d49c6a8 100644 --- a/internal/storage/uow/dolt_sql_provider.go +++ b/internal/storage/uow/dolt_sql_provider.go @@ -24,6 +24,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 ( @@ -54,6 +55,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 @@ -134,6 +139,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. @@ -146,6 +162,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 { @@ -285,10 +308,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) } @@ -302,6 +333,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 @@ -327,6 +391,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"+ @@ -783,6 +860,7 @@ func openAndInitSchema(ctx context.Context, ep proxy.Endpoint, database, rootUse expectedProjectID: expectedProjectID, preview: opts.preview, readOnly: opts.readOnly, + proxiedServerMode: opts.proxiedServerMode, } } diff --git a/internal/storage/uow/doltserver_provider_test.go b/internal/storage/uow/doltserver_provider_test.go index b6b03f7d0..dab2f6174 100644 --- a/internal/storage/uow/doltserver_provider_test.go +++ b/internal/storage/uow/doltserver_provider_test.go @@ -115,6 +115,69 @@ func TestNewDoltServerUOWProvider_HappyPath(t *testing.T) { t.Cleanup(func() { _ = provider.Close(context.Background()) }) } +// TestNewDoltServerUOWProvider_CarriesProxiedServerMode pins the link of the +// proxied-server marker that the data-behind gate tests cannot see: they build +// the provider with the field already set, so they stay green if the open +// stops copying the option into the providers it builds, the one the gate +// runs on included. Both polarities, because the open `bd serve` makes for a +// server-mode workspace passes no option and must keep the runnable-here +// remedy. The two opens share one server. +func TestNewDoltServerUOWProvider_CarriesProxiedServerMode(t *testing.T) { + testutil.RequireDoltBinary(t) + bin, err := exec.LookPath("dolt") + require.NoError(t, err) + + 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) + verifiedShutdownCleanup(t, storeRootDir) + cfgPath := writeServerConfig(t, port) + logPath := filepath.Join(t.TempDir(), "server.log") + + for _, tt := range []struct { + name string + opts []ProviderOption + want bool + }{ + {name: "server-mode open", want: false}, + {name: "proxied-server open", opts: []ProviderOption{WithProxiedServerMode()}, want: true}, + } { + t.Run(tt.name, func(t *testing.T) { + provider, err := NewDoltServerUOWProvider( + context.Background(), + storeRootDir, + "beads", + logPath, + cfgPath, + proxy.BackendLocalServer, + "root", + "", + bin, + 0, + 0, + false, + "", + tt.opts..., + ) + require.NoError(t, err) + require.NotNil(t, provider) + t.Cleanup(func() { _ = provider.Close(context.Background()) }) + + sqlProvider, ok := provider.(*doltSQLProvider) + require.True(t, ok, "NewDoltServerUOWProvider returned %T, want *doltSQLProvider", provider) + assert.Equal(t, tt.want, sqlProvider.proxiedServerMode, "provider.proxiedServerMode") + }) + } +} + func TestNewDoltServerUOWProvider_ConcurrentInstantiation(t *testing.T) { testutil.RequireDoltBinary(t) bin, err := exec.LookPath("dolt") diff --git a/internal/storage/uow/preview_provider_test.go b/internal/storage/uow/preview_provider_test.go index 6f815c55e..034df323c 100644 --- a/internal/storage/uow/preview_provider_test.go +++ b/internal/storage/uow/preview_provider_test.go @@ -62,12 +62,22 @@ func TestInitSchemaPreviewMissingDatabaseFailsClosed(t *testing.T) { } // TestApplyProviderOptions keeps the option plumbing honest: the ordinary -// callers pass nothing and must stay on the mutating open. +// callers pass nothing and must stay on the mutating open, and only an open +// that asks for it is treated as proxied-server mode. func TestApplyProviderOptions(t *testing.T) { - if got := applyProviderOptions(nil); got.preview { + got := applyProviderOptions(nil) + if got.preview { t.Fatal("applyProviderOptions(nil).preview = true, want false") } + if got.proxiedServerMode { + t.Fatal("applyProviderOptions(nil).proxiedServerMode = true, want false") + } if got := applyProviderOptions([]ProviderOption{WithPreview()}); !got.preview { t.Fatal("applyProviderOptions(WithPreview()).preview = false, want true") } + // The proxied marker changes what a refusal says, not how the open + // behaves, so it must not switch on either open posture as well. + if got := applyProviderOptions([]ProviderOption{WithProxiedServerMode()}); !got.proxiedServerMode || got.preview || got.readOnly { + t.Fatalf("applyProviderOptions(WithProxiedServerMode()) = %+v, want only proxiedServerMode set", got) + } }