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
//! Ariadne: Semantic Thread Weaver.
//!
//! "Follow the thread."
//!
//! Ariadne weaves narrative threads through the graph by connecting events
//! based on explicit causality (edges) and implicit semantic similarity (vectors),
//! while respecting the arrow of time.
//!
//! # Concepts
//! - **Thread**: A sequence of nodes (events) connected by causal links.
//! - **Warp (Edges)**: Explicit connections (e.g., `NEXT`, `CAUSED_BY`).
//! - **Weft (Vectors)**: Implicit connections based on semantic similarity.
//! - **Time Arrow**: Threads must move forward in time.

use crate::AletheiaDB;
use crate::core::error::Result;
use crate::core::id::NodeId;
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashSet};

/// A step in a narrative thread.
#[derive(Debug, Clone)]
pub struct ThreadStep {
    /// The node at this step.
    pub node_id: NodeId,
    /// The time of this event (from property).
    pub timestamp: i64,
    /// How we reached this node.
    pub connection_type: ConnectionType,
}

/// How a step is connected to the previous one.
#[derive(Debug, Clone, PartialEq)]
pub enum ConnectionType {
    /// Start of the thread.
    Start,
    /// Explicit edge traversal.
    Edge {
        /// Label of the traversed edge.
        label: String,
    },
    /// Implicit semantic jump via vector similarity.
    SemanticJump {
        /// Similarity score (higher means more semantically similar).
        similarity: f32,
    },
}

/// Internal state for the priority queue.
#[derive(Debug)]
struct SearchState {
    node_id: NodeId,
    timestamp: i64,
    g_cost: f32, // Actual cost from start
    f_cost: f32, // Total estimated cost (g + h)
    path: Vec<ThreadStep>,
}

impl PartialEq for SearchState {
    fn eq(&self, other: &Self) -> bool {
        self.f_cost == other.f_cost
    }
}

impl Eq for SearchState {}

impl Ord for SearchState {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse for min-heap (lowest cost first)
        other
            .f_cost
            .partial_cmp(&self.f_cost)
            .unwrap_or(Ordering::Equal)
    }
}

impl PartialOrd for SearchState {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// The Ariadne Weaver engine.
///
/// # The Spark
/// Traditional graph traversals are bound by the explicit edges created during data ingestion.
/// If an edge is missing, the traversal stops. Ariadne overcomes this by utilizing vector embeddings
/// to "jump" across the graph when explicit relationships are absent, effectively combining structured
/// relationships with semantic search.
pub struct Ariadne<'a> {
    db: &'a AletheiaDB,
}

impl<'a> Ariadne<'a> {
    /// Create a new Ariadne instance.
    pub fn new(db: &'a AletheiaDB) -> Self {
        Self { db }
    }

