fix(uow): render the shaped gate body on the proxied read-through (bd-mfsky)

readThroughRefusedMigration printed gateErr.UserMessage() only for the
#6575 data-behind stop. Every smart-gate decision that reaches the proxied
read-only open (shared-no-remote, adopt, fork-skew) still went to the
template arm. That arm prints Error(), the one-line summary, and then
"Run 'bd migrate schema' once every client of this server is upgraded".
The operator lost the per-decision body the gate had computed, and on
adopt and fork-skew the appended sentence contradicted that body ("Do NOT
migrate here", "Migrating cannot un-fork it"). It also named a verb whose
consent a remote-backed store never reads (schema.SharedConsentCommandForced).

Widen the shaped arm to Decision != "" || IsDataBehind(), the predicate
embeddeddolt's lenientGateWarningBody uses since #6660. The data-behind
output is byte-identical. The blunt fallback keeps its Error() and
consent-sentence form. The stale "mirrors embedded mode" doc comment now
names the shared body-then-mode-line shape.

TestReadThroughRefusedMigrationWarning drives the real function through
sqlmock and captured stderr across fork-skew, adopt, shared-no-remote,
data-behind and the blunt shared-store fallback. It pins the UserMessage
lead, a body-only marker per decision, termination, and the absence of
`bd migrate --force` and of the bare consent verb where they would be
wrong. Before the fix the three decision rows fail.

PR #6662 follow-up.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Eddie the Engineer 2026-10-16 17:21:31 +00:00
commit b1fdfbebd4
3 changed files with 152 additions and 10 deletions

View file

@ -220,8 +220,8 @@ func TestInitSchemaProxiedDataBehind(t *testing.T) {
})
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
// readThroughRefusedMigration's blunt-fallback template names the
// shared consent verb. 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)

View file

