lean_ctx_cli/cli/
audit_report.rs1use std::collections::HashMap;
3
4use lean_ctx::core::audit_trail::{AuditEntry, AuditEventType};
5
6pub fn cmd_evidence(args: &[String]) {
12 let flag = |name: &str| -> Option<String> {
13 args.iter()
14 .position(|a| a == name)
15 .and_then(|pos| args.get(pos + 1).cloned())
16 };
17 let (Some(from), Some(to)) = (flag("--from"), flag("--to")) else {
18 eprintln!(
19 "audit evidence: --from and --to (RFC 3339) are required\n\n\
20USAGE:\n lean-ctx audit evidence --from 2026-05-01T00:00:00Z --to 2026-06-01T00:00:00Z \\\n\
21 [--pack <name|path>] [--framework <id>] [--out bundle.zip]\n\n\
22--framework adds a framework coverage report (LeanCTX Enterprise).\n\
23Verify without LeanCTX: leanctx-verify <bundle.zip> [--pubkey <hex>]"
24 );
25 std::process::exit(2);
26 };
27
28 let spec = lean_ctx::core::evidence_bundle::BundleSpec {
29 from,
30 to,
31 pack: flag("--pack"),
32 out: flag("--out").map(std::path::PathBuf::from),
33 };
34 let framework = flag("--framework");
35 let assess: lean_ctx::core::evidence_bundle::AssessFrameworks =
36 lean_ctx::core::intelligence_runtime::compliance_frameworks::assess;
37 match lean_ctx::core::evidence_bundle::generate_with(
38 &spec,
39 framework.as_deref().map(|id| (id, assess)),
40 ) {
41 Ok(result) => {
42 println!("evidence bundle written: {}", result.path.display());
43 println!("bundle sha256: {}", result.sha256);
44 println!("audit entries: {}", result.entries);
45 for f in &result.files {
46 println!(" {f}");
47 }
48 println!(
49 "\nverify offline (no LeanCTX needed):\n leanctx-verify {}",
50 result.path.display()
51 );
52 }
53 Err(e) => {
54 eprintln!("audit evidence: {e}");
55 std::process::exit(1);
56 }
57 }
58}
59
60pub fn cmd_determinism(args: &[String]) {
62 if !args.is_empty() {
63 eprintln!("USAGE: lean-ctx audit determinism");
64 std::process::exit(2);
65 }
66 let report = lean_ctx_proxy::determinism_guard::audit_report();
67 println!(
68 "{}",
69 serde_json::to_string(&report).expect("determinism audit report serializes")
70 );
71}
72
73pub fn generate_report() -> String {
74 let entries = lean_ctx::core::audit_trail::load_recent(10000);
75 let chain = lean_ctx::core::audit_trail::verify_chain_full();
76
77 let mut report = String::new();
78 report.push_str("# lean-ctx Compliance Report\n\n");
79 report.push_str(&format!(
80 "Generated: {}\n",
81 lean_ctx_time::UtcDateTime::now().to_rfc3339()
82 ));
83 report.push_str(&format!("Audit Trail Entries: {}\n", entries.len()));
84 report.push_str(&format!(
85 "Chain Integrity: {}\n\n",
86 if chain.valid { "VALID" } else { "BROKEN" }
87 ));
88
89 let mut by_agent: HashMap<String, Vec<&AuditEntry>> = HashMap::new();
90 for e in &entries {
91 by_agent.entry(e.agent_id.clone()).or_default().push(e);
92 }
93
94 report.push_str("## Per-Agent Summary\n\n");
95 for (agent, agent_entries) in &by_agent {
96 let tool_calls = agent_entries
97 .iter()
98 .filter(|e| matches!(e.event_type, AuditEventType::ToolCall))
99 .count();
100 let denials = agent_entries
101 .iter()
102 .filter(|e| matches!(e.event_type, AuditEventType::ToolDenied))
103 .count();
104 report.push_str(&format!("### Agent: {agent}\n"));
105 report.push_str(&format!("- Tool calls: {tool_calls}\n"));
106 report.push_str(&format!("- Denials: {denials}\n\n"));
107 }
108
109 let security_events: Vec<_> = entries
110 .iter()
111 .filter(|e| !matches!(e.event_type, AuditEventType::ToolCall))
112 .collect();
113 report.push_str(&format!(
114 "## Security Events ({} total)\n\n",
115 security_events.len()
116 ));
117 for e in security_events.iter().take(50) {
118 report.push_str(&format!(
119 "- [{}] {:?} tool={} agent={}\n",
120 e.timestamp, e.event_type, e.tool, e.agent_id
121 ));
122 }
123
124 report.push_str("\n\n");
125 report.push_str(&lean_ctx::core::owasp_alignment::summary());
126
127 report
128}