trusty-memory 0.10.0

MCP server (stdio + HTTP/SSE) for trusty-memory
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
//! Concurrent multi-transport performance test suite for the trusty-memory
//! daemon (PR #149 multi-transport refactor).
//!
//! Why: PR #149 introduced a single daemon process speaking three transports
//! (HTTP/SSE on port 7070, JSON-RPC over UDS, and the
//! `trusty-memory-mcp-bridge` stdio shim that fronts UDS). The redb-locking
//! design hinges on the daemon being able to handle concurrent traffic from
//! all three transports without contention, dropped responses, or runaway
//! latency. The existing `tests/uds_roundtrip.rs` tests prove the happy path
//! but do not measure throughput, p99 latency, error rates under burst load,
//! or the daemon's behaviour under sustained pressure.
//!
//! What: six `#[ignore]`-tagged integration tests that each measure a
//! different facet of the live daemon's concurrent-performance envelope:
//!   - HTTP concurrent reads (`test_http_concurrent_reads`)
//!   - HTTP concurrent mixed reads + writes (`test_http_concurrent_rw`)
//!   - HTTP burst test (`test_http_burst`)
//!   - UDS JSON-RPC concurrent requests (`test_uds_concurrent`)
//!   - MCP bridge concurrent processes (`test_bridge_concurrent`)
//!   - HTTP sustained-load stability (`test_http_sustained_load`)
//!
//! Test: all tests are marked `#[ignore]` so they only run with
//!   `cargo test -p trusty-memory --test concurrent_perf -- --include-ignored --nocapture`.
//!   They require a live daemon listening on the canonical HTTP + UDS
//!   addresses. The suite refuses to start (panic with a clear message) if
//!   `GET /health` is unreachable or the UDS socket is missing; it does NOT
//!   require `status: "ok"` because the daemon's `/health` self-probe is
//!   racy under load (see `assert_daemon_alive` for full rationale).

use futures::future::join_all;
use serde_json::{json, Value};
use std::path::PathBuf;
use std::process::Stdio;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::UnixStream;
use tokio::process::Command;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Canonical HTTP endpoint exposed by the live daemon.
///
/// Why: the daemon's startup pins this address in
/// `<data_root>/http_addr`; tests against the live local daemon hit the
/// same well-known port that Claude Code's `.mcp.json` references.
/// Test: `assert_daemon_alive` validates the daemon is reachable here
/// before any sub-test runs.
const HTTP_BASE: &str = "http://127.0.0.1:7070";

/// Default bridge binary on PATH (installed via `cargo install --path`).
///
/// Why: the bridge process tests fork this binary and pipe NDJSON
/// through stdin/stdout. PATH-relative resolution keeps the test
/// portable across dev environments.
/// Test: `test_bridge_concurrent` errors out clearly if the binary is
/// missing.
const BRIDGE_BIN: &str = "trusty-memory-mcp-bridge";

/// Soft request timeout for HTTP calls.
///
/// Why: a daemon under heavy contention may still complete a request,
/// just slowly. 30 s is generous enough that a stalled-but-recoverable
/// request still counts as a success; longer than that, we want to
/// flag it as a real failure.
/// Test: used by every reqwest client constructor in this file.
const HTTP_TIMEOUT: Duration = Duration::from_secs(30);

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Resolve the UDS path written by the live daemon to
/// `<data_root>/uds_addr`, falling back to the canonical `$TMPDIR`
/// location used by the production daemon on macOS.
///
/// Why: tests want the actual socket the live daemon is bound to. The
/// daemon writes the absolute path to its data-root discovery file
/// during startup; we read it directly. If the discovery file is
/// missing (older daemon, or a non-default data root), the canonical
/// `$TMPDIR/trusty-memory.sock` path is the production default.
/// What: tries `~/Library/Application Support/trusty-memory/uds_addr`
/// (macOS), then `$XDG_DATA_HOME/.../uds_addr` (Linux), then falls
/// back to the OS-default socket path.
/// Test: `assert_daemon_alive` opens this socket as a smoke check.
fn resolve_uds_path() -> PathBuf {
    // Try the data-root discovery file first.
    if let Ok(data_dir) = trusty_common::resolve_data_dir("trusty-memory") {
        let addr_file = data_dir.join("uds_addr");
        if let Ok(contents) = std::fs::read_to_string(&addr_file) {
            let path = contents.trim();
            if !path.is_empty() {
                return PathBuf::from(path);
            }
        }
    }
    // Canonical $TMPDIR fallback (production default on macOS).
    let runtime = std::env::var("XDG_RUNTIME_DIR")
        .ok()
        .filter(|s| !s.is_empty())
        .or_else(|| std::env::var("TMPDIR").ok().filter(|s| !s.is_empty()))
        .unwrap_or_else(|| std::env::temp_dir().to_string_lossy().to_string());
    PathBuf::from(runtime).join("trusty-memory.sock")
}

/// Build a reqwest client suitable for the test suite.
///
/// Why: every HTTP test wants the same timeout settings, pooled
/// connection reuse, and HTTP/1.1 (the daemon does not speak HTTP/2).
/// Centralising the builder prevents copy-paste drift.
/// What: returns a configured `reqwest::Client`.
/// Test: used by every HTTP test below.
fn http_client() -> reqwest::Client {
    reqwest::Client::builder()
        .timeout(HTTP_TIMEOUT)
        .tcp_nodelay(true)
        .pool_max_idle_per_host(64)
        .build()
        .expect("reqwest client build")
}

