anamnesis-adapter-claude-code 0.1.0

Anamnesis adapter for Claude Code (~/.claude/projects)
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
//! Filesystem walker for `~/.claude/projects/`.
//!
//! Reads ONLY directory listings and file metadata during detection;
//! content reads happen during `scan` (the import phase).
//!
//! ## JSONL recursion (PR-F, BLUEPRINT ยง18.4 F1)
//!
//! Modern Claude Code nests session files as
//! `<project>/<session-uuid>/subagents/*.jsonl`. We recurse `*.jsonl`
//! discovery up to [`MAX_JSONL_DEPTH`] levels under each project dir,
//! skipping `memory/` (handled separately) and hidden entries. Memory
//! markdown remains exactly one level deep (`<project>/memory/*.md`).

use std::path::{Path, PathBuf};

/// Maximum recursion depth for `.jsonl` discovery under a project dir.
/// Real Claude Code data nests 3 levels (`<project>/<uuid>/subagents/*.jsonl`);
/// 6 gives generous headroom without inviting accidental whole-tree walks.
const MAX_JSONL_DEPTH: usize = 6;

/// Result of scanning a single project subdirectory.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProjectScan {
    /// Project directory (`~/.claude/projects/<encoded-path>`).
    pub project_dir: PathBuf,
    /// `*.jsonl` files (typically conversation sessions).
    pub jsonl_files: Vec<PathBuf>,
    /// `memory/*.md` files except `MEMORY.md` (index file, never imported).
    pub memory_files: Vec<PathBuf>,
}

/// Walk `projects_root` and return one `ProjectScan` per project subdir.
///
/// - Skips hidden entries and non-directories under the root.
/// - Returns an empty `Vec` if `projects_root` does not exist; the caller
///   decides whether that's an error.
/// - Never reads file content.
pub fn scan_projects_root(projects_root: &Path) -> std::io::Result<Vec<ProjectScan>> {
    if !projects_root.exists() {
        return Ok(Vec::new());
    }
    let mut out = Vec::new();
    for entry in std::fs::read_dir(projects_root)? {
        let entry = entry?;
        let path = entry.path();
        if !entry.file_type()?.is_dir() {
            continue;
        }
        if is_hidden(&path) {
            continue;
        }
        out.push(scan_project_dir(&path)?);
    }
    out.sort_by(|a, b| a.project_dir.cmp(&b.project_dir));
    Ok(out)
}

fn scan_project_dir(project_dir: &Path) -> std::io::Result<ProjectScan> {
    let mut scan = ProjectScan {
        project_dir: project_dir.to_path_buf(),
        ..Default::default()
    };
    for entry in std::fs::read_dir(project_dir)? {
        let entry = entry?;
        let path = entry.path();
        let ft = entry.file_type()?;
        if ft.is_file() && path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
            scan.jsonl_files.push(path);
        } else if ft.is_dir() {
            let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            if name == "memory" {
                scan.memory_files = scan_memory_dir(&path)?;
            } else if !name.is_empty() && !name.starts_with('.') {
                // Recurse for nested session files
                // (`<session-uuid>/subagents/*.jsonl` is the common shape).
                collect_jsonl_recursive(&path, 1, &mut scan.jsonl_files)?;
            }
        }
    }
    scan.jsonl_files.sort();
    scan.memory_files.sort();
    Ok(scan)
}

/// Recursively collect `*.jsonl` files under `dir`, up to
/// [`MAX_JSONL_DEPTH`] levels deep. Skips hidden entries and any
/// directory called `memory` (which is owned by the markdown branch).
///
/// Errors on intermediate entries are swallowed with a `tracing::warn!` โ€”
/// a single unreadable subdir must not kill the whole project scan.
fn collect_jsonl_recursive(
    dir: &Path,
    depth: usize,
    out: &mut Vec<PathBuf>,
) -> std::io::Result<()> {
    if depth > MAX_JSONL_DEPTH {
        return Ok(());
    }
    let entries = match std::fs::read_dir(dir) {
        Ok(it) => it,
        Err(e) => {
            tracing::warn!(dir = %dir.display(), error = %e, "skipping unreadable subdir");
            return Ok(());
        }
    };
    for entry in entries {
        let entry = match entry {
            Ok(e) => e,
            Err(e) => {
                tracing::warn!(dir = %dir.display(), error = %e, "skipping bad dirent");
                continue;
            }
        };
        let path = entry.path();
        let ft = match entry.file_type() {
            Ok(t) => t,
            Err(_) => continue,
        };
        if ft.is_file() {
            if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
                out.push(path);
            }
        } else if ft.is_dir() {
            let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            if name.is_empty() || name.starts_with('.') || name == "memory" {
                continue;
            }
            collect_jsonl_recursive(&path, depth + 1, out)?;
        }
    }
    Ok(())
}

