yantrikdb 0.6.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
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
//! CK-1.6: Intent Inference Engine
//!
//! Bayesian intent inference without LLM — detects what the user likely wants
//! from the cognitive state graph alone. Intent hypotheses are generated from
//! multiple signal sources, scored via a linear feature model, and ranked by
//! posterior probability.
//!
//! ## Signal Sources (hypothesis generators)
//!
//! 1. **Goal-driven**: Active goals with urgency → "user wants to advance goal X"
//! 2. **Routine-driven**: Detected routines near trigger time → "user is about to do Y"
//! 3. **Need-driven**: Unmet needs with rising intensity → "user needs Z"
//! 4. **Episode-driven**: Recent episodes suggest continuation → "user wants to continue A"
//! 5. **Opportunity-driven**: Time-bounded chances → "user should seize opportunity B"
//!
//! ## Scoring
//!
//! Each hypothesis gets a 10-dimensional feature vector:
//! - [0] goal_alignment: how well this intent advances active goals
//! - [1] temporal_match: routine phase proximity / deadline urgency
//! - [2] recency: how recently supporting evidence was observed
//! - [3] frequency: how often this intent has been acted on historically
//! - [4] entity_overlap: shared entities between intent and recent context
//! - [5] valence_match: emotional alignment with user's current state
//! - [6] evidence_strength: number and weight of supporting beliefs
//! - [7] need_intensity: urgency of the underlying need (if any)
//! - [8] confidence: average confidence of supporting nodes
//! - [9] novelty: inverse of how routine this intent is (surprise factor)
//!
//! Features are combined via learned weights (initially hand-tuned) to produce
//! a raw score, then softmax-normalized across competing hypotheses to get
//! posterior probabilities.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use super::state::*;

// ── Feature Dimensions ──

/// Number of dimensions in the intent feature vector.
pub const FEATURE_DIM: usize = 10;

/// Feature indices for readability.
pub mod feature {
    pub const GOAL_ALIGNMENT: usize = 0;
    pub const TEMPORAL_MATCH: usize = 1;
    pub const RECENCY: usize = 2;
    pub const FREQUENCY: usize = 3;
    pub const ENTITY_OVERLAP: usize = 4;
    pub const VALENCE_MATCH: usize = 5;
    pub const EVIDENCE_STRENGTH: usize = 6;
    pub const NEED_INTENSITY: usize = 7;
    pub const CONFIDENCE: usize = 8;
    pub const NOVELTY: usize = 9;
}

// ── Configuration ──

/// Configuration for intent inference.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntentConfig {
    /// Feature weights for the linear scoring model.
    /// Length must equal FEATURE_DIM.
    pub weights: Vec<f64>,

    /// Minimum posterior probability to surface an intent hypothesis.
    pub min_posterior: f64,

    /// Maximum number of hypotheses to return.
    pub max_hypotheses: usize,

    /// Softmax temperature — lower = more decisive, higher = more exploratory.
    /// 1.0 = standard softmax, 0.5 = sharper, 2.0 = flatter.
    pub temperature: f64,

    /// Time window (seconds) for "recent" episodes.
    pub recency_window_secs: f64,

    /// Routine proximity window: how close to trigger time counts as "imminent".
    pub routine_proximity_secs: f64,

    /// Minimum goal urgency to generate goal-driven hypotheses.
    pub min_goal_urgency: f64,

    /// Minimum need intensity to generate need-driven hypotheses.
    pub min_need_intensity: f64,

    /// Whether to include opportunity-driven hypotheses.
    pub include_opportunities: bool,
}

impl Default for IntentConfig {
    fn default() -> Self {
        Self {
            // Hand-tuned initial weights — will be refined by adaptive learning (CK-3).
            // Goal alignment and temporal match are strongest signals.
            weights: vec![
                0.25, // goal_alignment  — strongest: goals drive proactive action
                0.20, // temporal_match  — strong: time context is very predictive
                0.12, // recency         — moderate: recent activity predicts intent
                0.10, // frequency       — moderate: habits predict behavior
                0.08, // entity_overlap  — moderate: shared context signals relevance
                0.05, // valence_match   — weak: emotional alignment matters less
                0.08, // evidence_strength — moderate: well-evidenced intents are real
                0.05, // need_intensity  — weak: needs are softer signals
                0.04, // confidence      — weak: confidence of supporting nodes
                0.03, // novelty         — weakest: surprise is a tiebreaker
            ],
            min_posterior: 0.05,
            max_hypotheses: 8,
            temperature: 1.0,
            recency_window_secs: 3600.0 * 4.0,  // 4 hours
            routine_proximity_secs: 1800.0,      // 30 minutes
            min_goal_urgency: 0.3,
            min_need_intensity: 0.4,
            include_opportunities: true,
        }
    }
}

// ── Hypothesis Source ──

/// What signal source generated this intent hypothesis.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IntentSource {
    /// An active goal with sufficient urgency.
    GoalDriven,
    /// A routine near its trigger time.
    RoutineDriven,
    /// An unmet need with rising intensity.
    NeedDriven,
    /// Recent episodes suggest continuation or follow-up.
    EpisodeDriven,
    /// A time-bounded opportunity about to expire.
    OpportunityDriven,
}

