Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(security): add approval gate for tool call interception

Steven Enamakelcc710eef6a45
diff --git a/crates/openhuman-core/src/security/approval/gate.rs b/crates/openhuman-core/src/security/approval/gate.rs
index 99a652e09f..e95ae2e7e0 100644
--- a/crates/openhuman-core/src/security/approval/gate.rs
+++ b/crates/openhuman-core/src/security/approval/gate.rs
@@ -277,6 +277,10 @@ pub(crate) struct RequestRoute {
/// sub-agent routed through the origin fallback (#5499). Carried so the
/// replay path re-emits the same `detached` flag the live event had.
pub(crate) detached: bool,
+ /// The store the request was parked in, captured under the parking task's
+ /// storage scope, so a decision made from outside that scope (an RPC with
+ /// no agent or profile of its own) reaches the row where it was written.
+ pub(super) docs: Option<super::store_documents::Docs>,
}
/// Coordinator for pending approvals.
diff --git a/crates/openhuman-core/src/security/approval/gate_intercept.rs b/crates/openhuman-core/src/security/approval/gate_intercept.rs
index 8ab9f1fabe..8e02226299 100644
--- a/crates/openhuman-core/src/security/approval/gate_intercept.rs
+++ b/crates/openhuman-core/src/security/approval/gate_intercept.rs
@@ -531,6 +531,7 @@ impl ApprovalGate {
approval_scope: scope.clone(),
thread_key: thread_key.clone(),
detached,
+ docs: store::capture_docs(&self.config).ok(),
},
);
store::insert_pending(&self.config, &pending, &self.session_id)?;
diff --git a/crates/openhuman-core/src/security/approval/gate_state.rs b/crates/openhuman-core/src/security/approval/gate_state.rs
index 2987561b1d..09cd94de43 100644
--- a/crates/openhuman-core/src/security/approval/gate_state.rs
+++ b/crates/openhuman-core/src/security/approval/gate_state.rs
@@ -68,7 +68,7 @@ impl ApprovalGate {
{
anyhow::bail!("this action requires a one-time approval or denial");
}
- let decided = store::decide(&self.config, request_id, decision)?;
+ let decided = self.decide_in_parked_scope(request_id, decision)?;
if let Some(row) = &decided {
// `ApproveAlwaysForTool` persistence (append to `autonomy.auto_approve`
// + reload the live policy) is handled by the `approval_decide` RPC
@@ -97,6 +97,26 @@ impl ApprovalGate {
Ok(decided)
}
+ /// Commits `decision` in the store the request was parked in when this
+ /// process parked it (the parking task's storage scope, remembered on its
+ /// route), else in the caller's scope — an orphan from an earlier launch has
+ /// no route, so the caller's scope is where it can be looked for.
+ pub(super) fn decide_in_parked_scope(
+ &self,
+ request_id: &str,
+ decision: ApprovalDecision,
+ ) -> anyhow::Result<Option<PendingApproval>> {
+ let parked_in = self
+ .request_routes
+ .lock()
+ .get(request_id)
+ .and_then(|route| route.docs.clone());
+ match parked_in {
+ Some(docs) => store::decide_in(&docs, request_id, decision),
+ None => store::decide(&self.config, request_id, decision),
+ }
+ }
+
/// [`Self::decide`] on behalf of `agent`: refuses with
/// [`ApprovalError::WrongAgent`] when the request was parked by any other
/// agent (or by the process itself), so one agent cannot answer another's
@@ -143,7 +163,7 @@ impl ApprovalGate {
let mut denied = 0;
for row in rows {
let Some(decided) =
- store::decide(&self.config, &row.request_id, ApprovalDecision::Deny)?
+ self.decide_in_parked_scope(&row.request_id, ApprovalDecision::Deny)?
else {
continue;
};
diff --git a/crates/openhuman-core/src/security/approval/store.rs b/crates/openhuman-core/src/security/approval/store.rs
index 4e8c4b4c4d..3546a43dfa 100644
--- a/crates/openhuman-core/src/security/approval/store.rs
+++ b/crates/openhuman-core/src/security/approval/store.rs
@@ -96,6 +96,16 @@ pub(super) fn capture_docs(config: &Config) -> Result<Docs, String> {
super::store_documents::current(config).map_err(|error| error.to_string())
}
+/// [`decide`] in an already-resolved store (the one a request was parked in).
+pub(super) fn decide_in(
+ docs: &Docs,
+ request_id: &str,
+ decision: ApprovalDecision,
+) -> Result<Option<PendingApproval>> {
+ publish_expired(&docs.expire_stale(Utc::now())?);
+ docs.decide(request_id, decision)
+}
+
/// [`decide`] against a store captured by [`capture_docs`].
pub(super) fn decide_captured(
captured: &Result<Docs, String>,
@@ -103,10 +113,7 @@ pub(super) fn decide_captured(
decision: ApprovalDecision,
) -> Result<Option<PendingApproval>> {
match captured {
- Ok(docs) => {
- publish_expired(&docs.expire_stale(Utc::now())?);
- docs.decide(request_id, decision)
- }
+ Ok(docs) => decide_in(docs, request_id, decision),
Err(error) => Err(anyhow::anyhow!(
"[approval::store] storage scope was unresolved when the request parked: {error}"
)),
diff --git a/crates/openhuman-core/src/security/approval/store_agent.rs b/crates/openhuman-core/src/security/approval/store_agent.rs
index ee4cce341c..9922f86933 100644
--- a/crates/openhuman-core/src/security/approval/store_agent.rs
+++ b/crates/openhuman-core/src/security/approval/store_agent.rs
@@ -13,16 +13,33 @@ pub fn exists(_config: &Config) -> bool {
/// [`list_pending`](super::list_pending) narrowed to the rows of `agent` (see
/// [`PendingApproval::belongs_to`]).
+///
+/// An embedded agent's requests are kept in its own storage scope, so the rows
+/// of `Some(agent)` are read there whichever scope the caller runs in (under a
+/// SaaS profile every agent shares the profile's scope, which is the caller's).
pub fn list_pending_for_agent(
config: &Config,
agent: Option<&str>,
) -> Result<Vec<PendingApproval>> {
- Ok(super::list_pending(config)?
+ let rows = match agent {
+ Some(agent) if !acts_for_a_profile() => {
+ let docs = super::super::store_documents::for_agent(config, agent)?;
+ super::publish_expired(&docs.expire_stale(chrono::Utc::now())?);
+ docs.list_pending()?
+ }
+ _ => super::list_pending(config)?,
+ };
+ Ok(rows
.into_iter()
.filter(|row| row.belongs_to(agent))
.collect())
}
+/// Whether the calling task serves a SaaS profile, whose agents share one scope.
+fn acts_for_a_profile() -> bool {
+ crate::core::runtime::current_tenant().is_ok_and(|tenant| tenant.profile.is_some())
+}
+
/// The agent that parked the still-undecided `request_id`: `Ok(None)` when no
/// such row exists, `Ok(Some(None))` for a row the process parked itself.
pub fn pending_agent(config: &Config, request_id: &str) -> Result<Option<Option<String>>> {
diff --git a/crates/openhuman-core/src/security/approval/store_documents.rs b/crates/openhuman-core/src/security/approval/store_documents.rs
index 7bda87c068..a8d017fd49 100644
--- a/crates/openhuman-core/src/security/approval/store_documents.rs
+++ b/crates/openhuman-core/src/security/approval/store_documents.rs
@@ -47,7 +47,7 @@ const CAS_ATTEMPTS: usize = 32;
const DOMAIN: &str = "approval::store";
/// The approval store over one scoped document handle.
-#[derive(Clone)]
+#[derive(Clone, Debug)]
pub(super) struct Docs {
docs: Arc<dyn DocumentStore>,
}
@@ -63,6 +63,17 @@ pub(super) fn current(config: &Config) -> Result<Docs> {
Ok(Docs::new(&opened.scoped()?))
}
+/// The store of embedded agent `agent`'s scope, whichever scope the caller
+/// runs in.
+pub(super) fn for_agent(config: &Config, agent: &str) -> Result<Docs> {
+ let opened = local::open(DOMAIN, &config.workspace_dir)?;
+ let scoped = opened
+ .backend
+ .for_scope(&crate::storage::scope_for_agent(agent))
+ .map_err(|error| anyhow!("[approval::store] open the agent's scope: {error}"))?;
+ Ok(Docs::new(&scoped))
+}
+
/// The legacy `approval.db`, as the storage importer reads it.
pub(crate) fn import_source() -> crate::storage::import::Source {
crate::storage::import::Source {