interplane.Get Omega
Read in your language

aien-sovereign-core

Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core / crates / spark-inquisitor / src / audit.rs

251 lines · 9945 bytes Raw Blame History
1use crate::constitution::{ConstitutionalChecker, ViolationKind};
2
3#[derive(Debug, Clone)]
4pub struct AuditReport {
5 pub clean: bool,
6 pub violations: Vec<String>,
7 pub telemetry_detected: bool,
8 pub unslop_violations: Vec<String>,
9 pub secret_violations: Vec<String>,
10}
11
12impl AuditReport {
13 pub fn generate_markdown_report(&self) -> String {
14 if self.clean {
15 return "#### ✅ Constitutional 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**: Constitutional invariants verified.".to_string();
16 }
17
18 let mut out = String::from("#### ❌ Constitutional Diff Audit: FAILED\n\n");
19 if !self.violations.is_empty() {
20 out.push_str("**Telemetry, Surveillance, and Environment Violations:**\n");
21 for v in &self.violations {
22 out.push_str(&format!("* {}\n", v));
23 }
24 out.push('\n');
25 }
26 if !self.secret_violations.is_empty() {
27 out.push_str("**Plaintext Secrets and Credential Violations:**\n");
28 for s in &self.secret_violations {
29 out.push_str(&format!("* {}\n", s));
30 }
31 out.push('\n');
32 }
33 if !self.unslop_violations.is_empty() {
34 out.push_str("**Sovereign Voice and Unslop Invariant Violations:**\n");
35 for u in &self.unslop_violations {
36 out.push_str(&format!("* {}\n", u));
37 }
38 out.push('\n');
39 }
40 out.push_str("**Action Required:** Remove all detected tracking hooks, secrets, and forbidden tokens before review can proceed.");
41 out
42 }
43}
44
45pub struct DiffAuditor;
46
47impl DiffAuditor {
48 pub fn audit_diff(text: &str) -> AuditReport {
49 Self::audit_text(text)
50 }
51
52 pub fn audit_text(text: &str) -> AuditReport {
53 let mut violations = Vec::new();
54 let mut telemetry_detected = false;
55 let mut unslop_violations = Vec::new();
56 let mut secret_violations = Vec::new();
57
58 let telemetry_patterns = [
59 "google-analytics", "segment.io", "mixpanel", "sentry.io",
60 "datadog", "tracking_id", "analytics.js",
61 "amplitude.com", "hotjar", "clarity.ms",
62 "send_telemetry", "report_telemetry", "collect_telemetry",
63 ];
64
65 let mut current_file = String::new();
66 let mut in_binary_patch = false;
67
68 for line in text.lines() {
69 if line.starts_with("diff --git ") {
70 in_binary_patch = false;
71 current_file = Self::extract_target_file(line).unwrap_or_default();
72 if ConstitutionalChecker::is_env_file(&current_file) {
73 violations.push(format!("Forbidden plaintext .env secret file detected in diff header: {}", current_file));
74 }
75 continue;
76 }
77
78 if line.starts_with("+++ ") {
79 if let Some(target) = Self::extract_plus_file(line) {
80 current_file = target;
81 if ConstitutionalChecker::is_env_file(&current_file) {
82 violations.push(format!("Forbidden plaintext .env secret file added in diff: {}", current_file));
83 }
84 }
85 continue;
86 }
87
88 if line.starts_with("Binary files ") {
89 if let Some(target) = Self::extract_binary_target(line) {
90 if ConstitutionalChecker::is_env_file(&target) {
91 violations.push(format!("Forbidden plaintext .env secret file in binary diff: {}", target));
92 }
93 }
94 continue;
95 }
96
97 if line.starts_with("GIT binary patch") {
98 in_binary_patch = true;
99 continue;
100 }
101
102 if in_binary_patch {
103 continue;
104 }
105
106 // Exclude inquisitor internal pattern definitions from false positives
107 if current_file.contains("spark-inquisitor") || current_file.contains("constitution.rs") {
108 continue;
109 }
110
111 let lower = line.to_lowercase();
112
113 // Telemetry pattern scanning
114 for pattern in &telemetry_patterns {
115 if lower.contains(pattern) && (line.starts_with('+') && !line.starts_with("+++")) {
116 violations.push(format!("Telemetry pattern '{}' detected in added line: {}", pattern, line.trim()));
117 telemetry_detected = true;
118 }
119 }
120
121 // In non-markdown source code, also flag raw telemetry calls
122 if !current_file.ends_with(".md") && line.starts_with('+') && !line.starts_with("+++") {
123 if lower.contains("telemetry")
124 && !lower.contains("gpu-telemetry")
125 && !lower.contains("zero telemetry") && !lower.contains("zero-telemetry") && !lower.contains("no-telemetry")
126 && !lower.contains("no telemetry")
127 && !lower.contains("anti-telemetry")
128 && !lower.contains("block telemetry")
129 {
130 violations.push(format!("Telemetry indicator detected in source line: {}", line.trim()));
131 telemetry_detected = true;
132 }
133 }
134
135 // Constitutional unslop and secret invariant scanning on added lines
136 if line.starts_with('+') && !line.starts_with("+++") {
137 let const_violations = ConstitutionalChecker::check_line(line, &current_file);
138 for cv in const_violations {
139 match cv {
140 ViolationKind::Dash(msg) => unslop_violations.push(msg),
141 ViolationKind::Buzzword(msg) => unslop_violations.push(msg),
142 ViolationKind::AntithesisTrope(msg) => unslop_violations.push(msg),
143 ViolationKind::Secret(msg) => secret_violations.push(msg),
144 ViolationKind::EnvFile(msg) => violations.push(msg),
145 }
146 }
147 }
148 }
149
150 let clean = violations.is_empty() && unslop_violations.is_empty() && secret_violations.is_empty();
151
152 AuditReport {
153 clean,
154 violations,
155 telemetry_detected,
156 unslop_violations,
157 secret_violations,
158 }
159 }
160
161 fn extract_target_file(line: &str) -> Option<String> {
162 let remainder = line.strip_prefix("diff --git ")?.trim();
163 if remainder.contains('"') {
164 let parts: Vec<&str> = remainder.split("\" \"").collect();
165 if parts.len() == 2 {
166 let target = parts[1].trim_matches('"');
167 let stripped = target.strip_prefix("b/").unwrap_or(target);
168 return Some(stripped.to_string());
169 }
170 }
171 let tokens: Vec<&str> = remainder.split_whitespace().collect();
172 if tokens.len() >= 2 {
173 let target = tokens[1];
174 let stripped = target.strip_prefix("b/").unwrap_or(target);
175 return Some(stripped.to_string());
176 }
177 None
178 }
179
180 fn extract_plus_file(line: &str) -> Option<String> {
181 let path = line.strip_prefix("+++ ")?.trim();
182 if path == "/dev/null" {
183 return None;
184 }
185 let clean = path.trim_matches('"');
186 let stripped = clean.strip_prefix("b/").unwrap_or(clean);
187 Some(stripped.to_string())
188 }
189
190 fn extract_binary_target(line: &str) -> Option<String> {
191 // e.g. "Binary files /dev/null and b/.env differ"
192 let tokens: Vec<&str> = line.split_whitespace().collect();
193 if tokens.len() >= 5 {
194 let target = tokens[4].trim_matches('"');
195 let stripped = target.strip_prefix("b/").unwrap_or(target);
196 return Some(stripped.to_string());
197 }
198 None
199 }
200}
201
202#[cfg(test)]
203mod tests {
204 use super::*;
205
206 #[test]
207 fn test_clean_diff() {
208 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+}";
209 let report = DiffAuditor::audit_text(diff);
210 assert!(report.clean);
211 let md = report.generate_markdown_report();
212 assert!(md.contains("PASS"));
213 assert!(!md.contains('\u{2014}'));
214 assert!(!md.contains('\u{2013}'));
215 }
216
217 #[test]
218 fn test_telemetry_flagged() {
219 let diff = "diff --git a/src/net.rs b/src/net.rs\n+ let _ = send_telemetry(\"https://google-analytics.com/collect\");";
220 let report = DiffAuditor::audit_text(diff);
221 assert!(!report.clean);
222 assert!(report.telemetry_detected);
223 let md = report.generate_markdown_report();
224 assert!(md.contains("FAILED"));
225 assert!(md.contains("google-analytics"));
226 }
227
228 #[test]
229 fn test_unslop_flagged() {
230 let diff = "diff --git a/src/lib.rs b/src/lib.rs\n+// This is a game-changer feature -- unleash seamless power.";
231 let report = DiffAuditor::audit_text(diff);
232 assert!(!report.clean);
233 assert!(!report.unslop_violations.is_empty());
234 }
235
236 #[test]
237 fn test_secret_flagged() {
238 let diff = "diff --git a/src/config.rs b/src/config.rs\n+let key = \"AKIA1234567890ABCDEF\";";
239 let report = DiffAuditor::audit_text(diff);
240 assert!(!report.clean);
241 assert!(!report.secret_violations.is_empty());
242 }
243
244 #[test]
245 fn test_env_file_flagged() {
246 let diff = "diff --git a/.env b/.env\n+SECRET_KEY=12345";
247 let report = DiffAuditor::audit_text(diff);
248 assert!(!report.clean);
249 assert!(!report.violations.is_empty());
250 }
251}