/// Send a JSON-RPC request to the live daemon over HTTP and return the
/// parsed envelope.
///
/// Why: every HTTP-driven test needs the same `POST /rpc` dance; this
/// helper keeps the call site minimal so the tests focus on the
/// concurrency pattern under measurement.
/// What: POSTs the JSON envelope, awaits the response, parses the
/// body as JSON. Returns the parsed envelope plus the round-trip
/// latency.
/// Test: every HTTP test below.
async fn http_rpc(client: &reqwest::Client, req: Value) -> Result<(Value, Duration), String> {
    let url = format!("{HTTP_BASE}/rpc");
    let started = Instant::now();
    let resp = client
        .post(&url)
        .json(&req)
        .send()
        .await
        .map_err(|e| format!("send: {e}"))?;
    if !resp.status().is_success() {
        return Err(format!("http status {}", resp.status()));
    }
    let body: Value = resp.json().await.map_err(|e| format!("parse: {e}"))?;
    let elapsed = started.elapsed();
    Ok((body, elapsed))
}

/// Pre-flight assert: the live daemon is reachable.
///
/// Why: the suite is `#[ignore]` precisely because it requires the
/// live daemon. If the operator forgot to start it, fail loudly with
/// an instruction instead of timing out 100 concurrent requests.
/// What: GETs `/health`, asserts HTTP 200 and a parseable body with
/// a `version` field. Does NOT assert `status == "ok"` because the
/// daemon's `/health` route runs an internal store/recall probe that
/// is intentionally racy under load — a parallel test issuing 500
/// concurrent requests can make the embedder + HNSW reindexing fall
/// behind the probe's deadline and flip the status to "degraded"
/// even though the daemon is still answering every external request
/// correctly. Returns the daemon version string for log output.
/// Test: called first by every test in this file.
async fn assert_daemon_alive(client: &reqwest::Client) -> String {
    let url = format!("{HTTP_BASE}/health");
    let resp = client.get(&url).send().await.unwrap_or_else(|e| {
        panic!(
            "live daemon not reachable at {HTTP_BASE} ({e}); start it with `trusty-memory start`"
        );
    });
    assert!(
        resp.status().is_success(),
        "GET /health returned {}",
        resp.status()
    );
    let body: Value = resp.json().await.expect("parse /health");
    let version = body["version"].as_str().unwrap_or("?").to_string();
    let uds = resolve_uds_path();
    assert!(
        uds.exists(),
        "UDS socket missing at {} — daemon may not have bound the UDS listener",
        uds.display()
    );
    version
}

/// Stronger health probe: returns true iff `/health` returns 200 and
/// has a `version` field. Does NOT require `status == "ok"` (see
/// `assert_daemon_alive` for rationale).
///
/// Why: the sustained-load test needs to confirm the daemon is still
/// responsive after the load run, without tripping on the racy
/// store/recall self-probe.
/// What: GETs `/health`, returns `Ok((rss_mb, raw_status))` on
/// success or `Err(message)` otherwise.
/// Test: used by `test_http_sustained_load` and
/// `test_http_concurrent_rw`.
async fn probe_health(client: &reqwest::Client) -> Result<(f64, String), String> {
    let url = format!("{HTTP_BASE}/health");
    let resp = client
        .get(&url)
        .send()
        .await
        .map_err(|e| format!("send: {e}"))?;
    if !resp.status().is_success() {
        return Err(format!("status {}", resp.status()));
    }
    let body: Value = resp.json().await.map_err(|e| format!("parse: {e}"))?;
    let rss = body["rss_mb"].as_f64().unwrap_or(0.0);
    let status = body["status"].as_str().unwrap_or("?").to_string();
    Ok((rss, status))
}

/// Provision an isolated test palace and seed it with one memory entry.
///
/// Why: tests must not pollute real palaces. Creating a UUID-suffixed
/// palace per test keeps the data scoped and lets the daemon's
/// existing GC clean it up later. The seed entry guarantees
/// `memory_recall` against this palace returns something useful so
/// recall throughput measurements aren't dominated by an empty-index
/// fast path.
/// What: calls `palace_create` then `memory_remember` (with `force =
/// true` to bypass the min-token gate). Returns the palace name.
/// Test: every test that performs writes calls this first.
async fn provision_palace(client: &reqwest::Client, tag: &str) -> String {
    let palace = format!("perf-{tag}-{}", uuid::Uuid::new_v4());
    let create = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "palace_create",
        "params": {"name": palace}
    });
    let (resp, _) = http_rpc(client, create).await.expect("palace_create");
    assert!(
        resp.get("error").is_none_or(|e| e.is_null()),
        "palace_create failed: {resp:?}"
    );

    // Seed entry — long enough to satisfy the min-token gate (which
    // defaults to ~6 tokens) so a baseline recall returns content.
    let seed = json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "memory_remember",
        "params": {
            "palace": palace,
            "text": "Seed entry for concurrent perf testing: this fixture exists so recall queries against the palace return at least one result, exercising the BM25 + vector retrieval pipeline rather than the empty-index fast path.",
            "force": true
        }
    });
    let (resp, _) = http_rpc(client, seed).await.expect("seed memory_remember");
    assert!(
        resp.get("error").is_none_or(|e| e.is_null()),
        "seed memory_remember failed: {resp:?}"
    );
    palace
}

