rmcp-memex 0.5.0

RAG/memory MCP server with LanceDB vector storage
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
use crate::{
    BM25Config, BM25Index, EmbeddingConfig, MLXBridge, ProviderConfig, SearchOptions, SliceLayer,
    SliceMode, compute_content_hash, rag::RAGPipeline, storage::StorageManager,
};
use anyhow::{Result, anyhow};
use serde_json::json;
use std::sync::Arc;
use tokio::sync::Mutex;

/// Try to connect to MLX server for tests. Returns None if unavailable.
/// Tests that need embeddings should skip if this returns None.
async fn try_mlx_bridge() -> Option<Arc<Mutex<MLXBridge>>> {
    // Use test config - try localhost only, fail fast
    let config = EmbeddingConfig {
        required_dimension: 4096,
        max_batch_chars: 32000,
        max_batch_items: 16,
        providers: vec![ProviderConfig {
            name: "test-local".to_string(),
            base_url: "http://localhost:12345".to_string(),
            model: "test-model".to_string(),
            priority: 1,
            endpoint: "/v1/embeddings".to_string(),
        }],
        ..Default::default()
    };

    match MLXBridge::new(&config).await {
        Ok(bridge) => Some(Arc::new(Mutex::new(bridge))),
        Err(_) => None,
    }
}

/// Macro to skip test if MLX is unavailable
macro_rules! require_mlx {
    ($mlx:expr) => {
        match $mlx {
            Some(bridge) => bridge,
            None => {
                eprintln!("⚠ Skipping test: MLX server unavailable at localhost:12345");
                return Ok(());
            }
        }
    };
}

#[tokio::test]
async fn memory_roundtrip_and_search() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");

    let storage = Arc::new(StorageManager::new(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let rag = RAGPipeline::new(mlx, storage.clone()).await?;

    // Index a memory chunk with explicit Flat mode so ID is preserved
    let returned_id = rag
        .index_text_with_mode(
            Some("testns"),
            "doc1".to_string(),
            "Ala ma kota".to_string(),
            json!({"lang": "pl"}),
            SliceMode::Flat,
        )
        .await?;

    // ID should be unchanged with Flat mode
    assert_eq!(returned_id, "doc1");

    // Read it back
    let fetched = rag
        .lookup_memory("testns", "doc1")
        .await?
        .ok_or_else(|| anyhow!("doc missing"))?;
    assert_eq!(fetched.text, "Ala ma kota");
    assert_eq!(fetched.namespace, "testns");

    // Semantic search within namespace
    let results = rag.search_memory("testns", "kota", 1).await?;
    assert!(!results.is_empty(), "expected at least one search result");
    assert_eq!(results[0].namespace, "testns");

    Ok(())
}

#[tokio::test]
async fn rag_pipeline_syncs_bm25_writes() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");
    let bm25_path = tmp.path().join(".bm25");

    let storage = Arc::new(StorageManager::new_lance_only(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let bm25 = Arc::new(BM25Index::new(
        &BM25Config::default().with_path(bm25_path.to_string_lossy().into_owned()),
    )?);
    let rag = RAGPipeline::new_with_bm25(mlx, storage, Some(bm25.clone())).await?;

    rag.index_text_with_mode(
        Some("testns"),
        "doc1".to_string(),
        "Ala ma kota".to_string(),
        json!({"lang": "pl"}),
        SliceMode::Flat,
    )
    .await?;

    let results = bm25.search("kota", Some("testns"), 10)?;
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].0, "doc1");

    Ok(())
}

