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
//! Engine-level temporal reasoning API.
//!
//! Wires the temporal reasoning primitives from `cognition::temporal` into
//! `YantrikDB` methods that load nodes/edges from the database and apply
//! temporal analysis.

use crate::error::Result;
use crate::state::{CognitiveEdgeKind, CognitiveNode, NodeId, NodeKind, NodePayload};
use crate::temporal::{
    self, BurstConfig, BurstResult, DeadlineUrgencyConfig, DetectedPeriod, DomainRecencyMap,
    EwmaTracker, LabeledEvent, MotifConfig, PeriodicityConfig, PeriodicityResult, RecencyConfig,
    SeasonalHistogram, TemporalEvent, TemporalMotif, TemporalOrder, TemporalRelevanceConfig,
    TimeInterval,
};

use super::{now, YantrikDB};

impl YantrikDB {
    // ── Periodicity ──

    /// Detect periodicities in a routine node's triggering history.
    ///
    /// Loads Episode nodes linked via Triggers edges to the routine,
    /// extracts their timestamps, and runs periodicity detection.
    pub fn detect_routine_periodicity(
        &self,
        routine_id: NodeId,
        config: &PeriodicityConfig,
    ) -> Result<PeriodicityResult> {
        let edges = self.load_cognitive_edges_to(routine_id)?;
        let trigger_sources: Vec<NodeId> = edges
            .iter()
            .filter(|e| e.kind == CognitiveEdgeKind::Triggers)
            .map(|e| e.src)
            .collect();

        let mut timestamps = Vec::new();
        for src_id in trigger_sources {
            if let Some(node) = self.load_cognitive_node(src_id)? {
                if let NodePayload::Episode(ep) = &node.payload {
                    timestamps.push(ep.occurred_at);
                } else {
                    // Use last_updated_ms as fallback timestamp
                    timestamps.push(node.attrs.last_updated_ms as f64 / 1000.0);
                }
            }
        }

        Ok(temporal::detect_periodicity(&timestamps, config))
    }

    /// Detect periodicities from all Episode nodes.
    ///
    /// Groups episodes by label pattern and runs periodicity detection
    /// on each group.
    pub fn detect_episode_periodicities(
        &self,
        config: &PeriodicityConfig,
    ) -> Result<Vec<(String, PeriodicityResult)>> {
        let episodes = self.load_cognitive_nodes_by_kind(NodeKind::Episode)?;

        // Group by summary prefix (first word as label)
        let mut groups: std::collections::HashMap<String, Vec<f64>> =
            std::collections::HashMap::new();
        for ep_node in &episodes {
            if let NodePayload::Episode(ep) = &ep_node.payload {
                let label = ep
                    .summary
                    .split_whitespace()
                    .next()
                    .unwrap_or("unknown")
                    .to_lowercase();
                groups.entry(label).or_default().push(ep.occurred_at);
            }
        }

        let mut results = Vec::new();
        for (label, timestamps) in groups {
            let result = temporal::detect_periodicity(&timestamps, config);
            if !result.detected.is_empty() {
                results.push((label, result));
            }
        }

        // Sort by strongest correlation
        results.sort_by(|a, b| {
            let best_a = a.1.detected.first().map(|d| d.correlation).unwrap_or(0.0);
            let best_b = b.1.detected.first().map(|d| d.correlation).unwrap_or(0.0);
            best_b.total_cmp(&best_a)
        });

        Ok(results)
    }

    // ── Burst Detection ──

    /// Detect bursts in Episode node creation timestamps.
    pub fn detect_episode_bursts(&self, config: &BurstConfig) -> Result<BurstResult> {
        let episodes = self.load_cognitive_nodes_by_kind(NodeKind::Episode)?;
        let timestamps: Vec<f64> = episodes
            .iter()
            .filter_map(|n| {
                if let NodePayload::Episode(ep) = &n.payload {
                    Some(ep.occurred_at)
                } else {
                    None
                }
            })
            .collect();

        Ok(temporal::detect_bursts(&timestamps, config))
    }

    // ── Temporal Motifs ──

    /// Mine temporal motifs from Episode nodes.
    ///
    /// Extracts (label, timestamp) pairs from episodes and discovers
    /// recurring sequences.
    pub fn mine_episode_motifs(&self, config: &MotifConfig) -> Result<Vec<TemporalMotif>> {
        let episodes = self.load_cognitive_nodes_by_kind(NodeKind::Episode)?;
        let mut events: Vec<LabeledEvent> = episodes
            .iter()
            .filter_map(|n| {
                if let NodePayload::Episode(ep) = &n.payload {
                    Some(LabeledEvent {
                        label: ep
                            .summary
                            .split_whitespace()
                            .next()
                            .unwrap_or("unknown")
                            .to_lowercase(),
                        timestamp: ep.occurred_at,
                    })
                } else {
                    None
                }
            })
            .collect();

        Ok(temporal::mine_temporal_motifs(&events, config))
    }

