mnemo-engine 0.6.0

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
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
//! 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}"
    );
}

// === Phase 3 PR 10b: --embed-cmd / text params ============================
//
// Fake-embedder choice: inline Python. All three GitHub-Actions runners
// (ubuntu-latest, macos-latest, windows-latest) ship Python 3 by default.
// Rust-helper-binary alternatives would need either a published `[[bin]]`
// (clutters the shipped crate) or a `[[example]]` + hard-coded relative
// path (brittle on parallel-test layouts). Python is one import away on
// every supported CI target, keeps the fake embedder as a 10-line
// readable artifact rather than another compilation unit, and matches
// what real users will typically use (sentence-transformers in Python).

/// Platform-appropriate name of the Python 3 interpreter. On Linux and
/// macOS CI images, `python3` is on PATH. On Windows CI images,
/// `python` is on PATH (and `python3` may or may not be).
fn python_cmd() -> &'static str {
    if cfg!(windows) {
        "python"
    } else {
        "python3"
    }
}

/// Write a Python script into a tempfile and return its path.
/// Caller keeps the tempfile alive for the duration of the subprocess.
fn write_py_script(dir: &std::path::Path, name: &str, body: &str) -> std::path::PathBuf {
    let p = dir.join(name);
    std::fs::write(&p, body).unwrap();
    p
}

/// Deterministic text-dependent 4-D fake embedder (PR 10b.2). The
/// vector encodes `len(text)` on one hash-selected axis, so tests
/// can assert that a reply was embedded from the right input and
/// catch misalignment (if EmbedCmd ever handed back a stale leftover
/// line, the lengths wouldn't match).
///
/// Also implements four special triggers for the respawn / timeout /
/// alignment tests:
///   - "HANG"   → block forever (never writes a reply).
///   - "JUNK"   → emit a non-JSON line.
///   - "DOUBLE" → emit two reply lines for one input.
///   - everything else → the normal text-dependent reply.
const FAKE_EMBED_PY: &str = r#"
import sys, json, time
DIMS = 4
def encode(s):
    axis = sum(bytearray(s.encode("utf-8"))) % DIMS
    v = [0.0] * DIMS
    v[axis] = float(len(s)) + 1.0  # non-zero, text-dependent
    return v
for line in sys.stdin:
    s = json.loads(line)
    if s == "HANG":
        time.sleep(3600)
    elif s == "JUNK":
        print("not-valid-json", flush=True)
    elif s == "DOUBLE":
        print(json.dumps(encode(s)), flush=True)
        print(json.dumps(encode("stowaway")), flush=True)
    else:
        print(json.dumps(encode(s)), flush=True)
"#;

/// Python helper that encodes a text the same way `FAKE_EMBED_PY`
/// does, invoked from tests to compute an expected vector.
fn fake_vector_for(text: &str) -> Vec<f32> {
    let axis = text.bytes().map(|b| b as usize).sum::<usize>() % 4;
    let mut v = vec![0.0f32; 4];
    v[axis] = text.len() as f32 + 1.0;
    v
}

/// Fake embedder that prints an unsolicited startup banner to stdout
/// before entering the read loop (common Python-logging footgun).
/// EmbedCmd must detect the extra line and refuse to proceed.
const BANNER_EMBED_PY: &str = r#"
import sys, json
print("[banner] model loaded", flush=True)
DIMS = 4
for line in sys.stdin:
    s = json.loads(line)
    axis = sum(bytearray(s.encode("utf-8"))) % DIMS
    v = [0.0] * DIMS
    v[axis] = float(len(s)) + 1.0
    print(json.dumps(v), flush=True)
"#;

/// Fake embedder that reads one line, then exits non-zero — exercises
/// the "child crashed mid-session" branch of EmbedCmd.
const CRASHING_EMBED_PY: &str = r#"
import sys, json
for line in sys.stdin:
    _ = json.loads(line)
    sys.exit(1)
"#;

/// Fake embedder that returns the wrong width (2 dims instead of 4).
const WRONG_WIDTH_EMBED_PY: &str = r#"
import sys, json
for line in sys.stdin:
    _ = json.loads(line)
    print(json.dumps([1.0, 2.0]), flush=True)
"#;

