spark-inquisitor
Ultra-fast compiled native Rust gatekeeper and automated code reviewer
Brought by drakestapleton
spark-inquisitor / src / constitution.rs
| 1 | use regex::Regex; |
| 2 | use std::sync::OnceLock; |
| 3 | |
| 4 | static AWS_REGEX: OnceLock<Regex> = OnceLock::new(); |
| 5 | static GITHUB_PAT_REGEX: OnceLock<Regex> = OnceLock::new(); |
| 6 | static GITHUB_FINE_PAT_REGEX: OnceLock<Regex> = OnceLock::new(); |
| 7 | static PRIV_KEY_REGEX: OnceLock<Regex> = OnceLock::new(); |
| 8 | static DELVE_RE: OnceLock<Regex> = OnceLock::new(); |
| 9 | static TAPESTRY_RE: OnceLock<Regex> = OnceLock::new(); |
| 10 | static TESTAMENT_RE: OnceLock<Regex> = OnceLock::new(); |
| 11 | static CRUCIAL_RE: OnceLock<Regex> = OnceLock::new(); |
| 12 | static PIVOTAL_RE: OnceLock<Regex> = OnceLock::new(); |
| 13 | static ELEVATE_RE: OnceLock<Regex> = OnceLock::new(); |
| 14 | static GAME_CHANGER_RE: OnceLock<Regex> = OnceLock::new(); |
| 15 | static UNLEASH_RE: OnceLock<Regex> = OnceLock::new(); |
| 16 | static BEACON_TROPE_RE: OnceLock<Regex> = OnceLock::new(); |
| 17 | static HARNESS_VERB_RE: OnceLock<Regex> = OnceLock::new(); |
| 18 | static ANTITHESIS_NOT_ONLY_RE: OnceLock<Regex> = OnceLock::new(); |
| 19 | static ANTITHESIS_NOT_X_BUT_Y_RE: OnceLock<Regex> = OnceLock::new(); |
| 20 | |
| 21 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 22 | pub enum ViolationKind { |
| 23 | Dash(String), |
| 24 | Buzzword(String), |
| 25 | AntithesisTrope(String), |
| 26 | Secret(String), |
| 27 | EnvFile(String), |
| 28 | } |
| 29 | |
| 30 | pub struct ConstitutionalChecker; |
| 31 | |
| 32 | impl ConstitutionalChecker { |
| 33 | pub fn check_line(line: &str, _file_path: &str) -> Vec<ViolationKind> { |
| 34 | let mut violations = Vec::new(); |
| 35 | |
| 36 | // 1. Dash detection: em dash (\u{2014}) and en dash (\u{2013}) |
| 37 | if line.contains('\u{2014}') || line.contains('\u{2013}') { |
| 38 | violations.push(ViolationKind::Dash(format!( |
| 39 | "Em/En dash detected in line: {}", |
| 40 | line.trim() |
| 41 | ))); |
| 42 | } |
| 43 | |
| 44 | let lower = line.to_lowercase(); |
| 45 | |
| 46 | // 2. Plaintext Secrets |
| 47 | // AWS Access Key ID: AKIA followed by 16 alphanumeric uppercase |
| 48 | let aws_regex = AWS_REGEX.get_or_init(|| Regex::new(r"\bAKIA[0-9A-Z]{16}\b").unwrap()); |
| 49 | if aws_regex.is_match(line) { |
| 50 | violations.push(ViolationKind::Secret( |
| 51 | "Plaintext AWS Access Key detected in line: [REDACTED_AWS_KEY]".to_string(), |
| 52 | )); |
| 53 | } |
| 54 | |
| 55 | // GitHub Personal Access Tokens (ghp_... or github_pat_...) |
| 56 | let github_pat_regex = |
| 57 | GITHUB_PAT_REGEX.get_or_init(|| Regex::new(r"\bghp_[A-Za-z0-9]{36,}\b").unwrap()); |
| 58 | let github_fine_pat_regex = GITHUB_FINE_PAT_REGEX |
| 59 | .get_or_init(|| Regex::new(r"\bgithub_pat_[A-Za-z0-9_]{50,}\b").unwrap()); |
| 60 | if github_pat_regex.is_match(line) || github_fine_pat_regex.is_match(line) { |
| 61 | violations.push(ViolationKind::Secret( |
| 62 | "Plaintext GitHub Personal Access Token detected in line: [REDACTED_GH_TOKEN]" |
| 63 | .to_string(), |
| 64 | )); |
| 65 | } |
| 66 | |
| 67 | // Private Keys |
| 68 | let priv_key_regex = PRIV_KEY_REGEX.get_or_init(|| { |
| 69 | Regex::new(r"-----BEGIN (?:[A-Z0-9 ]+)?PRIVATE KEY-----|BEGIN RSA PRIVATE KEY").unwrap() |
| 70 | }); |
| 71 | if priv_key_regex.is_match(line) { |
| 72 | violations.push(ViolationKind::Secret( |
| 73 | "Plaintext Private Key header detected in line: [REDACTED_PRIVATE_KEY]".to_string(), |
| 74 | )); |
| 75 | } |
| 76 | |
| 77 | // 3. Banned AI Buzzwords & Tropes |
| 78 | // Delve |
| 79 | let delve_re = DELVE_RE.get_or_init(|| Regex::new(r"\bdelv(?:e|es|ed|ing)\b").unwrap()); |
| 80 | if delve_re.is_match(&lower) { |
| 81 | violations.push(ViolationKind::Buzzword(format!( |
| 82 | "Forbidden AI buzzword 'delve' detected in line: {}", |
| 83 | line.trim() |
| 84 | ))); |
| 85 | } |
| 86 | |
| 87 | // Tapestry |
| 88 | let tapestry_re = TAPESTRY_RE.get_or_init(|| Regex::new(r"\btapestr(?:y|ies)\b").unwrap()); |
| 89 | if tapestry_re.is_match(&lower) { |
| 90 | violations.push(ViolationKind::Buzzword(format!( |
| 91 | "Forbidden AI buzzword 'tapestry' detected in line: {}", |
| 92 | line.trim() |
| 93 | ))); |
| 94 | } |
| 95 | |
| 96 | // Testament |
| 97 | let testament_re = TESTAMENT_RE.get_or_init(|| Regex::new(r"\btestament\b").unwrap()); |
| 98 | if testament_re.is_match(&lower) { |
| 99 | violations.push(ViolationKind::Buzzword(format!( |
| 100 | "Forbidden AI buzzword 'testament' detected in line: {}", |
| 101 | line.trim() |
| 102 | ))); |
| 103 | } |
| 104 | |
| 105 | // Crucial |
| 106 | let crucial_re = CRUCIAL_RE.get_or_init(|| Regex::new(r"\bcrucial\b").unwrap()); |
| 107 | if crucial_re.is_match(&lower) { |
| 108 | violations.push(ViolationKind::Buzzword(format!( |
| 109 | "Forbidden AI buzzword 'crucial' detected in line: {}", |
| 110 | line.trim() |
| 111 | ))); |
| 112 | } |
| 113 | |
| 114 | // Pivotal |
| 115 | let pivotal_re = PIVOTAL_RE.get_or_init(|| Regex::new(r"\bpivotal\b").unwrap()); |
| 116 | if pivotal_re.is_match(&lower) { |
| 117 | violations.push(ViolationKind::Buzzword(format!( |
| 118 | "Forbidden AI buzzword 'pivotal' detected in line: {}", |
| 119 | line.trim() |
| 120 | ))); |
| 121 | } |
| 122 | |
| 123 | // Elevate (exclude CLI flags like --elevate or identifiers with hyphens/underscores) |
| 124 | let elevate_re = |
| 125 | ELEVATE_RE.get_or_init(|| Regex::new(r"\belevat(?:e|es|ed|ing)\b").unwrap()); |
| 126 | if elevate_re.is_match(&lower) |
| 127 | && !lower.contains("--elevate") |
| 128 | && !lower.contains("elevate_") |
| 129 | { |
| 130 | violations.push(ViolationKind::Buzzword(format!( |
| 131 | "Forbidden AI buzzword 'elevate' detected in line: {}", |
| 132 | line.trim() |
| 133 | ))); |
| 134 | } |
| 135 | |
| 136 | // Game-changer |
| 137 | let game_changer_re = |
| 138 | GAME_CHANGER_RE.get_or_init(|| Regex::new(r"\bgame[ -]?changer\b").unwrap()); |
| 139 | if game_changer_re.is_match(&lower) { |
| 140 | violations.push(ViolationKind::Buzzword(format!( |
| 141 | "Forbidden AI buzzword 'game-changer' detected in line: {}", |
| 142 | line.trim() |
| 143 | ))); |
| 144 | } |
| 145 | |
| 146 | // Unleash (exclude CLI flags like --unleash or --unleash-features or identifiers with hyphens/underscores) |
| 147 | let unleash_re = |
| 148 | UNLEASH_RE.get_or_init(|| Regex::new(r"\bunleash(?:es|ed|ing)?\b").unwrap()); |
| 149 | if unleash_re.is_match(&lower) |
| 150 | && !lower.contains("--unleash") |
| 151 | && !lower.contains("unleash_") |
| 152 | { |
| 153 | violations.push(ViolationKind::Buzzword(format!( |
| 154 | "Forbidden AI buzzword 'unleash' detected in line: {}", |
| 155 | line.trim() |
| 156 | ))); |
| 157 | } |
| 158 | |
| 159 | // Beacon: when used as trope |
| 160 | let beacon_trope_re = BEACON_TROPE_RE.get_or_init(|| { |
| 161 | Regex::new( |
| 162 | r"\b(?:(?:a|shining)\s+)?beacon\s+(?:of|for|in|to|that)\b|\bas\s+a\s+beacon\b", |
| 163 | ) |
| 164 | .unwrap() |
| 165 | }); |
| 166 | if beacon_trope_re.is_match(&lower) { |
| 167 | violations.push(ViolationKind::Buzzword(format!( |
| 168 | "Forbidden AI buzzword trope 'beacon' detected in line: {}", |
| 169 | line.trim() |
| 170 | ))); |
| 171 | } |
| 172 | |
| 173 | // Harness (as verb) |
| 174 | let harness_verb_re = HARNESS_VERB_RE.get_or_init(|| Regex::new(r"\b(?:to|can|could|will|would|must|shall|should|may|might|let's|lets)\s+harness\b|\bharness(?:es|ed|ing)\s+(?:the|our|their|its|this|these|those|power|potential|all)\b|\bharnessing\b").unwrap()); |
| 175 | if harness_verb_re.is_match(&lower) { |
| 176 | violations.push(ViolationKind::Buzzword(format!( |
| 177 | "Forbidden AI buzzword 'harness' (as verb) detected in line: {}", |
| 178 | line.trim() |
| 179 | ))); |
| 180 | } |
| 181 | |
| 182 | // 4. Banned Antithesis Tropes: |
| 183 | let antithesis_not_only_re = ANTITHESIS_NOT_ONLY_RE |
| 184 | .get_or_init(|| Regex::new(r"\bnot\s+only\s+[^,;\n]+,\s*but\s+").unwrap()); |
| 185 | let antithesis_not_x_but_y_re = ANTITHESIS_NOT_X_BUT_Y_RE.get_or_init(|| { |
| 186 | Regex::new( |
| 187 | r"\b(?:it(?:'s|\s+is)\s+)?not\s+([a-zA-Z0-9_\-]+),\s*but\s+([a-zA-Z0-9_\-]+)\b", |
| 188 | ) |
| 189 | .unwrap() |
| 190 | }); |
| 191 | if antithesis_not_only_re.is_match(&lower) || antithesis_not_x_but_y_re.is_match(&lower) { |
| 192 | violations.push(ViolationKind::AntithesisTrope(format!( |
| 193 | "Forbidden antithesis trope detected in line: {}", |
| 194 | line.trim() |
| 195 | ))); |
| 196 | } |
| 197 | |
| 198 | violations |
| 199 | } |
| 200 | |
| 201 | pub fn is_env_file(path: &str) -> bool { |
| 202 | let clean_path = path.trim().trim_matches('"').trim_matches('\''); |
| 203 | let filename = clean_path.split('/').next_back().unwrap_or(clean_path); |
| 204 | filename == ".env" || filename.starts_with(".env.") |
| 205 | } |
| 206 | } |