agentstacks 0.2.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
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
//! A tool extension directory, loaded and checked.
//!
//! It is separate from [`ExtensionManifest`] because it is the only unit that reads a directory:
//! it walks the tree, reads the manifest and hashes the whole, where the manifest only parses text.
//!
//! # Responsibilities
//!
//! - [`Extension::load`] turns the directory behind a [`FileTree`] into a checked [`Extension`], or
//!   every refusal it contains.
//! - It walks the directory once under the shared layout rules: dot entries are skipped first,
//!   empty directories are ignored, and symlinks, special files and non-UTF-8 paths are refused,
//!   with the symlink refusal worded for an extension. A walk refusal does not stop the load: it is
//!   reported together with every other refusal.
//! - It reads every walked file exactly once, hashing it as it goes; the manifest's text comes from
//!   that same read. A file that cannot be read is an I/O refusal at its own path, reported with
//!   every other refusal, and leaves the extension without a tree hash.
//! - It requires `extension.toml` (a manifest the walk refused is not reported as absent), parses
//!   it, and checks that `entry`, without its leading `./` segments, is one of the walked regular
//!   files. An `entry` the walk refused is not reported as missing. Every refusal of those checks
//!   is reported together. A manifest that is not UTF-8 is refused as `<file>: not valid UTF-8`.
//! - When nothing is refused it records the directory's tree hash and the dot entries it ignored.
//!
//! # Non-responsibilities
//!
//! What the adapter exports and how it runs belongs to the extension host. Comparing the tree hash
//! with the lock belongs to the plan and apply stage that reconciles a lock with the extensions on
//! disk, not to the lock's own types.

use std::path::{Path, PathBuf};

use crate::port::FileTree;
use crate::types::extension::error::ExtensionError;
use crate::types::extension::manifest::ExtensionManifest;
use crate::types::project::SubPath;
use crate::types::report::Diagnostic;
use crate::types::report::Diagnostics;
use crate::types::report::DoesNotExist;
use crate::types::report::FormatError;
use crate::types::report::Location;
use crate::types::tree::ContentHash;
use crate::types::tree::WalkError;
use crate::types::tree::hash_walked;
use crate::types::tree::utf8_text;
use crate::types::tree::walk;

/// The manifest every extension directory holds at its root.
const MANIFEST_FILE: &str = "extension.toml";

/// A tool extension directory that passed every check.
///
/// # Examples
///
/// ```
/// use agentstacks::fs::StdFileTree;
/// use agentstacks::types::Extension;
///
/// let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/extensions/valid/minimal");
/// let extension = Extension::load(&StdFileTree::new(dir))
///     .map_err(|refusals| format!("{refusals:?}"))?;
/// assert_eq!(extension.manifest().entry().as_str(), "adapter.lua");
/// assert!(extension.dir().ends_with("valid/minimal"));
/// # Ok::<(), String>(())
/// ```
#[derive(Debug, Clone)]
pub struct Extension {
    dir: PathBuf,
    manifest: ExtensionManifest,
    tree: ContentHash,
    ignored: Vec<PathBuf>,
}

