basemind 0.24.0

Full AI context layer over MCP — tree-sitter code-map, document RAG (PDF/Office/HTML/email + OCR + reranker), shared agent memory, on-demand web crawl, git history + blame + per-symbol diff. 300+ languages, 10+ coding-agent harnesses, content-addressed Fjall + LanceDB.
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
//! The canonical graph-view payload and its pluggable text renderers (ADR-0005).
//!
//! One payload — [`GraphView`] — is a superset of the common node-link exchange shape: nodes carry
//! identity, label, location, kind, community + community label (ADR-0004), and centrality; edges
//! carry endpoints, kind, and provenance/confidence/weight (ADR-0002). Every renderer here, and the
//! future UI (ADR-0006), consumes this one payload.
//!
//! The renderers are **pure and deterministic** (they iterate the view in id order, so the same
//! view always yields byte-identical output — snapshot-testable) and **offline** (plain strings, no
//! assets, no network). This ships the machine/text formats plus the self-contained interactive
//! HTML page (see `graph_html` for the [`GraphFormat::Html`] renderer); a static SVG picture is the
//! one format still deferred. The view builder that assembles a `GraphView` from the shared
//! code-graph lives in `helpers_graphview` (it needs the L1 cache).

use std::fmt::Write as _;

use crate::path::RelPath;

/// One node in the canonical payload. `id` is a dense `0..node_count` index; edges reference it.
#[derive(Debug, Clone)]
pub(crate) struct GraphViewNode {
    pub(crate) id: u32,
    pub(crate) name: String,
    pub(crate) kind: String,
    pub(crate) path: Option<RelPath>,
    pub(crate) start_row: Option<u32>,
    pub(crate) start_col: Option<u32>,
    pub(crate) community: u32,
    pub(crate) community_label: String,
    pub(crate) centrality: u64,
}

/// One typed, provenance-tagged edge. `from`/`to` are node `id`s.
#[derive(Debug, Clone)]
pub(crate) struct GraphViewEdge {
    pub(crate) from: u32,
    pub(crate) to: u32,
    pub(crate) kind: String,
    pub(crate) provenance: String,
    pub(crate) confidence: f32,
    pub(crate) weight: u32,
}

/// The canonical graph-view payload consumed by every renderer and the UI.
#[derive(Debug, Default)]
pub(crate) struct GraphView {
    pub(crate) nodes: Vec<GraphViewNode>,
    pub(crate) edges: Vec<GraphViewEdge>,
    /// True when the underlying scan was truncated or the view was capped.
    pub(crate) truncated: bool,
}

/// Which text renderer to run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum GraphFormat {
    /// Node-link JSON — the common interchange shape (`{directed, multigraph, nodes, links}`).
    NodeLink,
    /// Graphviz DOT.
    Dot,
    /// Mermaid `graph` flowchart.
    Mermaid,
    /// GraphML (XML) for graph-database / tooling import.
    GraphMl,
    /// Cypher `CREATE` statements for Neo4j-style import.
    Cypher,
    /// A self-contained, offline interactive HTML page (zero dependencies).
    Html,
    /// A static, self-contained SVG picture (server-side deterministic layout).
    Svg,
}

impl GraphFormat {
    /// Parse the tool `format` param. Accepts a few common spellings.
    pub(crate) fn parse(s: &str) -> Option<GraphFormat> {
        match s {
            "node_link" | "nodelink" | "json" => Some(GraphFormat::NodeLink),
            "dot" | "graphviz" => Some(GraphFormat::Dot),
            "mermaid" => Some(GraphFormat::Mermaid),
            "graphml" => Some(GraphFormat::GraphMl),
            "cypher" => Some(GraphFormat::Cypher),
            "html" | "interactive" => Some(GraphFormat::Html),
            "svg" => Some(GraphFormat::Svg),
            _ => None,
        }
    }

    pub(crate) fn as_str(self) -> &'static str {
        match self {
            GraphFormat::NodeLink => "node_link",
            GraphFormat::Dot => "dot",
            GraphFormat::Mermaid => "mermaid",
            GraphFormat::GraphMl => "graphml",
            GraphFormat::Cypher => "cypher",
            GraphFormat::Html => "html",
            GraphFormat::Svg => "svg",
        }
    }

    /// File extension for this format, used when an export is written to the cache (ADR-0005).
    pub(crate) fn extension(self) -> &'static str {
        match self {
            GraphFormat::NodeLink => "json",
            GraphFormat::Dot => "dot",
            GraphFormat::Mermaid => "mmd",
            GraphFormat::GraphMl => "graphml",
            GraphFormat::Cypher => "cypher",
            GraphFormat::Html => "html",
            GraphFormat::Svg => "svg",
        }
    }
}

