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
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
//! Integration tests for temporal vector support (Phase 3).
//!
//! Tests end-to-end temporal vector functionality including:
//! - Temporal vector index creation and configuration
//! - Snapshot creation with different strategies
//! - Point-in-time vector queries
//! - Time-range vector queries
//! - Snapshot pruning with retention policies

use aletheiadb::core::error::Result;
use aletheiadb::core::id::NodeId;
use aletheiadb::core::temporal::{TimeRange, time};
use aletheiadb::index::vector::temporal::{
    RetentionPolicy, SnapshotStrategy, TemporalVectorConfig, TemporalVectorIndex,
};
use aletheiadb::index::vector::{DistanceMetric, HnswConfig};
use std::time::Duration;

/// Helper to create a test temporal vector index with default config
fn create_test_index() -> Result<TemporalVectorIndex> {
    let hnsw_config = HnswConfig::new(4, DistanceMetric::Cosine);
    let config = TemporalVectorConfig {
        snapshot_strategy: SnapshotStrategy::TransactionInterval(2),
        retention_policy: RetentionPolicy::KeepN(10),
        max_snapshots: 100,
        full_snapshot_interval: 10,
        hnsw_config: Some(hnsw_config),
    };
    TemporalVectorIndex::new(config)
}

#[test]
fn test_temporal_index_creation() -> Result<()> {
    let index = create_test_index()?;
    assert_eq!(index.dimensions(), 4);
    assert_eq!(index.distance_metric(), DistanceMetric::Cosine);
    assert_eq!(index.snapshot_count(), 0);
    Ok(())
}

#[test]
fn test_snapshot_creation_with_transaction_interval() -> Result<()> {
    let index = create_test_index()?;

    // Add vectors
    let node1 = NodeId::new(1).unwrap();
    let node2 = NodeId::new(2).unwrap();
    let vec1 = vec![1.0, 0.0, 0.0, 0.0];
    let vec2 = vec![0.0, 1.0, 0.0, 0.0];

    index.add(node1, &vec1, 1000.into())?;
    index.add(node2, &vec2, 2000.into())?;

    assert_eq!(index.snapshot_count(), 0);

    // Trigger snapshot (transaction interval = 2)
    index.on_transaction()?;
    assert_eq!(index.snapshot_count(), 0); // Not yet

    index.on_transaction()?;
    assert_eq!(index.snapshot_count(), 1); // Now created

    Ok(())
}

#[test]
fn test_point_in_time_vector_query() -> Result<()> {
    let index = create_test_index()?;

    // Add vectors at different times
    let node1 = NodeId::new(1).unwrap();
    let node2 = NodeId::new(2).unwrap();
    let node3 = NodeId::new(3).unwrap();

    let timestamp1 = time::now();
    index.add(node1, &[1.0, 0.0, 0.0, 0.0], timestamp1)?;
    index.add(node2, &[0.9, 0.1, 0.0, 0.0], timestamp1)?;

    // Create snapshot
    index.on_transaction()?;
    index.on_transaction()?;
    assert_eq!(index.snapshot_count(), 1);

    // Add more vectors after snapshot
    let timestamp2 = (1000 + timestamp1.wallclock()).into();
    index.add(node3, &[0.0, 0.0, 1.0, 0.0], timestamp2)?;

    // Query at snapshot time (should not include node3)
    let query = vec![1.0, 0.0, 0.0, 0.0];
    let results = index.find_similar_as_of(&query, 10, timestamp1)?;

    // Should only find nodes from the snapshot
    assert!(results.len() <= 2);
    assert!(results.iter().all(|(id, _)| *id == node1 || *id == node2));

    Ok(())
}

#[test]
fn test_time_range_vector_query() -> Result<()> {
    let index = create_test_index()?;

    let base_time = time::now();

    // Create multiple snapshots with vectors
    for i in 0..3 {
        let node_id = NodeId::new(i).unwrap();
        let timestamp = ((i as i64 * 1000) + base_time.wallclock()).into();
        index.add(node_id, &[i as f32, 0.0, 0.0, 0.0], timestamp)?;

        index.on_transaction_at(timestamp)?;
        index.on_transaction_at(timestamp)?;
    }

    assert!(index.snapshot_count() >= 2);

    // Query across time range
    let query = vec![1.0, 0.0, 0.0, 0.0];
    let time_range = TimeRange::between(base_time, (3000 + base_time.wallclock()).into()).unwrap();
    let results = index.find_similar_in_range(&query, 5, time_range)?;

    // Should have results for multiple snapshots
    assert!(!results.is_empty());

    // Each result should have a timestamp and similar nodes
    for (timestamp, similar_nodes) in results {
        assert!(timestamp >= base_time);
        assert!(!similar_nodes.is_empty());
    }

    Ok(())
}