impl Extension {
    /// Loads the extension in the directory behind `tree`.
    ///
    /// # Errors
    ///
    /// Returns every refusal the directory holds. A refused `api` therefore never yields an
    /// [`Extension`].
    ///
    /// # Examples
    ///
    /// ```
    /// use agentstacks::fs::StdFileTree;
    /// use agentstacks::types::Extension;
    ///
    /// # fn main() -> Result<(), String> {
    /// let fixtures = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/extensions");
    ///
    /// let extension = Extension::load(&StdFileTree::new(format!("{fixtures}/valid/minimal")))
    ///     .map_err(|refusals| format!("{refusals:?}"))?;
    /// assert_eq!(extension.manifest().name().as_str(), "claude");
    ///
    /// let refused = Extension::load(&StdFileTree::new(format!("{fixtures}/invalid/api-one")))
    ///     .err()
    ///     .ok_or("api 1 is not supported")?;
    /// let shown: Vec<String> = refused.into_sorted().iter().map(ToString::to_string).collect();
    /// assert!(shown[0].ends_with("extension.api: 1 not supported (this agentstacks speaks 0)"));
    /// # Ok(())
    /// # }
    /// ```
    pub fn load<T: FileTree>(tree: &T) -> Result<Self, Diagnostics> {
        let (walked, walk_refusals) = walk(tree);
        let mut diagnostics = Diagnostics::new();
        diagnostics.extend(walk_refusals.into_iter().map(reword_walk_diagnostic));
        let mut manifest_bytes = None;
        let hash = hash_walked(tree, &walked, &mut diagnostics, |path, bytes| {
            if path.as_str() == MANIFEST_FILE {
                manifest_bytes = Some(bytes);
            }
        });
        let manifest_path = tree.root().join(MANIFEST_FILE);
        let listed = walked
            .files()
            .iter()
            .any(|path| path.as_str() == MANIFEST_FILE);
        if !listed {
            if !walked.covers(Path::new(MANIFEST_FILE)) {
                diagnostics.push(Diagnostic::new(
                    Location::file(tree.root()),
                    ExtensionError::NotAnExtension,
                ));
            }
            return Err(diagnostics);
        }
        // A listed manifest without bytes failed to read, and that refusal is already reported.
        let Some(bytes) = manifest_bytes else {
            return Err(diagnostics);
        };
        let text = match utf8_text(bytes) {
            Ok(text) => text,
            Err(error) => {
                diagnostics.push(Diagnostic::new(Location::file(&manifest_path), error));
                return Err(diagnostics);
            }
        };
        let (parsed, entry) = ExtensionManifest::parse_with_entry(&manifest_path, &text);
        let manifest = match parsed {
            Ok(manifest) => Some(manifest),
            Err(refusals) => {
                diagnostics.extend(refusals);
                None
            }
        };
        if let Some(entry) = entry {
            let wanted = entry.normalized();
            let walked_file = walked.files().iter().any(|path| path.as_str() == wanted);
            if !walked_file && !walked.covers(Path::new(wanted)) {
                diagnostics.push(Diagnostic::new(
                    Location::at_key(&manifest_path, "extension.entry"),
                    ExtensionError::from(DoesNotExist::new(entry.as_str())),
                ));
            }
        }
        let (Some(manifest), Some(hash)) = (manifest.filter(|_| diagnostics.is_empty()), hash)
        else {
            return Err(diagnostics);
        };
        Ok(Self {
            dir: tree.root().to_path_buf(),
            manifest,
            tree: hash,
            ignored: walked.ignored().to_vec(),
        })
    }

    /// The directory the extension was loaded from, as the tree names it in messages.
    #[must_use]
    pub fn dir(&self) -> &Path {
        &self.dir
    }

    /// The extension's checked manifest.
    #[must_use]
    pub const fn manifest(&self) -> &ExtensionManifest {
        &self.manifest
    }

    /// The project files the extension may read, as its manifest lists them.
    #[must_use]
    pub fn reads(&self) -> &[SubPath] {
        self.manifest.reads()
    }

    /// The hash of the whole directory.
    #[must_use]
    pub const fn tree(&self) -> &ContentHash {
        &self.tree
    }

    /// The dot entries the walk ignored, relative to the directory and sorted by path bytes.
    #[must_use]
    pub fn ignored(&self) -> &[PathBuf] {
        &self.ignored
    }
}

