rag-plusplus-core 0.1.0

High-performance retrieval engine with SIMD-accelerated vector search and trajectory memory
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
//! Branch Operations and Types
//!
//! Core types for branch management in trajectory DAGs.

use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::trajectory::graph::NodeId;

/// Get current Unix timestamp in seconds.
#[inline]
fn current_timestamp() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0)
}

/// Unique identifier for a branch.
pub type BranchId = u64;

/// Status of a branch in the state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BranchStatus {
    /// Currently active branch (being explored)
    Active,
    /// Archived branch (not active but preserved)
    Archived,
    /// Merged into another branch
    Merged,
    /// Recovered from "lost" state
    Recovered,
    /// Orphaned (parent was deleted)
    Orphaned,
}

impl Default for BranchStatus {
    fn default() -> Self {
        Self::Active
    }
}

/// A fork point where branching occurred.
///
/// Represents a node in the DAG where multiple children exist,
/// creating a decision point in the trajectory.
#[derive(Debug, Clone)]
pub struct ForkPoint {
    /// The node ID where forking occurred
    pub node_id: NodeId,
    /// All child branches at this fork
    pub children: Vec<BranchId>,
    /// The selected/active child branch (if any)
    pub selected_child: Option<BranchId>,
    /// Timestamp when fork was created
    pub created_at: i64,
    /// Depth in the trajectory graph
    pub depth: u32,
}

impl ForkPoint {
    /// Create a new fork point.
    pub fn new(node_id: NodeId, children: Vec<BranchId>, depth: u32) -> Self {
        Self {
            node_id,
            children,
            selected_child: None,
            created_at: current_timestamp(),
            depth,
        }
    }

    /// Check if this fork has multiple paths (is actually a branch point).
    #[inline]
    pub fn is_branch_point(&self) -> bool {
        self.children.len() > 1
    }

    /// Get the number of child branches.
    #[inline]
    pub fn child_count(&self) -> usize {
        self.children.len()
    }

    /// Select a child branch as the active path.
    pub fn select(&mut self, branch_id: BranchId) -> Result<(), BranchError> {
        if self.children.contains(&branch_id) {
            self.selected_child = Some(branch_id);
            Ok(())
        } else {
            Err(BranchError::InvalidBranch(branch_id))
        }
    }
}

/// A branch in the trajectory state machine.
///
/// Represents a linear path through the DAG from a fork point to a leaf
/// (or to another fork point). Branches can be split off into independent
/// state machines and later merged back.
#[derive(Debug, Clone)]
pub struct Branch {
    /// Unique identifier for this branch
    pub id: BranchId,
    /// Where this branch diverged from its parent
    pub fork_point: NodeId,
    /// Current tip (head) of the branch
    pub head: NodeId,
    /// Current status of the branch
    pub status: BranchStatus,
    /// Parent branch (if this is a child branch)
    pub parent_branch: Option<BranchId>,
    /// Child branches that forked from this one
    pub child_branches: Vec<BranchId>,
    /// Nodes that belong exclusively to this branch
    pub nodes: Vec<NodeId>,
    /// Metadata associated with this branch
    pub metadata: HashMap<String, String>,
    /// Timestamp when branch was created
    pub created_at: i64,
    /// Timestamp when branch was last updated
    pub updated_at: i64,
    /// Human-readable label (optional)
    pub label: Option<String>,
}

impl Branch {
    /// Create a new branch.
    pub fn new(id: BranchId, fork_point: NodeId, head: NodeId) -> Self {
        let now = current_timestamp();
        Self {
            id,
            fork_point,
            head,
            status: BranchStatus::Active,
            parent_branch: None,
            child_branches: Vec::new(),
            nodes: vec![head],
            metadata: HashMap::new(),
            created_at: now,
            updated_at: now,
            label: None,
        }
    }

    /// Create a root branch (no fork point).
    pub fn root(id: BranchId, root_node: NodeId) -> Self {
        let now = current_timestamp();
        Self {
            id,
            fork_point: root_node,
            head: root_node,
            status: BranchStatus::Active,
            parent_branch: None,
            child_branches: Vec::new(),
            nodes: vec![root_node],
            metadata: HashMap::new(),
            created_at: now,
            updated_at: now,
            label: Some("main".to_string()),
        }
    }

    /// Check if this branch is active.
    #[inline]
    pub fn is_active(&self) -> bool {
        self.status == BranchStatus::Active
    }

    /// Check if this branch is the main/root branch.
    #[inline]
    pub fn is_root(&self) -> bool {
        self.parent_branch.is_none()
    }

