remem-ai 0.6.38

Local-first coding agent memory for Claude Code and OpenAI Codex
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
use std::ffi::OsStr;
use std::fs::{self, DirEntry, File, Metadata};
use std::io::{self, Read};
use std::path::{Path, PathBuf};

use super::types::{Check, Status};
use crate::db;

const SQLITE_PLAINTEXT_HEADER: &[u8; 16] = b"SQLite format 3\0";

#[derive(Debug)]
struct PlaintextArtifact {
    path: PathBuf,
    size_bytes: u64,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum FileHeader {
    Plaintext,
    NonPlaintext,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum LiveDatabaseState {
    Encrypted,
    Plaintext,
    Missing,
    Unverified,
}

impl LiveDatabaseState {
    fn description(self) -> &'static str {
        match self {
            Self::Encrypted => "encrypted and readable",
            Self::Plaintext => "plaintext",
            Self::Missing => "missing",
            Self::Unverified => "unverified",
        }
    }

    fn remediation(self) -> &'static str {
        match self {
            Self::Encrypted => {
                "remem doctor did not delete any files. Run `remem status` to confirm the \
encrypted live database still opens, then run `remem admin backup` to create a new encrypted \
backup. After the new backup succeeds, manually delete the listed plaintext copies or retain \
them only in encrypted storage. Moving them outside the remem data dir alone does not protect them"
            }
            Self::Plaintext => {
                "remem doctor did not delete any files. First check for the encryption-blocking \
files `REMEM_DATA_DIR/remem.db.bak` and \
`REMEM_DATA_DIR/remem.db.enc`. If either exists, verify that \
`REMEM_DATA_DIR/backups/` is a real directory and not a symbolic link, choose a distinct unused \
destination name there for each blocker, and move each blocker without overwriting any existing \
file. Only after every existing blocker has been preserved successfully should you run `remem \
encrypt`, then run `remem status` to confirm the live database opens. Run `remem doctor` and \
verify that its Plaintext residue detail reports the live database as encrypted and readable; \
that check is expected to remain `Fail` while the preserved plaintext copies exist. Only after \
that confirmation should you run `remem admin backup` to create a new encrypted backup. After \
the backup succeeds, manually delete the listed plaintext copies or retain them only in encrypted \
storage, then rerun `remem doctor` until Plaintext residue passes. Moving them outside the remem \
data dir alone does not protect them"
            }
            Self::Missing => {
                "remem doctor did not delete any files. Do not create a new backup or delete any \
listed copy yet. First restore the missing live database, resolve any key mismatch, and make the \
database readable. After `remem status` succeeds and `remem doctor` reports the live database as \
encrypted and readable, run `remem admin backup` to create a new encrypted backup; only after \
that succeeds should you manually delete the listed plaintext copies or retain them only in \
encrypted storage. Moving them outside the remem data dir alone does not protect them"
            }
            Self::Unverified => {
                "remem doctor did not delete any files. Do not create a new backup or delete any \
listed copy yet. First repair the live database and resolve any key or readability problem. After \
`remem status` succeeds and `remem doctor` reports the live database as encrypted and readable, \
run `remem admin backup` to create a new encrypted backup; only after that succeeds should you \
manually delete the listed plaintext copies or retain them only in encrypted storage. Moving them \
outside the remem data dir alone does not protect them"
            }
        }
    }
}

/// Detect plaintext SQLite residue next to the live database and throughout
/// the managed backup directory.
///
/// The shared doctor connection is the source of truth for live-database
/// readability. A non-plaintext header alone is not proof of encryption: a
/// corrupt or inaccessible database can have the same bytes.
pub(super) fn check_plaintext_artifacts(live_db_readable: bool) -> Check {
    let db_path = db::db_path();
    let Some(data_dir) = db_path.parent().map(Path::to_path_buf) else {
        return Check::new(
            "Plaintext residue",
            Status::Warn,
            "cannot scan plaintext residue because the database path has no parent directory",
        );
    };
    check_plaintext_artifacts_in(&data_dir, &db_path, live_db_readable)
}

