remem-ai 0.4.9

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
use crate::memory::lesson::{LessonMemory, LessonMetadata};
use crate::workstream::{WorkStream, WorkStreamStatus};

use super::super::host::HostKind;
use super::super::policy::ContextLimits;
use super::super::render::{
    build_context_header, build_context_stats_footer, empty_context_output, ContextRenderStats,
    SectionRenderStats,
};
use super::super::sections::{
    render_core_memory, render_core_memory_with_limits, render_lessons_with_limit,
    render_memory_index, render_memory_index_with_limits,
    render_memory_index_with_limits_excluding, render_recent_sessions,
    render_recent_sessions_with_limit, render_workstreams, render_workstreams_with_limits,
};
use super::super::types::{ContextRequest, SessionSummaryBrief};
use super::{sample_memory, sample_memory_with_epoch, sample_workstream};

#[test]
fn render_recent_sessions_truncates_completed_line() {
    let mut output = String::new();
    let summaries = vec![SessionSummaryBrief {
        request: "Implement feature".to_string(),
        completed: Some(format!("{}\nignored", "x".repeat(130))),
        created_at_epoch: 1_710_000_000,
    }];

    render_recent_sessions(&mut output, &summaries);

    assert!(output.contains("Implement feature"));
    assert!(output.contains("=> "));
    assert!(output.contains("..."));
    assert!(!output.contains("ignored"));
}

#[test]
fn render_recent_sessions_truncates_request_text() {
    let mut output = String::new();
    let long_request = format!("Investigate SessionStart budget {}", "x".repeat(300));
    let summaries = vec![SessionSummaryBrief {
        request: long_request.clone(),
        completed: Some("done".to_string()),
        created_at_epoch: 1_710_000_000,
    }];

    render_recent_sessions(&mut output, &summaries);

    assert!(output.contains("Investigate SessionStart budget"));
    assert!(output.contains("..."));
    assert!(!output.contains(&long_request));
}

#[test]
fn render_recent_sessions_respects_char_limit() {
    let mut output = String::new();
    let summaries = vec![
        SessionSummaryBrief {
            request: "Short followup".to_string(),
            completed: Some("done".to_string()),
            created_at_epoch: 1_710_000_000,
        },
        SessionSummaryBrief {
            request: "Second session should not fit".to_string(),
            completed: Some("done".to_string()),
            created_at_epoch: 1_710_000_100,
        },
    ];

    render_recent_sessions_with_limit(&mut output, &summaries, 70);

    assert!(output.contains("Short followup"));
    assert!(!output.contains("Second session should not fit"));
    assert!(output.chars().count() <= 70);
}

#[test]
fn render_memory_index_prioritizes_known_types() {
    let mut output = String::new();
    let memories = vec![
        sample_memory(1, "custom", "Custom title"),
        sample_memory(2, "bugfix", "Fix title"),
        sample_memory(3, "decision", "Decision title"),
    ];

    render_memory_index(&mut output, &memories);

    let decision_pos = output.find("**Decisions**").unwrap();
    let bugfix_pos = output.find("**Bug Fixes**").unwrap();
    let custom_pos = output.find("**custom**").unwrap();
    assert!(decision_pos < bugfix_pos);
    assert!(bugfix_pos < custom_pos);
}

#[test]
fn render_memory_index_labels_and_orders_procedure_memories() {
    let mut output = String::new();
    let memories = vec![
        sample_memory(1, "session_activity", "Recent session"),
        sample_memory(2, "procedure", "Review procedure"),
        sample_memory(3, "discovery", "Discovery title"),
    ];

    render_memory_index(&mut output, &memories);

    let discovery_pos = output.find("**Discoveries**").unwrap();
    let procedure_pos = output.find("**Procedures**").unwrap();
    let session_pos = output.find("**Sessions**").unwrap();
    assert!(output.contains("Review procedure"));
    assert!(discovery_pos < procedure_pos);
    assert!(procedure_pos < session_pos);
}

