lean-ctx-engine 3.12.2

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and proxy integrations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// SPDX-License-Identifier: Apache-2.0
//! The one check through which a graph reference — a file, a symbol, an edge
//! end — leaves toward a tool or host (G04).
//!
//! A graph row names the project file it was derived from. Nothing in the row
//! changes when that file is deleted, restricted or rewritten, so every
//! reference is asked again when it is served:
//!
//! 1. **All modes:** the path must resolve inside the project root through
//!    the path jail the rooted reader uses (`pathjail::jail_path`) and be a
//!    regular file there (links are not followed). Gone or outside ⇒ not
//!    served. In Community developer mode this is the whole rule — the mode
//!    makes no protection claim, as with the originless rule.
//! 2. **Under protection** (`stored::source_authority_required`): the full
//!    rule of `bm25_index::source_gate::SourceGate` for every reference — the
//!    admitted rooted reader must return the file now, storable, under the
//!    current project policy and role, and a symbol's name must still occur
//!    in it. Same gate, same bound: over the bound the reference is refused.
//!
//! There is deliberately no shortcut under protection. The graph records a
//! content fingerprint for its own staleness check and could record the
//! gateway policy digest, but neither covers the project policy pack or the
//! role, so "unchanged since an admitted build" does not prove today's
//! rights. A staleness fingerprint is not an authorization.
//!
//! A reference that is not a project file (`scheme://…`, a cross-source node)
//! has no source authority on record: withheld under protection, served in
//! Community developer mode.
//!
//! One gate serves one opened graph, which is one request: decisions are
//! memoised per file and never outlive it.

use std::cell::{OnceCell, RefCell};
use std::collections::{BTreeSet, HashMap, HashSet};
use std::path::Path;

use rusqlite::Connection;

use super::{CodeGraph, Node};
use crate::core::bm25_index::source_gate::SourceGate;

/// Tool identity graph sources are re-read for under protection.
const GRAPH_TOOL: &str = "ctx_graph";

/// Distinct files one request can decide under protection: the content-read
/// bound of `SourceGate`. It chooses between a whole-graph and a local
/// ranking; the gate itself enforces the bound.
const PROTECTED_FILES: usize = crate::core::bm25_index::source_gate::MAX_SOURCES;

/// Files one gate decides and remembers. Over it a file the gate has not
/// decided yet is refused: not checked, not served, not remembered.
const MAX_FILES: usize = 65_536;

/// The serve-time check of one opened graph.
pub(crate) struct GraphGate {
    project_root: String,
    protected: OnceCell<bool>,
    /// Per file asked so far: whether it is a regular file inside the root.
    present: RefCell<HashMap<String, bool>>,
    sources: RefCell<SourceGate>,
}

impl GraphGate {
    pub(crate) fn for_root(project_root: &str) -> Self {
        Self {
            project_root: project_root.to_owned(),
            protected: OnceCell::new(),
            present: RefCell::new(HashMap::new()),
            sources: RefCell::new(SourceGate::new(Path::new(project_root), GRAPH_TOOL)),
        }
    }

    /// Whether the file `path` may be named now.
    pub(crate) fn file(&self, path: &str) -> bool {
        self.reference(path, "")
    }

    /// Whether the symbol `name` of `path` may be named now.
    pub(crate) fn symbol(&self, path: &str, name: &str) -> bool {
        self.reference(path, identifier(name))
    }

    /// Whether protection is active for this request (read once).
    fn protected(&self) -> bool {
        *self
            .protected
            .get_or_init(crate::core::context_admission::stored::source_authority_required)
    }

    /// Whether `text` of a node that names no project file (a commit, a
    /// knowledge fact, an issue, a CI run) may be served now. Such a node
    /// records no source binding, so it follows the originless rule of
    /// `serve_stored`: withheld while protection is active in any layer,
    /// served in Community developer mode when today's content rules leave
    /// it unchanged.
    fn originless(&self, text: &str) -> bool {
        !self.protected()
            && crate::core::context_admission::stored::serve_stored(text, None, None)
                .is_ok_and(|served| served == text)
    }

