aletheiadb 0.1.0

A high-performance bi-temporal graph database for LLM integration
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
//! Temporal Resonance Engine ("Echo").
//!
//! # The Spark
//! The "Echo" engine generates a `TemporalFingerprint` for a node based on its
//! mutation history (when it was updated) and compares these fingerprints
//! to find "resonant" nodes.
//!
//! # The Details
//! This module implements "Temporal Resonance", a way to find nodes that share
//! similar historical activity patterns.
//!
//! # The Hook
//! "Find me every sensor that failed with the same stuttering pattern as Sensor X."

use crate::AletheiaDB;
use crate::core::error::Result;
use crate::core::history::EntityHistory;
use crate::core::id::NodeId;
use crate::core::temporal::time;

/// A normalized representation of a node's temporal activity.
///
/// # The Details
/// Temporal fingerprints are used to compute the similarity of update histories between nodes.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "semantic-temporal")]
/// # fn main() {
/// use aletheiadb::experimental::echo::TemporalFingerprint;
///
/// let fp1 = TemporalFingerprint { bins: vec![1.0, 0.0], resolution_us: 1000 };
/// let fp2 = TemporalFingerprint { bins: vec![0.0, 1.0], resolution_us: 1000 };
/// let fp3 = TemporalFingerprint { bins: vec![1.0, 0.0], resolution_us: 1000 };
///
/// // fp1 and fp2 are orthogonal
/// assert!(fp1.similarity(&fp2) < 0.01);
///
/// // fp1 and fp3 are identical
/// assert!(fp1.similarity(&fp3) > 0.99);
/// # }
/// # #[cfg(not(feature = "semantic-temporal"))]
/// # fn main() {}
/// ```
#[derive(Debug, Clone)]
pub struct TemporalFingerprint {
    /// Normalized activity bins (sum of squares = 1.0 for cosine similarity).
    pub bins: Vec<f32>,
    /// The resolution of each bin in microseconds.
    pub resolution_us: i64,
}

impl TemporalFingerprint {
    /// Compute the Cosine Similarity between this fingerprint and another.
    /// Returns a score between 0.0 (orthogonal) and 1.0 (identical).
    ///
    /// # Panics
    /// Panics if fingerprints have different lengths.
    pub fn similarity(&self, other: &Self) -> f32 {
        if self.bins.len() != other.bins.len() {
            // In a real system we might handle this gracefully, but for Nova we expect
            // the Resonator to produce consistent fingerprints.
            panic!(
                "Fingerprint length mismatch: {} vs {}",
                self.bins.len(),
                other.bins.len()
            );
        }

        let dot_product: f32 = self
            .bins
            .iter()
            .zip(other.bins.iter())
            .map(|(a, b)| a * b)
            .sum();

        // Since bins are pre-normalized (unit vectors), cosine similarity is just the dot product.
        // We clamp to [0.0, 1.0] to handle floating point errors.
        dot_product.clamp(0.0, 1.0)
    }
}

/// Trait for generating temporal fingerprints from entity history.
pub trait Resonator {
    /// Generate a fingerprint from the given history.
    fn resonate(&self, history: &EntityHistory) -> TemporalFingerprint;
}

/// A resonator that measures activity density over a fixed time window.
///
/// # The Details
/// It looks at the "Last N" microseconds of history and bins updates into
/// a histogram.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "semantic-temporal")]
/// # fn main() {
/// use aletheiadb::experimental::echo::{ActivityDensityResonator, Resonator};
///
/// let resonator = ActivityDensityResonator {
///     window_size_us: 3600 * 1_000_000, // 1 hour
///     num_bins: 60,                     // 1 minute resolution
/// };
/// # }
/// # #[cfg(not(feature = "semantic-temporal"))]
/// # fn main() {}
/// ```
pub struct ActivityDensityResonator {
    /// Total time window to consider (in microseconds).
    pub window_size_us: i64,
    /// Number of bins to divide the window into.
    pub num_bins: usize,
}

impl Default for ActivityDensityResonator {
    fn default() -> Self {
        Self {
            window_size_us: 3600 * 1_000_000, // 1 hour
            num_bins: 60,                     // 1 minute resolution
        }
    }
}

