claude-wrapper 0.13.4

A type-safe Claude Code CLI wrapper for Rust
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
//! Read-side access to Claude Code's per-project **memory**
//! directories.
//!
//! Claude Code's auto-memory persists facts per project under
//! `~/.claude/projects/<slug>/memory/`: a `MEMORY.md` index (one
//! line per memory, loaded into context each session) plus one fact
//! per `<stem>.md` file with YAML frontmatter. A typical fact file:
//!
//! ```text
//! ---
//! name: cluster-72-canonical-slot-crash
//! description: one-line summary used for recall relevance
//! metadata:
//!   type: project
//! ---
//!
//! The fact body, with [[wiki-style]] links to other memories.
//! ```
//!
//! This module is read-only on purpose, like the other
//! introspection modules. The layout is undocumented Claude Code
//! internal state (observed against CLI 2.1.219) and can change
//! across CLI versions, so parsing is permissive: `name`,
//! `description`, and the `type` under `metadata:` are typed
//! (as plain strings); every other frontmatter key lands in
//! [`Memory::extra`] verbatim.
//!
//! Three levels of granularity:
//!
//! - [`MemoryRoot::list_projects_with_memory`] -- which projects
//!   have a memory directory at all.
//! - [`MemoryRoot::list`] -- summaries of one project's memory
//!   files.
//! - [`MemoryRoot::get`] -- one memory's full record including the
//!   body; [`MemoryRoot::index`] -- the raw `MEMORY.md`.
//!
//! # Example
//!
//! ```no_run
//! use claude_wrapper::memory::MemoryRoot;
//!
//! # fn example() -> claude_wrapper::Result<()> {
//! let root = MemoryRoot::home()?;
//! for project in root.list_projects_with_memory()? {
//!     println!("{}: {} memories", project.slug, project.entry_count);
//!     for m in root.list(&project.slug)? {
//!         println!("  {}: {}", m.name, m.description.as_deref().unwrap_or(""));
//!     }
//! }
//! # Ok(()) }
//! ```

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use serde::Serialize;

use crate::artifacts::split_frontmatter;
use crate::error::{Error, Result};

/// Root directory of Claude Code's per-project state. Defaults to
/// `~/.claude/projects` (memory directories live under each project
/// slug); override with [`MemoryRoot::at`] for tests or non-default
/// installs.
#[derive(Debug, Clone)]
pub struct MemoryRoot {
    path: PathBuf,
}

impl MemoryRoot {
    /// Resolve the default `~/.claude/projects`. Errors if `$HOME`
    /// (or the platform-specific user home) cannot be determined.
    pub fn home() -> Result<Self> {
        let home = home_dir().ok_or_else(|| Error::Artifacts {
            message: "could not determine user home directory".to_string(),
        })?;
        Ok(Self {
            path: home.join(".claude").join("projects"),
        })
    }

    /// Use a specific path as the projects root. Useful for tests
    /// (point at a tempdir) and for non-default installs.
    pub fn at(path: impl Into<PathBuf>) -> Self {
        Self { path: path.into() }
    }

    /// The configured root directory.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// List every project slug that has a `memory/` directory,
    /// sorted by slug. Projects without one are omitted; a missing
    /// root returns an empty vec.
    pub fn list_projects_with_memory(&self) -> Result<Vec<ProjectMemorySummary>> {
        let entries = match fs::read_dir(&self.path) {
            Ok(it) => it,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
            Err(e) => return Err(e.into()),
        };
        let mut out = Vec::new();
        for entry in entries.flatten() {
            let project_dir = entry.path();
            if !project_dir.is_dir() {
                continue;
            }
            let Some(slug) = project_dir.file_name().and_then(|s| s.to_str()) else {
                continue;
            };
            let memory_dir = project_dir.join("memory");
            if !memory_dir.is_dir() {
                continue;
            }
            let entry_count = memory_files(&memory_dir).len();
            let has_index = memory_dir.join("MEMORY.md").is_file();
            out.push(ProjectMemorySummary {
                slug: slug.to_string(),
                memory_dir,
                entry_count,
                has_index,
            });
        }
        out.sort_by(|a, b| a.slug.cmp(&b.slug));
        Ok(out)
    }