/// Compute (min, mean, p50, p95, p99, max) over a vector of durations.
///
/// Why: each test reports a full latency distribution, not just a
/// mean. Sorting in place + index-based percentiles is the simplest
/// correct approach for the sample sizes we exercise (< 10 000).
/// What: sorts the input, computes the six statistics, returns them
/// as a tuple of `Duration` values. Panics if `samples` is empty
/// (a programmer error — we never call this without samples).
/// Test: used by every test that prints a latency table.
fn latency_stats(mut samples: Vec<Duration>) -> LatencyStats {
    assert!(!samples.is_empty(), "latency_stats: empty sample vector");
    samples.sort_unstable();
    let n = samples.len();
    let pct = |p: f64| -> Duration {
        // Round so p99 of 100 samples picks index 98, p99 of 1000 picks 989.
        let idx = ((p * n as f64).ceil() as usize)
            .saturating_sub(1)
            .min(n - 1);
        samples[idx]
    };
    let sum: Duration = samples.iter().sum();
    let mean = sum / n as u32;
    LatencyStats {
        n,
        min: samples[0],
        mean,
        p50: pct(0.50),
        p95: pct(0.95),
        p99: pct(0.99),
        max: samples[n - 1],
    }
}

/// Six-number latency summary returned by [`latency_stats`].
#[derive(Debug, Clone, Copy)]
struct LatencyStats {
    n: usize,
    min: Duration,
    mean: Duration,
    p50: Duration,
    p95: Duration,
    p99: Duration,
    max: Duration,
}

impl std::fmt::Display for LatencyStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "n={:>5}  min={:>7?}  mean={:>7?}  p50={:>7?}  p95={:>7?}  p99={:>7?}  max={:>7?}",
            self.n, self.min, self.mean, self.p50, self.p95, self.p99, self.max
        )
    }
}

// ---------------------------------------------------------------------------
// Test 1 — HTTP concurrent reads
// ---------------------------------------------------------------------------

/// 50 concurrent reader tasks × 20 requests each = 1 000 read ops total.
///
/// Why: validates the HTTP server's ability to fan out read traffic
/// without dropping responses or letting tail latency explode. Pure
/// reads (memory_recall against a single seeded palace) so we measure
/// the read-path concurrency, not write contention.
/// What: spawns 50 tokio tasks, each alternates `memory_recall` and
/// `GET /health` 20 times. Aggregates per-task and global latency.
/// Asserts: zero failed requests, p99 < 500 ms.
/// Test: this test.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_http_concurrent_reads() {
    let client = http_client();
    let version = assert_daemon_alive(&client).await;
    let palace = provision_palace(&client, "http-reads").await;

    let n_tasks: usize = 50;
    let per_task: usize = 20;
    let mut tasks = Vec::new();
    let started = Instant::now();
    for i in 0..n_tasks {
        let client = client.clone();
        let palace = palace.clone();
        tasks.push(tokio::spawn(async move {
            let mut latencies: Vec<Duration> = Vec::with_capacity(per_task);
            let mut errors: usize = 0;
            for j in 0..per_task {
                let result = if j.is_multiple_of(2) {
                    // GET /health
                    let url = format!("{HTTP_BASE}/health");
                    let t = Instant::now();
                    match client.get(&url).send().await {
                        Ok(r) if r.status().is_success() => {
                            let _ = r.bytes().await;
                            Ok(t.elapsed())
                        }
                        Ok(r) => Err(format!("status {}", r.status())),
                        Err(e) => Err(format!("send: {e}")),
                    }
                } else {
                    // memory_recall
                    let req = json!({
                        "jsonrpc": "2.0",
                        "id": i * 100 + j,
                        "method": "memory_recall",
                        "params": {"palace": palace, "query": "seed entry", "top_k": 5}
                    });
                    match http_rpc(&client, req).await {
                        Ok((body, d)) => {
                            if body.get("error").map(|e| !e.is_null()).unwrap_or(false) {
                                Err(format!("rpc error: {}", body["error"]))
                            } else {
                                Ok(d)
                            }
                        }
                        Err(e) => Err(e),
                    }
                };
                match result {
                    Ok(d) => latencies.push(d),
                    Err(_) => errors += 1,
                }
            }
            (latencies, errors)
        }));
    }

    let mut all_latencies: Vec<Duration> = Vec::with_capacity(n_tasks * per_task);
    let mut total_errors = 0usize;
    for j in tasks {
        let (lats, errs) = j.await.expect("task join");
        all_latencies.extend(lats);
        total_errors += errs;
    }
    let total_elapsed = started.elapsed();
    let ops = (n_tasks * per_task) as f64;
    let throughput = ops / total_elapsed.as_secs_f64();
    let stats = latency_stats(all_latencies);

    println!();
    println!("=== test_http_concurrent_reads (daemon v{version}) ===");
    println!("  tasks={n_tasks}  per_task={per_task}  total_ops={ops:.0}  errors={total_errors}");
    println!("  wall={total_elapsed:?}  throughput={throughput:.1} req/s");
    println!("  latency: {stats}");

    // Liveness assertions: the daemon must answer every request and
    // never take longer than the HTTP timeout. Specific latency
    // numbers are reported above for regression tracking; we don't
    // wedge a hard threshold here because the daemon's recall path
    // shares an embedder mutex that serialises concurrent queries —
    // p99 latency naturally grows with task fan-out and the
    // threshold belongs in the regression doc, not the test.
    assert_eq!(total_errors, 0, "expected 0 errors, got {total_errors}");
    assert!(
        stats.max < HTTP_TIMEOUT,
        "max latency {:?} exceeded HTTP timeout {HTTP_TIMEOUT:?}",
        stats.max
    );
}

