mnemonist 0.10.0

CLI tool for mnemonist — manage AI agent memory
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
use insta::assert_json_snapshot;
use serde_json::Value;
use std::path::PathBuf;
use std::process::Command;

/// Get the path to the mnemonist binary built by cargo.
fn mnemonist_bin() -> PathBuf {
    let mut path = PathBuf::from(env!("CARGO_BIN_EXE_mnemonist"));
    // Fallback: if the macro doesn't resolve, try target/debug
    if !path.exists() {
        path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../target/debug/mnemonist");
    }
    path
}

/// Run mnemonist with the given args in an isolated HOME directory.
/// Returns (stdout as JSON, exit code).
fn run(home: &std::path::Path, args: &[&str]) -> (Value, i32) {
    let output = Command::new(mnemonist_bin())
        .args(args)
        .env("HOME", home)
        .output()
        .expect("failed to execute mnemonist");

    let code = output.status.code().unwrap_or(-1);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: Value =
        serde_json::from_str(&stdout).unwrap_or_else(|_| panic!("invalid JSON output: {stdout}"));
    (json, code)
}

#[test]
fn memorize_creates_memory_file() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    // Memorize
    let (json, code) = run(
        &home,
        &[
            "memorize",
            "always use tests",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_eq!(code, 0);
    assert_eq!(json["ok"], true);
    assert_eq!(json["data"]["action"], "created");
    assert!(
        json["data"]["file"]
            .as_str()
            .unwrap()
            .starts_with("feedback_")
    );
}

#[test]
fn memorize_with_type_and_name() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let (json, code) = run(
        &home,
        &[
            "memorize",
            "user prefers vim",
            "-t",
            "user",
            "--name",
            "vim-preference",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_eq!(code, 0);
    assert_eq!(json["data"]["file"], "user_vim-preference.md");
}

#[test]
fn note_adds_to_inbox() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let (json, code) = run(
        &home,
        &["note", "check logging", "--root", project.to_str().unwrap()],
    );
    assert_eq!(code, 0);
    assert_eq!(json["data"]["inbox_size"], 1);
    assert_eq!(json["data"]["capacity"], 7);
}

#[test]
fn remember_finds_memorized_content() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    run(
        &home,
        &[
            "memorize",
            "prefer rust for cli tools",
            "--root",
            project.to_str().unwrap(),
        ],
    );

    let (json, code) = run(
        &home,
        &[
            "remember",
            "rust",
            "--level",
            "project",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_eq!(code, 0);
    let memories = json["data"]["memories"].as_array().unwrap();
    assert_eq!(memories.len(), 1);
    assert!(memories[0]["body"].as_str().unwrap().contains("rust"));
}

#[test]
fn remember_returns_empty_for_no_match() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let (json, code) = run(
        &home,
        &[
            "remember",
            "nonexistent",
            "--level",
            "project",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_eq!(code, 0);
    assert_eq!(json["data"]["memories"].as_array().unwrap().len(), 0);
}

#[test]
fn reflect_shows_memories_and_inbox() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    run(
        &home,
        &[
            "memorize",
            "prefer rust",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    run(
        &home,
        &["note", "todo item", "--root", project.to_str().unwrap()],
    );

    let (json, code) = run(&home, &["reflect", "--root", project.to_str().unwrap()]);
    assert_eq!(code, 0);
    assert_eq!(json["data"]["memories"].as_array().unwrap().len(), 1);
    assert_eq!(json["data"]["inbox"]["size"], 1);
}

#[test]
fn consolidate_promotes_inbox_items() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    run(
        &home,
        &[
            "note",
            "important finding",
            "--root",
            project.to_str().unwrap(),
        ],
    );

    // Dry run first
    let (json, code) = run(
        &home,
        &[
            "consolidate",
            "--dry-run",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_eq!(code, 0);
    assert_eq!(json["data"]["promoted"], 1);
    assert_eq!(json["data"]["dry_run"], true);

    // Real consolidation
    let (json, code) = run(&home, &["consolidate", "--root", project.to_str().unwrap()]);
    assert_eq!(code, 0);
    assert_eq!(json["data"]["promoted"], 1);
    assert_eq!(json["data"]["dry_run"], false);

    // Verify inbox is now empty
    let (json, _) = run(&home, &["reflect", "--root", project.to_str().unwrap()]);
    assert_eq!(json["data"]["inbox"]["size"], 0);
}

#[test]
fn forget_removes_memory() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let (memorize_json, _) = run(
        &home,
        &[
            "memorize",
            "temp memory",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    let filename = memorize_json["data"]["file"].as_str().unwrap();

    // Forget it
    let (json, code) = run(
        &home,
        &["forget", filename, "--root", project.to_str().unwrap()],
    );
    assert_eq!(code, 0);
    assert_eq!(json["data"]["action"], "forgotten");

    // Verify it's gone
    let (json, _) = run(&home, &["reflect", "--root", project.to_str().unwrap()]);
    assert_eq!(json["data"]["memories"].as_array().unwrap().len(), 0);
}

#[test]
fn forget_nonexistent_fails() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let (json, code) = run(
        &home,
        &[
            "forget",
            "nonexistent.md",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_eq!(code, 1);
    assert_eq!(json["ok"], false);
}

#[test]
fn config_init_and_get() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();

    // Init config
    let (json, code) = run(&home, &["config", "init"]);
    assert_eq!(code, 0);
    assert_eq!(json["data"]["action"], "created");

    // Get a known key
    let (json, code) = run(&home, &["config", "get", "embedding.model"]);
    assert_eq!(code, 0);
    assert_eq!(json["data"]["value"], "all-MiniLM-L6-v2");
}

#[test]
fn config_set_updates_value() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();

    run(&home, &["config", "init"]);

    // Set
    let (json, code) = run(&home, &["config", "set", "embedding.model", "test-model"]);
    assert_eq!(code, 0);
    assert_eq!(json["data"]["value"], "test-model");

    // Verify
    let (json, _) = run(&home, &["config", "get", "embedding.model"]);
    assert_eq!(json["data"]["value"], "test-model");
}

#[test]
fn memorize_stdin_json() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let input = serde_json::json!({
        "type": "user",
        "name": "stdin-test",
        "description": "test from stdin",
        "body": "detailed body content",
        "level": "project"
    });

    let output = Command::new(mnemonist_bin())
        .args([
            "memorize",
            "ignored",
            "--stdin",
            "--root",
            project.to_str().unwrap(),
        ])
        .env("HOME", &home)
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            use std::io::Write;
            child
                .stdin
                .take()
                .unwrap()
                .write_all(input.to_string().as_bytes())
                .unwrap();
            child.wait_with_output()
        })
        .expect("failed to run with stdin");

    assert!(output.status.success());
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["data"]["file"], "user_stdin-test.md");
}

#[test]
fn multiple_notes_respect_capacity() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    // Add more notes than the default capacity (7)
    for i in 0..10 {
        run(
            &home,
            &[
                "note",
                &format!("note number {i}"),
                "--root",
                project.to_str().unwrap(),
            ],
        );
    }

    let (json, _) = run(&home, &["reflect", "--root", project.to_str().unwrap()]);
    // Inbox should be capped at capacity (7)
    assert!(json["data"]["inbox"]["size"].as_u64().unwrap() <= 7);
}

