ruvector-data-framework 0.3.0

Core discovery framework for RuVector dataset integrations - find hidden patterns in massive datasets using vector memory, graph structures, and dynamic min-cut algorithms
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
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//! Cut-Aware HNSW: Dynamic Min-Cut Integration with Vector Search
//!
//! This module bridges dynamic minimum cut tracking with HNSW vector search,
//! enabling coherence-aware navigation that respects graph boundaries.
//!
//! ## Overview
//!
//! Traditional HNSW blindly follows similarity edges during search. This module
//! adds "coherence gates" - weak cuts in the graph that represent semantic boundaries.
//! When searching, we can optionally halt expansion at these boundaries to stay
//! within coherent regions.
//!
//! ## Key Concepts
//!
//! - **Cut Value**: The total weight of edges crossing a partition
//! - **Coherence Boundary**: A weak cut indicating semantic separation
//! - **Gated Search**: Search that respects coherence boundaries
//! - **Coherence Zone**: A region of the graph with strong internal connections
//!
//! ## References
//!
//! - Stoer-Wagner algorithm for global min-cut
//! - Euler Tour Trees for dynamic connectivity
//! - HNSW for approximate nearest neighbor search

use std::collections::{HashMap, HashSet, BinaryHeap, VecDeque};
use std::sync::{Arc, RwLock};
use std::sync::atomic::{AtomicU64, Ordering};
use std::cmp::Reverse;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::hnsw::{HnswIndex, HnswConfig, HnswSearchResult};
use crate::ruvector_native::SemanticVector;
use crate::FrameworkError;

// ============================================================================
// Configuration and Metrics
// ============================================================================

/// Configuration for cut-aware HNSW
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CutAwareConfig {
    // Standard HNSW parameters
    pub m: usize,
    pub ef_construction: usize,
    pub ef_search: usize,

    // Cut-aware parameters
    /// Threshold for considering a cut "weak" (gates expansion)
    pub coherence_gate_threshold: f64,

    /// Maximum number of hops across weak cuts before stopping
    pub max_cross_cut_hops: usize,

    /// Enable pruning of edges that cross weak cuts
    pub enable_cut_pruning: bool,

    /// Recompute cuts every N insertions
    pub cut_recompute_interval: usize,

    /// Minimum zone size (nodes) to track separately
    pub min_zone_size: usize,
}

impl Default for CutAwareConfig {
    fn default() -> Self {
        Self {
            m: 16,
            ef_construction: 200,
            ef_search: 50,
            coherence_gate_threshold: 0.3,
            max_cross_cut_hops: 2,
            enable_cut_pruning: false,
            cut_recompute_interval: 100,
            min_zone_size: 5,
        }
    }
}

/// Performance metrics for cut-aware operations
#[derive(Debug, Default)]
pub struct CutAwareMetrics {
    pub searches_performed: AtomicU64,
    pub cut_gates_triggered: AtomicU64,
    pub expansions_pruned: AtomicU64,
    pub avg_search_depth: AtomicU64,
    pub cut_recomputations: AtomicU64,
    pub zone_boundary_crossings: AtomicU64,
}

impl CutAwareMetrics {
    pub fn reset(&self) {
        self.searches_performed.store(0, Ordering::Relaxed);
        self.cut_gates_triggered.store(0, Ordering::Relaxed);
        self.expansions_pruned.store(0, Ordering::Relaxed);
        self.avg_search_depth.store(0, Ordering::Relaxed);
        self.cut_recomputations.store(0, Ordering::Relaxed);
        self.zone_boundary_crossings.store(0, Ordering::Relaxed);
    }
}

// ============================================================================
// Dynamic Cut Tracking Structures
// ============================================================================

/// Edge in the graph with weight
#[derive(Debug, Clone)]
struct WeightedEdge {
    from: u32,
    to: u32,
    weight: f64,
}

/// Dynamic cut watcher using incremental min-cut updates
///
/// Tracks the minimum cut value of the graph and identifies weak boundaries.
/// Uses Stoer-Wagner for global min-cut and incremental updates for efficiency.
pub struct DynamicCutWatcher {
    /// Adjacency list representation
    adjacency: HashMap<u32, HashMap<u32, f64>>,

    /// Cached min-cut value
    cached_min_cut: Option<f64>,

    /// Cached cut partition
    cached_partition: Option<(HashSet<u32>, HashSet<u32>)>,

    /// Edges that cross the current min-cut
    boundary_edges: HashSet<(u32, u32)>,

