#[derive(Debug, Clone, serde::Serialize)]
pub struct HealthReport {
pub initialized: bool,
pub kernel_enabled: bool,
pub dedup_hit_rate: f64,
pub dedup_total_checks: usize,
pub schema_optimizations: usize,
pub schema_tokens_saved: usize,
pub evidence_total_envelopes: usize,
pub evidence_chain_entries: usize,
pub config_source: String,
pub subsystem_count: usize,
}
#[must_use]
pub fn kernel_health() -> HealthReport {
let startup_status = super::startup::status();
let initialized = super::startup::is_initialized() && startup_status.initialized;
let configured_features = super::kernel_config::features();
let kernel_enabled = super::kernel_config::is_enabled()
&& startup_status.kernel_enabled
&& configured_features.enabled;
let dedup = super::dedup_wiring::dedup_stats();
let schema = super::schema_wiring::schema_savings();
let evidence = super::envelope_wiring::evidence_summary();
let (effective_features, config_source) = super::config_bridge::effective_config();
HealthReport {
initialized,
kernel_enabled: kernel_enabled && effective_features.enabled,
dedup_hit_rate: dedup.hit_rate,
dedup_total_checks: dedup.total_checks,
schema_optimizations: schema.optimizations_applied,
schema_tokens_saved: schema.total_tokens_saved,
evidence_total_envelopes: evidence.total_envelopes,
evidence_chain_entries: evidence.chain_entries,
config_source: format!("{config_source:?}"),
subsystem_count: subsystem_names().len(),
}
}
#[must_use]
pub fn is_healthy() -> bool {
let report = kernel_health();
report.initialized && report.kernel_enabled
}
#[must_use]
pub fn format_health() -> String {
let report = kernel_health();
let state = if report.kernel_enabled { "ON" } else { "OFF" };
format!(
"Kernel: {state} | Dedup: {:.0}% hit | Schema: {} opts, {} tok saved | Evidence: {} entries",
report.dedup_hit_rate * 100.0,
report.schema_optimizations,
report.schema_tokens_saved,
report.evidence_chain_entries,
)
}
#[must_use]
pub fn subsystem_names() -> &'static [&'static str] {
&[
"startup",
"kernel_config",
"dedup",
"schema",
"evidence",
"config_bridge",
]
}
#[cfg(test)]
pub mod tests {
use super::{format_health, is_healthy, kernel_health};
use crate::core::context_kernel::{
dedup_wiring, envelope_wiring, kernel_config, schema_wiring, startup,
};
fn isolated() -> std::sync::MutexGuard<'static, ()> {
let guard = kernel_config::KERNEL_TEST_LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
kernel_config::reset_features();
startup::reset();
dedup_wiring::reset_dedup();
schema_wiring::reset_schema_state();
envelope_wiring::reset_evidence();
guard
}
#[test]
fn health_after_init() {
let _guard = isolated();
startup::initialize();
assert!(kernel_health().initialized);
}
#[test]
fn health_shows_dedup_stats() {
let _guard = isolated();
let _ = dedup_wiring::check_content("health.rs", "same", false);
let _ = dedup_wiring::check_content("health.rs", "same", false);
let report = kernel_health();
assert!(report.dedup_hit_rate > 0.0);
assert_eq!(report.dedup_total_checks, 2);
}
#[test]
fn healthy_when_enabled() {
let _guard = isolated();
startup::initialize();
assert!(is_healthy());
}
#[test]
fn format_readable() {
let _guard = isolated();
startup::initialize();
assert!(format_health().contains("Kernel:"));
}
}