interplane.Get Omega
Read in your language

spark-rsi

Resilient systems interface and fault-tolerant agent communication

Brought by drakestapleton

branch main Branches Tags History

spark-rsi / src / verifier.rs

272 lines · 8853 bytes Raw Blame History
1use crate::models::InvariantReport;
2use std::fs;
3use std::path::Path;
4use std::process::Command;
5
6pub 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
20pub const ANTITHESIS_TROPES: &[&str] = &[
21 "it's not ",
22 "it is not ",
23 "not only ",
24 "not just ",
25];
26
27pub const TRANSITIONAL_FLUFF: &[&str] = &[
28 "furthermore",
29 "moreover",
30 "in conclusion",
31 "at its core",
32];
33
34pub struct InvariantVerifier;
35
36impl InvariantVerifier {
37 pub fn verify_unslop_text(text: &str) -> (bool, usize, usize, Vec<String>, Vec<String>) {
38 let em_count = text.matches('\u{2014}').count();
39 let en_count = text.matches('\u{2013}').count();
40
41 let lower = text.to_lowercase();
42 let mut buzzwords_found = Vec::new();
43 for &bw in FORBIDDEN_BUZZWORDS {
44 if lower.contains(bw) {
45 buzzwords_found.push(bw.to_string());
46 }
47 }
48
49 let mut tropes_found = Vec::new();
50 for &trope in ANTITHESIS_TROPES {
51 if lower.contains(trope) {
52 tropes_found.push(trope.trim().to_string());
53 }
54 }
55 for &fluff in TRANSITIONAL_FLUFF {
56 if lower.contains(fluff) {
57 tropes_found.push(fluff.to_string());
58 }
59 }
60
61 let clean = em_count == 0 && en_count == 0 && buzzwords_found.is_empty() && tropes_found.is_empty();
62 (clean, em_count, en_count, buzzwords_found, tropes_found)
63 }
64
65 pub fn scan_dir_for_secrets(root: &Path) -> (bool, Vec<String>) {
66 let mut leaks = Vec::new();
67 Self::scan_dir_secrets_inner(root, &mut leaks, 0);
68 let clean = leaks.is_empty();
69 (clean, leaks)
70 }
71
72 fn scan_dir_secrets_inner(dir: &Path, leaks: &mut Vec<String>, depth: usize) {
73 if depth > 8 {
74 return;
75 }
76 let entries = match fs::read_dir(dir) {
77 Ok(e) => e,
78 Err(_) => return,
79 };
80
81 for entry in entries.flatten() {
82 let path = entry.path();
83 let name = path.file_name().and_then(|n| n.to_str()).unwrap_or_default();
84
85 if name == "target" || name == ".git" {
86 continue;
87 }
88
89 let path_str = path.display().to_string();
90 if path_str.ends_with("src/verifier.rs") || path_str.ends_with("tests/integration_tests.rs") {
91 continue;
92 }
93
94 if path.is_file() {
95 if name == ".env" || name.starts_with(".env.") {
96 leaks.push(format!("Found prohibited env file on disk: {}", path.display()));
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.push(format!("Found private key signature in {}", path.display()));
109 break;
110 }
111 }
112 }
113 } else if path.is_dir() {
114 Self::scan_dir_secrets_inner(&path, leaks, depth + 1);
115 }
116 }
117 }
118
119 pub fn verify_compilation_and_tests(target_dir: &Path) -> (bool, Option<String>, bool, Option<String>) {
120 let cargo_toml = target_dir.join("Cargo.toml");
121 if !cargo_toml.exists() {
122 return (true, None, true, Some("No Cargo.toml present; skipped".to_string()));
123 }
124
125 let check_res = Command::new("cargo")
126 .arg("check")
127 .arg("--manifest-path")
128 .arg(&cargo_toml)
129 .output();
130
131 let (compile_passed, compile_err) = match check_res {
132 Ok(out) => {
133 if out.status.success() {
134 (true, None)
135 } else {
136 let err = String::from_utf8_lossy(&out.stderr).to_string();
137 (false, Some(err))
138 }
139 }
140 Err(e) => (false, Some(format!("Failed to spawn cargo check: {}", e))),
141 };
142
143 if !compile_passed {
144 return (false, compile_err, false, None);
145 }
146
147 let test_res = Command::new("cargo")
148 .arg("test")
149 .arg("--manifest-path")
150 .arg(&cargo_toml)
151 .arg("--")
152 .arg("--nocapture")
153 .output();
154
155 let (test_passed, test_summary) = match test_res {
156 Ok(out) => {
157 let stdout = String::from_utf8_lossy(&out.stdout).to_string();
158 let stderr = String::from_utf8_lossy(&out.stderr).to_string();
159 let summary = if out.status.success() {
160 "All tests passed".to_string()
161 } else {
162 format!("Test failures:\n{}\n{}", stdout, stderr)
163 };
164 (out.status.success(), Some(summary))
165 }
166 Err(e) => (false, Some(format!("Failed to spawn cargo test: {}", e))),
167 };
168
169 (compile_passed, compile_err, test_passed, test_summary)
170 }
171
172 pub fn run_full_verification(target_dir: &Path) -> InvariantReport {
173 let mut notes = Vec::new();
174
175 let mut total_em = 0;
176 let mut total_en = 0;
177 let mut buzzwords = Vec::new();
178 let mut tropes = Vec::new();
179
180 let check_files = ["README.md", "docs/PHILOSOPHY.md", "CONTRIBUTING.md"];
181 for rel in &check_files {
182 let p = target_dir.join(rel);
183 if p.exists() {
184 if let Ok(content) = fs::read_to_string(&p) {
185 let (_, em, en, bw, tr) = Self::verify_unslop_text(&content);
186 total_em += em;
187 total_en += en;
188 buzzwords.extend(bw);
189 tropes.extend(tr);
190 }
191 }
192 }
193 buzzwords.sort();
194 buzzwords.dedup();
195 tropes.sort();
196 tropes.dedup();
197
198 let unslop_clean = total_em == 0 && total_en == 0 && buzzwords.is_empty() && tropes.is_empty();
199 if !unslop_clean {
200 notes.push(format!(
201 "Unslop violation: em_dashes={}, en_dashes={}, buzzwords={:?}, tropes={:?}",
202 total_em, total_en, buzzwords, tropes
203 ));
204 }
205
206 let (secrets_clean, leaks) = Self::scan_dir_for_secrets(target_dir);
207 if !secrets_clean {
208 notes.push(format!("Zero disk secrets violation: {:?}", leaks));
209 }
210
211 let (compile_ok, compile_err, test_ok, test_summary) =
212 Self::verify_compilation_and_tests(target_dir);
213
214 if !compile_ok {
215 notes.push("Compilation failed".to_string());
216 }
217 if !test_ok {
218 notes.push("Tests failed".to_string());
219 }
220
221 let passed = unslop_clean && secrets_clean && compile_ok && test_ok;
222
223 InvariantReport {
224 passed,
225 unslop_clean,
226 em_dash_detected: total_em,
227 en_dash_detected: total_en,
228 forbidden_buzzwords_detected: buzzwords,
229 antithesis_tropes_detected: tropes,
230 zero_disk_secrets_clean: secrets_clean,
231 secret_leaks: leaks,
232 compilation_passed: compile_ok,
233 compilation_error: compile_err,
234 tests_passed: test_ok,
235 test_output_summary: test_summary,
236 notes,
237 }
238 }
239}
240
241#[cfg(test)]
242mod tests {
243 use super::*;
244
245 #[test]
246 fn test_unslop_clean_text() {
247 let sample = "High performance Rust code running on SparkOS hardware.";
248 let (clean, em, en, bw, tr) = InvariantVerifier::verify_unslop_text(sample);
249 assert!(clean);
250 assert_eq!(em, 0);
251 assert_eq!(en, 0);
252 assert!(bw.is_empty());
253 assert!(tr.is_empty());
254 }
255
256 #[test]
257 fn test_unslop_detects_em_dash() {
258 let sample = "This is good \u{2014} but that is bad.";
259 let (clean, em, _, _, _) = InvariantVerifier::verify_unslop_text(sample);
260 assert!(!clean);
261 assert_eq!(em, 1);
262 }
263
264 #[test]
265 fn test_unslop_detects_buzzwords() {
266 let sample = "We will delve into this crucial topic.";
267 let (clean, _, _, bw, _) = InvariantVerifier::verify_unslop_text(sample);
268 assert!(!clean);
269 assert!(bw.contains(&"delve".to_string()));
270 assert!(bw.contains(&"crucial".to_string()));
271 }
272}