cmx-core 0.3.0

Embeddable core for installing agent skills across platforms — cmx-aware lockfile tracking, plan/apply installs, version guards
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
use anyhow::{Result, bail};
use chrono::{DateTime, Utc};
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Mutex;

use super::clock::Clock;
use super::filesystem::{DirEntry, Filesystem};
use super::git::GitClient;
use super::llm::LlmClient;

// ---------------------------------------------------------------------------
// FakeFilesystem
// ---------------------------------------------------------------------------

/// In-memory filesystem for tests.
///
/// Files are stored as byte vectors keyed by their (absolute or relative)
/// path.  Directory entries are derived automatically from file paths.  All
/// methods that mutate state accept `&self` via interior mutability
/// (`RefCell`) so that test helpers and trait implementations share the same
/// borrow.
pub struct FakeFilesystem {
    files: RefCell<BTreeMap<PathBuf, Vec<u8>>>,
    dirs: RefCell<BTreeSet<PathBuf>>,
    fail_on_write: RefCell<Option<PathBuf>>,
    fail_on_rename: RefCell<Option<PathBuf>>,
    fail_on_copy: RefCell<bool>,
}

impl FakeFilesystem {
    pub fn new() -> Self {
        Self {
            files: RefCell::new(BTreeMap::new()),
            dirs: RefCell::new(BTreeSet::new()),
            fail_on_write: RefCell::new(None),
            fail_on_rename: RefCell::new(None),
            fail_on_copy: RefCell::new(false),
        }
    }

    /// Cause the next `write()` to the given path to return an error.
    pub fn set_fail_on_write(&self, path: impl Into<PathBuf>) {
        *self.fail_on_write.borrow_mut() = Some(path.into());
    }

    /// Cause `rename()` targeting the given destination path to return an error.
    pub fn set_fail_on_rename(&self, path: impl Into<PathBuf>) {
        *self.fail_on_rename.borrow_mut() = Some(path.into());
    }

    /// Cause all `copy_file()` calls to return an error.
    pub fn set_fail_on_copy(&self, fail: bool) {
        *self.fail_on_copy.borrow_mut() = fail;
    }

    /// Insert a file with the given content, automatically registering all
    /// ancestor directories.
    pub fn add_file(&self, path: impl Into<PathBuf>, content: impl Into<Vec<u8>>) {
        let path = path.into();
        // Register all ancestor directories
        let mut current = path.parent();
        while let Some(parent) = current {
            if parent != Path::new("") {
                self.dirs.borrow_mut().insert(parent.to_path_buf());
            }
            current = parent.parent();
        }
        self.files.borrow_mut().insert(path, content.into());
    }

    /// Explicitly register a directory path.
    pub fn add_dir(&self, path: impl Into<PathBuf>) {
        self.dirs.borrow_mut().insert(path.into());
    }

    /// Return the stored content for a path, or `None` if absent.
    pub fn get_file_content(&self, path: &Path) -> Option<Vec<u8>> {
        self.files.borrow().get(path).cloned()
    }

    /// Return true if the path has been added as a file.
    pub fn file_exists(&self, path: &Path) -> bool {
        self.files.borrow().contains_key(path)
    }

    /// Return a sorted snapshot of every file currently stored in the fake filesystem.
    pub fn snapshot_files(&self) -> BTreeMap<PathBuf, Vec<u8>> {
        self.files.borrow().clone()
    }
}

impl Default for FakeFilesystem {
    fn default() -> Self {
        Self::new()
    }
}

impl Filesystem for FakeFilesystem {
    fn exists(&self, path: &Path) -> bool {
        self.files.borrow().contains_key(path) || self.dirs.borrow().contains(path)
    }

    fn is_dir(&self, path: &Path) -> bool {
        self.dirs.borrow().contains(path)
    }

    fn is_file(&self, path: &Path) -> bool {
        self.files.borrow().contains_key(path)
    }

