lean-ctx-server 3.12.3

The LeanCTX MCP server and its tools on top of LeanCTX | OSS.
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// SPDX-License-Identifier: Apache-2.0
use std::path::{Path, PathBuf};
use std::time::Duration;

use rmcp::ErrorData;
use rmcp::model::Tool;
use serde_json::{Map, Value, json};

use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_int, get_str};
use lean_ctx::core::git::{clone, repo_url, run_git};
use lean_ctx::core::protocol::append_savings;
use lean_ctx::core::tokens::count_tokens;
use lean_ctx::tool_defs::tool_def;

const DEFAULT_MAX_TOKENS: usize = 6000;
const MAX_TREE_LINES: usize = 400;
const MAX_GREP_LINES: usize = 200;
/// Tool identity recorded with the admission of remote repository content.
const TOOL: &str = "ctx_git_read";

/// `ctx_git_read` — read a remote repository via a cached shallow clone instead
/// of scraping its web page. Modes: overview / tree / read / grep.
pub struct CtxGitReadTool;

impl McpTool for CtxGitReadTool {
    fn name(&self) -> &'static str {
        "ctx_git_read"
    }

    fn tool_def(&self) -> Tool {
        tool_def(
            "ctx_git_read",
            "Read remote git repos via cached shallow clone (not HTML scraping).\n\
             modes: overview (tree + README) | tree (file list) | read (file content) | grep (search).\n\
             Accepts repo URLs and GitHub/GitLab blob/tree links (ref+path auto-detected).\n\
             https-only, SSRF-guarded. Prefer over ctx_url_read for whole-repo access.",
            json!({
                "type": "object",
                "properties": {
                    "url": { "type": "string", "description": "https repo URL (blob/tree links auto-detect ref+path)" },
                    "mode": {
                        "type": "string",
                        "enum": ["overview", "tree", "read", "grep"],
                        "description": "overview|tree|read|grep"
                    },
                    "path": { "type": "string", "description": "Path within repo" },
                    "ref": { "type": "string", "description": "Branch/tag/commit (overrides URL ref)" },
                    "query": { "type": "string", "description": "Search term for grep mode" },
                    "max_tokens": { "type": "integer", "description": "Token budget (default: 6000)" },
                    "timeout_secs": { "type": "integer", "description": "Timeout seconds (default: 90, max: 300)" }
                },
                "allOf": [
                    {
                        "if": { "properties": { "mode": { "const": "grep" } }, "required": ["mode"] },
                        "then": { "required": ["mode", "query"] }
                    }
                ],
                "required": ["url"]
            }),
        )
    }

    fn handle(
        &self,
        args: &Map<String, Value>,
        _ctx: &ToolContext,
    ) -> Result<ToolOutput, ErrorData> {
        let url = get_str(args, "url")
            .ok_or_else(|| ErrorData::invalid_params("url is required", None))?;

        let mut repo = repo_url::parse(&url).ok_or_else(|| {
            ErrorData::invalid_params(
                "not a recognized https repo URL (expected https://<host>/<owner>/<repo>[/blob|tree/<ref>/<path>])",
                None,
            )
        })?;
        if let Some(r) = get_str(args, "ref") {
            repo.git_ref = Some(r);
        }

        let path = get_str(args, "path").or_else(|| repo.subpath.clone());
        let mode = get_str(args, "mode").unwrap_or_else(|| {
            if path.is_some() {
                "read".to_string()
            } else {
                "overview".to_string()
            }
        });
        let max_tokens = get_int(args, "max_tokens")
            .map_or(DEFAULT_MAX_TOKENS, |n| n.clamp(200, 50_000) as usize);
        let timeout = Duration::from_secs(
            get_int(args, "timeout_secs").map_or(clone::DEFAULT_CLONE_TIMEOUT_SECS, |n| {
                n.clamp(5, 300) as u64
            }),
        );
        let query = get_str(args, "query");

        let result = tokio::task::block_in_place(|| {
            let dir = clone::ensure_repo(&repo, timeout)?;
            match mode.as_str() {
                "overview" => render_overview(&dir, &repo, max_tokens),
                "tree" => render_tree(&dir, path.as_deref()),
                "read" => {
                    let p = path
                        .as_deref()
                        .ok_or_else(|| "read mode requires 'path'".to_string())?;
                    render_read(&dir, &repo, p, max_tokens)
                }
                "grep" => {
                    let q = query
                        .as_deref()
                        .ok_or_else(|| "grep mode requires 'query'".to_string())?;
                    render_grep(&dir, q, path.as_deref())
                }
                other => Err(format!(
                    "invalid mode '{other}' (use: overview, tree, read, grep)"
                )),
            }
        });

        match result {
            Ok(rendered) => {
                let sent = count_tokens(&rendered.body);
                let saved = rendered.original_tokens.saturating_sub(sent);
                let text = append_savings(&rendered.body, rendered.original_tokens, sent);
                Ok(ToolOutput {
                    text,
                    original_tokens: rendered.original_tokens,
                    saved_tokens: saved,
                    mode: Some(mode),
                    path: Some(rendered.label),
                    changed: false,
                    shell_outcome: None,
                    content_blocks: None,
                })
            }
            Err(e) => Err(ErrorData::invalid_params(
                format!("ctx_git_read failed: {e}"),
                None,
            )),
        }
    }
}

