* fix(windows): report Dolt server run-state correctly isServerProbablyRunning() checked the server PID with proc.Signal(syscall.Signal(0)), the Unix null-signal probe. Windows has no signals (os.Process.Signal rejects everything but Kill), so the probe always errored and every live PID looked dead -- bd doctor / drift always reported the shared Dolt server as not running on Windows. An existence-only replacement is wrong too: on Windows a terminated process's kernel object stays openable while any handle to it lingers (an un-Wait()ed parent, AV/EDR, a debugger, Task Manager), so it reports a dead server as alive for a window after exit. Split the probe into a build-tagged processAlive(pid), matching internal/doltserver and internal/linear: Unix keeps Signal(0); Windows uses OpenProcess(SYNCHRONIZE|PROCESS_QUERY_LIMITED_INFORMATION) + WaitForSingleObject(h,0) + CloseHandle -- a terminated process's handle is signaled (WAIT_OBJECT_0), a running one times out (WAIT_TIMEOUT). Unix behavior is unchanged. Adds a Windows regression test that reproduces the lingering-handle false positive deterministically (fails on the naive OpenProcess-success probe, passes on this fix). Agent-Signature: claude-code-opus-4.8-high on behalf of Marco Del Pin * fix(windows): address #4808 review: WAIT_OBJECT_0 precondition, Windows PR-time test lane, naming - Require WAIT_OBJECT_0 from WaitForSingleObject in the lingering-handle test precondition (the pinned x/sys returns nil error on WAIT_TIMEOUT; only WAIT_FAILED errors), matching the pattern of the newer Windows tests in internal/doltserver and internal/linear. - Add a targeted windows-latest PR-time job (test-windows-liveness) running the ^TestIsServerProbablyRunning tests, wired into the CI gate: the //go:build windows regression test is compiled out on the Ubuntu jobs, and main.yml's Windows job is push-to-main build/smoke only, so without this lane the regression guard never executes at PR time. - Rename h/s to processHandle/waitResult in processAlive. Scope note (review should-fix 1): this PR fixes the bd config drift probe only, processAlive's single production caller. bd doctor's server run-state goes through doltserver.IsRunning, fixed separately upstream by594c464cc. * fix(windows): rename processAlive to pidAlive to avoid merge-ref collision with #5004 test helper Upstreambb9bb7487(#5004, merged 2026-07-24) added a test-only processAlive helper in cmd/bd/proxied_local_helpers_test.go (//go:build cgo && unix). On the PR merge ref both declarations coexist in package main and the cgo unix lanes fail to compile (processAlive redeclared - Build (Embedded Dolt) + PR Core). Renaming the production helper introduced by this PR is the minimal fix; the #5004 test helper is untouched. * fix(windows): check CloseHandle in pidAlive to satisfy errcheck The PR gate went red after the maintainer merge of current main wired the windows lint lane into ci-gate: cmd/bd/config_drift_windows.go:26:27: Error return value of windows.CloseHandle is not checked (errcheck) failing both Build Artifacts and PR Lint (wrapper timing) on run 30422840210. Adopt the pattern every other windows file in the tree already uses for a deferred handle close - doltserver_windows.go:110, synclock_windows.go:14, procid_windows.go:35/47, process_executable_windows.go:21: defer func() { _ = windows.CloseHandle(processHandle) }() This file was the only defer windows.CloseHandle(...) without the wrapper. Verified with the linter itself rather than by inspection: errcheck v1.20.0, GOOS=windows, -tags gms_pure_go. fixed tree -> 0 hits on config_drift_windows.go:26 line reverted -> 1 hit on config_drift_windows.go:26 so the check demonstrably goes red on the old line and green on the new one. Also GOOS=windows go vet ./cmd/bd clean, gofmt clean. The three remaining bare CloseHandle calls in config_drift_windows_test.go are deliberately untouched: .golangci.yml sets tests: false, which is why CI named only line 26. --------- Co-authored-by: Test <test@test.com> Co-authored-by: matt wilkie <maphew@gmail.com> Co-authored-by: Marco Del Pin <marco@omniaevo.it>
359 lines
9.1 KiB
Go
359 lines
9.1 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"strconv"
|
||
"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/dolt"
|
||
)
|
||
|
||
// 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`,
|
||
SilenceUsage: true,
|
||
SilenceErrors: true,
|
||
RunE: func(_ *cobra.Command, _ []string) error {
|
||
evt := metrics.NewCommandEvent("config-drift")
|
||
defer func() {
|
||
if c := metrics.Global(); c != nil {
|
||
c.CloseEventAndAdd(evt)
|
||
}
|
||
}()
|
||
|
||
items := runDriftChecks()
|
||
|
||
if jsonOutput {
|
||
if err := outputJSON(items); err != nil {
|
||
return err
|
||
}
|
||
} else {
|
||
printDriftItems(items)
|
||
}
|
||
|
||
for _, item := range items {
|
||
if item.Status == driftStatusDrift {
|
||
return SilentExit()
|
||
}
|
||
}
|
||
return nil
|
||
},
|
||
}
|
||
|
||
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",
|
||
}}
|
||
}
|
||
|
||
ctx := context.Background()
|
||
st, err := dolt.NewFromConfigWithOptions(ctx, beadsDir, &dolt.Config{
|
||
ReadOnly: true,
|
||
DisableAutoStart: true,
|
||
})
|
||
if err != nil {
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusSkipped,
|
||
Message: fmt.Sprintf("Cannot open Dolt store: %v", err),
|
||
}}
|
||
}
|
||
defer func() { _ = st.Close() }()
|
||
|
||
remotes, err := st.ListRemotes(ctx)
|
||
if err != nil {
|
||
return []DriftItem{{
|
||
Check: "remote",
|
||
Status: driftStatusSkipped,
|
||
Message: fmt.Sprintf("Cannot list remotes: %v", err),
|
||
}}
|
||
}
|
||
|
||
var originURL string
|
||
for _, r := range remotes {
|
||
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 != "" && !remoteURLMatchesConfig(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 != "" && remoteURLMatchesConfig(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(remotes) > 0 {
|
||
names := make([]string, len(remotes))
|
||
for i, r := range remotes {
|
||
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",
|
||
}}
|
||
}
|
||
|
||
wantServer := doltserver.IsSharedServerMode()
|
||
|
||
serverDir := beadsDir
|
||
if wantServer {
|
||
var err error
|
||
serverDir, err = doltserver.SharedServerPath()
|
||
if err != nil {
|
||
return []DriftItem{{
|
||
Check: "server",
|
||
Status: driftStatusDrift,
|
||
Message: fmt.Sprintf("dolt.shared-server is enabled but its state directory cannot be resolved: %v", err),
|
||
Expected: "resolvable shared server state directory",
|
||
Actual: "unavailable",
|
||
}}
|
||
}
|
||
}
|
||
|
||
serverRunning := isServerProbablyRunning(serverDir)
|
||
|
||
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
|
||
}
|
||
|
||
return pidAlive(pid)
|
||
}
|
||
|
||
// 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)
|
||
}
|
||
}
|
||
}
|