/// Fake embedder that returns an all-zero vector of the correct width —
/// FIX-1 R-001.6: this must be rejected via `fail_and_restart` even
/// though its length and JSON shape are both fine, because an all-zero
/// vector cannot be scored by cosine or dot-product similarity.
const ZERO_VECTOR_EMBED_PY: &str = r#"
import sys, json
for line in sys.stdin:
    _ = json.loads(line)
    print(json.dumps([0.0, 0.0, 0.0, 0.0]), flush=True)
"#;

/// Fake embedder that returns a vector containing a value that
/// overflows `f32` — `1e40` is above `f32::MAX` (~3.4e38), so
/// serde_json decodes it as `f32::INFINITY`. FIX-1 R-001.6: any
/// non-finite value (±∞ or, through a non-standard JSON decoder,
/// NaN) must fail-and-restart. We drive the check through +∞
/// because standard JSON doesn't allow a bare `NaN` token —
/// serde_json rejects that at parse time (and surfaces as
/// "malformed JSON" on its own), so a Python `math.nan` would
/// never reach the finiteness check we care about here.
const NON_FINITE_VECTOR_EMBED_PY: &str = r#"
import sys, json
for line in sys.stdin:
    _ = json.loads(line)
    print(json.dumps([1.0, 1e40, 2.0, 3.0]), flush=True)
"#;

/// Fake embedder that touches a marker file on startup. Used to verify
/// the child is spawned once across multiple MCP tool calls.
/// Vector is text-dependent (PR 10b.2 convention).
const STARTUP_MARKER_EMBED_PY: &str = r#"
import sys, json, os
with open(os.environ["MNEMO_TEST_MARKER"], "a") as f:
    f.write("start\n")
DIMS = 4
for line in sys.stdin:
    s = json.loads(line)
    axis = sum(bytearray(s.encode("utf-8"))) % DIMS
    v = [0.0] * DIMS
    v[axis] = float(len(s)) + 1.0
    print(json.dumps(v), flush=True)
"#;

fn embed_cmd_arg(script_path: &std::path::Path) -> String {
    // Quote the path so shlex_split in EmbedCmd parses it as one argv
    // entry even if the tempdir contains spaces (common on Windows CI
    // where runner paths include "runneradmin").
    format!("{} \"{}\"", python_cmd(), script_path.display())
}

#[test]
fn cli_remember_and_recall_text_round_trip() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("em.mnemo");
    let pp = "em-pw";
    // Seed with the scaffold manifest so `set_embedder` has a target
    // for the identity write.
    {
        let mut db = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
        db.remember(Memory::scaffold_manifest(4)).unwrap();
        db.flush().unwrap();
    }
    let script = write_py_script(dir.path(), "fake.py", FAKE_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);

    // mnemo remember --text "alpha" --embed-cmd '…' --embed-name fake-4d
    let out = Command::new(bin())
        .args([
            "remember",
            path.to_str().unwrap(),
            "--text",
            "alpha",
            "--embed-cmd",
            &ecmd,
            "--embed-name",
            "fake-4d",
            "--embed-normalize",
            "true",
        ])
        .env("MNEMO_PASSPHRASE", pp)
        .output()
        .expect("spawn mnemo remember");
    assert!(
        out.status.success(),
        "mnemo remember failed: status={:?} stdout={} stderr={}",
        out.status,
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr),
    );
    // Stdout should be a ULID (one line, 26 chars).
    let ulid = String::from_utf8(out.stdout).unwrap();
    assert!(
        ulid.trim().len() >= 26,
        "expected a ULID on stdout, got {ulid:?}"
    );

    // mnemo recall --text "alpha" ... → the just-stored memory should
    // surface first.
    let out = Command::new(bin())
        .args([
            "recall",
            path.to_str().unwrap(),
            "--text",
            "alpha",
            "--embed-cmd",
            &ecmd,
            "--embed-name",
            "fake-4d",
            "--top-k",
            "5",
            "--format",
            "json",
            "--no-track",
        ])
        .env("MNEMO_PASSPHRASE", pp)
        .output()
        .expect("spawn mnemo recall");
    assert!(
        out.status.success(),
        "mnemo recall failed: status={:?} stderr={}",
        out.status,
        String::from_utf8_lossy(&out.stderr),
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(
        stdout.contains("alpha"),
        "recall --text output should contain the stored content, got: {stdout}"
    );
}

