hank 0.4.0

In-memory, multi-tenant code-analysis engine — the structural signal for the Bobbin × Quipu stack
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
//! Export the referential structure as RDF Turtle in the `bobbin:` code ontology.
//!
//! This is the governed projection of Hank's live graph — the substrate under
//! Phase-4 promotion (`--to quipu`). It emits *precise, typed referential
//! structure* (modules, symbols, `definedIn` / `calls` / `imports` edges),
//! **not** the embedding-oriented chunking Bobbin produces. Facts validate
//! against Quipu's
//! `shapes/code-entities.ttl` (`CodeModule`, `CodeSymbol`), whose namespace this
//! mirrors. Document/`Section` nodes and `Section → references → CodeSymbol`
//! edges fold in as the markdown extractor lands (spec §5.10).

use std::collections::{BTreeSet, HashMap};
use std::path::Path;

use crate::docref::{doc_files, extract_doc_sections, Mention};
use crate::errors::Result;
use crate::extract::{extract_structure, source_files};

/// The code-ontology namespace (matches `shapes/code-entities.ttl`).
const ONTO: &str = "http://aegis.gastown.local/ontology/";

/// Emit the referential structure of every source file this build can PARSE
/// under `root` as Turtle, attributing entities to repository `repo`.
///
/// EVERY LANGUAGE, not just Rust (the aegis-81t2 class, found again here): the
/// walk is [`source_files`] — the same drift-proof (path, language) pairing the
/// guard's graph uses — so a Python or TypeScript repo promotes its real
/// structure instead of a green empty write. Name/stem resolution is scoped
/// PER LANGUAGE: a global name map would mint cross-language call edges from
/// simple collisions (`main`, `run`, `init` exist everywhere), and a lying
/// edge in the knowledge graph is worse than a missing one.
pub fn to_turtle(root: &Path, repo: &str) -> Result<String> {
    // Working-tree source stream: the drift-proof (path, language) walk, each
    // file read from disk.
    let sources: Vec<(String, String, &'static str)> = source_files(root)
        .into_iter()
        .filter_map(|(file, language)| {
            let source = std::fs::read_to_string(&file).ok()?;
            Some((rel_path(&file, root), source, language))
        })
        .collect();
    let docs: Vec<(String, String)> = doc_files(root)
        .into_iter()
        .filter_map(|file| {
            let source = std::fs::read_to_string(&file).ok()?;
            Some((rel_path(&file, root), source))
        })
        .collect();
    to_turtle_from(repo, &sources, &docs)
}

/// Promote-time export at a COMMITTED ref (FR-22): read source and doc content
/// from the git tree at `reference`, never the working tree — so uncommitted
/// churn (an in-flight overlay edit, an unsaved buffer) can NEVER be promoted.
/// The IRIs are identical to [`to_turtle`]'s (repo + repo-relative path), so a
/// re-promotion of the same commit supersedes rather than forking the graph.
pub fn to_turtle_at(root: &Path, repo: &str, reference: &str) -> Result<String> {
    let files = crate::git::list_files_at(root, reference);
    let sources: Vec<(String, String, &'static str)> = files
        .iter()
        .filter_map(|path| {
            let ext = path.extension().and_then(std::ffi::OsStr::to_str)?;
            let language = crate::extract::language_for_extension(ext)?;
            let source = crate::git::read_blob_at(root, reference, path)?;
            Some((path.display().to_string(), source, language))
        })
        .collect();
    let docs: Vec<(String, String)> = files
        .iter()
        .filter(|p| p.extension().and_then(std::ffi::OsStr::to_str) == Some("md"))
        .filter_map(|path| {
            let source = crate::git::read_blob_at(root, reference, path)?;
            Some((path.display().to_string(), source))
        })
        .collect();
    to_turtle_from(repo, &sources, &docs)
}

/// Build the Turtle projection from pre-materialized `(rel, source, language)`
/// source triples and `(rel, source)` doc pairs — the single body both
/// [`to_turtle`] (working tree) and [`to_turtle_at`] (committed tree) share, so
/// the two entry points cannot drift in what they emit, only in where the bytes
/// come from.
fn to_turtle_from(
    repo: &str,
    sources: &[(String, String, &'static str)],
    doc_sources: &[(String, String)],
) -> Result<String> {
    let mut modules: Vec<(String, String, &'static str)> = Vec::new();
    let mut symbols: Vec<SymbolTriple> = Vec::new();
    let mut by_name: HashMap<String, Vec<String>> = HashMap::new();
    let mut raw_calls: Vec<(String, String)> = Vec::new();
    // module IRI → its import-name references; and module stem → module IRI(s),
    // for resolving `use`/`mod` references to sibling modules.
    let mut raw_imports: Vec<(String, Vec<String>)> = Vec::new();
    let mut by_stem: HashMap<String, Vec<String>> = HashMap::new();
    // Symbol IRI → its owning module's stem, for narrowing a `qualifier::symbol`
    // doc mention to the right module (FR-33 resolution).
    let mut stem_by_sym: HashMap<String, String> = HashMap::new();

    for (rel, source, language) in sources {
        let (rel, language) = (rel.as_str(), *language);
        let Ok(structure) = extract_structure(source, language) else {
            continue;
        };
        let module = module_iri(repo, rel);
        // Language-scoped keys: `use foo` in Rust must never resolve to a
        // Python module named foo, and a call to `run` must stay within the
        // language whose parser saw it.
        let stem = format!("{language}\u{0}{}", module_stem(rel));
        modules.push((module.clone(), rel.to_string(), language));
        by_stem
            .entry(stem.clone())
            .or_default()
            .push(module.clone());
        for symbol in &structure.symbols {
            let iri = symbol_iri(&module, &symbol.scope, &symbol.name);
            symbols.push(SymbolTriple {
                iri: iri.clone(),
                name: symbol.name.clone(),
                kind: symbol.kind.as_str().to_string(),
                module: module.clone(),
            });
            stem_by_sym.insert(iri.clone(), stem.clone());
            by_name
                .entry(format!("{language}\u{0}{}", symbol.name))
                .or_default()
                .push(iri);
        }
        for call in &structure.calls {
            raw_calls.push((
                format!("{language}\u{0}{}", call.caller),
                format!("{language}\u{0}{}", call.callee),
            ));
        }
        raw_imports.push((
            module.clone(),
            structure
                .import_refs
                .iter()
                .map(|r| format!("{language}\u{0}{r}"))
                .collect(),
        ));
    }

    let mut call_edges: BTreeSet<(String, String)> = BTreeSet::new();
    for (caller, callee) in raw_calls {
        if let (Some(from), Some(to)) = (by_name.get(&caller), by_name.get(&callee)) {
            for a in from {
                for b in to {
                    if a != b {
                        call_edges.insert((a.clone(), b.clone()));
                    }
                }
            }
        }
    }

    // Resolve each import reference to a sibling module by matching its stem.
    let mut import_edges: BTreeSet<(String, String)> = BTreeSet::new();
    for (from, refs) in raw_imports {
        for name in refs {
            if let Some(targets) = by_stem.get(&name) {
                for to in targets {
                    if *to != from {
                        import_edges.insert((from.clone(), to.clone()));
                    }
                }
            }
        }
    }

    // Doc→code references (FR-33): parse each markdown file into headed
    // sections, resolve every symbol mention against the code graph built above,
    // and emit `Section --references--> CodeSymbol`. Only documents/sections that
    // resolve at least one reference are materialized — the graph is referential
    // structure, not every heading in the repo.
    let mut docs: Vec<DocTriple> = Vec::new();
    let mut sections: Vec<SectionTriple> = Vec::new();
    let mut ref_edges: BTreeSet<(String, String)> = BTreeSet::new();
    for (rel, source) in doc_sources {
        let rel = rel.clone();
        let doc = document_iri(repo, &rel);
        let source = source.as_str();
        let mut doc_used = false;
        for section in extract_doc_sections(source) {
            let sec_iri = format!("{doc}#{}", section.slug);
            let mut sec_used = false;
            for mention in &section.mentions {
                for target in resolve(mention, &by_name, &stem_by_sym) {
                    if ref_edges.insert((sec_iri.clone(), target)) {
                        sec_used = true;
                    }
                }
            }
            if sec_used {
                sections.push(SectionTriple {
                    iri: sec_iri,
                    heading: section.heading,
                    depth: section.depth,
                    document: doc.clone(),
                });
                doc_used = true;
            }
        }
        if doc_used {
            docs.push(DocTriple {
                iri: doc,
                path: rel,
            });
        }
    }

    Ok(render(
        repo,
        &modules,
        &symbols,
        &call_edges,
        &import_edges,
        &docs,
        &sections,
        &ref_edges,
    ))
}

/// Resolve a doc [`Mention`] to concrete `CodeSymbol` IRIs, never inventing one:
/// a mention contributes edges only if its name matches an extracted symbol. A
/// `qualifier::` hint narrows the match to symbols in the module of that stem
/// when any qualify; otherwise every same-named symbol is returned (recall over
/// precision, per the "start permissive" guidance).
fn resolve(
    mention: &Mention,
    by_name: &HashMap<String, Vec<String>>,
    stem_by_sym: &HashMap<String, String>,
) -> Vec<String> {
    // Docs are PROSE: a mention has no language, so it searches ACROSS the
    // language-scoped keys (`{language}\0{name}`) that keep call/import
    // resolution honest. A doc citing `reachable` may legitimately mean the
    // Rust one or the Python one — recall over precision, as before.
    let candidates: Vec<String> = by_name
        .iter()
        .filter(|(key, _)| {
            key.rsplit_once('\u{0}')
                .map_or(key.as_str(), |(_, name)| name)
                == mention.symbol
        })
        .flat_map(|(_, iris)| iris.iter().cloned())
        .collect();
    if candidates.is_empty() {
        return Vec::new();
    }
    if let Some(qualifier) = &mention.qualifier {
        let narrowed: Vec<String> = candidates
            .iter()
            .filter(|iri| {
                stem_by_sym.get(*iri).is_some_and(|stem| {
                    stem.rsplit_once('\u{0}')
                        .map_or(stem.as_str(), |(_, bare)| bare)
                        .eq_ignore_ascii_case(qualifier)
                })
            })
            .cloned()
            .collect();
        if !narrowed.is_empty() {
            return narrowed;
        }
    }
    candidates
}

/// A `CodeSymbol` ready to emit.
struct SymbolTriple {
    iri: String,
    name: String,
    kind: String,
    module: String,
}

/// A `Document` ready to emit.
struct DocTriple {
    iri: String,
    path: String,
}

/// A `Section` ready to emit (only materialized when it has ≥1 reference).
struct SectionTriple {
    iri: String,
    heading: String,
    depth: usize,
    document: String,
}

/// Render the collected structure as a Turtle document.
#[allow(clippy::too_many_arguments)]
fn render(
    repo: &str,
    modules: &[(String, String, &'static str)],
    symbols: &[SymbolTriple],
    call_edges: &BTreeSet<(String, String)>,
    import_edges: &BTreeSet<(String, String)>,
    docs: &[DocTriple],
    sections: &[SectionTriple],
    ref_edges: &BTreeSet<(String, String)>,
) -> String {
    let mut out = String::new();
    out.push_str("@prefix bobbin: <http://aegis.gastown.local/ontology/> .\n");
    out.push_str("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n");

    for (iri, rel, language) in modules {
        out.push_str(&format!(
            "<{iri}> a bobbin:CodeModule ;\n    bobbin:filePath \"{}\" ;\n    \
             bobbin:repo \"{}\" ;\n    bobbin:language \"{}\" .\n\n",
            esc(rel),
            esc(repo),
            esc(language),
        ));
    }

    for symbol in symbols {
        out.push_str(&format!(
            "<{}> a bobbin:CodeSymbol ;\n    bobbin:name \"{}\" ;\n    \
             bobbin:symbolKind \"{}\" ;\n    bobbin:definedIn <{}> .\n",
            symbol.iri,
            esc(&symbol.name),
            esc(&symbol.kind),
            symbol.module,
        ));
    }

    if !call_edges.is_empty() {
        out.push('\n');
        for (from, to) in call_edges {
            out.push_str(&format!("<{from}> bobbin:calls <{to}> .\n"));
        }
    }

    if !import_edges.is_empty() {
        out.push('\n');
        for (from, to) in import_edges {
            out.push_str(&format!("<{from}> bobbin:imports <{to}> .\n"));
        }
    }

    // ── Documents / Sections / references (FR-33) ──
    if !docs.is_empty() {
        out.push('\n');
        for doc in docs {
            out.push_str(&format!(
                "<{}> a bobbin:Document ;\n    bobbin:filePath \"{}\" ;\n    \
                 bobbin:repo \"{}\" .\n\n",
                doc.iri,
                esc(&doc.path),
                esc(repo),
            ));
        }
    }

    for section in sections {
        out.push_str(&format!(
            "<{}> a bobbin:Section ;\n    bobbin:heading \"{}\" ;\n    \
             bobbin:headingDepth {} ;\n    bobbin:inDocument <{}> .\n",
            section.iri,
            esc(&section.heading),
            section.depth,
            section.document,
        ));
    }

    if !ref_edges.is_empty() {
        out.push('\n');
        for (from, to) in ref_edges {
            out.push_str(&format!("<{from}> bobbin:references <{to}> .\n"));
        }
    }
    out
}

/// The module-name stem used to resolve `use`/`mod` references to a module: the
/// file stem, except a `mod.rs` takes its parent directory's name (Rust's
/// directory-module convention). Root modules (`lib`/`main`) keep their stem;
/// they are valid import *sources* but rarely import targets.
fn module_stem(rel: &str) -> String {
    let path = Path::new(rel);
    let stem = path
        .file_stem()
        .map_or_else(String::new, |s| s.to_string_lossy().into_owned());
    // Directory-module conventions, one per ecosystem, same shape: the file
    // that IS its directory. Rust's mod.rs, Python's __init__.py, JS/TS's
    // index.*. Each takes the parent directory's name so `import pkg` and
    // `use pkg` resolve to the module that defines pkg.
    if stem == "mod" || stem == "__init__" || stem == "index" {
        return path
            .parent()
            .and_then(Path::file_name)
            .map_or(stem, |n| n.to_string_lossy().into_owned());
    }
    stem
}

/// Mint a `CodeModule` IRI: `{ONTO}code/{repo}/{path}` with `/` percent-encoded
/// in the path segment (mirrors Quipu's `namespace.rs` construction).
fn module_iri(repo: &str, rel: &str) -> String {
    format!("{ONTO}code/{repo}/{}", rel.replace('/', "%2F"))
}

/// Mint a `CodeSymbol` IRI: `{module}::{scope1}::{scope2}::{name}` — the scope
/// chain is what keeps two same-named symbols in one file on distinct IRIs
/// (aegis-1q14: without it, 42 same-kind collisions across bobbin/hank/quipu
/// silently merged, unioning different symbols' call edges). Scope segments are
/// raw source text (impl types can be `Foo<T>` or `dyn Trait`), so IRI-hostile
/// characters are percent-encoded; `::` between segments is the one separator.
fn symbol_iri(module: &str, scope: &[String], name: &str) -> String {
    let mut iri = String::from(module);
    for seg in scope {
        iri.push_str("::");
        iri.push_str(&iri_segment(seg));
    }
    iri.push_str("::");
    iri.push_str(&iri_segment(name));
    iri
}

/// Percent-encode the characters that are illegal or structural in an IRI
/// reference (space and angle brackets from generic types, quotes, and `%`
/// itself first so encoding is injective — two different raw segments can never
/// encode to the same IRI text).
fn iri_segment(raw: &str) -> String {
    let mut out = String::with_capacity(raw.len());
    for c in raw.chars() {
        match c {
            '%' => out.push_str("%25"),
            ' ' => out.push_str("%20"),
            '<' => out.push_str("%3C"),
            '>' => out.push_str("%3E"),
            '"' => out.push_str("%22"),
            '{' => out.push_str("%7B"),
            '}' => out.push_str("%7D"),
            '|' => out.push_str("%7C"),
            '\\' => out.push_str("%5C"),
            '^' => out.push_str("%5E"),
            '`' => out.push_str("%60"),
            '\n' | '\t' => {}
            _ => out.push(c),
        }
    }
    out
}

/// Mint a `Document` IRI: `{ONTO}doc/{repo}/{path}` with `/` percent-encoded in
/// the path segment (mirrors Quipu's `document_iri`; the section anchor is
/// appended as `#{slug}` by the caller).
fn document_iri(repo: &str, rel: &str) -> String {
    format!("{ONTO}doc/{repo}/{}", rel.replace('/', "%2F"))
}

/// Path relative to `root`, falling back to the file name when the root is the
/// file itself.
fn rel_path(file: &Path, root: &Path) -> String {
    match file.strip_prefix(root) {
        Ok(p) if !p.as_os_str().is_empty() => p.display().to_string(),
        _ => file.file_name().map_or_else(
            || file.display().to_string(),
            |n| n.to_string_lossy().into_owned(),
        ),
    }
}

/// Escape a Turtle string literal.
fn esc(value: &str) -> String {
    value.replace('\\', "\\\\").replace('"', "\\\"")
}

#[cfg(test)]
#[path = "export_test.rs"]
mod export_test;