Skip to main content

lean_ctx_server/tools/ctx_graph_landing/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2//! `ctx_graph action=landing` — landing impact between two commits.
3//!
4//! For `base..head` it reports the changed symbol units, their transitive
5//! callers (depth-bounded), and the affected tests: tests that are changed,
6//! that reach a changed unit, or that share a file with a module-level change.
7//! Tests whose path or name matches `security|auth|policy|jail|redact` are
8//! always in the affected set. With a second head (`other`) it also reports
9//! the semantic overlap of the two changes: both change the same unit, or a
10//! unit changed on one side calls a unit changed on the other.
11//!
12//! Selection fails closed: a changed file that is neither parsed source nor
13//! attributable to the units including it (`include_str!` and friends), a
14//! module-level change whose dependents cannot be named, a git or read error,
15//! the search cap or the depth bound all set `full_suite=true` with a note.
16//! Dependents are found through calls and whole-word references, so changed
17//! constants, statics, types and traits reach their users. If the security
18//! tests cannot be resolved the side reports `full_suite`. Docs-only changes
19//! select only the security tests. An overlap verdict is `unknown` when a
20//! dependent search behind it was incomplete.
21//!
22//! Everything is read from committed objects (never the working tree) and
23//! every collection is ordered, so the output is a function of the commits,
24//! the depth and the format alone (#498).
25
26mod analyze;
27mod git;
28mod render;
29mod units;
30
31#[cfg(test)]
32mod tests;
33
34use std::path::Path;
35
36use analyze::analyze;
37
38pub const SCHEMA: &str = "lean-ctx.landing-impact.v1";
39pub const DEFAULT_DEPTH: usize = 3;
40pub const MAX_DEPTH: usize = 8;
41
42fn is_json(format: Option<&str>) -> bool {
43    matches!(format, Some(f) if f.eq_ignore_ascii_case("json"))
44}
45
46/// MCP / CLI entry point. `base` defaults to `HEAD~1`, `head` to `HEAD`.
47pub fn handle(
48    base: Option<&str>,
49    head: Option<&str>,
50    other: Option<&str>,
51    depth: Option<usize>,
52    root: &str,
53    format: Option<&str>,
54) -> String {
55    let pick = |r: Option<&str>, d: &'static str| {
56        r.map(str::trim)
57            .filter(|s| !s.is_empty())
58            .unwrap_or(d)
59            .to_string()
60    };
61    let base = pick(base, "HEAD~1");
62    let head = pick(head, "HEAD");
63    let other = other.map(str::trim).filter(|s| !s.is_empty());
64    let depth = depth.unwrap_or(DEFAULT_DEPTH).clamp(1, MAX_DEPTH);
65    match analyze(Path::new(root), &base, &head, other, depth) {
66        Ok(report) if is_json(format) => render::json(&report),
67        Ok(report) => render::text(&report),
68        Err(e) if is_json(format) => render::json_error(&e),
69        Err(e) => format!("landing impact failed: {e}"),
70    }
71}
72
73/// `lean-ctx graph landing <base> [head] [--other <ref>] [--depth <n>] [--json]`.
74pub fn cli(args: &[String], root: &str, format: Option<&str>) -> String {
75    let mut positional: Vec<&str> = Vec::new();
76    let mut other: Option<&str> = None;
77    let mut depth: Option<usize> = None;
78    let mut i = 0;
79    while i < args.len() {
80        let a = args[i].as_str();
81        if let Some(v) = a.strip_prefix("--other=") {
82            other = Some(v);
83        } else if a == "--other" {
84            other = args.get(i + 1).map(String::as_str);
85            i += 1;
86        } else if let Some(v) = a.strip_prefix("--depth=") {
87            depth = v.parse().ok();
88        } else if a == "--depth" {
89            depth = args.get(i + 1).and_then(|v| v.parse().ok());
90            i += 1;
91        } else if !a.starts_with("--") {
92            positional.push(a);
93        }
94        i += 1;
95    }
96    handle(
97        positional.first().copied(),
98        positional.get(1).copied(),
99        other,
100        depth,
101        root,
102        format,
103    )
104}