mnemo-engine 0.4.1

Encrypted, single-file, portable agent memory engine with multi-signal recall, sessions, snapshots, and an IVF+PQ index
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
//! Smoke tests for the `mnemo` CLI exploration commands (`get`, `list`,
//! `recall`).
//!
//! These tests build a tiny database with the library, then invoke the
//! freshly built `mnemo` binary via [`std::process::Command`], asserting on
//! exit code and standard-output substrings. They are deliberately light:
//! the library API is exercised by `tests/integration.rs`; this file only
//! verifies that the CLI wiring around `db.get` / `db.memories` / `db.recall`
//! parses arguments and renders output as expected.

use std::process::Command;

use mnemo::{KdfParams, Memory, MemoryType, Mnemo, MnemoConfig};
use tempfile::tempdir;

/// Path to the `mnemo` binary built by Cargo for these tests.
fn bin() -> &'static str {
    env!("CARGO_BIN_EXE_mnemo")
}

/// Cheap KDF so the tests are fast.
fn fast_cfg(dimensions: usize) -> MnemoConfig {
    MnemoConfig {
        dimensions,
        kdf: KdfParams::fast(),
        ..Default::default()
    }
}

/// Create a tiny test database with three memories and return (path,
/// passphrase, id-of-the-semantic-memory).
fn seed_db() -> (tempfile::TempDir, std::path::PathBuf, &'static str, String) {
    let dir = tempdir().unwrap();
    let path = dir.path().join("smoke.mnemo");
    let pp = "smoke-pw";

    let mut db = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
    let semantic_id = db
        .remember(
            Memory::new(
                "the user prefers tea",
                MemoryType::Semantic,
                vec![1.0, 0.0, 0.0, 0.0],
            )
            .with_agent("assistant")
            .with_importance(0.9),
        )
        .unwrap();
    db.remember(
        Memory::new(
            "ran the deploy script",
            MemoryType::Episodic,
            vec![0.0, 1.0, 0.0, 0.0],
        )
        .with_agent("assistant")
        .with_importance(0.4),
    )
    .unwrap();
    db.remember(
        Memory::new(
            "escalate via pager",
            MemoryType::Procedural,
            vec![0.0, 0.0, 1.0, 0.0],
        )
        .with_agent("ops")
        .with_importance(0.6),
    )
    .unwrap();
    db.flush().unwrap();
    db.close().unwrap();

    (dir, path, pp, semantic_id.to_string())
}

/// Run the CLI with `MNEMO_PASSPHRASE` and return (status, stdout, stderr).
fn run_cli(args: &[&str], pp: &str) -> (std::process::ExitStatus, String, String) {
    let out = Command::new(bin())
        .args(args)
        .env("MNEMO_PASSPHRASE", pp)
        .output()
        .expect("failed to invoke mnemo binary");
    let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
    let stderr = String::from_utf8_lossy(&out.stderr).into_owned();
    (out.status, stdout, stderr)
}

#[test]
fn list_table_shows_three_memories() {
    let (_dir, path, pp, _) = seed_db();
    let (status, stdout, stderr) =
        run_cli(&["list", path.to_str().unwrap(), "--format", "table"], pp);
    assert!(status.success(), "list failed: {stderr}");
    assert!(
        stdout.contains("showing 3 of 3 memories"),
        "header missing: {stdout}"
    );
    assert!(
        stdout.contains("the user prefers tea"),
        "content missing: {stdout}"
    );
    assert!(stdout.contains("[semantic]"), "type tag missing: {stdout}");
}

#[test]
fn list_filters_by_type_and_agent() {
    let (_dir, path, pp, _) = seed_db();
    let (status, stdout, _) = run_cli(
        &[
            "list",
            path.to_str().unwrap(),
            "--type",
            "procedural",
            "--agent",
            "ops",
        ],
        pp,
    );
    assert!(status.success());
    assert!(stdout.contains("escalate via pager"));
    assert!(!stdout.contains("prefers tea"));
    assert!(!stdout.contains("deploy script"));
}

#[test]
fn list_json_emits_total_and_count() {
    let (_dir, path, pp, _) = seed_db();
    let (status, stdout, _) = run_cli(
        &[
            "list",
            path.to_str().unwrap(),
            "--format",
            "json",
            "--limit",
            "2",
        ],
        pp,
    );
    assert!(status.success());
    let v: serde_json::Value = serde_json::from_str(&stdout).expect("json output");
    assert_eq!(v["total"], 3);
    assert_eq!(v["count"], 2);
    let memories = v["memories"].as_array().expect("memories array");
    assert_eq!(memories.len(), 2);
    // Vector is stripped by default.
    assert!(
        memories[0].get("vector").is_none(),
        "vector should be omitted"
    );
}