    // ── Topological Ordering ──

    /// Compute temporal order of Episode nodes using PrecedesTemporally edges.
    pub fn temporal_order_episodes(&self) -> Result<TemporalOrder> {
        let episodes = self.load_cognitive_nodes_by_kind(NodeKind::Episode)?;
        let temporal_edges =
            self.load_cognitive_edges_by_kind(CognitiveEdgeKind::PrecedesTemporally)?;
        let trigger_edges = self.load_cognitive_edges_by_kind(CognitiveEdgeKind::Triggers)?;

        // Build predecessor map from edges
        let mut predecessors: std::collections::HashMap<u32, Vec<NodeId>> =
            std::collections::HashMap::new();
        for edge in temporal_edges.iter().chain(trigger_edges.iter()) {
            predecessors
                .entry(edge.dst.to_raw())
                .or_default()
                .push(edge.src);
        }

        let events: Vec<TemporalEvent> = episodes
            .iter()
            .map(|n| {
                let ts = if let NodePayload::Episode(ep) = &n.payload {
                    ep.occurred_at
                } else {
                    n.attrs.last_updated_ms as f64 / 1000.0
                };
                TemporalEvent {
                    node_id: n.id,
                    timestamp: ts,
                    predecessors: predecessors
                        .get(&n.id.to_raw())
                        .cloned()
                        .unwrap_or_default(),
                }
            })
            .collect();

        Ok(temporal::topological_order(&events))
    }

    // ── Seasonal Analysis ──

    /// Build hour-of-day histogram from Episode timestamps.
    pub fn episode_hour_histogram(&self) -> Result<SeasonalHistogram> {
        let episodes = self.load_cognitive_nodes_by_kind(NodeKind::Episode)?;
        let mut hist = SeasonalHistogram::hour_of_day();

        for n in &episodes {
            if let NodePayload::Episode(ep) = &n.payload {
                let hour = temporal::hour_of_day_utc(ep.occurred_at);
                hist.add(hour);
            }
        }

        Ok(hist)
    }

    /// Build day-of-week histogram from Episode timestamps.
    pub fn episode_dow_histogram(&self) -> Result<SeasonalHistogram> {
        let episodes = self.load_cognitive_nodes_by_kind(NodeKind::Episode)?;
        let mut hist = SeasonalHistogram::day_of_week();

        for n in &episodes {
            if let NodePayload::Episode(ep) = &n.payload {
                let dow = temporal::day_of_week_utc(ep.occurred_at);
                hist.add(dow);
            }
        }

        Ok(hist)
    }

    // ── Temporal Relevance Scoring ──

    /// Score all nodes of a given kind by temporal relevance.
    ///
    /// Returns `(NodeId, relevance_score)` sorted by relevance descending.
    pub fn temporal_relevance_scores(
        &self,
        kind: NodeKind,
        config: &TemporalRelevanceConfig,
    ) -> Result<Vec<(NodeId, f64)>> {
        let nodes = self.load_cognitive_nodes_by_kind(kind)?;
        let ts = now();

        let mut scored: Vec<(NodeId, f64)> = nodes
            .iter()
            .map(|n| {
                let last_updated = n.attrs.last_updated_ms as f64 / 1000.0;

                // Extract deadline if applicable
                let deadline = match &n.payload {
                    NodePayload::Task(t) => t.deadline,
                    NodePayload::Goal(g) => g.deadline,
                    NodePayload::Opportunity(o) => Some(o.expires_at),
                    _ => None,
                };

                // Extract next_occurrence if routine
                let next_occ = match &n.payload {
                    NodePayload::Routine(r) => Some(r.next_occurrence(ts)),
                    _ => None,
                };

                let score = temporal::temporal_relevance_composite(
                    last_updated,
                    ts,
                    next_occ,
                    deadline,
                    config,
                );

                (n.id, score)
            })
            .collect();

        scored.sort_by(|a, b| b.1.total_cmp(&a.1));

        Ok(scored)
    }

    // ── Deadline Dashboard ──

