yapi-core 0.4.2

Coding agent core: sessions, compaction, settings, resources, built-in tools, system prompt, packages, extension runner and MCP. An internal library of yapi, with no stable API.
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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
//! What yapi loads from chest manifests: the installed extensions, with the
//! grants, event policy and module root each runs with, and the placed
//! skills.

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

use serde::Deserialize as _;
use yapi_types::rpc::SourceInfo;
use yapi_types::settings::{DefaultProjectTrust, FilteredPackage};

use super::fetch::placed_name;
use super::lock::fresh;
use super::manifest::{Entry, MANIFEST_ENV, Manifest, Section, from_env};
use super::{Mode, Project};
use crate::extensions::{EventPolicy, Grants};
use crate::packages::npm::{Npm, escaped};
use crate::packages::resolve::{
    PackageInput, ProjectOnly, ResolvedPaths, ResourceType, package_resources,
};
use crate::settings::{Scope, SettingsManager};
use crate::tools::path::home_dir;
use crate::trust::{TrustStore, resolve_trusted};

/// The project manifest of `project_root`: the file [`MANIFEST_ENV`] names
/// relative to it, else `chest.toml`. `None` when the variable names a file
/// outside the project, as chest refuses it too. Its lock is the file beside
/// it named with `.lock`.
pub fn project_manifest(project_root: &Path) -> Option<PathBuf> {
    let name = from_env().ok()?;
    Some(project_root.join(name.as_deref().unwrap_or("chest.toml")))
}

/// How messages name the chest entry that gives an extension file the
/// resolver reports with `source` and `scope`: `extensions.<name> in
/// <manifest>`, the manifest named as chest's own messages name it. `None`
/// for a file chest doesn't give.
pub fn entry_label(source: &str, scope: &str, project_root: &Path) -> Option<String> {
    let name = source.strip_prefix("chest:")?;
    let manifest = match scope {
        "user" => "~/.chest/chest.toml".to_owned(),
        _ => {
            let manifest = project_manifest(project_root)?;
            manifest.file_name()?.to_string_lossy().into_owned()
        }
    };
    Some(format!("extensions.{name} in {manifest}"))
}

/// Whether `manifest` installs anything, beyond describing its folder as a
/// package with `[package]`.
fn consumes(manifest: &Manifest) -> bool {
    [Section::Skills, Section::Agents, Section::Extensions]
        .into_iter()
        .any(|section| !manifest.section(section).is_empty())
        || !manifest.patch.is_empty()
}

/// Whether the project manifest of `project_root` installs anything, so it
/// needs the project's trust as other project files that run code do. One
/// with only `[package]`, which describes the project as a package, does not.
/// A manifest that does not parse needs trust too.
pub fn needs_trust(project_root: &Path) -> bool {
    let Some(Ok(text)) = project_manifest(project_root).map(std::fs::read_to_string) else {
        return false;
    };
    Manifest::parse(&text, Scope::Project).map_or(true, |manifest| consumes(&manifest))
}

/// What an extension runs with: its grants, its `pi.events` policy, and the
/// folder it loads modules from without file access. It has no default, so
/// every file says which one it gets.
#[derive(Clone, Debug, PartialEq)]
pub struct Policy {
    /// What it may reach.
    pub grants: Grants,
    /// The channels it may send to, and hear from, extensions whose grants
    /// differ.
    pub events: EventPolicy,
    /// The folder that holds its package and the package's own
    /// dependencies.
    pub root: Option<PathBuf>,
}

impl Policy {
    /// What a package from `yapi install` runs with, as in pi: every grant
    /// and the shared event policy.
    pub fn unrestricted() -> Policy {
        Policy {
            grants: Grants::default(),
            events: EventPolicy::shared(),
            root: None,
        }
    }

    /// What a file chest cannot vouch for runs with: no grant, the
    /// isolated event policy, and modules only from `root`, a folder or
    /// the file itself.
    pub fn closed(root: &Path) -> Policy {
        Policy {
            grants: Grants::none(),
            events: EventPolicy::isolated(),
            root: Some(root.to_path_buf()),
        }
    }

    /// What both `self` and `other` allow: the grants and channels of both,
    /// and the narrower of two nested module roots, or none.
    fn intersection(&self, other: &Policy) -> Policy {
        let canonical = |root: &PathBuf| root.canonicalize().unwrap_or_else(|_| root.clone());
        let roots = (
            self.root.as_ref().map(canonical),
            other.root.as_ref().map(canonical),
        );
        let root = match roots {
            (Some(left), Some(right)) if left.starts_with(&right) => Some(left),
            (Some(left), Some(right)) if right.starts_with(&left) => Some(right),
            _ => None,
        };
        Policy {
            grants: self.grants.intersection(other.grants),
            events: self.events.intersection(&other.events),
            root,
        }
    }
}

/// The policies of chest's packages, by the scope and source the resolver
/// gives their files, such as `("project", "chest:todo")`.
/// [`installed_inputs`] builds the packages and their policies in one place,
/// so they always match.
pub type Policies = HashMap<(String, String), Policy>;