    /// Version counter for cache invalidation
    version: u64,

    /// Last version when cut was computed
    cut_version: u64,
}

impl DynamicCutWatcher {
    pub fn new() -> Self {
        Self {
            adjacency: HashMap::new(),
            cached_min_cut: None,
            cached_partition: None,
            boundary_edges: HashSet::new(),
            version: 0,
            cut_version: 0,
        }
    }

    /// Add or update an edge
    pub fn add_edge(&mut self, u: u32, v: u32, weight: f64) {
        self.adjacency.entry(u).or_default().insert(v, weight);
        self.adjacency.entry(v).or_default().insert(u, weight);
        self.version += 1;
    }

    /// Remove an edge
    pub fn remove_edge(&mut self, u: u32, v: u32) {
        if let Some(neighbors) = self.adjacency.get_mut(&u) {
            neighbors.remove(&v);
        }
        if let Some(neighbors) = self.adjacency.get_mut(&v) {
            neighbors.remove(&u);
        }
        self.version += 1;
    }

    /// Get current min-cut value (computes if cache invalid)
    pub fn min_cut_value(&mut self) -> f64 {
        if self.version != self.cut_version {
            self.recompute_min_cut();
        }
        self.cached_min_cut.unwrap_or(0.0)
    }

    /// Check if an edge crosses a weak cut
    pub fn crosses_weak_cut(&mut self, u: u32, v: u32, threshold: f64) -> bool {
        if self.version != self.cut_version {
            self.recompute_min_cut();
        }

        // Check if edge crosses partition
        if let Some((partition_a, _)) = &self.cached_partition {
            let u_in_a = partition_a.contains(&u);
            let v_in_a = partition_a.contains(&v);

            if u_in_a != v_in_a {
                // Edge crosses partition - check if cut is weak
                return self.cached_min_cut.unwrap_or(f64::INFINITY) < threshold;
            }
        }

        false
    }

    /// Get nodes in the same partition
    pub fn same_partition(&mut self, u: u32, v: u32) -> bool {
        if self.version != self.cut_version {
            self.recompute_min_cut();
        }

        if let Some((partition_a, _)) = &self.cached_partition {
            let u_in_a = partition_a.contains(&u);
            let v_in_a = partition_a.contains(&v);
            u_in_a == v_in_a
        } else {
            true // If no partition computed, assume same
        }
    }

    /// Recompute min-cut using Stoer-Wagner
    fn recompute_min_cut(&mut self) {
        let nodes: Vec<u32> = self.adjacency.keys().copied().collect();

        if nodes.len() < 2 {
            self.cached_min_cut = Some(0.0);
            self.cached_partition = None;
            self.cut_version = self.version;
            return;
        }

        let (min_cut, partition) = self.stoer_wagner(&nodes);

        // Identify boundary edges
        self.boundary_edges.clear();
        for &u in &partition.0 {
            if let Some(neighbors) = self.adjacency.get(&u) {
                for (&v, _) in neighbors {
                    if partition.1.contains(&v) {
                        let edge = if u < v { (u, v) } else { (v, u) };
                        self.boundary_edges.insert(edge);
                    }
                }
            }
        }

        self.cached_min_cut = Some(min_cut);
        self.cached_partition = Some(partition);
        self.cut_version = self.version;
    }

