aingle_cortex 0.7.6

Córtex API - REST/GraphQL/SPARQL interface for AIngle semantic graphs
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
// Copyright 2019-2026 Apilium Technologies OÜ. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR Commercial

//! Local graph neighborhood for a single note: typed edges (link / semantic / tag)
//! up to depth 2 for the Akashi per-note graph panel (VC-2).

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};

use crate::service::context::{note_context_cached, NEIGHBOR_FLOOR};
use crate::service::triple_util::{basename, obj_string, resolve_link_target, strip_brackets};
use crate::service::vault_map::is_maps_path;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// The typed local neighborhood graph around a center note.
#[derive(Debug, Clone, serde::Serialize, Default)]
pub struct LocalGraph {
    /// The center note path.
    pub center: String,
    /// All nodes in this neighborhood (center + neighbors).
    pub nodes: Vec<GNode>,
    /// All typed edges in this neighborhood.
    pub edges: Vec<TypedEdge>,
    /// `true` when the embedder has enough dimensions for semantic edges.
    pub semantic_ready: bool,
}

/// A node in the local neighborhood graph.
#[derive(Debug, Clone, serde::Serialize)]
pub struct GNode {
    /// Full relative path (canonical identity).
    pub id: String,
    /// Human-readable label (basename without extension).
    pub label: String,
    /// `"center"` for the focal note; `"note"` for all others.
    pub kind: String,
    /// Semantic cluster id. Always `-1` here (clustering is global / expensive).
    pub cluster: i64,
    /// Number of edges in THIS graph touching this node.
    pub degree: usize,
    /// Creation date sourced from the note's `created` frontmatter scalar (e.g. `"2025-09-14"`).
    /// `None` when the note has no `created` triple.
    pub timestamp: Option<String>,
}

/// A typed, optionally weighted edge in the local neighborhood graph.
#[derive(Debug, Clone, serde::Serialize)]
pub struct TypedEdge {
    pub source: String,
    pub target: String,
    /// `"link"` | `"semantic"` | `"tag"`
    pub kind: String,
    /// Cosine similarity score — present only for semantic edges.
    pub score: Option<f32>,
    /// For tag edges: the shared tag name.
    pub label: Option<String>,
    /// Signed DAG action hash for semantic edges (🔒). `None` if unavailable.
    pub provenance_anchor: Option<String>,
}

// ---------------------------------------------------------------------------
// Private constants
// ---------------------------------------------------------------------------

const NODE_CAP: usize = 80;
const SEM_PER_NODE: usize = 5;
const MAX_DEPTH: usize = 2;
/// Max tag-edges added per (node, tag) pair — prevents explosion on popular tags.
const TAG_FANOUT_CAP: usize = 6;
/// Maximum frontier size before the per-node semantic pass at each BFS level.
/// Caps the depth-2 semantic N+1: a hub with many link-neighbors would otherwise
/// trigger one `note_context_cached` call per frontier node. Sorting the frontier
/// first ensures deterministic behavior when truncating.
const SEM_FRONTIER_CAP: usize = 16;

// ---------------------------------------------------------------------------
// Core function
// ---------------------------------------------------------------------------