// ---------------------------------------------------------------------------
// Test 2 — HTTP concurrent mixed reads + writes
// ---------------------------------------------------------------------------

/// 20 writer tasks × 10 writes + 20 reader tasks × 10 reads, run
/// concurrently against a fresh palace.
///
/// Why: write contention is the most likely source of latency
/// spikes — redb's exclusive write lock serialises drawer commits.
/// This test confirms that reads can still flow while writers compete
/// for the lock, and that the daemon doesn't error out under that
/// pressure.
/// What: spawns 40 tasks (20W + 20R) in parallel. Writers call
/// `memory_remember` with unique text per request; readers call
/// `memory_recall`. Aggregates per-class throughput and error counts.
/// Asserts: all writes succeed, all reads succeed.
/// Test: this test.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_http_concurrent_rw() {
    let client = http_client();
    let version = assert_daemon_alive(&client).await;
    let palace = provision_palace(&client, "http-rw").await;

    let n_writers: usize = 20;
    let n_readers: usize = 20;
    let per_task: usize = 10;
    let started = Instant::now();
    type TaskResult = (String, Vec<Duration>, usize, Vec<String>);
    let mut tasks: Vec<tokio::task::JoinHandle<TaskResult>> = Vec::new();

    // Writers.
    for i in 0..n_writers {
        let client = client.clone();
        let palace = palace.clone();
        tasks.push(tokio::spawn(async move {
            let mut latencies = Vec::with_capacity(per_task);
            let mut errors = 0usize;
            let mut sample_errors: Vec<String> = Vec::new();
            for j in 0..per_task {
                let unique = uuid::Uuid::new_v4();
                let req = json!({
                    "jsonrpc": "2.0",
                    "id": 10_000 + i * 100 + j,
                    "method": "memory_remember",
                    "params": {
                        "palace": palace,
                        "text": format!("Concurrent writer {i} request {j} with unique nonce {unique}\
                                         long enough to satisfy the min-token gate and exercise \
                                         the BM25 + vector embedding pipeline end-to-end."),
                        "force": true,
                    }
                });
                match http_rpc(&client, req).await {
                    Ok((body, d)) => {
                        if body.get("error").map(|e| !e.is_null()).unwrap_or(false) {
                            errors += 1;
                            if sample_errors.len() < 2 {
                                sample_errors.push(format!("{}", body["error"]));
                            }
                        } else {
                            latencies.push(d);
                        }
                    }
                    Err(e) => {
                        errors += 1;
                        if sample_errors.len() < 2 {
                            sample_errors.push(format!("transport: {e}"));
                        }
                    }
                }
            }
            ("write".to_string(), latencies, errors, sample_errors)
        }));
    }

    // Readers.
    for i in 0..n_readers {
        let client = client.clone();
        let palace = palace.clone();
        tasks.push(tokio::spawn(async move {
            let mut latencies = Vec::with_capacity(per_task);
            let mut errors = 0usize;
            let mut sample_errors: Vec<String> = Vec::new();
            for j in 0..per_task {
                let req = json!({
                    "jsonrpc": "2.0",
                    "id": 20_000 + i * 100 + j,
                    "method": "memory_recall",
                    "params": {"palace": palace, "query": "concurrent writer request", "top_k": 5}
                });
                match http_rpc(&client, req).await {
                    Ok((body, d)) => {
                        if body.get("error").map(|e| !e.is_null()).unwrap_or(false) {
                            errors += 1;
                            if sample_errors.len() < 2 {
                                sample_errors.push(format!("{}", body["error"]));
                            }
                        } else {
                            latencies.push(d);
                        }
                    }
                    Err(e) => {
                        errors += 1;
                        if sample_errors.len() < 2 {
                            sample_errors.push(format!("transport: {e}"));
                        }
                    }
                }
            }
            ("read".to_string(), latencies, errors, sample_errors)
        }));
    }

    let mut write_latencies = Vec::new();
    let mut read_latencies = Vec::new();
    let mut write_errors = 0usize;
    let mut read_errors = 0usize;
    let mut write_samples: Vec<String> = Vec::new();
    let mut read_samples: Vec<String> = Vec::new();
    for j in tasks {
        let (kind, lats, errs, samples) = j.await.expect("task join");
        if kind == "write" {
            write_latencies.extend(lats);
            write_errors += errs;
            for s in samples {
                if write_samples.len() < 3 {
                    write_samples.push(s);
                }
            }
        } else {
            read_latencies.extend(lats);
            read_errors += errs;
            for s in samples {
                if read_samples.len() < 3 {
                    read_samples.push(s);
                }
            }
        }
    }
    let total_elapsed = started.elapsed();
    let total_writes = n_writers * per_task;
    let total_reads = n_readers * per_task;
    let total_ops = total_writes + total_reads;
    let throughput = total_ops as f64 / total_elapsed.as_secs_f64();
    let write_success_rate = (write_latencies.len() as f64) / (total_writes as f64) * 100.0;
    let read_success_rate = (read_latencies.len() as f64) / (total_reads as f64) * 100.0;

    println!();
    println!("=== test_http_concurrent_rw (daemon v{version}) ===");
    println!(
        "  writers={n_writers}×{per_task}={total_writes}  \
         readers={n_readers}×{per_task}={total_reads}  total={total_ops}"
    );
    println!("  wall={total_elapsed:?}  throughput={throughput:.1} ops/s");
    if !write_latencies.is_empty() {
        println!("  WRITE: {}", latency_stats(write_latencies.clone()));
    }
    println!("  WRITE errors={write_errors}  success_rate={write_success_rate:.2}%");
    for s in &write_samples {
        println!("    write sample error: {s}");
    }
    if !read_latencies.is_empty() {
        println!("  READ : {}", latency_stats(read_latencies.clone()));
    }
    println!("  READ  errors={read_errors}   success_rate={read_success_rate:.2}%");
    for s in &read_samples {
        println!("    read sample error: {s}");
    }

    // #154 fixed: per-palace write mutex + unique tmp names ensure ≥95% success
    assert!(
        read_success_rate >= 95.0,
        "read success_rate {read_success_rate:.2}% below 95% floor"
    );
    assert!(
        write_success_rate >= 95.0,
        "write success rate {:.1}% below 95% — is #154 fix (PR #161) deployed?",
        write_success_rate
    );
}

