Skip to content

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

fix(flows): name the object when a workflow graph fails to deserialize

M3gA-Mindc7354a6c7a20
diff --git a/crates/openhuman-core/src/flows/ops/validation.rs b/crates/openhuman-core/src/flows/ops/validation.rs
index 84018f44d0..9373d32fd6 100644
--- a/crates/openhuman-core/src/flows/ops/validation.rs
+++ b/crates/openhuman-core/src/flows/ops/validation.rs
@@ -101,8 +101,87 @@ pub(super) fn ensure_config_aware_engine_compatible(
/// validation can surface many at once.
pub(crate) fn migrate_and_deserialize_graph(graph_json: Value) -> Result<WorkflowGraph, String> {
let migrated = tinyflows::migrate::migrate(graph_json).map_err(|e| e.to_string())?;
- let graph: WorkflowGraph = serde_json::from_value(migrated).map_err(|e| e.to_string())?;
- Ok(graph)
+ // `serde_json` errors carry no path, and `missing field `name`` on its own
+ // is unactionable here: every field of `WorkflowGraph` is
+ // `#[serde(default)]`, so the fault is always in a nested object -- and an
+ // agent that reads it as the top-level `name` it already set will retry
+ // unchanged until it exhausts its iteration cap.
+ serde_json::from_value::<WorkflowGraph>(migrated.clone())
+ .map_err(|e| locate_graph_error(&migrated, &e))
+}
+
+/// The `WorkflowGraph` fields whose elements carry their own required fields.
+const ELEMENT_ARRAYS: &[&str] = &["nodes", "inputs", "agents", "edges"];
+
+/// Names the element a graph-level deserialization error came from.
+///
+/// Re-deserializes each member of the arrays that carry required fields and
+/// reports the first that fails on its own, as `nodes[1]: <serde error>`. None
+/// of these types use `deny_unknown_fields`, so an element that parses in
+/// isolation is one the graph-level parse accepted too, and a failure found
+/// here is the real fault rather than an artefact of checking it alone.
+///
+/// Runs only on the error path, and falls back to the bare message when the
+/// fault is not in a single element -- a wrong type for `nodes` itself, say.
+fn locate_graph_error(migrated: &Value, err: &serde_json::Error) -> String {
+ // Re-parse with the element arrays emptied. If that still fails, the fault
+ // is in the graph's own fields -- a non-string `name`, say -- and scanning
+ // members would pin it on the first member that happens to be invalid too,
+ // which is a confident wrong answer rather than a vague right one.
+ let mut skeleton = migrated.clone();
+ if let Some(fields) = skeleton.as_object_mut() {
+ for field in ELEMENT_ARRAYS {
+ if let Some(slot) = fields.get_mut(*field) {
+ *slot = Value::Array(Vec::new());
+ }
+ }
+ }
+ if serde_json::from_value::<WorkflowGraph>(skeleton).is_err() {
+ return locate_top_level_error(migrated, err);
+ }
+
+ macro_rules! locate {
+ ($field:literal, $ty:ty) => {
+ if let Some(items) = migrated.get($field).and_then(Value::as_array) {
+ for (index, item) in items.iter().enumerate() {
+ if let Err(inner) = serde_json::from_value::<$ty>(item.clone()) {
+ return format!("{}[{}]: {}", $field, index, inner);
+ }
+ }
+ }
+ };
+ }
+
+ locate!("nodes", tinyflows::model::Node);
+ locate!("inputs", tinyflows::model::WorkflowInput);
+ locate!("agents", tinyflows::model::AgentDefinition);
+ locate!("edges", tinyflows::model::Edge);
+
+ err.to_string()
+}
+
+/// Names the graph's own field when the fault is at the top level.
+///
+/// `serde_json` reports a type mismatch as `invalid type: integer \`123\`,
+/// expected a string` with **no field name** -- the same unactionable shape as
+/// the missing-field case this helper exists to fix, so it gets the same
+/// treatment.
+///
+/// Every `WorkflowGraph` field is `#[serde(default)]`, so an object carrying a
+/// single field parses if and only if that field is valid. Probing one key at a
+/// time therefore names the offender without a hardcoded field list. Unknown
+/// keys parse (no `deny_unknown_fields`) and are skipped.
+fn locate_top_level_error(migrated: &Value, err: &serde_json::Error) -> String {
+ if let Some(fields) = migrated.as_object() {
+ for (key, value) in fields {
+ let probe = Value::Object([(key.clone(), value.clone())].into_iter().collect());
+ if let Err(inner) = serde_json::from_value::<WorkflowGraph>(probe) {
+ return format!("{key}: {inner}");
+ }
+ }
+ }
+
+ err.to_string()
}
/// Maps a portable `tinyflows` [`ValidationError`](tinyflows::error::ValidationError)
diff --git a/crates/openhuman-core/src/flows/ops_migration_and_side_effect_tests.rs b/crates/openhuman-core/src/flows/ops_migration_and_side_effect_tests.rs
index 8fe4840cec..9484f3ef4c 100644
--- a/crates/openhuman-core/src/flows/ops_migration_and_side_effect_tests.rs
+++ b/crates/openhuman-core/src/flows/ops_migration_and_side_effect_tests.rs
@@ -605,3 +605,93 @@ async fn draft_promote_of_invalid_graph_is_rejected_and_keeps_the_draft() {
// The draft survives a failed promote so the user can fix it.
assert!(flows_draft_get(&config, &draft.id).is_ok());
}
+
+/// Regression for #6540: a graph whose *node* omits `name` must report which
+/// object is at fault, not a bare `missing field \`name\``.
+///
+/// The bare message is worse than uninformative here, because the one reading
+/// it cannot even rule out the obvious candidate: every field of
+/// `WorkflowGraph` is `#[serde(default)]`, so the top-level `name` can never be
+/// the cause — yet an authoring agent that assumes it is will re-send the same
+/// graph until it exhausts its iteration cap. That is exactly what happened
+/// live: six identical retries, 392k input tokens, no workflow created.
+#[test]
+fn migrate_and_deserialize_graph_names_the_node_that_is_missing_a_field() {
+ let missing_node_name = json!({
+ "name": "top-level name is present and is NOT the problem",
+ "nodes": [
+ { "id": "start", "kind": "trigger", "name": "Trigger" },
+ { "id": "nameless", "kind": "trigger" }
+ ]
+ });
+
+ let err = migrate_and_deserialize_graph(missing_node_name)
+ .expect_err("a node without `name` must not deserialize");
+
+ // The path is the point of the fix: `nodes[1]` is the second node, and
+ // naming it is what distinguishes this from the two other required `name`
+ // fields reachable from one graph (`Port::name`, `WorkflowInput::name`).
+ assert!(
+ err.contains("nodes[1]"),
+ "error must name the offending node's path, got: {err}"
+ );
+ // ...and the original serde detail must survive the wrapping, or the fix
+ // would have traded one unactionable message for another.
+ assert!(
+ err.contains("missing field") && err.contains("name"),
+ "error must still say which field is missing, got: {err}"
+ );
+}
+
+/// The premise of the test above: a graph with no top-level `name` at all
+/// deserializes fine. If this ever starts failing, `missing field \`name\``
+/// becomes genuinely ambiguous and the path in the message matters more, not
+/// less.
+#[test]
+fn migrate_and_deserialize_graph_accepts_a_graph_with_no_top_level_name() {
+ let no_top_level_name = json!({
+ "nodes": [ { "id": "start", "kind": "trigger", "name": "Trigger" } ]
+ });
+
+ let graph = migrate_and_deserialize_graph(no_top_level_name)
+ .expect("top-level `name` is #[serde(default)] and must not be required");
+ assert_eq!(
+ graph.name, "",
+ "absent top-level name must default to empty"
+ );
+}
+
+/// Regression for the misattribution case raised in review on #6545: when the
+/// graph's own fields are at fault *and* a node is independently invalid, the
+/// error must not be pinned on the node.
+///
+/// Locating by "first member that fails on its own" is only sound once the
+/// top level is known good — otherwise it turns a correct vague message into a
+/// confident wrong one, which is worse than what this PR set out to fix.
+#[test]
+fn migrate_and_deserialize_graph_does_not_blame_a_node_for_a_top_level_fault() {
+ let top_level_fault = json!({
+ // Wrong type, not missing: `name` is `String`, so this fails the graph
+ // parse on its own account.
+ "name": 123,
+ "nodes": [
+ // Independently invalid too — the bait for the member scan.
+ { "id": "nameless", "kind": "trigger" }
+ ]
+ });
+
+ let err = migrate_and_deserialize_graph(top_level_fault)
+ .expect_err("a non-string top-level `name` must not deserialize");
+
+ // Assert what must NOT be claimed. Comparing the rendered string to the
+ // bare serde message would pass for the wrong reason: the two messages can
+ // coincide without the failing path being the same one.
+ assert!(
+ !err.contains("nodes["),
+ "a top-level fault must not be attributed to a node, got: {err}"
+ );
+ assert!(
+ err.contains("name"),
+ "error should still name the offending field, got: {err}"
+ );
+}