wm-tools 9.1.7

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
//! Consciousness tools — citta.status, citta.reflect, dream.status, dream.trigger,
//! smarana.status, smarana.trace, apotheosis.check, citta.history, dream.analyze,
//! consciousness.depth.

#![forbid(unsafe_code)]

use async_trait::async_trait;

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

pub struct CittaStatusTool {
    stats: ToolStats,
    effects: EffectRow,
}

impl CittaStatusTool {
    #[must_use]
    pub fn new() -> Self {
        Self {
            stats: ToolStats::default(),
            effects: EffectRow::pure(),
        }
    }
}

impl Default for CittaStatusTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Tool for CittaStatusTool {
    fn name(&self) -> &str {
        "citta.status"
    }
    fn gana(&self) -> Gana {
        Gana::Ghost
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Current citta (consciousness) vector status"
    }
    async fn call(&self, ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        Ok(json!({
            "status": "success",
            "brain_wave": format!("{:?}", ctx.brain_wave),
            "citta_coherence": ctx.citta_coherence,
            "citta_valence": ctx.citta_valence,
            "karma_debt": ctx.karma_debt,
            "intent_score": ctx.intent_score,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `citta.reflect` — introspection on recent dispatch outcomes.
pub struct CittaReflectTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

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

#[async_trait]
impl Tool for CittaReflectTool {
    fn name(&self) -> &str {
        "citta.reflect"
    }
    fn gana(&self) -> Gana {
        Gana::Ghost
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Reflect on recent citta events and coherence trends"
    }
    async fn call(&self, _ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let citta_mems = self.store.scan(Galaxy::Citta, 50)?;
        let count = citta_mems.len();
        let avg_importance = if count > 0 {
            citta_mems
                .iter()
                .map(|m| m.metadata.importance)
                .sum::<f32>()
                / count as f32
        } else {
            0.0
        };
        Ok(json!({
            "status": "success",
            "citta_memories": count,
            "avg_importance": (avg_importance * 100.0).round() / 100.0,
            "reflection": if count > 10 { "rich inner life" } else if count > 0 { "emerging awareness" } else { "dormant" },
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `dream.status` — dream cycle status.
pub struct DreamStatusTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

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

#[async_trait]
impl Tool for DreamStatusTool {
    fn name(&self) -> &str {
        "dream.status"
    }
    fn gana(&self) -> Gana {
        Gana::Abundance
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Dream cycle status — memories in Dreams galaxy"
    }
    async fn call(&self, _ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let count = self.store.count(Galaxy::Dreams)?;
        Ok(json!({
            "status": "success",
            "dream_memories": count,
            "state": if count > 0 { "has dreamt" } else { "has not dreamt" },
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `dream.trigger` — write a dream trigger marker.
pub struct DreamTriggerTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

impl DreamTriggerTool {
    pub fn new(store: Arc<MemoryStore>) -> Self {
        Self {
            store,
            stats: ToolStats::default(),
            effects: EffectRow {
                writes: vec![Resource::Galaxy("dreams".into())],
                cost: wm_core::CostEstimate {
                    expensive: true,
                    ..Default::default()
                },
                ..Default::default()
            },
        }
    }
}

#[async_trait]
impl Tool for DreamTriggerTool {
    fn name(&self) -> &str {
        "dream.trigger"
    }
    fn gana(&self) -> Gana {
        Gana::Abundance
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Trigger a dream cycle marker — writes to Dreams galaxy"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let reason = args
            .get("reason")
            .and_then(|v| v.as_str())
            .unwrap_or("manual");
        let mut mem = Memory::new(
            Galaxy::Dreams,
            json!({
                "type": "dream_trigger",
                "reason": reason,
            })
            .to_string(),
        );
        mem.metadata.tags = vec!["dream".into(), "trigger".into()];
        mem.metadata.importance = 0.8;
        self.store.put(Galaxy::Dreams, &mem)?;
        Ok(json!({
            "status": "success",
            "trigger_id": mem.metadata.id,
            "reason": reason,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

// ── Tier 5 Ghost Tools ─────────────────────────────────────────────

/// `smarana.status` — Retention score and recall statistics.
///
/// Reports the current smarana (memory retention) state from the Context
/// and recent citta memories that recorded recall/miss events.
pub struct SmaranaStatusTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

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

#[async_trait]
impl Tool for SmaranaStatusTool {
    fn name(&self) -> &str {
        "smarana.status"
    }
    fn gana(&self) -> Gana {
        Gana::Ghost
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Retention score and recall statistics from smarana (memory retention)"
    }
    async fn call(&self, ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let citta_mems = self.store.scan(Galaxy::Citta, 100)?;
        let recall_events = citta_mems
            .iter()
            .filter(|m| m.metadata.tags.iter().any(|t| t == "recall" || t == "miss"))
            .count();
        let recall_successes = citta_mems
            .iter()
            .filter(|m| m.metadata.tags.iter().any(|t| t == "recall"))
            .count();
        let retention_score = if recall_events > 0 {
            recall_successes as f32 / recall_events as f32
        } else {
            1.0
        };
        Ok(json!({
            "status": "success",
            "retention_score": (retention_score * 100.0).round() / 100.0,
            "recall_events": recall_events,
            "successful_recalls": recall_successes,
            "citta_coherence": ctx.citta_coherence,
            "citta_valence": ctx.citta_valence,
            "interpretation": if retention_score > 0.8 { "excellent retention" } else if retention_score > 0.5 { "moderate retention" } else if recall_events > 0 { "poor retention" } else { "no recall data" },
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `smarana.trace` — Trace retention decay over time.
///
/// Analyzes citta memories over time to show how retention score has
/// changed, providing a temporal trace of memory retention quality.
pub struct SmaranaTraceTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

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

#[async_trait]
impl Tool for SmaranaTraceTool {
    fn name(&self) -> &str {
        "smarana.trace"
    }
    fn gana(&self) -> Gana {
        Gana::Ghost
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Trace retention decay over time from citta memory history"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let limit = args
            .get("limit")
            .and_then(serde_json::Value::as_u64)
            .unwrap_or(50) as usize;
        let citta_mems = self.store.scan(Galaxy::Citta, limit)?;

        let mut trace: Vec<Value> = Vec::new();
        let mut running_recalls = 0u32;
        let mut running_misses = 0u32;

        for mem in &citta_mems {
            let is_recall = mem.metadata.tags.iter().any(|t| t == "recall");
            let is_miss = mem.metadata.tags.iter().any(|t| t == "miss");
            if is_recall {
                running_recalls += 1;
            }
            if is_miss {
                running_misses += 1;
            }
            if is_recall || is_miss {
                let total = running_recalls + running_misses;
                let score = if total > 0 {
                    running_recalls as f32 / total as f32
                } else {
                    1.0
                };
                trace.push(json!({
                    "memory_id": mem.metadata.id,
                    "timestamp": mem.metadata.created_at.to_rfc3339(),
                    "event": if is_recall { "recall" } else { "miss" },
                    "running_score": (score * 100.0).round() / 100.0,
                    "running_recalls": running_recalls,
                    "running_misses": running_misses,
                }));
            }
        }

        let current_score = if running_recalls + running_misses > 0 {
            running_recalls as f32 / (running_recalls + running_misses) as f32
        } else {
            1.0
        };

        Ok(json!({
            "status": "success",
            "trace_points": trace.len(),
            "current_retention_score": (current_score * 100.0).round() / 100.0,
            "total_recalls": running_recalls,
            "total_misses": running_misses,
            "trace": trace,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `apotheosis.check` — Self-improvement trend check.
///
/// Reports the current apotheosis score, trend direction, and improvement
/// status based on recent citta memory analysis.
pub struct ApotheosisCheckTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

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

#[async_trait]
impl Tool for ApotheosisCheckTool {
    fn name(&self) -> &str {
        "apotheosis.check"
    }
    fn gana(&self) -> Gana {
        Gana::Ghost
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Self-improvement trend check with apotheosis score and direction"
    }
    async fn call(&self, ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let citta_mems = self.store.scan(Galaxy::Citta, 200)?;

        // Compute a proxy apotheosis score from citta memory quality
        let total = citta_mems.len();
        let high_importance = citta_mems
            .iter()
            .filter(|m| m.metadata.importance > 0.7)
            .count();
        let avg_importance = if total > 0 {
            citta_mems
                .iter()
                .map(|m| m.metadata.importance)
                .sum::<f32>()
                / total as f32
        } else {
            0.5
        };

        // Use coherence from context as the coherence component
        let coherence = ctx.citta_coherence;
        // Estimate effectiveness from recall success (proxy)
        let recall_count = citta_mems
            .iter()
            .filter(|m| m.metadata.tags.iter().any(|t| t == "recall"))
            .count();
        let effectiveness = if total > 0 {
            recall_count as f32 / total as f32
        } else {
            0.5
        };

        // Composite apotheosis score: 0.4 * effectiveness + 0.3 * coherence + 0.3 * avg_importance
        let apotheosis_score =
            (coherence.mul_add(0.3, effectiveness * 0.4) + avg_importance * 0.3).clamp(0.0, 1.0);

        // Compute trend from importance over time (first half vs second half)
        let half = total / 2;
        let first_half_avg = if half > 0 {
            citta_mems[..half]
                .iter()
                .map(|m| m.metadata.importance)
                .sum::<f32>()
                / half as f32
        } else {
            0.0
        };
        let second_half_avg = if total - half > 0 {
            citta_mems[half..]
                .iter()
                .map(|m| m.metadata.importance)
                .sum::<f32>()
                / (total - half) as f32
        } else {
            0.0
        };
        let trend = second_half_avg - first_half_avg;

        Ok(json!({
            "status": "success",
            "apotheosis_score": (apotheosis_score * 1000.0).round() / 1000.0,
            "trend": (trend * 1000.0).round() / 1000.0,
            "improving": trend > 0.01,
            "coherence": (coherence * 100.0).round() / 100.0,
            "effectiveness": (effectiveness * 100.0).round() / 100.0,
            "avg_importance": (avg_importance * 100.0).round() / 100.0,
            "high_importance_count": high_importance,
            "total_citta_memories": total,
            "interpretation": if trend > 0.05 { "strongly improving" } else if trend > 0.01 { "improving" } else if trend > -0.01 { "stable" } else { "declining" },
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `citta.history` — Recent citta heartbeats and valence history.
///
/// Returns recent citta memories with their coherence and valence values,
/// providing a temporal history of consciousness states.
pub struct CittaHistoryTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

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

#[async_trait]
impl Tool for CittaHistoryTool {
    fn name(&self) -> &str {
        "citta.history"
    }
    fn gana(&self) -> Gana {
        Gana::Ghost
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Recent citta heartbeats and valence history from consciousness records"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let limit = args
            .get("limit")
            .and_then(serde_json::Value::as_u64)
            .unwrap_or(50) as usize;
        let citta_mems = self.store.scan(Galaxy::Citta, limit)?;

        let history: Vec<Value> = citta_mems
            .iter()
            .rev()
            .map(|mem| {
                json!({
                    "id": mem.metadata.id,
                    "timestamp": mem.metadata.created_at.to_rfc3339(),
                    "importance": (mem.metadata.importance * 100.0).round() / 100.0,
                    "tags": mem.metadata.tags,
                    "content_preview": mem.content.chars().take(100).collect::<String>(),
                })
            })
            .collect();

        let count = history.len();
        let avg_importance = if count > 0 {
            citta_mems
                .iter()
                .map(|m| m.metadata.importance)
                .sum::<f32>()
                / count as f32
        } else {
            0.0
        };

        Ok(json!({
            "status": "success",
            "count": count,
            "avg_importance": (avg_importance * 100.0).round() / 100.0,
            "history": history,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `dream.analyze` — Analyze dream cycle outputs and consolidation quality.
///
/// Scans the Dreams galaxy for dream memories and analyzes their content,
/// tags, and importance to assess dream cycle quality.
pub struct DreamAnalyzeTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

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

#[async_trait]
impl Tool for DreamAnalyzeTool {
    fn name(&self) -> &str {
        "dream.analyze"
    }
    fn gana(&self) -> Gana {
        Gana::Ghost
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Analyze dream cycle outputs and consolidation quality from Dreams galaxy"
    }
    async fn call(&self, _ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let dream_mems = self.store.scan(Galaxy::Dreams, 200)?;
        let count = dream_mems.len();

        let triggers = dream_mems
            .iter()
            .filter(|m| m.metadata.tags.iter().any(|t| t == "trigger"))
            .count();
        let consolidations = dream_mems
            .iter()
            .filter(|m| m.metadata.tags.iter().any(|t| t == "consolidation"))
            .count();
        let serendipity = dream_mems
            .iter()
            .filter(|m| m.metadata.tags.iter().any(|t| t == "serendipity"))
            .count();

        let avg_importance = if count > 0 {
            dream_mems
                .iter()
                .map(|m| m.metadata.importance)
                .sum::<f32>()
                / count as f32
        } else {
            0.0
        };

        // Collect all tags from dream memories
        let mut tag_counts: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        for mem in &dream_mems {
            for tag in &mem.metadata.tags {
                *tag_counts.entry(tag.clone()).or_insert(0) += 1;
            }
        }
        let mut sorted_tags: Vec<(String, usize)> = tag_counts.into_iter().collect();
        sorted_tags.sort_by_key(|x| std::cmp::Reverse(x.1));
        let top_tags: Vec<Value> = sorted_tags
            .iter()
            .take(10)
            .map(|(tag, count)| json!({"tag": tag, "count": count}))
            .collect();

        // Analyze content types
        let mut type_counts: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        for mem in &dream_mems {
            if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&mem.content) {
                if let Some(t) = parsed.get("type").and_then(|v| v.as_str()) {
                    *type_counts.entry(t.to_string()).or_insert(0) += 1;
                }
            }
        }
        let mut sorted_types: Vec<(String, usize)> = type_counts.into_iter().collect();
        sorted_types.sort_by_key(|x| std::cmp::Reverse(x.1));
        let type_breakdown: Vec<Value> = sorted_types
            .iter()
            .map(|(t, c)| json!({"type": t, "count": c}))
            .collect();

        Ok(json!({
            "status": "success",
            "total_dreams": count,
            "triggers": triggers,
            "consolidations": consolidations,
            "serendipity_events": serendipity,
            "avg_importance": (avg_importance * 100.0).round() / 100.0,
            "top_tags": top_tags,
            "content_types": type_breakdown,
            "quality": if avg_importance > 0.7 { "high quality dreams" } else if avg_importance > 0.4 { "moderate quality" } else if count > 0 { "low quality" } else { "no dreams yet" },
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

/// `consciousness.depth` — Measure depth of consciousness state.
///
/// Computes a composite consciousness depth score from coherence, valence,
/// brain-wave state, and citta memory richness.
pub struct ConsciousnessDepthTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

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

#[async_trait]
impl Tool for ConsciousnessDepthTool {
    fn name(&self) -> &str {
        "consciousness.depth"
    }
    fn gana(&self) -> Gana {
        Gana::Ghost
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "Measure depth of consciousness state from coherence, valence, and citta richness"
    }
    async fn call(&self, ctx: &mut Context, _args: Value) -> wm_core::Result<Value> {
        let citta_mems = self.store.scan(Galaxy::Citta, 200)?;
        let citta_count = citta_mems.len();

        // Brain-wave depth: Gamma=1.0, Beta=0.8, Alpha=0.6, Theta=0.9, Delta=0.3
        let bw_depth = match ctx.brain_wave {
            wm_core::BrainWave::Gamma => 1.0,
            wm_core::BrainWave::Beta => 0.8,
            wm_core::BrainWave::Alpha => 0.6,
            wm_core::BrainWave::Theta => 0.9,
            wm_core::BrainWave::Delta => 0.3,
        };

        // Coherence component (0.0-1.0)
        let coherence = ctx.citta_coherence.clamp(0.0, 1.0);

        // Valence component: absolute value (polarization = depth)
        let valence_depth = ctx.citta_valence.abs().clamp(0.0, 1.0);

        // Citta richness: log-scaled count of citta memories
        let richness = if citta_count > 0 {
            (citta_count as f32).ln_1p() / 10.0
        } else {
            0.0
        };
        let richness = richness.clamp(0.0, 1.0);

        // Composite depth: weighted average
        let depth = (bw_depth * 0.3 + coherence * 0.3 + valence_depth * 0.2 + richness * 0.2)
            .clamp(0.0, 1.0);

        Ok(json!({
            "status": "success",
            "depth_score": (depth * 1000.0).round() / 1000.0,
            "components": {
                "brain_wave_depth": (bw_depth * 100.0).round() / 100.0,
                "coherence": (coherence * 100.0).round() / 100.0,
                "valence_depth": (valence_depth * 100.0).round() / 100.0,
                "citta_richness": (richness * 100.0).round() / 100.0,
            },
            "brain_wave": format!("{:?}", ctx.brain_wave),
            "citta_memories": citta_count,
            "interpretation": if depth > 0.8 { "deep consciousness" } else if depth > 0.6 { "moderate depth" } else if depth > 0.3 { "shallow" } else { "near-dormant" },
        }))
    }
    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))
    }

    #[tokio::test]
    async fn smarana_status_empty() {
        let (_tmp, store) = open_store();
        let tool = SmaranaStatusTool::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["recall_events"], 0);
        assert_eq!(result["retention_score"], 1.0);
    }

    #[tokio::test]
    async fn smarana_status_with_recall_events() {
        let (_tmp, store) = open_store();
        let mut mem = Memory::new(Galaxy::Citta, "recall event".into());
        mem.metadata.tags = vec!["recall".into()];
        store.put(Galaxy::Citta, &mem).unwrap();

        let mut mem2 = Memory::new(Galaxy::Citta, "miss event".into());
        mem2.metadata.tags = vec!["miss".into()];
        store.put(Galaxy::Citta, &mem2).unwrap();

        let tool = SmaranaStatusTool::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["recall_events"], 2);
        assert_eq!(result["successful_recalls"], 1);
    }

    #[tokio::test]
    async fn smarana_trace_basic() {
        let (_tmp, store) = open_store();
        let mut mem = Memory::new(Galaxy::Citta, "recall 1".into());
        mem.metadata.tags = vec!["recall".into()];
        store.put(Galaxy::Citta, &mem).unwrap();

        let tool = SmaranaTraceTool::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!(result["trace_points"].as_u64().unwrap() >= 1);
    }

    #[tokio::test]
    async fn smarana_trace_empty() {
        let (_tmp, store) = open_store();
        let tool = SmaranaTraceTool::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["trace_points"], 0);
    }

    #[tokio::test]
    async fn apotheosis_check_empty() {
        let (_tmp, store) = open_store();
        let tool = ApotheosisCheckTool::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_citta_memories"], 0);
    }

    #[tokio::test]
    async fn apotheosis_check_with_memories() {
        let (_tmp, store) = open_store();
        for i in 0..10 {
            let mut mem = Memory::new(Galaxy::Citta, format!("citta event {i}"));
            mem.metadata.importance = (i as f32).mul_add(0.05, 0.5);
            if i % 3 == 0 {
                mem.metadata.tags = vec!["recall".into()];
            }
            store.put(Galaxy::Citta, &mem).unwrap();
        }

        let tool = ApotheosisCheckTool::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_citta_memories"], 10);
        assert!(result["apotheosis_score"].as_f64().unwrap() > 0.0);
    }

    #[tokio::test]
    async fn citta_history_empty() {
        let (_tmp, store) = open_store();
        let tool = CittaHistoryTool::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["count"], 0);
    }

    #[tokio::test]
    async fn citta_history_with_memories() {
        let (_tmp, store) = open_store();
        let mut mem = Memory::new(Galaxy::Citta, "consciousness event".into());
        mem.metadata.importance = 0.8;
        mem.metadata.tags = vec!["recall".into()];
        store.put(Galaxy::Citta, &mem).unwrap();

        let tool = CittaHistoryTool::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["count"], 1);
        let history = result["history"].as_array().unwrap();
        assert_eq!(history.len(), 1);
    }

    #[tokio::test]
    async fn dream_analyze_empty() {
        let (_tmp, store) = open_store();
        let tool = DreamAnalyzeTool::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_dreams"], 0);
        assert_eq!(result["quality"], "no dreams yet");
    }

    #[tokio::test]
    async fn dream_analyze_with_dreams() {
        let (_tmp, store) = open_store();
        let mut mem = Memory::new(
            Galaxy::Dreams,
            json!({"type": "dream_trigger", "reason": "test"}).to_string(),
        );
        mem.metadata.tags = vec!["dream".into(), "trigger".into()];
        mem.metadata.importance = 0.8;
        store.put(Galaxy::Dreams, &mem).unwrap();

        let mut mem2 = Memory::new(
            Galaxy::Dreams,
            json!({"type": "consolidation", "merged": 3}).to_string(),
        );
        mem2.metadata.tags = vec!["dream".into(), "consolidation".into()];
        mem2.metadata.importance = 0.7;
        store.put(Galaxy::Dreams, &mem2).unwrap();

        let tool = DreamAnalyzeTool::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_dreams"], 2);
        assert_eq!(result["triggers"], 1);
        assert_eq!(result["consolidations"], 1);
    }

    #[tokio::test]
    async fn consciousness_depth_basic() {
        let (_tmp, store) = open_store();
        let tool = ConsciousnessDepthTool::new(store);
        let mut ctx = Context::new(wm_core::BrainWave::Gamma);
        ctx.citta_coherence = 0.9;
        ctx.citta_valence = 0.5;
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert!(result["depth_score"].as_f64().unwrap() > 0.5);
    }

    #[tokio::test]
    async fn consciousness_depth_delta_low() {
        let (_tmp, store) = open_store();
        let tool = ConsciousnessDepthTool::new(store);
        let mut ctx = Context::new(wm_core::BrainWave::Delta);
        ctx.citta_coherence = 0.3;
        ctx.citta_valence = 0.0;
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert!(result["depth_score"].as_f64().unwrap() < 0.5);
    }

    #[tokio::test]
    async fn consciousness_depth_gamma_high() {
        let (_tmp, store) = open_store();
        // Add citta memories for richness (need enough for high richness score)
        for _ in 0..50 {
            let mem = Memory::new(Galaxy::Citta, "event".into());
            store.put(Galaxy::Citta, &mem).unwrap();
        }
        let tool = ConsciousnessDepthTool::new(store);
        let mut ctx = Context::new(wm_core::BrainWave::Gamma);
        ctx.citta_coherence = 1.0;
        ctx.citta_valence = 0.8;
        let result = tool.call(&mut ctx, json!({})).await.unwrap();
        assert_eq!(result["status"], "success");
        assert!(result["depth_score"].as_f64().unwrap() > 0.7);
        assert_eq!(result["interpretation"], "deep consciousness");
    }
}