agix-base 0.4.4

Agix — foundation types, traits, and context
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
//! Tool authorization, in two parts: [`PermissionPolicy`] decides whether a
//! call runs, [`PermissionGrants`] keeps the allows a person or a skill hands
//! out along the way. A session holds the two together as a [`PermissionSet`].
//! What a policy weighs about a call is its [`ToolFacts`].

use async_trait::async_trait;
use serde_json::Value;
use std::path::PathBuf;
use std::sync::Arc;

use crate::context::Session;
use crate::interface::tool::{CallTraits, Tool, ToolContext, ToolOrigin};
use crate::permission::{PermissionMode, PermissionRule};

mod profile;
pub use profile::{Approval, ExecProfile, Obligations};

/// How the permission modes treat a tool's calls: `ToolSpec::stance`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ModeStance {
    #[default]
    Normal,
    /// A coordination primitive that touches no file, shell or network: a
    /// self-contained call of it is spared `Plan` and `DontAsk`.
    Coordination,
    /// The call that takes the session out of plan mode, which plan mode
    /// would otherwise refuse as a change of state.
    LeavesPlan,
}

/// What a call names, as permission rules are matched against it.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Subject {
    #[default]
    None,
    /// Compared to rule patterns verbatim.
    Opaque(String),
    /// A path read; `Read(...)` rules apply to it as well as the tool's own.
    ReadPath(String),
    /// A path written; `Edit(...)` rules apply to it as well as the tool's own.
    WritePath(String),
    /// A command line, matched one simple command at a time.
    Shell(String),
    Url(String),
}

impl Subject {
    pub fn content(&self) -> Option<&str> {
        match self {
            Self::None => None,
            Self::Opaque(s)
            | Self::ReadPath(s)
            | Self::WritePath(s)
            | Self::Shell(s)
            | Self::Url(s) => Some(s),
        }
    }
}

/// How far a call reaches beyond the session itself.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Reach {
    /// Only the session's own state: todos, tasks, the mailbox, the plan.
    SessionOnly,
    /// Starts other calls, each judged on its own.
    Delegates,
    /// Only endpoints the operator configured.
    Operator,
    /// Within the roots the session works in without asking: the readable
    /// ones for a read, the writable ones for a write.
    Inside,
    /// A path outside those roots.
    Outside,
    /// Runs code the model did not write that may change files inside the
    /// project only: asked about unless the mode accepts edits.
    EditsProject,
    /// A host the model chose; `listed` when `web.allowed_domains` names it.
    Host {
        listed: bool,
    },
    /// An MCP server's tool, whose word about itself does not count.
    Server,
    /// Leaves something behind that acts later on its own.
    Persists(&'static str),
    /// The tool knows this input must not run, and says why.
    Refused(String),
    Unknown,
}

/// The call runs a command inside the session's sandbox.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Runs {
    /// It keeps running after the call returns.
    pub background: bool,
}

impl Runs {
    pub fn foreground() -> Self {
        Self { background: false }
    }
    pub fn background() -> Self {
        Self { background: true }
    }
}

/// Content others wrote that a call hands back through an endpoint its
/// [`Reach`] does not show: an operator's endpoint relaying it.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Relays {
    /// What a search found on the web.
    Web,
    /// What an MCP server serves, by its configured name when the call names it.
    McpServer(Option<String>),
}

/// What a tool says about one call for the permission policy to weigh:
/// [`Tool::permission_facts`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct PermissionFacts {
    pub subject: Subject,
    pub reach: Reach,
    pub runs: Option<Runs>,
    /// What to put to a person when the call is asked about.
    pub question: Option<String>,
    pub relays: Option<Relays>,
    /// A second name the call answers to for deny rules: a tool that acts on
    /// one MCP server's behalf is also `mcp__<server>`.
    pub also_named: Option<String>,
    /// Where a write the call makes lands, as judged: the call writes there
    /// or nowhere.
    pub lands_at: Option<std::path::PathBuf>,
    /// The host's declaration of the tool lets the call through: counted
    /// only for a tool of [`ToolOrigin::Host`].
    pub host_permits: bool,
}

