|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "path/filepath" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/fsnotify/fsnotify" |
| 11 | + "github.com/roborev-dev/roborev/internal/agent" |
| 12 | + "github.com/roborev-dev/roborev/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +// ConfigGetter provides access to the current config |
| 16 | +type ConfigGetter interface { |
| 17 | + Config() *config.Config |
| 18 | +} |
| 19 | + |
| 20 | +// StaticConfig wraps a config for use without hot-reloading (e.g., in tests) |
| 21 | +type StaticConfig struct { |
| 22 | + cfg *config.Config |
| 23 | +} |
| 24 | + |
| 25 | +// NewStaticConfig creates a ConfigGetter that always returns the same config |
| 26 | +func NewStaticConfig(cfg *config.Config) *StaticConfig { |
| 27 | + return &StaticConfig{cfg: cfg} |
| 28 | +} |
| 29 | + |
| 30 | +// Config returns the static config |
| 31 | +func (sc *StaticConfig) Config() *config.Config { |
| 32 | + return sc.cfg |
| 33 | +} |
| 34 | + |
| 35 | +// ConfigWatcher watches config.toml for changes and reloads configuration. |
| 36 | +// |
| 37 | +// Hot-reloadable settings take effect immediately: default_agent, job_timeout, |
| 38 | +// allow_unsafe_agents, anthropic_api_key, review_context_count. |
| 39 | +// |
| 40 | +// Settings requiring restart: server_addr, max_workers, [sync] section. |
| 41 | +// These are read at startup and the running values are preserved even if the |
| 42 | +// config file changes. CLI flag overrides (--addr, --workers) only apply to |
| 43 | +// restart-required settings, so they remain in effect for the daemon's lifetime. |
| 44 | +// The config object may show file values after reload, but the actual running |
| 45 | +// server address and worker pool size are fixed at startup. |
| 46 | +type ConfigWatcher struct { |
| 47 | + configPath string |
| 48 | + cfg *config.Config |
| 49 | + cfgMu sync.RWMutex |
| 50 | + broadcaster Broadcaster |
| 51 | + watcher *fsnotify.Watcher |
| 52 | + stopCh chan struct{} |
| 53 | + stopOnce sync.Once |
| 54 | + lastReloadedAt time.Time // Time of last successful config reload |
| 55 | +} |
| 56 | + |
| 57 | +// NewConfigWatcher creates a new config watcher |
| 58 | +func NewConfigWatcher(configPath string, cfg *config.Config, broadcaster Broadcaster) *ConfigWatcher { |
| 59 | + return &ConfigWatcher{ |
| 60 | + configPath: configPath, |
| 61 | + cfg: cfg, |
| 62 | + broadcaster: broadcaster, |
| 63 | + stopCh: make(chan struct{}), |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Start begins watching the config file for changes |
| 68 | +func (cw *ConfigWatcher) Start(ctx context.Context) error { |
| 69 | + // Skip watching if no config path provided (e.g., in tests) |
| 70 | + if cw.configPath == "" { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + watcher, err := fsnotify.NewWatcher() |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + cw.watcher = watcher |
| 79 | + |
| 80 | + // Watch the directory containing the config file, not the file itself. |
| 81 | + // This handles editors that do atomic writes (delete + create). |
| 82 | + configDir := filepath.Dir(cw.configPath) |
| 83 | + configFile := filepath.Base(cw.configPath) |
| 84 | + |
| 85 | + if err := watcher.Add(configDir); err != nil { |
| 86 | + watcher.Close() |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + go cw.watchLoop(ctx, configFile) |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +// Stop stops the config watcher. Safe to call multiple times. |
| 95 | +func (cw *ConfigWatcher) Stop() { |
| 96 | + cw.stopOnce.Do(func() { |
| 97 | + close(cw.stopCh) |
| 98 | + if cw.watcher != nil { |
| 99 | + cw.watcher.Close() |
| 100 | + } |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +// Config returns the current config with read lock |
| 105 | +func (cw *ConfigWatcher) Config() *config.Config { |
| 106 | + cw.cfgMu.RLock() |
| 107 | + defer cw.cfgMu.RUnlock() |
| 108 | + return cw.cfg |
| 109 | +} |
| 110 | + |
| 111 | +// LastReloadedAt returns the time of the last successful config reload |
| 112 | +func (cw *ConfigWatcher) LastReloadedAt() time.Time { |
| 113 | + cw.cfgMu.RLock() |
| 114 | + defer cw.cfgMu.RUnlock() |
| 115 | + return cw.lastReloadedAt |
| 116 | +} |
| 117 | + |
| 118 | +func (cw *ConfigWatcher) watchLoop(ctx context.Context, configFile string) { |
| 119 | + // Debounce timer to handle rapid file changes |
| 120 | + var debounceTimer *time.Timer |
| 121 | + debounceDelay := 200 * time.Millisecond |
| 122 | + |
| 123 | + for { |
| 124 | + select { |
| 125 | + case <-ctx.Done(): |
| 126 | + return |
| 127 | + case <-cw.stopCh: |
| 128 | + return |
| 129 | + case event, ok := <-cw.watcher.Events: |
| 130 | + if !ok { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + // Only react to changes to our config file |
| 135 | + if filepath.Base(event.Name) != configFile { |
| 136 | + continue |
| 137 | + } |
| 138 | + |
| 139 | + // React to write, create, or rename events |
| 140 | + // Rename is needed for editors that do atomic saves via rename (e.g., vim) |
| 141 | + if event.Op&(fsnotify.Write|fsnotify.Create|fsnotify.Rename) == 0 { |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + // Debounce: reset timer on each event |
| 146 | + if debounceTimer != nil { |
| 147 | + debounceTimer.Stop() |
| 148 | + } |
| 149 | + debounceTimer = time.AfterFunc(debounceDelay, func() { |
| 150 | + cw.reloadConfig() |
| 151 | + }) |
| 152 | + |
| 153 | + case err, ok := <-cw.watcher.Errors: |
| 154 | + if !ok { |
| 155 | + return |
| 156 | + } |
| 157 | + log.Printf("Config watcher error: %v", err) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func (cw *ConfigWatcher) reloadConfig() { |
| 163 | + newCfg, err := config.LoadGlobalFrom(cw.configPath) |
| 164 | + if err != nil { |
| 165 | + log.Printf("Failed to reload config: %v", err) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + cw.cfgMu.Lock() |
| 170 | + oldCfg := cw.cfg |
| 171 | + cw.cfg = newCfg |
| 172 | + cw.lastReloadedAt = time.Now() |
| 173 | + cw.cfgMu.Unlock() |
| 174 | + |
| 175 | + // Update global agent settings |
| 176 | + agent.SetAllowUnsafeAgents(newCfg.AllowUnsafeAgents != nil && *newCfg.AllowUnsafeAgents) |
| 177 | + agent.SetAnthropicAPIKey(newCfg.AnthropicAPIKey) |
| 178 | + |
| 179 | + // Log what changed (for debugging) |
| 180 | + logConfigChanges(oldCfg, newCfg) |
| 181 | + |
| 182 | + // Broadcast config reloaded event to notify connected clients |
| 183 | + cw.broadcaster.Broadcast(Event{ |
| 184 | + Type: "config.reloaded", |
| 185 | + TS: time.Now(), |
| 186 | + }) |
| 187 | + |
| 188 | + log.Printf("Config reloaded successfully") |
| 189 | +} |
| 190 | + |
| 191 | +func logConfigChanges(old, new *config.Config) { |
| 192 | + if old.DefaultAgent != new.DefaultAgent { |
| 193 | + log.Printf("Config change: default_agent %q -> %q", old.DefaultAgent, new.DefaultAgent) |
| 194 | + } |
| 195 | + if old.ReviewContextCount != new.ReviewContextCount { |
| 196 | + log.Printf("Config change: review_context_count %d -> %d", old.ReviewContextCount, new.ReviewContextCount) |
| 197 | + } |
| 198 | + if old.JobTimeoutMinutes != new.JobTimeoutMinutes { |
| 199 | + log.Printf("Config change: job_timeout_minutes %d -> %d", old.JobTimeoutMinutes, new.JobTimeoutMinutes) |
| 200 | + } |
| 201 | + oldUnsafe := old.AllowUnsafeAgents != nil && *old.AllowUnsafeAgents |
| 202 | + newUnsafe := new.AllowUnsafeAgents != nil && *new.AllowUnsafeAgents |
| 203 | + if oldUnsafe != newUnsafe { |
| 204 | + log.Printf("Config change: allow_unsafe_agents %v -> %v", oldUnsafe, newUnsafe) |
| 205 | + } |
| 206 | + if old.MaxWorkers != new.MaxWorkers { |
| 207 | + log.Printf("Config change: max_workers %d -> %d (requires daemon restart to take effect)", old.MaxWorkers, new.MaxWorkers) |
| 208 | + } |
| 209 | + if old.ServerAddr != new.ServerAddr { |
| 210 | + log.Printf("Config change: server_addr %q -> %q (requires daemon restart to take effect)", old.ServerAddr, new.ServerAddr) |
| 211 | + } |
| 212 | +} |
0 commit comments