memcan 0.39.0

Persistent memory for Claude Code — CLI client
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
//! Local file walking for index-code command.
//!
//! No dependency on memcan-core — just std filesystem operations.

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

// Keep in sync with SKIP_DIRS in rs/memcan-core/src/indexing/code.rs.
const SKIP_DIRS: &[&str] = &[
    ".claude",
    ".git",
    ".next",
    ".tox",
    ".venv",
    "__pycache__",
    "build",
    "dist",
    "node_modules",
    "target",
    "vendor",
];

const ALLOWED_EXTENSIONS: &[&str] = &["rs", "py", "go", "ts", "tsx"];

/// The closed set of recognized `--tech-stack` values (canonical lowercase).
pub const SUPPORTED_TECH_STACKS: &[&str] = &["rust", "python", "go", "typescript"];

/// Canonicalize a user-supplied tech-stack name to its lowercase form.
///
/// Matching is case-insensitive (`Rust` -> `rust`). Returns `None` for an
/// unrecognized stack so callers can reject it loudly. The canonical name is
/// what gets stored on each record, so server-side exact-match search finds it.
pub fn canonical_tech_stack(stack: &str) -> Option<&'static str> {
    SUPPORTED_TECH_STACKS
        .iter()
        .copied()
        .find(|s| *s == stack.to_ascii_lowercase())
}

/// Maps an explicit tech stack to the file extensions it permits.
///
/// Matching is case-insensitive (`Rust` == `rust`). Returns `None` for an
/// unrecognized stack so the caller can reject it loudly rather than silently
/// indexing every language.
///
/// Keep the stack->extension mapping in sync with `lang_extensions()` in
/// rs/memcan-core/src/indexing/code.rs.
fn extensions_for_stack(stack: &str) -> Option<&'static [&'static str]> {
    match canonical_tech_stack(stack)? {
        "rust" => Some(&["rs"]),
        "python" => Some(&["py"]),
        "go" => Some(&["go"]),
        "typescript" => Some(&["ts", "tsx"]),
        _ => None,
    }
}

pub struct WalkOptions<'a> {
    pub max_file_size: u64,
    /// When `Some`, restrict walked files to the extensions of this tech stack.
    /// When `None`, walk all supported extensions.
    pub tech_stack: Option<&'a str>,
}

#[derive(Debug)]
pub struct WalkedFile {
    pub relative_path: String,
    pub content: String,
}

pub fn walk_directory(root: &Path, opts: &WalkOptions) -> Result<Vec<WalkedFile>, String> {
    let root = root
        .canonicalize()
        .map_err(|e| format!("cannot resolve root directory: {e}"))?;
    let allowed: &[&str] = match opts.tech_stack {
        Some(stack) => extensions_for_stack(stack).ok_or_else(|| {
            format!("unknown --tech-stack '{stack}'; supported: rust, python, go, typescript")
        })?,
        None => ALLOWED_EXTENSIONS,
    };
    let mut files = Vec::new();
    let mut stack: Vec<PathBuf> = vec![root.clone()];

    while let Some(dir) = stack.pop() {
        let entries =
            fs::read_dir(&dir).map_err(|e| format!("cannot read {}: {e}", dir.display()))?;
        for entry in entries {
            let entry = match entry {
                Ok(e) => e,
                Err(_) => continue,
            };
            let path = entry.path();

            let ft = match entry.file_type() {
                Ok(ft) => ft,
                Err(_) => continue,
            };

            if ft.is_symlink() {
                continue;
            }

            if ft.is_dir() {
                let name = entry.file_name();
                let name_str = name.to_string_lossy();
                if !SKIP_DIRS.contains(&name_str.as_ref()) {
                    stack.push(path);
                }
                continue;
            }

            if !ft.is_file() {
                continue;
            }

            let ext = match path.extension().and_then(|e| e.to_str()) {
                Some(e) => e.to_ascii_lowercase(),
                None => continue,
            };
            if !allowed.contains(&ext.as_str()) {
                continue;
            }

            let meta = match fs::metadata(&path) {
                Ok(m) => m,
                Err(_) => continue,
            };
            if meta.len() > opts.max_file_size {
                continue;
            }

            let content = match fs::read_to_string(&path) {
                Ok(c) => c,
                Err(_) => continue,
            };

            let relative = path
                .strip_prefix(&root)
                .unwrap_or(&path)
                .to_string_lossy()
                .to_string();

            files.push(WalkedFile {
                relative_path: relative,
                content,
            });
        }
    }

    files.sort_by(|a, b| a.relative_path.cmp(&b.relative_path));
    Ok(files)
}

