This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/aitimemachine/beads/cmd/bd/doctor_health.go
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

243 lines
6.7 KiB
Go

package main
import (
"database/sql"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/steveyegge/beads/cmd/bd/doctor"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/doltserver"
"github.com/steveyegge/beads/internal/storage/doltutil"
"github.com/steveyegge/beads/internal/ui"
)
// runCheckHealth runs lightweight health checks for git hooks.
// Silent on success, prints a hint if issues detected.
// Respects hints.doctor config setting.
func runCheckHealth(path string) {
beadsDir := doctor.ResolveBeadsDirForRepo(path)
// Check if .beads/ exists
if _, err := os.Stat(beadsDir); os.IsNotExist(err) {
// No .beads directory - nothing to check
return
}
// Load config for Dolt server connection info
cfg, err := configfile.Load(beadsDir)
if err != nil || cfg == nil {
// Can't load config - only check hooks
if issue := doctor.CheckHooksQuick(Version); issue != "" {
printCheckHealthHint([]string{issue})
}
return
}
// Try connecting to Dolt server for config/version checks.
// Use doltserver.DefaultConfig for port resolution — GetDoltServerPort
// falls back to 3307 which is wrong for ephemeral ports.
host := cfg.GetDoltServerHost()
port := doltserver.DefaultConfig(beadsDir).Port
if port == 0 {
// No server running yet — skip DB checks, only check hooks.
if issue := doctor.CheckHooksQuick(Version); issue != "" {
printCheckHealthHint([]string{issue})
}
return
}
database := cfg.GetDoltDatabase()
var issues []string
dsn := doltutil.ServerDSN{
Host: host,
Port: port,
User: cfg.GetDoltServerUser(),
Password: cfg.GetDoltServerPasswordForPort(port),
Database: database,
Timeout: 2 * time.Second,
TLS: cfg.GetDoltServerTLS(),
}.String()
db, err := sql.Open("mysql", dsn)
if err == nil {
defer db.Close()
// Quick ping to verify server is reachable
if pingErr := db.Ping(); pingErr == nil {
// Check if hints.doctor is disabled in config
if hintsDisabledDB(db) {
return
}
// Check version mismatch
if issue := checkVersionMismatchDB(db); issue != "" {
issues = append(issues, issue)
}
}
// If ping fails, server not running — skip DB checks silently
}
// Check outdated git hooks
if issue := doctor.CheckHooksQuick(Version); issue != "" {
issues = append(issues, issue)
}
// If any issues found, print hint
if len(issues) > 0 {
printCheckHealthHint(issues)
}
// Silent exit on success
}
// runDeepValidation runs full graph integrity validation
func runDeepValidation(path string) {
// Show warning about potential slowness
fmt.Println("Running deep validation (may be slow on large databases)...")
fmt.Println()
result := doctor.RunDeepValidation(path)
if jsonOutput {
jsonBytes, err := doctor.DeepValidationResultJSON(result)
if err != nil {
FatalError("%v", err)
}
fmt.Println(string(jsonBytes))
} else {
doctor.PrintDeepValidationResult(result)
}
if !result.OverallOK {
os.Exit(1)
}
}
// runServerHealth runs Dolt server mode health checks
func runServerHealth(path string) {
result := doctor.RunServerHealthChecks(path)
if jsonOutput {
jsonBytes, err := json.Marshal(result)
if err != nil {
FatalError("failed to marshal health check result: %v", err)
}
fmt.Println(string(jsonBytes))
} else {
fmt.Println("Dolt Server Mode Health Check")
fmt.Println()
printServerHealthResult(result)
}
if !result.OverallOK {
os.Exit(1)
}
}
// printServerHealthResult prints the server health check results
func printServerHealthResult(result doctor.ServerHealthResult) {
var passCount, warnCount, failCount int
var warnings []doctor.DoctorCheck
for _, check := range result.Checks {
var statusIcon string
switch check.Status {
case statusOK:
statusIcon = ui.RenderPassIcon()
passCount++
case statusWarning:
statusIcon = ui.RenderWarnIcon()
warnCount++
warnings = append(warnings, check)
case statusError:
statusIcon = ui.RenderFailIcon()
failCount++
warnings = append(warnings, check)
}
fmt.Printf(" %s %s", statusIcon, check.Name)
if check.Message != "" {
fmt.Printf("%s", ui.RenderMuted(" "+check.Message))
}
fmt.Println()
if check.Detail != "" {
// Indent detail lines
for _, line := range strings.Split(check.Detail, "\n") {
fmt.Printf(" %s%s\n", ui.MutedStyle.Render(ui.TreeLast), ui.RenderMuted(line))
}
}
}
fmt.Println()
// Summary line
fmt.Println(ui.RenderSeparator())
summary := fmt.Sprintf("%s %d passed %s %d warnings %s %d failed",
ui.RenderPassIcon(), passCount,
ui.RenderWarnIcon(), warnCount,
ui.RenderFailIcon(), failCount,
)
fmt.Println(summary)
// Print fixes for any errors/warnings
if len(warnings) > 0 {
fmt.Println()
fmt.Println(ui.RenderWarn(ui.IconWarn + " FIXES NEEDED"))
for i, check := range warnings {
if check.Fix == "" {
continue
}
line := fmt.Sprintf("%s: %s", check.Name, check.Message)
if check.Status == statusError {
fmt.Printf(" %s %s %s\n", ui.RenderFailIcon(), ui.RenderFail(fmt.Sprintf("%d.", i+1)), ui.RenderFail(line))
} else {
fmt.Printf(" %s %s %s\n", ui.RenderWarnIcon(), ui.RenderWarn(fmt.Sprintf("%d.", i+1)), line)
}
fmt.Printf(" %s%s\n", ui.MutedStyle.Render(ui.TreeLast), check.Fix)
}
} else if result.OverallOK {
fmt.Println()
fmt.Printf("%s\n", ui.RenderPass("✓ All server health checks passed"))
}
}
// printCheckHealthHint prints the health check hint and exits with error.
func printCheckHealthHint(issues []string) {
fmt.Fprintf(os.Stderr, "💡 bd doctor recommends a health check:\n")
for _, issue := range issues {
fmt.Fprintf(os.Stderr, " • %s\n", issue)
}
fmt.Fprintf(os.Stderr, " Run 'bd doctor' for details, or 'bd doctor --fix' to auto-repair\n")
fmt.Fprintf(os.Stderr, " (Suppress with: bd config set %s false)\n", ConfigKeyHintsDoctor)
os.Exit(1)
}
// hintsDisabledDB checks if hints.doctor is set to "false" using an existing DB connection.
// Used by runCheckHealth to avoid multiple DB opens.
func hintsDisabledDB(db *sql.DB) bool {
var value string
err := db.QueryRow("SELECT value FROM config WHERE `key` = ?", ConfigKeyHintsDoctor).Scan(&value)
if err != nil {
return false // Key not set, assume hints enabled
}
return strings.ToLower(value) == "false"
}
// checkVersionMismatchDB checks if CLI version differs from database bd_version.
// Uses an existing DB connection.
func checkVersionMismatchDB(db *sql.DB) string {
var dbVersion string
err := db.QueryRow("SELECT value FROM local_metadata WHERE `key` = 'bd_version'").Scan(&dbVersion)
if err != nil {
return "" // Can't read version, skip
}
if dbVersion != "" && dbVersion != Version {
return fmt.Sprintf("Version mismatch (CLI: %s, database: %s)", Version, dbVersion)
}
return ""
}