#[test]
fn render_memory_index_excludes_preferences() {
    let mut output = String::new();
    let memories = vec![
        sample_memory(1, "preference", "Preference title"),
        sample_memory(2, "decision", "Decision title"),
    ];

    render_memory_index(&mut output, &memories);

    assert!(output.contains("Decision title"));
    assert!(!output.contains("Preference title"));
    assert!(!output.contains("**Preferences**"));
}

#[test]
fn render_memory_index_excludes_lessons() {
    let mut output = String::new();
    let memories = vec![
        sample_memory(1, "lesson", "Lesson title"),
        sample_memory(2, "decision", "Decision title"),
    ];

    render_memory_index(&mut output, &memories);

    assert!(output.contains("Decision title"));
    assert!(!output.contains("Lesson title"));
    assert!(!output.contains("**Lessons**"));
}

#[test]
fn render_lessons_respects_item_and_char_limits() {
    let mut output = String::new();
    let lessons = vec![
        sample_lesson(1, "First lesson", 0.9, 3),
        sample_lesson(2, "Second lesson", 0.8, 1),
    ];

    let rendered = render_lessons_with_limit(&mut output, &lessons, 1, 240);

    assert_eq!(rendered, 1);
    assert!(output.contains("## Lessons"));
    assert!(output.contains("First lesson"));
    assert!(output.contains("reinforced 3"));
    assert!(!output.contains("Second lesson"));
    assert!(output.chars().count() <= 240);
}

#[test]
fn render_memory_index_respects_item_limit() {
    let mut output = String::new();
    let limits = ContextLimits {
        memory_index_limit: 2,
        ..ContextLimits::default()
    };
    let memories = vec![
        sample_memory(1, "decision", "Decision one"),
        sample_memory(2, "decision", "Decision two"),
        sample_memory(3, "decision", "Decision three"),
    ];

    render_memory_index_with_limits(&mut output, &memories, &limits);

    assert!(output.contains("Decision one"));
    assert!(output.contains("Decision two"));
    assert!(!output.contains("Decision three"));
}

#[test]
fn render_memory_index_truncates_first_item_to_char_limit() {
    let mut output = String::new();
    let limits = ContextLimits {
        memory_index_char_limit: 48,
        ..ContextLimits::default()
    };
    let long_title = "Decision title that is far too long for the index budget";
    let memories = vec![sample_memory(1, "decision", long_title)];

    let rendered = render_memory_index_with_limits(&mut output, &memories, &limits);
    let body = output.strip_prefix("## Index\n").unwrap().trim_end();

    assert_eq!(rendered, 1);
    assert!(body.chars().count() <= limits.memory_index_char_limit);
    assert!(output.contains("..."));
    assert!(!output.contains(long_title));
}

#[test]
fn render_memory_index_can_skip_core_selected_ids() {
    let mut core_output = String::new();
    let now = chrono::Utc::now().timestamp();
    let memories = vec![
        sample_memory_with_epoch(1, "bugfix", "Core bugfix", now),
        sample_memory_with_epoch(2, "decision", "Index decision", now),
    ];
    let core_summary = render_core_memory_with_limits(
        &mut core_output,
        &memories,
        &ContextLimits {
            core_item_limit: 1,
            ..ContextLimits::default()
        },
    );
    let excluded_ids = core_summary.ids.into_iter().collect();

    let mut index_output = String::new();
    let rendered = render_memory_index_with_limits_excluding(
        &mut index_output,
        &memories,
        &ContextLimits::default(),
        &excluded_ids,
    );

    assert_eq!(rendered, 1);
    assert!(!index_output.contains("Core bugfix"));
    assert!(index_output.contains("Index decision"));
}