    /// Check if this branch has been merged.
    #[inline]
    pub fn is_merged(&self) -> bool {
        self.status == BranchStatus::Merged
    }

    /// Archive this branch (preserve but mark inactive).
    pub fn archive(&mut self) {
        self.status = BranchStatus::Archived;
        self.updated_at = current_timestamp();
    }

    /// Mark this branch as merged.
    pub fn mark_merged(&mut self) {
        self.status = BranchStatus::Merged;
        self.updated_at = current_timestamp();
    }

    /// Recover this branch (reactivate from archived/orphaned).
    pub fn recover(&mut self) {
        self.status = BranchStatus::Recovered;
        self.updated_at = current_timestamp();
    }

    /// Add a node to this branch.
    pub fn add_node(&mut self, node_id: NodeId) {
        self.nodes.push(node_id);
        self.head = node_id;
        self.updated_at = current_timestamp();
    }

    /// Get the length of this branch (number of nodes).
    #[inline]
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    /// Check if branch is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Set a label for this branch.
    pub fn set_label(&mut self, label: impl Into<String>) {
        self.label = Some(label.into());
        self.updated_at = current_timestamp();
    }

    /// Add metadata to this branch.
    pub fn add_metadata(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.metadata.insert(key.into(), value.into());
        self.updated_at = current_timestamp();
    }
}

/// Operations that can be performed on branches.
///
/// These operations form the core of the "lost branch" solution,
/// ported from DLM's StateMachine operations.
#[derive(Debug, Clone)]
pub enum BranchOperation {
    /// Split a branch at a node, creating a new independent branch.
    /// Ported from DLM `StateMachine.split()`.
    Split {
        /// Source branch being split
        from: BranchId,
        /// New branch created by the split
        to: BranchId,
        /// Node where the split occurred
        at: NodeId,
        /// Timestamp of the operation
        timestamp: i64,
    },
    /// Merge a branch back into another.
    /// Ported from DLM `StateMachine.merge()`.
    Merge {
        /// Branch being merged (will become inactive)
        from: BranchId,
        /// Branch receiving the merge
        into: BranchId,
        /// Node where merge occurred
        at: NodeId,
        /// Timestamp of the operation
        timestamp: i64,
    },
    /// Recover a "lost" branch, reactivating it.
    Recover {
        /// Branch being recovered
        branch: BranchId,
        /// How it was recovered
        strategy: String,
        /// Timestamp of the operation
        timestamp: i64,
    },
    /// Traverse from one branch to another.
    Traverse {
        /// Branch being left
        from: BranchId,
        /// Branch being entered
        to: BranchId,
        /// Timestamp of the operation
        timestamp: i64,
    },
    /// Archive a branch (preserve but deactivate).
    Archive {
        /// Branch being archived
        branch: BranchId,
        /// Reason for archiving
        reason: Option<String>,
        /// Timestamp of the operation
        timestamp: i64,
    },
    /// Create a new branch at a fork point.
    Fork {
        /// Parent branch
        parent: BranchId,
        /// New branch created
        child: BranchId,
        /// Fork point node
        at: NodeId,
        /// Timestamp of the operation
        timestamp: i64,
    },
}

impl BranchOperation {
    /// Create a new split operation.
    pub fn split(from: BranchId, to: BranchId, at: NodeId) -> Self {
        Self::Split {
            from,
            to,
            at,
            timestamp: current_timestamp(),
        }
    }

    /// Create a new merge operation.
    pub fn merge(from: BranchId, into: BranchId, at: NodeId) -> Self {
        Self::Merge {
            from,
            into,
            at,
            timestamp: current_timestamp(),
        }
    }

    /// Create a new recover operation.
    pub fn recover(branch: BranchId, strategy: impl Into<String>) -> Self {
        Self::Recover {
            branch,
            strategy: strategy.into(),
            timestamp: current_timestamp(),
        }
    }

    /// Create a new traverse operation.
    pub fn traverse(from: BranchId, to: BranchId) -> Self {
        Self::Traverse {
            from,
            to,
            timestamp: current_timestamp(),
        }
    }

    /// Create a new archive operation.
    pub fn archive(branch: BranchId, reason: Option<String>) -> Self {
        Self::Archive {
            branch,
            reason,
            timestamp: current_timestamp(),
        }
    }

    /// Create a new fork operation.
    pub fn fork(parent: BranchId, child: BranchId, at: NodeId) -> Self {
        Self::Fork {
            parent,
            child,
            at,
            timestamp: current_timestamp(),
        }
    }