// ---------------------------------------------------------------------------
// Test 3 — HTTP burst test
// ---------------------------------------------------------------------------

/// Fire 500 requests simultaneously via `join_all` and measure the
/// full latency distribution + error rate.
///
/// Why: bursts simulate the worst-case at session start (Claude Code
/// fans out a flurry of MCP calls during the first prompt). A
/// well-behaved daemon should still return p99 latency under 2 s
/// even when 500 requests arrive within microseconds of each other.
/// What: builds 500 futures (half `memory_remember`, half
/// `memory_recall`), drives them through `join_all`, computes
/// min/mean/p95/p99/max + error rate.
/// Asserts: error rate < 1 %.
/// Test: this test.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_http_burst() {
    let client = http_client();
    let version = assert_daemon_alive(&client).await;
    let palace = provision_palace(&client, "burst").await;

    let n: usize = 500;
    let mut futs = Vec::with_capacity(n);
    for i in 0..n {
        let client = client.clone();
        let palace = palace.clone();
        let req = if i.is_multiple_of(2) {
            json!({
                "jsonrpc": "2.0",
                "id": i,
                "method": "memory_remember",
                "params": {
                    "palace": palace,
                    "text": format!("Burst-test entry {i} with sufficient content length to satisfy \
                                     the minimum-token threshold and produce a real embedding via \
                                     the indexing pipeline."),
                    "force": true,
                }
            })
        } else {
            json!({
                "jsonrpc": "2.0",
                "id": i,
                "method": "memory_recall",
                "params": {"palace": palace, "query": "burst test entry", "top_k": 5}
            })
        };
        futs.push(async move { http_rpc(&client, req).await });
    }

    let started = Instant::now();
    let results = join_all(futs).await;
    let total_elapsed = started.elapsed();

    let mut latencies = Vec::with_capacity(n);
    let mut transport_errors = 0usize;
    let mut rpc_errors = 0usize;
    let mut sample_errors: Vec<String> = Vec::new();
    for r in results {
        match r {
            Ok((body, d)) => {
                if body.get("error").map(|e| !e.is_null()).unwrap_or(false) {
                    rpc_errors += 1;
                    if sample_errors.len() < 3 {
                        sample_errors.push(format!("rpc: {}", body["error"]));
                    }
                } else {
                    latencies.push(d);
                }
            }
            Err(e) => {
                transport_errors += 1;
                if sample_errors.len() < 3 {
                    sample_errors.push(format!("transport: {e}"));
                }
            }
        }
    }
    let errors = transport_errors + rpc_errors;
    let error_rate = (errors as f64) / (n as f64) * 100.0;
    let throughput = n as f64 / total_elapsed.as_secs_f64();
    let n_success = latencies.len();
    let stats = if latencies.is_empty() {
        None
    } else {
        Some(latency_stats(latencies))
    };

    println!();
    println!("=== test_http_burst (daemon v{version}) ===");
    println!("  n={n}  wall={total_elapsed:?}  throughput={throughput:.1} req/s");
    println!("  errors={errors} (transport={transport_errors} rpc={rpc_errors})  error_rate={error_rate:.2}%");
    for s in &sample_errors {
        println!("    sample: {s}");
    }
    if let Some(s) = stats {
        println!("  latency: {s}");
    }
    let success_rate = (n_success as f64) / (n as f64) * 100.0;
    println!("  success_rate={success_rate:.2}%");

    // #154 fixed: per-palace write mutex + unique tmp names ensure ≥95% success
    // even under 500-request simultaneous burst.
    assert!(
        n_success > 0,
        "burst returned zero successful responses (transport={transport_errors} rpc={rpc_errors})"
    );
    assert!(
        success_rate > 95.0,
        "burst success rate {:.1}% below 95% — is #154 fix (PR #161) deployed?",
        success_rate
    );
    let (_, status_after) = probe_health(&client).await.expect("post-burst health");
    println!("  post-burst /health.status = {status_after}");
}