/// The policies of extension files, so a policy applies whichever source of
/// the same file loads it.
#[derive(Clone, Debug, Default)]
pub struct FilePolicies {
    /// The policy of each file of chest's packages, by canonical path: what
    /// every entry that gives it allows.
    pub files: HashMap<PathBuf, Policy>,
    /// The canonical folders and files of entries chest left out, whose
    /// files run with no grant, whatever loads them.
    pub closed: Vec<PathBuf>,
}

impl FilePolicies {
    /// What extension file `file`, a canonical path, runs with: no grant
    /// and no module but itself in an entry chest left out, else its
    /// entries' policy, and for a file chest does not know, every grant, as
    /// packages from `yapi install` keep pi's grants.
    pub fn of(&self, file: &Path) -> Policy {
        // Not the entry's folder as the root, which may hold far more.
        if self.closes(file) {
            return Policy::closed(file);
        }
        self.files
            .get(file)
            .cloned()
            .unwrap_or_else(Policy::unrestricted)
    }

    /// Whether chest decides what `file` runs with.
    pub fn manages(&self, file: &Path) -> bool {
        self.files.contains_key(file) || self.closes(file)
    }

    /// Whether `file` belongs to an entry chest left out.
    pub fn closes(&self, file: &Path) -> bool {
        self.closed.iter().any(|root| file.starts_with(root))
    }

    /// The warning for extension file `file`, a canonical path, that
    /// `source` names by `path` though chest decides what it runs with, as
    /// the path and message to show, or `None` when chest does not. Both are
    /// escaped, since a cloned project's file names can hold anything.
    pub fn also_loaded(&self, file: &Path, path: &str, source: &str) -> Option<(String, String)> {
        if !self.manages(file) {
            return None;
        }
        let message = match self.closes(file) {
            true => format!(
                "{path} is also loaded from {source}, but its chest entry does not load, so it runs with no capabilities"
            ),
            false => format!("{path} is also loaded from {source}; chest's grants apply"),
        };
        Some((escaped(path), escaped(&message)))
    }
}

/// The policy of each extension file the resolver gave a `chest:` source in
/// `resolved`, loaded or shadowed, from its package's entry in
/// `installed`, with the entries `installed` left out. A file that several
/// entries give gets what all of them allow, and when that is less than one
/// of them allows, adds a line to `problems` that names the entries, as
/// found for the project in `project_root`. So does a file that an entry
/// left out holds too, which runs with no grant. A chest file whose package
/// has no entry runs with no grant and the isolated event policy, and adds
/// a line to `problems` under its source.
pub fn file_policies(
    resolved: &ResolvedPaths,
    installed: &Installed,
    project_root: &Path,
    problems: &mut Vec<(String, String)>,
) -> FilePolicies {
    let files = resolved.of(ResourceType::Extensions).iter();
    let files = files.map(|file| &file.info).chain(&resolved.shadowed);
    let mut found = FilePolicies {
        files: HashMap::new(),
        closed: installed
            .closed
            .iter()
            .map(|closed| closed.root.clone())
            .collect(),
    };
    // Each file as its first source names it, with the entries that give
    // it, and whether one of them allows fewer grants, or fewer channels,
    // than another.
    let mut givers: Vec<(PathBuf, &SourceInfo, Vec<String>, [bool; 2])> = Vec::new();
    for info in files.filter(|info| info.source.starts_with("chest:")) {
        let key = (info.scope.clone(), info.source.clone());
        let file = Path::new(&info.path);
        let file = file.canonicalize().unwrap_or_else(|_| file.into());
        let policy = installed.policies.get(&key).cloned().unwrap_or_else(|| {
            let message = format!(
                "{}: no chest entry grants this file, so it runs with no capabilities",
                info.path
            );
            problems.push((escaped(&info.source), escaped(&message)));
            Policy::closed(&file)
        });
        let label = entry_label(&info.source, &info.scope, project_root);
        let label = label.unwrap_or_else(|| info.source.clone());
        let policy = match found.files.get(&file) {
            Some(other) => {
                if let Some((.., labels, [grants, events])) =
                    givers.iter_mut().find(|giver| giver.0 == file)
                {
                    if !labels.contains(&label) {
                        labels.push(label);
                    }
                    *grants |= other.grants != policy.grants;
                    *events |= other.events != policy.events;
                }
                other.intersection(&policy)
            }
            None => {
                givers.push((file.clone(), info, vec![label], [false; 2]));
                policy
            }
        };
        found.files.insert(file, policy);
    }
    // What a file that `count` entries give, with `policy`, gets less of
    // than one of them allows, by whether that is grants and event channels.
    let only = |narrowed: [bool; 2], policy: &Policy, count: usize| {
        let all = if count == 2 { "both" } else { "all" };
        match narrowed {
            [false, false] => None,
            [true, false] => Some(format!("grants {all} give: {}", policy.grants)),
            [false, true] => Some(format!("event channels {all} give")),
            [true, true] => Some(format!(
                "event channels and the grants {all} give: {}",
                policy.grants
            )),
        }
    };
    for (file, info, labels, narrowed) in givers {
        let (path, source) = (&info.path, &info.source);
        let Some(policy) = found.files.get(&file) else {
            continue;
        };
        let closer = installed
            .closed
            .iter()
            .find(|closed| file.starts_with(&closed.root));
        if let Some(Closed {
            entry: closer,
            remove,
            install,
            ..
        }) = closer
        {
            // Once installed, the entry left out gives the file too.
            let fix = match install {
                Some((command, once)) => {
                    let [grants, events] = narrowed;
                    let narrowed = [
                        grants || policy.grants != once.grants,
                        events || policy.events != once.events,
                    ];
                    let after = policy.intersection(once);
                    let gives = only(narrowed, &after, labels.len() + 1);
                    let gives = gives.map(|gives| format!(", which gives it only the {gives}"));
                    format!(
                        "run `{command}`{}, or `{remove}`",
                        gives.unwrap_or_default()
                    )
                }
                None => format!("run `{remove}`"),
            };
            let message = format!(
                "{path} is in {}, but {closer} holds it too and does not load, so it runs with no capabilities: {fix}",
                labels.join(" and ")
            );
            problems.push((escaped(source), escaped(&message)));
            continue;
        }
        let Some(narrowed) = only(narrowed, policy, labels.len()) else {
            continue;
        };
        let message = format!(
            "{path} is in {}, so it gets only the {narrowed}",
            labels.join(" and ")
        );
        problems.push((escaped(source), escaped(&message)));
    }
    found
}