/// Redact dynamic fields from CLI JSON output for deterministic snapshots.
fn redact_cli_json(mut json: Value) -> Value {
    // Redact timestamps and paths that vary between runs
    if let Some(data) = json.get_mut("data") {
        // Redact path fields
        for key in ["path", "context"] {
            if data.get(key).is_some_and(|v| v.is_string()) {
                data[key] = Value::String("[path]".to_string());
            }
        }
        // Redact embedded field (depends on Ollama availability)
        if data.get("embedded").is_some() {
            data["embedded"] = Value::String("[env-dependent]".to_string());
        }
        // Redact config show output (contains paths)
        if data.get("config").is_some_and(|v| v.is_string()) {
            data["config"] = Value::String("[toml]".to_string());
        }
        // Redact memories array timestamps
        if let Some(memories) = data.get_mut("memories") {
            if let Some(arr) = memories.as_array_mut() {
                for mem in arr {
                    for ts_key in ["last_accessed", "created_at", "indexed_at"] {
                        if mem.get(ts_key).is_some_and(|v| v.is_string()) {
                            mem[ts_key] = Value::String("[timestamp]".to_string());
                        }
                    }
                }
            }
        }
        // Redact inbox item timestamps
        if let Some(inbox) = data.get_mut("inbox") {
            if let Some(items) = inbox.get_mut("items") {
                if let Some(arr) = items.as_array_mut() {
                    for item in arr {
                        if item.get("created_at").is_some_and(|v| v.is_string()) {
                            item["created_at"] = Value::String("[timestamp]".to_string());
                        }
                    }
                }
            }
        }
        // Redact consolidation timestamps
        if let Some(actions) = data.get_mut("actions") {
            if let Some(arr) = actions.as_array_mut() {
                for action in arr {
                    for ts_key in ["created_at", "last_accessed"] {
                        if action.get(ts_key).is_some_and(|v| v.is_string()) {
                            action[ts_key] = Value::String("[timestamp]".to_string());
                        }
                    }
                }
            }
        }
    }
    json
}

#[test]
fn snapshot_memorize_output() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let (json, _) = run(
        &home,
        &[
            "memorize",
            "always write tests",
            "-t",
            "feedback",
            "--name",
            "write-tests",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    let json = redact_cli_json(json);
    assert_json_snapshot!(json);
}

#[test]
fn snapshot_note_output() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let (json, _) = run(
        &home,
        &[
            "note",
            "investigate logging",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_json_snapshot!(json);
}

#[test]
fn snapshot_reflect_output() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    run(
        &home,
        &[
            "memorize",
            "prefer rust",
            "-t",
            "feedback",
            "--name",
            "prefer-rust",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    run(
        &home,
        &["note", "check logging", "--root", project.to_str().unwrap()],
    );

    let (json, _) = run(&home, &["reflect", "--root", project.to_str().unwrap()]);
    assert_json_snapshot!(redact_cli_json(json));
}

#[test]
fn snapshot_consolidate_dry_run_output() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    run(
        &home,
        &[
            "note",
            "important observation",
            "--root",
            project.to_str().unwrap(),
        ],
    );

    let (json, _) = run(
        &home,
        &[
            "consolidate",
            "--dry-run",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_json_snapshot!(redact_cli_json(json));
}

#[test]
fn snapshot_config_get_output() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();

    run(&home, &["config", "init"]);
    let (json, _) = run(&home, &["config", "get", "embedding.model"]);
    assert_json_snapshot!(json);
}

#[test]
fn snapshot_forget_error_output() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir_all(&home).unwrap();
    let project = tmp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();

    let (json, _) = run(
        &home,
        &[
            "forget",
            "nonexistent.md",
            "--root",
            project.to_str().unwrap(),
        ],
    );
    assert_json_snapshot!(json);
}