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-adapters/blob/main/src/verifier.rs # spark-adapters Hardware abstraction adapters for local AI accelerators 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-adapters / src / verifier.rs` 188 lines · 6587 bytes Raw Blame History 1 use crate::models::{VerificationOutcome, VerificationStrategy}; 2 3 pub struct Verifier; 4 5 impl Verifier { 6 pub fn verify(content: &str, strategy: VerificationStrategy) -> VerificationOutcome { 7 let mut violations = Vec::new(); 8 let mut score: f32 = 1.0; 9 10 // 1. Unslop and Tone Checks 11 if content.contains('\u{2014}') || content.contains('—') || content.contains("—") { 12 violations.push("Contains forbidden em dash (—)".to_string()); 13 score -= 0.25; 14 } 15 if content.contains('\u{2013}') || content.contains('–') || content.contains("—") { 16 violations.push("Contains forbidden en dash (–)".to_string()); 17 score -= 0.15; 18 } 19 20 let banned_words = [ 21 "delve", "tapestry", "crucial", "beacon", 22 "game-changer", "unleash", "seamlessly", 23 "elevate", "pivotal" 24 ]; 25 let lower = content.to_lowercase(); 26 for word in banned_words { 27 if lower.contains(word) { 28 violations.push(format!("Contains banned AI cliché: '{}'", word)); 29 score -= 0.15; 30 } 31 } 32 33 let sycophancy_phrases = [ 34 "certainly!", "great question!", "sure thing!", 35 "i would be happy to help", "as an ai language model" 36 ]; 37 for phrase in sycophancy_phrases { 38 if lower.contains(phrase) { 39 violations.push(format!("Contains sycophantic phrase: '{}'", phrase)); 40 score -= 0.2; 41 } 42 } 43 44 // 2. Zero-Secret Leak Checks 45 let secret_signatures = [ 46 "-----BEGIN PRIVATE KEY-----", 47 "-----BEGIN OPENSSH PRIVATE KEY-----", 48 "sk-proj-", 49 "ghp_", 50 ]; 51 for sig in secret_signatures { 52 if content.contains(sig) { 53 violations.push(format!("Critical: Contains secret signature: {}", sig)); 54 score = 0.0; 55 } 56 } 57 58 // 3. Strategy-specific validation 59 let mut compiler_output = None; 60 match strategy { 61 VerificationStrategy::CompilerCheck => { 62 if let Some(err) = Self::check_balanced_delimiters(content) { 63 violations.push(format!("Code structure syntax error: {}", err)); 64 score -= 0.3; 65 compiler_output = Some(err); 66 } else { 67 compiler_output = Some("Balanced delimiters and code syntax verified.".to_string()); 68 } 69 } 70 VerificationStrategy::JsonSchema => { 71 if let Some(json_start) = content.find('{') { 72 if let Some(json_end) = content.rfind('}') { 73 let slice = &content[json_start..=json_end]; 74 if serde_json::from_str::(slice).is_err() { 75 violations.push("Embedded JSON payload is malformed".to_string()); 76 score -= 0.3; 77 } 78 } else { 79 violations.push("Incomplete JSON object brackets".to_string()); 80 score -= 0.3; 81 } 82 } 83 } 84 VerificationStrategy::UnslopStrict => { 85 // Strict zero-tolerance for any unslop violation 86 if !violations.is_empty() { 87 score = score.min(0.5); 88 } 89 } 90 VerificationStrategy::DualConsensus => {} 91 } 92 93 score = score.max(0.0).min(1.0); 94 let passed = violations.is_empty() || (score >= 0.70 && !violations.iter().any(|v| v.starts_with("Critical"))); 95 96 VerificationOutcome { 97 passed, 98 score, 99 rule_violations: violations, 100 compiler_output, 101 } 102 } 103 104 fn check_balanced_delimiters(content: &str) -> Option { 105 let mut stack = Vec::new(); 106 let mut in_string = false; 107 let mut escape = false; 108 109 for (i, c) in content.chars().enumerate() { 110 if escape { 111 escape = false; 112 continue; 113 } 114 if c == '\\' { 115 escape = true; 116 continue; 117 } 118 if c == '"' { 119 in_string = !in_string; 120 continue; 121 } 122 if in_string { 123 continue; 124 } 125 126 match c { 127 '(' | '[' | '{' => stack.push((c, i)), 128 ')' => { 129 if let Some(('(', _)) = stack.last() { 130 stack.pop(); 131 } else { 132 return Some(format!("Unmatched ')' at position {}", i)); 133 } 134 } 135 ']' => { 136 if let Some(('[', _)) = stack.last() { 137 stack.pop(); 138 } else { 139 return Some(format!("Unmatched ']' at position {}", i)); 140 } 141 } 142 '}' => { 143 if let Some(('{', _)) = stack.last() { 144 stack.pop(); 145 } else { 146 return Some(format!("Unmatched '}}' at position {}", i)); 147 } 148 } 149 _ => {} 150 } 151 } 152 153 if let Some((unmatched, pos)) = stack.pop() { 154 Some(format!("Unclosed delimiter '{}' opened at position {}", unmatched, pos)) 155 } else { 156 None 157 } 158 } 159 } 160 161 #[cfg(test)] 162 mod tests { 163 use super::*; 164 165 #[test] 166 fn test_clean_response_passes() { 167 let text = "Here is the pure Rust ring buffer implementation with zero allocations."; 168 let out = Verifier::verify(text, VerificationStrategy::UnslopStrict); 169 assert!(out.passed); 170 assert_eq!(out.score, 1.0); 171 assert!(out.rule_violations.is_empty()); 172 } 173 174 #[test] 175 fn test_em_dash_detected() { 176 let text = "This module is fast — and very reliable."; 177 let out = Verifier::verify(text, VerificationStrategy::UnslopStrict); 178 assert!(!out.passed); 179 assert!(out.rule_violations.iter().any(|v| v.contains("em dash"))); 180 } 181 182 #[test] 183 fn test_banned_cliche_detected() { 184 let text = "Let us delve into the architecture."; 185 let out = Verifier::verify(text, VerificationStrategy::UnslopStrict); 186 assert!(out.rule_violations.iter().any(|v| v.contains("delve"))); 187 } 188 }