trusty-memory 0.26.2

MCP server (stdio + Unix socket) 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
//! Concurrent performance test suite for the trusty-memory daemon.
//!
//! Why: the socket design hinges on the daemon handling concurrent traffic
//! without contention, dropped responses, or runaway latency. redb holds an
//! exclusive write lock and the recall path shares an embedder mutex, so both
//! are places where fan-out can turn into a stall that no single-threaded test
//! would find.
//!
//! What (#6286): four `#[ignore]`-tagged tests, each measuring a different
//! facet of the concurrency envelope:
//!   - concurrent reads (`test_concurrent_reads`)
//!   - concurrent mixed reads + writes (`test_concurrent_rw`)
//!   - a burst (`test_burst`)
//!   - sustained-load stability (`test_sustained_load`)
//!
//! **They stand up their own daemon on a temp socket.** They used to POST
//! `/rpc` and `GET /health` at `http://127.0.0.1:7070` and panic when nothing
//! answered — so after ADR-0032 retired that listener they could only fail, and
//! `--include-ignored` was red by construction. Driving a daemon this file
//! starts makes them deterministic, keeps them off the operator's real palaces,
//! and means the numbers describe one machine's daemon rather than whatever was
//! already running on it.
//!
//! `#[ignore]` stays: these run for tens of seconds and report a latency
//! distribution, which is a measurement rather than a gate.
//!
//! Test: run them with
//!   `cargo test -p trusty-memory --test concurrent_perf -- --include-ignored --nocapture`.

use futures::future::join_all;
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::oneshot;
use trusty_common::uds::send_framed_request_capped;
use trusty_common::uds::server::RpcResponse;
use trusty_memory::AppState;

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

/// Soft per-request timeout.
///
/// Why: a daemon under heavy contention may still complete a request, just
/// slowly. 30s is generous enough that a stalled-but-recoverable request still
/// counts as a success; past that, it is a real failure.
/// Test: every call below, and the `stats.max < CALL_TIMEOUT` assertion each
/// test ends with.
const CALL_TIMEOUT: Duration = Duration::from_secs(30);

/// Per-request timeout for the 500-way simultaneous burst.
///
/// Why it is not [`CALL_TIMEOUT`]: every write in the burst serialises behind
/// redb's exclusive write lock, so the tail is the queue, not a stall. Measured
/// against a daemon this file starts, the burst's p50 is ~40ms and its max sits
/// exactly on whatever the budget is — 49 of 500 requests hit a 30s wall and
/// were counted as DROPS by an assertion that is about drops, which is the
/// timeout deciding the result instead of measuring it.
///
/// The burst's question is whether the daemon answers all 500 without losing
/// any. 120s is the room that question needs; the latency distribution the test
/// prints is where the queue depth is actually reported.
const BURST_CALL_TIMEOUT: Duration = Duration::from_secs(120);

// ---------------------------------------------------------------------------
// The daemon under test
// ---------------------------------------------------------------------------

/// A daemon this suite started, on a socket only this suite dials.
///
/// Dropping it stops the accept loop. The data root and the socket directory
/// are both leaked tempdirs — the process reaps them, and neither is anywhere
/// near the operator's real data.
struct PerfDaemon {
    socket: PathBuf,
    stop: Option<oneshot::Sender<()>>,
}

impl Drop for PerfDaemon {
    fn drop(&mut self) {
        if let Some(stop) = self.stop.take() {
            let _ = stop.send(());
        }
    }
}

/// Start a daemon and wait until its socket answers.
///
/// Why the mock embedder: the real ONNX model is a HuggingFace download, and a
/// perf test that spent its first minute fetching one would be measuring the
/// network. `seed_shared_embedder_with_mock` is idempotent and process-wide.
async fn start_daemon() -> PerfDaemon {
    trusty_common::memory_core::retrieval::seed_shared_embedder_with_mock();

    let data = tempfile::tempdir().expect("tempdir for the data root");
    let root = data.path().to_path_buf();
    std::mem::forget(data);
    // #88: bypass the project-slug gate — these palaces have no project root.
    // SAFETY: every test in this process wants the same idempotent "1".
    unsafe {
        std::env::set_var("TRUSTY_SKIP_PALACE_ENFORCEMENT", "1");
    }
    let state = AppState::new(root);
    // #911: flip past the warming preflight so handlers run.
    state.set_ready();

    let sockets = tempfile::tempdir().expect("tempdir for the socket");
    let socket = sockets.path().join("trusty-memory.sock");
    std::mem::forget(sockets);

    let (stop, shutdown) = oneshot::channel::<()>();
    let serve_socket = socket.clone();
    tokio::spawn(async move {
        let _ = trusty_memory::transport::uds::serve_with_shutdown(state, &serve_socket, async {
            let _ = shutdown.await;
        })
        .await;
    });

    for _ in 0..400 {
        if trusty_common::uds::socket_is_serving(&socket, Duration::from_millis(50)).await {
            break;
        }
        tokio::time::sleep(Duration::from_millis(25)).await;
    }

    PerfDaemon {
        socket,
        stop: Some(stop),
    }
}

/// A cheap-to-clone handle the tasks dial with.
///
/// Why it is not a pooled client: each call dials, exchanges one frame pair,
/// and closes — there is no connection to pool, so cloning is a `PathBuf`
/// clone and the fan-out is real rather than multiplexed over one socket.
#[derive(Clone)]
struct Client {
    socket: PathBuf,
    timeout: Duration,
}