// ---------------------------------------------------------------------------
// Test 4 — UDS JSON-RPC concurrent
// ---------------------------------------------------------------------------

/// 20 concurrent UDS connections × 10 pipelined requests = 200 ops.
///
/// Why: the UDS transport is the production hot path for Claude Code
/// (every MCP tool call goes through the bridge → UDS). This test
/// verifies that 20 simultaneous sockets can each pipeline 10
/// requests without errors, and reports throughput so we have a
/// baseline to compare against future regressions.
/// What: spawns 20 tokio tasks, each opens its own UDS connection
/// and sends 10 NDJSON-framed `memory_recall` requests, reading 10
/// NDJSON-framed responses. Validates JSON-RPC 2.0 shape on every
/// response.
/// Asserts: every response is well-formed (jsonrpc == "2.0", id
/// echoed, no error field). Zero errors.
/// Test: this test.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_uds_concurrent() {
    let client = http_client();
    let version = assert_daemon_alive(&client).await;
    let palace = provision_palace(&client, "uds").await;
    let uds_path = resolve_uds_path();
    println!("UDS path: {}", uds_path.display());

    let n_conns: usize = 20;
    let per_conn: usize = 10;
    let started = Instant::now();
    let mut tasks: Vec<tokio::task::JoinHandle<(Vec<Duration>, usize)>> = Vec::new();
    for i in 0..n_conns {
        let path = uds_path.clone();
        let palace = palace.clone();
        tasks.push(tokio::spawn(async move {
            let stream = match UnixStream::connect(&path).await {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("connect {} failed: {e}", path.display());
                    return (Vec::new(), per_conn);
                }
            };
            let (read_half, mut write_half) = stream.into_split();
            let mut reader = BufReader::new(read_half);
            let mut latencies = Vec::with_capacity(per_conn);
            let mut errors = 0usize;
            for j in 0..per_conn {
                let req = json!({
                    "jsonrpc": "2.0",
                    "id": i * 1_000 + j,
                    "method": "memory_recall",
                    "params": {"palace": palace, "query": "seed entry", "top_k": 5}
                });
                let line = match serde_json::to_string(&req) {
                    Ok(s) => s + "\n",
                    Err(_) => {
                        errors += 1;
                        continue;
                    }
                };
                let t = Instant::now();
                if write_half.write_all(line.as_bytes()).await.is_err() {
                    errors += 1;
                    continue;
                }
                if write_half.flush().await.is_err() {
                    errors += 1;
                    continue;
                }
                let mut response_line = String::new();
                if reader.read_line(&mut response_line).await.is_err() {
                    errors += 1;
                    continue;
                }
                let elapsed = t.elapsed();
                // Validate JSON-RPC 2.0 envelope.
                let body: Value = match serde_json::from_str(&response_line) {
                    Ok(v) => v,
                    Err(_) => {
                        errors += 1;
                        continue;
                    }
                };
                if body["jsonrpc"] != "2.0" {
                    errors += 1;
                    continue;
                }
                if body["id"] != json!(i * 1_000 + j) {
                    errors += 1;
                    continue;
                }
                if body.get("error").map(|e| !e.is_null()).unwrap_or(false) {
                    errors += 1;
                    continue;
                }
                latencies.push(elapsed);
            }
            (latencies, errors)
        }));
    }

    let mut all_latencies = Vec::with_capacity(n_conns * per_conn);
    let mut total_errors = 0usize;
    for j in tasks {
        let (lats, errs) = j.await.expect("task join");
        all_latencies.extend(lats);
        total_errors += errs;
    }
    let total_elapsed = started.elapsed();
    let ops = (n_conns * per_conn) as f64;
    let throughput = ops / total_elapsed.as_secs_f64();
    let stats = latency_stats(all_latencies);

    println!();
    println!("=== test_uds_concurrent (daemon v{version}) ===");
    println!("  conns={n_conns}  per_conn={per_conn}  total_ops={ops:.0}  errors={total_errors}");
    println!("  wall={total_elapsed:?}  throughput={throughput:.1} req/s");
    println!("  latency: {stats}");

    assert_eq!(total_errors, 0, "expected 0 errors, got {total_errors}");
}

// ---------------------------------------------------------------------------
// Test 5 — MCP bridge concurrent
// ---------------------------------------------------------------------------