#[test]
fn test_snapshot_pruning_with_keep_n() -> Result<()> {
    let hnsw_config = HnswConfig::new(4, DistanceMetric::Cosine);
    let config = TemporalVectorConfig {
        snapshot_strategy: SnapshotStrategy::TransactionInterval(1),
        retention_policy: RetentionPolicy::KeepN(3),
        max_snapshots: 100,
        full_snapshot_interval: 10,
        hnsw_config: Some(hnsw_config),
    };
    let index = TemporalVectorIndex::new(config)?;

    // Create 5 snapshots
    for i in 0..5 {
        let node_id = NodeId::new(i).unwrap();
        index.add(
            node_id,
            &[i as f32, 0.0, 0.0, 0.0],
            ((i * 1000) as i64).into(),
        )?;
        index.on_transaction()?;
    }

    assert_eq!(index.snapshot_count(), 5);

    // Prune to keep only 3 most recent
    let removed = index.prune_snapshots()?;
    assert_eq!(removed, 2);
    assert_eq!(index.snapshot_count(), 3);

    Ok(())
}

#[test]
fn test_snapshot_pruning_with_keep_duration() -> Result<()> {
    let hnsw_config = HnswConfig::new(4, DistanceMetric::Cosine);
    let config = TemporalVectorConfig {
        snapshot_strategy: SnapshotStrategy::TransactionInterval(1),
        retention_policy: RetentionPolicy::KeepDuration(Duration::from_secs(10)),
        max_snapshots: 100,
        full_snapshot_interval: 10,
        hnsw_config: Some(hnsw_config),
    };
    let index = TemporalVectorIndex::new(config)?;

    // Create snapshots (all recent)
    for i in 0..3 {
        let node_id = NodeId::new(i).unwrap();
        index.add(
            node_id,
            &[i as f32, 0.0, 0.0, 0.0],
            ((i * 1000) as i64).into(),
        )?;
        index.on_transaction()?;
    }

    let count_before = index.snapshot_count();

    // Prune with duration policy (all snapshots are recent, so none should be removed)
    let removed = index.prune_snapshots()?;
    assert_eq!(removed, 0);
    assert_eq!(index.snapshot_count(), count_before);

    Ok(())
}

#[test]
fn test_snapshot_info_retrieval() -> Result<()> {
    let index = create_test_index()?;

    // Create a snapshot
    index.add(NodeId::new(1).unwrap(), &[1.0, 0.0, 0.0, 0.0], 1000.into())?;
    index.on_transaction()?;
    index.on_transaction()?;

    let info = index.get_snapshot_info()?;
    assert_eq!(info.len(), 1);

    let snapshot_info = &info[0];
    assert_eq!(snapshot_info.snapshot_id, 0);
    assert!(snapshot_info.timestamp.wallclock() > 0);
    assert!(snapshot_info.vector_count > 0);
    assert!(snapshot_info.size_bytes > 0);

    Ok(())
}

