agentstacks 1.0.0

Core library of agentstacks: packs, tool extensions, plans, and the executor that applies them.
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
//! A [`FileTree`] over the real disk.
//!
//! It is the one place that calls `std::fs`, so the rules in `types` stay testable without a disk
//! and every disk-specific refusal, a symlink or a path leaving the root, is decided here.
//!
//! # Responsibilities
//!
//! - [`StdFileTree`] answers the port's three reads with `std::fs::symlink_metadata`,
//!   `std::fs::read_dir` and `std::fs::read`. It reports a symlink as a symlink and never follows
//!   one: `read_dir` and `read_file` check every component of the path below the root with
//!   `symlink_metadata` just before reading, and refuse a symlink. A path swapped for a symlink
//!   after a walk checked it is therefore refused, not read. The check and the read are two
//!   system calls, so a swap in the instant between them is not prevented.
//! - [`FileTree::root`] reports a label, not the directory it reads: `StdFileTree::new` labels
//!   the tree with its directory, `StdFileTree::labelled` with a path as the author wrote it.
//! - It reads only below its directory: a path that is absolute, or that holds a `..` or a root
//!   component, is refused with [`io::ErrorKind::InvalidInput`] before any disk access.
//!
//! # Non-responsibilities
//!
//! It adds no rules about what a directory may hold and never writes. It is the only `std::fs`
//! caller in the crate.

use std::ffi::OsString;
use std::io;
use std::path::{Component, Path, PathBuf};

use crate::port::{EntryKind, FileTree};

/// The refusal for a symlink met on the way to a read, worded without naming a pack or extension.
const SYMLINK_REFUSED: &str = "symlinks are not followed";

/// A directory on the real disk, read through the [`FileTree`] port.
///
/// # Examples
///
/// ```
/// use std::path::Path;
///
/// use agentstacks::fs::StdFileTree;
/// use agentstacks::port::{EntryKind, FileTree};
///
/// let dir = tempfile::tempdir()?;
/// std::fs::write(dir.path().join("pack.toml"), "name = \"core\"\n")?;
///
/// let tree = StdFileTree::new(dir.path());
/// assert_eq!(tree.kind(Path::new("pack.toml"))?, EntryKind::File);
/// assert_eq!(tree.read_text(Path::new("pack.toml"))?, "name = \"core\"\n");
///
/// // A path that could leave the root is refused before any disk access.
/// assert!(tree.read_file(Path::new("../outside")).is_err());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct StdFileTree {
    /// The directory every read resolves under.
    dir: PathBuf,
    /// The path [`FileTree::root`] reports, the one shown in messages.
    label: PathBuf,
}

impl StdFileTree {
    /// A tree rooted at `root`, which is also the path it reports in messages. The directory is
    /// not touched until a read.
    ///
    /// # Examples
    ///
    /// ```
    /// use agentstacks::fs::StdFileTree;
    /// use agentstacks::port::FileTree;
    ///
    /// let tree = StdFileTree::new("packs/core");
    /// assert_eq!(tree.root(), std::path::Path::new("packs/core"));
    /// ```
    pub fn new(root: impl Into<PathBuf>) -> Self {
        let dir = root.into();
        Self {
            label: dir.clone(),
            dir,
        }
    }

    /// A tree that reads the directory `dir` and names itself `label` in messages, so a refusal
    /// can show a path as the author wrote it rather than the directory it resolved to. The
    /// directory is not touched until a read.
    ///
    /// # Examples
    ///
    /// ```
    /// use agentstacks::fs::StdFileTree;
    /// use agentstacks::port::FileTree;
    ///
    /// let dir = tempfile::tempdir()?;
    /// std::fs::write(dir.path().join("pack.toml"), "name = \"core\"\n")?;
    ///
    /// let tree = StdFileTree::labelled(dir.path(), "packs/core");
    /// assert_eq!(tree.root(), std::path::Path::new("packs/core"));
    /// assert_eq!(tree.read_text(std::path::Path::new("pack.toml"))?, "name = \"core\"\n");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn labelled(dir: impl Into<PathBuf>, label: impl Into<PathBuf>) -> Self {
        Self {
            dir: dir.into(),
            label: label.into(),
        }
    }

    /// `rel` under the directory, or the directory itself when `rel` is empty.
    ///
    /// # Errors
    ///
    /// [`io::ErrorKind::InvalidInput`] when `rel` could leave the root: it is absolute or holds a
    /// `..`, a root or a prefix component.
    fn resolve(&self, rel: &Path) -> io::Result<PathBuf> {
        let stays_inside = rel
            .components()
            .all(|part| matches!(part, Component::Normal(_) | Component::CurDir));
        if !stays_inside {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "path must stay inside the tree",
            ));
        }
        Ok(if rel.as_os_str().is_empty() {
            self.dir.clone()
        } else {
            self.dir.join(rel)
        })
    }

    /// Like [`resolve`](Self::resolve), and also refuses when `rel` or any directory above it
    /// below the root is a symlink at this moment. A component that does not exist is left to the
    /// read that follows, which reports it.
    ///
    /// # Errors
    ///
    /// [`io::ErrorKind::InvalidInput`] when `rel` could leave the root, or when a component is a
    /// symlink, displayed as `symlinks are not followed`.
    fn resolve_unlinked(&self, rel: &Path) -> io::Result<PathBuf> {
        let full = self.resolve(rel)?;
        let mut walked = self.dir.clone();
        for part in rel.components() {
            if let Component::Normal(name) = part {
                walked.push(name);
                if std::fs::symlink_metadata(&walked)
                    .is_ok_and(|meta| meta.file_type().is_symlink())
                {
                    return Err(io::Error::new(io::ErrorKind::InvalidInput, SYMLINK_REFUSED));
                }
            }
        }
        Ok(full)
    }
}