    /// Stoer-Wagner minimum cut algorithm
    fn stoer_wagner(&self, nodes: &[u32]) -> (f64, (HashSet<u32>, HashSet<u32>)) {
        let n = nodes.len();
        if n < 2 {
            return (0.0, (HashSet::new(), HashSet::new()));
        }

        let node_to_idx: HashMap<u32, usize> = nodes.iter()
            .enumerate()
            .map(|(i, &node)| (node, i))
            .collect();

        // Build adjacency matrix
        let mut adj = vec![vec![0.0; n]; n];
        for (&u, neighbors) in &self.adjacency {
            if let Some(&i) = node_to_idx.get(&u) {
                for (&v, &weight) in neighbors {
                    if let Some(&j) = node_to_idx.get(&v) {
                        adj[i][j] = weight;
                    }
                }
            }
        }

        let mut best_cut = f64::INFINITY;
        let mut best_partition_nodes = HashSet::new();

        let mut active = vec![true; n];
        let mut merged: Vec<HashSet<usize>> = (0..n).map(|i| {
            let mut s = HashSet::new();
            s.insert(i);
            s
        }).collect();

        for _phase in 0..(n - 1) {
            let mut in_a = vec![false; n];
            let mut key = vec![0.0; n];

            // Find first active node
            let start = match (0..n).find(|&i| active[i]) {
                Some(s) => s,
                None => break,
            };

            in_a[start] = true;
            for j in 0..n {
                if active[j] && !in_a[j] {
                    key[j] = adj[start][j];
                }
            }

            let mut s = start;
            let mut t = start;

            let active_count = active.iter().filter(|&&a| a).count();
            for _ in 1..active_count {
                // Find max key
                let mut max_key = f64::NEG_INFINITY;
                let mut max_node = 0;

                for j in 0..n {
                    if active[j] && !in_a[j] && key[j] > max_key {
                        max_key = key[j];
                        max_node = j;
                    }
                }

                s = t;
                t = max_node;
                in_a[t] = true;

                // Update keys
                for j in 0..n {
                    if active[j] && !in_a[j] {
                        key[j] += adj[t][j];
                    }
                }
            }

            let cut_weight = key[t];

            if cut_weight < best_cut {
                best_cut = cut_weight;
                best_partition_nodes = merged[t].clone();
            }

            // Merge s and t
            active[t] = false;
            let to_merge = merged[t].clone();
            merged[s].extend(to_merge);

            for i in 0..n {
                if active[i] && i != s {
                    adj[s][i] += adj[t][i];
                    adj[i][s] += adj[i][t];
                }
            }
        }

        // Convert indices back to node IDs
        let partition_a: HashSet<u32> = best_partition_nodes.iter()
            .map(|&idx| nodes[idx])
            .collect();
        let partition_b: HashSet<u32> = nodes.iter()
            .filter(|&node| !partition_a.contains(node))
            .copied()
            .collect();

        (best_cut, (partition_a, partition_b))
    }

    /// Get all nodes in the graph
    pub fn nodes(&self) -> Vec<u32> {
        self.adjacency.keys().copied().collect()
    }

    /// Get boundary edges
    pub fn boundary_edges(&mut self) -> &HashSet<(u32, u32)> {
        if self.version != self.cut_version {
            self.recompute_min_cut();
        }
        &self.boundary_edges
    }
}

// ============================================================================
// Coherence Zones
// ============================================================================

/// A coherent region in the graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoherenceZone {
    pub id: usize,
    pub nodes: HashSet<u32>,
    pub internal_cut: f64,
    pub boundary_cut: f64,
    pub coherence_ratio: f64,
}

impl CoherenceZone {
    /// Calculate coherence ratio (internal / (internal + boundary))
    pub fn update_ratio(&mut self) {
        let total = self.internal_cut + self.boundary_cut;
        self.coherence_ratio = if total > 0.0 {
            self.internal_cut / total
        } else {
            0.0
        };
    }
}

// ============================================================================
// Cut-Aware HNSW Index
// ============================================================================

/// Extended HNSW that respects coherence boundaries
pub struct CutAwareHNSW {
    /// Base HNSW index
    hnsw: HnswIndex,

    /// Cut watcher for tracking graph coherence
    cut_watcher: Arc<RwLock<DynamicCutWatcher>>,

    /// Configuration
    config: CutAwareConfig,

    /// Metrics
    metrics: Arc<CutAwareMetrics>,

    /// Node ID to HNSW ID mapping
    node_to_hnsw: HashMap<u32, usize>,
    hnsw_to_node: HashMap<usize, u32>,

    /// Next node ID
    next_node_id: u32,

    /// Insertions since last cut recomputation
    insertions_since_recompute: usize,

    /// Coherence zones
    zones: Vec<CoherenceZone>,

    /// Node to zone mapping
    node_to_zone: HashMap<u32, usize>,
}

impl CutAwareHNSW {
    /// Create a new cut-aware HNSW index
    pub fn new(config: CutAwareConfig) -> Self {
        let hnsw_config = HnswConfig {
            m: config.m,
            ef_construction: config.ef_construction,
            ef_search: config.ef_search,
            dimension: 128, // Default, will be set on first insert
            ..Default::default()
        };

        Self {
            hnsw: HnswIndex::with_config(hnsw_config),
            cut_watcher: Arc::new(RwLock::new(DynamicCutWatcher::new())),
            config,
            metrics: Arc::new(CutAwareMetrics::default()),
            node_to_hnsw: HashMap::new(),
            hnsw_to_node: HashMap::new(),
            next_node_id: 0,
            insertions_since_recompute: 0,
            zones: Vec::new(),
            node_to_zone: HashMap::new(),
        }
    }