#[tokio::test]
async fn rag_pipeline_rolls_back_lance_write_when_bm25_write_fails() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");
    let bm25_path = tmp.path().join(".bm25");

    let storage = Arc::new(StorageManager::new_lance_only(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let bm25 = Arc::new(BM25Index::new(
        &BM25Config::default()
            .with_path(bm25_path.to_string_lossy().into_owned())
            .with_read_only(true),
    )?);
    let rag = RAGPipeline::new_with_bm25(mlx, storage.clone(), Some(bm25)).await?;

    let err = rag
        .index_text_with_mode(
            Some("rollback-ns"),
            "doc1".to_string(),
            "This batch should never survive a failed BM25 write because rollback keeps LanceDB truthful."
                .to_string(),
            json!({"kind": "rollback-guard"}),
            SliceMode::Flat,
        )
        .await
        .expect_err("read-only BM25 should reject the write path");

    assert!(
        err.to_string().contains("read-only mode"),
        "unexpected error: {err}"
    );
    assert!(
        storage
            .get_all_in_namespace("rollback-ns")
            .await?
            .is_empty(),
        "LanceDB rows should be rolled back when BM25 indexing fails"
    );

    Ok(())
}

#[tokio::test]
async fn onion_text_index_overrides_stale_slice_mode_metadata() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");

    let storage = Arc::new(StorageManager::new_lance_only(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let rag = RAGPipeline::new(mlx, storage).await?;

    rag.index_text_with_mode(
        Some("onion-test"),
        "doc1".to_string(),
        "This is a longer document about onions and embeddings. It should create layered slices for metadata verification."
            .to_string(),
        json!({"slice_mode": "flat"}),
        SliceMode::OnionFast,
    )
    .await?;

    let docs = rag
        .storage_manager()
        .get_all_in_namespace("onion-test")
        .await?;
    assert!(!docs.is_empty(), "expected onion slices to be stored");
    assert!(
        docs.iter()
            .all(|doc| doc.metadata["slice_mode"] == "onion-fast"),
        "all stored slices should carry the actual onion slice_mode"
    );

    Ok(())
}

#[test]
fn search_options_default_to_outer_layer() {
    assert_eq!(
        SearchOptions::default().layer_filter,
        Some(SliceLayer::Outer)
    );
}

#[test]
fn test_content_hash_deterministic() {
    let content = "Test content for hashing";
    let hash1 = compute_content_hash(content);
    let hash2 = compute_content_hash(content);

    // Same content should produce identical hash
    assert_eq!(hash1, hash2);

    // Hash should be 64 characters (SHA256 hex)
    assert_eq!(hash1.len(), 64);

    // Different content should produce different hash
    let hash3 = compute_content_hash("Different content");
    assert_ne!(hash1, hash3);
}

#[test]
fn test_content_hash_slight_difference() {
    // Even a single character difference should produce completely different hash
    let hash1 = compute_content_hash("Test content");
    let hash2 = compute_content_hash("Test content."); // Added period

    assert_ne!(hash1, hash2);
}

#[tokio::test]
async fn test_exact_dedup_skips_identical_content() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");

    // Create a test file
    let test_file = tmp.path().join("test.txt");
    let content = "This is test content for deduplication testing.";
    std::fs::write(&test_file, content)?;

    let storage = Arc::new(StorageManager::new_lance_only(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let rag = RAGPipeline::new(mlx, storage.clone()).await?;

    // First indexing should succeed
    let result1 = rag
        .index_document_with_dedup(&test_file, Some("dedup-test"), SliceMode::Flat)
        .await?;

    assert!(result1.was_indexed(), "First indexing should succeed");

    // Second indexing of SAME content should skip
    let result2 = rag
        .index_document_with_dedup(&test_file, Some("dedup-test"), SliceMode::Flat)
        .await?;

    assert!(
        result2.is_skipped(),
        "Second indexing should be skipped as duplicate"
    );

    // Content hashes should match
    assert_eq!(result1.content_hash(), result2.content_hash());

    Ok(())
}

#[tokio::test]
async fn test_dedup_allows_different_content() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");

    // Create two test files with different content
    let test_file1 = tmp.path().join("test1.txt");
    let test_file2 = tmp.path().join("test2.txt");
    std::fs::write(&test_file1, "Content of file one.")?;
    std::fs::write(&test_file2, "Content of file two.")?; // Different content

    let storage = Arc::new(StorageManager::new_lance_only(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let rag = RAGPipeline::new(mlx, storage.clone()).await?;

    // Index first file
    let result1 = rag
        .index_document_with_dedup(&test_file1, Some("dedup-test"), SliceMode::Flat)
        .await?;
    assert!(result1.was_indexed());

    // Index second file - should NOT be skipped (different content)
    let result2 = rag
        .index_document_with_dedup(&test_file2, Some("dedup-test"), SliceMode::Flat)
        .await?;
    assert!(result2.was_indexed(), "Different content should be indexed");

    // Hashes should be different
    assert_ne!(result1.content_hash(), result2.content_hash());

    Ok(())
}

#[tokio::test]
async fn test_dedup_different_namespaces() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");

    // Create a test file
    let test_file = tmp.path().join("test.txt");
    std::fs::write(&test_file, "Same content in different namespaces.")?;

    let storage = Arc::new(StorageManager::new_lance_only(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let rag = RAGPipeline::new(mlx, storage.clone()).await?;

    // Index in first namespace
    let result1 = rag
        .index_document_with_dedup(&test_file, Some("namespace-a"), SliceMode::Flat)
        .await?;
    assert!(result1.was_indexed());

    // Same content in different namespace should also be indexed
    // (dedup is per-namespace, not global)
    let result2 = rag
        .index_document_with_dedup(&test_file, Some("namespace-b"), SliceMode::Flat)
        .await?;
    assert!(
        result2.was_indexed(),
        "Same content in different namespace should be indexed"
    );

    Ok(())
}

#[tokio::test]
async fn test_has_content_hash() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");

    let storage = Arc::new(StorageManager::new_lance_only(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let rag = RAGPipeline::new(mlx, storage.clone()).await?;

    // Create and index a test file
    let test_file = tmp.path().join("test.txt");
    let content = "Content for hash lookup test.";
    std::fs::write(&test_file, content)?;

    let content_hash = compute_content_hash(content);

    // Before indexing, hash should not exist
    let exists_before = rag
        .storage_manager()
        .has_content_hash("hash-test", &content_hash)
        .await?;
    assert!(!exists_before, "Hash should not exist before indexing");

    // Index the file
    let result = rag
        .index_document_with_dedup(&test_file, Some("hash-test"), SliceMode::Flat)
        .await?;
    assert!(result.was_indexed());

    // After indexing, hash should exist
    let exists_after = rag
        .storage_manager()
        .has_content_hash("hash-test", &content_hash)
        .await?;
    assert!(exists_after, "Hash should exist after indexing");

    // Non-existent hash should return false
    let fake_hash = compute_content_hash("non-existent content");
    let fake_exists = rag
        .storage_manager()
        .has_content_hash("hash-test", &fake_hash)
        .await?;
    assert!(!fake_exists, "Non-existent hash should return false");

    Ok(())
}

#[tokio::test]
async fn memory_upsert_replaces_onion_family_by_original_id() -> Result<()> {
    let mlx = require_mlx!(try_mlx_bridge().await);

    let tmp = tempfile::tempdir()?;
    let db_path = tmp.path().join(".lancedb");

    let storage = Arc::new(StorageManager::new_lance_only(&db_path.to_string_lossy()).await?);
    storage.ensure_collection().await?;

    let rag = RAGPipeline::new(mlx, storage.clone()).await?;
    let namespace = "memory-upsert-onion";
    let initial_text = "This is a long document about indexing and embeddings. It exists to force onion slicing during the first upsert. \
The content is intentionally verbose and repetitive so the outer and core layers both exist and can later be replaced cleanly. \
Project Vista keeps appearing here to provide a stable metadata anchor for search filters.";
    let updated_text = "This is an updated long document about project search filters, namespace cache invalidation, and release hardening. \
It should fully replace the previous onion family instead of appending stale slices. \
The text stays comfortably above the slicing threshold so onion-fast still writes both outer and core documents.";

    rag.memory_upsert(
        namespace,
        "doc-1".to_string(),
        initial_text.to_string(),
        json!({"slice_mode": "onion-fast", "project": "Vista"}),
    )
    .await?;

    let initial_docs = rag
        .storage_manager()
        .get_all_in_namespace(namespace)
        .await?;
    assert_eq!(
        initial_docs.len(),
        2,
        "onion-fast should create outer + core"
    );
    assert!(
        initial_docs
            .iter()
            .all(|doc| doc.metadata["original_id"] == "doc-1")
    );

    let fetched = rag
        .lookup_memory(namespace, "doc-1")
        .await?
        .ok_or_else(|| anyhow!("expected lookup by original id to resolve onion family"))?;
    assert_eq!(fetched.layer, Some(SliceLayer::Outer));
    assert_eq!(fetched.metadata["original_id"], "doc-1");

    rag.memory_upsert(
        namespace,
        "doc-1".to_string(),
        updated_text.to_string(),
        json!({"slice_mode": "onion-fast", "project": "Vista"}),
    )
    .await?;

    let updated_docs = rag
        .storage_manager()
        .get_all_in_namespace(namespace)
        .await?;
    assert_eq!(
        updated_docs.len(),
        2,
        "upsert should replace the onion family instead of duplicating it"
    );
    assert!(
        updated_docs
            .iter()
            .all(|doc| doc.metadata["original_id"] == "doc-1")
    );
    assert!(
        updated_docs
            .iter()
            .any(|doc| doc.document.contains("namespace cache invalidation")),
        "updated content should be present in the stored family"
    );

    let deleted = rag.remove_memory(namespace, "doc-1").await?;
    assert_eq!(
        deleted, 2,
        "remove_memory should delete the whole onion family"
    );
    assert!(
        rag.storage_manager()
            .get_all_in_namespace(namespace)
            .await?
            .is_empty()
    );

    Ok(())
}