Skip to main content

tilo_action_policy/
acting.rs

1//! Who made a change: one tree, and the one admission every action API takes.
2
3use crate::{
4    Actor, Admission, Answer, Asker, Authority, Context, Grant, GrantId, Grantee, Invoked,
5    MemberId, Party, Reach, Refused, TreePath, WorkspacePolicy, admit,
6};
7
8/// [`crate::Acting::asker`] for an acting with software or a device between
9/// the person and the change when `delegated`.
10#[must_use]
11pub const fn asker_of(delegated: bool, host_reaches_person: bool) -> Asker {
12    Asker {
13        reachable: !delegated || host_reaches_person,
14        delegated,
15    }
16}
17
18impl crate::Acting {
19    /// Who an invocation of this acting asks: the person, who can answer when
20    /// they act themselves or when the host running it reaches them
21    /// (`host_reaches_person`); and whether someone acts for them, who is
22    /// asked before an action the person attends.
23    #[must_use]
24    pub fn asker(&self, host_reaches_person: bool) -> Asker {
25        asker_of(self.delegated(), host_reaches_person)
26    }
27
28    /// The preset every handed agent of the tree carries in `workspace`
29    /// ([`crate::Acting::handed`], [`crate::roles::agent_preset`]), for the
30    /// person it acts for: the one place a device's delegation to its agent
31    /// becomes grants, so the device that admitted the agent and every
32    /// receiver of its changes decide it alike.
33    #[must_use]
34    pub fn handed_preset(&self, workspace: &str) -> Vec<Grant> {
35        self.nodes()
36            .into_iter()
37            .filter(|node| node.handed)
38            .filter_map(|node| Some((node.member_id()?.as_agent()?, node.acted_for_person()?)))
39            .flat_map(|((agent, own), person)| {
40                crate::roles::agent_preset(workspace, (agent, own.as_str()), person)
41            })
42            .collect()
43    }
44}
45
46impl<'a> Authority<'a> {
47    /// The authority of `acting` under `policy`: an action is allowed only when
48    /// every node of its tree allows it.
49    ///
50    /// A person is decided by their grants and their role's; from a device that
51    /// is not theirs, by what such a device may do at most
52    /// ([`crate::Cap::FOREIGN_DEVICE`]). An agent or bot by the grants naming
53    /// it in the workspace and the ones its person gave it (`granted`). An
54    /// automation by what the people it runs for gave it.
55    #[must_use]
56    pub fn of_acting(
57        acting: &'a crate::Acting,
58        policy: &'a WorkspacePolicy,
59        granted: &'a [Grant],
60    ) -> Self {
61        let mut people: usize = 0;
62        let mut person_parts = Vec::new();
63        let mut others = Vec::new();
64        // An automation acts with its owner's party: the tree names it for the
65        // person it runs for, and its steps are that person's grants. One that
66        // runs for nobody allows nothing.
67        let owned = !acting.acted_for.is_empty();
68        // The answers a person gave the agent session that caused the acting decide for the
69        // whole tree, as the agent's own do.
70        let answers: Vec<&Grant> = match &acting.cause {
71            Some(cause) => crate::session_answers(&policy.grants, cause.as_str()).collect(),
72            None => Vec::new(),
73        };
74        for node in acting.nodes() {
75            match &node.by {
76                Actor::Member(member) => match member {
77                    MemberId::Person(person) => {
78                        people = people.saturating_add(1);
79                        let party = policy
80                            .party(person.as_str())
81                            .with_grants(granted)
82                            .with_grants(answers.iter().copied());
83                        person_parts.push(if node.foreign_device {
84                            party.capped(crate::Cap::FOREIGN_DEVICE)
85                        } else {
86                            party
87                        });
88                    }
89                    MemberId::Agent { .. } | MemberId::Bot(_) => {
90                        others.push(
91                            software_party(member, policy, granted)
92                                .with_grants(answers.iter().copied()),
93                        );
94                    }
95                },
96                // A visitor holds what the workspace opens to any key, and nothing else.
97                Actor::Visitor(_) => others.push(policy.party_of(Grantee::AnyKey)),
98                Actor::Automation(_) if owned => {}
99                // An app acting by itself holds the grants naming it, never a person's.
100                Actor::Automation(_) => others.push(Party::default()),
101                // An app acting by itself, in the process its device launched as it, holds the
102                // grants naming it, with those its device hands it from its signed manifest.
103                Actor::App(app) => others.push(
104                    policy
105                        .party_of(Grantee::app(app.as_str()))
106                        .with_grants(granted),
107                ),
108            }
109        }
110        let mut parts = person_parts.into_iter().chain(others);
111        Self {
112            person: parts.next().unwrap_or_default(),
113            bounds: parts.collect(),
114            people,
115            app: acting.app.as_ref(),
116            app_kinds: None,
117        }
118    }
119}
120
121/// A member of `member` as a grant names it.
122#[must_use]
123pub fn granted_to(member: &MemberId) -> Grantee {
124    Grantee::Member(member.clone())
125}
126
127/// An agent's or bot's own grants: the workspace's naming it, and those its
128/// acting carries (`granted`, what its certificate's delegation gives it). An
129/// agent is the pair of its id and the person it acts for, whom its
130/// certificate chains to: only the grants and answers naming that pair reach
131/// it, and once its person answered it ([`WorkspacePolicy::answers_narrow`]) what
132/// `granted` hands it decides nothing.
133fn software_party<'a>(
134    member: &MemberId,
135    policy: &'a WorkspacePolicy,
136    granted: &'a [Grant],
137) -> Party<'a> {
138    match member {
139        // A person's answer for their agent narrows it to what their answers name, whatever its
140        // delegation hands it.
141        MemberId::Agent { id, person } => policy.party_of_agent(id, person.as_str()).narrowed_by(
142            granted,
143            policy
144                .grants_of_agent(id, person.as_str())
145                .filter(|grant| crate::agent_answers::is_answer(grant)),
146        ),
147        member => policy.party_of(granted_to(member)).with_grants(granted),
148    }
149}
150
151/// One invocation, as admission weighs it.
152#[derive(Clone, Copy, Debug)]
153pub struct Invocation<'a> {
154    /// The action's node.
155    pub node: &'a TreePath,
156    /// The kind of action it is.
157    pub kind: crate::ActionKind,
158    /// Whether it exercises authority over the workspace or others in it
159    /// ([`crate::Verdict::privileged`]).
160    pub privileged: bool,
161    /// Whether it needs its person attending.
162    pub attended: bool,
163    /// The node of the resource it changes, decided as well where it is one:
164    /// so a person restricted on one resource cannot change it through an
165    /// action they may otherwise take.
166    pub resource: Option<&'a TreePath>,
167    /// How far the invocation it continues was admitted to reach, when it
168    /// continues one: what goes further asks first.
169    pub ceiling: Option<Reach>,
170}
171
172/// The grants' answer for an action of `kind`, privileged when
173/// `privileged`, at `node`, for `authority` at `cx`, and the `Once` grants
174/// relying on it spends. A step going further than `ceiling` (the
175/// invocation it continues) is asked for as its own request.
176#[must_use]
177pub fn answer_at(
178    authority: &Authority<'_>,
179    (node, kind, privileged): (&TreePath, crate::ActionKind, bool),
180    ceiling: Option<Reach>,
181    cx: &Context<'_>,
182) -> (Answer, Vec<GrantId>) {
183    let verdict = verdict_at(authority, (node, kind, privileged), ceiling, cx);
184    (verdict.answer, verdict.spent())
185}
186
187/// The verdict of `answer_at`, kept whole: what it was decided by, so admission can tell the
188/// answer a person gave in advance ([`Authority::answered_in_advance`]) from one asking them
189/// now.
190fn verdict_at<'a>(
191    authority: &'a Authority<'_>,
192    (node, kind, privileged): (&TreePath, crate::ActionKind, bool),
193    ceiling: Option<Reach>,
194    cx: &Context<'_>,
195) -> crate::Verdict<'a> {
196    let mut verdict = authority.decide(node, kind, cx);
197    if privileged {
198        verdict = verdict.privileged();
199    }
200    let exceeds = ceiling.is_some_and(|ceiling| !ceiling.covers(kind, privileged));
201    if verdict.answer == Answer::Allow && exceeds {
202        verdict.answer = Answer::Ask;
203    }
204    verdict
205}
206
207/// Admit `invocation` for `authority`, asked of `asker`, at `cx`: whether
208/// it runs or asks first, and the `Once` grants admitting it spends.
209///
210/// # Errors
211/// Whose answer refused it.
212pub fn admit_invocation(
213    authority: &Authority<'_>,
214    asker: Asker,
215    invocation: Invocation<'_>,
216    cx: &Context<'_>,
217) -> Result<(Admission, Vec<GrantId>), Refused> {
218    let verdict = verdict_at(
219        authority,
220        (invocation.node, invocation.kind, invocation.privileged),
221        invocation.ceiling,
222        cx,
223    );
224    // What its person already answered for this very action or resource was not theirs to ask
225    // again, though it reaches outside the system: asking would put to them what they answered.
226    let answered_in_advance = authority.answered_in_advance(&verdict);
227    let answer = verdict.answer;
228    let spent = verdict.spent();
229    let resource = invocation.resource.map(|resource| {
230        answer_at(
231            authority,
232            (resource, invocation.kind, false),
233            invocation.ceiling,
234            cx,
235        )
236        .0
237    });
238    let admission = admit(
239        Invoked {
240            answer,
241            attended: invocation.attended,
242            outside: invocation.kind == crate::ActionKind::Outside,
243            answered_in_advance,
244            resource,
245        },
246        asker,
247    )?;
248    Ok((admission, spent))
249}
250
251#[cfg(test)]
252mod tests {
253    use super::*;
254    use crate::{
255        Acting, ActionKind, Answer, Context, Grant, Grantee, KindSet, Role, SESSION_ANSWER_PREFIX,
256        Verdict, roles,
257    };
258
259    const WORKSPACE: &str = "w";
260
261    fn policy() -> WorkspacePolicy {
262        let mut policy = WorkspacePolicy::new(WORKSPACE, "owner");
263        policy.members.insert("editor".into(), Role::Editor);
264        policy.members.insert("viewer".into(), Role::Viewer);
265        policy
266    }
267
268    fn app() -> TreePath {
269        TreePath::workspace(WORKSPACE).app("so.tilo.notes")
270    }
271
272    fn answer(
273        acting: &Acting,
274        policy: &WorkspacePolicy,
275        granted: &[Grant],
276        kind: ActionKind,
277    ) -> Answer {
278        Authority::of_acting(acting, policy, granted)
279            .decide(&app(), kind, &Context::at_secs(1))
280            .answer
281    }
282
283    tilo_test_env::scenario! {
284        fn whoever_acts_is_decided_by_every_party_it_acts_for;
285
286        phase a_person_is_decided_by_their_grants_and_their_roles;
287        {
288            let policy = policy();
289            assert_eq!(
290                answer(
291                    &Acting::person("editor"),
292                    &policy,
293                    &[],
294                    ActionKind::Undoable
295                ),
296                Answer::Allow
297            );
298            assert_eq!(
299                answer(
300                    &Acting::person("viewer"),
301                    &policy,
302                    &[],
303                    ActionKind::Undoable
304                ),
305                Answer::Deny
306            );
307        }
308
309        phase every_person_in_the_tree_has_to_allow_it;
310        {
311            let policy = policy();
312            // What the agent's turn gives it: reading and changing the workspace.
313            let turn = [Grant::new(
314                "turn:scribe:0",
315                Grantee::agent("scribe", "editor"),
316                TreePath::workspace(WORKSPACE),
317                KindSet::READ_UNDOABLE,
318                Answer::Allow,
319                Grantee::person("editor"),
320            )];
321            let agent_for_editor_and_owner = Acting::agent("scribe", "editor")
322                .acting_for(Acting::person("editor"))
323                .acting_for(Acting::person("owner"));
324            assert_eq!(
325                answer(
326                    &agent_for_editor_and_owner,
327                    &policy,
328                    &turn,
329                    ActionKind::Undoable
330                ),
331                Answer::Allow
332            );
333            let agent_for_a_viewer = agent_for_editor_and_owner.acting_for(Acting::person("viewer"));
334            assert_eq!(
335                answer(&agent_for_a_viewer, &policy, &turn, ActionKind::Undoable),
336                Answer::Deny,
337                "the viewer's grants decide too"
338            );
339        }
340
341        phase an_app_acting_by_itself_is_decided_by_the_grants_naming_it;
342        {
343            let app = Acting::by_app("so.tilo.notes");
344            assert_eq!(
345                answer(&app, &policy(), &[], ActionKind::Undoable),
346                Answer::Deny,
347                "an app holds no person's grants, the owner's included"
348            );
349            // Nor does the owner's device lend it theirs by signing what it wrote.
350            let own = TreePath::workspace(WORKSPACE).app("so.tilo.notes");
351            let signed = crate::MemberAccess::person(policy(), "owner")
352                .invoked_by(&app)
353                .expect("the owner's device writes for an app it runs");
354            assert_eq!(
355                signed.answer(&own, ActionKind::Undoable, &Context::at_secs(1)),
356                Answer::Deny,
357                "the device that signs an app's write bounds it, and grants it nothing"
358            );
359            // Installing Notes gave it its own records, and nothing beyond them.
360            let mut policy = policy();
361            policy.grants.push(crate::roles::app_records_grant(
362                WORKSPACE,
363                "so.tilo.notes",
364                "owner",
365            ));
366            assert_eq!(
367                answer(&app, &policy, &[], ActionKind::Undoable),
368                Answer::Allow
369            );
370            let elsewhere = TreePath::workspace(WORKSPACE)
371                .app("so.tilo.chat")
372                .kind("chat.messages");
373            assert_eq!(
374                Authority::of_acting(&app, &policy, &[])
375                    .decide(&elsewhere, ActionKind::Undoable, &Context::at_secs(1))
376                    .answer,
377                Answer::Deny,
378                "another app's records are no part of what installing gave"
379            );
380        }
381
382        phase an_agent_needs_its_own_grants_as_well_as_its_persons;
383        {
384            let mut policy = policy();
385            policy.grants.push(Grant::new(
386                "scribe-may-edit",
387                Grantee::agent("scribe", "editor"),
388                TreePath::workspace(WORKSPACE),
389                KindSet::READ_UNDOABLE,
390                Answer::Allow,
391                Grantee::person("editor"),
392            ));
393            let acting = Acting::agent("scribe", "editor").acting_for(Acting::person("editor"));
394            assert_eq!(
395                answer(&acting, &policy, &[], ActionKind::Undoable),
396                Answer::Allow
397            );
398            let unbounded = Acting::agent("other", "editor").acting_for(Acting::person("editor"));
399            assert_eq!(
400                answer(&unbounded, &policy, &[], ActionKind::Undoable),
401                Answer::Deny,
402                "an agent with no grants of its own does nothing"
403            );
404        }
405
406        phase an_agent_answering_someone_else_keeps_its_own_persons_refusal;
407        {
408            // The agent's certificate gives it the workspace; its own person, the owner, answers
409            // no for it on the whole workspace. Its turn answers the editor, through the owner.
410            let turn = [Grant::new(
411                "turn:scribe:0",
412                Grantee::agent("scribe", "owner"),
413                TreePath::workspace(WORKSPACE),
414                KindSet::READ_UNDOABLE,
415                Answer::Allow,
416                Grantee::person("owner"),
417            )];
418            let answering_the_editor = Acting::agent("scribe", "owner")
419                .acting_for(Acting::person("editor"))
420                .agent_of("owner");
421            assert_eq!(
422                answer(&answering_the_editor, &policy(), &turn, ActionKind::Undoable),
423                Answer::Allow,
424                "both people and the agent's certificate allow it"
425            );
426            let mut refusing = policy();
427            refusing.grants.push(crate::agent_answers::grant(
428                WORKSPACE,
429                ("scribe", "owner"),
430                (&crate::agent_answers::AnswerScope::Workspace, ActionKind::Undoable),
431                Answer::Deny,
432                Grantee::person("owner"),
433            ));
434            assert_eq!(
435                answer(&answering_the_editor, &refusing, &turn, ActionKind::Undoable),
436                Answer::Deny,
437                "what its own person answered for it refuses it, whoever's turn it answers"
438            );
439            let mut widening = refusing.clone();
440            // The editor answers only for an agent of their own: `scribe` of the editor is
441            // another agent.
442            widening.grants.push(crate::agent_answers::grant(
443                WORKSPACE,
444                ("scribe", "editor"),
445                (
446                    &crate::agent_answers::AnswerScope::app("so.tilo.notes"),
447                    ActionKind::Undoable,
448                ),
449                Answer::Allow,
450                Grantee::person("editor"),
451            ));
452            assert_eq!(
453                answer(&answering_the_editor, &widening, &turn, ActionKind::Undoable),
454                Answer::Deny,
455                "the more specific yes of the person a turn answers widens nothing the agent's own \
456                 person refused"
457            );
458        }
459
460        phase a_sessions_answers_decide_its_steps_and_no_other;
461        {
462            let session = "so.tilo.session/agent-session/release";
463            let mut policy = policy();
464            policy.grants.push(Grant::new(
465                "scribe-asks",
466                Grantee::agent("scribe", "editor"),
467                TreePath::workspace(WORKSPACE),
468                KindSet::READ_UNDOABLE,
469                Answer::Ask,
470                Grantee::person("editor"),
471            ));
472            policy.grants.push(Grant::new(
473                format!("{SESSION_ANSWER_PREFIX}{session}:app:so.tilo.notes:undoable"),
474                Grantee::agent("scribe", "editor"),
475                app(),
476                KindSet::UNDOABLE,
477                Answer::Allow,
478                Grantee::person("editor"),
479            ));
480            let run = Acting::agent("scribe", "editor")
481                .acting_for(Acting::person("editor"))
482                .caused_by(session);
483            assert_eq!(
484                answer(&run, &policy, &[], ActionKind::Undoable),
485                Answer::Allow,
486                "the person's answer for the session decides its steps"
487            );
488            let another = Acting::agent("scribe", "editor")
489                .acting_for(Acting::person("editor"))
490                .caused_by("so.tilo.session/agent-session/other");
491            assert_eq!(
492                answer(&another, &policy, &[], ActionKind::Undoable),
493                Answer::Ask,
494                "another session's answers decide nothing here"
495            );
496        }
497
498        phase an_automation_acts_with_its_owners_party_bounded_by_its_steps;
499        {
500            let policy = policy();
501            let automation = Acting::automation("1")
502                .acting_for(Acting::agent("scribe", "editor").acting_for(Acting::person("editor")));
503            let turn = [Grant::new(
504                "turn:scribe:0",
505                Grantee::agent("scribe", "editor"),
506                TreePath::workspace(WORKSPACE),
507                KindSet::READ_UNDOABLE,
508                Answer::Allow,
509                Grantee::person("editor"),
510            )];
511            assert_eq!(
512                answer(&automation, &policy, &turn, ActionKind::Undoable),
513                Answer::Allow,
514                "the tree's people and software decide; the automation is its owner's"
515            );
516            // A step it was given may narrow what it does there.
517            let mut granted = turn.to_vec();
518            granted.push(Grant::new(
519                "automation-1:outside",
520                Grantee::person("editor"),
521                TreePath::workspace(WORKSPACE),
522                KindSet::OUTSIDE,
523                Answer::Deny,
524                Grantee::person("editor"),
525            ));
526            assert_eq!(
527                answer(&automation, &policy, &granted, ActionKind::Outside),
528                Answer::Deny
529            );
530            let homeless = Acting::automation("1");
531            assert_eq!(
532                answer(&homeless, &policy, &turn, ActionKind::Undoable),
533                Answer::Deny,
534                "an automation that runs for nobody allows nothing"
535            );
536        }
537
538        phase a_device_that_is_not_theirs_governs_nothing_and_asks_before_reaching_out;
539        {
540            let policy = policy();
541            let foreign = Acting::person("owner").on_foreign_device(true);
542            assert_eq!(
543                answer(&foreign, &policy, &[], ActionKind::Outside),
544                Answer::Ask
545            );
546            let invite = roles::governance_action(WORKSPACE, roles::governance::ADD_MEMBER);
547            let verdict: Verdict<'_> = Authority::of_acting(&foreign, &policy, &[]).decide(
548                &invite,
549                roles::GOVERNANCE_KIND,
550                &Context::at_secs(1),
551            );
552            assert_eq!(verdict.answer, Answer::Deny, "never administers a workspace");
553            assert_eq!(
554                answer(&Acting::person("owner"), &policy, &[], ActionKind::Undoable),
555                Answer::Allow
556            );
557        }
558
559        phase an_app_reaches_only_the_kinds_it_declares_in_its_own_node;
560        {
561            let policy = policy();
562            let acting = Acting::person("editor").in_app("so.tilo.notes");
563            let kinds = ["notes.note".to_owned()];
564            let answer = |node: TreePath| {
565                Authority::of_acting(&acting, &policy, &[])
566                    .in_app(&kinds)
567                    .decide(&node, ActionKind::Undoable, &Context::at_secs(1))
568                    .answer
569            };
570            let own = TreePath::workspace(WORKSPACE).app("so.tilo.notes");
571            assert_eq!(answer(own.clone().kind("notes.note")), Answer::Allow);
572            assert_eq!(
573                answer(own.kind("notes.book")),
574                Answer::Deny,
575                "a kind the app does not declare is out of its reach"
576            );
577            assert_eq!(
578                answer(
579                    TreePath::workspace(WORKSPACE)
580                        .app("so.tilo.chat")
581                        .kind("chat.messages")
582                ),
583                Answer::Allow,
584                "in another app's node the person's own grants decide"
585            );
586        }
587
588        phase an_apps_own_connection_holds_the_grants_naming_it;
589        {
590            // An app's held connection acts as no person: it holds the grants that name the
591            // app, and an empty person's grants are nothing.
592            let app = "so.tilo.calendar";
593            let node = TreePath::workspace(WORKSPACE).app("tilo.accounts");
594            let granted = [Grant::new(
595                "manifest",
596                Grantee::app(app),
597                node.clone(),
598                crate::KindSet::READ,
599                Answer::Allow,
600                Grantee::person("owner"),
601            )];
602            let holding = Acting::by_app(app).in_app(app);
603            assert_eq!(
604                Authority::of_acting(&holding, &policy(), &granted)
605                    .decide(&node, ActionKind::Read, &Context::at_secs(1))
606                    .answer,
607                Answer::Allow,
608                "the grant naming the app decides for its own connection"
609            );
610            // Someone acting through the app for nobody is not the app: it holds nothing.
611            let nobody = Acting::member(MemberId::person("")).in_app(app);
612            assert_eq!(
613                Authority::of_acting(&nobody, &policy(), &granted)
614                    .decide(&node, ActionKind::Read, &Context::at_secs(1))
615                    .answer,
616                Answer::Deny,
617                "nobody acting through the app borrows its grants"
618            );
619            assert_eq!(
620                Authority::of_acting(&holding, &policy(), &[])
621                    .decide(&node, ActionKind::Read, &Context::at_secs(1))
622                    .answer,
623                Answer::Deny,
624                "and nothing reaches it that names no one"
625            );
626        }
627
628        phase a_tree_asks_the_person_unless_someone_acts_for_them;
629        {
630            let person = Acting::person("editor");
631            assert!(person.asker(true).reachable);
632            assert!(!person.asker(true).delegated);
633            let agent = Acting::agent("scribe", "editor").acting_for(Acting::person("editor"));
634            assert!(agent.asker(true).delegated);
635            assert!(
636                !agent.asker(false).reachable,
637                "with nobody to ask, an ask refuses"
638            );
639        }
640    }
641
642    tilo_test_env::scenario! {
643        fn a_persons_answer_in_advance_lifts_the_ask_of_what_their_agent_sends;
644
645        phase an_agents_send_asks_its_person;
646        {
647            let policy = policy();
648            let turn = [
649                Grant::new(
650                    "turn:scribe:0",
651                    Grantee::agent("scribe", "editor"),
652                    TreePath::workspace(WORKSPACE),
653                    KindSet::READ_UNDOABLE,
654                    Answer::Allow,
655                    Grantee::person("editor"),
656                ),
657                Grant::new(
658                    "turn:scribe:1",
659                    Grantee::agent("scribe", "editor"),
660                    TreePath::workspace(WORKSPACE),
661                    KindSet::OUTSIDE,
662                    Answer::Ask,
663                    Grantee::person("editor"),
664                ),
665            ];
666            let acting = Acting::agent("scribe", "editor").acting_for(Acting::person("editor"));
667            let send = TreePath::workspace(WORKSPACE)
668                .app("so.tilo.notes")
669                .kind("note")
670                .resource("n1")
671                .action("send");
672            let asked = |turn: &[Grant]| {
673                admit_invocation(
674                    &Authority::of_acting(&acting, &policy, turn),
675                    Asker {
676                        reachable: true,
677                        delegated: true,
678                    },
679                    Invocation {
680                        node: &send,
681                        kind: ActionKind::Outside,
682                        privileged: false,
683                        attended: false,
684                        resource: None,
685                        ceiling: None,
686                    },
687                    &Context::at_secs(1),
688                )
689                .expect("it is admitted, or asks")
690                .0
691            };
692            assert_eq!(
693                asked(&turn),
694                Admission::AsksFirst,
695                "an agent's send waits for its person"
696            );
697
698            // The person's own "Always allow this" for that very action lifts the ask.
699            let scope = crate::agent_answers::AnswerScope::action("so.tilo.notes", "note", "send");
700            let mut answered = turn.to_vec();
701            answered.push(crate::agent_answers::grant(
702                WORKSPACE,
703                ("scribe", "editor"),
704                (&scope, ActionKind::Outside),
705                Answer::Allow,
706                Grantee::person("editor"),
707            ));
708            assert_eq!(
709                asked(&answered),
710                Admission::Runs,
711                "what its person answered in advance runs unasked"
712            );
713
714            // An answer for another action is no answer for this one.
715            let elsewhere =
716                crate::agent_answers::AnswerScope::action("so.tilo.notes", "note", "archive");
717            let mut other = turn.to_vec();
718            other.push(crate::agent_answers::grant(
719                WORKSPACE,
720                ("scribe", "editor"),
721                (&elsewhere, ActionKind::Outside),
722                Answer::Allow,
723                Grantee::person("editor"),
724            ));
725            assert_eq!(
726                asked(&other),
727                Admission::AsksFirst,
728                "only an answer for that very action lifts its ask"
729            );
730        }
731
732        phase an_attended_action_its_person_answered_in_advance_runs_unasked_too;
733        {
734            let policy = policy();
735            let mut turn = vec![Grant::new(
736                "turn:scribe:0",
737                Grantee::agent("scribe", "editor"),
738                TreePath::workspace(WORKSPACE),
739                KindSet::READ_UNDOABLE,
740                Answer::Allow,
741                Grantee::person("editor"),
742            )];
743            let acting = Acting::agent("scribe", "editor").acting_for(Acting::person("editor"));
744            let act = TreePath::workspace(WORKSPACE)
745                .app("so.tilo.notes")
746                .kind("note")
747                .resource("n1")
748                .action("edit");
749            let admission = |turn: &[Grant]| {
750                admit_invocation(
751                    &Authority::of_acting(&acting, &policy, turn),
752                    Asker {
753                        reachable: true,
754                        delegated: true,
755                    },
756                    Invocation {
757                        node: &act,
758                        kind: ActionKind::Undoable,
759                        privileged: false,
760                        attended: true,
761                        resource: None,
762                        ceiling: None,
763                    },
764                    &Context::at_secs(1),
765                )
766                .expect("it is admitted, or asks")
767                .0
768            };
769            assert_eq!(admission(&turn), Admission::AsksFirst);
770            let scope = crate::agent_answers::AnswerScope::action("so.tilo.notes", "note", "edit");
771            turn.push(crate::agent_answers::grant(
772                WORKSPACE,
773                ("scribe", "editor"),
774                (&scope, ActionKind::Undoable),
775                Answer::Allow,
776                Grantee::person("editor"),
777            ));
778            assert_eq!(
779                admission(&turn),
780                Admission::Runs,
781                "an attended action its person answered in advance runs unasked"
782            );
783        }
784    }
785}