struct Rendered {
    body: String,
    original_tokens: usize,
    label: String,
}

fn render_overview(
    dir: &Path,
    repo: &repo_url::RepoRef,
    max_tokens: usize,
) -> Result<Rendered, String> {
    let files = list_files(dir, None)?;
    let total = files.len();
    let top = top_level_summary(&files);

    let mut body = format!(
        "# {} ({} files)\n\nRef: {}\n\n## Top-level\n{}\n",
        repo.project_path(),
        total,
        repo.git_ref.as_deref().unwrap_or("default (HEAD)"),
        top
    );
    match find_and_read_readme(dir) {
        Some((_, Ok(readme))) if !readme.is_empty() => {
            body.push_str("\n## README\n");
            body.push_str(&readme);
        }
        Some((name, Err(reason))) => {
            // A README that is withheld is named as such; the overview never
            // looks as if the repository had none.
            body.push_str("\n## README\n");
            body.push_str(&crate::tools::admitted_sources::withheld_line(
                name, &reason,
            ));
        }
        Some((_, Ok(_))) | None => {}
    }
    let original_tokens = count_tokens(&body);
    Ok(Rendered {
        body: budget(&body, max_tokens),
        original_tokens,
        label: format!("{} overview", repo.project_path()),
    })
}

fn render_tree(dir: &Path, subpath: Option<&str>) -> Result<Rendered, String> {
    let files = list_files(dir, subpath)?;
    let original_tokens = count_tokens(&files.join("\n"));
    let shown: Vec<&String> = files.iter().take(MAX_TREE_LINES).collect();
    let mut body = shown
        .iter()
        .map(|s| s.as_str())
        .collect::<Vec<_>>()
        .join("\n");
    if files.len() > shown.len() {
        body.push_str(&format!(
            "\n… {} more file(s) (narrow with `path`)",
            files.len() - shown.len()
        ));
    }
    Ok(Rendered {
        body,
        original_tokens,
        label: format!("tree {}", subpath.unwrap_or(".")),
    })
}

fn render_read(
    dir: &Path,
    repo: &repo_url::RepoRef,
    rel: &str,
    max_tokens: usize,
) -> Result<Rendered, String> {
    let file = safe_join(dir, rel)?;
    if file.is_dir() {
        // A directory was requested in read mode — show its listing instead.
        return render_tree(dir, Some(rel));
    }
    // Admission precedes the header, the token count and the budget cut: the
    // whole file is judged, then only admitted text is rendered.
    let content = admit_clone_file(dir, rel)
        .map_err(|reason| format!("{rel}: {reason}"))?
        .text;
    let header = format!(
        "// {} @ {}\n",
        rel,
        repo.git_ref.as_deref().unwrap_or("HEAD")
    );
    let body = format!("{header}{content}");
    let original_tokens = count_tokens(&body);
    Ok(Rendered {
        body: budget(&body, max_tokens),
        original_tokens,
        label: format!("{}:{}", repo.project_path(), rel),
    })
}