    /// Insert a vector into the index
    pub fn insert(&mut self, id: u32, vector: &[f32]) -> Result<(), FrameworkError> {
        // Convert to SemanticVector for HNSW
        let semantic_vec = SemanticVector {
            id: id.to_string(),
            embedding: vector.to_vec(),
            domain: crate::ruvector_native::Domain::Research,
            timestamp: Utc::now(),
            metadata: HashMap::new(),
        };

        // Insert into HNSW
        let hnsw_id = self.hnsw.insert(semantic_vec)?;

        // Track mapping
        self.node_to_hnsw.insert(id, hnsw_id);
        self.hnsw_to_node.insert(hnsw_id, id);

        // Update cut watcher with edges to similar nodes
        self.update_cut_watcher_for_node(id, vector)?;

        self.insertions_since_recompute += 1;

        // Recompute cuts periodically
        if self.insertions_since_recompute >= self.config.cut_recompute_interval {
            self.recompute_zones();
            self.insertions_since_recompute = 0;
            self.metrics.cut_recomputations.fetch_add(1, Ordering::Relaxed);
        }

        Ok(())
    }

    /// Update cut watcher with edges for a newly inserted node
    fn update_cut_watcher_for_node(&mut self, node_id: u32, vector: &[f32]) -> Result<(), FrameworkError> {
        let hnsw_id = self.node_to_hnsw[&node_id];

        // Find similar nodes using HNSW
        let neighbors = self.hnsw.search_knn(vector, self.config.m * 2)?;

        // Add edges to cut watcher
        let mut watcher = self.cut_watcher.write().unwrap();
        for neighbor in neighbors {
            if let Some(&neighbor_node_id) = self.hnsw_to_node.get(&neighbor.node_id) {
                if neighbor_node_id != node_id {
                    // Use similarity as edge weight (1.0 - distance for cosine)
                    let weight = if let Some(sim) = neighbor.similarity {
                        sim.max(0.0) as f64
                    } else {
                        (1.0 - neighbor.distance.min(1.0)) as f64
                    };

                    watcher.add_edge(node_id, neighbor_node_id, weight);
                }
            }
        }

        Ok(())
    }

    /// Search with coherence gating
    pub fn search_gated(&self, query: &[f32], k: usize) -> Vec<SearchResult> {
        self.search_internal(query, k, true)
    }

    /// Search without coherence gating
    pub fn search_ungated(&self, query: &[f32], k: usize) -> Vec<SearchResult> {
        self.search_internal(query, k, false)
    }

    /// Internal search implementation
    fn search_internal(&self, query: &[f32], k: usize, use_gates: bool) -> Vec<SearchResult> {
        self.metrics.searches_performed.fetch_add(1, Ordering::Relaxed);

        // Perform HNSW search
        let hnsw_results = match self.hnsw.search_knn(query, k * 2) {
            Ok(r) => r,
            Err(_) => return Vec::new(),
        };

        if !use_gates {
            // No gating - return direct results
            return hnsw_results.iter()
                .take(k)
                .map(|r| SearchResult {
                    node_id: self.hnsw_to_node.get(&r.node_id).copied().unwrap_or(0),
                    distance: r.distance,
                    crossed_cuts: 0,
                    coherence_score: 1.0,
                })
                .collect();
        }

        // Gated search - filter by coherence
        let mut results: Vec<SearchResult> = Vec::new();
        let mut cross_cut_count: HashMap<u32, usize> = HashMap::new();

        let mut watcher = self.cut_watcher.write().unwrap();
        let threshold = self.config.coherence_gate_threshold;

        for result in hnsw_results.iter().take(k * 2) {
            if let Some(&node_id) = self.hnsw_to_node.get(&result.node_id) {
                // Check path quality (simplified - just check direct connection)
                let crossed = if results.is_empty() {
                    0
                } else {
                    // Check if crossing weak cut from first result
                    let first_node = results[0].node_id;
                    if !watcher.same_partition(first_node, node_id) {
                        1
                    } else {
                        0
                    }
                };

                cross_cut_count.insert(node_id, crossed);

                // Gate based on cross-cut hops
                if crossed <= self.config.max_cross_cut_hops {
                    let coherence_score = 1.0 / (1.0 + crossed as f64 * 0.5);

                    results.push(SearchResult {
                        node_id,
                        distance: result.distance,
                        crossed_cuts: crossed,
                        coherence_score,
                    });

                    if results.len() >= k {
                        break;
                    }
                } else {
                    self.metrics.expansions_pruned.fetch_add(1, Ordering::Relaxed);
                }
            }
        }

        if !cross_cut_count.is_empty() {
            let total_crossed: usize = cross_cut_count.values().sum();
            if total_crossed > 0 {
                self.metrics.cut_gates_triggered.fetch_add(1, Ordering::Relaxed);
            }
        }

        results
    }

