//! Publishes resolved MCP configuration and host timing rollouts for future turns. use std::sync::Arc; use crate::ConfigRefreshOutcome; use crate::config::Config; use crate::config::Constrained; use crate::config::RuntimeConfigRefresh; use crate::session::Session; use akode_features::Feature; use tracing::warn; impl Session { pub(crate) async fn refresh_runtime_config( &self, expected_config: Arc, next_config: Config, ) -> ConfigRefreshOutcome { self.refresh_config(expected_config, next_config, RuntimeConfigRefresh::User) .await } pub(crate) async fn refresh_mcp_config( &self, expected_config: Arc, next_config: Config, ) -> ConfigRefreshOutcome { self.refresh_config(expected_config, next_config, RuntimeConfigRefresh::Mcp) .await } pub(super) async fn refresh_config( &self, expected_config: Arc, next_config: Config, scope: RuntimeConfigRefresh, ) -> ConfigRefreshOutcome { let (mut config, outcome, rejection_kind) = match expected_config.resolve_runtime_refresh(&next_config, scope) { Ok(config) => (config, ConfigRefreshOutcome::Published, None), Err(error) => { let mut config = expected_config.as_ref().clone(); if matches!(scope, RuntimeConfigRefresh::Mcp) { // The host-resolved map already carries fresh requirements and // preserved session overrides. Keep ordinary-server revocations even // when retained layers make the merge invalid. config.mcp_servers = next_config.mcp_servers.clone(); } config.config_layer_stack = config .config_layer_stack .with_mcp_requirements_from(&next_config.config_layer_stack); if let Err(err) = config.features.refresh_mcp_features(&next_config.features) { warn!(%err, "failed to apply managed MCP feature restrictions"); config.mcp_servers = Constrained::allow_any(Default::default()); } (config, ConfigRefreshOutcome::Rejected, Some(error.kind())) } }; if !matches!(scope, RuntimeConfigRefresh::UserFiles) { for (feature, description) in [(Feature::Mcp20260728, "MCP protocol")] { if let Err(err) = config .features .set_enabled(feature, next_config.features.enabled(feature)) { warn!("failed to refresh {description} config: {err}"); } } } // Publish host rollout changes only to future turn snapshots. Active turns // must retain one capture policy across tool dispatch and Guardian review. if matches!(scope, RuntimeConfigRefresh::User) && let Err(err) = config.features.set_enabled( Feature::CriticalPathTiming, next_config.features.enabled(Feature::CriticalPathTiming), ) { warn!("failed to refresh critical path timing config: {err}"); } // Per-model settings of the active provider apply to new turns without a restart. let refresh_model_settings = !matches!(scope, RuntimeConfigRefresh::Mcp); if refresh_model_settings && let Some(next) = next_config.model_providers.get(&config.model_provider_id) { let models = next.models.clone(); if let Some(provider) = config.model_providers.get_mut(&config.model_provider_id) { provider.models = models.clone(); } config.model_provider.models = models; } let config = Arc::new(config); let notify_contributors = !matches!(scope, RuntimeConfigRefresh::Mcp) && !self.services.extensions.config_contributors().is_empty(); let (previous_config, new_config) = { let mut state = self.state.lock().await; // Loaded snapshots can publish only while their captured owner is current. if !Arc::ptr_eq( &state.session_configuration.original_config_do_not_use, &expected_config, ) { return ConfigRefreshOutcome::Stale; } if let Some(rejection_kind) = rejection_kind { warn!( ?rejection_kind, "keeping previous configuration after rejected refresh" ); } if matches!(scope, RuntimeConfigRefresh::UserFiles) { self.services.skills_service.clear_cache(); self.services.plugins_manager.clear_cache(); } // A host refresh carries current rollout settings for recording. A // legacy file reload uses its original snapshot and bypasses this path. if matches!(scope, RuntimeConfigRefresh::User) { self.services .executed_tool_calls .refresh(&next_config.features); } let previous_config = notify_contributors .then(|| self.build_effective_session_config(&state.session_configuration)); state.session_configuration.original_config_do_not_use = Arc::clone(&config); if refresh_model_settings { state.session_configuration.model_info_overrides.context_windows = config.model_context_windows(); self.services .model_client .set_model_settings(config.model_provider.models.clone()); } if matches!(scope, RuntimeConfigRefresh::Mcp) { self.services.mcp_runtime.invalidate_resource_caches(); } self.mark_mcp_runtime_dirty(); let new_config = notify_contributors .then(|| self.build_effective_session_config(&state.session_configuration)); (previous_config, new_config) }; self.emit_config_changed_contributors(previous_config.as_ref(), new_config.as_ref()); self.schedule_mcp_prewarm(); self.refresh_hooks(config).await; outcome } }