fn check_plaintext_artifacts_in(data_dir: &Path, db_path: &Path, live_db_readable: bool) -> Check {
    let mut plaintext = Vec::new();
    let mut issues = Vec::new();
    scan_sibling_artifacts(data_dir, db_path, &mut plaintext, &mut issues);
    scan_backup_artifacts(data_dir, &mut plaintext, &mut issues);
    let live_state = inspect_live_database(db_path, live_db_readable, &mut issues);
    plaintext.sort_by(|left, right| left.path.cmp(&right.path));
    plaintext.dedup_by(|left, right| left.path == right.path);
    issues.sort();
    issues.dedup();
    if plaintext.is_empty() {
        if issues.is_empty() {
            return Check::new(
                "Plaintext residue",
                Status::Ok,
                "no plaintext database artifacts found beside the live database or in the managed backups directory tree",
            );
        }
        return Check::new(
            "Plaintext residue",
            Status::Warn,
            format!(
                "no plaintext database artifact was confirmed, but inspection was incomplete: {}",
                issues.join("; ")
            ),
        );
    }
    let artifacts = plaintext
        .iter()
        .map(|artifact| {
            format!(
                "{} ({:.1} MB)",
                artifact.path.display(),
                artifact.size_bytes as f64 / 1_048_576.0
            )
        })
        .collect::<Vec<_>>()
        .join(", ");
    let inspection_issues = if issues.is_empty() {
        String::new()
    } else {
        format!(" Inspection issue(s): {}.", issues.join("; "))
    };
    let detail = format!(
        "confirmed plaintext database artifact(s): {artifacts}. Live database is {}.{inspection_issues} {}",
        live_state.description(),
        live_state.remediation()
    );
    let status = if live_state == LiveDatabaseState::Encrypted {
        Status::Fail
    } else {
        Status::Warn
    };
    Check::new("Plaintext residue", status, detail)
}

fn scan_sibling_artifacts(
    data_dir: &Path,
    db_path: &Path,
    plaintext: &mut Vec<PlaintextArtifact>,
    issues: &mut Vec<String>,
) {
    let Some(db_file_name) = db_path.file_name().and_then(OsStr::to_str) else {
        issues.push(format!(
            "cannot derive a UTF-8 database filename from {}",
            db_path.display()
        ));
        return;
    };
    let bak_name = format!("{db_file_name}.bak");
    let bak_prefix = format!("{db_file_name}.bak-");
    let enc_name = format!("{db_file_name}.enc");
    scan_directory(
        data_dir,
        false,
        false,
        false,
        |name| {
            name.to_str().is_some_and(|name| {
                name == bak_name
                    || name == enc_name
                    || name.starts_with(&bak_prefix)
                    || (name != db_file_name
                        && Path::new(name)
                            .extension()
                            .and_then(OsStr::to_str)
                            .is_some_and(|extension| {
                                ["db", "db3", "sqlite", "sqlite3"]
                                    .iter()
                                    .any(|candidate| extension.eq_ignore_ascii_case(candidate))
                            }))
            })
        },
        plaintext,
        issues,
        None,
    );
    scan_directory(
        data_dir,
        false,
        true,
        true,
        |name| name != OsStr::new(db_file_name),
        plaintext,
        issues,
        None,
    );
}

fn scan_backup_artifacts(
    data_dir: &Path,
    plaintext: &mut Vec<PlaintextArtifact>,
    issues: &mut Vec<String>,
) {
    let root = data_dir.join("backups");
    let mut directories = vec![root.clone()];
    while let Some(directory) = directories.pop() {
        scan_directory(
            &directory,
            directory == root,
            true,
            false,
            |_| true,
            plaintext,
            issues,
            Some(&mut directories),
        );
    }
}