#[test]
fn render_workstreams_includes_next_action_when_present() {
    let mut output = String::new();
    let workstreams = vec![WorkStream {
        id: 7,
        project: "demo/project".to_string(),
        title: "Refactor context".to_string(),
        description: None,
        status: WorkStreamStatus::Active,
        progress: None,
        next_action: Some("split renderers".to_string()),
        blockers: None,
        created_at_epoch: 0,
        updated_at_epoch: 0,
        completed_at_epoch: None,
    }];

    render_workstreams(&mut output, &workstreams);

    assert!(output.contains("#7 [active] Refactor context -> split renderers"));
}

#[test]
fn render_workstreams_respects_item_and_char_limits() {
    let mut output = String::new();
    let workstreams = vec![
        sample_workstream(1, "First stream", Some("ship the first fix")),
        sample_workstream(2, "Second stream", Some("ship the second fix")),
        sample_workstream(3, "Third stream", Some("ship the third fix")),
    ];

    render_workstreams_with_limits(&mut output, &workstreams, 2, 200);

    assert!(output.contains("#1 [active] First stream"));
    assert!(output.contains("#2 [active] Second stream"));
    assert!(!output.contains("#3 [active] Third stream"));
    assert!(output.chars().count() <= 200);
}

#[test]
fn render_workstreams_stops_at_char_limit() {
    let mut output = String::new();
    let workstreams = vec![
        sample_workstream(1, "First", Some("fix")),
        sample_workstream(2, "Second", Some("fix")),
    ];

    render_workstreams_with_limits(&mut output, &workstreams, 10, 48);

    assert!(output.contains("#1 [active] First"));
    assert!(!output.contains("#2 [active] Second"));
    assert!(output.chars().count() <= 48);
}

#[test]
fn render_core_memory_prioritizes_higher_score_memories() {
    let mut output = String::new();
    let now = chrono::Utc::now().timestamp();
    let memories = vec![
        sample_memory_with_epoch(1, "discovery", "Lower score", now),
        sample_memory_with_epoch(2, "decision", "Higher score", now),
    ];

    render_core_memory(&mut output, &memories);

    let high_pos = output.find("**#2 Higher score**").unwrap();
    let low_pos = output.find("**#1 Lower score**").unwrap();
    assert!(high_pos < low_pos);
}

#[test]
fn render_core_memory_truncates_first_item_to_char_limit() {
    let mut output = String::new();
    let limits = ContextLimits {
        core_char_limit: 120,
        ..ContextLimits::default()
    };
    let mut long_memory = sample_memory(1, "decision", "Compact title");
    long_memory.text = "x".repeat(500);
    let memories = vec![long_memory];

    render_core_memory_with_limits(&mut output, &memories, &limits);

    let body = output.strip_prefix("## Core\n").unwrap().trim_end();
    assert!(output.chars().count() <= limits.core_char_limit);
    assert!(body.chars().count() <= limits.core_char_limit);
    assert!(output.contains("..."));
}

#[test]
fn render_core_memory_keeps_type_diversity_before_filling_same_type() {
    let mut output = String::new();
    let now = chrono::Utc::now().timestamp();
    let memories = vec![
        sample_memory_with_epoch(1, "decision", "Decision one", now),
        sample_memory_with_epoch(2, "decision", "Decision two", now),
        sample_memory_with_epoch(3, "decision", "Decision three", now),
        sample_memory_with_epoch(4, "discovery", "Operational discovery", now),
    ];

    render_core_memory(&mut output, &memories);

    let discovery_pos = output.find("**#4 Operational discovery**").unwrap();
    let third_decision_pos = output.find("**#3 Decision three**").unwrap();
    assert!(discovery_pos < third_decision_pos);
}

#[test]
fn render_core_memory_does_not_backfill_with_memory_self_diagnostics() {
    let mut output = String::new();
    let now = chrono::Utc::now().timestamp();
    let memories = vec![
        sample_memory_with_epoch(1, "decision", "Memory injection diagnosis", now),
        sample_memory_with_epoch(2, "discovery", "Runtime hook finding", now),
    ];

    render_core_memory(&mut output, &memories);

    let runtime_pos = output.find("**#2 Runtime hook finding**").unwrap();
    assert!(runtime_pos < output.len());
    assert!(!output.contains("Memory injection diagnosis"));
}