    /// Weave a narrative thread from a start node.
    ///
    /// This explores the graph to find a coherent sequence of events.
    /// It prefers explicit edges but will "jump" via vector similarity
    /// if edges are unavailable or more "expensive" heuristically.
    ///
    /// * `start_node` - The origin event.
    /// * `goal_node` - Optional explicit destination.
    /// * `time_property` - Property name for timestamps.
    /// * `vector_property` - Property name for embeddings.
    /// * `max_steps` - Maximum length of the thread.
    /// * `beam_width` - Number of candidates to consider at each step.
    ///
    /// # The Spark
    /// Standard graph traversals only follow explicit edges, ignoring semantic similarity.
    /// Pure vector searches find similar concepts but ignore causal relationships and time.
    /// `weave` bridges this gap by finding a path that prefers explicit causal edges but can
    /// gracefully "jump" across the graph using vector similarity when edges are missing or sub-optimal.
    ///
    /// # The Details
    /// The algorithm uses an A* search variant where:
    /// - **Edges** are cheap (cost = 1.0).
    /// - **Semantic Jumps** are expensive and scale inversely with similarity (cost = 1.5 + (1.0 - similarity) * 5.0).
    /// - **Time Arrow** is strictly enforced: every step must move forward in time.
    ///
    /// # Examples
    /// ```rust
    /// # #[cfg(feature = "semantic-temporal")]
    /// # fn main() {
    /// #   use aletheiadb::AletheiaDB;
    /// #   use aletheiadb::core::property::PropertyMapBuilder;
    /// #   use aletheiadb::index::vector::{HnswConfig, DistanceMetric};
    /// #   use aletheiadb::experimental::ariadne::Ariadne;
    /// #
    /// #   let db = AletheiaDB::new().unwrap();
    /// #   db.enable_vector_index("embedding", HnswConfig::new(2, DistanceMetric::Cosine)).unwrap();
    /// #
    /// #   // A starts the story
    /// #   let a = db.create_node("Event", PropertyMapBuilder::new()
    /// #       .insert("time", 10i64)
    /// #       .insert_vector("embedding", &[1.0, 0.0]).build()).unwrap();
    /// #
    /// #   // B is caused by A
    /// #   let b = db.create_node("Event", PropertyMapBuilder::new()
    /// #       .insert("time", 20i64)
    /// #       .insert_vector("embedding", &[0.9, 0.4]).build()).unwrap();
    /// #   db.create_edge(a, b, "NEXT", PropertyMapBuilder::new().build()).unwrap();
    /// #
    /// #   // C is not explicitly linked, but semantically close to B
    /// #   let c = db.create_node("Event", PropertyMapBuilder::new()
    /// #       .insert("time", 30i64)
    /// #       .insert_vector("embedding", &[0.8, 0.5]).build()).unwrap();
    /// #
    /// #   let ariadne = Ariadne::new(&db);
    /// #   let path = ariadne.weave(a, Some(c), "time", "embedding", 10, 10).unwrap();
    /// #
    /// #   assert_eq!(path.first().unwrap().node_id, a);
    /// #   assert_eq!(path.last().unwrap().node_id, c);
    /// # }
    /// # #[cfg(not(feature = "semantic-temporal"))]
    /// # fn main() {}
    /// ```
    pub fn weave(
        &self,
        start_node: NodeId,
        goal_node: Option<NodeId>,
        time_property: &str,
        vector_property: &str,
        max_steps: usize,
        beam_width: usize,
    ) -> Result<Vec<ThreadStep>> {
        let start_time = self.get_time(start_node, time_property)?;

        let start_step = ThreadStep {
            node_id: start_node,
            timestamp: start_time,
            connection_type: ConnectionType::Start,
        };

        // If goal is provided, get its vector for heuristic
        let goal_vector = if let Some(goal) = goal_node {
            if let Ok(node) = self.db.get_node(goal) {
                node.properties
                    .get(vector_property)
                    .and_then(|v| v.as_vector())
                    .map(|v| v.to_vec())
            } else {
                None
            }
        } else {
            None
        };

        let mut pq = BinaryHeap::new();
        let heuristic = self.calculate_heuristic(start_node, vector_property, &goal_vector);
        pq.push(SearchState {
            node_id: start_node,
            timestamp: start_time,
            g_cost: 0.0,
            f_cost: heuristic,
            path: vec![start_step],
        });

        let mut visited = HashSet::new();

        let mut best_path = Vec::new();
        let mut max_path_len = 0;

        while let Some(state) = pq.pop() {
            if !visited.insert(state.node_id) {
                continue;
            }

            // Check if we reached goal
            if let Some(goal) = goal_node
                && state.node_id == goal
            {
                return Ok(state.path);
            }

            // Keep track of the longest valid path found so far
            if state.path.len() > max_path_len {
                max_path_len = state.path.len();
                best_path = state.path.clone();
            }

            if state.path.len() >= max_steps {
                continue;
            }

            // 1. Gather candidates from explicit edges
            // We favor explicit edges (low cost)
            let edges = self.db.current.get_outgoing_edges(state.node_id);
            for edge_id in edges {
                if let Ok(edge) = self.db.get_edge(edge_id) {
                    let target = edge.target;
                    if visited.contains(&target) {
                        continue;
                    }

                    if let Ok(target_time) = self.get_time(target, time_property)
                        && target_time >= state.timestamp
                    {
                        // Valid explicit step
                        let label = self.resolve_label(edge.label);
                        let mut new_path = state.path.clone();
                        new_path.push(ThreadStep {
                            node_id: target,
                            timestamp: target_time,
                            connection_type: ConnectionType::Edge { label },
                        });

                        let heuristic =
                            self.calculate_heuristic(target, vector_property, &goal_vector);

                        // Edges are cheap (cost += 1.0)
                        let new_g = state.g_cost + 1.0;
                        let f_cost = new_g + heuristic;
                        pq.push(SearchState {
                            node_id: target,
                            timestamp: target_time,
                            g_cost: new_g,
                            f_cost,
                            path: new_path,
                        });
                    }
                }
            }

            // 2. Gather candidates from semantic jumps
            // Only if we haven't found enough explicit edges, or to explore alternatives.
            // We search for vectors similar to the CURRENT node's vector.
            if let Ok(current_node) = self.db.get_node(state.node_id)
                && let Some(current_vector) = current_node
                    .properties
                    .get(vector_property)
                    .and_then(|v| v.as_vector())
            {
                let min_time = state.timestamp;

                // Use the custom predicate to filter by time!
                let candidates = self.db.find_similar_with_predicate(
                    vector_property,
                    current_vector,
                    beam_width, // Get top-k candidates
                    |candidate_id| {
                        // Filter: Candidate must be after current time
                        if *candidate_id == state.node_id {
                            return false;
                        } // Exclude self

                        // Check time property
                        if let Ok(node) = self.db.get_node(*candidate_id)
                            && let Some(val) = node.properties.get(time_property)
                        {
                            let time = val.as_int().unwrap_or(0);
                            return time >= min_time;
                        }
                        false
                    },
                );

                if let Ok(results) = candidates {
                    for (target, score) in results {
                        if visited.contains(&target) {
                            continue;
                        }

                        // Calculate timestamp again (or we could have returned it from predicate if signature allowed)
                        if let Ok(target_time) = self.get_time(target, time_property) {
                            let mut new_path = state.path.clone();
                            new_path.push(ThreadStep {
                                node_id: target,
                                timestamp: target_time,
                                connection_type: ConnectionType::SemanticJump { similarity: score },
                            });

                            let heuristic =
                                self.calculate_heuristic(target, vector_property, &goal_vector);

                            // Jumps are more expensive than edges.
                            // Cost based on dissimilarity (1.0 - score).
                            // Multiplier to discourage jumps if edges exist.
                            // Base cost of 1.5 ensures edges (cost 1.0) are preferred even for identical vectors.
                            let jump_cost = 1.5 + (1.0 - score) * 5.0;

                            let new_g = state.g_cost + jump_cost;
                            let f_cost = new_g + heuristic;
                            pq.push(SearchState {
                                node_id: target,
                                timestamp: target_time,
                                g_cost: new_g,
                                f_cost,
                                path: new_path,
                            });
                        }
                    }
                }
            }
        }

        Ok(best_path)
    }

