spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / meta / tier.rs
| 1 | use crate::models::{ImprovementProposal, ProposalKind}; |
| 2 | use p256::ecdsa::signature::Verifier; |
| 3 | use p256::ecdsa::{Signature, VerifyingKey}; |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | |
| 6 | #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] |
| 7 | pub enum CandidateTier { |
| 8 | Tier1Target, |
| 9 | Tier2Engine, |
| 10 | } |
| 11 | |
| 12 | pub struct TierGovernance; |
| 13 | |
| 14 | impl TierGovernance { |
| 15 | /// Engine paths that classify a proposal as Tier 2 (Engine Code) |
| 16 | pub const ENGINE_PREFIXES: &'static [&'static str] = &[ |
| 17 | "src/daemon.rs", |
| 18 | "src/models.rs", |
| 19 | "src/observe.rs", |
| 20 | "src/propose", |
| 21 | "src/graph", |
| 22 | "src/balance", |
| 23 | "src/meta", |
| 24 | "src/ratify.rs", |
| 25 | "src/supervisor", |
| 26 | "src/actor", |
| 27 | "src/evaluator", |
| 28 | "src/isolation", |
| 29 | "src/ledger", |
| 30 | "src/verifier", |
| 31 | ]; |
| 32 | |
| 33 | /// Strictly forbidden paths for Tier 2: tests, scoring rules, containment boundaries, legal covenants |
| 34 | pub const IMMUTABLE_ENGINE_BOUNDARIES: &'static [&'static str] = &[ |
| 35 | "tests", |
| 36 | "src/evaluator", |
| 37 | "src/isolation", |
| 38 | "src/ledger", |
| 39 | ".rsi/holdouts", |
| 40 | "CONSTITUTION.md", |
| 41 | "LICENSE", |
| 42 | ]; |
| 43 | |
| 44 | pub fn classify_proposal(proposal: &ImprovementProposal) -> CandidateTier { |
| 45 | if proposal.kind == ProposalKind::MetaEngineImprovement { |
| 46 | return CandidateTier::Tier2Engine; |
| 47 | } |
| 48 | let target = proposal |
| 49 | .target_file |
| 50 | .trim_start_matches("./") |
| 51 | .trim_start_matches('/'); |
| 52 | for prefix in Self::ENGINE_PREFIXES { |
| 53 | if target == *prefix || target.starts_with(prefix) { |
| 54 | return CandidateTier::Tier2Engine; |
| 55 | } |
| 56 | } |
| 57 | CandidateTier::Tier1Target |
| 58 | } |
| 59 | |
| 60 | pub fn validate_tier2_candidate_boundaries(target_file: &str) -> Result<(), String> { |
| 61 | let normalized = target_file.trim_start_matches("./").trim_start_matches('/'); |
| 62 | for boundary in Self::IMMUTABLE_ENGINE_BOUNDARIES { |
| 63 | if normalized == *boundary || normalized.starts_with(boundary) { |
| 64 | return Err(format!( |
| 65 | "Tier 2 Engine Invariant Violation: Candidate Engine N+1 cannot modify immutable boundary '{}'. Scoring rules, containment, tests, and holdouts are strictly immutable.", |
| 66 | boundary |
| 67 | )); |
| 68 | } |
| 69 | } |
| 70 | Ok(()) |
| 71 | } |
| 72 | |
| 73 | pub fn verify_operator_authorization( |
| 74 | proposal_digest: &[u8], |
| 75 | signature_hex: &str, |
| 76 | verifying_key: &VerifyingKey, |
| 77 | ) -> Result<bool, String> { |
| 78 | let sig_bytes = hex::decode(signature_hex) |
| 79 | .map_err(|e| format!("Invalid operator signature hex: {}", e))?; |
| 80 | let signature = Signature::from_slice(&sig_bytes) |
| 81 | .map_err(|e| format!("Invalid P-256 operator signature format: {}", e))?; |
| 82 | verifying_key |
| 83 | .verify(proposal_digest, &signature) |
| 84 | .map(|_| true) |
| 85 | .map_err(|e| { |
| 86 | format!( |
| 87 | "Operator cryptographic signature verification failed: {}", |
| 88 | e |
| 89 | ) |
| 90 | }) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #[cfg(test)] |
| 95 | mod tests { |
| 96 | use super::*; |
| 97 | use p256::ecdsa::signature::Signer; |
| 98 | use p256::ecdsa::SigningKey; |
| 99 | use sha2::{Digest, Sha256}; |
| 100 | |
| 101 | #[test] |
| 102 | fn test_tier_classification() { |
| 103 | let p1 = ImprovementProposal { |
| 104 | id: "p1".to_string(), |
| 105 | title: "unslop target".to_string(), |
| 106 | description: "clean".to_string(), |
| 107 | target_file: "README.md".to_string(), |
| 108 | proposed_patch: "clean text".to_string(), |
| 109 | kind: ProposalKind::UnslopSanitization, |
| 110 | created_at: "now".to_string(), |
| 111 | sandbox_path: None, |
| 112 | operator_signature: None, |
| 113 | }; |
| 114 | assert_eq!( |
| 115 | TierGovernance::classify_proposal(&p1), |
| 116 | CandidateTier::Tier1Target |
| 117 | ); |
| 118 | |
| 119 | let p2 = ImprovementProposal { |
| 120 | id: "p2".to_string(), |
| 121 | title: "speedup proposal".to_string(), |
| 122 | description: "optimize".to_string(), |
| 123 | target_file: "src/propose/mod.rs".to_string(), |
| 124 | proposed_patch: "code".to_string(), |
| 125 | kind: ProposalKind::Optimization, |
| 126 | created_at: "now".to_string(), |
| 127 | sandbox_path: None, |
| 128 | operator_signature: None, |
| 129 | }; |
| 130 | assert_eq!( |
| 131 | TierGovernance::classify_proposal(&p2), |
| 132 | CandidateTier::Tier2Engine |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn test_tier2_immutable_boundary_rejection() { |
| 138 | assert!(TierGovernance::validate_tier2_candidate_boundaries("src/propose/mod.rs").is_ok()); |
| 139 | assert!(TierGovernance::validate_tier2_candidate_boundaries("src/daemon.rs").is_ok()); |
| 140 | |
| 141 | assert!(TierGovernance::validate_tier2_candidate_boundaries("tests/test_foo.rs").is_err()); |
| 142 | assert!( |
| 143 | TierGovernance::validate_tier2_candidate_boundaries("src/evaluator/stats.rs").is_err() |
| 144 | ); |
| 145 | assert!( |
| 146 | TierGovernance::validate_tier2_candidate_boundaries("src/isolation/container.rs") |
| 147 | .is_err() |
| 148 | ); |
| 149 | assert!(TierGovernance::validate_tier2_candidate_boundaries("CONSTITUTION.md").is_err()); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn test_operator_signature_verification() { |
| 154 | let signing_key = SigningKey::from_bytes(&[42u8; 32].into()).unwrap(); |
| 155 | let verifying_key = signing_key.verifying_key(); |
| 156 | |
| 157 | let patch = "pub fn engine_accelerate() {}"; |
| 158 | let digest = Sha256::digest(patch.as_bytes()); |
| 159 | let signature: Signature = signing_key.sign(&digest); |
| 160 | let sig_hex = hex::encode(signature.to_bytes()); |
| 161 | |
| 162 | let res = TierGovernance::verify_operator_authorization(&digest, &sig_hex, &verifying_key); |
| 163 | assert!(res.is_ok()); |
| 164 | assert!(res.unwrap()); |
| 165 | |
| 166 | let bad_digest = Sha256::digest(b"tampered patch"); |
| 167 | let bad_res = |
| 168 | TierGovernance::verify_operator_authorization(&bad_digest, &sig_hex, &verifying_key); |
| 169 | assert!(bad_res.is_err()); |
| 170 | } |
| 171 | } |