Skip to content

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

fix(composio): handle missing tool execution result gracefully

Steven Enamakel7d98758fa85e
diff --git a/crates/openhuman-core/src/integrations/composio/tools/execute.rs b/crates/openhuman-core/src/integrations/composio/tools/execute.rs
new file mode 100644
index 0000000000..fc9b73edc3
--- /dev/null
+++ b/crates/openhuman-core/src/integrations/composio/tools/execute.rs
@@ -0,0 +1,346 @@
+//! The `composio_execute` agent tool: sandbox/scope gating, calendar and
+//! task-recency argument defaults, dispatch through
+//! [`super::super::execute_dispatch`], and response shaping.
+
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use serde_json::{json, Value};
+
+use crate::agent::harness::current_sandbox_mode;
+use crate::agent::harness::current_task_recency_window;
+use crate::agent::harness::definition::SandboxMode;
+use crate::config::rpc as config_rpc;
+use crate::config::Config;
+use crate::tools::traits::{PermissionLevel, Tool, ToolCategory, ToolResult};
+
+use super::super::client::create_composio_client;
+use super::super::ops::load_user_scope_pref;
+use super::super::providers::{toolkit_from_slug, ToolScope};
+use super::visibility::{action_mutates_external_state, evaluate_tool_visibility, resolve_action_scope, scope_error_message};
+use super::visibility::ToolDecision;
+
+pub struct ComposioExecuteTool {
+ /// Held instead of a pre-baked `ComposioClient` so the
+ /// [`crate::config::ComposioConfig::mode`] toggle is
+ /// honoured on every call.
+ ///
+ /// The earlier shape stored a backend-bound `ComposioClient` baked
+ /// at agent boot. When the user toggled
+ /// `composio.mode = "direct"` mid-session the
+ /// `ComposioConfigChanged` event invalidated caches, but this tool's
+ /// pre-baked client kept routing executions through
+ /// `staging-api.tinyhumans.ai/agent-integrations/composio/execute`
+ /// — silently bypassing the direct-mode user's personal Composio
+ /// tenant. Resolving the client per call via
+ /// [`create_composio_client`] keeps dispatch in lockstep with the
+ /// live config, matching
+ /// [`crate::integrations::composio::ops::composio_execute`]. See
+ /// issue #1710.
+ config: Arc<Config>,
+}
+
+impl ComposioExecuteTool {
+ pub fn new(config: Arc<Config>) -> Self {
+ Self { config }
+ }
+}
+
+
+#[async_trait]
+impl Tool for ComposioExecuteTool {
+ fn name(&self) -> &str {
+ "composio_execute"
+ }
+ fn description(&self) -> &str {
+ "Execute a Composio action by slug. `tool` is the action slug returned from \
+ composio_list_tools (e.g. 'GMAIL_SEND_EMAIL'); `arguments` is an object that \
+ conforms to that tool's parameter schema. Returns the provider result plus \
+ cost (USD)."
+ }
+ fn parameters_schema(&self) -> Value {
+ json!({
+ "type": "object",
+ "properties": {
+ "tool": {
+ "type": "string",
+ "description": "Composio action slug, e.g. 'GMAIL_SEND_EMAIL'."
+ },
+ "arguments": {
+ "type": "object",
+ "description": "Action-specific arguments. Shape depends on the tool."
+ },
+ "connection_id": {
+ "type": "string",
+ "description": "Optional. Target a specific account when multiple are connected for a toolkit. Use the connection_id from '## Connected Integrations'. Omit to use the default account."
+ }
+ },
+ "required": ["tool"],
+ "additionalProperties": false
+ })
+ }
+ fn permission_level(&self) -> PermissionLevel {
+ // Some composio actions send emails, create files, etc. — treat
+ // as write-level to respect channel permission caps.
+ PermissionLevel::Write
+ }
+ fn external_effect(&self) -> bool {
+ true
+ }
+ fn external_effect_with_args(&self, args: &Value) -> bool {
+ args.get("tool")
+ .and_then(Value::as_str)
+ .map(str::trim)
+ .filter(|slug| !slug.is_empty())
+ .map(action_mutates_external_state)
+ .unwrap_or(true)
+ }
+ fn category(&self) -> ToolCategory {
+ ToolCategory::Workflow
+ }
+ async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {
+ let tool = args
+ .get("tool")
+ .and_then(|v| v.as_str())
+ .unwrap_or("")
+ .trim()
+ .to_string();
+ if tool.is_empty() {
+ return Ok(ToolResult::error(
+ "composio_execute: 'tool' is required (e.g. GMAIL_SEND_EMAIL)",
+ ));
+ }
+ let arguments = args.get("arguments").cloned();
+ let connection_id = args
+ .get("connection_id")
+ .and_then(|v| v.as_str())
+ .unwrap_or("");
+ // INFO-level entry log — visible without bumping log level. Logs
+ // ARG KEYS only by default so traces don't leak email bodies / PII;
+ // the full arg JSON goes through a paired DEBUG log below for deep
+ // debugging. Together these let `grep "[composio][execute]"` show
+ // the full agent→backend audit trail at default verbosity.
+ let arg_keys: Vec<&str> = arguments
+ .as_ref()
+ .and_then(|v| v.as_object())
+ .map(|obj| obj.keys().map(|k| k.as_str()).collect())
+ .unwrap_or_default();
+ tracing::info!(
+ tool = %tool,
+ connection_id = %connection_id,
+ arg_keys = ?arg_keys,
+ "[composio][execute] >> dispatch"
+ );
+ if let Some(ref args_json) = arguments {
+ tracing::debug!(
+ tool = %tool,
+ args = %args_json,
+ "[composio][execute] >> full args (DEBUG)"
+ );
+ }
+
+ // Agent-level sandbox gate (issue #685) — applies on top of the
+ // user's scope preference below. When the currently-executing
+ // agent declares `sandbox_mode = "read_only"` in its
+ // `agent.toml`, we refuse to dispatch any Write- or Admin-scoped
+ // composio action regardless of what the user's scope pref
+ // allows, so a strictly-read-only agent (planner, critic,
+ // morning_briefing, …) can never mutate user state via the
+ // composio surface. `SandboxMode::None` / `Sandboxed` (and the
+ // `None` task-local value used by direct CLI / JSON-RPC / unit
+ // tests) pass through unchanged.
+ if matches!(current_sandbox_mode(), Some(SandboxMode::ReadOnly)) {
+ let scope = resolve_action_scope(&tool).await;
+ if matches!(scope, ToolScope::Write | ToolScope::Admin) {
+ tracing::info!(
+ tool = %tool,
+ scope = scope.as_str(),
+ "[composio][sandbox] execute blocked: agent is read-only, action is {}",
+ scope.as_str()
+ );
+ return Ok(ToolResult::error(format!(
+ "composio_execute: action `{tool}` is classified `{}` and is refused \
+ because the calling agent is in strict read-only mode. Only `read`-scoped \
+ actions are available to this agent.",
+ scope.as_str()
+ )));
+ }
+ }
+
+ // Enforce per-user scope preferences before delegating to backend.
+ // Uses `self.config` rather than the live-reloaded config resolved
+ // further down: a scope-pref read is not the direct-mode-toggle
+ // hazard the composio client resolution below guards against, and
+ // restructuring the reload earlier would move it ahead of the
+ // sandbox/task-window setup this function does first.
+ match evaluate_tool_visibility(self.config.as_ref(), &tool).await {
+ ToolDecision::Allow | ToolDecision::PassthroughCheckScope { .. } => {}
+ ToolDecision::BlockedByScope { scope } => {
+ let toolkit = toolkit_from_slug(&tool).unwrap_or_default();
+ let pref = load_user_scope_pref(self.config.as_ref(), &toolkit).await;
+ let msg = scope_error_message(&tool, scope, pref);
+ tracing::info!(
+ tool = %tool,
+ toolkit = %toolkit,
+ scope = scope.as_str(),
+ "[composio][scopes] execute blocked by user scope pref"
+ );
+ return Ok(ToolResult::error(msg));
+ }
+ ToolDecision::NotCurated => {
+ let toolkit = toolkit_from_slug(&tool).unwrap_or_default();
+ tracing::info!(
+ tool = %tool,
+ toolkit = %toolkit,
+ "[composio][scopes] execute blocked: action not in curated whitelist"
+ );
+ return Ok(ToolResult::error(format!(
+ "composio_execute: action `{tool}` is not in the curated whitelist for \
+ toolkit `{toolkit}`. Use composio_list_tools to see available actions."
+ )));
+ }
+ }
+
+ // Inject `timeZone` / `singleEvents` defaults for Google
+ // Calendar list slugs so the host's IANA zone reaches the API
+ // regardless of how the model built the args (issue #1714).
+ // No-op for every other slug; respects caller-supplied values.
+ let iana = super::googlecalendar_args::current_iana_timezone();
+ tracing::debug!(
+ target: "composio",
+ slug = %tool,
+ iana = %iana,
+ "[composio][dispatcher] applying calendar query defaults pre-dispatch"
+ );
+ let arguments =
+ super::googlecalendar_args::apply_calendar_query_defaults(&tool, arguments, &iana);
+
+ // Task-recency window (morning briefing): when the calling agent
+ // installed a window, inject best-effort server-side narrowing for
+ // curated task-fetch slugs. No-op for every other slug and for
+ // normal chat / CLI / JSON-RPC (window unset → None). The
+ // authoritative enforcement is the post-filter on the response below.
+ let task_window_since = current_task_recency_window().map(|w| {
+ chrono::Utc::now()
+ - chrono::Duration::from_std(w).unwrap_or_else(|_| chrono::Duration::zero())
+ });
+ let arguments = match task_window_since {
+ Some(since) => super::task_window::apply_window_args(&tool, arguments, since),
+ None => arguments,
+ };
+
+ // Resolve the client through the mode-aware factory on every
+ // call so a direct-mode toggle takes effect immediately
+ // (#1710). The pre-baked-client variant of this code routed all
+ // executions through the backend tinyhumans tenant regardless
+ // of mode — silently breaking direct mode for tool execution.
+ // [#1710 Wave 4] Reload config fresh per execute so a mid-session
+ // `composio.mode` toggle takes effect at the very next tool call.
+ // Anchor the reload to this tool's original config path rather
+ // than re-resolving process-global `OPENHUMAN_WORKSPACE`; the
+ // tool is scoped to the user/workspace it was created for.
+ let live_config = match config_rpc::reload_config_snapshot_with_timeout(
+ self.config.as_ref(),
+ )
+ .await
+ {
+ Ok(c) => c,
+ Err(e) => {
+ tracing::warn!(error = %e, "[composio] tool execute.execute: load_config failed");
+ return Ok(ToolResult::error(format!(
+ "composio_execute: failed to load live config: {e}"
+ )));
+ }
+ };
+ let kind = match create_composio_client(&live_config) {
+ Ok(kind) => kind,
+ Err(e) => {
+ tracing::warn!(error = %e, "[composio] tool execute.execute: factory failed");
+ return Ok(ToolResult::error(format!("composio_execute failed: {e}")));
+ }
+ };
+
+ let started = std::time::Instant::now();
+ // Centralized prepare → retry → error-mapping pipeline (#1797),
+ // mode-aware over the backend/direct split (#1710).
+ let res = super::execute_dispatch::execute_composio_action_kind(
+ kind,
+ &tool,
+ arguments,
+ &live_config.composio.entity_id,
+ )
+ .await;
+ let elapsed_ms = started.elapsed().as_millis() as u64;
+ match res {
+ Ok(resp) => {
+ // Authoritative task-recency enforcement: drop task rows older
+ // than the window. No-op unless a window is installed AND the
+ // slug is a curated task-fetch action. Runs before the
+ // markdown/JSON body decision so the agent reads filtered data.
+ let resp = match task_window_since {
+ Some(since) => super::task_window::filter_response(&tool, resp, since),
+ None => resp,
+ };
+ tracing::info!(
+ tool = %tool,
+ successful = resp.successful,
+ error = ?resp.error,
+ elapsed_ms,
+ cost_usd = resp.cost_usd,
+ "[composio][execute] << result"
+ );
+ tracing::debug!(
+ tool = %tool,
+ response = ?resp,
+ "[composio][execute] << full response (DEBUG)"
+ );
+ crate::core::bus::BUS.publish(
+ crate::core::events::DomainEvent::ComposioActionExecuted {
+ tool: tool.clone(),
+ success: resp.successful,
+ error: resp.error.clone(),
+ cost_usd: resp.cost_usd,
+ elapsed_ms,
+ },
+ );
+ // Prefer the backend-rendered markdown when available
+ // (tinyhumansai/backend#683). The backend handles parsing
+ // for all composio actions; if a tool isn't formatted
+ // server-side `markdown_formatted` is None and we fall
+ // back to the raw JSON envelope.
+ let body = if resp.successful {
+ match resp
+ .markdown_formatted
+ .as_deref()
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ {
+ Some(md) => md.to_string(),
+ None => serde_json::to_string(&resp).unwrap_or_else(|_| "{}".into()),
+ }
+ } else {
+ serde_json::to_string(&resp).unwrap_or_else(|_| "{}".into())
+ };
+ Ok(ToolResult::success(body))
+ }
+ Err(e) => {
+ tracing::warn!(
+ tool = %tool,
+ error = %e,
+ elapsed_ms,
+ "[composio][execute] << dispatch error"
+ );
+ crate::core::bus::BUS.publish(
+ crate::core::events::DomainEvent::ComposioActionExecuted {
+ tool: tool.clone(),
+ success: false,
+ error: Some(e.to_string()),
+ cost_usd: 0.0,
+ elapsed_ms,
+ },
+ );
+ Ok(ToolResult::error(e))
+ }
+ }
+ }
+}