/// 10 concurrent `trusty-memory-mcp-bridge` processes, each doing 5
/// initialize+ping exchanges over stdio.
///
/// Why: Claude Code spawns the bridge per-session; this test exercises
/// the bridge's per-process resource path (UDS connect, stdin/stdout
/// copy_bidirectional) under concurrent load. Forks 10 child
/// processes and pipes JSON-RPC through each.
/// What: spawns 10 `tokio::process::Command` children with piped
/// stdin/stdout, writes 5 initialize+ping pairs (10 requests total per
/// bridge), reads 10 responses, kills the bridge by closing stdin.
/// Asserts: every exchange completes (correct response IDs come back),
/// every child exits cleanly within 30 s.
/// Test: this test.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_bridge_concurrent() {
    let client = http_client();
    let version = assert_daemon_alive(&client).await;

    let n_bridges: usize = 10;
    let per_bridge: usize = 5;
    let started = Instant::now();
    let mut tasks: Vec<tokio::task::JoinHandle<Result<Vec<Duration>, String>>> = Vec::new();
    for i in 0..n_bridges {
        tasks.push(tokio::spawn(async move {
            let mut child = match Command::new(BRIDGE_BIN)
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()
            {
                Ok(c) => c,
                Err(e) => return Err(format!("spawn {BRIDGE_BIN}: {e}")),
            };

            let mut stdin = child
                .stdin
                .take()
                .ok_or_else(|| "stdin not captured".to_string())?;
            let stdout = child
                .stdout
                .take()
                .ok_or_else(|| "stdout not captured".to_string())?;
            let mut reader = BufReader::new(stdout);

            let mut latencies = Vec::with_capacity(per_bridge * 2);
            // Each "exchange" is one initialize + one ping. We use
            // tools/list (a real protocol method) instead of
            // initialize because the bridge is a byte pipe and
            // doesn't care; tools/list returns a non-trivial payload
            // so the framing dance is exercised end-to-end.
            for j in 0..per_bridge {
                let id_a = i * 1_000 + j * 2;
                let id_b = i * 1_000 + j * 2 + 1;
                let req_a = serde_json::to_string(&json!({
                    "jsonrpc": "2.0",
                    "id": id_a,
                    "method": "tools/list",
                    "params": {}
                }))
                .map_err(|e| format!("serialise: {e}"))?
                    + "\n";
                let req_b = serde_json::to_string(&json!({
                    "jsonrpc": "2.0",
                    "id": id_b,
                    "method": "ping",
                    "params": {}
                }))
                .map_err(|e| format!("serialise: {e}"))?
                    + "\n";

                let t = Instant::now();
                stdin
                    .write_all(req_a.as_bytes())
                    .await
                    .map_err(|e| format!("write a: {e}"))?;
                stdin
                    .write_all(req_b.as_bytes())
                    .await
                    .map_err(|e| format!("write b: {e}"))?;
                stdin.flush().await.map_err(|e| format!("flush: {e}"))?;

                // Read response A.
                let mut line_a = String::new();
                reader
                    .read_line(&mut line_a)
                    .await
                    .map_err(|e| format!("read a: {e}"))?;
                if line_a.is_empty() {
                    return Err("eof before response a".to_string());
                }
                let resp_a: Value = serde_json::from_str(line_a.trim_end())
                    .map_err(|e| format!("parse a: {e} (line: {line_a:?})"))?;
                if resp_a["id"] != json!(id_a) {
                    return Err(format!(
                        "expected id {id_a} got {} (full: {resp_a})",
                        resp_a["id"]
                    ));
                }
                if resp_a.get("error").map(|e| !e.is_null()).unwrap_or(false) {
                    return Err(format!("rpc error a: {}", resp_a["error"]));
                }

                // Read response B.
                let mut line_b = String::new();
                reader
                    .read_line(&mut line_b)
                    .await
                    .map_err(|e| format!("read b: {e}"))?;
                if line_b.is_empty() {
                    return Err("eof before response b".to_string());
                }
                let resp_b: Value = serde_json::from_str(line_b.trim_end())
                    .map_err(|e| format!("parse b: {e} (line: {line_b:?})"))?;
                if resp_b["id"] != json!(id_b) {
                    return Err(format!("expected id {id_b} got {}", resp_b["id"]));
                }
                if resp_b.get("error").map(|e| !e.is_null()).unwrap_or(false) {
                    return Err(format!("rpc error b: {}", resp_b["error"]));
                }
                latencies.push(t.elapsed());
            }

            // Close stdin to signal the bridge to exit.
            drop(stdin);
            // Wait for the bridge to terminate, but don't wait forever.
            let exit_status = tokio::time::timeout(Duration::from_secs(10), child.wait())
                .await
                .map_err(|_| "timeout waiting for bridge exit".to_string())?
                .map_err(|e| format!("child.wait: {e}"))?;
            if !exit_status.success() {
                return Err(format!("bridge exited with {exit_status:?}"));
            }
            Ok(latencies)
        }));
    }

    let mut all_latencies: Vec<Duration> = Vec::new();
    let mut failures: Vec<String> = Vec::new();
    for (i, t) in tasks.into_iter().enumerate() {
        match t.await.expect("task join") {
            Ok(lats) => all_latencies.extend(lats),
            Err(e) => failures.push(format!("bridge {i}: {e}")),
        }
    }
    let total_elapsed = started.elapsed();
    let exchanges = (n_bridges * per_bridge) as f64;
    let throughput = exchanges / total_elapsed.as_secs_f64();

    println!();
    println!("=== test_bridge_concurrent (daemon v{version}) ===");
    println!(
        "  bridges={n_bridges}  per_bridge={per_bridge}  total_exchanges={exchanges:.0}  failures={}",
        failures.len()
    );
    println!("  wall={total_elapsed:?}  throughput={throughput:.1} exchanges/s");
    if !all_latencies.is_empty() {
        println!(
            "  per-exchange (init+ping): {}",
            latency_stats(all_latencies)
        );
    }
    for f in &failures {
        println!("  FAIL: {f}");
    }
    assert!(failures.is_empty(), "{} bridge(s) failed", failures.len());
}

// ---------------------------------------------------------------------------
// Test 6 — Sustained-load stability
// ---------------------------------------------------------------------------

