wm-tools 9.1.9

Curated tool implementations for the WhiteMagic MCP server.
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
//! Network tools — association mining, pattern detection, network analysis.
//!
//! Tools:
//! - `association.mine` — Cross-galaxy association mining using keyword overlap
//! - `pattern.detect` — Detect structural patterns (hubs, bridges, chains) in the association graph
//! - `emergence.report` — Detailed emergence analysis from tag frequency distribution
//! - `network.stats` — Global network statistics (nodes, edges, density, degree distribution)
//! - `network.centrality` — Degree centrality metrics for memories in the association graph
//! - `network.clusters` — Identify connected components / clusters in the association graph

#![forbid(unsafe_code)]

use async_trait::async_trait;

use serde_json::{Value, json};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use wm_core::{Context, EffectRow, Galaxy, Gana, Resource, Tool, ToolStats};
use wm_memory::{AssociationStore, MemoryStore};

use super::common::{galaxy_name, parse_galaxy};

/// `association.mine` — Cross-galaxy association mining.
///
/// Scans memories across all memory-storing galaxies and proposes associations
/// based on keyword overlap (Jaccard similarity). Unlike `memory.associate_mine`
/// which works within a single galaxy, this tool works across all galaxies.
pub struct AssociationMineTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

impl AssociationMineTool {
    pub fn new(store: Arc<MemoryStore>) -> Self {
        Self {
            store,
            stats: ToolStats::default(),
            effects: EffectRow {
                writes: vec![Resource::Galaxy("associations".into())],
                ..Default::default()
            },
        }
    }
}

