mati 0.1.2

An enforcement layer for codebase knowledge: confirmed gotchas gate what AI agents read and edit at the hook level. Not a passive memory store.
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
//! Go tree-sitter parser — entry points, imports, TODOs.
//!
//! Entry point filtering: captures ALL `function_declaration` and
//! `method_declaration` nodes, then filters in the dispatch loop:
//! - Only exported identifiers (first character is uppercase)
//!
//! Imports: captures both single-path and grouped import declarations.

use std::cell::RefCell;
use std::sync::LazyLock;

use anyhow::Result;

use super::{extract_todo, ImportKind, ImportStatement, StaticFileAnalysis};
use crate::analysis::walker::{Language, WalkedFile};

// ── Static handles ────────────────────────────────────────────────────────────

static GO_LANGUAGE: LazyLock<tree_sitter::Language> =
    LazyLock::new(|| tree_sitter_go::LANGUAGE.into());

const GO_QUERY_SRC: &str = r#"
  (function_declaration name: (identifier) @fn_name)
  (method_declaration name: (field_identifier) @method_name)

  (import_spec path: (interpreted_string_literal) @import)

  (type_declaration (type_spec name: (type_identifier) @type_name))

  (if_statement) @branch
  (for_statement) @branch
  (expression_switch_statement) @branch
  (select_statement) @branch
  (type_switch_statement) @branch

  (comment) @comment
"#;

static GO_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
    tree_sitter::Query::new(&GO_LANGUAGE, GO_QUERY_SRC).expect("parser/go: invalid query")
});

static GO_CAPTURES: LazyLock<GoCaptures> = LazyLock::new(|| GoCaptures::new(&GO_QUERY));

thread_local! {
    static GO_PARSER: RefCell<tree_sitter::Parser> = RefCell::new({
        let mut p = tree_sitter::Parser::new();
        p.set_language(&GO_LANGUAGE).expect("parser/go: grammar load failed");
        p
    });
}

// ── Capture indices ───────────────────────────────────────────────────────────

struct GoCaptures {
    fn_name: u32,
    method_name: u32,
    import: u32,
    type_name: u32,
    branch: u32,
    comment: u32,
}

impl GoCaptures {
    fn new(query: &tree_sitter::Query) -> Self {
        let idx = |name: &str| {
            query
                .capture_index_for_name(name)
                .unwrap_or_else(|| panic!("parser/go: query missing @{name}"))
        };
        Self {
            fn_name: idx("fn_name"),
            method_name: idx("method_name"),
            import: idx("import"),
            type_name: idx("type_name"),
            branch: idx("branch"),
            comment: idx("comment"),
        }
    }
}

// ── Parser ────────────────────────────────────────────────────────────────────

pub(super) fn parse_go(file: &WalkedFile, source: &str) -> Result<StaticFileAnalysis> {
    let tree = GO_PARSER.with(|cell| cell.borrow_mut().parse(source.as_bytes(), None));

    let tree = match tree {
        Some(t) => t,
        None => {
            tracing::warn!("parser/go: tree-sitter failed on {}", file.rel_path);
            return Ok(StaticFileAnalysis::empty(file));
        }
    };

    let query = &*GO_QUERY;
    let ci = &*GO_CAPTURES;
    let src = source.as_bytes();

    let mut out = StaticFileAnalysis {
        path: file.rel_path.clone(),
        language: Language::Go,
        entry_points: Vec::with_capacity(16),
        exported_types: Vec::with_capacity(8),
        imports: Vec::with_capacity(16),
        todos: Vec::new(),
        unsafe_count: 0,
        unwrap_count: 0,
        panic_count: 0,
        branch_count: 0,
        module_doc: None,
        content_hash: None,
        line_count: 0,
    };

    let mut doc_lines: Vec<(usize, String)> = Vec::new();
    let mut cursor = tree_sitter::QueryCursor::new();
    for m in cursor.matches(query, tree.root_node(), src) {
        for capture in m.captures {
            let idx = capture.index;
            let node = capture.node;

            if idx == ci.branch {
                out.branch_count += 1;
            } else if idx == ci.fn_name || idx == ci.method_name {
                if let Ok(name) = node.utf8_text(src) {
                    if is_exported(name) {
                        out.entry_points.push(name.to_owned());
                    }
                }
            } else if idx == ci.type_name {
                if let Ok(name) = node.utf8_text(src) {
                    if is_exported(name) {
                        out.exported_types.push(name.to_owned());
                    }
                }
            } else if idx == ci.import {
                if let Ok(path) = node.utf8_text(src) {
                    // Strip surrounding quotes from the interpreted_string_literal
                    let stripped = path.trim_matches('"');
                    let line = node.start_position().row as u32 + 1;
                    let kind = if is_go_stdlib(stripped) {
                        ImportKind::External
                    } else {
                        ImportKind::Normal
                    };
                    out.imports.push(ImportStatement::new(stripped, kind, line));
                }
            } else if idx == ci.comment {
                if let Ok(text) = node.utf8_text(src) {
                    let row = node.start_position().row;
                    let line = row as u32 + 1;
                    if let Some(todo) = extract_todo(text, line) {
                        out.todos.push(todo);
                    }
                    // Capture file-top `// ...` comments as package doc.
                    // Go convention: package doc is a block of // comments
                    // immediately before the package declaration.
                    if row < 10 {
                        let stripped = text.trim_start_matches("//").trim().to_string();
                        if !stripped.is_empty()
                            && !stripped.starts_with("go:build")
                            && !stripped.starts_with("+build")
                        {
                            doc_lines.push((row, stripped));
                        }
                    }
                }
            }
        }
    }

    if !doc_lines.is_empty() {
        doc_lines.sort_by_key(|(r, _)| *r);
        let start_row = doc_lines[0].0;
        let contiguous: Vec<&str> = doc_lines
            .iter()
            .enumerate()
            .take_while(|(i, (r, _))| *r == start_row + i)
            .map(|(_, (_, text))| text.as_str())
            .collect();
        if !contiguous.is_empty() {
            out.module_doc = Some(super::normalize_doc(&contiguous.join(" ")));
        }
    }

    Ok(out)
}

