Skip to content

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

feat(approval): route approval store through document backend

Steven Enamakeldac5539ef9b5
diff --git a/crates/openhuman-core/src/security/approval/mod.rs b/crates/openhuman-core/src/security/approval/mod.rs
index 142017a42c..4acf392c89 100644
--- a/crates/openhuman-core/src/security/approval/mod.rs
+++ b/crates/openhuman-core/src/security/approval/mod.rs
@@ -18,6 +18,7 @@ pub mod redact;
pub mod rpc;
pub mod schemas;
pub mod store;
+mod store_documents;
pub mod types;
pub use gate::{
diff --git a/crates/openhuman-core/src/security/approval/store.rs b/crates/openhuman-core/src/security/approval/store.rs
index 2e3cf4054d..a8d5742ef8 100644
--- a/crates/openhuman-core/src/security/approval/store.rs
+++ b/crates/openhuman-core/src/security/approval/store.rs
@@ -22,6 +22,13 @@
//! Follows the same `with_connection` shape as `notifications/store.rs`
//! and `cron/store.rs`: synchronous `rusqlite::Connection` opened per
//! call, schema applied idempotently.
+//!
+//! When the host configured a storage backend ([`crate::storage`]), every
+//! function here uses `store_documents` instead: the same operations on the
+//! `tinystoragedrivers` document port, under the current call's storage
+//! scope (the acting agent; `local` on a single-user host; refused in SaaS
+//! mode without one). With no backend — the desktop default — nothing
+//! changes and `approval/approval.db` is used as before.
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
@@ -224,6 +231,9 @@ fn with_connection<T>(config: &Config, f: impl FnOnce(&Connection) -> Result<T>)
/// internal correlation only and is never re-exposed on
/// [`PendingApproval`] (see that type's doc-comment).
pub fn insert_pending(config: &Config, pending: &PendingApproval, session_id: &str) -> Result<()> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.insert_pending(pending, session_id);
+ }
with_connection(config, |conn| {
let args = serde_json::to_string(&pending.args_redacted)
.context("[approval::store] serialize args_redacted")?;
@@ -266,6 +276,11 @@ pub fn insert_pending(config: &Config, pending: &PendingApproval, session_id: &s
/// (`decided_at` + `decision`) without leaving expired rows pending
/// forever.
pub fn expire_stale(config: &Config) -> Result<usize> {
+ if let Some(docs) = super::store_documents::current()? {
+ let expired = docs.expire_stale(Utc::now())?;
+ publish_expired(&expired);
+ return Ok(expired.len());
+ }
with_connection(config, |conn| {
Ok(expire_stale_with_now(conn, Utc::now())?.len())
})
@@ -275,6 +290,10 @@ pub fn expire_stale(config: &Config) -> Result<usize> {
/// which launch queued them. Orphan rows from prior sessions remain
/// visible until they are explicitly decided or expire.
pub fn list_pending(config: &Config) -> Result<Vec<PendingApproval>> {
+ if let Some(docs) = super::store_documents::current()? {
+ publish_expired(&docs.expire_stale(Utc::now())?);
+ return docs.list_pending();
+ }
with_connection(config, |conn| {
expire_stale_with_now(conn, Utc::now())?;
@@ -304,6 +323,9 @@ pub fn list_pending(config: &Config) -> Result<Vec<PendingApproval>> {
/// where the TTL elapses concurrently with a committed approval
/// (CodeRabbit review on PR #2367).
pub fn get_decision(config: &Config, request_id: &str) -> Result<Option<ApprovalDecision>> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.get_decision(request_id);
+ }
with_connection(config, |conn| {
let mut stmt = conn
.prepare(
@@ -333,6 +355,10 @@ pub fn decide(
request_id: &str,
decision: ApprovalDecision,
) -> Result<Option<PendingApproval>> {
+ if let Some(docs) = super::store_documents::current()? {
+ publish_expired(&docs.expire_stale(Utc::now())?);
+ return docs.decide(request_id, decision);
+ }
with_connection(config, |conn| {
expire_stale_with_now(conn, Utc::now())?;
@@ -389,6 +415,9 @@ pub fn record_execution(
outcome: ExecutionOutcome,
error: Option<&str>,
) -> Result<bool> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.record_execution(request_id, outcome, error);
+ }
with_connection(config, |conn| {
let now = Utc::now().to_rfc3339();
// Sanitize before truncation so the durable audit row can't
@@ -434,6 +463,9 @@ pub fn record_execution(
/// List recently decided approval rows for durable audit views.
pub fn list_recent_decisions(config: &Config, limit: usize) -> Result<Vec<ApprovalAuditEntry>> {
let limit = limit.clamp(1, 500);
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.list_recent_decisions(limit);
+ }
with_connection(config, |conn| {
let mut stmt = conn
.prepare(
@@ -459,6 +491,9 @@ pub fn list_recent_decisions(config: &Config, limit: usize) -> Result<Vec<Approv
/// Drop all rows owned by `session_id` — called when the gate detects
/// a session changeover so stale parked rows do not accumulate.
pub fn purge_session(config: &Config, session_id: &str) -> Result<usize> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.purge_session(session_id);
+ }
with_connection(config, |conn| {
let removed = conn
.execute(
@@ -546,18 +581,25 @@ fn expire_stale_with_now(conn: &Connection, now: DateTime<Utc>) -> Result<Vec<Pe
rows = updated,
"[approval::store] lazily expired stale pending_approvals rows"
);
- for row in &about_to_expire {
+ publish_expired(&about_to_expire);
+ Ok(about_to_expire)
+}
+
+/// Tells the web channel each expired row's parked card is now stale — a
+/// sweep runs with no live `ApprovalGate` in scope, so the store is the only
+/// place that observes an expiry.
+fn publish_expired(expired: &[PendingApproval]) {
+ for row in expired {
BUS.publish(DomainEvent::ApprovalDecided {
request_id: row.request_id.clone(),
tool_name: row.tool_name.clone(),
- decision: deny.to_string(),
+ decision: ApprovalDecision::Deny.as_str().to_string(),
thread_id: None,
client_id: None,
tool_call_id: row.tool_call_id.clone(),
resolution: Some("expired".to_string()),
});
}
- Ok(about_to_expire)
}
fn row_to_audit_entry(row: &rusqlite::Row<'_>) -> rusqlite::Result<ApprovalAuditEntry> {
diff --git a/crates/openhuman-core/src/security/approval/store_documents_tests.rs b/crates/openhuman-core/src/security/approval/store_documents_tests.rs
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/crates/openhuman-core/src/security/approval/store_flow_trust.rs b/crates/openhuman-core/src/security/approval/store_flow_trust.rs
index 60cfbfefb5..02f5edf1b3 100644
--- a/crates/openhuman-core/src/security/approval/store_flow_trust.rs
+++ b/crates/openhuman-core/src/security/approval/store_flow_trust.rs
@@ -15,6 +15,9 @@ use crate::config::Config;
use super::super::types::{ApprovalDecision, ApprovalSourceContext};
use super::with_connection;
+/// The action summary of a flow pre-authorization audit row.
+const PREAUTHORIZATION_SUMMARY: &str = "Pre-authorized for this flow when it was saved and enabled";
+
/// Record a save-time flow pre-authorization in the durable audit trail as a
/// born-decided row (`decided_at = created_at`, decision
/// `approve_always_for_flow`): it never appears in `list_pending` (which
@@ -29,6 +32,19 @@ pub fn record_flow_preauthorization(
tool_name: &str,
session_id: &str,
) -> Result<()> {
+ if let Some(docs) = super::super::store_documents::current()? {
+ return docs.insert_decided(
+ tool_name,
+ PREAUTHORIZATION_SUMMARY,
+ session_id,
+ &ApprovalSourceContext::Flow {
+ flow_id: flow_id.to_string(),
+ run_id: String::new(),
+ node_id: None,
+ },
+ ApprovalDecision::ApproveAlwaysForFlow,
+ );
+ }
with_connection(config, |conn| {
let now = Utc::now().to_rfc3339();
let source_context = serde_json::to_string(&ApprovalSourceContext::Flow {
@@ -46,7 +62,7 @@ pub fn record_flow_preauthorization(
params![
uuid::Uuid::new_v4().to_string(),
tool_name,
- "Pre-authorized for this flow when it was saved and enabled",
+ PREAUTHORIZATION_SUMMARY,
"{}",
session_id,
now,
@@ -64,6 +80,9 @@ pub fn record_flow_preauthorization(
/// flow-origin park. `INSERT OR IGNORE` makes re-granting an already-trusted
/// pair a harmless no-op rather than a primary-key error.
pub fn insert_flow_trust(config: &Config, flow_id: &str, tool_name: &str) -> Result<()> {
+ if let Some(docs) = super::super::store_documents::current()? {
+ return docs.insert_flow_trust(flow_id, tool_name);
+ }
with_connection(config, |conn| {
conn.execute(
"INSERT OR IGNORE INTO flow_tool_trust (flow_id, tool_name, created_at)
@@ -80,6 +99,9 @@ pub fn insert_flow_trust(config: &Config, flow_id: &str, tool_name: &str) -> Res
/// save-time pre-authorization manifest (`flows_approval_manifest`) to diff
/// "what the graph needs" against "what is already granted".
pub fn list_flow_trust(config: &Config, flow_id: &str) -> Result<Vec<String>> {
+ if let Some(docs) = super::super::store_documents::current()? {
+ return docs.list_flow_trust(flow_id);
+ }
with_connection(config, |conn| {
let mut stmt = conn
.prepare(
@@ -105,6 +127,9 @@ pub fn delete_flow_trust(
flow_id: &str,
tool_names: Option<&[String]>,
) -> Result<usize> {
+ if let Some(docs) = super::super::store_documents::current()? {
+ return docs.delete_flow_trust(flow_id, tool_names);
+ }
with_connection(config, |conn| {
let removed = match tool_names {
None => conn
@@ -135,6 +160,9 @@ pub fn delete_flow_trust(
/// this flow" trust. Consulted by [`super::gate::ApprovalGate::intercept_audited`]
/// before parking a `Workflow`-origin tool call.
pub fn is_flow_tool_trusted(config: &Config, flow_id: &str, tool_name: &str) -> Result<bool> {
+ if let Some(docs) = super::super::store_documents::current()? {
+ return docs.is_flow_tool_trusted(flow_id, tool_name);
+ }
with_connection(config, |conn| {
let exists: bool = conn
.query_row(