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
//! Trajectory Memory Module
//!
//! High-performance algorithms for trajectory-structured memory, implementing
//! concepts from IRCP (Inverse Ring Contextual Propagation) and RCP (Ring
//! Contextual Propagation).
//!
//! # Components
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`graph`] | DAG traversal, path finding, branch detection |
//! | [`phase`] | Phase inference from episode patterns |
//! | [`salience`] | Importance weighting for bounded forgetting |
//! | [`ring`] | Circular topology for multi-scale context (includes DualRing) |
//! | [`coordinate`] | 5D positioning system (DLM coordinates with complexity) |
//! | [`coordinate_tree`] | Tree operations with O(log n) LCA via binary lifting |
//! | [`conservation`] | Conservation metrics for forgetting validation |
//! | [`ircp`] | Inverse Ring Contextual Propagation attention |
//! | [`chainlink`] | ChainLink interaction framework with 4-component estimation |
//! | [`branch`] | Branch state machine for split/merge/recover operations |
//! | [`chain`] | Multi-chain management for conversation tracking |
//!
//! # Conceptual Framework
//!
//! - **Trajectory**: A sequence of experiences (episodes) forming a path through time
//! - **Episode**: A unit of experience (message turn, interaction, decision point)
//! - **Phase**: A qualitative state within a trajectory:
//! - `Exploration`: Initial inquiry, questions, topic discovery
//! - `Consolidation`: Building understanding, code implementation
//! - `Synthesis`: Summarization, decisions, conclusions
//! - `Debugging`: Error resolution, troubleshooting
//! - `Planning`: Roadmaps, structured plans
//! - **Salience**: Importance weight [0, 1] for bounded forgetting
//! - **Conservation**: Metrics ensuring memory operations preserve invariants
//!
//! # Coordinate System (5D DLM)
//!
//! Each episode has a 5D position (extending TPO with complexity):
//!
//! | Dimension | Type | Meaning |
//! |-----------|------|---------|
//! | `depth` | u32 | Distance from root (0 = root) |
//! | `sibling_order` | u32 | Position among siblings |
//! | `homogeneity` | f32 | Semantic similarity to parent [0, 1] |
//! | `temporal` | f32 | Normalized timestamp [0, 1] |
//! | `complexity` | u32 | Message complexity (n_parts from DLM) |
//!
//! # Dual Ring Structure (IRCP/RCP)
//!
//! The `DualRing` provides two simultaneous orderings over the same episodes:
//!
//! | Ring | Direction | Ordering | Use Case |
//! |------|-----------|----------|----------|
//! | **Temporal** (RCP) | Forward | Time/causal sequence | "What context led here?" |
//! | **Influence** (IRCP) | Inverse | By influence weight | "What had most impact?" |
//!
//! ```text
//! Temporal Ring (RCP): [E₀] → [E₁] → [E₂] → [E₃] → [E₄] ─┐
//! ↑ │
//! └─────────────────────────────────┘
//!
//! Influence Ring (IRCP): [E₂] → [E₄] → [E₀] → [E₃] → [E₁] ─┐
//! ↑ (sorted by influence) │
//! └────────────────────────────────┘
//! ```
//!
//! # Conservation Laws
//!
//! Per RCP, memory operations should preserve:
//!
//! | Law | Formula | Meaning |
//! |-----|---------|---------|
//! | Magnitude | Σ aᵢ‖eᵢ‖ = const | Context doesn't disappear |
//! | Energy | ½Σᵢⱼ aᵢaⱼ cos(eᵢ,eⱼ) | Attention capacity conserved |
//! | Information | -Σ aᵢ log(aᵢ) | Shannon entropy of attention |
//!
//! # Performance
//!
//! These algorithms are implemented in Rust for performance because they run
//! repeatedly during retrieval and corpus maintenance. Key optimizations:
//!
//! - SIMD-accelerated distance computations (via `distance` module)
//! - Cache-friendly contiguous storage in Ring topology
//! - O(1) neighbor access in ring structure
//! - Efficient hash-based DAG traversal
//!
//! # Example Usage
//!
//! ```ignore
//! use rag_plusplus_core::trajectory::{
//! TrajectoryGraph, Edge, EdgeType, PathSelectionPolicy,
//! PhaseInferencer, TurnFeatures, TrajectoryPhase,
//! SalienceScorer, SalienceFactors, Feedback,
//! Ring, TrajectoryCoordinate,
//! ConservationMetrics,
//! };
//!
//! // Build trajectory graph from edges
//! let edges = vec![
//! Edge { parent: 1, child: 2, edge_type: EdgeType::Continuation },
//! Edge { parent: 2, child: 3, edge_type: EdgeType::Continuation },
//! ];
//! let graph = TrajectoryGraph::from_edges(edges.iter().copied());
//!
//! // Find primary path through DAG
//! let primary = graph.find_primary_path(PathSelectionPolicy::FeedbackFirst);
//!
//! // Infer phases from episode features
//! let inferencer = PhaseInferencer::new();
//! let features = TurnFeatures::from_content(1, "user", "What is this?");
//! let (phase, confidence) = inferencer.infer_single(&features).unwrap();
//!
//! // Compute salience scores
//! let scorer = SalienceScorer::new();
//! let factors = SalienceFactors {
//! turn_id: 1,
//! feedback: Some(Feedback::ThumbsUp),
//! ..Default::default()
//! };
//! let salience = scorer.score_single(&factors, None);
//!
//! // Build ring structure
//! let ring = Ring::new(vec![1, 2, 3, 4, 5]);
//! let distance = ring.ring_distance(0, 3); // Shortest path: 2
//!
//! // Validate conservation
//! let embeddings = vec![vec![1.0, 0.0, 0.0], vec![0.0, 1.0, 0.0]];
//! let refs: Vec<&[f32]> = embeddings.iter().map(|e| e.as_slice()).collect();
//! let attention = vec![0.5, 0.5];
//! let metrics = ConservationMetrics::compute(&refs, &attention);
//! ```
// Re-exports
pub use ;
pub use ;
pub use ;
pub use ;
// Backward compatibility - ConversationPhase is deprecated in favor of TrajectoryPhase
pub use ConversationPhase;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;