#[test]
fn cli_recall_text_without_embed_cmd_errors_cleanly() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("em-noec.mnemo");
    let pp = "em-pw";
    {
        let _ = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
    }
    let out = Command::new(bin())
        .args([
            "recall",
            path.to_str().unwrap(),
            "--text",
            "alpha",
            // Deliberately NO --embed-cmd / --embed-name.
        ])
        .env("MNEMO_PASSPHRASE", pp)
        .output()
        .expect("spawn mnemo recall");
    assert!(
        !out.status.success(),
        "recall --text without --embed-cmd should fail"
    );
    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(
        combined.to_lowercase().contains("embed"),
        "error should mention embedder, got: {combined}"
    );
}

#[test]
fn cli_remember_text_crashing_embedder_errors_cleanly() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("em-crash.mnemo");
    let pp = "em-pw";
    {
        let _ = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
    }
    let script = write_py_script(dir.path(), "crash.py", CRASHING_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let out = Command::new(bin())
        .args([
            "remember",
            path.to_str().unwrap(),
            "--text",
            "boom",
            "--embed-cmd",
            &ecmd,
            "--embed-name",
            "fake-4d",
        ])
        .env("MNEMO_PASSPHRASE", pp)
        .output()
        .expect("spawn mnemo remember");
    assert!(
        !out.status.success(),
        "remember with a crashing embedder should fail"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    // Either the child "exited before this request" (if we noticed the
    // first-call exit before writing) OR a protocol/read failure after.
    // Both are acceptable; the point is a clear non-zero exit.
    assert!(
        stderr.to_lowercase().contains("embed-cmd") || stderr.to_lowercase().contains("embedder"),
        "stderr should mention the embedder failure, got: {stderr}"
    );
}

#[test]
fn cli_remember_text_wrong_width_errors_cleanly() {
    let dir = tempdir().unwrap();
    let path = dir.path().join("em-ww.mnemo");
    let pp = "em-pw";
    {
        let _ = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
    }
    let script = write_py_script(dir.path(), "ww.py", WRONG_WIDTH_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let out = Command::new(bin())
        .args([
            "remember",
            path.to_str().unwrap(),
            "--text",
            "two-dims",
            "--embed-cmd",
            &ecmd,
            "--embed-name",
            "fake-4d",
        ])
        .env("MNEMO_PASSPHRASE", pp)
        .output()
        .expect("spawn mnemo remember");
    assert!(
        !out.status.success(),
        "remember with wrong-width embedder should fail"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("dim") || stderr.contains("2"),
        "stderr should mention the dimension mismatch, got: {stderr}"
    );
}

#[test]
fn mcp_serve_with_embedder_text_params_and_persistent_child() {
    use std::io::Write;
    use std::process::{Command, Stdio};

    let dir = tempdir().unwrap();
    let path = dir.path().join("mcp-em.mnemo");
    let pp = "em-pw";
    // Seed with the scaffold manifest so the embedder's identity has
    // somewhere to land on set_embedder.
    {
        let mut db = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
        db.remember(Memory::scaffold_manifest(4)).unwrap();
        db.flush().unwrap();
    }
    let marker = dir.path().join("startup.marker");
    let script = write_py_script(dir.path(), "startup.py", STARTUP_MARKER_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);

    // Same retry-on-flock-race pattern as the plain mcp_serve test.
    let mut out_opt = None;
    let mut last_stderr = String::new();
    for attempt in 0..5u32 {
        if attempt > 0 {
            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",
                "--embed-cmd",
                &ecmd,
                "--embed-name",
                "fake-4d",
            ])
            .env("MNEMO_PASSPHRASE", pp)
            .env("MNEMO_TEST_MARKER", &marker)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("failed to spawn mnemo serve --mcp");

        let stdin = child.stdin.as_mut().expect("child stdin missing");
        // Three tool/call messages that go through the embedder, plus
        // the initialize dance. If the child is reused the marker file
        // ends with one line; if a new child spawned per call it would
        // have three.
        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/call","params":{"name":"remember","arguments":{"content":"alpha","memory_type":"semantic","text":"alpha"}}}"#,
            r#"{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"remember","arguments":{"content":"beta","memory_type":"semantic","text":"beta"}}}"#,
            r#"{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"recall","arguments":{"text":"alpha","top_k":5}}}"#,
        ];
        for line in script {
            writeln!(stdin, "{line}").unwrap();
        }
        drop(child.stdin.take());

        let out = child.wait_with_output().expect("child wait");
        last_stderr = String::from_utf8_lossy(&out.stderr).to_string();
        if out.status.success() {
            out_opt = Some(out);
            break;
        }
        if !last_stderr.contains("file already locked") {
            panic!(
                "mnemo serve --mcp failed (attempt {attempt}): status={:?} stderr={last_stderr}",
                out.status
            );
        }
    }
    let out = out_opt.unwrap_or_else(|| {
        panic!("mnemo serve --mcp never acquired the lock: stderr={last_stderr}")
    });

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("\"id\":2"),
        "expected a response to remember call id 2, got stdout: {stdout}"
    );
    assert!(
        stdout.contains("\"id\":4"),
        "expected a response to recall call id 4, got stdout: {stdout}"
    );
    // alpha must come back on the recall; the fake embedder is
    // deterministic so axis-of-"alpha" matches axis-of-"alpha".
    assert!(
        stdout.contains("alpha"),
        "expected 'alpha' in recall output, got: {stdout}"
    );
    // Marker file should have exactly ONE startup line — persistent child.
    let marker_text = std::fs::read_to_string(&marker).unwrap_or_default();
    let starts = marker_text.lines().filter(|l| *l == "start").count();
    assert_eq!(
        starts, 1,
        "embedder child should have started once; marker file: {marker_text:?}"
    );
}

