tilo-action-policy 0.5.2

Who may invoke which action: grants over the published tree and the one function that decides them.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
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
//! Who made a change: one tree, and the one admission every action API takes.

use crate::{
    Actor, Admission, Answer, Asker, Authority, Context, Grant, GrantId, Grantee, Invoked,
    MemberId, Party, Reach, Refused, TreePath, WorkspacePolicy, admit,
};

/// [`crate::Acting::asker`] for an acting with software or a device between
/// the person and the change when `delegated`.
#[must_use]
pub const fn asker_of(delegated: bool, host_reaches_person: bool) -> Asker {
    Asker {
        reachable: !delegated || host_reaches_person,
        delegated,
    }
}

impl crate::Acting {
    /// Who an invocation of this acting asks: the person, who can answer when
    /// they act themselves or when the host running it reaches them
    /// (`host_reaches_person`); and whether someone acts for them, who is
    /// asked before an action the person attends.
    #[must_use]
    pub fn asker(&self, host_reaches_person: bool) -> Asker {
        asker_of(self.delegated(), host_reaches_person)
    }

    /// The preset every handed agent of the tree carries in `workspace`
    /// ([`crate::Acting::handed`], [`crate::roles::agent_preset`]), for the
    /// person it acts for: the one place a device's delegation to its agent
    /// becomes grants, so the device that admitted the agent and every
    /// receiver of its changes decide it alike.
    #[must_use]
    pub fn handed_preset(&self, workspace: &str) -> Vec<Grant> {
        self.nodes()
            .into_iter()
            .filter(|node| node.handed)
            .filter_map(|node| Some((node.member_id()?.as_agent()?, node.acted_for_person()?)))
            .flat_map(|((agent, own), person)| {
                crate::roles::agent_preset(workspace, (agent, own.as_str()), person)
            })
            .collect()
    }
}

impl<'a> Authority<'a> {
    /// The authority of `acting` under `policy`: an action is allowed only when
    /// every node of its tree allows it.
    ///
    /// A person is decided by their grants and their role's; from a device that
    /// is not theirs, by what such a device may do at most
    /// ([`crate::Cap::FOREIGN_DEVICE`]). An agent or bot by the grants naming
    /// it in the workspace and the ones its person gave it (`granted`). An
    /// automation by what the people it runs for gave it.
    #[must_use]
    pub fn of_acting(
        acting: &'a crate::Acting,
        policy: &'a WorkspacePolicy,
        granted: &'a [Grant],
    ) -> Self {
        let mut people: usize = 0;
        let mut person_parts = Vec::new();
        let mut others = Vec::new();
        // An automation acts with its owner's party: the tree names it for the
        // person it runs for, and its steps are that person's grants. One that
        // runs for nobody allows nothing.
        let owned = !acting.acted_for.is_empty();
        // The answers a person gave the agent session that caused the acting decide for the
        // whole tree, as the agent's own do.
        let answers: Vec<&Grant> = match &acting.cause {
            Some(cause) => crate::session_answers(&policy.grants, cause.as_str()).collect(),
            None => Vec::new(),
        };
        for node in acting.nodes() {
            match &node.by {
                Actor::Member(member) => match member {
                    MemberId::Person(person) => {
                        people = people.saturating_add(1);
                        let party = policy
                            .party(person.as_str())
                            .with_grants(granted)
                            .with_grants(answers.iter().copied());
                        person_parts.push(if node.foreign_device {
                            party.capped(crate::Cap::FOREIGN_DEVICE)
                        } else {
                            party
                        });
                    }
                    MemberId::Agent { .. } | MemberId::Bot(_) => {
                        others.push(
                            software_party(member, policy, granted)
                                .with_grants(answers.iter().copied()),
                        );
                    }
                },
                // A visitor holds what the workspace opens to any key, and nothing else.
                Actor::Visitor(_) => others.push(policy.party_of(Grantee::AnyKey)),
                Actor::Automation(_) if owned => {}
                // An app acting by itself holds the grants naming it, never a person's.
                Actor::Automation(_) => others.push(Party::default()),
                // An app acting by itself, in the process its device launched as it, holds the
                // grants naming it, with those its device hands it from its signed manifest.
                Actor::App(app) => others.push(
                    policy
                        .party_of(Grantee::app(app.as_str()))
                        .with_grants(granted),
                ),
            }
        }
        let mut parts = person_parts.into_iter().chain(others);
        Self {
            person: parts.next().unwrap_or_default(),
            bounds: parts.collect(),
            people,
            app: acting.app.as_ref(),
            app_kinds: None,
        }
    }
}

