cmx-core 0.2.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
use anyhow::{Context, Result, bail};
use chrono::{DateTime, Utc};
use std::path::{Path, PathBuf};
use std::process::Command;

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

#[cfg(feature = "llm")]
use mojentic::llm::gateways::{OllamaGateway, OpenAIGateway};
#[cfg(feature = "llm")]
use mojentic::llm::{LlmBroker, LlmGateway, LlmMessage};
#[cfg(feature = "llm")]
use std::future::Future;
#[cfg(feature = "llm")]
use std::pin::Pin;
#[cfg(feature = "llm")]
use std::sync::Arc;

#[cfg(feature = "llm")]
use super::llm::LlmClient;
#[cfg(feature = "llm")]
use crate::types::{LlmConfig, LlmGatewayType};

// ---------------------------------------------------------------------------
// RealFilesystem
// ---------------------------------------------------------------------------

/// Production implementation of [`Filesystem`] that delegates to `std::fs`.
pub struct RealFilesystem;

impl Filesystem for RealFilesystem {
    fn exists(&self, path: &Path) -> bool {
        path.exists()
    }

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

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

    fn read_to_string(&self, path: &Path) -> Result<String> {
        std::fs::read_to_string(path).with_context(|| format!("Failed to read {}", path.display()))
    }

    fn read(&self, path: &Path) -> Result<Vec<u8>> {
        std::fs::read(path).with_context(|| format!("Failed to read {}", path.display()))
    }

    fn write(&self, path: &Path, contents: &str) -> Result<()> {
        std::fs::write(path, contents)
            .with_context(|| format!("Failed to write {}", path.display()))
    }

    fn write_bytes(&self, path: &Path, contents: &[u8]) -> Result<()> {
        std::fs::write(path, contents)
            .with_context(|| format!("Failed to write {}", path.display()))
    }

    fn create_dir_all(&self, path: &Path) -> Result<()> {
        std::fs::create_dir_all(path)
            .with_context(|| format!("Failed to create directory {}", path.display()))
    }

    fn copy_file(&self, src: &Path, dest: &Path) -> Result<()> {
        std::fs::copy(src, dest)
            .with_context(|| format!("Failed to copy {} to {}", src.display(), dest.display()))?;
        Ok(())
    }

    fn rename(&self, from: &Path, to: &Path) -> Result<()> {
        std::fs::rename(from, to)
            .with_context(|| format!("Failed to rename {} to {}", from.display(), to.display()))
    }

    fn remove_file(&self, path: &Path) -> Result<()> {
        std::fs::remove_file(path)
            .with_context(|| format!("Failed to remove file {}", path.display()))
    }

    fn remove_dir_all(&self, path: &Path) -> Result<()> {
        std::fs::remove_dir_all(path)
            .with_context(|| format!("Failed to remove directory {}", path.display()))
    }

    fn read_dir(&self, path: &Path) -> Result<Vec<DirEntry>> {
        let entries = std::fs::read_dir(path)
            .with_context(|| format!("Failed to read directory {}", path.display()))?;

        let mut result = Vec::new();
        for entry in entries {
            let entry =
                entry.with_context(|| format!("Failed to read entry in {}", path.display()))?;
            let file_name = entry.file_name().to_string_lossy().to_string();
            let entry_path = entry.path();
            let is_dir = entry_path.is_dir();
            result.push(DirEntry {
                path: entry_path,
                file_name,
                is_dir,
            });
        }

        Ok(result)
    }

    fn canonicalize(&self, path: &Path) -> Result<PathBuf> {
        path.canonicalize()
            .with_context(|| format!("Failed to canonicalize {}", path.display()))
    }
}

// ---------------------------------------------------------------------------
// RealGitClient
// ---------------------------------------------------------------------------

/// Production implementation of [`GitClient`] that shells out to `git`.
pub struct RealGitClient;

impl GitClient for RealGitClient {
    fn clone_repo(&self, url: &str, dest: &Path) -> Result<()> {
        let output = Command::new("git")
            .args(["clone", url, &dest.display().to_string()])
            .output()
            .context("Failed to run git clone")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("git clone failed: {stderr}");
        }

