remem-ai 0.4.3

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
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
use super::{resolve_local_note_path, sanitize_segment, save_memory, SaveMemoryRequest};
use crate::db::{self, test_support::ScopedTestDataDir};
#[cfg(unix)]
use std::os::unix::fs::symlink;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

#[test]
fn sanitize_segment_falls_back_for_empty_slug() {
    let got = sanitize_segment("!!!", "fallback", 64);
    assert_eq!(got, "fallback");
}

// --- path confinement tests ---

#[test]
fn resolve_absolute_path_inside_base_is_allowed() {
    let _dir = ScopedTestDataDir::new("path-inside");
    let base = crate::db::data_dir();
    let target = base.join("notes").join("test.md");
    let got = resolve_local_note_path("proj", Some("title"), Some(target.to_str().unwrap()));
    assert!(got.is_ok(), "path inside base should be allowed: {:?}", got);
}

#[test]
fn resolve_absolute_path_outside_base_is_rejected() {
    let _dir = ScopedTestDataDir::new("path-outside");
    let got = resolve_local_note_path("proj", Some("title"), Some("/etc/passwd"));
    assert!(
        got.is_err(),
        "absolute path outside base should be rejected"
    );
    assert!(got
        .unwrap_err()
        .to_string()
        .contains("outside the allowed directory"));
}

#[test]
fn resolve_relative_traversal_is_rejected() {
    let _dir = ScopedTestDataDir::new("path-traversal");
    let got = resolve_local_note_path("proj", Some("title"), Some("../../etc/passwd"));
    assert!(got.is_err(), "path traversal should be rejected");
    assert!(got
        .unwrap_err()
        .to_string()
        .contains("outside the allowed directory"));
}

#[test]
fn resolve_tilde_path_is_rejected() {
    let _dir = ScopedTestDataDir::new("path-tilde");
    let got = resolve_local_note_path("proj", Some("title"), Some("~/.ssh/authorized_keys"));
    assert!(got.is_err(), "tilde path should be rejected (not expanded)");
}

#[test]
fn save_memory_preference_defaults_to_project_scope() {
    let _dir = ScopedTestDataDir::new("preference-default-project-scope");
    let conn = db::open_db().expect("db should open");
    let req = SaveMemoryRequest {
        text: "Prefer project-specific workflow notes".to_string(),
        title: Some("Preference".to_string()),
        project: Some("proj".to_string()),
        memory_type: Some("preference".to_string()),
        local_copy_enabled: Some(false),
        ..SaveMemoryRequest::default()
    };

    let saved = save_memory(&conn, &req).expect("preference save should succeed");
    let scope: String = conn
        .query_row(
            "SELECT scope FROM memories WHERE id = ?1",
            [saved.id],
            |row| row.get(0),
        )
        .expect("scope query should succeed");
    assert_eq!(scope, "project");
}

#[test]
fn save_memory_outside_local_path_does_not_persist_memory() {
    let _dir = ScopedTestDataDir::new("save-outside-path-no-db-write");
    let conn = db::open_db().expect("db should open");
    let req = SaveMemoryRequest {
        text: "body".to_string(),
        title: Some("Memory".to_string()),
        project: Some("proj".to_string()),
        local_path: Some("/etc/passwd".to_string()),
        local_copy_enabled: Some(true),
        ..SaveMemoryRequest::default()
    };

    let err = save_memory(&conn, &req).expect_err("out-of-bounds local_path should fail");

    assert!(
        err.to_string().contains("outside the allowed directory"),
        "unexpected error: {err:?}"
    );

    let memory_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM memories", [], |row| row.get(0))
        .expect("count query should succeed");
    assert_eq!(memory_count, 0, "db should not persist a memory row");
}

#[test]
fn save_memory_local_write_failure_does_not_persist_memory() {
    let test_dir = ScopedTestDataDir::new("save-local-write-failure-no-db-write");
    let conn = db::open_db().expect("db should open");
    let blocking_file = test_dir.path.join("manual-notes").join("proj");
    std::fs::create_dir_all(blocking_file.parent().expect("blocking file parent"))
        .expect("create blocking file parent");
    std::fs::write(&blocking_file, "not a directory").expect("create blocking file");

    let local_path = blocking_file.join("forced-failure.md");
    let req = SaveMemoryRequest {
        text: "body".to_string(),
        title: Some("Memory".to_string()),
        project: Some("proj".to_string()),
        local_path: Some(local_path.display().to_string()),
        local_copy_enabled: Some(true),
        ..SaveMemoryRequest::default()
    };

    let err = save_memory(&conn, &req).expect_err("local write should abort save");

    assert!(
        err.to_string().contains("Not a directory")
            || err.to_string().contains("not a directory")
            || err.to_string().contains("File exists"),
        "unexpected error: {err:?}"
    );
    assert!(
        !local_path.exists(),
        "local copy path should not exist after a write failure: {:?}",
        local_path
    );

    let memory_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM memories", [], |row| row.get(0))
        .expect("count query should succeed");
    assert_eq!(memory_count, 0, "db should not persist a memory row");
}