#[async_trait]
impl Tool for AssociationMineTool {
    fn name(&self) -> &str {
        "association.mine"
    }
    fn gana(&self) -> Gana {
        Gana::Net
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Mine cross-galaxy associations using keyword overlap analysis"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let min_strength = args
            .get("min_strength")
            .and_then(Value::as_f64)
            .unwrap_or(0.3) as f32;
        let limit_per_galaxy = args
            .get("limit")
            .and_then(serde_json::Value::as_u64)
            .unwrap_or(100) as usize;
        let max_comparisons = args
            .get("max_comparisons")
            .and_then(serde_json::Value::as_u64)
            .unwrap_or(50_000) as usize;

        let env = self.store.env();
        let assoc_store = AssociationStore::open(env)?;

        // Collect memories from all memory-storing galaxies
        let mut all_memories: Vec<(Galaxy, &str, wm_memory::Memory)> = Vec::new();
        for galaxy in Galaxy::memory_galaxies() {
            let mems = self.store.scan(galaxy, limit_per_galaxy)?;
            for mem in mems {
                all_memories.push((galaxy, galaxy_name(galaxy), mem));
            }
        }

        let mut proposed = 0u32;
        let mut cross_galaxy_links = 0u32;
        let mut same_galaxy_links = 0u32;
        let mut comparisons = 0u32;

        for i in 0..all_memories.len() {
            if comparisons as usize >= max_comparisons {
                break;
            }
            for j in (i + 1)..all_memories.len() {
                comparisons += 1;
                if comparisons as usize >= max_comparisons {
                    break;
                }
                let (g1, _, ref a) = all_memories[i];
                let (g2, _, ref b) = all_memories[j];
                let a_words: HashSet<&str> = a.content.split_whitespace().collect();
                let b_words: HashSet<&str> = b.content.split_whitespace().collect();
                let intersection = a_words.intersection(&b_words).count();
                let union = a_words.union(&b_words).count();
                if union > 0 && intersection > 2 {
                    let strength = intersection as f32 / union as f32;
                    if strength > min_strength {
                        // Check if association already exists
                        if assoc_store
                            .get(env, a.metadata.id, b.metadata.id)
                            .ok()
                            .flatten()
                            .is_none()
                        {
                            let assoc = wm_memory::Association::new(
                                a.metadata.id,
                                b.metadata.id,
                                wm_memory::LinkType::Related,
                                strength,
                            );
                            let _ = assoc_store.put(env, &assoc);
                            proposed += 1;
                            if g1 == g2 {
                                same_galaxy_links += 1;
                            } else {
                                cross_galaxy_links += 1;
                            }
                        }
                    }
                }
            }
        }

        Ok(json!({
            "status": "success",
            "memories_scanned": all_memories.len(),
            "proposed_associations": proposed,
            "cross_galaxy_links": cross_galaxy_links,
            "same_galaxy_links": same_galaxy_links,
            "min_strength": min_strength,
            "comparisons": comparisons,
            "truncated": comparisons as usize >= max_comparisons,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `pattern.detect` — Detect structural patterns in the association graph.
///
/// Identifies hubs (high-degree nodes), bridges (nodes connecting clusters),
/// and chains (temporal sequences) in the association network.
pub struct PatternDetectTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

impl PatternDetectTool {
    pub fn new(store: Arc<MemoryStore>) -> Self {
        Self {
            store,
            stats: ToolStats::default(),
            effects: EffectRow::pure(),
        }
    }
}

#[async_trait]
impl Tool for PatternDetectTool {
    fn name(&self) -> &str {
        "pattern.detect"
    }
    fn gana(&self) -> Gana {
        Gana::Net
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Detect structural patterns (hubs, bridges, chains) in the association graph"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let top_k = args
            .get("top_k")
            .and_then(serde_json::Value::as_u64)
            .unwrap_or(10) as usize;

        let env = self.store.env();
        let assoc_store = AssociationStore::open(env)?;

        // Build degree map: UUID -> (in_degree, out_degree)
        let mut degree_map: HashMap<uuid::Uuid, (u32, u32)> = HashMap::new();
        let mut all_assocs: Vec<wm_memory::Association> = Vec::new();

        // Scan all associations by iterating through all memories
        for galaxy in Galaxy::memory_galaxies() {
            let mems = self.store.scan(galaxy, 10000)?;
            for mem in mems {
                let outgoing = assoc_store
                    .find_from(env, mem.metadata.id)
                    .unwrap_or_default();
                let incoming = assoc_store
                    .find_to(env, mem.metadata.id)
                    .unwrap_or_default();
                let out_deg = outgoing.len() as u32;
                let in_deg = incoming.len() as u32;
                if out_deg > 0 || in_deg > 0 {
                    degree_map.insert(mem.metadata.id, (in_deg, out_deg));
                }
                all_assocs.extend(outgoing);
                all_assocs.extend(incoming);
            }
        }

        // Detect hubs: nodes with high total degree
        let mut hubs: Vec<(uuid::Uuid, u32)> = degree_map
            .iter()
            .map(|(id, (ind, outd))| (*id, ind + outd))
            .collect();
        hubs.sort_by_key(|x| std::cmp::Reverse(x.1));
        let top_hubs: Vec<Value> = hubs
            .iter()
            .take(top_k)
            .map(|(id, deg)| {
                json!({
                    "memory_id": id,
                    "total_degree": deg,
                })
            })
            .collect();

        // Detect chains: temporal sequences (A -> B -> C where link_type is Temporal)
        let mut chains: Vec<Value> = Vec::new();
        let temporal_assocs: Vec<&wm_memory::Association> = all_assocs
            .iter()
            .filter(|a| a.link_type == wm_memory::LinkType::Temporal)
            .collect();
        let mut temporal_map: HashMap<uuid::Uuid, uuid::Uuid> = HashMap::new();
        for a in &temporal_assocs {
            temporal_map.insert(a.source, a.target);
        }
        for a in &temporal_assocs {
            let mut chain = vec![a.source, a.target];
            let mut current = a.target;
            while let Some(&next) = temporal_map.get(&current) {
                if chain.contains(&next) {
                    break;
                }
                chain.push(next);
                current = next;
            }
            if chain.len() >= 3 {
                chains.push(json!({
                    "chain": chain.iter().map(std::string::ToString::to_string).collect::<Vec<_>>(),
                    "length": chain.len(),
                }));
            }
        }

        // Detect bridges: nodes that appear in many cross-galaxy associations
        let mut bridge_counts: HashMap<uuid::Uuid, u32> = HashMap::new();
        for a in &all_assocs {
            let source_galaxy = self.find_memory_galaxy(a.source);
            let target_galaxy = self.find_memory_galaxy(a.target);
            if let (Some(sg), Some(tg)) = (source_galaxy, target_galaxy) {
                if sg != tg {
                    *bridge_counts.entry(a.source).or_insert(0) += 1;
                    *bridge_counts.entry(a.target).or_insert(0) += 1;
                }
            }
        }
        let mut bridges: Vec<(uuid::Uuid, u32)> = bridge_counts.into_iter().collect();
        bridges.sort_by_key(|x| std::cmp::Reverse(x.1));
        let top_bridges: Vec<Value> = bridges
            .iter()
            .take(top_k)
            .map(|(id, count)| {
                json!({
                    "memory_id": id,
                    "cross_galaxy_links": count,
                })
            })
            .collect();

        Ok(json!({
            "status": "success",
            "total_nodes": degree_map.len(),
            "total_edges": all_assocs.len(),
            "hubs": top_hubs,
            "chains": chains,
            "bridges": top_bridges,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

impl PatternDetectTool {
    fn find_memory_galaxy(&self, id: uuid::Uuid) -> Option<Galaxy> {
        Galaxy::memory_galaxies()
            .into_iter()
            .find(|&galaxy| self.store.get(galaxy, id).ok().flatten().is_some())
    }
}

/// `emergence.report` — Detailed emergence analysis from tag frequency distribution.
///
/// Scans all memories and computes tag frequency distribution, identifying
/// emerging tags (frequency increasing), dominant tags (high frequency),
/// and declining tags (low frequency relative to total).
pub struct EmergenceReportTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

impl EmergenceReportTool {
    pub fn new(store: Arc<MemoryStore>) -> Self {
        Self {
            store,
            stats: ToolStats::default(),
            effects: EffectRow::pure(),
        }
    }
}

#[async_trait]
impl Tool for EmergenceReportTool {
    fn name(&self) -> &str {
        "emergence.report"
    }
    fn gana(&self) -> Gana {
        Gana::Net
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Detailed emergence analysis with tag frequency distribution and trend detection"
    }
    async fn call(&self, _ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let mut tag_counts: HashMap<String, usize> = HashMap::new();
        let mut total_memories = 0usize;

        for galaxy in Galaxy::memory_galaxies() {
            let mems = self.store.scan(galaxy, 10000)?;
            for mem in mems {
                total_memories += 1;
                for tag in &mem.metadata.tags {
                    *tag_counts.entry(tag.clone()).or_insert(0) += 1;
                }
            }
        }

        let total_tags: usize = tag_counts.values().sum();
        let unique_tags = tag_counts.len();

        // Sort tags by frequency
        let mut sorted_tags: Vec<(String, usize)> = tag_counts.into_iter().collect();
        sorted_tags.sort_by_key(|x| std::cmp::Reverse(x.1));

        let dominant: Vec<Value> = sorted_tags
            .iter()
            .filter(|(_, count)| *count as f64 / total_memories.max(1) as f64 > 0.1)
            .take(20)
            .map(|(tag, count)| {
                json!({
                    "tag": tag,
                    "count": count,
                    "frequency": (*count as f64 / total_memories.max(1) as f64 * 100.0).round() / 100.0,
                })
            })
            .collect();

        let emerging: Vec<Value> = sorted_tags
            .iter()
            .filter(|(_, count)| *count >= 2 && *count <= 5)
            .take(20)
            .map(|(tag, count)| {
                json!({
                    "tag": tag,
                    "count": count,
                })
            })
            .collect();

        let rare: Vec<Value> = sorted_tags
            .iter()
            .filter(|(_, count)| *count == 1)
            .take(20)
            .map(|(tag, _)| json!({"tag": tag}))
            .collect();

        Ok(json!({
            "status": "success",
            "total_memories": total_memories,
            "unique_tags": unique_tags,
            "total_tag_instances": total_tags,
            "dominant_tags": dominant,
            "emerging_tags": emerging,
            "rare_tags": rare,
            "tag_diversity": (unique_tags as f64 / total_memories.max(1) as f64 * 100.0).round() / 100.0,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `network.stats` — Global network statistics.
///
/// Computes nodes, edges, density, degree distribution, and link type breakdown
/// for the entire association graph.
pub struct NetworkStatsTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

impl NetworkStatsTool {
    pub fn new(store: Arc<MemoryStore>) -> Self {
        Self {
            store,
            stats: ToolStats::default(),
            effects: EffectRow::pure(),
        }
    }
}

#[async_trait]
impl Tool for NetworkStatsTool {
    fn name(&self) -> &str {
        "network.stats"
    }
    fn gana(&self) -> Gana {
        Gana::Net
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Global association network statistics (nodes, edges, density, degree distribution)"
    }
    async fn call(&self, _ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let env = self.store.env();
        let assoc_store = AssociationStore::open(env)?;

        let total_edges = assoc_store.count(env)?;

        // Build degree map
        let mut degree_map: HashMap<uuid::Uuid, (u32, u32)> = HashMap::new();
        let mut link_type_counts: HashMap<&'static str, u32> = HashMap::new();
        let mut total_weight: f32 = 0.0;

        for galaxy in Galaxy::memory_galaxies() {
            let mems = self.store.scan(galaxy, 10000)?;
            for mem in mems {
                let outgoing = assoc_store
                    .find_from(env, mem.metadata.id)
                    .unwrap_or_default();
                let incoming = assoc_store
                    .find_to(env, mem.metadata.id)
                    .unwrap_or_default();
                let out_deg = outgoing.len() as u32;
                let in_deg = incoming.len() as u32;
                if out_deg > 0 || in_deg > 0 {
                    let entry = degree_map.entry(mem.metadata.id).or_insert((0, 0));
                    entry.0 += in_deg;
                    entry.1 += out_deg;
                }
                for a in &outgoing {
                    *link_type_counts.entry(a.link_type.as_str()).or_insert(0) += 1;
                    total_weight += a.weight;
                }
            }
        }

        let total_nodes = degree_map.len();
        let max_possible_edges = if total_nodes > 1 {
            total_nodes * (total_nodes - 1)
        } else {
            0
        };
        let density = if max_possible_edges > 0 {
            total_edges as f64 / max_possible_edges as f64
        } else {
            0.0
        };

        // Degree distribution
        let mut degree_distribution: Vec<u32> =
            degree_map.values().map(|(ind, outd)| ind + outd).collect();
        degree_distribution.sort_unstable();
        let avg_degree = if degree_distribution.is_empty() {
            0.0
        } else {
            f64::from(degree_distribution.iter().sum::<u32>()) / degree_distribution.len() as f64
        };
        let max_degree = degree_distribution.iter().copied().max().unwrap_or(0);

        let link_breakdown: Vec<Value> = link_type_counts
            .iter()
            .map(|(lt, count)| {
                json!({
                    "link_type": lt,
                    "count": count,
                })
            })
            .collect();

        let avg_weight = if total_edges > 0 {
            total_weight / total_edges as f32
        } else {
            0.0
        };

        Ok(json!({
            "status": "success",
            "nodes": total_nodes,
            "edges": total_edges,
            "density": (density * 10000.0).round() / 10000.0,
            "avg_degree": (avg_degree * 100.0).round() / 100.0,
            "max_degree": max_degree,
            "avg_weight": (avg_weight * 100.0).round() / 100.0,
            "link_type_breakdown": link_breakdown,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `network.centrality` — Degree centrality metrics.
///
/// Computes in-degree, out-degree, and total degree centrality for each node
/// in the association graph. Returns top-K nodes by centrality.
pub struct NetworkCentralityTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

impl NetworkCentralityTool {
    pub fn new(store: Arc<MemoryStore>) -> Self {
        Self {
            store,
            stats: ToolStats::default(),
            effects: EffectRow::pure(),
        }
    }
}

#[async_trait]
impl Tool for NetworkCentralityTool {
    fn name(&self) -> &str {
        "network.centrality"
    }
    fn gana(&self) -> Gana {
        Gana::Net
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Compute degree centrality metrics for memories in the association graph"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let top_k = args
            .get("top_k")
            .and_then(serde_json::Value::as_u64)
            .unwrap_or(20) as usize;
        let galaxy_str = args.get("galaxy").and_then(Value::as_str);

        let env = self.store.env();
        let assoc_store = AssociationStore::open(env)?;

        let galaxies: Vec<Galaxy> = match galaxy_str {
            Some(g) => vec![parse_galaxy(g)?],
            None => Galaxy::memory_galaxies().to_vec(),
        };

        let mut centrality: Vec<(uuid::Uuid, u32, u32, u32)> = Vec::new();

        for galaxy in &galaxies {
            let mems = self.store.scan(*galaxy, 10000)?;
            for mem in mems {
                let outgoing = assoc_store
                    .find_from(env, mem.metadata.id)
                    .unwrap_or_default();
                let incoming = assoc_store
                    .find_to(env, mem.metadata.id)
                    .unwrap_or_default();
                let out_deg = outgoing.len() as u32;
                let in_deg = incoming.len() as u32;
                if out_deg > 0 || in_deg > 0 {
                    centrality.push((mem.metadata.id, in_deg, out_deg, in_deg + out_deg));
                }
            }
        }

        centrality.sort_by_key(|x| std::cmp::Reverse(x.3));

        let max_degree = centrality.first().map_or(1, |c| c.3);

        let top_nodes: Vec<Value> = centrality
            .iter()
            .take(top_k)
            .map(|(id, ind, outd, total)| {
                let centrality_score = if max_degree > 0 {
                    f64::from(*total) / f64::from(max_degree)
                } else {
                    0.0
                };
                json!({
                    "memory_id": id,
                    "in_degree": ind,
                    "out_degree": outd,
                    "total_degree": total,
                    "centrality": (centrality_score * 1000.0).round() / 1000.0,
                })
            })
            .collect();

        Ok(json!({
            "status": "success",
            "total_nodes_with_edges": centrality.len(),
            "max_degree": max_degree,
            "top_nodes": top_nodes,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `network.clusters` — Identify connected components in the association graph.
///
/// Uses Union-Find to identify clusters of memories connected by associations.
/// Returns cluster sizes, largest clusters, and isolated node count.
pub struct NetworkClustersTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

impl NetworkClustersTool {
    pub fn new(store: Arc<MemoryStore>) -> Self {
        Self {
            store,
            stats: ToolStats::default(),
            effects: EffectRow::pure(),
        }
    }
}

#[async_trait]
impl Tool for NetworkClustersTool {
    fn name(&self) -> &str {
        "network.clusters"
    }
    fn gana(&self) -> Gana {
        Gana::Net
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Identify connected components and clusters in the association graph"
    }
    async fn call(&self, _ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let env = self.store.env();
        let assoc_store = AssociationStore::open(env)?;

        // Collect all nodes and edges
        let mut node_set: HashSet<uuid::Uuid> = HashSet::new();
        let mut edges: Vec<(uuid::Uuid, uuid::Uuid)> = Vec::new();

        for galaxy in Galaxy::memory_galaxies() {
            let mems = self.store.scan(galaxy, 10000)?;
            for mem in mems {
                let outgoing = assoc_store
                    .find_from(env, mem.metadata.id)
                    .unwrap_or_default();
                for a in outgoing {
                    node_set.insert(a.source);
                    node_set.insert(a.target);
                    edges.push((a.source, a.target));
                }
            }
        }

        // Union-Find
        let mut parent: HashMap<uuid::Uuid, uuid::Uuid> = HashMap::new();
        for &node in &node_set {
            parent.insert(node, node);
        }

        fn find(parent: &mut HashMap<uuid::Uuid, uuid::Uuid>, x: uuid::Uuid) -> uuid::Uuid {
            let mut current = x;
            while parent[&current] != current {
                let p = parent[&current];
                parent.insert(current, p);
                current = p;
            }
            current
        }

        for (a, b) in &edges {
            let ra = find(&mut parent, *a);
            let rb = find(&mut parent, *b);
            if ra != rb {
                parent.insert(ra, rb);
            }
        }

        // Count cluster sizes
        let mut cluster_sizes: HashMap<uuid::Uuid, usize> = HashMap::new();
        for &node in &node_set {
            let root = find(&mut parent, node);
            *cluster_sizes.entry(root).or_insert(0) += 1;
        }

        let mut sizes: Vec<usize> = cluster_sizes.values().copied().collect();
        sizes.sort_by(|a, b| b.cmp(a));

        let num_clusters = sizes.len();
        let largest_cluster = sizes.first().copied().unwrap_or(0);
        let isolated_nodes = sizes.iter().filter(|&&s| s == 1).count();
        let multi_node_clusters = sizes.iter().filter(|&&s| s > 1).count();

        let top_clusters: Vec<Value> = sizes
            .iter()
            .take(10)
            .enumerate()
            .map(|(i, &size)| {
                json!({
                    "cluster_rank": i + 1,
                    "size": size,
                })
            })
            .collect();

        Ok(json!({
            "status": "success",
            "total_nodes": node_set.len(),
            "total_edges": edges.len(),
            "num_clusters": num_clusters,
            "largest_cluster_size": largest_cluster,
            "isolated_nodes": isolated_nodes,
            "multi_node_clusters": multi_node_clusters,
            "top_clusters": top_clusters,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

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

    fn open_store() -> (tempfile::TempDir, Arc<MemoryStore>) {
        let tmp = tempdir().unwrap();
        let store = MemoryStore::open_default(tmp.path()).unwrap();
        (tmp, Arc::new(store))
    }

    fn make_memory(galaxy: Galaxy, content: &str, tags: &[&str]) -> wm_memory::Memory {
        let mut mem = wm_memory::Memory::new(galaxy, content.to_string());
        mem.metadata.tags = tags.iter().map(std::string::ToString::to_string).collect();
        mem
    }

    #[tokio::test]
    async fn association_mine_basic() {
        let (_tmp, store) = open_store();
        let tool = AssociationMineTool::new(store.clone());

        let m1 = make_memory(
            Galaxy::Codex,
            "rust is a fast systems programming language",
            &["rust", "programming"],
        );
        let m2 = make_memory(
            Galaxy::Codex,
            "rust is a safe systems programming language",
            &["rust", "programming"],
        );
        store.put(Galaxy::Codex, &m1).unwrap();
        store.put(Galaxy::Codex, &m2).unwrap();

        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert!(result["proposed_associations"].as_u64().unwrap() >= 1);
    }

    #[tokio::test]
    async fn association_mine_with_min_strength() {
        let (_tmp, store) = open_store();
        let tool = AssociationMineTool::new(store.clone());

        let m1 = make_memory(Galaxy::Codex, "rust fast systems", &["rust"]);
        let m2 = make_memory(Galaxy::Codex, "python slow scripting", &["python"]);
        store.put(Galaxy::Codex, &m1).unwrap();
        store.put(Galaxy::Codex, &m2).unwrap();

        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool
            .call(&mut ctx, json!({"min_strength": 0.9}))
            .await
            .unwrap();
        assert_eq!(result["proposed_associations"], 0);
    }

    #[tokio::test]
    async fn pattern_detect_empty_graph() {
        let (_tmp, store) = open_store();
        let tool = PatternDetectTool::new(store);
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert_eq!(result["total_nodes"], 0);
    }

    #[tokio::test]
    async fn pattern_detect_finds_hubs() {
        let (_tmp, store) = open_store();
        let env = store.env();
        let assoc_store = AssociationStore::open(env).unwrap();

        let m1 = make_memory(Galaxy::Codex, "hub memory about rust", &["rust"]);
        let m2 = make_memory(Galaxy::Codex, "spoke one about rust", &["rust"]);
        let m3 = make_memory(Galaxy::Codex, "spoke two about rust", &["rust"]);
        store.put(Galaxy::Codex, &m1).unwrap();
        store.put(Galaxy::Codex, &m2).unwrap();
        store.put(Galaxy::Codex, &m3).unwrap();

        assoc_store
            .put(
                env,
                &wm_memory::Association::new(
                    m1.metadata.id,
                    m2.metadata.id,
                    wm_memory::LinkType::Related,
                    0.8,
                ),
            )
            .unwrap();
        assoc_store
            .put(
                env,
                &wm_memory::Association::new(
                    m1.metadata.id,
                    m3.metadata.id,
                    wm_memory::LinkType::Related,
                    0.7,
                ),
            )
            .unwrap();

        let tool = PatternDetectTool::new(store.clone());
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert!(result["total_nodes"].as_u64().unwrap() >= 3);
        let hubs = result["hubs"].as_array().unwrap();
        assert!(!hubs.is_empty());
        let first_hub_degree = hubs[0]["total_degree"].as_u64().unwrap();
        assert!(first_hub_degree >= 2);
    }

    #[tokio::test]
    async fn emergence_report_basic() {
        let (_tmp, store) = open_store();
        let tool = EmergenceReportTool::new(store.clone());

        store
            .put(
                Galaxy::Codex,
                &make_memory(Galaxy::Codex, "rust fact", &["rust", "programming"]),
            )
            .unwrap();
        store
            .put(
                Galaxy::Codex,
                &make_memory(Galaxy::Codex, "python fact", &["python", "programming"]),
            )
            .unwrap();
        store
            .put(
                Galaxy::Codex,
                &make_memory(Galaxy::Codex, "rust again", &["rust"]),
            )
            .unwrap();

        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert_eq!(result["total_memories"], 3);
        assert!(result["unique_tags"].as_u64().unwrap() >= 2);
    }

    #[tokio::test]
    async fn emergence_report_empty() {
        let (_tmp, store) = open_store();
        let tool = EmergenceReportTool::new(store);
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert_eq!(result["total_memories"], 0);
    }

    #[tokio::test]
    async fn network_stats_empty() {
        let (_tmp, store) = open_store();
        let tool = NetworkStatsTool::new(store);
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert_eq!(result["nodes"], 0);
        assert_eq!(result["edges"], 0);
    }

    #[tokio::test]
    async fn network_stats_with_edges() {
        let (_tmp, store) = open_store();
        let env = store.env();
        let assoc_store = AssociationStore::open(env).unwrap();

        let m1 = make_memory(Galaxy::Codex, "memory one", &["test"]);
        let m2 = make_memory(Galaxy::Codex, "memory two", &["test"]);
        store.put(Galaxy::Codex, &m1).unwrap();
        store.put(Galaxy::Codex, &m2).unwrap();

        assoc_store
            .put(
                env,
                &wm_memory::Association::new(
                    m1.metadata.id,
                    m2.metadata.id,
                    wm_memory::LinkType::Related,
                    0.5,
                ),
            )
            .unwrap();

        let tool = NetworkStatsTool::new(store.clone());
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert_eq!(result["edges"], 1);
        assert!(result["nodes"].as_u64().unwrap() >= 2);
    }

    #[tokio::test]
    async fn network_centrality_basic() {
        let (_tmp, store) = open_store();
        let env = store.env();
        let assoc_store = AssociationStore::open(env).unwrap();

        let m1 = make_memory(Galaxy::Codex, "hub memory", &["hub"]);
        let m2 = make_memory(Galaxy::Codex, "spoke one", &["spoke"]);
        let m3 = make_memory(Galaxy::Codex, "spoke two", &["spoke"]);
        store.put(Galaxy::Codex, &m1).unwrap();
        store.put(Galaxy::Codex, &m2).unwrap();
        store.put(Galaxy::Codex, &m3).unwrap();

        assoc_store
            .put(
                env,
                &wm_memory::Association::new(
                    m1.metadata.id,
                    m2.metadata.id,
                    wm_memory::LinkType::Related,
                    0.8,
                ),
            )
            .unwrap();
        assoc_store
            .put(
                env,
                &wm_memory::Association::new(
                    m1.metadata.id,
                    m3.metadata.id,
                    wm_memory::LinkType::Related,
                    0.7,
                ),
            )
            .unwrap();

        let tool = NetworkCentralityTool::new(store.clone());
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({"top_k": 5})).await.unwrap();
        assert_eq!(result["status"], "success");
        let nodes = result["top_nodes"].as_array().unwrap();
        assert!(!nodes.is_empty());
        let first_degree = nodes[0]["total_degree"].as_u64().unwrap();
        assert!(first_degree >= 2);
    }

    #[tokio::test]
    async fn network_centrality_empty() {
        let (_tmp, store) = open_store();
        let tool = NetworkCentralityTool::new(store);
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert_eq!(result["total_nodes_with_edges"], 0);
    }

    #[tokio::test]
    async fn network_clusters_empty() {
        let (_tmp, store) = open_store();
        let tool = NetworkClustersTool::new(store);
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert_eq!(result["total_nodes"], 0);
        assert_eq!(result["num_clusters"], 0);
    }

    #[tokio::test]
    async fn network_clusters_connected() {
        let (_tmp, store) = open_store();
        let env = store.env();
        let assoc_store = AssociationStore::open(env).unwrap();

        let m1 = make_memory(Galaxy::Codex, "node one", &["cluster"]);
        let m2 = make_memory(Galaxy::Codex, "node two", &["cluster"]);
        let m3 = make_memory(Galaxy::Codex, "node three", &["cluster"]);
        store.put(Galaxy::Codex, &m1).unwrap();
        store.put(Galaxy::Codex, &m2).unwrap();
        store.put(Galaxy::Codex, &m3).unwrap();

        // m1 -> m2 -> m3 (all connected)
        assoc_store
            .put(
                env,
                &wm_memory::Association::new(
                    m1.metadata.id,
                    m2.metadata.id,
                    wm_memory::LinkType::Related,
                    0.5,
                ),
            )
            .unwrap();
        assoc_store
            .put(
                env,
                &wm_memory::Association::new(
                    m2.metadata.id,
                    m3.metadata.id,
                    wm_memory::LinkType::Related,
                    0.5,
                ),
            )
            .unwrap();

        let tool = NetworkClustersTool::new(store.clone());
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert_eq!(result["num_clusters"], 1);
        assert_eq!(result["largest_cluster_size"], 3);
        assert_eq!(result["isolated_nodes"], 0);
    }

    #[tokio::test]
    async fn network_clusters_isolated() {
        let (_tmp, store) = open_store();
        let env = store.env();
        let assoc_store = AssociationStore::open(env).unwrap();

        let m1 = make_memory(Galaxy::Codex, "node one", &["solo"]);
        let m2 = make_memory(Galaxy::Codex, "node two", &["solo"]);
        store.put(Galaxy::Codex, &m1).unwrap();
        store.put(Galaxy::Codex, &m2).unwrap();

        // Only one edge — m1 and m2 connected, no isolated nodes
        assoc_store
            .put(
                env,
                &wm_memory::Association::new(
                    m1.metadata.id,
                    m2.metadata.id,
                    wm_memory::LinkType::Related,
                    0.5,
                ),
            )
            .unwrap();

        let tool = NetworkClustersTool::new(store.clone());
        let mut ctx = Context::new(wm_core::BrainWave::Beta);
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["num_clusters"], 1);
        assert_eq!(result["isolated_nodes"], 0);
    }
}