git-prism 0.8.0

Agent-optimized git data MCP server — structured change manifests and full file snapshots for LLM agents
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
use super::{CallSite, Function, LanguageAnalyzer, MAX_RECURSION_DEPTH, body_hash_for_node};
use tree_sitter::Parser;

pub struct CSharpAnalyzer;

fn create_parser() -> Parser {
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_c_sharp::LANGUAGE.into())
        .expect("Error loading C# grammar");
    parser
}

fn signature_text(source: &[u8], node: &tree_sitter::Node) -> String {
    let start = node.start_byte();
    let body = node.child_by_field_name("body");
    let end = body.map_or(node.end_byte(), |b| b.start_byte());
    let raw = &source[start..end];
    String::from_utf8_lossy(raw).trim().to_string()
}

fn extract_methods_from_class(
    source: &[u8],
    class_node: &tree_sitter::Node,
    functions: &mut Vec<Function>,
) {
    let class_name = class_node
        .child_by_field_name("name")
        .and_then(|n| n.utf8_text(source).ok())
        .unwrap_or("");

    let body = match class_node.child_by_field_name("body") {
        Some(b) => b,
        None => return,
    };

    let mut cursor = body.walk();
    for child in body.children(&mut cursor) {
        if child.kind() == "method_declaration" || child.kind() == "constructor_declaration" {
            let method_name = child
                .child_by_field_name("name")
                .and_then(|n| n.utf8_text(source).ok())
                .unwrap_or("");
            let name = format!("{class_name}.{method_name}");
            let signature = signature_text(source, &child);
            let body_hash = body_hash_for_node(source, child);
            functions.push(Function {
                name,
                signature,
                start_line: child.start_position().row + 1,
                end_line: child.end_position().row + 1,
                body_hash,
            });
        }
    }
}

impl LanguageAnalyzer for CSharpAnalyzer {
    fn extract_functions(&self, source: &[u8]) -> anyhow::Result<Vec<Function>> {
        let mut parser = create_parser();
        let tree = parser
            .parse(source, None)
            .ok_or_else(|| anyhow::anyhow!("Failed to parse C# source"))?;
        let root = tree.root_node();
        let mut functions = Vec::new();

        // C# files may have a namespace declaration wrapping classes,
        // or classes at the top level. We need to handle both.
        //
        // `depth` bounds recursion to prevent stack overflow on adversarial
        // input (e.g., thousands of nested namespaces). Methods extracted
        // before the limit is hit are still returned.
        fn visit_node(
            source: &[u8],
            node: &tree_sitter::Node,
            functions: &mut Vec<Function>,
            depth: usize,
        ) {
            if depth >= MAX_RECURSION_DEPTH {
                tracing::warn!(
                    depth_limit = MAX_RECURSION_DEPTH,
                    language = "csharp",
                    operation = "functions",
                    "tree-sitter depth guard fired: recursive walk truncated; some functions may be missing"
                );
                return;
            }
            match node.kind() {
                "class_declaration" | "struct_declaration" | "record_declaration" => {
                    extract_methods_from_class(source, node, functions);
                }
                _ => {
                    let mut cursor = node.walk();
                    for child in node.children(&mut cursor) {
                        visit_node(source, &child, functions, depth + 1);
                    }
                }
            }
        }

        visit_node(source, &root, &mut functions, 0);

        Ok(functions)
    }

    fn extract_calls(&self, source: &[u8]) -> anyhow::Result<Vec<CallSite>> {
        let mut parser = create_parser();
        let tree = parser
            .parse(source, None)
            .ok_or_else(|| anyhow::anyhow!("Failed to parse C# source"))?;

        let mut calls = Vec::new();
        let mut stack = vec![tree.root_node()];
        while let Some(node) = stack.pop() {
            if node.kind() == "invocation_expression"
                && let Some(expr) = node.child(0)
            {
                let callee = expr.utf8_text(source).unwrap_or("").to_string();
                let (is_method_call, receiver) = match expr.kind() {
                    "member_access_expression" => {
                        let recv = expr
                            .child_by_field_name("expression")
                            .and_then(|n| n.utf8_text(source).ok())
                            .map(|s| s.to_string());
                        (true, recv)
                    }
                    _ => (false, None),
                };
                calls.push(CallSite {
                    callee,
                    line: node.start_position().row + 1,
                    is_method_call,
                    receiver,
                });
            }
            for i in (0..node.child_count()).rev() {
                if let Some(child) = node.child(i as u32) {
                    stack.push(child);
                }
            }
        }
        calls.sort_by_key(|c| c.line);
        Ok(calls)
    }

