rust-memex 0.6.5

Operator CLI + MCP server: canonical corpus second: semantic index second to aicx
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
use axum::{
    Json, Router,
    body::{Body, to_bytes},
    extract::State,
    http::{Method, Request, StatusCode, header},
    routing::post,
};
use rust_memex::{
    AuthManager, ChromaDocument, EmbeddingClient, EmbeddingConfig, McpCore, ProviderConfig,
    RAGPipeline, StorageManager,
    contracts::progress::{ReindexProgress, ReprocessProgress},
    http::{HttpServerConfig, HttpState, create_router},
};
use serde::Deserialize;
use serde_json::{Value, json};
use std::sync::Arc;
use tempfile::TempDir;
use tokio::{net::TcpListener, sync::Mutex, task::JoinHandle};
use tower::util::ServiceExt;

const AUTH_TOKEN: &str = "secret-token";
const TEST_DIMENSION: usize = 8;

#[derive(Clone)]
struct MockEmbeddingState {
    dimension: usize,
}

#[derive(Debug, Deserialize)]
struct MockEmbeddingRequest {
    input: Vec<String>,
}

#[derive(Debug, serde::Serialize)]
struct MockEmbeddingResponse {
    data: Vec<MockEmbeddingData>,
}

#[derive(Debug, serde::Serialize)]
struct MockEmbeddingData {
    embedding: Vec<f32>,
}

struct MockEmbeddingServer {
    base_url: String,
    handle: JoinHandle<()>,
}

impl Drop for MockEmbeddingServer {
    fn drop(&mut self) {
        self.handle.abort();
    }
}

struct TestApp {
    app: Router,
    storage: Arc<StorageManager>,
    _tmp: TempDir,
    _mock_server: MockEmbeddingServer,
}

async fn mock_embeddings(
    State(state): State<MockEmbeddingState>,
    Json(request): Json<MockEmbeddingRequest>,
) -> Json<MockEmbeddingResponse> {
    let data = request
        .input
        .into_iter()
        .enumerate()
        .map(|(index, _)| MockEmbeddingData {
            embedding: vec![index as f32 + 0.25; state.dimension],
        })
        .collect();
    Json(MockEmbeddingResponse { data })
}

async fn start_mock_embedding_server() -> MockEmbeddingServer {
    let app = Router::new()
        .route("/v1/embeddings", post(mock_embeddings))
        .with_state(MockEmbeddingState {
            dimension: TEST_DIMENSION,
        });

    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind mock");
    let address = listener.local_addr().expect("mock address");
    let handle = tokio::spawn(async move {
        axum::serve(listener, app).await.expect("mock server");
    });

    MockEmbeddingServer {
        base_url: format!("http://{}", address),
        handle,
    }
}

fn test_embedding_config(base_url: &str) -> EmbeddingConfig {
    EmbeddingConfig {
        required_dimension: TEST_DIMENSION,
        max_batch_chars: 16_000,
        max_batch_items: 8,
        providers: vec![ProviderConfig {
            name: "mock".to_string(),
            base_url: base_url.to_string(),
            model: "mock-embed".to_string(),
            priority: 1,
            endpoint: "/v1/embeddings".to_string(),
        }],
        reranker: Default::default(),
    }
}

async fn build_test_app() -> TestApp {
    let tmp = tempfile::tempdir().expect("tempdir");
    let db_path = tmp.path().join("lancedb");
    let mock_server = start_mock_embedding_server().await;
    let embedding_config = test_embedding_config(&mock_server.base_url);
    let embedding_client = Arc::new(Mutex::new(
        EmbeddingClient::new(&embedding_config)
            .await
            .expect("embedding client"),
    ));
    let storage = Arc::new(
        StorageManager::new(db_path.to_str().unwrap())
            .await
            .expect("storage"),
    );
    let rag = Arc::new(
        RAGPipeline::new(embedding_client.clone(), storage.clone())
            .await
            .expect("rag"),
    );

    let tokens_path = tmp.path().join("tokens.json");
    let auth_manager = Arc::new(AuthManager::new(
        tokens_path.to_string_lossy().to_string(),
        None,
    ));
    let mcp_core = Arc::new(McpCore::new(
        rag.clone(),
        None,
        embedding_client,
        1024 * 1024,
        vec![],
        auth_manager,
    ));
    let state = HttpState::new(rag, mcp_core);
    let config = HttpServerConfig {
        auth_token: Some(AUTH_TOKEN.to_string()),
        ..Default::default()
    };
    let app = create_router(state, &config);

    TestApp {
        app,
        storage,
        _tmp: tmp,
        _mock_server: mock_server,
    }
}

fn authed_json_request(method: Method, uri: &str, body: Value) -> Request<Body> {
    Request::builder()
        .method(method)
        .uri(uri)
        .header(header::CONTENT_TYPE, "application/json")
        .header(header::AUTHORIZATION, format!("Bearer {AUTH_TOKEN}"))
        .body(Body::from(body.to_string()))
        .expect("request")
}