    fn get_time(&self, node_id: NodeId, property: &str) -> Result<i64> {
        let node = self.db.get_node(node_id)?;
        let val = node.properties.get(property).ok_or_else(|| {
            crate::core::error::Error::Storage(crate::core::error::StorageError::PropertyNotFound(
                property.to_string(),
            ))
        })?;
        val.as_int().ok_or_else(|| {
            crate::core::error::Error::Query(crate::core::error::QueryError::TypeMismatch {
                expected: "Integer".to_string(),
                actual: format!("{:?}", val),
            })
        })
    }

    fn resolve_label(&self, label_id: crate::core::interning::InternedString) -> String {
        use crate::core::interning::GLOBAL_INTERNER;
        GLOBAL_INTERNER
            .resolve_with(label_id, |s| s.to_string())
            .unwrap_or_else(|| "unknown".to_string())
    }

    fn calculate_heuristic(
        &self,
        node_id: NodeId,
        vector_prop: &str,
        goal_vector: &Option<Vec<f32>>,
    ) -> f32 {
        if let Some(goal) = goal_vector
            && let Ok(node) = self.db.get_node(node_id)
            && let Some(vec) = node.properties.get(vector_prop).and_then(|v| v.as_vector())
        {
            // Simple Euclidean distance or 1-Cosine
            // Let's assume Cosine for now (normalized vectors)
            // HNSW usually returns similarity.
            // We need a cost.
            // Let's calculate manual cosine distance.
            // AletheiaDB doesn't expose a raw math util easily here, so implement basic dot product
            let dot: f32 = vec.iter().zip(goal.iter()).map(|(a, b)| a * b).sum();
            // Assuming normalized vectors, cosine dist = 1 - dot
            return (1.0 - dot).max(0.0);
        }
        0.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::property::PropertyMapBuilder;
    use crate::index::vector::{DistanceMetric, HnswConfig};

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

        // Setup vector index
        let config = HnswConfig::new(2, DistanceMetric::Cosine);
        db.enable_vector_index("embedding", config).unwrap();

        // Story: A -> B -> (Jump) -> C -> D
        // Times: 10 -> 20 -> (Jump) -> 30 -> 40

        // Event A: Start (Time 10)
        let props_a = PropertyMapBuilder::new()
            .insert("time", 10i64)
            .insert("name", "A")
            .insert_vector("embedding", &[1.0, 0.0])
            .build();
        let a = db.create_node("Event", props_a).unwrap();

        // Event B: Connected to A (Time 20)
        // Vector is close to A but distinct enough that Edge is cheaper than Jump to C
        let props_b = PropertyMapBuilder::new()
            .insert("time", 20i64)
            .insert("name", "B")
            .insert_vector("embedding", &[0.9, 0.4]) // Sim to A: 0.9 (approx)
            .build();
        let b = db.create_node("Event", props_b).unwrap();
        db.create_edge(a, b, "NEXT", PropertyMapBuilder::new().build())
            .unwrap();

        // Event C: Disconnected but semantically close to B (Time 30)
        // Vector is far from A, close to B
        let props_c = PropertyMapBuilder::new()
            .insert("time", 30i64)
            .insert("name", "C")
            .insert_vector("embedding", &[0.5, 0.8])
            .build();
        let c = db.create_node("Event", props_c).unwrap();

        // Event D: Connected to C (Time 40)
        let props_d = PropertyMapBuilder::new()
            .insert("time", 40i64)
            .insert("name", "D")
            .insert_vector("embedding", &[0.0, 1.0])
            .build();
        let d = db.create_node("Event", props_d).unwrap();
        db.create_edge(c, d, "NEXT", PropertyMapBuilder::new().build())
            .unwrap();

        // Event E: Past event (Time 5), semantically close to B. Should NOT be picked.
        let props_e = PropertyMapBuilder::new()
            .insert("time", 5i64)
            .insert("name", "E")
            .insert_vector("embedding", &[0.9, 0.4])
            .build();
        let _e = db.create_node("Event", props_e).unwrap();

        let ariadne = Ariadne::new(&db);

        // Weave from A to D
        let path = ariadne
            .weave(
                a,
                Some(d),
                "time",
                "embedding",
                10,
                100, // High beam width to ensure we find candidates
            )
            .unwrap();

        // Expected Path: A -> (Edge) -> B -> (Jump) -> C -> (Edge) -> D
        if path.len() != 4 {
            println!(
                "Path found: {:?}",
                path.iter().map(|s| s.node_id).collect::<Vec<_>>()
            );
        }

        assert_eq!(path.len(), 4);
        assert_eq!(path[0].node_id, a);
        assert_eq!(path[1].node_id, b);
        assert_eq!(path[2].node_id, c);
        assert_eq!(path[3].node_id, d);

        // Verify connection types
        assert!(matches!(
            path[1].connection_type,
            ConnectionType::Edge { .. }
        ));
        assert!(matches!(
            path[2].connection_type,
            ConnectionType::SemanticJump { .. }
        ));
        assert!(matches!(
            path[3].connection_type,
            ConnectionType::Edge { .. }
        ));
    }
}