#[test]
fn test_temporal_vector_with_different_strategies() -> Result<()> {
    // Test TimeInterval strategy
    let hnsw_config = HnswConfig::new(4, DistanceMetric::Cosine);
    let config = TemporalVectorConfig {
        snapshot_strategy: SnapshotStrategy::TimeInterval(1), // 1 second
        retention_policy: RetentionPolicy::KeepN(10),
        max_snapshots: 100,
        full_snapshot_interval: 10,
        hnsw_config: Some(hnsw_config.clone()),
    };

    let base_time = 1_000_000_000; // 1 second in microseconds
    let time_interval_index = TemporalVectorIndex::new_at(config, base_time.into())?;

    time_interval_index.add(
        NodeId::new(1).unwrap(),
        &[1.0, 0.0, 0.0, 0.0],
        base_time.into(),
    )?;

    // Add another vector 2 seconds later (should trigger snapshot)
    let later_time = base_time + 2_000_000; // 2 seconds later
    time_interval_index.add(
        NodeId::new(2).unwrap(),
        &[0.0, 1.0, 0.0, 0.0],
        later_time.into(),
    )?;
    time_interval_index.on_transaction_at(later_time.into())?;

    assert_eq!(time_interval_index.snapshot_count(), 1);

    // Test ChangeThreshold strategy
    let config = TemporalVectorConfig {
        snapshot_strategy: SnapshotStrategy::ChangeThreshold(0.5), // 50% changed
        retention_policy: RetentionPolicy::KeepN(10),
        max_snapshots: 100,
        full_snapshot_interval: 10,
        hnsw_config: Some(hnsw_config),
    };
    let change_threshold_index = TemporalVectorIndex::new_at(config, 1000.into())?;

    // Add 4 vectors (initial)
    for i in 0..4 {
        change_threshold_index.add(
            NodeId::new(i).unwrap(),
            &[i as f32, 0.0, 0.0, 0.0],
            1000.into(),
        )?;
    }

    // Change 2 vectors (50% change should trigger snapshot)
    change_threshold_index.add(NodeId::new(0).unwrap(), &[10.0, 0.0, 0.0, 0.0], 2000.into())?;
    change_threshold_index.add(NodeId::new(1).unwrap(), &[11.0, 0.0, 0.0, 0.0], 2000.into())?;
    change_threshold_index.on_transaction_at(2000.into())?;

    assert!(change_threshold_index.snapshot_count() >= 1);

    Ok(())
}

#[test]
fn test_empty_index_queries() -> Result<()> {
    let index = create_test_index()?;

    // Query on empty index should return empty results or error gracefully
    let query = vec![1.0, 0.0, 0.0, 0.0];
    let timestamp = time::now();

    // No snapshot exists, so should return error or empty
    let result = index.find_similar_as_of(&query, 10, timestamp);
    assert!(result.is_err() || result.unwrap().is_empty());

    Ok(())
}

#[test]
fn test_concurrent_snapshot_creation() -> Result<()> {
    use std::sync::Arc;
    use std::thread;

    let index = Arc::new(create_test_index()?);

    // Add vectors from multiple threads
    let mut handles = vec![];
    for i in 0..4 {
        let index_clone = Arc::clone(&index);
        let handle = thread::spawn(move || -> Result<()> {
            let node_id = NodeId::new(i).unwrap();
            index_clone.add(
                node_id,
                &[i as f32, 0.0, 0.0, 0.0],
                ((i * 1000) as i64).into(),
            )?;
            Ok(())
        });
        handles.push(handle);
    }

    // Wait for all threads
    for handle in handles {
        handle.join().unwrap()?;
    }

    // Trigger snapshots
    index.on_transaction()?;
    index.on_transaction()?;

    // Should have created at least one snapshot
    assert!(index.snapshot_count() > 0);

    Ok(())
}

#[test]
fn test_vector_dimension_validation() -> Result<()> {
    let index = create_test_index()?;

    let node_id = NodeId::new(1).unwrap();
    let wrong_dims = vec![1.0, 2.0]; // 2 dimensions, but index expects 4

    // Should fail with dimension mismatch
    let result = index.add(node_id, &wrong_dims, 1000.into());
    assert!(result.is_err());

    Ok(())
}

// ============================================================================
// Semantic Evolution and Drift Tracking Integration Tests (VS-045)
// ============================================================================

#[test]
fn test_semantic_evolution_end_to_end() -> Result<()> {
    let index = create_test_index()?;

    let node_id = NodeId::new(100).unwrap();
    let base_time = time::now();

    // Create a timeline of vector changes
    let vectors = [
        vec![1.0, 0.0, 0.0, 0.0],
        vec![0.9, 0.1, 0.0, 0.0],
        vec![0.7, 0.3, 0.0, 0.0],
        vec![0.5, 0.5, 0.0, 0.0],
    ];

    for (i, vector) in vectors.iter().enumerate() {
        let timestamp = ((i as i64 * 1000) + base_time.wallclock()).into();
        index.add(node_id, vector, timestamp)?;
        index.on_transaction_at(timestamp)?;
        index.on_transaction_at(timestamp)?; // Trigger snapshot with interval=2
    }

    // Get semantic evolution
    // Use a very wide time range to capture all snapshots (which use real timestamps)
    let time_range = TimeRange::between(0.into(), i64::MAX.into()).unwrap();
    let evolution = index.semantic_evolution(node_id, time_range)?;

    // Verify we captured all changes
    // Note: With transaction_interval=2, we should get a snapshot after every 2 transactions
    // 4 adds * 2 on_transaction calls = 8 transactions = 4 snapshots
    assert_eq!(evolution.len(), 4);

    // Verify vectors match
    for (i, (_, vector)) in evolution.iter().enumerate() {
        let expected = &vectors[i];
        let actual: &[f32] = vector.as_ref();
        assert_eq!(actual, expected.as_slice());
    }

    Ok(())
}