    /// List one project's memory files, sorted by file stem.
    /// `MEMORY.md` (the index) is excluded; read it with
    /// [`Self::index`]. A project without a memory directory (or an
    /// unknown slug) returns an empty vec. Files that fail to read
    /// contribute a tracing warning and are skipped.
    pub fn list(&self, slug: &str) -> Result<Vec<MemorySummary>> {
        let memory_dir = self.path.join(slug).join("memory");
        let mut out = Vec::new();
        for path in memory_files(&memory_dir) {
            match parse_memory_file(&path) {
                Ok(memory) => out.push(MemorySummary::from_memory(&memory)),
                Err(e) => tracing::warn!(?path, "skipping memory file: {e}"),
            }
        }
        out.sort_by(|a, b| a.file_stem.cmp(&b.file_stem));
        Ok(out)
    }

    /// Read one memory by file stem (the basename of `<stem>.md`
    /// under the project's memory directory). Errors if no such
    /// file exists.
    pub fn get(&self, slug: &str, file_stem: &str) -> Result<Memory> {
        let path = self
            .path
            .join(slug)
            .join("memory")
            .join(format!("{file_stem}.md"));
        if !path.is_file() {
            return Err(Error::Artifacts {
                message: format!("no memory at {}", path.display()),
            });
        }
        parse_memory_file(&path)
    }

    /// The raw `MEMORY.md` index content for one project, or `None`
    /// when the project has no memory directory or no index file.
    pub fn index(&self, slug: &str) -> Result<Option<String>> {
        let path = self.path.join(slug).join("memory").join("MEMORY.md");
        if !path.is_file() {
            return Ok(None);
        }
        Ok(Some(fs::read_to_string(&path)?))
    }
}

/// One project that has a memory directory, returned by
/// [`MemoryRoot::list_projects_with_memory`].
#[derive(Debug, Clone, Serialize)]
pub struct ProjectMemorySummary {
    /// Project slug (the encoded-path directory name).
    pub slug: String,
    /// Absolute path of the `memory/` directory.
    pub memory_dir: PathBuf,
    /// Number of memory files (excluding `MEMORY.md`).
    pub entry_count: usize,
    /// Whether a `MEMORY.md` index is present.
    pub has_index: bool,
}

/// Lightweight metadata for one memory file, returned by
/// [`MemoryRoot::list`]. Strips the body to keep listings cheap.
#[derive(Debug, Clone, Serialize)]
pub struct MemorySummary {
    /// File stem (the basename of `<stem>.md`). The canonical
    /// handle for [`MemoryRoot::get`].
    pub file_stem: String,
    /// Frontmatter `name` if present; falls back to `file_stem`.
    pub name: String,
    /// Frontmatter `description` if present.
    pub description: Option<String>,
    /// The `type` recorded under `metadata:` (`user`, `feedback`,
    /// `project`, `reference`, or anything future), carried as a
    /// plain string.
    pub memory_type: Option<String>,
    /// Absolute path to the source `.md`.
    pub file_path: PathBuf,
    /// File size in bytes.
    pub size_bytes: u64,
}

impl MemorySummary {
    fn from_memory(m: &Memory) -> Self {
        let size_bytes = fs::metadata(&m.file_path)
            .map(|meta| meta.len())
            .unwrap_or_default();
        Self {
            file_stem: m.file_stem.clone(),
            name: m.name.clone(),
            description: m.description.clone(),
            memory_type: m.memory_type.clone(),
            file_path: m.file_path.clone(),
            size_bytes,
        }
    }
}

