Skip to content

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

refactor(composio): extract connection ref validation into contract module

Steven Enamakel26837c79ab77
diff --git a/crates/openhuman-core/src/flows/ops/connection_ref_gate.rs b/crates/openhuman-core/src/flows/ops/connection_ref_gate.rs
index 5fe82d5e99..bde19edd12 100644
--- a/crates/openhuman-core/src/flows/ops/connection_ref_gate.rs
+++ b/crates/openhuman-core/src/flows/ops/connection_ref_gate.rs
@@ -17,35 +17,6 @@ use super::*;
// the live connection list is reachable — that the id names a real connected
// account of that toolkit, naming the correct ref when it can.
-/// Parses a `composio:<toolkit>:<id>` connection_ref into its `(toolkit, id)`
-/// segments. Mirrors [`crate::flows::tinyflows::caps::composio_connection_id`]'s
-/// rsplit for the id (everything after the LAST `:`), taking everything between
-/// the `composio:` prefix and that last `:` as the toolkit. Returns `None` for
-/// anything that isn't this shape (missing `composio:` prefix, no `:` after it,
-/// or an empty toolkit/id segment).
-fn parse_composio_connection_ref(conn_ref: &str) -> Option<(&str, &str)> {
- let rest = conn_ref.strip_prefix("composio:")?;
- let (toolkit, id) = rest.rsplit_once(':')?;
- if toolkit.trim().is_empty() || id.trim().is_empty() {
- return None;
- }
- Some((toolkit.trim(), id.trim()))
-}
-
-/// First connected account `connection_ref` for `toolkit` (case-insensitive)
-/// from `conns`, used to name the correct ref in a rejection's "did you mean"
-/// hint. `None` when the toolkit has no connection at all.
-fn first_connection_ref_for_toolkit(conns: &[FlowConnection], toolkit: &str) -> Option<String> {
- conns
- .iter()
- .find(|c| {
- c.toolkit
- .as_deref()
- .is_some_and(|t| t.eq_ignore_ascii_case(toolkit))
- })
- .map(|c| c.connection_ref.clone())
-}
-
/// Hard gate: for every Composio `tool_call` node carrying a `connection_ref`,
/// prove the ref names a real connected account of the SAME toolkit as the
/// slug. Fetches the live connection list once (same source
@@ -93,156 +64,27 @@ pub(super) fn validate_connection_refs_against(
graph: &WorkflowGraph,
connections: Option<&[FlowConnection]>,
) -> Vec<String> {
- use crate::integrations::composio::contract::toolkit_from_slug;
-
- let mut errors = Vec::new();
- for node in &graph.nodes {
- if node.kind != NodeKind::ToolCall {
- continue;
- }
- let Some(slug) = node.config.get("slug").and_then(Value::as_str) else {
- continue;
- };
- // `=`-derived slugs resolve at runtime; native `oh:` tools have no
- // Composio connection to name.
- if slug.starts_with('=') || slug.starts_with("oh:") {
- continue;
- }
- // A MISSING `connection_ref` stays allowed (unchanged): a Composio
- // tool_call with no ref runs against the ambient signed-in account and
- // the flow prompts for a connection at first run.
- let Some(conn_ref) = node.config.get("connection_ref").and_then(Value::as_str) else {
- continue;
- };
- if conn_ref.trim().is_empty() {
- continue;
- }
- let Some(slug_toolkit) = toolkit_from_slug(slug) else {
- continue;
- };
-
- let Some((ref_toolkit, ref_id)) = parse_composio_connection_ref(conn_ref) else {
- tracing::debug!(
- target: "flows",
- node = %node.id,
- %slug,
- toolkit = %slug_toolkit,
- %conn_ref,
- matched = false,
- "[flows] connection-ref check: malformed ref — rejecting"
- );
- errors.push(format!(
- "Node '{}': `connection_ref` `{conn_ref}` is malformed — a Composio account ref \
- must look like `composio:<toolkit>:<connection_id>` (e.g. \
- `composio:{slug_toolkit}:<id>`). Call list_flow_connections and copy a \
- `connection_ref` value verbatim.",
- node.id
- ));
- continue;
- };
+ use crate::integrations::composio::contract::validation::{
+ validate_connection_refs as validate, ConnectedAccountRef, ConnectionRefNode,
+ };
- // Toolkit segment vs the slug's toolkit — needs no I/O.
- if !ref_toolkit.eq_ignore_ascii_case(&slug_toolkit) {
- let suggestion = connections
- .and_then(|conns| first_connection_ref_for_toolkit(conns, &slug_toolkit));
- tracing::debug!(
- target: "flows",
- node = %node.id,
- %slug,
- toolkit = %slug_toolkit,
- %ref_toolkit,
- %ref_id,
- matched = false,
- "[flows] connection-ref check: toolkit segment does not match the slug's toolkit — rejecting"
- );
- let hint = match suggestion {
- Some(r) => format!(" — did you mean `{r}`?"),
- None => format!(
- " — no `{slug_toolkit}` account is connected; connect one with \
- composio_connect (or ask the user to), then use its `connection_ref`"
- ),
- };
- errors.push(format!(
- "Node '{}': `connection_ref` `{conn_ref}` names the `{ref_toolkit}` toolkit but the \
- tool_call slug `{slug}` is a `{slug_toolkit}` action{hint}.",
- node.id
- ));
- continue;
- }
-
- // Existence check: the id must name a real connected account of this
- // toolkit. Skipped (fail-open) when the connection list is unavailable.
- let Some(conns) = connections else {
- tracing::debug!(
- target: "flows",
- node = %node.id,
- %slug,
- toolkit = %slug_toolkit,
- %ref_id,
- "[flows] connection-ref check: toolkit matches; id-existence check skipped (connections unavailable)"
- );
- continue;
- };
- // The id must belong to a connection OF THIS TOOLKIT — not merely
- // exist somewhere. The transcript bug was a real TIKTOK connection id
- // stamped onto a `composio:twitter:` ref: the id exists globally, but
- // it is not a Twitter account, so it must still be rejected.
- let id_exists = conns.iter().any(|c| {
- c.toolkit
- .as_deref()
- .is_some_and(|t| t.eq_ignore_ascii_case(&slug_toolkit))
- && parse_composio_connection_ref(&c.connection_ref)
- .is_some_and(|(_, cid)| cid.eq_ignore_ascii_case(ref_id))
- });
- if id_exists {
- tracing::debug!(
- target: "flows",
- node = %node.id,
- %slug,
- toolkit = %slug_toolkit,
- %ref_id,
- matched = true,
- "[flows] connection-ref check: ref resolves to a real connected account — ok"
- );
- continue;
- }
- // Unknown id. Name the right ref for this toolkit if one exists.
- match first_connection_ref_for_toolkit(conns, &slug_toolkit) {
- Some(r) => {
- tracing::debug!(
- target: "flows",
- node = %node.id,
- %slug,
- toolkit = %slug_toolkit,
- %ref_id,
- matched = false,
- "[flows] connection-ref check: unknown id; toolkit has a different connected account — rejecting"
- );
- errors.push(format!(
- "Node '{}': `connection_ref` `{conn_ref}` does not match any connected \
- `{slug_toolkit}` account — did you mean `{r}`? Call list_flow_connections and \
- copy a `connection_ref` value verbatim.",
- node.id
- ));
- }
- None => {
- tracing::debug!(
- target: "flows",
- node = %node.id,
- %slug,
- toolkit = %slug_toolkit,
- %ref_id,
- matched = false,
- "[flows] connection-ref check: no connected account for this toolkit — rejecting"
- );
- errors.push(format!(
- "Node '{}': `connection_ref` `{conn_ref}` names a `{slug_toolkit}` account, but \
- no `{slug_toolkit}` account is connected — connect one with composio_connect \
- (or ask the user to), then use its `connection_ref`.",
- node.id
- ));
- }
- }
- }
- errors
+ let nodes: Vec<_> = graph
+ .nodes
+ .iter()
+ .filter(|node| node.kind == NodeKind::ToolCall)
+ .filter_map(|node| {
+ Some(ConnectionRefNode {
+ id: node.id.clone(),
+ slug: node.config.get("slug")?.as_str()?.to_string(),
+ connection_ref: node.config.get("connection_ref")?.as_str()?.to_string(),
+ })
+ })
+ .collect();
+ let accounts = connections.map(|items| {
+ items.iter().map(|item| ConnectedAccountRef {
+ connection_ref: item.connection_ref.clone(),
+ toolkit: item.toolkit.clone(),
+ }).collect::<Vec<_>>()
+ });
+ validate(&nodes, accounts.as_deref())
}
diff --git a/crates/openhuman-core/src/integrations/composio/contract/mod.rs b/crates/openhuman-core/src/integrations/composio/contract/mod.rs
index 367c9ed240..cc84b3e690 100644
--- a/crates/openhuman-core/src/integrations/composio/contract/mod.rs
+++ b/crates/openhuman-core/src/integrations/composio/contract/mod.rs
@@ -10,6 +10,7 @@
pub mod curated;
pub mod identity;
+pub mod validation;
pub use identity::{
canonicalize_values, normalize_connection_identifiers, render_connected_identities_section,
diff --git a/crates/openhuman-core/src/integrations/composio/contract/validation.rs b/crates/openhuman-core/src/integrations/composio/contract/validation.rs
new file mode 100644
index 0000000000..634dbc6d23
--- /dev/null
+++ b/crates/openhuman-core/src/integrations/composio/contract/validation.rs
@@ -0,0 +1,102 @@
+//! Composio contract validation that can be shared by flow authoring stages.
+
+/// A graph node reduced to the Composio fields needed by the connection gate.
+#[derive(Debug, Clone)]
+pub(crate) struct ConnectionRefNode {
+ pub id: String,
+ pub slug: String,
+ pub connection_ref: String,
+}
+
+/// A live account reduced to the fields used by the connection gate.
+#[derive(Debug, Clone)]
+pub(crate) struct ConnectedAccountRef {
+ pub connection_ref: String,
+ pub toolkit: Option<String>,
+}
+
+/// Parses a `composio:<toolkit>:<connection_id>` reference.
+pub(crate) fn parse_composio_connection_ref(conn_ref: &str) -> Option<(&str, &str)> {
+ let rest = conn_ref.strip_prefix("composio:")?;
+ let (toolkit, id) = rest.rsplit_once(':')?;
+ if toolkit.trim().is_empty() || id.trim().is_empty() {
+ return None;
+ }
+ Some((toolkit.trim(), id.trim()))
+}
+
+fn first_connection_ref_for_toolkit<'a>(
+ connections: &'a [ConnectedAccountRef],
+ toolkit: &str,
+) -> Option<&'a str> {
+ connections
+ .iter()
+ .find(|connection| {
+ connection
+ .toolkit
+ .as_deref()
+ .is_some_and(|value| value.eq_ignore_ascii_case(toolkit))
+ })
+ .map(|connection| connection.connection_ref.as_str())
+}
+
+/// Validates connection shape, toolkit match and (when available) account id.
+/// `None` connections means live lookup failed and preserves fail-open behavior.
+pub(crate) fn validate_connection_refs(
+ nodes: &[ConnectionRefNode],
+ connections: Option<&[ConnectedAccountRef]>,
+) -> Vec<String> {
+ use crate::integrations::composio::contract::toolkit_from_slug;
+
+ let mut errors = Vec::new();
+ for node in nodes {
+ let slug = node.slug.as_str();
+ if slug.starts_with('=') || slug.starts_with("oh:") || node.connection_ref.trim().is_empty() {
+ continue;
+ }
+ let Some(slug_toolkit) = toolkit_from_slug(slug) else {
+ continue;
+ };
+ let conn_ref = node.connection_ref.as_str();
+ let Some((ref_toolkit, ref_id)) = parse_composio_connection_ref(conn_ref) else {
+ errors.push(format!(
+ "Node '{}': `connection_ref` `{conn_ref}` is malformed — a Composio account ref \
+ must look like `composio:<toolkit>:<connection_id>` (e.g. \
+ `composio:{slug_toolkit}:<id>`). Call list_flow_connections and copy a \
+ `connection_ref` value verbatim.",
+ node.id
+ ));
+ continue;
+ };
+ if !ref_toolkit.eq_ignore_ascii_case(&slug_toolkit) {
+ let hint = match connections.and_then(|items| first_connection_ref_for_toolkit(items, &slug_toolkit)) {
+ Some(reference) => format!(" — did you mean `{reference}`?"),
+ None => format!(" — no `{slug_toolkit}` account is connected; connect one with composio_connect (or ask the user to), then use its `connection_ref`"),
+ };
+ errors.push(format!(
+ "Node '{}': `connection_ref` `{conn_ref}` names the `{ref_toolkit}` toolkit but the \
+ tool_call slug `{slug}` is a `{slug_toolkit}` action{hint}.",
+ node.id
+ ));
+ continue;
+ }
+ let Some(connections) = connections else { continue };
+ let id_exists = connections.iter().any(|connection| {
+ connection.toolkit.as_deref().is_some_and(|toolkit| toolkit.eq_ignore_ascii_case(&slug_toolkit))
+ && parse_composio_connection_ref(&connection.connection_ref)
+ .is_some_and(|(_, id)| id.eq_ignore_ascii_case(ref_id))
+ });
+ if id_exists { continue; }
+ match first_connection_ref_for_toolkit(connections, &slug_toolkit) {
+ Some(reference) => errors.push(format!(
+ "Node '{}': `connection_ref` `{conn_ref}` does not match any connected `{slug_toolkit}` account — did you mean `{reference}`?",
+ node.id
+ )),
+ None => errors.push(format!(
+ "Node '{}': `connection_ref` `{conn_ref}` names a `{slug_toolkit}` account, but none is connected. Connect one with composio_connect (or ask the user to), then use its `connection_ref`.",
+ node.id
+ )),
+ }
+ }
+ errors
+}