Skip to content

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

feat(composio): add user scope filtering for tool execution

Steven Enamakelfdd5a6f8dc31
diff --git a/crates/openhuman-core/src/integrations/composio/ops/user_scopes.rs b/crates/openhuman-core/src/integrations/composio/ops/user_scopes.rs
deleted file mode 100644
index 1a635bf116..0000000000
--- a/crates/openhuman-core/src/integrations/composio/ops/user_scopes.rs
+++ /dev/null
@@ -1,111 +0,0 @@
-//! Per-toolkit agent scope preferences, stored in
-//! `<workspace>/integrations/composio_user_scopes.json` through
-//! [`crate::integrations::composio::file_store`].
-//!
-//! The file maps a normalised toolkit slug to its [`UserScopePref`]. The
-//! `composio.{get,set}_user_scopes` handlers, the agent's visible tool list
-//! and the flows capability gate all read through [`load_or_default`].
-
-use std::collections::BTreeMap;
-
-use crate::config::Config;
-use crate::integrations::composio::file_store::{self, USER_SCOPES_FILE};
-
-/// The stored type: the contract's scope pref, which is also the wire shape of
-/// both handlers' replies.
-pub(crate) use crate::integrations::composio::contract::UserScopePref;
-
-/// The stored map: normalised toolkit slug → pref.
-type ScopeFile = BTreeMap<String, UserScopePref>;
-
-/// The row key for a toolkit — trimmed and ASCII-lowercased, so `"GitHub"`,
-/// `" github "` and `"github"` reach one row.
-fn kv_key(toolkit: &str) -> String {
- toolkit.trim().to_ascii_lowercase()
-}
-
-/// The scope pref stored for `toolkit`, or the default when none is.
-///
-/// **This read fails open**: an unreadable file lands on the same default
-/// (`read+write`, no `admin`) a user who never opened the toggle gets. A failed
-/// read is not evidence the user revoked anything, and refusing every
-/// integration on a transient I/O error would break a working app. The
-/// failure is logged so "defaulted because nothing is stored" stays
-/// distinguishable from "defaulted because the store is unreadable". The
-/// **write** below does not fail open — see [`save`].
-pub(crate) async fn load_or_default(config: &Config, toolkit: &str) -> UserScopePref {
- let key = kv_key(toolkit);
- if key.is_empty() {
- return UserScopePref::default();
- }
- let path = file_store::path(config, USER_SCOPES_FILE);
- match file_store::load::<ScopeFile>(&path).await {
- Ok(mut prefs) => match prefs.remove(&key) {
- Some(pref) => {
- tracing::debug!(
- toolkit = %key,
- read = pref.read,
- write = pref.write,
- admin = pref.admin,
- "[composio][scopes] pref loaded"
- );
- pref
- }
- None => {
- tracing::debug!(
- toolkit = %key,
- "[composio][scopes] no pref stored, using default (read+write)"
- );
- UserScopePref::default()
- }
- },
- Err(error) => {
- tracing::warn!(
- toolkit = %key,
- %error,
- "[composio][scopes] pref store unreadable, falling back to default"
- );
- UserScopePref::default()
- }
- }
-}
-
-/// Persist the scope pref for `toolkit`.
-///
-/// **This one fails closed.** A write that reported success without storing
-/// anything would leave the user looking at a toggle they just moved while the
-/// agent kept the old permissions. Every failure is an `Err` the RPC surfaces.
-///
-/// # Errors
-///
-/// An empty toolkit, or the scope file cannot be read or written.
-pub(crate) async fn save(
- config: &Config,
- toolkit: &str,
- pref: UserScopePref,
-) -> Result<(), String> {
- let key = kv_key(toolkit);
- if key.is_empty() {
- return Err("user_scopes: toolkit must not be empty".to_string());
- }
- let path = file_store::path(config, USER_SCOPES_FILE);
- let entry_key = key.clone();
- file_store::update(&path, move |prefs: &mut ScopeFile| {
- prefs.insert(entry_key.clone(), pref);
- ((), true)
- })
- .await?;
-
- tracing::info!(
- toolkit = %key,
- read = pref.read,
- write = pref.write,
- admin = pref.admin,
- "[composio][scopes] pref saved"
- );
- Ok(())
-}
-
-#[cfg(test)]
-#[path = "user_scopes_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/integrations/composio/ops/user_scopes_tests.rs b/crates/openhuman-core/src/integrations/composio/ops/user_scopes_tests.rs
deleted file mode 100644
index ba9dfe0c6c..0000000000
--- a/crates/openhuman-core/src/integrations/composio/ops/user_scopes_tests.rs
+++ /dev/null
@@ -1,85 +0,0 @@
-use super::*;
-
-fn config_in(dir: &tempfile::TempDir) -> Config {
- Config {
- workspace_dir: dir.path().to_path_buf(),
- ..Config::default()
- }
-}
-
-/// The RPC takes free text from a settings toggle, so `"GitHub"`,
-/// `" github "` and `"github"` have to reach one row.
-#[test]
-fn kv_key_normalises_the_toolkit() {
- assert_eq!(kv_key(" GitHub "), "github");
- assert_eq!(kv_key("SLACK"), "slack");
- assert_eq!(kv_key("gmail"), "gmail");
- assert_eq!(kv_key(" "), "", "an all-whitespace toolkit has no row");
-}
-
-/// The stored JSON is the three boolean fields.
-#[test]
-fn stored_value_is_the_three_boolean_fields() {
- let value = serde_json::to_value(UserScopePref {
- read: true,
- write: false,
- admin: true,
- })
- .expect("UserScopePref serialises");
- assert_eq!(
- value,
- serde_json::json!({ "read": true, "write": false, "admin": true })
- );
-}
-
-/// The default a failed or absent read falls back to: productive, not
-/// permissive-with-admin.
-#[test]
-fn default_pref_is_read_write_without_admin() {
- let pref = UserScopePref::default();
- assert!(pref.read);
- assert!(pref.write);
- assert!(!pref.admin, "admin must stay opt-in");
-}
-
-#[tokio::test]
-async fn save_then_load_round_trips_per_toolkit() {
- let dir = tempfile::tempdir().unwrap();
- let config = config_in(&dir);
- let pref = UserScopePref {
- read: true,
- write: false,
- admin: true,
- };
- save(&config, "GitHub", pref).await.unwrap();
-
- let loaded = load_or_default(&config, " github ").await;
- assert_eq!(
- serde_json::to_value(loaded).unwrap(),
- serde_json::to_value(pref).unwrap()
- );
- let other = load_or_default(&config, "slack").await;
- assert!(other.read && other.write && !other.admin);
-}
-
-#[tokio::test]
-async fn save_rejects_an_empty_toolkit() {
- let dir = tempfile::tempdir().unwrap();
- let config = config_in(&dir);
- assert!(save(&config, " ", UserScopePref::default()).await.is_err());
-}
-
-#[tokio::test]
-async fn unreadable_store_fails_open_to_the_default() {
- let dir = tempfile::tempdir().unwrap();
- let config = config_in(&dir);
- let path = file_store::path(&config, USER_SCOPES_FILE);
- std::fs::create_dir_all(path.parent().unwrap()).unwrap();
- std::fs::write(&path, b"not json").unwrap();
-
- let pref = load_or_default(&config, "github").await;
- assert!(pref.read && pref.write && !pref.admin);
- assert!(save(&config, "github", UserScopePref::default())
- .await
- .is_err());
-}
diff --git a/crates/openhuman-core/src/integrations/composio/tools/execute.rs b/crates/openhuman-core/src/integrations/composio/tools/execute.rs
index 4696a66e44..54ac12bf32 100644
--- a/crates/openhuman-core/src/integrations/composio/tools/execute.rs
+++ b/crates/openhuman-core/src/integrations/composio/tools/execute.rs
@@ -17,7 +17,7 @@ use tinytools::{PermissionLevel, Tool, ToolCallOptions, ToolCategory, ToolResult
use super::super::contract::{toolkit_from_slug, ToolScope};
use super::super::ops::load_user_scope_pref;
-use super::visibility::{
+use super::scope_gate::{
action_mutates_external_state, evaluate_tool_visibility, resolve_action_scope,
scope_error_message, ToolDecision,
};
diff --git a/crates/openhuman-core/src/integrations/composio/tools/list_tools.rs b/crates/openhuman-core/src/integrations/composio/tools/list_tools.rs
index b92a90d67f..759a7114d3 100644
--- a/crates/openhuman-core/src/integrations/composio/tools/list_tools.rs
+++ b/crates/openhuman-core/src/integrations/composio/tools/list_tools.rs
@@ -17,10 +17,11 @@ use super::super::module_client::{self as connectors};
use super::super::types::{
ComposioConnectionsResponse, ComposioListToolsRequest, ComposioToolsResponse,
};
-use super::visibility::{
- empty_uncurated_toolkits_message, filter_list_tools_response, normalized_scope_toolkits,
- render_tools_markdown, retain_connected_tools,
+use super::render::{
+ empty_uncurated_toolkits_message, normalized_scope_toolkits, render_tools_markdown,
+ retain_connected_tools,
};
+use super::scope_gate::filter_list_tools_response;
use tinyconnectors_bus::names::methods;
pub struct ComposioListToolsTool {