/// Full memory record returned by [`MemoryRoot::get`].
#[derive(Debug, Clone, Serialize)]
pub struct Memory {
    /// File stem (the basename of `<stem>.md`). The canonical
    /// handle for lookup.
    pub file_stem: String,
    /// Frontmatter `name` if present; falls back to `file_stem`.
    pub name: String,
    /// Frontmatter `description` if present.
    pub description: Option<String>,
    /// The `type` recorded under `metadata:`, as a plain string.
    pub memory_type: Option<String>,
    /// Absolute path to the source `.md`.
    pub file_path: PathBuf,
    /// Markdown body after the frontmatter block (trimmed of
    /// leading/trailing blank lines). `[[wiki-style]]` links are
    /// left verbatim.
    pub body: String,
    /// Frontmatter keys other than the typed ones, flattened line
    /// by line (nested YAML keys appear under their own names).
    /// Preserves unknown future fields verbatim as raw strings.
    pub extra: BTreeMap<String, String>,
}

/// Memory fact files in a directory: direct children matching
/// `*.md`, excluding the `MEMORY.md` index. Missing or unreadable
/// directories yield an empty list.
fn memory_files(dir: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_file() {
                continue;
            }
            if path.extension().and_then(|s| s.to_str()) != Some("md") {
                continue;
            }
            if path.file_name().and_then(|s| s.to_str()) == Some("MEMORY.md") {
                continue;
            }
            out.push(path);
        }
    }
    out
}

fn parse_memory_file(file_path: &Path) -> Result<Memory> {
    let file_stem = file_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or_default()
        .to_string();
    let raw = fs::read_to_string(file_path)?;
    let (frontmatter, body) = split_frontmatter(&raw);

    let mut name = file_stem.clone();
    let mut description = None;
    let mut memory_type = None;
    let mut extra = BTreeMap::new();

    if let Some(fm) = frontmatter {
        for line in fm.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            let Some((k, v)) = trimmed.split_once(':') else {
                continue;
            };
            let key = k.trim();
            let value = unquote(v.trim()).to_string();
            match key {
                "name" if !value.is_empty() => name = value,
                "description" if !value.is_empty() => description = Some(value),
                // The line-based parse flattens the nested
                // `metadata:` block, so its `type:` arrives as a
                // bare key.
                "type" if !value.is_empty() => memory_type = Some(value),
                _ if !key.is_empty() && !value.is_empty() => {
                    extra.insert(key.to_string(), value);
                }
                _ => {}
            }
        }
    }

    Ok(Memory {
        file_stem,
        name,
        description,
        memory_type,
        file_path: file_path.to_path_buf(),
        body: body.trim().to_string(),
        extra,
    })
}

/// Strip one pair of matching surrounding double quotes, if
/// present. Frontmatter values are sometimes written quoted (e.g.
/// descriptions containing punctuation).
fn unquote(value: &str) -> &str {
    value
        .strip_prefix('"')
        .and_then(|v| v.strip_suffix('"'))
        .unwrap_or(value)
}

