nabu-core 0.1.7

Core storage, indexing, and search for nabu: append-only JSONL capture and a rebuildable SQLite FTS5 index for coding-agent history.
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
//! Memory-folder capture and retrieval: scans each tool's native memory
//! folders (claude `projects/<id>/memory/`, codex `memories/`), appends each
//! file to the raw store as a `memory.file` event, and serves the captured
//! files back with raw citations.
//!
//! Memory files are first-class events: they are searchable through the same
//! FTS/embedding pipeline, dedupe on content (an unchanged file never appends
//! a second event), and read surfaces (`get_memory`) hydrate content from the
//! canonical raw store exactly like session payloads. OpenCode has no native
//! memory folder, so its discovery is intentionally empty.

use crate::{
    append_prepared_events, open_index, payload_for_raw_pointer, sanitize_session_id,
    CanonicalType, Error, MemoryFileContent, MemoryFileSummary, MemoryListPage, MemorySyncReport,
    NotFound, Result, Source, Tool, SCHEMA_VERSION,
};
use rusqlite::OptionalExtension;
use serde_json::json;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;

/// Reserved session-id prefix for memory pseudo-sessions. Native tool session
/// ids never use this prefix, so memory history cannot collide with real
/// sessions in the sessions table or raw-file namespace.
pub const MEMORY_SESSION_PREFIX: &str = "memory:";

/// Soft cap on a single memory file's on-disk size. Larger files are skipped
/// at sync time so a pathological multi-MB note cannot bloat the raw store
/// and FTS index in one pass.
pub const MAX_MEMORY_FILE_BYTES: u64 = 1_048_576;

/// Fixed advisory attached to every memory list/get response. Captured memory
/// is a snapshot of each tool's own folders at sync time — not live state and
/// not authoritative project truth.
pub const MEMORY_STALENESS_ADVISORY: &str = "Captured tool memory is a point-in-time snapshot of each tool's own memory folders. It can be stale relative to the live folders and to current project truth — treat it as historical context, not ground truth.";

/// Session id of the memory pseudo-session holding one file's captured
/// history: `memory:{project-slug}` for a claude project folder, or
/// `memory:global` for codex's global folder (and any tool without a project).
fn memory_session_id(tool: Tool, project: Option<&str>) -> String {
    match (tool, project) {
        (Tool::Claude, Some(project)) if !project.is_empty() => {
            format!("{MEMORY_SESSION_PREFIX}{project}")
        }
        _ => format!("{MEMORY_SESSION_PREFIX}global"),
    }
}

/// Validate tool/project/name identity rules shared by every get surface.
fn validate_memory_identity(tool: Tool, project: Option<&str>, name: &str) -> Result<()> {
    if name.is_empty() {
        return Err(Error::Validation(
            "memory file name must not be empty".to_string(),
        ));
    }
    match (tool, project) {
        (Tool::Claude, Some(project)) if !project.is_empty() => Ok(()),
        (Tool::Claude, _) => Err(Error::Validation(
            "project is required for claude memories (the projects/<id> folder name)".to_string(),
        )),
        (Tool::Codex | Tool::Opencode | Tool::Pi, Some(project)) if !project.is_empty() => Err(
            Error::Validation(format!("project must be omitted for {tool} memories")),
        ),
        (Tool::Codex | Tool::Opencode | Tool::Pi, _) => Ok(()),
    }
}

/// A memory file discovered in a tool's native folders. `project` is the
/// claude project slug (folder name under `projects/`); codex memories are
/// global, so their project is `None`. `name` is the path relative to the
/// memory root (`MEMORY.md`, `sub/deep.md`), always `/`-separated.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemoryFileMeta {
    pub tool: Tool,
    pub project: Option<String>,
    pub name: String,
    pub native_path: PathBuf,
    pub size: u64,
    pub modified_at: Option<String>,
}

fn home_dir() -> Result<PathBuf> {
    std::env::var_os("HOME")
        .map(PathBuf::from)
        .ok_or(Error::HomeUnavailable)
}

/// Claude's projects root: `$CLAUDE_CONFIG_DIR/projects`, else `~/.claude/projects`.
fn claude_projects_root() -> Result<PathBuf> {
    if let Some(config_dir) = std::env::var_os("CLAUDE_CONFIG_DIR") {
        return Ok(PathBuf::from(config_dir).join("projects"));
    }
    Ok(home_dir()?.join(".claude").join("projects"))
}

/// Codex's home: `$CODEX_HOME`, else `~/.codex`.
fn codex_home() -> Result<PathBuf> {
    if let Some(codex_home) = std::env::var_os("CODEX_HOME") {
        return Ok(PathBuf::from(codex_home));
    }
    Ok(home_dir()?.join(".codex"))
}

