remem-ai 0.4.4

Persistent memory for Claude Code — single binary, zero subprocesses
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
use crate::memory::types::tests_helper::setup_memory_schema;
use rusqlite::Connection;

use super::super::policy::{ContextLimits, ContextPolicy};
use super::super::query::{load_context_data, load_context_data_with_policy};
use super::super::render::{enforce_total_char_limit, enforce_total_char_limit_preserving_footer};
use super::{insert_global_memory, insert_memory, insert_memory_with_branch};

#[test]
fn load_context_data_dedupes_generated_topic_keys_by_workstream_context() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/vibeguard";
    let now = chrono::Utc::now().timestamp();

    for idx in 0..6 {
        insert_memory(
            &conn,
            idx + 1,
            project,
            Some(&format!("decision-{:016x}", idx)),
            "decision",
            &format!("Landing page decision {idx}"),
            "[Context: Build VibeGuard landing page and wireframe variants]\nDecision body",
            now - idx,
        );
    }
    insert_memory(
        &conn,
        99,
        project,
        Some("post-write-hook-worktree-hang"),
        "bugfix",
        "Fix post-write hook worktree hang",
        "Handle .git files when resolving worktree roots",
        now - 100,
    );

    let loaded = load_context_data(&conn, project, None);
    let landing_count = loaded
        .memories
        .iter()
        .filter(|memory| memory.title.contains("Landing page decision"))
        .count();

    assert_eq!(landing_count, 1);
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Fix post-write hook worktree hang"));
}

#[test]
fn load_context_data_clusters_contexts_by_pr_reference() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/vibeguard";
    let now = chrono::Utc::now().timestamp();

    insert_memory(
        &conn,
        1,
        project,
        Some("decision-1111111111111111"),
        "decision",
        "Skill install decision A",
        "[Context: Review VibeGuard PR #116 added agentsmd-audit and trajectory-review skills]\nBody",
        now,
    );
    insert_memory(
        &conn,
        2,
        project,
        Some("decision-2222222222222222"),
        "decision",
        "Skill install decision B",
        "[Context: Review VibeGuard PR 116 new skill installation contract]\nBody",
        now - 1,
    );

    let loaded = load_context_data(&conn, project, None);
    let pr_decision_count = loaded
        .memories
        .iter()
        .filter(|memory| memory.title.starts_with("Skill install decision"))
        .count();

    assert_eq!(pr_decision_count, 1);
}

#[test]
fn load_context_data_prefers_current_branch_within_dedup_cluster() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/vibeguard";
    let now = chrono::Utc::now().timestamp();

    insert_memory_with_branch(
        &conn,
        1,
        project,
        Some("decision-1111111111111111"),
        "decision",
        "Main branch landing decision",
        "[Context: Build VibeGuard landing page and wireframe variants]\nMain body",
        now,
        Some("main"),
    );
    insert_memory_with_branch(
        &conn,
        2,
        project,
        Some("decision-2222222222222222"),
        "decision",
        "Feature branch landing decision",
        "[Context: Build VibeGuard landing page and wireframe variants]\nFeature body",
        now - 10,
        Some("fix/context-selection"),
    );

    let loaded = load_context_data(&conn, project, Some("fix/context-selection"));

    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Feature branch landing decision"));
    assert!(!loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Main branch landing decision"));
}

#[test]
fn load_context_data_keeps_current_branch_memories_before_limit() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/vibeguard";
    let now = chrono::Utc::now().timestamp();

    for idx in 0..55 {
        insert_memory(
            &conn,
            idx + 1,
            project,
            Some(&format!("branchless-topic-{idx}")),
            "decision",
            &format!("Branchless decision {idx}"),
            "Recent branchless decision body",
            now - idx,
        );
    }
    insert_memory_with_branch(
        &conn,
        200,
        project,
        Some("current-branch-topic"),
        "bugfix",
        "Current branch operational fix",
        "Older but branch-specific fix body",
        now - 1_000,
        Some("fix/context-selection"),
    );

    let loaded = load_context_data(&conn, project, Some("fix/context-selection"));

    assert!(loaded.memories.len() > ContextLimits::default().memory_index_limit);
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Current branch operational fix"));
}

