Skip to content

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

fix(approval): fail captured decisions when the storage scope is unresolved

Steven Enamakel1660a6eecf60
diff --git a/crates/openhuman-core/src/security/approval/gate.rs b/crates/openhuman-core/src/security/approval/gate.rs
index d3e698b0ba..d6abbb42b4 100644
--- a/crates/openhuman-core/src/security/approval/gate.rs
+++ b/crates/openhuman-core/src/security/approval/gate.rs
@@ -248,7 +248,7 @@ struct WaiterGuard<'a> {
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>,
+ docs: Result<Option<super::store_documents::Docs>, String>,
armed: bool,
}
@@ -282,7 +282,7 @@ impl Drop for WaiterGuard<'_> {
}
let decided = store::decide_captured(
&self.gate.config,
- self.docs.as_ref(),
+ &self.docs,
&self.request_id,
ApprovalDecision::Deny,
);
diff --git a/crates/openhuman-core/src/security/approval/store.rs b/crates/openhuman-core/src/security/approval/store.rs
index 49ec012c09..e952d5f6bb 100644
--- a/crates/openhuman-core/src/security/approval/store.rs
+++ b/crates/openhuman-core/src/security/approval/store.rs
@@ -349,24 +349,28 @@ 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()
+/// no longer installed. A scope that cannot be resolved is kept as an error,
+/// so the later decision fails instead of switching to the SQLite store.
+pub(super) fn capture_docs() -> Result<Option<super::store_documents::Docs>, String> {
+ super::store_documents::current().map_err(|error| error.to_string())
}
-/// [`decide`] against a store captured by [`capture_docs`] when there is one.
+/// [`decide`] against a store captured by [`capture_docs`].
pub(super) fn decide_captured(
config: &Config,
- captured: Option<&super::store_documents::Docs>,
+ captured: &Result<Option<super::store_documents::Docs>, String>,
request_id: &str,
decision: ApprovalDecision,
) -> Result<Option<PendingApproval>> {
match captured {
- Some(docs) => {
+ Ok(Some(docs)) => {
publish_expired(&docs.expire_stale(Utc::now())?);
docs.decide(request_id, decision)
}
- None => decide(config, request_id, decision),
+ Ok(None) => decide(config, 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_documents.rs b/crates/openhuman-core/src/security/approval/store_documents.rs
index 5708c5fbdc..a5a2e2b7fa 100644
--- a/crates/openhuman-core/src/security/approval/store_documents.rs
+++ b/crates/openhuman-core/src/security/approval/store_documents.rs
@@ -71,6 +71,13 @@ fn rfc3339(at: DateTime<Utc>) -> String {
at.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true)
}
+/// The comparable expiry key: nanoseconds since the epoch, falling back to
+/// milliseconds for instants beyond the i64-nanosecond range (year 2262).
+fn expiry_key(at: DateTime<Utc>) -> i64 {
+ at.timestamp_nanos_opt()
+ .unwrap_or_else(|| at.timestamp_millis().saturating_mul(1_000_000))
+}
+
fn parse_time(raw: Option<&str>) -> Option<DateTime<Utc>> {
raw.and_then(|value| DateTime::parse_from_rfc3339(value).ok())
.map(|at| at.with_timezone(&Utc))
@@ -170,7 +177,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_millis()),
+ "expires_ts": pending.expires_at.map(expiry_key),
"source_context": pending
.source_context
.as_ref()
@@ -226,7 +233,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_millis();
+ let now_ts = expiry_key(now);
self.run(|docs| async move {
let query =
Query::filter(Filter::eq("pending", true).and(Filter::lte("expires_ts", now_ts)));
diff --git a/crates/openhuman-core/src/security/approval/store_documents_tests.rs b/crates/openhuman-core/src/security/approval/store_documents_tests.rs
index 69127d05b3..b6c92d0ba0 100644
--- a/crates/openhuman-core/src/security/approval/store_documents_tests.rs
+++ b/crates/openhuman-core/src/security/approval/store_documents_tests.rs
@@ -343,3 +343,32 @@ fn a_captured_store_decides_without_the_task_scope() {
.unwrap();
assert_eq!(decided.map(|p| p.request_id), Some("r".to_string()));
}
+
+#[test]
+fn an_unresolved_scope_fails_the_captured_decide_instead_of_using_sqlite() {
+ let config = crate::config::Config::default();
+ let captured = Err("no acting agent".to_string());
+ let error = crate::security::approval::store::decide_captured(
+ &config,
+ &captured,
+ "r",
+ ApprovalDecision::Deny,
+ )
+ .unwrap_err();
+ assert!(error.to_string().contains("unresolved"), "{error}");
+}
+
+#[test]
+fn expiry_is_compared_below_a_millisecond() {
+ let store = docs();
+ let whole = Utc::now().with_nanosecond(0).unwrap();
+ let due = whole + Duration::nanoseconds(900_000);
+ let mut p = pending("tight", 0, None);
+ p.expires_at = Some(due);
+ store.insert_pending(&p, "s").unwrap();
+ assert!(store
+ .expire_stale(whole + Duration::nanoseconds(100_000))
+ .unwrap()
+ .is_empty());
+ assert_eq!(store.expire_stale(due).unwrap().len(), 1);
+}