impl PermissionFacts {
    /// Claims nothing, so the call is asked about.
    pub fn unknown() -> Self {
        Self::new(Subject::None, Reach::Unknown)
    }

    pub fn new(subject: Subject, reach: Reach) -> Self {
        Self {
            subject,
            reach,
            runs: None,
            question: None,
            relays: None,
            also_named: None,
            lands_at: None,
            host_permits: false,
        }
    }

    pub fn with_runs(mut self, runs: Runs) -> Self {
        self.runs = Some(runs);
        self
    }

    pub fn with_question(mut self, question: impl Into<String>) -> Self {
        self.question = Some(question.into());
        self
    }

    pub fn relaying(mut self, relays: Relays) -> Self {
        self.relays = Some(relays);
        self
    }

    pub fn also_named(mut self, name: impl Into<String>) -> Self {
        self.also_named = Some(name.into());
        self
    }

    pub fn landing_at(mut self, path: std::path::PathBuf) -> Self {
        self.lands_at = Some(path);
        self
    }

    pub fn permitted_by_host(mut self) -> Self {
        self.host_permits = true;
        self
    }
}

/// Everything the permission policy reads about one call: the tool's spec,
/// its [`CallTraits`] for the input and its [`PermissionFacts`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ToolFacts {
    pub subject: Subject,
    pub traits: CallTraits,
    pub stance: ModeStance,
    pub origin: ToolOrigin,
    pub reach: Reach,
    pub runs: Option<Runs>,
    pub question: Option<String>,
    pub relays: Option<Relays>,
    pub also_named: Option<String>,
    pub lands_at: Option<std::path::PathBuf>,
    /// The host's declaration lets the call through. A tool's own word is
    /// not enough: the session running the call keeps it only for a host tool
    /// it admitted under that name.
    pub host_permits: bool,
}

impl ToolFacts {
    /// Gathered once per call, by the session that runs it and with the
    /// context it runs with.
    pub async fn gather(tool: &dyn Tool, input: &Value, ctx: &ToolContext) -> Self {
        let spec = tool.spec();
        let facts = tool.permission_facts(input, ctx).await;
        let host_permits = facts.host_permits && spec.origin == ToolOrigin::Host;
        Self {
            subject: facts.subject,
            traits: tool.call_traits(input),
            stance: spec.stance,
            origin: spec.origin,
            reach: facts.reach,
            runs: facts.runs,
            question: facts.question,
            relays: facts.relays,
            also_named: facts.also_named,
            lands_at: facts.lands_at,
            host_permits,
        }
    }

    /// For a name no tool answers to: claims nothing, not even a read.
    pub fn unknown_tool() -> Self {
        Self {
            subject: Subject::None,
            traits: CallTraits::default(),
            stance: ModeStance::Normal,
            origin: ToolOrigin::default(),
            reach: Reach::Unknown,
            runs: None,
            question: None,
            relays: None,
            also_named: None,
            lands_at: None,
            host_permits: false,
        }
    }
}

/// What a `PreToolUse` hook answered about the call.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum HookAnswer {
    #[default]
    None,
    Approve {
        hook: String,
    },
    Block {
        hook: String,
        reason: String,
    },
}

/// Whether someone can be asked about the call.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Attendance {
    Attended,
    Unattended,
    /// Nothing could reach a person even if one were there, and why.
    NoOneToAsk(&'static str),
}

/// What a policy concluded about one call.
#[derive(Debug, Clone)]
pub enum PermissionOutcome {
    Permit {
        basis: Basis,
        profile: ExecProfile,
        obligations: Obligations,
    },
    Deny {
        reason: Reason,
    },
    /// Put to whoever can answer: the engine emits
    /// `AgentEvent::PermissionPrompt` and waits for the answer.
    Prompt {
        prompt_id: String,
        message: String,
        paths: Vec<PathBuf>,
        reason: Reason,
        answerable: Answerable,
    },
}

impl PermissionOutcome {
    pub fn deny(stage: Stage, message: impl Into<String>) -> Self {
        Self::Deny {
            reason: Reason::new(stage, message),
        }
    }

