spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / tests / test_objective_evaluator.rs
| 1 | use spark_rsi::actor::{BlindJudge, HoldoutSuite}; |
| 2 | use spark_rsi::evaluator::layers::{ |
| 3 | CorrectnessLayer, DefectTestResult, LongitudinalReplayLayer, PerformanceLayer, |
| 4 | ResourceEfficiencyLayer, SecurityLayer, StyleLayer, |
| 5 | }; |
| 6 | use spark_rsi::evaluator::metrics::{ |
| 7 | LatencyDistribution, LatencyTimer, ProcessMetricsSnapshot, RusageMetrics, StatmMetrics, |
| 8 | }; |
| 9 | use spark_rsi::evaluator::stats::StatisticalEngine; |
| 10 | use spark_rsi::evaluator::{EvaluationReceipt, ObjectiveEvaluator}; |
| 11 | use std::fs; |
| 12 | |
| 13 | #[test] |
| 14 | fn test_rusage_and_statm_accounting() { |
| 15 | let rusage = RusageMetrics::capture_self().expect("Failed to capture self rusage"); |
| 16 | assert!(rusage.max_rss_kb > 0); |
| 17 | |
| 18 | let statm = StatmMetrics::read_self().expect("Failed to read self statm"); |
| 19 | assert!(statm.resident_pages > 0); |
| 20 | assert!(statm.resident_kb > 0); |
| 21 | |
| 22 | let snapshot = ProcessMetricsSnapshot::capture_current().expect("Failed to capture snapshot"); |
| 23 | assert!(snapshot.rusage.max_rss_kb > 0); |
| 24 | assert!(snapshot.statm.is_some()); |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn test_latency_timer_and_distribution() { |
| 29 | let timer = LatencyTimer::start(); |
| 30 | std::thread::sleep(std::time::Duration::from_millis(5)); |
| 31 | let elapsed = timer.elapsed_us(); |
| 32 | assert!(elapsed >= 4000.0); |
| 33 | |
| 34 | let samples: Vec<f64> = (1..=100).map(|i| (i as f64) * 10.0).collect(); |
| 35 | let dist = LatencyDistribution::from_samples(&samples, 100_000).expect("Failed distribution"); |
| 36 | assert_eq!(dist.count, 100); |
| 37 | assert!((dist.p50_us - 505.0).abs() < 1e-4); |
| 38 | assert!(dist.p95_us > 900.0); |
| 39 | assert!(dist.p99_us > 950.0); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn test_paired_bootstrap_and_tail_non_inferiority_matrix() { |
| 44 | // 30 iterations paired workload |
| 45 | let mut parent = Vec::with_capacity(30); |
| 46 | let mut candidate = Vec::with_capacity(30); |
| 47 | for i in 0..30 { |
| 48 | let base = 50.0 + (i as f64) * 1.2; |
| 49 | parent.push(base); |
| 50 | candidate.push(base * 0.85); // 15% improvement |
| 51 | } |
| 52 | |
| 53 | let boot = |
| 54 | StatisticalEngine::bootstrap_paired_comparison(&parent, &candidate, 10_000, Some(42)) |
| 55 | .expect("Bootstrap failed"); |
| 56 | assert!(boot.is_statistically_significant); |
| 57 | assert!(boot.p_value < 0.01); |
| 58 | assert!(boot.delta_pct < -10.0); |
| 59 | |
| 60 | // Tail percentile non-inferiority |
| 61 | let tail_res = StatisticalEngine::evaluate_tail_non_inferiority( |
| 62 | &parent, |
| 63 | &candidate, |
| 64 | 95.0, |
| 65 | 1.0, |
| 66 | 10_000, |
| 67 | Some(42), |
| 68 | ) |
| 69 | .expect("Tail non-inferiority failed"); |
| 70 | assert!(tail_res.passes_non_inferiority); |
| 71 | assert!(tail_res.ci_95_upper_pct <= 1.0); |
| 72 | } |
| 73 | |
| 74 | #[test] |
| 75 | fn test_fishers_exact_contingency_matrix() { |
| 76 | // Known 2x2 contingency table |
| 77 | let res = |
| 78 | StatisticalEngine::fishers_exact_test(2, 18, 15, 5).expect("Fisher exact test failed"); |
| 79 | assert!(res.p_value_two_sided < 0.01); |
| 80 | assert!(res.is_significant); |
| 81 | assert!(res.candidate_rate > res.parent_rate); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn test_six_layer_evaluator_end_to_end() { |
| 86 | let correctness = CorrectnessLayer::evaluate_synthetic(true, 50, 0, 12, 0, true); |
| 87 | assert!(correctness.passed); |
| 88 | |
| 89 | let security = SecurityLayer::evaluate_candidate( |
| 90 | &["src/observe.rs".to_string()], |
| 91 | "+ let cache_size = 1024;", |
| 92 | 0, |
| 93 | ); |
| 94 | assert!(security.passed); |
| 95 | |
| 96 | let style = |
| 97 | StyleLayer::evaluate_text("Direct systems programming in Rust. Zero unslop violations."); |
| 98 | assert!(style.passed); |
| 99 | |
| 100 | let parent_lat: Vec<f64> = (0..30).map(|i| 100.0 + (i as f64) * 0.5).collect(); |
| 101 | let cand_lat: Vec<f64> = (0..30).map(|i| 82.0 + (i as f64) * 0.4).collect(); |
| 102 | let performance = |
| 103 | PerformanceLayer::evaluate_latencies(&parent_lat, &cand_lat, true, 5000, Some(42)) |
| 104 | .expect("Performance eval failed"); |
| 105 | assert!(performance.passed); |
| 106 | |
| 107 | let p_ru = RusageMetrics { |
| 108 | user_time_us: 5000, |
| 109 | system_time_us: 2000, |
| 110 | max_rss_kb: 40_000, |
| 111 | voluntary_context_switches: 50, |
| 112 | involuntary_context_switches: 10, |
| 113 | }; |
| 114 | let c_ru = RusageMetrics { |
| 115 | user_time_us: 4200, |
| 116 | system_time_us: 1800, |
| 117 | max_rss_kb: 40_300, // 0.75% growth |
| 118 | voluntary_context_switches: 45, |
| 119 | involuntary_context_switches: 8, |
| 120 | }; |
| 121 | let resource_eff = ResourceEfficiencyLayer::evaluate(&p_ru, &c_ru, None, Some(49_152)); |
| 122 | assert!(resource_eff.passed); |
| 123 | |
| 124 | let longitudinal = LongitudinalReplayLayer::evaluate_results(&[ |
| 125 | DefectTestResult { |
| 126 | defect_id: "ROT-01".to_string(), |
| 127 | title: "Root of trust".to_string(), |
| 128 | passed: true, |
| 129 | details: None, |
| 130 | }, |
| 131 | DefectTestResult { |
| 132 | defect_id: "SEC-01".to_string(), |
| 133 | title: "Zero secrets".to_string(), |
| 134 | passed: true, |
| 135 | details: None, |
| 136 | }, |
| 137 | ]); |
| 138 | assert!(longitudinal.passed); |
| 139 | |
| 140 | let receipt = ObjectiveEvaluator::evaluate_candidate( |
| 141 | "cycle-integration-01", |
| 142 | "cand-integration-01", |
| 143 | "parent-integration-00", |
| 144 | correctness, |
| 145 | security, |
| 146 | style, |
| 147 | performance, |
| 148 | resource_eff, |
| 149 | longitudinal, |
| 150 | ); |
| 151 | |
| 152 | assert!(receipt.admitted); |
| 153 | assert!(receipt.passed_all_hard_invariants); |
| 154 | assert!(receipt.passed_statistical_gates); |
| 155 | assert!(receipt.verify_digest()); |
| 156 | assert_eq!(receipt.layer_results.len(), 6); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn test_blind_judge_receipt_roundtrip_and_persistence() { |
| 161 | let tmp = tempfile::tempdir().unwrap(); |
| 162 | let holdouts = tmp.path().join("holdouts"); |
| 163 | let outputs = tmp.path().join("eval_outputs"); |
| 164 | let candidate = tmp.path().join("cand"); |
| 165 | fs::create_dir_all(&candidate).unwrap(); |
| 166 | let parent = tmp.path().join("parent"); |
| 167 | fs::create_dir_all(&parent).unwrap(); |
| 168 | |
| 169 | let exe = spark_rsi::actor::judge::find_executable(std::path::Path::new(".")) |
| 170 | .expect("spark-rsi executable must exist"); |
| 171 | fs::copy(&exe, candidate.join("spark-rsi")).unwrap(); |
| 172 | fs::copy(&exe, parent.join("spark-rsi")).unwrap(); |
| 173 | |
| 174 | let suites = HoldoutSuite::builtin_suites(); |
| 175 | for suite in suites { |
| 176 | suite.save_to_dir(&holdouts).unwrap(); |
| 177 | } |
| 178 | |
| 179 | let signing_key = p256::ecdsa::SigningKey::from_bytes(&[88u8; 32].into()).unwrap(); |
| 180 | let verifying_key = p256::ecdsa::VerifyingKey::from(&signing_key); |
| 181 | let judge = BlindJudge::new(holdouts, outputs.clone()) |
| 182 | .with_signing_key(signing_key) |
| 183 | .with_non_inferiority_margin(1000.0); |
| 184 | let receipt = judge |
| 185 | .evaluate_cycle( |
| 186 | "cycle-persisted-99", |
| 187 | "cand-99", |
| 188 | "parent-98", |
| 189 | &candidate, |
| 190 | &parent, |
| 191 | ) |
| 192 | .expect("BlindJudge failed"); |
| 193 | |
| 194 | assert!(receipt.admitted); |
| 195 | assert!(receipt.verify_digest()); |
| 196 | assert!(receipt.signature.is_some()); |
| 197 | assert!(receipt.verify_signature(&verifying_key)); |
| 198 | |
| 199 | let target_file = outputs.join("cycle-persisted-99.json"); |
| 200 | assert!(target_file.exists()); |
| 201 | |
| 202 | let loaded = EvaluationReceipt::load_from_file(&target_file).expect("Failed to load receipt"); |
| 203 | assert_eq!(loaded.cycle_id, "cycle-persisted-99"); |
| 204 | assert!(loaded.verify_digest()); |
| 205 | assert_eq!(loaded.layer_results.len(), 6); |
| 206 | } |