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