trusty-memory 0.15.5

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
//! Tests for recall cross-palace fan-out, palace filter, triple-ID helpers,
//! and drawer utility functions (preview, entry JSON shape).

use super::super::kg_routes::{decode_triple_id, encode_triple_id};
use super::super::recall_routes::recall_entry_json;
use super::super::router;
use super::test_state;
use crate::service::{drawer_content_preview, DRAWER_PREVIEW_MAX_CHARS};
use axum::body::{to_bytes, Body};
use axum::http::{Request, StatusCode};
use serde_json::{json, Value};
use tower::util::ServiceExt;
use trusty_common::memory_core::palace::{Palace, PalaceId};
use trusty_common::memory_core::retrieval::RecallResult;
use uuid::Uuid;

/// Why: The base64url triple-ID round-trip is the core invariant for
/// `DELETE /kg/triples/<id>` — if encode/decode aren't inverses, the
/// handler will always 404 on valid IDs.
/// What: Encodes a (subject, predicate) pair, decodes the result, and
/// asserts exact equality with the originals. Also tests URL-safety.
/// Test: This test.
#[test]
fn decode_triple_id_round_trips() {
    let cases = [
        ("drawer:some-uuid", "has_tag"),
        ("entity:alice", "works_at"),
        ("entity:project/foo", "depends_on"),
        // edge: empty predicate
        ("subject", ""),
        // edge: subject with slashes + predicate with colons
        ("path/to/node", "rel:type:sub"),
    ];
    for (subject, predicate) in cases {
        let encoded = encode_triple_id(subject, predicate)
            .unwrap_or_else(|e| panic!("encode_triple_id failed: {e}"));
        // Must be URL-safe: no +, /, or = characters.
        assert!(
            !encoded.contains('+') && !encoded.contains('/') && !encoded.contains('='),
            "encoded triple id {encoded:?} is not URL-safe"
        );
        let (s, p) = decode_triple_id(&encoded)
            .unwrap_or_else(|| panic!("decode_triple_id failed for {encoded:?}"));
        assert_eq!(s, subject, "subject mismatch for ({subject}, {predicate})");
        assert_eq!(
            p, predicate,
            "predicate mismatch for ({subject}, {predicate})"
        );
    }
}

/// Why: `decode_triple_id` must return `None` on garbage input (not panic).
/// What: Passes invalid base64 and base64 without a null separator; asserts None.
/// Test: This test.
#[test]
fn decode_triple_id_returns_none_for_invalid_input() {
    assert!(decode_triple_id("not!!valid%%base64").is_none());
    // Valid base64url but no null separator → no split possible.
    use base64::Engine as _;
    let no_sep = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(b"no-separator");
    assert!(decode_triple_id(&no_sep).is_none());
}

/// Issue #1102 — `encode_triple_id` must reject subjects or predicates that
/// contain the null-byte separator (`\0`), not silently produce an ambiguous
/// (corrupt) encoded id.
///
/// Why: The null byte is the separator inside the encoded payload. If either
/// component itself carries a `\0`, the subsequent decode splits at the wrong
/// position and returns incorrect `(subject, predicate)` pairs for the
/// persisted `DELETE` path — this is a data-integrity bug, not just a
/// correctness assertion.
/// What: Calls `encode_triple_id` with a `\0`-containing subject and a
/// `\0`-containing predicate, asserts both return `Err` with a message
/// mentioning the null-byte separator.
/// Test: This test.
#[test]
fn encode_triple_id_rejects_null_byte() {
    let err = encode_triple_id("sub\0ject", "predicate")
        .expect_err("null byte in subject must return Err");
    assert!(
        err.contains("null-byte") || err.contains("\\0"),
        "error message should mention null-byte separator; got {err:?}"
    );

    let err = encode_triple_id("subject", "pred\0icate")
        .expect_err("null byte in predicate must return Err");
    assert!(
        err.contains("null-byte") || err.contains("\\0"),
        "error message should mention null-byte separator; got {err:?}"
    );

    // Clean inputs must still succeed.
    assert!(
        encode_triple_id("clean-subject", "clean-predicate").is_ok(),
        "clean inputs must encode successfully"
    );
}

