* fix(workspace): align resolved workspace messaging and config lookup intent(workspace): pure worktrees and external BEADS_DIR setups should work without implying a local .beads directory is required decision(config): derive config repo roots from FindBeadsDir-backed workspace resolution instead of a local-only .beads walk constraint(workspace-detection): keep FindBeadsDir ordering and storage behavior unchanged while making bd where the primary diagnostic hint learned(testing): shared-server env and global .beads ignore rules can distort CGO suites, so regression coverage now normalizes those test paths * fix(bootstrap): align no-workspace JSON wording intent(bootstrap): machine-readable no-workspace output should match the resolved-workspace guidance used elsewhere on this branch decision(bootstrap-json): reuse the shared workspace error helpers through a dedicated payload builder so tests can lock the JSON contract constraint(review-loop): keep the bootstrap CLI flow unchanged outside the no-workspace JSON branch while adding a focused regression test * fix(where): allow no-workspace diagnostics without db (beads-9f74-5l1) intent(where): no-workspace guidance should point to a command that actually runs when workspace resolution fails decision(startup): treat top-level where as a no-db command so it reaches its own handler before database preflight constraint(where-tests): subprocess mode can change the exact hint text, so the regression test locks the JSON contract and stable guidance fragments instead * fix(bootstrap): restore bd where guidance for no-workspace output (beads-9f74-3c3) intent(bootstrap): no-workspace bootstrap guidance should match the resolved-workspace diagnostics introduced on this branch decision(no-workspace-hints): reuse the shared hint that mentions bd where instead of the bootstrap-specific fallback text constraint(bootstrap-tests): cover both the payload helper and subprocess CLI path so wording regressions are caught before release * fix(workspace): remove no-workspace .beads fallbacks (beads-9f74-gr1) intent(workspace-resolution): finish the GH#3124 follow-up for runtime paths that still fabricated local .beads state rejected(no-workspace-fallback): inventing ./ .beads paths outside an active workspace hid shared-worktree resolution and created stray files decision(setup-recipes): keep built-in bd setup recipes usable without a workspace but require an active workspace for custom recipe state constraint(worktree-support): completion and backup helpers still need to resolve shared main-repo workspaces from a git worktree * fix(config): skip no-workspace store init for store-free commands intent(config): review-fix-loop found no-workspace config commands should reach their own graceful paths instead of failing in PersistentPreRun decision(config-preflight): detect store-free config subcommands before DB synthesis so config.yaml, git-backed, and diagnostic flows can run without an opened store constraint(json-contract): keep bd config validate --json machine-readable on stdout once pre-run stops short-circuiting into low-level DB errors learned(config-preflight): the parent config command not being in noDbCommands leaked store init into show, validate, drift, apply, and yaml-only config helpers * fix(ci): restore init prefix normalization and formatting intent(ci): clear the current PR failures instead of leaving the branch blocked behind known red checks decision(init-prefix): normalize dotted prefixes before persisting issue_prefix so config, metadata.json, and Dolt database naming stay aligned for Julia-style repos constraint(fmt-check): CI runs gofmt over the whole repo, so the existing spacing drift in internal/jira/tracker_test.go had to be cleaned up on this branch to get a green run learned(ci): the latest main CI was already red on the same formatting file and the same embedded shard, so rebasing surfaced pre-existing blockers rather than introducing a new shard-only regression
329 lines
8.5 KiB
Go
329 lines
8.5 KiB
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"strconv"
|
||
"strings"
|
||
"syscall"
|
||
|
||
"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/storage/doltutil"
|
||
)
|
||
|
||
// DriftItem represents a single drift check result.
|
||
type DriftItem struct {
|
||
Check string `json:"check"`
|
||
Status string `json:"status"` // "ok", "drift", "info", "skipped"
|
||
Message string `json:"message"`
|
||
Expected string `json:"expected,omitempty"`
|
||
Actual string `json:"actual,omitempty"`
|
||
}
|
||
|
||
const (
|
||
driftStatusOK = "ok"
|
||
driftStatusDrift = "drift"
|
||
driftStatusInfo = "info"
|
||
driftStatusSkipped = "skipped"
|
||
)
|
||
|
||
var configDriftCmd = &cobra.Command{
|
||
Use: "drift",
|
||
Short: "Detect config-vs-reality inconsistencies",
|
||
Long: `Detect drift between declared configuration and actual system state.
|
||
|
||
This is a read-only diagnostic that answers "is my environment consistent
|
||
with my config?" — no mutations are performed.
|
||
|
||
Checks:
|
||
- hooks Git hooks installed and up-to-date
|
||
- remote Dolt remote matches federation.remote config
|
||
- server Server state matches dolt.shared-server config
|
||
|
||
Exit codes:
|
||
0 No drift detected (all checks ok/info/skipped)
|
||
1 Drift detected (at least one check has status "drift")
|
||
|
||
Examples:
|
||
bd config drift
|
||
bd config drift --json`,
|
||
Run: func(_ *cobra.Command, _ []string) {
|
||
items := runDriftChecks()
|
||
|
||
if jsonOutput {
|
||
outputJSON(items)
|
||
} else {
|
||
printDriftItems(items)
|
||
}
|
||
|
||
// Exit 1 if any drift detected
|
||
for _, item := range items {
|
||
if item.Status == driftStatusDrift {
|
||
os.Exit(1)
|
||
}
|
||
}
|
||
},
|
||
}
|
||
|
||
func init() {
|
||
configCmd.AddCommand(configDriftCmd)
|
||
}
|
||
|
||
// runDriftChecks runs all drift checks and returns the results.
|
||
func runDriftChecks() []DriftItem {
|
||
var items []DriftItem
|
||
items = append(items, checkHooksDrift()...)
|
||
items = append(items, checkRemoteDrift()...)
|
||
items = append(items, checkServerDrift()...)
|
||
return items
|
||
}
|
||
|
||
// checkHooksDrift checks whether git hooks are installed and current.
|
||
func checkHooksDrift() []DriftItem {
|
||
// Verify we're in a git repo first
|
||
_, err := git.GetGitHooksDir()
|
||
if err != nil {
|
||
return []DriftItem{{
|
||
Check: "hooks",
|
||
Status: driftStatusSkipped,
|
||
Message: "Not a git repository",
|
||
}}
|
||
}
|
||
|
||
statuses := CheckGitHooks()
|
||
|
||
var items []DriftItem
|
||
var missing, outdated []string
|
||
|
||
for _, s := range statuses {
|
||
if !s.Installed {
|
||
missing = append(missing, s.Name)
|
||
} else if s.Outdated {
|
||
outdated = append(outdated, fmt.Sprintf("%s (have %s, want %s)", s.Name, s.Version, Version))
|
||
}
|
||
}
|
||
|
||
if len(missing) > 0 {
|
||
items = append(items, DriftItem{
|
||
Check: "hooks.missing",
|
||
Status: driftStatusDrift,
|
||
Message: fmt.Sprintf("Git hooks not installed: %s", strings.Join(missing, ", ")),
|
||
Expected: "installed",
|
||
Actual: "missing",
|
||
})
|
||
}
|
||
|
||
if len(outdated) > 0 {
|
||
items = append(items, DriftItem{
|
||
Check: "hooks.outdated",
|
||
Status: driftStatusDrift,
|
||
Message: fmt.Sprintf("Git hooks outdated: %s", strings.Join(outdated, ", ")),
|
||
Expected: "v" + Version,
|
||
Actual: "outdated",
|
||
})
|
||
}
|
||
|
||
if len(missing) == 0 && len(outdated) == 0 {
|
||
items = append(items, DriftItem{
|
||
Check: "hooks",
|
||
Status: driftStatusOK,
|
||
Message: "Git hooks installed and current",
|
||
})
|
||
}
|
||
|
||
return items
|
||
}
|
||
|
||
// checkRemoteDrift compares federation.remote config against actual Dolt remotes.
|
||
func checkRemoteDrift() []DriftItem {
|
||
federationRemote := config.GetString("federation.remote")
|
||
|
||
// Find the dolt data directory for CLI remote listing
|
||
beadsDir := beads.FindBeadsDir()
|
||
if beadsDir == "" {
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusSkipped,
|
||
Message: "No active beads workspace found",
|
||
}}
|
||
}
|
||
|
||
doltDir := doltserver.ResolveDoltDir(beadsDir)
|
||
|
||
cliRemotes, err := doltutil.ListCLIRemotes(doltDir)
|
||
if err != nil {
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusSkipped,
|
||
Message: fmt.Sprintf("Cannot list remotes: %v", err),
|
||
}}
|
||
}
|
||
|
||
// Find the "origin" remote (the conventional default)
|
||
var originURL string
|
||
for _, r := range cliRemotes {
|
||
if r.Name == "origin" {
|
||
originURL = r.URL
|
||
break
|
||
}
|
||
}
|
||
|
||
// Case 1: federation.remote set, no origin remote
|
||
if federationRemote != "" && originURL == "" {
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusDrift,
|
||
Message: "federation.remote is configured but no Dolt 'origin' remote exists",
|
||
Expected: federationRemote,
|
||
Actual: "(no origin remote)",
|
||
}}
|
||
}
|
||
|
||
// Case 2: federation.remote set, origin exists but doesn't match
|
||
if federationRemote != "" && originURL != "" && originURL != federationRemote {
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusDrift,
|
||
Message: "Dolt origin remote URL does not match federation.remote",
|
||
Expected: federationRemote,
|
||
Actual: originURL,
|
||
}}
|
||
}
|
||
|
||
// Case 3: federation.remote set and matches origin
|
||
if federationRemote != "" && originURL == federationRemote {
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusOK,
|
||
Message: "Dolt origin remote matches federation.remote",
|
||
}}
|
||
}
|
||
|
||
// Case 4: federation.remote not set but remotes exist
|
||
if federationRemote == "" && len(cliRemotes) > 0 {
|
||
names := make([]string, len(cliRemotes))
|
||
for i, r := range cliRemotes {
|
||
names[i] = r.Name
|
||
}
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusInfo,
|
||
Message: fmt.Sprintf("Dolt remotes configured (%s) but federation.remote is not set", strings.Join(names, ", ")),
|
||
}}
|
||
}
|
||
|
||
// Case 5: Neither configured
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusInfo,
|
||
Message: "No federation.remote configured and no Dolt remotes found",
|
||
}}
|
||
}
|
||
|
||
// checkServerDrift compares dolt.shared-server config against running server state.
|
||
// Uses non-mutating PID file check instead of doltserver.IsRunning() which can
|
||
// delete stale state files.
|
||
func checkServerDrift() []DriftItem {
|
||
beadsDir := beads.FindBeadsDir()
|
||
if beadsDir == "" {
|
||
return []DriftItem{{
|
||
Check: "server",
|
||
Status: driftStatusSkipped,
|
||
Message: "No active beads workspace found",
|
||
}}
|
||
}
|
||
|
||
sharedServerEnabled := config.GetString("dolt.shared-server")
|
||
wantServer := strings.EqualFold(sharedServerEnabled, "true")
|
||
|
||
serverRunning := isServerProbablyRunning(beadsDir)
|
||
|
||
if wantServer && !serverRunning {
|
||
return []DriftItem{{
|
||
Check: "server",
|
||
Status: driftStatusDrift,
|
||
Message: "dolt.shared-server is enabled but no server appears to be running",
|
||
Expected: "running",
|
||
Actual: "not running",
|
||
}}
|
||
}
|
||
|
||
if !wantServer && serverRunning {
|
||
return []DriftItem{{
|
||
Check: "server",
|
||
Status: driftStatusInfo,
|
||
Message: "Dolt server is running but dolt.shared-server is not enabled in config",
|
||
}}
|
||
}
|
||
|
||
if wantServer && serverRunning {
|
||
return []DriftItem{{
|
||
Check: "server",
|
||
Status: driftStatusOK,
|
||
Message: "Server running, consistent with dolt.shared-server config",
|
||
}}
|
||
}
|
||
|
||
// Neither configured nor running
|
||
return []DriftItem{{
|
||
Check: "server",
|
||
Status: driftStatusOK,
|
||
Message: "No shared server configured or running",
|
||
}}
|
||
}
|
||
|
||
// isServerProbablyRunning performs a non-mutating check for a running Dolt server.
|
||
// Reads the PID file and checks if the process exists, without deleting stale files.
|
||
func isServerProbablyRunning(beadsDir string) bool {
|
||
pidFile := filepath.Join(beadsDir, doltserver.PIDFileName)
|
||
data, err := os.ReadFile(pidFile) // #nosec G304 -- controlled path
|
||
if err != nil {
|
||
return false
|
||
}
|
||
|
||
pid, err := strconv.Atoi(strings.TrimSpace(string(data)))
|
||
if err != nil || pid <= 0 {
|
||
return false
|
||
}
|
||
|
||
// Check if the process exists (signal 0 = existence check)
|
||
proc, err := os.FindProcess(pid)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
// On Unix, FindProcess always succeeds; use Signal(0) to verify
|
||
err = proc.Signal(syscall.Signal(0))
|
||
return err == nil
|
||
}
|
||
|
||
// printDriftItems renders drift results in human-readable format.
|
||
func printDriftItems(items []DriftItem) {
|
||
if len(items) == 0 {
|
||
fmt.Println("No drift checks available")
|
||
return
|
||
}
|
||
|
||
statusIcon := map[string]string{
|
||
driftStatusOK: "✓",
|
||
driftStatusDrift: "✗",
|
||
driftStatusInfo: "ℹ",
|
||
driftStatusSkipped: "–",
|
||
}
|
||
|
||
for _, item := range items {
|
||
icon := statusIcon[item.Status]
|
||
if icon == "" {
|
||
icon = "?"
|
||
}
|
||
fmt.Fprintf(os.Stdout, " %s %s: %s\n", icon, item.Check, item.Message)
|
||
if item.Expected != "" || item.Actual != "" {
|
||
fmt.Fprintf(os.Stdout, " expected: %s\n", item.Expected)
|
||
fmt.Fprintf(os.Stdout, " actual: %s\n", item.Actual)
|
||
}
|
||
}
|
||
}
|