Skip to main content

aidaemon_core/
nested.rs

1//! Calls a running tool makes on its own behalf, judged by the run's
2//! safeguards before it makes them.
3//!
4//! A tool that drives another program (a coding agent the run delegated
5//! to) can be asked by that program for permission to act: run a command,
6//! edit a file, reach the network. The tool must not answer on its own, and
7//! must never ask the owner itself. It hands the action to the run as a
8//! call of one of the run's own tools ([`NestedCalls::authorize`]), and the
9//! run judges it exactly as it judges the model's calls: an effect the
10//! owner already declined is refused first, then every hook decides, and a
11//! hook that needs an answer is settled by the run's
12//! [`crate::InterruptResolver`] while the tool waits. An owner's decline
13//! binds the rest of the run, and every run that shares its declines.
14//!
15//! The call is only judged here; it is never executed by the run. Whatever
16//! the program then does is the tool's own work, reported in its result.
17
18use std::fmt;
19
20use tokio::sync::{mpsc, oneshot};
21
22use crate::message::ToolCall;
23
24/// The run's answer about a nested call.
25#[derive(Debug, Clone, PartialEq, Eq)]
26#[non_exhaustive]
27pub enum NestedVerdict {
28    /// Every safeguard allowed it, or the owner approved it.
29    Allow,
30    /// A safeguard refused it.
31    Deny { reason: String },
32    /// The owner declined it (the reason starts with
33    /// [`crate::OWNER_DECLINED`]); remembered for the rest of the run.
34    Declined { reason: String },
35    /// It needed the owner, and no answer came (or nobody can be asked in
36    /// this run). Nothing is remembered as declined.
37    Unanswered { reason: String },
38}
39
40impl NestedVerdict {
41    pub fn is_allowed(&self) -> bool {
42        matches!(self, Self::Allow)
43    }
44
45    /// Why it was not allowed; `None` when it was.
46    pub fn reason(&self) -> Option<&str> {
47        match self {
48            Self::Allow => None,
49            Self::Deny { reason } | Self::Declined { reason } | Self::Unanswered { reason } => {
50                Some(reason)
51            }
52        }
53    }
54}
55
56/// One call waiting to be judged.
57pub(crate) struct NestedAsk {
58    pub call: ToolCall,
59    pub reply: oneshot::Sender<NestedVerdict>,
60}
61
62/// The way a running tool has a call judged by its run (see the module
63/// docs). Present in [`crate::ToolContext::nested`] while the run drives
64/// the tool's call.
65#[derive(Clone)]
66pub struct NestedCalls {
67    tx: mpsc::UnboundedSender<NestedAsk>,
68}
69
70/// The reason a call gets when its run is no longer there to judge it.
71const RUN_GONE: &str = "the run ended before this call could be checked";
72
73impl NestedCalls {
74    pub(crate) fn new(tx: mpsc::UnboundedSender<NestedAsk>) -> Self {
75        Self { tx }
76    }
77
78    /// Judge `call` (a call of one of the run's own tools, describing what
79    /// the driven program asks to do) and wait for the answer, which may
80    /// be the owner's.
81    pub async fn authorize(&self, call: ToolCall) -> NestedVerdict {
82        let (reply, answer) = oneshot::channel();
83        if self.tx.send(NestedAsk { call, reply }).is_err() {
84            return NestedVerdict::Deny {
85                reason: RUN_GONE.to_string(),
86            };
87        }
88        answer.await.unwrap_or_else(|_| NestedVerdict::Deny {
89            reason: RUN_GONE.to_string(),
90        })
91    }
92}
93
94impl fmt::Debug for NestedCalls {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        f.debug_struct("NestedCalls").finish_non_exhaustive()
97    }
98}