spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / verifier.rs
| 1 | use crate::isolation::sandboxed_cargo; |
| 2 | use crate::models::InvariantReport; |
| 3 | use std::fs; |
| 4 | use std::path::Path; |
| 5 | |
| 6 | pub const FORBIDDEN_BUZZWORDS: &[&str] = &[ |
| 7 | "delve", |
| 8 | "tapestry", |
| 9 | "testament", |
| 10 | "beacon", |
| 11 | "crucial", |
| 12 | "pivotal", |
| 13 | "elevate", |
| 14 | "game-changer", |
| 15 | "unleash", |
| 16 | "harness", |
| 17 | "seamlessly", |
| 18 | ]; |
| 19 | |
| 20 | pub const ANTITHESIS_TROPES: &[&str] = &["it's not ", "it is not ", "not only ", "not just "]; |
| 21 | |
| 22 | pub const TRANSITIONAL_FLUFF: &[&str] = |
| 23 | &["furthermore", "moreover", "in conclusion", "at its core"]; |
| 24 | |
| 25 | pub struct InvariantVerifier; |
| 26 | |
| 27 | impl InvariantVerifier { |
| 28 | pub fn verify_unslop_text(text: &str) -> (bool, usize, usize, Vec<String>, Vec<String>) { |
| 29 | let em_count = text.matches('\u{2014}').count(); |
| 30 | let en_count = text.matches('\u{2013}').count(); |
| 31 | |
| 32 | let lower = text.to_lowercase(); |
| 33 | let mut buzzwords_found = Vec::new(); |
| 34 | for &bw in FORBIDDEN_BUZZWORDS { |
| 35 | if lower.contains(bw) { |
| 36 | buzzwords_found.push(bw.to_string()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | let mut tropes_found = Vec::new(); |
| 41 | for &trope in ANTITHESIS_TROPES { |
| 42 | if lower.contains(trope) { |
| 43 | tropes_found.push(trope.trim().to_string()); |
| 44 | } |
| 45 | } |
| 46 | for &fluff in TRANSITIONAL_FLUFF { |
| 47 | if lower.contains(fluff) { |
| 48 | tropes_found.push(fluff.to_string()); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | let clean = |
| 53 | em_count == 0 && en_count == 0 && buzzwords_found.is_empty() && tropes_found.is_empty(); |
| 54 | (clean, em_count, en_count, buzzwords_found, tropes_found) |
| 55 | } |
| 56 | |
| 57 | pub fn scan_dir_for_secrets(root: &Path) -> (bool, Vec<String>) { |
| 58 | let mut leaks = Vec::new(); |
| 59 | Self::scan_dir_secrets_inner(root, &mut leaks, 0); |
| 60 | let clean = leaks.is_empty(); |
| 61 | (clean, leaks) |
| 62 | } |
| 63 | |
| 64 | fn scan_dir_secrets_inner(dir: &Path, leaks: &mut Vec<String>, depth: usize) { |
| 65 | if depth > 8 { |
| 66 | return; |
| 67 | } |
| 68 | let entries = match fs::read_dir(dir) { |
| 69 | Ok(e) => e, |
| 70 | Err(_) => return, |
| 71 | }; |
| 72 | |
| 73 | for entry in entries.flatten() { |
| 74 | let path = entry.path(); |
| 75 | let name = path |
| 76 | .file_name() |
| 77 | .and_then(|n| n.to_str()) |
| 78 | .unwrap_or_default(); |
| 79 | |
| 80 | if name == "target" || name == ".git" { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | let path_str = path.display().to_string(); |
| 85 | if path_str.ends_with("src/verifier.rs") |
| 86 | || path_str.ends_with("tests/integration_tests.rs") |
| 87 | { |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | if path.is_file() { |
| 92 | if name == ".env" || name.starts_with(".env.") { |
| 93 | leaks.push(format!( |
| 94 | "Found prohibited env file on disk: {}", |
| 95 | path.display() |
| 96 | )); |
| 97 | } |
| 98 | |
| 99 | if let Ok(content) = fs::read_to_string(&path) { |
| 100 | let sec_sigs = [ |
| 101 | concat!("-----BEGIN ", "OPENSSH PRIVATE KEY-----"), |
| 102 | concat!("-----BEGIN ", "RSA PRIVATE KEY-----"), |
| 103 | concat!("-----BEGIN ", "EC PRIVATE KEY-----"), |
| 104 | concat!("-----BEGIN ", "PRIVATE KEY-----"), |
| 105 | ]; |
| 106 | for sig in sec_sigs { |
| 107 | if content.contains(sig) { |
| 108 | leaks |
| 109 | .push(format!("Found private key signature in {}", path.display())); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } else if path.is_dir() { |
| 115 | Self::scan_dir_secrets_inner(&path, leaks, depth + 1); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | pub fn verify_compilation_and_tests( |
| 121 | target_dir: &Path, |
| 122 | ) -> (bool, Option<String>, bool, Option<String>) { |
| 123 | let cargo_toml = target_dir.join("Cargo.toml"); |
| 124 | if !cargo_toml.exists() { |
| 125 | return ( |
| 126 | true, |
| 127 | None, |
| 128 | true, |
| 129 | Some("No Cargo.toml present; skipped".to_string()), |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | let (source_dir, build_dir) = match fs::canonicalize(target_dir) |
| 134 | .and_then(|source| tempfile::tempdir().map(|build_dir| (source, build_dir))) |
| 135 | { |
| 136 | Ok(paths) => paths, |
| 137 | Err(e) => { |
| 138 | return ( |
| 139 | false, |
| 140 | Some(format!("Failed to spawn cargo check: {}", e)), |
| 141 | false, |
| 142 | None, |
| 143 | ); |
| 144 | } |
| 145 | }; |
| 146 | let check_res = sandboxed_cargo( |
| 147 | &source_dir, |
| 148 | build_dir.path(), |
| 149 | &["check", "--offline", "--locked", "-j2"], |
| 150 | ) |
| 151 | .output(); |
| 152 | |
| 153 | let (compile_passed, compile_err) = match check_res { |
| 154 | Ok(out) => { |
| 155 | if out.status.success() { |
| 156 | (true, None) |
| 157 | } else { |
| 158 | let err = String::from_utf8_lossy(&out.stderr).to_string(); |
| 159 | (false, Some(err)) |
| 160 | } |
| 161 | } |
| 162 | Err(e) => (false, Some(format!("Failed to spawn cargo check: {}", e))), |
| 163 | }; |
| 164 | |
| 165 | if !compile_passed { |
| 166 | return (false, compile_err, false, None); |
| 167 | } |
| 168 | |
| 169 | let test_res = sandboxed_cargo( |
| 170 | &source_dir, |
| 171 | build_dir.path(), |
| 172 | &[ |
| 173 | "test", |
| 174 | "--offline", |
| 175 | "--locked", |
| 176 | "-j2", |
| 177 | "--", |
| 178 | "--test-threads=1", |
| 179 | "--nocapture", |
| 180 | ], |
| 181 | ) |
| 182 | .output(); |
| 183 | |
| 184 | let (test_passed, test_summary) = match test_res { |
| 185 | Ok(out) => { |
| 186 | let stdout = String::from_utf8_lossy(&out.stdout).to_string(); |
| 187 | let stderr = String::from_utf8_lossy(&out.stderr).to_string(); |
| 188 | let summary = if out.status.success() { |
| 189 | "All tests passed".to_string() |
| 190 | } else { |
| 191 | format!("Test failures:\n{}\n{}", stdout, stderr) |
| 192 | }; |
| 193 | (out.status.success(), Some(summary)) |
| 194 | } |
| 195 | Err(e) => (false, Some(format!("Failed to spawn cargo test: {}", e))), |
| 196 | }; |
| 197 | |
| 198 | (compile_passed, compile_err, test_passed, test_summary) |
| 199 | } |
| 200 | |
| 201 | pub fn run_full_verification(target_dir: &Path) -> InvariantReport { |
| 202 | Self::run_full_verification_scoped(target_dir, true) |
| 203 | } |
| 204 | |
| 205 | /// Runs invariants with build checks optionally disabled. |
| 206 | /// Non-code proposals (markdown sanitization) must not be gated by compilation, |
| 207 | /// because the sandbox cannot resolve sibling path dependencies. |
| 208 | pub fn run_full_verification_scoped( |
| 209 | target_dir: &Path, |
| 210 | run_build_checks: bool, |
| 211 | ) -> InvariantReport { |
| 212 | let mut notes = Vec::new(); |
| 213 | |
| 214 | let mut total_em = 0; |
| 215 | let mut total_en = 0; |
| 216 | let mut buzzwords = Vec::new(); |
| 217 | let mut tropes = Vec::new(); |
| 218 | |
| 219 | let check_files = ["README.md", "docs/PHILOSOPHY.md", "CONTRIBUTING.md"]; |
| 220 | for rel in &check_files { |
| 221 | let p = target_dir.join(rel); |
| 222 | if p.exists() { |
| 223 | if let Ok(content) = fs::read_to_string(&p) { |
| 224 | let (_, em, en, bw, tr) = Self::verify_unslop_text(&content); |
| 225 | total_em += em; |
| 226 | total_en += en; |
| 227 | buzzwords.extend(bw); |
| 228 | tropes.extend(tr); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | buzzwords.sort(); |
| 233 | buzzwords.dedup(); |
| 234 | tropes.sort(); |
| 235 | tropes.dedup(); |
| 236 | |
| 237 | let unslop_clean = |
| 238 | total_em == 0 && total_en == 0 && buzzwords.is_empty() && tropes.is_empty(); |
| 239 | if !unslop_clean { |
| 240 | notes.push(format!( |
| 241 | "Unslop violation: em_dashes={}, en_dashes={}, buzzwords={:?}, tropes={:?}", |
| 242 | total_em, total_en, buzzwords, tropes |
| 243 | )); |
| 244 | } |
| 245 | |
| 246 | let (secrets_clean, leaks) = Self::scan_dir_for_secrets(target_dir); |
| 247 | if !secrets_clean { |
| 248 | notes.push(format!("Zero disk secrets violation: {:?}", leaks)); |
| 249 | } |
| 250 | |
| 251 | let (compile_ok, compile_err, test_ok, test_summary) = if run_build_checks { |
| 252 | Self::verify_compilation_and_tests(target_dir) |
| 253 | } else { |
| 254 | ( |
| 255 | true, |
| 256 | None, |
| 257 | true, |
| 258 | Some("Skipped: non-code proposal".to_string()), |
| 259 | ) |
| 260 | }; |
| 261 | |
| 262 | if !compile_ok { |
| 263 | notes.push("Compilation failed".to_string()); |
| 264 | } |
| 265 | if !test_ok { |
| 266 | notes.push("Tests failed".to_string()); |
| 267 | } |
| 268 | |
| 269 | let passed = unslop_clean && secrets_clean && compile_ok && test_ok; |
| 270 | |
| 271 | InvariantReport { |
| 272 | passed, |
| 273 | unslop_clean, |
| 274 | em_dash_detected: total_em, |
| 275 | en_dash_detected: total_en, |
| 276 | forbidden_buzzwords_detected: buzzwords, |
| 277 | antithesis_tropes_detected: tropes, |
| 278 | zero_disk_secrets_clean: secrets_clean, |
| 279 | secret_leaks: leaks, |
| 280 | compilation_passed: compile_ok, |
| 281 | compilation_error: compile_err, |
| 282 | tests_passed: test_ok, |
| 283 | test_output_summary: test_summary, |
| 284 | notes, |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | #[cfg(test)] |
| 290 | mod tests { |
| 291 | use super::*; |
| 292 | |
| 293 | #[test] |
| 294 | fn test_unslop_clean_text() { |
| 295 | let sample = "High performance Rust code running on SparkOS hardware."; |
| 296 | let (clean, em, en, bw, tr) = InvariantVerifier::verify_unslop_text(sample); |
| 297 | assert!(clean); |
| 298 | assert_eq!(em, 0); |
| 299 | assert_eq!(en, 0); |
| 300 | assert!(bw.is_empty()); |
| 301 | assert!(tr.is_empty()); |
| 302 | } |
| 303 | |
| 304 | #[test] |
| 305 | fn test_unslop_detects_em_dash() { |
| 306 | let sample = "This is good \u{2014} but that is bad."; |
| 307 | let (clean, em, _, _, _) = InvariantVerifier::verify_unslop_text(sample); |
| 308 | assert!(!clean); |
| 309 | assert_eq!(em, 1); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn test_unslop_detects_buzzwords() { |
| 314 | let sample = "We will delve into this crucial topic."; |
| 315 | let (clean, _, _, bw, _) = InvariantVerifier::verify_unslop_text(sample); |
| 316 | assert!(!clean); |
| 317 | assert!(bw.contains(&"delve".to_string())); |
| 318 | assert!(bw.contains(&"crucial".to_string())); |
| 319 | } |
| 320 | } |