/// The native memory folders for a tool, each with its project identity
/// (claude project slug; `None` for codex's global folder). OpenCode has no
/// memory-folder concept today, so it contributes no roots; callers see an
/// empty list rather than an error, keeping the three-tool surface total.
fn memory_roots(tool: Tool) -> Result<Vec<(PathBuf, Option<String>)>> {
    match tool {
        Tool::Claude => {
            let projects = claude_projects_root()?;
            if !projects.is_dir() {
                return Ok(Vec::new());
            }
            let mut roots = Vec::new();
            let entries = fs::read_dir(&projects).map_err(|source| Error::Io {
                path: projects.clone(),
                source,
            })?;
            for entry in entries {
                let entry = entry.map_err(|source| Error::Io {
                    path: projects.clone(),
                    source,
                })?;
                let project_dir = entry.path();
                if !project_dir.is_dir() {
                    continue;
                }
                let memory = project_dir.join("memory");
                if memory.is_dir() {
                    roots.push((
                        memory,
                        project_dir
                            .file_name()
                            .map(|name| name.to_string_lossy().into_owned()),
                    ));
                }
            }
            Ok(roots)
        }
        Tool::Codex => {
            let memories = codex_home()?.join("memories");
            if memories.is_dir() {
                Ok(vec![(memories, None)])
            } else {
                Ok(Vec::new())
            }
        }
        Tool::Opencode | Tool::Pi => Ok(Vec::new()),
    }
}

/// Recursively collect regular files under `dir`, skipping hidden entries and
/// symlinks (symlinks are not followed, so a link out of the memory root
/// cannot pull foreign paths into the capture).
fn collect_memory_files(dir: &Path, output: &mut Vec<PathBuf>) -> Result<()> {
    let entries = fs::read_dir(dir).map_err(|source| Error::Io {
        path: dir.to_path_buf(),
        source,
    })?;
    for entry in entries {
        let entry = entry.map_err(|source| Error::Io {
            path: dir.to_path_buf(),
            source,
        })?;
        let path = entry.path();
        let Some(file_name) = path.file_name().and_then(|name| name.to_str()) else {
            continue;
        };
        if file_name.starts_with('.') {
            continue;
        }
        let metadata = match fs::symlink_metadata(&path) {
            Ok(metadata) => metadata,
            Err(source) if source.kind() == std::io::ErrorKind::NotFound => continue,
            Err(source) => {
                return Err(Error::Io {
                    path: path.clone(),
                    source,
                })
            }
        };
        if metadata.file_type().is_symlink() {
            continue;
        }
        if metadata.is_dir() {
            collect_memory_files(&path, output)?;
        } else if metadata.is_file() {
            output.push(path);
        }
    }
    Ok(())
}

/// Root-relative memory file name with stable `/` separators. Nested files
/// keep their path (`sub/deep.md`) so list/get/dedupe identity is unambiguous.
/// Returns `None` when `path` is not under `root` (e.g. after a race); callers
/// skip those entries rather than falling back to a bare basename.
fn relative_memory_name(root: &Path, path: &Path) -> Option<String> {
    let relative = path.strip_prefix(root).ok()?;
    let name = relative.to_string_lossy().replace('\\', "/");
    if name.is_empty() {
        None
    } else {
        Some(name)
    }
}

fn modified_at_rfc3339(system_time: std::time::SystemTime) -> Option<String> {
    OffsetDateTime::from(system_time).format(&Rfc3339).ok()
}

/// Discover every memory file a tool's native folders currently hold.
pub fn discover_memory_files(tool: Tool) -> Result<Vec<MemoryFileMeta>> {
    let mut metas = Vec::new();
    for (root, project) in memory_roots(tool)? {
        let mut files = Vec::new();
        collect_memory_files(&root, &mut files)?;
        for path in files {
            let metadata = match fs::metadata(&path) {
                Ok(metadata) => metadata,
                // A memory file removed between the walk and the stat is not
                // an error; it simply is not part of this pass.
                Err(source) if source.kind() == std::io::ErrorKind::NotFound => continue,
                Err(source) => {
                    return Err(Error::Io {
                        path: path.clone(),
                        source,
                    })
                }
            };
            let size = metadata.len();
            // Oversized files are skipped at discovery so sync never reads them.
            if size > MAX_MEMORY_FILE_BYTES {
                continue;
            }
            let Some(name) = relative_memory_name(&root, &path) else {
                continue;
            };
            metas.push(MemoryFileMeta {
                tool,
                project: project.clone(),
                name,
                native_path: path.clone(),
                size,
                modified_at: metadata.modified().ok().and_then(modified_at_rfc3339),
            });
        }
    }
    Ok(metas)
}