#[test]
fn render_core_memory_keeps_stale_decision_out_when_recent_context_is_available() {
    let mut output = String::new();
    let now = chrono::Utc::now().timestamp();
    let stale_epoch = now - 8 * 86400;
    let memories = vec![
        sample_memory_with_epoch(1, "decision", "Recent decision one", now),
        sample_memory_with_epoch(3, "discovery", "Recent discovery one", now),
        sample_memory_with_epoch(4, "discovery", "Recent discovery two", now),
        sample_memory_with_epoch(5, "preference", "Recent preference one", now),
        sample_memory_with_epoch(6, "preference", "Recent preference two", now),
        sample_memory_with_epoch(7, "decision", "Stale landing page decision", stale_epoch),
    ];

    render_core_memory(&mut output, &memories);

    assert!(!output.contains("Stale landing page decision"));
}

#[test]
fn context_stats_footer_reports_budget_scope_and_truncation() {
    let footer = build_context_stats_footer(&ContextRenderStats {
        host: "codex-cli".to_string(),
        branch: Some("fix/context".to_string()),
        hook_source: Some("compact".to_string()),
        total_char_limit: 12_000,
        memories_loaded: 7,
        core: SectionRenderStats {
            count: 2,
            chars: 430,
        },
        lessons: SectionRenderStats {
            count: 1,
            chars: 180,
        },
        index: SectionRenderStats {
            count: 5,
            chars: 800,
        },
        preferences: SectionRenderStats {
            count: 3,
            chars: 240,
        },
        project_preferences: 2,
        global_preferences: 1,
        sessions: SectionRenderStats {
            count: 4,
            chars: 620,
        },
        workstreams: SectionRenderStats {
            count: 1,
            chars: 80,
        },
        core_ids: vec![1, 2],
        output_chars: 3_200,
        truncated: true,
    });

    assert!(footer.contains("7 context memories loaded"));
    assert!(footer.contains("2 core (430 chars)"));
    assert!(footer.contains("1 lessons (180 chars)"));
    assert!(footer.contains("3 preferences (project:2 global:1"));
    assert!(footer.contains("host=codex-cli"));
    assert!(footer.contains("source=compact"));
    assert!(footer.contains("branch=fix/context"));
    assert!(footer.contains("total=3200 chars/~800 tokens"));
    assert!(footer.contains("truncated=yes"));
}

#[test]
fn context_header_marks_compact_reload_visibly() {
    let header = build_context_header("/tmp/remem", Some("main"), Some("compact"));

    assert!(header.starts_with("# [/tmp/remem @main] context "));
    assert!(header.contains("[REMEM POST-COMPACT RELOAD]"));
}

#[test]
fn empty_context_marks_compact_reload_visibly() {
    let output = empty_context_output(&ContextRequest {
        cwd: "/tmp/remem".to_string(),
        project: "/tmp/remem".to_string(),
        session_id: None,
        hook_source: Some("compact".to_string()),
        current_branch: Some("main".to_string()),
        host: HostKind::CodexCli,
        use_colors: false,
    });

    assert!(output.starts_with("# [/tmp/remem @main] context "));
    assert!(output.contains("[REMEM POST-COMPACT RELOAD]"));
    assert!(output.contains("REMEM_CONTEXT_SOURCE=compact"));
    assert!(output.contains("No previous sessions found."));
}

fn sample_lesson(id: i64, title: &str, confidence: f64, reinforcement_count: i64) -> LessonMemory {
    LessonMemory {
        memory: sample_memory(id, "lesson", title),
        metadata: LessonMetadata {
            memory_id: id,
            confidence,
            reinforcement_count,
            source_evidence: None,
            last_reinforced_at_epoch: 1_710_000_000,
            stale_after_epoch: None,
        },
    }
}