From 03f5afb6054a64832bcce840511fabbdee716c9e Mon Sep 17 00:00:00 2001 From: "Charles P. Cross" Date: Mon, 22 Dec 2025 18:47:18 -0500 Subject: [PATCH] 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 --- cmd/bd/daemon.go | 39 ++++++++++-- cmd/bd/daemon_event_loop.go | 26 ++++++-- cmd/bd/daemon_integration_test.go | 2 +- cmd/bd/daemon_lifecycle.go | 8 ++- cmd/bd/periodic_remote_sync_test.go | 62 +++++++++++++++++++ internal/rpc/protocol.go | 1 + internal/rpc/server_core.go | 4 +- .../server_routing_validation_diagnostics.go | 2 + internal/rpc/status_test.go | 6 +- 9 files changed, 133 insertions(+), 17 deletions(-) diff --git a/cmd/bd/daemon.go b/cmd/bd/daemon.go index 11bd30b2..84eb633c 100644 --- a/cmd/bd/daemon.go +++ b/cmd/bd/daemon.go @@ -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) diff --git a/cmd/bd/daemon_event_loop.go b/cmd/bd/daemon_event_loop.go index 782c3ad5..3eb1cefc 100644 --- a/cmd/bd/daemon_event_loop.go +++ b/cmd/bd/daemon_event_loop.go @@ -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 diff --git a/cmd/bd/daemon_integration_test.go b/cmd/bd/daemon_integration_test.go index 0cc68cd7..fcc47b11 100644 --- a/cmd/bd/daemon_integration_test.go +++ b/cmd/bd/daemon_integration_test.go @@ -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) }() diff --git a/cmd/bd/daemon_lifecycle.go b/cmd/bd/daemon_lifecycle.go index 047ec004..c10f3ce7 100644 --- a/cmd/bd/daemon_lifecycle.go +++ b/cmd/bd/daemon_lifecycle.go @@ -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") } diff --git a/cmd/bd/periodic_remote_sync_test.go b/cmd/bd/periodic_remote_sync_test.go index f909bb3c..7f7fd5e6 100644 --- a/cmd/bd/periodic_remote_sync_test.go +++ b/cmd/bd/periodic_remote_sync_test.go @@ -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 // ============================================================================= diff --git a/internal/rpc/protocol.go b/internal/rpc/protocol.go index 49f7312c..251c6f7d 100644 --- a/internal/rpc/protocol.go +++ b/internal/rpc/protocol.go @@ -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" diff --git a/internal/rpc/server_core.go b/internal/rpc/server_core.go index a6a1aee2..75783198 100644 --- a/internal/rpc/server_core.go +++ b/internal/rpc/server_core.go @@ -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 diff --git a/internal/rpc/server_routing_validation_diagnostics.go b/internal/rpc/server_routing_validation_diagnostics.go index 4dcadbb0..d8965100 100644 --- a/internal/rpc/server_routing_validation_diagnostics.go +++ b/internal/rpc/server_routing_validation_diagnostics.go @@ -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, diff --git a/internal/rpc/status_test.go b/internal/rpc/status_test.go index 3dfc1d86..8ee7c25d 100644 --- a/internal/rpc/status_test.go +++ b/internal/rpc/status_test.go @@ -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) }