#[test]
fn save_memory_db_failure_does_not_leave_local_copy_behind() {
    let test_dir = ScopedTestDataDir::new("save-db-failure-no-local-copy");
    let conn = db::open_db().expect("db should open");
    conn.execute_batch(
        "CREATE TRIGGER fail_memory_insert BEFORE INSERT ON memories BEGIN
            SELECT RAISE(ABORT, 'forced insert failure');
        END;",
    )
    .expect("failure trigger should be created");

    let local_path = test_dir
        .path
        .join("manual-notes")
        .join("proj")
        .join("forced-failure.md");
    let req = SaveMemoryRequest {
        text: "body".to_string(),
        title: Some("Memory".to_string()),
        project: Some("proj".to_string()),
        local_path: Some(local_path.display().to_string()),
        local_copy_enabled: Some(true),
        ..SaveMemoryRequest::default()
    };

    let err = save_memory(&conn, &req).expect_err("insert trigger should abort save");

    assert!(
        err.to_string().contains("forced insert failure"),
        "unexpected error: {err:?}"
    );
    assert!(
        !local_path.exists(),
        "local copy should not be written when db insert fails: {:?}",
        local_path
    );

    let memory_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM memories", [], |row| row.get(0))
        .expect("count query should succeed");
    assert_eq!(memory_count, 0, "db should not persist a memory row");
}

#[test]
fn save_memory_db_failure_restores_existing_local_copy() {
    let test_dir = ScopedTestDataDir::new("save-db-failure-restores-existing-local-copy");
    let conn = db::open_db().expect("db should open");
    conn.execute_batch(
        "CREATE TRIGGER fail_memory_insert BEFORE INSERT ON memories BEGIN
            SELECT RAISE(ABORT, 'forced insert failure');
        END;",
    )
    .expect("failure trigger should be created");

    let local_path = test_dir
        .path
        .join("manual-notes")
        .join("proj")
        .join("existing-note.md");
    std::fs::create_dir_all(local_path.parent().expect("existing note parent"))
        .expect("create existing note parent");
    std::fs::write(&local_path, "original note body").expect("seed existing note");

    let req = SaveMemoryRequest {
        text: "body".to_string(),
        title: Some("Memory".to_string()),
        project: Some("proj".to_string()),
        local_path: Some(local_path.display().to_string()),
        local_copy_enabled: Some(true),
        ..SaveMemoryRequest::default()
    };

    let err = save_memory(&conn, &req).expect_err("insert trigger should abort save");

    assert!(
        err.to_string().contains("forced insert failure"),
        "unexpected error: {err:?}"
    );
    assert_eq!(
        std::fs::read_to_string(&local_path).expect("existing note should remain readable"),
        "original note body",
        "db failure should restore the prior local note contents"
    );

    let memory_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM memories", [], |row| row.get(0))
        .expect("count query should succeed");
    assert_eq!(memory_count, 0, "db should not persist a memory row");
}

#[test]
fn save_memory_existing_directory_local_path_does_not_persist_memory() {
    let test_dir = ScopedTestDataDir::new("save-directory-local-path-rejected");
    let conn = db::open_db().expect("db should open");

    let local_path = test_dir
        .path
        .join("manual-notes")
        .join("proj")
        .join("existing-dir");
    let nested_entry = local_path.join("nested.txt");
    std::fs::create_dir_all(&local_path).expect("create existing directory local path");
    std::fs::write(&nested_entry, "keep me").expect("seed nested entry");

    let req = SaveMemoryRequest {
        text: "body".to_string(),
        title: Some("Memory".to_string()),
        project: Some("proj".to_string()),
        local_path: Some(local_path.display().to_string()),
        local_copy_enabled: Some(true),
        ..SaveMemoryRequest::default()
    };

    let err = save_memory(&conn, &req).expect_err("directory local_path should fail");

    assert!(
        err.to_string()
            .contains("must reference a file, not a directory"),
        "unexpected error: {err:?}"
    );
    assert!(
        local_path.is_dir(),
        "directory path should remain a directory"
    );
    assert_eq!(
        std::fs::read_to_string(&nested_entry).expect("nested entry should stay intact"),
        "keep me"
    );

    let memory_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM memories", [], |row| row.get(0))
        .expect("count query should succeed");
    assert_eq!(memory_count, 0, "db should not persist a memory row");
}