/// Render a view to the requested format.
pub(crate) fn render(view: &GraphView, format: GraphFormat) -> String {
    match format {
        GraphFormat::NodeLink => to_node_link(view),
        GraphFormat::Dot => to_dot(view),
        GraphFormat::Mermaid => to_mermaid(view),
        GraphFormat::GraphMl => to_graphml(view),
        GraphFormat::Cypher => to_cypher(view),
        GraphFormat::Html => super::graph_html::to_html(view),
        GraphFormat::Svg => super::graph_svg::to_svg(view),
    }
}

fn path_str(node: &GraphViewNode) -> Option<&str> {
    node.path.as_ref().and_then(|p| p.as_str())
}

/// Node-link JSON — a superset of the NetworkX/D3 shape so it interoperates with existing graph
/// consumers. Built via `serde_json` so escaping is handled correctly.
pub(super) fn to_node_link(view: &GraphView) -> String {
    let nodes: Vec<serde_json::Value> = view
        .nodes
        .iter()
        .map(|n| {
            serde_json::json!({
                "id": n.id,
                "label": n.name,
                "kind": n.kind,
                "path": path_str(n),
                "start_row": n.start_row,
                "start_col": n.start_col,
                "community": n.community,
                "community_label": n.community_label,
                "centrality": n.centrality,
            })
        })
        .collect();
    let links: Vec<serde_json::Value> = view
        .edges
        .iter()
        .map(|e| {
            serde_json::json!({
                "source": e.from,
                "target": e.to,
                "kind": e.kind,
                "provenance": e.provenance,
                "confidence": e.confidence,
                "weight": e.weight,
            })
        })
        .collect();
    let doc = serde_json::json!({
        "directed": true,
        "multigraph": true,
        "truncated": view.truncated,
        "nodes": nodes,
        "links": links,
    });
    serde_json::to_string_pretty(&doc).unwrap_or_else(|e| {
        tracing::warn!(error = %e, "graph-view node-link serialization failed; emitting empty graph");
        "{}".to_string()
    })
}

/// Drop control characters (the Unicode `Cc` category: C0 `<0x20`, DEL `0x7F`, and C1 `0x80..=0x9F`)
/// except those in `keep`. A scanned repo can hold hostile identifiers/filenames; a raw ESC/BEL/CSI
/// reaching `graph_export`'s `content` would execute in any terminal that prints it (CWE-150). Shared
/// by every text renderer so the guarantee is uniform (`to_node_link`/`to_html` are already safe —
/// serde_json escapes all control bytes).
fn strip_control(s: &str, keep: &[char]) -> String {
    s.chars().filter(|c| !c.is_control() || keep.contains(c)).collect()
}

/// Escape a string for a DOT double-quoted id/label: strip control bytes, collapse raw newlines to
/// spaces so a name can't spread the label across lines, then escape backslash and double-quote.
fn dot_escape(s: &str) -> String {
    strip_control(&s.replace(['\n', '\r'], " "), &[])
        .replace('\\', "\\\\")
        .replace('"', "\\\"")
}

fn to_dot(view: &GraphView) -> String {
    let mut out = String::new();
    out.push_str("digraph basemind {\n  rankdir=LR;\n  node [shape=box];\n");
    for n in &view.nodes {
        // Escape name and path independently, then join with the DOT line-break `\n`, so the
        // escaper never turns the separator itself into a literal backslash-n.
        let label = match path_str(n) {
            Some(p) => format!("{}\\n{}", dot_escape(&n.name), dot_escape(p)),
            None => dot_escape(&n.name),
        };
        let _ = writeln!(
            out,
            "  n{} [label=\"{}\", tooltip=\"{}\"];",
            n.id,
            label,
            dot_escape(&n.community_label)
        );
    }
    for e in &view.edges {
        let _ = writeln!(
            out,
            "  n{} -> n{} [label=\"{}\", penwidth={:.2}];",
            e.from,
            e.to,
            dot_escape(&e.kind),
            1.0 + e.confidence,
        );
    }
    out.push_str("}\n");
    out
}