#[test]
fn mcp_text_param_without_embedder_returns_helpful_error() {
    use std::io::Write;
    use std::process::{Command, Stdio};

    let dir = tempdir().unwrap();
    let path = dir.path().join("mcp-noem.mnemo");
    let pp = "em-pw";
    {
        let _ = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
    }

    let mut out_opt = None;
    let mut last_stderr = String::new();
    for attempt in 0..5u32 {
        if attempt > 0 {
            let backoff_ms = 100u64 << (attempt - 1);
            std::thread::sleep(std::time::Duration::from_millis(backoff_ms));
        }
        let mut child = Command::new(bin())
            // No --embed-cmd / --embed-name.
            .args(["serve", path.to_str().unwrap(), "--mcp"])
            .env("MNEMO_PASSPHRASE", pp)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("spawn mnemo serve --mcp");
        let stdin = child.stdin.as_mut().unwrap();
        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/call","params":{"name":"recall","arguments":{"text":"anything"}}}"#,
        ];
        for line in script {
            writeln!(stdin, "{line}").unwrap();
        }
        drop(child.stdin.take());
        let out = child.wait_with_output().expect("child wait");
        last_stderr = String::from_utf8_lossy(&out.stderr).to_string();
        if out.status.success() {
            out_opt = Some(out);
            break;
        }
        if !last_stderr.contains("file already locked") {
            panic!(
                "mnemo serve --mcp failed (attempt {attempt}): status={:?} stderr={last_stderr}",
                out.status
            );
        }
    }
    let out = out_opt.unwrap_or_else(|| {
        panic!("mnemo serve --mcp never acquired the lock: stderr={last_stderr}")
    });
    let stdout = String::from_utf8_lossy(&out.stdout);
    // The recall call should surface an RPC error pointing at
    // --embed-cmd / --embed-name.
    assert!(
        stdout.contains("--embed-cmd") || stdout.contains("embedder"),
        "expected a helpful error mentioning --embed-cmd, got: {stdout}"
    );
}

// === Phase 3 PR 10b.2: timeout, respawn, alignment =======================

