nodus 0.17.0

Local-first CLI for managing project-scoped agent packages.
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
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;

use anyhow::{Context, Result};
use rayon::prelude::*;
use tempfile::{Builder, NamedTempFile};

use crate::paths::strip_path_prefix;

pub const STORE_ROOT: &str = "store/blake3";

#[derive(Debug, Clone)]
pub struct StoredPackage {
    pub digest: String,
    pub snapshot_root: PathBuf,
}

pub trait SnapshotSource: Sync {
    fn digest(&self) -> &str;
    fn package_root(&self) -> &Path;
    fn package_files(&self) -> Result<Vec<PathBuf>>;
    fn read_package_file(&self, path: &Path) -> Result<Vec<u8>>;
}

pub fn snapshot_packages<T: SnapshotSource>(
    cache_root: &Path,
    packages: &[T],
) -> Result<Vec<StoredPackage>> {
    let store_root = cache_root.join(STORE_ROOT);
    fs::create_dir_all(&store_root)
        .with_context(|| format!("failed to create store root {}", store_root.display()))?;

    packages
        .par_iter()
        .map(|package| {
            let snapshot_root = snapshot_package(&store_root, package)?;
            Ok(StoredPackage {
                digest: package.digest().to_string(),
                snapshot_root,
            })
        })
        .collect::<Vec<_>>()
        .into_iter()
        .collect()
}

pub fn snapshot_path(cache_root: &Path, digest: &str) -> Result<PathBuf> {
    Ok(cache_root
        .join(STORE_ROOT)
        .join(digest_directory_name(digest)?))
}

pub fn write_atomic(path: &Path, contents: &[u8]) -> Result<()> {
    let parent = path
        .parent()
        .ok_or_else(|| anyhow::anyhow!("cannot atomically write {}", path.display()))?;
    fs::create_dir_all(parent)
        .with_context(|| format!("failed to create parent directory {}", parent.display()))?;

    let mut temp = NamedTempFile::new_in(parent)
        .with_context(|| format!("failed to create temp file in {}", parent.display()))?;
    temp.write_all(contents)
        .with_context(|| format!("failed to write temp file for {}", path.display()))?;
    temp.flush()
        .with_context(|| format!("failed to flush temp file for {}", path.display()))?;
    persist_temp_file_with_retry(temp, path)?;

    Ok(())
}

fn persist_temp_file_with_retry(mut temp: NamedTempFile, path: &Path) -> Result<()> {
    let mut attempt = 0;
    loop {
        match temp.persist(path) {
            Ok(_) => return Ok(()),
            Err(error) => {
                let tempfile::PersistError { file, error } = error;
                let retry_delay = atomic_persist_retry_delay(&error, attempt);
                let Some(delay) = retry_delay else {
                    return Err(error).with_context(|| {
                        format!(
                            "failed to persist atomically written file to {}",
                            path.display()
                        )
                    });
                };

                temp = file;
                attempt += 1;
                std::thread::sleep(delay);
            }
        }
    }
}

fn atomic_persist_retry_delay(error: &std::io::Error, attempt: usize) -> Option<Duration> {
    const WINDOWS_RETRY_DELAYS_MS: [u64; 5] = [10, 25, 50, 100, 200];

    if !cfg!(windows) {
        return None;
    }
    if !is_transient_windows_atomic_replace_error(error) {
        return None;
    }

    WINDOWS_RETRY_DELAYS_MS
        .get(attempt)
        .copied()
        .map(Duration::from_millis)
}

fn is_transient_windows_atomic_replace_error(error: &std::io::Error) -> bool {
    matches!(error.kind(), std::io::ErrorKind::PermissionDenied)
        || matches!(error.raw_os_error(), Some(5 | 32))
}