/// What the installed entries of chest manifests give yapi.
#[derive(Clone, Debug, Default)]
pub struct Installed {
    /// Extension packages, each as source `chest:<name>`.
    pub packages: Vec<PackageInput>,
    /// The canonical copies of placed skills, each a skill folder, with the
    /// source and scope of its entry.
    pub skills: Vec<PackageInput>,
    /// The packages' policies.
    pub policies: Policies,
    /// The folders and files of extension entries left out because their
    /// lock is stale or missing, and the extension files of those whose
    /// folder holds too much.
    pub closed: Vec<Closed>,
    /// The [`package_identity`] of each loaded npm or git extension entry,
    /// such as `npm:@acme/todo`, with the entry's name, its manifest's path,
    /// and whether that is the user's global manifest.
    pub identities: Vec<(String, String, String, bool)>,
}

/// A canonical folder or file of an extension entry chest leaves out,
/// which runs with no grant if something else loads it.
#[derive(Clone, Debug)]
pub struct Closed {
    /// The folder or file.
    pub root: PathBuf,
    /// Its entry, as `extensions.<name> in <manifest>`.
    pub entry: String,
    /// The command that removes the entry.
    pub remove: String,
    /// For an entry that `chest install` would load, that command and the
    /// policy the entry would then give.
    pub install: Option<(String, Policy)>,
}

impl Installed {
    fn extend(&mut self, other: Installed) {
        self.packages.extend(other.packages);
        self.skills.extend(other.skills);
        self.policies.extend(other.policies);
        self.closed.extend(other.closed);
        self.identities.extend(other.identities);
    }
}

/// Whether a run reads the user's global manifest: always, except in a
/// project-only run whose `projectOnly` setting in `settings` can keep none
/// of its entries, so nothing global is put back for nothing.
pub fn reads_global(settings: &SettingsManager) -> bool {
    !crate::config::project_only()
        || crate::packages::resolve::ProjectOnly::from_settings(settings.document(Scope::Global))
            .needs_chest()
}

/// One install of an npm or git package, as [`LeftOut`] compares them.
#[derive(Clone, Debug, PartialEq)]
enum Install {
    /// A chest extension entry: its name, its manifest's path, and whether
    /// that is the user's global manifest.
    Chest {
        name: String,
        path: String,
        global: bool,
    },
    /// A `packages` entry of a scope's settings, by its source.
    Settings { scope: Scope, source: String },
}

/// The copies a run leaves out of each npm or git package that several
/// sources install, so it loads once, from the first of: the project's
/// manifest, the project's settings `packages`, the global manifest and the
/// global settings `packages`. Of two entries of one manifest, the first
/// loads. pi's own rules decide between two settings copies.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct LeftOut(Vec<(Install, Install)>);