    /// The files of `files` that may enter a traversal or ranking over the
    /// whole graph. `edges` are the file pairs of that graph, `seeds` the
    /// files the request is about.
    ///
    /// Community developer mode, or a graph small enough to be decided in
    /// full: every file is asked. Under protection with more files than one
    /// request can decide ([`PROTECTED_FILES`]): the ranking is local — the
    /// seeds are asked first, then their neighbours, frontier by frontier and
    /// only through files that are served, until the bound is spent. Without
    /// seeds that is the empty set: no ranking rather than one over files
    /// nobody decided.
    pub(crate) fn ranking_files(
        &self,
        mut files: Vec<String>,
        edges: &[(String, String)],
        seeds: &[String],
    ) -> HashSet<String> {
        files.sort();
        files.dedup();
        if !self.protected() || files.len() <= PROTECTED_FILES {
            return files.into_iter().filter(|file| self.file(file)).collect();
        }
        let known: HashSet<&str> = files.iter().map(String::as_str).collect();
        let mut asked: HashSet<&str> = HashSet::new();
        let mut served: HashSet<String> = HashSet::new();
        let mut frontier: BTreeSet<&str> = seeds
            .iter()
            .map(String::as_str)
            .filter(|seed| known.contains(*seed))
            .collect();
        while !frontier.is_empty() && asked.len() < PROTECTED_FILES {
            let mut next: BTreeSet<&str> = BTreeSet::new();
            for file in frontier {
                if asked.len() >= PROTECTED_FILES {
                    break;
                }
                if !asked.insert(file) || !self.file(file) {
                    continue;
                }
                served.insert(file.to_owned());
                for (from, to) in edges {
                    if from == file {
                        next.insert(to.as_str());
                    } else if to == file {
                        next.insert(from.as_str());
                    }
                }
            }
            next.retain(|file| known.contains(*file) && !asked.contains(*file));
            frontier = next;
        }
        served
    }

    fn reference(&self, path: &str, needle: &str) -> bool {
        let protected = self.protected();
        if path.contains("://") {
            return !protected;
        }
        self.present(path) && (!protected || self.sources.borrow_mut().serves(path, needle))
    }

    fn present(&self, path: &str) -> bool {
        let known = self.present.borrow().get(path).copied();
        if let Some(known) = known {
            return known;
        }
        if self.present.borrow().len() >= MAX_FILES {
            return false;
        }
        let root = Path::new(&self.project_root);
        let present = crate::core::pathjail::jail_path(&root.join(path), root)
            .is_ok_and(|jailed| std::fs::symlink_metadata(jailed).is_ok_and(|meta| meta.is_file()));
        self.present.borrow_mut().insert(path.to_owned(), present);
        present
    }
}

/// The last identifier of a symbol name: `Config::load` → `load`,
/// `fmt::Display for Wrapper` → `Wrapper`. That is what must still occur in
/// the admitted text.
fn identifier(name: &str) -> &str {
    name.rsplit(|c: char| !(c.is_alphanumeric() || c == '_'))
        .find(|part| !part.is_empty())
        .unwrap_or("")
}

impl CodeGraph {
    /// This graph, gated for `project_root`: for an in-memory copy of a
    /// project's graph that is read back through the serving surface.
    #[must_use]
    pub(crate) fn gated_for(mut self, project_root: &str) -> Self {
        self.gate = Some(GraphGate::for_root(project_root));
        self
    }

    /// Whether a reference to the file `path` of this graph may be served
    /// now. A graph without a project root (in memory) has no source on
    /// record to ask and is not gated.
    pub fn serves_file(&self, path: &str) -> bool {
        self.gate.as_ref().is_none_or(|gate| gate.file(path))
    }

    /// Whether a reference to the symbol `name` of `path` may be served now.
    pub fn serves_symbol(&self, path: &str, name: &str) -> bool {
        self.gate
            .as_ref()
            .is_none_or(|gate| gate.symbol(path, name))
    }