#[test]
fn list_jsonl_with_vector_includes_embedding() {
    let (_dir, path, pp, _) = seed_db();
    let (status, stdout, _) = run_cli(
        &[
            "list",
            path.to_str().unwrap(),
            "--format",
            "jsonl",
            "--vector",
        ],
        pp,
    );
    assert!(status.success());
    let lines: Vec<&str> = stdout.lines().filter(|l| !l.is_empty()).collect();
    assert_eq!(lines.len(), 3);
    for line in &lines {
        let v: serde_json::Value = serde_json::from_str(line).expect("jsonl line");
        assert!(v["vector"].as_array().is_some(), "missing vector: {line}");
    }
}

#[test]
fn get_by_id_table_and_json() {
    let (_dir, path, pp, semantic_id) = seed_db();

    let (status, stdout, _) = run_cli(
        &["get", path.to_str().unwrap(), &semantic_id, "--verbose"],
        pp,
    );
    assert!(status.success());
    assert!(
        stdout.contains(&semantic_id),
        "id missing in table output: {stdout}"
    );
    assert!(stdout.contains("the user prefers tea"));
    assert!(stdout.contains("imp=0.90"));
    assert!(stdout.contains("scope"));

    let (status, stdout, _) = run_cli(
        &[
            "get",
            path.to_str().unwrap(),
            &semantic_id,
            "--format",
            "json",
        ],
        pp,
    );
    assert!(status.success());
    let v: serde_json::Value = serde_json::from_str(&stdout).expect("json from get");
    assert_eq!(v["content"], "the user prefers tea");
    assert_eq!(v["memory_type"], "Semantic");
    assert!(
        v.get("vector").is_none(),
        "vector should be omitted by default"
    );
}

#[test]
fn get_missing_id_fails_cleanly() {
    let (_dir, path, pp, _) = seed_db();
    // A valid ULID that isn't in the DB.
    let (status, _stdout, stderr) = run_cli(
        &["get", path.to_str().unwrap(), "01ARZ3NDEKTSV4RRFFQ69G5FAV"],
        pp,
    );
    assert!(!status.success(), "missing id should exit non-zero");
    assert!(
        stderr.contains("not found"),
        "expected NotFound message, got: {stderr}"
    );
}

#[test]
fn recall_returns_query_nearest_first() {
    let (_dir, path, pp, _) = seed_db();
    let (status, stdout, _) = run_cli(
        &[
            "recall",
            path.to_str().unwrap(),
            "--query",
            "0.9,0.1,0.0,0.0",
            "--top-k",
            "2",
        ],
        pp,
    );
    assert!(status.success());
    let first_line = stdout.lines().next().unwrap_or("");
    assert!(
        first_line.contains("the user prefers tea"),
        "expected tea first: {stdout}"
    );
    assert!(first_line.starts_with("score="));
}

#[test]
fn recall_json_carries_score_and_similarity() {
    let (_dir, path, pp, _) = seed_db();
    let (status, stdout, _) = run_cli(
        &[
            "recall",
            path.to_str().unwrap(),
            "--query",
            "0.9,0.1,0.0,0.0",
            "--top-k",
            "1",
            "--format",
            "json",
        ],
        pp,
    );
    assert!(status.success());
    let v: serde_json::Value = serde_json::from_str(&stdout).expect("json from recall");
    assert_eq!(v["count"], 1);
    let hits = v["hits"].as_array().expect("hits array");
    assert!(hits[0]["score"].is_number());
    assert!(hits[0]["similarity"].is_number());
    assert_eq!(hits[0]["memory"]["content"], "the user prefers tea");
}

#[test]
fn recall_filters_by_type_excludes_other_kinds() {
    let (_dir, path, pp, _) = seed_db();
    let (status, stdout, _) = run_cli(
        &[
            "recall",
            path.to_str().unwrap(),
            "--query",
            "1.0,0.0,0.0,0.0",
            "--type",
            "procedural",
        ],
        pp,
    );
    assert!(status.success());
    // Only the procedural memory should appear.
    assert!(stdout.contains("escalate via pager"));
    assert!(!stdout.contains("prefers tea"));
    assert!(!stdout.contains("deploy script"));
}

