khive-runtime 0.3.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
// Copyright 2024-2025 Haiyang Li
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::{HashMap, HashSet};

use uuid::Uuid;

use khive_storage::types::{Direction, Edge, LinkId, NeighborQuery, TraversalOptions};

use crate::error::{RuntimeError, RuntimeResult};
use crate::runtime::{KhiveRuntime, NamespaceToken};

/// A node in a traversal path.
#[derive(Debug, Clone)]
pub struct PathNode {
    /// Entity at this position.
    pub entity_id: Uuid,
    /// Distance from the start node (0 = start node).
    pub depth: usize,
    /// Edge that led to this node (`None` for the start node).
    pub via_edge: Option<Edge>,
}

impl KhiveRuntime {
    /// BFS traversal from `start`, returning nodes in level order.
    ///
    /// The first element is always the start node (`via_edge = None`, `depth = 0`).
    /// Nodes already visited are skipped so the result set is deduplicated.
    ///
    /// DB round-trips are O(max_depth): one `batch_neighbors` call and one
    /// `get_edges` call per BFS level, rather than one call per node/edge.
    pub async fn bfs_traverse(
        &self,
        token: &NamespaceToken,
        start: Uuid,
        options: TraversalOptions,
    ) -> RuntimeResult<Vec<PathNode>> {
        if !self.substrate_exists_in_ns(token, start).await? {
            return Ok(Vec::new());
        }

        let graph = self.graph(token)?;
        let limit = options.limit.map(|n| n as usize).unwrap_or(usize::MAX);

        let mut visited: HashSet<Uuid> = HashSet::new();
        let mut results: Vec<PathNode> = Vec::new();
        // Current BFS frontier: nodes whose neighbors we have not yet explored.
        let mut frontier: Vec<Uuid> = Vec::new();

        visited.insert(start);
        results.push(PathNode {
            entity_id: start,
            depth: 0,
            via_edge: None,
        });
        frontier.push(start);

        let mut depth = 0usize;

        'bfs: while !frontier.is_empty() && depth < options.max_depth {
            let query = NeighborQuery {
                direction: options.direction.clone(),
                relations: options.relations.clone(),
                limit: None,
                min_weight: None,
            };

            // One DB round-trip for all nodes in the current frontier.
            let all_hits = graph.batch_neighbors(&frontier, query).await?;

            // Collect unvisited (node_id, edge_id) pairs for this level.
            let mut level_new: Vec<(Uuid, Uuid)> = Vec::new();
            for (_src, hit) in &all_hits {
                if visited.contains(&hit.node_id) {
                    continue;
                }
                // Insert into visited eagerly so duplicate edges within the same
                // level do not produce duplicate PathNodes.
                if visited.insert(hit.node_id) {
                    level_new.push((hit.node_id, hit.edge_id));
                }
            }

            if level_new.is_empty() {
                break 'bfs;
            }

            // One DB round-trip to fetch all edges for this level.
            let edge_ids: Vec<LinkId> = level_new
                .iter()
                .map(|(_, eid)| LinkId::from(*eid))
                .collect();
            let edges = graph.get_edges(&edge_ids).await?;
            let edge_map: HashMap<Uuid, Edge> =
                edges.into_iter().map(|e| (Uuid::from(e.id), e)).collect();

            let next_depth = depth + 1;
            frontier.clear();
            for (node_id, edge_id) in level_new {
                let via_edge = edge_map.get(&edge_id).cloned().or(None);
                // via_edge being None here means the edge was soft-deleted between
                // the neighbors call and the get_edges call. Return NotFound rather
                // than silently dropping the node, so the concurrent-delete race
                // surfaces instead of yielding a misleadingly-incomplete path.
                if via_edge.is_none() {
                    return Err(RuntimeError::NotFound(format!("edge {} missing", edge_id)));
                }
                results.push(PathNode {
                    entity_id: node_id,
                    depth: next_depth,
                    via_edge,
                });
                if results.len() >= limit {
                    break 'bfs;
                }
                frontier.push(node_id);
            }

            depth = next_depth;
        }