#[test]
fn test_track_semantic_drift_over_time() -> Result<()> {
    let index = create_test_index()?;

    let node_id = NodeId::new(200).unwrap();
    let base_time = time::now();

    // Create vectors that progressively drift from [1,0,0,0]
    index.add(node_id, &[1.0, 0.0, 0.0, 0.0], base_time)?;
    index.on_transaction_at(base_time)?;
    index.on_transaction_at(base_time)?;

    let t2 = (1000 + base_time.wallclock()).into();
    index.add(node_id, &[0.8, 0.2, 0.0, 0.0], t2)?;
    index.on_transaction_at(t2)?;
    index.on_transaction_at(t2)?;

    let t3 = (2000 + base_time.wallclock()).into();
    index.add(node_id, &[0.5, 0.5, 0.0, 0.0], t3)?;
    index.on_transaction_at(t3)?;
    index.on_transaction_at(t3)?;

    let t4 = (3000 + base_time.wallclock()).into();
    index.add(node_id, &[0.0, 1.0, 0.0, 0.0], t4)?;
    index.on_transaction_at(t4)?;
    index.on_transaction_at(t4)?;

    // Track drift from original vector
    let reference = vec![1.0, 0.0, 0.0, 0.0];
    // Use wide time range to capture all snapshots
    let time_range = TimeRange::between(0.into(), i64::MAX.into()).unwrap();
    let drift = index.track_semantic_drift(node_id, &reference, time_range)?;

    // Should have 4 measurements
    assert_eq!(drift.len(), 4);

    // Verify drift increases over time (similarity decreases)
    assert!(drift[0].1 > drift[1].1); // First should be more similar
    assert!(drift[1].1 > drift[2].1); // Progressive drift
    assert!(drift[2].1 > drift[3].1); // Maximum drift at end

    Ok(())
}

#[test]
fn test_calculate_consecutive_drift_end_to_end() -> Result<()> {
    let index = create_test_index()?;

    let node_id = NodeId::new(300).unwrap();
    let base_time = time::now();

    // Create a timeline: stable -> change -> stable -> big change
    let vectors = [
        vec![1.0, 0.0, 0.0, 0.0],
        vec![1.0, 0.0, 0.0, 0.0], // Same as previous (no drift)
        vec![0.9, 0.1, 0.0, 0.0], // Small drift
        vec![0.0, 1.0, 0.0, 0.0], // Large drift
    ];

    for (i, vector) in vectors.iter().enumerate() {
        let timestamp = ((i as i64 * 1000) + base_time.wallclock()).into();
        index.add(node_id, vector, timestamp)?;
        index.on_transaction_at(timestamp)?;
        index.on_transaction_at(timestamp)?;
    }

    // Calculate consecutive drift
    // Use wide time range to capture all snapshots
    let time_range = TimeRange::between(0.into(), i64::MAX.into()).unwrap();
    let drift = index.calculate_consecutive_drift(node_id, time_range)?;

    // Should have 3 drift measurements (4 vectors -> 3 pairs)
    assert_eq!(drift.len(), 3);

    // First drift should be very small (identical vectors)
    assert!(drift[0].1 < 0.001);

    // Second drift should be small but non-zero
    assert!(drift[1].1 > 0.0 && drift[1].1 < 0.2);

    // Third drift should be large (orthogonal vectors)
    assert!(drift[2].1 > 0.8);

    Ok(())
}