/// Capture one tool's current memory files into the raw store. Each file
/// becomes a `memory.file` event in its memory pseudo-session; the dedupe key
/// is content-derived, so an unchanged file appends nothing. Non-UTF-8
/// (binary) files are skipped.
pub fn sync_memory(home: &Path, tool: Tool) -> Result<MemorySyncReport> {
    let synced_at = OffsetDateTime::now_utc().format(&Rfc3339)?;
    let discovered = discover_memory_files(tool)?;
    let mut discovered_count = 0usize;
    let mut events = Vec::with_capacity(discovered.len());
    for meta in discovered {
        let bytes = match fs::read(&meta.native_path) {
            Ok(bytes) => bytes,
            // File vanished since discovery: skip, it is not part of this pass.
            Err(source) if source.kind() == std::io::ErrorKind::NotFound => continue,
            Err(source) => {
                return Err(Error::Io {
                    path: meta.native_path.clone(),
                    source,
                })
            }
        };
        // Re-check size after read so a file that grew past the cap between
        // discover and open is still refused.
        if bytes.len() as u64 > MAX_MEMORY_FILE_BYTES {
            continue;
        }
        let content = match String::from_utf8(bytes) {
            Ok(text) => text,
            // Binary / non-UTF-8 memory files are not captured.
            Err(_) => continue,
        };
        discovered_count += 1;
        let session_id = memory_session_id(tool, meta.project.as_deref());
        let size = content.len() as u64;
        let payload = json!({
            "name": meta.name,
            "project": meta.project,
            "native_path": meta.native_path.display().to_string(),
            "content": content,
            "size": size,
            "modified_at": meta.modified_at,
            "synced_at": synced_at,
        });
        let mut envelope = crate::EventEnvelope {
            schema_version: SCHEMA_VERSION,
            captured_at: synced_at.clone(),
            tool,
            tool_version: None,
            session_id,
            filename_session_id: String::new(),
            turn_id: None,
            message_id: None,
            project_root: None,
            cwd: None,
            source: Source::MemorySync,
            source_event_type: "memory.file".to_string(),
            canonical_type: CanonicalType::MemoryFile,
            source_event_id: None,
            dedupe_key: String::new(),
            sequence: None,
            raw_file: None,
            raw_offset: None,
            payload,
            payload_ref: None,
        };
        // Sanitize after session_id is known; validate() in the append path
        // checks the two agree.
        envelope.filename_session_id = sanitize_session_id(&envelope.session_id);
        events.push(envelope);
    }

    let appended = if events.is_empty() {
        0
    } else {
        append_prepared_events(home, events)?
            .into_iter()
            .filter(|report| report.appended)
            .count()
    };
    Ok(MemorySyncReport {
        tool,
        discovered: discovered_count,
        appended,
    })
}

fn memory_summary_from_row(
    row: &rusqlite::Row<'_>,
) -> rusqlite::Result<(MemoryFileSummary, String, i64, Option<i64>)> {
    let tool_text: String = row.get(0)?;
    let tool = Tool::from_str(&tool_text).map_err(|_| rusqlite::Error::InvalidQuery)?;
    let summary = MemoryFileSummary {
        tool,
        session_id: row.get(1)?,
        project: row.get(2)?,
        name: row.get(3)?,
        native_path: row.get(4)?,
        size: row.get(5)?,
        modified_at: row.get(6)?,
        captured_at: row.get(7)?,
        raw_file: row.get(8)?,
        raw_line: row.get(9)?,
        raw_offset: row.get(10)?,
    };
    Ok((
        summary.clone(),
        summary.raw_file.clone(),
        summary.raw_line,
        summary.raw_offset,
    ))
}

/// List captured memory files, newest capture first. Returns the latest
/// captured version of each file (a file edited since its last sync appears
/// once, with its most recent event). Files removed from the tool's folders
/// remain listed: the capture is durable history, not a mirror. Identity is
/// `(tool, project, name)` where `name` is the root-relative path. The page
/// always carries [`MEMORY_STALENESS_ADVISORY`].
pub fn list_memories(home: &Path, tool: Option<Tool>, limit: usize) -> Result<MemoryListPage> {
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path)?;
    let mut statement = conn
        .prepare(
            "SELECT
               m.tool,
               m.session_id,
               m.project,
               m.name,
               m.native_path,
               m.size,
               m.modified_at,
               m.captured_at,
               e.raw_file,
               e.raw_line,
               e.raw_offset
             FROM memories m
             JOIN events e ON e.id = m.event_id
             WHERE (?1 IS NULL OR m.tool = ?1)
               AND m.id = (
                 SELECT MAX(m2.id) FROM memories m2
                 WHERE m2.tool = m.tool
                   AND m2.name = m.name
                   AND m2.project IS m.project
               )
             ORDER BY m.captured_at DESC, m.id DESC
             LIMIT ?2",
        )
        .map_err(|source| Error::Sqlite {
            path: db_path.clone(),
            source,
        })?;
    let rows = statement
        .query_map(
            (tool.map(Tool::as_str), limit.clamp(1, 1000) as i64),
            |row| {
                let (summary, _, _, _) = memory_summary_from_row(row)?;
                Ok(summary)
            },
        )
        .map_err(|source| Error::Sqlite {
            path: db_path.clone(),
            source,
        })?;
    let mut memories = Vec::new();
    for row in rows {
        memories.push(row.map_err(|source| Error::Sqlite {
            path: db_path.clone(),
            source,
        })?);
    }
    Ok(MemoryListPage {
        memories,
        advisory: MEMORY_STALENESS_ADVISORY.to_string(),
    })
}

