Files
beads/cmd/bd/doctor/daemon.go
Ryan ffe0dca2a3 feat(daemon): unify auto-sync config for simpler agent workflows (#904)
* feat(daemon): unify auto-sync config for simpler agent workflows

## Problem

Agents running `bd sync` at session end caused delays in the Claude Code
"event loop", slowing development. The daemon was already auto-exporting
DB→JSONL instantly, but auto-commit and auto-push weren't enabled by
default when sync-branch was configured - requiring manual `bd sync`.

Additionally, having three separate config options (auto-commit, auto-push,
auto-pull) was confusing and could get out of sync.

## Solution

Simplify to two intuitive sync modes:

1. **Read/Write Mode** (`daemon.auto-sync: true` or `BEADS_AUTO_SYNC=true`)
   - Enables auto-commit + auto-push + auto-pull
   - Full bidirectional sync - eliminates need for manual `bd sync`
   - Default when sync-branch is configured

2. **Read-Only Mode** (`daemon.auto-pull: true` or `BEADS_AUTO_PULL=true`)
   - Only receives updates from team
   - Does NOT auto-publish changes
   - Useful for experimental work or manual review before sharing

## Benefits

- **Faster agent workflows**: No more `bd sync` delays at session end
- **Simpler config**: Two modes instead of three separate toggles
- **Backward compatible**: Legacy auto_commit/auto_push settings still work
  (treated as auto-sync=true)
- **Adaptive `bd prime`**: Session close protocol adapts when daemon is
  auto-syncing (shows simplified 4-step git workflow, no `bd sync`)
- **Doctor warnings**: `bd doctor` warns about deprecated legacy config

## Changes

- cmd/bd/daemon.go: Add loadDaemonAutoSettings() with unified config logic
- cmd/bd/doctor.go: Add CheckLegacyDaemonConfig call
- cmd/bd/doctor/daemon.go: Add CheckDaemonAutoSync, CheckLegacyDaemonConfig
- cmd/bd/init_team.go: Use daemon.auto-sync in team wizard
- cmd/bd/prime.go: Detect daemon auto-sync, adapt session close protocol
- cmd/bd/prime_test.go: Add stubIsDaemonAutoSyncing for testing

* docs: add comprehensive daemon technical analysis

Add daemon-summary.md documenting the beads daemon architecture,
memory analysis (explaining the 30-35MB footprint), platform support
comparison, historical problems and fixes, and architectural guidance
for other projects implementing similar daemon patterns.

Key sections:
- Architecture deep dive with component diagrams
- Memory breakdown (SQLite WASM runtime is the main contributor)
- Platform support matrix (macOS/Linux full, Windows partial)
- Historical bugs and their fixes with reusable patterns
- Analysis of daemon usefulness without database (verdict: low value)
- Expert-reviewed improvement proposals (3 recommended, 3 skipped)
- Technical design patterns for other implementations

* feat: add cross-platform CI matrix and dual-mode test framework

Cross-Platform CI:
- Add Windows, macOS, Linux matrix to catch platform-specific bugs
- Linux: full tests with race detector and coverage
- macOS: full tests with race detector
- Windows: full tests without race detector (performance)
- Catches bugs like GH#880 (macOS path casing) and GH#387 (Windows daemon)

Dual-Mode Test Framework (cmd/bd/dual_mode_test.go):
- Runs tests in both direct mode and daemon mode
- Prevents recurring bug pattern (GH#719, GH#751, bd-fu83)
- Provides DualModeTestEnv with helper methods for common operations
- Includes 5 example tests demonstrating the pattern

Documentation:
- Add dual-mode testing section to CONTRIBUTING.md
- Document RunDualModeTest API and available helpers

Test Fixes:
- Fix sync_local_only_test.go gitPull/gitPush calls
- Add gate_no_daemon_test.go for beads-70c4 investigation

* fix(test): isolate TestFindBeadsDir tests with BEADS_DIR env var

The tests were finding the real project's .beads directory instead of
the temp directory because FindBeadsDir() walks up the directory tree.
Using BEADS_DIR env var provides proper test isolation.

* fix(test): stop daemon before running test suite guard

The test suite guard checks that tests don't modify the real repo's .beads
directory. However, a background daemon running auto-sync would touch
issues.jsonl during test execution, causing false positives.

Changes:
- Set BEADS_NO_DAEMON=1 to prevent daemon auto-start from tests
- Stop any running daemon for the repo before taking the "before" snapshot
- Uses exec to call `bd daemon --stop` to avoid import cycle issues

* chore: revert .beads/issues.jsonl to upstream/main

Per CONTRIBUTING.md, .beads/issues.jsonl should not be modified in PRs.
2026-01-06 12:52:19 -08:00

292 lines
9.0 KiB
Go

package doctor
import (
"context"
"database/sql"
"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/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 := filepath.Join(path, ".beads")
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()
dbPath := filepath.Join(beadsDir, "beads.db")
store, err := sqlite.New(ctx, dbPath)
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 := 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: "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",
}
}