Skip to main content

trusty_code/session/
registry_permission_events.rs

1//! `SessionRegistry`'s permission-prompt event plumbing and its
2//! `PermissionEvents` implementation. Split out of `registry_events.rs`
3//! (#8100) purely to keep that production file under the crate's 500-SLOC
4//! cap — a child module of `registry` exactly as `events` is, so it shares
5//! the same access to `SessionRegistry`'s private `record` helper.
6//!
7//! Why: `crate::permissions` publishes through a trait so it never depends on
8//! the session layer; this is the session-side half of that seam.
9//! What: `record_permission_requested`/`record_permission_resolved`, and the
10//! `PermissionEvents` impl that forwards to them and answers
11//! `prompter_attached`.
12//! Test: `registry_tests::record_permission_requested_publishes_event`,
13//! `registry_tests::record_permission_resolved_publishes_event`,
14//! `registry_tests::prompter_attached_while_a_session_attachment_lives`.
15
16use super::*;
17
18impl SessionRegistry {
19    /// Record a tool call suspended on an `ask` permission rule (#7948).
20    ///
21    /// Why: `crate::permissions` emits through its `PermissionEvents` trait so
22    /// it never depends on the session layer; this is the implementation side.
23    /// What: `subject` arrives already redacted and bounded by the gate and is
24    /// recorded as-is.
25    /// Test: `registry_tests::record_permission_requested_publishes_event`.
26    #[allow(clippy::too_many_arguments)]
27    pub fn record_permission_requested(
28        &self,
29        id: &str,
30        request_id: &str,
31        agent: &str,
32        agent_id: &str,
33        tool: &str,
34        subject: &str,
35        rule: &str,
36    ) -> Result<(), RpcError> {
37        self.ensure_exists(id)?;
38        self.record(
39            id,
40            Event::PermissionRequested {
41                session_id: id.to_string(),
42                request_id: request_id.to_string(),
43                agent: agent.to_string(),
44                agent_id: agent_id.to_string(),
45                tool: tool.to_string(),
46                subject: subject.to_string(),
47                rule: rule.to_string(),
48            },
49        );
50        Ok(())
51    }
52
53    /// Record a suspended permission request reaching a decision (#7948).
54    /// Test: `registry_tests::record_permission_resolved_publishes_event`.
55    pub fn record_permission_resolved(
56        &self,
57        id: &str,
58        request_id: &str,
59        agent: &str,
60        agent_id: &str,
61        decision: &str,
62        source: &str,
63    ) -> Result<(), RpcError> {
64        self.ensure_exists(id)?;
65        self.record(
66            id,
67            Event::PermissionResolved {
68                session_id: id.to_string(),
69                request_id: request_id.to_string(),
70                agent: agent.to_string(),
71                agent_id: agent_id.to_string(),
72                decision: decision.to_string(),
73                source: source.to_string(),
74            },
75        );
76        Ok(())
77    }
78}
79
80/// (#7948) The registry is the permission gate's event sink.
81///
82/// Why: `SessionRegistry::record` is the sole assigner of an event's `seq`;
83/// implementing the trait here keeps `crate::permissions` free of a registry
84/// dependency.
85/// What: a `session_not_found` is logged at `debug` and dropped. It affects
86/// only the event, never the decision: the gate still waits for an answer or
87/// the timeout, and a timeout denies.
88/// Test: `registry_tests::record_permission_requested_publishes_event`,
89/// `registry_tests::record_permission_resolved_publishes_event`.
90impl crate::permissions::PermissionEvents for SessionRegistry {
91    fn permission_requested(
92        &self,
93        session_id: &str,
94        request_id: &str,
95        agent: &str,
96        agent_id: &str,
97        tool: &str,
98        subject: &str,
99        rule: &str,
100    ) {
101        if let Err(e) = self.record_permission_requested(
102            session_id, request_id, agent, agent_id, tool, subject, rule,
103        ) {
104            tracing::debug!(session_id = %session_id, "record_permission_requested skipped: {e}");
105        }
106    }
107
108    fn permission_resolved(
109        &self,
110        session_id: &str,
111        request_id: &str,
112        agent: &str,
113        agent_id: &str,
114        decision: &str,
115        source: &str,
116    ) {
117        if let Err(e) = self
118            .record_permission_resolved(session_id, request_id, agent, agent_id, decision, source)
119        {
120            tracing::debug!(session_id = %session_id, "record_permission_resolved skipped: {e}");
121        }
122    }
123
124    /// Whether a prompter is watching THIS session and could answer its ask.
125    ///
126    /// Why (#8100): the signal must be per session. A daemon-global reading —
127    /// `crate::events::bus().receiver_count()`, this method's first cut — is
128    /// true for every session in the process as soon as one console attaches
129    /// to one of them, so the normal trusty-mpm shape (a watched PM session
130    /// dispatching headless `task.run` sub-agents) put every sub-agent back on
131    /// the 300 s stall. `SessionRegistry::claim_prompter` counts only the
132    /// paths bound to one session id that can BOTH see its
133    /// `permission_requested` event and answer with
134    /// `session.permission.respond`: `session.attach`, the `session.events`
135    /// stream, and the per-session HTTP SSE route. The workstream-aggregate
136    /// SSE route is an observer, not a prompter — it is bound to a workstream
137    /// whose membership is re-checked per event, so no session's prompt is
138    /// its to display — and deliberately claims nothing.
139    /// What: `prompter_count` reads `0` for a session the registry cannot
140    /// find, which denies. That is the fail-safe arm: a prompt for a session
141    /// that does not exist is a prompt nobody can be shown.
142    /// Test: `registry_tests::prompter_attached_while_a_session_attachment_lives`,
143    /// `registry_tests::prompter_claim_is_released_when_its_guard_drops`,
144    /// `registry_tests::prompter_attached_for_an_unknown_session_is_false`,
145    /// `registry_tests::prompter_count_survives_a_poisoned_registry_lock`,
146    /// `gate_tests::an_ask_on_an_unwatched_session_is_headless_while_a_sibling_is_watched`.
147    fn prompter_attached(&self, session_id: &str) -> bool {
148        self.prompter_count(session_id) > 0
149    }
150}