fn authed_multipart_request(uri: &str, boundary: &str, body: String) -> Request<Body> {
    Request::builder()
        .method(Method::POST)
        .uri(uri)
        .header(
            header::CONTENT_TYPE,
            format!("multipart/form-data; boundary={boundary}"),
        )
        .header(header::AUTHORIZATION, format!("Bearer {AUTH_TOKEN}"))
        .body(Body::from(body))
        .expect("multipart request")
}

fn parse_sse_events(body: &str) -> Vec<(String, Value)> {
    body.split("\n\n")
        .filter_map(|chunk| {
            let mut event = None;
            let mut data = None;
            for line in chunk.lines() {
                if let Some(value) = line.strip_prefix("event:") {
                    event = Some(value.trim().to_string());
                } else if let Some(value) = line.strip_prefix("data:") {
                    data =
                        Some(serde_json::from_str::<Value>(value.trim()).expect("valid sse json"));
                }
            }

            match (event, data) {
                (Some(event), Some(data)) => Some((event, data)),
                _ => None,
            }
        })
        .collect()
}

fn multipart_body(
    namespace: &str,
    skip_existing: bool,
    file_contents: &str,
    boundary: &str,
) -> String {
    format!(
        "--{boundary}\r\nContent-Disposition: form-data; name=\"namespace\"\r\n\r\n{namespace}\r\n\
--{boundary}\r\nContent-Disposition: form-data; name=\"skip_existing\"\r\n\r\n{skip_existing}\r\n\
--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"export.jsonl\"\r\nContent-Type: application/x-ndjson\r\n\r\n{file_contents}\r\n\
--{boundary}--\r\n"
    )
}

#[tokio::test]
async fn reprocess_endpoint_streams_progress_and_writes_target_namespace() {
    let test_app = build_test_app().await;
    let input_path = test_app._tmp.path().join("reprocess.jsonl");
    let payload = [
        json!({
            "id": "doc-1:outer",
            "text": "Short summary",
            "metadata": {"original_id": "doc-1", "layer": "outer"},
            "content_hash": "outer-hash"
        }),
        json!({
            "id": "doc-1:core",
            "text": "Longer full document content that should survive collapse.",
            "metadata": {"original_id": "doc-1", "layer": "core"},
            "content_hash": "core-hash"
        }),
    ]
    .into_iter()
    .map(|row| row.to_string())
    .collect::<Vec<_>>()
    .join("\n");
    tokio::fs::write(&input_path, payload)
        .await
        .expect("write jsonl");

    let response = test_app
        .app
        .clone()
        .oneshot(authed_json_request(
            Method::POST,
            "/sse/reprocess",
            json!({
                "input_path": input_path,
                "target_namespace": "reprocessed-ns",
                "slice_mode": "flat",
                "preprocess": false,
                "skip_existing": false
            }),
        ))
        .await
        .expect("sse response");

    assert_eq!(response.status(), StatusCode::OK);
    let body = to_bytes(response.into_body(), 1024 * 1024)
        .await
        .expect("sse body");
    let text = String::from_utf8(body.to_vec()).expect("utf8");
    let events = parse_sse_events(&text);
    assert!(events.iter().any(|(event, _)| event == "start"));
    assert!(events.iter().any(|(event, _)| event == "progress"));
    let result = events
        .iter()
        .find(|(event, _)| event == "result")
        .map(|(_, data)| data.clone())
        .expect("result event");
    let progress = events
        .iter()
        .find(|(event, _)| event == "progress")
        .map(|(_, data)| {
            serde_json::from_value::<ReprocessProgress>(data.clone()).expect("progress")
        })
        .expect("typed progress");

    assert_eq!(progress.source_label, input_path.display().to_string());
    assert_eq!(result["indexed_documents"], 1);
    assert_eq!(
        test_app
            .storage
            .count_namespace("reprocessed-ns")
            .await
            .expect("count"),
        1
    );
}

#[tokio::test]
async fn reindex_endpoint_defaults_target_namespace() {
    let test_app = build_test_app().await;
    test_app
        .storage
        .add_to_store(vec![ChromaDocument::new_flat_with_hash(
            "src-doc".to_string(),
            "source-ns".to_string(),
            vec![0.4; TEST_DIMENSION],
            json!({"doc_id": "src-doc"}),
            "Reindex me".to_string(),
            "hash-src-doc".to_string(),
        )])
        .await
        .expect("seed source");

    let response = test_app
        .app
        .clone()
        .oneshot(authed_json_request(
            Method::POST,
            "/sse/reindex",
            json!({
                "source_namespace": "source-ns",
                "slice_mode": "flat",
                "preprocess": false,
                "skip_existing": false
            }),
        ))
        .await
        .expect("reindex response");

    assert_eq!(response.status(), StatusCode::OK);
    let body = to_bytes(response.into_body(), 1024 * 1024)
        .await
        .expect("sse body");
    let text = String::from_utf8(body.to_vec()).expect("utf8");
    let events = parse_sse_events(&text);
    let progress = events
        .iter()
        .find(|(event, _)| event == "progress")
        .map(|(_, data)| serde_json::from_value::<ReindexProgress>(data.clone()).expect("progress"))
        .expect("typed progress");
    let result = events
        .iter()
        .find(|(event, _)| event == "result")
        .map(|(_, data)| data.clone())
        .expect("result event");

    assert_eq!(progress.namespace, "source-ns-reindexed");
    assert_eq!(result["target_namespace"], "source-ns-reindexed");
    assert_eq!(
        test_app
            .storage
            .count_namespace("source-ns-reindexed")
            .await
            .expect("count"),
        1
    );
}

