feat(daemon): add auto_pull config parameter for periodic remote sync

Add --auto-pull flag to control whether the daemon periodically pulls from
remote to check for updates from other clones.

Configuration precedence:
1. --auto-pull CLI flag (highest)
2. BEADS_AUTO_PULL environment variable
3. daemon.auto_pull in database config
4. Default: true when sync.branch is configured

When auto_pull is enabled, the daemon creates a remoteSyncTicker that
periodically calls doAutoImport() to pull remote changes. When disabled,
users must manually run 'git pull' to sync remote changes.

Changes:
- cmd/bd/daemon.go: Add --auto-pull flag and config reading logic
- cmd/bd/daemon_event_loop.go: Gate remoteSyncTicker on autoPull parameter
- cmd/bd/daemon_lifecycle.go: Add auto-pull to status output and spawn args
- internal/rpc/protocol.go: Add AutoPull field to StatusResponse
- internal/rpc/server_core.go: Add autoPull to Server struct and SetConfig
- internal/rpc/server_routing_validation_diagnostics.go: Include in status
- Tests updated to pass autoPull parameter

Closes #TBD
This commit is contained in:
Charles P. Cross
2025-12-22 18:47:18 -05:00
parent 82cbd98e50
commit 03f5afb605
9 changed files with 133 additions and 17 deletions

View File

