Skip to content

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

refactor(ops): delegate binding resolvability validation to `tinyflows::gates`

Steven Enamakel2850a63973a0
diff --git a/src/openhuman/flows/ops.rs b/src/openhuman/flows/ops.rs
index 461b490e7c..23ed93dc34 100644
--- a/src/openhuman/flows/ops.rs
+++ b/src/openhuman/flows/ops.rs
@@ -1321,331 +1321,30 @@ async fn graph_split_out_path_warnings(config: &Config, graph: &WorkflowGraph) -
// ─────────────────────────────────────────────────────────────────────────────
//
// `graph_wiring_warnings` (above) is advisory — it, and `dry_run_workflow`'s
-// null-resolution check (issue #4586), only WARN the author that a binding
-// resolves null. Neither is consulted by the builder before it proposes or
-// saves a graph, so a warned-about-but-ignored binding still ships. The
-// functions below are the HARD counterpart: `validate_binding_resolvability`
-// statically proves a `tool_call` node's `args` bindings are resolvable
-// *before* `propose_workflow`/`revise_workflow`/`save_workflow` accept the
-// graph at all (see their call sites), so the LLM builder is forced to fix
-// the wiring rather than merely being told about it.
-
-/// Node kinds whose real capability adapter wraps its structured output in
-/// the stable `{ json, text, raw }` envelope (`src/openhuman/flows/tinyflows/caps.rs`):
-/// a binding into one of these must dereference `.item.json.<field>`, never
-/// `.item.<field>` directly — the latter reads the envelope wrapper itself
-/// (an object with `json`/`text`/`raw` keys), not the field inside it, and
-/// resolves `null` at runtime. Every other node kind (`code`, `transform`,
-/// `split_out`, `merge`, `output_parser`, `sub_workflow`, `trigger`,
-/// `condition`, `switch`) emits its item directly with no envelope, so no
-/// convention applies to a binding that targets one of them.
-const ENVELOPING_KINDS: &[NodeKind] = &[NodeKind::Agent, NodeKind::ToolCall, NodeKind::HttpRequest];
-
-/// Recursively collects every `=`-prefixed expression leaf in a config
-/// `Value` tree, paired with its dotted location (array elements as numeric
-/// segments, e.g. `"args.cc.0"`) — the same location convention as
-/// `tinyflows::expr::resolve_traced`. Unlike that function this never
-/// evaluates an expression against a scope; it only locates the leaves so
-/// [`validate_binding_resolvability`] can statically pattern-match them.
-fn collect_expressions(value: &Value) -> Vec<(String, String)> {
- fn walk(value: &Value, location: &str, out: &mut Vec<(String, String)>) {
- match value {
- Value::Object(map) => {
- for (k, v) in map {
- let child = if location.is_empty() {
- k.clone()
- } else {
- format!("{location}.{k}")
- };
- walk(v, &child, out);
- }
- }
- Value::Array(items) => {
- for (i, v) in items.iter().enumerate() {
- let child = if location.is_empty() {
- i.to_string()
- } else {
- format!("{location}.{i}")
- };
- walk(v, &child, out);
- }
- }
- Value::String(s) if tinyflows::expr::is_expression(s) => {
- out.push((location.to_string(), s.clone()));
- }
- _ => {}
- }
- }
- let mut out = Vec::new();
- walk(value, "", &mut out);
- out
-}
-
-/// Matches the dotted-path form of a node-output binding —
-/// `=nodes.<ref_id>.item[.json].<field_path>` — returning `(ref_id, has_json,
-/// field_path)`. `has_json` is `true` when the expression dereferenced the
-/// `{json,text,raw}` envelope wrapper (`.item.json.<field_path>`) rather than
-/// the item directly (`.item.<field_path>`).
-///
-/// `field_path` captures the FULL remaining dotted path, not just its first
-/// segment — e.g. `"data.messages"` for `.item.json.data.messages`. This
-/// matters for a Composio `tool_call` ref, whose real output additionally
-/// wraps the field in `data` (see [`crate::openhuman::flows::tinyflows::caps::ToolContract::output_fields`]'s
-/// doc): callers that need to check field membership against a schema with
-/// no such wrapper (e.g. an `agent` node's `output_parser.schema`) should
-/// compare against just `field_path`'s first segment.
-///
-/// Only the dotted-path form is recognized here — the equivalent jq form
-/// (e.g. `=.nodes["ref"].items[0].field`) is an arbitrary jq program, not a
-/// fixed grammar, so it is not statically pattern-matched; that form is still
-/// covered dynamically by `dry_run_workflow`'s null-resolution check (#4586),
-/// which actually evaluates the expression at run time.
-fn parse_node_binding(expr: &str) -> Option<(String, bool, String)> {
- fn node_binding_regex() -> &'static regex::Regex {
- static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
- RE.get_or_init(|| {
- regex::Regex::new(
- r"^=nodes\.([A-Za-z_][A-Za-z0-9_]*)\.item(?:\.(json))?\.([A-Za-z_][A-Za-z0-9_.]*)",
- )
- .expect("static regex is valid")
- })
- }
- let caps = node_binding_regex().captures(expr)?;
- let ref_id = caps.get(1)?.as_str().to_string();
- let has_json = caps.get(2).is_some();
- let field_path = caps.get(3)?.as_str().trim_end_matches('.').to_string();
- if field_path.is_empty() {
- return None;
- }
- Some((ref_id, has_json, field_path))
-}
-
-/// Human-readable label for a [`NodeKind`], for
-/// [`validate_binding_resolvability`]'s envelope-violation message.
-fn node_kind_label(kind: &NodeKind) -> &'static str {
- match kind {
- NodeKind::Agent => "an agent",
- NodeKind::ToolCall => "a tool_call",
- NodeKind::HttpRequest => "an http_request",
- _ => "a node",
- }
-}
-
-/// jaq keywords/operators that read as valid jq syntax rather than natural-
-/// language prose; used by [`agent_prompt_looks_like_invalid_jq`]'s bareword
-/// scan so a genuine jq program (`if`/`then`/`else`/`end`, `and`/`or`,
-/// `reduce`/`foreach`, a `def`, …) is never mistaken for prose.
-const JQ_KEYWORDS: &[&str] = &[
- "and", "or", "not", "if", "then", "elif", "else", "end", "as", "def", "reduce", "foreach",
- "try", "catch", "import", "include", "label",
-];
-
-/// Best-effort detector for an agent-node `config.prompt` `=`-expression that
-/// is natural-language prose accidentally written in the `=`-binding
-/// convention, rather than a real jq program — the exact failure this check
-/// exists to catch: a builder writes something like `"=You are given an
-/// email: .item. Classify it…"`, which is not a valid jq program (jq's
-/// grammar has no rule for two bare identifiers in a row with nothing but
-/// whitespace between them — an operator or pipe is required), so
-/// `tinyflows::expr::evaluate` silently resolves it to `null` (its contract:
-/// "compile/run errors never panic, they yield `Value::Null`") and the agent
-/// turn then runs with an **empty prompt**.
-///
-/// `tinyflows` doesn't expose a compile-only jq check — `run_jq` is a private
-/// helper in `tinyflows::expr` and the module's evaluation contract is
-/// deliberately "never panics, malformed programs silently yield null" — so
-/// this is a conservative pattern match rather than a real compiler
-/// round-trip: quoted jq string literals are stripped first (so quoted prose
-/// inside a legitimate concatenation like `="Hi " + .item.name` is never
-/// scanned — this includes respecting a `\"` escape inside the string, so a
-/// quoted literal like `="Say \"hi\" to " + .item.name` doesn't desync the
-/// quote-toggle and leak its trailing prose into the bareword scan), then the
-/// remainder is scanned for **two or more consecutive** whitespace-separated
-/// barewords that are neither jq keywords nor path segments (`.foo`,
-/// `.foo.bar`) — a real jq program never juxtaposes two bare identifiers like
-/// that. Deliberately narrow (2+ in a row, not 1): a false negative here just
-/// leaves prose alone (nothing new was broken); a false positive would reject
-/// a legitimate author's graph.
-fn agent_prompt_looks_like_invalid_jq(expr_body: &str) -> bool {
- let mut stripped = String::with_capacity(expr_body.len());
- let mut in_str = false;
- let mut chars = expr_body.chars();
- while let Some(c) = chars.next() {
- // An escaped char inside a jq string literal (`\"`, `\\`, `\n`, …) —
- // consume both the backslash and the escaped char without toggling
- // `in_str`, so an escaped quote never prematurely ends the string.
- if in_str && c == '\\' {
- chars.next();
- continue;
- }
- if c == '"' {
- in_str = !in_str;
- continue;
- }
- if !in_str {
- stripped.push(c);
- }
- }
-
- let mut consecutive_bare_words = 0u32;
- for tok in stripped.split_whitespace() {
- let core = tok.trim_matches(|c: char| !c.is_ascii_alphabetic());
- let is_bare_word = !core.is_empty()
- && core.chars().all(|c| c.is_ascii_alphabetic())
- && !tok.starts_with('.')
- && !tok.contains('.')
- && !JQ_KEYWORDS.contains(&core.to_ascii_lowercase().as_str());
- if is_bare_word {
- consecutive_bare_words += 1;
- if consecutive_bare_words >= 2 {
- return true;
- }
- } else {
- consecutive_bare_words = 0;
- }
- }
- false
-}
+// null-resolution check, only WARN that a binding resolves null. The gate
+// below is the HARD counterpart, run before
+// `propose_workflow`/`revise_workflow`/`save_workflow` accept a graph at all,
+// so the builder is forced to fix the wiring rather than merely being told.
+//
+// The analysis is `tinyflows::gates`': every rule in it is a statement about
+// the DSL — that agent/tool_call/http_request output is wrapped in
+// `{json, text, raw}`, that a `=`-prefixed prose string is not a jq program,
+// that an agent produces only what its `output_parser.schema` declares — and
+// none of it depends on which host is asking. This host used to carry a
+// near-identical private copy, including its own `collect_expressions` and
+// `parse_node_binding`; that copy is gone.
+//
+// Anything that DOES depend on this host's vocabulary — which agent ids
+// resolve, which tool slugs exist, which integrations are connected — stays
+// here, in the gates that follow.
-/// Statically proves every `tool_call` node's `config.args` bindings are
-/// resolvable, rejecting the graph (a non-empty `Vec` = reject; empty =
-/// pass) when one is GUARANTEED to resolve `null` (or the wrong value) at
-/// runtime. See the [module section](self) header for why this exists
-/// alongside the advisory `graph_wiring_warnings`/`dry_run_workflow` checks.
+/// Refuses a graph whose bindings are statically proven unresolvable.
///
-/// Scoped to `tool_call` `args` for the field-addressability checks below —
-/// an `agent` node's free-text prompt has no static output schema to enforce
-/// a `nodes.<ref>.item.<field>` reference against, so a prose string that
-/// merely *mentions* such a path is left alone (degrades output quality, but
-/// doesn't break execution the way a `null` tool argument does). The ONE
-/// `agent`-prompt case this pass DOES reject is narrower and execution-
-/// breaking in its own right: `config.prompt` itself being a `=`-expression
-/// that reads as prose rather than a jq program (see
-/// [`agent_prompt_looks_like_invalid_jq`]) — that doesn't just degrade
-/// output, it guarantees `null`, i.e. an EMPTY prompt, exactly the
-/// `input_context` bug this whole gate was added to prevent (see the
-/// `flows/agents/workflow_builder/prompt.md` convention: `input_context`
-/// carries data, `prompt` stays a plain instruction).
-///
-/// For every `=nodes.<ref>.item[.json].<field>` binding found in a
-/// `tool_call`'s `args` (via [`collect_expressions`] + [`parse_node_binding`]):
-/// - a `<ref>` that doesn't resolve to a node in the graph is skipped — a
-/// dangling reference is already a `tinyflows::validate::validate`
-/// structural error, caught upstream of this pass.
-/// - a `<ref>` that IS an [`ENVELOPING_KINDS`] node and the expression used
-/// `.item.<field>` (no `.json`) is REJECTED: it dereferences the envelope
-/// wrapper, not the field inside it.
-/// - a `<ref>` that is an `agent` node is REJECTED unless it declares
-/// `config.output_parser.schema` with an object `properties` map
-/// containing `<field>` — the exact shape a real run's output-parser
-/// sub-port enforces; without it the agent's structured output has no
-/// addressable `<field>`.
-/// - a `<ref>` that is `tool_call`/`http_request` only gets the envelope
-/// check above — neither has a static output schema to check field
-/// membership against ahead of a real run.
-/// - any other referenced kind (`code`, `transform`, `split_out`, `merge`,
-/// `output_parser`, `sub_workflow`, `trigger`, `condition`, `switch`) has no
-/// schema or envelope convention to enforce and is accepted.
+/// A non-empty `Vec` rejects; empty passes. Delegates wholesale to
+/// [`tinyflows::gates::failures`] — see the section header for why nothing in
+/// it is host-specific.
pub(crate) fn validate_binding_resolvability(graph: &WorkflowGraph) -> Vec<String> {
- let mut errors = Vec::new();
-
- // Agent-prompt gate: reject a `prompt` that reads as prose written in the
- // `=`-binding convention (see `agent_prompt_looks_like_invalid_jq`'s doc) —
- // it is GUARANTEED to resolve `null`, handing the agent an empty prompt.
- // A plain (non-`=`) prompt, or a real jq/dotted-path expression, is
- // unaffected.
- for node in &graph.nodes {
- if node.kind != NodeKind::Agent {
- continue;
- }
- // Both runtime paths (`build_completion_messages` and
- // `node_request_to_prompt` in `tinyflows/caps.rs`) fall through to a
- // non-empty `messages` array once `prompt` resolves to `null` — which
- // is exactly what this bad `=`-expression prompt does. So a node that
- // declares real `messages` never actually runs on the null prompt;
- // rejecting the graph for it would be a false positive against a
- // vestigial/unused legacy `prompt` field.
- let messages_supply_the_turn = node
- .config
- .get("messages")
- .and_then(Value::as_array)
- .is_some_and(|entries| !entries.is_empty());
- if messages_supply_the_turn {
- continue;
- }
- let Some(prompt) = node.config.get("prompt").and_then(Value::as_str) else {
- continue;
- };
- if !tinyflows::expr::is_expression(prompt) {
- continue;
- }
- let body = prompt[1..].trim();
- if agent_prompt_looks_like_invalid_jq(body) {
- errors.push(format!(
- "Node '{}': `prompt` (`{prompt}`) looks like natural-language text written as \
- a `=`-expression, not a valid jq program — it will resolve to `null` at \
- runtime, handing the agent an EMPTY prompt. Fix: feed upstream data through \
- `config.input_context` (e.g. `\"input_context\": \"=item\"`) and make `prompt` \
- a plain instruction with no leading `=`.",
- node.id
- ));
- }
- }
-
- for node in &graph.nodes {
- if node.kind != NodeKind::ToolCall {
- continue;
- }
- let Some(args) = node.config.get("args") else {
- continue;
- };
- for (location, expr) in collect_expressions(args) {
- let Some((ref_id, has_json, field_path)) = parse_node_binding(&expr) else {
- continue;
- };
- let Some(ref_node) = graph.node(&ref_id) else {
- continue;
- };
-
- if ENVELOPING_KINDS.contains(&ref_node.kind) && !has_json {
- errors.push(format!(
- "Node '{}': arg `{location}` (`{expr}`) uses `.item.{field_path}` on {} node \
- `{ref_id}`, but agent/tool_call/http_request nodes wrap output in {{json, \
- text, raw}} — use `=nodes.{ref_id}.item.json.{field_path}` instead.",
- node.id,
- node_kind_label(&ref_node.kind),
- ));
- continue;
- }
-
- if ref_node.kind == NodeKind::Agent {
- // Agent output has no Composio `data` wrapper — the schema's
- // top-level properties are checked against just the FIRST
- // segment of the bound path (agents don't publish nested
- // output schemas here).
- let field = field_path.split('.').next().unwrap_or(&field_path);
- let has_field = ref_node
- .config
- .get("output_parser")
- .and_then(|p| p.get("schema"))
- .filter(|s| !s.is_null())
- .and_then(|s| s.get("properties"))
- .and_then(Value::as_object)
- .is_some_and(|props| props.contains_key(field));
- if !has_field {
- errors.push(format!(
- "Node '{}': arg `{location}` (`{expr}`) binds to agent node `{ref_id}`, \
- which has no `output_parser.schema` declaring `{field}` — its \
- structured output has no addressable `{field}`, so this binding \
- resolves null at runtime. Fix: add `{field}` to node `{ref_id}`'s \
- output_parser.schema and bind via `=nodes.{ref_id}.item.json.{field}`.",
- node.id
- ));
- }
- }
- }
- }
- errors
+ tinyflows::gates::failures(graph)
}
// ─────────────────────────────────────────────────────────────────────────────