remem-ai 0.5.11

Persistent memory for Claude Code and Codex — single binary, automatic context
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
use crate::memory::{
    edge::{MemoryEdgeReference, MemoryEdgeSummary},
    raw_archive::{insert_raw_message, RawMessage, ROLE_ASSISTANT, ROLE_USER, SOURCE_HOOK},
    service::{MultiHopMeta, SearchResultSet},
    Memory,
};
use crate::retrieval::search::{ChannelContribution, SearchExplain, SearchExplainResult};
use serde_json::Value;

use super::{
    raw::{
        build_raw_search_json, build_raw_search_request, render_raw_search_results,
        search_raw_archive,
    },
    search::{
        build_search_json, build_search_request, preview_raw_text, preview_text,
        render_search_results,
    },
    show::{format_memory_timestamp, ShowJson},
    why::{render_why_memory, ContextGateSummary},
};

fn sample_memory() -> Memory {
    Memory {
        id: 1,
        session_id: None,
        project: "proj".to_string(),
        topic_key: None,
        title: "Title".to_string(),
        text: format!("{}\nsecond line", "a".repeat(120)),
        memory_type: "decision".to_string(),
        files: None,
        created_at_epoch: 0,
        updated_at_epoch: 0,
        status: "active".to_string(),
        branch: None,
        scope: "project".to_string(),
    }
}

fn sample_raw() -> RawMessage {
    RawMessage {
        id: 9,
        session_id: "session-1".to_string(),
        project: "proj".to_string(),
        role: "user".to_string(),
        content: format!("{}\nsecond line", "r".repeat(120)),
        source: "hook".to_string(),
        branch: Some("main".to_string()),
        cwd: None,
        created_at_epoch: 0,
    }
}

fn sample_explain() -> SearchExplain {
    SearchExplain {
        query: "needle".to_string(),
        project: Some("proj".to_string()),
        memory_type: Some("decision".to_string()),
        branch: Some("main".to_string()),
        include_stale: false,
        limit: 10,
        offset: 0,
        fetch_limit: 33,
        expanded_terms: vec!["needle".to_string()],
        core_terms: vec!["needle".to_string()],
        fts_query: Some("\"needle\"".to_string()),
        temporal_range: None,
        temporal_field: None,
        rrf_k: 60.0,
        channels: vec![crate::retrieval::search::SearchExplainChannel {
            name: "fts".to_string(),
            enabled: true,
            disabled_reason: None,
            hits: vec![crate::retrieval::search::ChannelHit {
                memory_id: 1,
                rank: 1,
            }],
        }],
        results: vec![SearchExplainResult {
            memory_id: 1,
            final_rank: 1,
            final_score: 0.016393,
            project: "proj".to_string(),
            scope: "project".to_string(),
            visibility: "project-local".to_string(),
            contributions: vec![ChannelContribution {
                channel: "fts".to_string(),
                rank: 1,
                score: 0.016393,
            }],
        }],
        has_more: false,
        raw_fallback_count: 0,
    }
}

#[test]
fn cli_query_preview_uses_first_line_and_truncates() {
    let memory = sample_memory();

    let preview = preview_text(&memory);
    assert_eq!(preview.len(), 80);
    assert!(preview.chars().all(|ch| ch == 'a'));
}

#[test]
fn cli_query_format_memory_timestamp_handles_invalid_epoch() {
    assert_eq!(format_memory_timestamp(i64::MAX), "");
}

#[test]
fn cli_search_request_carries_service_search_options() {
    let request = build_search_request(
        "needle",
        Some("/repo"),
        Some("decision"),
        5,
        10,
        Some("feature"),
        true,
        true,
        true,
    );

    assert_eq!(request.query.as_deref(), Some("needle"));
    assert_eq!(request.project.as_deref(), Some("/repo"));
    assert_eq!(request.memory_type.as_deref(), Some("decision"));
    assert_eq!(request.limit, 5);
    assert_eq!(request.offset, 10);
    assert_eq!(request.branch.as_deref(), Some("feature"));
    assert!(request.include_stale);
    assert!(request.multi_hop);
    assert!(request.explain);
}