/// Returns true if the identifier is exported in Go (first char is uppercase).
fn is_exported(name: &str) -> bool {
    name.chars().next().is_some_and(|c| c.is_uppercase())
}

/// Go stdlib packages have no dot in their first path segment. External
/// packages always start with a domain: `github.com`, `gopkg.in`, etc.
fn is_go_stdlib(import_path: &str) -> bool {
    let first_segment = import_path.split('/').next().unwrap_or("");
    !first_segment.contains('.')
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::record::TodoKind;
    use tempfile::TempDir;

    fn make_file(dir: &TempDir, rel: &str, content: &str) -> WalkedFile {
        let abs = dir.path().join(rel);
        if let Some(parent) = abs.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(&abs, content).unwrap();
        WalkedFile {
            abs_path: abs,
            rel_path: rel.to_owned(),
            language: Language::Go,
            size_bytes: content.len() as u64,
            mtime_secs: 0,
        }
    }

    fn parse(dir: &TempDir, source: &str) -> StaticFileAnalysis {
        let f = make_file(dir, "test.go", source);
        parse_go(&f, source).unwrap()
    }

    // ── Entry points: top-level functions ─────────────────────────────────────

    #[test]
    fn exported_func_in_entry_points() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\n\nfunc ExportedFunc() {}\n");
        assert!(a.entry_points.contains(&"ExportedFunc".to_owned()));
    }

    #[test]
    fn unexported_func_excluded() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\n\nfunc unexportedFunc() {}\n");
        assert!(!a.entry_points.contains(&"unexportedFunc".to_owned()));
    }

    #[test]
    fn main_func_excluded() {
        // main is lowercase — unexported by Go convention
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\n\nfunc main() {}\n");
        assert!(!a.entry_points.contains(&"main".to_owned()));
    }

    // ── Entry points: methods ─────────────────────────────────────────────────

    #[test]
    fn exported_method_in_entry_points() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\ntype Foo struct{}\n\nfunc (f Foo) ServeHTTP() {}\n";
        let a = parse(&dir, src);
        assert!(a.entry_points.contains(&"ServeHTTP".to_owned()));
    }

    #[test]
    fn unexported_method_excluded() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\ntype Foo struct{}\n\nfunc (f Foo) helper() {}\n";
        let a = parse(&dir, src);
        assert!(!a.entry_points.contains(&"helper".to_owned()));
    }

    #[test]
    fn pointer_receiver_method_exported() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\ntype Bar struct{}\n\nfunc (b *Bar) Handle() {}\n";
        let a = parse(&dir, src);
        assert!(a.entry_points.contains(&"Handle".to_owned()));
    }

    // ── Imports ───────────────────────────────────────────────────────────────

    #[test]
    fn single_import() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\nimport \"fmt\"\n\nfunc main() {}\n";
        let a = parse(&dir, src);
        assert!(a.imports.iter().any(|i| i.path == "fmt"));
    }

    #[test]
    fn grouped_imports() {
        let dir = TempDir::new().unwrap();
        let src = r#"package main

import (
    "fmt"
    "os"
    "net/http"
)

func main() {}
"#;
        let a = parse(&dir, src);
        assert!(a.imports.iter().any(|i| i.path == "fmt"));
        assert!(a.imports.iter().any(|i| i.path == "os"));
        assert!(a.imports.iter().any(|i| i.path == "net/http"));
    }

    #[test]
    fn import_quotes_stripped() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\nimport \"encoding/json\"\n\nfunc F() {}\n";
        let a = parse(&dir, src);
        // Import should not contain surrounding quotes
        assert!(a.imports.iter().all(|i| !i.path.starts_with('"')));
        assert!(a.imports.iter().any(|i| i.path == "encoding/json"));
    }

    // ── TODOs ─────────────────────────────────────────────────────────────────

    #[test]
    fn todo_in_line_comment() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\n// TODO: fix this\nfunc F() {}\n";
        let a = parse(&dir, src);
        assert_eq!(a.todos.len(), 1);
        assert_eq!(a.todos[0].kind, TodoKind::Todo);
    }

    #[test]
    fn fixme_in_comment() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\n// FIXME: broken\nfunc F() {}\n";
        let a = parse(&dir, src);
        assert_eq!(a.todos[0].kind, TodoKind::Fixme);
    }

    #[test]
    fn hack_in_comment() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\n// HACK: workaround\nfunc F() {}\n";
        let a = parse(&dir, src);
        assert_eq!(a.todos[0].kind, TodoKind::Hack);
    }

    #[test]
    fn plain_comment_not_captured_as_todo() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\n// just a comment\nfunc F() {}\n";
        let a = parse(&dir, src);
        assert!(a.todos.is_empty());
    }

    #[test]
    fn todo_line_number_one_based() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\nfunc F() {}\n// TODO: line 4\n";
        let a = parse(&dir, src);
        assert_eq!(a.todos[0].line, 4);
    }

    // ── Exported types ────────────────────────────────────────────────────────

    #[test]
    fn exported_type_in_exported_types() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\ntype MyHandler struct{}\n";
        let a = parse(&dir, src);
        assert!(a.exported_types.contains(&"MyHandler".to_owned()));
    }

    #[test]
    fn unexported_type_excluded() {
        let dir = TempDir::new().unwrap();
        let src = "package main\n\ntype internalState struct{}\n";
        let a = parse(&dir, src);
        assert!(!a.exported_types.contains(&"internalState".to_owned()));
    }

    // ── Edge cases ────────────────────────────────────────────────────────────

    #[test]
    fn empty_file() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "");
        assert!(a.entry_points.is_empty());
        assert!(a.imports.is_empty());
        assert_eq!(a.branch_count, 0);
    }

    #[test]
    fn path_preserved() {
        let dir = TempDir::new().unwrap();
        let f = make_file(&dir, "cmd/server/main.go", "package main\nfunc main() {}\n");
        let a = parse_go(&f, "package main\nfunc main() {}\n").unwrap();
        assert_eq!(a.path, "cmd/server/main.go");
    }

    #[test]
    fn no_rust_specific_fields_set() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\nfunc F() {}\n");
        assert_eq!(a.unsafe_count, 0);
        assert_eq!(a.unwrap_count, 0);
        assert_eq!(a.panic_count, 0);
    }

    #[test]
    fn branch_if() {
        let dir = TempDir::new().unwrap();
        let src = "package main\nfunc F(x bool) {\n    if x {\n    }\n}\n";
        let a = parse(&dir, src);
        assert_eq!(a.branch_count, 1);
    }

    #[test]
    fn branch_for() {
        let dir = TempDir::new().unwrap();
        let src = "package main\nfunc F() {\n    for i := 0; i < 10; i++ {\n    }\n}\n";
        let a = parse(&dir, src);
        assert_eq!(a.branch_count, 1);
    }

    // ── Stdlib classification tests ──────────────────────────────────────────

    #[test]
    fn fmt_is_external() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\nimport \"fmt\"\n");
        assert_eq!(a.imports[0].kind, ImportKind::External);
    }

    #[test]
    fn net_http_is_external() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\nimport \"net/http\"\n");
        assert_eq!(a.imports[0].kind, ImportKind::External);
    }

    #[test]
    fn encoding_json_is_external() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\nimport \"encoding/json\"\n");
        assert_eq!(a.imports[0].kind, ImportKind::External);
    }

    #[test]
    fn github_com_is_normal() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\nimport \"github.com/acme/foo\"\n");
        assert_eq!(a.imports[0].kind, ImportKind::Normal);
    }

    #[test]
    fn gopkg_in_is_normal() {
        let dir = TempDir::new().unwrap();
        let a = parse(&dir, "package main\nimport \"gopkg.in/yaml.v3\"\n");
        assert_eq!(a.imports[0].kind, ImportKind::Normal);
    }
}