Skip to main content

lean_ctx/core/context_kernel/
health.rs

1//! Aggregated Context Kernel health reporting.
2
3/// Snapshot of health and activity across Context Kernel subsystems.
4#[derive(Debug, Clone, serde::Serialize)]
5pub struct HealthReport {
6    /// Whether kernel startup initialization completed.
7    pub initialized: bool,
8    /// Whether the kernel master switch is enabled.
9    pub kernel_enabled: bool,
10    /// Fraction of deduplication checks that were cache hits.
11    pub dedup_hit_rate: f64,
12    /// Number of deduplication checks performed.
13    pub dedup_total_checks: usize,
14    /// Number of schema optimizations applied.
15    pub schema_optimizations: usize,
16    /// Estimated tokens saved by schema optimization.
17    pub schema_tokens_saved: usize,
18    /// Number of canonical evidence envelopes recorded.
19    pub evidence_total_envelopes: usize,
20    /// Number of evidence receipt-chain entries recorded.
21    pub evidence_chain_entries: usize,
22    /// Source of the effective kernel configuration.
23    pub config_source: String,
24    /// Number of subsystems represented by this report.
25    pub subsystem_count: usize,
26}
27
28/// Returns an aggregated snapshot of Context Kernel health and activity.
29#[must_use]
30pub fn kernel_health() -> HealthReport {
31    let startup_status = super::startup::status();
32    let initialized = super::startup::is_initialized() && startup_status.initialized;
33    let configured_features = super::kernel_config::features();
34    let kernel_enabled = super::kernel_config::is_enabled()
35        && startup_status.kernel_enabled
36        && configured_features.enabled;
37    let dedup = super::dedup_wiring::dedup_stats();
38    let schema = super::schema_wiring::schema_savings();
39    let evidence = super::envelope_wiring::evidence_summary();
40    let (effective_features, config_source) = super::config_bridge::effective_config();
41
42    HealthReport {
43        initialized,
44        kernel_enabled: kernel_enabled && effective_features.enabled,
45        dedup_hit_rate: dedup.hit_rate,
46        dedup_total_checks: dedup.total_checks,
47        schema_optimizations: schema.optimizations_applied,
48        schema_tokens_saved: schema.total_tokens_saved,
49        evidence_total_envelopes: evidence.total_envelopes,
50        evidence_chain_entries: evidence.chain_entries,
51        config_source: format!("{config_source:?}"),
52        subsystem_count: subsystem_names().len(),
53    }
54}
55
56/// Returns whether the initialized Context Kernel is enabled and error-free.
57#[must_use]
58pub fn is_healthy() -> bool {
59    let report = kernel_health();
60    report.initialized && report.kernel_enabled
61}
62
63/// Formats a one-line human-readable Context Kernel health summary.
64#[must_use]
65pub fn format_health() -> String {
66    let report = kernel_health();
67    let state = if report.kernel_enabled { "ON" } else { "OFF" };
68    format!(
69        "Kernel: {state} | Dedup: {:.0}% hit | Schema: {} opts, {} tok saved | Evidence: {} entries",
70        report.dedup_hit_rate * 100.0,
71        report.schema_optimizations,
72        report.schema_tokens_saved,
73        report.evidence_chain_entries,
74    )
75}
76
77/// Returns the names of subsystems represented by [`HealthReport`].
78#[must_use]
79pub fn subsystem_names() -> &'static [&'static str] {
80    &[
81        "startup",
82        "kernel_config",
83        "dedup",
84        "schema",
85        "evidence",
86        "config_bridge",
87    ]
88}
89
90#[cfg(test)]
91mod tests {
92    use super::{format_health, is_healthy, kernel_health};
93    use crate::core::context_kernel::{
94        dedup_wiring, envelope_wiring, kernel_config, schema_wiring, startup,
95    };
96
97    fn isolated() -> std::sync::MutexGuard<'static, ()> {
98        let guard = kernel_config::KERNEL_TEST_LOCK
99            .lock()
100            .unwrap_or_else(std::sync::PoisonError::into_inner);
101        kernel_config::reset_features();
102        startup::reset();
103        dedup_wiring::reset_dedup();
104        schema_wiring::reset_schema_state();
105        envelope_wiring::reset_evidence();
106        guard
107    }
108
109    #[test]
110    fn health_after_init() {
111        let _guard = isolated();
112        startup::initialize();
113        assert!(kernel_health().initialized);
114    }
115
116    #[test]
117    fn health_shows_dedup_stats() {
118        let _guard = isolated();
119        let _ = dedup_wiring::check_content("health.rs", "same");
120        let _ = dedup_wiring::check_content("health.rs", "same");
121        let report = kernel_health();
122        assert!(report.dedup_hit_rate > 0.0);
123        assert_eq!(report.dedup_total_checks, 2);
124    }
125
126    #[test]
127    fn healthy_when_enabled() {
128        let _guard = isolated();
129        startup::initialize();
130        assert!(is_healthy());
131    }
132
133    #[test]
134    fn format_readable() {
135        let _guard = isolated();
136        startup::initialize();
137        assert!(format_health().contains("Kernel:"));
138    }
139}