    /// A question anyone who can answer may answer, under a fresh id.
    pub fn prompt(stage: Stage, message: impl Into<String>) -> Self {
        let message = message.into();
        Self::Prompt {
            prompt_id: uuid::Uuid::new_v4().to_string(),
            reason: Reason::new(stage, message.clone()),
            message,
            paths: Vec::new(),
            answerable: Answerable::HookOrPerson,
        }
    }

    pub fn is_permit(&self) -> bool {
        matches!(self, Self::Permit { .. })
    }
}

/// On whose word a call runs.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Basis {
    Rule(PermissionRule),
    /// It stays where the session works without asking.
    Contained,
    AcceptEdits,
    Bypass,
    /// A `PreToolUse` hook approved it, by name.
    HookApproved(String),
    /// A command nobody can be asked about, which the session's sandbox holds.
    UnattendedConfined,
    /// The host's declaration of its own tool (`permit: "allow"`).
    HostDeclared,
    Person,
}

impl Basis {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Rule(_) => "rule",
            Self::Contained => "contained",
            Self::AcceptEdits => "accept_edits",
            Self::Bypass => "bypass",
            Self::HookApproved(_) => "hook_approved",
            Self::UnattendedConfined => "unattended_confined",
            Self::HostDeclared => "host_declared",
            Self::Person => "person",
        }
    }
}

/// Who may answer a question a policy asks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Answerable {
    HookOrPerson,
    /// The deployment's managed layer asks it: a hook's approve does not
    /// answer it.
    PersonOnly,
}

/// Where in the policy's order a call was refused or asked about.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Stage {
    ManagedUnreadable,
    UnknownTool,
    Credentials,
    Hook,
    ToolRefusal,
    /// The session's sandbox cannot hold the command as fully as
    /// `sandbox.require` demands.
    Confinement,
    Rule,
    ManagedRule,
    /// A rule the delegate runs under: its type's deny rules.
    DelegateRule,
    BypassImmune,
    PlanMode,
    DontAsk,
    /// A write to the agent's or the repository's configuration, which the
    /// tool's own word does not let through.
    Configuration,
    ModeDefault,
    Attendance,
    /// A write under a skill directory a host listed for some session
    /// (`options.skill_dirs`): read-only to every session.
    HostSkillDir,
}

impl Stage {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::ManagedUnreadable => "managed_unreadable",
            Self::UnknownTool => "unknown_tool",
            Self::Credentials => "credentials",
            Self::Hook => "hook",
            Self::ToolRefusal => "tool_refusal",
            Self::Confinement => "confinement",
            Self::Rule => "rule",
            Self::ManagedRule => "managed_rule",
            Self::DelegateRule => "delegate_rule",
            Self::BypassImmune => "bypass_immune",
            Self::PlanMode => "plan_mode",
            Self::DontAsk => "dont_ask",
            Self::Configuration => "configuration",
            Self::ModeDefault => "mode_default",
            Self::Attendance => "attendance",
            Self::HostSkillDir => "host_skill_dir",
        }
    }
}

/// Why a call was refused or asked about.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Reason {
    pub stage: Stage,
    pub rule: Option<PermissionRule>,
    pub message: String,
}

impl Reason {
    pub fn new(stage: Stage, message: impl Into<String>) -> Self {
        Self {
            stage,
            rule: None,
            message: message.into(),
        }
    }

    pub fn with_rule(mut self, rule: PermissionRule) -> Self {
        self.rule = Some(rule);
        self
    }
}

impl std::fmt::Display for Reason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

/// One tool call put to a [`PermissionPolicy`].
#[derive(Clone, Copy)]
#[non_exhaustive]
pub struct PermissionRequest<'a> {
    pub tool_name: &'a str,
    /// The tool the call dispatches to, looked up in the registry dispatch
    /// uses. `None` is a name nothing answers to.
    pub tool: Option<&'a Arc<dyn Tool>>,
    pub input: &'a serde_json::Value,
    /// What the tool says about the call, gathered with `ctx`.
    pub facts: &'a ToolFacts,
    /// The context the call runs with: the judged session's.
    pub ctx: &'a ToolContext,
    /// The live state of the session the policy belongs to: its permission
    /// mode as it is now, what it has read.
    pub session: &'a Arc<dyn Session>,
    /// Set when the call is a delegate's — a sub-agent's, a team member's —
    /// relayed to this session's policy.
    pub delegate: Option<Delegate<'a>>,
    pub hook: &'a HookAnswer,
    pub attendance: Attendance,
}

