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.
This commit is contained in:
Ryan
2026-01-06 12:52:19 -08:00
committed by GitHub
parent 7b0f398f11
commit ffe0dca2a3
15 changed files with 2247 additions and 137 deletions

View File

@@ -14,11 +14,9 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/beads/cmd/bd/doctor"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/daemon"
"github.com/steveyegge/beads/internal/rpc"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/syncbranch"
)
var daemonCmd = &cobra.Command{
@@ -71,68 +69,8 @@ Run 'bd daemon' with no flags to see available options.`,
// GH#871: Read from config.yaml first (team-shared), then fall back to SQLite (legacy)
// (skip if --stop, --status, --health, --metrics)
if start && !stop && !status && !health && !metrics {
if !cmd.Flags().Changed("auto-commit") {
// Check config.yaml first (GH#871: team-wide settings)
if config.GetBool("daemon.auto_commit") {
autoCommit = true
} else if dbPath := beads.FindDatabasePath(); dbPath != "" {
// Fall back to SQLite for backwards compatibility
ctx := context.Background()
store, err := sqlite.New(ctx, dbPath)
if err == nil {
if configVal, err := store.GetConfig(ctx, "daemon.auto_commit"); err == nil && configVal == "true" {
autoCommit = true
}
_ = store.Close()
}
}
}
if !cmd.Flags().Changed("auto-push") {
// Check config.yaml first (GH#871: team-wide settings)
if config.GetBool("daemon.auto_push") {
autoPush = true
} else if dbPath := beads.FindDatabasePath(); dbPath != "" {
// Fall back to SQLite for backwards compatibility
ctx := context.Background()
store, err := sqlite.New(ctx, dbPath)
if err == nil {
if configVal, err := store.GetConfig(ctx, "daemon.auto_push"); err == nil && configVal == "true" {
autoPush = true
}
_ = store.Close()
}
}
}
if !cmd.Flags().Changed("auto-pull") {
// Check environment variable first
if envVal := os.Getenv("BEADS_AUTO_PULL"); envVal != "" {
autoPull = envVal == "true" || envVal == "1"
} else if config.GetBool("daemon.auto_pull") {
// Check config.yaml (GH#871: team-wide settings)
autoPull = true
} else if dbPath := beads.FindDatabasePath(); dbPath != "" {
// Fall back to SQLite for backwards compatibility
ctx := context.Background()
store, err := sqlite.New(ctx, dbPath)
if err == nil {
if configVal, err := store.GetConfig(ctx, "daemon.auto_pull"); err == nil {
if configVal == "true" {
autoPull = true
} else if configVal == "false" {
autoPull = false
}
} else {
// Default: auto_pull is true when sync-branch is configured
// Use syncbranch.IsConfigured() which checks env var and config.yaml
// (the common case), not just SQLite (legacy)
if syncbranch.IsConfigured() {
autoPull = true
}
}
_ = store.Close()
}
}
}
// Load auto-commit/push/pull defaults from env vars, config, or sync-branch
autoCommit, autoPush, autoPull = loadDaemonAutoSettings(cmd, autoCommit, autoPush, autoPull)
}
if interval <= 0 {
@@ -602,3 +540,144 @@ func runDaemonLoop(interval time.Duration, autoCommit, autoPush, autoPull, local
runEventLoop(ctx, cancel, ticker, doSync, server, serverErrChan, parentPID, log)
}
}
// loadDaemonAutoSettings loads daemon sync mode settings.
//
// # Two Sync Modes
//
// Read/Write Mode (full sync):
//
// daemon.auto-sync: true (or BEADS_AUTO_SYNC=true)
//
// Enables auto-commit, auto-push, AND auto-pull. Full bidirectional sync
// with team. Eliminates need for manual `bd sync`. This is the default
// when sync-branch is configured.
//
// Read-Only Mode:
//
// daemon.auto-pull: true (or BEADS_AUTO_PULL=true)
//
// Only enables auto-pull (receive updates from team). Does NOT auto-publish
// your changes. Useful for experimental work or manual review before sharing.
//
// # Precedence
//
// 1. auto-sync=true → Read/Write mode (all three ON, no exceptions)
// 2. auto-sync=false → Write-side OFF, auto-pull can still be enabled
// 3. auto-sync not set → Legacy compat mode:
// - If either BEADS_AUTO_COMMIT/daemon.auto_commit or BEADS_AUTO_PUSH/daemon.auto_push
// is enabled, treat as auto-sync=true (full read/write)
// - Otherwise check auto-pull for read-only mode
// 4. Fallback: all default to true when sync-branch configured
//
// Note: The individual auto-commit/auto-push settings are deprecated.
// Use auto-sync for read/write mode, auto-pull for read-only mode.
func loadDaemonAutoSettings(cmd *cobra.Command, autoCommit, autoPush, autoPull bool) (bool, bool, bool) {
dbPath := beads.FindDatabasePath()
if dbPath == "" {
return autoCommit, autoPush, autoPull
}
ctx := context.Background()
store, err := sqlite.New(ctx, dbPath)
if err != nil {
return autoCommit, autoPush, autoPull
}
defer func() { _ = store.Close() }()
// Check if sync-branch is configured (used for defaults)
syncBranch, _ := store.GetConfig(ctx, "sync.branch")
hasSyncBranch := syncBranch != ""
// Check unified auto-sync setting first (controls auto-commit + auto-push)
unifiedAutoSync := ""
if envVal := os.Getenv("BEADS_AUTO_SYNC"); envVal != "" {
unifiedAutoSync = envVal
} else if configVal, _ := store.GetConfig(ctx, "daemon.auto-sync"); configVal != "" {
unifiedAutoSync = configVal
}
// Handle unified auto-sync setting
if unifiedAutoSync != "" {
enabled := unifiedAutoSync == "true" || unifiedAutoSync == "1"
if enabled {
// auto-sync=true: MASTER CONTROL, forces all three ON
// Individual CLI flags are ignored - you said "full sync"
autoCommit = true
autoPush = true
autoPull = true
return autoCommit, autoPush, autoPull
}
// auto-sync=false: Write-side (commit/push) locked OFF
// Only auto-pull can be individually enabled (for read-only mode)
autoCommit = false
autoPush = false
// Auto-pull can still be enabled via CLI flag or individual config
if cmd.Flags().Changed("auto-pull") {
// Use the CLI flag value (already in autoPull)
} else if envVal := os.Getenv("BEADS_AUTO_PULL"); envVal != "" {
autoPull = envVal == "true" || envVal == "1"
} else if configVal, _ := store.GetConfig(ctx, "daemon.auto-pull"); configVal != "" {
autoPull = configVal == "true"
} else if configVal, _ := store.GetConfig(ctx, "daemon.auto_pull"); configVal != "" {
autoPull = configVal == "true"
} else if hasSyncBranch {
// Default auto-pull to true when sync-branch configured
autoPull = true
} else {
autoPull = false
}
return autoCommit, autoPush, autoPull
}
// No unified setting - check legacy individual settings for backward compat
// If either legacy auto-commit or auto-push is enabled, treat as auto-sync=true
legacyCommit := false
legacyPush := false
// Check legacy auto-commit (env var or config)
if envVal := os.Getenv("BEADS_AUTO_COMMIT"); envVal != "" {
legacyCommit = envVal == "true" || envVal == "1"
} else if configVal, _ := store.GetConfig(ctx, "daemon.auto_commit"); configVal != "" {
legacyCommit = configVal == "true"
}
// Check legacy auto-push (env var or config)
if envVal := os.Getenv("BEADS_AUTO_PUSH"); envVal != "" {
legacyPush = envVal == "true" || envVal == "1"
} else if configVal, _ := store.GetConfig(ctx, "daemon.auto_push"); configVal != "" {
legacyPush = configVal == "true"
}
// If either legacy write-side option is enabled, enable full auto-sync
// (backward compat: user wanted writes, so give them full sync)
if legacyCommit || legacyPush {
autoCommit = true
autoPush = true
autoPull = true
return autoCommit, autoPush, autoPull
}
// Neither legacy write option enabled - check auto-pull for read-only mode
if !cmd.Flags().Changed("auto-pull") {
if envVal := os.Getenv("BEADS_AUTO_PULL"); envVal != "" {
autoPull = envVal == "true" || envVal == "1"
} else if configVal, _ := store.GetConfig(ctx, "daemon.auto-pull"); configVal != "" {
autoPull = configVal == "true"
} else if configVal, _ := store.GetConfig(ctx, "daemon.auto_pull"); configVal != "" {
autoPull = configVal == "true"
} else if hasSyncBranch {
// Default auto-pull to true when sync-branch configured
autoPull = true
}
}
// Fallback: if sync-branch configured and no explicit settings, default to full sync
if hasSyncBranch && !cmd.Flags().Changed("auto-commit") && !cmd.Flags().Changed("auto-push") {
autoCommit = true
autoPush = true
autoPull = true
}
return autoCommit, autoPush, autoPull
}