Skip to main content

lean_ctx_server/tools/
ctx_artifacts.rs

1// SPDX-License-Identifier: Apache-2.0
2use std::path::Path;
3
4use serde::Serialize;
5
6#[derive(Debug, Serialize)]
7struct ArtifactsStatus {
8    project_root: String,
9    registry_count: usize,
10    resolved_count: usize,
11    missing_count: usize,
12    index_file: String,
13    index_exists: bool,
14    warnings: Vec<String>,
15}
16
17pub fn handle(
18    action: &str,
19    project_root: &Path,
20    query: Option<&str>,
21    top_k: Option<usize>,
22    format: Option<&str>,
23) -> String {
24    match action {
25        "list" => list(project_root, format),
26        "status" => status(project_root, format),
27        "index" | "reindex" => reindex(project_root, format),
28        "search" => search(project_root, query, top_k, format),
29        "remove" => remove(project_root, query, format),
30        _ => "Unknown action. Use: list, status, reindex, search".to_string(),
31    }
32}
33
34fn list(project_root: &Path, format: Option<&str>) -> String {
35    let resolved = lean_ctx::core::artifacts::load_resolved(project_root);
36    match format.unwrap_or("json") {
37        "markdown" | "md" => {
38            let mut out = String::new();
39            out.push_str("# Context artifacts\n\n");
40            out.push_str(&format!(
41                "- Project root: `{}`\n\n",
42                project_root.to_string_lossy()
43            ));
44            if !resolved.warnings.is_empty() {
45                out.push_str("## Warnings\n");
46                for w in &resolved.warnings {
47                    out.push_str(&format!("- {w}\n"));
48                }
49                out.push('\n');
50            }
51            if resolved.artifacts.is_empty() {
52                out.push_str("_No artifacts registered._\n");
53                return out;
54            }
55            out.push_str("## Artifacts\n");
56            for a in &resolved.artifacts {
57                let kind = if a.is_dir { "dir" } else { "file" };
58                let exists = if a.exists { "exists" } else { "missing" };
59                out.push_str(&format!(
60                    "- `{}` ({kind}, {exists}) — {}\n",
61                    a.path, a.description
62                ));
63            }
64            out
65        }
66        _ => serde_json::to_string_pretty(&resolved)
67            .unwrap_or_else(|e| format!("{{\"error\":\"serialization failed: {e}\"}}")),
68    }
69}
70
71fn status(project_root: &Path, format: Option<&str>) -> String {
72    let resolved = lean_ctx::core::artifacts::load_resolved(project_root);
73    let index_file = lean_ctx_ext::core::artifact_index::index_file_path(project_root);
74    let index_exists = index_file.exists();
75
76    let missing = resolved.artifacts.iter().filter(|a| !a.exists).count();
77    let st = ArtifactsStatus {
78        project_root: project_root.to_string_lossy().to_string(),
79        registry_count: resolved.artifacts.len(),
80        resolved_count: resolved.artifacts.len(),
81        missing_count: missing,
82        index_file: index_file.to_string_lossy().to_string(),
83        index_exists,
84        warnings: resolved.warnings,
85    };
86
87    match format.unwrap_or("json") {
88        "markdown" | "md" => {
89            let mut out = String::new();
90            out.push_str("# Artifacts status\n\n");
91            out.push_str(&format!("- Project root: `{}`\n", st.project_root));
92            out.push_str(&format!("- Registry entries: `{}`\n", st.registry_count));
93            out.push_str(&format!("- Missing: `{}`\n", st.missing_count));
94            out.push_str(&format!("- Index file: `{}`\n", st.index_file));
95            out.push_str(&format!(
96                "- Index exists: `{}`\n\n",
97                if st.index_exists { "yes" } else { "no" }
98            ));
99            if !st.warnings.is_empty() {
100                out.push_str("## Warnings\n");
101                for w in &st.warnings {
102                    out.push_str(&format!("- {w}\n"));
103                }
104                out.push('\n');
105            }
106            out
107        }
108        _ => serde_json::to_string_pretty(&st)
109            .unwrap_or_else(|e| format!("{{\"error\":\"serialization failed: {e}\"}}")),
110    }
111}
112
113fn reindex(project_root: &Path, format: Option<&str>) -> String {
114    let (idx, warnings) = lean_ctx_ext::core::artifact_index::rebuild_from_scratch(project_root);
115    let index_file = lean_ctx_ext::core::artifact_index::index_file_path(project_root);
116    let res = serde_json::json!({
117        "project_root": project_root.to_string_lossy().to_string(),
118        "index_file": index_file.to_string_lossy().to_string(),
119        "files": idx.files.len(),
120        "chunks": idx.doc_count,
121        "warnings": warnings,
122    });
123    match format.unwrap_or("json") {
124        "markdown" | "md" => {
125            let mut out = String::new();
126            out.push_str("# Artifacts reindex\n\n");
127            out.push_str(&format!(
128                "- Project root: `{}`\n- Files: `{}`\n- Chunks: `{}`\n- Index file: `{}`\n",
129                res["project_root"].as_str().unwrap_or_default(),
130                res["files"].as_u64().unwrap_or(0),
131                res["chunks"].as_u64().unwrap_or(0),
132                res["index_file"].as_str().unwrap_or_default()
133            ));
134            if let Some(w) = res["warnings"].as_array()
135                && !w.is_empty()
136            {
137                out.push_str("\n## Warnings\n");
138                for ww in w {
139                    if let Some(s) = ww.as_str() {
140                        out.push_str(&format!("- {s}\n"));
141                    }
142                }
143            }
144            out
145        }
146        _ => serde_json::to_string_pretty(&res)
147            .unwrap_or_else(|e| format!("{{\"error\":\"serialization failed: {e}\"}}")),
148    }
149}
150
151fn search(
152    project_root: &Path,
153    query: Option<&str>,
154    top_k: Option<usize>,
155    format: Option<&str>,
156) -> String {
157    let Some(q) = query.map(str::trim).filter(|s| !s.is_empty()) else {
158        return "query is required for action=search".to_string();
159    };
160    let k = top_k.unwrap_or(10).clamp(1, 50);
161    let (idx, mut warnings) = lean_ctx_ext::core::artifact_index::load_or_build(project_root);
162    let results = idx.search(q, k);
163    if idx.doc_count == 0 {
164        warnings.push("artifact index is empty (no indexed chunks)".to_string());
165    }
166    let res = serde_json::json!({
167        "project_root": project_root.to_string_lossy().to_string(),
168        "query": q,
169        "top_k": k,
170        "results": results,
171        "warnings": warnings,
172    });
173    match format.unwrap_or("json") {
174        "markdown" | "md" => {
175            let mut out = String::new();
176            out.push_str("# Artifact search\n\n");
177            out.push_str(&format!(
178                "- Query: `{}`\n- Results: `{}`\n\n",
179                q,
180                res["results"].as_array().map_or(0, Vec::len)
181            ));
182            if let Some(w) = res["warnings"].as_array()
183                && !w.is_empty()
184            {
185                out.push_str("## Warnings\n");
186                for ww in w {
187                    if let Some(s) = ww.as_str() {
188                        out.push_str(&format!("- {s}\n"));
189                    }
190                }
191                out.push('\n');
192            }
193            out.push_str("## Results\n");
194            for r in results {
195                out.push_str(&format!(
196                    "- `{}` ({}–{}): {}\n",
197                    r.file_path,
198                    r.start_line,
199                    r.end_line,
200                    r.snippet.replace('\n', " ")
201                ));
202            }
203            out
204        }
205        _ => serde_json::to_string_pretty(&res)
206            .unwrap_or_else(|e| format!("{{\"error\":\"serialization failed: {e}\"}}")),
207    }
208}
209
210fn remove(project_root: &Path, name: Option<&str>, format: Option<&str>) -> String {
211    let Some(name) = name.map(str::trim).filter(|s| !s.is_empty()) else {
212        return "name is required for action=remove".to_string();
213    };
214
215    let lean_path = project_root.join(".lean-ctx-artifacts.json");
216    let lean_path = if lean_path.exists() {
217        lean_path
218    } else {
219        let legacy = project_root.join(".leanctxcontextartifacts.json");
220        if legacy.exists() {
221            legacy
222        } else {
223            let socrati = project_root.join(".socraticodecontextartifacts.json");
224            if socrati.exists() {
225                return "registry is in .socraticodecontextartifacts.json; migrate to .lean-ctx-artifacts.json to edit"
226                    .to_string();
227            }
228            return "no artifact registry file found".to_string();
229        }
230    };
231
232    // The registry is a file in the working tree, which a cloned repository
233    // populates: a symlinked registry must neither be read nor rewritten
234    // through the link (GHSA-mq5c-rjv9-2fc3). The rewrite goes through the
235    // confined write and keeps the file's permission bits.
236    let mode = match std::fs::symlink_metadata(&lean_path) {
237        Ok(meta) if meta.is_file() => registry_mode(&meta),
238        Ok(_) => {
239            return format!(
240                "refusing to edit {}: not a regular file (symlink?)",
241                lean_path.display()
242            );
243        }
244        Err(e) => return format!("failed to read registry: {e}"),
245    };
246
247    let content = match std::fs::read_to_string(&lean_path) {
248        Ok(s) => s,
249        Err(e) => return format!("failed to read registry: {e}"),
250    };
251
252    let (as_array, mut specs): (bool, Vec<lean_ctx::core::artifacts::ArtifactSpec>) =
253        match serde_json::from_str::<serde_json::Value>(&content) {
254            Ok(v) => {
255                if let Some(arr) = v.as_array() {
256                    let mut out = Vec::new();
257                    for item in arr {
258                        if let Ok(s) = serde_json::from_value::<
259                            lean_ctx::core::artifacts::ArtifactSpec,
260                        >(item.clone())
261                        {
262                            out.push(s);
263                        }
264                    }
265                    (true, out)
266                } else if let Some(obj) = v.as_object() {
267                    if let Some(arts) = obj.get("artifacts").and_then(|a| a.as_array()) {
268                        let mut out = Vec::new();
269                        for item in arts {
270                            if let Ok(s) = serde_json::from_value::<
271                                lean_ctx::core::artifacts::ArtifactSpec,
272                            >(item.clone())
273                            {
274                                out.push(s);
275                            }
276                        }
277                        (false, out)
278                    } else {
279                        return "invalid registry schema (expected array or {artifacts:[...]})"
280                            .to_string();
281                    }
282                } else {
283                    return "invalid registry schema (expected array or object)".to_string();
284                }
285            }
286            Err(e) => return format!("invalid JSON: {e}"),
287        };
288
289    let before = specs.len();
290    specs.retain(|s| s.name.trim() != name);
291    let removed = before.saturating_sub(specs.len());
292
293    if removed == 0 {
294        return format!("artifact not found: {name}");
295    }
296
297    let new_json = if as_array {
298        serde_json::to_string_pretty(&specs)
299    } else {
300        serde_json::to_string_pretty(&serde_json::json!({ "artifacts": specs }))
301    }
302    .unwrap_or_else(|e| format!("{{\"error\":\"serialization failed: {e}\"}}"));
303
304    let registry_name = lean_path.file_name().map(Path::new).unwrap_or(&lean_path);
305    if let Err(e) = lean_ctx::core::pathjail_write::write_below(
306        project_root,
307        registry_name,
308        new_json.as_bytes(),
309        mode,
310    ) {
311        return format!("failed to write registry: {e}");
312    }
313
314    let res = serde_json::json!({
315        "project_root": project_root.to_string_lossy().to_string(),
316        "registry_file": lean_path.to_string_lossy().to_string(),
317        "removed": removed,
318        "name": name
319    });
320    match format.unwrap_or("json") {
321        "markdown" | "md" => {
322            format!(
323                "# Artifact removed\n\n- Name: `{}`\n- Removed: `{}`\n- Registry: `{}`\n",
324                name,
325                removed,
326                res["registry_file"].as_str().unwrap_or_default(),
327            )
328        }
329        _ => serde_json::to_string_pretty(&res)
330            .unwrap_or_else(|e| format!("{{\"error\":\"serialization failed: {e}\"}}")),
331    }
332}
333
334#[cfg(unix)]
335fn registry_mode(meta: &std::fs::Metadata) -> u32 {
336    use std::os::unix::fs::PermissionsExt;
337    meta.permissions().mode() & 0o777
338}
339
340#[cfg(not(unix))]
341fn registry_mode(_meta: &std::fs::Metadata) -> u32 {
342    0o644
343}
344
345#[cfg(all(test, unix))]
346mod tests {
347    use super::*;
348
349    const REGISTRY: &str = r#"[{"name":"a","path":"a.md","description":"A"},{"name":"b","path":"b.md","description":"B"}]"#;
350
351    /// A cloned repository ships the registry as a link to a JSON file
352    /// outside it: `remove` neither reads nor rewrites that file.
353    #[test]
354    fn remove_refuses_a_symlinked_registry() {
355        let tmp = tempfile::tempdir().unwrap();
356        let root = tmp.path().join("proj");
357        let outside = tmp.path().join("outside.json");
358        std::fs::create_dir_all(&root).unwrap();
359        std::fs::write(&outside, REGISTRY).unwrap();
360        std::os::unix::fs::symlink(&outside, root.join(".lean-ctx-artifacts.json")).unwrap();
361
362        let out = remove(&root, Some("a"), None);
363        assert!(out.starts_with("refusing"), "{out}");
364        assert_eq!(std::fs::read_to_string(&outside).unwrap(), REGISTRY);
365    }
366
367    #[test]
368    fn remove_rewrites_a_plain_registry() {
369        let tmp = tempfile::tempdir().unwrap();
370        let file = tmp.path().join(".lean-ctx-artifacts.json");
371        std::fs::write(&file, REGISTRY).unwrap();
372
373        let out = remove(tmp.path(), Some("a"), None);
374        assert!(out.contains("\"removed\": 1"), "{out}");
375        let left = std::fs::read_to_string(&file).unwrap();
376        assert!(
377            !left.contains("\"a.md\"") && left.contains("\"b.md\""),
378            "{left}"
379        );
380    }
381}