/// Escape a label for a Mermaid node text wrapped in double quotes: strip control bytes, collapse
/// newlines to spaces, and neutralize the closing quote.
fn mermaid_escape(s: &str) -> String {
    strip_control(&s.replace(['\n', '\r'], " "), &[]).replace('"', "&quot;")
}

fn to_mermaid(view: &GraphView) -> String {
    let mut out = String::from("graph LR\n");
    for n in &view.nodes {
        let _ = writeln!(out, "  n{}[\"{}\"]", n.id, mermaid_escape(&n.name));
    }
    for e in &view.edges {
        let _ = writeln!(out, "  n{} -->|{}| n{}", e.from, mermaid_escape(&e.kind), e.to);
    }
    out
}

/// Escape text for an XML attribute/body. Drops control characters (C0 below U+0020 that XML 1.0
/// forbids, plus DEL/C1) except tab/newline/carriage-return — they have no legal escape and would
/// make the document non-well-formed — then escapes the five XML metacharacters.
pub(super) fn xml_escape(s: &str) -> String {
    strip_control(s, &['\t', '\n', '\r'])
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

fn to_graphml(view: &GraphView) -> String {
    let mut out = String::new();
    out.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    out.push_str("<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n");
    for (id, ty, name) in [
        ("d_label", "string", "label"),
        ("d_kind", "string", "kind"),
        ("d_path", "string", "path"),
        ("d_community", "long", "community"),
        ("d_community_label", "string", "community_label"),
        ("d_centrality", "long", "centrality"),
    ] {
        let _ = writeln!(
            out,
            "  <key id=\"{id}\" for=\"node\" attr.name=\"{name}\" attr.type=\"{ty}\"/>"
        );
    }
    for (id, ty, name) in [
        ("e_kind", "string", "kind"),
        ("e_provenance", "string", "provenance"),
        ("e_confidence", "double", "confidence"),
        ("e_weight", "long", "weight"),
    ] {
        let _ = writeln!(
            out,
            "  <key id=\"{id}\" for=\"edge\" attr.name=\"{name}\" attr.type=\"{ty}\"/>"
        );
    }
    out.push_str("  <graph edgedefault=\"directed\">\n");
    for n in &view.nodes {
        let _ = writeln!(out, "    <node id=\"n{}\">", n.id);
        let _ = writeln!(out, "      <data key=\"d_label\">{}</data>", xml_escape(&n.name));
        let _ = writeln!(out, "      <data key=\"d_kind\">{}</data>", xml_escape(&n.kind));
        if let Some(p) = path_str(n) {
            let _ = writeln!(out, "      <data key=\"d_path\">{}</data>", xml_escape(p));
        }
        let _ = writeln!(out, "      <data key=\"d_community\">{}</data>", n.community);
        let _ = writeln!(
            out,
            "      <data key=\"d_community_label\">{}</data>",
            xml_escape(&n.community_label)
        );
        let _ = writeln!(out, "      <data key=\"d_centrality\">{}</data>", n.centrality);
        out.push_str("    </node>\n");
    }
    for (i, e) in view.edges.iter().enumerate() {
        let _ = writeln!(
            out,
            "    <edge id=\"e{i}\" source=\"n{}\" target=\"n{}\">",
            e.from, e.to
        );
        let _ = writeln!(out, "      <data key=\"e_kind\">{}</data>", xml_escape(&e.kind));
        let _ = writeln!(
            out,
            "      <data key=\"e_provenance\">{}</data>",
            xml_escape(&e.provenance)
        );
        let _ = writeln!(out, "      <data key=\"e_confidence\">{:.3}</data>", e.confidence);
        let _ = writeln!(out, "      <data key=\"e_weight\">{}</data>", e.weight);
        out.push_str("    </edge>\n");
    }
    out.push_str("  </graph>\n</graphml>\n");
    out
}

/// Escape a string for a single-quoted Cypher literal: strip control bytes, then escape backslash
/// and single-quote.
fn cypher_escape(s: &str) -> String {
    strip_control(s, &[]).replace('\\', "\\\\").replace('\'', "\\'")
}

/// Map an edge kind to a Cypher relationship type (uppercase, the community convention). The rel
/// type is emitted unquoted, so restrict it to a safe `[A-Z0-9_]` token defensively — `kind` is a
/// fixed enum (calls/imports/inherits/contains) today, but this keeps a future variant from ever
/// becoming an injection point.
fn cypher_rel(kind: &str) -> String {
    let rel: String = kind
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || *c == '_')
        .collect::<String>()
        .to_ascii_uppercase();
    if rel.is_empty() { "REL".to_string() } else { rel }
}