fn scan_directory(
    directory: &Path,
    missing_ok: bool,
    ignore_child_directories: bool,
    ignore_short_files: bool,
    is_candidate: impl Fn(&OsStr) -> bool,
    plaintext: &mut Vec<PlaintextArtifact>,
    issues: &mut Vec<String>,
    mut child_directories: Option<&mut Vec<PathBuf>>,
) {
    let directory_metadata = match fs::symlink_metadata(directory) {
        Ok(metadata) => metadata,
        Err(error) if missing_ok && error.kind() == io::ErrorKind::NotFound => return,
        Err(error) => {
            issues.push(format!(
                "cannot inspect directory {}: {error}",
                directory.display()
            ));
            return;
        }
    };
    if directory_metadata.file_type().is_symlink() {
        issues.push(format!(
            "refusing to scan symbolic-link directory {}",
            directory.display()
        ));
        return;
    }
    if !directory_metadata.is_dir() {
        issues.push(format!(
            "scan path {} is not a directory",
            directory.display()
        ));
        return;
    }
    let entries = match fs::read_dir(directory) {
        Ok(entries) => entries,
        Err(error) => {
            issues.push(format!(
                "cannot enumerate directory {}: {error}",
                directory.display()
            ));
            return;
        }
    };
    for entry in entries {
        let entry = match entry {
            Ok(entry) => entry,
            Err(error) => {
                issues.push(format!(
                    "cannot enumerate an entry in {}: {error}",
                    directory.display()
                ));
                continue;
            }
        };
        if !is_candidate(&entry.file_name()) {
            continue;
        }
        if let Some(directories) = child_directories.as_deref_mut() {
            match entry.file_type() {
                Ok(file_type) if file_type.is_dir() => {
                    directories.push(entry.path());
                    continue;
                }
                Ok(_) => {}
                Err(error) => {
                    issues.push(format!(
                        "cannot determine file type for {}: {error}",
                        entry.path().display()
                    ));
                    continue;
                }
            }
        }
        inspect_candidate(
            &entry,
            ignore_child_directories,
            ignore_short_files,
            plaintext,
            issues,
        );
    }
}

fn inspect_candidate(
    entry: &DirEntry,
    ignore_directories: bool,
    ignore_short_files: bool,
    plaintext: &mut Vec<PlaintextArtifact>,
    issues: &mut Vec<String>,
) {
    let path = entry.path();
    let entry_type = match entry.file_type() {
        Ok(file_type) => file_type,
        Err(error) => {
            issues.push(format!(
                "cannot determine file type for {}: {error}",
                path.display()
            ));
            return;
        }
    };
    if entry_type.is_symlink() {
        issues.push(format!(
            "refusing to inspect symbolic-link artifact {}",
            path.display()
        ));
        return;
    }
    if entry_type.is_dir() && ignore_directories {
        return;
    }
    if !entry_type.is_file() {
        issues.push(format!(
            "artifact candidate {} is not a regular file",
            path.display()
        ));
        return;
    }
    let path_metadata = match fs::symlink_metadata(&path) {
        Ok(metadata) => metadata,
        Err(error) => {
            issues.push(format!(
                "cannot inspect metadata for {}: {error}",
                path.display()
            ));
            return;
        }
    };
    if path_metadata.file_type().is_symlink() || !path_metadata.is_file() {
        issues.push(format!(
            "artifact candidate {} changed before it could be inspected safely",
            path.display()
        ));
        return;
    }
    match inspect_regular_file(&path, &path_metadata, ignore_short_files) {
        Ok((FileHeader::Plaintext, size_bytes)) => {
            plaintext.push(PlaintextArtifact { path, size_bytes });
        }
        Ok((FileHeader::NonPlaintext, _)) => {}
        Err(error) => issues.push(error),
    }
}

fn inspect_live_database(
    db_path: &Path,
    live_db_readable: bool,
    issues: &mut Vec<String>,
) -> LiveDatabaseState {
    let path_metadata = match fs::symlink_metadata(db_path) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == io::ErrorKind::NotFound => {
            return LiveDatabaseState::Missing;
        }
        Err(error) => {
            issues.push(format!(
                "cannot inspect live database metadata {}: {error}",
                db_path.display()
            ));
            return LiveDatabaseState::Unverified;
        }
    };
    if path_metadata.file_type().is_symlink() {
        issues.push(format!(
            "refusing to inspect symbolic-link live database {}",
            db_path.display()
        ));
        return LiveDatabaseState::Unverified;
    }
    if !path_metadata.is_file() {
        issues.push(format!(
            "live database {} is not a regular file",
            db_path.display()
        ));
        return LiveDatabaseState::Unverified;
    }
    match inspect_regular_file(db_path, &path_metadata, false) {
        Ok((FileHeader::Plaintext, _)) => LiveDatabaseState::Plaintext,
        Ok((FileHeader::NonPlaintext, _)) if live_db_readable => LiveDatabaseState::Encrypted,
        Ok((FileHeader::NonPlaintext, _)) => LiveDatabaseState::Unverified,
        Err(error) => {
            issues.push(error);
            LiveDatabaseState::Unverified
        }
    }
}

