beads/cmd/bd/dolt_remote_adopt.go
matt wilkie 61a016b662
fix(dolt): require consent before adopting a Dolt remote derived from git origin (#5068) (#5188)
* fix(dolt): require consent before adopting a Dolt remote derived from git origin (#5068)

On a rig with no Dolt remote configured, `bd dolt push` and `bd sync` derived
one from git origin, called AddRemote, persisted sync.remote into
.beads/config.yaml, committed that config change under the user's git identity,
and uploaded the full issue history. No prompt, no flag, no opt-out. The
reporter's git origin was a public repo, so private issue history was published
by a command they believed targeted an already-configured remote.

The pre-existing hasConfiguredRemote probe (59241bf16) is not a guard against
this: it is a correctness fix for a stale-empty-listing edge case in the
GH#2118 cold-start window, and it only decides *whether a remote is already
configured*, never whether adopting a new one is wanted.

Adoption now fails closed:

  - interactive: show the derived URL and every side effect that follows a yes
    (AddRemote, the sync.remote write, the config commit made under the user's
    git identity, the upload) and default to NO. An empty line, EOF or a read
    error is a decline.
  - non-interactive: refuse and exit non-zero, naming the URL that would have
    been adopted and the explicit opt-in. Not knowing where the upload was
    going was half the reported complaint.
  - --yes/-y consents ahead of time for scripted use, and still announces the
    target so a scripted run leaves a record of where it uploaded.
  - --no-adopt or BD_NO_REMOTE_ADOPT=1 disables adoption; it beats --yes, so a
    --yes buried in an alias cannot defeat a deliberate machine-level opt-out.

Deliberately NOT reusing confirmPrompt from bootstrap.go: it returns true when
stdin is not a TTY. That is the right default for "proceed with the thing you
asked for" and the wrong one here.

`bd sync` gets the same gate and the same flags. It publishes the same history
to the same derived remote, so leaving it ungated would just move the hole.
This is the part most likely to need a maintainer ruling: an automated `bd
sync` on a rig that was relying on implicit adoption now needs --yes or a
configured remote.

Also moved workspace resolution below the gate. selectedDoltBeadsDir calls
prepareSelectedNoDBContext, which mutates process and on-disk workspace state,
so on the old ordering that mutation happened before consent. Found because
tests that called the real function polluted the shared test binary and broke
TestInitDatabaseAdoptsExistingProjectID.

The consent decision is a pure function (decideRemoteAdoption) plus a pure
application step (applyAdoptionConsent), so the fail-closed property is
asserted by table tests rather than inferred from the call site, and the tests
need no TTY, store, or repo to mutate.

Rigs that already have a remote configured are unaffected: that path is an
early return above the gate, and the existing persisted-remote veto test now
passes AssumeYes so it still fails if the veto is ever moved below consent.

Validated: go build ./... ; go vet ./cmd/bd ; the adoption/sync/init test set
passes and no longer pollutes. Full `make test` is in the local verification
queue (the cmd/bd package exceeds go test's default 10m timeout in-session).

Agent-Signature: claude-opus-5-medium on behalf of maphew

* test(dolt): update the embedded adoption tests for the consent gate (#5068)

CI caught what my local `make test` could not: TestEmbeddedInit's
dolt_push_lazily_adopts_later_git_origin and
dolt_push_adopts_target_origin_with_dash_c assert that `bd dolt push` adopts a
git origin, and a test process has no TTY, so the new gate correctly refuses.

Those two subtests cover a capability this PR keeps, not the behavior it
removes, so they now pass --yes — the scripted consent. Their assertions are
otherwise untouched.

Added dolt_push_refuses_to_adopt_without_consent alongside them: no TTY, no
--yes, and then all four things that must NOT have happened — the push fails,
the refusal names the remote it would have adopted, no Dolt remote was added,
sync.remote was not persisted, and refs/dolt/data is absent on the bare remote
(i.e. nothing was uploaded). That last assertion is the one the issue is
actually about.

Why local validation missed it: cmd/bd/init_embedded_test.go skips unless
BEADS_TEST_EMBEDDED_DOLT=1, and CI runs it as sharded "Test (Embedded Dolt Cmd
N/20)" jobs. `make test` going green does not exercise it. Recorded as a bd
memory so the next agent touching push/sync/init runs it explicitly.

Agent-Signature: claude-opus-5-medium on behalf of maphew

* review fixes: --json is non-interactive; refusals name the caller's own command

Two consent-surface gaps: a JSON-mode run on a pty would block forever on
the interactive prompt (JSON is a machine contract — refuse with the
--yes guidance instead, matching the hook-migration consent gate), and
the refusal/prompt text hardcoded `bd dolt push --yes`, steering bd sync
users into a pull-less push. Adoption messages now carry an adoptOptIn
naming the command actually run. Also: push --help documents adoption,
and the dead errors.Is line is gone.

Agent-Signature: claude-code-claude-fable-5-unknown-reasoning on behalf of Steve Yegge
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xmjba2GjaHnrvaU2sM8Kz5

---------

Co-authored-by: Beadwright <beadwright@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-31 09:41:37 -07:00

189 lines
7.7 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"strings"
"golang.org/x/term"
)
// Git-origin adoption is the one write in the push path that can send private
// issue history somewhere the user never named (#5068). Before this gate,
// `bd dolt push` on a rig with no Dolt remote derived one from git origin,
// wired it, persisted sync.remote, committed that config change under the
// user's git identity, and uploaded — with no prompt, no flag and no opt-out.
// A public git origin therefore published the whole issue database on a
// command the user believed targeted an already-configured remote.
//
// The rule here is fail-closed: adoption is a consent decision, so the absence
// of consent is a refusal, never a default yes. Note that confirmPrompt in
// bootstrap.go deliberately returns true when stdin is not a TTY — that is the
// right default for "proceed with the thing you asked for" and the wrong one
// for this, which is why adoption does not reuse it.
// adoptOptIn is the caller's identity for adoption messages: the command that
// re-runs this operation with consent, and the verb the interactive question
// uses. The sync path shares the whole adoption flow with push
// (syncAdoptGitOrigin), and a refusal that steers a sync user into
// `bd dolt push --yes` would trade their pull away — the opt-in must name the
// command they actually ran.
type adoptOptIn struct {
rerun string // e.g. "bd dolt push --yes"
action string // completes "Adopt this remote and <action>?"
}
var (
pushAdoptOptIn = adoptOptIn{rerun: "bd dolt push --yes", action: "push"}
syncAdoptOptIn = adoptOptIn{rerun: "bd sync --yes", action: "sync"}
)
// adoptPolicy carries the caller's consent inputs for git-origin adoption.
// It is a value, not global state, so the decision is testable without a TTY,
// a store, or a repo to mutate.
type adoptPolicy struct {
// AssumeYes is --yes/-y: consent given ahead of time, for scripted use.
AssumeYes bool
// Disabled is --no-adopt or BD_NO_REMOTE_ADOPT=1: never adopt.
Disabled bool
// Interactive reports whether we can actually ask. False means fail-closed.
Interactive bool
}
// adoptDecision is what to do once a git origin URL has been derived but
// before anything has been written.
type adoptDecision int
const (
// adoptRefuse: do not adopt. The caller explains why and stops.
adoptRefuse adoptDecision = iota
// adoptAsk: prompt the user, showing the derived URL.
adoptAsk
// adoptProceed: consent already given (--yes).
adoptProceed
)
// adoptRefusal distinguishes the two refusals, which need different messages
// and different exit behavior from the caller.
type adoptRefusal int
const (
adoptRefusalNone adoptRefusal = iota
// adoptRefusedDisabled: the user asked us not to adopt. Not an error.
adoptRefusedDisabled
// adoptRefusedNonInteractive: we cannot ask and were not told. An error.
adoptRefusedNonInteractive
)
// decideRemoteAdoption is the whole policy, kept pure so the fail-closed
// property is asserted by a table test rather than inferred from the call site.
//
// Precedence: an explicit --no-adopt beats an explicit --yes (the safer of two
// contradictory instructions wins), and both beat interactivity.
func decideRemoteAdoption(p adoptPolicy) (adoptDecision, adoptRefusal) {
switch {
case p.Disabled:
return adoptRefuse, adoptRefusedDisabled
case p.AssumeYes:
return adoptProceed, adoptRefusalNone
case p.Interactive:
return adoptAsk, adoptRefusalNone
default:
return adoptRefuse, adoptRefusedNonInteractive
}
}
// currentAdoptPolicy builds the policy from flags plus BD_NO_REMOTE_ADOPT.
// stdinIsTerminal is injected so tests do not depend on how they were invoked.
// jsonMode forces non-interactive even on a TTY: --json is a machine contract,
// and a consent prompt inside it hangs whatever is parsing the stream (same
// rule as validateHookMigrationApplyConsent — JSON callers opt in with --yes).
func currentAdoptPolicy(assumeYes, noAdopt, stdinIsTerminal, jsonMode bool) adoptPolicy {
return adoptPolicy{
AssumeYes: assumeYes,
Disabled: noAdopt || envNoRemoteAdopt(),
Interactive: stdinIsTerminal && !jsonMode,
}
}
func envNoRemoteAdopt() bool {
v := strings.TrimSpace(strings.ToLower(os.Getenv("BD_NO_REMOTE_ADOPT")))
return v == "1" || v == "true" || v == "yes"
}
func stdinIsTerminal() bool {
return term.IsTerminal(int(os.Stdin.Fd()))
}
// adoptionRefusedError is the message for the non-interactive refusal. It
// names the URL that would have been adopted, because "a remote was derived"
// is useless without knowing which one — the reporter's whole complaint was
// not knowing where the upload was going.
func adoptionRefusedError(remoteURL string, optIn adoptOptIn) error {
return fmt.Errorf(`no Dolt remote is configured, and bd will not adopt one without consent.
Derived from git origin: %s
This would publish your entire issue history to that remote. If it is what you
want, opt in explicitly:
%-38s # adopt the derived remote and %s
bd dolt remote add origin <url> # or name the remote yourself first
To never adopt implicitly, pass --no-adopt or set BD_NO_REMOTE_ADOPT=1.`, remoteURL, optIn.rerun, optIn.action)
}
// applyAdoptionConsent is the gate as the push path uses it: given a derived
// URL and a policy, either return proceed=true (everything after this may
// write) or stop. It is separated from adoptGitOriginRemoteForPush so the
// fail-closed behavior can be asserted without resolving a workspace — that
// resolution mutates whatever repo the test binary runs in, which is exactly
// what the syncAdoptGitOrigin seam comment in sync.go warns about.
//
// proceed=false with err=nil is the deliberate --no-adopt case: the caller
// falls through to its ordinary no-remote handling rather than failing.
func applyAdoptionConsent(remoteURL string, policy adoptPolicy, optIn adoptOptIn) (bool, error) {
switch decision, refusal := decideRemoteAdoption(policy); decision {
case adoptRefuse:
if refusal == adoptRefusedNonInteractive {
return false, adoptionRefusedError(remoteURL, optIn)
}
return false, nil
case adoptAsk:
if !confirmRemoteAdoption(remoteURL, optIn) {
return false, fmt.Errorf("remote adoption declined; nothing was written and nothing was pushed")
}
return true, nil
default: // adoptProceed
// --yes: consent given ahead of time. Still announce the target, so a
// scripted run leaves a record of where it uploaded.
fmt.Fprintf(os.Stderr, "Adopting Dolt remote origin from git origin: %s\n", remoteURL)
return true, nil
}
}
// confirmRemoteAdoption asks, showing the URL and every side effect that
// follows a yes. The config.yaml commit is named here because it is made under
// the user's git identity in their repo, which is a surprise worth disclosing
// before the fact rather than in a line printed after it happened (#5068
// acceptance item 4).
func confirmRemoteAdoption(remoteURL string, optIn adoptOptIn) bool {
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "No Dolt remote is configured for this rig.")
fmt.Fprintf(os.Stderr, " Derived from git origin: %s\n", remoteURL)
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Adopting it will:")
fmt.Fprintln(os.Stderr, " • add it as Dolt remote \"origin\"")
fmt.Fprintln(os.Stderr, " • write sync.remote into .beads/config.yaml")
fmt.Fprintln(os.Stderr, " • commit that config change under your git identity")
fmt.Fprintln(os.Stderr, " • upload your full issue history there")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintf(os.Stderr, "Adopt this remote and %s? [y/N] ", optIn.action)
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
// Default is NO: an empty line, EOF, or a read error must not consent.
return strings.TrimSpace(strings.ToLower(line)) == "y" ||
strings.TrimSpace(strings.ToLower(line)) == "yes"
}