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!("Generated: {}\n", chrono::Utc::now().to_rfc3339()));
80 report.push_str(&format!("Audit Trail Entries: {}\n", entries.len()));
81 report.push_str(&format!(
82 "Chain Integrity: {}\n\n",
83 if chain.valid { "VALID" } else { "BROKEN" }
84 ));
85
86 let mut by_agent: HashMap<String, Vec<&AuditEntry>> = HashMap::new();
87 for e in &entries {
88 by_agent.entry(e.agent_id.clone()).or_default().push(e);
89 }
90
91 report.push_str("## Per-Agent Summary\n\n");
92 for (agent, agent_entries) in &by_agent {
93 let tool_calls = agent_entries
94 .iter()
95 .filter(|e| matches!(e.event_type, AuditEventType::ToolCall))
96 .count();
97 let denials = agent_entries
98 .iter()
99 .filter(|e| matches!(e.event_type, AuditEventType::ToolDenied))
100 .count();
101 report.push_str(&format!("### Agent: {agent}\n"));
102 report.push_str(&format!("- Tool calls: {tool_calls}\n"));
103 report.push_str(&format!("- Denials: {denials}\n\n"));
104 }
105
106 let security_events: Vec<_> = entries
107 .iter()
108 .filter(|e| !matches!(e.event_type, AuditEventType::ToolCall))
109 .collect();
110 report.push_str(&format!(
111 "## Security Events ({} total)\n\n",
112 security_events.len()
113 ));
114 for e in security_events.iter().take(50) {
115 report.push_str(&format!(
116 "- [{}] {:?} tool={} agent={}\n",
117 e.timestamp, e.event_type, e.tool, e.agent_id
118 ));
119 }
120
121 report.push_str("\n\n");
122 report.push_str(&lean_ctx::core::owasp_alignment::summary());
123
124 report
125}