/// Issue #1102 — `extract_partial_error` pure-function unit tests.
///
/// Why: The helper was added to guard against partial-success envelopes being
/// silently passed through as 200 OK, but the original test never reached that
/// branch. These tests call the pure function directly with synthetic
/// `serde_json::Value`s and pin every branch: (1) non-empty string errors
/// array, (2) non-empty `{"message":"..."}` object array, (3) empty array,
/// (4) no `errors` key.
/// What: Four focused `#[test]` functions below.
/// Test: These tests.
#[test]
fn extract_partial_error_string_array_returns_first_error() {
    use super::super::recall_routes::extract_partial_error;

    // (1) Non-empty errors array of plain strings.
    let v = json!({
        "errors": ["palace alpha timed out", "palace beta unreachable"],
        "results": []
    });
    let msg = extract_partial_error(&v);
    assert!(msg.is_some(), "non-empty string errors must yield Some");
    let msg = msg.unwrap();
    assert!(
        msg.contains("palace alpha timed out"),
        "message must include the first error string; got {msg:?}"
    );
    assert!(
        msg.contains("2 error(s)"),
        "message must include the error count; got {msg:?}"
    );
}

/// Why: Same as above but for the `{"message":"..."}` object shape that some
/// future partial-success envelopes may use.
/// What: Calls `extract_partial_error` with an `errors` array of objects
/// carrying a `"message"` field and asserts the text is extracted correctly.
/// Test: This test.
#[test]
fn extract_partial_error_object_array_extracts_message_field() {
    use super::super::recall_routes::extract_partial_error;

    // (2) Non-empty errors array of {"message": "..."} objects.
    let v = json!({
        "errors": [
            {"message": "KG query failed", "code": 503},
            {"message": "BM25 timeout"}
        ]
    });
    let msg = extract_partial_error(&v);
    assert!(msg.is_some(), "object errors must yield Some");
    let msg = msg.unwrap();
    assert!(
        msg.contains("KG query failed"),
        "message must include the first object's message; got {msg:?}"
    );
    assert!(
        msg.contains("2 error(s)"),
        "message must include the error count; got {msg:?}"
    );
}

/// Why: An empty `errors` array means no failure occurred; the guard must
/// not convert a healthy envelope into an error.
/// What: Calls `extract_partial_error` with `errors: []` and asserts None.
/// Test: This test.
#[test]
fn extract_partial_error_empty_array_returns_none() {
    use super::super::recall_routes::extract_partial_error;

    // (3) Empty errors array → no failure.
    let v = json!({ "errors": [], "results": [{"content": "ok"}] });
    assert!(
        extract_partial_error(&v).is_none(),
        "empty errors array must return None"
    );
}

/// Why: Successful recall returns a JSON array (not an object), so
/// `get("errors")` returns None. The guard must handle this gracefully.
/// What: Calls `extract_partial_error` with a JSON array and with an object
/// that has no `"errors"` key; asserts None in both cases.
/// Test: This test.
#[test]
fn extract_partial_error_no_errors_key_returns_none() {
    use super::super::recall_routes::extract_partial_error;

    // (4a) Plain JSON array (the success shape from recall_all).
    let v = json!([{"content": "hello", "score": 0.9}]);
    assert!(
        extract_partial_error(&v).is_none(),
        "JSON array input must return None"
    );

    // (4b) Object without an errors key.
    let v = json!({ "results": [{"content": "world"}] });
    assert!(
        extract_partial_error(&v).is_none(),
        "object without errors key must return None"
    );
}

// -------------------------------------------------------------------------
// Issue #465 — GET /api/v1/recall?palace= must honour the palace filter
// -------------------------------------------------------------------------

/// Why (issue #465): `GET /api/v1/recall?palace=<id>&q=...` was silently
/// ignoring the `palace=` parameter and always fanning out across all
/// palaces, returning results from the wrong palace. This test proves the
/// route now scopes the recall to the requested palace.
/// What: creates two palaces with distinct drawers, requests recall with
/// `palace=` set to one of them, and asserts the response is a JSON array
/// (the per-palace shape), not the cross-palace object shape.
/// Test: this test.
#[tokio::test]
async fn recall_all_handler_honors_palace_filter() {
    let state = test_state();
    // Pre-create a palace so the handler can open it.
    let palace = Palace {
        id: PalaceId::new("filter-target"),
        name: "filter-target".to_string(),
        description: None,
        created_at: chrono::Utc::now(),
        data_dir: state.data_root.join("filter-target"),
    };
    state
        .registry
        .create_palace(&state.data_root, palace)
        .expect("create_palace");

    let app = router().with_state(state);
    // With palace= set, the handler should delegate to the per-palace path.
    // Even with no drawers, a valid palace returns a JSON array (possibly
    // empty), NOT a 404 or a cross-palace object shape.
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/api/v1/recall?q=anything&palace=filter-target")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(
        resp.status(),
        StatusCode::OK,
        "recall with valid palace= must return 200"
    );
    let bytes = to_bytes(resp.into_body(), 4096).await.unwrap();
    let v: Value = serde_json::from_slice(&bytes).unwrap();
    assert!(
        v.is_array(),
        "recall with palace= must return a JSON array (per-palace shape); got {v}"
    );
}