#[test]
fn cli_remember_text_hang_times_out_bounded() {
    // The fake blocks forever on "HANG". With --embed-timeout 2 the
    // CLI must error well under its default 60s. Elapsed-time bound
    // of 20s is deliberately loose to absorb CI variance; the correct
    // timeout fires ~2–3s in.
    let dir = tempdir().unwrap();
    let path = dir.path().join("hang.mnemo");
    let pp = "em-pw";
    {
        let _ = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
    }
    let script = write_py_script(dir.path(), "hang.py", FAKE_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let start = std::time::Instant::now();
    let out = Command::new(bin())
        .args([
            "remember",
            path.to_str().unwrap(),
            "--text",
            "HANG",
            "--embed-cmd",
            &ecmd,
            "--embed-name",
            "fake-4d",
            "--embed-timeout",
            "2",
        ])
        .env("MNEMO_PASSPHRASE", pp)
        .output()
        .expect("spawn mnemo remember");
    let elapsed = start.elapsed();
    assert!(
        !out.status.success(),
        "remember on HANG should error (child never replies)"
    );
    assert!(
        elapsed < std::time::Duration::from_secs(20),
        "hang must trip the --embed-timeout bound; elapsed={elapsed:?}"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("timed out") || stderr.contains("embed-cmd"),
        "stderr should name the timeout, got: {stderr}"
    );
}

#[test]
fn embed_cmd_recovers_after_junk_reply_with_correct_vector() {
    use mnemo::{EmbedCmd, Embedder, EmbedderIdentity};
    // Call "JUNK" → fake prints an invalid-JSON line → EmbedCmd errors
    // and kills the child. Next call for "abc" must respawn and return
    // abc's text-dependent vector, not a leftover.
    let dir = tempdir().unwrap();
    let script = write_py_script(dir.path(), "junk.py", FAKE_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let identity = EmbedderIdentity {
        name: "fake-4d".into(),
        dimensions: 4,
        normalize: true,
    };
    let em = EmbedCmd::new(&ecmd, identity)
        .unwrap()
        .with_timeout(std::time::Duration::from_secs(5));
    let err = em.embed(&["JUNK"]).unwrap_err();
    let msg = format!("{err}");
    assert!(
        msg.contains("malformed JSON") || msg.contains("respawn"),
        "JUNK should raise a protocol error, got: {msg}"
    );
    // Next call must respawn + return abc's actual vector.
    let out = em.embed(&["abc"]).unwrap();
    assert_eq!(out.len(), 1);
    assert_eq!(out[0], fake_vector_for("abc"));
}

#[test]
fn embed_cmd_recovers_after_double_reply_with_correct_vector() {
    use mnemo::{EmbedCmd, Embedder, EmbedderIdentity};
    // "DOUBLE" prints TWO lines for one input. The first is read as
    // the DOUBLE reply (fine); the second is a stale leftover. The
    // guard in `embed()` must detect it before writing the next
    // request OR the width check must catch the stray reply. Either
    // way, the next call for "abc" must return abc's vector.
    let dir = tempdir().unwrap();
    let script = write_py_script(dir.path(), "double.py", FAKE_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let identity = EmbedderIdentity {
        name: "fake-4d".into(),
        dimensions: 4,
        normalize: true,
    };
    let em = EmbedCmd::new(&ecmd, identity)
        .unwrap()
        .with_timeout(std::time::Duration::from_secs(5));
    // DOUBLE succeeds on this call (returns the first JSON line).
    let out = em.embed(&["DOUBLE"]).unwrap();
    assert_eq!(out.len(), 1);
    assert_eq!(out[0], fake_vector_for("DOUBLE"));
    // Give the background reader thread a moment to actually deliver
    // the stowaway line into the channel. Without this the drain-pass
    // on the next call can race the reader and miss it; we want to
    // test the detection path, not the race window.
    std::thread::sleep(std::time::Duration::from_millis(200));
    // The second (stale) line is now queued. Next call MUST either
    // detect-and-restart OR, if it slipped through, return the right
    // vector. Either way the result for "abc" must be abc's vector,
    // never the stowaway's.
    let next = em.embed(&["abc"]);
    match next {
        Ok(v) => {
            assert_eq!(v.len(), 1);
            assert_eq!(
                v[0],
                fake_vector_for("abc"),
                "next call MUST return abc's vector, never a leftover"
            );
        }
        Err(e) => {
            // Error is acceptable (planner: "an error is allowed; a
            // mismatched vector never is"). But a FOLLOWING call must
            // then succeed and return the right vector.
            let msg = format!("{e}");
            assert!(
                msg.contains("respawn") || msg.contains("outside a reply"),
                "if first call errors it must be a clear protocol error, got: {msg}"
            );
            let recovery = em.embed(&["abc"]).unwrap();
            assert_eq!(recovery[0], fake_vector_for("abc"));
        }
    }
}

#[test]
fn embed_cmd_rejects_all_zero_vector_and_respawns() {
    // FIX-1 R-001.6: an all-zero reply of the correct width must trip
    // `fail_and_restart`. Follow-up call for "abc" must spawn a fresh
    // child and return abc's real vector, not a leftover from the
    // offending reply.
    use mnemo::{EmbedCmd, Embedder, EmbedderIdentity};
    let dir = tempdir().unwrap();
    let script = write_py_script(dir.path(), "zero.py", ZERO_VECTOR_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let identity = EmbedderIdentity {
        name: "fake-4d".into(),
        dimensions: 4,
        normalize: true,
    };
    let em = EmbedCmd::new(&ecmd, identity)
        .unwrap()
        .with_timeout(std::time::Duration::from_secs(5));
    let err = em.embed(&["anything"]).unwrap_err();
    let msg = format!("{err}");
    assert!(
        msg.contains("all-zero") && msg.contains("respawn"),
        "zero vector must surface as the dedicated protocol error, got: {msg}"
    );
    // Follow-up spawns a fresh child (which will also return zeros,
    // because the script is deterministic). The point is that we
    // never got the zero vector *back* as a successful embed result.
    let err2 = em.embed(&["abc"]).unwrap_err();
    assert!(format!("{err2}").contains("all-zero"));
}

#[test]
fn embed_cmd_rejects_non_finite_vector_and_respawns() {
    // FIX-1 R-001.6: an ±∞ entry anywhere in the reply must trip
    // `fail_and_restart` even though the width is correct — a non-
    // finite similarity score poisons every ranked list it reaches.
    // We drive the check through `+∞` (produced by serde_json's f32
    // decoder on an overflowing JSON number like `1e40`) rather than
    // NaN, which standard JSON doesn't allow as a bare token and
    // serde_json rejects at parse time.
    use mnemo::{EmbedCmd, Embedder, EmbedderIdentity};
    let dir = tempdir().unwrap();
    let script = write_py_script(dir.path(), "non_finite.py", NON_FINITE_VECTOR_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let identity = EmbedderIdentity {
        name: "fake-4d".into(),
        dimensions: 4,
        normalize: true,
    };
    let em = EmbedCmd::new(&ecmd, identity)
        .unwrap()
        .with_timeout(std::time::Duration::from_secs(5));
    let err = em.embed(&["anything"]).unwrap_err();
    let msg = format!("{err}");
    assert!(
        msg.contains("non-finite") && msg.contains("respawn"),
        "±∞ reply must surface as the dedicated protocol error, got: {msg}"
    );
}

#[test]
fn embed_cmd_detects_stdout_banner_before_requests() {
    use mnemo::{EmbedCmd, Embedder, EmbedderIdentity};
    let dir = tempdir().unwrap();
    let script = write_py_script(dir.path(), "banner.py", BANNER_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let identity = EmbedderIdentity {
        name: "fake-4d".into(),
        dimensions: 4,
        normalize: true,
    };
    let em = EmbedCmd::new(&ecmd, identity)
        .unwrap()
        .with_timeout(std::time::Duration::from_secs(5));
    // Give the child a brief moment to print its banner before the
    // first embed() call runs its drain-pass; otherwise the race may
    // put the banner into the reply slot and we'd see a malformed-JSON
    // error instead. Either outcome is correct per the planner's rule
    // ("an error is allowed; a mismatched vector never is") but the
    // "stdout outside a reply" message is the clearer one.
    std::thread::sleep(std::time::Duration::from_millis(200));
    let err = em.embed(&["anything"]).unwrap_err();
    let msg = format!("{err}");
    assert!(
        msg.contains("stdout") || msg.contains("outside a reply") || msg.contains("malformed"),
        "banner should surface as a stdout-protocol error, got: {msg}"
    );
    // Note: we don't assert recovery here. BANNER_EMBED_PY is unusual
    // in that every respawn also prints a banner, so a follow-up call
    // will error again — the respawn mechanism is tested in the
    // JUNK/DOUBLE tests, which use a well-behaved fake after the
    // offending call. What we're pinning down here is just that a
    // startup banner surfaces as a clear protocol error, never as a
    // mismatched vector.
}

#[test]
fn embed_cmd_two_thousand_text_batch_does_not_deadlock() {
    use mnemo::{EmbedCmd, Embedder, EmbedderIdentity};
    // Pre-10b.2 (write-all-then-read-all) this would deadlock on a
    // batch big enough to fill the child's stdout pipe buffer. The
    // reader thread drains stdout in parallel with our stdin writes,
    // so a 2000-line batch completes in the normal path.
    let dir = tempdir().unwrap();
    let script = write_py_script(dir.path(), "batch.py", FAKE_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);
    let identity = EmbedderIdentity {
        name: "fake-4d".into(),
        dimensions: 4,
        normalize: true,
    };
    let em = EmbedCmd::new(&ecmd, identity)
        .unwrap()
        .with_timeout(std::time::Duration::from_secs(30));
    // 2000 unique-ish texts so the fake doesn't short-circuit via a
    // trigger ("HANG"/"JUNK"/"DOUBLE").
    let texts: Vec<String> = (0..2000).map(|i| format!("text-{i:04}")).collect();
    let refs: Vec<&str> = texts.iter().map(|s| s.as_str()).collect();
    let out = em.embed(&refs).unwrap();
    assert_eq!(out.len(), 2000);
    // Spot-check a few vectors line up with their inputs.
    for i in [0, 123, 999, 1999] {
        assert_eq!(
            out[i],
            fake_vector_for(&texts[i]),
            "vector at index {i} must correspond to its input text"
        );
    }
}

#[test]
fn mcp_hang_bounded_and_server_survives_for_next_call() {
    use std::io::Write;
    use std::process::{Command, Stdio};

    let dir = tempdir().unwrap();
    let path = dir.path().join("mcp-hang.mnemo");
    let pp = "em-pw";
    {
        let mut db = Mnemo::create(&path, pp, fast_cfg(4)).unwrap();
        db.remember(Memory::scaffold_manifest(4)).unwrap();
        db.flush().unwrap();
    }
    let script = write_py_script(dir.path(), "hang.py", FAKE_EMBED_PY);
    let ecmd = embed_cmd_arg(&script);

    let mut out_opt = None;
    let mut last_stderr = String::new();
    for attempt in 0..5u32 {
        if attempt > 0 {
            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",
                "--embed-cmd",
                &ecmd,
                "--embed-name",
                "fake-4d",
                "--embed-timeout",
                "2",
            ])
            .env("MNEMO_PASSPHRASE", pp)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("spawn mnemo serve --mcp");

        let stdin = child.stdin.as_mut().unwrap();
        // HANG → RPC error (bounded by --embed-timeout 2). Next call
        // for a normal text must succeed with the correct vector.
        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/call","params":{"name":"remember","arguments":{"content":"will_hang","memory_type":"semantic","text":"HANG"}}}"#,
            r#"{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"remember","arguments":{"content":"survives","memory_type":"semantic","text":"survives"}}}"#,
        ];
        for line in script {
            writeln!(stdin, "{line}").unwrap();
        }
        drop(child.stdin.take());

        let start = std::time::Instant::now();
        let out = child.wait_with_output().expect("child wait");
        let elapsed = start.elapsed();
        last_stderr = String::from_utf8_lossy(&out.stderr).to_string();
        if out.status.success() {
            assert!(
                elapsed < std::time::Duration::from_secs(30),
                "server should return before 30s even with a HANG; elapsed={elapsed:?}"
            );
            out_opt = Some(out);
            break;
        }
        if !last_stderr.contains("file already locked") {
            panic!(
                "mnemo serve --mcp failed (attempt {attempt}): status={:?} stderr={last_stderr}",
                out.status
            );
        }
    }
    let out = out_opt.unwrap_or_else(|| {
        panic!("mnemo serve --mcp never acquired the lock: stderr={last_stderr}")
    });
    let stdout = String::from_utf8_lossy(&out.stdout);
    // The HANG call should produce an RPC error (id 2).
    assert!(
        stdout.contains("\"id\":2") && (stdout.contains("timed out") || stdout.contains("error")),
        "expected an RPC error on the HANG call id 2, got stdout: {stdout}"
    );
    // The next call (id 3) must have succeeded — server stayed up
    // and the child was respawned.
    assert!(
        stdout.contains("\"id\":3"),
        "expected a response to call id 3 after the HANG; got: {stdout}"
    );
    assert!(
        !stdout
            .lines()
            .any(|l| l.contains("\"id\":3") && l.contains("error")),
        "call id 3 (post-HANG) should have succeeded, not errored: {stdout}"
    );
}