@@ -52,6 +52,7 @@ Run 'bd daemon' with no flags to see available options.`,
interval, _ := cmd.Flags().GetDuration("interval")
autoCommit, _ := cmd.Flags().GetBool("auto-commit")
autoPush, _ := cmd.Flags().GetBool("auto-push")
autoPull, _ := cmd.Flags().GetBool("auto-pull")
localMode, _ := cmd.Flags().GetBool("local")
logFile, _ := cmd.Flags().GetString("log")
foreground, _ := cmd.Flags().GetBool("foreground")
@@ -89,6 +90,31 @@ Run 'bd daemon' with no flags to see available options.`,
}
}
}
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 dbPath := beads.FindDatabasePath(); dbPath != "" {
// Check database config
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
if syncBranch, err := store.GetConfig(ctx, "sync.branch"); err == nil && syncBranch != "" {
autoPull = true
}
}
_ = store.Close()
}
}
}
}
if interval <= 0 {
@@ -212,14 +238,14 @@ Run 'bd daemon' with no flags to see available options.`,
if localMode {
fmt.Printf("Starting bd daemon in LOCAL mode (interval: %v, no git sync)\n", interval)
} else {
fmt.Printf("Starting bd daemon (interval: %v, auto-commit: %v, auto-push: %v)\n",
interval, autoCommit, autoPush)
fmt.Printf("Starting bd daemon (interval: %v, auto-commit: %v, auto-push: %v, auto-pull: %v)\n",
interval, autoCommit, autoPush, autoPull)
}
if logFile != "" {
fmt.Printf("Logging to: %s\n", logFile)
}
startDaemon(interval, autoCommit, autoPush, localMode, foreground, logFile, pidFile)
startDaemon(interval, autoCommit, autoPush, autoPull, localMode, foreground, logFile, pidFile)
},
}
@@ -228,6 +254,7 @@ func init() {
daemonCmd.Flags().Duration("interval", 5*time.Second, "Sync check interval")
daemonCmd.Flags().Bool("auto-commit", false, "Automatically commit changes")
daemonCmd.Flags().Bool("auto-push", false, "Automatically push commits")
daemonCmd.Flags().Bool("auto-pull", false, "Automatically pull from remote (default: true when sync.branch configured)")
daemonCmd.Flags().Bool("local", false, "Run in local-only mode (no git required, no sync)")
daemonCmd.Flags().Bool("stop", false, "Stop running daemon")
daemonCmd.Flags().Bool("stop-all", false, "Stop all running bd daemons")
@@ -252,7 +279,7 @@ func computeDaemonParentPID() int {
}
return os.Getppid()
}
func runDaemonLoop(interval time.Duration, autoCommit, autoPush, localMode bool, logPath, pidFile string) {
func runDaemonLoop(interval time.Duration, autoCommit, autoPush, autoPull, localMode bool, logPath, pidFile string) {
logF, log := setupDaemonLogger(logPath)
defer func() { _ = logF.Close() }()
@@ -474,7 +501,7 @@ func runDaemonLoop(interval time.Duration, autoCommit, autoPush, localMode bool,
}
// Set daemon configuration for status reporting
server.SetConfig(autoCommit, autoPush, localMode, interval.String(), daemonMode)
server.SetConfig(autoCommit, autoPush, autoPull, localMode, interval.String(), daemonMode)
// Register daemon in global registry
registry, err := daemon.NewRegistry()
@@ -537,7 +564,7 @@ func runDaemonLoop(interval time.Duration, autoCommit, autoPush, localMode bool,
doExport = createExportFunc(ctx, store, autoCommit, autoPush, log)
doAutoImport = createAutoImportFunc(ctx, store, log)
}
runEventDrivenLoop(ctx, cancel, server, serverErrChan, store, jsonlPath, doExport, doAutoImport, parentPID, log)
runEventDrivenLoop(ctx, cancel, server, serverErrChan, store, jsonlPath, doExport, doAutoImport, autoPull, parentPID, log)
}
case "poll":
log.log("Using polling mode (interval: %v)", interval)

View File

@@ -36,6 +36,7 @@ func runEventDrivenLoop(
jsonlPath string,
doExport func(),
doAutoImport func(),
autoPull bool,
parentPID int,
log daemonLogger,
) {
@@ -100,9 +101,20 @@ func runEventDrivenLoop(
// This is essential for multi-clone workflows where the file watcher only
// sees local changes but remote may have updates from other clones.
// Default is 30 seconds; configurable via BEADS_REMOTE_SYNC_INTERVAL.
remoteSyncInterval := getRemoteSyncInterval(log)
remoteSyncTicker := time.NewTicker(remoteSyncInterval)
defer remoteSyncTicker.Stop()
// Only enabled when autoPull is true (default when sync.branch is configured).
var remoteSyncTicker *time.Ticker
if autoPull {
remoteSyncInterval := getRemoteSyncInterval(log)
if remoteSyncInterval > 0 {
remoteSyncTicker = time.NewTicker(remoteSyncInterval)
defer remoteSyncTicker.Stop()
log.log("Auto-pull enabled: checking remote every %v", remoteSyncInterval)
} else {
log.log("Auto-pull disabled: remote-sync-interval is 0")
}
} else {
log.log("Auto-pull disabled: use 'git pull' manually to sync remote changes")
}
// Parent process check (every 10 seconds)
parentCheckTicker := time.NewTicker(10 * time.Second)
@@ -126,7 +138,13 @@ func runEventDrivenLoop(
// Periodic health validation (not sync)
checkDaemonHealth(ctx, store, log)
case <-remoteSyncTicker.C:
case <-func() <-chan time.Time {
if remoteSyncTicker != nil {
return remoteSyncTicker.C
}
// Never fire if auto-pull is disabled
return make(chan time.Time)
}():
// Periodic remote sync to pull updates from other clones
// This ensures the daemon sees changes pushed by other clones
// even when the local file watcher doesn't trigger

View File

@@ -586,7 +586,7 @@ func TestEventDrivenLoop_PeriodicRemoteSync(t *testing.T) {
done := make(chan struct{})
go func() {
runEventDrivenLoop(ctx2, cancel2, server, serverErrChan, testStore, jsonlPath, doExport, doAutoImport, 0, log)
runEventDrivenLoop(ctx2, cancel2, server, serverErrChan, testStore, jsonlPath, doExport, doAutoImport, true, 0, log)
close(done)
}()

View File

@@ -102,6 +102,7 @@ func showDaemonStatus(pidFile string) {
fmt.Printf(" Sync Interval: %s\n", rpcStatus.SyncInterval)
fmt.Printf(" Auto-Commit: %v\n", rpcStatus.AutoCommit)
fmt.Printf(" Auto-Push: %v\n", rpcStatus.AutoPush)
fmt.Printf(" Auto-Pull: %v\n", rpcStatus.AutoPull)
if rpcStatus.LocalMode {
fmt.Printf(" Local Mode: %v (no git sync)\n", rpcStatus.LocalMode)
}
@@ -359,7 +360,7 @@ func stopAllDaemons() {
}
// startDaemon starts the daemon (in foreground if requested, otherwise background)
func startDaemon(interval time.Duration, autoCommit, autoPush, localMode, foreground bool, logFile, pidFile string) {
func startDaemon(interval time.Duration, autoCommit, autoPush, autoPull, localMode, foreground bool, logFile, pidFile string) {
logPath, err := getLogFilePath(logFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
@@ -368,7 +369,7 @@ func startDaemon(interval time.Duration, autoCommit, autoPush, localMode, foregr
// Run in foreground if --foreground flag set or if we're the forked child process
if foreground || os.Getenv("BD_DAEMON_FOREGROUND") == "1" {
runDaemonLoop(interval, autoCommit, autoPush, localMode, logPath, pidFile)
runDaemonLoop(interval, autoCommit, autoPush, autoPull, localMode, logPath, pidFile)
return
}
@@ -387,6 +388,9 @@ func startDaemon(interval time.Duration, autoCommit, autoPush, localMode, foregr
if autoPush {
args = append(args, "--auto-push")
}
if autoPull {
args = append(args, "--auto-pull")
}
if localMode {
args = append(args, "--local")
}

View File

@@ -254,6 +254,68 @@ func TestSyncBranchPull_FetchesRemoteUpdates(t *testing.T) {
t.Log("Sync branch pull correctly fetches remote updates")
}
// =============================================================================
// AUTO-PULL CONFIGURATION TESTS
// =============================================================================
// TestAutoPullGatesRemoteSyncTicker validates that the remoteSyncTicker is only
// created when autoPull is true.
func TestAutoPullGatesRemoteSyncTicker(t *testing.T) {
// Read the daemon_event_loop.go file and check for autoPull gating
content, err := os.ReadFile("daemon_event_loop.go")
if err != nil {
t.Fatalf("Failed to read daemon_event_loop.go: %v", err)
}
code := string(content)
// Check that remoteSyncTicker is gated on autoPull
if !strings.Contains(code, "if autoPull") {
t.Fatal("autoPull check not found - remoteSyncTicker not gated on autoPull")
}
// Check that autoPull parameter exists in function signature
if !strings.Contains(code, "autoPull bool") {
t.Fatal("autoPull bool parameter not found in runEventDrivenLoop signature")
}
// Check for disabled message when autoPull is false
if !strings.Contains(code, "Auto-pull disabled") {
t.Fatal("Auto-pull disabled message not found")
}
t.Log("remoteSyncTicker is correctly gated on autoPull parameter")
}
// TestAutoPullDefaultBehavior validates that auto_pull defaults to true when
// sync.branch is configured.
func TestAutoPullDefaultBehavior(t *testing.T) {
// Read daemon.go and check for default behavior
content, err := os.ReadFile("daemon.go")
if err != nil {
t.Fatalf("Failed to read daemon.go: %v", err)
}
code := string(content)
// Check that auto_pull reads from daemon.auto_pull config
if !strings.Contains(code, "daemon.auto_pull") {
t.Fatal("daemon.auto_pull config check not found")
}
// Check that auto_pull defaults based on sync.branch
if !strings.Contains(code, "sync.branch") {
t.Fatal("sync.branch check for auto_pull default not found")
}
// Check for BEADS_AUTO_PULL environment variable
if !strings.Contains(code, "BEADS_AUTO_PULL") {
t.Fatal("BEADS_AUTO_PULL environment variable not checked")
}
t.Log("auto_pull correctly defaults to true when sync.branch is configured")
}
// =============================================================================
// HELPER FUNCTIONS
// =============================================================================

View File

@@ -300,6 +300,7 @@ type StatusResponse struct {
// Daemon configuration
AutoCommit bool `json:"auto_commit"` // Whether auto-commit is enabled
AutoPush bool `json:"auto_push"` // Whether auto-push is enabled
AutoPull bool `json:"auto_pull"` // Whether auto-pull is enabled (periodic remote sync)
LocalMode bool `json:"local_mode"` // Whether running in local-only mode (no git)
SyncInterval string `json:"sync_interval"` // Sync interval (e.g., "5s")
DaemonMode string `json:"daemon_mode"` // Sync mode: "poll" or "events"

View File

@@ -57,6 +57,7 @@ type Server struct {
// Daemon configuration (set via SetConfig after creation)
autoCommit bool
autoPush bool
autoPull bool
localMode bool
syncInterval string
daemonMode string
@@ -159,11 +160,12 @@ func (s *Server) MutationChan() <-chan MutationEvent {
}
// SetConfig sets the daemon configuration for status reporting
func (s *Server) SetConfig(autoCommit, autoPush, localMode bool, syncInterval, daemonMode string) {
func (s *Server) SetConfig(autoCommit, autoPush, autoPull, localMode bool, syncInterval, daemonMode string) {
s.mu.Lock()
defer s.mu.Unlock()
s.autoCommit = autoCommit
s.autoPush = autoPush
s.autoPull = autoPull
s.localMode = localMode
s.syncInterval = syncInterval
s.daemonMode = daemonMode

View File

@@ -280,6 +280,7 @@ func (s *Server) handleStatus(_ *Request) Response {
s.mu.RLock()
autoCommit := s.autoCommit
autoPush := s.autoPush
autoPull := s.autoPull
localMode := s.localMode
syncInterval := s.syncInterval
daemonMode := s.daemonMode
@@ -297,6 +298,7 @@ func (s *Server) handleStatus(_ *Request) Response {
ExclusiveLockHolder: lockHolder,
AutoCommit: autoCommit,
AutoPush: autoPush,
AutoPull: autoPull,
LocalMode: localMode,
SyncInterval: syncInterval,
DaemonMode: daemonMode,

View File

@@ -97,7 +97,7 @@ func TestStatusEndpointWithConfig(t *testing.T) {
server := NewServer(socketPath, store, tmpDir, dbPath)
// Set config before starting
server.SetConfig(true, true, false, "10s", "events")
server.SetConfig(true, true, true, false, "10s", "events")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -155,7 +155,7 @@ func TestStatusEndpointLocalMode(t *testing.T) {
server := NewServer(socketPath, store, tmpDir, dbPath)
// Set config for local mode
server.SetConfig(false, false, true, "5s", "poll")
server.SetConfig(false, false, false, true, "5s", "poll")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -284,7 +284,7 @@ func TestSetConfigConcurrency(t *testing.T) {
done := make(chan bool)
for i := 0; i < 10; i++ {
go func(n int) {
server.SetConfig(n%2 == 0, n%3 == 0, n%4 == 0, "5s", "events")
server.SetConfig(n%2 == 0, n%3 == 0, n%5 == 0, n%4 == 0, "5s", "events")
done <- true
}(i)
}