fn render_grep(dir: &Path, query: &str, subpath: Option<&str>) -> Result<Rendered, String> {
    let mut args: Vec<&str> = vec![
        "grep",
        "--no-color",
        "-n",
        "-I",
        "-i",
        "--heading",
        "-e",
        query,
    ];
    if let Some(p) = subpath {
        args.push("--");
        args.push(p);
    }
    let out = run_git(&args, dir, Duration::from_secs(30), &[])?;
    // git grep exits 1 with no matches — treat that as an empty result, not error.
    if !out.success && !out.stdout.is_empty() {
        return Err(out.stderr.trim().to_string());
    }
    // The scope is admitted whether or not git found anything: what the
    // result says about withheld sources must not depend on the query.
    let scope = list_files(dir, subpath)?;
    let original_tokens = count_tokens(&out.stdout);
    Ok(Rendered {
        body: admitted_grep(dir, query, &out.stdout, &scope),
        original_tokens,
        label: format!("grep '{query}'"),
    })
}

/// Files of one grep scope that are admitted before selection.
const MAX_GREP_SCOPE_FILES: usize = 2000;
/// Original bytes one grep scope may read for admission.
const MAX_GREP_SCOPE_BYTES: u64 = 64 * 1024 * 1024;

/// Rebuild `git grep --heading -n` output from sources admitted BEFORE
/// selection (G01), the way `ctx_search` treats a project.
///
/// Every file of the searched scope is admitted — independently of whether it
/// matched. git's output is then used for exactly two things: which file a
/// group belongs to and which line numbers matched. A line is rendered only
/// for a file the admission delivered, with its text taken from the admitted
/// file. A file that is withheld contributes nothing, not even its name, and
/// the note that reports withheld sources is a count over the WHOLE scope:
/// the result is the same whether a withheld file matched the query or not,
/// so a query cannot be used to probe its content. For a scope no rule
/// touches the result is byte-identical to git's own output.
fn admitted_grep(dir: &Path, query: &str, stdout: &str, scope: &[String]) -> String {
    let matched: std::collections::HashSet<&str> = stdout
        .lines()
        .filter(|line| matched_line_number(line).is_none())
        .collect();

    let mut admitted: std::collections::HashMap<&str, Vec<String>> =
        std::collections::HashMap::new();
    let mut withheld = 0usize;
    let mut covered = 0usize;
    let mut bytes_left = MAX_GREP_SCOPE_BYTES;
    for file in scope.iter().take(MAX_GREP_SCOPE_FILES) {
        let Ok(bytes) = read_clone_text_bytes(dir, file) else {
            // Not a text source (binary, too large, unreadable): git's `-I`
            // skips it as well; it is neither searched nor withheld.
            covered += 1;
            continue;
        };
        if bytes.len() as u64 > bytes_left {
            break;
        }
        bytes_left -= bytes.len() as u64;
        covered += 1;
        match crate::tools::admitted_sources::admit_remote_file(bytes, file, TOOL) {
            Ok(source) => {
                if matched.contains(file.as_str()) {
                    admitted.insert(
                        file.as_str(),
                        source.text.lines().map(str::to_string).collect(),
                    );
                }
            }
            Err(_) => withheld += 1,
        }
    }

    let mut rendered: Vec<String> = Vec::new();
    // `None`: no heading seen yet, or the current file was not admitted.
    let mut current: Option<&Vec<String>> = None;
    for line in stdout.lines() {
        if rendered.len() >= MAX_GREP_LINES {
            break;
        }
        if let Some(number) = matched_line_number(line) {
            // A matched line the admitted text does not have is dropped:
            // git's raw copy of it is never used.
            if let Some(text) = current.and_then(|lines| lines.get(number - 1)) {
                rendered.push(format!("{number}:{text}"));
            }
            continue;
        }
        current = admitted.get(line);
        if current.is_some() {
            rendered.push(line.to_string());
        }
    }

    let mut body = if rendered.is_empty() {
        format!("No matches for '{query}'.")
    } else {
        rendered.join("\n")
    };
    if withheld > 0 {
        // Same wording as `ctx_search`.
        body.push_str(&format!("\n({withheld} sources withheld under policy)"));
    }
    if covered < scope.len() {
        body.push_str(&format!(
            "\n(search covered the first {covered} of {} files — narrow with `path`)",
            scope.len()
        ));
    }
    body
}