impl Resonator for ActivityDensityResonator {
    fn resonate(&self, history: &EntityHistory) -> TemporalFingerprint {
        let mut bins = vec![0.0; self.num_bins];
        let bin_size_us = self.window_size_us / self.num_bins as i64;

        // We anchor the window to "now" (or the latest possible time).
        // Actually, to be deterministic for historical data, let's anchor to the *latest version*
        // in the history, OR use `time::now()`.
        // Using `time::now()` makes it sensitive to *when* you run the query (decaying echo).
        // Using `latest version` makes it relative to the entity's own timeline.
        //
        // "Echo" implies matching patterns in absolute time usually (e.g. "did everything crash at 2pm?").
        // So let's anchor to `time::now()`.
        let now = time::now().wallclock();
        let start_time = now - self.window_size_us;

        for version in &history.versions {
            let ts = version.temporal.valid_time().start().wallclock();

            if ts >= start_time && ts <= now {
                let offset = ts - start_time;
                let bin_idx = (offset / bin_size_us) as usize;
                if bin_idx < self.num_bins {
                    bins[bin_idx] += 1.0;
                }
            }
        }

        // Normalize (L2 Norm)
        let magnitude: f32 = bins.iter().map(|x| x * x).sum::<f32>().sqrt();
        if magnitude > 0.0 {
            for x in &mut bins {
                *x /= magnitude;
            }
        }

        TemporalFingerprint {
            bins,
            resolution_us: bin_size_us,
        }
    }
}

#[cfg(feature = "semantic-temporal")]
/// The Echo Chamber finds resonant nodes.
///
/// # The Details
/// Finds other nodes that have similar temporal update patterns (e.g., nodes that
/// are frequently updated at the same times).
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "semantic-temporal")]
/// # fn main() {
/// use aletheiadb::AletheiaDB;
/// use aletheiadb::experimental::echo::EchoChamber;
/// use aletheiadb::core::id::NodeId;
///
/// let db = AletheiaDB::new().unwrap();
/// let chamber = EchoChamber::new(&db);
///
/// let target_node = NodeId::new(1).unwrap();
/// let candidates = vec![NodeId::new(2).unwrap(), NodeId::new(3).unwrap()];
///
/// // chamber.find_echoes(target_node, &candidates);
/// # }
/// # #[cfg(not(feature = "semantic-temporal"))]
/// # fn main() {}
/// ```
/// A simulator for testing semantic drift and reinforcement loops.
///
/// # Why?
/// In recommender systems, repeatedly suggesting content similar to a user's
/// previous interactions creates an echo chamber. This struct simulates that
/// process over time to measure the degradation of content diversity.
pub struct EchoChamber<'a> {
    db: &'a AletheiaDB,
    resonator: Box<dyn Resonator>,
}

#[cfg(not(feature = "semantic-temporal"))]
/// The Echo Chamber finds resonant nodes.
///
/// # The Details
/// Finds other nodes that have similar temporal update patterns (e.g., nodes that
/// are frequently updated at the same times).
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "semantic-temporal")]
/// # fn main() {
/// use aletheiadb::AletheiaDB;
/// use aletheiadb::experimental::echo::EchoChamber;
/// use aletheiadb::core::id::NodeId;
///
/// let db = AletheiaDB::new().unwrap();
/// let chamber = EchoChamber::new(&db);
///
/// let target_node = NodeId::new(1).unwrap();
/// let candidates = vec![NodeId::new(2).unwrap(), NodeId::new(3).unwrap()];
///
/// // chamber.find_echoes(target_node, &candidates);
/// # }
/// # #[cfg(not(feature = "semantic-temporal"))]
/// # fn main() {}
/// ```
#[deprecated(
    note = "EchoChamber requires the 'nova' feature. Add 'features = [\"nova\"]' to your Cargo.toml."
)]
/// A simulator for testing semantic drift and reinforcement loops.
///
/// # Why?
/// In recommender systems, repeatedly suggesting content similar to a user's
/// previous interactions creates an echo chamber. This struct simulates that
/// process over time to measure the degradation of content diversity.
pub struct EchoChamber<'a> {
    _marker: std::marker::PhantomData<&'a AletheiaDB>,
}

#[cfg(feature = "semantic-temporal")]
impl<'a> EchoChamber<'a> {
    /// Create a new EchoChamber with default settings.
    pub fn new(db: &'a AletheiaDB) -> Self {
        Self {
            db,
            resonator: Box::new(ActivityDensityResonator::default()),
        }
    }