fn inspect_regular_file(
    path: &Path,
    path_metadata: &Metadata,
    ignore_short_file: bool,
) -> Result<(FileHeader, u64), String> {
    let mut file =
        File::open(path).map_err(|error| format!("cannot open {}: {error}", path.display()))?;
    let opened_metadata = file
        .metadata()
        .map_err(|error| format!("cannot inspect opened file {}: {error}", path.display()))?;
    if !opened_metadata.is_file() || !same_file(path_metadata, &opened_metadata) {
        return Err(format!(
            "file {} changed before it could be inspected safely",
            path.display()
        ));
    }
    let mut header = [0_u8; SQLITE_PLAINTEXT_HEADER.len()];
    let short_file = match file.read_exact(&mut header) {
        Ok(()) => false,
        Err(error) if ignore_short_file && error.kind() == io::ErrorKind::UnexpectedEof => true,
        Err(error) => {
            return Err(if error.kind() == io::ErrorKind::UnexpectedEof {
                format!(
                    "cannot confirm {} because it is shorter than the SQLite header",
                    path.display()
                )
            } else {
                format!("cannot read header from {}: {error}", path.display())
            });
        }
    };
    let current_metadata = fs::symlink_metadata(path)
        .map_err(|error| format!("cannot recheck metadata for {}: {error}", path.display()))?;
    if current_metadata.file_type().is_symlink()
        || !current_metadata.is_file()
        || !same_file(&opened_metadata, &current_metadata)
    {
        return Err(format!(
            "file {} changed while it was being inspected",
            path.display()
        ));
    }
    let header = if !short_file && &header == SQLITE_PLAINTEXT_HEADER {
        FileHeader::Plaintext
    } else {
        FileHeader::NonPlaintext
    };
    Ok((header, opened_metadata.len()))
}

#[cfg(unix)]
fn same_file(left: &Metadata, right: &Metadata) -> bool {
    use std::os::unix::fs::MetadataExt;
    left.dev() == right.dev() && left.ino() == right.ino()
}

#[cfg(not(unix))]
fn same_file(left: &Metadata, right: &Metadata) -> bool {
    let _ = (left, right);
    true
}

#[cfg(test)]
mod regression_tests;

#[cfg(test)]
mod tests {
    use super::*;