impl IntentSource {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::GoalDriven => "goal_driven",
            Self::RoutineDriven => "routine_driven",
            Self::NeedDriven => "need_driven",
            Self::EpisodeDriven => "episode_driven",
            Self::OpportunityDriven => "opportunity_driven",
        }
    }
}

// ── Scored Hypothesis ──

/// A scored intent hypothesis ready for ranking.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScoredIntent {
    /// Natural language description of the intent.
    pub description: String,

    /// What signal source generated this hypothesis.
    pub source: IntentSource,

    /// The 10-dimensional feature vector.
    pub features: Vec<f64>,

    /// Raw score from the linear model (before softmax).
    pub raw_score: f64,

    /// Posterior probability after softmax normalization [0.0, 1.0].
    pub posterior: f64,

    /// NodeIds of the cognitive nodes that support this hypothesis.
    pub supporting_nodes: Vec<NodeId>,

    /// NodeId of the source node (goal, routine, need, etc.) that spawned this.
    pub source_node: NodeId,
}

// ── Inference Result ──

/// Result of running intent inference on the cognitive graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntentInferenceResult {
    /// Ranked hypotheses (highest posterior first).
    pub hypotheses: Vec<ScoredIntent>,

    /// Total number of hypotheses generated before filtering.
    pub total_generated: usize,

    /// Number filtered out by min_posterior threshold.
    pub filtered_count: usize,

    /// Inference duration in microseconds.
    pub duration_us: u64,
}

// ── Feature Extraction ──