#[test]
fn cli_search_render_shows_multi_hop_has_more_and_raw_fallback() {
    let result = SearchResultSet {
        memories: vec![sample_memory()],
        multi_hop: Some(MultiHopMeta {
            hops: 2,
            entities_discovered: vec!["Graphiti".to_string(), "Mem0".to_string()],
        }),
        has_more: true,
        explain: None,
        raw_hits: vec![sample_raw()],
    };

    let output = render_search_results(&result, 10, 5);

    assert!(output.contains("Multi-hop: hops=2 entities=Graphiti, Mem0"));
    assert!(output.contains("Found 1 result(s):"));
    assert!(output.contains("remem show 1"));
    assert!(output.contains("remem why 1"));
    assert!(output.contains("remem search \"<query>\" --offset 15"));
    assert!(output.contains("Raw archive fallback:"));
    assert!(output.contains("[raw:9] user | proj | 1970-01-01 | branch=main"));
    assert!(output.contains(
        "use raw hits for recall only; promote durable conclusions with review/save_memory."
    ));
}

#[test]
fn cli_search_render_uses_raw_fallback_when_curated_is_empty() {
    let result = SearchResultSet {
        memories: vec![],
        multi_hop: None,
        has_more: false,
        explain: None,
        raw_hits: vec![sample_raw()],
    };

    let output = render_search_results(&result, 0, 10);

    assert!(output.contains("No curated memories found."));
    assert!(output.contains("Raw archive fallback:"));
    assert!(output.contains("use raw hits for recall only"));
}

#[test]
fn cli_search_render_includes_explain_without_memory_content_dump() {
    let result = SearchResultSet {
        memories: vec![sample_memory()],
        multi_hop: None,
        has_more: false,
        explain: Some(sample_explain()),
        raw_hits: vec![],
    };

    let output = render_search_results(&result, 0, 10);

    assert!(output.contains("Search explain:"));
    assert!(output.contains("channels:"));
    assert!(output.contains("fts: 1#1"));
    assert!(output.contains("visibility=project-local"));
    assert!(output.contains("contributions: fts#1=0.016393"));
    assert!(!output.contains("second line"));
}

#[test]
fn cli_search_render_includes_explain_for_empty_results() {
    let result = SearchResultSet {
        memories: vec![],
        multi_hop: None,
        has_more: false,
        explain: Some(sample_explain()),
        raw_hits: vec![],
    };

    let output = render_search_results(&result, 0, 10);

    assert!(output.contains("No curated memories found."));
    assert!(output.contains("remem search \"<query>\" --include-stale"));
    assert!(output.contains("remem search \"<query>\" --multi-hop"));
    assert!(output.contains("remem search \"<query>\" --project /path/to/repo"));
    assert!(output.contains("Search explain:"));
    assert!(output.contains("fts_query: Some(\"\\\"needle\\\"\")"));
}

#[test]
fn cli_query_raw_preview_uses_first_line_and_truncates() {
    let raw = sample_raw();

    let preview = preview_raw_text(&raw);

    assert_eq!(preview.len(), 100);
    assert!(preview.chars().all(|ch| ch == 'r'));
}