/// A member of `member` as a grant names it.
#[must_use]
pub fn granted_to(member: &MemberId) -> Grantee {
    Grantee::Member(member.clone())
}

/// An agent's or bot's own grants: the workspace's naming it, and those its
/// acting carries (`granted`, what its certificate's delegation gives it). An
/// agent is the pair of its id and the person it acts for, whom its
/// certificate chains to: only the grants and answers naming that pair reach
/// it, and once its person answered it ([`WorkspacePolicy::answers_narrow`]) what
/// `granted` hands it decides nothing.
fn software_party<'a>(
    member: &MemberId,
    policy: &'a WorkspacePolicy,
    granted: &'a [Grant],
) -> Party<'a> {
    match member {
        // A person's answer for their agent narrows it to what their answers name, whatever its
        // delegation hands it.
        MemberId::Agent { id, person } => policy.party_of_agent(id, person.as_str()).narrowed_by(
            granted,
            policy
                .grants_of_agent(id, person.as_str())
                .filter(|grant| crate::agent_answers::is_answer(grant)),
        ),
        member => policy.party_of(granted_to(member)).with_grants(granted),
    }
}

/// One invocation, as admission weighs it.
#[derive(Clone, Copy, Debug)]
pub struct Invocation<'a> {
    /// The action's node.
    pub node: &'a TreePath,
    /// The kind of action it is.
    pub kind: crate::ActionKind,
    /// Whether it exercises authority over the workspace or others in it
    /// ([`crate::Verdict::privileged`]).
    pub privileged: bool,
    /// Whether it needs its person attending.
    pub attended: bool,
    /// The node of the resource it changes, decided as well where it is one:
    /// so a person restricted on one resource cannot change it through an
    /// action they may otherwise take.
    pub resource: Option<&'a TreePath>,
    /// How far the invocation it continues was admitted to reach, when it
    /// continues one: what goes further asks first.
    pub ceiling: Option<Reach>,
}

/// The grants' answer for an action of `kind`, privileged when
/// `privileged`, at `node`, for `authority` at `cx`, and the `Once` grants
/// relying on it spends. A step going further than `ceiling` (the
/// invocation it continues) is asked for as its own request.
#[must_use]
pub fn answer_at(
    authority: &Authority<'_>,
    (node, kind, privileged): (&TreePath, crate::ActionKind, bool),
    ceiling: Option<Reach>,
    cx: &Context<'_>,
) -> (Answer, Vec<GrantId>) {
    let verdict = verdict_at(authority, (node, kind, privileged), ceiling, cx);
    (verdict.answer, verdict.spent())
}

/// The verdict of `answer_at`, kept whole: what it was decided by, so admission can tell the
/// answer a person gave in advance ([`Authority::answered_in_advance`]) from one asking them
/// now.
fn verdict_at<'a>(
    authority: &'a Authority<'_>,
    (node, kind, privileged): (&TreePath, crate::ActionKind, bool),
    ceiling: Option<Reach>,
    cx: &Context<'_>,
) -> crate::Verdict<'a> {
    let mut verdict = authority.decide(node, kind, cx);
    if privileged {
        verdict = verdict.privileged();
    }
    let exceeds = ceiling.is_some_and(|ceiling| !ceiling.covers(kind, privileged));
    if verdict.answer == Answer::Allow && exceeds {
        verdict.answer = Answer::Ask;
    }
    verdict
}

