mr-ability 0.6.0

Core ability library for MemRec
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
//! # 分层检索引擎
//!
//! 协调 TierAssigner 和 BudgetController,实现分层检索。

use anyhow::Result;
use mr_common::{
    EdgeHint, Memory, MemoryEdge, RepresentationTier, TierThresholds, TieredMemory,
    TieredSearchResult,
};
use uuid::Uuid;

use crate::storage::{EdgeStorage, MemoryStorage, TieredStorage};
use crate::tiered::{BudgetController, TierAssigner};

#[derive(Debug, Clone)]
pub struct TieredEngineConfig {
    pub max_tokens: u32,
    pub thresholds: TierThresholds,
}

impl Default for TieredEngineConfig {
    fn default() -> Self {
        TieredEngineConfig {
            max_tokens: 4000,
            thresholds: TierThresholds::default(),
        }
    }
}

pub struct TieredEngine<M, E, T>
where
    M: MemoryStorage,
    E: EdgeStorage,
    T: TieredStorage,
{
    memory_store: M,
    edge_store: E,
    tiered_store: T,
    assigner: TierAssigner,
    budget: BudgetController,
}

impl<M, E, T> TieredEngine<M, E, T>
where
    M: MemoryStorage,
    E: EdgeStorage,
    T: TieredStorage,
{
    pub fn new(
        memory_store: M,
        edge_store: E,
        tiered_store: T,
        config: TieredEngineConfig,
    ) -> Self {
        let assigner = TierAssigner::new(config.thresholds);
        let budget = BudgetController::new(config.max_tokens);
        TieredEngine {
            memory_store,
            edge_store,
            tiered_store,
            assigner,
            budget,
        }
    }

    pub async fn build_tiered_result(
        &self,
        memory_ids: &[Uuid],
        scores: &[f32],
    ) -> Result<TieredSearchResult> {
        let mut results = Vec::with_capacity(memory_ids.len());

        for (memory_id, score) in memory_ids.iter().zip(scores.iter()) {
            if let Some(memory) = self.memory_store.get(memory_id).await? {
                let tiered = self.build_tiered_memory(memory, *score).await?;
                results.push(tiered);
            }
        }

        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let total_tokens = self.budget.check_and_downgrade(&mut results);

        Ok(TieredSearchResult {
            total_results: results.len(),
            total_tokens,
            budget: self.budget.max_tokens(),
            results,
        })
    }

    async fn build_tiered_memory(&self, memory: Memory, score: f32) -> Result<TieredMemory> {
        let tier = self.assigner.assign(score);
        let (content, token_count) = self.generate_tier_content(&memory, tier).await?;

        let facet_themes = vec![];
        let edge_hints = self.build_edge_hints(&memory.id).await?;

        Ok(TieredMemory {
            memory_id: memory.id,
            tier,
            content,
            score,
            token_count,
            facet_themes,
            edge_hints,
        })
    }

    async fn generate_tier_content(
        &self,
        memory: &Memory,
        tier: RepresentationTier,
    ) -> Result<(String, u32)> {
        match tier {
            RepresentationTier::Full => {
                let tokens = BudgetController::estimate_token_count(&memory.content);
                Ok((memory.content.clone(), tokens))
            }
            RepresentationTier::Truncated => {
                let truncated = self.truncate_content(&memory.content, 200);
                let tokens = BudgetController::estimate_token_count(&truncated);
                Ok((truncated, tokens.min(200)))
            }
            RepresentationTier::Summary => {
                if let Some(summary) = self.tiered_store.get_summary(&memory.id).await? {
                    let tokens = BudgetController::estimate_token_count(&summary);
                    Ok((summary, tokens.min(80)))
                } else {
                    let truncated = self.truncate_content(&memory.content, 80);
                    Ok((truncated, 80))
                }
            }
            RepresentationTier::DenseProxy => {
                if let Some(keywords) = self.tiered_store.get_keywords(&memory.id).await? {
                    let proxy = format!("[{}]", keywords.join(", "));
                    Ok((proxy, 30))
                } else {
                    let tags = memory.tags.clone();
                    let proxy = if tags.is_empty() {
                        "[no keywords]".to_string()
                    } else {
                        format!("[{}]", tags.join(", "))
                    };
                    Ok((proxy, 30))
                }
            }
        }
    }

    fn truncate_content(&self, content: &str, max_tokens: u32) -> String {
        let char_limit = (max_tokens as f32 / 0.4) as usize;
        if content.chars().count() <= char_limit {
            return content.to_string();
        }

        let truncated: String = content.chars().take(char_limit).collect();
        let mut result = truncated;

        for end in (0..result.len()).rev() {
            let c = result.chars().nth(end);
            if let Some(c) = c {
                if c == '' || c == '' || c == '' || c == '.' || c == '!' || c == '?' {
                    result = result.chars().take(end + 1).collect();
                    result.push_str("...");
                    return result;
                }
            }
        }

        result.push_str("...");
        result
    }

    async fn build_edge_hints(&self, memory_id: &Uuid) -> Result<Vec<EdgeHint>> {
        let edges = self.edge_store.neighbors(memory_id).await?;
        let mut hints = Vec::new();

        for edge in edges.iter().take(3) {
            let target_summary = self.get_target_summary(edge).await?;
            hints.push(EdgeHint {
                edge_type: edge.edge_type,
                target_summary,
                weight: edge.weight,
            });
        }

        Ok(hints)
    }

    async fn get_target_summary(&self, edge: &MemoryEdge) -> Result<String> {
        let target_id = edge.target_id;
        if let Some(summary) = self.tiered_store.get_summary(&target_id).await? {
            return Ok(summary);
        }

        if let Some(memory) = self.memory_store.get(&target_id).await? {
            let truncated = self.truncate_content(&memory.content, 50);
            return Ok(truncated);
        }

        Ok("unknown".to_string())
    }

    pub fn assigner(&self) -> &TierAssigner {
        &self.assigner
    }

    pub fn budget(&self) -> &BudgetController {
        &self.budget
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::{GraphSearchHit, GraphTraversalParams};
    use async_trait::async_trait;
    use mr_common::{EdgeType, MemorySource, MemoryType};

    struct MockMemoryStore {
        memories: std::sync::Arc<tokio::sync::Mutex<std::collections::HashMap<Uuid, Memory>>>,
    }

    impl MockMemoryStore {
        fn new() -> Self {
            MockMemoryStore {
                memories: std::sync::Arc::new(tokio::sync::Mutex::new(
                    std::collections::HashMap::new(),
                )),
            }
        }

        async fn insert(&self, memory: Memory) {
            let mut map = self.memories.lock().await;
            map.insert(memory.id, memory);
        }
    }

    #[async_trait]
    impl MemoryStorage for MockMemoryStore {
        async fn save(&self, memory: &Memory) -> Result<()> {
            let mut map = self.memories.lock().await;
            map.insert(memory.id, memory.clone());
            Ok(())
        }
        async fn get(&self, id: &Uuid) -> Result<Option<Memory>> {
            let map = self.memories.lock().await;
            Ok(map.get(id).cloned())
        }
        async fn update(&self, memory: &Memory) -> Result<()> {
            self.save(memory).await
        }
        async fn delete(&self, id: &Uuid) -> Result<bool> {
            let mut map = self.memories.lock().await;
            Ok(map.remove(id).is_some())
        }
        async fn list(&self, limit: usize) -> Result<Vec<Memory>> {
            let map = self.memories.lock().await;
            Ok(map.values().take(limit).cloned().collect())
        }
        async fn list_by_type(
            &self,
            _memory_type: MemoryType,
            limit: usize,
        ) -> Result<Vec<Memory>> {
            self.list(limit).await
        }
        async fn list_by_tag(&self, _tag: &str, limit: usize) -> Result<Vec<Memory>> {
            self.list(limit).await
        }
        async fn list_by_importance(&self, _min: f32, _max: f32) -> Result<Vec<Memory>> {
            self.list(100).await
        }
        async fn list_deleted(&self) -> Result<Vec<Memory>> {
            Ok(vec![])
        }
        async fn count(&self) -> Result<usize> {
            let map = self.memories.lock().await;
            Ok(map.len())
        }
        async fn count_deleted(&self) -> Result<usize> {
            Ok(0)
        }
        async fn list_by_project(&self, _project_id: &Uuid) -> Result<Vec<Memory>> {
            self.list(100).await
        }
        async fn get_chunks_by_group(&self, _chunk_group_id: &Uuid) -> Result<Vec<Memory>> {
            Ok(vec![])
        }
        async fn list_older_than(&self, _cutoff: &str, limit: usize) -> Result<Vec<Memory>> {
            self.list(limit).await
        }
    }

    struct MockEdgeStore {
        edges: std::sync::Arc<tokio::sync::Mutex<Vec<MemoryEdge>>>,
    }

    impl MockEdgeStore {
        fn new() -> Self {
            MockEdgeStore {
                edges: std::sync::Arc::new(tokio::sync::Mutex::new(vec![])),
            }
        }
    }

    #[async_trait]
    impl EdgeStorage for MockEdgeStore {
        async fn save(&self, edge: &MemoryEdge) -> Result<()> {
            let mut edges = self.edges.lock().await;
            edges.push(edge.clone());
            Ok(())
        }
        async fn get(&self, _id: &Uuid) -> Result<Option<MemoryEdge>> {
            Ok(None)
        }
        async fn delete(&self, _id: &Uuid) -> Result<bool> {
            Ok(false)
        }
        async fn list_out_edges(&self, _source_id: &Uuid) -> Result<Vec<MemoryEdge>> {
            Ok(vec![])
        }
        async fn list_in_edges(&self, _target_id: &Uuid) -> Result<Vec<MemoryEdge>> {
            Ok(vec![])
        }
        async fn list_by_type(&self, _edge_type: EdgeType) -> Result<Vec<MemoryEdge>> {
            Ok(vec![])
        }
        async fn count(&self) -> Result<usize> {
            let edges = self.edges.lock().await;
            Ok(edges.len())
        }
        async fn neighbors(&self, _memory_id: &Uuid) -> Result<Vec<MemoryEdge>> {
            Ok(vec![])
        }
        async fn traverse(
            &self,
            _seed_ids: &[Uuid],
            _params: GraphTraversalParams,
        ) -> Result<Vec<GraphSearchHit>> {
            Ok(vec![])
        }
    }

    struct MockTieredStore {
        summaries: std::sync::Arc<tokio::sync::Mutex<std::collections::HashMap<Uuid, String>>>,
        keywords: std::sync::Arc<tokio::sync::Mutex<std::collections::HashMap<Uuid, Vec<String>>>>,
    }

    impl MockTieredStore {
        fn new() -> Self {
            MockTieredStore {
                summaries: std::sync::Arc::new(tokio::sync::Mutex::new(
                    std::collections::HashMap::new(),
                )),
                keywords: std::sync::Arc::new(tokio::sync::Mutex::new(
                    std::collections::HashMap::new(),
                )),
            }
        }
    }

    #[async_trait]
    impl TieredStorage for MockTieredStore {
        async fn save_summary(&self, memory_id: &Uuid, summary: &str) -> Result<()> {
            let mut map = self.summaries.lock().await;
            map.insert(*memory_id, summary.to_string());
            Ok(())
        }
        async fn get_summary(&self, memory_id: &Uuid) -> Result<Option<String>> {
            let map = self.summaries.lock().await;
            Ok(map.get(memory_id).cloned())
        }
        async fn delete_summary(&self, memory_id: &Uuid) -> Result<bool> {
            let mut map = self.summaries.lock().await;
            Ok(map.remove(memory_id).is_some())
        }
        async fn save_keywords(&self, memory_id: &Uuid, keywords: &[String]) -> Result<()> {
            let mut map = self.keywords.lock().await;
            map.insert(*memory_id, keywords.to_vec());
            Ok(())
        }
        async fn get_keywords(&self, memory_id: &Uuid) -> Result<Option<Vec<String>>> {
            let map = self.keywords.lock().await;
            Ok(map.get(memory_id).cloned())
        }
        async fn delete_keywords(&self, memory_id: &Uuid) -> Result<bool> {
            let mut map = self.keywords.lock().await;
            Ok(map.remove(memory_id).is_some())
        }
    }

    fn make_memory(content: &str, tags: Vec<&str>) -> Memory {
        use std::collections::HashMap;
        let now = chrono::Utc::now();
        Memory {
            id: Uuid::new_v4(),
            content: content.to_string(),
            memory_type: MemoryType::Decision,
            source: MemorySource::User,
            project_id: None,
            tags: tags.into_iter().map(|s| s.to_string()).collect(),
            importance: 0.5,
            created_at: now,
            last_accessed: now,
            access_count: 0,
            summary: None,
            embedding: None,
            metadata: HashMap::new(),
            is_deleted: false,
            deleted_at: None,
            chunk_group_id: None,
            chunk_index: None,
            chunk_total: None,
            scope: mr_common::MemoryScope::default(),
        }
    }

    #[tokio::test]
    async fn test_build_tiered_result() {
        let memory_store = MockMemoryStore::new();
        let edge_store = MockEdgeStore::new();
        let tiered_store = MockTieredStore::new();

        let m1 = make_memory("This is a full content test", vec!["test"]);
        let m2 = make_memory("Short content", vec!["short"]);
        let id1 = m1.id;
        let id2 = m2.id;

        memory_store.insert(m1).await;
        memory_store.insert(m2).await;

        let engine = TieredEngine::new(
            memory_store,
            edge_store,
            tiered_store,
            TieredEngineConfig::default(),
        );

        let result = engine
            .build_tiered_result(&[id1, id2], &[0.9, 0.4])
            .await
            .unwrap();

        assert_eq!(result.total_results, 2);
        assert!(result.total_tokens > 0);
        assert_eq!(result.budget, 4000);
        assert_eq!(result.results[0].tier, RepresentationTier::Full);
        assert_eq!(result.results[1].tier, RepresentationTier::Summary);
    }

    #[tokio::test]
    async fn test_truncate_content() {
        let memory_store = MockMemoryStore::new();
        let edge_store = MockEdgeStore::new();
        let tiered_store = MockTieredStore::new();

        let engine = TieredEngine::new(
            memory_store,
            edge_store,
            tiered_store,
            TieredEngineConfig::default(),
        );

        let short = "This is short.";
        let truncated = engine.truncate_content(short, 200);
        assert_eq!(truncated, short);

        let long = "This is a very long content that should be truncated at a sentence boundary. And this is another sentence.";
        let truncated = engine.truncate_content(long, 10);
        assert!(truncated.ends_with("..."));
    }

    #[tokio::test]
    async fn test_tiered_result_with_keywords() {
        let memory_store = MockMemoryStore::new();
        let edge_store = MockEdgeStore::new();
        let tiered_store = MockTieredStore::new();

        let m = make_memory("Content", vec![]);
        let id = m.id;
        memory_store.insert(m).await;

        tiered_store
            .save_keywords(&id, &["rust".to_string(), "async".to_string()])
            .await
            .unwrap();

        let engine = TieredEngine::new(
            memory_store,
            edge_store,
            tiered_store,
            TieredEngineConfig::default(),
        );

        let result = engine.build_tiered_result(&[id], &[0.1]).await.unwrap();

        assert_eq!(result.results[0].tier, RepresentationTier::DenseProxy);
        assert!(result.results[0].content.contains("rust"));
    }
}