    fn temp_dir(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "remem-plaintext-artifacts-{name}-{}-{}",
            std::process::id(),
            chrono::Utc::now().timestamp_nanos_opt().unwrap_or_default()
        ));
        fs::create_dir_all(&dir).expect("temp dir should create");
        dir
    }

    fn write_plaintext_db(path: &Path) {
        let mut content = SQLITE_PLAINTEXT_HEADER.to_vec();
        content.extend_from_slice(&[0_u8; 32]);
        fs::write(path, content).expect("plaintext fixture should write");
    }

    fn write_non_plaintext_db(path: &Path) {
        fs::write(path, [0xAB_u8; 64]).expect("non-plaintext fixture should write");
    }

    fn check(dir: &Path, live_db_readable: bool) -> Check {
        check_plaintext_artifacts_in(dir, &dir.join("remem.db"), live_db_readable)
    }

    fn cleanup(dir: &Path) {
        fs::remove_dir_all(dir).ok();
    }

    #[test]
    fn reports_ok_without_artifacts_and_excludes_sidecars_and_near_matches() {
        let dir = temp_dir("ok");
        write_non_plaintext_db(&dir.join("remem.db"));
        for name in [
            "remem.db-wal",
            "remem.db-shm",
            "remem.db-journal",
            "remem.db.bakcopy",
            "remem.db.enc-old",
            "unrelated.txt",
        ] {
            write_non_plaintext_db(&dir.join(name));
        }
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Ok);
        cleanup(&dir);
    }

    #[test]
    fn missing_data_directory_is_an_incomplete_inspection_warning() {
        let dir = temp_dir("missing-data-dir");
        let db_path = dir.join("remem.db");
        cleanup(&dir);
        let result = check_plaintext_artifacts_in(&dir, &db_path, false);
        assert_eq!(result.status, Status::Warn);
        assert!(result.detail.contains("cannot inspect directory"));
        assert!(result.detail.contains(&dir.display().to_string()));
    }

    #[test]
    fn encrypted_sibling_and_backup_artifacts_are_ok() {
        let dir = temp_dir("encrypted-artifacts");
        write_non_plaintext_db(&dir.join("remem.db"));
        write_non_plaintext_db(&dir.join("remem.db.bak"));
        write_non_plaintext_db(&dir.join("remem.db.bak-"));
        write_non_plaintext_db(&dir.join("remem.db.enc"));
        fs::create_dir(dir.join("backups")).unwrap();
        write_non_plaintext_db(&dir.join("backups").join("backup.sqlite"));
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Ok);
        cleanup(&dir);
    }

    #[test]
    fn scans_only_exact_sibling_artifact_names_in_stable_order() {
        let dir = temp_dir("siblings");
        write_non_plaintext_db(&dir.join("remem.db"));
        for name in [
            "remem.db.enc",
            "remem.db.bak-z",
            "remem.db.bak",
            "remem.db.bak-a",
        ] {
            write_plaintext_db(&dir.join(name));
        }
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Fail);
        let positions = [
            "remem.db.bak (",
            "remem.db.bak-a (",
            "remem.db.bak-z (",
            "remem.db.enc (",
        ]
        .map(|needle| result.detail.find(needle).expect("artifact should appear"));
        assert!(positions.windows(2).all(|pair| pair[0] < pair[1]));
        cleanup(&dir);
    }

    #[test]
    fn scans_default_and_custom_files_in_backups_but_ignores_encrypted_backup() {
        let dir = temp_dir("backups");
        write_non_plaintext_db(&dir.join("remem.db"));
        let backups = dir.join("backups");
        fs::create_dir(&backups).unwrap();
        write_plaintext_db(&backups.join("remem-backup-20260728-120000.sqlite"));
        write_plaintext_db(&backups.join("custom-name.sqlite"));
        write_non_plaintext_db(&backups.join("encrypted.sqlite"));
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Fail);
        assert!(result.detail.contains("custom-name.sqlite"));
        assert!(result
            .detail
            .contains("remem-backup-20260728-120000.sqlite"));
        assert!(!result.detail.contains("encrypted.sqlite"));
        cleanup(&dir);
    }

    #[test]
    fn scans_nested_backup_destinations() {
        let dir = temp_dir("nonrecursive");
        write_non_plaintext_db(&dir.join("remem.db"));
        let nested = dir.join("backups").join("nested");
        fs::create_dir_all(&nested).unwrap();
        write_plaintext_db(&nested.join("plaintext.sqlite"));
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Fail);
        assert!(result.detail.contains("nested/plaintext.sqlite"));
        cleanup(&dir);
    }

    #[test]
    fn plaintext_artifact_fails_only_for_readable_non_plaintext_live_db() {
        let dir = temp_dir("live-encrypted");
        write_non_plaintext_db(&dir.join("remem.db"));
        write_plaintext_db(&dir.join("remem.db.bak"));
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Fail);
        assert!(result.detail.contains("encrypted and readable"));
        assert!(result.detail.contains("`remem status`"));
        assert!(result.detail.contains("`remem admin backup`"));
        assert!(result.detail.contains("create a new encrypted backup"));
        assert!(result.detail.contains("After the new backup succeeds"));
        assert!(result.detail.contains("did not delete any files"));
        assert!(result.detail.contains("alone does not protect"));
        assert!(!result.detail.contains("`remem encrypt`"));
        cleanup(&dir);
    }

    #[test]
    fn artifact_with_plaintext_live_db_warns_even_if_connection_is_readable() {
        let dir = temp_dir("live-plaintext");
        write_plaintext_db(&dir.join("remem.db"));
        write_plaintext_db(&dir.join("remem.db.enc"));
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Warn);
        let detail = result.detail.as_str();
        assert!(detail.contains("Live database is plaintext"));
        assert!(detail.contains("every existing blocker has been preserved successfully"));
        assert!(detail.contains("expected to remain `Fail`"));
        assert!(detail.contains("Only after that confirmation"));
        assert!(detail.contains("rerun `remem doctor` until Plaintext residue passes"));
        assert!(detail.contains("did not delete any files"));
        assert!(detail.contains("alone does not protect"));
        cleanup(&dir);
    }

    #[test]
    fn artifact_with_missing_live_db_warns() {
        let dir = temp_dir("live-missing");
        write_plaintext_db(&dir.join("remem.db.bak"));
        let result = check(&dir, false);
        assert_eq!(result.status, Status::Warn);
        let detail = result.detail.as_str();
        assert!(detail.contains("Live database is missing"));
        assert!(detail.contains("Do not create a new backup or delete any listed copy yet"));
        assert!(detail.contains("restore the missing live database"));
        assert!(detail.contains("After `remem status` succeeds"));
        assert!(
            detail.contains("`remem doctor` reports the live database as encrypted and readable")
        );
        assert!(detail.contains("did not delete any files"));
        assert!(detail.contains("alone does not protect"));
        cleanup(&dir);
    }

    #[test]
    fn artifact_with_unreadable_non_plaintext_live_db_warns() {
        let dir = temp_dir("live-unverified");
        write_non_plaintext_db(&dir.join("remem.db"));
        write_plaintext_db(&dir.join("remem.db.bak"));
        let result = check(&dir, false);
        assert_eq!(result.status, Status::Warn);
        let detail = result.detail.as_str();
        assert!(detail.contains("Live database is unverified"));
        assert!(detail.contains("Do not create a new backup or delete any listed copy yet"));
        assert!(
            detail.contains("repair the live database and resolve any key or readability problem")
        );
        assert!(detail.contains("After `remem status` succeeds"));
        assert!(
            detail.contains("`remem doctor` reports the live database as encrypted and readable")
        );
        assert!(detail.contains("did not delete any files"));
        assert!(detail.contains("alone does not protect"));
        cleanup(&dir);
    }

    #[test]
    fn short_live_db_is_unverified_and_errors_are_aggregated_with_plaintext() {
        let dir = temp_dir("short-live");
        fs::write(dir.join("remem.db"), b"short").unwrap();
        write_plaintext_db(&dir.join("remem.db.bak"));
        fs::write(dir.join("remem.db.enc"), b"also short").unwrap();
        let result = check(&dir, false);
        assert_eq!(result.status, Status::Warn);
        assert!(result.detail.contains("remem.db.bak"));
        assert!(result.detail.contains("remem.db.enc"));
        assert!(result.detail.contains("remem.db"));
        assert!(result.detail.contains("Inspection issue(s)"));
        cleanup(&dir);
    }

    #[test]
    fn non_directory_backups_path_warns_without_hiding_plaintext() {
        let dir = temp_dir("bad-backups");
        write_non_plaintext_db(&dir.join("remem.db"));
        write_plaintext_db(&dir.join("remem.db.bak"));
        fs::write(dir.join("backups"), b"not a directory").unwrap();
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Fail);
        assert!(result.detail.contains("remem.db.bak"));
        assert!(result.detail.contains("is not a directory"));
        cleanup(&dir);
    }

    #[cfg(unix)]
    #[test]
    fn symbolic_link_artifact_is_not_followed_and_warns() {
        use std::os::unix::fs::symlink;

        let dir = temp_dir("symlink");
        write_non_plaintext_db(&dir.join("remem.db"));
        fs::create_dir(dir.join("targets")).unwrap();
        let target = dir.join("targets/target.sqlite");
        write_plaintext_db(&target);
        fs::create_dir(dir.join("backups")).unwrap();
        symlink(&target, dir.join("backups").join("linked.sqlite")).unwrap();
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Warn);
        assert!(result.detail.contains("symbolic-link artifact"));
        assert!(!result
            .detail
            .contains("confirmed plaintext database artifact(s)"));
        cleanup(&dir);
    }

    #[cfg(unix)]
    #[test]
    fn symbolic_link_error_is_reported_alongside_confirmed_plaintext() {
        use std::os::unix::fs::symlink;

        let dir = temp_dir("symlink-and-plaintext");
        write_non_plaintext_db(&dir.join("remem.db"));
        write_plaintext_db(&dir.join("remem.db.bak"));
        fs::create_dir(dir.join("targets")).unwrap();
        let target = dir.join("targets/target.sqlite");
        write_plaintext_db(&target);
        fs::create_dir(dir.join("backups")).unwrap();
        symlink(&target, dir.join("backups").join("linked.sqlite")).unwrap();
        let result = check(&dir, true);
        assert_eq!(result.status, Status::Fail);
        assert!(result.detail.contains("remem.db.bak"));
        assert!(result.detail.contains("symbolic-link artifact"));
        cleanup(&dir);
    }
}