Skip to main content

bamboo_server/app_state/
config_runtime.rs

1use super::*;
2
3impl AppState {
4    /// Reload the provider based on current configuration
5    ///
6    /// Re-reads the configuration and creates a new LLM provider
7    /// instance, allowing runtime switching of providers or models.
8    ///
9    /// # Returns
10    ///
11    /// `Ok(())` if the provider was successfully reloaded.
12    ///
13    /// # Errors
14    ///
15    /// Returns an error if:
16    /// - Configuration cannot be read
17    /// - Provider initialization fails (e.g., invalid API key)
18    ///
19    /// # Example
20    ///
21    /// ```rust,no_run
22    /// use bamboo_server::app_state::AppState;
23    /// use std::path::PathBuf;
24    ///
25    /// #[tokio::main]
26    /// async fn main() {
27    ///     let state = AppState::new(PathBuf::from("/path/to/.bamboo"))
28    ///         .await
29    ///         .expect("failed to initialize app state");
30    ///
31    ///     // User updated config file...
32    ///     state.reload_provider().await.expect("Provider reload failed");
33    /// }
34    /// ```
35    pub async fn reload_provider(&self) -> Result<(), bamboo_llm::LLMError> {
36        let config = self.config.read().await.clone();
37
38        self.provider_registry
39            .reload_from_config(&config, self.app_data_dir.clone())
40            .await?;
41
42        let default_provider_name = self.provider_registry.default_provider_name();
43        tracing::info!(
44            default_provider = %default_provider_name,
45            legacy_provider = %config.provider,
46            has_provider_instances = config.has_provider_instances(),
47            "Reloading provider runtime from current config"
48        );
49
50        let new_provider = self.provider_registry.get_default().unwrap_or_else(|| {
51            let message = if config.has_provider_instances() {
52                format!(
53                    "Default provider instance '{}' is not available or failed to initialize",
54                    default_provider_name
55                )
56            } else {
57                format!(
58                    "Provider '{}' is not available or failed to initialize",
59                    config.provider
60                )
61            };
62            Arc::new(UnconfiguredProvider { message }) as Arc<dyn LLMProvider>
63        });
64
65        let mut provider = self.provider.write().await;
66        *provider = new_provider;
67
68        tracing::info!(
69            default_provider = %default_provider_name,
70            "Provider reloaded successfully"
71        );
72        Ok(())
73    }
74
75    /// Reload the configuration from file
76    ///
77    /// Reads the configuration file again and updates the in-memory
78    /// config. Note: This does NOT automatically reload the provider;
79    /// call `reload_provider()` afterwards if needed.
80    ///
81    /// # Returns
82    ///
83    /// The newly loaded configuration.
84    ///
85    /// # Example
86    ///
87    /// ```rust,no_run
88    /// use bamboo_server::app_state::AppState;
89    /// use std::path::PathBuf;
90    ///
91    /// #[tokio::main]
92    /// async fn main() {
93    ///     let state = AppState::new(PathBuf::from("/path/to/.bamboo"))
94    ///         .await
95    ///         .expect("failed to initialize app state");
96    ///
97    ///     // Reload config from disk
98    ///     let new_config = state.reload_config().await;
99    ///
100    ///     // Optionally reload provider with new config
101    ///     state.reload_provider().await.ok();
102    /// }
103    /// ```
104    pub async fn reload_config(&self) -> Config {
105        // Read from disk INSIDE the write lock. If the disk read happened before
106        // acquiring the lock, a concurrent update_config() could persist new
107        // state in that gap and then be clobbered here by the stale disk copy
108        // (in-memory-only mutations silently lost). Holding the lock across the
109        // read+swap serializes reload with update_config's in-memory mutation.
110        // Config::from_data_dir is a sync read (no await), so this doesn't hold
111        // the lock across an await point. #41.
112        // Hold the config-IO lock across the read+swap so it can't interleave
113        // with a config write's mutate+persist (which would let us read the disk
114        // BEFORE that write persisted, then clobber its in-memory mutation). #126.
115        let _io = self.config_io_lock.lock().await;
116        let mut config = self.config.write().await;
117        let new_config = Config::from_data_dir(Some(self.app_data_dir.clone()));
118        *config = new_config.clone();
119        new_config
120    }
121
122    async fn persist_config_snapshot(&self, config: Config) -> anyhow::Result<()> {
123        let data_dir = self.app_data_dir.clone();
124        tokio::task::spawn_blocking(move || config.save_to_dir(data_dir))
125            .await
126            .map_err(|e| anyhow::anyhow!("Config save task failed: {e}"))??;
127        Ok(())
128    }
129
130    /// Unified config update entrypoint.
131    ///
132    /// Invariants:
133    /// - Update in-memory first
134    /// - Persist to disk
135    /// - Apply runtime side-effects last (provider reload, MCP reconcile)
136    pub async fn update_config<F>(
137        &self,
138        update: F,
139        effects: ConfigUpdateEffects,
140    ) -> Result<Config, AppError>
141    where
142        F: FnOnce(&mut Config) -> Result<(), AppError>,
143    {
144        // Hold the config-IO lock across BOTH the in-memory mutation AND the disk
145        // persist, so a concurrent reload_config can't read the disk in the gap
146        // before we persist and then clobber this mutation with the stale copy
147        // (#126). The lock is dropped before apply_config_effects — slow side
148        // effects (provider reload) don't need to block reloads/other updates.
149        let snapshot = {
150            let _io = self.config_io_lock.lock().await;
151            let snapshot = {
152                let mut cfg = self.config.write().await;
153                update(&mut cfg)?;
154                cfg.publish_env_vars();
155                cfg.clone()
156            };
157            self.persist_config_snapshot(snapshot.clone())
158                .await
159                .map_err(|e| {
160                    AppError::InternalError(anyhow::anyhow!("Failed to save config: {e}"))
161                })?;
162            snapshot
163        };
164
165        self.apply_config_effects(snapshot.clone(), effects).await?;
166        Ok(snapshot)
167    }
168
169    /// Replace the full config (used for JSON merge endpoints).
170    pub async fn replace_config(
171        &self,
172        new_config: Config,
173        effects: ConfigUpdateEffects,
174    ) -> Result<Config, AppError> {
175        // Same #126 serialization as update_config: mutate + persist under the
176        // config-IO lock so a reload can't interleave; effects run unlocked.
177        {
178            let _io = self.config_io_lock.lock().await;
179            {
180                let mut cfg = self.config.write().await;
181                *cfg = new_config.clone();
182                cfg.publish_env_vars();
183            }
184            self.persist_config_snapshot(new_config.clone())
185                .await
186                .map_err(|e| {
187                    AppError::InternalError(anyhow::anyhow!("Failed to save config: {e}"))
188                })?;
189        }
190
191        self.apply_config_effects(new_config.clone(), effects)
192            .await?;
193        Ok(new_config)
194    }
195
196    async fn apply_config_effects(
197        &self,
198        new_config: Config,
199        effects: ConfigUpdateEffects,
200    ) -> Result<(), AppError> {
201        if effects.reload_provider {
202            self.reload_provider().await.map_err(|e| {
203                AppError::InternalError(anyhow::anyhow!(
204                    "Failed to reload provider after updating config: {e}"
205                ))
206            })?;
207        }
208
209        if effects.reconcile_mcp {
210            self.mcp_manager
211                .reconcile_from_config(&new_config.mcp)
212                .await;
213        }
214
215        Ok(())
216    }
217}