impl LeftOut {
    /// The copies left out of `installed` and the packages of `settings`,
    /// each with the copy that loads instead. With `keep`, a project-only
    /// run's keep list, a global copy that only a kind list keeps comes
    /// after the copies the run keeps whole, and one it keeps no file of
    /// doesn't count. Global paths are relative to `agent_dir`.
    pub fn new(
        installed: &Installed,
        settings: &SettingsManager,
        keep: Option<&ProjectOnly>,
        agent_dir: &Path,
    ) -> LeftOut {
        let rank = |source: &str, global: bool| match keep {
            Some(keep) if global && !keep.keeps_package(source, agent_dir) => {
                keep.needs_package(source, agent_dir).then_some(1)
            }
            _ => Some(0),
        };
        let chest = |global: bool| {
            let identities = installed.identities.iter();
            identities
                .filter(move |(_, _, _, other)| *other == global)
                .filter_map(move |(id, name, path, global)| {
                    let rank = rank(&format!("chest:{name}"), *global)?;
                    let (name, path, global) = (name.clone(), path.clone(), *global);
                    Some((rank, id.clone(), Install::Chest { name, path, global }))
                })
        };
        let from_settings = |scope: Scope| {
            let packages = crate::packages::settings_packages(settings, scope);
            let sources = packages
                .iter()
                .filter_map(crate::packages::entry_source)
                .filter_map(|source| {
                    let rank = rank(source, scope == Scope::Global)?;
                    let id = crate::packages::source::parse(source).identity(Path::new(""));
                    let source = source.to_owned();
                    Some((rank, id, Install::Settings { scope, source }))
                });
            sources.collect::<Vec<_>>()
        };
        let mut copies: Vec<(u8, String, Install)> = chest(false)
            .chain(from_settings(Scope::Project))
            .chain(chest(true))
            .chain(from_settings(Scope::Global))
            .collect();
        // Two settings copies are pi's to decide.
        let pair = |a: &Install, b: &Install| {
            !matches!((a, b), (Install::Settings { .. }, Install::Settings { .. }))
        };
        if let Some(keep) = keep {
            // A global copy's files are read only when it collides.
            let home = home_dir();
            let input = |copy: &Install| match copy {
                Install::Chest { name, .. } => {
                    let source = format!("chest:{name}");
                    let mut inputs = installed.packages.iter();
                    let found =
                        inputs.find(|input| input.scope == "user" && input.source == source);
                    found.cloned()
                }
                Install::Settings { source, .. } => {
                    let global = crate::packages::settings_packages(settings, Scope::Global);
                    let inputs = crate::packages::resolve::package_inputs(
                        &[],
                        &global,
                        |_| agent_dir.to_path_buf(),
                        |source, _| crate::packages::install_location(source, agent_dir),
                    );
                    inputs.into_iter().find(|input| input.source == *source)
                }
            };
            let counts: Vec<bool> = copies
                .iter()
                .enumerate()
                .map(|(index, (rank, id, copy))| {
                    let mut others = copies.iter().enumerate();
                    *rank == 0
                        || !others.any(|(other_index, (_, other, install))| {
                            other_index != index && same_package(id, other) && pair(copy, install)
                        })
                        || input(copy)
                            .is_some_and(|input| keep.keeps_part(&input, agent_dir, &home))
                })
                .collect();
            let copies_counts = copies.into_iter().zip(counts);
            copies = copies_counts
                .filter_map(|(copy, counts)| counts.then_some(copy))
                .collect();
        }
        // Stable, so each rank keeps the order above.
        copies.sort_by_key(|(rank, ..)| *rank);
        let mut left_out = Vec::new();
        // Only copies that load count, as a whole package overlaps two of
        // its folders that don't overlap each other.
        let mut loaded: Vec<(&str, &Install)> = Vec::new();
        for (_, id, copy) in &copies {
            let kept = loaded.iter().find(|(other, _)| same_package(other, id));
            match kept {
                Some((_, kept)) if pair(kept, copy) => {
                    left_out.push((copy.clone(), (*kept).clone()));
                }
                _ => loaded.push((id, copy)),
            }
        }
        LeftOut(left_out)
    }

    /// Whether chest's package `input` is left out.
    pub fn chest(&self, input: &PackageInput) -> bool {
        self.0.iter().any(|(copy, _)| match copy {
            Install::Chest { name, global, .. } => {
                input.source == format!("chest:{name}") && (input.scope == "user") == *global
            }
            Install::Settings { .. } => false,
        })
    }

    /// Whether the `packages` entry of `scope`'s settings with `source` is
    /// left out.
    pub fn settings(&self, scope: Scope, source: &str) -> bool {
        let copy = Install::Settings {
            scope,
            source: source.to_owned(),
        };
        self.0.iter().any(|(left_out, _)| *left_out == copy)
    }

