diff --git a/crates/openhuman-core/src/integrations/composio/flow_gate.rs b/crates/openhuman-core/src/integrations/composio/flow_gate.rs index 0e1d0424f15..4ab25f008fe 100644 --- a/crates/openhuman-core/src/integrations/composio/flow_gate.rs +++ b/crates/openhuman-core/src/integrations/composio/flow_gate.rs @@ -72,9 +72,10 @@ pub(crate) async fn flow_tool_allowed( tracing::debug!(target: "flows", %slug, %toolkit, "[flows] tool_call curation: reject — slug is not a curated action of this toolkit"); return false; }; + // A pref that cannot be read admits nothing (fail closed). let allowed = load_user_scope_pref(ctx, config, &toolkit) .await - .allows(curated.scope); + .is_ok_and(|pref| pref.allows(curated.scope)); tracing::debug!(target: "flows", %slug, %toolkit, allowed, "[flows] tool_call curation: static curated catalog decision"); return allowed; } @@ -102,7 +103,7 @@ pub(crate) async fn flow_tool_allowed( } let allowed = load_user_scope_pref(ctx, config, &toolkit) .await - .allows(classify_unknown(slug)); + .is_ok_and(|pref| pref.allows(classify_unknown(slug))); tracing::debug!(target: "flows", %slug, %toolkit, allowed, "[flows] tool_call curation: live catalog + scope decision"); allowed } diff --git a/crates/openhuman-core/src/integrations/composio/ops/mod.rs b/crates/openhuman-core/src/integrations/composio/ops/mod.rs index 7239b781656..1807563223e 100644 --- a/crates/openhuman-core/src/integrations/composio/ops/mod.rs +++ b/crates/openhuman-core/src/integrations/composio/ops/mod.rs @@ -59,7 +59,7 @@ pub use triggers::{ }; // The `composio.{get,set}_user_scopes` handlers' storage half. pub(crate) use scope_prefs::{ - ensure_migrated as ensure_user_scopes_migrated, load_or_default as load_user_scope_pref, + ensure_migrated as ensure_user_scopes_migrated, load as load_user_scope_pref, save as save_user_scope_pref, }; diff --git a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs index 6d01c184610..1d1d765aa06 100644 --- a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs +++ b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs.rs @@ -120,21 +120,24 @@ pub(crate) async fn ensure_migrated(ctx: &crate::core::runtime::DispatchCtx, con migrate_legacy(ctx, config).await; } -/// The scope pref stored for `toolkit`, or the default when none is. +/// The scope pref stored for `toolkit` (the module's default when none is). /// -/// **This read fails open**: a module that cannot be reached 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 error would break a working app. The -/// failure is logged. The **write** below does not fail open. -pub(crate) async fn load_or_default( +/// **This read fails closed**: an unreachable module or a stored row that +/// cannot be read is an `Err`, never the default. The default is `read+write`, +/// so answering it for a user who turned `write` off would widen what the agent +/// may run. Callers that gate on the pref treat an `Err` as "not allowed". +/// +/// # Errors +/// +/// An empty toolkit, or the module could not answer. +pub(crate) async fn load( ctx: &crate::core::runtime::DispatchCtx, config: &Config, toolkit: &str, -) -> UserScopePref { +) -> Result { let key = UserScopePref::key(toolkit); if key.is_empty() { - return UserScopePref::default(); + return Err("user_scopes: toolkit must not be empty".to_string()); } migrate_legacy(ctx, config).await; let request = ComposioGetUserScopesRequest { @@ -156,11 +159,11 @@ pub(crate) async fn load_or_default( admin = pref.admin, "{LOG} pref loaded" ); - pref + Ok(pref) } Err(error) => { - tracing::warn!(toolkit = %key, %error, "{LOG} pref unavailable, using default"); - UserScopePref::default() + tracing::warn!(toolkit = %key, %error, "{LOG} pref unavailable, failing closed"); + Err(error) } } } diff --git a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs_tests.rs b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs_tests.rs index 4e0a5b31d61..894dedaca4b 100644 --- a/crates/openhuman-core/src/integrations/composio/ops/scope_prefs_tests.rs +++ b/crates/openhuman-core/src/integrations/composio/ops/scope_prefs_tests.rs @@ -45,8 +45,8 @@ async fn save_rejects_an_empty_toolkit() { /// A stored preference is never lost: either the module took it (the read /// returns it and the stored value is emptied) or the module was unreachable -/// (the read fails open to the default and the value is left exactly as it was -/// so the migration retries). +/// (the read fails closed and the value is left exactly as it was so the +/// migration retries). #[tokio::test] async fn a_legacy_pref_either_reaches_the_module_or_stays_stored() { let dir = tempfile::tempdir().unwrap(); @@ -66,7 +66,7 @@ async fn a_legacy_pref_either_reaches_the_module_or_stays_stored() { admin: true, }; - let pref = load_or_default( + let pref = load( &crate::core::runtime::DispatchCtx::ambient(), &config, "github", @@ -81,18 +81,28 @@ async fn a_legacy_pref_either_reaches_the_module_or_stays_stored() { .await .unwrap(); if done { - assert_eq!(pref, stored, "the migrated row must decide visibility"); + assert_eq!(pref, Ok(stored), "the migrated row must decide visibility"); assert!(left.is_empty(), "the stored value is emptied once moved"); } else { - assert_eq!( - pref, - UserScopePref::default(), - "unreachable module fails open" + assert!( + pref.is_err(), + "an unreachable module fails closed: {pref:?}" ); assert_eq!(left.get("github"), Some(&stored)); } } +#[tokio::test] +async fn load_rejects_an_empty_toolkit_instead_of_answering_the_default() { + let dir = tempfile::tempdir().unwrap(); + let config = config_in(&dir); + assert!( + load(&crate::core::runtime::DispatchCtx::ambient(), &config, " ") + .await + .is_err() + ); +} + #[tokio::test] async fn a_workspace_with_no_legacy_file_is_marked_migrated() { let dir = tempfile::tempdir().unwrap(); diff --git a/crates/openhuman-core/src/integrations/composio/schemas/definitions.rs b/crates/openhuman-core/src/integrations/composio/schemas/definitions.rs index 43627be4556..ba66f33f773 100644 --- a/crates/openhuman-core/src/integrations/composio/schemas/definitions.rs +++ b/crates/openhuman-core/src/integrations/composio/schemas/definitions.rs @@ -291,7 +291,8 @@ pub fn schemas(function: &str) -> ControllerSchema { description: "Read the per-toolkit user scope preference (read/write/admin) used to gate \ composio_execute. Defaults to {read:true, write:true, admin:false} when no \ - pref is stored.", + pref is stored; errors (rather than answering the default) when the stored \ + pref cannot be read.", inputs: vec![FieldSchema { name: "toolkit", ty: TypeSchema::String, diff --git a/crates/openhuman-core/src/integrations/composio/schemas/handlers_identity.rs b/crates/openhuman-core/src/integrations/composio/schemas/handlers_identity.rs index b384ebefdab..2b30a7ea117 100644 --- a/crates/openhuman-core/src/integrations/composio/schemas/handlers_identity.rs +++ b/crates/openhuman-core/src/integrations/composio/schemas/handlers_identity.rs @@ -52,26 +52,23 @@ pub(super) fn handle_get_user_scopes( toolkit = %toolkit, "[composio:scopes] handler entry" ); - // Reads through the bound memory driver's `Graph` family, not through - // the engine's `load_or_default` (which resolved the in-process client - // itself — openhuman#5560 deleted that client). The read still fails - // OPEN onto the default pref for every failure mode, deliberately; see - // `ops::user_scopes`. - // - // The config load is `?`-free for the same reason: this handler has - // never had a failing path, and turning "settings file momentarily - // unreadable" into an RPC error would make the scopes panel fail where - // it used to render the defaults. - let pref = match ctx.load_config().await { - Ok(config) => ops::load_user_scope_pref(&ctx, &config, &toolkit).await, + // Fails closed: a pref the module cannot read is an error, not the + // default. The default is `read+write`, so showing it for a user who + // turned `write` off would misstate what the agent may run; the + // Connections panel renders the error instead. + let config = ctx.load_config().await?; + let pref = match ops::load_user_scope_pref(&ctx, &config, &toolkit).await { + Ok(pref) => pref, Err(error) => { tracing::warn!( method = "composio.get_user_scopes", toolkit = %toolkit, %error, - "[composio:scopes] config load failed, using default pref (read+write)" + "[composio:scopes] pref unavailable" ); - contract::UserScopePref::default() + return Err(format!( + "scope preference for `{toolkit}` is unavailable: {error}" + )); } }; tracing::debug!( diff --git a/crates/openhuman-core/src/integrations/composio/tools/execute.rs b/crates/openhuman-core/src/integrations/composio/tools/execute.rs index 63ee5fb0e69..d59db95cd93 100644 --- a/crates/openhuman-core/src/integrations/composio/tools/execute.rs +++ b/crates/openhuman-core/src/integrations/composio/tools/execute.rs @@ -227,7 +227,9 @@ impl ComposioExecuteTool { ToolDecision::Allow | ToolDecision::PassthroughCheckScope { .. } => {} ToolDecision::BlockedByScope { scope } => { let toolkit = toolkit_from_slug(&tool).unwrap_or_default(); - let pref = load_user_scope_pref(ctx, self.config.as_ref(), &toolkit).await; + let pref = load_user_scope_pref(ctx, self.config.as_ref(), &toolkit) + .await + .ok(); let msg = scope_error_message(&tool, scope, pref); tracing::info!( tool = %tool, 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 ff0001a0f69..06158033f7d 100644 --- a/crates/openhuman-core/src/integrations/composio/tools/scope_gate.rs +++ b/crates/openhuman-core/src/integrations/composio/tools/scope_gate.rs @@ -72,11 +72,19 @@ pub(crate) fn action_mutates_external_state(slug: &str) -> bool { /// Decide whether a Composio action slug should be visible / executable /// for the current user. /// +/// The preference applied when the stored one cannot be read: no scope on. +pub(super) const NOTHING_ALLOWED: UserScopePref = UserScopePref { + read: false, + write: false, + admin: false, +}; + /// Asks the connector module which curated actions the user's stored scope /// preference exposes for the slug's toolkit (`ListVisibleTools`), so the /// preference and the catalog are read from the module that enforces them. -/// A module that cannot be reached answers as the default preference would, -/// which is what the host did before the preference moved. +/// A module that cannot be reached fails closed: every scope is treated as +/// off, so nothing is allowed. The default preference (`read+write`) would +/// widen a user's `write=false`. pub(super) async fn evaluate_tool_visibility( ctx: &crate::core::runtime::DispatchCtx, config: &Config, @@ -98,11 +106,11 @@ pub(super) async fn evaluate_tool_visibility( { Ok(visible) => decide_from_visible(slug, &visible), Err(error) => { - tracing::debug!( + tracing::warn!( toolkit = %toolkit, - "[composio][scopes] list_visible_tools failed, using the default preference: {error}" + "[composio][scopes] list_visible_tools failed, failing closed: {error}" ); - decide_visibility(&toolkit, slug, UserScopePref::default()) + decide_visibility(&toolkit, slug, NOTHING_ALLOWED) } } } @@ -169,9 +177,26 @@ fn decide_visibility(toolkit: &str, slug: &str, pref: UserScopePref) -> ToolDeci /// toggles the scope in the Connections UI. The agent has no tool to /// flip scopes (see the note above the removed `ComposioEnableScopeTool` /// for why) — it can only describe the gate and point at the UI. -pub(super) fn scope_error_message(slug: &str, scope: ToolScope, pref: UserScopePref) -> String { +/// +/// `pref` is `None` when the stored preference could not be read. The scope +/// may well be on, and the Connections toggles are disabled until the read +/// works again, so that message reports the outage instead of the unlock path. +pub(super) fn scope_error_message( + slug: &str, + scope: ToolScope, + pref: Option, +) -> String { let toolkit = toolkit_from_slug(slug).unwrap_or_default(); let scope_str = scope.as_str(); + let Some(pref) = pref else { + return format!( + "composio_execute: action `{slug}` was not run because the user's scope \ + preferences for `{toolkit}` could not be read right now. This is a \ + temporary storage problem, not a disabled `{scope_str}` scope: tell the \ + user the action is unavailable for the moment and to try again shortly. \ + Do not tell them to change their Connections settings.", + ); + }; format!( "composio_execute: action `{slug}` is classified `{scope_str}` and is \ disabled in the user's current scope preferences for `{toolkit}` \ 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 index bfe33304de0..04b6b7f4908 100644 --- a/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs +++ b/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs @@ -17,6 +17,29 @@ fn a_curated_write_action_follows_the_write_flag() { )); } +/// What `evaluate_tool_visibility` applies when the module cannot answer: an +/// unreadable preference must not widen a user's `write=false` to the default +/// `read+write`. Nothing is allowed, curated or not, read or write. +#[test] +fn an_unreadable_preference_allows_nothing() { + assert!(matches!( + decide_visibility("gmail", "GMAIL_SEND_EMAIL", NOTHING_ALLOWED), + ToolDecision::BlockedByScope { + scope: ToolScope::Write + } + )); + assert!(matches!( + decide_visibility("flowstestkit", "FLOWSTESTKIT_SEND_THING", NOTHING_ALLOWED), + ToolDecision::BlockedByScope { + scope: ToolScope::Write + } + )); + assert!(matches!( + decide_visibility("flowstestkit", "FLOWSTESTKIT_GET_THING", NOTHING_ALLOWED), + ToolDecision::BlockedByScope { .. } + )); +} + #[test] fn an_action_outside_a_curated_catalog_is_not_curated() { let decision = decide_visibility("gmail", "GMAIL_NOT_A_REAL_ACTION", UserScopePref::default()); @@ -55,10 +78,18 @@ 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), + Some(only(true, false, false)), ); assert!(message.contains("Connections → gmail → write"), "{message}"); assert!(message.contains("write=false"), "{message}"); + + let unread = scope_error_message("GMAIL_SEND_EMAIL", ToolScope::Write, None); + assert!(unread.contains("could not be read"), "{unread}"); + assert!(!unread.contains("write="), "{unread}"); + assert!( + !unread.contains("Connections →"), + "an unreadable pref must not send the user to a disabled toggle: {unread}" + ); } fn visible( @@ -186,3 +217,79 @@ async fn a_preference_still_in_the_legacy_file_gates_execution() { } )); } + +/// Puts back whatever storage backend the process had when dropped. +struct RestoreInstalledStorage(Option>); + +impl Drop for RestoreInstalledStorage { + fn drop(&mut self) { + crate::storage::clear(); + if let Some(previous) = self.0.take() { + crate::storage::install(previous); + } + } +} + +/// The wiring, not only the fallback value: when the connector module cannot +/// read a stored preference, every gate that consults it denies. +/// +/// The module caches its link to the host's storage on first use. Saving a +/// preference while a backend is installed connects that link; removing the +/// backend then makes the module's next preference read fail (`Unavailable`), +/// as a storage outage would. +#[tokio::test] +#[allow( + clippy::await_holding_lock, + reason = "the storage-slot guard must cover the whole test; the runtime is current-thread" +)] +async fn an_unreadable_preference_denies_at_every_gate() { + use crate::integrations::composio::module_client::module_guard; + let _serialised = module_guard().await; + let _slot = crate::storage::test_support::lock_storage_slot(); + let ctx = crate::core::runtime::DispatchCtx::ambient(); + let tmp = tempfile::tempdir().unwrap(); + let mut config = Config::default(); + config.workspace_dir = tmp.path().join("workspace"); + config.config_path = tmp.path().join("config.toml"); + + let restore = RestoreInstalledStorage(crate::storage::install( + crate::storage::test_support::memory_backend(), + )); + crate::integrations::composio::ops::save_user_scope_pref( + &ctx, + &config, + "gmail", + only(true, false, false), + ) + .await + .expect("the module stores the pref while storage is installed"); + crate::storage::clear(); + + let read = + crate::integrations::composio::ops::load_user_scope_pref(&ctx, &config, "gmail").await; + assert!( + read.is_err(), + "an unreadable pref is an error, not the default: {read:?}" + ); + assert!( + !crate::integrations::composio::flow_gate::flow_tool_allowed( + &ctx, + &config, + "GMAIL_SEND_EMAIL", + None + ) + .await, + "the flows gate must not admit a write action on an unreadable pref" + ); + let decision = evaluate_tool_visibility(&ctx, &config, "GMAIL_SEND_EMAIL").await; + assert!( + matches!( + decision, + ToolDecision::BlockedByScope { + scope: ToolScope::Write + } + ), + "the execute gate must block a write action on an unreadable pref" + ); + drop(restore); +}