impl FileTree for StdFileTree {
    fn root(&self) -> &Path {
        &self.label
    }

    fn kind(&self, rel: &Path) -> io::Result<EntryKind> {
        let file_type = std::fs::symlink_metadata(self.resolve(rel)?)?.file_type();
        Ok(if file_type.is_symlink() {
            EntryKind::Symlink
        } else if file_type.is_file() {
            EntryKind::File
        } else if file_type.is_dir() {
            EntryKind::Dir
        } else {
            EntryKind::Other
        })
    }

    fn read_dir(&self, rel: &Path) -> io::Result<Vec<OsString>> {
        std::fs::read_dir(self.resolve_unlinked(rel)?)?
            .map(|entry| entry.map(|entry| entry.file_name()))
            .collect()
    }

    fn read_file(&self, rel: &Path) -> io::Result<Vec<u8>> {
        std::fs::read(self.resolve_unlinked(rel)?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::tree::{tree_hash, walk};
    use std::error::Error;
    use std::fs;

    type TestResult = Result<(), Box<dyn Error>>;

    const EXAMPLE_HASH: &str =
        "sha256:b753fa890a9101e377ba1a71365c2257312468aa5d939b5e084de025486dc99b";

    fn write(dir: &Path, relative: &str, bytes: &[u8]) -> io::Result<()> {
        let path = dir.join(relative);
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(path, bytes)
    }

    fn sorted_names(tree: &StdFileTree, rel: &str) -> io::Result<Vec<OsString>> {
        let mut names = tree.read_dir(Path::new(rel))?;
        names.sort();
        Ok(names)
    }

    #[test]
    fn root_is_the_directory_given() {
        assert_eq!(
            StdFileTree::new("packs/core").root(),
            Path::new("packs/core")
        );
    }

    #[test]
    fn new_tree_label_is_its_root() {
        let path = Path::new("some/dir");
        assert_eq!(StdFileTree::new(path).root(), path);
    }

    #[test]
    fn labelled_tree_reads_the_directory_but_names_the_label() -> TestResult {
        let temp = tempfile::tempdir()?;
        write(temp.path(), "pack.toml", b"name = \"core\"\n")?;
        write(temp.path(), "d/a.md", b"a\n")?;
        let tree = StdFileTree::labelled(temp.path(), "packs/core");
        assert_eq!(tree.root(), Path::new("packs/core"));
        assert_eq!(tree.read_text(Path::new("pack.toml"))?, "name = \"core\"\n");
        assert_eq!(tree.kind(Path::new("d"))?, EntryKind::Dir);
        assert_eq!(sorted_names(&tree, "d")?, ["a.md"]);
        assert_eq!(tree.read_file(Path::new("d/a.md"))?, b"a\n");
        Ok(())
    }

    #[test]
    fn kind_tells_a_file_from_a_directory() -> TestResult {
        let temp = tempfile::tempdir()?;
        write(temp.path(), "d/a.md", b"a\n")?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(tree.kind(Path::new("d"))?, EntryKind::Dir);
        assert_eq!(tree.kind(Path::new("d/a.md"))?, EntryKind::File);
        Ok(())
    }

    #[test]
    fn read_dir_lists_names_below_a_relative_path_and_the_root() -> TestResult {
        let temp = tempfile::tempdir()?;
        write(temp.path(), "a.md", b"")?;
        write(temp.path(), "d/b.md", b"")?;
        write(temp.path(), "d/e/c.md", b"")?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(sorted_names(&tree, "")?, ["a.md", "d"]);
        assert_eq!(sorted_names(&tree, "d")?, ["b.md", "e"]);
        Ok(())
    }

    #[test]
    fn read_file_returns_the_bytes() -> TestResult {
        let temp = tempfile::tempdir()?;
        write(temp.path(), "d/a.md", b"a\r\n")?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(tree.read_file(Path::new("d/a.md"))?, b"a\r\n");
        Ok(())
    }

    #[test]
    fn a_missing_path_is_not_found() -> TestResult {
        let temp = tempfile::tempdir()?;
        let tree = StdFileTree::new(temp.path().join("missing"));
        let kinds = [
            tree.kind(Path::new("")).err().map(|e| e.kind()),
            tree.read_dir(Path::new("")).err().map(|e| e.kind()),
            tree.read_file(Path::new("a.md")).err().map(|e| e.kind()),
        ];
        assert_eq!(kinds, [Some(io::ErrorKind::NotFound); 3]);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn a_symlink_is_reported_and_not_followed() -> TestResult {
        let temp = tempfile::tempdir()?;
        let target = tempfile::tempdir()?;
        write(temp.path(), "a.md", b"a\n")?;
        std::os::unix::fs::symlink(temp.path().join("a.md"), temp.path().join("link"))?;
        std::os::unix::fs::symlink(target.path(), temp.path().join("sub"))?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(tree.kind(Path::new("link"))?, EntryKind::Symlink);
        assert_eq!(tree.kind(Path::new("sub"))?, EntryKind::Symlink);
        assert_eq!(tree.kind(Path::new("a.md"))?, EntryKind::File);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn a_file_swapped_for_a_symlink_after_the_walk_is_refused() -> TestResult {
        let temp = tempfile::tempdir()?;
        let outside = tempfile::tempdir()?;
        write(temp.path(), "a.md", b"a\n")?;
        write(outside.path(), "secret", b"secret\n")?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(tree.read_file(Path::new("a.md"))?, b"a\n");

        fs::remove_file(temp.path().join("a.md"))?;
        std::os::unix::fs::symlink(outside.path().join("secret"), temp.path().join("a.md"))?;
        let error = tree.read_file(Path::new("a.md")).err();
        assert_eq!(
            error.as_ref().map(io::Error::kind),
            Some(io::ErrorKind::InvalidInput)
        );
        assert_eq!(
            error.map(|e| e.to_string()),
            Some("symlinks are not followed".to_owned())
        );
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn a_directory_swapped_for_a_symlink_after_the_walk_is_refused() -> TestResult {
        let temp = tempfile::tempdir()?;
        let outside = tempfile::tempdir()?;
        write(temp.path(), "d/a.md", b"a\n")?;
        write(outside.path(), "a.md", b"outside\n")?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(tree.read_dir(Path::new("d"))?, ["a.md"]);

        fs::remove_dir_all(temp.path().join("d"))?;
        std::os::unix::fs::symlink(outside.path(), temp.path().join("d"))?;
        let refused = [
            tree.read_dir(Path::new("d")).err().map(|e| e.to_string()),
            tree.read_file(Path::new("d/a.md"))
                .err()
                .map(|e| e.to_string()),
        ];
        let message = Some("symlinks are not followed".to_owned());
        assert_eq!(refused, [message.clone(), message]);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn a_symlinked_root_is_still_read() -> TestResult {
        let temp = tempfile::tempdir()?;
        let real = tempfile::tempdir()?;
        write(real.path(), "a.md", b"a\n")?;
        let link = temp.path().join("root");
        std::os::unix::fs::symlink(real.path(), &link)?;
        assert_eq!(
            StdFileTree::new(&link).read_file(Path::new("a.md"))?,
            b"a\n"
        );
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn a_unix_socket_is_other() -> TestResult {
        let temp = tempfile::tempdir()?;
        let _socket = std::os::unix::net::UnixListener::bind(temp.path().join("sock"))?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(tree.kind(Path::new("sock"))?, EntryKind::Other);
        Ok(())
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn a_non_utf8_name_is_listed_as_is() -> TestResult {
        use std::ffi::OsStr;
        use std::os::unix::ffi::OsStrExt;

        let temp = tempfile::tempdir()?;
        let bad = OsStr::from_bytes(&[0x66, 0xFF]);
        fs::write(temp.path().join(bad), b"x")?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(tree.read_dir(Path::new(""))?, [bad.to_os_string()]);
        assert_eq!(tree.kind(Path::new(bad))?, EntryKind::File);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn an_unreadable_file_returns_an_io_error() -> TestResult {
        use std::os::unix::fs::PermissionsExt;

        let temp = tempfile::tempdir()?;
        write(temp.path(), "a.md", b"a\n")?;
        let locked = temp.path().join("a.md");
        fs::set_permissions(&locked, fs::Permissions::from_mode(0o000))?;
        let readable = fs::read(&locked).is_ok();
        let result = StdFileTree::new(temp.path()).read_file(Path::new("a.md"));
        fs::set_permissions(&locked, fs::Permissions::from_mode(0o644))?;
        // A process that ignores file modes (root) can still read the file; nothing to refuse.
        if !readable {
            assert_eq!(
                result.err().map(|e| e.kind()),
                Some(io::ErrorKind::PermissionDenied)
            );
        }
        Ok(())
    }

    #[test]
    fn a_path_that_could_leave_the_root_is_refused_before_any_read() -> TestResult {
        let temp = tempfile::tempdir()?;
        let outside = temp.path().join("outside.md");
        write(temp.path(), "outside.md", b"secret\n")?;
        write(temp.path(), "root/inside.md", b"inside\n")?;
        let tree = StdFileTree::new(temp.path().join("root"));
        let refused = [
            Path::new("../outside.md"),
            Path::new("a/../../outside.md"),
            Path::new(".."),
            outside.as_path(),
            Path::new("/"),
        ];
        for rel in refused {
            let kinds = [
                tree.kind(rel).err().map(|e| e.kind()),
                tree.read_dir(rel).err().map(|e| e.kind()),
                tree.read_file(rel).err().map(|e| e.kind()),
            ];
            assert_eq!(
                kinds,
                [Some(io::ErrorKind::InvalidInput); 3],
                "{}",
                rel.display()
            );
        }
        let error = tree.read_file(Path::new("../outside.md")).err();
        assert_eq!(
            error.map(|e| e.to_string()),
            Some("path must stay inside the tree".to_owned())
        );
        Ok(())
    }

    #[test]
    fn a_path_below_the_root_is_still_read() -> TestResult {
        let temp = tempfile::tempdir()?;
        write(temp.path(), "d/a.md", b"a\n")?;
        let tree = StdFileTree::new(temp.path());
        assert_eq!(tree.read_file(Path::new("./d/a.md"))?, b"a\n");
        assert_eq!(tree.kind(Path::new("."))?, EntryKind::Dir);
        assert_eq!(tree.kind(Path::new(""))?, EntryKind::Dir);
        Ok(())
    }

    #[test]
    fn tree_hash_over_the_disk_matches_the_example_tree() -> TestResult {
        let temp = tempfile::tempdir()?;
        write(temp.path(), "a.md", b"a\n")?;
        write(temp.path(), "b/c.lua", b"x\n")?;
        let hash = tree_hash(&StdFileTree::new(temp.path())).map_err(|d| format!("{d:?}"))?;
        assert_eq!(hash.to_string(), EXAMPLE_HASH);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn tree_hash_over_the_disk_ignores_file_modes() -> TestResult {
        use std::os::unix::fs::PermissionsExt;

        let temp = tempfile::tempdir()?;
        write(temp.path(), "a.md", b"a\n")?;
        write(temp.path(), "b/c.lua", b"x\n")?;
        fs::set_permissions(
            temp.path().join("b/c.lua"),
            fs::Permissions::from_mode(0o755),
        )?;
        let hash = tree_hash(&StdFileTree::new(temp.path())).map_err(|d| format!("{d:?}"))?;
        assert_eq!(hash.to_string(), EXAMPLE_HASH);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn walk_over_the_disk_refuses_a_symlink_at_the_root_path() -> TestResult {
        let temp = tempfile::tempdir()?;
        write(temp.path(), "a.md", b"a\n")?;
        std::os::unix::fs::symlink(temp.path().join("a.md"), temp.path().join("link"))?;
        let (walked, diagnostics) = walk(&StdFileTree::new(temp.path()));
        let messages: Vec<String> = diagnostics
            .into_sorted()
            .iter()
            .map(ToString::to_string)
            .collect();
        assert_eq!(walked.files().len(), 1);
        assert_eq!(
            messages,
            [format!(
                "{}/link: symlinks are not allowed in a pack",
                temp.path().display()
            )]
        );
        Ok(())
    }

    #[test]
    fn walk_over_a_missing_directory_reports_the_platform_error() -> TestResult {
        let temp = tempfile::tempdir()?;
        let missing = temp.path().join("missing");
        let cause = fs::read_dir(&missing).err().map(|e| e.to_string());
        let messages: Vec<String> = walk(&StdFileTree::new(&missing))
            .1
            .into_sorted()
            .iter()
            .map(ToString::to_string)
            .collect();
        assert_eq!(
            messages,
            cause
                .map(|c| format!("{}: {c}", missing.display()))
                .into_iter()
                .collect::<Vec<_>>()
        );
        Ok(())
    }
}