codex-memory 3.0.15

A simple memory storage service with MCP interface for Claude Desktop
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
use crate::common::TestDatabaseManager;
use anyhow::Result;
use codex_memory::models::{SearchParams, SearchStrategy};
use codex_memory::Storage;
use serial_test::serial;
use std::sync::Arc;
use std::time::Instant;
use tokio::time::Duration;

#[tokio::test]
#[serial]
async fn test_search_performance_baseline() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Store 100 test memories for performance testing
    let start_setup = Instant::now();

    for i in 1..=100 {
        let content = format!("Test memory content number {} with various programming topics including rust, python, javascript, and database design", i);
        let context = format!("Performance test context for memory {}", i);
        let summary = format!("Performance test summary {}", i);
        let tags = vec![
            format!("test-{}", i % 10),
            "performance".to_string(),
            if i % 3 == 0 {
                "rust".to_string()
            } else if i % 3 == 1 {
                "python".to_string()
            } else {
                "javascript".to_string()
            },
            "programming".to_string(),
        ];

        storage
            .store(&content, context, summary, Some(tags))
            .await?;
    }

    let setup_duration = start_setup.elapsed();
    println!("Setup time for 100 memories: {:?}", setup_duration);
    assert!(
        setup_duration < Duration::from_secs(10),
        "Setup should complete within 10 seconds"
    );

    // Test search performance
    let search_params = SearchParams {
        query: "programming rust python".to_string(),
        similarity_threshold: 0.1,
        max_results: 10,
        ..Default::default()
    };

    // Warmup searches
    for _ in 0..3 {
        let _ = storage.search_memories(search_params.clone()).await?;
    }

    // Measure search performance
    let search_start = Instant::now();
    let results = storage.search_memories(search_params.clone()).await?;
    let search_duration = search_start.elapsed();

    println!("Search time for 100 memories: {:?}", search_duration);
    assert!(
        search_duration < Duration::from_millis(500),
        "Search should complete within 500ms"
    );
    assert!(!results.is_empty(), "Should find results");

    // Test multiple concurrent searches
    let concurrent_start = Instant::now();
    let mut handles = vec![];

    for i in 0..10 {
        let storage_clone = storage.clone();
        let params = SearchParams {
            query: format!("programming test {}", i),
            similarity_threshold: 0.1,
            max_results: 5,
            ..Default::default()
        };

        let handle = tokio::spawn(async move { storage_clone.search_memories(params).await });
        handles.push(handle);
    }

    let mut concurrent_results = vec![];
    for handle in handles {
        let result = handle.await??;
        concurrent_results.push(result);
    }

    let concurrent_duration = concurrent_start.elapsed();
    println!(
        "Concurrent search time (10 parallel): {:?}",
        concurrent_duration
    );
    assert!(
        concurrent_duration < Duration::from_secs(2),
        "Concurrent searches should complete within 2 seconds"
    );
    assert_eq!(
        concurrent_results.len(),
        10,
        "All concurrent searches should complete"
    );

    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_search_strategy_performance_comparison() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Store diverse content for strategy testing
    let content_types = [
        (
            "Machine learning algorithms and neural networks",
            "AI research",
            vec![
                "machine-learning".to_string(),
                "ai".to_string(),
                "algorithms".to_string(),
            ],
        ),
        (
            "Web development with React and Node.js",
            "Frontend development",
            vec![
                "web".to_string(),
                "react".to_string(),
                "javascript".to_string(),
            ],
        ),
        (
            "Database optimization techniques",
            "Backend development",
            vec![
                "database".to_string(),
                "optimization".to_string(),
                "sql".to_string(),
            ],
        ),
        (
            "Rust systems programming guide",
            "Programming tutorial",
            vec![
                "rust".to_string(),
                "systems".to_string(),
                "programming".to_string(),
            ],
        ),
        (
            "Python data analysis tutorial",
            "Data science",
            vec![
                "python".to_string(),
                "data-science".to_string(),
                "analysis".to_string(),
            ],
        ),
    ];

    // Store 20 of each type (100 total)
    for (base_content, base_context, base_tags) in content_types {
        for i in 1..=20 {
            let content = format!("{} - example {}", base_content, i);
            let context = format!("{} - instance {}", base_context, i);
            let summary = format!("Summary for example {}", i);
            storage
                .store(&content, context, summary, Some(base_tags.clone()))
                .await?;
        }
    }

    let query = "machine learning algorithms";
    let strategies = [
        SearchStrategy::TagsFirst,
        SearchStrategy::ContentFirst,
        SearchStrategy::Hybrid,
    ];

    let mut strategy_timings = vec![];

    for strategy in strategies {
        let params = SearchParams {
            query: query.to_string(),
            search_strategy: strategy.clone(),
            similarity_threshold: 0.1,
            max_results: 10,
            ..Default::default()
        };

        // Warmup
        let _ = storage.search_memories(params.clone()).await?;

        // Measure performance
        let start = Instant::now();
        let results = storage.search_memories(params).await?;
        let duration = start.elapsed();

        let strategy_clone = strategy.clone();
        strategy_timings.push((strategy_clone, duration, results.len()));
        println!(
            "Strategy {:?}: {:?} ({} results)",
            strategy,
            duration,
            results.len()
        );

        // All strategies should complete quickly
        assert!(
            duration < Duration::from_millis(200),
            "Strategy {:?} should complete within 200ms",
            strategy
        );
        assert!(
            !results.is_empty(),
            "Strategy {:?} should find results",
            strategy
        );
    }

    // Verify all strategies performed reasonably
    for (strategy, timing, _) in &strategy_timings {
        assert!(
            timing < &Duration::from_millis(200),
            "Strategy {:?} performance regression",
            strategy
        );
    }

    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_large_result_set_performance() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Store 500 memories with common terms
    let start_bulk_store = Instant::now();

    for i in 1..=500 {
        let content = format!("Programming tutorial {} covering rust python javascript and web development frameworks", i);
        let context = "Tutorial content".to_string();
        let summary = format!("Tutorial summary {}", i);
        let tags = vec!["programming".to_string(), "tutorial".to_string()];

        storage
            .store(&content, context, summary, Some(tags))
            .await?;
    }

    let bulk_store_duration = start_bulk_store.elapsed();
    println!(
        "Bulk store time for 500 memories: {:?}",
        bulk_store_duration
    );

    // Test different result set sizes
    let result_sizes = [10, 50, 100, 200];

    for &max_results in &result_sizes {
        let params = SearchParams {
            query: "programming tutorial".to_string(),
            max_results,
            similarity_threshold: 0.1,
            ..Default::default()
        };

        let start = Instant::now();
        let results = storage.search_memories(params).await?;
        let duration = start.elapsed();

        println!(
            "Search for {} results: {:?} (found {})",
            max_results,
            duration,
            results.len()
        );

        // Performance should scale reasonably
        assert!(
            duration < Duration::from_secs(1),
            "Search for {} results should complete within 1 second",
            max_results
        );
        assert!(
            results.len() <= max_results,
            "Should not exceed max_results limit"
        );
        assert!(!results.is_empty(), "Should find results");

        // Verify results are properly sorted
        let mut prev_score = 1.0;
        for result in &results {
            assert!(
                result.combined_score <= prev_score,
                "Results should be sorted by descending score"
            );
            prev_score = result.combined_score;
        }
    }

    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_memory_usage_under_load() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Store memories with varying content sizes
    let content_sizes = [100, 500, 1000, 2000]; // characters

    for &size in &content_sizes {
        for i in 1..=25 {
            let content = "Programming tutorial content "
                .repeat(size / 30)
                .chars()
                .take(size)
                .collect::<String>();
            let context = format!("Context for size {} memory {}", size, i);
            let summary = format!("Summary {}", i);
            let tags = vec![format!("size-{}", size), "performance".to_string()];

            storage
                .store(&content, context, summary, Some(tags))
                .await?;
        }
    }

    // Test searches with different content size filters
    for &size in &content_sizes {
        let params = SearchParams {
            query: "programming tutorial".to_string(),
            tag_filter: Some(vec![format!("size-{}", size)]),
            max_results: 20,
            ..Default::default()
        };

        let start = Instant::now();
        let results = storage.search_memories(params).await?;
        let duration = start.elapsed();

        println!(
            "Search content size {}: {:?} ({} results)",
            size,
            duration,
            results.len()
        );
        assert!(
            duration < Duration::from_millis(300),
            "Search should handle content size {} efficiently",
            size
        );

        // Verify all results have the expected size tag
        for result in &results {
            assert!(result.memory.tags.contains(&format!("size-{}", size)));
        }
    }

    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_stress_concurrent_operations() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Pre-populate with some data
    for i in 1..=50 {
        let content = format!("Stress test content {}", i);
        let context = "Stress test context".to_string();
        let summary = format!("Stress summary {}", i);
        let tags = vec!["stress".to_string(), "test".to_string()];

        storage
            .store(&content, context, summary, Some(tags))
            .await?;
    }

    let start_stress = Instant::now();
    let mut handles = vec![];

    // Launch concurrent operations: stores, searches, and retrievals
    for i in 0..20 {
        let storage_clone = storage.clone();

        if i % 3 == 0 {
            // Store operation
            let handle = tokio::spawn(async move {
                let content = format!("Concurrent store operation {}", i);
                let context = "Concurrent context".to_string();
                let summary = format!("Concurrent summary {}", i);
                let tags = Some(vec!["concurrent".to_string()]);

                storage_clone.store(&content, context, summary, tags).await
            });
            handles.push(handle);
        } else {
            // Search operation
            let handle = tokio::spawn(async move {
                let params = SearchParams {
                    query: format!("stress test {}", i),
                    max_results: 5,
                    ..Default::default()
                };
                storage_clone
                    .search_memories(params)
                    .await
                    .map(|_| uuid::Uuid::new_v4())
            });
            handles.push(handle);
        }
    }

    // Wait for all operations to complete
    let mut results = vec![];
    for handle in handles {
        let result = handle.await?;
        results.push(result);
    }

    let stress_duration = start_stress.elapsed();
    println!(
        "Stress test duration (20 concurrent ops): {:?}",
        stress_duration
    );

    assert!(
        stress_duration < Duration::from_secs(5),
        "Stress test should complete within 5 seconds"
    );
    assert_eq!(
        results.len(),
        20,
        "All concurrent operations should complete"
    );

    // Verify database integrity after stress
    let final_params = SearchParams {
        query: "stress test".to_string(),
        max_results: 100,
        ..Default::default()
    };

    let final_results = storage.search_memories(final_params).await?;
    assert!(
        !final_results.is_empty(),
        "Database should remain functional after stress"
    );

    manager.cleanup().await?;
    Ok(())
}