pub fn detect_tech_stack(files: &[WalkedFile]) -> Result<String, String> {
    let mut exts: HashSet<String> = HashSet::new();
    for f in files {
        if let Some(ext) = Path::new(&f.relative_path)
            .extension()
            .and_then(|e| e.to_str())
        {
            exts.insert(ext.to_ascii_lowercase());
        }
    }

    let has_rs = exts.contains("rs");
    let has_py = exts.contains("py");
    let has_go = exts.contains("go");
    let has_ts = exts.contains("ts") || exts.contains("tsx");

    let count = [has_rs, has_py, has_go, has_ts]
        .iter()
        .filter(|&&b| b)
        .count();

    if count == 0 {
        return Err("no supported files found".to_string());
    }
    if count > 1 {
        return Err("mixed languages detected, please specify --tech-stack explicitly".to_string());
    }

    if has_rs {
        Ok("rust".to_string())
    } else if has_py {
        Ok("python".to_string())
    } else if has_go {
        Ok("go".to_string())
    } else {
        Ok("typescript".to_string())
    }
}

pub fn chunk_into_batches<T>(items: Vec<T>, batch_size: usize) -> Vec<Vec<T>> {
    let batch_size = batch_size.max(1);
    let mut batches = Vec::new();
    let mut current = Vec::with_capacity(batch_size);
    for item in items {
        current.push(item);
        if current.len() >= batch_size {
            batches.push(std::mem::take(&mut current));
            current = Vec::with_capacity(batch_size);
        }
    }
    if !current.is_empty() {
        batches.push(current);
    }
    batches
}

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

    fn setup_test_dir() -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        // Create valid source files
        fs::write(root.join("main.rs"), "fn main() {}").unwrap();
        fs::write(root.join("lib.py"), "print('hello')").unwrap();

        // Create a subdirectory with a file
        fs::create_dir_all(root.join("src")).unwrap();
        fs::write(root.join("src").join("util.rs"), "pub fn util() {}").unwrap();

        // Create skip directories
        fs::create_dir_all(root.join(".git")).unwrap();
        fs::write(root.join(".git").join("config"), "git config").unwrap();

        fs::create_dir_all(root.join("node_modules")).unwrap();
        fs::write(root.join("node_modules").join("pkg.ts"), "export {}").unwrap();

        fs::create_dir_all(root.join("target")).unwrap();
        fs::write(root.join("target").join("debug.rs"), "// build output").unwrap();

        fs::create_dir_all(root.join(".claude").join("worktrees").join("agent-abc")).unwrap();
        fs::write(
            root.join(".claude")
                .join("worktrees")
                .join("agent-abc")
                .join("stray.rs"),
            "fn stray() {}",
        )
        .unwrap();

        // Create unsupported extension
        fs::write(root.join("readme.md"), "# Readme").unwrap();
        fs::write(root.join("data.json"), "{}").unwrap();

        dir
    }

    #[test]
    fn walk_finds_supported_files() {
        let dir = setup_test_dir();
        let opts = WalkOptions {
            max_file_size: 1_048_576,
            tech_stack: None,
        };
        let files = walk_directory(dir.path(), &opts).unwrap();
        let paths: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();

        assert!(paths.contains(&"main.rs"));
        assert!(paths.contains(&"lib.py"));
        assert!(paths.contains(&"src/util.rs"));
    }

    #[test]
    fn walk_skips_excluded_dirs() {
        let dir = setup_test_dir();
        let opts = WalkOptions {
            max_file_size: 1_048_576,
            tech_stack: None,
        };
        let files = walk_directory(dir.path(), &opts).unwrap();
        let paths: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();

        for p in &paths {
            assert!(!p.starts_with(".git/"), "should skip .git: {p}");
            assert!(
                !p.starts_with("node_modules/"),
                "should skip node_modules: {p}"
            );
            assert!(!p.starts_with("target/"), "should skip target: {p}");
            assert!(!p.starts_with(".claude/"), "should skip .claude: {p}");
        }
    }

    #[test]
    fn walk_skips_unsupported_extensions() {
        let dir = setup_test_dir();
        let opts = WalkOptions {
            max_file_size: 1_048_576,
            tech_stack: None,
        };
        let files = walk_directory(dir.path(), &opts).unwrap();
        let paths: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();

        assert!(!paths.contains(&"readme.md"));
        assert!(!paths.contains(&"data.json"));
    }

    #[test]
    fn walk_skips_large_files() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("big.rs"), "x".repeat(200)).unwrap();
        fs::write(dir.path().join("small.rs"), "fn f() {}").unwrap();

        let opts = WalkOptions {
            max_file_size: 100,
            tech_stack: None,
        };
        let files = walk_directory(dir.path(), &opts).unwrap();
        let paths: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();

        assert!(!paths.contains(&"big.rs"));
        assert!(paths.contains(&"small.rs"));
    }

    #[test]
    fn walk_restricts_extensions_to_explicit_tech_stack() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("a.rs"), "fn a() {}").unwrap();
        fs::write(dir.path().join("b.ts"), "export {}").unwrap();

        let opts = WalkOptions {
            max_file_size: 1_048_576,
            tech_stack: Some("rust"),
        };
        let files = walk_directory(dir.path(), &opts).unwrap();
        let paths: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();

        assert!(paths.contains(&"a.rs"));
        assert!(!paths.contains(&"b.ts"), "should skip .ts when stack=rust");
    }

    #[test]
    fn walk_rejects_unknown_explicit_tech_stack() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("a.rs"), "fn a() {}").unwrap();

        let opts = WalkOptions {
            max_file_size: 1_048_576,
            tech_stack: Some("rsut"),
        };
        let err = walk_directory(dir.path(), &opts).unwrap_err();
        assert!(err.contains("unknown --tech-stack 'rsut'"), "got: {err}");
    }

    #[test]
    fn walk_tech_stack_is_case_insensitive() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("a.RS"), "fn a() {}").unwrap();
        fs::write(dir.path().join("b.ts"), "export {}").unwrap();

        let opts = WalkOptions {
            max_file_size: 1_048_576,
            tech_stack: Some("Rust"),
        };
        let files = walk_directory(dir.path(), &opts).unwrap();
        let paths: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();

        assert!(paths.contains(&"a.RS"), "uppercase .RS should match rust");
        assert!(!paths.contains(&"b.ts"));
    }

    #[test]
    fn detect_tech_stack_is_case_insensitive() {
        let files = vec![WalkedFile {
            relative_path: "src/Main.RS".into(),
            content: String::new(),
        }];
        assert_eq!(detect_tech_stack(&files).unwrap(), "rust");
    }

    #[test]
    fn detect_single_language_rust() {
        let files = vec![
            WalkedFile {
                relative_path: "src/main.rs".into(),
                content: String::new(),
            },
            WalkedFile {
                relative_path: "src/lib.rs".into(),
                content: String::new(),
            },
        ];
        assert_eq!(detect_tech_stack(&files).unwrap(), "rust");
    }

    #[test]
    fn detect_single_language_python() {
        let files = vec![WalkedFile {
            relative_path: "app.py".into(),
            content: String::new(),
        }];
        assert_eq!(detect_tech_stack(&files).unwrap(), "python");
    }

    #[test]
    fn detect_single_language_typescript() {
        let files = vec![
            WalkedFile {
                relative_path: "index.ts".into(),
                content: String::new(),
            },
            WalkedFile {
                relative_path: "App.tsx".into(),
                content: String::new(),
            },
        ];
        assert_eq!(detect_tech_stack(&files).unwrap(), "typescript");
    }

    #[test]
    fn detect_mixed_languages_errors() {
        let files = vec![
            WalkedFile {
                relative_path: "main.rs".into(),
                content: String::new(),
            },
            WalkedFile {
                relative_path: "script.py".into(),
                content: String::new(),
            },
        ];
        let err = detect_tech_stack(&files).unwrap_err();
        assert!(err.contains("mixed languages"));
    }

    #[test]
    fn detect_no_files_errors() {
        let files: Vec<WalkedFile> = vec![];
        let err = detect_tech_stack(&files).unwrap_err();
        assert!(err.contains("no supported files"));
    }

    #[test]
    fn chunk_batches_exact() {
        let items: Vec<i32> = (1..=6).collect();
        let batches = chunk_into_batches(items, 3);
        assert_eq!(batches.len(), 2);
        assert_eq!(batches[0], vec![1, 2, 3]);
        assert_eq!(batches[1], vec![4, 5, 6]);
    }

    #[test]
    fn chunk_batches_remainder() {
        let items: Vec<i32> = (1..=7).collect();
        let batches = chunk_into_batches(items, 3);
        assert_eq!(batches.len(), 3);
        assert_eq!(batches[2], vec![7]);
    }

    #[test]
    fn chunk_batches_empty() {
        let items: Vec<i32> = vec![];
        let batches = chunk_into_batches(items, 3);
        assert!(batches.is_empty());
    }

    #[test]
    fn chunk_batches_single_item() {
        let items = vec![42];
        let batches = chunk_into_batches(items, 5);
        assert_eq!(batches.len(), 1);
        assert_eq!(batches[0], vec![42]);
    }
}