Skip to content

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

refactor(flows): move native tool result handling into native_ops

Steven Enamakel909f9b650264
diff --git a/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs b/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
index 90ea151fb3..51dd6cfb7f 100644
--- a/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
@@ -138,63 +138,6 @@ pub(crate) fn reject_unsuccessful_composio_response(
.map_err(EngineError::Capability)
}
-/// Native-tool analogue of [`reject_unsuccessful_composio_response`].
-///
-/// `execute_tool` returns `Ok(outcome)` for a tool that *ran* but *failed* —
-/// the failure rides on [`ToolResult::is_error`] (quota exceeded, file missing,
-/// no integration client configured). Nothing downstream inspected that flag,
-/// so the tinyflows engine recorded the step — and therefore the run — as
-/// `Success` even though the tool never did its job. Concretely: a file-upload
-/// step could fail, the next node would bind a `null` URL, and the run still
-/// reported "completed".
-///
-/// Mirrors the Composio branch's contract so both paths turn a failed step into
-/// `StepStatus::Error` (and, via `degrade_completed_status`, a failed run)
-/// rather than a false "Completed".
-pub(crate) fn reject_failed_native_tool_result(
- slug: &str,
- result: &tinytools::ToolResult,
-) -> Result<()> {
- if !result.is_error {
- return Ok(());
- }
- let rendered = result.output();
- let detail = match rendered.trim() {
- "" => "no error detail returned by the tool",
- d => d,
- };
- tracing::warn!(
- target: "flows",
- %slug,
- %detail,
- "[flows] tool_call: native tool reported is_error — failing the step"
- );
- Err(EngineError::Capability(format!(
- "tool_call `{slug}` failed: {detail}"
- )))
-}
-
-/// Unwraps a native (`oh:`) tool's [`ToolResult`] into the value a downstream
-/// node actually binds against.
-///
-/// Serializing the `ToolResult` verbatim (the previous behavior) placed the
-/// whole envelope on `item.json`, so reaching a field required
-/// `=nodes.<id>.item.json.content[0].data.<field>`. That expression does
-/// evaluate, but no builder agent ever emits it, which left native tools
-/// effectively unbindable in practice.
-///
-/// A lone `Json` block therefore returns its `data` directly, so a native node
-/// binds with the same `=nodes.<id>.item.json.<field>` shape used everywhere
-/// else. Anything else (plain text, or mixed/multiple blocks) collapses to
-/// `{ "text": <output()> }` so there is always a predictable field to bind.
-pub(crate) fn native_tool_payload(result: &tinytools::ToolResult) -> Value {
- use tinytools::ToolContent;
- match result.content.as_slice() {
- [ToolContent::Json { data }] => data.clone(),
- _ => json!({ "text": result.output() }),
- }
-}
-
/// A [`ToolInvoker`] decorator that runs the host's Composio required-arg
/// preflight before delegating to `inner`.
///
diff --git a/crates/openhuman-core/src/flows/tinyflows/caps/ops_tool_results_and_credentials_tests.rs b/crates/openhuman-core/src/flows/tinyflows/caps/ops_tool_results_and_credentials_tests.rs
index f6c4849313..2e9b087b9a 100644
--- a/crates/openhuman-core/src/flows/tinyflows/caps/ops_tool_results_and_credentials_tests.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/caps/ops_tool_results_and_credentials_tests.rs
@@ -1,72 +1,5 @@
use super::*;
-// ── native `oh:` tool result handling ──────────────────────────────────
-
-#[test]
-fn native_tool_payload_unwraps_a_single_json_block() {
- // `storage_get_link` returns exactly one Json block. A downstream node
- // must be able to bind `=nodes.<id>.item.json.url` — the same shape
- // used everywhere else — not `...item.json.content[0].data.url`.
- let result = ToolResult::json(json!({
- "url": "https://example.test/presigned",
- "expires_at": "2026-01-01T00:00:00Z",
- }));
- let payload = native_tool_payload(&result);
- assert_eq!(payload["url"], "https://example.test/presigned");
- assert_eq!(payload["expires_at"], "2026-01-01T00:00:00Z");
- assert!(
- payload.get("content").is_none() && payload.get("is_error").is_none(),
- "the ToolResult envelope must not leak into item.json: {payload}"
- );
-}
-
-#[test]
-fn native_tool_payload_collapses_text_to_a_bindable_field() {
- let payload = native_tool_payload(&ToolResult::success("done"));
- assert_eq!(payload["text"], "done");
-}
-
-#[test]
-fn native_tool_payload_collapses_mixed_blocks_to_text() {
- let result = ToolResult {
- content: vec![
- ToolContent::Text {
- text: "line".into(),
- },
- ToolContent::Json {
- data: json!({"k": 1}),
- },
- ],
- is_error: false,
- markdown_formatted: None,
- ..ToolResult::default()
- };
- let payload = native_tool_payload(&result);
- let text = payload["text"].as_str().expect("text field");
- assert!(text.contains("line") && text.contains('k'), "got {text}");
-}
-
-#[test]
-fn native_tool_failure_fails_the_step_instead_of_recording_success() {
- // The bug this guards: `execute_tool` returns Ok for a tool that ran
- // and FAILED (is_error), so the engine recorded the step — and the run
- // — as Success while a downstream node bound a null value.
- let result = ToolResult::error("storage quota exceeded");
- let err = reject_failed_native_tool_result("oh:storage_upload_file", &result)
- .expect_err("an is_error ToolResult must fail the step");
- let msg = format!("{err:?}");
- assert!(
- msg.contains("storage_upload_file") && msg.contains("storage quota exceeded"),
- "error must name the tool and the provider detail: {msg}"
- );
-}
-
-#[test]
-fn native_tool_success_passes_through() {
- let result = ToolResult::json(json!({"file_id": "f_1"}));
- assert!(reject_failed_native_tool_result("oh:storage_upload_file", &result).is_ok());
-}
-
/// A `http_cred:<name>` ref resolves to the stored bearer credential and
/// injects `Authorization: Bearer <token>` onto the outbound request.
#[test]
diff --git a/crates/openhuman-core/src/flows/tinyflows/caps/tools/native.rs b/crates/openhuman-core/src/flows/tinyflows/caps/tools/native.rs
index 2f607c0c53..67246a735a 100644
--- a/crates/openhuman-core/src/flows/tinyflows/caps/tools/native.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/caps/tools/native.rs
@@ -81,8 +81,11 @@ impl ToolBackend for NativeToolBackend {
.await
.map_err(EngineError::Capability)
.and_then(|outcome| {
- super::super::reject_failed_native_tool_result(slug, &outcome.result)
- .map(|_| super::super::native_tool_payload(&outcome.result))
+ crate::tools::native_ops::flow_backend::reject_failed_native_tool_result(
+ slug,
+ &outcome.result,
+ )
+ .map(|_| crate::tools::native_ops::flow_backend::native_tool_payload(&outcome.result))
});
// E-m1: close out the approval audit row (unlike the http/code/
diff --git a/crates/openhuman-core/src/tools/native_ops.rs b/crates/openhuman-core/src/tools/native_ops.rs
index e947f17425..0f8912062f 100644
--- a/crates/openhuman-core/src/tools/native_ops.rs
+++ b/crates/openhuman-core/src/tools/native_ops.rs
@@ -263,3 +263,6 @@ pub async fn execute_tool_with_security(
result,
})
}
+
+#[path = "native_ops/flow_backend.rs"]
+pub(crate) mod flow_backend;
diff --git a/crates/openhuman-core/src/tools/native_ops/flow_backend.rs b/crates/openhuman-core/src/tools/native_ops/flow_backend.rs
new file mode 100644
index 0000000000..34242a0c7b
--- /dev/null
+++ b/crates/openhuman-core/src/tools/native_ops/flow_backend.rs
@@ -0,0 +1,43 @@
+//! Adapts native tool results to the value and failure contract expected by
+//! TinyFlows. Tool execution itself remains in [`super`], which owns the
+//! native registry and its policy snapshot.
+
+use serde_json::{json, Value};
+use tinytools::ToolResult;
+
+/// Turns a failed native tool result into the flow-facing error body.
+pub(crate) fn reject_failed_native_tool_result(
+ slug: &str,
+ result: &ToolResult,
+) -> Result<(), String> {
+ if !result.is_error {
+ return Ok(());
+ }
+ let rendered = result.output();
+ let detail = match rendered.trim() {
+ "" => "no error detail returned by the tool",
+ detail => detail,
+ };
+ tracing::warn!(
+ target: "flows",
+ %slug,
+ %detail,
+ "[flows] tool_call: native tool reported is_error — failing the step"
+ );
+ Err(format!("tool_call `{slug}` failed: {detail}"))
+}
+
+/// Projects a tool result to the value downstream flow expressions bind.
+/// A single JSON block is returned directly; text and mixed blocks expose the
+/// rendered output under `text`.
+pub(crate) fn native_tool_payload(result: &ToolResult) -> Value {
+ use tinytools::ToolContent;
+ match result.content.as_slice() {
+ [ToolContent::Json { data }] => data.clone(),
+ _ => json!({ "text": result.output() }),
+ }
+}
+
+#[cfg(test)]
+#[path = "flow_backend_tests.rs"]
+mod tests;