const NO_HOOK: &HookAnswer = &HookAnswer::None;

impl<'a> PermissionRequest<'a> {
    /// A call no hook answered, as attended as `ctx` says.
    pub fn new(
        tool_name: &'a str,
        tool: Option<&'a Arc<dyn Tool>>,
        input: &'a serde_json::Value,
        facts: &'a ToolFacts,
        ctx: &'a ToolContext,
        session: &'a Arc<dyn Session>,
    ) -> Self {
        Self {
            tool_name,
            tool,
            input,
            facts,
            ctx,
            session,
            delegate: None,
            hook: NO_HOOK,
            attendance: ctx.attendance,
        }
    }

    pub fn for_delegate(mut self, delegate: Delegate<'a>) -> Self {
        self.delegate = Some(delegate);
        self
    }

    pub fn with_hook(mut self, hook: &'a HookAnswer) -> Self {
        self.hook = hook;
        self
    }

    pub fn with_attendance(mut self, attendance: Attendance) -> Self {
        self.attendance = attendance;
        self
    }

    /// The state the call is judged against: the delegate's when there is
    /// one, the session's otherwise.
    pub fn judged(&self) -> &'a Arc<dyn Session> {
        self.delegate.map_or(self.session, |d| d.session)
    }
}

/// A delegate's call as the session that relays it hands it on: judged
/// against the delegate's state, in `mode`, and held to `rules` on top of the
/// policy's own.
#[derive(Clone, Copy)]
#[non_exhaustive]
pub struct Delegate<'a> {
    /// The session the call runs in.
    pub session: &'a Arc<dyn Session>,
    /// The delegate's mode as every session between it and the policy holds
    /// it (`PermissionMode::for_delegate`, once per relaying session). The
    /// policy holds it to its own session's mode in turn.
    pub mode: PermissionMode,
    /// What the delegation itself adds: its `deny` rules refuse the call in
    /// any mode; its `allow` rules are the delegate's own grants, which answer
    /// only the question the mode would put — never a deny, an ask rule, or
    /// a question only a person may answer.
    pub rules: &'a [PermissionRule],
    /// The delegate may only be stricter than each session relaying it: an
    /// agent type that may not loosen (`AgentTypeSource::may_loosen`) named
    /// its mode. A session that bypasses does not take it along.
    pub tightens_only: bool,
}

impl<'a> Delegate<'a> {
    /// `session`'s call in its own live mode, with nothing added.
    pub fn of(session: &'a Arc<dyn Session>) -> Self {
        Self::new(session, session.permission_mode(), &[])
    }

    pub fn new(
        session: &'a Arc<dyn Session>,
        mode: PermissionMode,
        rules: &'a [PermissionRule],
    ) -> Self {
        Self {
            session,
            mode,
            rules,
            tightens_only: false,
        }
    }

    /// The mode its call is judged in, relayed through a session in `parent`.
    pub fn mode_under(&self, parent: PermissionMode) -> PermissionMode {
        if self.tightens_only {
            self.mode.min_permissive(parent)
        } else {
            parent.for_delegate(self.mode)
        }
    }
}

/// Whether a tool call runs. The engine's own is
/// `permissions::policy::Policy` (rules, modes, the
/// tools' own verdicts).
#[async_trait]
pub trait PermissionPolicy: Send + Sync {
    async fn decide(&self, req: &PermissionRequest<'_>) -> PermissionOutcome;
}

/// Which paths a deny rule covers, for a tool that walks a directory on its
/// own: a search is judged on the directory it starts in, so each file under
/// it has to be judged as it is reached.
pub trait PathDenials: Send + Sync {
    /// The paths `tool` must leave out of its walk. `extra` is the rules a
    /// delegation adds on top of the policy's own. `None` when no rule
    /// covers a path.
    fn denial(
        &self,
        tool: &str,
        cwd: &std::path::Path,
        extra: &[PermissionRule],
    ) -> Option<PathDenial>;