    fn read_to_string(&self, path: &Path) -> Result<String> {
        match self.files.borrow().get(path).cloned() {
            Some(bytes) => Ok(String::from_utf8(bytes)?),
            None => bail!("File not found: {}", path.display()),
        }
    }

    fn read(&self, path: &Path) -> Result<Vec<u8>> {
        match self.files.borrow().get(path).cloned() {
            Some(bytes) => Ok(bytes),
            None => bail!("File not found: {}", path.display()),
        }
    }

    fn write(&self, path: &Path, contents: &str) -> Result<()> {
        if self.fail_on_write.borrow().as_deref() == Some(path) {
            bail!("Failed to write {}", path.display());
        }
        self.add_file(path.to_path_buf(), contents.as_bytes().to_vec());
        Ok(())
    }

    fn write_bytes(&self, path: &Path, contents: &[u8]) -> Result<()> {
        self.add_file(path.to_path_buf(), contents.to_vec());
        Ok(())
    }

    fn create_dir_all(&self, path: &Path) -> Result<()> {
        let mut current = Some(path);
        while let Some(p) = current {
            if p != Path::new("") {
                self.dirs.borrow_mut().insert(p.to_path_buf());
            }
            current = p.parent();
        }
        Ok(())
    }

    fn copy_file(&self, src: &Path, dest: &Path) -> Result<()> {
        if *self.fail_on_copy.borrow() {
            bail!(
                "FakeFilesystem: copy_file configured to fail ({} -> {})",
                src.display(),
                dest.display()
            );
        }
        let bytes = self
            .files
            .borrow()
            .get(src)
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("Source file not found: {}", src.display()))?;
        self.add_file(dest.to_path_buf(), bytes);
        Ok(())
    }

    fn rename(&self, from: &Path, to: &Path) -> Result<()> {
        if self.fail_on_rename.borrow().as_deref() == Some(to) {
            bail!("FakeFilesystem: rename configured to fail for {}", to.display());
        }
        let bytes = self.files.borrow().get(from).cloned().ok_or_else(|| {
            anyhow::anyhow!("Source file not found for rename: {}", from.display())
        })?;
        self.add_file(to.to_path_buf(), bytes);
        self.files.borrow_mut().remove(from);
        Ok(())
    }

    fn remove_file(&self, path: &Path) -> Result<()> {
        if self.files.borrow_mut().remove(path).is_none() {
            bail!("File not found: {}", path.display());
        }
        Ok(())
    }

    fn remove_dir_all(&self, path: &Path) -> Result<()> {
        // Remove all files whose path starts with `path`
        let prefix = path.to_path_buf();
        self.files.borrow_mut().retain(|k, _| !k.starts_with(&prefix));
        // Remove all dirs whose path starts with `path`
        self.dirs.borrow_mut().retain(|k| !k.starts_with(&prefix));
        Ok(())
    }

    fn read_dir(&self, path: &Path) -> Result<Vec<DirEntry>> {
        if !self.dirs.borrow().contains(path) && !self.files.borrow().contains_key(path) {
            bail!("Failed to read directory {}", path.display());
        }

        let mut seen: BTreeSet<PathBuf> = BTreeSet::new();
        let mut entries = Vec::new();

        // Collect immediate children from files
        for file_path in self.files.borrow().keys() {
            if let Some(parent) = file_path.parent()
                && parent == path
            {
                let file_name = file_path
                    .file_name()
                    .expect("file path with a matched parent must have a final component")
                    .to_string_lossy()
                    .to_string();
                if seen.insert(file_path.clone()) {
                    entries.push(DirEntry {
                        path: file_path.clone(),
                        file_name,
                        is_dir: false,
                    });
                }
            }
        }

        // Collect immediate children from dirs
        for dir_path in self.dirs.borrow().clone() {
            if let Some(parent) = dir_path.parent()
                && parent == path
                && seen.insert(dir_path.clone())
            {
                let file_name = dir_path
                    .file_name()
                    .expect("dir path with a matched parent must have a final component")
                    .to_string_lossy()
                    .to_string();
                entries.push(DirEntry {
                    path: dir_path,
                    file_name,
                    is_dir: true,
                });
            }
        }

        Ok(entries)
    }

    fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
        // In tests we treat the path as already canonical
        Ok(path.to_path_buf())
    }
}