/// The 1-based line number of a `<n>:<text>` match line; `None` for a heading.
fn matched_line_number(line: &str) -> Option<usize> {
    let (number, _) = line.split_once(':')?;
    number.parse::<usize>().ok().filter(|number| *number > 0)
}

/// The bytes of one text file of the clone. `Err` (content-free) for a path
/// outside the clone, an unreadable or oversized file and a binary body —
/// none of which is inspectable as text.
fn read_clone_text_bytes(dir: &Path, rel: &str) -> Result<Vec<u8>, String> {
    let file = safe_join(dir, rel)?;
    let cap = lean_ctx::core::limits::max_read_bytes() as u64;
    let size = std::fs::metadata(&file)
        .map_err(|e| format!("cannot read file: {e}"))?
        .len();
    if size > cap {
        return Err(format!(
            "file too large ({size} bytes, limit {cap} bytes via LCTX_MAX_READ_BYTES)"
        ));
    }
    let bytes = std::fs::read(&file).map_err(|e| format!("cannot read file: {e}"))?;
    if lean_ctx::core::binary_detect::has_binary_extension(rel)
        || lean_ctx::core::text_decode::looks_binary(&bytes[..bytes.len().min(8192)])
    {
        return Err("source format is not inspectable as text".to_owned());
    }
    Ok(bytes)
}

/// Read one file of the clone and admit it as a source that is not a project
/// file, under its repository-relative path. `Err` is a content-free reason.
fn admit_clone_file(
    dir: &Path,
    rel: &str,
) -> Result<lean_ctx::core::context_admission::SourceAdmission, String> {
    let bytes = read_clone_text_bytes(dir, rel)?;
    crate::tools::admitted_sources::admit_remote_file(bytes, rel, TOOL)
}

// ── helpers ─────────────────────────────────────────────────────────────────

/// Tracked files (gitignore-aware), optionally scoped to a subpath.
fn list_files(dir: &Path, subpath: Option<&str>) -> Result<Vec<String>, String> {
    let mut args = vec!["ls-files"];
    if let Some(p) = subpath {
        args.push("--");
        args.push(p);
    }
    let out = run_git(&args, dir, Duration::from_secs(20), &[])?.ok_stdout()?;
    Ok(out
        .lines()
        .filter(|l| !l.trim().is_empty())
        .map(str::to_string)
        .collect())
}