/// Words the walker's symlink refusal for an extension and passes every other diagnostic through.
fn reword_walk_diagnostic(diagnostic: Diagnostic) -> Diagnostic {
    if matches!(diagnostic.error(), FormatError::Walk(WalkError::Symlink)) {
        Diagnostic::new(diagnostic.location().clone(), ExtensionError::Symlink)
    } else {
        diagnostic
    }
}

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

    use super::*;
    use crate::port::FakeFileTree;
    use crate::types::tree::tree_hash;

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

    const ROOT: &str = "extensions/claude";
    const MANIFEST: &str = "extensions/claude/extension.toml";

    const EXAMPLE: &str = "[extension]\n\
        name        = \"claude\"\n\
        version     = \"0.1.0\"\n\
        api         = 0\n\
        description = \"Renders packs into Claude Code project config\"\n\
        entry       = \"adapter.lua\"\n";

    fn minimal() -> FakeFileTree {
        FakeFileTree::new(ROOT)
            .file("extension.toml", EXAMPLE.as_bytes())
            .file("adapter.lua", b"return {}\n")
    }

    fn manifest_with(old: &str, new: &str) -> String {
        EXAMPLE.replace(old, new)
    }

    fn refusals(result: Result<Extension, Diagnostics>) -> Vec<String> {
        result
            .err()
            .unwrap_or_default()
            .into_iter()
            .map(|d| d.to_string())
            .collect()
    }

    fn sorted(mut shown: Vec<String>) -> Vec<String> {
        shown.sort();
        shown
    }

    #[test]
    fn extension_load_accepts_a_minimal_extension() -> TestResult {
        let extension = Extension::load(&minimal()).map_err(|d| format!("{d:?}"))?;
        assert_eq!(extension.manifest().name().as_str(), "claude");
        assert_eq!(extension.manifest().entry().as_str(), "adapter.lua");
        assert_eq!(extension.dir(), Path::new(ROOT));
        assert!(extension.ignored().is_empty());
        Ok(())
    }

    #[test]
    fn extension_reads_are_the_manifests() -> TestResult {
        let manifest = manifest_with(
            "entry       = \"adapter.lua\"\n",
            "entry       = \"adapter.lua\"\nreads = [\".claude/settings.json\"]\n",
        );
        let tree = minimal().file("extension.toml", manifest.as_bytes());
        let extension = Extension::load(&tree).map_err(|d| format!("{d:?}"))?;
        assert_eq!(extension.reads(), extension.manifest().reads());
        assert_eq!(extension.reads().len(), 1);
        assert_eq!(extension.reads()[0].as_str(), ".claude/settings.json");
        Ok(())
    }

    #[test]
    fn extension_load_refuses_a_directory_without_a_manifest() {
        let tree = FakeFileTree::new(ROOT).file("adapter.lua", b"");
        assert_eq!(
            refusals(Extension::load(&tree)),
            ["extensions/claude: not an extension (no extension.toml)"]
        );
    }

    #[test]
    fn extension_load_refuses_a_missing_entry() {
        let tree = FakeFileTree::new(ROOT).file("extension.toml", EXAMPLE.as_bytes());
        assert_eq!(
            refusals(Extension::load(&tree)),
            [format!(
                "{MANIFEST}: extension.entry: \"adapter.lua\" does not exist"
            )]
        );
    }

    #[test]
    fn extension_load_does_not_find_an_entry_inside_a_dot_directory() {
        let manifest = manifest_with("adapter.lua", ".hidden/adapter.lua");
        let tree = FakeFileTree::new(ROOT)
            .file("extension.toml", manifest.as_bytes())
            .file(".hidden/adapter.lua", b"");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [format!(
                "{MANIFEST}: extension.entry: \".hidden/adapter.lua\" does not exist"
            )]
        );
    }

    #[test]
    fn extension_load_does_not_take_a_directory_for_an_entry() {
        let manifest = manifest_with("adapter.lua", "lib.lua");
        let tree = FakeFileTree::new(ROOT)
            .file("extension.toml", manifest.as_bytes())
            .file("lib.lua/inner.txt", b"");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [format!(
                "{MANIFEST}: extension.entry: \"lib.lua\" does not exist"
            )]
        );
    }

    #[test]
    fn extension_load_passes_walk_refusals_through() {
        assert_eq!(
            refusals(Extension::load(&minimal().symlink("link"))),
            ["extensions/claude/link: symlinks are not allowed in an extension"]
        );
        assert_eq!(
            refusals(Extension::load(&minimal().special("sock"))),
            ["extensions/claude/sock: only regular files and directories are allowed"]
        );
    }

    #[test]
    fn extension_load_does_not_call_a_manifest_the_walk_refused_absent() {
        let tree = FakeFileTree::new(ROOT)
            .symlink("extension.toml")
            .file("adapter.lua", b"");
        assert_eq!(
            refusals(Extension::load(&tree)),
            ["extensions/claude/extension.toml: symlinks are not allowed in an extension"]
        );
    }

    #[test]
    fn extension_load_reports_a_walk_refusal_with_the_manifest_refusals() {
        let manifest = manifest_with("\"claude\"", "\"Claude\"");
        let tree = FakeFileTree::new(ROOT)
            .file("extension.toml", manifest.as_bytes())
            .file("adapter.lua", b"")
            .symlink("link")
            .special("sock");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [
                format!("{MANIFEST}: extension.name: \"Claude\" is not a valid name"),
                "extensions/claude/link: symlinks are not allowed in an extension".to_owned(),
                "extensions/claude/sock: only regular files and directories are allowed".to_owned(),
            ]
        );
    }

    #[test]
    fn extension_load_reports_a_walk_refusal_with_a_missing_entry() {
        let tree = FakeFileTree::new(ROOT)
            .file("extension.toml", EXAMPLE.as_bytes())
            .symlink("link");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [
                format!("{MANIFEST}: extension.entry: \"adapter.lua\" does not exist"),
                "extensions/claude/link: symlinks are not allowed in an extension".to_owned(),
            ]
        );
    }

    #[test]
    fn extension_load_does_not_call_an_entry_the_walk_refused_missing() {
        let tree = FakeFileTree::new(ROOT)
            .file("extension.toml", EXAMPLE.as_bytes())
            .symlink("adapter.lua");
        assert_eq!(
            refusals(Extension::load(&tree)),
            ["extensions/claude/adapter.lua: symlinks are not allowed in an extension"]
        );
    }

    #[test]
    fn extension_load_finds_an_entry_written_with_a_leading_current_directory() -> TestResult {
        for written in ["./adapter.lua", "././adapter.lua", ".//adapter.lua"] {
            let manifest = manifest_with("adapter.lua", written);
            let tree = minimal().file("extension.toml", manifest.as_bytes());
            let extension = Extension::load(&tree).map_err(|d| format!("{d:?}"))?;
            assert_eq!(extension.manifest().entry().as_str(), written);
            assert_eq!(extension.manifest().entry().normalized(), "adapter.lua");
        }
        Ok(())
    }

    #[test]
    fn extension_load_names_a_missing_entry_as_written() {
        let manifest = manifest_with("adapter.lua", "./lib/adapter.lua");
        let tree = FakeFileTree::new(ROOT)
            .file("extension.toml", manifest.as_bytes())
            .file("adapter.lua", b"");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [format!(
                "{MANIFEST}: extension.entry: \"./lib/adapter.lua\" does not exist"
            )]
        );
    }

    #[cfg(unix)]
    #[test]
    fn extension_load_passes_a_non_utf8_path_refusal_through() -> TestResult {
        use std::ffi::OsStr;
        use std::os::unix::ffi::OsStrExt;

        let name = OsStr::from_bytes(&[0x66, 0xFF]);
        let tree = minimal().file(name, b"");
        let shown = refusals(Extension::load(&tree));
        assert_eq!(shown.len(), 1);
        let diagnostic = shown.first().ok_or("one refusal")?;
        assert!(
            diagnostic.starts_with("extensions/claude/f"),
            "{diagnostic}"
        );
        assert!(
            diagnostic.ends_with(": path is not valid UTF-8"),
            "{diagnostic}"
        );
        Ok(())
    }

    #[test]
    fn extension_load_ignores_dot_entries_and_lists_them() -> TestResult {
        let plain = Extension::load(&minimal()).map_err(|d| format!("{d:?}"))?;
        let tree = minimal().file(".DS_Store", b"x").file(".git/config", b"y");
        let extension = Extension::load(&tree).map_err(|d| format!("{d:?}"))?;
        assert_eq!(
            extension.ignored(),
            [PathBuf::from(".DS_Store"), PathBuf::from(".git")]
        );
        assert_eq!(extension.tree(), plain.tree());
        Ok(())
    }

    #[test]
    fn extension_load_ignores_empty_directories() -> TestResult {
        let plain = Extension::load(&minimal()).map_err(|d| format!("{d:?}"))?;
        let extension = Extension::load(&minimal().dir("lib")).map_err(|d| format!("{d:?}"))?;
        assert_eq!(extension.tree(), plain.tree());
        Ok(())
    }

    #[test]
    fn extension_load_records_the_tree_hash() -> TestResult {
        let tree = minimal();
        let extension = Extension::load(&tree).map_err(|d| format!("{d:?}"))?;
        let expected = tree_hash(&tree).map_err(|d| format!("{d:?}"))?;
        assert_eq!(extension.tree(), &expected);

        let changed = minimal().file("adapter.lua", b"return {} \n");
        let other = Extension::load(&changed).map_err(|d| format!("{d:?}"))?;
        assert_ne!(other.tree(), extension.tree());
        Ok(())
    }

    #[test]
    fn extension_load_refuses_api_one_without_returning_an_extension() {
        let tree = FakeFileTree::new(ROOT)
            .file(
                "extension.toml",
                manifest_with("api         = 0", "api         = 1").as_bytes(),
            )
            .file("adapter.lua", b"");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [format!(
                "{MANIFEST}: extension.api: 1 not supported (this agentstacks speaks 0)"
            )]
        );
    }

    #[test]
    fn extension_load_reports_an_unreadable_manifest_as_io() -> TestResult {
        let unreadable = minimal().unreadable("extension.toml");
        let not_utf8 = FakeFileTree::new(ROOT)
            .file("extension.toml", &[0xFF, 0xFE])
            .file("adapter.lua", b"");
        for (tree, message) in [
            (unreadable, FakeFileTree::denied().to_string()),
            (not_utf8, "not valid UTF-8".to_owned()),
        ] {
            let diagnostics = Extension::load(&tree)
                .err()
                .ok_or("expected a refusal")?
                .into_sorted();
            assert_eq!(diagnostics.len(), 1);
            let diagnostic = diagnostics.first().ok_or("one refusal")?;
            assert_eq!(diagnostic.to_string(), format!("{MANIFEST}: {message}"));
            assert!(matches!(diagnostic.error(), FormatError::Io(_)));
        }
        Ok(())
    }

    #[test]
    fn extension_load_reports_an_unreadable_file_while_hashing() {
        let tree = minimal().unreadable("adapter.lua");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [format!("{ROOT}/adapter.lua: {}", FakeFileTree::denied())]
        );
    }

    #[test]
    fn extension_load_reports_an_unreadable_file_with_the_manifest_refusals() {
        let manifest = manifest_with("\"claude\"", "\"Claude\"");
        let tree = FakeFileTree::new(ROOT)
            .file("extension.toml", manifest.as_bytes())
            .unreadable("adapter.lua");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [
                format!("{ROOT}/adapter.lua: {}", FakeFileTree::denied()),
                format!("{MANIFEST}: extension.name: \"Claude\" is not a valid name"),
            ]
        );
    }

    #[test]
    fn extension_load_reports_an_unreadable_file_without_a_manifest() {
        let tree = FakeFileTree::new(ROOT).unreadable("adapter.lua");
        assert_eq!(
            refusals(Extension::load(&tree)),
            [
                format!("{ROOT}: not an extension (no extension.toml)"),
                format!("{ROOT}/adapter.lua: {}", FakeFileTree::denied()),
            ]
        );
    }

    #[test]
    fn extension_load_reports_every_refusal_together() {
        let manifest = manifest_with("\"claude\"", "\"Claude\"") + "foo = 1\n";
        let tree = FakeFileTree::new(ROOT).file("extension.toml", manifest.as_bytes());
        assert_eq!(
            sorted(refusals(Extension::load(&tree))),
            sorted(vec![
                format!("{MANIFEST}: extension.name: \"Claude\" is not a valid name"),
                format!("{MANIFEST}: extension.foo: unknown key"),
                format!("{MANIFEST}: extension.entry: \"adapter.lua\" does not exist"),
            ])
        );
    }

    #[test]
    fn reword_walk_diagnostic_rewords_only_the_symlink_refusal() {
        let at = || Location::file("extensions/claude/x");
        let reworded = reword_walk_diagnostic(Diagnostic::new(at(), WalkError::Symlink));
        assert_eq!(
            reworded.to_string(),
            "extensions/claude/x: symlinks are not allowed in an extension"
        );
        for error in [WalkError::SpecialFile, WalkError::NotUtf8] {
            let kept = reword_walk_diagnostic(Diagnostic::new(at(), error));
            assert_eq!(kept.to_string(), format!("extensions/claude/x: {error}"));
        }
        let io = io::Error::new(io::ErrorKind::PermissionDenied, "denied");
        let kept = reword_walk_diagnostic(Diagnostic::new(at(), io));
        assert_eq!(kept.to_string(), "extensions/claude/x: denied");
    }
}