Skip to main content

zeph_core/bootstrap/
mod.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Application bootstrap: config resolution, provider/memory/tool construction.
5
6pub mod config;
7pub mod health;
8pub mod mcp;
9pub mod oauth;
10pub mod provider;
11pub mod skills;
12
13pub use config::{parse_vault_args, resolve_config_path};
14pub use health::{health_check, warmup_provider};
15pub use mcp::{
16    create_mcp_manager, create_mcp_manager_with_vault, create_mcp_registry, wire_trust_calibration,
17};
18pub use oauth::VaultCredentialStore;
19#[cfg(feature = "candle")]
20pub use provider::select_device;
21pub use provider::{
22    BootstrapError, build_provider_for_switch, build_provider_from_entry, create_named_provider,
23    create_provider, create_summary_provider,
24};
25pub use skills::{
26    create_embedding_provider, create_skill_matcher, effective_embedding_model, managed_skills_dir,
27};
28
29use std::path::{Path, PathBuf};
30use std::sync::Arc;
31
32use tokio::sync::{RwLock, mpsc, watch};
33use zeph_llm::any::AnyProvider;
34use zeph_llm::provider::LlmProvider;
35use zeph_memory::GraphStore;
36use zeph_memory::QdrantOps;
37use zeph_memory::semantic::SemanticMemory;
38use zeph_skills::loader::SkillMeta;
39use zeph_skills::matcher::SkillMatcherBackend;
40use zeph_skills::registry::SkillRegistry;
41use zeph_skills::watcher::{SkillEvent, SkillWatcher};
42
43use crate::config::{Config, SecretResolver};
44use crate::config_watcher::{ConfigEvent, ConfigWatcher};
45use crate::vault::AgeVaultProvider;
46use crate::vault::{EnvVaultProvider, VaultProvider};
47
48pub struct AppBuilder {
49    config: Config,
50    config_path: PathBuf,
51    vault: Box<dyn VaultProvider>,
52    /// Present when the vault backend is `age`. Used to pass to `create_mcp_manager_with_vault`
53    /// for OAuth credential persistence across sessions.
54    age_vault: Option<Arc<RwLock<AgeVaultProvider>>>,
55    qdrant_ops: Option<QdrantOps>,
56}
57
58pub struct VaultArgs {
59    pub backend: String,
60    pub key_path: Option<String>,
61    pub vault_path: Option<String>,
62}
63
64pub struct WatcherBundle {
65    pub skill_watcher: Option<SkillWatcher>,
66    pub skill_reload_rx: mpsc::Receiver<SkillEvent>,
67    pub config_watcher: Option<ConfigWatcher>,
68    pub config_reload_rx: mpsc::Receiver<ConfigEvent>,
69}
70
71impl AppBuilder {
72    /// Resolve config, load it, create vault, resolve secrets.
73    ///
74    /// CLI-provided overrides take priority over environment variables and config.
75    ///
76    /// # Errors
77    ///
78    /// Returns [`BootstrapError`] if config loading, validation, vault construction,
79    /// secret resolution, or Qdrant URL parsing fails.
80    pub async fn new(
81        config_override: Option<&Path>,
82        vault_override: Option<&str>,
83        vault_key_override: Option<&Path>,
84        vault_path_override: Option<&Path>,
85    ) -> Result<Self, BootstrapError> {
86        let config_path = resolve_config_path(config_override);
87        let mut config = Config::load(&config_path)?;
88        config.validate()?;
89        config.llm.check_legacy_format()?;
90
91        let vault_args = parse_vault_args(
92            &config,
93            vault_override,
94            vault_key_override,
95            vault_path_override,
96        );
97        let (vault, age_vault): (
98            Box<dyn VaultProvider>,
99            Option<Arc<RwLock<AgeVaultProvider>>>,
100        ) = match vault_args.backend.as_str() {
101            "env" => (Box::new(EnvVaultProvider), None),
102            "age" => {
103                let key = vault_args.key_path.ok_or_else(|| {
104                    BootstrapError::Provider("--vault-key required for age backend".into())
105                })?;
106                let path = vault_args.vault_path.ok_or_else(|| {
107                    BootstrapError::Provider("--vault-path required for age backend".into())
108                })?;
109                let provider = AgeVaultProvider::new(Path::new(&key), Path::new(&path))
110                    .map_err(BootstrapError::VaultInit)?;
111                let arc = Arc::new(RwLock::new(provider));
112                let boxed: Box<dyn VaultProvider> =
113                    Box::new(crate::vault::ArcAgeVaultProvider(Arc::clone(&arc)));
114                (boxed, Some(arc))
115            }
116            other => {
117                return Err(BootstrapError::Provider(format!(
118                    "unknown vault backend: {other}"
119                )));
120            }
121        };
122
123        config.resolve_secrets(vault.as_ref()).await?;
124
125        let qdrant_ops = match config.memory.vector_backend {
126            crate::config::VectorBackend::Qdrant => {
127                let ops = QdrantOps::new(&config.memory.qdrant_url).map_err(|e| {
128                    BootstrapError::Provider(format!(
129                        "invalid qdrant_url '{}': {e}",
130                        config.memory.qdrant_url
131                    ))
132                })?;
133                Some(ops)
134            }
135            crate::config::VectorBackend::Sqlite => None,
136        };
137
138        Ok(Self {
139            config,
140            config_path,
141            vault,
142            age_vault,
143            qdrant_ops,
144        })
145    }
146
147    pub fn qdrant_ops(&self) -> Option<&QdrantOps> {
148        self.qdrant_ops.as_ref()
149    }
150
151    pub fn config(&self) -> &Config {
152        &self.config
153    }
154
155    pub fn config_mut(&mut self) -> &mut Config {
156        &mut self.config
157    }
158
159    pub fn config_path(&self) -> &Path {
160        &self.config_path
161    }
162
163    /// Returns the vault provider used for secret resolution.
164    ///
165    /// Retained as part of the public `Bootstrap` API for external callers
166    /// that may inspect or override vault behavior at runtime.
167    pub fn vault(&self) -> &dyn VaultProvider {
168        self.vault.as_ref()
169    }
170
171    /// Returns the shared age vault, if the backend is `age`.
172    ///
173    /// Pass this to `create_mcp_manager_with_vault` so OAuth tokens are persisted
174    /// across sessions.
175    pub fn age_vault_arc(&self) -> Option<&Arc<RwLock<AgeVaultProvider>>> {
176        self.age_vault.as_ref()
177    }
178
179    /// # Errors
180    ///
181    /// Returns [`BootstrapError`] if provider creation or health check fails.
182    pub async fn build_provider(
183        &self,
184    ) -> Result<
185        (
186            AnyProvider,
187            tokio::sync::mpsc::UnboundedSender<String>,
188            tokio::sync::mpsc::UnboundedReceiver<String>,
189        ),
190        BootstrapError,
191    > {
192        let mut provider = create_provider(&self.config)?;
193
194        let (status_tx, status_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
195        let status_tx_clone = status_tx.clone();
196        provider.set_status_tx(status_tx);
197
198        health_check(&provider).await;
199
200        if let AnyProvider::Ollama(ref mut ollama) = provider
201            && let Ok(info) = ollama.fetch_model_info().await
202            && let Some(ctx) = info.context_length
203        {
204            ollama.set_context_window(ctx);
205            tracing::info!(context_window = ctx, "detected Ollama model context window");
206        }
207
208        if let Some(ctx) = provider.context_window()
209            && !matches!(provider, AnyProvider::Ollama(_))
210        {
211            tracing::info!(context_window = ctx, "detected provider context window");
212        }
213
214        Ok((provider, status_tx_clone, status_rx))
215    }
216
217    pub fn auto_budget_tokens(&self, provider: &AnyProvider) -> usize {
218        if self.config.memory.auto_budget && self.config.memory.context_budget_tokens == 0 {
219            if let Some(ctx_size) = provider.context_window() {
220                tracing::info!(model_context = ctx_size, "auto-configured context budget");
221                ctx_size
222            } else {
223                0
224            }
225        } else {
226            self.config.memory.context_budget_tokens
227        }
228    }
229
230    /// # Errors
231    ///
232    /// Returns [`BootstrapError`] if `SQLite` cannot be initialized or if `vector_backend = "Qdrant"`
233    /// but `qdrant_ops` is `None` (invariant violation — should not happen if `AppBuilder::new`
234    /// succeeded).
235    pub async fn build_memory(
236        &self,
237        provider: &AnyProvider,
238    ) -> Result<SemanticMemory, BootstrapError> {
239        let embed_model = self.embedding_model();
240        // Resolve the database path: prefer database_url (PostgreSQL) over sqlite_path.
241        let db_path: &str = self
242            .config
243            .memory
244            .database_url
245            .as_deref()
246            .unwrap_or(&self.config.memory.sqlite_path);
247
248        if zeph_db::is_postgres_url(db_path) {
249            return Err(BootstrapError::Memory(
250                "database_url points to PostgreSQL but binary was compiled with the \
251                 sqlite feature. Recompile with --features postgres."
252                    .to_string(),
253            ));
254        }
255
256        let mut memory = match self.config.memory.vector_backend {
257            crate::config::VectorBackend::Sqlite => {
258                SemanticMemory::with_sqlite_backend_and_pool_size(
259                    db_path,
260                    provider.clone(),
261                    &embed_model,
262                    self.config.memory.semantic.vector_weight,
263                    self.config.memory.semantic.keyword_weight,
264                    self.config.memory.sqlite_pool_size,
265                )
266                .await
267                .map_err(|e| BootstrapError::Memory(e.to_string()))?
268            }
269            crate::config::VectorBackend::Qdrant => {
270                let ops = self
271                    .qdrant_ops
272                    .as_ref()
273                    .ok_or_else(|| {
274                        BootstrapError::Memory(
275                            "qdrant_ops must be Some when vector_backend = Qdrant".into(),
276                        )
277                    })?
278                    .clone();
279                SemanticMemory::with_qdrant_ops(
280                    db_path,
281                    ops,
282                    provider.clone(),
283                    &embed_model,
284                    self.config.memory.semantic.vector_weight,
285                    self.config.memory.semantic.keyword_weight,
286                    self.config.memory.sqlite_pool_size,
287                )
288                .await
289                .map_err(|e| BootstrapError::Memory(e.to_string()))?
290            }
291        };
292
293        memory = memory.with_ranking_options(
294            self.config.memory.semantic.temporal_decay_enabled,
295            self.config.memory.semantic.temporal_decay_half_life_days,
296            self.config.memory.semantic.mmr_enabled,
297            self.config.memory.semantic.mmr_lambda,
298        );
299
300        memory = memory.with_importance_options(
301            self.config.memory.semantic.importance_enabled,
302            self.config.memory.semantic.importance_weight,
303        );
304
305        if self.config.memory.semantic.enabled && memory.is_vector_store_connected().await {
306            tracing::info!("semantic memory enabled, vector store connected");
307            match memory.embed_missing().await {
308                Ok(n) if n > 0 => tracing::info!("backfilled {n} missing embedding(s)"),
309                Ok(_) => {}
310                Err(e) => tracing::warn!("embed_missing failed: {e:#}"),
311            }
312        }
313
314        if self.config.memory.graph.enabled {
315            // Open a dedicated pool for graph operations to prevent pool starvation.
316            // Community detection and spreading activation can saturate the shared message pool
317            // (pool_size=5), causing pool.acquire() cancellation and semaphore drift in sqlx 0.8.
318            let graph_pool = zeph_db::DbConfig {
319                url: db_path.to_string(),
320                max_connections: self.config.memory.graph.pool_size,
321                pool_size: self.config.memory.graph.pool_size,
322            }
323            .connect()
324            .await
325            .map_err(|e| BootstrapError::Memory(e.to_string()))?;
326            let store = Arc::new(GraphStore::new(graph_pool));
327            memory = memory.with_graph_store(store);
328            tracing::info!(
329                pool_size = self.config.memory.graph.pool_size,
330                "graph memory enabled, GraphStore attached with dedicated pool"
331            );
332        }
333
334        if self.config.memory.admission.enabled {
335            memory = memory.with_admission_control(self.build_admission_control(provider));
336        }
337
338        Ok(memory)
339    }
340
341    fn build_admission_control(
342        &self,
343        fallback_provider: &AnyProvider,
344    ) -> zeph_memory::AdmissionControl {
345        let admission_provider = if self.config.memory.admission.admission_provider.is_empty() {
346            fallback_provider.clone()
347        } else {
348            match create_named_provider(
349                &self.config.memory.admission.admission_provider,
350                &self.config,
351            ) {
352                Ok(p) => {
353                    tracing::info!(
354                        provider = %self.config.memory.admission.admission_provider,
355                        "A-MAC admission provider configured"
356                    );
357                    p
358                }
359                Err(e) => {
360                    tracing::warn!(
361                        provider = %self.config.memory.admission.admission_provider,
362                        error = %e,
363                        "A-MAC admission provider resolution failed — primary provider will be used"
364                    );
365                    fallback_provider.clone()
366                }
367            }
368        };
369        let w = &self.config.memory.admission.weights;
370        let weights = zeph_memory::AdmissionWeights {
371            future_utility: w.future_utility,
372            factual_confidence: w.factual_confidence,
373            semantic_novelty: w.semantic_novelty,
374            temporal_recency: w.temporal_recency,
375            content_type_prior: w.content_type_prior,
376            goal_utility: w.goal_utility,
377        };
378        let mut control = zeph_memory::AdmissionControl::new(
379            self.config.memory.admission.threshold,
380            self.config.memory.admission.fast_path_margin,
381            weights,
382        )
383        .with_provider(admission_provider);
384
385        if self.config.memory.admission.goal_conditioned_write {
386            let goal_provider = if self
387                .config
388                .memory
389                .admission
390                .goal_utility_provider
391                .is_empty()
392            {
393                None
394            } else {
395                match create_named_provider(
396                    &self.config.memory.admission.goal_utility_provider,
397                    &self.config,
398                ) {
399                    Ok(p) => Some(p),
400                    Err(e) => {
401                        tracing::warn!(
402                            provider = %self.config.memory.admission.goal_utility_provider,
403                            error = %e,
404                            "goal_utility_provider not found, LLM refinement disabled"
405                        );
406                        None
407                    }
408                }
409            };
410            control = control.with_goal_gate(zeph_memory::GoalGateConfig {
411                threshold: self.config.memory.admission.goal_utility_threshold,
412                provider: goal_provider,
413                weight: self.config.memory.admission.goal_utility_weight,
414            });
415            tracing::info!(
416                threshold = self.config.memory.admission.goal_utility_threshold,
417                weight = self.config.memory.admission.goal_utility_weight,
418                "A-MAC: goal-conditioned write gate enabled"
419            );
420        }
421
422        if self.config.memory.admission.admission_strategy == zeph_config::AdmissionStrategy::Rl {
423            tracing::warn!(
424                "admission_strategy = \"rl\" is configured but the RL model is not yet wired \
425                 into the admission path — falling back to heuristic. See #2416."
426            );
427        }
428
429        tracing::info!(
430            threshold = self.config.memory.admission.threshold,
431            "A-MAC admission control enabled"
432        );
433        control
434    }
435
436    pub async fn build_skill_matcher(
437        &self,
438        provider: &AnyProvider,
439        meta: &[&SkillMeta],
440        memory: &SemanticMemory,
441    ) -> Option<SkillMatcherBackend> {
442        let embed_model = self.embedding_model();
443        create_skill_matcher(
444            &self.config,
445            provider,
446            meta,
447            memory,
448            &embed_model,
449            self.qdrant_ops.as_ref(),
450        )
451        .await
452    }
453
454    pub fn build_registry(&self) -> SkillRegistry {
455        {
456            let managed = managed_skills_dir();
457            match zeph_skills::bundled::provision_bundled_skills(&managed) {
458                Ok(report) => {
459                    if !report.installed.is_empty() {
460                        tracing::info!(
461                            skills = ?report.installed,
462                            "provisioned new bundled skills"
463                        );
464                    }
465                    if !report.updated.is_empty() {
466                        tracing::info!(
467                            skills = ?report.updated,
468                            "updated bundled skills"
469                        );
470                    }
471                    for (name, err) in &report.failed {
472                        tracing::warn!(skill = %name, error = %err, "failed to provision bundled skill");
473                    }
474                }
475                Err(e) => {
476                    tracing::warn!(error = %e, "bundled skill provisioning failed");
477                }
478            }
479        }
480
481        let skill_paths = self.skill_paths();
482        let registry = SkillRegistry::load(&skill_paths);
483
484        if self.config.skills.trust.scan_on_load {
485            let findings = registry.scan_loaded();
486            if findings.is_empty() {
487                tracing::debug!("skill content scan: no injection patterns found");
488            } else {
489                tracing::warn!(
490                    count = findings.len(),
491                    "skill content scan complete: {} skill(s) with potential injection patterns",
492                    findings.len()
493                );
494            }
495        }
496
497        if self.config.skills.trust.scanner.capability_escalation_check {
498            // Build a trust-level mapping from all loaded skill metas.
499            // Skills without a trust record default to the configured default_level.
500            let default_level = self.config.skills.trust.default_level;
501            let trust_levels: Vec<(String, zeph_tools::SkillTrustLevel)> = registry
502                .all_meta()
503                .iter()
504                .map(|meta| (meta.name.clone(), default_level))
505                .collect();
506
507            let violations = registry.check_escalations(&trust_levels);
508            for v in &violations {
509                tracing::warn!(
510                    skill = %v.skill_name,
511                    denied_tools = ?v.denied_tools,
512                    "capability escalation: skill declares tools exceeding its trust level"
513                );
514            }
515            if violations.is_empty() {
516                tracing::debug!("capability escalation check: no violations found");
517            }
518        }
519
520        registry
521    }
522
523    pub fn skill_paths(&self) -> Vec<PathBuf> {
524        let mut paths: Vec<PathBuf> = self.config.skills.paths.iter().map(PathBuf::from).collect();
525        let managed_dir = managed_skills_dir();
526        if !paths.contains(&managed_dir) {
527            paths.push(managed_dir);
528        }
529        paths
530    }
531
532    pub fn managed_skills_dir() -> PathBuf {
533        managed_skills_dir()
534    }
535
536    pub fn build_watchers(&self) -> WatcherBundle {
537        let skill_paths = self.skill_paths();
538        let (reload_tx, skill_reload_rx) = mpsc::channel(4);
539        let skill_watcher = match SkillWatcher::start(&skill_paths, reload_tx) {
540            Ok(w) => {
541                tracing::info!("skill watcher started");
542                Some(w)
543            }
544            Err(e) => {
545                tracing::warn!("skill watcher unavailable: {e:#}");
546                None
547            }
548        };
549
550        let (config_reload_tx, config_reload_rx) = mpsc::channel(4);
551        let config_watcher = match ConfigWatcher::start(&self.config_path, config_reload_tx) {
552            Ok(w) => {
553                tracing::info!("config watcher started");
554                Some(w)
555            }
556            Err(e) => {
557                tracing::warn!("config watcher unavailable: {e:#}");
558                None
559            }
560        };
561
562        WatcherBundle {
563            skill_watcher,
564            skill_reload_rx,
565            config_watcher,
566            config_reload_rx,
567        }
568    }
569
570    pub fn build_shutdown() -> (watch::Sender<bool>, watch::Receiver<bool>) {
571        watch::channel(false)
572    }
573
574    pub fn embedding_model(&self) -> String {
575        effective_embedding_model(&self.config)
576    }
577
578    pub fn build_summary_provider(&self) -> Option<AnyProvider> {
579        // Structured config takes precedence over the string-based summary_model.
580        if let Some(ref entry) = self.config.llm.summary_provider {
581            return match build_provider_from_entry(entry, &self.config) {
582                Ok(sp) => {
583                    tracing::info!(
584                        provider_type = ?entry.provider_type,
585                        model = ?entry.model,
586                        "summary provider configured via [llm.summary_provider]"
587                    );
588                    Some(sp)
589                }
590                Err(e) => {
591                    tracing::warn!("failed to create summary provider: {e:#}, using primary");
592                    None
593                }
594            };
595        }
596        self.config.llm.summary_model.as_ref().and_then(
597            |model_spec| match create_summary_provider(model_spec, &self.config) {
598                Ok(sp) => {
599                    tracing::info!(model = %model_spec, "summary provider configured via llm.summary_model");
600                    Some(sp)
601                }
602                Err(e) => {
603                    tracing::warn!("failed to create summary provider: {e:#}, using primary");
604                    None
605                }
606            },
607        )
608    }
609
610    /// Build the quarantine summarizer provider when `security.content_isolation.quarantine.enabled = true`.
611    ///
612    /// Returns `None` when quarantine is disabled or provider resolution fails.
613    /// Emits a `tracing::warn` on resolution failure (quarantine silently disabled).
614    pub fn build_quarantine_provider(
615        &self,
616    ) -> Option<(AnyProvider, zeph_sanitizer::QuarantineConfig)> {
617        let ci = &self.config.security.content_isolation;
618        let qc = &ci.quarantine;
619        if !qc.enabled {
620            if ci.mcp_to_acp_boundary {
621                tracing::warn!(
622                    "mcp_to_acp_boundary is enabled but quarantine is disabled — \
623                     cross-boundary MCP tool results in ACP sessions will be \
624                     spotlighted but NOT quarantine-summarized; enable \
625                     [security.content_isolation.quarantine] for full protection"
626                );
627            }
628            return None;
629        }
630        match create_named_provider(&qc.model, &self.config) {
631            Ok(p) => {
632                tracing::info!(model = %qc.model, "quarantine provider configured");
633                Some((p, qc.clone()))
634            }
635            Err(e) => {
636                tracing::warn!(
637                    model = %qc.model,
638                    error = %e,
639                    "quarantine provider resolution failed, quarantine disabled"
640                );
641                None
642            }
643        }
644    }
645
646    /// Build the guardrail filter when `security.guardrail.enabled = true`.
647    ///
648    /// Returns `None` when guardrail is disabled or provider resolution fails.
649    /// Emits a `tracing::warn` on resolution failure (guardrail silently disabled).
650    pub fn build_guardrail_filter(&self) -> Option<zeph_sanitizer::guardrail::GuardrailFilter> {
651        let (provider, config) = self.build_guardrail_provider()?;
652        match zeph_sanitizer::guardrail::GuardrailFilter::new(provider, &config) {
653            Ok(filter) => Some(filter),
654            Err(e) => {
655                tracing::warn!(error = %e, "guardrail filter construction failed, guardrail disabled");
656                None
657            }
658        }
659    }
660
661    /// Build the guardrail provider and config pair for use in multi-session contexts.
662    ///
663    /// Returns `None` when guardrail is disabled or provider resolution fails.
664    pub fn build_guardrail_provider(
665        &self,
666    ) -> Option<(AnyProvider, zeph_sanitizer::guardrail::GuardrailConfig)> {
667        let gc = &self.config.security.guardrail;
668        if !gc.enabled {
669            return None;
670        }
671        let provider_name = gc.provider.as_deref().unwrap_or("ollama");
672        match create_named_provider(provider_name, &self.config) {
673            Ok(p) => {
674                tracing::info!(
675                    provider = %provider_name,
676                    model = ?gc.model,
677                    "guardrail provider configured"
678                );
679                Some((p, gc.clone()))
680            }
681            Err(e) => {
682                tracing::warn!(
683                    provider = %provider_name,
684                    error = %e,
685                    "guardrail provider resolution failed, guardrail disabled"
686                );
687                None
688            }
689        }
690    }
691
692    /// Build a dedicated provider for the judge detector when `detector_mode = judge`.
693    ///
694    /// Returns `None` when mode is `Regex` or `judge_model` is empty (primary provider used).
695    /// Emits a `tracing::warn` when mode is `Judge` but no model is specified.
696    pub fn build_judge_provider(&self) -> Option<AnyProvider> {
697        use crate::config::DetectorMode;
698        let learning = &self.config.skills.learning;
699        if learning.detector_mode != DetectorMode::Judge {
700            return None;
701        }
702        if learning.judge_model.is_empty() {
703            tracing::warn!(
704                "detector_mode=judge but judge_model is empty — primary provider will be used for judging"
705            );
706            return None;
707        }
708        match create_named_provider(&learning.judge_model, &self.config) {
709            Ok(jp) => {
710                tracing::info!(model = %learning.judge_model, "judge provider configured");
711                Some(jp)
712            }
713            Err(e) => {
714                tracing::warn!("failed to create judge provider: {e:#}, using primary");
715                None
716            }
717        }
718    }
719
720    /// Build an `LlmClassifier` for `detector_mode = "model"` feedback detection.
721    ///
722    /// Resolves `feedback_provider` from `[[llm.providers]]` registry.
723    /// Pass the session's primary provider as `primary` for fallback when `feedback_provider`
724    /// is empty. Returns `None` with a warning on resolution failure — never fails startup.
725    pub fn build_feedback_classifier(
726        &self,
727        primary: &AnyProvider,
728    ) -> Option<zeph_llm::classifier::llm::LlmClassifier> {
729        use crate::config::DetectorMode;
730        let learning = &self.config.skills.learning;
731        if learning.detector_mode != DetectorMode::Model {
732            return None;
733        }
734        let provider = if learning.feedback_provider.is_empty() {
735            tracing::debug!("feedback_provider empty — using primary provider for LlmClassifier");
736            Some(primary.clone())
737        } else {
738            match crate::bootstrap::provider::create_named_provider(
739                &learning.feedback_provider,
740                &self.config,
741            ) {
742                Ok(p) => {
743                    tracing::info!(
744                        provider = %learning.feedback_provider,
745                        "LlmClassifier feedback provider configured"
746                    );
747                    Some(p)
748                }
749                Err(e) => {
750                    tracing::warn!(
751                        provider = %learning.feedback_provider,
752                        error = %e,
753                        "feedback_provider not found in registry, degrading to regex-only"
754                    );
755                    None
756                }
757            }
758        };
759        if let Some(p) = provider {
760            Some(zeph_llm::classifier::llm::LlmClassifier::new(
761                std::sync::Arc::new(p),
762            ))
763        } else {
764            tracing::warn!(
765                "detector_mode=model but no provider available, degrading to regex-only"
766            );
767            None
768        }
769    }
770
771    /// Build a dedicated provider for compaction probe LLM calls.
772    ///
773    /// Returns `None` when `probe_provider` is empty (falls back to summary provider at call site).
774    /// Emits a `tracing::warn` on resolution failure (summary/primary provider used as fallback).
775    pub fn build_probe_provider(&self) -> Option<AnyProvider> {
776        let name = &self.config.memory.compression.probe.probe_provider;
777        if name.is_empty() {
778            return None;
779        }
780        match create_named_provider(name, &self.config) {
781            Ok(p) => {
782                tracing::info!(provider = %name, "compaction probe provider configured");
783                Some(p)
784            }
785            Err(e) => {
786                tracing::warn!(
787                    provider = %name,
788                    error = %e,
789                    "probe provider resolution failed — summary/primary provider will be used"
790                );
791                None
792            }
793        }
794    }
795
796    /// Build a dedicated provider for `compress_context` LLM calls (#2356).
797    ///
798    /// Returns `None` when `compress_provider` is empty (falls back to primary provider at call site).
799    /// Emits a `tracing::warn` on resolution failure (primary provider used as fallback).
800    pub fn build_compress_provider(&self) -> Option<AnyProvider> {
801        let name = &self.config.memory.compression.compress_provider;
802        if name.is_empty() {
803            return None;
804        }
805        match create_named_provider(name, &self.config) {
806            Ok(p) => {
807                tracing::info!(provider = %name, "compress_context provider configured");
808                Some(p)
809            }
810            Err(e) => {
811                tracing::warn!(
812                    provider = %name,
813                    error = %e,
814                    "compress_context provider resolution failed — primary provider will be used"
815                );
816                None
817            }
818        }
819    }
820
821    /// Build a dedicated provider for ACON compression guidelines LLM calls.
822    ///
823    /// Returns `None` when `guidelines_provider` is empty (falls back to primary provider at call site).
824    ///
825    /// # Errors (logged, not propagated)
826    ///
827    /// Emits a `tracing::warn` on resolution failure; primary provider is used as fallback.
828    pub fn build_guidelines_provider(&self) -> Option<AnyProvider> {
829        let name = &self
830            .config
831            .memory
832            .compression_guidelines
833            .guidelines_provider;
834        if name.is_empty() {
835            return None;
836        }
837        match create_named_provider(name, &self.config) {
838            Ok(p) => {
839                tracing::info!(provider = %name, "compression guidelines provider configured");
840                Some(p)
841            }
842            Err(e) => {
843                tracing::warn!(
844                    provider = %name,
845                    error = %e,
846                    "guidelines provider resolution failed — primary provider will be used"
847                );
848                None
849            }
850        }
851    }
852
853    /// Build a dedicated provider for All-Mem consolidation LLM calls.
854    ///
855    /// Returns `None` when `consolidation_provider` is empty (falls back to primary provider at
856    /// call site) or when provider resolution fails (logs a warning, fails open).
857    pub fn build_consolidation_provider(&self) -> Option<AnyProvider> {
858        let name = &self.config.memory.consolidation.consolidation_provider;
859        if name.is_empty() {
860            return None;
861        }
862        match create_named_provider(name, &self.config) {
863            Ok(p) => {
864                tracing::info!(provider = %name, "consolidation provider configured");
865                Some(p)
866            }
867            Err(e) => {
868                tracing::warn!(
869                    provider = %name,
870                    error = %e,
871                    "consolidation provider resolution failed — primary provider will be used"
872                );
873                None
874            }
875        }
876    }
877
878    /// Build a dedicated provider for orchestration planner LLM calls.
879    ///
880    /// Returns `None` when `planner_provider` is empty (falls back to primary provider at call site).
881    ///
882    /// # Errors (logged, not propagated)
883    ///
884    /// Emits a `tracing::warn` on resolution failure; primary provider is used as fallback.
885    pub fn build_planner_provider(&self) -> Option<AnyProvider> {
886        let name = &self.config.orchestration.planner_provider;
887        if name.is_empty() {
888            return None;
889        }
890        match create_named_provider(name, &self.config) {
891            Ok(p) => {
892                tracing::info!(provider = %name, "planner provider configured");
893                Some(p)
894            }
895            Err(e) => {
896                tracing::warn!(
897                    provider = %name,
898                    error = %e,
899                    "planner provider resolution failed — primary provider will be used"
900                );
901                None
902            }
903        }
904    }
905
906    /// Build the `PlanVerifier` provider from `[orchestration] verify_provider`.
907    ///
908    /// Returns `None` when `verify_provider` is empty (falls back to the primary provider at
909    /// runtime) or when provider resolution fails (logs a warning, fails open).
910    pub fn build_verify_provider(&self) -> Option<AnyProvider> {
911        let name = &self.config.orchestration.verify_provider;
912        if name.is_empty() {
913            return None;
914        }
915        match create_named_provider(name, &self.config) {
916            Ok(p) => {
917                tracing::info!(provider = %name, "verify provider configured");
918                Some(p)
919            }
920            Err(e) => {
921                tracing::warn!(
922                    provider = %name,
923                    error = %e,
924                    "verify provider resolution failed — primary provider will be used"
925                );
926                None
927            }
928        }
929    }
930    pub fn build_eval_provider(&self) -> Option<AnyProvider> {
931        let model_spec = self.config.experiments.eval_model.as_deref()?;
932        match create_summary_provider(model_spec, &self.config) {
933            Ok(p) => {
934                tracing::info!(eval_model = %model_spec, "experiment eval provider configured");
935                Some(p)
936            }
937            Err(e) => {
938                tracing::warn!(
939                    eval_model = %model_spec,
940                    error = %e,
941                    "failed to create eval provider — primary provider will be used as judge"
942                );
943                None
944            }
945        }
946    }
947
948    /// Build a dedicated provider for `MemScene` label/profile LLM generation.
949    ///
950    /// Returns `None` when `tiers.scene_provider` is empty (caller falls back to primary provider).
951    /// Emits a `tracing::warn` on resolution failure; primary provider is used as fallback.
952    pub fn build_scene_provider(&self) -> Option<AnyProvider> {
953        let name = &self.config.memory.tiers.scene_provider;
954        if name.is_empty() {
955            return None;
956        }
957        match create_named_provider(name, &self.config) {
958            Ok(p) => {
959                tracing::info!(provider = %name, "scene consolidation provider configured");
960                Some(p)
961            }
962            Err(e) => {
963                tracing::warn!(
964                    provider = %name,
965                    error = %e,
966                    "scene provider resolution failed — primary provider will be used"
967                );
968                None
969            }
970        }
971    }
972}
973
974#[cfg(test)]
975mod tests;