    /// Get the timestamp of this operation.
    pub fn timestamp(&self) -> i64 {
        match self {
            Self::Split { timestamp, .. } => *timestamp,
            Self::Merge { timestamp, .. } => *timestamp,
            Self::Recover { timestamp, .. } => *timestamp,
            Self::Traverse { timestamp, .. } => *timestamp,
            Self::Archive { timestamp, .. } => *timestamp,
            Self::Fork { timestamp, .. } => *timestamp,
        }
    }
}

/// Errors that can occur during branch operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BranchError {
    /// Branch with given ID was not found
    BranchNotFound(BranchId),
    /// Node with given ID was not found
    NodeNotFound(NodeId),
    /// Operation would create a cycle
    CycleDetected,
    /// Cannot split the root node
    CannotSplitRoot,
    /// Branch is already merged
    AlreadyMerged(BranchId),
    /// Invalid branch ID for operation
    InvalidBranch(BranchId),
    /// Cannot merge branch with itself
    SelfMerge,
    /// Branch has no parent
    NoParent(BranchId),
    /// Operation not allowed in current state
    InvalidState(String),
}

impl std::fmt::Display for BranchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BranchNotFound(id) => write!(f, "Branch {} not found", id),
            Self::NodeNotFound(id) => write!(f, "Node {} not found", id),
            Self::CycleDetected => write!(f, "Operation would create a cycle"),
            Self::CannotSplitRoot => write!(f, "Cannot split the root node"),
            Self::AlreadyMerged(id) => write!(f, "Branch {} is already merged", id),
            Self::InvalidBranch(id) => write!(f, "Invalid branch ID: {}", id),
            Self::SelfMerge => write!(f, "Cannot merge branch with itself"),
            Self::NoParent(id) => write!(f, "Branch {} has no parent", id),
            Self::InvalidState(msg) => write!(f, "Invalid state: {}", msg),
        }
    }
}

impl std::error::Error for BranchError {}

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

    #[test]
    fn test_branch_creation() {
        let branch = Branch::new(1, 100, 101);
        assert_eq!(branch.id, 1);
        assert_eq!(branch.fork_point, 100);
        assert_eq!(branch.head, 101);
        assert!(branch.is_active());
        // Note: parent_branch is None by default, so is_root() returns true
        // This is expected for a newly created branch without explicit parent
        assert!(branch.parent_branch.is_none());
    }

    #[test]
    fn test_root_branch() {
        let branch = Branch::root(0, 1);
        assert!(branch.is_root());
        assert_eq!(branch.label, Some("main".to_string()));
    }

    #[test]
    fn test_branch_archive() {
        let mut branch = Branch::new(1, 100, 101);
        assert!(branch.is_active());
        branch.archive();
        assert!(!branch.is_active());
        assert_eq!(branch.status, BranchStatus::Archived);
    }

    #[test]
    fn test_branch_recover() {
        let mut branch = Branch::new(1, 100, 101);
        branch.archive();
        branch.recover();
        assert_eq!(branch.status, BranchStatus::Recovered);
    }

    #[test]
    fn test_fork_point() {
        let mut fork = ForkPoint::new(100, vec![1, 2, 3], 5);
        assert!(fork.is_branch_point());
        assert_eq!(fork.child_count(), 3);

        assert!(fork.select(2).is_ok());
        assert_eq!(fork.selected_child, Some(2));

        assert!(fork.select(99).is_err());
    }

    #[test]
    fn test_branch_operations() {
        let split_op = BranchOperation::split(1, 2, 100);
        assert!(split_op.timestamp() > 0);

        let merge_op = BranchOperation::merge(2, 1, 100);
        assert!(merge_op.timestamp() > 0);

        let recover_op = BranchOperation::recover(1, "manual");
        assert!(recover_op.timestamp() > 0);
    }

    #[test]
    fn test_branch_add_node() {
        let mut branch = Branch::new(1, 100, 101);
        assert_eq!(branch.len(), 1);

        branch.add_node(102);
        assert_eq!(branch.len(), 2);
        assert_eq!(branch.head, 102);
        assert!(branch.nodes.contains(&102));
    }

    #[test]
    fn test_branch_metadata() {
        let mut branch = Branch::new(1, 100, 101);
        branch.add_metadata("source", "regeneration");
        branch.set_label("experiment-1");

        assert_eq!(branch.metadata.get("source"), Some(&"regeneration".to_string()));
        assert_eq!(branch.label, Some("experiment-1".to_string()));
    }
}