#[test]
fn cli_search_json_report_is_machine_parseable() -> std::result::Result<(), serde_json::Error> {
    let result = SearchResultSet {
        memories: vec![sample_memory()],
        multi_hop: Some(MultiHopMeta {
            hops: 1,
            entities_discovered: vec!["Mem0".to_string()],
        }),
        has_more: true,
        explain: Some(sample_explain()),
        raw_hits: vec![sample_raw()],
    };
    let output = build_search_json(
        "needle",
        Some("proj"),
        Some("decision"),
        3,
        6,
        Some("main"),
        true,
        true,
        true,
        &result,
    );

    let text = serde_json::to_string(&output)?;
    let parsed: Value = serde_json::from_str(&text)?;

    assert_eq!(parsed["query"], "needle");
    assert_eq!(parsed["limit"], 3);
    assert_eq!(parsed["next_offset"], 9);
    assert_eq!(parsed["results"][0]["id"], 1);
    assert_eq!(parsed["raw_hits"][0]["id"], 9);
    assert_eq!(parsed["multi_hop"]["entities_discovered"][0], "Mem0");
    assert_eq!(parsed["explain_details"]["query"], "needle");
    Ok(())
}

#[test]
fn cli_raw_search_request_carries_raw_filters() {
    let request = build_raw_search_request(
        "literal phrase",
        Some("/repo"),
        Some("main"),
        Some("user"),
        21,
        40,
    );

    assert_eq!(request.query, "literal phrase");
    assert_eq!(request.project.as_deref(), Some("/repo"));
    assert_eq!(request.branch.as_deref(), Some("main"));
    assert_eq!(request.role.as_deref(), Some("user"));
    assert_eq!(request.limit, 21);
    assert_eq!(request.offset, 40);
}

#[test]
fn cli_raw_search_uses_raw_archive_filters() -> anyhow::Result<()> {
    let conn = rusqlite::Connection::open_in_memory()?;
    crate::migrate::run_migrations(&conn)?;
    insert_raw_message(
        &conn,
        "s-user-main",
        "/repo",
        ROLE_USER,
        "literal phrase from user on main",
        SOURCE_HOOK,
        Some("main"),
        Some("/repo"),
    )?;
    insert_raw_message(
        &conn,
        "s-assistant-main",
        "/repo",
        ROLE_ASSISTANT,
        "literal phrase from assistant on main",
        SOURCE_HOOK,
        Some("main"),
        Some("/repo"),
    )?;
    insert_raw_message(
        &conn,
        "s-user-feature",
        "/repo",
        ROLE_USER,
        "literal phrase from user on feature",
        SOURCE_HOOK,
        Some("feature"),
        Some("/repo"),
    )?;

    let request = build_raw_search_request(
        "literal phrase",
        Some("/repo"),
        Some("main"),
        Some("user"),
        20,
        0,
    );
    let rows = search_raw_archive(&conn, &request)?;

    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].role, ROLE_USER);
    assert_eq!(rows[0].branch.as_deref(), Some("main"));
    assert!(rows[0].content.contains("literal phrase"));
    Ok(())
}

#[test]
fn cli_raw_search_render_labels_rows_as_raw_not_curated() {
    let output = render_raw_search_results(&[sample_raw()], 20, 20, true);

    assert!(output.contains("Raw archive rows (not curated memories):"));
    assert!(
        output.contains("[raw:9] user | proj | 1970-01-01 00:00 UTC | source=hook | branch=main")
    );
    assert!(output.contains("raw rows are captured chat turns, not curated memories."));
    assert!(output.contains("remem raw search \"<query>\" --offset 40"));
}

#[test]
fn cli_raw_search_render_empty_mentions_curated_search() {
    let output = render_raw_search_results(&[], 0, 20, false);

    assert!(output.contains("No raw archive rows found."));
    assert!(output.contains("Curated search may still have promoted memories"));
    assert!(output.contains("remem search \"<query>\""));
}