fn snapshot_package<T: SnapshotSource>(store_root: &Path, package: &T) -> Result<PathBuf> {
    let digest_dir_name = digest_directory_name(package.digest())?;
    let digest_dir = store_root.join(digest_dir_name);
    let files = package.package_files()?;
    if digest_dir.exists() {
        if snapshot_is_complete(&digest_dir, package.package_root(), &files)? {
            return Ok(digest_dir);
        }

        match fs::remove_dir_all(&digest_dir) {
            Ok(()) => {}
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => {
                return Err(error).with_context(|| {
                    format!(
                        "failed to remove incomplete snapshot {}",
                        digest_dir.display()
                    )
                });
            }
        }
    }

    if digest_dir.exists() {
        return Ok(digest_dir);
    }

    let staging = Builder::new()
        .prefix(&format!(".tmp-{}-", digest_dir_name.replace('/', "_")))
        .tempdir_in(store_root)
        .with_context(|| {
            format!(
                "failed to create staging dir for snapshot {}",
                digest_dir.display()
            )
        })?;
    let staging_root = staging.path().to_path_buf();

    for file in files {
        let relative = strip_path_prefix(&file, package.package_root()).with_context(|| {
            format!("failed to make {} relative to package root", file.display())
        })?;
        let target = staging_root.join(relative);
        if let Some(parent) = target.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("failed to create snapshot directory {}", parent.display())
            })?;
        }
        let contents = package
            .read_package_file(&file)
            .with_context(|| format!("failed to read {} for snapshot", file.display()))?;
        write_atomic(&target, &contents).with_context(|| {
            format!(
                "failed to copy {} into snapshot {}",
                file.display(),
                target.display()
            )
        })?;
    }

    if let Some(parent) = digest_dir.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create store parent {}", parent.display()))?;
    }

    match fs::rename(&staging_root, &digest_dir) {
        Ok(()) => {
            let _ = staging.keep();
            Ok(digest_dir)
        }
        Err(_) if digest_dir.exists() => Ok(digest_dir),
        Err(error) => Err(error).with_context(|| {
            format!(
                "failed to promote snapshot {} into {}",
                staging_root.display(),
                digest_dir.display()
            )
        }),
    }
}

fn snapshot_is_complete(
    snapshot_root: &Path,
    package_root: &Path,
    files: &[PathBuf],
) -> Result<bool> {
    for file in files {
        let relative = file.strip_prefix(package_root).with_context(|| {
            format!("failed to make {} relative to package root", file.display())
        })?;
        if !snapshot_root.join(relative).is_file() {
            return Ok(false);
        }
    }

    Ok(true)
}