#[test]
fn load_context_data_limits_memory_self_diagnostics_before_index_rendering() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/vibeguard";
    let now = chrono::Utc::now().timestamp();

    for idx in 0..8 {
        insert_memory(
            &conn,
            idx + 1,
            project,
            Some(&format!("decision-{:016x}", idx)),
            "decision",
            &format!("Memory injection diagnosis {idx}"),
            "Debug remem context SessionStart memories loaded behavior",
            now - idx,
        );
    }
    insert_memory(
        &conn,
        100,
        project,
        Some("guard-paths-source-path"),
        "bugfix",
        "Fix guard path source selection",
        "Use source path evidence in guard output",
        now - 20,
    );

    let loaded = load_context_data(&conn, project, None);
    let self_diagnostic_count = loaded
        .memories
        .iter()
        .filter(|memory| memory.title.contains("Memory injection diagnosis"))
        .count();

    assert_eq!(self_diagnostic_count, 2);
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Fix guard path source selection"));
}

#[test]
fn load_context_data_excludes_preferences_from_main_memory_pool() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/computer";
    let now = chrono::Utc::now().timestamp();

    for idx in 0..60 {
        insert_memory(
            &conn,
            idx + 1,
            project,
            Some(&format!("preference-topic-{idx}")),
            "preference",
            &format!("Preference {idx}"),
            "User prefers evidence-backed coordination",
            now - idx,
        );
    }
    insert_memory(
        &conn,
        100,
        project,
        Some("context-budget-decision"),
        "decision",
        "Use host-aware context compiler",
        "Split preferences from the main memory index",
        now - 100,
    );
    insert_memory(
        &conn,
        101,
        project,
        Some("context-budget-discovery"),
        "discovery",
        "Preference flood starves core memories",
        "The main index was dominated by preferences",
        now - 101,
    );

    let loaded = load_context_data(&conn, project, None);

    assert!(!loaded
        .memories
        .iter()
        .any(|memory| memory.memory_type == "preference"));
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Use host-aware context compiler"));
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Preference flood starves core memories"));
}

#[test]
fn load_context_data_excludes_preferences_before_candidate_limit() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/computer";
    let now = chrono::Utc::now().timestamp();

    for idx in 0..130 {
        insert_memory(
            &conn,
            idx + 1,
            project,
            Some(&format!("preference-topic-{idx}")),
            "preference",
            &format!("Preference {idx}"),
            "User prefers evidence-backed coordination",
            now - idx,
        );
    }
    insert_memory(
        &conn,
        200,
        project,
        Some("context-budget-decision"),
        "decision",
        "Use host-aware context compiler",
        "Split preferences from the main memory index",
        now - 1_000,
    );
    insert_memory(
        &conn,
        201,
        project,
        Some("context-budget-bugfix"),
        "bugfix",
        "Keep core memories visible",
        "Filter preferences before applying the candidate cap",
        now - 1_001,
    );

    let loaded = load_context_data(&conn, project, None);

    assert!(loaded
        .memories
        .iter()
        .all(|memory| memory.memory_type != "preference"));
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Use host-aware context compiler"));
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Keep core memories visible"));
}

#[test]
fn load_context_data_excludes_global_non_preferences_from_main_memory_pool() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/remem";
    let now = chrono::Utc::now().timestamp();

    insert_global_memory(
        &conn,
        1,
        "manual",
        Some("mihomo-proxy-group-duplicate-name"),
        "bugfix",
        "Mihomo proxy group duplicate name",
        "This global bugfix should not appear in project SessionStart Core or Index.",
        now,
    );
    insert_memory(
        &conn,
        2,
        project,
        Some("context-project-only-memory"),
        "decision",
        "Keep context memory project scoped",
        "SessionStart should load project-local non-preference memories.",
        now - 1,
    );

    let loaded = load_context_data(&conn, project, None);

    assert!(!loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Mihomo proxy group duplicate name"));
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Keep context memory project scoped"));
}