    /// Whether `node` may be named now — the one entry point for a node of
    /// any kind. See [`Self::serves_node_parts`].
    pub fn serves_node(&self, node: &Node) -> bool {
        self.serves_node_parts(
            node.kind.as_str(),
            &node.file_path,
            &node.name,
            node.metadata.as_deref(),
        )
    }

    /// [`Self::serves_node`] for a row read as columns (`nodes.kind`, the
    /// path, `nodes.name`, `nodes.metadata`).
    ///
    /// - A node of a project file: a `symbol`, or a `test` named other than
    ///   its file, by the symbol rule; every other kind by the file rule.
    /// - A node without a file on record (commit, knowledge, issue, CI run):
    ///   no node records a source binding that could be re-checked, so the
    ///   originless rule decides its name and, when given, its metadata —
    ///   withheld under protection, served in Community developer mode while
    ///   today's content rules leave the text unchanged. Pass the metadata
    ///   whenever it is shown or exported.
    pub fn serves_node_parts(
        &self,
        kind: &str,
        file_path: &str,
        name: &str,
        metadata: Option<&str>,
    ) -> bool {
        let Some(gate) = self.gate.as_ref() else {
            return true;
        };
        if file_path.is_empty() {
            return gate.originless(name) && metadata.is_none_or(|text| gate.originless(text));
        }
        match kind {
            "symbol" | "test" if name != file_path => gate.symbol(file_path, name),
            _ => gate.file(file_path),
        }
    }

    /// `neighbours` of the file `anchor`: nothing when the anchor itself may
    /// not be served, otherwise the neighbours that may.
    pub(super) fn served_neighbours(&self, anchor: &str, neighbours: Vec<String>) -> Vec<String> {
        if !self.serves_file(anchor) {
            return Vec::new();
        }
        neighbours
            .into_iter()
            .filter(|path| self.serves_file(path))
            .collect()
    }
}

/// A graph store as a whole-graph computation reads it — a ranking, a
/// community detection, a rule scan: the rows, and the serve-time source
/// check that decides which of them exist for the computation (G04).
///
/// [`CodeGraph`] answers with the gate of the request that opened it. A bare
/// [`Connection`] has no project root on record and admits everything; it is
/// the form of a graph that is not served from a project (in memory).
pub trait GraphSource {
    /// The connection the rows are read from.
    fn store(&self) -> &Connection;

    /// Whether this source asks current sources at all. `false` for a store
    /// without a project root: a caller that would name several files in one
    /// row must then leave that row to a check that can decide them.
    fn checks_sources(&self) -> bool;

    /// Whether the file `path` may be used now.
    fn file_served(&self, path: &str) -> bool;

    /// Whether the symbol `name` of `path` may be used now.
    fn symbol_served(&self, path: &str, name: &str) -> bool;

    /// The files of `files` that may enter a computation over the whole
    /// graph (`GraphGate::ranking_files`).
    fn rankable_files(
        &self,
        files: Vec<String>,
        edges: &[(String, String)],
        seeds: &[String],
    ) -> HashSet<String>;
}

/// A reference to a source is that source (`&graph` where `graph` is already
/// a `&CodeGraph`).
impl<T: GraphSource + ?Sized> GraphSource for &T {
    fn store(&self) -> &Connection {
        (**self).store()
    }

    fn checks_sources(&self) -> bool {
        (**self).checks_sources()
    }

    fn file_served(&self, path: &str) -> bool {
        (**self).file_served(path)
    }

    fn symbol_served(&self, path: &str, name: &str) -> bool {
        (**self).symbol_served(path, name)
    }

    fn rankable_files(
        &self,
        files: Vec<String>,
        edges: &[(String, String)],
        seeds: &[String],
    ) -> HashSet<String> {
        (**self).rankable_files(files, edges, seeds)
    }
}

impl GraphSource for Connection {
    fn store(&self) -> &Connection {
        self
    }

    fn checks_sources(&self) -> bool {
        false
    }

