lean_ctx_server/tools/
ctx_proof.rs1use std::path::Path;
3
4use lean_ctx::core::context_ir::{ContextIrV1, write_project_context_ir};
5use lean_ctx::core::context_proof::{
6 ProofOptions, ProofSources, collect_v1, proof_destination, write_project_proof,
7};
8
9pub fn handle_export(
10 project_root: &str,
11 format: Option<&str>,
12 write: bool,
13 filename: Option<&str>,
14 max_evidence: Option<usize>,
15 max_ledger_files: Option<usize>,
16 sources: ProofSources,
17) -> Result<String, String> {
18 let opts = ProofOptions {
19 max_evidence: max_evidence.unwrap_or(50),
20 max_ledger_files: max_ledger_files.unwrap_or(10),
21 };
22 let proof = collect_v1(sources, opts);
23 let ir = ContextIrV1::load();
24
25 let mut out = String::new();
26 let mut written: Option<String> = None;
27 let mut written_ir: Option<String> = None;
28 let mut written_architecture_json: Option<String> = None;
29 let mut written_architecture_html: Option<String> = None;
30 if write {
31 let root = Path::new(project_root);
32 let ts = lean_ctx_time::UtcDateTime::now()
33 .format("%Y-%m-%d_%H%M%S")
34 .to_string();
35 let default_proof_name = format!("context-proof-v1_{ts}.json");
36 let ir_name = format!("context-ir-v1_{ts}.json");
37 let architecture_json_name = format!("architecture-overview-v1_{ts}.json");
38 let architecture_html_name = format!("architecture-overview-v1_{ts}.html");
39 let proof_name = filename.unwrap_or(default_proof_name.as_str());
40
41 let path = write_project_proof(root, &proof, Some(proof_name))?;
42 written = Some(path.to_string_lossy().to_string());
43
44 let ir_path = write_project_context_ir(root, &ir, Some(&ir_name))?;
45 written_ir = Some(ir_path.to_string_lossy().to_string());
46
47 {
50 let status =
51 crate::tools::ctx_impact::handle("status", None, project_root, None, Some("json"));
52 let freshness = serde_json::from_str::<serde_json::Value>(&status)
53 .ok()
54 .and_then(|v| {
55 v.get("freshness")
56 .and_then(|f| f.as_str())
57 .map(std::string::ToString::to_string)
58 })
59 .unwrap_or_else(|| "unknown".to_string());
60 if freshness != "fresh" {
61 let _ = crate::tools::ctx_impact::handle("build", None, project_root, None, None);
62 }
63
64 let arch_json = crate::tools::ctx_architecture::handle(
65 "overview",
66 None,
67 project_root,
68 Some("json"),
69 );
70
71 let json_redacted = lean_ctx::core::redaction::redact_text(&arch_json);
72 let json_path =
73 proof_destination(root, &architecture_json_name)?.write(&json_redacted)?;
74 written_architecture_json = Some(json_path.to_string_lossy().to_string());
75
76 let html = render_architecture_overview_html(&arch_json);
77 let html_redacted = lean_ctx::core::redaction::redact_text(&html);
78 let html_path =
79 proof_destination(root, &architecture_html_name)?.write(&html_redacted)?;
80 written_architecture_html = Some(html_path.to_string_lossy().to_string());
81 }
82 }
83
84 match format.unwrap_or("json") {
85 "summary" => {
86 out.push_str("ContextProofV1 exported\n");
87 if let Some(p) = written {
88 out.push_str(&format!("path: {p}\n"));
89 }
90 if let Some(p) = written_ir {
91 out.push_str(&format!("context_ir_path: {p}\n"));
92 }
93 if let Some(p) = written_architecture_json {
94 out.push_str(&format!("architecture_overview_json_path: {p}\n"));
95 }
96 if let Some(p) = written_architecture_html {
97 out.push_str(&format!("architecture_overview_html_path: {p}\n"));
98 }
99 out.push_str(&format!("schema_version: {}\n", proof.schema_version));
100 out.push_str(&format!(
101 "project_root_hash: {}\n",
102 proof.project.project_root_hash.clone().unwrap_or_default()
103 ));
104 out.push_str(&format!("role: {}\n", proof.role.name));
105 out.push_str(&format!("profile: {}\n", proof.profile.name));
106 out.push_str(&format!(
107 "context_ir_schema_version: {}\n",
108 ir.schema_version
109 ));
110 out.push_str(&format!("context_ir_items: {}\n", ir.items.len()));
111 }
112 "both" => {
113 if let Some(p) = &written {
114 out.push_str(&format!("[proof_path: {p}]\n\n"));
115 }
116 if let Some(p) = &written_ir {
117 out.push_str(&format!("[context_ir_path: {p}]\n\n"));
118 }
119 if let Some(p) = &written_architecture_json {
120 out.push_str(&format!("[architecture_overview_json_path: {p}]\n\n"));
121 }
122 if let Some(p) = &written_architecture_html {
123 out.push_str(&format!("[architecture_overview_html_path: {p}]\n\n"));
124 }
125 out.push_str(&serde_json::to_string_pretty(&proof).map_err(|e| e.to_string())?);
126 }
127 _ => {
128 if let Some(p) = &written {
129 out.push_str(&format!("[proof_path: {p}]\n\n"));
130 }
131 if let Some(p) = &written_ir {
132 out.push_str(&format!("[context_ir_path: {p}]\n\n"));
133 }
134 if let Some(p) = &written_architecture_json {
135 out.push_str(&format!("[architecture_overview_json_path: {p}]\n\n"));
136 }
137 if let Some(p) = &written_architecture_html {
138 out.push_str(&format!("[architecture_overview_html_path: {p}]\n\n"));
139 }
140 out.push_str(&serde_json::to_string_pretty(&proof).map_err(|e| e.to_string())?);
141 }
142 }
143
144 Ok(out)
145}
146
147fn escape_html(s: &str) -> String {
148 let mut out = String::with_capacity(s.len());
149 for ch in s.chars() {
150 match ch {
151 '&' => out.push_str("&"),
152 '<' => out.push_str("<"),
153 '>' => out.push_str(">"),
154 '"' => out.push_str("""),
155 '\'' => out.push_str("'"),
156 _ => out.push(ch),
157 }
158 }
159 out
160}
161
162fn render_architecture_overview_html(json_payload: &str) -> String {
163 let escaped = escape_html(json_payload);
164 format!(
165 r#"<!doctype html>
166<html lang="en">
167<head>
168 <meta charset="utf-8" />
169 <meta name="viewport" content="width=device-width, initial-scale=1" />
170 <title>LeanCTX — Architecture Overview</title>
171 <style>
172 :root {{ color-scheme: light dark; }}
173 body {{ font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; margin: 24px; }}
174 h1 {{ margin: 0 0 12px 0; }}
175 p {{ margin: 0 0 18px 0; opacity: 0.8; }}
176 pre {{ padding: 16px; border-radius: 10px; overflow: auto; }}
177 </style>
178</head>
179<body>
180 <h1>Architecture Overview (JSON)</h1>
181 <p>Generated by LeanCTX proof export. Content is redacted-by-default for CI safety.</p>
182 <pre>{escaped}</pre>
183</body>
184</html>"#
185 )
186}