Skip to main content

adk_kernel/agents/
caller.rs

1//! Who asks the agent table for a change, and from where (spec 3.3 §5.7, §7.2, S-I18).
2
3use adk_core::principal::{SubjectKind, SubjectRef};
4use serde::{Deserialize, Serialize};
5
6use crate::control::ControlTransport;
7
8/// How sure the kernel is that the caller is the person at a screen (spec 3.3 §7.2).
9///
10/// # Example
11///
12/// ```rust
13/// use adk_kernel::agents::Presence;
14///
15/// assert!(!Presence::None.is_present());
16/// assert!(Presence::PairedLocal.is_present() && Presence::Confirmed.is_present());
17/// assert_eq!(serde_json::to_value(Presence::PairedLocal).unwrap(), "pairedLocal");
18/// ```
19#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21pub enum Presence {
22    /// Nothing shows the person is there.
23    #[default]
24    None,
25    /// The kernel's own terminal, over its socket or pipe (decision 22).
26    PairedLocal,
27    /// The person confirmed it is them ("Confirm it's you").
28    Confirmed,
29}
30
31impl Presence {
32    /// Whether this is enough for a high-risk change.
33    pub fn is_present(self) -> bool {
34        !matches!(self, Self::None)
35    }
36
37    /// The presence's name.
38    pub fn as_str(self) -> &'static str {
39        match self {
40            Self::None => "none",
41            Self::PairedLocal => "pairedLocal",
42            Self::Confirmed => "confirmed",
43        }
44    }
45}
46
47/// The caller of the agent table (spec 3.3 §5.7, S-I18): who, from which surface and how,
48/// through which device and transport, with what presence, and the calling process where the
49/// operating system reports it. Every `kernel.agent.*` record carries these.
50///
51/// The table refuses a caller that is not a `Human`, refuses a device, and refuses high
52/// risk or money without presence (S-I11).
53///
54/// # Example
55///
56/// ```rust
57/// use adk_core::principal::SubjectRef;
58/// use adk_kernel::agents::{AgentCaller, Presence};
59/// use adk_kernel::control::ControlTransport;
60///
61/// let caller = AgentCaller::new(SubjectRef::human("alice"), "terminal", "keyboard")
62///     .with_presence(Presence::PairedLocal)
63///     .with_transport(ControlTransport::Socket)
64///     .with_peer_pid(4242);
65/// assert!(caller.refusal().is_none());
66/// let agent = AgentCaller::new(SubjectRef::service("scheduler"), "web", "pointer");
67/// assert!(agent.refusal().is_some());
68/// ```
69#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(rename_all = "camelCase")]
71#[non_exhaustive]
72pub struct AgentCaller {
73    /// Who.
74    pub by: SubjectRef,
75    /// The surface: `web`, `terminal`, `phone`, …
76    pub surface: String,
77    /// How: `keyboard`, `pointer`, `touch`, …
78    pub how: String,
79    /// The paired device the request came through, if any.
80    #[serde(default, skip_serializing_if = "Option::is_none")]
81    pub device: Option<String>,
82    /// How sure the kernel is the person is there.
83    #[serde(default)]
84    pub presence: Presence,
85    /// The control transport the request came on, when it came through the control plane.
86    #[serde(default, skip_serializing_if = "Option::is_none")]
87    pub transport: Option<ControlTransport>,
88    /// The calling process, where the operating system reports it (decision 22).
89    #[serde(default, skip_serializing_if = "Option::is_none")]
90    pub peer_pid: Option<u32>,
91}
92
93impl AgentCaller {
94    /// `by` on `surface`, `how`, without a device, presence, transport or process.
95    pub fn new(by: SubjectRef, surface: impl Into<String>, how: impl Into<String>) -> Self {
96        Self {
97            by,
98            surface: surface.into(),
99            how: how.into(),
100            device: None,
101            presence: Presence::None,
102            transport: None,
103            peer_pid: None,
104        }
105    }
106
107    /// Sets the device.
108    #[must_use]
109    pub fn with_device(mut self, device: impl Into<String>) -> Self {
110        self.device = Some(device.into());
111        self
112    }
113
114    /// Sets the presence.
115    #[must_use]
116    pub fn with_presence(mut self, presence: Presence) -> Self {
117        self.presence = presence;
118        self
119    }
120
121    /// Sets the transport.
122    #[must_use]
123    pub fn with_transport(mut self, transport: ControlTransport) -> Self {
124        self.transport = Some(transport);
125        self
126    }
127
128    /// Sets the calling process.
129    #[must_use]
130    pub fn with_peer_pid(mut self, pid: u32) -> Self {
131        self.peer_pid = Some(pid);
132        self
133    }
134
135    /// Why the table refuses this caller any change, whatever its risk: it is not a person
136    /// (S-I11), or it came through a paired device, which only answers suggestions (decision
137    /// 16). `None` when it may change agents.
138    pub fn refusal(&self) -> Option<String> {
139        if self.by.kind != SubjectKind::Human {
140            return Some(format!(
141                "only a person adds, changes, pauses or removes an agent, and {} is not one \
142                 (spec 3.3 S-I11)",
143                self.by.id
144            ));
145        }
146        if self.device.is_some() {
147            return Some(
148                "a paired device answers suggested agents only; set up and change agents in \
149                 the web home or the terminal (spec 3.3 decision 16)"
150                    .to_string(),
151            );
152        }
153        None
154    }
155}