@ -372,8 +372,11 @@ func dataBehindAdopter(readOnly bool) *schema.FastForwardAdopter {
// database cannot be quietly written at a schema this binary believes is
// behind.
//
// This mirrors what embedded mode has done since bd-578h9.5 for its
// read-only-command intent. Without it, extending the gate to the proxied path
// This mirrors embedded mode's read-only-command intent (bd-578h9.5), and the
// warning keeps the body-then-mode-line shape of embeddeddolt's
// lenientGateWarning: a refusal the smart gate shaped leads with its own
// UserMessage body, and only the blunt fallback leads with the one-line
// summary. Without the read-through, extending the gate to the proxied path
// would newly brick every read on a workspace waiting for its operator to
// consent — which is the opposite of the contract the gate's own message
// promises ("read commands keep working against the current schema").
@ -391,12 +394,17 @@ 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() {
// A refusal the smart gate shaped — any Decision, or the #6575 data-behind
// stop — carries its own per-decision body in UserMessage. Print that
// block, which REPLACES the template below rather than decorating it, and
// say only what read-through adds on top. It keys on the same predicate as
// embeddeddolt's lenientGateWarningBody (bd-mfsky, #6660). The template's
// consent sentence would contradict each of those bodies: the data-behind
// remedy is the pull, adopt and fork-skew say not to migrate here at all,
// and on those remote-backed arms the bare verb's consent is never read
// (schema.SharedConsentCommandForced). shared-no-remote's body already
// prescribes that verb itself.
if gateErr.Decision != "" || gateErr.IsDataBehind() {
fmt.Fprintf(os.Stderr,
"Warning: %[1]s"+
" Read-only command: continuing on schema v%[2]d without migrating.\n"+

View file

@ -236,3 +236,137 @@ func TestInitSchemaSharedStoreGate(t *testing.T) {
}
})
}
// TestReadThroughRefusedMigrationWarning pins the composed stderr of the
// proxied read-through (bd-mfsky, the uow leg of #6660). Every refusal the
// smart gate shaped — a non-empty Decision, or the #6575 data-behind stop —
// must lead with the gate's own UserMessage body, as embedded's
// lenientGateWarningBody and server mode's warnLenientOpenRefusal render it.
// Only the blunt fallback keeps the one-line summary plus the consent
// sentence. It drives the real function, so it pins what an operator reads.
func TestReadThroughRefusedMigrationWarning(t *testing.T) {
const modeLine = " Read-only command: continuing on schema v65 without migrating.\n"
for _, tc := range []struct {
name string
gateErr *schema.RemoteMigrateGateError
// marker is text only this refusal's own body produces, so a
// Decision literal that drifts from the schema constant lands on a
// different body and fails here instead of passing vacuously.
marker string
// shaped rows must render UserMessage; the blunt fallback keeps
// Error() and the consent sentence.
shaped bool
// consentVerbOK marks the rows whose text legitimately names the
// bare consent verb: shared-no-remote has no remote, so that verb is
// the consent there, and the blunt fallback's sentence is unchanged.
consentVerbOK bool
// forceOK marks data-behind, whose "Do NOT use `bd migrate --force`"
// paragraph names the command it forbids.
forceOK bool
}{
{
name: "fork-skew",
gateErr: &schema.RemoteMigrateGateError{
CurrentVersion: 65, LatestVersion: 70, Pending: 5,
Decision: "fork-skew", SkewVersions: []int{66}, Shared: true, Proxied: true,
},
marker: "cannot un-fork it",
shaped: true,
},
{
name: "adopt",
gateErr: &schema.RemoteMigrateGateError{
CurrentVersion: 65, LatestVersion: 70, Pending: 5,
Decision: "adopt", Shared: true, Proxied: true,
},
marker: "adopt the remote's migrated database instead",
shaped: true,
},
{
name: "shared-no-remote",
gateErr: &schema.RemoteMigrateGateError{
CurrentVersion: 65, LatestVersion: 70, Pending: 5,
Decision: "shared-no-remote", Shared: true, Proxied: true,
},
marker: "Upgrade bd on every client of this server first",
shaped: true,
consentVerbOK: true,
},
{
name: "data-behind",
gateErr: &schema.RemoteMigrateGateError{
CurrentVersion: 65, LatestVersion: 70, Pending: 5,
FallbackReason: "data-behind", DataDiverged: true, Shared: true, Proxied: true,
},
marker: "Pull first",
shaped: true,
forceOK: true,
},
{
name: "blunt shared-store fallback",
gateErr: &schema.RemoteMigrateGateError{
CurrentVersion: 65, LatestVersion: 70, Pending: 5,
FallbackReason: "shared-store", Shared: true, Proxied: true,
},
marker: "Run '" + schema.SharedConsentCommand + "'",
consentVerbOK: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
if want := tc.name == "data-behind"; tc.gateErr.IsDataBehind() != want {
t.Fatalf("IsDataBehind() = %v, want %v — the fixture's FallbackReason literal drifted", !want, want)
}
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("create sql mock: %v", err)
}
defer db.Close()
ctx := context.Background()
conn, err := db.Conn(ctx)
if err != nil {
t.Fatalf("pin connection: %v", err)
}
defer conn.Close()
mock.ExpectExec(regexp.QuoteMeta("USE `beads`")).
WillReturnResult(sqlmock.NewResult(0, 0))
var readErr error
stderr := captureStderr(t, func() {
readErr = readThroughRefusedMigration(ctx, conn, "beads", tc.gateErr)
})
if readErr != nil {
t.Fatalf("readThroughRefusedMigration() = %v, want nil — the read proceeds on the old schema", readErr)
}
if !strings.Contains(stderr, tc.marker) {
t.Errorf("warning missing %q:\n%s", tc.marker, stderr)
}
// Body first, then the mode line, as on embedded: the shaped body
// leads the warning rather than being replaced by the summary.
lead := "Warning: " + tc.gateErr.Error() + "\n" + modeLine
if tc.shaped {
lead = "Warning: " + tc.gateErr.UserMessage() + modeLine
}
if !strings.HasPrefix(stderr, lead) {
t.Errorf("warning does not open with\n%s\ngot:\n%s", lead, stderr)
}
if !strings.HasSuffix(stderr, "\n") {
t.Errorf("warning is unterminated:\n%q", stderr)
}
if !tc.forceOK && strings.Contains(stderr, "bd migrate --force") {
t.Errorf("warning prescribes `bd migrate --force`:\n%s", stderr)
}
// adopt and fork-skew are remote-backed, where the bare verb's
// consent is never read (schema.SharedConsentCommandForced), and
// their bodies say not to migrate at all. Scrub the forced forms
// first: each contains the bare verb.
scrubbed := strings.ReplaceAll(stderr, schema.SharedConsentCommandForcedGlobal, "")
scrubbed = strings.ReplaceAll(scrubbed, schema.SharedConsentCommandForced, "")
if !tc.consentVerbOK && strings.Contains(scrubbed, schema.SharedConsentCommand) {
t.Errorf("warning prescribes the bare consent verb %q:\n%s", schema.SharedConsentCommand, stderr)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
})
}
}