    /// Get all nodes approaching their deadlines, sorted by urgency.
    ///
    /// Returns `(NodeId, description, deadline, urgency)` for nodes
    /// with deadlines within `horizon_secs` of now.
    pub fn deadline_dashboard(&self, horizon_secs: f64) -> Result<Vec<(NodeId, String, f64, f64)>> {
        let ts = now();
        let config = DeadlineUrgencyConfig::default();
        let mut entries = Vec::new();

        // Tasks with deadlines
        let tasks = self.load_cognitive_nodes_by_kind(NodeKind::Task)?;
        for n in &tasks {
            if let NodePayload::Task(t) = &n.payload {
                if let Some(dl) = t.deadline {
                    if dl - ts < horizon_secs {
                        let urgency = temporal::deadline_urgency(dl, ts, &config);
                        entries.push((n.id, t.description.clone(), dl, urgency));
                    }
                }
            }
        }

        // Goals with deadlines
        let goals = self.load_cognitive_nodes_by_kind(NodeKind::Goal)?;
        for n in &goals {
            if let NodePayload::Goal(g) = &n.payload {
                if let Some(dl) = g.deadline {
                    if dl - ts < horizon_secs {
                        let urgency = temporal::deadline_urgency(dl, ts, &config);
                        entries.push((n.id, g.description.clone(), dl, urgency));
                    }
                }
            }
        }

        // Opportunities expiring soon
        let opps = self.load_cognitive_nodes_by_kind(NodeKind::Opportunity)?;
        for n in &opps {
            if let NodePayload::Opportunity(o) = &n.payload {
                if o.expires_at - ts < horizon_secs {
                    let urgency = temporal::deadline_urgency(o.expires_at, ts, &config);
                    entries.push((n.id, o.description.clone(), o.expires_at, urgency));
                }
            }
        }

        // Sort by urgency descending
        entries.sort_by(|a, b| b.3.total_cmp(&a.3));

        Ok(entries)
    }
}

// ── Tests ──

#[cfg(test)]
mod tests {
    use crate::engine::YantrikDB;
    use crate::state::*;
    use crate::temporal::{
        BurstConfig, DeadlineUrgencyConfig, MotifConfig, PeriodicityConfig, TemporalRelevanceConfig,
    };

    fn test_db() -> YantrikDB {
        YantrikDB::new(":memory:", 8).unwrap()
    }

    fn persist_episode(
        db: &YantrikDB,
        alloc: &mut NodeIdAllocator,
        summary: &str,
        occurred_at: f64,
    ) -> NodeId {
        let id = alloc.alloc(NodeKind::Episode);
        let node = CognitiveNode::new(
            id,
            summary.to_string(),
            NodePayload::Episode(EpisodePayload {
                memory_rid: format!("rid_{}", id.seq()),
                summary: summary.to_string(),
                occurred_at,
                participants: vec![],
            }),
        );
        db.persist_cognitive_node(&node).unwrap();
        id
    }

    #[test]
    fn test_episode_hour_histogram() {
        let db = test_db();
        let mut alloc = NodeIdAllocator::new();

        // Create episodes at 9am UTC (32400s into day)
        for i in 0..10 {
            let ts = 86400.0 * i as f64 + 32400.0;
            persist_episode(&db, &mut alloc, "morning_check", ts);
        }
        // A few at 3pm UTC (54000s)
        for i in 0..3 {
            let ts = 86400.0 * i as f64 + 54000.0;
            persist_episode(&db, &mut alloc, "afternoon_review", ts);
        }
        db.persist_node_id_allocator(&alloc).unwrap();

        let hist = db.episode_hour_histogram().unwrap();
        let (peak_bin, peak_count) = hist.peak();
        assert_eq!(peak_bin, 9); // 9am UTC
        assert_eq!(peak_count, 10);
    }

    #[test]
    fn test_deadline_dashboard() {
        let db = test_db();
        let mut alloc = NodeIdAllocator::new();

        // Task with deadline 1 hour from now
        let task_id = alloc.alloc(NodeKind::Task);
        let now_ts = crate::engine::now();
        let task = CognitiveNode::new(
            task_id,
            "Urgent task".to_string(),
            NodePayload::Task(TaskPayload {
                description: "Urgent task".to_string(),
                status: TaskStatus::InProgress,
                goal_id: None,
                deadline: Some(now_ts + 3600.0),
                priority: Priority::High,
                estimated_minutes: Some(30),
                prerequisites: vec![],
            }),
        );
        db.persist_cognitive_node(&task).unwrap();
        db.persist_node_id_allocator(&alloc).unwrap();

        let dashboard = db.deadline_dashboard(86400.0).unwrap(); // 1 day horizon
        assert_eq!(dashboard.len(), 1);
        assert_eq!(dashboard[0].0, task_id);
        assert!(dashboard[0].3 > 0.0); // Has some urgency
    }