#[cfg(unix)]
#[test]
fn save_memory_db_failure_restores_write_only_existing_local_copy() {
    let test_dir = ScopedTestDataDir::new("save-db-failure-restores-write-only-local-copy");
    let conn = db::open_db().expect("db should open");
    conn.execute_batch(
        "CREATE TRIGGER fail_memory_insert BEFORE INSERT ON memories BEGIN
            SELECT RAISE(ABORT, 'forced insert failure');
        END;",
    )
    .expect("failure trigger should be created");

    let local_path = test_dir
        .path
        .join("manual-notes")
        .join("proj")
        .join("write-only-note.md");
    std::fs::create_dir_all(local_path.parent().expect("existing note parent"))
        .expect("create existing note parent");
    std::fs::write(&local_path, "original note body").expect("seed existing note");

    let mut permissions = std::fs::metadata(&local_path)
        .expect("read existing permissions")
        .permissions();
    permissions.set_mode(0o200);
    std::fs::set_permissions(&local_path, permissions).expect("make existing note write-only");

    let req = SaveMemoryRequest {
        text: "body".to_string(),
        title: Some("Memory".to_string()),
        project: Some("proj".to_string()),
        local_path: Some(local_path.display().to_string()),
        local_copy_enabled: Some(true),
        ..SaveMemoryRequest::default()
    };

    let err = save_memory(&conn, &req).expect_err("insert trigger should abort save");

    assert!(
        err.to_string().contains("forced insert failure"),
        "unexpected error: {err:?}"
    );

    let mut readable_permissions = std::fs::metadata(&local_path)
        .expect("restored note should exist")
        .permissions();
    readable_permissions.set_mode(0o600);
    std::fs::set_permissions(&local_path, readable_permissions)
        .expect("make restored note readable");

    assert_eq!(
        std::fs::read_to_string(&local_path).expect("restored note should be readable"),
        "original note body",
        "db failure should restore the prior local note contents"
    );

    let memory_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM memories", [], |row| row.get(0))
        .expect("count query should succeed");
    assert_eq!(memory_count, 0, "db should not persist a memory row");
}

#[cfg(unix)]
#[test]
fn save_memory_existing_symlink_local_path_stays_a_symlink() {
    let test_dir = ScopedTestDataDir::new("save-symlink-local-path-preserved");
    let conn = db::open_db().expect("db should open");

    let project_dir = test_dir.path.join("manual-notes").join("proj");
    std::fs::create_dir_all(&project_dir).expect("create project dir");

    let target_path = project_dir.join("target-note.md");
    std::fs::write(&target_path, "original note body").expect("seed symlink target");

    let local_path = project_dir.join("symlink-note.md");
    symlink(&target_path, &local_path).expect("create local note symlink");

    let req = SaveMemoryRequest {
        text: "updated body".to_string(),
        title: Some("Memory".to_string()),
        project: Some("proj".to_string()),
        local_path: Some(local_path.display().to_string()),
        local_copy_enabled: Some(true),
        ..SaveMemoryRequest::default()
    };

    let saved = save_memory(&conn, &req).expect("save through symlink should succeed");

    assert_eq!(saved.status, "saved");
    assert!(
        std::fs::symlink_metadata(&local_path)
            .expect("local path metadata")
            .file_type()
            .is_symlink(),
        "local path should remain a symlink"
    );

    let symlink_target = std::fs::read_link(&local_path).expect("read symlink target");
    assert_eq!(
        symlink_target, target_path,
        "symlink target should be preserved"
    );

    let updated = std::fs::read_to_string(&target_path).expect("read updated target");
    assert!(
        updated.contains("updated body"),
        "saved note should be written through the symlink target: {updated}"
    );
}

#[test]
fn resolve_base_dir_itself_is_rejected() {
    let _dir = ScopedTestDataDir::new("path-base-itself");
    let base = crate::db::data_dir();
    let got = resolve_local_note_path("proj", Some("title"), Some(base.to_str().unwrap()));
    assert!(
        got.is_err(),
        "base dir itself should be rejected — must be a file inside base"
    );
}

#[test]
fn resolve_none_local_path_returns_default() {
    let _dir = ScopedTestDataDir::new("path-default");
    unsafe { std::env::remove_var("REMEM_SAVE_MEMORY_LOCAL_DIR") };

    let got = resolve_local_note_path("proj", Some("title"), None);
    assert!(got.is_ok());
    let path = got.unwrap();
    assert!(path.is_absolute());
    let base = crate::db::data_dir();
    assert!(
        path.starts_with(&base),
        "default path {:?} should be inside {:?}",
        path,
        base
    );
}

#[test]
fn resolve_none_local_path_allows_env_directory_inside_base() {
    let _dir = ScopedTestDataDir::new("path-default-env-inside");
    let base = crate::db::data_dir();
    let env_dir = base.join("manual-notes-custom");
    unsafe { std::env::set_var("REMEM_SAVE_MEMORY_LOCAL_DIR", &env_dir) };

    let got = resolve_local_note_path("proj", Some("title"), None);
    unsafe { std::env::remove_var("REMEM_SAVE_MEMORY_LOCAL_DIR") };

    assert!(
        got.is_ok(),
        "env path inside base should be allowed: {got:?}"
    );
    let path = got.unwrap();
    assert!(
        path.starts_with(&env_dir),
        "default path {:?} should be inside env dir {:?}",
        path,
        env_dir
    );
}

