|
| 1 | +//! Inspect the local append-only admission decision ledger. |
| 2 | +
|
| 3 | +use anyhow::{bail, Context, Result}; |
| 4 | +use colored::Colorize; |
| 5 | +use serde::Serialize; |
| 6 | + |
| 7 | +use crate::cli::{create_memory, load_config}; |
| 8 | +use contribai::core::admission::{ |
| 9 | + AdmissionAuditDecision, AdmissionAuditRecord, AdmissionAuditVerification, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Serialize)] |
| 13 | +struct AdmissionAuditOutput { |
| 14 | + schema_version: u8, |
| 15 | + chain: AdmissionAuditVerification, |
| 16 | + records: Vec<AdmissionAuditRecord>, |
| 17 | +} |
| 18 | + |
| 19 | +/// List and verify locally recorded admission decisions. This command performs no network access. |
| 20 | +pub fn run_admissions( |
| 21 | + config_path: Option<&str>, |
| 22 | + repository: Option<&str>, |
| 23 | + decision: Option<&str>, |
| 24 | + limit: usize, |
| 25 | + json: bool, |
| 26 | +) -> Result<()> { |
| 27 | + if !(1..=1000).contains(&limit) { |
| 28 | + bail!("admission audit limit must be between 1 and 1000"); |
| 29 | + } |
| 30 | + let normalized_decision = decision.map(|value| value.trim().to_ascii_lowercase()); |
| 31 | + if let Some(value) = normalized_decision.as_deref() { |
| 32 | + if !AdmissionAuditDecision::is_valid_filter(value) { |
| 33 | + bail!( |
| 34 | + "invalid admission decision {value:?}; expected approved, blocked, rejected, skipped, or error" |
| 35 | + ); |
| 36 | + } |
| 37 | + } |
| 38 | + let normalized_repository = repository.map(str::trim).filter(|value| !value.is_empty()); |
| 39 | + let config = load_config(config_path)?; |
| 40 | + let memory = create_memory(&config)?; |
| 41 | + let chain = memory |
| 42 | + .verify_admission_audit_chain() |
| 43 | + .context("verifying the admission audit chain")?; |
| 44 | + let records = memory |
| 45 | + .get_admission_audits(normalized_repository, normalized_decision.as_deref(), limit) |
| 46 | + .context("reading the admission audit ledger")?; |
| 47 | + let output = AdmissionAuditOutput { |
| 48 | + schema_version: 1, |
| 49 | + chain, |
| 50 | + records, |
| 51 | + }; |
| 52 | + |
| 53 | + if json { |
| 54 | + println!("{}", serde_json::to_string_pretty(&output)?); |
| 55 | + } else { |
| 56 | + print_pretty( |
| 57 | + &output, |
| 58 | + normalized_repository, |
| 59 | + normalized_decision.as_deref(), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + if !output.chain.valid { |
| 64 | + bail!("local admission audit chain verification failed"); |
| 65 | + } |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | + |
| 69 | +fn print_pretty(output: &AdmissionAuditOutput, repository: Option<&str>, decision: Option<&str>) { |
| 70 | + println!("{}", "ContribAI Admission Audit".cyan().bold()); |
| 71 | + println!("{}", "━".repeat(68).dimmed()); |
| 72 | + let chain_status = if output.chain.valid { |
| 73 | + "VALID".green().bold() |
| 74 | + } else { |
| 75 | + "INVALID".red().bold() |
| 76 | + }; |
| 77 | + println!( |
| 78 | + " Chain: {} ({} records checked)", |
| 79 | + chain_status, output.chain.records_checked |
| 80 | + ); |
| 81 | + if let Some(value) = repository { |
| 82 | + println!(" Repository filter: {}", value.cyan()); |
| 83 | + } |
| 84 | + if let Some(value) = decision { |
| 85 | + println!(" Decision filter: {}", value.cyan()); |
| 86 | + } |
| 87 | + println!(); |
| 88 | + |
| 89 | + if output.records.is_empty() { |
| 90 | + println!( |
| 91 | + " {}", |
| 92 | + "No admission decisions match the current filter.".dimmed() |
| 93 | + ); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + for record in &output.records { |
| 98 | + let decision = match record.decision { |
| 99 | + AdmissionAuditDecision::Approved => record.decision.as_str().green().bold(), |
| 100 | + AdmissionAuditDecision::Blocked => record.decision.as_str().yellow().bold(), |
| 101 | + AdmissionAuditDecision::Rejected | AdmissionAuditDecision::Error => { |
| 102 | + record.decision.as_str().red().bold() |
| 103 | + } |
| 104 | + AdmissionAuditDecision::Skipped => record.decision.as_str().dimmed().bold(), |
| 105 | + }; |
| 106 | + println!( |
| 107 | + " {} {} {} [{}]", |
| 108 | + record.recorded_at.format("%Y-%m-%d %H:%M"), |
| 109 | + record.repository.cyan(), |
| 110 | + record.stage.as_str().dimmed(), |
| 111 | + decision |
| 112 | + ); |
| 113 | + println!( |
| 114 | + " Receipt {} Scope {} files / {} changed lines", |
| 115 | + short_receipt(&record.receipt).dimmed(), |
| 116 | + record.file_count, |
| 117 | + record.changed_lines |
| 118 | + ); |
| 119 | + if let Some(permit) = &record.permit_id { |
| 120 | + println!(" Permit {}", permit.dimmed()); |
| 121 | + } |
| 122 | + println!(" {}", record.reason); |
| 123 | + println!(); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +fn short_receipt(receipt: &str) -> &str { |
| 128 | + receipt.get(..12).unwrap_or(receipt) |
| 129 | +} |
| 130 | + |
| 131 | +#[cfg(test)] |
| 132 | +mod tests { |
| 133 | + use super::*; |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn decision_filter_is_strict_and_case_normalizable() { |
| 137 | + assert!(AdmissionAuditDecision::is_valid_filter("blocked")); |
| 138 | + assert!(!AdmissionAuditDecision::is_valid_filter("allow")); |
| 139 | + assert_eq!("APPROVED".to_ascii_lowercase(), "approved"); |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn receipt_preview_is_safe_for_short_values() { |
| 144 | + assert_eq!(short_receipt("abcdef"), "abcdef"); |
| 145 | + assert_eq!(short_receipt("0123456789abcdef"), "0123456789ab"); |
| 146 | + } |
| 147 | +} |
0 commit comments