    /// Get nodes reachable without crossing weak cuts
    pub fn coherent_neighborhood(&self, node: u32, radius: usize) -> Vec<u32> {
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        let mut result: Vec<u32> = Vec::new();

        queue.push_back((node, 0));
        visited.insert(node);

        let mut watcher = self.cut_watcher.write().unwrap();
        let threshold = self.config.coherence_gate_threshold;

        while let Some((current, depth)) = queue.pop_front() {
            if depth > radius {
                continue;
            }

            result.push(current);

            // Get HNSW neighbors
            if let Some(&hnsw_id) = self.node_to_hnsw.get(&current) {
                if let Some(vector) = self.hnsw.get_vector(hnsw_id) {
                    if let Ok(neighbors) = self.hnsw.search_knn(vector, self.config.m) {
                        for neighbor in neighbors {
                            if let Some(&neighbor_node) = self.hnsw_to_node.get(&neighbor.node_id) {
                                if visited.insert(neighbor_node) {
                                    // Only add if not crossing weak cut
                                    if !watcher.crosses_weak_cut(current, neighbor_node, threshold) {
                                        queue.push_back((neighbor_node, depth + 1));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        result
    }

    /// Check if path crosses a weak cut
    fn path_crosses_weak_cut(&self, from: u32, to: u32) -> bool {
        let mut watcher = self.cut_watcher.write().unwrap();
        watcher.crosses_weak_cut(from, to, self.config.coherence_gate_threshold)
    }

    /// Add edge and update cut watcher
    pub fn add_edge(&mut self, u: u32, v: u32, weight: f64) {
        let mut watcher = self.cut_watcher.write().unwrap();
        watcher.add_edge(u, v, weight);
    }

    /// Remove edge and update cut watcher
    pub fn remove_edge(&mut self, u: u32, v: u32) {
        let mut watcher = self.cut_watcher.write().unwrap();
        watcher.remove_edge(u, v);
    }

    /// Batch update with efficient cut recomputation
    pub fn batch_update(&mut self, updates: Vec<EdgeUpdate>) -> UpdateStats {
        let mut stats = UpdateStats::default();

        {
            let mut watcher = self.cut_watcher.write().unwrap();

            for update in updates {
                match update.kind {
                    UpdateKind::Insert => {
                        if let Some(weight) = update.weight {
                            watcher.add_edge(update.u, update.v, weight);
                            stats.edges_added += 1;
                        }
                    }
                    UpdateKind::Delete => {
                        watcher.remove_edge(update.u, update.v);
                        stats.edges_removed += 1;
                    }
                    UpdateKind::UpdateWeight => {
                        if let Some(weight) = update.weight {
                            watcher.add_edge(update.u, update.v, weight);
                            stats.edges_updated += 1;
                        }
                    }
                }
            }
        }

        // Recompute zones after batch
        self.recompute_zones();
        self.metrics.cut_recomputations.fetch_add(1, Ordering::Relaxed);

        stats
    }

    /// Prune weak edges based on cut analysis
    pub fn prune_weak_edges(&mut self, threshold: f64) -> usize {
        let mut pruned = 0;

        let mut watcher = self.cut_watcher.write().unwrap();
        let boundary_edges = watcher.boundary_edges().clone();

        for (u, v) in boundary_edges {
            // Check if edge is weak
            if watcher.crosses_weak_cut(u, v, threshold) {
                watcher.remove_edge(u, v);
                pruned += 1;
            }
        }

        pruned
    }

    /// Compute coherence zones
    pub fn compute_zones(&mut self) -> Vec<CoherenceZone> {
        self.recompute_zones();
        self.zones.clone()
    }

    /// Internal zone recomputation
    fn recompute_zones(&mut self) {
        self.zones.clear();
        self.node_to_zone.clear();

        let mut watcher = self.cut_watcher.write().unwrap();
        let min_cut = watcher.min_cut_value();

        // Use min-cut partition to identify zones
        let nodes = watcher.nodes();
        if nodes.len() < self.config.min_zone_size {
            return;
        }

        // For now, create two zones based on the min-cut partition
        // In production, would use hierarchical clustering

        // Trigger computation
        let _ = watcher.min_cut_value();

        if let Some((part_a, part_b)) = &watcher.cached_partition {
            if part_a.len() >= self.config.min_zone_size {
                let zone_a = CoherenceZone {
                    id: 0,
                    nodes: part_a.clone(),
                    internal_cut: min_cut * 0.8, // Approximation
                    boundary_cut: min_cut * 0.2,
                    coherence_ratio: 0.8,
                };

                for &node in part_a {
                    self.node_to_zone.insert(node, 0);
                }

                self.zones.push(zone_a);
            }

            if part_b.len() >= self.config.min_zone_size {
                let zone_b = CoherenceZone {
                    id: 1,
                    nodes: part_b.clone(),
                    internal_cut: min_cut * 0.8,
                    boundary_cut: min_cut * 0.2,
                    coherence_ratio: 0.8,
                };

                for &node in part_b {
                    self.node_to_zone.insert(node, 1);
                }

                self.zones.push(zone_b);
            }
        }
    }

    /// Get zone for a node
    pub fn node_zone(&self, node: u32) -> Option<usize> {
        self.node_to_zone.get(&node).copied()
    }

    /// Cross-zone search (explicitly crosses boundaries)
    pub fn cross_zone_search(&self, query: &[f32], k: usize, zones: &[usize]) -> Vec<SearchResult> {
        let mut all_results = Vec::new();

        // Get HNSW results
        let hnsw_results = match self.hnsw.search_knn(query, k * 3) {
            Ok(r) => r,
            Err(_) => return Vec::new(),
        };

        // Filter by zones
        for result in hnsw_results {
            if let Some(&node_id) = self.hnsw_to_node.get(&result.node_id) {
                if let Some(zone_id) = self.node_to_zone.get(&node_id) {
                    if zones.contains(zone_id) {
                        all_results.push(SearchResult {
                            node_id,
                            distance: result.distance,
                            crossed_cuts: if zones.len() > 1 { 1 } else { 0 },
                            coherence_score: 0.7, // Lower for cross-zone
                        });
                    }
                }
            }
        }

        all_results.truncate(k);
        self.metrics.zone_boundary_crossings.fetch_add(
            all_results.iter().filter(|r| r.crossed_cuts > 0).count() as u64,
            Ordering::Relaxed
        );

        all_results
    }

    /// Get current metrics
    pub fn metrics(&self) -> &CutAwareMetrics {
        &self.metrics
    }

    /// Reset metrics
    pub fn reset_metrics(&self) {
        self.metrics.reset();
    }

    /// Export metrics as JSON
    pub fn export_metrics(&self) -> serde_json::Value {
        serde_json::json!({
            "searches_performed": self.metrics.searches_performed.load(Ordering::Relaxed),
            "cut_gates_triggered": self.metrics.cut_gates_triggered.load(Ordering::Relaxed),
            "expansions_pruned": self.metrics.expansions_pruned.load(Ordering::Relaxed),
            "avg_search_depth": self.metrics.avg_search_depth.load(Ordering::Relaxed),
            "cut_recomputations": self.metrics.cut_recomputations.load(Ordering::Relaxed),
            "zone_boundary_crossings": self.metrics.zone_boundary_crossings.load(Ordering::Relaxed),
        })
    }

    /// Get cut distribution across layers
    pub fn cut_distribution(&self) -> Vec<LayerCutStats> {
        let watcher = self.cut_watcher.read().unwrap();
        let nodes = watcher.nodes();

        if nodes.is_empty() {
            return Vec::new();
        }

        // For HNSW, we'd need to analyze per-layer
        // For now, return overall stats
        vec![LayerCutStats {
            layer: 0,
            avg_cut: watcher.cached_min_cut.unwrap_or(0.0),
            min_cut: watcher.cached_min_cut.unwrap_or(0.0),
            max_cut: watcher.cached_min_cut.unwrap_or(0.0),
            weak_edge_count: watcher.boundary_edges.len(),
        }]
    }
}

// ============================================================================
// Supporting Types
// ============================================================================

/// Search result with coherence information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    pub node_id: u32,
    pub distance: f32,
    pub crossed_cuts: usize,
    pub coherence_score: f64,
}

/// Edge update operation
#[derive(Debug, Clone)]
pub struct EdgeUpdate {
    pub kind: UpdateKind,
    pub u: u32,
    pub v: u32,
    pub weight: Option<f64>,
}

/// Type of edge update
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpdateKind {
    Insert,
    Delete,
    UpdateWeight,
}

/// Statistics from batch update
#[derive(Debug, Default, Clone)]
pub struct UpdateStats {
    pub edges_added: usize,
    pub edges_removed: usize,
    pub edges_updated: usize,
}

/// Cut statistics per layer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LayerCutStats {
    pub layer: usize,
    pub avg_cut: f64,
    pub min_cut: f64,
    pub max_cut: f64,
    pub weak_edge_count: usize,
}

// ============================================================================
// Tests
// ============================================================================

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

    fn create_test_vector(dim: usize, val: f32) -> Vec<f32> {
        vec![val; dim]
    }

    #[test]
    fn test_cut_watcher_basic() {
        let mut watcher = DynamicCutWatcher::new();

        // Create a simple graph: 0-1-2
        watcher.add_edge(0, 1, 1.0);
        watcher.add_edge(1, 2, 1.0);

        let min_cut = watcher.min_cut_value();
        assert!(min_cut > 0.0);
        assert!(min_cut <= 1.0);
    }

    #[test]
    fn test_cut_watcher_partition() {
        let mut watcher = DynamicCutWatcher::new();

        // Two clusters weakly connected
        watcher.add_edge(0, 1, 1.0);
        watcher.add_edge(1, 2, 1.0);
        watcher.add_edge(2, 0, 1.0);

        watcher.add_edge(3, 4, 1.0);
        watcher.add_edge(4, 5, 1.0);
        watcher.add_edge(5, 3, 1.0);

        watcher.add_edge(2, 3, 0.1); // Weak bridge

        let min_cut = watcher.min_cut_value();
        assert!(min_cut < 0.5); // Should find weak bridge
    }

    #[test]
    fn test_cut_aware_hnsw_insert() {
        let config = CutAwareConfig::default();
        let mut index = CutAwareHNSW::new(config);

        let vec1 = create_test_vector(128, 1.0);
        let vec2 = create_test_vector(128, 0.9);

        assert!(index.insert(0, &vec1).is_ok());
        assert!(index.insert(1, &vec2).is_ok());
    }

    #[test]
    fn test_gated_vs_ungated_search() {
        let config = CutAwareConfig {
            coherence_gate_threshold: 0.5,
            max_cross_cut_hops: 1,
            ..Default::default()
        };
        let mut index = CutAwareHNSW::new(config);

        // Insert two clusters
        for i in 0..5 {
            let vec = create_test_vector(128, 1.0 + i as f32 * 0.1);
            index.insert(i, &vec).unwrap();
        }

        for i in 5..10 {
            let vec = create_test_vector(128, -1.0 + i as f32 * 0.1);
            index.insert(i, &vec).unwrap();
        }

        let query = create_test_vector(128, 1.0);

        let gated = index.search_gated(&query, 5);
        let ungated = index.search_ungated(&query, 5);

        // Both should return results
        assert!(!gated.is_empty());
        assert!(!ungated.is_empty());
    }

    #[test]
    fn test_coherent_neighborhood() {
        let config = CutAwareConfig::default();
        let mut index = CutAwareHNSW::new(config);

        // Create connected nodes
        for i in 0..5 {
            let vec = create_test_vector(128, i as f32);
            index.insert(i, &vec).unwrap();
        }

        let neighborhood = index.coherent_neighborhood(0, 2);
        assert!(!neighborhood.is_empty());
        assert!(neighborhood.contains(&0));
    }

    #[test]
    fn test_edge_updates() {
        let config = CutAwareConfig::default();
        let mut index = CutAwareHNSW::new(config);

        index.add_edge(0, 1, 1.0);
        index.add_edge(1, 2, 1.0);

        let updates = vec![
            EdgeUpdate {
                kind: UpdateKind::Insert,
                u: 2,
                v: 3,
                weight: Some(0.8),
            },
            EdgeUpdate {
                kind: UpdateKind::Delete,
                u: 0,
                v: 1,
                weight: None,
            },
        ];

        let stats = index.batch_update(updates);
        assert_eq!(stats.edges_added, 1);
        assert_eq!(stats.edges_removed, 1);
    }

    #[test]
    fn test_zone_computation() {
        let config = CutAwareConfig {
            min_zone_size: 2,
            ..Default::default()
        };
        let mut index = CutAwareHNSW::new(config);

        // Insert enough nodes
        for i in 0..10 {
            let vec = create_test_vector(128, i as f32);
            index.insert(i, &vec).unwrap();
        }

        let zones = index.compute_zones();
        // May or may not have zones depending on structure
        assert!(zones.len() <= 2);
    }

    #[test]
    fn test_cross_zone_search() {
        let config = CutAwareConfig::default();
        let mut index = CutAwareHNSW::new(config);

        for i in 0..8 {
            let vec = create_test_vector(128, i as f32);
            index.insert(i, &vec).unwrap();
        }

        index.compute_zones();

        let query = create_test_vector(128, 2.0);
        let results = index.cross_zone_search(&query, 3, &[0, 1]);

        assert!(!results.is_empty());
    }

    #[test]
    fn test_prune_weak_edges() {
        let config = CutAwareConfig::default();
        let mut index = CutAwareHNSW::new(config);

        index.add_edge(0, 1, 1.0);
        index.add_edge(1, 2, 0.1); // Weak edge
        index.add_edge(2, 3, 1.0);

        let pruned = index.prune_weak_edges(0.5);
        assert!(pruned >= 0); // May prune depending on cut
    }

    #[test]
    fn test_metrics_tracking() {
        let config = CutAwareConfig::default();
        let mut index = CutAwareHNSW::new(config);

        for i in 0..5 {
            let vec = create_test_vector(128, i as f32);
            index.insert(i, &vec).unwrap();
        }

        let query = create_test_vector(128, 2.0);
        index.search_gated(&query, 3);
        index.search_ungated(&query, 3);

        let metrics = index.metrics();
        assert!(metrics.searches_performed.load(Ordering::Relaxed) >= 2);
    }

    #[test]
    fn test_export_metrics() {
        let config = CutAwareConfig::default();
        let index = CutAwareHNSW::new(config);

        let json = index.export_metrics();
        assert!(json.is_object());
        assert!(json["searches_performed"].is_number());
    }

    #[test]
    fn test_cut_distribution() {
        let config = CutAwareConfig::default();
        let mut index = CutAwareHNSW::new(config);

        for i in 0..5 {
            let vec = create_test_vector(128, i as f32);
            index.insert(i, &vec).unwrap();
        }

        let dist = index.cut_distribution();
        assert!(!dist.is_empty());
    }

    #[test]
    fn test_path_crosses_weak_cut() {
        let config = CutAwareConfig {
            coherence_gate_threshold: 0.3,
            ..Default::default()
        };
        let mut index = CutAwareHNSW::new(config);

        // Create two clusters
        index.add_edge(0, 1, 1.0);
        index.add_edge(1, 2, 1.0);
        index.add_edge(3, 4, 1.0);
        index.add_edge(2, 3, 0.1); // Weak bridge

        // Force recomputation
        {
            let mut watcher = index.cut_watcher.write().unwrap();
            watcher.min_cut_value();
        }

        // Check crossing
        let crosses = index.path_crosses_weak_cut(0, 4);
        // Result depends on partition computation
        assert!(crosses || !crosses); // Just verify it doesn't panic
    }

    #[test]
    fn test_stoer_wagner_triangle() {
        let mut watcher = DynamicCutWatcher::new();

        // Triangle with equal weights
        watcher.add_edge(0, 1, 1.0);
        watcher.add_edge(1, 2, 1.0);
        watcher.add_edge(2, 0, 1.0);

        let min_cut = watcher.min_cut_value();
        assert!((min_cut - 2.0).abs() < 0.01); // Cut should be 2.0
    }

    #[test]
    fn test_boundary_edge_tracking() {
        let mut watcher = DynamicCutWatcher::new();

        // Two components with single bridge
        watcher.add_edge(0, 1, 1.0);
        watcher.add_edge(1, 0, 1.0);
        watcher.add_edge(2, 3, 1.0);
        watcher.add_edge(3, 2, 1.0);
        watcher.add_edge(1, 2, 0.5); // Bridge

        let _ = watcher.min_cut_value();
        let boundary = watcher.boundary_edges();

        // Should identify bridge edge
        assert!(!boundary.is_empty());
    }

    #[test]
    fn test_reset_metrics() {
        let config = CutAwareConfig::default();
        let index = CutAwareHNSW::new(config);

        index.metrics.searches_performed.store(100, Ordering::Relaxed);
        index.reset_metrics();

        assert_eq!(index.metrics.searches_performed.load(Ordering::Relaxed), 0);
    }
}