    #[test]
    fn test_temporal_relevance_scoring() {
        let db = test_db();
        let mut alloc = NodeIdAllocator::new();

        // Create two tasks — one recent, one old
        let recent_id = alloc.alloc(NodeKind::Task);
        let mut recent = CognitiveNode::new(
            recent_id,
            "Recent task".to_string(),
            NodePayload::Task(TaskPayload {
                description: "Recent task".to_string(),
                status: TaskStatus::InProgress,
                goal_id: None,
                deadline: None,
                priority: Priority::Medium,
                estimated_minutes: None,
                prerequisites: vec![],
            }),
        );
        // Recent — default last_updated_ms is now

        let old_id = alloc.alloc(NodeKind::Task);
        let mut old = CognitiveNode::new(
            old_id,
            "Old task".to_string(),
            NodePayload::Task(TaskPayload {
                description: "Old task".to_string(),
                status: TaskStatus::InProgress,
                goal_id: None,
                deadline: None,
                priority: Priority::Low,
                estimated_minutes: None,
                prerequisites: vec![],
            }),
        );
        old.attrs.last_updated_ms = 1000; // Very old

        db.persist_cognitive_node(&recent).unwrap();
        db.persist_cognitive_node(&old).unwrap();
        db.persist_node_id_allocator(&alloc).unwrap();

        let config = TemporalRelevanceConfig::default();
        let scores = db
            .temporal_relevance_scores(NodeKind::Task, &config)
            .unwrap();
        assert_eq!(scores.len(), 2);
        // Recent task should score higher
        assert_eq!(scores[0].0, recent_id);
        assert!(scores[0].1 > scores[1].1);
    }

    #[test]
    fn test_episode_motif_mining() {
        let db = test_db();
        let mut alloc = NodeIdAllocator::new();

        // Create a repeating pattern: email → calendar → standup
        for day in 0..4 {
            let base = 1_700_000_000.0 + day as f64 * 86400.0;
            persist_episode(&db, &mut alloc, "email check", base + 100.0);
            persist_episode(&db, &mut alloc, "calendar review", base + 600.0);
            persist_episode(&db, &mut alloc, "standup meeting", base + 1200.0);
        }
        db.persist_node_id_allocator(&alloc).unwrap();

        let config = MotifConfig {
            max_gap_secs: 7200.0,
            min_length: 2,
            max_length: 3,
            min_occurrences: 3,
        };
        let motifs = db.mine_episode_motifs(&config).unwrap();
        // Should find the email→calendar motif
        let has_email_cal = motifs.iter().any(|m| {
            m.sequence.len() >= 2 && m.sequence[0] == "email" && m.sequence[1] == "calendar"
        });
        assert!(
            has_email_cal,
            "Should detect email→calendar motif: {:?}",
            motifs
        );
    }

    #[test]
    fn test_temporal_order_episodes() {
        let db = test_db();
        let mut alloc = NodeIdAllocator::new();

        let e1 = persist_episode(&db, &mut alloc, "first", 100.0);
        let e2 = persist_episode(&db, &mut alloc, "second", 200.0);
        let e3 = persist_episode(&db, &mut alloc, "third", 300.0);

        // Add temporal edges: e1 → e2 → e3
        db.persist_cognitive_edge(&CognitiveEdge {
            src: e1,
            dst: e2,
            kind: CognitiveEdgeKind::PrecedesTemporally,
            weight: 1.0,
            confidence: 1.0,
            observation_count: 1,
            created_at_ms: 100_000,
            last_confirmed_ms: 100_000,
        })
        .unwrap();
        db.persist_cognitive_edge(&CognitiveEdge {
            src: e2,
            dst: e3,
            kind: CognitiveEdgeKind::PrecedesTemporally,
            weight: 1.0,
            confidence: 1.0,
            observation_count: 1,
            created_at_ms: 200_000,
            last_confirmed_ms: 200_000,
        })
        .unwrap();
        db.persist_node_id_allocator(&alloc).unwrap();

        let order = db.temporal_order_episodes().unwrap();
        assert_eq!(order.ordered.len(), 3);
        assert_eq!(order.ordered[0], e1);
        assert_eq!(order.ordered[1], e2);
        assert_eq!(order.ordered[2], e3);
        assert!(order.cycles.is_empty());
    }
}