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
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
use crate::common::TestDatabaseManager;
use anyhow::Result;
use codex_memory::{mcp_server::MCPHandlers, Storage};
use serde_json::json;
use serial_test::serial;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::time::timeout;

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

    // Test storing many items rapidly
    let num_items = 1000;
    let content_base = "High throughput test content item ";

    println!("Testing storage of {} items...", num_items);
    let start = Instant::now();

    let mut handles = vec![];
    for i in 0..num_items {
        let storage_clone = storage.clone();
        let content = format!("{}{}", content_base, i);

        let handle = tokio::spawn(async move {
            storage_clone
                .store(
                    &content,
                    format!("Context for item {}", i),
                    format!("Summary of item {}", i),
                    Some(vec![format!("tag-{}", i), "stress-test".to_string()]),
                )
                .await
        });
        handles.push(handle);
    }

    let mut successes = 0;
    let mut failures = 0;

    for handle in handles {
        match handle.await {
            Ok(Ok(_)) => successes += 1,
            Ok(Err(e)) => {
                println!("Storage failed: {}", e);
                failures += 1;
            }
            Err(e) => {
                println!("Task failed: {}", e);
                failures += 1;
            }
        }
    }

    let duration = start.elapsed();
    let throughput = successes as f64 / duration.as_secs_f64();

    println!("High throughput test results:");
    println!("  Duration: {:?}", duration);
    println!("  Successes: {}/{}", successes, num_items);
    println!("  Failures: {}", failures);
    println!("  Throughput: {:.2} items/second", throughput);

    // Performance expectations (adjust based on system capabilities)
    assert!(
        successes > (num_items * 90 / 100),
        "At least 90% should succeed"
    );
    assert!(throughput > 10.0, "Should achieve at least 10 items/second");
    assert!(
        duration < Duration::from_secs(120),
        "Should complete within 2 minutes"
    );

    // Verify we can retrieve all stored items
    let stats = storage.stats().await?;
    assert!(
        stats.total_memories >= successes as i64,
        "All successful items should be stored"
    );

    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));

    // Monitor memory usage during intensive operations
    let mut base_memory = get_memory_usage();
    println!("Initial memory usage: {} bytes", base_memory);

    // Create content of various sizes to stress memory management
    let content_sizes = vec![1024, 10_240, 102_400, 1_024_000]; // 1KB to 1MB
    let items_per_size = 100;

    for size in content_sizes {
        println!("Testing {} items of {} bytes each", items_per_size, size);

        let content = "x".repeat(size);
        let mut handles = vec![];

        let size_start = Instant::now();

        for i in 0..items_per_size {
            let storage_clone = storage.clone();
            let content_clone = content.clone();

            let handle = tokio::spawn(async move {
                storage_clone
                    .store(
                        &content_clone,
                        format!("Context for {}B item #{}", size, i),
                        "Test summary".to_string(),
                        Some(vec![format!("size-{}", size)]),
                    )
                    .await
            });
            handles.push(handle);
        }

        // Wait for all items of this size
        for handle in handles {
            let _ = handle.await;
        }

        let size_duration = size_start.elapsed();
        let current_memory = get_memory_usage();

        // Use saturating subtraction to handle cases where memory decreases
        // (e.g., due to garbage collection or other processes freeing memory)
        let memory_increase = current_memory.saturating_sub(base_memory);

        println!("  Duration: {:?}", size_duration);
        println!("  Memory increase: {} bytes", memory_increase);

        // Avoid division by zero and handle zero memory increase
        if memory_increase > 0 {
            println!(
                "  Memory per item: {} bytes",
                memory_increase / items_per_size as u64
            );
        } else {
            println!("  Memory per item: 0 bytes (memory decreased or unchanged)");
        }

        // Check for reasonable memory usage (not exact due to system variance)
        let expected_data = size * items_per_size;

        // Skip validation if memory didn't increase (can happen due to GC or pooling)
        if memory_increase > 0 {
            let memory_overhead_ratio = memory_increase as f64 / expected_data as f64;

            println!("  Memory overhead ratio: {:.2}x", memory_overhead_ratio);

            // Memory usage should be reasonable (allow for overhead but not excessive)
            // Database systems have significant overhead for indexes, metadata, connection pools, etc.
            // Allow higher threshold for smaller data sizes where fixed overhead dominates
            // Updated thresholds based on real-world PostgreSQL overhead patterns with generous margins for CI/test variability
            let threshold = if size <= 1_024 {
                2000.0
            } else if size <= 10_240 {
                200.0
            } else {
                100.0
            };
            assert!(
                memory_overhead_ratio < threshold,
                "Memory overhead should not exceed {}x data size (was {:.2}x)",
                threshold,
                memory_overhead_ratio
            );
        } else {
            println!("  Memory overhead ratio: N/A (memory decreased)");
        }

        // Update base memory for next iteration
        base_memory = current_memory;
    }

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

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

    // Pre-populate some data for reading
    let mut stored_ids = vec![];
    for i in 0..100 {
        let id = storage
            .store(
                &format!("Pre-stored content {}", i),
                format!("Context {}", i),
                format!("Summary {}", i),
                Some(vec![format!("prestored-{}", i)]),
            )
            .await?;
        stored_ids.push(id);
    }

    println!("Testing concurrent read/write operations...");

    let start = Instant::now();
    let mut write_handles = vec![];
    let mut read_handles = vec![];

    // Launch concurrent writers
    for i in 0..50 {
        let storage_clone = storage.clone();
        let handle = tokio::spawn(async move {
            storage_clone
                .store(
                    &format!("Concurrent write {}", i),
                    format!("Write context {}", i),
                    "Test summary".to_string(),
                    Some(vec!["concurrent-write".to_string()]),
                )
                .await
        });
        write_handles.push(handle);
    }

    // Launch concurrent readers
    for i in 0..50 {
        let storage_clone = storage.clone();
        let ids_clone = stored_ids.clone();

        let handle = tokio::spawn(async move {
            // Use simple round-robin selection instead of random
            let id = ids_clone[i % ids_clone.len()];
            storage_clone.get(id).await
        });
        read_handles.push(handle);
    }

    // Wait for all operations
    let mut write_successes = 0;
    let mut read_successes = 0;
    let mut failures = 0;

    // Process write handles
    for handle in write_handles {
        match handle.await {
            Ok(Ok(_)) => write_successes += 1,
            Ok(Err(e)) => {
                println!("Write operation failed: {}", e);
                failures += 1;
            }
            Err(e) => {
                println!("Write task failed: {}", e);
                failures += 1;
            }
        }
    }

    // Process read handles
    for handle in read_handles {
        match handle.await {
            Ok(Ok(_)) => read_successes += 1,
            Ok(Err(e)) => {
                println!("Read operation failed: {}", e);
                failures += 1;
            }
            Err(e) => {
                println!("Read task failed: {}", e);
                failures += 1;
            }
        }
    }

    let duration = start.elapsed();
    let total_ops = write_successes + read_successes;
    let ops_per_second = total_ops as f64 / duration.as_secs_f64();

    println!("Concurrent R/W test results:");
    println!("  Duration: {:?}", duration);
    println!("  Write successes: {}/50", write_successes);
    println!("  Read successes: {}/50", read_successes);
    println!("  Failures: {}", failures);
    println!("  Operations per second: {:.2}", ops_per_second);

    // Performance expectations
    assert!(write_successes > 45, "Most writes should succeed");
    assert!(read_successes > 45, "Most reads should succeed");
    assert!(
        ops_per_second > 20.0,
        "Should achieve reasonable throughput"
    );
    assert!(
        duration < Duration::from_secs(30),
        "Should complete quickly"
    );

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

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

    // Stress test the connection pool with many simultaneous operations
    println!("Stress testing database connection pool...");

    let concurrent_operations = 100;
    let mut handles = vec![];

    let start = Instant::now();

    for i in 0..concurrent_operations {
        let storage_clone = storage.clone();

        let handle = tokio::spawn(async move {
            // Mix different types of operations to stress different connection patterns
            match i % 4 {
                0 => {
                    // Store operation
                    storage_clone
                        .store(
                            &format!("Connection stress test {}", i),
                            "Test context".to_string(),
                            "Test summary".to_string(),
                            Some(vec!["connection-stress".to_string()]),
                        )
                        .await
                }
                1 => {
                    // Stats operation
                    storage_clone.stats().await.map(|_| uuid::Uuid::new_v4())
                }
                2 => {
                    // Search operation (if available - fallback to stats)
                    storage_clone.stats().await.map(|_| uuid::Uuid::new_v4())
                }
                3 => {
                    // Multiple quick stats operations
                    for _ in 0..3 {
                        let _ = storage_clone.stats().await;
                    }
                    Ok(uuid::Uuid::new_v4())
                }
                _ => unreachable!(),
            }
        });
        handles.push(handle);
    }

    // Wait for all operations with timeout
    let mut successes = 0;
    let mut failures = 0;
    let mut timeouts = 0;

    for handle in handles {
        match timeout(Duration::from_secs(30), handle).await {
            Ok(Ok(Ok(_))) => successes += 1,
            Ok(Ok(Err(e))) => {
                println!("Operation failed: {}", e);
                failures += 1;
            }
            Ok(Err(e)) => {
                println!("Task failed: {}", e);
                failures += 1;
            }
            Err(_) => {
                println!("Operation timed out");
                timeouts += 1;
            }
        }
    }

    let duration = start.elapsed();

    println!("Connection pool stress test results:");
    println!("  Duration: {:?}", duration);
    println!("  Successes: {}/{}", successes, concurrent_operations);
    println!("  Failures: {}", failures);
    println!("  Timeouts: {}", timeouts);

    // Most operations should succeed (allow for some connection contention)
    assert!(
        successes > (concurrent_operations * 80 / 100),
        "At least 80% of operations should succeed"
    );

    // Should not have excessive timeouts (indicates connection pool issues)
    assert!(
        timeouts < (concurrent_operations * 20 / 100),
        "Should not have excessive timeouts"
    );

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

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

    // Test MCP handler performance under load
    println!("Testing MCP handler performance...");

    let num_requests = 200;
    let mut handles = vec![];

    let start = Instant::now();

    for i in 0..num_requests {
        let handlers_clone = handlers.clone();

        let handle = tokio::spawn(async move {
            let params = json!({
                "content": format!("MCP performance test content {}", i),
                "context": format!("Performance test context {}", i),
                "summary": format!("Performance test summary {}", i),
                "tags": [format!("mcp-perf-{}", i), "performance"]
            });

            handlers_clone
                .handle_tool_call("store_memory", params)
                .await
        });
        handles.push(handle);
    }

    // Measure completion time
    let mut successes = 0;
    let mut failures = 0;

    for handle in handles {
        match timeout(Duration::from_secs(60), handle).await {
            Ok(Ok(Ok(_))) => successes += 1,
            Ok(Ok(Err(e))) => {
                println!("MCP request failed: {}", e);
                failures += 1;
            }
            Ok(Err(e)) => {
                println!("Task failed: {}", e);
                failures += 1;
            }
            Err(_) => {
                println!("MCP request timed out");
                failures += 1;
            }
        }
    }

    let duration = start.elapsed();
    let requests_per_second = successes as f64 / duration.as_secs_f64();

    println!("MCP handler performance results:");
    println!("  Duration: {:?}", duration);
    println!("  Successes: {}/{}", successes, num_requests);
    println!("  Failures: {}", failures);
    println!("  Requests per second: {:.2}", requests_per_second);

    // Performance expectations for MCP handlers
    assert!(
        successes > (num_requests * 85 / 100),
        "Most MCP requests should succeed"
    );
    assert!(
        requests_per_second > 5.0,
        "Should handle reasonable request rate"
    );
    assert!(
        duration < Duration::from_secs(120),
        "Should complete within reasonable time"
    );

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

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

    // Test performance impact of deduplication
    println!("Testing memory deduplication performance...");

    // Store the same content multiple times to test deduplication efficiency
    let duplicate_content = "This content will be stored multiple times to test deduplication performance and efficiency under various load conditions.";
    let num_duplicates = 500;

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

    for i in 0..num_duplicates {
        let storage_clone = storage.clone();
        let content = duplicate_content.to_string();

        let handle = tokio::spawn(async move {
            storage_clone
                .store(
                    &content,
                    format!("Duplicate test context {}", i),
                    format!("Duplicate test summary {}", i),
                    Some(vec![format!("dup-{}", i)]),
                )
                .await
        });
        handles.push(handle);
    }

    // Wait for all duplicates
    let mut first_id = None;
    let mut successes = 0;
    let mut dedup_count = 0;

    for handle in handles {
        match handle.await {
            Ok(Ok(id)) => {
                successes += 1;
                if let Some(first) = first_id {
                    if first == id {
                        dedup_count += 1;
                    }
                } else {
                    first_id = Some(id);
                }
            }
            Ok(Err(e)) => println!("Duplicate store failed: {}", e),
            Err(e) => println!("Task failed: {}", e),
        }
    }

    let duration = start.elapsed();
    let dedup_ratio = dedup_count as f64 / successes as f64;

    println!("Deduplication performance results:");
    println!("  Duration: {:?}", duration);
    println!("  Successes: {}/{}", successes, num_duplicates);
    println!("  Deduplication hits: {}", dedup_count);
    println!("  Deduplication ratio: {:.2}%", dedup_ratio * 100.0);

    // Check that deduplication is working efficiently
    assert!(
        dedup_ratio > 0.95,
        "Should achieve high deduplication ratio"
    );
    assert!(
        duration < Duration::from_secs(60),
        "Deduplication should be fast"
    );

    // Verify that we only have one actual record despite many stores
    let stats = storage.stats().await?;
    assert_eq!(
        stats.total_memories, 1,
        "Should only have one deduplicated record"
    );

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

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

    // Test performance with increasingly large content
    let base_content = "This is test content that will be repeated to create large payloads for performance testing. ";
    let sizes = vec![
        (100, "Small"),    // ~10KB
        (1000, "Medium"),  // ~100KB
        (10000, "Large"),  // ~1MB
        (50000, "XLarge"), // ~5MB
    ];

    for (multiplier, size_name) in sizes {
        let content = base_content.repeat(multiplier);
        println!("Testing {} content ({} chars)...", size_name, content.len());

        let start = Instant::now();

        let result = storage
            .store(
                &content,
                format!("{} content context", size_name),
                format!("{} content summary", size_name),
                Some(vec![format!("size-{}", size_name.to_lowercase())]),
            )
            .await;

        let store_duration = start.elapsed();

        match result {
            Ok(id) => {
                println!("  Store time: {:?}", store_duration);

                // Test retrieval performance
                let retrieve_start = Instant::now();
                let retrieved = storage.get(id).await?;
                let retrieve_duration = retrieve_start.elapsed();

                println!("  Retrieve time: {:?}", retrieve_duration);

                assert!(retrieved.is_some(), "Should retrieve large content");
                assert_eq!(retrieved.unwrap().content.len(), content.len());

                // Performance expectations (adjust based on system)
                let max_store_time = Duration::from_secs(30);
                let max_retrieve_time = Duration::from_secs(10);

                assert!(
                    store_duration < max_store_time,
                    "{} content store should complete within {:?}",
                    size_name,
                    max_store_time
                );
                assert!(
                    retrieve_duration < max_retrieve_time,
                    "{} content retrieval should complete within {:?}",
                    size_name,
                    max_retrieve_time
                );
            }
            Err(e) => {
                println!("  {} content failed: {}", size_name, e);
                // Large content might legitimately fail due to system limits
                if multiplier < 10000 {
                    // But smaller content should succeed
                    panic!("{} content should not fail: {}", size_name, e);
                }
            }
        }

        println!();
    }

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

/// Helper function to get current memory usage (simplified)
fn get_memory_usage() -> u64 {
    // This is a simplified version - in production, use more sophisticated memory monitoring
    std::process::Command::new("ps")
        .args(["-o", "rss=", "-p", &std::process::id().to_string()])
        .output()
        .ok()
        .and_then(|output| String::from_utf8(output.stdout).ok())
        .and_then(|s| s.trim().parse::<u64>().ok())
        .unwrap_or(0)
        * 1024 // Convert KB to bytes
}