#[test]
fn cli_raw_search_json_report_is_machine_parseable() -> std::result::Result<(), serde_json::Error> {
    let output = build_raw_search_json(
        "literal phrase",
        Some("proj"),
        Some("main"),
        Some("user"),
        20,
        40,
        true,
        &[sample_raw()],
    );

    let text = serde_json::to_string(&output)?;
    let parsed: Value = serde_json::from_str(&text)?;

    assert_eq!(parsed["query"], "literal phrase");
    assert_eq!(parsed["project"], "proj");
    assert_eq!(parsed["branch"], "main");
    assert_eq!(parsed["role"], "user");
    assert_eq!(parsed["source_type"], "raw_archive");
    assert_eq!(
        parsed["note"],
        "raw archive rows are captured chat turns, not curated memories"
    );
    assert_eq!(parsed["count"], 1);
    assert_eq!(parsed["has_more"], true);
    assert_eq!(parsed["next_offset"], 60);
    assert_eq!(parsed["results"][0]["source_type"], "raw_archive");
    assert_eq!(parsed["results"][0]["content"], sample_raw().content);
    Ok(())
}

#[test]
fn cli_show_json_report_is_machine_parseable() -> std::result::Result<(), serde_json::Error> {
    let output = ShowJson {
        found: true,
        id: 1,
        memory: Some(sample_memory()),
        relations: None,
    };

    let text = serde_json::to_string(&output)?;
    let parsed: Value = serde_json::from_str(&text)?;

    assert_eq!(parsed["found"], true);
    assert_eq!(parsed["id"], 1);
    assert_eq!(parsed["memory"]["title"], "Title");
    Ok(())
}

#[test]
fn cli_show_json_can_expose_memory_relations() -> std::result::Result<(), serde_json::Error> {
    let output = ShowJson {
        found: true,
        id: 2,
        memory: Some(sample_memory()),
        relations: Some(MemoryEdgeSummary {
            incoming_count: 1,
            outgoing_count: 0,
            incoming: vec![MemoryEdgeReference {
                id: 7,
                edge_type: "supersedes".to_string(),
                from_memory_id: Some(1),
                to_memory_id: Some(2),
                state_key_id: None,
                source_candidate_id: Some(5),
                evidence_event_ids: vec![10, 11],
                source_operation_id: Some(9),
                confidence: Some(0.92),
                reason: Some("candidate replaces active state/topic memories".to_string()),
                created_at_epoch: 100,
            }],
            outgoing: Vec::new(),
        }),
    };

    let text = serde_json::to_string(&output)?;
    let parsed: Value = serde_json::from_str(&text)?;

    assert_eq!(parsed["relations"]["incoming_count"], 1);
    assert_eq!(
        parsed["relations"]["incoming"][0]["edge_type"],
        "supersedes"
    );
    assert_eq!(parsed["relations"]["incoming"][0]["from_memory_id"], 1);
    assert_eq!(parsed["relations"]["incoming"][0]["source_candidate_id"], 5);
    assert_eq!(
        parsed["relations"]["incoming"][0]["evidence_event_ids"][1],
        11
    );
    Ok(())
}

#[test]
fn cli_why_render_distinguishes_visibility_from_query_scoring() {
    let gate = ContextGateSummary {
        host: "codex-cli".to_string(),
        project: "proj".to_string(),
        output_mode: "suppressed".to_string(),
        emit_count: 2,
        suppress_count: 1,
        updated_at_epoch: 0,
        last_emitted_epoch: 0,
    };

    let output = render_why_memory(
        &sample_memory(),
        Some("proj"),
        Some("main"),
        Some(&gate),
        None,
    );

    assert!(output.contains("Memory #1"));
    assert!(output.contains("project match: exact proj"));
    assert!(output.contains("branch match: branchless; visible in branch-scoped search for main"));
    assert!(output.contains("type: core memory type"));
    assert!(output.contains("status: active; default search can include it"));
    assert!(output.contains("currentness: no TTL/currentness metadata available"));
    assert!(output.contains("query scoring: query-specific"));
    assert!(output.contains("context visibility: memory index candidate and core candidate"));
    assert!(output.contains("context gate: latest codex-cli output for proj: mode=suppressed"));
    assert!(output.contains("gate rows are context-output level, not per-memory proof"));
    assert!(output.contains("remem show 1"));
}