fn scan_memory_dir(memory_dir: &Path) -> std::io::Result<Vec<PathBuf>> {
    let mut out = Vec::new();
    for entry in std::fs::read_dir(memory_dir)? {
        let entry = entry?;
        let path = entry.path();
        if !entry.file_type()?.is_file() {
            continue;
        }
        let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
            continue;
        };
        if name == "MEMORY.md" {
            continue; // index file โ€” see BLUEPRINT ยง6.8 rule 3
        }
        if path.extension().and_then(|e| e.to_str()) == Some("md") {
            out.push(path);
        }
    }
    Ok(out)
}

fn is_hidden(p: &Path) -> bool {
    p.file_name()
        .and_then(|n| n.to_str())
        .map(|s| s.starts_with('.'))
        .unwrap_or(false)
}

/// Sum `(memory_files, jsonl_files)` across all projects โ€” used by the
/// detector for a cheap record-count estimate.
pub fn count_records(scans: &[ProjectScan]) -> (u64, u64) {
    let mut mem = 0u64;
    let mut jsonl = 0u64;
    for s in scans {
        mem += s.memory_files.len() as u64;
        jsonl += s.jsonl_files.len() as u64;
    }
    (mem, jsonl)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::io::Write;

    fn tmp() -> tempdir_lite::TempDir {
        tempdir_lite::TempDir::new("anamnesis-scanner")
    }

    fn touch(p: &Path, content: &str) {
        if let Some(parent) = p.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        let mut f = fs::File::create(p).unwrap();
        f.write_all(content.as_bytes()).unwrap();
    }

    #[test]
    fn missing_root_returns_empty() {
        let scans = scan_projects_root(Path::new("/nonexistent/path/anamnesis-test")).unwrap();
        assert!(scans.is_empty());
    }

    #[test]
    fn finds_jsonl_and_memory_files() {
        let tmp = tmp();
        let root = tmp.path().join("projects");
        let proj_a = root.join("project-a");
        touch(&proj_a.join("session-1.jsonl"), "{}");
        touch(&proj_a.join("session-2.jsonl"), "{}");
        touch(
            &proj_a.join("memory").join("user_role.md"),
            "---\nname: r\n---\nx",
        );
        touch(
            &proj_a.join("memory").join("feedback_x.md"),
            "---\nname: f\n---\ny",
        );
        touch(&proj_a.join("memory").join("MEMORY.md"), "index");

        let scans = scan_projects_root(&root).unwrap();
        assert_eq!(scans.len(), 1);
        let s = &scans[0];
        assert_eq!(s.project_dir, proj_a);
        assert_eq!(s.jsonl_files.len(), 2);
        assert_eq!(s.memory_files.len(), 2, "MEMORY.md must be excluded");
        assert!(s
            .memory_files
            .iter()
            .all(|p| p.file_name().unwrap() != "MEMORY.md"));
    }

    #[test]
    fn multiple_projects_are_sorted() {
        let tmp = tmp();
        let root = tmp.path().join("projects");
        touch(&root.join("proj-z").join("s.jsonl"), "");
        touch(&root.join("proj-a").join("s.jsonl"), "");
        touch(&root.join("proj-m").join("s.jsonl"), "");
        let scans = scan_projects_root(&root).unwrap();
        assert_eq!(scans.len(), 3);
        let names: Vec<_> = scans
            .iter()
            .map(|s| {
                s.project_dir
                    .file_name()
                    .unwrap()
                    .to_string_lossy()
                    .into_owned()
            })
            .collect();
        assert_eq!(names, vec!["proj-a", "proj-m", "proj-z"]);
    }

    #[test]
    fn hidden_dirs_skipped() {
        let tmp = tmp();
        let root = tmp.path().join("projects");
        touch(&root.join(".hidden").join("a.jsonl"), "");
        touch(&root.join("visible").join("a.jsonl"), "");
        let scans = scan_projects_root(&root).unwrap();
        assert_eq!(scans.len(), 1);
        assert_eq!(scans[0].project_dir.file_name().unwrap(), "visible");
    }

    #[test]
    fn non_jsonl_non_memory_files_ignored() {
        let tmp = tmp();
        let root = tmp.path().join("projects");
        touch(&root.join("p").join("a.jsonl"), "");
        touch(&root.join("p").join("ignore.txt"), "");
        touch(&root.join("p").join("ignore.log"), "");
        let scans = scan_projects_root(&root).unwrap();
        assert_eq!(scans[0].jsonl_files.len(), 1);
        assert_eq!(scans[0].memory_files.len(), 0);
    }

    #[test]
    fn nested_subagent_jsonl_is_discovered() {
        // Real Claude Code layout: <project>/<session-uuid>/subagents/*.jsonl
        // Before PR-F these were silently dropped (97.8% of jsonl data lost
        // on a real ~/.claude/projects/ in the wild).
        let tmp = tmp();
        let root = tmp.path().join("projects");
        let proj = root.join("-Users-x-y");
        touch(&proj.join("top.jsonl"), "{}");
        touch(
            &proj
                .join("8c525fd3-9ed6-4e59-b27c-ba544b76a425")
                .join("subagents")
                .join("agent-a.jsonl"),
            "{}",
        );
        touch(
            &proj
                .join("8c525fd3-9ed6-4e59-b27c-ba544b76a425")
                .join("subagents")
                .join("agent-b.jsonl"),
            "{}",
        );
        // Memory directory must still NOT be entered by jsonl recursion.
        touch(
            &proj.join("memory").join("user_role.md"),
            "---\nname: r\n---\nx",
        );
        touch(
            &proj.join("memory").join("not-jsonl.jsonl"),
            "should be ignored",
        );

        let scans = scan_projects_root(&root).unwrap();
        assert_eq!(scans.len(), 1);
        let s = &scans[0];
        assert_eq!(
            s.jsonl_files.len(),
            3,
            "1 top-level + 2 nested subagents = 3; memory/ jsonl must NOT be counted"
        );
        assert!(
            s.jsonl_files.iter().any(|p| p.ends_with("agent-a.jsonl")),
            "agent-a.jsonl from nested subagents/ must be discovered"
        );
        assert!(
            !s.jsonl_files
                .iter()
                .any(|p| p.components().any(|c| c.as_os_str() == "memory")),
            "no jsonl under memory/ should be picked up"
        );
        assert_eq!(s.memory_files.len(), 1, "memory md still found");
    }

    #[test]
    fn hidden_subdirs_are_not_recursed() {
        let tmp = tmp();
        let root = tmp.path().join("projects");
        let proj = root.join("proj");
        touch(&proj.join("a.jsonl"), "");
        touch(&proj.join(".cache").join("hidden.jsonl"), "");
        let scans = scan_projects_root(&root).unwrap();
        assert_eq!(scans.len(), 1);
        assert_eq!(scans[0].jsonl_files.len(), 1);
        assert!(scans[0].jsonl_files[0].ends_with("a.jsonl"));
    }

    #[test]
    fn recursion_depth_is_bounded() {
        // Construct a chain of MAX_JSONL_DEPTH + 2 levels, place a jsonl
        // at the deepest level, and verify it's NOT discovered.
        let tmp = tmp();
        let root = tmp.path().join("projects");
        let mut leaf = root.join("proj");
        for i in 0..(MAX_JSONL_DEPTH + 2) {
            leaf = leaf.join(format!("level-{i}"));
        }
        touch(&leaf.join("too-deep.jsonl"), "");
        let scans = scan_projects_root(&root).unwrap();
        assert!(
            scans[0]
                .jsonl_files
                .iter()
                .all(|p| !p.ends_with("too-deep.jsonl")),
            "jsonl deeper than MAX_JSONL_DEPTH must not appear"
        );
    }

    #[test]
    fn count_records_sums_all_projects() {
        let tmp = tmp();
        let root = tmp.path().join("projects");
        touch(&root.join("a").join("s.jsonl"), "");
        touch(&root.join("a").join("memory").join("m1.md"), "");
        touch(&root.join("b").join("s1.jsonl"), "");
        touch(&root.join("b").join("s2.jsonl"), "");
        touch(&root.join("b").join("memory").join("m1.md"), "");
        touch(&root.join("b").join("memory").join("m2.md"), "");
        let scans = scan_projects_root(&root).unwrap();
        let (mem, jsonl) = count_records(&scans);
        assert_eq!(mem, 3);
        assert_eq!(jsonl, 3);
    }
}

#[cfg(test)]
mod tempdir_lite {
    //! Minimal RAII tempdir helper so we don't pull a tempdir crate just
    //! for scanner tests.
    use std::path::{Path, PathBuf};

    pub struct TempDir(PathBuf);

    static NONCE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

    impl TempDir {
        pub fn new(prefix: &str) -> Self {
            let base = std::env::temp_dir();
            let pid = std::process::id();
            // Atomic counter avoids same-nanosecond collisions on
            // platforms with coarse timer resolution (Windows).
            let n = NONCE.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            let dir = base.join(format!("{prefix}-{pid}-{n}"));
            std::fs::create_dir_all(&dir).expect("create tempdir");
            Self(dir)
        }
        pub fn path(&self) -> &Path {
            &self.0
        }
    }

    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }
}