        Ok(())
    }

    fn pull(&self, repo_path: &Path) -> Result<()> {
        let output = Command::new("git")
            .args(["-C", &repo_path.display().to_string(), "pull", "--quiet"])
            .output()
            .context("Failed to run git pull")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("git pull failed: {stderr}");
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// SystemClock
// ---------------------------------------------------------------------------

/// Production implementation of [`Clock`] that returns `Utc::now()`.
pub struct SystemClock;

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

// ---------------------------------------------------------------------------
// MojenticLlmClient
// ---------------------------------------------------------------------------

/// Production [`LlmClient`] that delegates to the mojentic LLM library.
#[cfg(feature = "llm")]
pub struct MojenticLlmClient {
    config: LlmConfig,
}

#[cfg(feature = "llm")]
impl MojenticLlmClient {
    pub fn new(config: LlmConfig) -> Self {
        Self { config }
    }

    fn make_broker(&self) -> LlmBroker {
        let gateway: Arc<dyn LlmGateway + Send + Sync> = match self.config.gateway {
            LlmGatewayType::OpenAI => Arc::new(OpenAIGateway::default()),
            LlmGatewayType::Ollama => Arc::new(OllamaGateway::new()),
        };
        LlmBroker::new(&self.config.model, gateway, None)
    }
}

#[cfg(feature = "llm")]
impl LlmClient for MojenticLlmClient {
    fn analyze(
        &self,
        system_prompt: &str,
        user_prompt: &str,
    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + '_>> {
        // not unit-tested: live network boundary
        let broker = self.make_broker();
        let messages = vec![
            LlmMessage::system(system_prompt),
            LlmMessage::user(user_prompt),
        ];
        Box::pin(async move {
            broker
                .generate(&messages, None, None, None)
                .await
                .context("LLM analysis failed")
        })
    }
}

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

    // -----------------------------------------------------------------------
    // RealFilesystem
    // -----------------------------------------------------------------------

    #[test]
    fn real_filesystem_write_read_string_roundtrip() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("hello.txt");
        let fs = RealFilesystem;
        fs.write(&path, "hello world").unwrap();
        assert_eq!(fs.read_to_string(&path).unwrap(), "hello world");
    }

    #[test]
    fn real_filesystem_write_bytes_read_roundtrip() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("bytes.bin");
        let fs = RealFilesystem;
        let data = vec![0u8, 1, 2, 255];
        fs.write_bytes(&path, &data).unwrap();
        assert_eq!(fs.read(&path).unwrap(), data);
    }

    #[test]
    fn real_filesystem_create_dir_all_nested() {
        let dir = TempDir::new().unwrap();
        let nested = dir.path().join("a").join("b").join("c");
        let fs = RealFilesystem;
        fs.create_dir_all(&nested).unwrap();
        assert!(nested.is_dir());
    }

    #[test]
    fn real_filesystem_copy_file() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("src.txt");
        let dst = dir.path().join("dst.txt");
        let fs = RealFilesystem;
        fs.write(&src, "copy me").unwrap();
        fs.copy_file(&src, &dst).unwrap();
        assert_eq!(fs.read_to_string(&dst).unwrap(), "copy me");
        // source still exists
        assert!(fs.exists(&src));
    }

    #[test]
    fn real_filesystem_rename() {
        let dir = TempDir::new().unwrap();
        let from = dir.path().join("from.txt");
        let to = dir.path().join("to.txt");
        let fs = RealFilesystem;
        fs.write(&from, "data").unwrap();
        fs.rename(&from, &to).unwrap();
        assert!(!from.exists());
        assert_eq!(fs.read_to_string(&to).unwrap(), "data");
    }

    #[test]
    fn real_filesystem_remove_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("tmp.txt");
        let fs = RealFilesystem;
        fs.write(&path, "").unwrap();
        assert!(fs.exists(&path));
        fs.remove_file(&path).unwrap();
        assert!(!fs.exists(&path));
    }

    #[test]
    fn real_filesystem_remove_dir_all() {
        let dir = TempDir::new().unwrap();
        let sub = dir.path().join("sub");
        let file = sub.join("file.txt");
        let fs = RealFilesystem;
        fs.create_dir_all(&sub).unwrap();
        fs.write(&file, "x").unwrap();
        fs.remove_dir_all(&sub).unwrap();
        assert!(!sub.exists());
    }

    #[test]
    fn real_filesystem_read_dir_entries() {
        let dir = TempDir::new().unwrap();
        let fs = RealFilesystem;
        fs.write(&dir.path().join("alpha.txt"), "a").unwrap();
        fs.write(&dir.path().join("beta.txt"), "b").unwrap();
        let entries = fs.read_dir(dir.path()).unwrap();
        let names: std::collections::BTreeSet<_> =
            entries.iter().map(|e| e.file_name.as_str()).collect();
        assert!(names.contains("alpha.txt"));
        assert!(names.contains("beta.txt"));
        // Verify DirEntry fields
        for entry in &entries {
            assert!(!entry.is_dir);
            assert!(entry.path.exists());
        }
    }

    #[test]
    fn real_filesystem_read_dir_contains_subdir_entry() {
        let dir = TempDir::new().unwrap();
        let sub = dir.path().join("subdir");
        let fs = RealFilesystem;
        fs.create_dir_all(&sub).unwrap();
        let entries = fs.read_dir(dir.path()).unwrap();
        let subdir_entry = entries.iter().find(|e| e.file_name == "subdir").unwrap();
        assert!(subdir_entry.is_dir);
    }

    #[test]
    fn real_filesystem_exists_is_dir_is_file() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("f.txt");
        let fs = RealFilesystem;

        assert!(!fs.exists(&path));
        assert!(!fs.is_file(&path));
        assert!(!fs.is_dir(&path));

        fs.write(&path, "").unwrap();
        assert!(fs.exists(&path));
        assert!(fs.is_file(&path));
        assert!(!fs.is_dir(&path));

        assert!(fs.is_dir(dir.path()));
        assert!(!fs.is_file(dir.path()));
    }

    #[test]
    fn real_filesystem_canonicalize() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("c.txt");
        let fs = RealFilesystem;
        fs.write(&path, "").unwrap();
        let canonical = fs.canonicalize(&path).unwrap();
        assert!(canonical.is_absolute());
        assert!(canonical.exists());
    }

    #[test]
    fn real_filesystem_read_to_string_missing_returns_err() {
        let dir = TempDir::new().unwrap();
        let missing = dir.path().join("no_such_file.txt");
        let fs = RealFilesystem;
        let err = fs.read_to_string(&missing).unwrap_err();
        assert!(err.to_string().contains("Failed to read"));
    }

    // -----------------------------------------------------------------------
    // SystemClock
    // -----------------------------------------------------------------------

    #[test]
    fn system_clock_returns_current_wall_time() {
        let before = Utc::now();
        let clock = SystemClock;
        let result = clock.now();
        let after = Utc::now();
        assert!(result >= before, "clock result should be >= before");
        assert!(result <= after, "clock result should be <= after");
    }

    // -----------------------------------------------------------------------
    // RealGitClient
    // -----------------------------------------------------------------------

    fn git_available() -> bool {
        Command::new("git").arg("--version").output().is_ok()
    }

    #[test]
    fn real_git_client_clone_and_pull() {
        if !git_available() {
            eprintln!("git not found on PATH — skipping RealGitClient tests");
            return;
        }

        let source_dir = TempDir::new().unwrap();
        // Init a bare-ish source repo with one commit
        Command::new("git")
            .args(["init", "-b", "main"])
            .current_dir(source_dir.path())
            .output()
            .unwrap();
        Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(source_dir.path())
            .output()
            .unwrap();
        Command::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(source_dir.path())
            .output()
            .unwrap();
        std::fs::write(source_dir.path().join("README.md"), "hello").unwrap();
        Command::new("git")
            .args(["add", "."])
            .current_dir(source_dir.path())
            .output()
            .unwrap();
        Command::new("git")
            .args(["commit", "-m", "init"])
            .current_dir(source_dir.path())
            .output()
            .unwrap();

        let clone_dir = TempDir::new().unwrap();
        let dest = clone_dir.path().join("repo");
        let client = RealGitClient;

        client.clone_repo(&source_dir.path().display().to_string(), &dest).unwrap();
        assert!(dest.join("README.md").exists());

        // pull should succeed on an up-to-date clone
        client.pull(&dest).unwrap();
    }

    #[test]
    fn real_git_client_clone_nonexistent_returns_err() {
        if !git_available() {
            eprintln!("git not found on PATH — skipping RealGitClient error test");
            return;
        }
        let dest = TempDir::new().unwrap();
        let client = RealGitClient;
        let err = client
            .clone_repo("/nonexistent/path/that/does/not/exist", dest.path())
            .unwrap_err();
        assert!(err.to_string().contains("git clone failed"));
    }

    // -----------------------------------------------------------------------
    // MojenticLlmClient (hermetic only — no network calls)
    // -----------------------------------------------------------------------

    #[cfg(feature = "llm")]
    #[test]
    fn mojentic_llm_client_new_stores_config() {
        use crate::types::{LlmConfig, LlmGatewayType};
        let config = LlmConfig {
            gateway: LlmGatewayType::OpenAI,
            model: "gpt-4o".to_string(),
        };
        let client = MojenticLlmClient::new(config.clone());
        assert_eq!(client.config.model, "gpt-4o");
        assert_eq!(client.config.gateway, LlmGatewayType::OpenAI);
    }

    #[cfg(feature = "llm")]
    #[test]
    fn mojentic_llm_client_make_broker_openai_does_not_panic() {
        use crate::types::{LlmConfig, LlmGatewayType};
        let client = MojenticLlmClient::new(LlmConfig {
            gateway: LlmGatewayType::OpenAI,
            model: "gpt-4o".to_string(),
        });
        let _broker = client.make_broker();
    }

    #[cfg(feature = "llm")]
    #[test]
    fn mojentic_llm_client_make_broker_ollama_does_not_panic() {
        use crate::types::{LlmConfig, LlmGatewayType};
        let client = MojenticLlmClient::new(LlmConfig {
            gateway: LlmGatewayType::Ollama,
            model: "llama3".to_string(),
        });
        let _broker = client.make_broker();
    }
}