#[test]
fn resolve_none_local_path_rejects_env_directory_outside_base() {
    let _dir = ScopedTestDataDir::new("path-default-env-outside");
    let outside = std::env::temp_dir().join("remem-outside-manual-notes");
    unsafe { std::env::set_var("REMEM_SAVE_MEMORY_LOCAL_DIR", &outside) };

    let got = resolve_local_note_path("proj", Some("title"), None);
    unsafe { std::env::remove_var("REMEM_SAVE_MEMORY_LOCAL_DIR") };

    assert!(
        got.is_err(),
        "env path outside base should be rejected instead of bypassing confinement"
    );
    assert!(got
        .unwrap_err()
        .to_string()
        .contains("outside the allowed directory"));
}

#[test]
fn resolve_empty_local_path_uses_confined_default() {
    let _dir = ScopedTestDataDir::new("path-default-empty");
    let outside = std::env::temp_dir().join("remem-outside-empty-manual-notes");
    unsafe { std::env::set_var("REMEM_SAVE_MEMORY_LOCAL_DIR", &outside) };

    let got = resolve_local_note_path("proj", Some("title"), Some("   "));
    unsafe { std::env::remove_var("REMEM_SAVE_MEMORY_LOCAL_DIR") };

    assert!(
        got.is_err(),
        "empty local_path should follow the same confined default branch"
    );
}

#[test]
fn resolve_none_local_path_allows_relative_remem_data_dir_default() {
    let _guard = ScopedTestDataDir::new("path-default-relative-data-dir");
    let original_cwd = std::env::current_dir().expect("read cwd");
    let temp_root = std::env::temp_dir().join(format!(
        "remem-relative-data-dir-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system time before unix epoch")
            .as_nanos()
    ));
    let project_root = temp_root.join("workspace");
    std::fs::create_dir_all(&project_root).expect("create project root");
    let expected_base = project_root
        .canonicalize()
        .expect("canonicalize project root")
        .join(".remem");
    unsafe {
        std::env::set_current_dir(&project_root).expect("enter project root");
        std::env::set_var("REMEM_DATA_DIR", ".remem");
        std::env::remove_var("REMEM_SAVE_MEMORY_LOCAL_DIR");
    }

    let got = resolve_local_note_path("proj", Some("title"), None);

    unsafe {
        std::env::set_current_dir(&original_cwd).expect("restore cwd");
        std::env::remove_var("REMEM_DATA_DIR");
    }
    let _ = std::fs::remove_dir_all(&temp_root);

    assert!(
        got.is_ok(),
        "relative REMEM_DATA_DIR default path should be allowed: {got:?}"
    );
    let path = got.unwrap();
    assert!(
        path.is_absolute(),
        "resolved path should be absolute: {path:?}"
    );
    assert!(
        path.starts_with(&expected_base),
        "resolved path {path:?} should stay inside {:?}",
        expected_base
    );
}

#[test]
fn resolve_none_local_path_allows_relative_remem_data_dir_with_parent_segments() {
    let _guard = ScopedTestDataDir::new("path-default-relative-data-dir-parent-segments");
    let original_cwd = std::env::current_dir().expect("read cwd");
    let temp_root = std::env::temp_dir().join(format!(
        "remem-relative-parent-data-dir-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system time before unix epoch")
            .as_nanos()
    ));
    let workspace_root = temp_root.join("workspace");
    let project_root = workspace_root.join("project");
    std::fs::create_dir_all(&project_root).expect("create project root");
    let expected_base = workspace_root
        .canonicalize()
        .expect("canonicalize workspace root")
        .join(".remem");
    unsafe {
        std::env::set_current_dir(&project_root).expect("enter project root");
        std::env::set_var("REMEM_DATA_DIR", "../.remem");
        std::env::remove_var("REMEM_SAVE_MEMORY_LOCAL_DIR");
    }

    let got = resolve_local_note_path("proj", Some("title"), None);

    unsafe {
        std::env::set_current_dir(&original_cwd).expect("restore cwd");
        std::env::remove_var("REMEM_DATA_DIR");
    }
    let _ = std::fs::remove_dir_all(&temp_root);

    assert!(
        got.is_ok(),
        "relative REMEM_DATA_DIR with parent segments should be allowed: {got:?}"
    );
    let path = got.unwrap();
    assert!(
        path.starts_with(&expected_base),
        "resolved path {path:?} should stay inside {:?}",
        expected_base
    );
}