#[test]
fn test_semantic_evolution_with_gaps() -> Result<()> {
    let index = create_test_index()?;

    let node1 = NodeId::new(400).unwrap();
    let node2 = NodeId::new(401).unwrap();
    let base_time = time::now();

    // Add node1 at times 1000 and 3000
    index.add(node1, &[1.0, 0.0, 0.0, 0.0], base_time)?;
    index.on_transaction_at(base_time)?;
    index.on_transaction_at(base_time)?;

    // Add node2 at time 2000 (different node)
    let t2 = (1000 + base_time.wallclock()).into();
    index.add(node2, &[0.0, 1.0, 0.0, 0.0], t2)?;
    index.on_transaction_at(t2)?;
    index.on_transaction_at(t2)?;

    // Add node1 again at time 3000
    let t3 = (2000 + base_time.wallclock()).into();
    index.add(node1, &[0.0, 0.0, 1.0, 0.0], t3)?;
    index.on_transaction_at(t3)?;
    index.on_transaction_at(t3)?;

    // Get evolution for node1
    // Use wide time range to capture all snapshots
    let time_range = TimeRange::between(0.into(), i64::MAX.into()).unwrap();
    let evolution = index.semantic_evolution(node1, time_range)?;

    // Node1 appears in all 3 snapshots:
    // 1. Initial add of node1 -> snapshot has {node1}
    // 2. Add node2 -> snapshot still has {node1, node2} (node1 wasn't removed)
    // 3. Update node1 -> snapshot has {node1 (updated), node2}
    assert_eq!(evolution.len(), 3);
    assert_eq!(evolution[0].1.as_ref(), &[1.0, 0.0, 0.0, 0.0]); // Initial
    assert_eq!(evolution[1].1.as_ref(), &[1.0, 0.0, 0.0, 0.0]); // Same (not removed)
    assert_eq!(evolution[2].1.as_ref(), &[0.0, 0.0, 1.0, 0.0]); // Updated

    Ok(())
}

#[test]
fn test_empty_evolution_for_nonexistent_node() -> Result<()> {
    let index = create_test_index()?;

    let base_time = time::now();

    // Add some other node
    index.add(NodeId::new(1).unwrap(), &[1.0, 0.0, 0.0, 0.0], base_time)?;
    index.on_transaction()?;
    index.on_transaction()?;

    // Query for non-existent node
    let nonexistent = NodeId::new(999).unwrap();
    let time_range = TimeRange::between(base_time, (1000 + base_time.wallclock()).into()).unwrap();
    let evolution = index.semantic_evolution(nonexistent, time_range)?;

    // Should return empty, not error
    assert_eq!(evolution.len(), 0);

    Ok(())
}

#[test]
fn test_drift_calculation_with_normalized_vectors() -> Result<()> {
    use aletheiadb::core::vector::normalize;

    let index = create_test_index()?;

    let node_id = NodeId::new(500).unwrap();
    let base_time = time::now();

    // Use normalized vectors for more predictable cosine similarity
    let v1 = normalize(&[1.0, 0.0, 0.0, 0.0]);
    let v2 = normalize(&[0.707, 0.707, 0.0, 0.0]); // 45 degrees
    let v3 = normalize(&[0.0, 1.0, 0.0, 0.0]); // 90 degrees

    index.add(node_id, &v1, base_time)?;
    index.on_transaction_at(base_time)?;
    index.on_transaction_at(base_time)?;

    let t2 = (1000 + base_time.wallclock()).into();
    index.add(node_id, &v2, t2)?;
    index.on_transaction_at(t2)?;
    index.on_transaction_at(t2)?;

    let t3 = (2000 + base_time.wallclock()).into();
    index.add(node_id, &v3, t3)?;
    index.on_transaction_at(t3)?;
    index.on_transaction_at(t3)?;

    // Calculate consecutive drift
    // Use wide time range to capture all snapshots
    let time_range = TimeRange::between(0.into(), i64::MAX.into()).unwrap();
    let drift = index.calculate_consecutive_drift(node_id, time_range)?;

    // Should have 2 drift measurements
    assert_eq!(drift.len(), 2);

    // Drift from v1 to v2 is a 45-degree rotation, and so is v2 to v3.
    // Therefore, the drift values should be approximately equal.
    // drift = 1.0 - cos(45deg) = 1.0 - 1/sqrt(2) ≈ 0.2929
    let expected_drift = 1.0 - (1.0 / std::f32::consts::SQRT_2);
    assert!((drift[0].1 - expected_drift).abs() < 1e-4);
    assert!((drift[1].1 - expected_drift).abs() < 1e-4);

    Ok(())
}