/// Read one captured memory file by its tool identity, hydrating content from
/// the canonical raw store. For claude, `project` is required (memory is
/// per-project); for codex/opencode it must be `None`. `name` is the
/// root-relative path (`MEMORY.md`, `sub/deep.md`).
pub fn get_memory(
    home: &Path,
    tool: Tool,
    project: Option<&str>,
    name: &str,
) -> Result<MemoryFileContent> {
    validate_memory_identity(tool, project, name)?;
    let db_path = home.join("index").join("harness.db");
    let conn = open_index(&db_path)?;
    let mut statement = conn
        .prepare(
            "SELECT
               m.tool,
               m.session_id,
               m.project,
               m.name,
               m.native_path,
               m.size,
               m.modified_at,
               m.captured_at,
               e.raw_file,
               e.raw_line,
               e.raw_offset
             FROM memories m
             JOIN events e ON e.id = m.event_id
             WHERE m.tool = ?1 AND m.name = ?2 AND m.project IS ?3
             ORDER BY m.id DESC
             LIMIT 1",
        )
        .map_err(|source| Error::Sqlite {
            path: db_path.clone(),
            source,
        })?;
    let row = statement
        .query_row((tool.as_str(), name, project), |row| {
            memory_summary_from_row(row)
        })
        .optional()
        .map_err(|source| Error::Sqlite {
            path: db_path.clone(),
            source,
        })?;
    let Some((summary, raw_file, raw_line, raw_offset)) = row else {
        return Err(Error::NotFound(NotFound::Memory {
            tool: tool.as_str().to_string(),
            project: project.map(str::to_string),
            name: name.to_string(),
        }));
    };

    let payload = payload_for_raw_pointer(&raw_file, raw_line, raw_offset)?;
    let content = payload
        .get("content")
        .and_then(serde_json::Value::as_str)
        .unwrap_or_default()
        .to_string();
    Ok(MemoryFileContent {
        tool: summary.tool,
        project: summary.project,
        name: summary.name,
        native_path: summary.native_path,
        size: summary.size,
        modified_at: summary.modified_at,
        captured_at: summary.captured_at,
        session_id: summary.session_id,
        raw_file: raw_file.to_string(),
        raw_line,
        raw_offset,
        content,
        advisory: MEMORY_STALENESS_ADVISORY.to_string(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{index_once, init_home, search_history};
    use std::sync::Mutex;
    use tempfile::tempdir;

    /// Serialize env-mutating tests (set_var is process-global) and restore
    /// the prior values on drop.
    struct EnvGuard {
        _lock: std::sync::MutexGuard<'static, ()>,
        vars: Vec<(&'static str, Option<std::ffi::OsString>)>,
    }

    impl EnvGuard {
        fn set(vars: &[(&'static str, &std::ffi::OsStr)]) -> EnvGuard {
            static LOCK: Mutex<()> = Mutex::new(());
            let _lock = LOCK.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            let previous = vars
                .iter()
                .map(|(name, _)| (*name, std::env::var_os(name)))
                .collect();
            for (name, value) in vars {
                std::env::set_var(name, value);
            }
            EnvGuard {
                _lock,
                vars: previous,
            }
        }
    }

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            for (name, value) in &self.vars {
                match value {
                    Some(value) => std::env::set_var(name, value),
                    None => std::env::remove_var(name),
                }
            }
        }
    }

    #[test]
    fn discovery_honors_tool_env_roots_and_skips_hidden() {
        let temp = tempdir().unwrap();
        let claude_config = temp.path().join("claude-config");
        let projects = claude_config.join("projects");
        let memory_dir = projects.join("-Users-me-project").join("memory");
        fs::create_dir_all(&memory_dir).unwrap();
        fs::write(memory_dir.join("note.md"), "# note").unwrap();
        fs::write(memory_dir.join(".hidden.md"), "hidden").unwrap();
        fs::create_dir_all(memory_dir.join("sub")).unwrap();
        fs::write(memory_dir.join("sub").join("deep.md"), "deep").unwrap();

        let codex_home = temp.path().join("codex");
        let codex_memories = codex_home.join("memories");
        fs::create_dir_all(&codex_memories).unwrap();
        fs::write(codex_memories.join("prefs.md"), "prefs").unwrap();

        let _guard = EnvGuard::set(&[
            ("HOME", temp.path().as_os_str()),
            ("CLAUDE_CONFIG_DIR", claude_config.as_os_str()),
            ("CODEX_HOME", codex_home.as_os_str()),
        ]);

        let claude = discover_memory_files(Tool::Claude).unwrap();
        let mut names: Vec<_> = claude
            .iter()
            .map(|meta| (meta.name.clone(), meta.project.clone()))
            .collect();
        names.sort();
        assert_eq!(
            names,
            vec![
                ("note.md".to_string(), Some("-Users-me-project".to_string())),
                (
                    "sub/deep.md".to_string(),
                    Some("-Users-me-project".to_string())
                ),
            ]
        );

        let codex = discover_memory_files(Tool::Codex).unwrap();
        assert_eq!(codex.len(), 1);
        assert_eq!(codex[0].name, "prefs.md");
        assert_eq!(codex[0].project, None);

        assert!(discover_memory_files(Tool::Opencode).unwrap().is_empty());
    }

    #[test]
    fn sync_appends_then_dedupes_unchanged_and_indexes() {
        let temp = tempdir().unwrap();
        let home = temp.path().join("home");
        init_home(&home).unwrap();

        let claude_config = temp.path().join("claude-config");
        let memory_dir = claude_config
            .join("projects")
            .join("-Users-me-project")
            .join("memory");
        fs::create_dir_all(&memory_dir).unwrap();
        let memory_file = memory_dir.join("MEMORY.md");
        fs::write(&memory_file, "# remember the salt is immutable").unwrap();
        let _guard = EnvGuard::set(&[
            ("HOME", temp.path().as_os_str()),
            ("CLAUDE_CONFIG_DIR", claude_config.as_os_str()),
            ("CODEX_HOME", temp.path().join("codex").as_os_str()),
        ]);

        let first = sync_memory(&home, Tool::Claude).unwrap();
        assert_eq!(first.discovered, 1);
        assert_eq!(first.appended, 1);

        // Unchanged file: no new event.
        let second = sync_memory(&home, Tool::Claude).unwrap();
        assert_eq!(second.discovered, 1);
        assert_eq!(second.appended, 0);

        // Changed file: one new event appended (append-only history).
        fs::write(&memory_file, "# remember the salt is immutable and frozen").unwrap();
        let third = sync_memory(&home, Tool::Claude).unwrap();
        assert_eq!(third.discovered, 1);
        assert_eq!(third.appended, 1);

        index_once(&home).unwrap();

        let page = list_memories(&home, Some(Tool::Claude), 10).unwrap();
        assert_eq!(page.advisory, MEMORY_STALENESS_ADVISORY);
        assert_eq!(page.memories.len(), 1);
        assert_eq!(page.memories[0].name, "MEMORY.md");
        assert_eq!(
            page.memories[0].project.as_deref(),
            Some("-Users-me-project")
        );
        assert_eq!(page.memories[0].session_id, "memory:-Users-me-project");
        assert_eq!(
            page.memories[0].native_path,
            memory_file.display().to_string()
        );

        let content =
            get_memory(&home, Tool::Claude, Some("-Users-me-project"), "MEMORY.md").unwrap();
        assert!(content.content.contains("frozen"));
        assert_eq!(content.advisory, MEMORY_STALENESS_ADVISORY);
        assert_eq!(content.raw_line, 2);
        assert!(content
            .raw_file
            .ends_with("claude_memory_-Users-me-project.jsonl"));
    }

    #[test]
    fn nested_memory_names_are_addressable_and_distinct() {
        let temp = tempdir().unwrap();
        let home = temp.path().join("home");
        init_home(&home).unwrap();

        let claude_config = temp.path().join("claude-config");
        let memory_dir = claude_config
            .join("projects")
            .join("-Users-me-project")
            .join("memory");
        fs::create_dir_all(memory_dir.join("sub")).unwrap();
        fs::write(memory_dir.join("note.md"), "top-level note").unwrap();
        fs::write(memory_dir.join("sub").join("note.md"), "nested note").unwrap();
        let _guard = EnvGuard::set(&[
            ("HOME", temp.path().as_os_str()),
            ("CLAUDE_CONFIG_DIR", claude_config.as_os_str()),
            ("CODEX_HOME", temp.path().join("codex").as_os_str()),
        ]);

        sync_memory(&home, Tool::Claude).unwrap();
        index_once(&home).unwrap();

        let page = list_memories(&home, Some(Tool::Claude), 10).unwrap();
        let mut names: Vec<_> = page.memories.iter().map(|m| m.name.clone()).collect();
        names.sort();
        assert_eq!(
            names,
            vec!["note.md".to_string(), "sub/note.md".to_string()]
        );

        let top = get_memory(&home, Tool::Claude, Some("-Users-me-project"), "note.md").unwrap();
        assert!(top.content.contains("top-level"));
        let nested = get_memory(
            &home,
            Tool::Claude,
            Some("-Users-me-project"),
            "sub/note.md",
        )
        .unwrap();
        assert!(nested.content.contains("nested"));
    }

    #[test]
    fn sync_skips_binary_and_oversized_memory_files() {
        let temp = tempdir().unwrap();
        let home = temp.path().join("home");
        init_home(&home).unwrap();

        let codex_home = temp.path().join("codex");
        let memories = codex_home.join("memories");
        fs::create_dir_all(&memories).unwrap();
        fs::write(memories.join("ok.md"), "keep me").unwrap();
        fs::write(memories.join("bin.dat"), [0xff, 0xfe, 0x00, 0x01]).unwrap();
        let oversized = vec![b'a'; MAX_MEMORY_FILE_BYTES as usize + 1];
        fs::write(memories.join("huge.md"), &oversized).unwrap();
        let _guard = EnvGuard::set(&[
            ("HOME", temp.path().as_os_str()),
            ("CODEX_HOME", codex_home.as_os_str()),
            ("CLAUDE_CONFIG_DIR", temp.path().join("claude").as_os_str()),
        ]);

        let report = sync_memory(&home, Tool::Codex).unwrap();
        assert_eq!(report.discovered, 1);
        assert_eq!(report.appended, 1);
        index_once(&home).unwrap();
        let listed = list_memories(&home, Some(Tool::Codex), 10).unwrap();
        assert_eq!(listed.memories.len(), 1);
        assert_eq!(listed.memories[0].name, "ok.md");
        assert_eq!(listed.advisory, MEMORY_STALENESS_ADVISORY);
    }

    #[test]
    fn memory_events_are_searchable_and_distinct_from_sessions() {
        let temp = tempdir().unwrap();
        let home = temp.path().join("home");
        init_home(&home).unwrap();

        let codex_home = temp.path().join("codex");
        let memories = codex_home.join("memories");
        fs::create_dir_all(&memories).unwrap();
        fs::write(memories.join("prefs.md"), "prefer cargo test over miri").unwrap();
        let _guard = EnvGuard::set(&[
            ("HOME", temp.path().as_os_str()),
            ("CODEX_HOME", codex_home.as_os_str()),
            ("CLAUDE_CONFIG_DIR", temp.path().join("claude").as_os_str()),
        ]);

        sync_memory(&home, Tool::Codex).unwrap();
        index_once(&home).unwrap();

        let hits = search_history(&home, "miri", 10).unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].canonical_type, "memory.file");
        assert_eq!(hits[0].tool, Tool::Codex);
        assert_eq!(hits[0].session_id, "memory:global");
        assert!(hits[0].session_id.starts_with(MEMORY_SESSION_PREFIX));

        // Memory pseudo-sessions are not sessions: list_sessions stays clean.
        let sessions = crate::list_sessions(&home, None, None, None, 10).unwrap();
        assert!(sessions.is_empty());
    }

    #[test]
    fn get_memory_rejects_wrong_project_identity() {
        let temp = tempdir().unwrap();
        let home = temp.path().join("home");
        init_home(&home).unwrap();

        let claude_config = temp.path().join("claude-config");
        let memory_dir = claude_config
            .join("projects")
            .join("-Users-me-project")
            .join("memory");
        fs::create_dir_all(&memory_dir).unwrap();
        fs::write(memory_dir.join("MEMORY.md"), "content").unwrap();
        let _guard = EnvGuard::set(&[
            ("HOME", temp.path().as_os_str()),
            ("CLAUDE_CONFIG_DIR", claude_config.as_os_str()),
            ("CODEX_HOME", temp.path().join("codex").as_os_str()),
        ]);

        sync_memory(&home, Tool::Claude).unwrap();
        index_once(&home).unwrap();

        let result = get_memory(
            &home,
            Tool::Claude,
            Some("-Users-other-project"),
            "MEMORY.md",
        );
        assert!(matches!(result, Err(Error::NotFound(_))));

        // Core enforces project rules for non-claude tools.
        let bad = get_memory(&home, Tool::Codex, Some("x"), "MEMORY.md");
        assert!(matches!(bad, Err(Error::Validation(_))));
    }

    #[test]
    fn sync_reports_empty_for_tools_without_memory_folders() {
        let temp = tempdir().unwrap();
        let home = temp.path().join("home");
        init_home(&home).unwrap();
        let _guard = EnvGuard::set(&[
            ("HOME", temp.path().as_os_str()),
            ("CLAUDE_CONFIG_DIR", temp.path().join("claude").as_os_str()),
            ("CODEX_HOME", temp.path().join("codex").as_os_str()),
        ]);

        let report = sync_memory(&home, Tool::Opencode).unwrap();
        assert_eq!(report.discovered, 0);
        assert_eq!(report.appended, 0);
    }

    /// The pre-memory `events` table definition (source/canonical_type CHECKs
    /// without the memory values). Kept in the test so the migration is proven
    /// against the shape an existing install actually has.
    const LEGACY_EVENTS_TABLE: &str = r#"
CREATE TABLE events (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  tool TEXT NOT NULL CHECK (tool IN ('codex', 'claude', 'opencode')),
  session_id TEXT NOT NULL,
  dedupe_key TEXT NOT NULL UNIQUE,
  schema_version INTEGER NOT NULL,
  captured_at TEXT NOT NULL,
  tool_version TEXT,
  turn_id TEXT,
  message_id TEXT,
  project_root TEXT,
  cwd TEXT,
  source TEXT NOT NULL CHECK (
    source IN ('hook', 'event_stream', 'transcript_tail', 'sdk_session_store', 'backfill', 'exec_json', 'app_server')
  ),
  source_event_type TEXT NOT NULL,
  source_event_id TEXT,
  tool_invocation_id TEXT,
  canonical_type TEXT NOT NULL CHECK (
    canonical_type IN (
      'session.started', 'session.resumed', 'session.ended', 'user.message',
      'assistant.delta', 'assistant.message', 'tool.call', 'tool.result',
      'permission.requested', 'permission.replied', 'file.changed',
      'compaction.before', 'compaction.after', 'source.discontinuity', 'error'
    )
  ),
  sequence INTEGER,
  raw_file TEXT NOT NULL,
  raw_line INTEGER,
  raw_offset INTEGER,
  payload_json TEXT,
  payload_ref TEXT,
  searchable_text TEXT NOT NULL DEFAULT '',
  compaction_state TEXT NOT NULL DEFAULT 'unknown'
);
"#;

    #[test]
    fn legacy_index_rebuilds_events_table_for_memory_without_losing_rows() {
        let temp = tempdir().unwrap();
        let home = temp.path().join("home");
        let db_path = home.join("index").join("harness.db");
        fs::create_dir_all(db_path.parent().unwrap()).unwrap();

        // Build a pre-memory store: sessions + the legacy events table plus the
        // derived tables the open path indexes against, with one real event and
        // the pre-migration events indexes that DROP TABLE must restore.
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        conn.execute_batch(
            "PRAGMA foreign_keys = ON;
             CREATE TABLE sessions (
               tool TEXT NOT NULL,
               session_id TEXT NOT NULL,
               filename_session_id TEXT NOT NULL,
               project_root TEXT,
               cwd TEXT,
               started_at TEXT,
               updated_at TEXT,
               raw_file TEXT NOT NULL,
               event_count INTEGER NOT NULL DEFAULT 0,
               message_count INTEGER NOT NULL DEFAULT 0,
               tool_event_count INTEGER NOT NULL DEFAULT 0,
               compaction_count INTEGER NOT NULL DEFAULT 0,
               PRIMARY KEY (tool, session_id)
             );
             CREATE TABLE tool_events (
               id INTEGER PRIMARY KEY AUTOINCREMENT,
               event_id INTEGER NOT NULL UNIQUE,
               tool TEXT NOT NULL,
               session_id TEXT NOT NULL,
               tool_name TEXT,
               command TEXT,
               status TEXT,
               duration_ms INTEGER,
               input_text TEXT,
               output_text TEXT
             );
             CREATE TABLE compactions (
               id INTEGER PRIMARY KEY AUTOINCREMENT,
               event_id INTEGER NOT NULL UNIQUE,
               tool TEXT NOT NULL,
               session_id TEXT NOT NULL,
               trigger TEXT,
               raw_file TEXT NOT NULL,
               raw_line INTEGER,
               raw_offset INTEGER,
               created_at TEXT NOT NULL
             );
             CREATE TABLE checkpoints (
               id INTEGER PRIMARY KEY AUTOINCREMENT,
               source_tool TEXT NOT NULL,
               source_kind TEXT NOT NULL,
               source_path TEXT NOT NULL,
               source_identity TEXT,
               session_id TEXT,
               byte_offset INTEGER NOT NULL DEFAULT 0,
               source_size INTEGER NOT NULL DEFAULT 0,
               source_mtime INTEGER,
               last_line_hash TEXT,
               last_successful_import_timestamp TEXT,
               updated_at TEXT NOT NULL,
               UNIQUE (source_tool, source_kind, source_path)
             );
             CREATE TABLE schema_migrations (
               version INTEGER PRIMARY KEY,
               name TEXT NOT NULL,
               applied_at TEXT NOT NULL
             );
             CREATE TABLE metadata (
               key TEXT PRIMARY KEY,
               value TEXT NOT NULL
             );",
        )
        .unwrap();
        conn.execute_batch(LEGACY_EVENTS_TABLE).unwrap();
        conn.execute_batch(
            "CREATE INDEX idx_events_tool_session_raw ON events(tool, session_id, raw_line, raw_offset);
             CREATE INDEX idx_events_canonical_captured ON events(canonical_type, captured_at);
             CREATE INDEX idx_events_session_captured ON events(tool, session_id, captured_at);
             CREATE INDEX idx_events_tool_captured ON events(tool, captured_at);",
        )
        .unwrap();
        conn.execute_batch(
            r#"INSERT INTO sessions(tool, session_id, filename_session_id, started_at, updated_at, raw_file)
             VALUES ('codex', 'session-1', 'session-1', '2026-01-01T00:00:00Z', '2026-01-01T00:00:00Z', 'raw/codex/codex_session-1.jsonl');
             INSERT INTO events(
               tool, session_id, dedupe_key, schema_version, captured_at, source,
               source_event_type, canonical_type, raw_file, raw_line, searchable_text, compaction_state,
               payload_json
             ) VALUES (
               'codex', 'session-1', 'sha256:legacy', 1, '2026-01-01T00:00:00Z', 'hook',
               'UserPromptSubmit', 'user.message', 'raw/codex/codex_session-1.jsonl', 1, 'legacy prompt', 'none',
               '{"prompt": "legacy prompt"}'
             );"#,
        )
        .unwrap();
        drop(conn);

        // First open runs the migration (events CHECK gains memory.file).
        let conn = crate::open_index(&db_path).unwrap();
        let sql: String = conn
            .query_row(
                "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'events'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert!(sql.contains("'memory.file'"), "{sql}");
        assert!(sql.contains("'memory_sync'"), "{sql}");

        // DROP TABLE events destroys indexes; open_index must restore the full set.
        let mut index_names = conn
            .prepare(
                "SELECT name FROM sqlite_master
                 WHERE type = 'index' AND tbl_name = 'events' AND name NOT LIKE 'sqlite_%'
                 ORDER BY name",
            )
            .unwrap()
            .query_map([], |row| row.get::<_, String>(0))
            .unwrap()
            .collect::<std::result::Result<Vec<_>, _>>()
            .unwrap();
        index_names.sort();
        assert_eq!(
            index_names,
            vec![
                "idx_events_canonical_captured".to_string(),
                "idx_events_session_captured".to_string(),
                "idx_events_tool_captured".to_string(),
                "idx_events_tool_session_raw".to_string(),
            ]
        );

        // The legacy row survived with its id, and autoincrement continues.
        let (count, max_id): (i64, i64) = conn
            .query_row("SELECT COUNT(*), MAX(id) FROM events", [], |row| {
                Ok((row.get(0)?, row.get(1)?))
            })
            .unwrap();
        assert_eq!((count, max_id), (1, 1));
        let migrated_at: Option<String> = conn
            .query_row(
                "SELECT applied_at FROM schema_migrations WHERE version = 2",
                [],
                |row| row.get(0),
            )
            .optional()
            .unwrap();
        assert!(migrated_at.is_some());

        // A memory event now inserts cleanly through the new CHECK.
        conn.execute_batch(
            "INSERT INTO sessions(tool, session_id, filename_session_id, started_at, updated_at, raw_file)
             VALUES ('codex', 'memory:global', 'memory_global', '2026-01-02T00:00:00Z', '2026-01-02T00:00:00Z', 'raw/codex/codex_memory_global.jsonl');
             INSERT INTO events(
               tool, session_id, dedupe_key, schema_version, captured_at, source,
               source_event_type, canonical_type, raw_file, raw_line, searchable_text, compaction_state,
               payload_json
             ) VALUES (
               'codex', 'memory:global', 'sha256:memory-event', 1, '2026-01-02T00:00:00Z', 'memory_sync',
               'memory.file', 'memory.file', 'raw/codex/codex_memory_global.jsonl', 1, 'note', 'none',
               '{\"name\": \"note.md\", \"content\": \"note\"}'
             );",
        )
        .unwrap();
        let id: i64 = conn
            .query_row(
                "SELECT id FROM events WHERE dedupe_key = 'sha256:memory-event'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(id, 2);
        drop(conn);

        // Reopening is a no-op: the migration does not run twice.
        let conn = crate::open_index(&db_path).unwrap();
        let migration_count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM schema_migrations WHERE version = 2",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(migration_count, 1);
    }
}