fn home_dir() -> Option<PathBuf> {
    if let Ok(h) = std::env::var("HOME")
        && !h.is_empty()
    {
        return Some(PathBuf::from(h));
    }
    if let Ok(h) = std::env::var("USERPROFILE")
        && !h.is_empty()
    {
        return Some(PathBuf::from(h));
    }
    None
}

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

    fn write_memory(root: &Path, slug: &str, stem: &str, contents: &str) -> PathBuf {
        let dir = root.join(slug).join("memory");
        fs::create_dir_all(&dir).expect("create memory dir");
        let path = dir.join(format!("{stem}.md"));
        let mut f = fs::File::create(&path).expect("create memory file");
        f.write_all(contents.as_bytes()).expect("write memory file");
        path
    }

    fn fixture_root() -> tempfile::TempDir {
        let tmp = tempfile::tempdir().expect("tempdir");
        write_memory(
            tmp.path(),
            "-Users-me-Code-projA",
            "user-name",
            "---\nname: user-name\ndescription: \"preferred name - quoted\"\nmetadata:\n  type: user\n---\n\nThe user goes by Zed. See [[other-memory]].\n",
        );
        write_memory(
            tmp.path(),
            "-Users-me-Code-projA",
            "no-frontmatter",
            "Just a body.\n",
        );
        fs::write(
            tmp.path()
                .join("-Users-me-Code-projA")
                .join("memory")
                .join("MEMORY.md"),
            "# Memory index\n\n- [User name](user-name.md)\n",
        )
        .unwrap();
        // A project without a memory directory.
        fs::create_dir_all(tmp.path().join("-Users-me-Code-projB")).unwrap();
        tmp
    }

    #[test]
    fn list_projects_with_memory_omits_projects_without() {
        let tmp = fixture_root();
        let root = MemoryRoot::at(tmp.path());
        let projects = root.list_projects_with_memory().expect("list");
        assert_eq!(projects.len(), 1);
        assert_eq!(projects[0].slug, "-Users-me-Code-projA");
        assert_eq!(projects[0].entry_count, 2);
        assert!(projects[0].has_index);
    }

    #[test]
    fn list_projects_missing_root_returns_empty() {
        let tmp = tempfile::tempdir().unwrap();
        let root = MemoryRoot::at(tmp.path().join("does-not-exist"));
        assert!(root.list_projects_with_memory().expect("ok").is_empty());
    }

    #[test]
    fn list_excludes_index_and_parses_metadata() {
        let tmp = fixture_root();
        let root = MemoryRoot::at(tmp.path());
        let memories = root.list("-Users-me-Code-projA").expect("list");
        let stems: Vec<&str> = memories.iter().map(|m| m.file_stem.as_str()).collect();
        assert_eq!(stems, ["no-frontmatter", "user-name"]);
        let m = memories
            .iter()
            .find(|m| m.file_stem == "user-name")
            .unwrap();
        assert_eq!(m.name, "user-name");
        assert_eq!(m.description.as_deref(), Some("preferred name - quoted"));
        assert_eq!(m.memory_type.as_deref(), Some("user"));
        assert!(m.size_bytes > 0);
    }

    #[test]
    fn list_unknown_slug_returns_empty() {
        let tmp = fixture_root();
        let root = MemoryRoot::at(tmp.path());
        assert!(root.list("nope").expect("ok").is_empty());
        assert!(root.list("-Users-me-Code-projB").expect("ok").is_empty());
    }

    #[test]
    fn get_returns_body_and_falls_back_to_stem() {
        let tmp = fixture_root();
        let root = MemoryRoot::at(tmp.path());
        let m = root.get("-Users-me-Code-projA", "user-name").expect("get");
        assert!(m.body.contains("[[other-memory]]"));
        let nf = root
            .get("-Users-me-Code-projA", "no-frontmatter")
            .expect("get");
        assert_eq!(nf.name, "no-frontmatter");
        assert_eq!(nf.memory_type, None);
        assert_eq!(nf.body, "Just a body.");
    }

    #[test]
    fn get_unknown_stem_errors() {
        let tmp = fixture_root();
        let root = MemoryRoot::at(tmp.path());
        let err = root.get("-Users-me-Code-projA", "nope").unwrap_err();
        assert!(err.to_string().contains("no memory at"));
    }

    #[test]
    fn index_reads_memory_md_or_none() {
        let tmp = fixture_root();
        let root = MemoryRoot::at(tmp.path());
        let idx = root.index("-Users-me-Code-projA").expect("ok");
        assert!(idx.expect("present").contains("# Memory index"));
        assert!(root.index("-Users-me-Code-projB").expect("ok").is_none());
        assert!(root.index("nope").expect("ok").is_none());
    }

    #[test]
    fn unknown_frontmatter_keys_land_in_extra() {
        let tmp = tempfile::tempdir().unwrap();
        write_memory(
            tmp.path(),
            "-slug",
            "weird",
            "---\nname: weird\nmetadata:\n  type: reference\n  originSessionId: abc\ncustom: kept\n---\nbody\n",
        );
        let root = MemoryRoot::at(tmp.path());
        let m = root.get("-slug", "weird").expect("get");
        assert_eq!(m.memory_type.as_deref(), Some("reference"));
        assert_eq!(
            m.extra.get("originSessionId").map(String::as_str),
            Some("abc")
        );
        assert_eq!(m.extra.get("custom").map(String::as_str), Some("kept"));
        // The bare `metadata:` container line has no value; dropped.
        assert!(!m.extra.contains_key("metadata"));
    }
}