/// Build the typed local neighborhood graph for `note` at BFS depth `depth`.
pub async fn local_graph(state: &crate::state::AppState, note: &str, depth: usize) -> LocalGraph {
    use aingle_graph::{Predicate, TriplePattern};

    let depth = depth.clamp(1, MAX_DEPTH);
    let semantic_grade = state.embedder.dimensions() >= 128;

    // -----------------------------------------------------------------------
    // 1. Load structural data from the graph once.
    // -----------------------------------------------------------------------
    // notes: all ingested note paths
    // links_raw: (subject, object-string) for every links_to triple
    // tagged_raw: (subject, tag) for every tagged triple
    type PairVec = Vec<(String, String)>;
    let (notes, links_raw, tagged_raw, created_map): (
        Vec<String>,
        PairVec,
        PairVec,
        BTreeMap<String, String>,
    ) = {
        let g = state.graph.read().await;
        let collect = |pred: &str| -> PairVec {
            g.find(TriplePattern::any().with_predicate(Predicate::named(pred)))
                .unwrap_or_default()
                .into_iter()
                .filter_map(|t| {
                    obj_string(&t).map(|o| (strip_brackets(&t.subject.to_string()).to_string(), o))
                })
                .collect()
        };
        let mut ns: Vec<String> = collect(crate::service::ingest::PRED_SOURCE_HASH)
            .into_iter()
            .map(|(s, _)| s)
            .collect();
        ns.sort();
        ns.dedup();
        let lnks = collect("links_to");
        let tags = collect("tagged");
        // Build created-date map: note_path → date. "date" as fallback, "created" takes precedence.
        let mut cmap: BTreeMap<String, String> = collect("date").into_iter().collect();
        for (k, v) in collect("created") {
            cmap.insert(k, v);
        }
        (ns, lnks, tags, cmap)
    };

    let note_set: BTreeSet<&str> = notes.iter().map(|s| s.as_str()).collect();

    // Basename index for wikilink resolution.
    let mut by_base: BTreeMap<String, String> = BTreeMap::new();
    for n in &notes {
        by_base.entry(basename(n)).or_insert_with(|| n.clone());
    }

    let resolve =
        |target: &str| -> Option<String> { resolve_link_target(target, &note_set, &by_base) };

    // Resolved outgoing links: (src, dst) — both are full paths, neither a maps path.
    let links: Vec<(String, String)> = links_raw
        .iter()
        .filter_map(|(src, tgt)| resolve(tgt).map(|dst| (src.clone(), dst)))
        .filter(|(src, dst)| src != dst)
        .filter(|(src, _)| note_set.contains(src.as_str()) && !is_maps_path(src))
        .filter(|(_, dst)| note_set.contains(dst.as_str()) && !is_maps_path(dst))
        .collect();

    // Pre-index links for O(1) per-node lookup in the BFS loop — avoids
    // re-scanning the full `links` vec twice per node.
    let mut by_src: BTreeMap<String, Vec<String>> = BTreeMap::new();
    let mut by_dst: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for (src, dst) in &links {
        by_src.entry(src.clone()).or_default().push(dst.clone());
        by_dst.entry(dst.clone()).or_default().push(src.clone());
    }

    // tag_of_note: note → set<tag>
    // notes_of_tag: tag → vec<note> (sorted, deduped)
    let mut tag_of_note: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    let mut notes_of_tag: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for (note_path, tag) in &tagged_raw {
        if note_set.contains(note_path.as_str()) && !is_maps_path(note_path) {
            tag_of_note
                .entry(note_path.clone())
                .or_default()
                .insert(tag.clone());
            notes_of_tag
                .entry(tag.clone())
                .or_default()
                .push(note_path.clone());
        }
    }
    for v in notes_of_tag.values_mut() {
        v.sort();
        v.dedup();
    }

    // -----------------------------------------------------------------------
    // 2. BFS to collect edges.
    // -----------------------------------------------------------------------
    let mut edges: Vec<TypedEdge> = Vec::new();
    let mut visited: HashSet<String> = HashSet::new();
    visited.insert(note.to_string());

    let mut frontier: VecDeque<String> = VecDeque::new();
    frontier.push_back(note.to_string());

    let mut semantic_ready = semantic_grade;

    for _level in 0..depth {
        let mut next_frontier: Vec<String> = Vec::new();
        while let Some(n) = frontier.pop_front() {
            if is_maps_path(&n) {
                continue;
            }

            // --- link edges (outgoing from n) ---
            if let Some(dsts) = by_src.get(&n) {
                for dst in dsts {
                    edges.push(TypedEdge {
                        source: n.clone(),
                        target: dst.clone(),
                        kind: "link".to_string(),
                        score: None,
                        label: None,
                        provenance_anchor: None,
                    });
                    if !visited.contains(dst) {
                        visited.insert(dst.clone());
                        next_frontier.push(dst.clone());
                    }
                }
            }
            // --- link edges (incoming to n) ---
            if let Some(srcs) = by_dst.get(&n) {
                for src in srcs {
                    edges.push(TypedEdge {
                        source: src.clone(),
                        target: n.clone(),
                        kind: "link".to_string(),
                        score: None,
                        label: None,
                        provenance_anchor: None,
                    });
                    if !visited.contains(src) {
                        visited.insert(src.clone());
                        next_frontier.push(src.clone());
                    }
                }
            }

            // --- semantic edges ---
            if semantic_grade {
                let ctx = note_context_cached(state, &n, SEM_PER_NODE).await;
                if !ctx.semantic_ready {
                    semantic_ready = false;
                } else {
                    for nb in ctx.neighbors {
                        if nb.score < NEIGHBOR_FLOOR {
                            continue;
                        }
                        if is_maps_path(&nb.path) {
                            continue;
                        }
                        edges.push(TypedEdge {
                            source: n.clone(),
                            target: nb.path.clone(),
                            kind: "semantic".to_string(),
                            score: Some(nb.score),
                            label: None,
                            provenance_anchor: nb.provenance_anchor,
                        });
                        if !visited.contains(&nb.path) {
                            visited.insert(nb.path.clone());
                            next_frontier.push(nb.path.clone());
                        }
                    }
                }
            }

            // --- tag edges ---
            if let Some(tags) = tag_of_note.get(&n) {
                for tag in tags {
                    if let Some(peers) = notes_of_tag.get(tag) {
                        let mut added = 0usize;
                        for peer in peers {
                            if peer == &n || is_maps_path(peer) {
                                continue;
                            }
                            if added >= TAG_FANOUT_CAP {
                                break;
                            }
                            edges.push(TypedEdge {
                                source: n.clone(),
                                target: peer.clone(),
                                kind: "tag".to_string(),
                                score: None,
                                label: Some(tag.clone()),
                                provenance_anchor: None,
                            });
                            if !visited.contains(peer) {
                                visited.insert(peer.clone());
                                next_frontier.push(peer.clone());
                            }
                            added += 1;
                        }
                    }
                }
            }
        }

        // Cap the next frontier before promoting to bound semantic cost at the
        // next level (≤ SEM_FRONTIER_CAP × note_context_cached calls).
        // Depth-1 behavior is identical: next_frontier is never used again.
        next_frontier.sort();
        next_frontier.truncate(SEM_FRONTIER_CAP);
        for n in next_frontier {
            frontier.push_back(n);
        }
    }

    // -----------------------------------------------------------------------
    // 3. Deduplicate edges.
    // -----------------------------------------------------------------------
    // Links are directional — dedupe by (source, target, kind).
    // Semantic/tag are symmetric — dedupe order-insensitively by (min,max,kind).
    let mut seen_link: HashSet<(String, String)> = HashSet::new();
    let mut seen_sym: HashSet<(String, String, String, String)> = HashSet::new();
    let mut deduped: Vec<TypedEdge> = Vec::new();

    for e in edges {
        // Remove self-loops.
        if e.source == e.target {
            continue;
        }
        match e.kind.as_str() {
            "link" => {
                let key = (e.source.clone(), e.target.clone());
                if seen_link.insert(key) {
                    deduped.push(e);
                }
            }
            _ => {
                // symmetric kinds: (tag, semantic)
                let (lo, hi) = if e.source <= e.target {
                    (e.source.clone(), e.target.clone())
                } else {
                    (e.target.clone(), e.source.clone())
                };
                // Include the tag label so a pair sharing two distinct tags
                // yields two edges. Semantic label is always None → "" → no clash.
                let tag_label = if e.kind == "tag" {
                    e.label.clone().unwrap_or_default()
                } else {
                    String::new()
                };
                let key = (lo, hi, e.kind.clone(), tag_label);
                if seen_sym.insert(key) {
                    deduped.push(e);
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // 4. Collect all node ids referenced by edges, plus the center.
    // -----------------------------------------------------------------------
    let mut all_node_ids: HashSet<String> = HashSet::new();
    all_node_ids.insert(note.to_string());
    for e in &deduped {
        all_node_ids.insert(e.source.clone());
        all_node_ids.insert(e.target.clone());
    }

    // -----------------------------------------------------------------------
    // 5. Cap: keep center + highest-degree nodes; drop edges to removed nodes.
    // -----------------------------------------------------------------------
    let mut degree_map: HashMap<String, usize> = HashMap::new();
    for id in &all_node_ids {
        degree_map.insert(id.clone(), 0);
    }
    for e in &deduped {
        *degree_map.entry(e.source.clone()).or_default() += 1;
        *degree_map.entry(e.target.clone()).or_default() += 1;
    }

    let kept_ids: HashSet<String> = if all_node_ids.len() > NODE_CAP {
        // Always keep center; fill remaining slots by degree descending.
        let mut by_degree: Vec<(String, usize)> = degree_map
            .iter()
            .filter(|(id, _)| id.as_str() != note)
            .map(|(id, &d)| (id.clone(), d))
            .collect();
        by_degree.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
        let mut kept: HashSet<String> = HashSet::new();
        kept.insert(note.to_string());
        for (id, _) in by_degree.into_iter().take(NODE_CAP - 1) {
            kept.insert(id);
        }
        kept
    } else {
        all_node_ids.clone()
    };

    // Drop edges that reference removed nodes.
    let mut final_edges: Vec<TypedEdge> = deduped
        .into_iter()
        .filter(|e| kept_ids.contains(&e.source) && kept_ids.contains(&e.target))
        .collect();
    // Sort for stable cross-run output.
    final_edges.sort_by(|a, b| {
        a.source
            .cmp(&b.source)
            .then(a.target.cmp(&b.target))
            .then(a.kind.cmp(&b.kind))
    });

    // Recompute degree map for final kept set.
    let mut final_degree: HashMap<String, usize> = HashMap::new();
    for id in &kept_ids {
        final_degree.insert(id.clone(), 0);
    }
    for e in &final_edges {
        *final_degree.entry(e.source.clone()).or_default() += 1;
        *final_degree.entry(e.target.clone()).or_default() += 1;
    }

    // Build nodes vector.
    let mut nodes: Vec<GNode> = kept_ids
        .iter()
        .map(|id| {
            let kind = if id == note { "center" } else { "note" }.to_string();
            let degree = *final_degree.get(id).unwrap_or(&0);
            GNode {
                label: basename(id),
                id: id.clone(),
                kind,
                cluster: -1,
                degree,
                timestamp: created_map.get(id).cloned(),
            }
        })
        .collect();
    nodes.sort_by(|a, b| a.id.cmp(&b.id));

    LocalGraph {
        center: note.to_string(),
        nodes,
        edges: final_edges,
        semantic_ready,
    }
}

// ---------------------------------------------------------------------------
// Cached variant
// ---------------------------------------------------------------------------

/// Like [`local_graph`] but memoised on `(triple_count, total_memory_bytes)`.
///
/// Map key is `(note_path, depth)`. Cap: 256 entries (clear-on-exceed).
pub async fn local_graph_cached(
    state: &crate::state::AppState,
    note: &str,
    depth: usize,
) -> LocalGraph {
    let tc = { state.graph.read().await.stats().triple_count };
    let mem_bytes = { state.memory.read().await.stats().total_memory_bytes };
    let version_key = (tc, mem_bytes);
    let map_key = (note.to_string(), depth);

    // Check cache — release lock before any await.
    {
        let cache = state
            .local_graph_cache
            .lock()
            .expect("local_graph cache poisoned");
        if let Some((cached_key, graph)) = cache.get(&map_key) {
            if *cached_key == version_key {
                return graph.clone();
            }
        }
    }

    // Compute without holding the mutex.
    let result = local_graph(state, note, depth).await;

    // Store result.
    {
        let mut cache = state
            .local_graph_cache
            .lock()
            .expect("local_graph cache poisoned");
        if cache.len() > 256 {
            cache.clear();
        }
        cache.insert(map_key, (version_key, result.clone()));
    }

    result
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use aingle_graph::{NodeId, Predicate, Triple, Value};
    use ineru::{Embedder, Embedding, MemoryEntry};

    use crate::state::AppState;

    // -----------------------------------------------------------------------
    // Stub embedder: 128-dim (same as context.rs tests).
    // text with "alpha" → e0=[1,0,…], "zzz" → e1=[0,1,…], else → e2=[0,0,1,…]
    // Cosine(alpha,alpha) = 1.0 ≥ NEIGHBOR_FLOOR(0.88) → passes.
    // -----------------------------------------------------------------------
    struct StubEmbedder;

    impl Embedder for StubEmbedder {
        fn embed_passage(&self, text: &str) -> Embedding {
            let mut v = vec![0.0_f32; 128];
            if text.contains("alpha") {
                v[0] = 1.0;
            } else if text.contains("zzz") {
                v[1] = 1.0;
            } else {
                v[2] = 1.0;
            }
            Embedding::new(v)
        }

        fn embed_query(&self, text: &str) -> Embedding {
            self.embed_passage(text)
        }

        fn dimensions(&self) -> usize {
            128
        }

        fn relevance_thresholds(&self) -> (f32, f32) {
            (0.5, 0.1)
        }
    }

    fn stub_state() -> AppState {
        AppState::with_db_path_and_embedder(":memory:", None, Arc::new(StubEmbedder)).unwrap()
    }

    async fn insert_triple_node(state: &AppState, s: &str, p: &str, o_node: &str) {
        let g = state.graph.write().await;
        g.insert(Triple::new(
            NodeId::named(s),
            Predicate::named(p),
            Value::Node(NodeId::named(o_node)),
        ))
        .unwrap();
    }

    async fn insert_triple_lit(state: &AppState, s: &str, p: &str, o: &str) {
        let g = state.graph.write().await;
        g.insert(Triple::new(
            NodeId::named(s),
            Predicate::named(p),
            Value::literal(o),
        ))
        .unwrap();
    }

    async fn register_note(state: &AppState, path: &str) {
        insert_triple_lit(state, path, crate::service::ingest::PRED_SOURCE_HASH, "h").await;
    }

    async fn insert_chunk(state: &AppState, source_path: &str, text: &str, emb: Vec<f32>) {
        let mut mem = state.memory.write().await;
        let mut e = MemoryEntry::new(
            crate::service::ingest::CHUNK_ENTRY_TYPE,
            serde_json::json!({ "text": text, "source_path": source_path }),
        );
        e.embedding = Some(Embedding::new(emb));
        mem.remember(e).unwrap();
    }

    fn e0() -> Vec<f32> {
        let mut v = vec![0.0_f32; 128];
        v[0] = 1.0;
        v
    }

    // -----------------------------------------------------------------------
    // 1. link_edge_from_wikilink
    // -----------------------------------------------------------------------
    /// A `links_to` triple (Value::Node) from a.md to b yields a "link" edge a→b,
    /// and center is "a.md".
    #[tokio::test]
    async fn link_edge_from_wikilink() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        // wikilink stored as Value::Node (basename without extension)
        insert_triple_node(&state, "a.md", "links_to", "b").await;

        let g = super::local_graph(&state, "a.md", 1).await;
        assert_eq!(g.center, "a.md");
        let link = g.edges.iter().find(|e| e.kind == "link");
        assert!(link.is_some(), "must have a link edge: {:?}", g.edges);
        let link = link.unwrap();
        assert_eq!(link.source, "a.md");
        assert_eq!(link.target, "b.md");
    }

    // -----------------------------------------------------------------------
    // 2. semantic_edge_from_neighbor
    // -----------------------------------------------------------------------
    /// With the stub 128-d embedder and alpha-topic chunks, a.md and b.md both
    /// project onto e0. note_context yields them as mutual neighbors with score
    /// 1.0 ≥ NEIGHBOR_FLOOR → a "semantic" edge with score.is_some().
    #[tokio::test]
    async fn semantic_edge_from_neighbor() {
        let state = stub_state();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        insert_chunk(&state, "a.md", "alpha content for a", e0()).await;
        insert_chunk(&state, "b.md", "alpha content for b", e0()).await;

        let g = super::local_graph(&state, "a.md", 1).await;
        assert!(
            g.semantic_ready,
            "StubEmbedder(128d) must be semantic_ready"
        );
        let sem = g.edges.iter().find(|e| e.kind == "semantic");
        assert!(sem.is_some(), "must have a semantic edge: {:?}", g.edges);
        assert!(
            sem.unwrap().score.is_some(),
            "semantic edge must carry a score"
        );
    }

    // -----------------------------------------------------------------------
    // 3. semantic_edge_carries_provenance (dag-gated)
    // -----------------------------------------------------------------------
    #[cfg(feature = "dag")]
    #[tokio::test]
    async fn semantic_edge_carries_provenance() {
        let state =
            AppState::with_db_path_and_embedder(":memory:", None, Arc::new(StubEmbedder)).unwrap();
        {
            let mut graph = state.graph.write().await;
            graph.enable_dag();
        }
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        insert_chunk(&state, "a.md", "alpha content for a", e0()).await;
        insert_chunk(&state, "b.md", "alpha content for b", e0()).await;

        // Record a signed DAG action for b.md so provenance_anchor_for("b.md") is Some.
        {
            let graph = state.graph.read().await;
            let dag_store = graph.dag_store().expect("DAG must be enabled");
            let parents = dag_store.tips().expect("tips must be readable");
            let mut action = aingle_graph::dag::DagAction {
                parents,
                author: aingle_graph::NodeId::named("test"),
                seq: 0,
                timestamp: chrono::Utc::now(),
                payload: aingle_graph::dag::DagPayload::Custom {
                    payload_type: "ingest".to_string(),
                    payload_summary: "b.md ingested".to_string(),
                    payload: None,
                    subject: Some("b.md".to_string()),
                },
                signature: None,
            };
            let key = aingle_graph::dag::DagSigningKey::generate();
            key.sign(&mut action);
            dag_store
                .put(&action)
                .expect("put signed action must succeed");
        }

        let g = super::local_graph(&state, "a.md", 1).await;
        let sem = g
            .edges
            .iter()
            .find(|e| e.kind == "semantic" && e.target == "b.md")
            .expect("must have semantic edge a→b");
        assert!(
            sem.provenance_anchor.is_some(),
            "semantic edge to b.md must carry provenance_anchor when a signed DAG action exists: {:?}",
            sem
        );
    }

    // -----------------------------------------------------------------------
    // 4. tag_edge_from_shared_tag
    // -----------------------------------------------------------------------
    /// a.md and b.md both tagged "x" → a "tag" edge with label == Some("x").
    #[tokio::test]
    async fn tag_edge_from_shared_tag() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        insert_triple_lit(&state, "a.md", "tagged", "x").await;
        insert_triple_lit(&state, "b.md", "tagged", "x").await;

        let g = super::local_graph(&state, "a.md", 1).await;
        let tag_edge = g.edges.iter().find(|e| e.kind == "tag");
        assert!(tag_edge.is_some(), "must have a tag edge: {:?}", g.edges);
        assert_eq!(
            tag_edge.unwrap().label.as_deref(),
            Some("x"),
            "tag edge label must be the shared tag"
        );
    }

    // -----------------------------------------------------------------------
    // 5. hash_embedder_omits_semantic
    // -----------------------------------------------------------------------
    /// The default 64-dim hash embedder fails the semantic gate → semantic_ready==false,
    /// no "semantic" edges; link and tag edges still appear.
    #[tokio::test]
    async fn hash_embedder_omits_semantic() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        // A link edge (so we know other edges work).
        insert_triple_node(&state, "a.md", "links_to", "b").await;

        let g = super::local_graph(&state, "a.md", 1).await;
        assert!(
            !g.semantic_ready,
            "64-dim hash embedder must set semantic_ready=false"
        );
        assert!(
            g.edges.iter().all(|e| e.kind != "semantic"),
            "no semantic edges with hash embedder: {:?}",
            g.edges
        );
        // Link edges still present.
        assert!(
            g.edges.iter().any(|e| e.kind == "link"),
            "link edges must still appear: {:?}",
            g.edges
        );
    }

    // -----------------------------------------------------------------------
    // 6. maps_excluded
    // -----------------------------------------------------------------------
    /// Notes under `_maps/` are never included in the graph even when they
    /// share tags or links with the center note.
    #[tokio::test]
    async fn maps_excluded() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "a.md").await;
        register_note(&state, "_maps/vault-map.md").await;
        insert_triple_lit(&state, "a.md", "tagged", "x").await;
        insert_triple_lit(&state, "_maps/vault-map.md", "tagged", "x").await;
        // Also a direct link to make sure links are filtered too.
        insert_triple_node(&state, "a.md", "links_to", "vault-map").await;

        let g = super::local_graph(&state, "a.md", 1).await;
        assert!(
            !g.nodes.iter().any(|n| n.id.starts_with("_maps/")),
            "_maps/ nodes must be excluded: {:?}",
            g.nodes
        );
        assert!(
            !g.edges
                .iter()
                .any(|e| e.target.starts_with("_maps/") || e.source.starts_with("_maps/")),
            "_maps/ edges must be excluded: {:?}",
            g.edges
        );
    }

    // -----------------------------------------------------------------------
    // 7. caps_respected
    // -----------------------------------------------------------------------
    /// With more than NODE_CAP neighbors, nodes.len() <= NODE_CAP and center is present.
    #[tokio::test]
    async fn caps_respected() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "center.md").await;
        // Create NODE_CAP + 10 = 90 notes, each sharing a tag with center.md.
        for i in 0..90 {
            let path = format!("note{i}.md");
            register_note(&state, &path).await;
            insert_triple_lit(&state, &path, "tagged", "bigtag").await;
        }
        insert_triple_lit(&state, "center.md", "tagged", "bigtag").await;

        let g = super::local_graph(&state, "center.md", 1).await;
        assert!(
            g.nodes.len() <= super::NODE_CAP,
            "nodes.len() ({}) must be <= NODE_CAP ({}): center present: {}",
            g.nodes.len(),
            super::NODE_CAP,
            g.nodes.iter().any(|n| n.id == "center.md")
        );
        assert!(
            g.nodes.iter().any(|n| n.id == "center.md"),
            "center must always be in the graph: {:?}",
            g.nodes.iter().map(|n| &n.id).collect::<Vec<_>>()
        );
    }

    // -----------------------------------------------------------------------
    // 8. depth_two_expands_frontier
    // -----------------------------------------------------------------------
    /// A→B→C via wikilinks: depth=2 reaches c.md, depth=1 does not.
    #[tokio::test]
    async fn depth_two_expands_frontier() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        register_note(&state, "c.md").await;
        insert_triple_node(&state, "a.md", "links_to", "b").await;
        insert_triple_node(&state, "b.md", "links_to", "c").await;

        let g1 = super::local_graph(&state, "a.md", 1).await;
        assert!(
            !g1.nodes.iter().any(|n| n.id == "c.md"),
            "depth=1 must NOT include c.md: {:?}",
            g1.nodes.iter().map(|n| &n.id).collect::<Vec<_>>()
        );

        let g2 = super::local_graph(&state, "a.md", 2).await;
        assert!(
            g2.nodes.iter().any(|n| n.id == "c.md"),
            "depth=2 must include c.md (reached via a→b→c): {:?}",
            g2.nodes.iter().map(|n| &n.id).collect::<Vec<_>>()
        );
    }

    // -----------------------------------------------------------------------
    // 9. incoming_link_edge
    // -----------------------------------------------------------------------
    /// X links_to A (incoming); local_graph("a.md", 1) must include x→a link edge.
    #[tokio::test]
    async fn incoming_link_edge() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "x.md").await;
        register_note(&state, "a.md").await;
        insert_triple_node(&state, "x.md", "links_to", "a").await;

        let g = super::local_graph(&state, "a.md", 1).await;
        let link = g
            .edges
            .iter()
            .find(|e| e.kind == "link" && e.source == "x.md" && e.target == "a.md");
        assert!(
            link.is_some(),
            "incoming link x→a must appear in graph centered on a.md: {:?}",
            g.edges
        );
    }

    // -----------------------------------------------------------------------
    // 10. pair_with_link_and_semantic_keeps_both
    // -----------------------------------------------------------------------
    /// A links_to B AND B is A's semantic neighbor → both a link edge AND a
    /// semantic edge must be present for the pair (different dedup sets).
    #[tokio::test]
    async fn pair_with_link_and_semantic_keeps_both() {
        let state = stub_state();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        insert_triple_node(&state, "a.md", "links_to", "b").await;
        insert_chunk(&state, "a.md", "alpha content for a", e0()).await;
        insert_chunk(&state, "b.md", "alpha content for b", e0()).await;

        let g = super::local_graph(&state, "a.md", 1).await;
        let has_link = g
            .edges
            .iter()
            .any(|e| e.kind == "link" && e.source == "a.md" && e.target == "b.md");
        let has_sem = g.edges.iter().any(|e| {
            e.kind == "semantic"
                && ((e.source == "a.md" && e.target == "b.md")
                    || (e.source == "b.md" && e.target == "a.md"))
        });
        assert!(has_link, "link edge a→b must be present: {:?}", g.edges);
        assert!(has_sem, "semantic edge a↔b must be present: {:?}", g.edges);
    }

    // -----------------------------------------------------------------------
    // 11. symmetric_semantic_dedup
    // -----------------------------------------------------------------------
    /// With a→b and b→a semantic edges produced at different BFS levels, dedup
    /// must yield exactly ONE semantic edge for the pair.
    #[tokio::test]
    async fn symmetric_semantic_dedup() {
        let state = stub_state();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        insert_chunk(&state, "a.md", "alpha content for a", e0()).await;
        insert_chunk(&state, "b.md", "alpha content for b", e0()).await;

        // depth=2: level-1 processes a.md → finds b.md; level-2 processes b.md → finds a.md.
        // Both produce a↔b semantic edge candidates. Dedup keeps exactly one.
        let g = super::local_graph(&state, "a.md", 2).await;
        let sem_count = g
            .edges
            .iter()
            .filter(|e| {
                e.kind == "semantic"
                    && ((e.source == "a.md" && e.target == "b.md")
                        || (e.source == "b.md" && e.target == "a.md"))
            })
            .count();
        assert_eq!(
            sem_count, 1,
            "symmetric a↔b semantic must yield exactly ONE edge, got {sem_count}: {:?}",
            g.edges
        );
    }

    // -----------------------------------------------------------------------
    // 12. local_graph_cached_hit_and_invalidation
    // -----------------------------------------------------------------------
    /// Cache hit: second call with unchanged graph returns same result.
    /// Invalidation: after a graph mutation, the next call recomputes.
    #[tokio::test]
    async fn local_graph_cached_hit_and_invalidation() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        insert_triple_node(&state, "a.md", "links_to", "b").await;

        // First call: computes and caches.
        let g1 = super::local_graph_cached(&state, "a.md", 1).await;
        assert!(
            g1.nodes.iter().any(|n| n.id == "b.md"),
            "b.md must be in graph"
        );

        // Second call: graph/memory unchanged → cache hit → identical result.
        let g2 = super::local_graph_cached(&state, "a.md", 1).await;
        assert_eq!(
            g1.nodes.len(),
            g2.nodes.len(),
            "cache hit must return same node count"
        );

        // Mutate: add c.md and a link a→c (changes triple_count).
        register_note(&state, "c.md").await;
        insert_triple_node(&state, "a.md", "links_to", "c").await;

        // Third call: version mismatch → invalidated → c.md appears.
        let g3 = super::local_graph_cached(&state, "a.md", 1).await;
        assert!(
            g3.nodes.iter().any(|n| n.id == "c.md"),
            "after mutation, c.md must appear in recomputed result: {:?}",
            g3.nodes.iter().map(|n| &n.id).collect::<Vec<_>>()
        );
    }

    // -----------------------------------------------------------------------
    // 13. cache_cap_clears_when_exceeded
    // -----------------------------------------------------------------------
    /// When local_graph_cache exceeds 256 entries, the next insert clears the map
    /// first, then inserts the new entry — so len() == 1 afterward.
    #[tokio::test]
    async fn cache_cap_clears_when_exceeded() {
        let state = AppState::with_db_path(":memory:", None).unwrap();

        // Pre-fill with 257 dummy entries to exceed the cap.
        {
            let mut cache = state.local_graph_cache.lock().unwrap();
            for i in 0..257usize {
                cache.insert(
                    (format!("dummy_{i}.md"), 1usize),
                    ((0, 0), super::LocalGraph::default()),
                );
            }
        }
        assert_eq!(
            state.local_graph_cache.lock().unwrap().len(),
            257,
            "pre-condition: cache must have 257 dummy entries"
        );

        // Call for a key not in the cache; cap fires before insert.
        let _ = super::local_graph_cached(&state, "fresh.md", 1).await;

        let cache = state.local_graph_cache.lock().unwrap();
        assert_eq!(
            cache.len(),
            1,
            "cap must clear oversized cache then insert one entry; got {} entries",
            cache.len()
        );
        assert!(
            cache.contains_key(&("fresh.md".to_string(), 1usize)),
            "fresh.md must be in cache after cap-and-insert"
        );
    }

    // -----------------------------------------------------------------------
    // timestamp field: created triple → GNode.timestamp
    // -----------------------------------------------------------------------

    /// A `created` triple for a note must surface its value in `GNode.timestamp`.
    /// A note without a `created` triple must have `GNode.timestamp == None`.
    #[tokio::test]
    async fn gnode_timestamp_from_created_triple() {
        let state = AppState::with_db_path(":memory:", None).unwrap();
        register_note(&state, "a.md").await;
        register_note(&state, "b.md").await;
        insert_triple_node(&state, "a.md", "links_to", "b").await;
        insert_triple_lit(&state, "a.md", "created", "2025-03-15").await;

        let g = super::local_graph(&state, "a.md", 1).await;
        let node_a = g
            .nodes
            .iter()
            .find(|n| n.id == "a.md")
            .expect("a.md must be in graph");
        assert_eq!(
            node_a.timestamp,
            Some("2025-03-15".to_string()),
            "GNode.timestamp must come from the created triple"
        );
        let node_b = g
            .nodes
            .iter()
            .find(|n| n.id == "b.md")
            .expect("b.md must be in graph");
        assert_eq!(
            node_b.timestamp, None,
            "GNode without created triple must have timestamp=None"
        );
    }

    // -----------------------------------------------------------------------
    // 14. frontier_cap_bounds_semantic (optional perf guard)
    // -----------------------------------------------------------------------
    /// A hub with >SEM_FRONTIER_CAP link-neighbors at depth=2 still completes
    /// and the result satisfies NODE_CAP and includes the center.
    #[tokio::test]
    async fn frontier_cap_bounds_semantic() {
        let state = stub_state();
        register_note(&state, "center.md").await;
        register_note(&state, "hub.md").await;
        insert_triple_node(&state, "center.md", "links_to", "hub").await;
        insert_chunk(&state, "center.md", "alpha content center", e0()).await;
        insert_chunk(&state, "hub.md", "alpha content hub", e0()).await;

        // 20 spokes — more than SEM_FRONTIER_CAP (16).
        for i in 0..20usize {
            let path = format!("spoke{i}.md");
            register_note(&state, &path).await;
            insert_triple_node(&state, "hub.md", "links_to", &format!("spoke{i}")).await;
            insert_chunk(&state, &path, "alpha content spoke", e0()).await;
        }

        let g = super::local_graph(&state, "center.md", 2).await;
        assert!(
            g.nodes.len() <= super::NODE_CAP,
            "nodes must be ≤ NODE_CAP ({}), got {}",
            super::NODE_CAP,
            g.nodes.len()
        );
        assert!(
            g.nodes.iter().any(|n| n.id == "center.md"),
            "center must always be in the graph"
        );
    }

    // -----------------------------------------------------------------------
    // 15. neural_local_graph_has_semantic_edge  (real e5 model, gated)
    // -----------------------------------------------------------------------
    /// End-to-end acceptance test using the real multilingual-e5-small model.
    /// Skipped when the model files are absent. Requires `ORT_DYLIB_PATH`.
    ///
    /// Two same-topic Spanish notes (dog care) must share a semantic edge;
    /// an off-topic note (elections) must not appear (below NEIGHBOR_FLOOR=0.88).
    #[cfg(feature = "neural-embeddings")]
    #[tokio::test]
    async fn neural_local_graph_has_semantic_edge() {
        let model_dir = std::env::var("INERU_E5_MODEL_DIR").unwrap_or_else(|_| {
            concat!(
                env!("CARGO_MANIFEST_DIR"),
                "/../ineru/test-models/multilingual-e5-small"
            )
            .to_string()
        });
        if !std::path::Path::new(&model_dir)
            .join("onnx/model.onnx")
            .exists()
        {
            eprintln!(
                "skipping neural_local_graph_has_semantic_edge: e5 model not found at {model_dir}"
            );
            return;
        }

        let embedder = crate::embedder::build_embedder(Some(&model_dir));
        assert_eq!(
            embedder.dimensions(),
            384,
            "neural embedder must be active (384d)"
        );

        let state = AppState::with_db_path_and_embedder(":memory:", None, embedder).unwrap();
        {
            let mut graph = state.graph.write().await;
            graph.enable_dag();
        }

        let dir = tempfile::tempdir().unwrap();
        // Two same-topic notes about dog care (reused from neural_note_context_finds_same_topic).
        std::fs::write(
            dir.path().join("perros1.md"),
            "# Cuidado de perros\n\nLos perros necesitan paseos diarios, agua fresca y una dieta equilibrada para estar sanos.\n",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("perros2.md"),
            "# Mascotas\n\nUn perro sano requiere ejercicio diario, hidratación constante y alimentación balanceada.\n",
        )
        .unwrap();
        // Off-topic note: elections have no semantic overlap with dog care.
        std::fs::write(
            dir.path().join("elecciones.md"),
            "# Elecciones\n\nLos resultados de las elecciones presidenciales determinan el futuro del país.\n",
        )
        .unwrap();

        crate::service::ingest::ingest_path(&state, dir.path().to_str().unwrap(), None)
            .await
            .unwrap();

        let g = super::local_graph(&state, "perros1.md", 1).await;

        assert!(
            g.semantic_ready,
            "neural embedder (384d) must set semantic_ready=true"
        );

        // There must be a semantic edge connecting perros1↔perros2 (either orientation).
        let has_sem_edge = g.edges.iter().any(|e| {
            e.kind == "semantic"
                && ((e.source == "perros1.md" && e.target == "perros2.md")
                    || (e.source == "perros2.md" && e.target == "perros1.md"))
        });
        assert!(
            has_sem_edge,
            "perros1.md and perros2.md (same-topic) must share a semantic edge: {:?}",
            g.edges
        );

        // elecciones.md is off-topic; cosine vs perros1 is below NEIGHBOR_FLOOR (0.88).
        assert!(
            !g.edges.iter().any(|e| {
                e.kind == "semantic"
                    && (e.source == "elecciones.md" || e.target == "elecciones.md")
            }),
            "off-topic elecciones.md must not have a semantic edge (below NEIGHBOR_FLOOR=0.88): {:?}",
            g.edges
        );
    }
}