fn to_cypher(view: &GraphView) -> String {
    let mut out = String::new();
    for n in &view.nodes {
        let path = path_str(n).unwrap_or("");
        let _ = writeln!(
            out,
            "CREATE (n{}:Symbol {{name:'{}', kind:'{}', path:'{}', community:{}, community_label:'{}', centrality:{}}})",
            n.id,
            cypher_escape(&n.name),
            cypher_escape(&n.kind),
            cypher_escape(path),
            n.community,
            cypher_escape(&n.community_label),
            n.centrality
        );
    }
    for e in &view.edges {
        let _ = writeln!(
            out,
            "CREATE (n{})-[:{} {{provenance:'{}', confidence:{:.3}, weight:{}}}]->(n{})",
            e.from,
            cypher_rel(&e.kind),
            cypher_escape(&e.provenance),
            e.confidence,
            e.weight,
            e.to
        );
    }
    out
}

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

    fn sample() -> GraphView {
        GraphView {
            nodes: vec![
                GraphViewNode {
                    id: 0,
                    name: "wr<a>&p".into(), // angle brackets + ampersand exercise XML escaping
                    kind: "function".into(),
                    path: Some(RelPath::from("src/core.rs")),
                    start_row: Some(1),
                    start_col: Some(0),
                    community: 0,
                    community_label: "src · engine".into(),
                    centrality: 20,
                },
                GraphViewNode {
                    id: 1,
                    name: "he\"l'p\\er".into(), // quote + single-quote + backslash exercise DOT/Cypher
                    kind: "function".into(),
                    path: Some(RelPath::from("src/core.rs")),
                    start_row: Some(2),
                    start_col: Some(0),
                    community: 0,
                    community_label: "src · engine".into(),
                    centrality: 10,
                },
            ],
            edges: vec![GraphViewEdge {
                from: 1,
                to: 0,
                kind: "calls".into(),
                provenance: "extracted".into(),
                confidence: 1.0,
                weight: 1,
            }],
            truncated: false,
        }
    }

    #[test]
    fn format_parse_accepts_synonyms() {
        assert_eq!(GraphFormat::parse("json"), Some(GraphFormat::NodeLink));
        assert_eq!(GraphFormat::parse("graphviz"), Some(GraphFormat::Dot));
        assert_eq!(GraphFormat::parse("mermaid"), Some(GraphFormat::Mermaid));
        assert_eq!(GraphFormat::parse("graphml"), Some(GraphFormat::GraphMl));
        assert_eq!(GraphFormat::parse("cypher"), Some(GraphFormat::Cypher));
        assert_eq!(GraphFormat::parse("html"), Some(GraphFormat::Html));
        assert_eq!(GraphFormat::parse("interactive"), Some(GraphFormat::Html));
        assert_eq!(GraphFormat::parse("svg"), Some(GraphFormat::Svg));
        assert_eq!(GraphFormat::parse("bogus"), None);
    }

    #[test]
    fn node_link_is_valid_json_with_nodes_and_links() {
        let out = render(&sample(), GraphFormat::NodeLink);
        let v: serde_json::Value = serde_json::from_str(&out).expect("valid json");
        assert_eq!(v["directed"], serde_json::json!(true));
        assert_eq!(v["nodes"].as_array().unwrap().len(), 2);
        assert_eq!(v["links"].as_array().unwrap().len(), 1);
        assert_eq!(v["links"][0]["source"], serde_json::json!(1));
        assert_eq!(v["links"][0]["target"], serde_json::json!(0));
        // Hostile characters round-trip through JSON exactly, without corrupting the document.
        assert_eq!(v["nodes"][0]["label"], serde_json::json!("wr<a>&p"));
        assert_eq!(v["nodes"][1]["label"], serde_json::json!("he\"l'p\\er"));
    }

    #[test]
    fn dot_escapes_quotes_and_wires_edges() {
        let out = render(&sample(), GraphFormat::Dot);
        assert!(out.starts_with("digraph basemind {"));
        assert!(out.contains("n1 -> n0"));
        // Backslash escaped to `\\`, then the double-quote to `\"` — `he"l'p\er` → `he\"l'p\\er`.
        assert!(out.contains("he\\\"l'p\\\\er"), "dot label escaping: {out}");
    }

    #[test]
    fn mermaid_escapes_quotes() {
        let out = render(&sample(), GraphFormat::Mermaid);
        assert!(out.starts_with("graph LR"));
        assert!(out.contains("n1 -->|calls| n0"));
        assert!(out.contains("he&quot;l'p\\er"), "mermaid quote escaping: {out}");
    }

    #[test]
    fn graphml_is_escaped_xml() {
        let out = render(&sample(), GraphFormat::GraphMl);
        assert!(out.contains("<graphml"));
        // Angle brackets and ampersand must all be entity-escaped in the element body.
        assert!(out.contains("wr&lt;a&gt;&amp;p"), "xml body escaping: {out}");
        assert!(out.contains("he&quot;l&apos;p\\er"), "xml quote/apos escaping: {out}");
        assert!(out.contains("source=\"n1\" target=\"n0\""));
    }

    #[test]
    fn cypher_escapes_and_maps_rel_type() {
        let out = render(&sample(), GraphFormat::Cypher);
        assert!(out.contains("CREATE (n0:Symbol"));
        assert!(out.contains("-[:CALLS "), "kind maps to uppercase rel type: {out}");
        // Backslash → `\\` and single-quote → `\'` keep the single-quoted literal closed:
        // `he"l'p\er` → `he"l\'p\\er`.
        assert!(out.contains("he\"l\\'p\\\\er"), "cypher literal escaping: {out}");
    }

    #[test]
    fn cypher_rel_rejects_unsafe_kind() {
        // Defense in depth: a non-enum kind is sanitized to a safe token, never injected raw.
        assert_eq!(cypher_rel("calls"), "CALLS");
        assert_eq!(cypher_rel("x]->(m) DETACH DELETE n //"), "XMDETACHDELETEN");
        assert_eq!(cypher_rel("!!!"), "REL");
    }

    #[test]
    fn xml_escape_drops_forbidden_control_chars() {
        // A raw C0 control char (U+0001) has no legal XML escape; it must be dropped, not emitted.
        let out = xml_escape("a\u{01}b");
        assert_eq!(out, "ab");
        // Tab/newline/return are legal and preserved.
        assert_eq!(xml_escape("a\tb\nc"), "a\tb\nc");
    }

    #[test]
    fn renderers_strip_terminal_escape_sequences() {
        // A hostile identifier carrying ESC (OSC/CSI introducer) + BEL must never reach the
        // terminal-facing output verbatim (CWE-150) — dot/mermaid/cypher/graphml all strip controls.
        let hostile = "ev\u{1b}]0;pwned\u{07}il\u{1b}[2J";
        for escaped in [
            dot_escape(hostile),
            mermaid_escape(hostile),
            cypher_escape(hostile),
            xml_escape(hostile),
        ] {
            assert!(
                !escaped.contains('\u{1b}') && !escaped.contains('\u{07}'),
                "control byte survived escaping: {escaped:?}"
            );
        }
        // End-to-end through a rendered document, with the hostile name on a real node.
        let mut view = sample();
        view.nodes[0].name = hostile.into();
        for fmt in [
            GraphFormat::Dot,
            GraphFormat::Mermaid,
            GraphFormat::Cypher,
            GraphFormat::GraphMl,
            GraphFormat::Svg,
        ] {
            let out = render(&view, fmt);
            assert!(
                !out.contains('\u{1b}') && !out.contains('\u{07}'),
                "{fmt:?} leaked a raw control byte"
            );
        }
    }

    #[test]
    fn renderers_are_deterministic() {
        let v = sample();
        for f in [
            GraphFormat::NodeLink,
            GraphFormat::Dot,
            GraphFormat::Mermaid,
            GraphFormat::GraphMl,
            GraphFormat::Cypher,
            GraphFormat::Html,
            GraphFormat::Svg,
        ] {
            assert_eq!(render(&v, f), render(&v, f), "{f:?}");
        }
    }
}