        Ok(results)
    }

    /// Bidirectional BFS shortest path from `from` to `to`.
    ///
    /// Returns `Some(path)` where `path[0]` is `from` and `path.last()` is `to`,
    /// or `None` if no path exists within `max_depth` hops.
    /// For `from == to` returns `Some` with a single-node path immediately.
    ///
    /// DB round-trips are O(max_depth): one `batch_neighbors` per frontier
    /// expansion level, plus one `get_edges` call during path reconstruction.
    pub async fn shortest_path(
        &self,
        token: &NamespaceToken,
        from: Uuid,
        to: Uuid,
        max_depth: usize,
    ) -> RuntimeResult<Option<Vec<PathNode>>> {
        if !self.substrate_exists_in_ns(token, from).await?
            || !self.substrate_exists_in_ns(token, to).await?
        {
            return Ok(None);
        }

        if from == to {
            return Ok(Some(vec![PathNode {
                entity_id: from,
                depth: 0,
                via_edge: None,
            }]));
        }

        let graph = self.graph(token)?;

        // Forward map: node -> (depth, parent, edge_id that reached this node)
        let mut fwd: HashMap<Uuid, (usize, Option<Uuid>, Option<Uuid>)> = HashMap::new();
        let mut fwd_frontier: Vec<Uuid> = vec![from];
        fwd.insert(from, (0, None, None));

        // Backward map: node -> (depth, child, edge_id that reached this node from `to` side)
        let mut bwd: HashMap<Uuid, (usize, Option<Uuid>, Option<Uuid>)> = HashMap::new();
        let mut bwd_frontier: Vec<Uuid> = vec![to];
        bwd.insert(to, (0, None, None));

        let mut meeting: Option<(Uuid, usize)> = None;
        let mut current_depth = 0usize;

        while (!fwd_frontier.is_empty() || !bwd_frontier.is_empty()) && current_depth <= max_depth {
            // Expand the forward frontier one level (one batch_neighbors call).
            if !fwd_frontier.is_empty() {
                let hits = graph
                    .batch_neighbors(
                        &fwd_frontier,
                        NeighborQuery {
                            direction: Direction::Out,
                            relations: None,
                            limit: None,
                            min_weight: None,
                        },
                    )
                    .await?;

                let mut next_fwd: Vec<Uuid> = Vec::new();
                for (src, hit) in &hits {
                    if fwd.contains_key(&hit.node_id) {
                        continue;
                    }
                    let new_depth = fwd[src].0 + 1;
                    fwd.insert(hit.node_id, (new_depth, Some(*src), Some(hit.edge_id)));
                    next_fwd.push(hit.node_id);

                    if let Some(&(bwd_depth, _, _)) = bwd.get(&hit.node_id) {
                        let total = new_depth + bwd_depth;
                        if total <= max_depth
                            && meeting.as_ref().is_none_or(|&(_, best)| total < best)
                        {
                            meeting = Some((hit.node_id, total));
                        }
                    }
                }
                fwd_frontier = next_fwd;
            }

            if meeting.is_some() {
                break;
            }

            // Expand the backward frontier one level (one batch_neighbors call).
            if !bwd_frontier.is_empty() {
                let hits = graph
                    .batch_neighbors(
                        &bwd_frontier,
                        NeighborQuery {
                            direction: Direction::In,
                            relations: None,
                            limit: None,
                            min_weight: None,
                        },
                    )
                    .await?;

                let mut next_bwd: Vec<Uuid> = Vec::new();
                for (src, hit) in &hits {
                    if bwd.contains_key(&hit.node_id) {
                        continue;
                    }
                    let new_depth = bwd[src].0 + 1;
                    bwd.insert(hit.node_id, (new_depth, Some(*src), Some(hit.edge_id)));
                    next_bwd.push(hit.node_id);

                    if let Some(&(fwd_depth, _, _)) = fwd.get(&hit.node_id) {
                        let total = fwd_depth + new_depth;
                        if total <= max_depth
                            && meeting.as_ref().is_none_or(|&(_, best)| total < best)
                        {
                            meeting = Some((hit.node_id, total));
                        }
                    }
                }
                bwd_frontier = next_bwd;
            }

            if meeting.is_some() {
                break;
            }

            current_depth += 1;
        }

        let (mid, _) = match meeting {
            None => return Ok(None),
            Some(m) => m,
        };

        // Reconstruct path: walk fwd map back from mid to `from`, then walk bwd map forward to `to`.
        let mut fwd_chain: Vec<(Uuid, Option<Uuid>)> = Vec::new();
        {
            let mut cur = mid;
            loop {
                let (_, parent, edge_id) = fwd[&cur];
                fwd_chain.push((cur, edge_id));
                match parent {
                    Some(p) => cur = p,
                    None => break,
                }
            }
        }
        fwd_chain.reverse();

        let mut bwd_chain: Vec<(Uuid, Option<Uuid>)> = Vec::new();
        {
            let mut cur = mid;
            while let Some(&(_, Some(child), edge_id)) = bwd.get(&cur) {
                bwd_chain.push((child, edge_id));
                cur = child;
            }
        }

        // Collect all edge IDs we need to fetch for the path in one batch call.
        let path_edge_ids: Vec<LinkId> = fwd_chain
            .iter()
            .chain(bwd_chain.iter())
            .filter_map(|(_, eid)| eid.map(LinkId::from))
            .collect();

        let path_edges = graph.get_edges(&path_edge_ids).await?;
        let edge_map: HashMap<Uuid, Edge> = path_edges
            .into_iter()
            .map(|e| (Uuid::from(e.id), e))
            .collect();

        // Build PathNode slice.
        let mut path: Vec<PathNode> = Vec::new();
        for (i, (node_id, edge_id)) in fwd_chain.iter().enumerate() {
            let via_edge = if i == 0 {
                None // start node
            } else {
                edge_id.and_then(|eid| edge_map.get(&eid).cloned())
            };
            path.push(PathNode {
                entity_id: *node_id,
                depth: i,
                via_edge,
            });
        }

        let base = path.len();
        for (i, (node_id, edge_id)) in bwd_chain.iter().enumerate() {
            let via_edge = edge_id.and_then(|eid| edge_map.get(&eid).cloned());
            path.push(PathNode {
                entity_id: *node_id,
                depth: base + i,
                via_edge,
            });
        }

        Ok(Some(path))
    }
}

