Skip to content

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

fix(composio): gate tool execution on scope checks

Steven Enamakel0c99f72aec9b
diff --git a/crates/openhuman-core/src/integrations/composio/ops/tools_ops.rs b/crates/openhuman-core/src/integrations/composio/ops/tools_ops.rs
index 563e47bdec..1676e8ade1 100644
--- a/crates/openhuman-core/src/integrations/composio/ops/tools_ops.rs
+++ b/crates/openhuman-core/src/integrations/composio/ops/tools_ops.rs
@@ -23,17 +23,10 @@ use tinyconnectors_bus::names::methods;
/// one component deciding both is the only arrangement that cannot disagree
/// with itself.
///
-/// It cannot yet, because the preference is not stored where the module can
-/// see it. The rows live in the bound memory driver's KV tier
-/// (`super::user_scopes`), and the module reads its own state directory. Asking
-/// it to filter today would filter against *its* defaults — `read+write`, no
-/// `admin` — rather than against what the user actually chose, and the two
-/// would disagree the moment anyone touched the toggle.
-///
-/// So enforcement stays whole on this side for now: `super::super::tools`
-/// applies the preference to what the agent sees, which is the path that leads
-/// to an action being run. Phase 4 moves the store, and this flag flips with
-/// it.
+/// The preference now lives in the module (`GetUserScopes`), so the two
+/// would agree; the flag stays `false` because the host's scope gate still
+/// applies the preference to what the agent sees (and refuses the execution),
+/// and filtering twice would only hide the host's "blocked by scope" message.
pub async fn composio_list_tools(
config: &Config,
toolkits: Option<Vec<String>>,
diff --git a/crates/openhuman-core/src/integrations/composio/tools/scope_gate.rs b/crates/openhuman-core/src/integrations/composio/tools/scope_gate.rs
index 386a0fa674..aaec221a6f 100644
--- a/crates/openhuman-core/src/integrations/composio/tools/scope_gate.rs
+++ b/crates/openhuman-core/src/integrations/composio/tools/scope_gate.rs
@@ -75,12 +75,14 @@ pub(super) async fn evaluate_tool_visibility(config: &Config, slug: &str) -> Too
return ToolDecision::Allow;
};
let pref = load_user_scope_pref(config, &toolkit).await;
- // The catalog covers every catalogued toolkit directly now — the
- // engine's `get_provider(toolkit).curated_tools()` hop this used to
- // prefer was pure indirection, verified identical to `catalog_for_toolkit`
- // for every toolkit that had a native provider.
- let catalog = catalog_for_toolkit(&toolkit);
- match catalog {
+ decide_visibility(&toolkit, slug, pref)
+}
+
+/// The pure half of [`evaluate_tool_visibility`], given the toolkit's stored
+/// preference. The catalog is the connector module's, read from the host's
+/// curated snapshot.
+fn decide_visibility(toolkit: &str, slug: &str, pref: UserScopePref) -> ToolDecision {
+ match catalog_for_toolkit(toolkit) {
Some(catalog) => match find_curated(&catalog, slug) {
Some(curated) if pref.allows(curated.scope) => ToolDecision::Allow,
Some(curated) => ToolDecision::BlockedByScope {
@@ -111,9 +113,26 @@ pub(super) async fn filter_list_tools_response(
// can't fold this into a single sync `retain` closure). Then zip
// each tool with its decision and collect the survivors — clearer
// than juggling a parallel index alongside `Vec::retain`.
+ // One preference read per toolkit, not per tool: each read is a bus call.
+ let mut prefs: std::collections::HashMap<String, UserScopePref> =
+ std::collections::HashMap::new();
let mut keep: Vec<bool> = Vec::with_capacity(before);
for t in &resp.tools {
- let decision = evaluate_tool_visibility(config, &t.function.name).await;
+ let slug = &t.function.name;
+ let decision = match toolkit_from_slug(slug) {
+ None => ToolDecision::Allow,
+ Some(toolkit) => {
+ let pref = match prefs.get(&toolkit) {
+ Some(pref) => *pref,
+ None => {
+ let pref = load_user_scope_pref(config, &toolkit).await;
+ prefs.insert(toolkit.clone(), pref);
+ pref
+ }
+ };
+ decide_visibility(&toolkit, slug, pref)
+ }
+ };
keep.push(matches!(
decision,
ToolDecision::Allow | ToolDecision::PassthroughCheckScope { .. }
@@ -157,3 +176,7 @@ pub(super) fn scope_error_message(slug: &str, scope: ToolScope, pref: UserScopeP
pref.read, pref.write, pref.admin,
)
}
+
+#[cfg(test)]
+#[path = "scope_gate_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs b/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs
new file mode 100644
index 0000000000..610b5568ae
--- /dev/null
+++ b/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs
@@ -0,0 +1,50 @@
+use super::*;
+
+fn only(read: bool, write: bool, admin: bool) -> UserScopePref {
+ UserScopePref { read, write, admin }
+}
+
+#[test]
+fn a_curated_write_action_follows_the_write_flag() {
+ let allowed = decide_visibility("gmail", "GMAIL_SEND_EMAIL", only(true, true, false));
+ assert!(matches!(allowed, ToolDecision::Allow));
+ let blocked = decide_visibility("gmail", "GMAIL_SEND_EMAIL", only(true, false, false));
+ assert!(matches!(
+ blocked,
+ ToolDecision::BlockedByScope {
+ scope: ToolScope::Write
+ }
+ ));
+}
+
+#[test]
+fn an_action_outside_a_curated_catalog_is_not_curated() {
+ let decision = decide_visibility("gmail", "GMAIL_NOT_A_REAL_ACTION", UserScopePref::default());
+ assert!(matches!(decision, ToolDecision::NotCurated));
+}
+
+#[test]
+fn an_uncatalogued_toolkit_falls_back_to_the_verb_heuristic() {
+ let read = decide_visibility("flowstestkit", "FLOWSTESTKIT_GET_THING", only(true, false, false));
+ assert!(matches!(read, ToolDecision::PassthroughCheckScope { .. }));
+ let admin = decide_visibility("flowstestkit", "FLOWSTESTKIT_DELETE_THING", only(true, true, false));
+ assert!(matches!(
+ admin,
+ ToolDecision::BlockedByScope {
+ scope: ToolScope::Admin
+ }
+ ));
+}
+
+#[test]
+fn unknown_slugs_default_to_write_for_the_sandbox_gate() {
+ assert_eq!(resolve_action_scope_sync(" "), ToolScope::Write);
+ assert!(action_mutates_external_state(" "));
+}
+
+#[test]
+fn the_scope_error_names_the_toolkit_and_the_unlock_path() {
+ let message = scope_error_message("GMAIL_SEND_EMAIL", ToolScope::Write, only(true, false, false));
+ assert!(message.contains("Connections → gmail → write"), "{message}");
+ assert!(message.contains("write=false"), "{message}");
+}