#[tokio::test]
async fn export_import_round_trip_and_migrate_namespace_work() {
    let test_app = build_test_app().await;
    test_app
        .storage
        .add_to_store(vec![
            ChromaDocument::new_flat_with_hash(
                "doc-a".to_string(),
                "export-src".to_string(),
                vec![0.1; TEST_DIMENSION],
                json!({"kind": "note"}),
                "Alpha body".to_string(),
                "hash-alpha".to_string(),
            ),
            ChromaDocument::new_flat_with_hash(
                "doc-b".to_string(),
                "export-src".to_string(),
                vec![0.2; TEST_DIMENSION],
                json!({"kind": "note"}),
                "Beta body".to_string(),
                "hash-beta".to_string(),
            ),
        ])
        .await
        .expect("seed export source");

    let export_response = test_app
        .app
        .clone()
        .oneshot(authed_json_request(
            Method::POST,
            "/api/export",
            json!({
                "namespace": "export-src",
                "include_embeddings": true
            }),
        ))
        .await
        .expect("export response");

    assert_eq!(export_response.status(), StatusCode::OK);
    assert_eq!(
        export_response.headers().get(header::CONTENT_TYPE).unwrap(),
        "application/x-ndjson"
    );
    let exported_bytes = to_bytes(export_response.into_body(), 1024 * 1024)
        .await
        .expect("export body");
    let exported_jsonl = String::from_utf8(exported_bytes.to_vec()).expect("utf8");
    let exported_rows = exported_jsonl
        .lines()
        .map(|line| serde_json::from_str::<Value>(line).expect("json line"))
        .collect::<Vec<_>>();
    assert_eq!(exported_rows.len(), 2);
    let exported_hashes = exported_rows
        .iter()
        .map(|row| {
            row["content_hash"]
                .as_str()
                .expect("exported content hash")
                .to_string()
        })
        .collect::<Vec<_>>();
    assert_eq!(exported_hashes, vec!["hash-alpha", "hash-beta"]);

    let boundary = "memex-boundary";
    let import_body = multipart_body("import-copy", false, &exported_jsonl, boundary);
    let import_response = test_app
        .app
        .clone()
        .oneshot(authed_multipart_request(
            "/api/import",
            boundary,
            import_body,
        ))
        .await
        .expect("import response");

    assert_eq!(import_response.status(), StatusCode::OK);
    let import_json: Value = serde_json::from_slice(
        &to_bytes(import_response.into_body(), 64 * 1024)
            .await
            .expect("import body"),
    )
    .expect("import json");
    assert_eq!(import_json["imported_count"], 2);
    assert_eq!(
        import_json["imported_count"]
            .as_u64()
            .expect("imported count") as usize,
        exported_rows.len()
    );
    assert_eq!(
        test_app
            .storage
            .count_namespace("import-copy")
            .await
            .expect("import count"),
        2
    );

    test_app
        .storage
        .add_to_store(vec![
            ChromaDocument::new_flat_with_hash(
                "move-a".to_string(),
                "rename-me".to_string(),
                vec![0.3; TEST_DIMENSION],
                json!({"kind": "rename"}),
                "Move alpha".to_string(),
                "hash-move-a".to_string(),
            ),
            ChromaDocument::new_flat_with_hash(
                "move-b".to_string(),
                "rename-me".to_string(),
                vec![0.4; TEST_DIMENSION],
                json!({"kind": "rename"}),
                "Move beta".to_string(),
                "hash-move-b".to_string(),
            ),
        ])
        .await
        .expect("seed migrate");

    let migrate_response = test_app
        .app
        .clone()
        .oneshot(authed_json_request(
            Method::POST,
            "/api/migrate-namespace",
            json!({
                "from": "rename-me",
                "to": "renamed-ns"
            }),
        ))
        .await
        .expect("migrate response");

    assert_eq!(migrate_response.status(), StatusCode::OK);
    let migrate_json: Value = serde_json::from_slice(
        &to_bytes(migrate_response.into_body(), 64 * 1024)
            .await
            .expect("migrate body"),
    )
    .expect("migrate json");
    assert_eq!(migrate_json["migrated_chunks"], 2);
    assert_eq!(
        test_app
            .storage
            .count_namespace("rename-me")
            .await
            .expect("old count"),
        0
    );
    assert_eq!(
        test_app
            .storage
            .count_namespace("renamed-ns")
            .await
            .expect("new count"),
        2
    );
}