#[test]
fn recall_query_file_json_works() {
    // --query-file accepts a JSON array of floats from a file path, as an
    // alternative to inline --query (which is unusable at high dims).
    let (dir, path, pp, _) = seed_db();
    let qf = dir.path().join("q.json");
    std::fs::write(&qf, "[1.0, 0.0, 0.0, 0.0]").unwrap();
    let (status, stdout, stderr) = run_cli(
        &[
            "recall",
            path.to_str().unwrap(),
            "--query-file",
            qf.to_str().unwrap(),
            "--top-k",
            "1",
        ],
        pp,
    );
    assert!(status.success(), "recall failed: {stderr}");
    assert!(stdout.contains("the user prefers tea"), "got: {stdout}");
}

#[test]
fn recall_query_file_whitespace_works() {
    // --query-file also accepts whitespace/newline-separated floats — handy
    // for piping from awk/jq without JSON serialization.
    let (dir, path, pp, _) = seed_db();
    let qf = dir.path().join("q.txt");
    std::fs::write(&qf, "1.0\n0.0\n0.0\n0.0\n").unwrap();
    let (status, stdout, stderr) = run_cli(
        &[
            "recall",
            path.to_str().unwrap(),
            "--query-file",
            qf.to_str().unwrap(),
            "--top-k",
            "1",
        ],
        pp,
    );
    assert!(status.success(), "recall failed: {stderr}");
    assert!(stdout.contains("the user prefers tea"), "got: {stdout}");
}

#[test]
fn recall_requires_query_source() {
    // Neither --query nor --query-file should fail loudly rather than silently
    // running with no query.
    let (_dir, path, pp, _) = seed_db();
    let (status, _stdout, stderr) =
        run_cli(&["recall", path.to_str().unwrap(), "--top-k", "1"], pp);
    assert!(!status.success(), "recall without query should fail");
    assert!(
        stderr.contains("--query") || stderr.contains("query"),
        "stderr should mention missing query, got: {stderr}"
    );
}

// === MCP stdio server ====================================================