    /// Configure the EchoChamber with a custom resonator.
    pub fn with_resonator<R: Resonator + 'static>(mut self, resonator: R) -> Self {
        self.resonator = Box::new(resonator);
        self
    }

    /// Find nodes that resonate with the target node.
    pub fn find_echoes(&self, target: NodeId, candidates: &[NodeId]) -> Result<Vec<(NodeId, f32)>> {
        // 1. Get Target History & Fingerprint
        let target_history = self.db.get_node_history(target)?;
        let target_fp = self.resonator.resonate(&target_history);

        let mut results = Vec::with_capacity(candidates.len());

        // 2. Scan Candidates
        // Note: In a real implementation, we would want an index for this.
        // For Nova, a linear scan over provided candidates is acceptable.
        for &candidate_id in candidates {
            if candidate_id == target {
                continue;
            }

            // We accept that history might be missing for some candidates
            if let Ok(history) = self.db.get_node_history(candidate_id) {
                let fp = self.resonator.resonate(&history);
                let score = target_fp.similarity(&fp);
                if score > 0.0 {
                    results.push((candidate_id, score));
                }
            }
        }

        // 3. Sort by Score (Descending)
        // unwrap_or is safe because we clamped scores to 0.0-1.0 (no NaNs ideally, but safety first)
        results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        Ok(results)
    }
}

#[cfg(not(feature = "semantic-temporal"))]
#[allow(deprecated)]
impl<'a> EchoChamber<'a> {
    /// Create a new EchoChamber with default settings.
    ///
    /// # Panics
    ///
    /// This method panics if the `nova` feature is not enabled.
    #[allow(unused_variables)]
    #[track_caller]
    pub fn new(db: &'a AletheiaDB) -> Self {
        panic!(
            "EchoChamber requires the 'nova' feature. Add 'features = [\"nova\"]' to your Cargo.toml."
        );
    }

    /// Configure the EchoChamber with a custom resonator.
    ///
    /// # Panics
    ///
    /// This method panics if the `nova` feature is not enabled.
    #[allow(unused_variables)]
    #[track_caller]
    pub fn with_resonator<R: Resonator + 'static>(self, resonator: R) -> Self {
        panic!(
            "EchoChamber requires the 'nova' feature. Add 'features = [\"nova\"]' to your Cargo.toml."
        );
    }

    /// Find nodes that resonate with the target node.
    ///
    /// # Panics
    ///
    /// This method panics if the `nova` feature is not enabled.
    #[allow(unused_variables)]
    #[track_caller]
    pub fn find_echoes(&self, target: NodeId, candidates: &[NodeId]) -> Result<Vec<(NodeId, f32)>> {
        panic!(
            "EchoChamber requires the 'nova' feature. Add 'features = [\"nova\"]' to your Cargo.toml."
        );
    }
}

#[cfg(all(test, feature = "semantic-temporal"))]
mod tests {
    use super::*;
    use crate::api::transaction::WriteOps;
    use crate::core::hlc::HybridTimestamp;
    use crate::core::property::PropertyMapBuilder;
    use crate::core::temporal::{Timestamp, time};

    #[test]
    fn test_temporal_fingerprint_similarity() {
        let fp1 = TemporalFingerprint {
            bins: vec![1.0, 0.0], // Unit vector
            resolution_us: 1000,
        };
        let fp2 = TemporalFingerprint {
            bins: vec![0.0, 1.0], // Orthogonal
            resolution_us: 1000,
        };
        let fp3 = TemporalFingerprint {
            bins: vec![
                std::f32::consts::FRAC_1_SQRT_2,
                std::f32::consts::FRAC_1_SQRT_2,
            ], // ~45 degrees
            resolution_us: 1000,
        };

        assert!(fp1.similarity(&fp1) > 0.99); // Identity
        assert!(fp1.similarity(&fp2) < 0.01); // Orthogonal
        assert!(fp1.similarity(&fp3) > 0.6 && fp1.similarity(&fp3) < 0.8); // 45 deg
    }