    /// A warning for each copy left out, by the path of the chest manifest
    /// involved, that says which copy loads and how to remove the other.
    /// With `approve`, the command for a project's settings trusts the
    /// project for that command.
    pub fn problems(&self, approve: bool) -> Vec<(String, String)> {
        let scope = |global: bool| {
            if global {
                Scope::Global
            } else {
                Scope::Project
            }
        };
        let home = home_dir();
        let home = home.canonicalize().unwrap_or(home);
        let place = |copy: &Install| match copy {
            Install::Chest { name, path, global } => {
                let label = super::manifest_label(Path::new(path), scope(*global), &home);
                format!("extensions.{name} in {label}")
            }
            Install::Settings { scope, source } => match scope {
                Scope::Project => format!("{source} in the project's settings"),
                Scope::Global => format!("{source} in the global settings"),
            },
        };
        let local = match approve {
            true => "yapi remove -l --approve",
            false => "yapi remove -l",
        };
        let problems = self.0.iter().map(|(copy, kept)| {
            let remove = match copy {
                Install::Chest { name, path, global } => {
                    let command = super::command(Path::new(path), scope(*global));
                    format!("{command} remove {name}")
                }
                Install::Settings { scope, source } => match scope {
                    Scope::Project => format!("{local} {source}"),
                    Scope::Global => format!("yapi remove {source}"),
                },
            };
            let message = format!(
                "{} is left out, because {} installs the same package: remove it with `{remove}`",
                place(copy),
                place(kept),
            );
            // Two settings copies are pi's to decide, so one is chest's.
            let path = match (copy, kept) {
                (Install::Chest { path, .. }, _) | (_, Install::Chest { path, .. }) => path.clone(),
                _ => String::new(),
            };
            (escaped(&path), escaped(&message))
        });
        problems.collect()
    }
}

/// The warnings, by manifest path, for each copy of a package that a run
/// leaves out because another source installs it too, as [`LeftOut`]
/// decides. Their `yapi remove -l` commands take `--approve` unless the
/// project of `settings` is trusted without it, by the decisions in
/// `agent_dir` or the global `defaultProjectTrust`.
pub fn duplicates(
    installed: &Installed,
    settings: &SettingsManager,
    keep: Option<&ProjectOnly>,
    agent_dir: &Path,
) -> Vec<(String, String)> {
    // As `yapi remove -l` decides it, without this run's `--approve`.
    let default = settings
        .document(Scope::Global)
        .get("defaultProjectTrust")
        .and_then(|value| DefaultProjectTrust::deserialize(value).ok());
    let store = TrustStore::new(agent_dir);
    let trusted = resolve_trusted(settings.project_dir(), &store, None, default);
    LeftOut::new(installed, settings, keep, agent_dir).problems(!trusted.unwrap_or(false))
}

/// What chest gives one run of yapi, with the problems to show by manifest
/// path: warnings and notices of the startup sync, entries left out, and
/// manifests that fail to load.
#[derive(Clone, Debug, Default)]
pub struct Loaded {
    /// The installed entries.
    pub installed: Installed,
    /// Each problem's manifest path and message.
    pub problems: Vec<(String, String)>,
}

impl Loaded {
    /// Adds problem `message` under manifest `path`, both with their
    /// unprintable characters escaped: they quote files a cloned project
    /// holds, and reach the terminal.
    fn warn(&mut self, (path, message): (String, String)) {
        self.problems.push((escaped(&path), escaped(&message)));
    }
}

/// The package that npm or git extension entry `entry` installs, as
/// [`LeftOut`] compares copies: `npm:<name>` or `git:<host>/<path>`, as
/// settings packages are matched. An entry with `path` takes one folder of
/// it, which `#<path>` names, without `.` or empty segments.
pub fn package_identity(entry: &Entry) -> Option<String> {
    let package = match (&entry.npm, &entry.git) {
        (Some(npm), _) => format!("npm:{npm}"),
        (None, Some(git)) => {
            crate::packages::source::parse(&format!("git:{git}")).identity(Path::new(""))
        }
        (None, None) => return None,
    };
    let path = entry.path.iter().flat_map(|path| path.split('/'));
    let path: Vec<&str> = path
        .filter(|segment| !matches!(*segment, "" | "."))
        .collect();
    Some(match path.is_empty() {
        true => package,
        false => format!("{package}#{}", path.join("/")),
    })
}

/// Whether two [`package_identity`] values, or settings identities, name
/// copies of one package: the same package and `path`, or the whole package
/// and one folder of it. Two `path`s of one package are two packages.
pub fn same_package(a: &str, b: &str) -> bool {
    let folder_of = |whole: &str, folder: &str| {
        folder
            .strip_prefix(whole)
            .is_some_and(|rest| rest.starts_with('#'))
    };
    a == b || folder_of(a, b) || folder_of(b, a)
}