/// Why (issue #465): when `palace=` refers to a non-existent palace, the
/// handler must return a 404 — not silently fall back to cross-palace recall.
/// What: requests recall with a `palace=` that was never created and asserts
/// the response is 404.
/// Test: this test.
#[tokio::test]
async fn recall_all_handler_palace_filter_missing_palace_returns_404() {
    let state = test_state();
    let app = router().with_state(state);
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/api/v1/recall?q=anything&palace=nonexistent-palace")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(
        resp.status(),
        StatusCode::NOT_FOUND,
        "recall with palace= pointing to missing palace must return 404"
    );
}

/// Why (issue #465): when `palace=` is absent, the endpoint must continue
/// to fan out across all palaces (original cross-palace behaviour).
/// What: with no palace= param and no palaces created, the cross-palace
/// fan-out returns an empty JSON array (no palaces → nothing to search).
/// Test: this test.
#[tokio::test]
async fn recall_all_handler_fans_out_without_palace_param() {
    let state = test_state();
    let app = router().with_state(state);
    let resp = app
        .oneshot(
            Request::builder()
                .uri("/api/v1/recall?q=anything")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(
        resp.status(),
        StatusCode::OK,
        "cross-palace recall with no palace= must return 200"
    );
    let bytes = to_bytes(resp.into_body(), 4096).await.unwrap();
    let v: Value = serde_json::from_slice(&bytes).unwrap();
    // No palaces → empty array.
    assert!(
        v.is_array(),
        "cross-palace recall must return a JSON array; got {v}"
    );
}

// -------------------------------------------------------------------------
// Issue #466 — POST /api/v1/remember must reject short content synchronously
// -------------------------------------------------------------------------

/// Why (issue #466): content that is too short was silently dropped by the
/// background worker while the HTTP response claimed `202 Accepted`.
/// Callers believed the memory was stored when it wasn't — silent data loss.
/// The fix: validate the minimum word count synchronously and return 422
/// before queueing so the caller gets an actionable error immediately.
/// What: POSTs content with fewer than REMEMBER_MIN_WORDS words and asserts
/// the response is 422, not 202.
/// Test: this test.
#[tokio::test]
async fn remember_async_rejects_short_content() {
    let state = test_state();
    let app = router().with_state(state);
    // "hi" is 1 word — well below REMEMBER_MIN_WORDS (4).
    for body in [
        json!({"content": "hi"}),
        json!({"content": "two words"}),
        json!({"content": "three word content"}),
    ] {
        let resp = app
            .clone()
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/api/v1/remember")
                    .header("content-type", "application/json")
                    .body(Body::from(body.to_string()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(
            resp.status(),
            StatusCode::UNPROCESSABLE_ENTITY,
            "short content must return 422; body={body}"
        );
    }
}

/// Why (issue #466): content that meets the minimum word count must still
/// return 202, proving the synchronous gate does not over-reject.
/// What: POSTs exactly REMEMBER_MIN_WORDS words and asserts 202.
/// Test: this test (companion to `remember_async_rejects_short_content`).
#[tokio::test]
async fn remember_async_accepts_content_at_min_words() {
    let state = test_state();
    // Pre-create a palace so the spawned task can find it.
    let palace = Palace {
        id: PalaceId::new("min-words-test"),
        name: "min-words-test".to_string(),
        description: None,
        created_at: chrono::Utc::now(),
        data_dir: state.data_root.join("min-words-test"),
    };
    state
        .registry
        .create_palace(&state.data_root, palace)
        .expect("create_palace");

    let app = router().with_state(state);
    // Exactly 4 words — the minimum.
    let body = json!({
        "content": "four words exactly here",
        "palace": "min-words-test",
    });
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/remember")
                .header("content-type", "application/json")
                .body(Body::from(body.to_string()))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(
        resp.status(),
        StatusCode::ACCEPTED,
        "content at minimum word count must return 202"
    );
    let bytes = to_bytes(resp.into_body(), 512).await.unwrap();
    let v: Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(
        v["status"], "queued",
        "accepted body must carry status=queued"
    );
}

// -------------------------------------------------------------------------
// Drawer utility tests (moved from health_tests.rs to stay under 500-line cap)
// -------------------------------------------------------------------------

/// Why: `drawer_content_preview` is a shared utility that collapses whitespace
/// and truncates long content for display in recall listings. This pins its
/// contract so regressions are caught immediately without requiring HTTP tests.
/// What: Exercises the trim, whitespace-collapse, truncation, and exact-limit
/// code paths of `drawer_content_preview`.
/// Test: this test.
#[test]
fn drawer_preview_collapses_whitespace_and_truncates() {
    // Short single-line content is returned verbatim.
    assert_eq!(drawer_content_preview("hello world"), "hello world");

    // Multiline / tab-laden content collapses to single-spaced text.
    assert_eq!(
        drawer_content_preview("first line\n\nsecond\tline   third"),
        "first line second line third"
    );

    // Leading / trailing whitespace is stripped.
    assert_eq!(drawer_content_preview("   padded   "), "padded");

    // Empty content yields an empty preview (fallback signal for clients).
    assert_eq!(drawer_content_preview(""), "");

    // Long content is truncated to DRAWER_PREVIEW_MAX_CHARS with an ellipsis.
    let long = "x".repeat(DRAWER_PREVIEW_MAX_CHARS + 50);
    let preview = drawer_content_preview(&long);
    assert_eq!(preview.chars().count(), DRAWER_PREVIEW_MAX_CHARS);
    assert!(preview.ends_with(''));

    // Content right at the limit is not truncated.
    let exact = "y".repeat(DRAWER_PREVIEW_MAX_CHARS);
    assert_eq!(drawer_content_preview(&exact), exact);
}

/// Issue #69 — `recall_entry_json` hoists the drawer's fields to the top
/// level so `content` is directly reachable.
///
/// Why: The recall API previously wrapped the drawer under a `"drawer"`
/// key, so clients scanning the top level for `content`/`tags` found
/// nothing and recall always looked empty. This locks the flattened shape
/// in place so the regression cannot silently return.
/// What: Builds a `RecallResult`, runs it through `recall_entry_json`, and
/// asserts `content`, `tags`, and `importance` are at the top level, that
/// `score`/`layer` sit alongside them, and that the old `drawer` wrapper
/// key is gone.
/// Test: this test.
#[test]
fn recall_entry_json_hoists_drawer_fields() {
    use trusty_common::memory_core::Drawer;

    let room = Uuid::new_v4();
    let mut drawer = Drawer::new(room, "the answer is 42");
    drawer.tags = vec!["source:kuzu".to_string()];
    drawer.importance = 0.7;

    let entry = recall_entry_json(RecallResult {
        drawer,
        score: 0.699,
        layer: 1,
    });

    // Content must be reachable WITHOUT a `drawer` wrapper (issue #69).
    assert_eq!(
        entry.get("content").and_then(|v| v.as_str()),
        Some("the answer is 42"),
        "content must be at the top level, got {entry:?}"
    );
    assert!(
        entry.get("drawer").is_none(),
        "the legacy `drawer` wrapper must not be present, got {entry:?}"
    );
    // Other drawer fields are hoisted too.
    assert_eq!(
        entry["importance"].as_f64().map(|f| (f * 10.0).round()),
        Some(7.0)
    );
    assert_eq!(
        entry["tags"][0].as_str(),
        Some("source:kuzu"),
        "tags must be hoisted, got {entry:?}"
    );
    // Ranking metadata sits alongside the hoisted fields.
    assert_eq!(entry["layer"].as_u64(), Some(1));
    assert!(
        entry["score"]
            .as_f64()
            .is_some_and(|s| (s - 0.699).abs() < 1e-6),
        "score must be preserved, got {entry:?}"
    );
}