Skip to content

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

test(approval): pin timestamp ordering, expiry precision and captured-scope decide

Steven Enamakel52d142828fa1
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 8a17d7308d..69127d05b3 100644
--- a/crates/openhuman-core/src/security/approval/store_documents_tests.rs
+++ b/crates/openhuman-core/src/security/approval/store_documents_tests.rs
@@ -1,5 +1,5 @@
use super::*;
-use chrono::Duration;
+use chrono::{Duration, Timelike};
use tinystoragedrivers::{MemoryStorage, Scope, StorageBackend};
fn docs_in(storage: &MemoryStorage, scope: &str) -> Docs {
@@ -160,15 +160,23 @@ fn execution_is_recorded_after_a_decision_and_only_once() {
assert_eq!(stored.doc["execution_outcome"], json!("failure"));
let error = stored.doc["execution_error"].as_str().unwrap();
assert!(error.chars().count() <= 512, "capped");
+ assert!(
+ !error.contains("sk-abcdefghijklmnopqrstuvwxyz"),
+ "the secret is redacted before storage: {error}"
+ );
}
#[test]
fn recent_decisions_are_newest_first_and_capped() {
let store = docs();
- for id in ["a", "b", "c"] {
+ let base = Utc::now();
+ for (n, id) in ["a", "b", "c"].into_iter().enumerate() {
store.insert_pending(&pending(id, 0, None), "s").unwrap();
- store.decide(id, ApprovalDecision::ApproveOnce).unwrap();
- std::thread::sleep(std::time::Duration::from_millis(5));
+ // Distinct sub-second stamps (the fixed-width format must order them).
+ let at = base + Duration::milliseconds(n as i64 * 100);
+ store
+ .decide_at(id, ApprovalDecision::ApproveOnce, at)
+ .unwrap();
}
store
.insert_pending(&pending("open", 0, None), "s")
@@ -289,3 +297,49 @@ fn a_missing_error_stays_missing() {
assert!(audit_error(None).is_none());
assert_eq!(audit_error(Some("plain")).as_deref(), Some("plain"));
}
+
+#[test]
+fn sub_second_timestamps_order_and_expire_on_time() {
+ let store = docs();
+ let base = Utc::now();
+ // Whole-second and sub-second values in the same second must sort by time.
+ let whole = base.with_nanosecond(0).unwrap();
+ for (id, at) in [("x", whole + Duration::milliseconds(500)), ("w", whole)] {
+ let mut p = pending(id, 0, None);
+ p.created_at = at;
+ store.insert_pending(&p, "s").unwrap();
+ }
+ let ids: Vec<String> = store
+ .list_pending()
+ .unwrap()
+ .into_iter()
+ .map(|p| p.request_id)
+ .collect();
+ assert_eq!(ids, ["w", "x"]);
+
+ // An approval due 900 ms into a second is not expired at 100 ms.
+ let due = whole + Duration::milliseconds(900);
+ let mut p = pending("late", 0, None);
+ p.expires_at = Some(due);
+ store.insert_pending(&p, "s").unwrap();
+ assert!(store
+ .expire_stale(whole + Duration::milliseconds(100))
+ .unwrap()
+ .is_empty());
+ assert_eq!(store.expire_stale(due).unwrap().len(), 1);
+}
+
+#[test]
+fn a_captured_store_decides_without_the_task_scope() {
+ let store = docs();
+ store.insert_pending(&pending("r", 0, None), "s").unwrap();
+ let config = crate::config::Config::default();
+ let decided = crate::security::approval::store::decide_captured(
+ &config,
+ Some(&store),
+ "r",
+ ApprovalDecision::Deny,
+ )
+ .unwrap();
+ assert_eq!(decided.map(|p| p.request_id), Some("r".to_string()));
+}