    /// The paths `Read` deny rules name outright, resolved from `cwd` as they
    /// are judged there; a glob other than a trailing `/**` or `/*` names none.
    fn denied_read_paths(&self, cwd: &std::path::Path, extra: &[PermissionRule]) -> Vec<PathBuf>;
}

/// Whether a deny rule covers this path.
pub type PathDenial = Arc<dyn Fn(&std::path::Path) -> bool + Send + Sync>;

/// How long a [`Grant`] lasts.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum GrantScope {
    /// Until the next user turn starts: a skill's `allowed-tools`.
    Turn,
    /// For the rest of the session: "always allow" answered for this session.
    Session,
    /// For this project, kept in its `settings.local.json`; a session with no
    /// project keeps it for the session.
    Project,
}

/// How an allow's content meets a call's.
#[derive(
    Clone,
    Copy,
    PartialEq,
    Eq,
    Debug,
    Default,
    serde::Serialize,
    serde::Deserialize,
    schemars::JsonSchema,
)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum GrantMatch {
    /// As a `settings.json` rule's content is: `*`, `prefix:*`, path and URL
    /// patterns.
    #[default]
    Pattern,
    /// Only a call whose content is the same text.
    Exact,
}

impl GrantMatch {
    pub fn is_pattern(&self) -> bool {
        *self == Self::Pattern
    }
}

/// An allow for calls to one tool.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Grant {
    pub tool_name: String,
    /// `None` allows every call to the tool. Otherwise matched as `matching`
    /// says.
    ///
    /// A grant relayed from project-controlled data — a skill's frontmatter —
    /// must not be `None`: anyone who can write a file in the repository could
    /// then switch off confirmation for `Bash` wholesale.
    pub content: Option<String>,
    pub scope: GrantScope,
    /// A [`GrantScope::Turn`] grant is a skill's rule pattern; a
    /// [`GrantScope::Session`] or [`GrantScope::Project`] one is a person's
    /// answer about one call, so it covers that call's content exactly and
    /// not whatever the text happens to match as a pattern.
    pub matching: GrantMatch,
}

impl Grant {
    pub fn new(tool_name: impl Into<String>, content: Option<String>, scope: GrantScope) -> Self {
        Self {
            tool_name: tool_name.into(),
            content,
            matching: match scope {
                GrantScope::Turn => GrantMatch::Pattern,
                GrantScope::Session | GrantScope::Project => GrantMatch::Exact,
            },
            scope,
        }
    }
}

#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum GrantError {
    /// Kept for the session, but not written where it would outlast it.
    #[error("kept for this session only, not written to {}: {reason}", .path.display())]
    NotPersisted { path: PathBuf, reason: String },
}

/// The file a project-scoped "allow every time" is written to.
pub trait RuleFile: Send + Sync + std::fmt::Debug {
    fn path(&self) -> &std::path::Path;
    /// Add `rule` as an allow. `Ok(false)` when it was already there.
    fn append_allow(&self, rule: &str, matching: GrantMatch) -> Result<bool, String>;
}

/// Where the allows handed out during a session are kept.
#[async_trait]
pub trait PermissionGrants: Send + Sync {
    async fn grant(&self, grant: Grant) -> Result<(), GrantError>;
    /// Drop every grant of `scope`.
    fn clear(&self, scope: GrantScope);
}

/// A session's permission: the policy its calls are judged by and where the
/// allows it is handed are kept. With no `grants` nothing is kept, so nobody
/// is offered an "allow every time".
#[derive(Clone)]
pub struct PermissionSet {
    pub policy: Arc<dyn PermissionPolicy>,
    pub grants: Option<Arc<dyn PermissionGrants>>,
    pub path_denials: Option<Arc<dyn PathDenials>>,
}

impl PermissionSet {
    /// One value that both decides and keeps grants, so what it is granted
    /// changes what it decides.
    pub fn of<P: PermissionPolicy + PermissionGrants + PathDenials + 'static>(p: Arc<P>) -> Self {
        Self {
            policy: p.clone(),
            grants: Some(p.clone()),
            path_denials: Some(p),
        }
    }

    pub fn without_grants(policy: Arc<dyn PermissionPolicy>) -> Self {
        Self {
            policy,
            grants: None,
            path_denials: None,
        }
    }
}