Skip to content

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

feat(security): add document approval store and gate interception

Steven Enamakele22bd34896b2
diff --git a/crates/openhuman-core/src/security/approval/gate.rs b/crates/openhuman-core/src/security/approval/gate.rs
index 56dade16b7..d3e698b0ba 100644
--- a/crates/openhuman-core/src/security/approval/gate.rs
+++ b/crates/openhuman-core/src/security/approval/gate.rs
@@ -246,6 +246,9 @@ struct WaiterGuard<'a> {
gate: &'a ApprovalGate,
request_id: String,
thread_id: Option<String>,
+ /// Storage scope of the parking call, kept for `Drop`: the acting agent's
+ /// task-local is gone by then.
+ docs: Option<super::store_documents::Docs>,
armed: bool,
}
@@ -277,7 +280,12 @@ impl Drop for WaiterGuard<'_> {
self.gate
.clear_thread_route_if_owned(thread_id, &self.request_id);
}
- let decided = store::decide(&self.gate.config, &self.request_id, ApprovalDecision::Deny);
+ let decided = store::decide_captured(
+ &self.gate.config,
+ self.docs.as_ref(),
+ &self.request_id,
+ ApprovalDecision::Deny,
+ );
if let Ok(Some(row)) = decided {
let route = self.gate.take_request_route(&self.request_id);
BUS.publish(DomainEvent::ApprovalDecided {
diff --git a/crates/openhuman-core/src/security/approval/gate_intercept.rs b/crates/openhuman-core/src/security/approval/gate_intercept.rs
index 03b1937ebb..6c9b55b9a7 100644
--- a/crates/openhuman-core/src/security/approval/gate_intercept.rs
+++ b/crates/openhuman-core/src/security/approval/gate_intercept.rs
@@ -591,6 +591,7 @@ impl ApprovalGate {
gate: self,
request_id: request_id.clone(),
thread_id: chat_thread_id.clone(),
+ docs: store::capture_docs(),
armed: true,
};
diff --git a/crates/openhuman-core/src/security/approval/store.rs b/crates/openhuman-core/src/security/approval/store.rs
index a8d5742ef8..49ec012c09 100644
--- a/crates/openhuman-core/src/security/approval/store.rs
+++ b/crates/openhuman-core/src/security/approval/store.rs
@@ -347,6 +347,29 @@ pub fn get_decision(config: &Config, request_id: &str) -> Result<Option<Approval
})
}
+/// The document store of the current call, for a caller that decides later
+/// from outside this call's task scope (a `Drop`), where the acting agent is
+/// no longer installed.
+pub(super) fn capture_docs() -> Option<super::store_documents::Docs> {
+ super::store_documents::current().ok().flatten()
+}
+
+/// [`decide`] against a store captured by [`capture_docs`] when there is one.
+pub(super) fn decide_captured(
+ config: &Config,
+ captured: Option<&super::store_documents::Docs>,
+ request_id: &str,
+ decision: ApprovalDecision,
+) -> Result<Option<PendingApproval>> {
+ match captured {
+ Some(docs) => {
+ publish_expired(&docs.expire_stale(Utc::now())?);
+ docs.decide(request_id, decision)
+ }
+ None => decide(config, request_id, decision),
+ }
+}
+
/// Mark a pending row as decided and return the now-decided row.
/// Returns `Ok(None)` if no row matched (already decided, expired, or
/// unknown id).
diff --git a/crates/openhuman-core/src/security/approval/store_documents.rs b/crates/openhuman-core/src/security/approval/store_documents.rs
index 04233fea62..43825a12cd 100644
--- a/crates/openhuman-core/src/security/approval/store_documents.rs
+++ b/crates/openhuman-core/src/security/approval/store_documents.rs
@@ -66,8 +66,9 @@ fn storage(error: StorageError) -> anyhow::Error {
anyhow!("[approval::store] storage: {error}")
}
+/// Fixed-width (nanosecond, `Z`) so the strings sort in time order.
fn rfc3339(at: DateTime<Utc>) -> String {
- at.to_rfc3339()
+ at.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true)
}
fn parse_time(raw: Option<&str>) -> Option<DateTime<Utc>> {
@@ -169,7 +170,7 @@ impl Docs {
"session_id": session_id,
"created_at": rfc3339(pending.created_at),
"expires_at": pending.expires_at.map(rfc3339),
- "expires_ts": pending.expires_at.map(|at| at.timestamp()),
+ "expires_ts": pending.expires_at.map(|at| at.timestamp_millis()),
"source_context": pending
.source_context
.as_ref()
@@ -225,7 +226,7 @@ impl Docs {
pub(super) fn expire_stale(&self, now: DateTime<Utc>) -> Result<Vec<PendingApproval>> {
let deny = ApprovalDecision::Deny.as_str();
let decided_at = rfc3339(now);
- let now_ts = now.timestamp();
+ let now_ts = now.timestamp_millis();
self.run(|docs| async move {
let query =
Query::filter(Filter::eq("pending", true).and(Filter::lte("expires_ts", now_ts)));
@@ -242,8 +243,8 @@ impl Docs {
Some(next)
})
.await?;
- if moved.is_some() {
- expired.push(to_pending(&stale));
+ if let Some(moved) = moved {
+ expired.push(to_pending(&moved));
}
}
Ok(expired)
@@ -278,7 +279,18 @@ impl Docs {
decision: ApprovalDecision,
) -> Result<Option<PendingApproval>> {
let id = request_id.to_string();
- let decided_at = rfc3339(Utc::now());
+ self.decide_at(request_id, decision, Utc::now())
+ }
+
+ /// [`decide`](Self::decide) stamped `at`, so ordering can be pinned.
+ pub(super) fn decide_at(
+ &self,
+ request_id: &str,
+ decision: ApprovalDecision,
+ at: DateTime<Utc>,
+ ) -> Result<Option<PendingApproval>> {
+ let id = request_id.to_string();
+ let decided_at = rfc3339(at);
self.run(|docs| async move {
let decided = update(&docs, &id, |doc| {
if doc.get("pending") != Some(&json!(true)) {