/// The extensions and skills `project` has installed, locked and up to
/// date, or patched, for a run in `project_root`: a stale or unlocked entry
/// gives nothing until `chest install`. Entries left out for a reason the
/// user should see add a line to `problems`.
pub fn installed_inputs(
    project: &Project,
    project_root: &Path,
    problems: &mut Vec<String>,
) -> Installed {
    let scope = match project.scope {
        Scope::Project => "project",
        Scope::Global => "user",
    };
    let mut installed = Installed::default();
    let lock = project.lock.clone().unwrap_or_default();
    let record = project.record();
    // A copy an older lock pinned stays out until it is put back.
    let current = |section: Section, name: &str, entry: &Entry| {
        lock.section(section)
            .iter()
            .find(|locked| locked.name == name)
            .filter(|locked| fresh(entry, locked))
            .filter(|locked| project.present(section, locked))
            .filter(|locked| project.holds_locked(section, entry, locked, &record))
            .cloned()
    };
    for (name, entry) in &project.manifest.extensions {
        let patched = project.manifest.patch.contains_key(name);
        let roots = project.extension_roots(name);
        let label = format!("extensions.{name} in {}", project.label());
        // Locked or not, it never loads. Only the files it would load run
        // closed, as other files there belong to other sources.
        if let Some((root, module_root)) = &roots
            && project.too_wide(module_root, &[project_root])
        {
            problems.push(format!(
                "extensions.{name}: {} holds more than the extension, such as the project, the home or the agent folder, so it does not load",
                module_root.display()
            ));
            let local = crate::packages::source::is_local(&format!("chest:{name}"));
            let files = package_resources(root, None, local).extensions;
            let files = files.iter().map(|file| Path::new(&file.info.path));
            let remove = format!("{} remove {name}", project.command());
            let closed = files.filter_map(|file| file.canonicalize().ok());
            let closed = closed.map(|root| Closed {
                root,
                entry: label.clone(),
                remove: remove.clone(),
                install: None,
            });
            installed.closed.extend(closed);
            continue;
        }
        let policy = |root: PathBuf| Policy {
            grants: project.manifest.grants(entry),
            events: entry.events.clone().unwrap_or_default(),
            root: Some(root),
        };
        if !patched && current(Section::Extensions, name, entry).is_none() {
            // Its files may still load through pi's discovery or settings.
            if let Some(root) = roots.and_then(|(_, root)| root.canonicalize().ok()) {
                let install = Some((project.install_command(), policy(root.clone())));
                installed.closed.push(Closed {
                    root,
                    entry: label,
                    remove: format!("{} remove {name}", project.command()),
                    install,
                });
            }
            continue;
        }
        let Some((root, module_root)) = roots else {
            continue;
        };
        if !root.exists() {
            // A store folder that is missing is the sync's to put back, or
            // to warn of.
            if patched || entry.git.is_none() && entry.npm.is_none() {
                problems.push(format!(
                    "extensions.{name}: {} does not exist",
                    root.display()
                ));
            }
            continue;
        }
        let source = format!("chest:{name}");
        let filter = [&entry.skills, &entry.prompts, &entry.themes]
            .iter()
            .any(|list| list.is_some())
            .then(|| FilteredPackage {
                source: source.clone(),
                autoload: None,
                extensions: None,
                skills: entry.skills.clone(),
                prompts: entry.prompts.clone(),
                themes: entry.themes.clone(),
            });
        let key = (scope.to_owned(), source.clone());
        installed.policies.insert(key, policy(module_root));
        // A patched entry still installs its package, so its patch loads in
        // place of other copies.
        if let Some(identity) = package_identity(entry) {
            let path = project.path.display().to_string();
            let global = project.scope == Scope::Global;
            installed
                .identities
                .push((identity, name.clone(), path, global));
        }
        installed.packages.push(PackageInput {
            source,
            scope: scope.to_owned(),
            root,
            filter,
        });
    }
    // Skills load from their canonical copy, wherever the folders put it.
    let Some(home) = project.homes(Section::Skills).into_iter().next() else {
        return installed;
    };
    for (name, entry) in &project.manifest.skills {
        let patch = project.manifest.patch.get(name);
        let roots: Vec<PathBuf> = match (
            patch.and_then(|patch| patch.path.as_deref()),
            current(Section::Skills, name, entry),
        ) {
            // A patched skill loads where `chest install` placed it, else
            // from its patch. A patch of a whole package, as `chest install`
            // reads it, places each of its skills under its own name.
            (Some(path), _) => {
                let patch = project.resolve(path);
                if !patch.exists() {
                    problems.push(format!("skills.{name}: {} does not exist", patch.display()));
                    continue;
                }
                let package = entry.path.is_none() && (entry.git.is_some() || entry.npm.is_some());
                let items = if package {
                    match super::fetch::items(&patch, Section::Skills, None, name) {
                        Ok((items, _)) => items,
                        Err(err) => {
                            problems.push(format!("skills.{name}: {err}"));
                            continue;
                        }
                    }
                } else {
                    vec![(placed_name(Section::Skills, name), patch)]
                };
                items
                    .into_iter()
                    .map(|(item, from)| {
                        let placed = home.join(item);
                        if placed.exists() { placed } else { from }
                    })
                    .collect()
            }
            (None, Some(locked)) => project
                .locked_items(Section::Skills, &locked)
                .into_iter()
                .map(|(item, _)| home.join(item))
                .collect(),
            (None, None) => continue,
        };
        for root in roots {
            installed.skills.push(PackageInput {
                source: format!("chest:{name}"),
                scope: scope.to_owned(),
                root,
                filter: None,
            });
        }
    }
    installed
}