#[test]
fn load_context_data_excludes_global_non_preferences_from_basename_search() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/remem";
    let now = chrono::Utc::now().timestamp();
    let limits = ContextLimits {
        candidate_fetch_limit: 1,
        memory_index_limit: 10,
        core_item_limit: 3,
        ..ContextLimits::default()
    };
    let policy = ContextPolicy::from_limits(limits);

    insert_memory(
        &conn,
        1,
        project,
        Some("older-local-memory"),
        "decision",
        "Older local remem decision",
        "A project-local decision should still appear through basename search.",
        now - 10,
    );
    insert_memory(
        &conn,
        2,
        project,
        Some("newer-local-memory"),
        "decision",
        "Newer local context decision",
        "This fills the tiny recent candidate limit.",
        now,
    );
    for idx in 0..25 {
        insert_global_memory(
            &conn,
            idx + 3,
            "manual",
            Some(&format!("global-remem-memory-{idx}")),
            "bugfix",
            &format!("Global remem bugfix {idx}"),
            "A global result matching the basename query should not enter context.",
            now + 1 + idx,
        );
    }

    let loaded = load_context_data_with_policy(&conn, project, None, &policy);

    assert!(!loaded
        .memories
        .iter()
        .any(|memory| memory.title.starts_with("Global remem bugfix")));
    assert!(loaded
        .memories
        .iter()
        .any(|memory| memory.title == "Older local remem decision"));
}

#[test]
fn enforce_total_char_limit_truncates_rendered_output() {
    let mut output = format!("{}{}", "# [/tmp/demo] context\n", "x".repeat(500));

    enforce_total_char_limit(&mut output, 120);

    assert!(output.chars().count() <= 120);
    assert!(output.contains("REMEM_CONTEXT_TOTAL_CHAR_LIMIT"));
}

#[test]
fn enforce_total_char_limit_preserves_footer_when_it_fits() {
    let footer = "22 context memories loaded. 2 core memories. 20 indexed memories. 5 preferences. 5 sessions.\n";
    let mut output = format!("{}{}{}", "# [/tmp/demo] context\n", "x".repeat(500), footer);

    enforce_total_char_limit_preserving_footer(&mut output, 180, footer);

    assert!(output.chars().count() <= 180);
    assert!(output.contains("REMEM_CONTEXT_TOTAL_CHAR_LIMIT"));
    assert!(output.ends_with(footer));
}

#[test]
fn context_limits_env_override_and_legacy_alias_are_respected() {
    let mut vars = std::collections::HashMap::new();
    vars.insert("REMEM_CONTEXT_OBSERVATIONS", "7".to_string());
    vars.insert("REMEM_CONTEXT_CORE_ITEM_LIMIT", "3".to_string());
    vars.insert("REMEM_CONTEXT_PREFERENCE_CHAR_LIMIT", "900".to_string());

    let limits = ContextLimits::from_env_reader(|key| vars.get(key).cloned());

    assert_eq!(limits.memory_index_limit, 7);
    assert_eq!(limits.core_item_limit, 3);
    assert_eq!(limits.preference_char_limit, 900);
}

#[test]
fn context_limits_new_memory_index_env_wins_over_legacy_alias() {
    let mut vars = std::collections::HashMap::new();
    vars.insert("REMEM_CONTEXT_OBSERVATIONS", "7".to_string());
    vars.insert("REMEM_CONTEXT_MEMORY_INDEX_LIMIT", "11".to_string());

    let limits = ContextLimits::from_env_reader(|key| vars.get(key).cloned());

    assert_eq!(limits.memory_index_limit, 11);
}

#[test]
fn load_context_data_keeps_core_candidates_when_index_limit_is_small() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);
    let project = "/tmp/vibeguard";
    let now = chrono::Utc::now().timestamp();
    let limits = ContextLimits {
        memory_index_limit: 1,
        core_item_limit: 4,
        ..ContextLimits::default()
    };
    let policy = ContextPolicy::from_limits(limits);

    for idx in 0..8 {
        insert_memory(
            &conn,
            idx + 1,
            project,
            Some(&format!("decision-topic-{idx}")),
            "decision",
            &format!("Decision {idx}"),
            "Decision body",
            now - idx,
        );
    }

    let loaded = load_context_data_with_policy(&conn, project, None, &policy);

    assert!(loaded.memories.len() > limits.memory_index_limit);
    assert!(loaded.memories.len() >= limits.core_item_limit);
}