    #[test]
    fn test_echo_chamber_integration() {
        let db = AletheiaDB::new().unwrap();

        // Define a "Now" for our test universe
        let now_wallclock = time::now().wallclock();
        // Window: Last 100 seconds
        let window = 100 * 1_000_000;

        // Create 3 nodes with specific histories

        // Node A (Target): Updates at T-80s, T-50s
        // Node B (Resonant): Updates at T-80s, T-50s (Identical pattern)
        // Node C (Noise): Updates at T-20s (Different pattern)

        let t_minus_80 = HybridTimestamp::new(now_wallclock - 80 * 1_000_000, 0).unwrap();
        let t_minus_50 = HybridTimestamp::new(now_wallclock - 50 * 1_000_000, 0).unwrap();
        let t_minus_20 = HybridTimestamp::new(now_wallclock - 20 * 1_000_000, 0).unwrap();

        let props = PropertyMapBuilder::new().insert("val", 0).build();

        // Helper to populate history
        let create_node_with_history = |timestamps: Vec<Timestamp>| -> NodeId {
            assert!(
                !timestamps.is_empty(),
                "history requires at least one timestamp"
            );

            // Create and commit the base version first.
            let id = {
                let mut tx = db.write_transaction().unwrap();
                let id = tx
                    .create_node_with_valid_time("Node", props.clone(), Some(timestamps[0]))
                    .unwrap();
                tx.commit().unwrap();
                id
            };

            // Apply subsequent versions as committed updates.
            for &ts in &timestamps[1..] {
                let mut tx = db.write_transaction().unwrap();
                tx.update_node_with_valid_time(id, props.clone(), Some(ts))
                    .unwrap();
                tx.commit().unwrap();
            }

            id
        };

        let node_a = create_node_with_history(vec![t_minus_80, t_minus_50]);
        let node_b = create_node_with_history(vec![t_minus_80, t_minus_50]);
        let node_c = create_node_with_history(vec![t_minus_20]);

        // Configure Resonator: 100s window, 10 bins (10s per bin)
        let resonator = ActivityDensityResonator {
            window_size_us: window,
            num_bins: 10,
        };

        let chamber = EchoChamber::new(&db).with_resonator(resonator);

        // Find echoes for Node A among B and C
        let echoes = chamber.find_echoes(node_a, &[node_b, node_c]).unwrap();

        // Expectation:
        // Node B should be ~1.0 (Identical)
        // Node C should be ~0.0 (Different bins)

        assert_eq!(echoes.len(), 1, "Should only return non-zero similarity"); // C might be 0.0

        let (id, score) = echoes[0];
        assert_eq!(id, node_b);
        assert!(
            score > 0.9,
            "Node B should resonate strongly (score: {})",
            score
        );

        // Verify C is not in results or very low
        let c_score = echoes.iter().find(|(id, _)| *id == node_c);
        if let Some((_, score)) = c_score {
            assert!(
                *score < 0.1,
                "Node C should not resonate (score: {})",
                score
            );
        }
    }
}

#[cfg(all(test, not(feature = "semantic-temporal")))]
#[allow(deprecated)]
mod stub_tests {
    use super::*;

    #[test]
    #[should_panic(
        expected = "EchoChamber requires the 'nova' feature. Add 'features = [\"nova\"]' to your Cargo.toml."
    )]
    fn test_stub_panic_on_new() {
        let db = AletheiaDB::new().unwrap();
        let _ = EchoChamber::new(&db);
    }

    #[test]
    #[should_panic(
        expected = "EchoChamber requires the 'nova' feature. Add 'features = [\"nova\"]' to your Cargo.toml."
    )]
    fn test_stub_panic_on_with_resonator() {
        let chamber = EchoChamber {
            _marker: std::marker::PhantomData,
        };
        // We need a dummy resonator. Since Resonator trait is public but ActivityDensityResonator is only stubbed out...
        // Actually ActivityDensityResonator is not gated?
        // Let's check the code above. ActivityDensityResonator struct definition is NOT gated.
        // So we can use it.
        let resonator = ActivityDensityResonator::default();
        let _ = chamber.with_resonator(resonator);
    }

    #[test]
    #[should_panic(
        expected = "EchoChamber requires the 'nova' feature. Add 'features = [\"nova\"]' to your Cargo.toml."
    )]
    fn test_stub_panic_on_find_echoes() {
        let chamber = EchoChamber {
            _marker: std::marker::PhantomData,
        };
        let _ = chamber.find_echoes(NodeId::new(0).unwrap(), &[]);
    }
}