fn digest_directory_name(digest: &str) -> Result<&str> {
    digest
        .strip_prefix("blake3:")
        .or_else(|| digest.strip_prefix("sha256:"))
        .ok_or_else(|| anyhow::anyhow!("unsupported digest format `{digest}`"))
}

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

    use tempfile::TempDir;

    use super::*;
    use crate::report::Reporter;
    use crate::resolver::resolve_project_for_sync;

    fn write_file(path: &Path, contents: &str) {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        let mut file = fs::File::create(path).unwrap();
        file.write_all(contents.as_bytes()).unwrap();
    }

    #[test]
    fn snapshots_package_contents_into_the_local_store() {
        let temp = TempDir::new().unwrap();
        let cache = TempDir::new().unwrap();
        write_file(
            &temp.path().join("skills/review/SKILL.md"),
            "---\nname: Review\ndescription: Example.\n---\n# Review\n",
        );

        let reporter = Reporter::silent();
        let resolution = resolve_project_for_sync(temp.path(), cache.path(), &reporter).unwrap();
        let stored = snapshot_packages(cache.path(), &resolution.packages).unwrap();

        assert_eq!(stored.len(), 1);
        assert!(
            stored[0]
                .snapshot_root
                .starts_with(cache.path().join(STORE_ROOT))
        );
        assert!(!stored[0].snapshot_root.starts_with(temp.path()));
        assert!(
            stored[0]
                .snapshot_root
                .join("skills/review/SKILL.md")
                .exists()
        );
    }

    #[test]
    fn recreates_incomplete_snapshots() {
        let temp = TempDir::new().unwrap();
        let cache = TempDir::new().unwrap();
        write_file(
            &temp.path().join("skills/review/SKILL.md"),
            "---\nname: Review\ndescription: Example.\n---\n# Review\n",
        );
        write_file(
            &temp.path().join("rules/common/coding-style.md"),
            "be consistent\n",
        );

        let reporter = Reporter::silent();
        let resolution = resolve_project_for_sync(temp.path(), cache.path(), &reporter).unwrap();
        let stored = snapshot_packages(cache.path(), &resolution.packages).unwrap();
        let snapshot_root = &stored[0].snapshot_root;

        fs::remove_file(snapshot_root.join("rules/common/coding-style.md")).unwrap();
        let rebuilt = snapshot_packages(cache.path(), &resolution.packages).unwrap();

        assert_eq!(rebuilt[0].snapshot_root, *snapshot_root);
        assert!(
            rebuilt[0]
                .snapshot_root
                .join("rules/common/coding-style.md")
                .exists()
        );
    }

    #[test]
    fn reuses_the_same_snapshot_for_duplicate_package_digests() {
        let temp = TempDir::new().unwrap();
        let cache = TempDir::new().unwrap();
        write_file(
            &temp.path().join("nodus.toml"),
            r#"
[dependencies]
alpha = { path = "vendor/alpha" }
beta = { path = "vendor/beta" }
"#,
        );
        write_file(
            &temp.path().join("vendor/alpha/skills/shared/SKILL.md"),
            "---\nname: Shared\ndescription: Example.\n---\n# Shared\n",
        );
        write_file(
            &temp.path().join("vendor/beta/skills/shared/SKILL.md"),
            "---\nname: Shared\ndescription: Example.\n---\n# Shared\n",
        );

        let reporter = Reporter::silent();
        let resolution = resolve_project_for_sync(temp.path(), cache.path(), &reporter).unwrap();
        let stored = snapshot_packages(cache.path(), &resolution.packages).unwrap();

        let mut dependency_digests = resolution
            .packages
            .iter()
            .filter(|package| matches!(package.alias.as_str(), "alpha" | "beta"))
            .map(|package| package.digest.clone())
            .collect::<Vec<_>>();
        dependency_digests.sort();
        dependency_digests.dedup();
        assert_eq!(dependency_digests.len(), 1);

        let dependency_snapshots = stored
            .iter()
            .filter(|package| package.digest == dependency_digests[0])
            .map(|package| package.snapshot_root.clone())
            .collect::<Vec<_>>();
        assert_eq!(dependency_snapshots.len(), 2);
        assert_eq!(dependency_snapshots[0], dependency_snapshots[1]);
        assert!(
            dependency_snapshots[0]
                .join("skills/shared/SKILL.md")
                .is_file()
        );
    }

    #[test]
    fn digest_directory_name_accepts_blake3_prefix() {
        assert_eq!(digest_directory_name("blake3:abc123").unwrap(), "abc123");
    }

    #[test]
    fn digest_directory_name_accepts_legacy_sha256_prefix() {
        assert_eq!(digest_directory_name("sha256:abc123").unwrap(), "abc123");
    }

    #[test]
    fn digest_directory_name_rejects_unknown_prefix() {
        assert!(digest_directory_name("md5:abc123").is_err());
    }

    #[test]
    fn atomically_writes_files() {
        let temp = TempDir::new().unwrap();
        let target = temp.path().join("nested/output.txt");

        write_atomic(&target, b"hello").unwrap();

        assert_eq!(fs::read_to_string(target).unwrap(), "hello");
    }

    #[cfg(windows)]
    #[test]
    fn atomically_replaces_file_after_transient_windows_lock() {
        use std::os::windows::fs::OpenOptionsExt;
        use std::sync::mpsc;
        use std::thread;
        use std::time::Duration;

        let temp = TempDir::new().unwrap();
        let target = temp.path().join("output.txt");
        write_file(&target, "old");

        let locked = fs::OpenOptions::new()
            .read(true)
            .share_mode(0)
            .open(&target)
            .unwrap();

        let (started_tx, started_rx) = mpsc::channel();
        let target_for_writer = target.clone();
        let writer = thread::spawn(move || {
            started_tx.send(()).unwrap();
            write_atomic(&target_for_writer, b"new")
        });
        started_rx.recv().unwrap();
        thread::sleep(Duration::from_millis(50));
        drop(locked);

        writer.join().unwrap().unwrap();
        assert_eq!(fs::read_to_string(target).unwrap(), "new");
    }
}