spark-inquisitor
Ultra-fast compiled native Rust gatekeeper and automated code reviewer
Brought by drakestapleton
spark-inquisitor / src / main.rs
| 1 | use clap::{Parser, Subcommand}; |
| 2 | use colored::*; |
| 3 | use spark_inquisitor::{ |
| 4 | generate_inquisitor_interview, generate_pr_review, triage_issue, DiffAuditor, |
| 5 | TestimonyEvaluator, |
| 6 | }; |
| 7 | use std::fs; |
| 8 | use std::path::PathBuf; |
| 9 | |
| 10 | #[derive(Parser, Debug)] |
| 11 | #[command(name = "spark-inquisitor")] |
| 12 | #[command(author = "Drake Stapleton <drake.aien@proton.me> & AIEN <aien.atlas@proton.me>")] |
| 13 | #[command(version = "0.2.0")] |
| 14 | #[command(about = "Autonomous Sovereign Inquisitor & Pull Request Alignment Gatekeeper")] |
| 15 | struct Cli { |
| 16 | #[command(subcommand)] |
| 17 | command: Commands, |
| 18 | } |
| 19 | |
| 20 | #[derive(Subcommand, Debug)] |
| 21 | enum Commands { |
| 22 | /// Review a PR diff and generate unified code review markdown comment |
| 23 | Review { |
| 24 | #[arg(long, help = "GitHub username of PR author")] |
| 25 | author: String, |
| 26 | |
| 27 | #[arg(long, help = "Pull Request number")] |
| 28 | pr: u64, |
| 29 | |
| 30 | #[arg( |
| 31 | long, |
| 32 | default_value = "External Contribution", |
| 33 | help = "Pull Request title" |
| 34 | )] |
| 35 | title: String, |
| 36 | |
| 37 | #[arg(long, help = "Path to diff file")] |
| 38 | diff: PathBuf, |
| 39 | |
| 40 | #[arg(long, help = "Path to write comment markdown")] |
| 41 | output: Option<PathBuf>, |
| 42 | |
| 43 | #[arg(long, help = "Path to write labels text (comma-separated)")] |
| 44 | output_labels: Option<PathBuf>, |
| 45 | }, |
| 46 | |
| 47 | /// Triage an issue (bug report, complaint, feature request, inquiry) |
| 48 | TriageIssue { |
| 49 | #[arg(long, help = "GitHub username of issue author")] |
| 50 | author: String, |
| 51 | |
| 52 | #[arg(long, help = "Issue number")] |
| 53 | issue: u64, |
| 54 | |
| 55 | #[arg(long, default_value = "Issue Report", help = "Issue title")] |
| 56 | title: String, |
| 57 | |
| 58 | #[arg(long, help = "Path to issue body text file")] |
| 59 | body: PathBuf, |
| 60 | |
| 61 | #[arg(long, help = "Path to write comment markdown")] |
| 62 | output_comment: Option<PathBuf>, |
| 63 | |
| 64 | #[arg(long, help = "Path to write labels text (comma-separated)")] |
| 65 | output_labels: Option<PathBuf>, |
| 66 | }, |
| 67 | |
| 68 | /// Generate the Sovereign Alignment Interview comment for a Pull Request |
| 69 | Interview { |
| 70 | #[arg(long, help = "GitHub username of PR author")] |
| 71 | author: String, |
| 72 | |
| 73 | #[arg(long, help = "Pull Request number")] |
| 74 | pr: u64, |
| 75 | |
| 76 | #[arg( |
| 77 | long, |
| 78 | default_value = "External Contribution", |
| 79 | help = "Pull Request title" |
| 80 | )] |
| 81 | title: String, |
| 82 | }, |
| 83 | |
| 84 | /// Audit a git diff for telemetry, surveillance, and unslop violations |
| 85 | Audit { |
| 86 | #[arg(long, help = "Path to diff file")] |
| 87 | diff: PathBuf, |
| 88 | |
| 89 | #[arg( |
| 90 | long, |
| 91 | help = "Treat stylistic unslop checks as advisory warnings rather than hard failures" |
| 92 | )] |
| 93 | advisory_style: bool, |
| 94 | }, |
| 95 | |
| 96 | /// Automatically sanitize unslop formatting (em dashes, en dashes) in specified file |
| 97 | Fix { |
| 98 | #[arg(long, help = "Path to file to fix")] |
| 99 | file: PathBuf, |
| 100 | }, |
| 101 | |
| 102 | /// Evaluate contributor testimony against the Sovereign Constitution |
| 103 | Evaluate { |
| 104 | #[arg(long, help = "Path to testimony text file")] |
| 105 | testimony: PathBuf, |
| 106 | |
| 107 | #[arg( |
| 108 | long, |
| 109 | default_value = "contributor", |
| 110 | help = "GitHub username of author" |
| 111 | )] |
| 112 | author: String, |
| 113 | |
| 114 | #[arg(long, help = "Path to write comment markdown")] |
| 115 | output_comment: Option<PathBuf>, |
| 116 | |
| 117 | #[arg(long, help = "Path to write labels text (comma-separated)")] |
| 118 | output_labels: Option<PathBuf>, |
| 119 | }, |
| 120 | |
| 121 | /// Audit a git diff with the sovereign native probe evaluation engine |
| 122 | Probe { |
| 123 | #[arg(long, help = "Path to diff file")] |
| 124 | diff: PathBuf, |
| 125 | |
| 126 | #[arg( |
| 127 | long, |
| 128 | default_value = "reference", |
| 129 | help = "Probe backend: reference or http" |
| 130 | )] |
| 131 | backend: String, |
| 132 | |
| 133 | #[arg(long, help = "Endpoint for the http backend")] |
| 134 | endpoint: Option<String>, |
| 135 | |
| 136 | #[arg( |
| 137 | long, |
| 138 | default_value_t = 0.95, |
| 139 | help = "Minimum telemetry safety probability" |
| 140 | )] |
| 141 | min_telemetry: f64, |
| 142 | |
| 143 | #[arg(long, default_value_t = 2.0, help = "Minimum quality score")] |
| 144 | min_quality: f64, |
| 145 | |
| 146 | #[arg(long, help = "Path to write the probe report markdown")] |
| 147 | output: Option<PathBuf>, |
| 148 | }, |
| 149 | |
| 150 | /// Run internal self-diagnostics |
| 151 | Doctor, |
| 152 | } |
| 153 | |
| 154 | fn main() { |
| 155 | let cli = Cli::parse(); |
| 156 | |
| 157 | match cli.command { |
| 158 | Commands::Review { |
| 159 | author, |
| 160 | pr, |
| 161 | title, |
| 162 | diff, |
| 163 | output, |
| 164 | output_labels, |
| 165 | } => { |
| 166 | let content = match fs::read_to_string(&diff) { |
| 167 | Ok(c) => c, |
| 168 | Err(e) => { |
| 169 | eprintln!( |
| 170 | "{} Failed to read diff file {}: {}", |
| 171 | "error:".red().bold(), |
| 172 | diff.display(), |
| 173 | e |
| 174 | ); |
| 175 | std::process::exit(1); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | let review = generate_pr_review(&author, pr, &title, &content); |
| 180 | |
| 181 | if let Some(out_path) = output { |
| 182 | if let Err(e) = fs::write(&out_path, &review.markdown) { |
| 183 | eprintln!( |
| 184 | "{} Failed to write review markdown to {}: {}", |
| 185 | "error:".red().bold(), |
| 186 | out_path.display(), |
| 187 | e |
| 188 | ); |
| 189 | std::process::exit(1); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if let Some(lbl_path) = output_labels { |
| 194 | let labels_str = review.labels.join(","); |
| 195 | if let Err(e) = fs::write(&lbl_path, &labels_str) { |
| 196 | eprintln!( |
| 197 | "{} Failed to write labels to {}: {}", |
| 198 | "error:".red().bold(), |
| 199 | lbl_path.display(), |
| 200 | e |
| 201 | ); |
| 202 | std::process::exit(1); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | println!("{}", review.markdown); |
| 207 | |
| 208 | if !review.report.clean { |
| 209 | std::process::exit(1); |
| 210 | } |
| 211 | } |
| 212 | Commands::TriageIssue { |
| 213 | author, |
| 214 | issue, |
| 215 | title, |
| 216 | body, |
| 217 | output_comment, |
| 218 | output_labels, |
| 219 | } => { |
| 220 | let body_content = fs::read_to_string(&body).unwrap_or_default(); |
| 221 | |
| 222 | let result = triage_issue(&author, issue, &title, &body_content); |
| 223 | |
| 224 | if let Some(out_path) = output_comment { |
| 225 | if let Err(e) = fs::write(&out_path, &result.markdown) { |
| 226 | eprintln!( |
| 227 | "{} Failed to write triage comment to {}: {}", |
| 228 | "error:".red().bold(), |
| 229 | out_path.display(), |
| 230 | e |
| 231 | ); |
| 232 | std::process::exit(1); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if let Some(lbl_path) = output_labels { |
| 237 | let labels_str = result.labels.join(","); |
| 238 | if let Err(e) = fs::write(&lbl_path, &labels_str) { |
| 239 | eprintln!( |
| 240 | "{} Failed to write labels to {}: {}", |
| 241 | "error:".red().bold(), |
| 242 | lbl_path.display(), |
| 243 | e |
| 244 | ); |
| 245 | std::process::exit(1); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | println!("{}", result.markdown); |
| 250 | } |
| 251 | Commands::Interview { author, pr, title } => { |
| 252 | let comment = generate_inquisitor_interview(&author, pr, &title); |
| 253 | println!("{}", comment); |
| 254 | } |
| 255 | Commands::Audit { |
| 256 | diff, |
| 257 | advisory_style, |
| 258 | } => { |
| 259 | let content = match fs::read_to_string(&diff) { |
| 260 | Ok(c) => c, |
| 261 | Err(e) => { |
| 262 | eprintln!( |
| 263 | "{} Failed to read diff file {}: {}", |
| 264 | "error:".red().bold(), |
| 265 | diff.display(), |
| 266 | e |
| 267 | ); |
| 268 | std::process::exit(1); |
| 269 | } |
| 270 | }; |
| 271 | let report = DiffAuditor::audit_text(&content); |
| 272 | let has_hard_failures = !report.violations.is_empty() |
| 273 | || !report.secret_violations.is_empty() |
| 274 | || report.telemetry_detected; |
| 275 | |
| 276 | if report.clean { |
| 277 | println!( |
| 278 | "{} Diff is clean: zero telemetry, zero unslop violations.", |
| 279 | "success:".green().bold() |
| 280 | ); |
| 281 | } else if advisory_style && !has_hard_failures { |
| 282 | println!( |
| 283 | "{} Diff passed critical security invariants.", |
| 284 | "success:".green().bold() |
| 285 | ); |
| 286 | println!( |
| 287 | "{} Stylistic recommendations (non-blocking in advisory mode):", |
| 288 | "advisory:".yellow().bold() |
| 289 | ); |
| 290 | for u in &report.unslop_violations { |
| 291 | println!(" * {}", u.yellow()); |
| 292 | } |
| 293 | } else { |
| 294 | eprintln!("{} Constitutional audit failed:", "failure:".red().bold()); |
| 295 | for v in &report.violations { |
| 296 | eprintln!(" * {}", v.red()); |
| 297 | } |
| 298 | for s in &report.secret_violations { |
| 299 | eprintln!(" * {}", s.red()); |
| 300 | } |
| 301 | for u in &report.unslop_violations { |
| 302 | eprintln!(" * {}", u.yellow()); |
| 303 | } |
| 304 | std::process::exit(1); |
| 305 | } |
| 306 | } |
| 307 | Commands::Fix { file } => match fs::read_to_string(&file) { |
| 308 | Ok(content) => { |
| 309 | let fixed = content |
| 310 | .replace(" \u{2014} ", ": ") |
| 311 | .replace('\u{2014}', ", ") |
| 312 | .replace('\u{2013}', "-"); |
| 313 | if fixed != content { |
| 314 | if let Err(e) = fs::write(&file, &fixed) { |
| 315 | eprintln!( |
| 316 | "{} Failed to write to {}: {}", |
| 317 | "error:".red().bold(), |
| 318 | file.display(), |
| 319 | e |
| 320 | ); |
| 321 | std::process::exit(1); |
| 322 | } |
| 323 | println!( |
| 324 | "{} Sanitized em/en dashes in {}", |
| 325 | "success:".green().bold(), |
| 326 | file.display() |
| 327 | ); |
| 328 | } else { |
| 329 | println!( |
| 330 | "{} File {} is already clean.", |
| 331 | "notice:".cyan().bold(), |
| 332 | file.display() |
| 333 | ); |
| 334 | } |
| 335 | } |
| 336 | Err(e) => { |
| 337 | eprintln!( |
| 338 | "{} Failed to read file {}: {}", |
| 339 | "error:".red().bold(), |
| 340 | file.display(), |
| 341 | e |
| 342 | ); |
| 343 | std::process::exit(1); |
| 344 | } |
| 345 | }, |
| 346 | Commands::Evaluate { |
| 347 | testimony, |
| 348 | author, |
| 349 | output_comment, |
| 350 | output_labels, |
| 351 | } => { |
| 352 | let content = match fs::read_to_string(&testimony) { |
| 353 | Ok(c) => c, |
| 354 | Err(e) => { |
| 355 | eprintln!( |
| 356 | "{} Failed to read testimony file {}: {}", |
| 357 | "error:".red().bold(), |
| 358 | testimony.display(), |
| 359 | e |
| 360 | ); |
| 361 | std::process::exit(1); |
| 362 | } |
| 363 | }; |
| 364 | let verdict = TestimonyEvaluator::evaluate(&content); |
| 365 | let markdown = verdict.generate_markdown_verdict(&author); |
| 366 | |
| 367 | if let Some(out_path) = output_comment { |
| 368 | if let Err(e) = fs::write(&out_path, &markdown) { |
| 369 | eprintln!( |
| 370 | "{} Failed to write verdict comment to {}: {}", |
| 371 | "error:".red().bold(), |
| 372 | out_path.display(), |
| 373 | e |
| 374 | ); |
| 375 | std::process::exit(1); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if let Some(lbl_path) = output_labels { |
| 380 | let labels_str = if verdict.approved { |
| 381 | "sovereign-aligned".to_string() |
| 382 | } else { |
| 383 | "needs-testimony".to_string() |
| 384 | }; |
| 385 | if let Err(e) = fs::write(&lbl_path, &labels_str) { |
| 386 | eprintln!( |
| 387 | "{} Failed to write labels to {}: {}", |
| 388 | "error:".red().bold(), |
| 389 | lbl_path.display(), |
| 390 | e |
| 391 | ); |
| 392 | std::process::exit(1); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | println!("{}", markdown); |
| 397 | |
| 398 | if verdict.approved { |
| 399 | println!("{} {}", "PASS:".green().bold(), verdict.reason); |
| 400 | println!("Score: {:.2}/1.00", verdict.score); |
| 401 | } else { |
| 402 | eprintln!("{} {}", "FAIL:".red().bold(), verdict.reason); |
| 403 | eprintln!("Score: {:.2}/1.00", verdict.score); |
| 404 | std::process::exit(1); |
| 405 | } |
| 406 | } |
| 407 | Commands::Probe { |
| 408 | diff, |
| 409 | backend, |
| 410 | endpoint, |
| 411 | min_telemetry, |
| 412 | min_quality, |
| 413 | output, |
| 414 | } => { |
| 415 | let content = match fs::read_to_string(&diff) { |
| 416 | Ok(c) => c, |
| 417 | Err(e) => { |
| 418 | eprintln!( |
| 419 | "{} Failed to read diff file {}: {}", |
| 420 | "error:".red().bold(), |
| 421 | diff.display(), |
| 422 | e |
| 423 | ); |
| 424 | std::process::exit(1); |
| 425 | } |
| 426 | }; |
| 427 | |
| 428 | let runtime = match tokio::runtime::Runtime::new() { |
| 429 | Ok(runtime) => runtime, |
| 430 | Err(e) => { |
| 431 | eprintln!( |
| 432 | "{} Failed to initialize async runtime: {}", |
| 433 | "error:".red().bold(), |
| 434 | e |
| 435 | ); |
| 436 | std::process::exit(1); |
| 437 | } |
| 438 | }; |
| 439 | |
| 440 | match runtime.block_on(spark_inquisitor::run_probe_audit( |
| 441 | &content, |
| 442 | &backend, |
| 443 | endpoint, |
| 444 | min_telemetry, |
| 445 | min_quality, |
| 446 | )) { |
| 447 | Ok(report) => { |
| 448 | if let Some(path) = &output { |
| 449 | if let Err(e) = fs::write(path, &report.markdown) { |
| 450 | eprintln!( |
| 451 | "{} Failed to write probe report to {}: {}", |
| 452 | "error:".red().bold(), |
| 453 | path.display(), |
| 454 | e |
| 455 | ); |
| 456 | std::process::exit(1); |
| 457 | } |
| 458 | } |
| 459 | println!("{}", report.markdown); |
| 460 | if !report.clean { |
| 461 | std::process::exit(1); |
| 462 | } |
| 463 | } |
| 464 | Err(e) => { |
| 465 | eprintln!("{} Probe audit failed: {}", "error:".red().bold(), e); |
| 466 | std::process::exit(2); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | Commands::Doctor => { |
| 471 | println!("⚖️ Sovereign Inquisitor Diagnostics: OK"); |
| 472 | println!(" - Invariant rules: Active"); |
| 473 | println!(" - Telemetry scanner: Active"); |
| 474 | println!(" - Unslop scanner: Active"); |
| 475 | println!(" - Contributor Oath evaluator: Active"); |
| 476 | println!(" - Unified PR Reviewer: Active"); |
| 477 | println!(" - Issue Triage Engine: Active"); |
| 478 | } |
| 479 | } |
| 480 | } |