/// The manifests yapi reads for `project_root` that install anything: the
/// project's, only when `trusted`, then the user's global one, only when
/// `global`, or why each cannot be read.
fn manifests(
    project_root: &Path,
    trusted: bool,
    global: bool,
) -> Vec<Result<Project, (String, String)>> {
    let mut found = Vec::new();
    if trusted && let Err(problem) = from_env() {
        let problem = format!("{problem}, so no project manifest loads");
        found.push(Err((MANIFEST_ENV.to_owned(), problem)));
    }
    let project = trusted.then(|| project_manifest(project_root)).flatten();
    let project = project.map(|path| (path, Scope::Project));
    let global = global.then(|| (home_dir().join(".chest/chest.toml"), Scope::Global));
    for (path, scope) in [project, global].into_iter().flatten() {
        if !path.exists() {
            continue;
        }
        match Project::open(&path, scope) {
            Ok(project) if consumes(&project.manifest) => found.push(Ok(project)),
            Ok(_) => {}
            Err(err) => found.push(Err((path.display().to_string(), err.to_string()))),
        }
    }
    found
}

/// Adds the inputs of `project` for a run in `project_root` to `loaded`,
/// with its problems under its path.
fn add(loaded: &mut Loaded, project: &Project, project_root: &Path) {
    let mut problems = Vec::new();
    loaded
        .installed
        .extend(installed_inputs(project, project_root, &mut problems));
    let path = project.path.display().to_string();
    // Print mode shows only the message, so it names its manifest.
    for problem in problems {
        loaded.warn((path.clone(), format!("{}: {problem}", project.label())));
    }
}

/// What chest gives a run in `project_root`, as [`load`] would, without
/// installing anything: for `yapi config`, which shows what a run loads.
pub fn installed(project_root: &Path, trusted: bool, global: bool) -> Loaded {
    let mut loaded = Loaded::default();
    for project in manifests(project_root, trusted, global) {
        match project {
            Ok(project) => add(&mut loaded, &project, project_root),
            Err(problem) => loaded.warn(problem),
        }
    }
    loaded
}

