* fix(routing): auto-enable hydration and flush JSONL after routed create Fixes split-brain bug where issues routed to different repos (via routing.mode=auto) weren't visible in bd list because JSONL wasn't updated and hydration wasn't configured. **Problem**: When routing.mode=auto routes issues to a separate repo (e.g., ~/.beads-planning), those issues don't appear in 'bd list' because: 1. Target repo's JSONL isn't flushed after create 2. Multi-repo hydration (repos.additional) not configured automatically 3. No doctor warnings about the misconfiguration **Changes**: 1. **Auto-flush JSONL after routed create** (cmd/bd/create.go) - After routing issue to target repo, immediately flush to JSONL - Tries target daemon's export RPC first (if daemon running) - Falls back to direct JSONL export if no daemon - Ensures hydration can read the new issue immediately 2. **Enable hydration in bd init --contributor** (cmd/bd/init_contributor.go) - Wizard now automatically adds planning repo to repos.additional - Users no longer need to manually run 'bd repo add' - Routed issues appear in bd list immediately after setup 3. **Add doctor check for hydrated repo daemons** (cmd/bd/doctor/daemon.go) - New CheckHydratedRepoDaemons() warns if daemons not running - Without daemons, JSONL becomes stale and hydration breaks - Suggests: cd <repo> && bd daemon start --local 4. **Add doctor check for routing+hydration mismatch** (cmd/bd/doctor/config_values.go) - Validates routing targets are in repos.additional - Catches split-brain configuration before users encounter it - Suggests: bd repo add <routing-target> **Testing**: Builds successfully. Unit/integration tests pending. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * test(routing): add comprehensive tests for routing fixes Add unit tests for all 4 routing/hydration fixes: 1. **create_routing_flush_test.go** - Test JSONL flush after routing - TestFlushRoutedRepo_DirectExport: Verify direct JSONL export - TestPerformAtomicExport: Test atomic file operations - TestFlushRoutedRepo_PathExpansion: Test path handling - TestRoutingWithHydrationIntegration: E2E routing+hydration test 2. **daemon_test.go** - Test hydrated repo daemon check - TestCheckHydratedRepoDaemons: Test with/without daemons running - Covers no repos, daemons running, daemons missing scenarios 3. **config_values_test.go** - Test routing+hydration validation - Test routing without hydration (should warn) - Test routing with correct hydration (should pass) - Test routing target not in hydration list (should warn) - Test maintainer="." edge case (should pass) All tests follow existing patterns and use t.TempDir() for isolation. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix(tests): fix test failures and refine routing validation logic Fixes test failures and improves validation accuracy: 1. **Fix routing+hydration validation** (config_values.go) - Exclude "." from hasRoutingTargets check (current repo doesn't need hydration) - Prevents false warnings when maintainer="." or contributor="." 2. **Fix test ID generation** (create_routing_flush_test.go) - Use auto-generated IDs instead of hard-coded "beads-test1" - Respects test store prefix configuration (test-) - Fixed json.NewDecoder usage (file handle, not os.Open result) 3. **Fix config validation tests** (config_values_test.go) - Create actual directories for routing paths to pass path validation - Tests now verify both routing+hydration AND path existence checks 4. **Fix daemon test expectations** (daemon_test.go) - When database unavailable, check returns "No additional repos" not error - This is correct behavior (graceful degradation) All tests now pass: - TestFlushRoutedRepo* (3 tests) - TestPerformAtomicExport - TestCheckHydratedRepoDaemons (3 subtests) - TestCheckConfigValues routing tests (5 subtests) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * docs: clarify when git config beads.role maintainer is needed Clarify that maintainer role config is only needed in edge case: - Using GitHub HTTPS URL without credentials - But you have write access (are a maintainer) In most cases, beads auto-detects correctly via: - SSH URLs (git@github.com:owner/repo.git) - HTTPS with credentials This prevents confusion - users with SSH or credential-based HTTPS don't need to manually configure their role. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix(lint): address linter warnings in routing flush code - Add missing sqlite import in daemon.go - Fix unchecked client.Close() error return - Fix unchecked tempFile.Close() error returns - Mark unused parameters with _ prefix - Add nolint:gosec for safe tempPath construction Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Roland Tritsch <roland@ailtir.com> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
387 lines
12 KiB
Go
387 lines
12 KiB
Go
package doctor
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/steveyegge/beads/internal/daemon"
|
|
"github.com/steveyegge/beads/internal/git"
|
|
"github.com/steveyegge/beads/internal/rpc"
|
|
"github.com/steveyegge/beads/internal/storage/factory"
|
|
"github.com/steveyegge/beads/internal/storage/sqlite"
|
|
"github.com/steveyegge/beads/internal/syncbranch"
|
|
)
|
|
|
|
// CheckDaemonStatus checks the health of the daemon for a workspace.
|
|
// It checks for stale sockets, multiple daemons, and version mismatches.
|
|
func CheckDaemonStatus(path string, cliVersion string) DoctorCheck {
|
|
// Normalize path for reliable comparison (handles symlinks)
|
|
wsNorm, err := filepath.EvalSymlinks(path)
|
|
if err != nil {
|
|
// Fallback to absolute path if EvalSymlinks fails
|
|
wsNorm, _ = filepath.Abs(path)
|
|
}
|
|
|
|
// Use global daemon discovery (registry-based)
|
|
daemons, err := daemon.DiscoverDaemons(nil)
|
|
if err != nil {
|
|
return DoctorCheck{
|
|
Name: "Daemon Health",
|
|
Status: StatusWarning,
|
|
Message: "Unable to check daemon health",
|
|
Detail: err.Error(),
|
|
}
|
|
}
|
|
|
|
// Filter to this workspace using normalized paths
|
|
var workspaceDaemons []daemon.DaemonInfo
|
|
for _, d := range daemons {
|
|
dPath, err := filepath.EvalSymlinks(d.WorkspacePath)
|
|
if err != nil {
|
|
dPath, _ = filepath.Abs(d.WorkspacePath)
|
|
}
|
|
if dPath == wsNorm {
|
|
workspaceDaemons = append(workspaceDaemons, d)
|
|
}
|
|
}
|
|
|
|
// Check for stale socket directly (catches cases where RPC failed so WorkspacePath is empty)
|
|
// Follow redirect to resolve actual beads directory (bd-tvus fix)
|
|
beadsDir := resolveBeadsDir(filepath.Join(path, ".beads"))
|
|
socketPath := filepath.Join(beadsDir, "bd.sock")
|
|
if _, err := os.Stat(socketPath); err == nil {
|
|
// Socket exists - try to connect
|
|
if len(workspaceDaemons) == 0 {
|
|
// Socket exists but no daemon found in registry - likely stale
|
|
return DoctorCheck{
|
|
Name: "Daemon Health",
|
|
Status: StatusWarning,
|
|
Message: "Stale daemon socket detected",
|
|
Detail: fmt.Sprintf("Socket exists at %s but daemon is not responding", socketPath),
|
|
Fix: "Run 'bd daemons killall' to clean up stale sockets",
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(workspaceDaemons) == 0 {
|
|
return DoctorCheck{
|
|
Name: "Daemon Health",
|
|
Status: StatusOK,
|
|
Message: "No daemon running (will auto-start on next command)",
|
|
}
|
|
}
|
|
|
|
// Warn if multiple daemons for same workspace
|
|
if len(workspaceDaemons) > 1 {
|
|
return DoctorCheck{
|
|
Name: "Daemon Health",
|
|
Status: StatusWarning,
|
|
Message: fmt.Sprintf("Multiple daemons detected for this workspace (%d)", len(workspaceDaemons)),
|
|
Fix: "Run 'bd daemons killall' to clean up duplicate daemons",
|
|
}
|
|
}
|
|
|
|
// Check for stale or version mismatched daemons
|
|
for _, d := range workspaceDaemons {
|
|
if !d.Alive {
|
|
return DoctorCheck{
|
|
Name: "Daemon Health",
|
|
Status: StatusWarning,
|
|
Message: "Stale daemon detected",
|
|
Detail: fmt.Sprintf("PID %d is not alive", d.PID),
|
|
Fix: "Run 'bd daemons killall' to clean up stale daemons",
|
|
}
|
|
}
|
|
|
|
if d.Version != cliVersion {
|
|
return DoctorCheck{
|
|
Name: "Daemon Health",
|
|
Status: StatusWarning,
|
|
Message: fmt.Sprintf("Version mismatch (daemon: %s, CLI: %s)", d.Version, cliVersion),
|
|
Fix: "Run 'bd daemons killall' to restart daemons with current version",
|
|
}
|
|
}
|
|
}
|
|
|
|
return DoctorCheck{
|
|
Name: "Daemon Health",
|
|
Status: StatusOK,
|
|
Message: fmt.Sprintf("Daemon running (PID %d, version %s)", workspaceDaemons[0].PID, workspaceDaemons[0].Version),
|
|
}
|
|
}
|
|
|
|
// CheckVersionMismatch checks if the database version matches the CLI version.
|
|
// Returns a warning message if there's a mismatch, or empty string if versions match or can't be read.
|
|
func CheckVersionMismatch(db *sql.DB, cliVersion string) string {
|
|
var dbVersion string
|
|
err := db.QueryRow("SELECT value FROM metadata WHERE key = 'bd_version'").Scan(&dbVersion)
|
|
if err != nil {
|
|
return "" // Can't read version, skip
|
|
}
|
|
|
|
if dbVersion != "" && dbVersion != cliVersion {
|
|
return fmt.Sprintf("Version mismatch (CLI: %s, database: %s)", cliVersion, dbVersion)
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
// CheckGitSyncSetup checks if git repository and sync-branch are configured for daemon sync.
|
|
// This is informational - beads works fine without git sync, but users may want to enable it.
|
|
func CheckGitSyncSetup(path string) DoctorCheck {
|
|
// Check if we're in a git repository
|
|
_, err := git.GetGitDir()
|
|
if err != nil {
|
|
return DoctorCheck{
|
|
Name: "Git Sync Setup",
|
|
Status: StatusWarning,
|
|
Message: "No git repository (background sync unavailable)",
|
|
Detail: "The daemon requires a git repository for background sync. Without it, beads runs in direct mode.",
|
|
Fix: "Run 'git init' to enable background sync",
|
|
Category: CategoryRuntime,
|
|
}
|
|
}
|
|
|
|
// Git repo exists - check if sync-branch is configured
|
|
if !syncbranch.IsConfigured() {
|
|
return DoctorCheck{
|
|
Name: "Git Sync Setup",
|
|
Status: StatusOK,
|
|
Message: "Git repository detected (sync-branch not configured)",
|
|
Detail: "Beads commits directly to current branch. For team collaboration or to keep beads changes isolated, consider using a sync-branch.",
|
|
Fix: "Run 'bd config set sync.branch beads-sync' to use a dedicated branch for beads metadata",
|
|
Category: CategoryRuntime,
|
|
}
|
|
}
|
|
|
|
return DoctorCheck{
|
|
Name: "Git Sync Setup",
|
|
Status: StatusOK,
|
|
Message: "Git repository and sync-branch configured",
|
|
Category: CategoryRuntime,
|
|
}
|
|
}
|
|
|
|
// CheckDaemonAutoSync checks if daemon has auto-commit/auto-push enabled when
|
|
// sync-branch is configured. Missing auto-sync slows down agent workflows.
|
|
func CheckDaemonAutoSync(path string) DoctorCheck {
|
|
_, beadsDir := getBackendAndBeadsDir(path)
|
|
socketPath := filepath.Join(beadsDir, "bd.sock")
|
|
|
|
// Check if daemon is running
|
|
if _, err := os.Stat(socketPath); os.IsNotExist(err) {
|
|
return DoctorCheck{
|
|
Name: "Daemon Auto-Sync",
|
|
Status: StatusOK,
|
|
Message: "Daemon not running (will use defaults on next start)",
|
|
}
|
|
}
|
|
|
|
// Check if sync-branch is configured
|
|
ctx := context.Background()
|
|
store, err := factory.NewFromConfigWithOptions(ctx, beadsDir, factory.Options{ReadOnly: true})
|
|
if err != nil {
|
|
return DoctorCheck{
|
|
Name: "Daemon Auto-Sync",
|
|
Status: StatusOK,
|
|
Message: "Could not check config (database unavailable)",
|
|
}
|
|
}
|
|
defer func() { _ = store.Close() }()
|
|
|
|
syncBranch, _ := store.GetConfig(ctx, "sync.branch")
|
|
if syncBranch == "" {
|
|
return DoctorCheck{
|
|
Name: "Daemon Auto-Sync",
|
|
Status: StatusOK,
|
|
Message: "No sync-branch configured (auto-sync not applicable)",
|
|
}
|
|
}
|
|
|
|
// Sync-branch is configured - check daemon's auto-commit/auto-push status
|
|
client, err := rpc.TryConnect(socketPath)
|
|
if err != nil || client == nil {
|
|
return DoctorCheck{
|
|
Name: "Daemon Auto-Sync",
|
|
Status: StatusWarning,
|
|
Message: "Could not connect to daemon to check auto-sync status",
|
|
}
|
|
}
|
|
defer func() { _ = client.Close() }()
|
|
|
|
status, err := client.Status()
|
|
if err != nil {
|
|
return DoctorCheck{
|
|
Name: "Daemon Auto-Sync",
|
|
Status: StatusWarning,
|
|
Message: "Could not get daemon status",
|
|
Detail: err.Error(),
|
|
}
|
|
}
|
|
|
|
if !status.AutoCommit || !status.AutoPush {
|
|
var missing []string
|
|
if !status.AutoCommit {
|
|
missing = append(missing, "auto-commit")
|
|
}
|
|
if !status.AutoPush {
|
|
missing = append(missing, "auto-push")
|
|
}
|
|
return DoctorCheck{
|
|
Name: "Daemon Auto-Sync",
|
|
Status: StatusWarning,
|
|
Message: fmt.Sprintf("Daemon running without %v (slows agent workflows)", missing),
|
|
Detail: "With sync-branch configured, auto-commit and auto-push should be enabled",
|
|
Fix: "Restart daemon: bd daemon stop && bd daemon start",
|
|
}
|
|
}
|
|
|
|
return DoctorCheck{
|
|
Name: "Daemon Auto-Sync",
|
|
Status: StatusOK,
|
|
Message: "Auto-commit and auto-push enabled",
|
|
}
|
|
}
|
|
|
|
// CheckLegacyDaemonConfig checks for deprecated daemon config options and
|
|
// encourages migration to the unified daemon.auto-sync setting.
|
|
func CheckLegacyDaemonConfig(path string) DoctorCheck {
|
|
_, beadsDir := getBackendAndBeadsDir(path)
|
|
|
|
ctx := context.Background()
|
|
store, err := factory.NewFromConfigWithOptions(ctx, beadsDir, factory.Options{ReadOnly: true})
|
|
if err != nil {
|
|
return DoctorCheck{
|
|
Name: "Daemon Config",
|
|
Status: StatusOK,
|
|
Message: "Could not check config (database unavailable)",
|
|
}
|
|
}
|
|
defer func() { _ = store.Close() }()
|
|
|
|
// Check for deprecated individual settings
|
|
var legacySettings []string
|
|
|
|
if val, _ := store.GetConfig(ctx, "daemon.auto_commit"); val != "" {
|
|
legacySettings = append(legacySettings, "daemon.auto_commit")
|
|
}
|
|
if val, _ := store.GetConfig(ctx, "daemon.auto_push"); val != "" {
|
|
legacySettings = append(legacySettings, "daemon.auto_push")
|
|
}
|
|
|
|
if len(legacySettings) > 0 {
|
|
return DoctorCheck{
|
|
Name: "Daemon Config",
|
|
Status: StatusWarning,
|
|
Message: fmt.Sprintf("Deprecated config options found: %v", legacySettings),
|
|
Detail: "These options still work but are deprecated. Use daemon.auto-sync for read/write mode or daemon.auto-pull for read-only mode.",
|
|
Fix: "Run: bd config delete daemon.auto_commit && bd config delete daemon.auto_push && bd config set daemon.auto-sync true",
|
|
}
|
|
}
|
|
|
|
return DoctorCheck{
|
|
Name: "Daemon Config",
|
|
Status: StatusOK,
|
|
Message: "Using current config format",
|
|
}
|
|
}
|
|
|
|
// CheckHydratedRepoDaemons checks if daemons are running for all repos
|
|
// configured in repos.additional. Without running daemons, JSONL files won't
|
|
// be kept updated, causing multi-repo hydration to become stale (bd-fix-routing).
|
|
func CheckHydratedRepoDaemons(path string) DoctorCheck {
|
|
beadsDir := filepath.Join(path, ".beads")
|
|
dbPath := filepath.Join(beadsDir, "beads.db")
|
|
|
|
ctx := context.Background()
|
|
store, err := sqlite.New(ctx, dbPath)
|
|
if err != nil {
|
|
return DoctorCheck{
|
|
Name: "Hydrated Repo Daemons",
|
|
Status: StatusOK,
|
|
Message: "Could not check config (database unavailable)",
|
|
}
|
|
}
|
|
defer func() { _ = store.Close() }()
|
|
|
|
// Get repos.additional from config
|
|
additionalReposStr, _ := store.GetConfig(ctx, "repos.additional")
|
|
if additionalReposStr == "" {
|
|
return DoctorCheck{
|
|
Name: "Hydrated Repo Daemons",
|
|
Status: StatusOK,
|
|
Message: "No additional repos configured (N/A)",
|
|
}
|
|
}
|
|
|
|
// Parse additional repos (stored as JSON array string)
|
|
var additionalRepos []string
|
|
if err := unmarshalConfigValue(additionalReposStr, &additionalRepos); err != nil {
|
|
return DoctorCheck{
|
|
Name: "Hydrated Repo Daemons",
|
|
Status: StatusWarning,
|
|
Message: "Could not parse repos.additional config",
|
|
Detail: err.Error(),
|
|
}
|
|
}
|
|
|
|
if len(additionalRepos) == 0 {
|
|
return DoctorCheck{
|
|
Name: "Hydrated Repo Daemons",
|
|
Status: StatusOK,
|
|
Message: "No additional repos configured (N/A)",
|
|
}
|
|
}
|
|
|
|
// Check each additional repo for running daemon
|
|
var missingDaemons []string
|
|
for _, repoPath := range additionalRepos {
|
|
// Expand ~ to home directory
|
|
expandedPath := expandPath(repoPath)
|
|
|
|
// Construct socket path
|
|
socketPath := filepath.Join(expandedPath, ".beads", "bd.sock")
|
|
|
|
// Try to connect to daemon
|
|
client, err := rpc.TryConnect(socketPath)
|
|
if err == nil && client != nil {
|
|
_ = client.Close()
|
|
// Daemon is running, all good
|
|
} else {
|
|
// No daemon running
|
|
missingDaemons = append(missingDaemons, repoPath)
|
|
}
|
|
}
|
|
|
|
if len(missingDaemons) > 0 {
|
|
return DoctorCheck{
|
|
Name: "Hydrated Repo Daemons",
|
|
Status: StatusWarning,
|
|
Message: fmt.Sprintf("Daemons not running in %d hydrated repo(s)", len(missingDaemons)),
|
|
Detail: fmt.Sprintf("Missing daemons in: %v", missingDaemons),
|
|
Fix: "For each repo, run: cd <repo> && bd daemon start --local",
|
|
}
|
|
}
|
|
|
|
return DoctorCheck{
|
|
Name: "Hydrated Repo Daemons",
|
|
Status: StatusOK,
|
|
Message: fmt.Sprintf("All %d hydrated repo(s) have running daemons", len(additionalRepos)),
|
|
}
|
|
}
|
|
|
|
// unmarshalConfigValue unmarshals a JSON config value
|
|
func unmarshalConfigValue(value string, target interface{}) error {
|
|
// Config values are stored as JSON
|
|
if value == "" {
|
|
return nil
|
|
}
|
|
|
|
// Unmarshal JSON into target
|
|
return json.Unmarshal([]byte(value), target)
|
|
}
|