/// Drive the MCP server end-to-end: spawn `mnemo serve --mcp <path>`,
/// pipe a scripted JSON-RPC session through stdin, capture stdout, and
/// assert the responses. Uses one long-lived child process rather than
/// one per request so we exercise the actual line-oriented dispatch loop.
#[test]
fn mcp_serve_initialize_list_and_call_tools() {
    use std::io::Write;
    use std::process::{Command, Stdio};

    // Seed the file with a *scoped* writer so its exclusive lock is
    // released before the server subprocess tries to acquire its own.
    // The temp dir stays alive for the whole test so the file survives.
    let dir = tempdir().unwrap();
    let path = dir.path().join("mcp.mnemo");
    let pp = "smoke-pw";
    {
        let mut db = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
        db.remember(
            Memory::new(
                "the user prefers tea",
                MemoryType::Semantic,
                vec![1.0, 0.0, 0.0, 0.0],
            )
            .with_importance(0.9),
        )
        .unwrap();
        db.flush().unwrap();
    }

    // ---- Race note (Linux only, observed on Rust 1.75 MSRV CI, 2026-08-29) ----
    // cargo runs `integration.rs` and `cli_smoke.rs` in parallel; several
    // integration tests fork subprocesses of their own via `Command::spawn`.
    // On Rust 1.75 Linux, `Command::spawn` still uses fork+exec, which
    // briefly clones ALL parent FDs into the sibling child before exec's
    // CLOEXEC pass drops them. If a sibling test forks while our own
    // scoped seed writer above is mid-drop, an inherited copy of the
    // writer's FD can keep the flock on `mcp.mnemo` alive for a few
    // milliseconds after our writer's `db` has dropped. Our `mnemo serve
    // --mcp` subprocess then loses the race for `try_lock_exclusive` and
    // exits with `MnemoError::Locked`. Stable Rust switched `spawn` to
    // `posix_spawn`, which skips the FD-clone window, so this reproduces
    // only on the MSRV Linux job.
    //
    // Fix scope: **test-only retry**. We re-spawn the whole session up
    // to 5 times with 100/200/400/800 ms backoff, but ONLY when the
    // child stderr carries the specific `file already locked` marker.
    // Any other non-zero exit falls through to the normal assertion
    // below with the same panic message it used to have. Do not weaken
    // the engine — the exclusive lock is a correctness invariant for
    // Phase 1.1; the race is inherent to fork+exec-plus-parallel-tests
    // on 1.75 and cannot recur once the process is stable.
    // -------------------------------------------------------------------------
    let mut out_opt = None;
    let mut last_stderr = String::new();
    for attempt in 0..5u32 {
        if attempt > 0 {
            // 100 ms → 200 → 400 → 800; cumulative worst case ~1.5 s.
            let backoff_ms = 100u64 << (attempt - 1);
            std::thread::sleep(std::time::Duration::from_millis(backoff_ms));
        }
        let mut child = Command::new(bin())
            .args(["serve", path.to_str().unwrap(), "--mcp"])
            .env("MNEMO_PASSPHRASE", pp)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("failed to spawn mnemo serve --mcp");

        // Scripted client session: initialize, then list tools, then call
        // `stats`. Each line is one JSON-RPC message. Closing stdin ends
        // the loop and the child exits.
        let stdin = child.stdin.as_mut().expect("child stdin missing");
        let script = [
            r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{}}}"#,
            r#"{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}"#,
            r#"{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}"#,
            r#"{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"stats","arguments":{}}}"#,
        ];
        for line in script {
            writeln!(stdin, "{line}").unwrap();
        }
        // Close stdin — signals EOF to the server's read loop.
        drop(child.stdin.take());

        // Wait for shutdown (bounded — the server should exit as soon as
        // stdin closes; hanging here would fail the test with a timeout
        // at the test-runner level).
        let run = child.wait_with_output().expect("child wait failed");
        if !run.status.success() {
            let stderr_snapshot = String::from_utf8_lossy(&run.stderr).into_owned();
            if stderr_snapshot.contains("file already locked") {
                // Race lost — retry after backoff.
                last_stderr = stderr_snapshot;
                continue;
            }
            // Any other failure: bubble up unchanged.
            out_opt = Some(run);
            break;
        }
        out_opt = Some(run);
        break;
    }
    let out = out_opt.unwrap_or_else(|| {
        panic!(
            "mnemo serve --mcp lost the fork/flock race on all 5 attempts \
             (bounded retry exhausted); last stderr:\n{last_stderr}"
        )
    });
    let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
    let stderr = String::from_utf8_lossy(&out.stderr).into_owned();
    assert!(
        out.status.success(),
        "server exited non-zero: status={:?} stderr={stderr}",
        out.status
    );

    // Parse the response lines back and assert on structure.
    let lines: Vec<&str> = stdout.lines().filter(|l| !l.trim().is_empty()).collect();
    assert!(
        lines.len() >= 3,
        "expected ≥3 response lines (init, tools/list, stats), got {}: {stdout}",
        lines.len()
    );

    let init: serde_json::Value = serde_json::from_str(lines[0]).expect("init parse");
    assert_eq!(init["jsonrpc"], "2.0");
    assert_eq!(init["id"], 1);
    assert_eq!(init["result"]["protocolVersion"], "2025-06-18");
    assert_eq!(init["result"]["serverInfo"]["name"], "mnemo");

    let list: serde_json::Value = serde_json::from_str(lines[1]).expect("tools/list parse");
    let tools = list["result"]["tools"].as_array().expect("tools array");
    let names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect();
    for expected in [
        "about",
        "remember",
        "recall",
        "forget",
        "list",
        "snapshot_list",
        "stats",
    ] {
        assert!(
            names.contains(&expected),
            "missing tool '{expected}' in {names:?}"
        );
    }

    let stats: serde_json::Value = serde_json::from_str(lines[2]).expect("stats parse");
    let content = &stats["result"]["content"][0]["text"];
    assert!(
        content.is_string(),
        "stats content should be a text block: {stats}"
    );
    let stats_body: serde_json::Value =
        serde_json::from_str(content.as_str().unwrap()).expect("stats body parse");
    assert_eq!(
        stats_body["memories"], 1,
        "seeded exactly one live memory: {stats_body}"
    );
}

#[test]
fn mcp_serve_refuses_without_passphrase() {
    // Env var missing → server errors at startup with a clear message
    // and non-zero exit, never opening the file.
    use std::process::{Command, Stdio};

    let dir = tempdir().unwrap();
    let path = dir.path().join("mcp-nopw.mnemo");
    {
        let mut db = Mnemo::create(&path, "smoke-pw", fast_cfg(4)).unwrap();
        db.remember(Memory::new(
            "x",
            MemoryType::Semantic,
            vec![1.0, 0.0, 0.0, 0.0],
        ))
        .unwrap();
        db.flush().unwrap();
    }

    let out = Command::new(bin())
        .args(["serve", path.to_str().unwrap(), "--mcp"])
        .env_remove("MNEMO_PASSPHRASE")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .expect("spawn failed");
    let stderr = String::from_utf8_lossy(&out.stderr).into_owned();
    assert!(
        !out.status.success(),
        "expected non-zero exit without passphrase"
    );
    assert!(
        stderr.contains("MNEMO_PASSPHRASE"),
        "stderr should mention the env var, got: {stderr}"
    );
}