{{ message }}
Repository navigation
Expand file tree
/
Copy pathepisodic_memory.rs
More file actions
738 lines (639 loc) · 22 KB
/
Copy pathepisodic_memory.rs
File metadata and controls
738 lines (639 loc) · 22 KB
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! Episodic Memory - Long-term memory for past trajectories and experiences
//!
//! Stores episodes with HNSW-indexed embeddings for efficient similarity search.
//! Supports memory compression for older episodes to manage storage.
use chrono::{DateTime, Duration, Utc};
use parking_lot::RwLock;
use ruvector_core::index::hnsw::HnswIndex;
use ruvector_core::index::VectorIndex;
use ruvector_core::types::{DistanceMetric, HnswConfig};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use crate::error::{Result, RuvLLMError};
/// Configuration for episodic memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EpisodicMemoryConfig {
/// Embedding dimension
pub embedding_dim: usize,
/// Maximum episodes to store
pub max_episodes: usize,
/// HNSW M parameter
pub hnsw_m: usize,
/// HNSW ef_construction parameter
pub hnsw_ef_construction: usize,
/// HNSW ef_search parameter
pub hnsw_ef_search: usize,
/// Age threshold for compression (in days)
pub compression_age_days: i64,
/// Compression ratio (0.0 - 1.0, lower = more compression)
pub compression_ratio: f32,
/// Enable automatic compression
pub auto_compress: bool,
}
impl Default for EpisodicMemoryConfig {
fn default() -> Self {
Self {
embedding_dim: 768,
max_episodes: 10_000,
hnsw_m: 16,
hnsw_ef_construction: 100,
hnsw_ef_search: 50,
compression_age_days: 7,
compression_ratio: 0.5,
auto_compress: true,
}
}
}
/// A trajectory representing a sequence of actions and states
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trajectory {
/// Trajectory identifier
pub id: String,
/// Sequence of state-action pairs
pub steps: Vec<TrajectoryStep>,
/// Final outcome (success: 1.0, failure: 0.0)
pub outcome: f32,
/// Quality score
pub quality_score: f32,
/// Task type
pub task_type: String,
/// Agent that executed this trajectory
pub agent_type: Option<String>,
/// Total duration
pub duration_ms: u64,
/// Created timestamp
pub created_at: DateTime<Utc>,
}
/// A single step in a trajectory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrajectoryStep {
/// State description
pub state: String,
/// Action taken
pub action: String,
/// Result of action
pub result: Option<String>,
/// Step embedding
pub embedding: Option<Vec<f32>>,
/// Reward signal
pub reward: f32,
/// Timestamp
pub timestamp: DateTime<Utc>,
}
/// Episode metadata for indexing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EpisodeMetadata {
/// Episode ID
pub episode_id: String,
/// Task description
pub task_description: String,
/// Task type
pub task_type: String,
/// Outcome (0.0-1.0)
pub outcome: f32,
/// Quality score
pub quality_score: f32,
/// Agent used
pub agent_type: Option<String>,
/// Number of steps
pub step_count: usize,
/// Duration in milliseconds
pub duration_ms: u64,
/// Is compressed
pub is_compressed: bool,
/// Tags for filtering
pub tags: Vec<String>,
/// Created timestamp
pub created_at: DateTime<Utc>,
}
/// An episode in long-term memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Episode {
/// Episode ID
pub id: String,
/// Episode embedding (summary)
pub embedding: Vec<f32>,
/// Episode metadata
pub metadata: EpisodeMetadata,
/// Full trajectory (may be compressed)
pub trajectory: Option<Trajectory>,
/// Compressed representation
pub compressed: Option<CompressedEpisode>,
}
/// Compressed episode representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressedEpisode {
/// Compressed embedding (may be lower dimension)
pub embedding: Vec<f32>,
/// Summary text
pub summary: String,
/// Key observations
pub key_observations: Vec<String>,
/// Key actions
pub key_actions: Vec<String>,
/// Learned patterns
pub patterns: Vec<String>,
/// Original step count
pub original_step_count: usize,
/// Compression timestamp
pub compressed_at: DateTime<Utc>,
}
/// Memory compressor for old episodes
pub struct MemoryCompressor {
/// Compression ratio
ratio: f32,
/// Target embedding dimension (for dimensionality reduction)
target_dim: Option<usize>,
}
impl MemoryCompressor {
/// Create new compressor
pub fn new(ratio: f32, target_dim: Option<usize>) -> Self {
Self { ratio, target_dim }
}
/// Compress a trajectory into a summary
pub fn compress(&self, trajectory: &Trajectory) -> CompressedEpisode {
// Select key steps based on reward
let total_steps = trajectory.steps.len();
let keep_count = ((total_steps as f32) * self.ratio).max(1.0) as usize;
let mut steps_with_reward: Vec<(usize, &TrajectoryStep)> =
trajectory.steps.iter().enumerate().collect();
steps_with_reward.sort_by(|a, b| {
b.1.reward
.partial_cmp(&a.1.reward)
.unwrap_or(std::cmp::Ordering::Equal)
});
let key_steps: Vec<&TrajectoryStep> = steps_with_reward
.into_iter()
.take(keep_count)
.map(|(_, s)| s)
.collect();
let key_observations: Vec<String> = key_steps.iter().map(|s| s.state.clone()).collect();
let key_actions: Vec<String> = key_steps.iter().map(|s| s.action.clone()).collect();
// Generate summary
let summary = format!(
"Task: {} | Outcome: {:.2} | Steps: {} | Key actions: {}",
trajectory.task_type,
trajectory.outcome,
total_steps,
key_actions.len()
);
// Extract patterns (simplified - in production, use clustering)
let patterns = self.extract_patterns(&key_actions);
// Compute compressed embedding (average of key step embeddings or reduce dimensions)
let embedding = self.compress_embedding(&key_steps);
CompressedEpisode {
embedding,
summary,
key_observations,
key_actions,
patterns,
original_step_count: total_steps,
compressed_at: Utc::now(),
}
}
/// Extract common patterns from actions
fn extract_patterns(&self, actions: &[String]) -> Vec<String> {
let mut patterns = Vec::new();
// Simple pattern extraction - look for repeated action types
let mut action_counts: HashMap<String, usize> = HashMap::new();
for action in actions {
// Extract action type (first word)
if let Some(action_type) = action.split_whitespace().next() {
*action_counts.entry(action_type.to_string()).or_insert(0) += 1;
}
}
// Keep patterns that appear more than once
for (pattern, count) in action_counts {
if count > 1 {
patterns.push(format!("{}:{}", pattern, count));
}
}
patterns
}
/// Compress embedding (average or reduce dimensions)
fn compress_embedding(&self, steps: &[&TrajectoryStep]) -> Vec<f32> {
let embeddings: Vec<&Vec<f32>> =
steps.iter().filter_map(|s| s.embedding.as_ref()).collect();
if embeddings.is_empty() {
return Vec::new();
}
let dim = embeddings[0].len();
let target_dim = self.target_dim.unwrap_or(dim);
// Average embeddings
let mut avg = vec![0.0f32; dim];
for emb in &embeddings {
for (i, v) in emb.iter().enumerate() {
avg[i] += v;
}
}
let n = embeddings.len() as f32;
for v in &mut avg {
*v /= n;
}
// Simple dimensionality reduction if needed (truncation - in production use PCA)
if target_dim < dim {
avg.truncate(target_dim);
}
avg
}
}
/// Statistics for episodic memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EpisodicMemoryStats {
/// Total episodes stored
pub total_episodes: u64,
/// Compressed episodes
pub compressed_episodes: u64,
/// Uncompressed episodes
pub uncompressed_episodes: u64,
/// Total searches
pub total_searches: u64,
/// Average search latency in microseconds
pub avg_search_latency_us: u64,
/// Successful retrievals
pub successful_retrievals: u64,
}
/// Long-term episodic memory with HNSW indexing
pub struct EpisodicMemory {
/// Configuration
config: EpisodicMemoryConfig,
/// HNSW index for similarity search
index: Arc<RwLock<HnswIndex>>,
/// Episode storage
episodes: Arc<RwLock<HashMap<String, Episode>>>,
/// Memory compressor
compressor: MemoryCompressor,
/// Statistics
stats: EpisodicMemoryStatsInternal,
}
#[derive(Debug, Default)]
struct EpisodicMemoryStatsInternal {
total_searches: AtomicU64,
successful_retrievals: AtomicU64,
total_search_latency_us: AtomicU64,
}
impl EpisodicMemory {
/// Create new episodic memory with configuration
pub fn new(config: EpisodicMemoryConfig) -> Result<Self> {
let hnsw_config = HnswConfig {
m: config.hnsw_m,
ef_construction: config.hnsw_ef_construction,
ef_search: config.hnsw_ef_search,
max_elements: config.max_episodes,
};
let index = HnswIndex::new(config.embedding_dim, DistanceMetric::Cosine, hnsw_config)
.map_err(|e| RuvLLMError::Ruvector(e.to_string()))?;
let compressor = MemoryCompressor::new(config.compression_ratio, None);
Ok(Self {
config,
index: Arc::new(RwLock::new(index)),
episodes: Arc::new(RwLock::new(HashMap::new())),
compressor,
stats: EpisodicMemoryStatsInternal::default(),
})
}
/// Store an episode from a trajectory
pub fn store_episode(
&self,
trajectory: Trajectory,
summary_embedding: Vec<f32>,
tags: Vec<String>,
) -> Result<String> {
let episode_id = trajectory.id.clone();
let metadata = EpisodeMetadata {
episode_id: episode_id.clone(),
task_description: trajectory.task_type.clone(),
task_type: trajectory.task_type.clone(),
outcome: trajectory.outcome,
quality_score: trajectory.quality_score,
agent_type: trajectory.agent_type.clone(),
step_count: trajectory.steps.len(),
duration_ms: trajectory.duration_ms,
is_compressed: false,
tags,
created_at: trajectory.created_at,
};
let episode = Episode {
id: episode_id.clone(),
embedding: summary_embedding.clone(),
metadata,
trajectory: Some(trajectory),
compressed: None,
};
// Add to HNSW index
{
let mut index = self.index.write();
index.add(episode_id.clone(), summary_embedding)?;
}
// Store episode
{
let mut episodes = self.episodes.write();
episodes.insert(episode_id.clone(), episode);
}
// Trigger compression if needed
if self.config.auto_compress {
self.compress_old_episodes()?;
}
// Enforce max episodes
self.enforce_limit()?;
Ok(episode_id)
}
/// Search for similar episodes
pub fn search_similar(&self, query_embedding: &[f32], k: usize) -> Result<Vec<Episode>> {
let start = std::time::Instant::now();
let results = {
let index = self.index.read();
index.search(query_embedding, k)?
};
let episodes = self.episodes.read();
let found: Vec<Episode> = results
.into_iter()
.filter_map(|r| episodes.get(&r.id).cloned())
.collect();
let latency = start.elapsed().as_micros() as u64;
self.stats.total_searches.fetch_add(1, Ordering::SeqCst);
self.stats
.total_search_latency_us
.fetch_add(latency, Ordering::SeqCst);
if !found.is_empty() {
self.stats
.successful_retrievals
.fetch_add(1, Ordering::SeqCst);
}
Ok(found)
}
/// Search with filtering
pub fn search_with_filter<F>(
&self,
query_embedding: &[f32],
k: usize,
filter: F,
) -> Result<Vec<Episode>>
where
F: Fn(&EpisodeMetadata) -> bool,
{
// Search more than needed to account for filtering
let search_k = k * 3;
let results = self.search_similar(query_embedding, search_k)?;
let filtered: Vec<Episode> = results
.into_iter()
.filter(|e| filter(&e.metadata))
.take(k)
.collect();
Ok(filtered)
}
/// Search by task type
pub fn search_by_task_type(
&self,
query_embedding: &[f32],
task_type: &str,
k: usize,
) -> Result<Vec<Episode>> {
self.search_with_filter(query_embedding, k, |meta| meta.task_type == task_type)
}
/// Search successful episodes only
pub fn search_successful(
&self,
query_embedding: &[f32],
min_quality: f32,
k: usize,
) -> Result<Vec<Episode>> {
self.search_with_filter(query_embedding, k, |meta| {
meta.outcome > 0.5 && meta.quality_score >= min_quality
})
}
/// Compress old episodes
pub fn compress_old_episodes(&self) -> Result<usize> {
let threshold = Utc::now() - Duration::days(self.config.compression_age_days);
let mut compressed_count = 0;
let episodes_to_compress: Vec<String> = {
let episodes = self.episodes.read();
episodes
.iter()
.filter(|(_, e)| {
e.metadata.created_at < threshold
&& !e.metadata.is_compressed
&& e.trajectory.is_some()
})
.map(|(id, _)| id.clone())
.collect()
};
for id in episodes_to_compress {
if let Some(episode) = self.episodes.write().get_mut(&id) {
if let Some(trajectory) = episode.trajectory.take() {
let compressed = self.compressor.compress(&trajectory);
episode.compressed = Some(compressed);
episode.metadata.is_compressed = true;
compressed_count += 1;
}
}
}
Ok(compressed_count)
}
/// Get episode by ID
pub fn get(&self, id: &str) -> Option<Episode> {
self.episodes.read().get(id).cloned()
}
/// Delete episode
pub fn delete(&self, id: &str) -> Result<bool> {
let removed = {
let mut episodes = self.episodes.write();
episodes.remove(id).is_some()
};
if removed {
let mut index = self.index.write();
index.remove(&id.to_string())?;
}
Ok(removed)
}
/// Enforce storage limit
fn enforce_limit(&self) -> Result<()> {
let mut episodes = self.episodes.write();
while episodes.len() > self.config.max_episodes {
// Find oldest compressed episode to remove
if let Some(oldest) = episodes
.iter()
.filter(|(_, e)| e.metadata.is_compressed)
.min_by_key(|(_, e)| e.metadata.created_at)
.map(|(id, _)| id.clone())
{
episodes.remove(&oldest);
let mut index = self.index.write();
let _ = index.remove(&oldest);
} else if let Some(oldest) = episodes
.iter()
.min_by_key(|(_, e)| e.metadata.created_at)
.map(|(id, _)| id.clone())
{
// Fall back to removing oldest uncompressed
episodes.remove(&oldest);
let mut index = self.index.write();
let _ = index.remove(&oldest);
} else {
break;
}
}
Ok(())
}
/// Get statistics
pub fn stats(&self) -> EpisodicMemoryStats {
let episodes = self.episodes.read();
let compressed = episodes
.iter()
.filter(|(_, e)| e.metadata.is_compressed)
.count() as u64;
let total = episodes.len() as u64;
let searches = self.stats.total_searches.load(Ordering::SeqCst);
let total_latency = self.stats.total_search_latency_us.load(Ordering::SeqCst);
let avg_latency = total_latency.checked_div(searches).unwrap_or(0);
EpisodicMemoryStats {
total_episodes: total,
compressed_episodes: compressed,
uncompressed_episodes: total - compressed,
total_searches: searches,
avg_search_latency_us: avg_latency,
successful_retrievals: self.stats.successful_retrievals.load(Ordering::SeqCst),
}
}
/// Clear all episodes
pub fn clear(&self) -> Result<()> {
self.episodes.write().clear();
// Recreate index
let hnsw_config = HnswConfig {
m: self.config.hnsw_m,
ef_construction: self.config.hnsw_ef_construction,
ef_search: self.config.hnsw_ef_search,
max_elements: self.config.max_episodes,
};
let new_index = HnswIndex::new(
self.config.embedding_dim,
DistanceMetric::Cosine,
hnsw_config,
)
.map_err(|e| RuvLLMError::Ruvector(e.to_string()))?;
*self.index.write() = new_index;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_embedding(dim: usize) -> Vec<f32> {
vec![0.1; dim]
}
fn test_trajectory() -> Trajectory {
Trajectory {
id: "traj-1".to_string(),
steps: vec![
TrajectoryStep {
state: "Initial state".to_string(),
action: "read_file /src/main.rs".to_string(),
result: Some("file contents".to_string()),
embedding: Some(vec![0.1; 128]),
reward: 0.5,
timestamp: Utc::now(),
},
TrajectoryStep {
state: "After reading".to_string(),
action: "edit_file /src/main.rs".to_string(),
result: Some("edited".to_string()),
embedding: Some(vec![0.2; 128]),
reward: 0.8,
timestamp: Utc::now(),
},
],
outcome: 1.0,
quality_score: 0.9,
task_type: "coding".to_string(),
agent_type: Some("coder".to_string()),
duration_ms: 5000,
created_at: Utc::now(),
}
}
#[test]
fn test_episodic_memory_creation() {
let config = EpisodicMemoryConfig {
embedding_dim: 128,
..Default::default()
};
let memory = EpisodicMemory::new(config).unwrap();
assert_eq!(memory.stats().total_episodes, 0);
}
#[test]
fn test_store_and_search() {
let config = EpisodicMemoryConfig {
embedding_dim: 128,
..Default::default()
};
let memory = EpisodicMemory::new(config).unwrap();
let trajectory = test_trajectory();
let embedding = test_embedding(128);
let id = memory
.store_episode(trajectory, embedding.clone(), vec!["test".to_string()])
.unwrap();
assert_eq!(id, "traj-1");
assert_eq!(memory.stats().total_episodes, 1);
let results = memory.search_similar(&embedding, 5).unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].id, "traj-1");
}
#[test]
fn test_search_with_filter() {
let config = EpisodicMemoryConfig {
embedding_dim: 128,
..Default::default()
};
let memory = EpisodicMemory::new(config).unwrap();
let trajectory = test_trajectory();
let embedding = test_embedding(128);
memory
.store_episode(trajectory, embedding.clone(), vec!["test".to_string()])
.unwrap();
// Filter by task type
let results = memory.search_by_task_type(&embedding, "coding", 5).unwrap();
assert_eq!(results.len(), 1);
let results = memory
.search_by_task_type(&embedding, "research", 5)
.unwrap();
assert_eq!(results.len(), 0);
}
#[test]
fn test_compression() {
let compressor = MemoryCompressor::new(0.5, None);
let trajectory = test_trajectory();
let compressed = compressor.compress(&trajectory);
assert!(!compressed.summary.is_empty());
assert!(!compressed.key_actions.is_empty());
assert_eq!(compressed.original_step_count, 2);
}
#[test]
fn test_delete() {
let config = EpisodicMemoryConfig {
embedding_dim: 128,
..Default::default()
};
let memory = EpisodicMemory::new(config).unwrap();
let trajectory = test_trajectory();
let embedding = test_embedding(128);
memory.store_episode(trajectory, embedding, vec![]).unwrap();
assert!(memory.get("traj-1").is_some());
assert!(memory.delete("traj-1").unwrap());
assert!(memory.get("traj-1").is_none());
}
#[test]
fn test_clear() {
let config = EpisodicMemoryConfig {
embedding_dim: 128,
..Default::default()
};
let memory = EpisodicMemory::new(config).unwrap();
let trajectory = test_trajectory();
let embedding = test_embedding(128);
memory.store_episode(trajectory, embedding, vec![]).unwrap();
assert_eq!(memory.stats().total_episodes, 1);
memory.clear().unwrap();
assert_eq!(memory.stats().total_episodes, 0);
}
}
You can’t perform that action at this time.
