yantrikdb 0.13.1

Cognitive memory engine for persistent AI systems
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
//! CK-5 Benchmark Suite — Generative Understanding Primitives
//!
//! Categories 27–33, covering the seven CK-5 subsystems:
//!   27. Analogical Reasoning
//!   28. Schema Induction
//!   29. Episodic Narrative Memory
//!   30. Counterfactual Simulator
//!   31. Probabilistic Belief Network
//!   32. Experience Replay / Dream Consolidation
//!   33. Perspective Engine
//!
//! Each category runs 4–7 persona-driven tests that exercise the core
//! pure-function APIs without requiring a database.

use std::collections::HashMap;
use std::time::Instant;

use super::benchmark::{BenchResult, BenchRun, PersonaScenario};
use super::state::*;

// ── Category 27: Analogical Reasoning ────────────────────────────────

pub fn run_analogy_tests(run: &mut BenchRun, scenario: &PersonaScenario) {
    use super::analogy::*;

    let persona = Some(scenario.name.clone());

    // Build subgraph groups from scenario nodes by metadata domain.
    let groups = build_subgraph_groups(&scenario.nodes, &scenario.edges);

    // Test 1: Subgraph group construction.
    let start = Instant::now();
    let group_count = groups.len();
    run.add(BenchResult {
        category: "analogy".into(),
        test_name: "subgraph_grouping".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("groups".into()),
        metric_value: Some(group_count as f64),
        details: Some(format!(
            "nodes={} edges={} groups={}",
            scenario.nodes.len(),
            scenario.edges.len(),
            group_count,
        )),
    });

    // Test 2: Analogical opportunity detection.
    let start = Instant::now();
    let node_refs: Vec<&CognitiveNode> = scenario.nodes.iter().collect();
    let opportunities = detect_analogical_opportunities(&node_refs, &scenario.edges, &groups, 0.3);
    run.add(BenchResult {
        category: "analogy".into(),
        test_name: "opportunity_detection".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("opportunities".into()),
        metric_value: Some(opportunities.len() as f64),
        details: None,
    });

    // Test 3: Find analogies with query.
    let start = Instant::now();
    let mut mappings_found = 0;
    let mut all_quality_valid = true;
    if !groups.is_empty() {
        let query = AnalogicalQuery {
            source_subgraph: groups[0].nodes.iter().map(|n| n.id).collect(),
            source_edges: groups[0].edges.clone(),
            source_domain: groups[0].domain.clone(),
            search_scope: AnalogyScope::CrossDomain,
            min_relational_depth: 1,
            max_results: 10,
        };
        let mappings = find_analogies(&query, &node_refs, &groups);
        for m in &mappings {
            if m.structural_similarity < 0.0 || m.structural_similarity > 1.0 {
                all_quality_valid = false;
            }
        }
        mappings_found = mappings.len();
    }
    run.add(BenchResult {
        category: "analogy".into(),
        test_name: "find_analogies".into(),
        persona: persona.clone(),
        passed: all_quality_valid,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("mappings".into()),
        metric_value: Some(mappings_found as f64),
        details: None,
    });

    // Test 4: Analogy store serialization roundtrip.
    let start = Instant::now();
    let store = AnalogyStore::default();
    let json = serde_json::to_string(&store).unwrap_or_default();
    let restored: Result<AnalogyStore, _> = serde_json::from_str(&json);
    run.add(BenchResult {
        category: "analogy".into(),
        test_name: "store_roundtrip".into(),
        persona: persona.clone(),
        passed: restored.is_ok(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: None,
        metric_value: None,
        details: None,
    });

    // Test 5: Strength decay on empty store.
    let start = Instant::now();
    let now_ms = (now_secs() * 1000.0) as u64;
    let mut store_mut = AnalogyStore::default();
    let report = analogy_strength_decay(&mut store_mut, now_ms, 86_400_000 * 30);
    run.add(BenchResult {
        category: "analogy".into(),
        test_name: "strength_decay".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("remaining".into()),
        metric_value: Some(report.remaining as f64),
        details: Some(format!(
            "pruned_quality={} pruned_stale={}",
            report.pruned_low_quality, report.pruned_stale,
        )),
    });
}

/// Build subgraph groups by clustering nodes by their metadata "domain" tag.
fn build_subgraph_groups(
    nodes: &[CognitiveNode],
    edges: &[CognitiveEdge],
) -> Vec<super::analogy::SubgraphGroup> {
    let mut domain_map: HashMap<String, Vec<&CognitiveNode>> = HashMap::new();
    for node in nodes {
        let domain = node
            .metadata
            .get("domain")
            .and_then(|v| v.as_str())
            .unwrap_or("general")
            .to_string();
        domain_map.entry(domain).or_default().push(node);
    }

    domain_map
        .into_iter()
        .map(|(domain, domain_nodes)| {
            let node_id_set: std::collections::HashSet<NodeId> =
                domain_nodes.iter().map(|n| n.id).collect();
            let domain_edges: Vec<CognitiveEdge> = edges
                .iter()
                .filter(|e| node_id_set.contains(&e.src) && node_id_set.contains(&e.dst))
                .cloned()
                .collect();
            super::analogy::SubgraphGroup {
                domain,
                nodes: domain_nodes.into_iter().cloned().collect(),
                edges: domain_edges,
            }
        })
        .collect()
}

// ── Category 28: Schema Induction ────────────────────────────────────

pub fn run_schema_induction_tests(run: &mut BenchRun, scenario: &PersonaScenario) {
    use super::schema_induction::*;

    let persona = Some(scenario.name.clone());
    let now_ms = (now_secs() * 1000.0) as u64;

    // Build episodes from persona action-schema and task nodes.
    let episodes = build_episodes_from_scenario(&scenario.nodes, now_ms);

    // Test 1: Episode extraction.
    let start = Instant::now();
    let ep_count = episodes.len();
    run.add(BenchResult {
        category: "schema_induction".into(),
        test_name: "episode_extraction".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("episodes".into()),
        metric_value: Some(ep_count as f64),
        details: None,
    });

    // Test 2: Observe episodes into schema store.
    let start = Instant::now();
    let mut store = SchemaStore::default();
    for ep in &episodes {
        observe_episode(ep, &mut store);
    }
    let schema_count = store.len();
    run.add(BenchResult {
        category: "schema_induction".into(),
        test_name: "episode_observation".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("schemas_induced".into()),
        metric_value: Some(schema_count as f64),
        details: Some(format!("episodes_observed={}", ep_count)),
    });

    // Test 3: Schema matching against context.
    let start = Instant::now();
    let mut all_scores_bounded = true;
    let ctx = ContextSnapshot {
        node_kinds_present: scenario
            .nodes
            .iter()
            .map(|n| n.kind())
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect(),
        edge_kinds_present: scenario
            .edges
            .iter()
            .map(|e| (e.kind, Direction::Any))
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect(),
        attributes: HashMap::new(),
        belief_confidences: scenario
            .nodes
            .iter()
            .filter(|n| n.kind() == NodeKind::Belief)
            .map(|n| (n.label.clone(), n.attrs.confidence))
            .collect(),
        now_ms,
    };
    let matches = match_schemas(&ctx, &store);
    for (_, score) in &matches {
        if *score < 0.0 || *score > 1.0 {
            all_scores_bounded = false;
        }
    }
    let total_matches = matches.len();
    run.add(BenchResult {
        category: "schema_induction".into(),
        test_name: "schema_matching".into(),
        persona: persona.clone(),
        passed: all_scores_bounded,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("total_matches".into()),
        metric_value: Some(total_matches as f64),
        details: None,
    });

    // Test 4: Schema maintenance.
    let start = Instant::now();
    let report = schema_maintenance(&mut store, now_ms, 86_400_000 * 90);
    let post_count = store.len();
    run.add(BenchResult {
        category: "schema_induction".into(),
        test_name: "schema_maintenance".into(),
        persona: persona.clone(),
        passed: post_count <= schema_count,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("pruned".into()),
        metric_value: Some(report.pruned_low_confidence as f64),
        details: Some(format!(
            "before={} after={} merged={}",
            schema_count, post_count, report.merged,
        )),
    });

    // Test 5: Store serialization roundtrip.
    let start = Instant::now();
    let json = serde_json::to_string(&store).unwrap_or_default();
    let restored: Result<SchemaStore, _> = serde_json::from_str(&json);
    run.add(BenchResult {
        category: "schema_induction".into(),
        test_name: "store_roundtrip".into(),
        persona: persona.clone(),
        passed: restored.is_ok(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("json_bytes".into()),
        metric_value: Some(json.len() as f64),
        details: None,
    });
}

/// Build synthetic EpisodeData from persona action-schema and task nodes.
fn build_episodes_from_scenario(
    nodes: &[CognitiveNode],
    now_ms: u64,
) -> Vec<super::schema_induction::EpisodeData> {
    use super::schema_induction::{EpisodeData, SchemaCondition};

    let mut episodes = Vec::new();

    for (i, node) in nodes.iter().enumerate() {
        if node.kind() == NodeKind::ActionSchema || node.kind() == NodeKind::Task {
            episodes.push(EpisodeData {
                episode_id: node.id,
                conditions: vec![SchemaCondition::NodeKindPresent(node.kind())],
                action_type: node.label.clone(),
                outcome_positive: i % 3 != 0,
                outcome_description: format!("outcome for {}", node.label),
                outcome_valence: if i % 3 != 0 { 0.7 } else { -0.2 },
                timestamp_ms: now_ms - (i as u64) * 3_600_000,
            });
        }
    }

    episodes
}

// ── Category 29: Episodic Narrative Memory ────────────────────────────

pub fn run_narrative_tests(run: &mut BenchRun, scenario: &PersonaScenario) {
    use super::narrative::*;

    let persona = Some(scenario.name.clone());
    let now_ms = (now_secs() * 1000.0) as u64;

    // Build narrative episodes from persona episode nodes.
    let episodes = build_narrative_episodes(&scenario.nodes, now_ms);

    // Test 1: Episode construction.
    let start = Instant::now();
    let ep_count = episodes.len();
    run.add(BenchResult {
        category: "narrative".into(),
        test_name: "episode_construction".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("episodes".into()),
        metric_value: Some(ep_count as f64),
        details: None,
    });

    // Test 2: Arc assignment.
    let start = Instant::now();
    let mut timeline = AutobiographicalTimeline::default();
    let mut arc_ids = Vec::new();
    for ep in &episodes {
        let arc_id = assign_to_arc(ep, &mut timeline);
        arc_ids.push(arc_id);
    }
    let unique_arcs: std::collections::HashSet<_> = arc_ids.iter().collect();
    run.add(BenchResult {
        category: "narrative".into(),
        test_name: "arc_assignment".into(),
        persona: persona.clone(),
        passed: !arc_ids.is_empty() || episodes.is_empty(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("unique_arcs".into()),
        metric_value: Some(unique_arcs.len() as f64),
        details: Some(format!("episodes={} arcs={}", ep_count, unique_arcs.len())),
    });

    // Test 3: Arc health check.
    let start = Instant::now();
    let alerts = arc_health_check(&timeline, now_ms);
    run.add(BenchResult {
        category: "narrative".into(),
        test_name: "arc_health_check".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("alerts".into()),
        metric_value: Some(alerts.len() as f64),
        details: None,
    });

    // Test 4: Arc summary generation.
    let start = Instant::now();
    let mut summaries_generated = 0;
    for arc in &timeline.arcs {
        let summary = generate_arc_summary(arc);
        if !summary.is_empty() {
            summaries_generated += 1;
        }
    }
    run.add(BenchResult {
        category: "narrative".into(),
        test_name: "arc_summary_generation".into(),
        persona: persona.clone(),
        passed: summaries_generated == timeline.arcs.len(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("summaries".into()),
        metric_value: Some(summaries_generated as f64),
        details: None,
    });

    // Test 5: Timeline query — active arcs.
    let start = Instant::now();
    let result = query_timeline(&timeline, &NarrativeQuery::ActiveArcs);
    run.add(BenchResult {
        category: "narrative".into(),
        test_name: "query_active_arcs".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("active_arcs".into()),
        metric_value: Some(result.arcs.len() as f64),
        details: None,
    });

    // Test 6: Timeline serialization roundtrip.
    let start = Instant::now();
    let json = serde_json::to_string(&timeline).unwrap_or_default();
    let restored: Result<AutobiographicalTimeline, _> = serde_json::from_str(&json);
    run.add(BenchResult {
        category: "narrative".into(),
        test_name: "timeline_roundtrip".into(),
        persona: persona.clone(),
        passed: restored.is_ok(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("json_bytes".into()),
        metric_value: Some(json.len() as f64),
        details: None,
    });
}

/// Build narrative episodes from persona Episode-type nodes.
fn build_narrative_episodes(
    nodes: &[CognitiveNode],
    now_ms: u64,
) -> Vec<super::narrative::NarrativeEpisode> {
    use super::narrative::NarrativeEpisode;

    let mut episodes = Vec::new();

    for (i, node) in nodes.iter().enumerate() {
        if node.kind() == NodeKind::Episode {
            let domain = node
                .metadata
                .get("domain")
                .and_then(|v| v.as_str())
                .unwrap_or("general")
                .to_string();
            episodes.push(NarrativeEpisode {
                episode_id: node.id,
                summary: node.label.clone(),
                participants: vec![node.id],
                domains: vec![domain],
                sentiment: node.attrs.valence,
                timestamp_ms: now_ms - (i as u64) * 86_400_000,
                related_goal: None,
            });
        }
    }

    episodes
}

// ── Category 30: Counterfactual Simulator ─────────────────────────────

pub fn run_counterfactual_tests(run: &mut BenchRun, scenario: &PersonaScenario) {
    use super::causal::*;
    use super::counterfactual::*;

    let persona = Some(scenario.name.clone());
    let now = now_secs();

    // Build a causal store from scenario edges.
    let mut store = CausalStore::new();
    seed_causal_from_graph(&mut store, &scenario.edges, now);
    let config = CounterfactualConfig::default();

    // Test 1: Config defaults.
    let start = Instant::now();
    let horizon_ok = config.max_horizon > 0 && config.max_horizon <= 20;
    let decay_ok = config.confidence_decay > 0.0 && config.confidence_decay < 1.0;
    run.add(BenchResult {
        category: "counterfactual".into(),
        test_name: "config_defaults".into(),
        persona: persona.clone(),
        passed: horizon_ok && decay_ok,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("max_horizon".into()),
        metric_value: Some(config.max_horizon as f64),
        details: Some(format!(
            "horizon={} conf_decay={:.2} hop_decay={:.2}",
            config.max_horizon, config.confidence_decay, config.hop_decay,
        )),
    });

    // Test 2: Simulate counterfactual queries.
    let start = Instant::now();
    let mut simulations_run = 0;
    let mut all_results_valid = true;
    let edge_list = store.edges();
    for edge in edge_list.iter().take(5) {
        let query = CounterfactualQuery {
            intervention: Intervention::ForceActivation {
                node: edge.cause.clone(),
                strength: 1.0,
            },
            observation: None,
            horizon_steps: 3,
            query_type: CounterfactualType::WhatIf,
        };
        let result = simulate_counterfactual(&query, &store, &config);
        if result.confidence < 0.0 || result.confidence > 1.0 {
            all_results_valid = false;
        }
        simulations_run += 1;
    }
    run.add(BenchResult {
        category: "counterfactual".into(),
        test_name: "simulate_counterfactual".into(),
        persona: persona.clone(),
        passed: all_results_valid,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("simulations".into()),
        metric_value: Some(simulations_run as f64),
        details: None,
    });

    // Test 3: Sensitivity analysis.
    let start = Instant::now();
    let mut sensitivity_entries = 0;
    if let Some(edge) = edge_list.first() {
        let query = CounterfactualQuery {
            intervention: Intervention::ForceActivation {
                node: edge.cause.clone(),
                strength: 1.0,
            },
            observation: None,
            horizon_steps: 3,
            query_type: CounterfactualType::WhatIf,
        };
        let entries = sensitivity_analysis(&query, &store, &config);
        sensitivity_entries = entries.len();
    }
    run.add(BenchResult {
        category: "counterfactual".into(),
        test_name: "sensitivity_analysis".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("entries".into()),
        metric_value: Some(sensitivity_entries as f64),
        details: None,
    });

    // Test 4: Compare alternatives.
    let start = Instant::now();
    let mut comparison_valid = true;
    if edge_list.len() >= 2 {
        let diff = compare_alternatives(&edge_list[0].cause, &edge_list[1].cause, &store, &config);
        if diff.is_nan() {
            comparison_valid = false;
        }
    }
    run.add(BenchResult {
        category: "counterfactual".into(),
        test_name: "compare_alternatives".into(),
        persona: persona.clone(),
        passed: comparison_valid,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: None,
        metric_value: None,
        details: Some(format!("edges_available={}", edge_list.len())),
    });

    // Test 5: Config serialization roundtrip.
    let start = Instant::now();
    let json = serde_json::to_string(&config).unwrap_or_default();
    let restored: Result<CounterfactualConfig, _> = serde_json::from_str(&json);
    run.add(BenchResult {
        category: "counterfactual".into(),
        test_name: "config_roundtrip".into(),
        persona: persona.clone(),
        passed: restored.is_ok(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: None,
        metric_value: None,
        details: None,
    });
}

/// Seed causal store from cognitive graph edges that imply causality.
fn seed_causal_from_graph(
    store: &mut super::causal::CausalStore,
    edges: &[CognitiveEdge],
    now: f64,
) -> usize {
    use super::causal::*;

    let mut count = 0;
    for edge in edges {
        let strength = match edge.kind {
            CognitiveEdgeKind::Causes => 0.8,
            CognitiveEdgeKind::Predicts => 0.6,
            CognitiveEdgeKind::Triggers => 0.7,
            CognitiveEdgeKind::Prevents => -0.5,
            _ => continue,
        };
        let cause = CausalNode::GraphNode(edge.src);
        let effect = CausalNode::GraphNode(edge.dst);
        let trace = CausalTrace {
            evidence: vec![CausalEvidence::TemporalPrecedence {
                co_occurrences: 5,
                avg_lag_secs: 60.0,
                lag_stddev_secs: 10.0,
            }],
            primary_method: DiscoveryMethod::TemporalAssociation,
            summary: format!("{:?} edge", edge.kind),
        };
        let ce = CausalEdge {
            cause,
            effect,
            strength,
            confidence: 0.7,
            observation_count: 5,
            intervention_count: 0,
            non_occurrence_count: 1,
            median_lag_secs: 60.0,
            lag_iqr_secs: 20.0,
            context_strengths: Vec::new(),
            trace,
            created_at: now,
            updated_at: now,
            stage: CausalStage::Established,
        };
        store.upsert(ce);
        count += 1;
    }
    count
}

// ── Category 31: Probabilistic Belief Network ─────────────────────────

pub fn run_belief_network_tests(run: &mut BenchRun, scenario: &PersonaScenario) {
    use super::belief_network::*;

    let persona = Some(scenario.name.clone());

    // Build a belief network from persona belief nodes and edges.
    let (network, var_ids) = build_belief_network_from_scenario(&scenario.nodes, &scenario.edges);

    // Test 1: Network construction.
    let start = Instant::now();
    let var_count = network.variable_count();
    let fac_count = network.factor_count();
    run.add(BenchResult {
        category: "belief_network".into(),
        test_name: "network_construction".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("variables".into()),
        metric_value: Some(var_count as f64),
        details: Some(format!("vars={} factors={}", var_count, fac_count)),
    });

    // Test 2: Belief propagation.
    let start = Instant::now();
    let mut net = network.clone();
    let config = BPConfig::default();
    let bp_result = loopy_belief_propagation(&mut net, &config);
    run.add(BenchResult {
        category: "belief_network".into(),
        test_name: "belief_propagation".into(),
        persona: persona.clone(),
        passed: bp_result.converged || var_count == 0,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("iterations".into()),
        metric_value: Some(bp_result.iterations as f64),
        details: Some(format!(
            "converged={} max_change={:.6}",
            bp_result.converged, bp_result.max_change,
        )),
    });

    // Test 3: Network diagnostics.
    let start = Instant::now();
    let health = network_diagnostics(&net);
    run.add(BenchResult {
        category: "belief_network".into(),
        test_name: "network_diagnostics".into(),
        persona: persona.clone(),
        passed: health.healthy || var_count == 0,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("avg_confidence".into()),
        metric_value: Some(health.avg_confidence),
        details: Some(format!(
            "components={} extreme_priors={} instabilities={}",
            health.components,
            health.extreme_priors.len(),
            health.potential_instabilities.len(),
        )),
    });

    // Test 4: Information gain.
    let start = Instant::now();
    if var_ids.len() >= 2 {
        let mut net2 = network.clone();
        let ig = information_gain(&mut net2, var_ids[0], var_ids[1], &config);
        run.add(BenchResult {
            category: "belief_network".into(),
            test_name: "information_gain".into(),
            persona: persona.clone(),
            passed: ig.is_finite(),
            duration_us: start.elapsed().as_micros() as f64,
            metric_name: Some("info_gain".into()),
            metric_value: Some(ig),
            details: None,
        });
    } else {
        run.add(BenchResult {
            category: "belief_network".into(),
            test_name: "information_gain".into(),
            persona: persona.clone(),
            passed: true,
            duration_us: start.elapsed().as_micros() as f64,
            metric_name: None,
            metric_value: None,
            details: Some("insufficient_variables".into()),
        });
    }

    // Test 5: Network serialization roundtrip.
    let start = Instant::now();
    let json = serde_json::to_string(&network).unwrap_or_default();
    let restored: Result<BeliefNetwork, _> = serde_json::from_str(&json);
    let mut roundtrip_ok = restored.is_ok();
    if let Ok(mut restored_net) = restored {
        restored_net.rebuild_indices();
        roundtrip_ok = restored_net.variable_count() == var_count;
    }
    run.add(BenchResult {
        category: "belief_network".into(),
        test_name: "network_roundtrip".into(),
        persona: persona.clone(),
        passed: roundtrip_ok,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("json_bytes".into()),
        metric_value: Some(json.len() as f64),
        details: None,
    });
}

/// Build a BeliefNetwork from persona belief nodes and edges.
fn build_belief_network_from_scenario(
    nodes: &[CognitiveNode],
    edges: &[CognitiveEdge],
) -> (
    super::belief_network::BeliefNetwork,
    Vec<super::belief_network::VariableId>,
) {
    use super::belief_network::*;

    // Extract beliefs: (node_id, label, log_odds from confidence).
    let mut beliefs: Vec<(NodeId, String, f64)> = Vec::new();
    for node in nodes {
        if node.kind() == NodeKind::Belief {
            let p = node.attrs.confidence.clamp(0.01, 0.99);
            let log_odds = (p / (1.0 - p)).ln();
            beliefs.push((node.id, node.label.clone(), log_odds));
        }
    }

    let belief_id_set: std::collections::HashSet<NodeId> =
        beliefs.iter().map(|(id, _, _)| *id).collect();

    let mut edge_data: Vec<(NodeId, NodeId, EdgeRelation, f64)> = Vec::new();
    for edge in edges {
        if belief_id_set.contains(&edge.src) && belief_id_set.contains(&edge.dst) {
            let relation = match edge.kind {
                CognitiveEdgeKind::Supports => EdgeRelation::Supports,
                CognitiveEdgeKind::Contradicts => EdgeRelation::Contradicts,
                CognitiveEdgeKind::Causes => EdgeRelation::Causes,
                CognitiveEdgeKind::Predicts => EdgeRelation::Predicts,
                _ => EdgeRelation::Correlates,
            };
            edge_data.push((edge.src, edge.dst, relation, edge.weight));
        }
    }

    let belief_refs: Vec<(NodeId, &str, f64)> = beliefs
        .iter()
        .map(|(id, label, lo)| (*id, label.as_str(), *lo))
        .collect();

    let network = build_network_from_edges(&belief_refs, &edge_data);
    let var_ids: Vec<VariableId> = (0..beliefs.len()).map(|i| VariableId(i as u64)).collect();

    (network, var_ids)
}

// ── Category 32: Experience Replay / Dream Consolidation ──────────────

pub fn run_replay_tests(run: &mut BenchRun, scenario: &PersonaScenario) {
    use super::replay::*;

    let persona = Some(scenario.name.clone());
    let now_ms = (now_secs() * 1000.0) as u64;

    // Test 1: Engine initialization.
    let start = Instant::now();
    let mut engine = ReplayEngine::new();
    let summary = replay_summary(&engine);
    run.add(BenchResult {
        category: "replay".into(),
        test_name: "engine_init".into(),
        persona: persona.clone(),
        passed: summary.buffer_size == 0 && summary.total_replays == 0,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: None,
        metric_value: None,
        details: None,
    });

    // Test 2: Add experiences to buffer.
    let start = Instant::now();
    let mut attempted = 0;
    for (i, node) in scenario.nodes.iter().enumerate().take(20) {
        if node.kind() == NodeKind::Episode || node.kind() == NodeKind::Task {
            let domain = node
                .metadata
                .get("domain")
                .and_then(|v| v.as_str())
                .unwrap_or("general")
                .to_string();
            let action = ActionRecord {
                description: node.label.clone(),
                domain: domain.clone(),
                involved_nodes: vec![node.id],
            };
            let outcome = OutcomeData {
                utility: 0.3 + (i as f64 % 7.0) * 0.1,
                expected: i % 3 != 0,
                domains: vec![domain],
                affected_nodes: vec![node.id],
            };
            add_to_buffer(
                &mut engine,
                node.id,
                0.5, // expected utility
                action,
                outcome,
                now_ms - (i as u64) * 600_000,
            );
            attempted += 1;
        }
    }
    let post_summary = replay_summary(&engine);
    run.add(BenchResult {
        category: "replay".into(),
        test_name: "buffer_population".into(),
        persona: persona.clone(),
        passed: post_summary.buffer_size <= attempted,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("buffer_size".into()),
        metric_value: Some(post_summary.buffer_size as f64),
        details: Some(format!("attempted={}", attempted)),
    });

    // Test 3: Should-replay check.
    let start = Instant::now();
    let should = should_replay(&engine, now_ms);
    run.add(BenchResult {
        category: "replay".into(),
        test_name: "should_replay_check".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("should_replay".into()),
        metric_value: Some(if should { 1.0 } else { 0.0 }),
        details: None,
    });

    // Test 4: Reprioritize buffer.
    let start = Instant::now();
    reprioritize_buffer(&mut engine, now_ms);
    run.add(BenchResult {
        category: "replay".into(),
        test_name: "reprioritize".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("buffer_size".into()),
        metric_value: Some(post_summary.buffer_size as f64),
        details: None,
    });

    // Test 5: Dream cycle.
    let start = Instant::now();
    let beliefs: Vec<(NodeId, f64)> = scenario
        .nodes
        .iter()
        .filter(|n| n.kind() == NodeKind::Belief)
        .map(|n| (n.id, n.attrs.confidence))
        .collect();
    let report = run_replay_cycle(&mut engine, &beliefs, now_ms);
    run.add(BenchResult {
        category: "replay".into(),
        test_name: "dream_cycle".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("replays_executed".into()),
        metric_value: Some(report.replays_executed as f64),
        details: Some(format!(
            "beliefs={} causal={} assoc={}",
            report.beliefs_updated, report.causal_updates, report.new_associations,
        )),
    });

    // Test 6: Engine serialization roundtrip.
    let start = Instant::now();
    let json = serde_json::to_string(&engine).unwrap_or_default();
    let restored: Result<ReplayEngine, _> = serde_json::from_str(&json);
    run.add(BenchResult {
        category: "replay".into(),
        test_name: "engine_roundtrip".into(),
        persona: persona.clone(),
        passed: restored.is_ok(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("json_bytes".into()),
        metric_value: Some(json.len() as f64),
        details: None,
    });
}

// ── Category 33: Perspective Engine ───────────────────────────────────

pub fn run_perspective_tests(run: &mut BenchRun, scenario: &PersonaScenario) {
    use super::perspective::*;

    let persona = Some(scenario.name.clone());
    let now_ms = (now_secs() * 1000.0) as u64;

    // Test 1: Preset creation.
    let start = Instant::now();
    let mut store = PerspectiveStore::new();
    let presets = ["creative", "deadline", "reflective"];
    let mut preset_ids = Vec::new();
    for name in &presets {
        let id = store.alloc_id();
        if let Some(p) = create_preset(name, id, now_ms) {
            store.insert(p);
            preset_ids.push(id);
        }
    }
    run.add(BenchResult {
        category: "perspective".into(),
        test_name: "preset_creation".into(),
        persona: persona.clone(),
        passed: preset_ids.len() == 3,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("presets_created".into()),
        metric_value: Some(preset_ids.len() as f64),
        details: None,
    });

    // Test 2: Perspective activation and stack.
    let start = Instant::now();
    let mut stack = PerspectiveStack::new();
    for &id in &preset_ids {
        activate_perspective(&mut stack, id, &store);
    }
    let stack_depth = stack.depth();
    run.add(BenchResult {
        category: "perspective".into(),
        test_name: "stack_activation".into(),
        persona: persona.clone(),
        passed: stack_depth == preset_ids.len(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("stack_depth".into()),
        metric_value: Some(stack_depth as f64),
        details: None,
    });

    // Test 3: Salience resolution.
    let start = Instant::now();
    let mut all_valid = true;
    for node in &scenario.nodes {
        let domain = node.metadata.get("domain").and_then(|v| v.as_str());
        let tags: Vec<String> = node
            .metadata
            .get("tags")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect()
            })
            .unwrap_or_default();
        let resolved = resolve_salience(&stack, node.id, node.kind(), domain, &tags, 1.0, &store);
        if resolved < 0.0 || resolved > 10.0 {
            all_valid = false;
        }
    }
    run.add(BenchResult {
        category: "perspective".into(),
        test_name: "salience_resolution".into(),
        persona: persona.clone(),
        passed: all_valid,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("nodes_resolved".into()),
        metric_value: Some(scenario.nodes.len() as f64),
        details: None,
    });

    // Test 4: Edge weight resolution.
    let start = Instant::now();
    let mut edge_resolutions = 0;
    let mut all_edge_valid = true;
    for edge in &scenario.edges {
        let resolved = resolve_edge_weight(&stack, edge.kind, edge.weight, &store);
        if resolved.is_nan() || resolved.is_infinite() {
            all_edge_valid = false;
        }
        edge_resolutions += 1;
    }
    run.add(BenchResult {
        category: "perspective".into(),
        test_name: "edge_weight_resolution".into(),
        persona: persona.clone(),
        passed: all_edge_valid,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("edges_resolved".into()),
        metric_value: Some(edge_resolutions as f64),
        details: None,
    });

    // Test 5: Cognitive style blending.
    let start = Instant::now();
    let style = resolve_cognitive_style(&stack, &store);
    let style_valid = style.exploration_vs_exploitation >= 0.0
        && style.exploration_vs_exploitation <= 1.0
        && style.risk_tolerance >= 0.0
        && style.risk_tolerance <= 1.0;
    run.add(BenchResult {
        category: "perspective".into(),
        test_name: "cognitive_style_blend".into(),
        persona: persona.clone(),
        passed: style_valid,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("exploration".into()),
        metric_value: Some(style.exploration_vs_exploitation),
        details: Some(format!(
            "risk={:.2} abstraction={:.2} social={:.2}",
            style.risk_tolerance, style.abstraction_level, style.social_weight,
        )),
    });

    // Test 6: Conflict detection.
    let start = Instant::now();
    let conflicts = perspective_conflict_check(&stack, &store);
    run.add(BenchResult {
        category: "perspective".into(),
        test_name: "conflict_detection".into(),
        persona: persona.clone(),
        passed: true,
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("conflicts".into()),
        metric_value: Some(conflicts.len() as f64),
        details: None,
    });

    // Test 7: Store serialization roundtrip.
    let start = Instant::now();
    let json = serde_json::to_string(&store).unwrap_or_default();
    let restored: Result<PerspectiveStore, _> = serde_json::from_str(&json);
    run.add(BenchResult {
        category: "perspective".into(),
        test_name: "store_roundtrip".into(),
        persona: persona.clone(),
        passed: restored.is_ok(),
        duration_us: start.elapsed().as_micros() as f64,
        metric_name: Some("json_bytes".into()),
        metric_value: Some(json.len() as f64),
        details: None,
    });
}