    fn file_served(&self, _path: &str) -> bool {
        true
    }

    fn symbol_served(&self, _path: &str, _name: &str) -> bool {
        true
    }

    fn rankable_files(
        &self,
        files: Vec<String>,
        _edges: &[(String, String)],
        _seeds: &[String],
    ) -> HashSet<String> {
        files.into_iter().collect()
    }
}

impl GraphSource for CodeGraph {
    fn store(&self) -> &Connection {
        self.connection()
    }

    fn checks_sources(&self) -> bool {
        self.gate.is_some()
    }

    fn file_served(&self, path: &str) -> bool {
        self.serves_file(path)
    }

    fn symbol_served(&self, path: &str, name: &str) -> bool {
        self.serves_symbol(path, name)
    }

    fn rankable_files(
        &self,
        files: Vec<String>,
        edges: &[(String, String)],
        seeds: &[String],
    ) -> HashSet<String> {
        match self.gate.as_ref() {
            Some(gate) => gate.ranking_files(files, edges, seeds),
            None => files.into_iter().collect(),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::core::graph_index::{FileEntry, ProjectIndex, SymbolEntry};
    use crate::core::graph_provider::GraphProvider;
    use crate::core::policy::runtime::TestPolicyOverride;

    /// A file and its symbol are served while the file exists and are
    /// withheld — path, entry and symbol name — once it is removed.
    #[test]
    fn a_graph_reference_is_withheld_once_its_source_file_is_gone() {
        let _data = crate::core::data_dir::isolated_data_dir();
        let _community = TestPolicyOverride::set(None);
        let project = tempfile::tempdir().expect("project");
        let root = project.path().to_string_lossy().into_owned();
        std::fs::write(project.path().join("auth.rs"), "pub fn verify() {}\n").expect("source");

        let mut index = ProjectIndex::new(&root);
        index.files.insert(
            "auth.rs".to_owned(),
            FileEntry {
                path: "auth.rs".to_owned(),
                hash: String::new(),
                language: "rust".to_owned(),
                line_count: 1,
                token_count: 4,
                exports: vec!["verify".to_owned()],
                summary: "pub fn verify() {}".to_owned(),
            },
        );
        index.symbols.insert(
            "auth.rs::verify".to_owned(),
            SymbolEntry {
                file: "auth.rs".to_owned(),
                name: "verify".to_owned(),
                kind: "fn".to_owned(),
                start_line: 1,
                end_line: 1,
                is_exported: true,
            },
        );
        let provider = GraphProvider::GraphIndex(index);

        assert_eq!(provider.file_paths(), vec!["auth.rs".to_owned()]);
        assert_eq!(provider.all_symbols().len(), 1);
        assert!(provider.get_file_entry("auth.rs").is_some());

        std::fs::remove_file(project.path().join("auth.rs")).expect("remove");
        assert!(provider.file_paths().is_empty());
        assert!(provider.all_symbols().is_empty());
        assert!(provider.get_symbol("auth.rs::verify").is_none());
        assert!(provider.get_file_entry("auth.rs").is_none());
        assert!(provider.materialize_project_index(&root).files.is_empty());
    }

    /// Under protection a graph with more files than one request can decide
    /// is ranked locally: from the seeds outwards, never more files than the
    /// bound, never one the gate did not decide — and not at all without
    /// seeds.
    #[test]
    fn a_ranking_under_protection_uses_only_files_decided_from_its_seeds() {
        use super::{GraphGate, PROTECTED_FILES};

        let _data = crate::core::data_dir::isolated_data_dir();
        let pack = crate::core::policy::parse(
            "name = \"graph-ranking-test\"\nversion = \"1.0.0\"\ndescription = \"test\"\n\
             [redaction]\ncustomer = 'K-[0-9]{6}'",
        )
        .expect("valid policy");
        let _protected = TestPolicyOverride::set(Some(
            crate::core::policy::resolve(&pack).expect("resolved policy"),
        ));
        let project = tempfile::tempdir().expect("project");
        let root = project.path().to_string_lossy().into_owned();
        // A chain f000 → f001 → … two files longer than the bound.
        let files: Vec<String> = (0..PROTECTED_FILES + 2)
            .map(|n| format!("f{n:03}.rs"))
            .collect();
        for file in &files {
            std::fs::write(project.path().join(file), "pub fn item() {}\n").expect("source");
        }
        let edges: Vec<(String, String)> = files
            .windows(2)
            .map(|pair| (pair[0].clone(), pair[1].clone()))
            .collect();

        let gate = GraphGate::for_root(&root);
        let local = gate.ranking_files(files.clone(), &edges, &files[..1]);
        assert!(local.contains(&files[0]), "the seed is decided first");
        assert!(local.len() <= PROTECTED_FILES);
        assert!(
            !local.contains(&files[PROTECTED_FILES + 1]),
            "beyond the bound"
        );
        // Whatever the ranking uses, this gate decided and serves.
        assert!(local.iter().all(|file| gate.file(file)));
        // The far end was never decided; the spent gate refuses it.
        assert!(!gate.file(&files[PROTECTED_FILES + 1]));

        let unseeded = GraphGate::for_root(&root).ranking_files(files, &edges, &[]);
        assert!(unseeded.is_empty());
    }

    /// A withdrawn file is no step of a walk: with `a → w → b` and `a → n`,
    /// `b` is reached from `a` (and `a` from `b`) only while `w` is served.
    /// Once `w` is gone nothing is reached through it, its edges are not
    /// counted, and `a`'s neighbour that still exists is still found.
    #[test]
    fn a_traversal_does_not_pass_through_a_withdrawn_file() {
        use crate::core::property_graph::{CodeGraph, Edge, EdgeKind, Node};

        let _data = crate::core::data_dir::isolated_data_dir();
        let _community = TestPolicyOverride::set(None);
        let project = tempfile::tempdir().expect("project");
        let root = project.path().to_string_lossy().into_owned();
        for file in ["a.rs", "w.rs", "b.rs", "n.rs"] {
            std::fs::write(project.path().join(file), "pub fn item() {}\n").expect("source");
        }

        let graph = CodeGraph::open(&root).expect("graph");
        let node = |file: &str| graph.upsert_node(&Node::file(file)).expect("node");
        let (a, w, b, n) = (node("a.rs"), node("w.rs"), node("b.rs"), node("n.rs"));
        for (from, to) in [(a, w), (w, b), (a, n)] {
            graph
                .upsert_edge(&Edge::new(from, to, EdgeKind::Imports))
                .expect("edge");
        }

        let chain = graph.dependency_chain("a.rs", "b.rs").expect("chain");
        assert_eq!(
            chain.map(|chain| chain.path),
            Some(vec![
                "a.rs".to_owned(),
                "w.rs".to_owned(),
                "b.rs".to_owned()
            ])
        );
        let impact = graph.impact_analysis("b.rs", 5).expect("impact");
        assert_eq!(impact.affected_files, ["a.rs", "w.rs"]);
        assert_eq!(impact.edges_traversed, 2);

        std::fs::remove_file(project.path().join("w.rs")).expect("withdraw");
        // A new request: a gate decides a file once.
        let graph = CodeGraph::open(&root).expect("graph");
        assert!(
            graph
                .dependency_chain("a.rs", "b.rs")
                .expect("chain")
                .is_none()
        );
        let impact = graph.impact_analysis("b.rs", 5).expect("impact");
        assert!(impact.affected_files.is_empty(), "{impact:?}");
        assert_eq!(impact.edges_traversed, 0);
        assert_eq!(impact.max_depth_reached, 0);
        let near = graph.dependency_chain("a.rs", "n.rs").expect("chain");
        assert_eq!(near.map(|chain| chain.depth), Some(1));
        let related: Vec<String> = graph
            .related_files("a.rs", 10)
            .expect("related")
            .into_iter()
            .map(|(path, _)| path)
            .collect();
        assert_eq!(related, ["n.rs"]);
    }
}