Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/spark-rsi-mv/blob/main/tests/integration_tests.rs # spark-rsi-mv Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `spark-rsi-mv / tests / integration_tests.rs` 356 lines ยท 12483 bytes Raw Blame History 1 use spark_rsi::balance::BalanceKernel; 2 use spark_rsi::models::{ProposalKind, RsiConfig}; 3 use spark_rsi::observe::{calculate_soul_tension, observe_codebase}; 4 use spark_rsi::propose::ProposalGenerator; 5 use spark_rsi::verifier::InvariantVerifier; 6 use std::fs; 7 use std::path::Path; 8 9 #[test] 10 fn test_balance_kernel_scalar() { 11 let verdict = BalanceKernel::evaluate(12.0, 10.0); 12 assert_eq!(verdict.kernel, "rust"); 13 assert_eq!(verdict.verdict, "balanced"); 14 assert!(verdict.score > 0.7); 15 } 16 17 #[test] 18 fn test_balance_kernel_simd() { 19 let drive_vec = [10.0, 8.0, 7.0, 9.0]; 20 let human_vec = [9.0, 8.0, 8.0, 8.0]; 21 let verdict = BalanceKernel::evaluate_simd(drive_vec, human_vec); 22 assert_eq!(verdict.mode, "vector"); 23 assert_eq!(verdict.verdict, "balanced"); 24 assert_eq!(verdict.drive, 34.0); 25 assert_eq!(verdict.humanity, 33.0); 26 } 27 28 #[test] 29 fn test_soul_tension_analysis() { 30 let sample = "We build, fix, finish, and solve problems with love, community, and discipline."; 31 let tension = calculate_soul_tension(sample); 32 assert!(tension.drive_score >= 4.0); 33 assert!(tension.humanity_score >= 3.0); 34 assert_eq!(tension.state, "Balanced"); 35 } 36 37 #[test] 38 fn test_invariant_unslop_enforcement() { 39 let clean_text = "Standard engineering documentation without banned elements."; 40 let (clean, em, en, bw, tr) = InvariantVerifier::verify_unslop_text(clean_text); 41 assert!(clean); 42 assert_eq!(em, 0); 43 assert_eq!(en, 0); 44 assert!(bw.is_empty()); 45 assert!(tr.is_empty()); 46 47 let em_dash_text = "Violation \u{2014} here."; 48 let (clean, em, _, _, _) = InvariantVerifier::verify_unslop_text(em_dash_text); 49 assert!(!clean); 50 assert_eq!(em, 1); 51 52 let buzzword_text = "We will delve into the tapestry of software."; 53 let (clean, _, _, bw, _) = InvariantVerifier::verify_unslop_text(buzzword_text); 54 assert!(!clean); 55 assert_eq!(bw.len(), 2); 56 } 57 58 #[test] 59 fn test_invariant_secret_scanner() { 60 let temp_dir = tempfile::tempdir().expect("tempdir"); 61 let safe_file = temp_dir.path().join("config.json"); 62 fs::write(&safe_file, "{\"setting\": true}").expect("write safe file"); 63 64 let (clean, leaks) = InvariantVerifier::scan_dir_for_secrets(temp_dir.path()); 65 assert!(clean); 66 assert!(leaks.is_empty()); 67 68 let env_file = temp_dir.path().join(".env"); 69 fs::write(&env_file, "SECRET=compromised").expect("write env file"); 70 71 let (clean, leaks) = InvariantVerifier::scan_dir_for_secrets(temp_dir.path()); 72 assert!(!clean); 73 assert_eq!(leaks.len(), 1); 74 } 75 76 #[test] 77 fn test_proposal_staging_in_sandbox() { 78 let temp_repo = tempfile::tempdir().expect("temp repo"); 79 let temp_sandbox = tempfile::tempdir().expect("temp sandbox"); 80 81 let file_path = temp_repo.path().join("test_file.txt"); 82 fs::write(&file_path, "original content").expect("write original"); 83 84 let mut proposal = ProposalGenerator::create_proposal( 85 "update file", 86 "update content in sandbox", 87 "test_file.txt", 88 "improved content", 89 ProposalKind::Optimization, 90 ); 91 92 let staged_path = 93 ProposalGenerator::stage_in_sandbox(&mut proposal, temp_repo.path(), temp_sandbox.path()) 94 .expect("stage should succeed"); 95 96 assert!(staged_path.exists()); 97 let staged_file = staged_path.join("test_file.txt"); 98 let content = fs::read_to_string(&staged_file).expect("read staged"); 99 assert_eq!(content, "improved content"); 100 } 101 102 #[test] 103 fn test_codebase_observation_current_repo() { 104 if !spark_rsi::not_run::user_scope_or_not_run("test_codebase_observation_current_repo") { 105 return; 106 } 107 let repo_path = Path::new("."); 108 let telemetry = observe_codebase(repo_path).expect("telemetry should succeed"); 109 assert!(!telemetry.git_branch.is_empty()); 110 // The build check runs `cargo check --offline --locked` in a jail that binds only the 111 // observed tree, so it cannot resolve this crate's sibling path dependencies 112 // (../aien-protocols). Assert it on the standalone canary target, which it can build. 113 let standalone = 114 observe_codebase(Path::new("fixtures/canary-target")).expect("telemetry should succeed"); 115 assert!(standalone.tests_passing); 116 } 117 118 #[tokio::test] 119 async fn test_rsi_cycle_execution() { 120 let tmp = tempfile::tempdir().unwrap(); 121 let rsi_root = tmp.path().join(".rsi"); 122 std::fs::create_dir_all(&rsi_root).unwrap(); 123 124 // Initialize Improvement Ledger 125 spark_rsi::ledger::ImprovementLedger::open(&rsi_root).expect("ledger init"); 126 127 // Initialize Holdouts 128 let holdouts = rsi_root.join("holdouts"); 129 std::fs::create_dir_all(&holdouts).unwrap(); 130 for s in spark_rsi::actor::judge::HoldoutSuite::builtin_suites() { 131 s.save_to_dir(&holdouts).unwrap(); 132 } 133 134 // P-256 signing key 135 let signing_key = p256::ecdsa::SigningKey::from_bytes(&[88u8; 32].into()).unwrap(); 136 let signing_key_hex = hex::encode(signing_key.to_bytes()); 137 138 let config = RsiConfig { 139 target_repo: ".".to_string(), 140 cortex_url: "http://127.0.0.1:18080".to_string(), 141 cortex_space: "atlas-memory".to_string(), 142 loop_interval_secs: 60, 143 sandbox_root: tmp.path().join("sandbox").display().to_string(), 144 rsi_root: rsi_root.display().to_string(), 145 holdouts_dir: Some(holdouts.display().to_string()), 146 signing_key_hex: Some(signing_key_hex), 147 require_latency_improvement: false, 148 canary_target: 3, 149 model_url: "http://127.0.0.1:9999/v1".to_string(), 150 non_inferiority_margin: Some(150.0), 151 ..Default::default() 152 }; 153 154 let result = spark_rsi::daemon::RsiEngine::run_cycle(&config) 155 .await 156 .expect("cycle should execute"); 157 158 assert!(result.elapsed_ms >= 0.0); 159 assert!(result.balance.is_some()); 160 let balance = result.balance.unwrap(); 161 assert_eq!(balance.verdict, "balanced"); 162 } 163 164 #[tokio::test] 165 async fn test_rsi_cycle_missing_holdouts_fails_closed() { 166 let tmp = tempfile::tempdir().unwrap(); 167 let rsi_root = tmp.path().join(".rsi"); 168 std::fs::create_dir_all(&rsi_root).unwrap(); 169 spark_rsi::ledger::ImprovementLedger::open(&rsi_root).unwrap(); 170 171 let signing_key = p256::ecdsa::SigningKey::from_bytes(&[88u8; 32].into()).unwrap(); 172 let signing_key_hex = hex::encode(signing_key.to_bytes()); 173 174 let config = RsiConfig { 175 target_repo: ".".to_string(), 176 rsi_root: rsi_root.display().to_string(), 177 holdouts_dir: Some( 178 tmp.path() 179 .join("nonexistent_holdouts") 180 .display() 181 .to_string(), 182 ), 183 signing_key_hex: Some(signing_key_hex), 184 ..Default::default() 185 }; 186 187 let res = spark_rsi::daemon::RsiEngine::run_cycle(&config).await; 188 assert!(res.is_err(), "Missing holdouts must fail closed"); 189 assert!(res.err().unwrap().contains("Missing holdouts")); 190 } 191 192 #[tokio::test] 193 async fn test_rsi_cycle_missing_signer_fails_closed() { 194 let tmp = tempfile::tempdir().unwrap(); 195 let rsi_root = tmp.path().join(".rsi"); 196 std::fs::create_dir_all(&rsi_root).unwrap(); 197 spark_rsi::ledger::ImprovementLedger::open(&rsi_root).unwrap(); 198 199 let holdouts = rsi_root.join("holdouts"); 200 std::fs::create_dir_all(&holdouts).unwrap(); 201 202 let config = RsiConfig { 203 target_repo: ".".to_string(), 204 rsi_root: rsi_root.display().to_string(), 205 holdouts_dir: Some(holdouts.display().to_string()), 206 signing_key_hex: None, 207 ..Default::default() 208 }; 209 210 let res = spark_rsi::daemon::RsiEngine::run_cycle(&config).await; 211 assert!(res.is_err(), "Missing signer must fail closed"); 212 assert!(res 213 .err() 214 .unwrap() 215 .contains("Missing cryptographic signing key")); 216 } 217 218 #[tokio::test] 219 async fn test_production_loop_end_to_end_with_candidate() { 220 let tmp = tempfile::tempdir().unwrap(); 221 let rsi_root = tmp.path().join(".rsi"); 222 std::fs::create_dir_all(&rsi_root).unwrap(); 223 224 let ledger = spark_rsi::ledger::ImprovementLedger::open(&rsi_root).expect("ledger init"); 225 226 // Initialize Holdouts 227 let holdouts = rsi_root.join("holdouts"); 228 std::fs::create_dir_all(&holdouts).unwrap(); 229 for s in spark_rsi::actor::judge::HoldoutSuite::builtin_suites() { 230 s.save_to_dir(&holdouts).unwrap(); 231 } 232 233 // P-256 signing key 234 let signing_key = p256::ecdsa::SigningKey::from_bytes(&[88u8; 32].into()).unwrap(); 235 let signing_key_hex = hex::encode(signing_key.to_bytes()); 236 237 // Create an isolated target repo with an unslop defect 238 let test_repo = tmp.path().join("test_repo"); 239 std::fs::create_dir_all(&test_repo).unwrap(); 240 241 // Copy complete source tree to test_repo including Cargo.lock, README.md, docs 242 std::process::Command::new("cp") 243 .args(&[ 244 "-r", 245 "Cargo.toml", 246 "Cargo.lock", 247 "README.md", 248 "src", 249 "docs", 250 test_repo.to_str().unwrap(), 251 ]) 252 .output() 253 .unwrap(); 254 std::fs::copy(env!("CARGO_BIN_EXE_spark-rsi"), test_repo.join("spark-rsi")) 255 .expect("copy this test invocation's compiled executable"); 256 257 // Initialize git in test_repo 258 let _ = std::process::Command::new("git") 259 .arg("init") 260 .current_dir(&test_repo) 261 .output(); 262 let _ = std::process::Command::new("git") 263 .args(&["config", "user.name", "AIEN"]) 264 .current_dir(&test_repo) 265 .output(); 266 let _ = std::process::Command::new("git") 267 .args(&["config", "user.email", "aien@spark.invalid"]) 268 .current_dir(&test_repo) 269 .output(); 270 271 // Inject an unslop violation into docs/PHILOSOPHY.md 272 let phil_path = test_repo.join("docs").join("PHILOSOPHY.md"); 273 let mut phil_content = std::fs::read_to_string(&phil_path).unwrap(); 274 phil_content.push_str("\n# Injected Standard \u{2014} unslop violation to sanitize\n"); 275 std::fs::write(&phil_path, phil_content).unwrap(); 276 277 let _ = std::process::Command::new("git") 278 .args(&["add", "."]) 279 .current_dir(&test_repo) 280 .output(); 281 let _ = std::process::Command::new("git") 282 .args(&["commit", "-m", "initial commit"]) 283 .current_dir(&test_repo) 284 .output(); 285 286 let config = RsiConfig { 287 target_repo: test_repo.display().to_string(), 288 cortex_url: "http://127.0.0.1:18080".to_string(), 289 cortex_space: "atlas-memory".to_string(), 290 loop_interval_secs: 60, 291 sandbox_root: tmp.path().join("sandbox").display().to_string(), 292 rsi_root: rsi_root.display().to_string(), 293 holdouts_dir: Some(holdouts.display().to_string()), 294 signing_key_hex: Some(signing_key_hex), 295 require_latency_improvement: false, 296 canary_target: 5, 297 model_url: "http://127.0.0.1:9999/v1".to_string(), 298 non_inferiority_margin: Some(150.0), 299 ..Default::default() 300 }; 301 302 let result = spark_rsi::daemon::RsiEngine::run_cycle(&config) 303 .await 304 .expect("cycle should execute"); 305 306 assert!( 307 !result.success, 308 "Production cycle must refuse promotion without measured canary observations" 309 ); 310 assert!( 311 result.proposal.is_some(), 312 "Unslop candidate proposal must be generated" 313 ); 314 let prop = result.proposal.unwrap(); 315 assert_eq!( 316 prop.kind, 317 spark_rsi::models::ProposalKind::UnslopSanitization 318 ); 319 320 assert!(result.invariants.is_some(), "Invariants must be verified"); 321 assert!(result.invariants.unwrap().passed, "Invariants must pass"); 322 323 assert!(result.balance.is_some(), "Balance kernel must be evaluated"); 324 assert_eq!(result.balance.unwrap().verdict, "balanced"); 325 326 assert!( 327 result.receipt.is_some(), 328 "BlindJudge receipt must be present" 329 ); 330 assert!( 331 result.receipt.unwrap().admitted, 332 "BlindJudge must admit the candidate" 333 ); 334 335 assert!( 336 result.generation.is_none(), 337 "A synthetic quota must not promote a generation" 338 ); 339 assert!( 340 result.ratification.is_none(), 341 "Ratification waits for a measured canary quota" 342 ); 343 344 // Verify ledger audit report 345 let audit = ledger 346 .verify_chain_integrity(Some(&signing_key.verifying_key())) 347 .expect("ledger audit"); 348 assert!( 349 audit.chain_valid, 350 "Ledger chain must be cryptographically valid" 351 ); 352 assert!( 353 audit.total_blocks >= 2, 354 "Ledger must record the refused promotion" 355 ); 356 }