/// What chest gives a run of yapi in `project_root`: the project manifest,
/// only when `trusted`, and the user's global one, only when `global`, each
/// when it installs anything. Each puts back what its lock pins and is
/// missing, unless `offline`, and skips stale or unlocked entries with a
/// warning. It never writes a lock or `.gitignore`.
pub async fn load(project_root: &Path, trusted: bool, offline: bool, global: bool) -> Loaded {
    let mut loaded = Loaded::default();
    let mut npm = None;
    for project in manifests(project_root, trusted, global) {
        let mut project = match project {
            Ok(project) => project,
            Err(problem) => {
                loaded.warn(problem);
                continue;
            }
        };
        let path = project.path.display().to_string();
        let npm = npm.get_or_insert_with(|| Npm::new(crate::packages::npm::config()));
        match project
            .sync(Mode::Startup { offline }, npm, &mut |_| {})
            .await
        {
            Ok(report) => {
                for message in report.warnings.into_iter().chain(report.notices) {
                    loaded.warn((path.clone(), message));
                }
            }
            Err(err) => loaded.warn((path.clone(), err.to_string())),
        }
        add(&mut loaded, &project, project_root);
    }
    loaded
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::packages::resolve::Resolved;
    use yapi_types::rpc::SourceInfo;

    /// yapi's startup runs `load` in futures that move between threads.
    #[test]
    fn startup_load_send() {
        fn send<T: Send>(_: T) {}
        send(load(Path::new("."), false, true, true));
    }

    /// A chest file whose package has no policy runs with none, with a
    /// warning, while other sources' files get no policy at all. Warnings
    /// escape a file name's control characters. A file
    /// that two entries give loads modules from the narrower root, and one
    /// in an entry chest left out from itself alone, with a warning that
    /// names that entry too.
    #[test]
    fn chest_files_fail_closed() {
        let file = |path: &str, source: &str, scope: &str| SourceInfo {
            path: path.into(),
            source: source.into(),
            scope: scope.into(),
            origin: "package".into(),
            base_dir: None,
        };
        let enabled = |info| Resolved {
            info,
            enabled: true,
        };
        // A cloned project's file name may hold a terminal escape.
        let spoof = "/p/\x1b[2J.js";
        let resolved = ResolvedPaths {
            extensions: vec![
                enabled(file("/p/a.js", "chest:a", "project")),
                enabled(file("/p/b.js", "npm:b", "project")),
                enabled(file(spoof, "chest:a", "project")),
            ],
            ..ResolvedPaths::default()
        };
        let mut problems = Vec::new();
        let policies = file_policies(
            &resolved,
            &Installed::default(),
            Path::new("/p"),
            &mut problems,
        );
        let (file_a, file_spoof) = (PathBuf::from("/p/a.js"), PathBuf::from(spoof));
        let files = HashMap::from([
            (file_a.clone(), Policy::closed(&file_a)),
            (file_spoof.clone(), Policy::closed(&file_spoof)),
        ]);
        assert_eq!(policies.files, files);
        assert_eq!(problems.len(), 2, "{problems:?}");
        let warned = policies.also_loaded(&file_spoof, spoof, "cli");
        let warned = warned
            .into_iter()
            .flat_map(|(path, message)| [path, message]);
        let texts: Vec<String> = problems
            .iter()
            .flat_map(|(source, message)| [source.clone(), message.clone()])
            .chain(warned)
            .collect();
        assert!(
            texts.len() == 6 && texts.iter().all(|text| !text.contains('\x1b')),
            "{texts:?}"
        );
        let rooted = |root: &str| Policy {
            root: Some(root.into()),
            ..Policy::unrestricted()
        };
        let key = |scope: &str, name: &str| (scope.to_owned(), format!("chest:{name}"));
        let filesystem = Grants {
            filesystem: true,
            ..Grants::none()
        };
        let network = Grants {
            network: true,
            ..Grants::none()
        };
        let narrow = |grants, events| Policy {
            grants,
            events,
            ..rooted("/p/sub")
        };
        let installed = Installed {
            policies: HashMap::from([
                (key("project", "s"), rooted("/p")),
                (key("user", "s"), narrow(filesystem, EventPolicy::shared())),
                // `e` narrows only the event channels, and `g` the grants too.
                (
                    key("project", "e"),
                    narrow(filesystem, EventPolicy::shared()),
                ),
                (
                    key("user", "e"),
                    narrow(filesystem, EventPolicy::isolated()),
                ),
                (key("project", "g"), rooted("/p")),
                (
                    key("user", "g"),
                    narrow(filesystem, EventPolicy::isolated()),
                ),
            ]),
            // `chest install` would load `x` with its own grants.
            closed: [("/q", None), ("/p/sub/c.js", Some(network))]
                .map(|(root, grants)| Closed {
                    root: root.into(),
                    entry: "extensions.x in chest.toml".into(),
                    remove: "chest remove x".into(),
                    install: grants.map(|grants| {
                        let policy = narrow(grants, EventPolicy::shared());
                        ("chest install".to_owned(), policy)
                    }),
                })
                .into(),
            ..Installed::default()
        };
        let resolved = ResolvedPaths {
            extensions: vec![
                enabled(file("/p/sub/a.js", "chest:s", "project")),
                enabled(file("/p/sub/b.js", "chest:s", "user")),
                enabled(file("/p/sub/c.js", "chest:s", "project")),
                enabled(file("/p/sub/e.js", "chest:e", "project")),
                enabled(file("/p/sub/g.js", "chest:g", "project")),
            ],
            shadowed: vec![
                file("/p/sub/a.js", "chest:s", "user"),
                file("/p/sub/b.js", "chest:s", "project"),
                file("/p/sub/c.js", "chest:s", "user"),
                file("/p/sub/e.js", "chest:e", "user"),
                file("/p/sub/g.js", "chest:g", "user"),
            ],
            ..ResolvedPaths::default()
        };
        problems.clear();
        let policies = file_policies(&resolved, &installed, Path::new("/p"), &mut problems);
        let root = |path: &str| policies.of(Path::new(path)).root;
        for file in ["/p/sub/a.js", "/p/sub/b.js"] {
            assert_eq!(root(file), Some("/p/sub".into()), "{file}");
        }
        // A file two entries give warns once, naming both, as it gets
        // only what both allow, or nothing when an entry left out holds it,
        // and then what installing that entry would leave it. The warning
        // says whether that narrows grants, event channels or both.
        let (project, user) = (
            "extensions.s in chest.toml",
            "extensions.s in ~/.chest/chest.toml",
        );
        let shared = |file: &str, first: &str, second: &str| {
            let message = format!(
                "{file} is in {first} and {second}, so it gets only the grants both give: fs"
            );
            ("chest:s".to_owned(), message)
        };
        let closed = format!(
            "/p/sub/c.js is in {project} and {user}, but extensions.x in chest.toml holds it too and does not load, so it runs with no capabilities: run `chest install`, which gives it only the grants all give: none, or `chest remove x`"
        );
        let narrowed = |name: &str, what: &str| {
            let message = format!(
                "/p/sub/{name}.js is in extensions.{name} in chest.toml and extensions.{name} in ~/.chest/chest.toml, so it gets only the {what}"
            );
            (format!("chest:{name}"), message)
        };
        let both = [
            shared("/p/sub/a.js", project, user),
            shared("/p/sub/b.js", user, project),
            ("chest:s".to_owned(), closed),
            narrowed("e", "event channels both give"),
            narrowed("g", "event channels and the grants both give: fs"),
        ];
        assert_eq!(problems, both);
        assert_eq!(root("/q/a.js"), Some("/q/a.js".into()));
        assert_eq!(root("/p/sub/c.js"), Some("/p/sub/c.js".into()));
    }
}