/// Extract features for a goal-driven intent hypothesis.
pub fn extract_goal_features(
    goal: &CognitiveNode,
    goals: &[&CognitiveNode],
    episodes: &[&CognitiveNode],
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<f64> {
    let mut f = vec![0.0; FEATURE_DIM];

    // [0] goal_alignment: urgency of this specific goal
    f[feature::GOAL_ALIGNMENT] = goal.attrs.urgency;

    // [1] temporal_match: deadline proximity if present
    if let NodePayload::Goal(ref gp) = goal.payload {
        if let Some(deadline) = gp.deadline {
            let time_left = (deadline - now).max(0.0);
            let hours_left = time_left / 3600.0;
            // Sigmoid-like urgency curve: peaks as deadline approaches
            f[feature::TEMPORAL_MATCH] = 1.0 / (1.0 + (hours_left / 24.0));
        }
    }

    // [2] recency: how recently this goal was updated
    let age_secs = goal.attrs.age_secs();
    f[feature::RECENCY] = (-age_secs / config.recency_window_secs).exp();

    // [3] frequency: evidence count as proxy for how often this goal is touched
    f[feature::FREQUENCY] = (goal.attrs.evidence_count as f64 / 10.0).min(1.0);

    // [4] entity_overlap: count edges connecting this goal to recent episodes
    let goal_raw = goal.id.to_raw();
    let overlap = edges.iter().filter(|e| {
        let src = e.src.to_raw();
        let tgt = e.dst.to_raw();
        (src == goal_raw || tgt == goal_raw)
            && episodes.iter().any(|ep| ep.id.to_raw() == src || ep.id.to_raw() == tgt)
    }).count();
    f[feature::ENTITY_OVERLAP] = (overlap as f64 / 5.0).min(1.0);

    // [5] valence_match: positive goals feel good
    f[feature::VALENCE_MATCH] = (goal.attrs.valence + 1.0) / 2.0; // normalize [-1,1] → [0,1]

    // [6] evidence_strength: number of edges supporting this goal
    let support = edges.iter().filter(|e| {
        e.dst.to_raw() == goal_raw && e.kind == CognitiveEdgeKind::AdvancesGoal
    }).count();
    f[feature::EVIDENCE_STRENGTH] = (support as f64 / 5.0).min(1.0);

    // [7] need_intensity: 0 for goal-driven (no need involved)
    f[feature::NEED_INTENSITY] = 0.0;

    // [8] confidence: goal confidence
    f[feature::CONFIDENCE] = goal.attrs.confidence;

    // [9] novelty: how novel this goal is (new goals are more interesting)
    f[feature::NOVELTY] = goal.attrs.novelty;

    clamp_features(&mut f);
    f
}

/// Extract features for a routine-driven intent hypothesis.
pub fn extract_routine_features(
    routine: &CognitiveNode,
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<f64> {
    let mut f = vec![0.0; FEATURE_DIM];

    if let NodePayload::Routine(ref rp) = routine.payload {
        // [0] goal_alignment: check if any edge links this routine to a goal
        let routine_raw = routine.id.to_raw();
        let goal_edges = edges.iter().filter(|e| {
            e.src.to_raw() == routine_raw && e.kind == CognitiveEdgeKind::AdvancesGoal
        }).count();
        f[feature::GOAL_ALIGNMENT] = (goal_edges as f64 / 3.0).min(1.0);

        // [1] temporal_match: how close we are to the next trigger time
        let time_until = rp.time_until_next(now);
        if time_until <= config.routine_proximity_secs {
            // Closer = higher score: 1.0 at trigger time, decaying outward
            f[feature::TEMPORAL_MATCH] = 1.0 - (time_until / config.routine_proximity_secs);
        }

        // [2] recency: last triggered recency
        if rp.last_triggered > 0.0 {
            let since_last = now - rp.last_triggered;
            f[feature::RECENCY] = (-since_last / config.recency_window_secs).exp();
        }

        // [3] frequency: observation count as proxy
        f[feature::FREQUENCY] = (rp.observation_count as f64 / 20.0).min(1.0);

        // [5] valence_match: routines are emotionally neutral by default
        f[feature::VALENCE_MATCH] = (routine.attrs.valence + 1.0) / 2.0;

        // [6] evidence_strength: reliability of the routine
        f[feature::EVIDENCE_STRENGTH] = rp.reliability;

        // [8] confidence: routine confidence
        f[feature::CONFIDENCE] = routine.attrs.confidence;

        // [9] novelty: inverse of reliability — unreliable routines are more "novel"
        f[feature::NOVELTY] = 1.0 - rp.reliability;
    }

    clamp_features(&mut f);
    f
}

/// Extract features for a need-driven intent hypothesis.
pub fn extract_need_features(
    need: &CognitiveNode,
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<f64> {
    let mut f = vec![0.0; FEATURE_DIM];

    if let NodePayload::Need(ref np) = need.payload {
        // [0] goal_alignment: check if need links to any goals
        let need_raw = need.id.to_raw();
        let goal_edges = edges.iter().filter(|e| {
            e.src.to_raw() == need_raw && e.kind == CognitiveEdgeKind::AdvancesGoal
        }).count();
        f[feature::GOAL_ALIGNMENT] = (goal_edges as f64 / 3.0).min(1.0);

        // [1] temporal_match: time since last satisfied (longer = more urgent)
        if let Some(last_sat) = np.last_satisfied {
            let hours_since = (now - last_sat) / 3600.0;
            f[feature::TEMPORAL_MATCH] = (hours_since / 24.0).min(1.0);
        } else {
            f[feature::TEMPORAL_MATCH] = 0.8; // never satisfied = fairly urgent
        }

        // [2] recency: how recently this need was identified
        let age_secs = need.attrs.age_secs();
        f[feature::RECENCY] = (-age_secs / config.recency_window_secs).exp();

        // [5] valence_match: unmet needs have negative valence
        f[feature::VALENCE_MATCH] = (need.attrs.valence + 1.0) / 2.0;

        // [7] need_intensity: the primary signal for need-driven intents
        f[feature::NEED_INTENSITY] = np.intensity;

        // [8] confidence: need confidence
        f[feature::CONFIDENCE] = need.attrs.confidence;

        // [9] novelty: new needs are more novel
        f[feature::NOVELTY] = need.attrs.novelty;
    }

    clamp_features(&mut f);
    f
}

/// Extract features for an episode-driven intent hypothesis.
///
/// Episode-driven intents detect that the user was recently engaged in an
/// activity and likely wants to continue or follow up.
pub fn extract_episode_features(
    episode: &CognitiveNode,
    related_episodes: &[&CognitiveNode],
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<f64> {
    let mut f = vec![0.0; FEATURE_DIM];

    // [0] goal_alignment: does this episode connect to an active goal?
    let ep_raw = episode.id.to_raw();
    let goal_edges = edges.iter().filter(|e| {
        e.src.to_raw() == ep_raw && e.kind == CognitiveEdgeKind::AdvancesGoal
    }).count();
    f[feature::GOAL_ALIGNMENT] = (goal_edges as f64 / 3.0).min(1.0);

    // [1] temporal_match: recency is the key signal for episode continuation
    if let NodePayload::Episode(ref ep) = episode.payload {
        let age_hours = (now - ep.occurred_at) / 3600.0;
        f[feature::TEMPORAL_MATCH] = (-age_hours / 4.0).exp(); // 4-hour half-life
    }

    // [2] recency: same as temporal for episodes
    let age_secs = episode.attrs.age_secs();
    f[feature::RECENCY] = (-age_secs / config.recency_window_secs).exp();

    // [3] frequency: how many related episodes exist (activity cluster)
    f[feature::FREQUENCY] = (related_episodes.len() as f64 / 5.0).min(1.0);

    // [4] entity_overlap: shared edges between this episode and others
    let overlap = edges.iter().filter(|e| {
        let src = e.src.to_raw();
        let tgt = e.dst.to_raw();
        (src == ep_raw || tgt == ep_raw)
            && related_episodes.iter().any(|r| r.id.to_raw() == src || r.id.to_raw() == tgt)
    }).count();
    f[feature::ENTITY_OVERLAP] = (overlap as f64 / 5.0).min(1.0);

    // [5] valence_match
    f[feature::VALENCE_MATCH] = (episode.attrs.valence + 1.0) / 2.0;

    // [6] evidence_strength: activation level (how much attention this has)
    f[feature::EVIDENCE_STRENGTH] = episode.attrs.activation;

    // [8] confidence
    f[feature::CONFIDENCE] = episode.attrs.confidence;

    // [9] novelty
    f[feature::NOVELTY] = episode.attrs.novelty;

    clamp_features(&mut f);
    f
}

/// Extract features for an opportunity-driven intent hypothesis.
pub fn extract_opportunity_features(
    opportunity: &CognitiveNode,
    edges: &[CognitiveEdge],
    now: f64,
    _config: &IntentConfig,
) -> Vec<f64> {
    let mut f = vec![0.0; FEATURE_DIM];

    if let NodePayload::Opportunity(ref op) = opportunity.payload {
        // [0] goal_alignment: how many goals does this opportunity advance?
        f[feature::GOAL_ALIGNMENT] = (op.relevant_goals.len() as f64 / 3.0).min(1.0);

        // [1] temporal_match: urgency based on expiry
        let time_left = (op.expires_at - now).max(0.0);
        let hours_left = time_left / 3600.0;
        f[feature::TEMPORAL_MATCH] = if hours_left < 24.0 {
            1.0 - (hours_left / 24.0) // urgent when expiring soon
        } else {
            0.2 // low urgency for distant opportunities
        };

        // [2] recency: how new this opportunity is
        let age_secs = opportunity.attrs.age_secs();
        f[feature::RECENCY] = (-age_secs / 14400.0).exp(); // 4-hour half-life

        // [4] entity_overlap: edges to other active nodes
        let op_raw = opportunity.id.to_raw();
        let connections = edges.iter().filter(|e| {
            e.src.to_raw() == op_raw || e.dst.to_raw() == op_raw
        }).count();
        f[feature::ENTITY_OVERLAP] = (connections as f64 / 5.0).min(1.0);

        // [6] evidence_strength: expected benefit
        f[feature::EVIDENCE_STRENGTH] = op.expected_benefit;

        // [8] confidence
        f[feature::CONFIDENCE] = opportunity.attrs.confidence;

        // [9] novelty: opportunities are inherently novel
        f[feature::NOVELTY] = opportunity.attrs.novelty.max(0.5);
    }

    clamp_features(&mut f);
    f
}

// ── Linear Scoring ──

/// Compute raw score from feature vector and weights via dot product.
pub fn linear_score(features: &[f64], weights: &[f64]) -> f64 {
    features.iter().zip(weights.iter()).map(|(f, w)| f * w).sum()
}

/// Softmax normalization with temperature.
///
/// Returns a vector of probabilities summing to 1.0.
/// Temperature < 1.0 sharpens the distribution (more decisive).
/// Temperature > 1.0 flattens it (more exploratory).
pub fn softmax(scores: &[f64], temperature: f64) -> Vec<f64> {
    if scores.is_empty() {
        return vec![];
    }

    let temp = temperature.max(0.01); // prevent division by zero
    let scaled: Vec<f64> = scores.iter().map(|s| s / temp).collect();

    // Subtract max for numerical stability
    let max_val = scaled.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
    let exps: Vec<f64> = scaled.iter().map(|s| (s - max_val).exp()).collect();
    let sum: f64 = exps.iter().sum();

    if sum <= 0.0 {
        // Degenerate case: uniform distribution
        let n = scores.len() as f64;
        return vec![1.0 / n; scores.len()];
    }

    exps.iter().map(|e| e / sum).collect()
}

// ── Hypothesis Generation ──

/// Generate intent hypotheses from goal nodes.
pub fn generate_goal_hypotheses(
    goals: &[&CognitiveNode],
    episodes: &[&CognitiveNode],
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<ScoredIntent> {
    goals
        .iter()
        .filter(|g| {
            g.attrs.urgency >= config.min_goal_urgency
                && matches!(g.payload, NodePayload::Goal(ref gp) if gp.status == GoalStatus::Active)
        })
        .map(|g| {
            let features = extract_goal_features(g, goals, episodes, edges, now, config);
            let raw_score = linear_score(&features, &config.weights);
            let description = match &g.payload {
                NodePayload::Goal(gp) => format!("Advance goal: {}", gp.description),
                _ => "Advance unknown goal".to_string(),
            };
            ScoredIntent {
                description,
                source: IntentSource::GoalDriven,
                features,
                raw_score,
                posterior: 0.0, // set after softmax
                supporting_nodes: vec![g.id],
                source_node: g.id,
            }
        })
        .collect()
}

/// Generate intent hypotheses from routine nodes near their trigger time.
pub fn generate_routine_hypotheses(
    routines: &[&CognitiveNode],
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<ScoredIntent> {
    routines
        .iter()
        .filter(|r| {
            if let NodePayload::Routine(ref rp) = r.payload {
                rp.time_until_next(now) <= config.routine_proximity_secs
            } else {
                false
            }
        })
        .map(|r| {
            let features = extract_routine_features(r, edges, now, config);
            let raw_score = linear_score(&features, &config.weights);
            let description = match &r.payload {
                NodePayload::Routine(rp) => format!("Routine: {}", rp.description),
                _ => "Unknown routine".to_string(),
            };
            ScoredIntent {
                description,
                source: IntentSource::RoutineDriven,
                features,
                raw_score,
                posterior: 0.0,
                supporting_nodes: vec![r.id],
                source_node: r.id,
            }
        })
        .collect()
}

/// Generate intent hypotheses from unmet need nodes.
pub fn generate_need_hypotheses(
    needs: &[&CognitiveNode],
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<ScoredIntent> {
    needs
        .iter()
        .filter(|n| {
            if let NodePayload::Need(ref np) = n.payload {
                np.intensity >= config.min_need_intensity
            } else {
                false
            }
        })
        .map(|n| {
            let features = extract_need_features(n, edges, now, config);
            let raw_score = linear_score(&features, &config.weights);
            let description = match &n.payload {
                NodePayload::Need(np) => format!("Address need: {}", np.description),
                _ => "Address unknown need".to_string(),
            };
            ScoredIntent {
                description,
                source: IntentSource::NeedDriven,
                features,
                raw_score,
                posterior: 0.0,
                supporting_nodes: vec![n.id],
                source_node: n.id,
            }
        })
        .collect()
}

/// Generate intent hypotheses from recent episodes.
///
/// Only the most recent episode is used as a "continuation" seed.
/// Older episodes within the recency window serve as supporting context.
pub fn generate_episode_hypotheses(
    episodes: &[&CognitiveNode],
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<ScoredIntent> {
    // Only generate from episodes within the recency window
    let recent: Vec<&CognitiveNode> = episodes
        .iter()
        .filter(|e| {
            if let NodePayload::Episode(ref ep) = e.payload {
                (now - ep.occurred_at) <= config.recency_window_secs
            } else {
                e.attrs.age_secs() <= config.recency_window_secs
            }
        })
        .copied()
        .collect();

    if recent.is_empty() {
        return vec![];
    }

    // Use the most recent episode as the primary seed
    let seed = recent
        .iter()
        .max_by(|a, b| {
            let a_time = match &a.payload {
                NodePayload::Episode(ep) => ep.occurred_at,
                _ => 0.0,
            };
            let b_time = match &b.payload {
                NodePayload::Episode(ep) => ep.occurred_at,
                _ => 0.0,
            };
            a_time.partial_cmp(&b_time).unwrap_or(std::cmp::Ordering::Equal)
        });

    match seed {
        Some(s) => {
            let features = extract_episode_features(s, &recent, edges, now, config);
            let raw_score = linear_score(&features, &config.weights);
            let description = match &s.payload {
                NodePayload::Episode(ep) => format!("Continue: {}", ep.summary),
                _ => "Continue recent activity".to_string(),
            };
            let supporting: Vec<NodeId> = recent.iter().map(|e| e.id).collect();
            vec![ScoredIntent {
                description,
                source: IntentSource::EpisodeDriven,
                features,
                raw_score,
                posterior: 0.0,
                supporting_nodes: supporting,
                source_node: s.id,
            }]
        }
        None => vec![],
    }
}

/// Generate intent hypotheses from opportunity nodes.
pub fn generate_opportunity_hypotheses(
    opportunities: &[&CognitiveNode],
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> Vec<ScoredIntent> {
    if !config.include_opportunities {
        return vec![];
    }

    opportunities
        .iter()
        .filter(|o| {
            if let NodePayload::Opportunity(ref op) = o.payload {
                op.expires_at > now // not yet expired
            } else {
                false
            }
        })
        .map(|o| {
            let features = extract_opportunity_features(o, edges, now, config);
            let raw_score = linear_score(&features, &config.weights);
            let description = match &o.payload {
                NodePayload::Opportunity(op) => format!("Seize opportunity: {}", op.description),
                _ => "Unknown opportunity".to_string(),
            };
            let mut supporting = vec![o.id];
            if let NodePayload::Opportunity(ref op) = o.payload {
                supporting.extend_from_slice(&op.relevant_goals);
            }
            ScoredIntent {
                description,
                source: IntentSource::OpportunityDriven,
                features,
                raw_score,
                posterior: 0.0,
                supporting_nodes: supporting,
                source_node: o.id,
            }
        })
        .collect()
}

// ── Full Inference Pipeline ──

/// Run the full intent inference pipeline on a set of cognitive nodes and edges.
///
/// This is the core function — it generates hypotheses from all signal sources,
/// scores them via the linear model, normalizes with softmax, filters by
/// min_posterior, and returns ranked results.
pub fn infer_intents(
    nodes: &[&CognitiveNode],
    edges: &[CognitiveEdge],
    now: f64,
    config: &IntentConfig,
) -> IntentInferenceResult {
    let start = std::time::Instant::now();

    // Partition nodes by kind
    let goals: Vec<&CognitiveNode> = nodes.iter()
        .filter(|n| n.id.kind() == NodeKind::Goal)
        .copied()
        .collect();
    let routines: Vec<&CognitiveNode> = nodes.iter()
        .filter(|n| n.id.kind() == NodeKind::Routine)
        .copied()
        .collect();
    let needs: Vec<&CognitiveNode> = nodes.iter()
        .filter(|n| n.id.kind() == NodeKind::Need)
        .copied()
        .collect();
    let episodes: Vec<&CognitiveNode> = nodes.iter()
        .filter(|n| n.id.kind() == NodeKind::Episode)
        .copied()
        .collect();
    let opportunities: Vec<&CognitiveNode> = nodes.iter()
        .filter(|n| n.id.kind() == NodeKind::Opportunity)
        .copied()
        .collect();

    // Generate hypotheses from all sources
    let mut all_hypotheses: Vec<ScoredIntent> = Vec::new();
    all_hypotheses.extend(generate_goal_hypotheses(&goals, &episodes, edges, now, config));
    all_hypotheses.extend(generate_routine_hypotheses(&routines, edges, now, config));
    all_hypotheses.extend(generate_need_hypotheses(&needs, edges, now, config));
    all_hypotheses.extend(generate_episode_hypotheses(&episodes, edges, now, config));
    all_hypotheses.extend(generate_opportunity_hypotheses(&opportunities, edges, now, config));

    let total_generated = all_hypotheses.len();

    if all_hypotheses.is_empty() {
        return IntentInferenceResult {
            hypotheses: vec![],
            total_generated: 0,
            filtered_count: 0,
            duration_us: start.elapsed().as_micros() as u64,
        };
    }

    // Softmax normalization across all hypotheses
    let raw_scores: Vec<f64> = all_hypotheses.iter().map(|h| h.raw_score).collect();
    let posteriors = softmax(&raw_scores, config.temperature);

    for (h, &p) in all_hypotheses.iter_mut().zip(posteriors.iter()) {
        h.posterior = p;
    }

    // Sort by posterior descending
    all_hypotheses.sort_by(|a, b| {
        b.posterior
            .partial_cmp(&a.posterior)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    // Filter by min_posterior and truncate
    let filtered: Vec<ScoredIntent> = all_hypotheses
        .into_iter()
        .filter(|h| h.posterior >= config.min_posterior)
        .take(config.max_hypotheses)
        .collect();

    let filtered_count = total_generated - filtered.len();

    IntentInferenceResult {
        hypotheses: filtered,
        total_generated,
        filtered_count,
        duration_us: start.elapsed().as_micros() as u64,
    }
}

/// Convert a ScoredIntent into an IntentPayload for storage as a CognitiveNode.
pub fn intent_to_payload(intent: &ScoredIntent) -> IntentPayload {
    IntentPayload {
        description: intent.description.clone(),
        features: intent.features.clone(),
        posterior: intent.posterior,
        candidate_actions: vec![], // filled by action schema (CK-1.7)
        source_context: intent.source.as_str().to_string(),
    }
}

/// Create a CognitiveNode for an intent hypothesis.
pub fn intent_to_node(intent: &ScoredIntent, alloc: &mut NodeIdAllocator) -> CognitiveNode {
    let id = alloc.alloc(NodeKind::IntentHypothesis);
    let mut attrs = CognitiveAttrs::default_for(NodeKind::IntentHypothesis);
    attrs.confidence = intent.posterior;
    attrs.activation = intent.raw_score.clamp(0.0, 1.0);
    attrs.salience = intent
        .features
        .get(feature::GOAL_ALIGNMENT)
        .copied()
        .unwrap_or(0.0)
        .max(attrs.salience);
    attrs.urgency = intent
        .features
        .get(feature::TEMPORAL_MATCH)
        .copied()
        .unwrap_or(0.0);
    attrs.provenance = Provenance::Inferred;

    CognitiveNode {
        id,
        label: intent.description.clone(),
        attrs,
        payload: NodePayload::IntentHypothesis(intent_to_payload(intent)),
        metadata: HashMap::new(),
    }
}

// ── Helpers ──

/// Clamp all feature values to [0.0, 1.0].
fn clamp_features(features: &mut [f64]) {
    for f in features.iter_mut() {
        *f = f.clamp(0.0, 1.0);
    }
}

// ── Tests ──

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

    fn make_goal(alloc: &mut NodeIdAllocator, desc: &str, urgency: f64) -> CognitiveNode {
        let id = alloc.alloc(NodeKind::Goal);
        let mut attrs = CognitiveAttrs::default_for(NodeKind::Goal);
        attrs.urgency = urgency;
        CognitiveNode {
            id,
            label: desc.to_string(),
            attrs,
            payload: NodePayload::Goal(GoalPayload {
                description: desc.to_string(),
                status: GoalStatus::Active,
                progress: 0.3,
                deadline: None,
                priority: Priority::High,
                parent_goal: None,
                completion_criteria: "Done when complete".to_string(),
            }),
            metadata: HashMap::new(),
        }
    }

    fn make_routine(alloc: &mut NodeIdAllocator, desc: &str, now: f64) -> CognitiveNode {
        let id = alloc.alloc(NodeKind::Routine);
        let attrs = CognitiveAttrs::default_for(NodeKind::Routine);
        CognitiveNode {
            id,
            label: desc.to_string(),
            attrs,
            payload: NodePayload::Routine(RoutinePayload {
                description: desc.to_string(),
                period_secs: 86400.0,          // daily
                phase_offset_secs: now + 600.0, // triggers in 10 minutes
                reliability: 0.85,
                observation_count: 15,
                last_triggered: now - 86400.0,
                action_description: "Do the thing".to_string(),
                weekday_mask: 0x7F,
            }),
            metadata: HashMap::new(),
        }
    }

    fn make_need(alloc: &mut NodeIdAllocator, desc: &str, intensity: f64) -> CognitiveNode {
        let id = alloc.alloc(NodeKind::Need);
        let attrs = CognitiveAttrs::default_for(NodeKind::Need);
        CognitiveNode {
            id,
            label: desc.to_string(),
            attrs,
            payload: NodePayload::Need(NeedPayload {
                description: desc.to_string(),
                category: NeedCategory::Informational,
                intensity,
                last_satisfied: None,
                satisfaction_pattern: "Search for information".to_string(),
            }),
            metadata: HashMap::new(),
        }
    }

    fn make_episode(alloc: &mut NodeIdAllocator, summary: &str, occurred_at: f64) -> CognitiveNode {
        let id = alloc.alloc(NodeKind::Episode);
        let attrs = CognitiveAttrs::default_for(NodeKind::Episode);
        CognitiveNode {
            id,
            label: summary.to_string(),
            attrs,
            payload: NodePayload::Episode(EpisodePayload {
                memory_rid: format!("rid_{}", id.to_raw()),
                summary: summary.to_string(),
                occurred_at,
                participants: vec!["user".to_string()],
            }),
            metadata: HashMap::new(),
        }
    }

    fn make_opportunity(alloc: &mut NodeIdAllocator, desc: &str, expires_at: f64) -> CognitiveNode {
        let id = alloc.alloc(NodeKind::Opportunity);
        let attrs = CognitiveAttrs::default_for(NodeKind::Opportunity);
        CognitiveNode {
            id,
            label: desc.to_string(),
            attrs,
            payload: NodePayload::Opportunity(OpportunityPayload {
                description: desc.to_string(),
                expires_at,
                expected_benefit: 0.8,
                required_action: "Act now".to_string(),
                relevant_goals: vec![],
            }),
            metadata: HashMap::new(),
        }
    }

    #[test]
    fn test_softmax_basic() {
        let scores = vec![1.0, 2.0, 3.0];
        let probs = softmax(&scores, 1.0);
        assert_eq!(probs.len(), 3);
        let sum: f64 = probs.iter().sum();
        assert!((sum - 1.0).abs() < 1e-10, "softmax should sum to 1.0");
        assert!(probs[2] > probs[1], "higher score → higher probability");
        assert!(probs[1] > probs[0], "higher score → higher probability");
    }

    #[test]
    fn test_softmax_temperature() {
        let scores = vec![1.0, 2.0, 3.0];
        let sharp = softmax(&scores, 0.5);
        let flat = softmax(&scores, 2.0);
        // Sharper temperature → winner takes more
        assert!(sharp[2] > flat[2], "lower temperature should sharpen distribution");
    }

    #[test]
    fn test_softmax_empty() {
        let probs = softmax(&[], 1.0);
        assert!(probs.is_empty());
    }

    #[test]
    fn test_linear_score() {
        let features = vec![1.0, 0.5, 0.0, 0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
        let weights = vec![0.25, 0.20, 0.12, 0.10, 0.08, 0.05, 0.08, 0.05, 0.04, 0.03];
        let score = linear_score(&features, &weights);
        let expected = 1.0 * 0.25 + 0.5 * 0.20 + 0.3 * 0.10;
        assert!((score - expected).abs() < 1e-10);
    }

    #[test]
    fn test_goal_hypothesis_generation() {
        let mut alloc = NodeIdAllocator::new();
        let g1 = make_goal(&mut alloc, "Finish report", 0.8);
        let g2 = make_goal(&mut alloc, "Buy groceries", 0.2); // below threshold
        let g3 = make_goal(&mut alloc, "Exercise", 0.5);

        let goals = vec![&g1, &g2, &g3];
        let config = IntentConfig::default();
        let now = 1710000000.0;

        let hypotheses = generate_goal_hypotheses(&goals, &[], &[], now, &config);

        // g2 should be filtered out (urgency 0.2 < min_goal_urgency 0.3)
        assert_eq!(hypotheses.len(), 2);
        assert!(hypotheses[0].description.contains("Finish report"));
        assert!(hypotheses[1].description.contains("Exercise"));
    }

    #[test]
    fn test_routine_hypothesis_generation() {
        let mut alloc = NodeIdAllocator::new();
        let now = 1710000000.0;
        let r1 = make_routine(&mut alloc, "Check email", now);
        // This routine triggers in 10 minutes — within proximity window

        let routines = vec![&r1];
        let config = IntentConfig::default();

        let hypotheses = generate_routine_hypotheses(&routines, &[], now, &config);

        assert_eq!(hypotheses.len(), 1);
        assert!(hypotheses[0].description.contains("Check email"));
        assert!(hypotheses[0].features[feature::TEMPORAL_MATCH] > 0.5);
    }

    #[test]
    fn test_need_hypothesis_generation() {
        let mut alloc = NodeIdAllocator::new();
        let n1 = make_need(&mut alloc, "Research Rust async patterns", 0.7);
        let n2 = make_need(&mut alloc, "Social interaction", 0.2); // below threshold

        let needs = vec![&n1, &n2];
        let config = IntentConfig::default();
        let now = 1710000000.0;

        let hypotheses = generate_need_hypotheses(&needs, &[], now, &config);

        assert_eq!(hypotheses.len(), 1);
        assert!(hypotheses[0].description.contains("Research"));
    }

    #[test]
    fn test_episode_hypothesis_generation() {
        let mut alloc = NodeIdAllocator::new();
        let now = 1710000000.0;
        let e1 = make_episode(&mut alloc, "Working on database migration", now - 1800.0);
        let e2 = make_episode(&mut alloc, "Debugging test failures", now - 600.0);

        let episodes = vec![&e1, &e2];
        let config = IntentConfig::default();

        let hypotheses = generate_episode_hypotheses(&episodes, &[], now, &config);

        // Should pick the most recent episode as the seed
        assert_eq!(hypotheses.len(), 1);
        assert!(hypotheses[0].description.contains("Debugging test failures"));
        assert_eq!(hypotheses[0].source, IntentSource::EpisodeDriven);
    }

    #[test]
    fn test_opportunity_hypothesis_generation() {
        let mut alloc = NodeIdAllocator::new();
        let now = 1710000000.0;
        let o1 = make_opportunity(&mut alloc, "Conference early-bird ends", now + 7200.0);
        let o2 = make_opportunity(&mut alloc, "Expired sale", now - 100.0); // expired

        let opportunities = vec![&o1, &o2];
        let config = IntentConfig::default();

        let hypotheses = generate_opportunity_hypotheses(&opportunities, &[], now, &config);

        assert_eq!(hypotheses.len(), 1);
        assert!(hypotheses[0].description.contains("Conference"));
    }

    #[test]
    fn test_full_inference_pipeline() {
        let mut alloc = NodeIdAllocator::new();
        let now = 1710000000.0;

        let g1 = make_goal(&mut alloc, "Finish report", 0.8);
        let r1 = make_routine(&mut alloc, "Check email", now);
        let n1 = make_need(&mut alloc, "Research async", 0.6);
        let e1 = make_episode(&mut alloc, "Writing code", now - 600.0);
        let o1 = make_opportunity(&mut alloc, "Conference", now + 3600.0);

        let nodes: Vec<&CognitiveNode> = vec![&g1, &r1, &n1, &e1, &o1];
        let config = IntentConfig::default();

        let result = infer_intents(&nodes, &[], now, &config);

        assert_eq!(result.total_generated, 5);
        assert!(!result.hypotheses.is_empty(), "should produce at least one hypothesis");

        // Posteriors should sum to ≤ 1.0 (some may be filtered)
        let sum: f64 = result.hypotheses.iter().map(|h| h.posterior).sum();
        assert!(sum <= 1.01, "posteriors should sum to ≤ 1.0, got {}", sum);

        // Should be sorted by posterior descending
        for w in result.hypotheses.windows(2) {
            assert!(w[0].posterior >= w[1].posterior, "should be sorted descending");
        }
    }

    #[test]
    fn test_intent_to_node() {
        let mut alloc = NodeIdAllocator::new();
        let intent = ScoredIntent {
            description: "Test intent".to_string(),
            source: IntentSource::GoalDriven,
            features: vec![0.8, 0.5, 0.3, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            raw_score: 0.4,
            posterior: 0.35,
            supporting_nodes: vec![],
            source_node: alloc.alloc(NodeKind::Goal),
        };

        let node = intent_to_node(&intent, &mut alloc);

        assert_eq!(node.id.kind(), NodeKind::IntentHypothesis);
        assert_eq!(node.label, "Test intent");
        assert!((node.attrs.confidence - 0.35).abs() < 1e-10); // posterior → confidence
        assert_eq!(node.attrs.provenance, Provenance::Inferred);
        if let NodePayload::IntentHypothesis(ref ip) = node.payload {
            assert_eq!(ip.features.len(), 10);
            assert!((ip.posterior - 0.35).abs() < 1e-10);
        } else {
            panic!("expected IntentHypothesis payload");
        }
    }

    #[test]
    fn test_empty_graph_produces_no_hypotheses() {
        let config = IntentConfig::default();
        let result = infer_intents(&[], &[], 1710000000.0, &config);

        assert_eq!(result.total_generated, 0);
        assert!(result.hypotheses.is_empty());
        assert_eq!(result.filtered_count, 0);
    }

    #[test]
    fn test_clamp_features() {
        let mut f = vec![-0.5, 1.5, 0.5, -0.1, 2.0];
        clamp_features(&mut f);
        assert_eq!(f, vec![0.0, 1.0, 0.5, 0.0, 1.0]);
    }

    #[test]
    fn test_feature_dimensions() {
        // Ensure default config has correct number of weights
        let config = IntentConfig::default();
        assert_eq!(config.weights.len(), FEATURE_DIM);

        // Ensure all feature extractors produce correct-length vectors
        let mut alloc = NodeIdAllocator::new();
        let now = 1710000000.0;

        let g = make_goal(&mut alloc, "Test", 0.5);
        let f = extract_goal_features(&g, &[], &[], &[], now, &config);
        assert_eq!(f.len(), FEATURE_DIM);

        let r = make_routine(&mut alloc, "Test", now);
        let f = extract_routine_features(&r, &[], now, &config);
        assert_eq!(f.len(), FEATURE_DIM);

        let n = make_need(&mut alloc, "Test", 0.5);
        let f = extract_need_features(&n, &[], now, &config);
        assert_eq!(f.len(), FEATURE_DIM);

        let e = make_episode(&mut alloc, "Test", now - 100.0);
        let f = extract_episode_features(&e, &[], &[], now, &config);
        assert_eq!(f.len(), FEATURE_DIM);

        let o = make_opportunity(&mut alloc, "Test", now + 3600.0);
        let f = extract_opportunity_features(&o, &[], now, &config);
        assert_eq!(f.len(), FEATURE_DIM);
    }
}