Skip to content

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

test(security): cover agent-keyed approval routing, scoped decide and the agent column (#7032)

oxoxDev7ffdf5703824
diff --git a/crates/openhuman-core/src/security/approval/gate_agent_tests.rs b/crates/openhuman-core/src/security/approval/gate_agent_tests.rs
index 40f678a2e4..8011eca914 100644
--- a/crates/openhuman-core/src/security/approval/gate_agent_tests.rs
+++ b/crates/openhuman-core/src/security/approval/gate_agent_tests.rs
@@ -159,3 +159,177 @@ async fn an_agent_without_auto_approve_all_parks_under_a_process_policy_that_has
"the agent's own policy must win over the process blanket approval"
);
}
+
+fn park(
+ gate: &Arc<ApprovalGate>,
+ agent: &str,
+) -> tokio::task::JoinHandle<(GateOutcome, Option<String>)> {
+ let gate = Arc::clone(gate);
+ let ctx = agent_ctx(agent, None, false);
+ tokio::spawn(CoreContext::scope(
+ ctx,
+ turn_origin::with_origin(
+ web_origin(),
+ APPROVAL_CHAT_CONTEXT.scope(chat_ctx(), async move {
+ gate.intercept_audited("shell", "run it", serde_json::json!({}))
+ .await
+ }),
+ ),
+ ))
+}
+
+async fn parked_for(gate: &ApprovalGate, agent: &str) -> String {
+ for _ in 0..200 {
+ if let Some(request_id) = gate.pending_for_agent_thread(Some(agent), "t-test") {
+ return request_id;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ panic!("{agent} never parked");
+}
+
+async fn requested_agent(
+ rx: &mut tinybus::events::EventReceiver<crate::core::events::DomainEvent>,
+ expected_request_id: &str,
+) -> Option<String> {
+ loop {
+ match rx.recv().await {
+ Some(crate::core::events::DomainEvent::ApprovalRequested {
+ request_id,
+ agent_id,
+ ..
+ }) if request_id == expected_request_id => return agent_id,
+ Some(_) => continue,
+ None => panic!("the bus closed before the expected event arrived"),
+ }
+ }
+}
+
+#[tokio::test]
+async fn agents_parked_on_one_thread_id_are_routed_and_decided_apart() {
+ crate::core::bus::init().await.expect("bus init");
+ let mut events = crate::core::bus::BUS
+ .get()
+ .expect("event bus initialized above")
+ .receiver();
+ let (gate, _dir) = test_gate();
+ let gate = Arc::new(gate);
+
+ let alpha = park(&gate, "route-alpha");
+ let beta = park(&gate, "route-beta");
+ let alpha_request = parked_for(&gate, "route-alpha").await;
+ let beta_request = parked_for(&gate, "route-beta").await;
+ assert_ne!(alpha_request, beta_request);
+ assert!(
+ gate.pending_for_agent_thread(None, "t-test").is_none(),
+ "the process's own route for the thread stays empty"
+ );
+ assert!(
+ gate.pending_for_thread("t-test").is_none(),
+ "an unscoped reply must not reach an agent's request"
+ );
+
+ let seen = tokio::time::timeout(
+ Duration::from_secs(5),
+ requested_agent(&mut events, &alpha_request),
+ )
+ .await
+ .expect("ApprovalRequested for alpha");
+ assert_eq!(seen.as_deref(), Some("route-alpha"));
+
+ let alpha_rows = gate.list_pending_for_agent(Some("route-alpha")).unwrap();
+ assert_eq!(alpha_rows.len(), 1);
+ assert_eq!(alpha_rows[0].request_id, alpha_request);
+ assert_eq!(alpha_rows[0].agent_id.as_deref(), Some("route-alpha"));
+ assert!(gate.list_pending_for_agent(None).unwrap().is_empty());
+
+ let refused = gate
+ .decide_for_agent("route-alpha", &beta_request, ApprovalDecision::ApproveOnce)
+ .expect_err("alpha must not decide beta's request");
+ assert_eq!(
+ refused.downcast_ref::<ApprovalError>(),
+ Some(&ApprovalError::WrongAgent {
+ request_id: beta_request.clone()
+ })
+ );
+ assert_eq!(
+ gate.pending_for_agent_thread(Some("route-beta"), "t-test"),
+ Some(beta_request.clone()),
+ "beta stays parked after the refused decision"
+ );
+
+ let decided = gate
+ .decide_for_agent("route-alpha", &alpha_request, ApprovalDecision::ApproveOnce)
+ .unwrap()
+ .expect("alpha's row decided");
+ assert_eq!(decided.agent_id.as_deref(), Some("route-alpha"));
+ let (outcome, _) = alpha.await.unwrap();
+ assert!(matches!(outcome, GateOutcome::Allow), "got {outcome:?}");
+ assert!(
+ !beta.is_finished(),
+ "alpha's decision must not release beta"
+ );
+
+ let denied = gate
+ .deny_all_for_agent("route-beta", "agent_removed")
+ .unwrap();
+ assert_eq!(denied, 1);
+ let (outcome, _) = beta.await.unwrap();
+ assert!(
+ matches!(outcome, GateOutcome::Deny { .. }),
+ "got {outcome:?}"
+ );
+ let decided = tokio::time::timeout(
+ Duration::from_secs(5),
+ find_approval_decided(&mut events, &beta_request),
+ )
+ .await
+ .expect("ApprovalDecided for beta");
+ match decided {
+ crate::core::events::DomainEvent::ApprovalDecided {
+ agent_id,
+ resolution,
+ decision,
+ ..
+ } => {
+ assert_eq!(agent_id.as_deref(), Some("route-beta"));
+ assert_eq!(resolution.as_deref(), Some("agent_removed"));
+ assert_eq!(decision, "deny");
+ }
+ other => panic!("unexpected event {other:?}"),
+ }
+ assert!(gate
+ .list_pending_for_agent(Some("route-beta"))
+ .unwrap()
+ .is_empty());
+}
+
+#[tokio::test]
+async fn an_agent_cannot_decide_a_request_the_process_parked() {
+ let (gate, _dir) = test_gate();
+ let gate = Arc::new(gate);
+ let g = Arc::clone(&gate);
+ let process = tokio::spawn(turn_origin::with_origin(
+ web_origin(),
+ APPROVAL_CHAT_CONTEXT.scope(chat_ctx(), async move {
+ g.intercept_audited("shell", "run it", serde_json::json!({}))
+ .await
+ }),
+ ));
+ let request_id = loop {
+ if let Some(request_id) = gate.pending_for_agent_thread(None, "t-test") {
+ break request_id;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ };
+
+ let refused = gate.decide_for_agent("intruder", &request_id, ApprovalDecision::Deny);
+ assert!(
+ refused.is_err(),
+ "an agent must not decide a process request"
+ );
+
+ gate.decide(&request_id, ApprovalDecision::Deny).unwrap();
+ let (outcome, _) = process.await.unwrap();
+ assert!(matches!(outcome, GateOutcome::Deny { .. }));
+}
diff --git a/crates/openhuman-core/src/security/approval/store_persistence_tests.rs b/crates/openhuman-core/src/security/approval/store_persistence_tests.rs
index 96f251e990..a81c6e3bb0 100644
--- a/crates/openhuman-core/src/security/approval/store_persistence_tests.rs
+++ b/crates/openhuman-core/src/security/approval/store_persistence_tests.rs
@@ -664,3 +664,84 @@ fn list_recent_decisions_marks_unparseable_args_instead_of_failing() {
json!({ "_error": "args_redacted not valid JSON" })
);
}
+
+#[test]
+fn agent_id_round_trips_and_older_rows_read_as_the_process() {
+ let (config, _dir) = test_config();
+ let mut owned = PendingApproval::new("owned", "shell", "run it", json!({}), None);
+ owned.agent_id = Some("alpha".into());
+ insert_pending(&config, &owned, "session-x").unwrap();
+ insert_pending(
+ &config,
+ &PendingApproval::new("process", "shell", "run it", json!({}), None),
+ "session-x",
+ )
+ .unwrap();
+
+ let alpha = list_pending_for_agent(&config, Some("alpha")).unwrap();
+ assert_eq!(alpha.len(), 1);
+ assert_eq!(alpha[0].request_id, "owned");
+ assert_eq!(alpha[0].agent_id.as_deref(), Some("alpha"));
+ let process = list_pending_for_agent(&config, None).unwrap();
+ assert_eq!(process.len(), 1);
+ assert_eq!(process[0].request_id, "process");
+
+ assert_eq!(
+ pending_agent(&config, "owned").unwrap(),
+ Some(Some("alpha".into()))
+ );
+ assert_eq!(pending_agent(&config, "process").unwrap(), Some(None));
+ assert_eq!(pending_agent(&config, "missing").unwrap(), None);
+ decide(&config, "owned", ApprovalDecision::Deny).unwrap();
+ assert_eq!(pending_agent(&config, "owned").unwrap(), None);
+}
+
+#[test]
+fn migration_adds_the_agent_column_and_index_to_an_existing_table() {
+ let (config, _dir) = test_config();
+ let db_path = config.workspace_dir.join("approval").join("approval.db");
+ std::fs::create_dir_all(db_path.parent().unwrap()).unwrap();
+ Connection::open(&db_path)
+ .unwrap()
+ .execute_batch(
+ "CREATE TABLE pending_approvals (
+ request_id TEXT PRIMARY KEY,
+ tool_name TEXT NOT NULL,
+ action_summary TEXT NOT NULL,
+ args_redacted TEXT NOT NULL,
+ session_id TEXT NOT NULL,
+ created_at TEXT NOT NULL,
+ expires_at TEXT,
+ decided_at TEXT,
+ decision TEXT
+ );
+ INSERT INTO pending_approvals
+ (request_id, tool_name, action_summary, args_redacted, session_id, created_at)
+ VALUES ('legacy', 'shell', 'legacy row', '{}', 'sess-X', '2030-01-01T00:00:00Z');",
+ )
+ .unwrap();
+
+ let rows = list_pending(&config).unwrap();
+ assert_eq!(rows.len(), 1);
+ assert!(
+ rows[0].agent_id.is_none(),
+ "a legacy row is the process's own"
+ );
+ let conn = Connection::open(&db_path).unwrap();
+ let columns: Vec<String> = conn
+ .prepare("PRAGMA table_info(pending_approvals)")
+ .unwrap()
+ .query_map(params![], |row| row.get::<_, String>(1))
+ .unwrap()
+ .map(Result::unwrap)
+ .collect();
+ assert!(columns.iter().any(|c| c == "agent_id"));
+ let indexes: Vec<String> = conn
+ .prepare("PRAGMA index_list(pending_approvals)")
+ .unwrap()
+ .query_map(params![], |row| row.get::<_, String>(1))
+ .unwrap()
+ .map(Result::unwrap)
+ .collect();
+ assert!(indexes.iter().any(|i| i == "idx_pending_approvals_agent"));
+}