impl Client {
    fn new(socket: &Path) -> Self {
        Self {
            socket: socket.to_path_buf(),
            timeout: CALL_TIMEOUT,
        }
    }

    /// The same client with a different per-call budget.
    ///
    /// Only the burst uses it — see [`BURST_CALL_TIMEOUT`].
    fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// One JSON-RPC call, returning the envelope and the round-trip latency.
    async fn rpc(&self, req: Value) -> Result<(Value, Duration), String> {
        let started = Instant::now();
        let response: RpcResponse = send_framed_request_capped(
            &self.socket,
            &req,
            self.timeout,
            trusty_memory::transport::uds::MAX_FRAME_BYTES,
        )
        .await
        .map_err(|e| format!("call: {e}"))?;
        let elapsed = started.elapsed();
        // Re-assembled into the envelope shape the assertions read, so a test
        // still branches on `body["error"]` the way it did over HTTP.
        let mut body = json!({ "jsonrpc": "2.0", "id": response.id });
        if let Some(result) = response.result {
            body["result"] = result;
        }
        if let Some(error) = response.error {
            body["error"] = json!({ "code": error.code, "message": error.message });
        }
        Ok((body, elapsed))
    }

    /// `memory.health`, returning `(rss_mb, status)`.
    ///
    /// Why the status is returned rather than asserted: the daemon's health
    /// probe does a real store-and-recall round trip, which is intentionally
    /// racy under load — 500 concurrent requests can push the embedder behind
    /// its deadline and flip the status to `degraded` while every external
    /// request is still answered correctly. The tests report it; they do not
    /// gate on it.
    async fn health(&self) -> Result<(f64, String), String> {
        self.health_full()
            .await
            .map(|(rss, status, _)| (rss, status))
    }

    /// [`Self::health`] plus the daemon version.
    ///
    /// `params: {}` is not optional: `memory.health` takes a `HealthQuery`, and
    /// a struct refuses `null` — omitting params is an invalid-params refusal
    /// rather than a defaulted call. Every production caller sends `{}` for the
    /// same reason.
    async fn health_full(&self) -> Result<(f64, String, String), String> {
        let (body, _) = self
            .rpc(json!({
                "jsonrpc": "2.0",
                "id": 1,
                "method": "memory.health",
                "params": {},
            }))
            .await?;
        if body.get("error").is_some_and(|e| !e.is_null()) {
            return Err(format!("health error: {}", body["error"]));
        }
        let result = &body["result"];
        Ok((
            result["rss_mb"].as_f64().unwrap_or(0.0),
            result["status"].as_str().unwrap_or("?").to_string(),
            result["version"].as_str().unwrap_or("?").to_string(),
        ))
    }

    /// One `memory.health` call, timed — the read half of the read/write mix.
    async fn health_timed(&self) -> Result<Duration, String> {
        let started = Instant::now();
        self.health().await?;
        Ok(started.elapsed())
    }
}

/// Start a daemon and hand back a client for it, plus its version.
///
/// The daemon must outlive the client, so the caller binds both.
async fn perf_daemon() -> (PerfDaemon, Client, String) {
    let daemon = start_daemon().await;
    let client = Client::new(&daemon.socket);
    let (_rss, _status, version) = client
        .health_full()
        .await
        .expect("the daemon this test started must answer memory.health");
    (daemon, client, version)
}

/// Provision an isolated test palace and seed it with one memory entry.
///
/// Why: the seed guarantees `memory_recall` returns something, so recall
/// throughput is not dominated by an empty-index fast path. The palace is
/// UUID-suffixed even though the daemon is already private — two tests sharing
/// a process would otherwise share a name.
/// What: `palace_create` then `memory_remember` with `force: true` (the
/// min-token gate would otherwise reject the fixture).
async fn provision_palace(client: &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, "force": true}
    });
    let (resp, _) = client.rpc(create).await.expect("palace_create");
    assert!(
        resp.get("error").is_none_or(|e| e.is_null()),
        "palace_create failed: {resp:?}"
    );

    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, _) = client.rpc(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_concurrent_reads() {
    let (_daemon, client, version) = perf_daemon().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) {
                    client.health_timed().await
                } 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 client.rpc(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_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 < CALL_TIMEOUT,
        "max latency {:?} exceeded HTTP timeout {CALL_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_concurrent_rw() {
    let (_daemon, client, version) = perf_daemon().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 client.rpc(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 client.rpc(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_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_burst() {
    let (_daemon, client, version) = perf_daemon().await;
    let palace = provision_palace(&client, "burst").await;
    // See `BURST_CALL_TIMEOUT`: the ordinary budget turns queue depth into
    // counted drops, and drops are what this test's assertion is about.
    let client = client.with_timeout(BURST_CALL_TIMEOUT);

    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 { client.rpc(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_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 {success_rate:.1}% below 95% — is #154 fix (PR #161) deployed?"
    );
    let (_, status_after) = client.health().await.expect("post-burst health");
    println!("  post-burst /health.status = {status_after}");
}

// ---------------------------------------------------------------------------
// Test 4 — 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_sustained_load() {
    let (_daemon, client, version) = perf_daemon().await;
    let palace = provision_palace(&client, "sustained").await;

    // Capture initial RSS for delta reporting.
    let (initial_rss, _) = client.health().await.expect("initial memory.health");

    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 client.rpc(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) = client.health().await.expect("final memory.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, _) = client
        .rpc(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_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
    );
}