    fn extract_imports(&self, source: &[u8]) -> anyhow::Result<Vec<String>> {
        let mut parser = create_parser();
        let tree = parser
            .parse(source, None)
            .ok_or_else(|| anyhow::anyhow!("Failed to parse C# source"))?;
        let root = tree.root_node();
        let mut imports = Vec::new();

        let mut cursor = root.walk();
        for child in root.children(&mut cursor) {
            if child.kind() == "using_directive" {
                let text = child.utf8_text(source).unwrap_or("");
                // Strip "using" keyword, optional "static" keyword, and trailing ";"
                let import_path = text
                    .trim()
                    .trim_start_matches("using")
                    .trim()
                    .trim_start_matches("static")
                    .trim()
                    .trim_end_matches(';')
                    .trim()
                    .to_string();
                if !import_path.is_empty() {
                    imports.push(import_path);
                }
            }
        }

        Ok(imports)
    }
}

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

    #[test]
    fn extracts_simple_method() {
        let source = br#"public class Calculator {
    public int Add(int a, int b) {
        return a + b;
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "Calculator.Add");
        assert_eq!(functions[0].start_line, 2);
        assert_eq!(functions[0].end_line, 4);
    }

    #[test]
    fn extracts_multiple_methods() {
        let source = br#"public class Math {
    public int Add(int a, int b) {
        return a + b;
    }

    public int Subtract(int a, int b) {
        return a - b;
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 2);
        assert_eq!(functions[0].name, "Math.Add");
        assert_eq!(functions[1].name, "Math.Subtract");
    }

    #[test]
    fn extracts_constructor() {
        let source = br#"public class Person {
    private string name;

    public Person(string name) {
        this.name = name;
    }

    public string GetName() {
        return this.name;
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 2);
        assert_eq!(functions[0].name, "Person.Person");
        assert_eq!(functions[1].name, "Person.GetName");
    }

    #[test]
    fn extracts_static_method() {
        let source = br#"public class Utils {
    public static string Format(string s) {
        return s.Trim();
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "Utils.Format");
    }

    // Checks both methods in one fixture; row > 0 ensures row+1 != row*1 and row+1 != row-1.
    #[test]
    fn it_reports_correct_line_numbers_for_methods_inside_namespace() {
        let source = br#"using System;

namespace MyApp {
    public class Calculator {
        public int Add(int a, int b) {
            return a + b;
        }

        public int Subtract(int a, int b) {
            return a - b;
        }
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 2);
        assert_eq!(functions[0].name, "Calculator.Add");
        assert_eq!(functions[0].start_line, 5);
        assert_eq!(functions[0].end_line, 7);
        assert_eq!(functions[1].name, "Calculator.Subtract");
        assert_eq!(functions[1].start_line, 9);
        assert_eq!(functions[1].end_line, 11);
    }

    #[test]
    fn empty_file_returns_no_functions() {
        let source = b"";
        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source).unwrap();
        assert!(functions.is_empty());
    }

    #[test]
    fn extracts_single_using() {
        let source = br#"using System;

public class Foo {}
"#;
        let analyzer = CSharpAnalyzer;
        let imports = analyzer.extract_imports(source).unwrap();
        assert_eq!(imports, vec!["System"]);
    }

    #[test]
    fn extracts_multiple_usings() {
        let source = br#"using System;
using System.Collections.Generic;
using System.Linq;

public class Foo {}
"#;
        let analyzer = CSharpAnalyzer;
        let imports = analyzer.extract_imports(source).unwrap();
        assert_eq!(
            imports,
            vec!["System", "System.Collections.Generic", "System.Linq"]
        );
    }

    #[test]
    fn extracts_static_using() {
        let source = br#"using static System.Math;

public class Foo {}
"#;
        let analyzer = CSharpAnalyzer;
        let imports = analyzer.extract_imports(source).unwrap();
        assert_eq!(imports, vec!["System.Math"]);
    }

    #[test]
    fn no_usings_returns_empty() {
        let source = br#"public class Foo {
    public void Bar() {}
}
"#;
        let analyzer = CSharpAnalyzer;
        let imports = analyzer.extract_imports(source).unwrap();
        assert!(imports.is_empty());
    }

    #[test]
    fn extracts_methods_from_namespaced_class() {
        let source = br#"namespace MyApp {
    public class Service {
        public void Run() {
            // do something
        }
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "Service.Run");
    }

    #[test]
    fn extracts_method_signature() {
        let source = br#"public class Calculator {
    public int Add(int a, int b) {
        return a + b;
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert!(
            functions[0]
                .signature
                .contains("public int Add(int a, int b)")
        );
    }

    #[test]
    fn extracts_invocation_expressions() {
        let source = br#"class Example {
    void Process() {
        int x = Calculate(input);
        Console.WriteLine(x);
        helper.DoWork();
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let calls = analyzer.extract_calls(source).unwrap();
        let callees: Vec<&str> = calls.iter().map(|c| c.callee.as_str()).collect();
        assert!(callees.contains(&"Calculate"));
        assert!(callees.contains(&"Console.WriteLine"));
        assert!(callees.contains(&"helper.DoWork"));
    }

    // Kill "delete match arm member_access_expression" mutant: assert that a
    // method call expressed as `receiver.Method()` is flagged is_method_call=true
    // with receiver populated. Without the member_access_expression arm, both
    // would fall through to (false, None).
    #[test]
    fn it_reports_method_call_with_receiver_for_member_access() {
        let source = br#"class Example {
    void Run() {
        helper.DoWork();
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let calls = analyzer.extract_calls(source).unwrap();
        let do_work = calls
            .iter()
            .find(|c| c.callee == "helper.DoWork")
            .expect("helper.DoWork call must be present");
        assert!(
            do_work.is_method_call,
            "member_access_expression call must be flagged as method call"
        );
        assert_eq!(do_work.receiver.as_deref(), Some("helper"));
    }

    // Kill extract_calls line-offset mutants (+ with - or *). Calls on lines 3, 4, 5
    // distinguish `row + 1` from `row * 1` and `row - 1`.
    #[test]
    fn it_reports_call_sites_on_correct_lines() {
        let source = b"class Example {
    void Run() {
        Foo();
        Bar();
        Baz();
    }
}
";
        let analyzer = CSharpAnalyzer;
        let calls = analyzer.extract_calls(source).unwrap();
        let foo = calls.iter().find(|c| c.callee == "Foo").expect("Foo call");
        let bar = calls.iter().find(|c| c.callee == "Bar").expect("Bar call");
        let baz = calls.iter().find(|c| c.callee == "Baz").expect("Baz call");
        assert_eq!(foo.line, 3);
        assert_eq!(bar.line, 4);
        assert_eq!(baz.line, 5);
    }

    #[test]
    fn empty_file_returns_no_calls() {
        let source = b"";
        let analyzer = CSharpAnalyzer;
        let calls = analyzer.extract_calls(source).unwrap();
        assert!(calls.is_empty());
    }

    /// Security regression: deeply-nested namespace declarations used to stack-overflow
    /// `visit_node` because it recursed without a depth limit. An attacker committing
    /// a C# file with thousands of nested namespaces could crash git-prism during
    /// `get_change_manifest`. The analyzer must now complete without crashing.
    ///
    /// Depth-guard warning: when `visit_node` hits MAX_RECURSION_DEPTH it must emit
    /// a tracing::warn! so operators can observe truncation in logs/OTLP.
    ///
    /// Uses 300 nesting levels — well past MAX_RECURSION_DEPTH (256) but shallow
    /// enough to run on the default test stack without spawning a new thread.
    /// `#[traced_test]` only captures logs from the current thread, so we must
    /// avoid spawning a new thread here.
    #[test]
    #[traced_test]
    fn it_emits_depth_guard_warning_on_deeply_nested_namespaces() {
        const GENERATED_NESTING_LEVELS: usize = 300;

        let mut source = String::new();
        for i in 0..GENERATED_NESTING_LEVELS {
            source.push_str(&format!("namespace N{i} {{\n"));
        }
        for _ in 0..GENERATED_NESTING_LEVELS {
            source.push_str("}\n");
        }

        let analyzer = CSharpAnalyzer;
        let _ = analyzer.extract_functions(source.as_bytes());
        assert!(logs_contain("depth guard fired"));
        assert!(logs_contain("language=\"csharp\""));
        assert!(logs_contain("operation=\"functions\""));
    }

    /// Triangulation: shallow input must NOT emit the depth-guard warning.
    #[test]
    #[traced_test]
    fn it_does_not_emit_depth_guard_warning_on_shallow_input() {
        let source = br#"namespace MyApp {
    public class Service {
        public void Run() {}
    }
}
"#;
        let analyzer = CSharpAnalyzer;
        let _ = analyzer.extract_functions(source);
        assert!(!logs_contain("depth guard fired"));
    }

    /// Uses nested namespaces (not classes) because `visit_node` stops recursing at
    /// a `class_declaration`, but falls through and recurses into children for any
    /// other node kind — including `namespace_declaration`.
    ///
    /// Runs on a thread with a 2 MB stack: roomy enough for bounded recursion to
    /// `MAX_RECURSION_DEPTH` but far too small for unbounded recursion to 5000 frames.
    #[test]
    fn deeply_nested_namespaces_do_not_stack_overflow() {
        const GENERATED_NESTING_LEVELS: usize = 5000;
        const CONSTRAINED_THREAD_STACK_BYTES: usize = 2 * 1024 * 1024;

        let mut source = String::new();
        for i in 0..GENERATED_NESTING_LEVELS {
            source.push_str(&format!("namespace N{i} {{\n"));
        }
        for _ in 0..GENERATED_NESTING_LEVELS {
            source.push_str("}\n");
        }

        let handle = std::thread::Builder::new()
            .stack_size(CONSTRAINED_THREAD_STACK_BYTES)
            .spawn(move || {
                let analyzer = CSharpAnalyzer;
                analyzer.extract_functions(source.as_bytes())
            })
            .expect("spawn analyzer thread");

        let result = handle
            .join()
            .expect("analyzer thread must not stack-overflow on deeply-nested input");
        let functions = result.expect("analyzer must return Ok on deeply-nested input");
        // Namespaces contain no methods, so no functions should be extracted.
        assert!(functions.is_empty());
    }

    /// Triangulation: moderately-nested namespaces with a class at the innermost level
    /// must still be extracted — the depth guard must not fire on legitimate code.
    ///
    /// C#'s `visit_node` recurses into ALL children of non-class nodes, so each
    /// semantic namespace level consumes several depth increments. This test uses
    /// 10 levels — well within any reasonable limit — to prove the guard does not
    /// erroneously block legitimate real-world code.
    #[test]
    fn it_extracts_class_from_moderately_nested_namespaces() {
        const GENERATED_NESTING_LEVELS: usize = 10;

        let mut source = String::new();
        for i in 0..GENERATED_NESTING_LEVELS {
            source.push_str(&format!("namespace N{i} {{\n"));
        }
        source.push_str("public class Leaf { public void Run() {} }\n");
        for _ in 0..GENERATED_NESTING_LEVELS {
            source.push_str("}\n");
        }

        let analyzer = CSharpAnalyzer;
        let functions = analyzer.extract_functions(source.as_bytes()).unwrap();
        let leaf = functions.iter().find(|f| f.name == "Leaf.Run");
        assert!(
            leaf.is_some(),
            "method inside class nested 10 levels deep must be extracted; got {} functions",
            functions.len()
        );
    }
}