A freshly (auto-)started sql-server can report an EMPTY dolt_remotes while the remote is persisted on disk in .dolt/repo_state.json (GH#2118). wy-xtv17/wy-82hc5 taught the push/sync no-remote gate to consult the on-disk probe, but three MUTATING siblings still decided 'no remote' from len(ListRemotes) alone (wy-6k7f7, found by the wy-82hc5 adversarial pass): - bd config apply blind-added the federation remote over the invisible one. currentOriginRemoteURL now resolves origin from evidence — SQL listing first, persisted enumeration as the cold-start fallback, a failed listing stays an error — routing apply into the same consistent/update legs it takes after the window. - bd dolt remote add skipped the URL-disagreement confirmation (an empty listing made existingURL "") and silently clobbered the persisted remote. ensureDoltRemote now recovers the persisted URL, so the idempotent re-add stays a no-op and a real disagreement gets the same confirm flow as a visible remote; the confirmed overwrite tolerates the cold server refusing to remove a remote it cannot see. - prepareCLIRouteForGitProtocol fell back to SQL DOLT_PUSH, which the cold server refuses with a bare 'remote origin not found'. A persisted git-protocol remote now recovers the CLI route outright (the CLI transport never needed the SQL listing — the push proceeds); a persisted non-git remote fails with the cold-start explanation instead of the bare error. Recovery beats detection here because the evidence carries URLs, not just existence: new DoltStore.PersistedRemoteInfos() exposes the name+URL enumeration with HasPersistedRemote's exact directory-search semantics (HasPersistedRemote is now derived from it), and the cmd layer peels decorators to reach it via persistedRemoteInfosFor, the recovery-grade sibling of persistedRemoteProberFor. Embedded rigs return nil — GH#2118 is a sql-server phenomenon. Read-only doctor/drift diagnostics still judge from the listing alone (they misreport, they don't mutate) — noted in hasConfiguredRemote's doc; the bead tracks them as lower priority. Claude-Session: https://claude.ai/code/session_01EF8QbCbF6JnTuoCKvu9xiG Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
492 lines
13 KiB
Go
492 lines
13 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"os"
|
||
"strings"
|
||
|
||
"github.com/spf13/cobra"
|
||
"github.com/steveyegge/beads/internal/beads"
|
||
"github.com/steveyegge/beads/internal/config"
|
||
"github.com/steveyegge/beads/internal/doltserver"
|
||
"github.com/steveyegge/beads/internal/git"
|
||
"github.com/steveyegge/beads/internal/metrics"
|
||
"github.com/steveyegge/beads/internal/storage"
|
||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||
"github.com/steveyegge/beads/internal/storage/doltutil"
|
||
)
|
||
|
||
// ApplyResult represents the outcome of a single apply action.
|
||
type ApplyResult struct {
|
||
Check string `json:"check"`
|
||
Action string `json:"action"`
|
||
Status string `json:"status"` // "applied", "dry_run", "skipped", "error", "ok"
|
||
Message string `json:"message"`
|
||
Error string `json:"error,omitempty"`
|
||
}
|
||
|
||
const (
|
||
applyStatusApplied = "applied"
|
||
applyStatusDryRun = "dry_run"
|
||
applyStatusSkipped = "skipped"
|
||
applyStatusError = "error"
|
||
applyStatusOK = "ok"
|
||
)
|
||
|
||
var configApplyCmd = &cobra.Command{
|
||
Use: "apply",
|
||
Short: "Reconcile system state to match configuration",
|
||
Long: `Reconcile actual system state to match declared configuration.
|
||
|
||
Runs drift detection and then fixes any mismatches it finds:
|
||
|
||
- hooks Reinstall git hooks if missing or outdated
|
||
- remote Add/update Dolt origin remote to match federation.remote
|
||
- server Start Dolt server if dolt.shared-server is enabled
|
||
|
||
This command is idempotent — safe to run multiple times. Use --dry-run
|
||
to preview what would change without making modifications.
|
||
|
||
Examples:
|
||
bd config apply
|
||
bd config apply --dry-run
|
||
bd config apply --json`,
|
||
SilenceUsage: true,
|
||
SilenceErrors: true,
|
||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||
evt := metrics.NewCommandEvent("config-apply")
|
||
defer func() {
|
||
if c := metrics.Global(); c != nil {
|
||
c.CloseEventAndAdd(evt)
|
||
}
|
||
}()
|
||
|
||
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||
results := runApply(dryRun)
|
||
|
||
if jsonOutput {
|
||
if err := outputJSON(results); err != nil {
|
||
return err
|
||
}
|
||
} else {
|
||
printApplyResults(results)
|
||
}
|
||
|
||
for _, r := range results {
|
||
if r.Status == applyStatusError {
|
||
return SilentExit()
|
||
}
|
||
}
|
||
return nil
|
||
},
|
||
}
|
||
|
||
func init() {
|
||
configApplyCmd.Flags().Bool("dry-run", false, "Show what would change without making modifications")
|
||
configCmd.AddCommand(configApplyCmd)
|
||
}
|
||
|
||
// runApply detects drift and fixes each category.
|
||
func runApply(dryRun bool) []ApplyResult {
|
||
driftItems := runDriftChecks()
|
||
|
||
// Group drift items by check domain to avoid duplicate actions
|
||
// (e.g., multiple hook items should trigger only one reinstall).
|
||
hasDrift := driftDomains(driftItems)
|
||
|
||
var results []ApplyResult
|
||
results = append(results, applyHooks(hasDrift["hooks"], dryRun))
|
||
serverResult := applyServer(hasDrift["server"], dryRun)
|
||
results = append(results, serverResult)
|
||
results = append(results, remoteApplyResult(driftItems, serverResult, dryRun, checkRemoteDrift))
|
||
return results
|
||
}
|
||
|
||
func shouldRecheckRemoteAfterServerStart(remoteSkipped bool, serverResult ApplyResult) bool {
|
||
return remoteSkipped && serverResult.Check == "server" && serverResult.Status == applyStatusApplied
|
||
}
|
||
|
||
func remoteApplyResult(driftItems []DriftItem, serverResult ApplyResult, dryRun bool, recheckRemote func() []DriftItem) ApplyResult {
|
||
remoteItems := driftItems
|
||
if shouldRecheckRemoteAfterServerStart(skippedDriftDomain(remoteItems, "remote"), serverResult) {
|
||
remoteItems = recheckRemote()
|
||
}
|
||
if skipped := skippedDriftItem(remoteItems, "remote"); skipped != nil {
|
||
return skippedRemoteApplyResult(*skipped)
|
||
}
|
||
return applyRemote(driftDomains(remoteItems)["remote"], dryRun)
|
||
}
|
||
|
||
func driftDomains(items []DriftItem) map[string]bool {
|
||
hasDrift := map[string]bool{}
|
||
for _, item := range items {
|
||
if item.Status == driftStatusDrift {
|
||
hasDrift[driftDomain(item.Check)] = true
|
||
}
|
||
}
|
||
return hasDrift
|
||
}
|
||
|
||
func skippedDriftDomain(items []DriftItem, domain string) bool {
|
||
return skippedDriftItem(items, domain) != nil
|
||
}
|
||
|
||
func skippedDriftItem(items []DriftItem, domain string) *DriftItem {
|
||
for _, item := range items {
|
||
if item.Status == driftStatusSkipped && driftDomain(item.Check) == domain {
|
||
skipped := item
|
||
return &skipped
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func driftDomain(check string) string {
|
||
if before, _, ok := strings.Cut(check, "."); ok {
|
||
return before
|
||
}
|
||
return check
|
||
}
|
||
|
||
func skippedRemoteApplyResult(item DriftItem) ApplyResult {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "none",
|
||
Status: applyStatusSkipped,
|
||
Message: item.Message,
|
||
}
|
||
}
|
||
|
||
func remoteApplyStoreConfig(dryRun bool) *dolt.Config {
|
||
return &dolt.Config{
|
||
ReadOnly: dryRun,
|
||
DisableAutoStart: true,
|
||
}
|
||
}
|
||
|
||
func remoteURLMatchesConfig(currentURL, configuredURL string) bool {
|
||
return doltutil.RemoteURLsMatch(currentURL, configuredURL)
|
||
}
|
||
|
||
// applyHooks reinstalls git hooks if drift was detected.
|
||
func applyHooks(drifted bool, dryRun bool) ApplyResult {
|
||
if !drifted {
|
||
return ApplyResult{
|
||
Check: "hooks",
|
||
Action: "none",
|
||
Status: applyStatusOK,
|
||
Message: "Git hooks are up to date",
|
||
}
|
||
}
|
||
|
||
if dryRun {
|
||
return ApplyResult{
|
||
Check: "hooks",
|
||
Action: "reinstall",
|
||
Status: applyStatusDryRun,
|
||
Message: "Would reinstall git hooks",
|
||
}
|
||
}
|
||
|
||
// Verify we're in a git repo
|
||
if _, err := git.GetGitHooksDir(); err != nil {
|
||
return ApplyResult{
|
||
Check: "hooks",
|
||
Action: "reinstall",
|
||
Status: applyStatusSkipped,
|
||
Message: "Not a git repository",
|
||
}
|
||
}
|
||
|
||
if err := installHooksWithOptions(managedHookNames, false, false, false, false); err != nil {
|
||
return ApplyResult{
|
||
Check: "hooks",
|
||
Action: "reinstall",
|
||
Status: applyStatusError,
|
||
Message: "Failed to reinstall git hooks",
|
||
Error: err.Error(),
|
||
}
|
||
}
|
||
|
||
return ApplyResult{
|
||
Check: "hooks",
|
||
Action: "reinstall",
|
||
Status: applyStatusApplied,
|
||
Message: fmt.Sprintf("Reinstalled git hooks (%d hooks updated)", len(managedHookNames)),
|
||
}
|
||
}
|
||
|
||
// applyRemote ensures the Dolt origin remote matches federation.remote config.
|
||
// originRemoteEvidence is the store surface currentOriginRemoteURL needs:
|
||
// the SQL-visible remotes plus the on-disk persisted enumeration. The
|
||
// concrete *dolt.DoltStore that applyRemote opens satisfies both directly —
|
||
// no decorator peel needed here (unlike cmd paths holding the wired store).
|
||
type originRemoteEvidence interface {
|
||
ListRemotes(ctx context.Context) ([]storage.RemoteInfo, error)
|
||
PersistedRemoteInfos() []storage.RemoteInfo
|
||
}
|
||
|
||
// currentOriginRemoteURL resolves the origin remote's URL from evidence, not
|
||
// from the SQL listing alone: a freshly (auto-)started sql-server can report
|
||
// an empty dolt_remotes while the remote is persisted on disk in
|
||
// .dolt/repo_state.json (GH#2118, wy-6k7f7). Recovering the persisted URL
|
||
// routes `bd config apply` into the same consistent/update legs it would
|
||
// take after the window, instead of blind-adding over an invisible remote.
|
||
// A failed listing returns the error — it is never evidence of "no remote".
|
||
func currentOriginRemoteURL(ctx context.Context, st originRemoteEvidence) (string, error) {
|
||
remotes, err := st.ListRemotes(ctx)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
for _, r := range remotes {
|
||
if r.Name == "origin" {
|
||
return r.URL, nil
|
||
}
|
||
}
|
||
for _, r := range st.PersistedRemoteInfos() {
|
||
if r.Name == "origin" {
|
||
return r.URL, nil
|
||
}
|
||
}
|
||
return "", nil
|
||
}
|
||
|
||
func applyRemote(drifted bool, dryRun bool) ApplyResult {
|
||
if !drifted {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "none",
|
||
Status: applyStatusOK,
|
||
Message: "Dolt remote configuration is consistent",
|
||
}
|
||
}
|
||
|
||
beadsDir := beads.FindBeadsDir()
|
||
if beadsDir == "" {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "configure",
|
||
Status: applyStatusSkipped,
|
||
Message: "No active beads workspace found",
|
||
}
|
||
}
|
||
|
||
federationRemote := config.GetString("federation.remote")
|
||
if federationRemote == "" {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "configure",
|
||
Status: applyStatusSkipped,
|
||
Message: "federation.remote is not set in config",
|
||
}
|
||
}
|
||
|
||
ctx := context.Background()
|
||
st, err := dolt.NewFromConfigWithOptions(ctx, beadsDir, remoteApplyStoreConfig(dryRun))
|
||
if err != nil {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "configure",
|
||
Status: applyStatusError,
|
||
Message: "Failed to open Dolt store",
|
||
Error: err.Error(),
|
||
}
|
||
}
|
||
defer func() { _ = st.Close() }()
|
||
|
||
currentURL, err := currentOriginRemoteURL(ctx, st)
|
||
if err != nil {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "configure",
|
||
Status: applyStatusError,
|
||
Message: "Failed to list Dolt remotes",
|
||
Error: err.Error(),
|
||
}
|
||
}
|
||
|
||
if dryRun {
|
||
if currentURL == "" {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "add_remote",
|
||
Status: applyStatusDryRun,
|
||
Message: fmt.Sprintf("Would add Dolt origin remote: %s", federationRemote),
|
||
}
|
||
}
|
||
if remoteURLMatchesConfig(currentURL, federationRemote) {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "none",
|
||
Status: applyStatusOK,
|
||
Message: "Dolt remote configuration is consistent",
|
||
}
|
||
}
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "update_remote",
|
||
Status: applyStatusDryRun,
|
||
Message: fmt.Sprintf("Would update Dolt origin remote from %s to %s", currentURL, federationRemote),
|
||
}
|
||
}
|
||
|
||
if currentURL == "" {
|
||
if err := st.AddRemote(ctx, "origin", federationRemote); err != nil {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "add_remote",
|
||
Status: applyStatusError,
|
||
Message: "Failed to add Dolt origin remote",
|
||
Error: err.Error(),
|
||
}
|
||
}
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "add_remote",
|
||
Status: applyStatusApplied,
|
||
Message: fmt.Sprintf("Added Dolt origin remote: %s", federationRemote),
|
||
}
|
||
}
|
||
|
||
if remoteURLMatchesConfig(currentURL, federationRemote) {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "none",
|
||
Status: applyStatusOK,
|
||
Message: "Dolt remote configuration is consistent",
|
||
}
|
||
}
|
||
|
||
oldURL := currentURL
|
||
if err := st.RemoveRemote(ctx, "origin"); err != nil {
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "update_remote",
|
||
Status: applyStatusError,
|
||
Message: "Failed to remove old Dolt origin remote",
|
||
Error: err.Error(),
|
||
}
|
||
}
|
||
|
||
if err := st.AddRemote(ctx, "origin", federationRemote); err != nil {
|
||
_ = st.AddRemote(ctx, "origin", oldURL)
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "update_remote",
|
||
Status: applyStatusError,
|
||
Message: "Failed to add new Dolt origin remote (old remote restored)",
|
||
Error: err.Error(),
|
||
}
|
||
}
|
||
|
||
return ApplyResult{
|
||
Check: "remote",
|
||
Action: "update_remote",
|
||
Status: applyStatusApplied,
|
||
Message: fmt.Sprintf("Updated Dolt origin remote from %s to %s", oldURL, federationRemote),
|
||
}
|
||
}
|
||
|
||
// applyServer starts the Dolt server if config says it should be running but it isn't.
|
||
// Does NOT stop a running server if config says it shouldn't be — that's too destructive.
|
||
func applyServer(drifted bool, dryRun bool) ApplyResult {
|
||
if !drifted {
|
||
return ApplyResult{
|
||
Check: "server",
|
||
Action: "none",
|
||
Status: applyStatusOK,
|
||
Message: "Server state is consistent with config",
|
||
}
|
||
}
|
||
|
||
beadsDir := beads.FindBeadsDir()
|
||
if beadsDir == "" {
|
||
return ApplyResult{
|
||
Check: "server",
|
||
Action: "start",
|
||
Status: applyStatusSkipped,
|
||
Message: "No active beads workspace found",
|
||
}
|
||
}
|
||
|
||
// Only start if config says server should be running
|
||
wantServer := config.GetString("dolt.shared-server") == "true"
|
||
if !wantServer {
|
||
// Server is running but config doesn't want it — report but don't stop
|
||
return ApplyResult{
|
||
Check: "server",
|
||
Action: "none",
|
||
Status: applyStatusSkipped,
|
||
Message: "Server is running but dolt.shared-server is not enabled; not stopping (use 'bd dolt stop' manually)",
|
||
}
|
||
}
|
||
|
||
// Reconciliation must honor the same auto-start policy as implicit storage
|
||
// opens. A disabled auto-start means the server is externally managed; keep
|
||
// Action="start" to describe the skipped reconciliation action consistently.
|
||
if doltserver.IsAutoStartDisabled() {
|
||
return ApplyResult{
|
||
Check: "server",
|
||
Action: "start",
|
||
Status: applyStatusSkipped,
|
||
Message: "Dolt shared server not started because auto-start is disabled; the server is externally managed",
|
||
}
|
||
}
|
||
|
||
serverDir := doltserver.ResolveServerDir(beadsDir)
|
||
|
||
if dryRun {
|
||
return ApplyResult{
|
||
Check: "server",
|
||
Action: "start",
|
||
Status: applyStatusDryRun,
|
||
Message: "Would start Dolt shared server",
|
||
}
|
||
}
|
||
|
||
state, err := doltserver.Start(serverDir)
|
||
if err != nil {
|
||
return ApplyResult{
|
||
Check: "server",
|
||
Action: "start",
|
||
Status: applyStatusError,
|
||
Message: "Failed to start Dolt server",
|
||
Error: err.Error(),
|
||
}
|
||
}
|
||
|
||
return ApplyResult{
|
||
Check: "server",
|
||
Action: "start",
|
||
Status: applyStatusApplied,
|
||
Message: fmt.Sprintf("Started Dolt server (PID %d, port %d)", state.PID, state.Port),
|
||
}
|
||
}
|
||
|
||
// printApplyResults renders apply results in human-readable format.
|
||
func printApplyResults(results []ApplyResult) {
|
||
if len(results) == 0 {
|
||
fmt.Println("No actions to apply")
|
||
return
|
||
}
|
||
|
||
statusIcon := map[string]string{
|
||
applyStatusApplied: "✓",
|
||
applyStatusDryRun: "~",
|
||
applyStatusSkipped: "–",
|
||
applyStatusError: "✗",
|
||
applyStatusOK: "✓",
|
||
}
|
||
|
||
for _, r := range results {
|
||
icon := statusIcon[r.Status]
|
||
if icon == "" {
|
||
icon = "?"
|
||
}
|
||
fmt.Fprintf(os.Stdout, " %s %s: %s\n", icon, r.Check, r.Message)
|
||
if r.Error != "" {
|
||
fmt.Fprintf(os.Stdout, " error: %s\n", r.Error)
|
||
}
|
||
}
|
||
}
|