fn top_level_summary(files: &[String]) -> String {
    use std::collections::BTreeMap;
    let mut counts: BTreeMap<String, usize> = BTreeMap::new();
    for f in files {
        let top = f.split('/').next().unwrap_or(f);
        let key = if top == f.as_str() {
            top.to_string() // a top-level file
        } else {
            format!("{top}/")
        };
        *counts.entry(key).or_insert(0) += 1;
    }
    counts
        .into_iter()
        .take(40)
        .map(|(k, n)| {
            if k.ends_with('/') {
                format!("- {k} ({n})")
            } else {
                format!("- {k}")
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// The repository's README, admitted: its name and either the delivered text
/// or the content-free reason it is withheld for. `None`: there is none.
fn find_and_read_readme(dir: &Path) -> Option<(&'static str, Result<String, String>)> {
    for name in [
        "README.md",
        "README.MD",
        "Readme.md",
        "README",
        "README.txt",
    ] {
        if dir.join(name).is_file() {
            return Some((
                name,
                admit_clone_file(dir, name).map(|admitted| admitted.text),
            ));
        }
    }
    None
}

/// Join `rel` under `base`, rejecting any path that escapes `base`.
fn safe_join(base: &Path, rel: &str) -> Result<PathBuf, String> {
    let rel = rel.trim_start_matches('/');
    if rel.split('/').any(|seg| seg == "..") {
        return Err("path may not contain '..'".to_string());
    }
    let joined = base.join(rel);
    let canon_base = std::fs::canonicalize(base).map_err(|e| e.to_string())?;
    match std::fs::canonicalize(&joined) {
        Ok(canon) if canon.starts_with(&canon_base) => Ok(canon),
        Ok(_) => Err("path escapes the repository".to_string()),
        Err(e) => Err(format!("path not found: {rel} ({e})")),
    }
}

fn budget(content: &str, max_tokens: usize) -> String {
    let tokens = count_tokens(content);
    if tokens <= max_tokens {
        return content.to_string();
    }
    let ratio = max_tokens as f64 / tokens as f64;
    let keep = ((content.chars().count() as f64 * ratio) as usize).max(1);
    let truncated: String = content.chars().take(keep).collect();
    lean_ctx::core::recovery::with_original_pointer(
        "ctx_git_read",
        "budget",
        content,
        format!("{truncated}\n\n…[truncated to ~{max_tokens} tokens]"),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn safe_join_blocks_parent_escape() {
        let tmp = std::env::temp_dir();
        assert!(safe_join(&tmp, "../etc/passwd").is_err());
        assert!(safe_join(&tmp, "a/../../b").is_err());
    }

    #[test]
    fn top_level_summary_groups_dirs_and_files() {
        let files = vec![
            "README.md".to_string(),
            "src/a.rs".to_string(),
            "src/b.rs".to_string(),
            "tests/t.rs".to_string(),
        ];
        let s = top_level_summary(&files);
        assert!(s.contains("- src/ (2)"));
        assert!(s.contains("- tests/ (1)"));
        assert!(s.contains("- README.md"));
    }

    const AWS_KEY: &str = concat!("AK", "IAIOSFODNN7EXAMPLE");

    fn clone_file(dir: &Path, rel: &str, content: &str) {
        let path = dir.join(rel);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, content).unwrap();
    }

    fn repo() -> repo_url::RepoRef {
        repo_url::parse("https://github.com/acme/widgets").expect("repo url")
    }

    fn blocking_policy() -> lean_ctx::core::policy::ResolvedPolicy {
        lean_ctx::core::policy::load(
            "name = \"ctx-git-read-test\"\nversion = \"1.0.0\"\ndescription = \"test\"\n\
             [filters]\nclassification = \"block\"\nblocked_labels = [\"CONFIDENTIAL\"]\n",
        )
        .unwrap()
    }

    /// G01: a file of a remote repository is a source. The old `read` mode
    /// returned `fs::read_to_string` of the clone verbatim — a key in the
    /// remote file reached the model, and a file the project policy blocks
    /// was delivered. A clean file keeps its bytes.
    #[test]
    fn a_remote_file_is_admitted_before_it_is_rendered() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let clone = tempfile::tempdir().unwrap();
        let clean = "pub fn widget() -> u8 {\n    7\n}\n";
        clone_file(clone.path(), "src/lib.rs", clean);
        clone_file(
            clone.path(),
            "deploy/settings.py",
            &format!("AWS_ACCESS_KEY_ID = \"{AWS_KEY}\"\nREGION = \"eu-central-2\"\n"),
        );
        clone_file(
            clone.path(),
            "docs/plan.md",
            "CONFIDENTIAL\nroadmap-marker for the launch\n",
        );
        {
            let _community = lean_ctx::core::policy::runtime::TestPolicyOverride::set(None);
            let rendered = render_read(clone.path(), &repo(), "src/lib.rs", 6000).unwrap();
            assert_eq!(rendered.body, format!("// src/lib.rs @ HEAD\n{clean}"));

            let rendered = render_read(clone.path(), &repo(), "deploy/settings.py", 6000).unwrap();
            assert!(!rendered.body.contains(AWS_KEY), "{}", rendered.body);
            assert!(rendered.body.contains("eu-central-2"), "{}", rendered.body);
        }
        let _policy =
            lean_ctx::core::policy::runtime::TestPolicyOverride::set(Some(blocking_policy()));
        let Err(reason) = render_read(clone.path(), &repo(), "docs/plan.md", 6000) else {
            panic!("a blocked remote file must not be rendered");
        };
        assert!(reason.contains("withheld"), "{reason}");
        assert!(!reason.contains("roadmap-marker"), "{reason}");
    }

    /// The README of the overview and every line of a grep result come from
    /// admitted sources; a withheld file is named, never rendered and never
    /// silently missing. git's own raw match text is not used.
    #[test]
    fn overview_readme_and_grep_lines_come_from_admitted_sources() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let clone = tempfile::tempdir().unwrap();
        clone_file(
            clone.path(),
            "notes.txt",
            "alpha widget\nbeta\ngamma widget\n",
        );
        clone_file(
            clone.path(),
            "deploy/settings.py",
            &format!("# widget deploy\nAWS_ACCESS_KEY_ID = \"{AWS_KEY}\" # widget\n"),
        );
        clone_file(
            clone.path(),
            "docs/plan.md",
            "CONFIDENTIAL\nwidget roadmap-marker\n",
        );
        clone_file(clone.path(), "README.md", "CONFIDENTIAL\nreadme-marker\n");
        // What `git grep --heading -n -i -e widget` prints for this clone.
        let git_output = format!(
            "deploy/settings.py\n1:# widget deploy\n2:AWS_ACCESS_KEY_ID = \"{AWS_KEY}\" # widget\n\
             docs/plan.md\n2:widget roadmap-marker\n\
             notes.txt\n1:alpha widget\n3:gamma widget\n"
        );
        // What `git ls-files` lists for this clone (the grep scope).
        let scope: Vec<String> = [
            "README.md",
            "deploy/settings.py",
            "docs/plan.md",
            "notes.txt",
        ]
        .map(String::from)
        .to_vec();
        {
            // Control: for a scope no rule touches the result is git's output.
            let _community = lean_ctx::core::policy::runtime::TestPolicyOverride::set(None);
            let clean_output = "notes.txt\n1:alpha widget\n3:gamma widget\n";
            assert_eq!(
                admitted_grep(clone.path(), "widget", clean_output, &scope),
                clean_output.trim_end()
            );
            let body = admitted_grep(clone.path(), "widget", &git_output, &scope);
            assert!(!body.contains(AWS_KEY), "{body}");
            assert!(body.contains("1:# widget deploy"), "{body}");
        }
        let _policy =
            lean_ctx::core::policy::runtime::TestPolicyOverride::set(Some(blocking_policy()));
        let body = admitted_grep(clone.path(), "widget", &git_output, &scope);
        assert!(!body.contains("roadmap-marker"), "{body}");
        assert!(
            !body.contains("docs/plan.md"),
            "a withheld file is not named: {body}"
        );
        assert!(
            body.contains("notes.txt\n1:alpha widget\n3:gamma widget"),
            "{body}"
        );
        // README.md and docs/plan.md are blocked by the pack. deploy/settings.py
        // is NOT withheld: a key inside otherwise admissible content is masked
        // and the rest delivered, with a pack exactly as without one. The
        // pack's built-in pass and the gateway spell the mask the same way, so
        // their two orders agree (`compose_active_gateway_output`).
        let keyed = admit_clone_file(clone.path(), "deploy/settings.py")
            .expect("a source with a masked key is delivered under a pack");
        assert_eq!(
            keyed.text,
            "# widget deploy\nAWS_ACCESS_KEY_ID = \"[REDACTED:AWS key]\" # widget\n"
        );
        assert!(
            body.contains(
                "deploy/settings.py\n1:# widget deploy\n\
                 2:AWS_ACCESS_KEY_ID = \"[REDACTED:AWS key]\" # widget"
            ),
            "{body}"
        );
        assert!(!body.contains(AWS_KEY), "{body}");
        // Counted over the scope.
        assert!(
            body.ends_with("\n(2 sources withheld under policy)"),
            "{body}"
        );

        // No oracle: a query that matches ONLY a withheld file and a query
        // that matches nothing produce the same result.
        let only_withheld = admitted_grep(
            clone.path(),
            "q",
            "docs/plan.md\n2:widget roadmap-marker\n",
            &scope,
        );
        let nothing = admitted_grep(clone.path(), "q", "", &scope);
        assert_eq!(only_withheld, nothing);
        assert_eq!(
            nothing,
            "No matches for 'q'.\n(2 sources withheld under policy)"
        );

        let (name, readme) = find_and_read_readme(clone.path()).expect("README present");
        assert_eq!(name, "README.md");
        let reason = readme.expect_err("a blocked README is withheld");
        assert!(!reason.contains("readme-marker"), "{reason}");
    }

    #[test]
    fn budget_truncates_oversized_content() {
        let big = "word ".repeat(4000);
        let out = budget(&big, 50);
        assert!(out.contains("[truncated"));
        assert!(count_tokens(&out) < count_tokens(&big));
    }
}