aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-inquisitor / src / audit.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use crate::constitution::{ConstitutionalChecker, ViolationKind}; |
| 17 | |
| 18 | /// Version of the audit report and exit-status contract. |
| 19 | /// |
| 20 | /// Format 2 (DRA-99): style findings (em/en dashes, banned buzzwords, antithesis |
| 21 | /// tropes) are advisory. They are still collected in `unslop_violations` and printed, |
| 22 | /// but they never make the report blocking. Plaintext secrets, `.env` files and |
| 23 | /// telemetry hooks stay blocking. Format 1 treated every finding as blocking. |
| 24 | pub const REPORT_FORMAT: &str = "spark-inquisitor-report/2"; |
| 25 | |
| 26 | #[derive(Debug, Clone)] |
| 27 | pub struct AuditReport { |
| 28 | /// True only when there are no findings at all, advisory ones included. |
| 29 | pub clean: bool, |
| 30 | /// Blocking: telemetry hooks and `.env` files. |
| 31 | pub violations: Vec<String>, |
| 32 | pub telemetry_detected: bool, |
| 33 | /// Advisory since report format 2: dashes, buzzwords, antithesis tropes. |
| 34 | pub unslop_violations: Vec<String>, |
| 35 | /// Blocking: plaintext secrets (cloud keys, tokens, private keys). |
| 36 | pub secret_violations: Vec<String>, |
| 37 | } |
| 38 | |
| 39 | impl AuditReport { |
| 40 | /// True when the diff carries a finding that must stop a merge: a plaintext |
| 41 | /// secret, a `.env` file or a telemetry hook. Style findings never count. |
| 42 | pub fn has_blocking_findings(&self) -> bool { |
| 43 | !self.violations.is_empty() || !self.secret_violations.is_empty() || self.telemetry_detected |
| 44 | } |
| 45 | |
| 46 | fn push_style_advisories(&self, out: &mut String) { |
| 47 | if !self.unslop_violations.is_empty() { |
| 48 | out.push_str("**Style advisories (non-blocking):**\n"); |
| 49 | for u in &self.unslop_violations { |
| 50 | out.push_str(&format!("* {}\n", u)); |
| 51 | } |
| 52 | out.push('\n'); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pub fn generate_markdown_report(&self) -> String { |
| 57 | let format_line = format!( |
| 58 | "- **Report format**: {} (style findings are advisory; secrets, .env files and telemetry block).", |
| 59 | REPORT_FORMAT |
| 60 | ); |
| 61 | |
| 62 | if self.clean { |
| 63 | return format!("#### ✅ Doctrine Diff Audit: PASS\n- **Telemetry Check**: Clean (zero tracking or surveillance hooks detected).\n- **Sovereign Voice & Formatting Check**: Clean (zero em/en dashes, zero banned buzzwords).\n- **Secrets & Credentials Check**: Clean (zero plaintext secrets, hardware vault only).\n- **Status**: Project standards verified.\n{}", format_line); |
| 64 | } |
| 65 | |
| 66 | if !self.has_blocking_findings() { |
| 67 | let mut out = format!("#### ✅ Doctrine Diff Audit: PASS (style advisories)\n- **Telemetry Check**: Clean (zero tracking or surveillance hooks detected).\n- **Secrets & Credentials Check**: Clean (zero plaintext secrets, zero .env files).\n- **Sovereign Voice & Formatting Check**: {} advisory finding(s), listed below; they do not block.\n{}\n\n", self.unslop_violations.len(), format_line); |
| 68 | self.push_style_advisories(&mut out); |
| 69 | out.push_str("**Status**: No blocking findings."); |
| 70 | return out; |
| 71 | } |
| 72 | |
| 73 | let mut out = String::from("#### ❌ Doctrine Diff Audit: FAILED\n\n"); |
| 74 | if !self.violations.is_empty() { |
| 75 | out.push_str("**Telemetry, Surveillance, and Environment Violations:**\n"); |
| 76 | for v in &self.violations { |
| 77 | out.push_str(&format!("* {}\n", v)); |
| 78 | } |
| 79 | out.push('\n'); |
| 80 | } |
| 81 | if !self.secret_violations.is_empty() { |
| 82 | out.push_str("**Plaintext Secrets and Credential Violations:**\n"); |
| 83 | for s in &self.secret_violations { |
| 84 | out.push_str(&format!("* {}\n", s)); |
| 85 | } |
| 86 | out.push('\n'); |
| 87 | } |
| 88 | self.push_style_advisories(&mut out); |
| 89 | out.push_str(&format!("{}\n\n", format_line)); |
| 90 | out.push_str("**Action Required:** Remove the blocking findings above (tracking hooks, plaintext secrets, .env files) before review can proceed. Style advisories do not block."); |
| 91 | out |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub struct DiffAuditor; |
| 96 | |
| 97 | impl DiffAuditor { |
| 98 | pub fn audit_diff(text: &str) -> AuditReport { |
| 99 | Self::audit_text(text) |
| 100 | } |
| 101 | |
| 102 | pub fn audit_text(text: &str) -> AuditReport { |
| 103 | let mut violations = Vec::new(); |
| 104 | let mut telemetry_detected = false; |
| 105 | let mut unslop_violations = Vec::new(); |
| 106 | let mut secret_violations = Vec::new(); |
| 107 | |
| 108 | let telemetry_patterns = [ |
| 109 | "google-analytics", |
| 110 | "segment.io", |
| 111 | "mixpanel", |
| 112 | "sentry.io", |
| 113 | "datadog", |
| 114 | "tracking_id", |
| 115 | "analytics.js", |
| 116 | "amplitude.com", |
| 117 | "hotjar", |
| 118 | "clarity.ms", |
| 119 | "send_telemetry", |
| 120 | "report_telemetry", |
| 121 | "collect_telemetry", |
| 122 | ]; |
| 123 | |
| 124 | let mut current_file = String::new(); |
| 125 | let mut in_binary_patch = false; |
| 126 | |
| 127 | for line in text.lines() { |
| 128 | if line.starts_with("diff --git ") { |
| 129 | in_binary_patch = false; |
| 130 | current_file = Self::extract_target_file(line).unwrap_or_default(); |
| 131 | if ConstitutionalChecker::is_env_file(¤t_file) { |
| 132 | violations.push(format!( |
| 133 | "Forbidden plaintext .env secret file detected in diff header: {}", |
| 134 | current_file |
| 135 | )); |
| 136 | } |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | if line.starts_with("+++ ") { |
| 141 | if let Some(target) = Self::extract_plus_file(line) { |
| 142 | current_file = target; |
| 143 | if ConstitutionalChecker::is_env_file(¤t_file) { |
| 144 | violations.push(format!( |
| 145 | "Forbidden plaintext .env secret file added in diff: {}", |
| 146 | current_file |
| 147 | )); |
| 148 | } |
| 149 | } |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | if line.starts_with("Binary files ") { |
| 154 | if let Some(target) = Self::extract_binary_target(line) { |
| 155 | if ConstitutionalChecker::is_env_file(&target) { |
| 156 | violations.push(format!( |
| 157 | "Forbidden plaintext .env secret file in binary diff: {}", |
| 158 | target |
| 159 | )); |
| 160 | } |
| 161 | } |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | if line.starts_with("GIT binary patch") { |
| 166 | in_binary_patch = true; |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | if in_binary_patch { |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | // Exclude inquisitor internal pattern definitions from false positives |
| 175 | if current_file.contains("spark-inquisitor") || current_file.contains("constitution.rs") |
| 176 | { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | let lower = line.to_lowercase(); |
| 181 | |
| 182 | // Telemetry pattern scanning |
| 183 | for pattern in &telemetry_patterns { |
| 184 | if lower.contains(pattern) && (line.starts_with('+') && !line.starts_with("+++")) { |
| 185 | violations.push(format!( |
| 186 | "Telemetry pattern '{}' detected in added line: {}", |
| 187 | pattern, |
| 188 | ConstitutionalChecker::displayable(line) |
| 189 | )); |
| 190 | telemetry_detected = true; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // In non-markdown source code, also flag raw telemetry calls |
| 195 | if !current_file.ends_with(".md") |
| 196 | && line.starts_with('+') |
| 197 | && !line.starts_with("+++") |
| 198 | && lower.contains("telemetry") |
| 199 | && !lower.contains("gpu-telemetry") |
| 200 | && !lower.contains("zero telemetry") |
| 201 | && !lower.contains("zero-telemetry") |
| 202 | && !lower.contains("no-telemetry") |
| 203 | && !lower.contains("no telemetry") |
| 204 | && !lower.contains("anti-telemetry") |
| 205 | && !lower.contains("block telemetry") |
| 206 | && !lower.contains("telemetryforbidden") |
| 207 | && !lower.contains("networkpurpose::telemetry") |
| 208 | && !lower.contains("forbid") |
| 209 | && !lower.contains("reject") |
| 210 | { |
| 211 | violations.push(format!( |
| 212 | "Telemetry indicator detected in source line: {}", |
| 213 | ConstitutionalChecker::displayable(line) |
| 214 | )); |
| 215 | telemetry_detected = true; |
| 216 | } |
| 217 | |
| 218 | // Constitutional unslop and secret invariant scanning on added lines |
| 219 | if line.starts_with('+') && !line.starts_with("+++") { |
| 220 | let const_violations = ConstitutionalChecker::check_line(line, ¤t_file); |
| 221 | for cv in const_violations { |
| 222 | match cv { |
| 223 | ViolationKind::Dash(msg) => unslop_violations.push(msg), |
| 224 | ViolationKind::Buzzword(msg) => unslop_violations.push(msg), |
| 225 | ViolationKind::AntithesisTrope(msg) => unslop_violations.push(msg), |
| 226 | ViolationKind::Secret(msg) => secret_violations.push(msg), |
| 227 | ViolationKind::EnvFile(msg) => violations.push(msg), |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | let clean = |
| 234 | violations.is_empty() && unslop_violations.is_empty() && secret_violations.is_empty(); |
| 235 | |
| 236 | AuditReport { |
| 237 | clean, |
| 238 | violations, |
| 239 | telemetry_detected, |
| 240 | unslop_violations, |
| 241 | secret_violations, |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | fn extract_target_file(line: &str) -> Option<String> { |
| 246 | let remainder = line.strip_prefix("diff --git ")?.trim(); |
| 247 | if remainder.contains('"') { |
| 248 | let parts: Vec<&str> = remainder.split("\" \"").collect(); |
| 249 | if parts.len() == 2 { |
| 250 | let target = parts[1].trim_matches('"'); |
| 251 | let stripped = target.strip_prefix("b/").unwrap_or(target); |
| 252 | return Some(stripped.to_string()); |
| 253 | } |
| 254 | } |
| 255 | let tokens: Vec<&str> = remainder.split_whitespace().collect(); |
| 256 | if tokens.len() >= 2 { |
| 257 | let target = tokens[1]; |
| 258 | let stripped = target.strip_prefix("b/").unwrap_or(target); |
| 259 | return Some(stripped.to_string()); |
| 260 | } |
| 261 | None |
| 262 | } |
| 263 | |
| 264 | fn extract_plus_file(line: &str) -> Option<String> { |
| 265 | let path = line.strip_prefix("+++ ")?.trim(); |
| 266 | if path == "/dev/null" { |
| 267 | return None; |
| 268 | } |
| 269 | let clean = path.trim_matches('"'); |
| 270 | let stripped = clean.strip_prefix("b/").unwrap_or(clean); |
| 271 | Some(stripped.to_string()) |
| 272 | } |
| 273 | |
| 274 | fn extract_binary_target(line: &str) -> Option<String> { |
| 275 | // e.g. "Binary files /dev/null and b/.env differ" |
| 276 | let tokens: Vec<&str> = line.split_whitespace().collect(); |
| 277 | if tokens.len() >= 5 { |
| 278 | let target = tokens[4].trim_matches('"'); |
| 279 | let stripped = target.strip_prefix("b/").unwrap_or(target); |
| 280 | return Some(stripped.to_string()); |
| 281 | } |
| 282 | None |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | #[cfg(test)] |
| 287 | mod tests { |
| 288 | use super::*; |
| 289 | |
| 290 | #[test] |
| 291 | fn test_clean_diff() { |
| 292 | let diff = "diff --git a/src/lib.rs b/src/lib.rs\n+pub fn add(a: i32, b: i32) -> i32 {\n+ a + b\n+}"; |
| 293 | let report = DiffAuditor::audit_text(diff); |
| 294 | assert!(report.clean); |
| 295 | let md = report.generate_markdown_report(); |
| 296 | assert!(md.contains("PASS")); |
| 297 | assert!(!md.contains('\u{2014}')); |
| 298 | assert!(!md.contains('\u{2013}')); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn test_telemetry_flagged() { |
| 303 | let diff = "diff --git a/src/net.rs b/src/net.rs\n+ let _ = send_telemetry(\"https://google-analytics.com/collect\");"; |
| 304 | let report = DiffAuditor::audit_text(diff); |
| 305 | assert!(!report.clean); |
| 306 | assert!(report.telemetry_detected); |
| 307 | let md = report.generate_markdown_report(); |
| 308 | assert!(md.contains("FAILED")); |
| 309 | assert!(md.contains("google-analytics")); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn test_unslop_flagged() { |
| 314 | let diff = "diff --git a/src/lib.rs b/src/lib.rs\n+// This is a game-changer feature -- unleash seamless power."; |
| 315 | let report = DiffAuditor::audit_text(diff); |
| 316 | assert!(!report.clean); |
| 317 | assert!(!report.unslop_violations.is_empty()); |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn test_secret_flagged() { |
| 322 | let diff = |
| 323 | "diff --git a/src/config.rs b/src/config.rs\n+let key = \"AKIA1234567890ABCDEF\";"; |
| 324 | let report = DiffAuditor::audit_text(diff); |
| 325 | assert!(!report.clean); |
| 326 | assert!(!report.secret_violations.is_empty()); |
| 327 | } |
| 328 | |
| 329 | #[test] |
| 330 | fn test_env_file_flagged() { |
| 331 | let diff = "diff --git a/.env b/.env\n+SECRET_KEY=12345"; |
| 332 | let report = DiffAuditor::audit_text(diff); |
| 333 | assert!(!report.clean); |
| 334 | assert!(!report.violations.is_empty()); |
| 335 | assert!(report.has_blocking_findings()); |
| 336 | } |
| 337 | |
| 338 | // DRA-99, report format 2: style findings alone are advisory. |
| 339 | #[test] |
| 340 | fn test_style_only_findings_are_advisory() { |
| 341 | let diff = "diff --git a/src/lib.rs b/src/lib.rs\n+// note \u{2014} a dash, a game-changer, and we delve"; |
| 342 | let report = DiffAuditor::audit_text(diff); |
| 343 | assert!(!report.clean); |
| 344 | assert_eq!(report.unslop_violations.len(), 3); |
| 345 | assert!(!report.has_blocking_findings()); |
| 346 | let md = report.generate_markdown_report(); |
| 347 | assert!(md.contains("Doctrine Diff Audit: PASS (style advisories)")); |
| 348 | assert!(md.contains("Style advisories (non-blocking)")); |
| 349 | assert!(md.contains("Em/En dash detected")); |
| 350 | assert!(md.contains(REPORT_FORMAT)); |
| 351 | assert!(!md.contains("FAILED")); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn test_secret_stays_blocking_beside_style_findings() { |
| 356 | let key = format!("AKIA{}", "ABCDEFGHIJKLMNOP"); |
| 357 | let diff = format!( |
| 358 | "diff --git a/src/config.rs b/src/config.rs\n+let key = \"{}\"; // \u{2014} dash too", |
| 359 | key |
| 360 | ); |
| 361 | let report = DiffAuditor::audit_text(&diff); |
| 362 | assert!(report.has_blocking_findings()); |
| 363 | assert!(!report.secret_violations.is_empty()); |
| 364 | assert!(!report.unslop_violations.is_empty()); |
| 365 | let md = report.generate_markdown_report(); |
| 366 | assert!(md.contains("Doctrine Diff Audit: FAILED")); |
| 367 | assert!(md.contains("Plaintext Secrets and Credential Violations")); |
| 368 | assert!(md.contains("Style advisories (non-blocking)")); |
| 369 | assert!(md.contains(REPORT_FORMAT)); |
| 370 | } |
| 371 | |
| 372 | #[test] |
| 373 | fn test_github_token_and_private_key_block() { |
| 374 | let token = format!("ghp_{}", "A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q7r8"); |
| 375 | let diff = format!("diff --git a/ci.sh b/ci.sh\n+export GH_TOKEN={}\n", token); |
| 376 | assert!(DiffAuditor::audit_text(&diff).has_blocking_findings()); |
| 377 | let diff = format!( |
| 378 | "diff --git a/id b/id\n+-----BEGIN OPENSSH {}-----\n", |
| 379 | "PRIVATE KEY" |
| 380 | ); |
| 381 | assert!(DiffAuditor::audit_text(&diff).has_blocking_findings()); |
| 382 | } |
| 383 | |
| 384 | #[test] |
| 385 | fn test_env_file_blocks_even_without_added_lines() { |
| 386 | let diff = |
| 387 | "diff --git a/config/.env.production b/config/.env.production\nnew file mode 100644\n"; |
| 388 | let report = DiffAuditor::audit_text(diff); |
| 389 | assert!(report.has_blocking_findings()); |
| 390 | } |
| 391 | |
| 392 | #[test] |
| 393 | fn test_no_finding_echoes_a_secret() { |
| 394 | let key = format!("AKIA{}", "ZXCVBNMLKJHGFDSA"); |
| 395 | let diff = format!( |
| 396 | "diff --git a/src/net.rs b/src/net.rs\n+send_telemetry(\"{}\"); // game-changer \u{2014} mixpanel\n", |
| 397 | key |
| 398 | ); |
| 399 | let report = DiffAuditor::audit_text(&diff); |
| 400 | assert!(report.has_blocking_findings()); |
| 401 | assert!(report.telemetry_detected); |
| 402 | assert!(!report.unslop_violations.is_empty()); |
| 403 | let md = report.generate_markdown_report(); |
| 404 | assert!(md.contains(crate::constitution::REDACTED_SECRET_LINE)); |
| 405 | assert!(!md.contains(&key)); |
| 406 | } |
| 407 | |
| 408 | #[test] |
| 409 | fn test_clean_report_names_format() { |
| 410 | let report = DiffAuditor::audit_text("diff --git a/a.rs b/a.rs\n+fn a() {}\n"); |
| 411 | assert!(report.clean); |
| 412 | assert!(!report.has_blocking_findings()); |
| 413 | assert!(report.generate_markdown_report().contains(REPORT_FORMAT)); |
| 414 | } |
| 415 | } |