spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / tests / test_meta_improvement_governance.rs
| 1 | use p256::ecdsa::signature::Signer; |
| 2 | use p256::ecdsa::{Signature, SigningKey}; |
| 3 | use sha2::{Digest, Sha256}; |
| 4 | use spark_rsi::ledger::{BlockType, ImprovementLedger}; |
| 5 | use spark_rsi::meta::{ |
| 6 | AbForkEvaluator, CandidateTier, MetaBenchmarkMetrics, TierGovernance, TrueRsiEvaluator, |
| 7 | }; |
| 8 | use spark_rsi::models::{ImprovementProposal, ProposalKind}; |
| 9 | use std::fs; |
| 10 | use std::path::Path; |
| 11 | |
| 12 | #[test] |
| 13 | fn test_tier_classification_and_immutable_containment() { |
| 14 | let target_proposal = ImprovementProposal { |
| 15 | id: "prop-target-01".to_string(), |
| 16 | title: "Clean documentation".to_string(), |
| 17 | description: "Remove unslop from docs".to_string(), |
| 18 | target_file: "README.md".to_string(), |
| 19 | proposed_patch: "# Cleaned".to_string(), |
| 20 | kind: ProposalKind::UnslopSanitization, |
| 21 | created_at: "2026-09-19T00:00:00Z".to_string(), |
| 22 | sandbox_path: None, |
| 23 | operator_signature: None, |
| 24 | }; |
| 25 | assert_eq!( |
| 26 | TierGovernance::classify_proposal(&target_proposal), |
| 27 | CandidateTier::Tier1Target |
| 28 | ); |
| 29 | |
| 30 | let engine_proposal = ImprovementProposal { |
| 31 | id: "prop-engine-01".to_string(), |
| 32 | title: "Optimize KV scheduler in propose subsystem".to_string(), |
| 33 | description: "Speed up proposal generation".to_string(), |
| 34 | target_file: "src/propose/mod.rs".to_string(), |
| 35 | proposed_patch: "pub fn fast_gen() {}".to_string(), |
| 36 | kind: ProposalKind::Optimization, |
| 37 | created_at: "2026-09-19T00:00:00Z".to_string(), |
| 38 | sandbox_path: None, |
| 39 | operator_signature: None, |
| 40 | }; |
| 41 | assert_eq!( |
| 42 | TierGovernance::classify_proposal(&engine_proposal), |
| 43 | CandidateTier::Tier2Engine |
| 44 | ); |
| 45 | |
| 46 | // Verify immutable containment boundaries for Tier 2 |
| 47 | assert!(TierGovernance::validate_tier2_candidate_boundaries("src/propose/mod.rs").is_ok()); |
| 48 | assert!(TierGovernance::validate_tier2_candidate_boundaries("src/daemon.rs").is_ok()); |
| 49 | assert!(TierGovernance::validate_tier2_candidate_boundaries("src/graph/mod.rs").is_ok()); |
| 50 | |
| 51 | // Attempts to modify tests, scoring rules, containment, or holdouts must fail closed |
| 52 | assert!( |
| 53 | TierGovernance::validate_tier2_candidate_boundaries("tests/integration_tests.rs").is_err() |
| 54 | ); |
| 55 | assert!(TierGovernance::validate_tier2_candidate_boundaries( |
| 56 | "src/evaluator/layers/correctness.rs" |
| 57 | ) |
| 58 | .is_err()); |
| 59 | assert!( |
| 60 | TierGovernance::validate_tier2_candidate_boundaries("src/isolation/container.rs").is_err() |
| 61 | ); |
| 62 | assert!( |
| 63 | TierGovernance::validate_tier2_candidate_boundaries(".rsi/holdouts/suite1.json").is_err() |
| 64 | ); |
| 65 | assert!(TierGovernance::validate_tier2_candidate_boundaries("CONSTITUTION.md").is_err()); |
| 66 | assert!(TierGovernance::validate_tier2_candidate_boundaries("LICENSE").is_err()); |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn test_operator_cryptographic_authorization_requirement() { |
| 71 | let operator_signing_key = SigningKey::from_bytes(&[55u8; 32].into()).unwrap(); |
| 72 | let operator_verifying_key = operator_signing_key.verifying_key(); |
| 73 | |
| 74 | let patch = "pub fn autonomous_engine_upgrade() {}"; |
| 75 | let patch_digest = Sha256::digest(patch.as_bytes()); |
| 76 | let sig: Signature = operator_signing_key.sign(&patch_digest); |
| 77 | let valid_sig_hex = hex::encode(sig.to_bytes()); |
| 78 | |
| 79 | let authorized_proposal = ImprovementProposal { |
| 80 | id: "meta-auth-01".to_string(), |
| 81 | title: "Autonomous Engine Upgrade".to_string(), |
| 82 | description: "Self-improvement patch signed by hardware TPM vault".to_string(), |
| 83 | target_file: "src/observe.rs".to_string(), |
| 84 | proposed_patch: patch.to_string(), |
| 85 | kind: ProposalKind::MetaEngineImprovement, |
| 86 | created_at: "2026-09-19T00:00:00Z".to_string(), |
| 87 | sandbox_path: None, |
| 88 | operator_signature: Some(valid_sig_hex), |
| 89 | }; |
| 90 | |
| 91 | let parent_metrics = MetaBenchmarkMetrics::new(10.0, 0.85, 1.0, 400.0, 4096); |
| 92 | let candidate_metrics = MetaBenchmarkMetrics::new(12.5, 0.90, 1.25, 360.0, 4096); |
| 93 | |
| 94 | let res = AbForkEvaluator::evaluate_tier2_candidate( |
| 95 | Path::new("."), |
| 96 | Path::new("."), |
| 97 | &authorized_proposal, |
| 98 | &parent_metrics, |
| 99 | &candidate_metrics, |
| 100 | Some(&operator_verifying_key), |
| 101 | 1.0, |
| 102 | None, |
| 103 | ); |
| 104 | assert!(res.is_ok(), "Authorized Tier 2 proposal must succeed"); |
| 105 | |
| 106 | // Missing signature must fail closed |
| 107 | let mut unsigned_proposal = authorized_proposal.clone(); |
| 108 | unsigned_proposal.operator_signature = None; |
| 109 | let err_unsigned = AbForkEvaluator::evaluate_tier2_candidate( |
| 110 | Path::new("."), |
| 111 | Path::new("."), |
| 112 | &unsigned_proposal, |
| 113 | &parent_metrics, |
| 114 | &candidate_metrics, |
| 115 | Some(&operator_verifying_key), |
| 116 | 1.0, |
| 117 | None, |
| 118 | ); |
| 119 | assert!(err_unsigned.is_err()); |
| 120 | assert!(err_unsigned |
| 121 | .unwrap_err() |
| 122 | .contains("Missing operator cryptographic signature")); |
| 123 | |
| 124 | // Forged signature must fail closed |
| 125 | let mut forged_proposal = authorized_proposal.clone(); |
| 126 | forged_proposal.operator_signature = Some("00".repeat(64)); |
| 127 | let err_forged = AbForkEvaluator::evaluate_tier2_candidate( |
| 128 | Path::new("."), |
| 129 | Path::new("."), |
| 130 | &forged_proposal, |
| 131 | &parent_metrics, |
| 132 | &candidate_metrics, |
| 133 | Some(&operator_verifying_key), |
| 134 | 1.0, |
| 135 | None, |
| 136 | ); |
| 137 | assert!(err_forged.is_err()); |
| 138 | assert!(err_forged.is_err()); |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn test_ab_fork_evaluation_engine_n_judges_engine_n_plus_1() { |
| 143 | let operator_signing_key = SigningKey::from_bytes(&[99u8; 32].into()).unwrap(); |
| 144 | let operator_verifying_key = operator_signing_key.verifying_key(); |
| 145 | |
| 146 | let patch = "pub fn speedup() {}"; |
| 147 | let patch_digest = Sha256::digest(patch.as_bytes()); |
| 148 | let sig: Signature = operator_signing_key.sign(&patch_digest); |
| 149 | |
| 150 | let proposal = ImprovementProposal { |
| 151 | id: "meta-ab-01".to_string(), |
| 152 | title: "Speedup Proposal".to_string(), |
| 153 | description: "Dynamic telemetry bottleneck repair".to_string(), |
| 154 | target_file: "src/propose/mod.rs".to_string(), |
| 155 | proposed_patch: patch.to_string(), |
| 156 | kind: ProposalKind::MetaEngineImprovement, |
| 157 | created_at: "2026-09-19T00:00:00Z".to_string(), |
| 158 | sandbox_path: None, |
| 159 | operator_signature: Some(hex::encode(sig.to_bytes())), |
| 160 | }; |
| 161 | |
| 162 | let parent_metrics = MetaBenchmarkMetrics::new(10.0, 0.80, 1.0, 500.0, 4096); |
| 163 | |
| 164 | // Scenario A: Candidate regresses on latency by 20% -> Rejected |
| 165 | let regressed_candidate = MetaBenchmarkMetrics::new(10.0, 0.80, 1.0, 600.0, 4096); |
| 166 | let res_regressed = AbForkEvaluator::evaluate_tier2_candidate( |
| 167 | Path::new("."), |
| 168 | Path::new("."), |
| 169 | &proposal, |
| 170 | &parent_metrics, |
| 171 | ®ressed_candidate, |
| 172 | Some(&operator_verifying_key), |
| 173 | 1.0, |
| 174 | None, |
| 175 | ) |
| 176 | .unwrap(); |
| 177 | assert_eq!(res_regressed.overall_classification, "REJECTED"); |
| 178 | assert!(!res_regressed.self_capability_improvement.passed); |
| 179 | |
| 180 | // Scenario B: Candidate accelerates discovery rate by 30% -> MetaCandidate |
| 181 | let amplified_candidate = MetaBenchmarkMetrics::new(13.0, 0.85, 1.3, 450.0, 4096); |
| 182 | let res_amplified = AbForkEvaluator::evaluate_tier2_candidate( |
| 183 | Path::new("."), |
| 184 | Path::new("."), |
| 185 | &proposal, |
| 186 | &parent_metrics, |
| 187 | &lified_candidate, |
| 188 | Some(&operator_verifying_key), |
| 189 | 1.0, |
| 190 | None, |
| 191 | ) |
| 192 | .unwrap(); |
| 193 | assert_eq!(res_amplified.overall_classification, "META_CANDIDATE"); |
| 194 | assert!(res_amplified.novel_discovery.passed); |
| 195 | assert!(res_amplified.self_capability_improvement.passed); |
| 196 | assert!(!res_amplified.recursive_persistence.passed); // Downstream evidence pending |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn test_three_criteria_true_rsi_lifecycle_and_ledger_compounding() { |
| 201 | let tmp = tempfile::tempdir().unwrap(); |
| 202 | let rsi_root = tmp.path().join(".rsi"); |
| 203 | fs::create_dir_all(&rsi_root).unwrap(); |
| 204 | |
| 205 | let ledger = ImprovementLedger::open(&rsi_root).expect("Ledger initialization failed"); |
| 206 | |
| 207 | // 1. Record generation N (MetaCandidate) promotion block |
| 208 | let meta_block = ledger |
| 209 | .append_block( |
| 210 | BlockType::Promotion, |
| 211 | "{\"candidate\": \"engine-gen-01\", \"classification\": \"META_CANDIDATE\"}" |
| 212 | .to_string(), |
| 213 | Vec::new(), |
| 214 | ) |
| 215 | .expect("MetaCandidate promotion block append"); |
| 216 | |
| 217 | // 2. Record downstream generation N+1 cycle evaluation block |
| 218 | let downstream_block = ledger |
| 219 | .append_block( |
| 220 | BlockType::Evaluation, |
| 221 | "{\"cycle_id\": \"cycle-02\", \"throughput_gain_pct\": 18.5}".to_string(), |
| 222 | Vec::new(), |
| 223 | ) |
| 224 | .expect("Downstream evaluation block append"); |
| 225 | |
| 226 | // 3. Evaluate Three Criteria with downstream evidence |
| 227 | let c1 = TrueRsiEvaluator::evaluate_criterion_1_novel_discovery( |
| 228 | "Identified unencoded KV cache contention under concurrency", |
| 229 | true, |
| 230 | &["static_heuristic", "hardcoded_rule"], |
| 231 | ); |
| 232 | assert!(c1.passed, "Criterion 1 NovelDiscovery must pass"); |
| 233 | |
| 234 | let c2 = TrueRsiEvaluator::evaluate_criterion_2_self_capability_improvement( |
| 235 | true, |
| 236 | "Discovery rate improved +22.5%, cycle latency -11.0%", |
| 237 | ); |
| 238 | assert!(c2.passed, "Criterion 2 SelfCapabilityImprovement must pass"); |
| 239 | |
| 240 | let c3 = TrueRsiEvaluator::evaluate_criterion_3_recursive_persistence( |
| 241 | Some(&meta_block.block_hash), |
| 242 | Some("cycle-02"), |
| 243 | Some(&downstream_block.block_hash), |
| 244 | Some("Downstream generation N+1 utilized lock-free cache primitive from N to resolve cycle-02 bottleneck"), |
| 245 | ); |
| 246 | assert!(c3.0.passed, "Criterion 3 RecursivePersistence must pass"); |
| 247 | |
| 248 | let verdict = TrueRsiEvaluator::evaluate_full(c1, c2, c3); |
| 249 | assert_eq!(verdict.overall_classification, "TRUE_RSI"); |
| 250 | assert!(verdict.evidence_payload.is_some()); |
| 251 | |
| 252 | // 4. Record append-only PromotionEvidence block in cryptographic ledger |
| 253 | let evidence_payload = verdict.evidence_payload.unwrap(); |
| 254 | let evidence_block = ledger |
| 255 | .append_promotion_evidence(&evidence_payload) |
| 256 | .expect("Appending promotion evidence to ledger failed"); |
| 257 | |
| 258 | assert_eq!(evidence_block.block_type, BlockType::PromotionEvidence); |
| 259 | |
| 260 | // 5. Cryptographically audit ledger integrity |
| 261 | let signing_key = SigningKey::from_bytes(&[88u8; 32].into()).unwrap(); |
| 262 | let verifying_key = signing_key.verifying_key(); |
| 263 | let _ = ledger.checkpoint(Some(&signing_key)); |
| 264 | |
| 265 | let audit = ledger |
| 266 | .verify_chain_integrity(Some(&verifying_key)) |
| 267 | .expect("Ledger audit failed"); |
| 268 | assert!( |
| 269 | audit.chain_valid, |
| 270 | "Cryptographic ledger hash chain must be valid" |
| 271 | ); |
| 272 | assert_eq!(audit.total_blocks, 4); // Genesis (0) + Promotion (1) + Evaluation (2) + PromotionEvidence (3) |
| 273 | } |