spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / meta / ab_fork.rs
| 1 | use crate::meta::benchmark_suite::{MetaBenchmarkMetrics, MetaBenchmarkSuite}; |
| 2 | use crate::meta::three_criteria::{TrueRsiEvaluator, TrueRsiVerdict}; |
| 3 | use crate::meta::tier::TierGovernance; |
| 4 | use crate::models::ImprovementProposal; |
| 5 | use p256::ecdsa::VerifyingKey; |
| 6 | use sha2::{Digest, Sha256}; |
| 7 | use std::path::Path; |
| 8 | |
| 9 | pub struct AbForkEvaluator; |
| 10 | |
| 11 | impl AbForkEvaluator { |
| 12 | /// Engine N judges candidate Engine N+1 on an isolated A/B fork |
| 13 | pub fn evaluate_tier2_candidate( |
| 14 | _parent_repo: &Path, |
| 15 | _candidate_repo: &Path, |
| 16 | proposal: &ImprovementProposal, |
| 17 | parent_metrics: &MetaBenchmarkMetrics, |
| 18 | candidate_metrics: &MetaBenchmarkMetrics, |
| 19 | operator_verifying_key: Option<&VerifyingKey>, |
| 20 | non_inferiority_margin_pct: f64, |
| 21 | downstream_evidence: Option<(&str, &str, &str, &str)>, // (meta_hash, down_cycle, down_hash, proof) |
| 22 | ) -> Result<TrueRsiVerdict, String> { |
| 23 | // 1. Validate containment boundaries: Engine N+1 cannot modify immutable boundaries |
| 24 | TierGovernance::validate_tier2_candidate_boundaries(&proposal.target_file)?; |
| 25 | |
| 26 | // 2. Validate Operator Cryptographic Signature for Tier 2 |
| 27 | let op_key = operator_verifying_key.ok_or_else(|| { |
| 28 | "Tier 2 Meta-Improvement Fails Closed: Operator cryptographic verifying key required for Engine self-modification.".to_string() |
| 29 | })?; |
| 30 | |
| 31 | let sig_hex = proposal.operator_signature.as_deref().ok_or_else(|| { |
| 32 | "Tier 2 Meta-Improvement Fails Closed: Missing operator cryptographic signature on candidate proposal.".to_string() |
| 33 | })?; |
| 34 | |
| 35 | let proposal_digest = Sha256::digest(proposal.proposed_patch.as_bytes()); |
| 36 | TierGovernance::verify_operator_authorization(&proposal_digest, sig_hex, op_key)?; |
| 37 | |
| 38 | // 3. A/B Benchmark Evaluation (Engine N measures Engine N+1) |
| 39 | let comparison = MetaBenchmarkSuite::evaluate_candidate( |
| 40 | parent_metrics, |
| 41 | candidate_metrics, |
| 42 | non_inferiority_margin_pct, |
| 43 | ); |
| 44 | |
| 45 | if !comparison.passed_non_inferiority { |
| 46 | return Ok(TrueRsiVerdict { |
| 47 | novel_discovery: crate::meta::three_criteria::CriterionResult { |
| 48 | criterion_name: "Criterion 1: Novel Discovery".to_string(), |
| 49 | passed: false, |
| 50 | evidence: "Candidate regressed beyond non-inferiority margins".to_string(), |
| 51 | }, |
| 52 | self_capability_improvement: crate::meta::three_criteria::CriterionResult { |
| 53 | criterion_name: "Criterion 2: Self-Capability Improvement".to_string(), |
| 54 | passed: false, |
| 55 | evidence: "Candidate regressed on developmental latency or validity" |
| 56 | .to_string(), |
| 57 | }, |
| 58 | recursive_persistence: crate::meta::three_criteria::CriterionResult { |
| 59 | criterion_name: "Criterion 3: Recursive Persistence & Compounding".to_string(), |
| 60 | passed: false, |
| 61 | evidence: "Evaluation aborted due to developmental regression".to_string(), |
| 62 | }, |
| 63 | overall_classification: "REJECTED".to_string(), |
| 64 | evidence_payload: None, |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | // 4. Evaluate Three Criteria |
| 69 | let c1 = TrueRsiEvaluator::evaluate_criterion_1_novel_discovery( |
| 70 | &proposal.description, |
| 71 | true, |
| 72 | &["hardcoded_fix", "static_rule"], |
| 73 | ); |
| 74 | |
| 75 | let metrics_summary = format!( |
| 76 | "Discovery delta: {:.2}%, Validity delta: {:.2}%, Latency delta: {:.2}%", |
| 77 | comparison.discovery_rate_delta_pct, |
| 78 | comparison.hypothesis_validity_delta_pct, |
| 79 | comparison.latency_delta_pct |
| 80 | ); |
| 81 | let c2 = TrueRsiEvaluator::evaluate_criterion_2_self_capability_improvement( |
| 82 | comparison.passed_amplification, |
| 83 | &metrics_summary, |
| 84 | ); |
| 85 | |
| 86 | let c3 = match downstream_evidence { |
| 87 | Some((meta_hash, down_cycle, down_hash, proof)) => { |
| 88 | TrueRsiEvaluator::evaluate_criterion_3_recursive_persistence( |
| 89 | Some(meta_hash), |
| 90 | Some(down_cycle), |
| 91 | Some(down_hash), |
| 92 | Some(proof), |
| 93 | ) |
| 94 | } |
| 95 | None => { |
| 96 | TrueRsiEvaluator::evaluate_criterion_3_recursive_persistence(None, None, None, None) |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | Ok(TrueRsiEvaluator::evaluate_full(c1, c2, c3)) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #[cfg(test)] |
| 105 | mod tests { |
| 106 | use super::*; |
| 107 | use crate::models::ProposalKind; |
| 108 | use p256::ecdsa::signature::Signer; |
| 109 | use p256::ecdsa::{Signature, SigningKey}; |
| 110 | |
| 111 | #[test] |
| 112 | fn test_ab_fork_evaluation_full_lifecycle() { |
| 113 | let signing_key = SigningKey::from_bytes(&[77u8; 32].into()).unwrap(); |
| 114 | let verifying_key = signing_key.verifying_key(); |
| 115 | |
| 116 | let patch = "pub fn new_engine_feature() {}"; |
| 117 | let digest = Sha256::digest(patch.as_bytes()); |
| 118 | let sig: Signature = signing_key.sign(&digest); |
| 119 | let sig_hex = hex::encode(sig.to_bytes()); |
| 120 | |
| 121 | let proposal = ImprovementProposal { |
| 122 | id: "meta-prop-01".to_string(), |
| 123 | title: "accelerate proposal engine".to_string(), |
| 124 | description: "Empirical discovery: graph centrality bottleneck in propose/mod.rs" |
| 125 | .to_string(), |
| 126 | target_file: "src/propose/mod.rs".to_string(), |
| 127 | proposed_patch: patch.to_string(), |
| 128 | kind: ProposalKind::MetaEngineImprovement, |
| 129 | created_at: "now".to_string(), |
| 130 | sandbox_path: None, |
| 131 | operator_signature: Some(sig_hex), |
| 132 | }; |
| 133 | |
| 134 | let parent_metrics = MetaBenchmarkMetrics::new(10.0, 0.85, 1.0, 400.0, 4096); |
| 135 | let candidate_metrics = MetaBenchmarkMetrics::new(13.0, 0.90, 1.3, 350.0, 4096); |
| 136 | |
| 137 | let res = AbForkEvaluator::evaluate_tier2_candidate( |
| 138 | Path::new("."), |
| 139 | Path::new("."), |
| 140 | &proposal, |
| 141 | &parent_metrics, |
| 142 | &candidate_metrics, |
| 143 | Some(&verifying_key), |
| 144 | 1.0, |
| 145 | Some(( |
| 146 | "meta-hash-01", |
| 147 | "cycle-downstream-02", |
| 148 | "downstream-hash-02", |
| 149 | "Downstream cycle 2 improved throughput using generation 1 acceleration", |
| 150 | )), |
| 151 | ); |
| 152 | |
| 153 | assert!(res.is_ok()); |
| 154 | let verdict = res.unwrap(); |
| 155 | assert_eq!(verdict.overall_classification, "TRUE_RSI"); |
| 156 | assert!(verdict.novel_discovery.passed); |
| 157 | assert!(verdict.self_capability_improvement.passed); |
| 158 | assert!(verdict.recursive_persistence.passed); |
| 159 | assert!(verdict.evidence_payload.is_some()); |
| 160 | } |
| 161 | } |