// ---------------------------------------------------------------------------
// FakeGitClient
// ---------------------------------------------------------------------------

/// Records git operations without executing them.
pub struct FakeGitClient {
    pub cloned: RefCell<Vec<(String, PathBuf)>>,
    pub pulled: RefCell<Vec<PathBuf>>,
    pub should_fail: bool,
}

impl FakeGitClient {
    pub fn new() -> Self {
        Self {
            cloned: RefCell::new(Vec::new()),
            pulled: RefCell::new(Vec::new()),
            should_fail: false,
        }
    }
}

impl Default for FakeGitClient {
    fn default() -> Self {
        Self::new()
    }
}

impl GitClient for FakeGitClient {
    fn clone_repo(&self, url: &str, dest: &Path) -> Result<()> {
        if self.should_fail {
            bail!("FakeGitClient: clone_repo configured to fail");
        }
        self.cloned.borrow_mut().push((url.to_string(), dest.to_path_buf()));
        Ok(())
    }

    fn pull(&self, repo_path: &Path) -> Result<()> {
        if self.should_fail {
            bail!("FakeGitClient: pull configured to fail");
        }
        self.pulled.borrow_mut().push(repo_path.to_path_buf());
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// FakeClock
// ---------------------------------------------------------------------------

/// Always returns the same instant.
pub struct FakeClock {
    pub now: DateTime<Utc>,
}

impl FakeClock {
    pub fn at(now: DateTime<Utc>) -> Self {
        Self { now }
    }
}

impl Clock for FakeClock {
    fn now(&self) -> DateTime<Utc> {
        self.now
    }
}

// ---------------------------------------------------------------------------
// FakeLlmClient
// ---------------------------------------------------------------------------

/// Returns a canned response string, or fails if `should_fail` is set.
/// Also records every `(system_prompt, user_prompt)` pair passed to `analyze`.
pub struct FakeLlmClient {
    pub response: String,
    pub should_fail: bool,
    pub calls: Mutex<Vec<(String, String)>>,
}

impl FakeLlmClient {
    pub fn new(response: impl Into<String>) -> Self {
        Self {
            response: response.into(),
            should_fail: false,
            calls: Mutex::new(Vec::new()),
        }
    }

    /// Return the most recent `(system_prompt, user_prompt)` pair, if any.
    pub fn last_call(&self) -> Option<(String, String)> {
        self.calls.lock().unwrap().last().cloned()
    }

    /// Return all recorded `(system_prompt, user_prompt)` pairs in call order.
    pub fn all_calls(&self) -> Vec<(String, String)> {
        self.calls.lock().unwrap().clone()
    }
}

impl LlmClient for FakeLlmClient {
    fn analyze(
        &self,
        system_prompt: &str,
        user_prompt: &str,
    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + '_>> {
        self.calls
            .lock()
            .unwrap()
            .push((system_prompt.to_string(), user_prompt.to_string()));
        let should_fail = self.should_fail;
        let response = self.response.clone();
        Box::pin(async move {
            if should_fail {
                bail!("FakeLlmClient: analyze configured to fail");
            }
            Ok(response)
        })
    }
}

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

    #[test]
    fn fake_filesystem_add_file_and_exists() {
        let fs = FakeFilesystem::new();
        let path = PathBuf::from("/home/user/test.txt");
        fs.add_file(path.clone(), b"hello".to_vec());
        assert!(fs.exists(&path));
        assert!(fs.is_file(&path));
        assert!(!fs.is_dir(&path));
    }

    #[test]
    fn fake_filesystem_add_file_registers_parent_dirs() {
        let fs = FakeFilesystem::new();
        fs.add_file("/home/user/subdir/file.txt", "content");
        assert!(fs.is_dir(&PathBuf::from("/home/user/subdir")));
        assert!(fs.is_dir(&PathBuf::from("/home/user")));
        assert!(fs.is_dir(&PathBuf::from("/home")));
    }

    #[test]
    fn fake_filesystem_read_to_string_returns_content() {
        let fs = FakeFilesystem::new();
        fs.add_file("/tmp/a.txt", "hello world");
        let content = fs.read_to_string(Path::new("/tmp/a.txt")).unwrap();
        assert_eq!(content, "hello world");
    }

    #[test]
    fn fake_filesystem_read_missing_file_errors() {
        let fs = FakeFilesystem::new();
        assert!(fs.read_to_string(Path::new("/nonexistent.txt")).is_err());
    }

    #[test]
    fn fake_filesystem_write_then_read() {
        let fs = FakeFilesystem::new();
        let path = PathBuf::from("/tmp/out.txt");
        fs.write(&path, "written content").unwrap();
        assert_eq!(fs.read_to_string(&path).unwrap(), "written content");
    }

    #[test]
    fn fake_filesystem_remove_file() {
        let fs = FakeFilesystem::new();
        let path = PathBuf::from("/tmp/to_remove.txt");
        fs.add_file(path.clone(), "data");
        fs.remove_file(&path).unwrap();
        assert!(!fs.exists(&path));
    }

    #[test]
    fn fake_filesystem_remove_dir_all_removes_children() {
        let fs = FakeFilesystem::new();
        fs.add_file("/skills/my-skill/SKILL.md", "---\n---\n");
        fs.add_file("/skills/my-skill/tool.py", "code");
        fs.remove_dir_all(Path::new("/skills/my-skill")).unwrap();
        assert!(!fs.exists(Path::new("/skills/my-skill/SKILL.md")));
        assert!(!fs.exists(Path::new("/skills/my-skill/tool.py")));
    }

    #[test]
    fn fake_filesystem_read_dir_lists_children() {
        let fs = FakeFilesystem::new();
        fs.add_file("/agents/alpha.md", "# agent");
        fs.add_file("/agents/beta.md", "# agent");
        let entries = fs.read_dir(Path::new("/agents")).unwrap();
        let names: BTreeSet<_> = entries.iter().map(|e| e.file_name.as_str()).collect();
        assert!(names.contains("alpha.md"));
        assert!(names.contains("beta.md"));
    }

    #[test]
    fn fake_git_client_records_clone() {
        let git = FakeGitClient::new();
        git.clone_repo("https://example.com/repo.git", Path::new("/tmp/repo")).unwrap();
        let cloned = git.cloned.borrow();
        assert_eq!(cloned.len(), 1);
        assert_eq!(cloned[0].0, "https://example.com/repo.git");
    }

    #[test]
    fn fake_git_client_records_pull() {
        let git = FakeGitClient::new();
        git.pull(Path::new("/tmp/repo")).unwrap();
        let pulled = git.pulled.borrow();
        assert_eq!(pulled.len(), 1);
        assert_eq!(pulled[0], PathBuf::from("/tmp/repo"));
    }

    #[test]
    fn fake_clock_returns_fixed_time() {
        use chrono::TimeZone;
        let fixed = Utc.with_ymd_and_hms(2024, 6, 1, 12, 0, 0).unwrap();
        let clock = FakeClock::at(fixed);
        assert_eq!(clock.now(), fixed);
    }

    #[tokio::test]
    async fn fake_llm_client_returns_canned_response() {
        let client = FakeLlmClient::new("This is the analysis.");
        let result = client.analyze("system", "user").await.unwrap();
        assert_eq!(result, "This is the analysis.");
    }

    #[tokio::test]
    async fn fake_llm_client_captures_prompts() {
        let client = FakeLlmClient::new("result");
        assert!(client.last_call().is_none(), "no calls yet");
        client.analyze("sys", "usr").await.unwrap();
        assert_eq!(
            client.last_call(),
            Some(("sys".to_string(), "usr".to_string())),
            "captures the (system, user) pair"
        );
        client.analyze("sys2", "usr2").await.unwrap();
        assert_eq!(client.all_calls().len(), 2, "accumulates calls");
        assert_eq!(
            client.last_call(),
            Some(("sys2".to_string(), "usr2".to_string())),
            "last_call returns the most recent"
        );
    }

    #[test]
    fn fake_filesystem_write_fails_on_configured_path() {
        let fs = FakeFilesystem::new();
        let fail_path = PathBuf::from("/config/restricted.json");
        let other_path = PathBuf::from("/config/allowed.json");

        fs.set_fail_on_write(fail_path.clone());

        // Write to the configured fail path returns Err
        assert!(fs.write(&fail_path, "data").is_err());

        // Write to a different path still succeeds
        assert!(fs.write(&other_path, "data").is_ok());
        assert_eq!(fs.read_to_string(&other_path).unwrap(), "data");
    }

    #[test]
    fn fake_filesystem_copy_fails_when_configured() {
        let fs = FakeFilesystem::new();
        let src = PathBuf::from("/src/file.txt");
        let dest = PathBuf::from("/dest/file.txt");
        fs.add_file(src.clone(), "content");

        fs.set_fail_on_copy(true);

        assert!(fs.copy_file(&src, &dest).is_err());
        // Verify nothing was copied
        assert!(!fs.file_exists(&dest));
    }

    #[tokio::test]
    async fn fake_llm_client_fails_when_configured() {
        let client = FakeLlmClient {
            response: "unreachable".to_string(),
            should_fail: true,
            calls: Mutex::new(Vec::new()),
        };
        let result = client.analyze("system", "user").await;
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("configured to fail"), "unexpected: {msg}");
    }