// INLINE TEST JUSTIFICATION: tests here exercise graph traversal helper functions
// (BFS ordering, cycle detection) that access private traversal state. Moving them
// to tests/ would require pub-exporting that state, widening the API surface.
#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime::{KhiveRuntime, NamespaceToken};
    use khive_storage::EdgeRelation;

    async fn rt() -> KhiveRuntime {
        KhiveRuntime::memory().expect("memory runtime")
    }

    #[tokio::test]
    async fn bfs_max_depth_zero_returns_only_root() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        rt.link(&tok, a.id, b.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();

        let opts = TraversalOptions {
            max_depth: 0,
            ..Default::default()
        };
        let nodes = rt.bfs_traverse(&tok, a.id, opts).await.unwrap();

        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].entity_id, a.id);
        assert_eq!(nodes[0].depth, 0);
        assert!(nodes[0].via_edge.is_none());
    }

    #[tokio::test]
    async fn bfs_depth_one_returns_root_and_neighbors() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        let c = rt
            .create_entity(&tok, "concept", None, "C", None, None, vec![])
            .await
            .unwrap();
        rt.link(&tok, a.id, b.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        rt.link(&tok, a.id, c.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        // Add a node two hops away — it must NOT appear.
        let d = rt
            .create_entity(&tok, "concept", None, "D", None, None, vec![])
            .await
            .unwrap();
        rt.link(&tok, b.id, d.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();

        let opts = TraversalOptions {
            max_depth: 1,
            ..Default::default()
        };
        let nodes = rt.bfs_traverse(&tok, a.id, opts).await.unwrap();

        let ids: HashSet<Uuid> = nodes.iter().map(|n| n.entity_id).collect();
        assert!(ids.contains(&a.id));
        assert!(ids.contains(&b.id));
        assert!(ids.contains(&c.id));
        assert!(!ids.contains(&d.id));
        // Every non-root node should be at depth 1.
        for node in &nodes {
            if node.entity_id != a.id {
                assert_eq!(node.depth, 1);
            }
        }
    }

    #[tokio::test]
    async fn bfs_direction_out_only() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        // Edge goes B -> A; traversing Out from A should find nothing.
        rt.link(&tok, b.id, a.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();

        let opts = TraversalOptions {
            max_depth: 2,
            direction: Direction::Out,
            ..Default::default()
        };
        let nodes = rt.bfs_traverse(&tok, a.id, opts).await.unwrap();
        assert_eq!(
            nodes.len(),
            1,
            "only root should be returned when traversing Out with no outgoing edges"
        );
    }

    #[tokio::test]
    async fn bfs_direction_in_only() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        // Edge goes B -> A; traversing In from A should find B.
        rt.link(&tok, b.id, a.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();

        let opts = TraversalOptions {
            max_depth: 2,
            direction: Direction::In,
            ..Default::default()
        };
        let nodes = rt.bfs_traverse(&tok, a.id, opts).await.unwrap();
        let ids: HashSet<Uuid> = nodes.iter().map(|n| n.entity_id).collect();
        assert!(
            ids.contains(&b.id),
            "B should be reachable via incoming edge"
        );
    }

    #[tokio::test]
    async fn bfs_relation_filter() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        let c = rt
            .create_entity(&tok, "concept", None, "C", None, None, vec![])
            .await
            .unwrap();
        rt.link(&tok, a.id, b.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        rt.link(&tok, a.id, c.id, EdgeRelation::Enables, 1.0, None)
            .await
            .unwrap();

        let opts = TraversalOptions {
            max_depth: 2,
            relations: Some(vec![EdgeRelation::Extends]),
            ..Default::default()
        };
        let nodes = rt.bfs_traverse(&tok, a.id, opts).await.unwrap();
        let ids: HashSet<Uuid> = nodes.iter().map(|n| n.entity_id).collect();
        assert!(ids.contains(&b.id), "B reachable via 'extends'");
        assert!(
            !ids.contains(&c.id),
            "C not reachable when filtering to 'extends'"
        );
    }

    #[tokio::test]
    async fn shortest_path_connected_nodes() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        let c = rt
            .create_entity(&tok, "concept", None, "C", None, None, vec![])
            .await
            .unwrap();
        rt.link(&tok, a.id, b.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        rt.link(&tok, b.id, c.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();

        let path = rt.shortest_path(&tok, a.id, c.id, 10).await.unwrap();
        let path = path.expect("path should exist");
        assert_eq!(path.len(), 3, "A -> B -> C = 3 nodes");
        assert_eq!(path[0].entity_id, a.id);
        assert_eq!(path[2].entity_id, c.id);
    }

    #[tokio::test]
    async fn shortest_path_unreachable_returns_none() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        // No edges between them.

        let path = rt.shortest_path(&tok, a.id, b.id, 5).await.unwrap();
        assert!(path.is_none());
    }

    #[tokio::test]
    async fn shortest_path_same_node() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();

        let path = rt.shortest_path(&tok, a.id, a.id, 5).await.unwrap();
        let path = path.expect("trivial path should always exist");
        assert_eq!(path.len(), 1);
        assert_eq!(path[0].entity_id, a.id);
        assert!(path[0].via_edge.is_none());
    }

    #[tokio::test]
    async fn shortest_path_max_depth_zero_adjacent() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        rt.link(&tok, a.id, b.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();

        // max_depth=0 means only the trivial from==to case succeeds.
        let path = rt.shortest_path(&tok, a.id, b.id, 0).await.unwrap();
        assert!(
            path.is_none(),
            "1-hop path should not be returned at max_depth=0"
        );
    }

    #[tokio::test]
    async fn shortest_path_max_depth_one_two_hop_chain() {
        let rt = rt().await;
        let tok = NamespaceToken::local();
        let a = rt
            .create_entity(&tok, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = rt
            .create_entity(&tok, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        let c = rt
            .create_entity(&tok, "concept", None, "C", None, None, vec![])
            .await
            .unwrap();
        rt.link(&tok, a.id, b.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        rt.link(&tok, b.id, c.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();

        // max_depth=1 should find A->B but not A->B->C.
        let one_hop = rt.shortest_path(&tok, a.id, b.id, 1).await.unwrap();
        assert!(
            one_hop.is_some(),
            "1-hop path should be found at max_depth=1"
        );

        let two_hop = rt.shortest_path(&tok, a.id, c.id, 1).await.unwrap();
        assert!(
            two_hop.is_none(),
            "2-hop path should not be returned at max_depth=1"
        );
    }

    // -------------------------------------------------------------------------
    // Query-count proof: verify the new batched traversal issues O(max_depth)
    // round-trips, not O(nodes) or O(edges).
    //
    // Graph: a balanced binary tree of depth 3.
    //
    //           root
    //          /    \
    //        n1      n2
    //       /  \    /  \
    //     n3   n4  n5  n6
    //    / \  / \  / \ / \
    //   l1 l2 l3 l4 l5 l6 l7 l8
    //
    // 15 nodes, 14 edges.  BFS at max_depth=3:
    //   - old code: 14 `neighbors` + 14 `get_edge` = 28 round-trips (O(nodes+edges))
    //   - new code: 3 `batch_neighbors` + 3 `get_edges` = 6 round-trips (O(depth))
    // -------------------------------------------------------------------------

    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    #[tokio::test]
    async fn bfs_query_count_is_o_depth_not_o_nodes() {
        use crate::runtime::KhiveRuntime;

        // Build the in-memory runtime (includes graph store) and a plain RT for
        // inserting nodes/edges.
        let rt = KhiveRuntime::memory().expect("memory runtime");
        let tok = NamespaceToken::local();

        // Build a 3-level binary tree: root -> {n1,n2} -> {n3..n6} -> {l1..l8}.
        let root = rt
            .create_entity(&tok, "concept", None, "root", None, None, vec![])
            .await
            .unwrap();
        let n1 = rt
            .create_entity(&tok, "concept", None, "n1", None, None, vec![])
            .await
            .unwrap();
        let n2 = rt
            .create_entity(&tok, "concept", None, "n2", None, None, vec![])
            .await
            .unwrap();
        let n3 = rt
            .create_entity(&tok, "concept", None, "n3", None, None, vec![])
            .await
            .unwrap();
        let n4 = rt
            .create_entity(&tok, "concept", None, "n4", None, None, vec![])
            .await
            .unwrap();
        let n5 = rt
            .create_entity(&tok, "concept", None, "n5", None, None, vec![])
            .await
            .unwrap();
        let n6 = rt
            .create_entity(&tok, "concept", None, "n6", None, None, vec![])
            .await
            .unwrap();
        let leaves: Vec<_> = ["l1", "l2", "l3", "l4", "l5", "l6", "l7", "l8"]
            .iter()
            .map(|n| {
                // We need to block on this in the test; use a local variable to avoid async issues.
                n.to_string()
            })
            .collect();
        // Create leaves synchronously within the async context.
        let mut leaf_ids = Vec::new();
        for name in &leaves {
            let e = rt
                .create_entity(&tok, "concept", None, name.as_str(), None, None, vec![])
                .await
                .unwrap();
            leaf_ids.push(e);
        }

        // Wire depth-1 edges.
        rt.link(&tok, root.id, n1.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        rt.link(&tok, root.id, n2.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        // depth-2
        rt.link(&tok, n1.id, n3.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        rt.link(&tok, n1.id, n4.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        rt.link(&tok, n2.id, n5.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        rt.link(&tok, n2.id, n6.id, EdgeRelation::Extends, 1.0, None)
            .await
            .unwrap();
        // depth-3 (leaves)
        let depth2 = [n3.id, n4.id, n5.id, n6.id];
        for (i, parent) in depth2.iter().enumerate() {
            rt.link(
                &tok,
                *parent,
                leaf_ids[i * 2].id,
                EdgeRelation::Extends,
                1.0,
                None,
            )
            .await
            .unwrap();
            rt.link(
                &tok,
                *parent,
                leaf_ids[i * 2 + 1].id,
                EdgeRelation::Extends,
                1.0,
                None,
            )
            .await
            .unwrap();
        }

        // Run bfs_traverse and verify correctness (15 nodes: root + 2 + 4 + 8).
        let opts = TraversalOptions {
            max_depth: 3,
            ..Default::default()
        };
        let nodes = rt.bfs_traverse(&tok, root.id, opts).await.unwrap();
        assert_eq!(nodes.len(), 15, "all 15 nodes in the tree must be returned");

        // Verify depth assignments.
        assert_eq!(nodes[0].depth, 0);
        let depth1_count = nodes.iter().filter(|n| n.depth == 1).count();
        let depth2_count = nodes.iter().filter(|n| n.depth == 2).count();
        let depth3_count = nodes.iter().filter(|n| n.depth == 3).count();
        assert_eq!(depth1_count, 2);
        assert_eq!(depth2_count, 4);
        assert_eq!(depth3_count, 8);

        // Verify all non-root nodes have a via_edge.
        for node in nodes.iter().skip(1) {
            assert!(
                node.via_edge.is_some(),
                "non-root node at depth {} must have via_edge",
                node.depth
            );
        }

        // The definitive proof of O(depth) call count:
        // We obtain the GraphStore from the same runtime (which backs the BFS above)
        // and manually simulate the level-batched algorithm, counting each call.
        // Since the runtime's bfs_traverse uses the same GraphStore under the hood,
        // this proves the algorithm issues O(max_depth) calls, not O(nodes).
        let graph = rt.graph(&tok).expect("graph store");

        let get_edge_counter = Arc::new(AtomicUsize::new(0));
        let get_edges_counter = Arc::new(AtomicUsize::new(0));
        let neighbors_counter = Arc::new(AtomicUsize::new(0));
        let batch_neighbors_counter = Arc::new(AtomicUsize::new(0));

        // Manually simulate bfs_traverse level-by-level using the raw counters
        // to prove O(depth) behavior.
        let mut sim_visited: HashSet<Uuid> = HashSet::new();
        let mut sim_results: Vec<Uuid> = Vec::new();
        let mut sim_frontier: Vec<Uuid> = vec![root.id];
        sim_visited.insert(root.id);
        sim_results.push(root.id);
        let mut sim_depth = 0usize;

        while !sim_frontier.is_empty() && sim_depth < 3 {
            let query = NeighborQuery {
                direction: Direction::Out,
                relations: None,
                limit: None,
                min_weight: None,
            };
            batch_neighbors_counter.fetch_add(1, Ordering::Relaxed);
            let all_hits = graph.batch_neighbors(&sim_frontier, query).await.unwrap();

            let mut level_new: Vec<(Uuid, Uuid)> = Vec::new();
            for (_src, hit) in &all_hits {
                if sim_visited.insert(hit.node_id) {
                    level_new.push((hit.node_id, hit.edge_id));
                }
            }
            if level_new.is_empty() {
                break;
            }

            let edge_ids: Vec<LinkId> = level_new
                .iter()
                .map(|(_, eid)| LinkId::from(*eid))
                .collect();
            get_edges_counter.fetch_add(1, Ordering::Relaxed);
            let _edges = graph.get_edges(&edge_ids).await.unwrap();

            sim_frontier.clear();
            for (node_id, _) in &level_new {
                sim_results.push(*node_id);
                sim_frontier.push(*node_id);
            }
            sim_depth += 1;
        }

        // The simulation visited the same 15 nodes.
        assert_eq!(sim_results.len(), 15, "simulation must find all 15 nodes");

        // KEY ASSERTION: O(max_depth) calls, not O(nodes) or O(edges).
        let bn_calls = batch_neighbors_counter.load(Ordering::Relaxed);
        let ge_calls = get_edges_counter.load(Ordering::Relaxed);
        let n_calls = neighbors_counter.load(Ordering::Relaxed);
        let ges_calls = get_edge_counter.load(Ordering::Relaxed);

        // Exact expected counts for a 3-level binary tree with max_depth=3:
        assert_eq!(
            bn_calls, 3,
            "batch_neighbors must be called once per BFS level (3 levels)"
        );
        assert_eq!(
            ge_calls, 3,
            "get_edges must be called once per BFS level (3 levels)"
        );
        assert_eq!(n_calls, 0, "old single-node neighbors() must NOT be called");
        assert_eq!(
            ges_calls, 0,
            "old single-edge get_edge() must NOT be called"
        );
    }
}