/// 10 concurrent clients firing requests for 10 seconds continuously.
///
/// Why: bursts catch contention spikes but not slow leaks. A
/// sustained run exposes RSS growth, file-descriptor leaks, and
/// thread-pool starvation that only manifest after many thousands
/// of ops. Asserting the daemon is still healthy at the end is the
/// minimum-viable liveness check.
/// What: 10 tokio tasks each loop for 10 s, alternating
/// `memory_remember` and `memory_recall`. After the loop, every task
/// returns its op count + error count. The test then GETs `/health`
/// and asserts the daemon is still `ok`. Reports RSS delta from the
/// `/health` payload (rss_mb field) for visibility.
/// Asserts: daemon still healthy after 10 s of pressure; error rate
/// < 1 %.
/// Test: this test.
#[ignore]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_http_sustained_load() {
    let client = http_client();
    let version = assert_daemon_alive(&client).await;
    let palace = provision_palace(&client, "sustained").await;

    // Capture initial RSS for delta reporting.
    let initial_health: Value = client
        .get(format!("{HTTP_BASE}/health"))
        .send()
        .await
        .expect("initial /health")
        .json()
        .await
        .expect("parse /health");
    let initial_rss = initial_health["rss_mb"].as_f64().unwrap_or(0.0);

    let duration = Duration::from_secs(10);
    let n_clients: usize = 10;
    let deadline = Instant::now() + duration;
    let total_ops = Arc::new(AtomicU64::new(0));
    let total_errors = Arc::new(AtomicU64::new(0));

    let mut tasks: Vec<tokio::task::JoinHandle<()>> = Vec::new();
    for i in 0..n_clients {
        let client = client.clone();
        let palace = palace.clone();
        let total_ops = Arc::clone(&total_ops);
        let total_errors = Arc::clone(&total_errors);
        tasks.push(tokio::spawn(async move {
            let mut j: u64 = 0;
            while Instant::now() < deadline {
                let req = if j.is_multiple_of(2) {
                    json!({
                        "jsonrpc": "2.0",
                        "id": i as u64 * 1_000_000 + j,
                        "method": "memory_remember",
                        "params": {
                            "palace": palace,
                            "text": format!("Sustained-load client {i} op {j} — long enough content to clear \
                                             the min-token gate and exercise the embedding + KG pipelines."),
                            "force": true,
                        }
                    })
                } else {
                    json!({
                        "jsonrpc": "2.0",
                        "id": i as u64 * 1_000_000 + j,
                        "method": "memory_recall",
                        "params": {"palace": palace, "query": "sustained load client", "top_k": 5}
                    })
                };
                match http_rpc(&client, req).await {
                    Ok((body, _)) => {
                        if body.get("error").map(|e| !e.is_null()).unwrap_or(false) {
                            total_errors.fetch_add(1, Ordering::Relaxed);
                        } else {
                            total_ops.fetch_add(1, Ordering::Relaxed);
                        }
                    }
                    Err(_) => {
                        total_errors.fetch_add(1, Ordering::Relaxed);
                    }
                }
                j += 1;
            }
        }));
    }

    let started = Instant::now();
    for t in tasks {
        let _ = t.await;
    }
    let wall = started.elapsed();
    let ops = total_ops.load(Ordering::Relaxed);
    let errs = total_errors.load(Ordering::Relaxed);
    let throughput = ops as f64 / wall.as_secs_f64();
    let error_rate = if ops + errs == 0 {
        0.0
    } else {
        (errs as f64) / ((ops + errs) as f64) * 100.0
    };

    // Final liveness check. We do NOT assert /health.status == "ok"
    // because the daemon's self-probe is racy under load (the probe
    // writes a drawer and recalls it; if the embedder/HNSW reindex
    // hasn't caught up by the deadline, the status flips to
    // "degraded" even though every external request is still being
    // answered correctly). Instead we wait briefly for the indexer
    // to drain, then assert the daemon is still *reachable* and
    // serving valid responses.
    tokio::time::sleep(Duration::from_secs(2)).await;
    let (final_rss, final_status) = probe_health(&client).await.expect("final /health");
    // One concrete tool call confirms the daemon is still serving
    // RPC traffic — independent of /health's self-probe verdict.
    let liveness_req = json!({
        "jsonrpc": "2.0",
        "id": 999_999,
        "method": "palace_list",
        "params": {}
    });
    let (live_body, _) = http_rpc(&client, liveness_req)
        .await
        .expect("post-load liveness palace_list");
    let live_ok = live_body.get("error").is_none_or(|e| e.is_null())
        && live_body["result"]["palaces"].is_array();

    println!();
    println!("=== test_http_sustained_load (daemon v{version}) ===");
    println!("  clients={n_clients}  wall={wall:?}  ops={ops}  errors={errs}");
    println!("  throughput={throughput:.1} ops/s  error_rate={error_rate:.2}%");
    println!(
        "  RSS: start={initial_rss:.0} MB  end={final_rss:.0} MB  delta={:+.0} MB",
        final_rss - initial_rss
    );
    println!("  final /health.status = {final_status}");
    println!("  post-load palace_list ok = {live_ok}");

    assert!(
        live_ok,
        "post-load liveness call (palace_list) must succeed; body = {live_body:?}"
    );
    // #154 fixed: per-palace write mutex + unique tmp names ensure ≥95% success
    assert!(
        error_rate < 5.0,
        "sustained error rate {:.1}% above 5% — is #154 fix (PR #161) deployed?",
        error_rate
    );
}