    #[test]
    fn fake_filesystem_rename_moves_file_and_removes_source() {
        let fs = FakeFilesystem::new();
        let from = PathBuf::from("/tmp/source.txt");
        let to = PathBuf::from("/tmp/dest.txt");
        fs.add_file(from.clone(), "content");

        fs.rename(&from, &to).unwrap();

        assert!(!fs.file_exists(&from), "source should be removed after rename");
        assert!(fs.file_exists(&to), "destination should exist after rename");
        assert_eq!(fs.read_to_string(&to).unwrap(), "content");
    }

    #[test]
    fn fake_filesystem_rename_replaces_existing_destination() {
        let fs = FakeFilesystem::new();
        let from = PathBuf::from("/tmp/source.txt");
        let to = PathBuf::from("/tmp/dest.txt");
        fs.add_file(from.clone(), "new content");
        fs.add_file(to.clone(), "old content");

        fs.rename(&from, &to).unwrap();

        assert!(!fs.file_exists(&from));
        assert_eq!(fs.read_to_string(&to).unwrap(), "new content");
    }

    #[test]
    fn fake_filesystem_rename_fails_on_configured_destination() {
        let fs = FakeFilesystem::new();
        let from = PathBuf::from("/tmp/source.txt");
        let to = PathBuf::from("/tmp/restricted.txt");
        fs.add_file(from.clone(), "content");
        fs.set_fail_on_rename(to.clone());

        assert!(fs.rename(&from, &to).is_err());
        // Source file should be untouched after failed rename
        assert!(fs.file_exists(&from), "source should remain after failed rename");
    }

    #[test]
    fn fake_filesystem_rename_errors_when_source_absent() {
        let fs = FakeFilesystem::new();
        let from = PathBuf::from("/tmp/nonexistent.txt");
        let to = PathBuf::from("/tmp/dest.txt");

        let result = fs.rename(&from, &to);
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("nonexistent.txt"), "unexpected: {msg}");
    }
}