The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(security): add approval store persistence tests
Steven Enamakelf0ac0b1f7591
diff --git a/crates/openhuman-core/src/security/approval/store_persistence_tests.rs b/crates/openhuman-core/src/security/approval/store_persistence_tests.rsindex 023f9d3db3..c03707a71b 100644--- a/crates/openhuman-core/src/security/approval/store_persistence_tests.rs+++ b/crates/openhuman-core/src/security/approval/store_persistence_tests.rs@@ -239,130 +239,6 @@ fn record_execution_unknown_id_is_safe_noop() {assert!(!ok, "unknown id must report no row updated");}-#[test]-fn migrate_columns_is_idempotent_on_v1_databases() {- // Simulate an older build by creating the v1 table shape- // manually (no executed_at / execution_outcome / execution_error)- // then opening the store via with_connection — the migration- // must add the missing columns without losing existing rows.- let dir = TempDir::new().unwrap();- let workspace = dir.path().to_path_buf();- let db_path = workspace.join("approval").join("approval.db");- std::fs::create_dir_all(db_path.parent().unwrap()).unwrap();- {- let conn = Connection::open(&db_path).unwrap();- conn.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- );",- )- .unwrap();- conn.execute(- "INSERT INTO pending_approvals- (request_id, tool_name, action_summary, args_redacted,- session_id, created_at)- VALUES ('legacy', 'composio', 'legacy row', '{}', 'sess-X', ?1)",- params![Utc::now().to_rfc3339()],- )- .unwrap();- }- let mut config = Config {- workspace_dir: workspace,- ..Config::default()- };- config.storage.url = Some("classic".into());- // First open triggers the migration; existing row survives.- let rows = list_pending(&config).unwrap();- assert_eq!(rows.len(), 1);- assert_eq!(rows[0].request_id, "legacy");- // After migration, record_execution must work end-to-end.- decide(&config, "legacy", ApprovalDecision::ApproveOnce).unwrap();- assert!(record_execution(&config, "legacy", ExecutionOutcome::Success, None).unwrap());- // Second open must be a no-op (migration is idempotent).- let rows = list_pending(&config).unwrap();- assert!(rows.is_empty(), "decided rows should not appear in pending");-}--#[test]-fn migrate_session_id_scrub_overwrites_legacy_values_and_bumps_user_version() {- // Simulate an older build that wrote credential-shaped values- // into `session_id`. After opening the store via- // `with_connection`, every pre-existing session_id must be- // overwritten with the redaction sentinel, and re-opening the- // store must be a no-op (idempotent — guarded by user_version).- let dir = TempDir::new().unwrap();- let workspace = dir.path().to_path_buf();- let db_path = workspace.join("approval").join("approval.db");- std::fs::create_dir_all(db_path.parent().unwrap()).unwrap();- // The bearer-shaped value below is a fixture, NOT a real- // credential — picked to be obviously recognizable in any- // diff if the scrub ever regresses.- let bearer_shaped = "deadbeefcafebabe1234567890abcdef";- {- let conn = Connection::open(&db_path).unwrap();- conn.execute_batch(SCHEMA).unwrap();- conn.execute(- "INSERT INTO pending_approvals- (request_id, tool_name, action_summary, args_redacted,- session_id, created_at)- VALUES ('legacy', 'composio', 'legacy row', '{}', ?1, ?2)",- params![bearer_shaped, Utc::now().to_rfc3339()],- )- .unwrap();- // Sanity-check: a fresh DB starts at user_version = 0.- let v: i64 = conn- .query_row("PRAGMA user_version", params![], |r| r.get(0))- .unwrap();- assert_eq!(v, 0);- }- let mut config = Config {- workspace_dir: workspace,- ..Config::default()- };- config.storage.url = Some("classic".into());- // First open runs the scrub.- let _ = list_pending(&config).unwrap();- {- let conn = Connection::open(&db_path).unwrap();- let stored: String = conn- .query_row(- "SELECT session_id FROM pending_approvals WHERE request_id = 'legacy'",- params![],- |r| r.get(0),- )- .unwrap();- assert_eq!(- stored, PRE_MIGRATION_SESSION_ID,- "scrub must overwrite legacy session_id with the redaction sentinel"- );- let v: i64 = conn- .query_row("PRAGMA user_version", params![], |r| r.get(0))- .unwrap();- assert_eq!(v, 1, "user_version must be bumped to 1 after scrub");- }- // Second open must NOT touch already-scrubbed rows.- let _ = list_pending(&config).unwrap();- {- let conn = Connection::open(&db_path).unwrap();- let stored: String = conn- .query_row(- "SELECT session_id FROM pending_approvals WHERE request_id = 'legacy'",- params![],- |r| r.get(0),- )- .unwrap();- assert_eq!(stored, PRE_MIGRATION_SESSION_ID);- }-}-#[test]fn list_pending_expires_stale_rows_before_returning() {let (config, _dir) = test_config();@@ -516,58 +392,6 @@ fn list_recent_decisions_clamps_zero_limit_to_one() {assert_eq!(rows.len(), 1);}-#[test]-fn list_recent_decisions_rejects_unknown_decision_values() {- let (config, _dir) = test_config();- insert_pending(&config, &sample("corrupt-decision", "sess-A"), "sess-A").unwrap();- with_connection(&config, |conn| {- conn.execute(- "UPDATE pending_approvals- SET decided_at = ?1, decision = ?2- WHERE request_id = ?3",- params![Utc::now().to_rfc3339(), "maybe", "corrupt-decision"],- )?;- Ok(())- })- .unwrap();-- let err = list_recent_decisions(&config, 10).unwrap_err();-- assert!(- err.to_string().contains("Invalid column type")- || err.to_string().contains("unknown approval decision"),- "unexpected error: {err}"- );-}--#[test]-fn list_recent_decisions_rejects_invalid_audit_timestamps() {- let (config, _dir) = test_config();- insert_pending(&config, &sample("corrupt-time", "sess-A"), "sess-A").unwrap();- with_connection(&config, |conn| {- conn.execute(- "UPDATE pending_approvals- SET decided_at = ?1, decision = ?2- WHERE request_id = ?3",- params![- "not-a-date",- ApprovalDecision::Deny.as_str(),- "corrupt-time"- ],- )?;- Ok(())- })- .unwrap();-- let err = list_recent_decisions(&config, 10).unwrap_err();-- assert!(- err.to_string().contains("Invalid column type")- || err.to_string().contains("premature end of input"),- "unexpected error: {err}"- );-}-#[test]fn insert_pending_round_trips_flow_source_context() {let (config, _dir) = test_config();@@ -636,37 +460,6 @@ fn list_pending_for_flow_run_filters_to_the_matching_flow_and_run() {assert_eq!(rows[0].request_id, "a");}-#[test]-fn list_recent_decisions_marks_unparseable_args_instead_of_failing() {- let (config, _dir) = test_config();- insert_pending(&config, &sample("corrupt-json", "sess-A"), "sess-A").unwrap();- with_connection(&config, |conn| {- conn.execute(- "UPDATE pending_approvals- SET args_redacted = ?1, decided_at = ?2, decision = ?3- WHERE request_id = ?4",- params![- "{not valid json",- Utc::now().to_rfc3339(),- ApprovalDecision::Deny.as_str(),- "corrupt-json"- ],- )?;- Ok(())- })- .unwrap();-- let rows = list_recent_decisions(&config, 10).unwrap();- let row = rows- .iter()- .find(|row| row.request_id == "corrupt-json")- .expect("corrupt audit row is still listed");- assert_eq!(- row.args_redacted,- 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();@@ -698,52 +491,3 @@ fn agent_id_round_trips_and_older_rows_read_as_the_process() {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"));-}diff --git a/crates/openhuman-core/src/security/approval/store_tests.rs b/crates/openhuman-core/src/security/approval/store_tests.rsindex e60987167e..8f8e24db07 100644--- a/crates/openhuman-core/src/security/approval/store_tests.rs+++ b/crates/openhuman-core/src/security/approval/store_tests.rs@@ -1,30 +1,18 @@use super::*;use crate::security::approval::types::{ApprovalDecision, PendingApproval};-use crate::storage::rusqlite;use chrono::Duration;use serde_json::json;use tempfile::TempDir;fn test_config() -> (Config, TempDir) {let dir = TempDir::new().unwrap();- let mut config = Config {+ let config = Config {workspace_dir: dir.path().to_path_buf(),..Config::default()};- // These tests exercise the legacy SQLite tables themselves.- config.storage.url = Some("classic".into());(config, dir)}-#[test]-fn schema_migration_surfaces_non_duplicate_alter_errors() {- let conn = rusqlite::Connection::open_in_memory().unwrap();- conn.execute_batch("CREATE VIEW pending_approvals AS SELECT 1 AS id")- .unwrap();- let error = migrate_columns(&conn).expect_err("a view cannot be migrated as a table");- assert!(error.to_string().contains("add column executed_at"));-}-/// Build a sample `PendingApproval`. The `_session_id` parameter/// is preserved as a positional argument for call-site readability/// (so a reader can see "this row belongs to sess-A") even though@@ -56,50 +44,45 @@ fn sample_with_expiry(}}+/// The stored document of `request_id`, as the store wrote it.+fn raw_doc(config: &Config, request_id: &str) -> Option<serde_json::Value> {+ let opened = crate::storage::local::open("approval::store-test", &config.workspace_dir)+ .expect("open the storage backend");+ let docs = std::sync::Arc::clone(opened.scoped().expect("scope").documents());+ let id = request_id.to_string();+ crate::storage::block_on(async move { docs.get("approvals", &id).await })+ .expect("read the approval document")+ .map(|stored| stored.doc)+}++fn text_field(doc: &serde_json::Value, field: &str) -> Option<String> {+ doc.get(field).and_then(|v| v.as_str()).map(str::to_string)+}+fn fetch_decision_state(config: &Config,request_id: &str,) -> Option<(Option<String>, Option<String>)> {- with_connection(config, |conn| {- let mut stmt = conn- .prepare("SELECT decided_at, decision FROM pending_approvals WHERE request_id = ?1")- .context("prepare raw decision lookup")?;- let mut rows = stmt- .query(params![request_id])- .context("query raw decision lookup")?;- if let Some(row) = rows.next().context("decision row next")? {- let decided_at: Option<String> = row.get(0)?;- let decision: Option<String> = row.get(1)?;- Ok(Some((decided_at, decision)))- } else {- Ok(None)- }+ raw_doc(config, request_id).map(|doc| {+ (+ text_field(&doc, "decided_at"),+ text_field(&doc, "decision"),+ )})- .unwrap()}-// ── record_execution / column-migration tests (#2135) ──────────+// ── record_execution tests (#2135) ──────────fn read_execution_row(config: &Config,request_id: &str,) -> (Option<String>, Option<String>, Option<String>) {- with_connection(config, |conn| {- let mut stmt = conn- .prepare(- "SELECT executed_at, execution_outcome, execution_error- FROM pending_approvals WHERE request_id = ?1",- )- .unwrap();- let mut rows = stmt.query(params![request_id]).unwrap();- let row = rows.next().unwrap().expect("row exists");- Ok((- row.get::<_, Option<String>>(0).unwrap(),- row.get::<_, Option<String>>(1).unwrap(),- row.get::<_, Option<String>>(2).unwrap(),- ))- })- .unwrap()+ let doc = raw_doc(config, request_id).expect("row exists");+ (+ text_field(&doc, "executed_at"),+ text_field(&doc, "execution_outcome"),+ text_field(&doc, "execution_error"),+ )}// ── source_context / flow_tool_trust (flow-approval-surface, PR2) ──────