/// Admit `invocation` for `authority`, asked of `asker`, at `cx`: whether
/// it runs or asks first, and the `Once` grants admitting it spends.
///
/// # Errors
/// Whose answer refused it.
pub fn admit_invocation(
    authority: &Authority<'_>,
    asker: Asker,
    invocation: Invocation<'_>,
    cx: &Context<'_>,
) -> Result<(Admission, Vec<GrantId>), Refused> {
    let verdict = verdict_at(
        authority,
        (invocation.node, invocation.kind, invocation.privileged),
        invocation.ceiling,
        cx,
    );
    // What its person already answered for this very action or resource was not theirs to ask
    // again, though it reaches outside the system: asking would put to them what they answered.
    let answered_in_advance = authority.answered_in_advance(&verdict);
    let answer = verdict.answer;
    let spent = verdict.spent();
    let resource = invocation.resource.map(|resource| {
        answer_at(
            authority,
            (resource, invocation.kind, false),
            invocation.ceiling,
            cx,
        )
        .0
    });
    let admission = admit(
        Invoked {
            answer,
            attended: invocation.attended,
            outside: invocation.kind == crate::ActionKind::Outside,
            answered_in_advance,
            resource,
        },
        asker,
    )?;
    Ok((admission, spent))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        Acting, ActionKind, Answer, Context, Grant, Grantee, KindSet, Role, SESSION_ANSWER_PREFIX,
        Verdict, roles,
    };

    const WORKSPACE: &str = "w";

    fn policy() -> WorkspacePolicy {
        let mut policy = WorkspacePolicy::new(WORKSPACE, "owner");
        policy.members.insert("editor".into(), Role::Editor);
        policy.members.insert("viewer".into(), Role::Viewer);
        policy
    }

    fn app() -> TreePath {
        TreePath::workspace(WORKSPACE).app("so.tilo.notes")
    }

    fn answer(
        acting: &Acting,
        policy: &WorkspacePolicy,
        granted: &[Grant],
        kind: ActionKind,
    ) -> Answer {
        Authority::of_acting(acting, policy, granted)
            .decide(&app(), kind, &Context::at_secs(1))
            .answer
    }

    tilo_test_env::scenario! {
        fn whoever_acts_is_decided_by_every_party_it_acts_for;

        phase a_person_is_decided_by_their_grants_and_their_roles;
        {
            let policy = policy();
            assert_eq!(
                answer(
                    &Acting::person("editor"),
                    &policy,
                    &[],
                    ActionKind::Undoable
                ),
                Answer::Allow
            );
            assert_eq!(
                answer(
                    &Acting::person("viewer"),
                    &policy,
                    &[],
                    ActionKind::Undoable
                ),
                Answer::Deny
            );
        }

        phase every_person_in_the_tree_has_to_allow_it;
        {
            let policy = policy();
            // What the agent's turn gives it: reading and changing the workspace.
            let turn = [Grant::new(
                "turn:scribe:0",
                Grantee::agent("scribe", "editor"),
                TreePath::workspace(WORKSPACE),
                KindSet::READ_UNDOABLE,
                Answer::Allow,
                Grantee::person("editor"),
            )];
            let agent_for_editor_and_owner = Acting::agent("scribe", "editor")
                .acting_for(Acting::person("editor"))
                .acting_for(Acting::person("owner"));
            assert_eq!(
                answer(
                    &agent_for_editor_and_owner,
                    &policy,
                    &turn,
                    ActionKind::Undoable
                ),
                Answer::Allow
            );
            let agent_for_a_viewer = agent_for_editor_and_owner.acting_for(Acting::person("viewer"));
            assert_eq!(
                answer(&agent_for_a_viewer, &policy, &turn, ActionKind::Undoable),
                Answer::Deny,
                "the viewer's grants decide too"
            );
        }

        phase an_app_acting_by_itself_is_decided_by_the_grants_naming_it;
        {
            let app = Acting::by_app("so.tilo.notes");
            assert_eq!(
                answer(&app, &policy(), &[], ActionKind::Undoable),
                Answer::Deny,
                "an app holds no person's grants, the owner's included"
            );
            // Nor does the owner's device lend it theirs by signing what it wrote.
            let own = TreePath::workspace(WORKSPACE).app("so.tilo.notes");
            let signed = crate::MemberAccess::person(policy(), "owner")
                .invoked_by(&app)
                .expect("the owner's device writes for an app it runs");
            assert_eq!(
                signed.answer(&own, ActionKind::Undoable, &Context::at_secs(1)),
                Answer::Deny,
                "the device that signs an app's write bounds it, and grants it nothing"
            );
            // Installing Notes gave it its own records, and nothing beyond them.
            let mut policy = policy();
            policy.grants.push(crate::roles::app_records_grant(
                WORKSPACE,
                "so.tilo.notes",
                "owner",
            ));
            assert_eq!(
                answer(&app, &policy, &[], ActionKind::Undoable),
                Answer::Allow
            );
            let elsewhere = TreePath::workspace(WORKSPACE)
                .app("so.tilo.chat")
                .kind("chat.messages");
            assert_eq!(
                Authority::of_acting(&app, &policy, &[])
                    .decide(&elsewhere, ActionKind::Undoable, &Context::at_secs(1))
                    .answer,
                Answer::Deny,
                "another app's records are no part of what installing gave"
            );
        }

        phase an_agent_needs_its_own_grants_as_well_as_its_persons;
        {
            let mut policy = policy();
            policy.grants.push(Grant::new(
                "scribe-may-edit",
                Grantee::agent("scribe", "editor"),
                TreePath::workspace(WORKSPACE),
                KindSet::READ_UNDOABLE,
                Answer::Allow,
                Grantee::person("editor"),
            ));
            let acting = Acting::agent("scribe", "editor").acting_for(Acting::person("editor"));
            assert_eq!(
                answer(&acting, &policy, &[], ActionKind::Undoable),
                Answer::Allow
            );
            let unbounded = Acting::agent("other", "editor").acting_for(Acting::person("editor"));
            assert_eq!(
                answer(&unbounded, &policy, &[], ActionKind::Undoable),
                Answer::Deny,
                "an agent with no grants of its own does nothing"
            );
        }

        phase an_agent_answering_someone_else_keeps_its_own_persons_refusal;
        {
            // The agent's certificate gives it the workspace; its own person, the owner, answers
            // no for it on the whole workspace. Its turn answers the editor, through the owner.
            let turn = [Grant::new(
                "turn:scribe:0",
                Grantee::agent("scribe", "owner"),
                TreePath::workspace(WORKSPACE),
                KindSet::READ_UNDOABLE,
                Answer::Allow,
                Grantee::person("owner"),
            )];
            let answering_the_editor = Acting::agent("scribe", "owner")
                .acting_for(Acting::person("editor"))
                .agent_of("owner");
            assert_eq!(
                answer(&answering_the_editor, &policy(), &turn, ActionKind::Undoable),
                Answer::Allow,
                "both people and the agent's certificate allow it"
            );
            let mut refusing = policy();
            refusing.grants.push(crate::agent_answers::grant(
                WORKSPACE,
                ("scribe", "owner"),
                (&crate::agent_answers::AnswerScope::Workspace, ActionKind::Undoable),
                Answer::Deny,
                Grantee::person("owner"),
            ));
            assert_eq!(
                answer(&answering_the_editor, &refusing, &turn, ActionKind::Undoable),
                Answer::Deny,
                "what its own person answered for it refuses it, whoever's turn it answers"
            );
            let mut widening = refusing.clone();
            // The editor answers only for an agent of their own: `scribe` of the editor is
            // another agent.
            widening.grants.push(crate::agent_answers::grant(
                WORKSPACE,
                ("scribe", "editor"),
                (
                    &crate::agent_answers::AnswerScope::app("so.tilo.notes"),
                    ActionKind::Undoable,
                ),
                Answer::Allow,
                Grantee::person("editor"),
            ));
            assert_eq!(
                answer(&answering_the_editor, &widening, &turn, ActionKind::Undoable),
                Answer::Deny,
                "the more specific yes of the person a turn answers widens nothing the agent's own \
                 person refused"
            );
        }

        phase a_sessions_answers_decide_its_steps_and_no_other;
        {
            let session = "so.tilo.session/agent-session/release";
            let mut policy = policy();
            policy.grants.push(Grant::new(
                "scribe-asks",
                Grantee::agent("scribe", "editor"),
                TreePath::workspace(WORKSPACE),
                KindSet::READ_UNDOABLE,
                Answer::Ask,
                Grantee::person("editor"),
            ));
            policy.grants.push(Grant::new(
                format!("{SESSION_ANSWER_PREFIX}{session}:app:so.tilo.notes:undoable"),
                Grantee::agent("scribe", "editor"),
                app(),
                KindSet::UNDOABLE,
                Answer::Allow,
                Grantee::person("editor"),
            ));
            let run = Acting::agent("scribe", "editor")
                .acting_for(Acting::person("editor"))
                .caused_by(session);
            assert_eq!(
                answer(&run, &policy, &[], ActionKind::Undoable),
                Answer::Allow,
                "the person's answer for the session decides its steps"
            );
            let another = Acting::agent("scribe", "editor")
                .acting_for(Acting::person("editor"))
                .caused_by("so.tilo.session/agent-session/other");
            assert_eq!(
                answer(&another, &policy, &[], ActionKind::Undoable),
                Answer::Ask,
                "another session's answers decide nothing here"
            );
        }

        phase an_automation_acts_with_its_owners_party_bounded_by_its_steps;
        {
            let policy = policy();
            let automation = Acting::automation("1")
                .acting_for(Acting::agent("scribe", "editor").acting_for(Acting::person("editor")));
            let turn = [Grant::new(
                "turn:scribe:0",
                Grantee::agent("scribe", "editor"),
                TreePath::workspace(WORKSPACE),
                KindSet::READ_UNDOABLE,
                Answer::Allow,
                Grantee::person("editor"),
            )];
            assert_eq!(
                answer(&automation, &policy, &turn, ActionKind::Undoable),
                Answer::Allow,
                "the tree's people and software decide; the automation is its owner's"
            );
            // A step it was given may narrow what it does there.
            let mut granted = turn.to_vec();
            granted.push(Grant::new(
                "automation-1:outside",
                Grantee::person("editor"),
                TreePath::workspace(WORKSPACE),
                KindSet::OUTSIDE,
                Answer::Deny,
                Grantee::person("editor"),
            ));
            assert_eq!(
                answer(&automation, &policy, &granted, ActionKind::Outside),
                Answer::Deny
            );
            let homeless = Acting::automation("1");
            assert_eq!(
                answer(&homeless, &policy, &turn, ActionKind::Undoable),
                Answer::Deny,
                "an automation that runs for nobody allows nothing"
            );
        }

        phase a_device_that_is_not_theirs_governs_nothing_and_asks_before_reaching_out;
        {
            let policy = policy();
            let foreign = Acting::person("owner").on_foreign_device(true);
            assert_eq!(
                answer(&foreign, &policy, &[], ActionKind::Outside),
                Answer::Ask
            );
            let invite = roles::governance_action(WORKSPACE, roles::governance::ADD_MEMBER);
            let verdict: Verdict<'_> = Authority::of_acting(&foreign, &policy, &[]).decide(
                &invite,
                roles::GOVERNANCE_KIND,
                &Context::at_secs(1),
            );
            assert_eq!(verdict.answer, Answer::Deny, "never administers a workspace");
            assert_eq!(
                answer(&Acting::person("owner"), &policy, &[], ActionKind::Undoable),
                Answer::Allow
            );
        }

        phase an_app_reaches_only_the_kinds_it_declares_in_its_own_node;
        {
            let policy = policy();
            let acting = Acting::person("editor").in_app("so.tilo.notes");
            let kinds = ["notes.note".to_owned()];
            let answer = |node: TreePath| {
                Authority::of_acting(&acting, &policy, &[])
                    .in_app(&kinds)
                    .decide(&node, ActionKind::Undoable, &Context::at_secs(1))
                    .answer
            };
            let own = TreePath::workspace(WORKSPACE).app("so.tilo.notes");
            assert_eq!(answer(own.clone().kind("notes.note")), Answer::Allow);
            assert_eq!(
                answer(own.kind("notes.book")),
                Answer::Deny,
                "a kind the app does not declare is out of its reach"
            );
            assert_eq!(
                answer(
                    TreePath::workspace(WORKSPACE)
                        .app("so.tilo.chat")
                        .kind("chat.messages")
                ),
                Answer::Allow,
                "in another app's node the person's own grants decide"
            );
        }

        phase an_apps_own_connection_holds_the_grants_naming_it;
        {
            // An app's held connection acts as no person: it holds the grants that name the
            // app, and an empty person's grants are nothing.
            let app = "so.tilo.calendar";
            let node = TreePath::workspace(WORKSPACE).app("tilo.accounts");
            let granted = [Grant::new(
                "manifest",
                Grantee::app(app),
                node.clone(),
                crate::KindSet::READ,
                Answer::Allow,
                Grantee::person("owner"),
            )];
            let holding = Acting::by_app(app).in_app(app);
            assert_eq!(
                Authority::of_acting(&holding, &policy(), &granted)
                    .decide(&node, ActionKind::Read, &Context::at_secs(1))
                    .answer,
                Answer::Allow,
                "the grant naming the app decides for its own connection"
            );
            // Someone acting through the app for nobody is not the app: it holds nothing.
            let nobody = Acting::member(MemberId::person("")).in_app(app);
            assert_eq!(
                Authority::of_acting(&nobody, &policy(), &granted)
                    .decide(&node, ActionKind::Read, &Context::at_secs(1))
                    .answer,
                Answer::Deny,
                "nobody acting through the app borrows its grants"
            );
            assert_eq!(
                Authority::of_acting(&holding, &policy(), &[])
                    .decide(&node, ActionKind::Read, &Context::at_secs(1))
                    .answer,
                Answer::Deny,
                "and nothing reaches it that names no one"
            );
        }

        phase a_tree_asks_the_person_unless_someone_acts_for_them;
        {
            let person = Acting::person("editor");
            assert!(person.asker(true).reachable);
            assert!(!person.asker(true).delegated);
            let agent = Acting::agent("scribe", "editor").acting_for(Acting::person("editor"));
            assert!(agent.asker(true).delegated);
            assert!(
                !agent.asker(false).reachable,
                "with nobody to ask, an ask refuses"
            );
        }
    }

    tilo_test_env::scenario! {
        fn a_persons_answer_in_advance_lifts_the_ask_of_what_their_agent_sends;

        phase an_agents_send_asks_its_person;
        {
            let policy = policy();
            let turn = [
                Grant::new(
                    "turn:scribe:0",
                    Grantee::agent("scribe", "editor"),
                    TreePath::workspace(WORKSPACE),
                    KindSet::READ_UNDOABLE,
                    Answer::Allow,
                    Grantee::person("editor"),
                ),
                Grant::new(
                    "turn:scribe:1",
                    Grantee::agent("scribe", "editor"),
                    TreePath::workspace(WORKSPACE),
                    KindSet::OUTSIDE,
                    Answer::Ask,
                    Grantee::person("editor"),
                ),
            ];
            let acting = Acting::agent("scribe", "editor").acting_for(Acting::person("editor"));
            let send = TreePath::workspace(WORKSPACE)
                .app("so.tilo.notes")
                .kind("note")
                .resource("n1")
                .action("send");
            let asked = |turn: &[Grant]| {
                admit_invocation(
                    &Authority::of_acting(&acting, &policy, turn),
                    Asker {
                        reachable: true,
                        delegated: true,
                    },
                    Invocation {
                        node: &send,
                        kind: ActionKind::Outside,
                        privileged: false,
                        attended: false,
                        resource: None,
                        ceiling: None,
                    },
                    &Context::at_secs(1),
                )
                .expect("it is admitted, or asks")
                .0
            };
            assert_eq!(
                asked(&turn),
                Admission::AsksFirst,
                "an agent's send waits for its person"
            );

            // The person's own "Always allow this" for that very action lifts the ask.
            let scope = crate::agent_answers::AnswerScope::action("so.tilo.notes", "note", "send");
            let mut answered = turn.to_vec();
            answered.push(crate::agent_answers::grant(
                WORKSPACE,
                ("scribe", "editor"),
                (&scope, ActionKind::Outside),
                Answer::Allow,
                Grantee::person("editor"),
            ));
            assert_eq!(
                asked(&answered),
                Admission::Runs,
                "what its person answered in advance runs unasked"
            );

            // An answer for another action is no answer for this one.
            let elsewhere =
                crate::agent_answers::AnswerScope::action("so.tilo.notes", "note", "archive");
            let mut other = turn.to_vec();
            other.push(crate::agent_answers::grant(
                WORKSPACE,
                ("scribe", "editor"),
                (&elsewhere, ActionKind::Outside),
                Answer::Allow,
                Grantee::person("editor"),
            ));
            assert_eq!(
                asked(&other),
                Admission::AsksFirst,
                "only an answer for that very action lifts its ask"
            );
        }

        phase an_attended_action_its_person_answered_in_advance_runs_unasked_too;
        {
            let policy = policy();
            let mut turn = vec![Grant::new(
                "turn:scribe:0",
                Grantee::agent("scribe", "editor"),
                TreePath::workspace(WORKSPACE),
                KindSet::READ_UNDOABLE,
                Answer::Allow,
                Grantee::person("editor"),
            )];
            let acting = Acting::agent("scribe", "editor").acting_for(Acting::person("editor"));
            let act = TreePath::workspace(WORKSPACE)
                .app("so.tilo.notes")
                .kind("note")
                .resource("n1")
                .action("edit");
            let admission = |turn: &[Grant]| {
                admit_invocation(
                    &Authority::of_acting(&acting, &policy, turn),
                    Asker {
                        reachable: true,
                        delegated: true,
                    },
                    Invocation {
                        node: &act,
                        kind: ActionKind::Undoable,
                        privileged: false,
                        attended: true,
                        resource: None,
                        ceiling: None,
                    },
                    &Context::at_secs(1),
                )
                .expect("it is admitted, or asks")
                .0
            };
            assert_eq!(admission(&turn), Admission::AsksFirst);
            let scope = crate::agent_answers::AnswerScope::action("so.tilo.notes", "note", "edit");
            turn.push(crate::agent_answers::grant(
                WORKSPACE,
                ("scribe", "editor"),
                (&scope, ActionKind::Undoable),
                Answer::Allow,
                Grantee::person("editor"),
            ));
            assert_eq!(
                admission(&turn),
                Admission::Runs,
                "an attended action its person answered in advance runs unasked"
            );
        }
    }
}