The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(validation): handle missing `ops` field in migration deserialization
Steven Enamakela9442abdb755
diff --git a/crates/openhuman-core/src/flows/ops/validation.rs b/crates/openhuman-core/src/flows/ops/validation.rsindex d5caf4841f..e5b71b89e3 100644--- a/crates/openhuman-core/src/flows/ops/validation.rs+++ b/crates/openhuman-core/src/flows/ops/validation.rs@@ -100,88 +100,7 @@ pub(super) fn ensure_config_aware_engine_compatible(/// unmigrateable schema) is genuinely a single error, whereas structural/// 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())?;- // `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()+ tinyflows::migrate::deserialize_graph(graph_json)}/// 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.rsindex 6db288e368..88ba2ec11b 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@@ -642,59 +642,3 @@ fn migrate_and_deserialize_graph_names_the_node_that_is_missing_a_field() {"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}"- );-}--#[path = "ops_migration_deserialize_tests.rs"]-mod deserialize_tests;diff --git a/crates/openhuman-core/src/flows/ops_migration_deserialize_tests.rs b/crates/openhuman-core/src/flows/ops_migration_deserialize_tests.rsdeleted file mode 100644index 6681351da2..0000000000--- a/crates/openhuman-core/src/flows/ops_migration_deserialize_tests.rs+++ /dev/null@@ -1,38 +0,0 @@-use super::*;--/// The member scan covers four arrays, and until now only `nodes` was ever-/// exercised — the other three arms were unreachable in the test suite while-/// looking correct by inspection.-#[test]-fn migrate_and_deserialize_graph_names_the_member_in_inputs_agents_and_edges() {- let bad_input = json!({ "inputs": [ { "type": "string" } ] });- let err = migrate_and_deserialize_graph(bad_input).expect_err("input without `name`");- assert!(err.starts_with("inputs[0]: "), "got: {err}");-- let bad_agent = json!({ "agents": [ { "name": "no id" } ] });- let err = migrate_and_deserialize_graph(bad_agent).expect_err("agent without `id`");- assert!(err.starts_with("agents[0]: "), "got: {err}");-- let bad_edge = json!({ "edges": [ { "from_port": "main" } ] });- let err = migrate_and_deserialize_graph(bad_edge).expect_err("edge without endpoints");- assert!(err.starts_with("edges[0]: "), "got: {err}");-}--#[test]-fn migrate_and_deserialize_graph_falls_back_when_no_member_is_at_fault() {- let nodes_not_an_array = json!({ "name": "valid", "nodes": "this is not an array" });- let err = migrate_and_deserialize_graph(nodes_not_an_array)- .expect_err("a non-array `nodes` must not deserialize");- assert!(!err.contains("nodes["), "got: {err}");- assert!(err.contains("invalid type"), "got: {err}");-}--#[test]-fn migrate_and_deserialize_graph_reports_a_non_object_graph_without_inventing_a_location() {- let err = migrate_and_deserialize_graph(json!("this is a string, not a workflow graph"))- .expect_err("a non-object graph must not deserialize");- // A non-object has no member to attribute: retain serde's bare type- // diagnostic rather than manufacturing a top-level field/path prefix.- assert!(err.starts_with("invalid type: "), "got: {err}");- assert!(!err.contains('['), "got: {err}");-}