diff --git a/crates/engine/src/cmd/actions_cmds.rs b/crates/engine/src/cmd/actions_cmds.rs index adc92858e65..fe24cec596e 100644 --- a/crates/engine/src/cmd/actions_cmds.rs +++ b/crates/engine/src/cmd/actions_cmds.rs @@ -512,6 +512,15 @@ pub fn restore_recording(s: &mut Session, idx: usize, action: &str, from: usize) /// Nested playback returns a structured failure even though the command itself succeeded. /// Promote it to the caller's failed step so no later parent steps run after a child fails. pub fn nested_failure(id: &str, value: &Value) -> Option { + // Batch keeps per-file results for direct callers, but a failed recorded Batch step stops its parent. + if id == "file.automate.batch" { + let failure = value.get("errors")?.as_array()?.first()?; + return Some(format!( + "Batch failed for {}: {}", + failure.get("file").and_then(Value::as_str).unwrap_or("an input"), + failure.get("error").and_then(Value::as_str).unwrap_or("unknown error") + )); + } (id == "actions.play" || id == "actions.conditional").then(|| value.get("failed")).flatten().map(|failure| { format!( "action `{}` step {} failed: {}", diff --git a/crates/engine/src/cmd/automate_cmds.rs b/crates/engine/src/cmd/automate_cmds.rs index ee253bea4ae..5bbcd692162 100644 --- a/crates/engine/src/cmd/automate_cmds.rs +++ b/crates/engine/src/cmd/automate_cmds.rs @@ -138,14 +138,17 @@ fn run_steps(s: &mut Session, steps: &[(String, Value)]) -> Vec { break; } match s.execute(id, params.clone()) { - // A called action reports a failed step inside an `Ok`; it stops the script too. - Ok(r) => match crate::actions_cmds::nested_failure(id, &r).or_else(|| nested_script_failure(id, &r)) { - Some(error) => { - results.push(json!({"command": id, "result": r, "error": error})); - break; + Ok(r) => { + // Direct Batch calls report file errors successfully to scripts. Called actions still fail the script. + let action_failure = if id == "file.automate.batch" { None } else { crate::actions_cmds::nested_failure(id, &r) }; + match action_failure.or_else(|| nested_script_failure(id, &r)) { + Some(error) => { + results.push(json!({"command": id, "result": r, "error": error})); + break; + } + None => results.push(json!({"command": id, "result": r})), } - None => results.push(json!({"command": id, "result": r})), - }, + } Err(e) => { results.push(json!({"command": id, "error": e.to_string()})); break; diff --git a/crates/engine/src/cmd/file_cmds/tests.rs b/crates/engine/src/cmd/file_cmds/tests.rs index 176e1c16e4d..7ba131b76fb 100644 --- a/crates/engine/src/cmd/file_cmds/tests.rs +++ b/crates/engine/src/cmd/file_cmds/tests.rs @@ -109,15 +109,25 @@ fn batch_recorded_stop_for_errors_retains_earlier_outputs_and_stops_before_later let p = json!({"input":dir,"output":output,"action":"Child","set":"S","stopOnError":true, "includeSubfolders":false,"overrideSaveAs":false,"flatOutput":true, "namingComponents":[{"component":"name"},{"component":"extension"}]}); - let driver = crate::actions_cmds::Action::new("Driver", vec![("file.automate.batch".into(), p)]); + let sentinel = json!({"width":7,"height":9}); + let driver = crate::actions_cmds::Action::new("Driver", vec![("file.automate.batch".into(), p.clone()), ("file.new".into(), sentinel.clone())]); let (bytes, skipped) = crate::atn_cmds::export("S", &[&driver], &[], false).unwrap(); assert!(skipped.is_empty()); - s.actions.list.extend(crate::atn_cmds::import(&bytes).unwrap().actions); + let imported = crate::atn_cmds::import(&bytes).unwrap(); + assert_eq!(imported.actions[0].steps[1].0, "file.new", "the after-Batch sentinel must be replayable"); + s.actions.list.extend(imported.actions); let result = s.execute("actions.play", json!({"action":"Driver"})).unwrap(); - assert_eq!(result["ran"], 1, "{result}"); - assert!(result.get("failed").is_none(), "{result}"); + assert_eq!(result["ran"], 0, "{result}"); + assert_eq!(result["failed"]["id"], "file.automate.batch"); + assert!(result["failed"]["error"].as_str().unwrap().contains("b.png"), "{result}"); + assert!(s.documents().is_empty(), "the command after the failed Batch must not run"); assert!(std::path::Path::new(&join(&output, "a.png")).is_file()); assert!(!std::path::Path::new(&join(&output, "c.png")).exists()); + // The measured scripting API reports success for the same stopped Batch and continues the script. + let result = s.execute("file.scripts.browse", json!({"steps":[["file.automate.batch",p],["file.new",sentinel]]})).unwrap(); + assert_eq!(result["ok"], true, "{result}"); + assert_eq!(result["results"][0]["result"]["errors"].as_array().unwrap().len(), 1); + assert_eq!(s.documents().len(), 1, "the direct script caller still runs its next command"); let invalid_output = join(&dir, "invalid"); let err = s.execute("file.automate.batch", json!({"input":[first],"output":invalid_output,"steps":[],"stopOnError":"true"})).unwrap_err(); assert!(err.to_string().contains("stopOnError"), "{err}"); diff --git a/docs/atn.md b/docs/atn.md index 5189883fe7f..2250ff9e09e 100644 --- a/docs/atn.md +++ b/docs/atn.md @@ -258,6 +258,9 @@ Recorded folder steps without an error log set `stopOnError`: the first failed input is reported, earlier outputs stay saved and later inputs are not processed. The native command still continues by default. That continuing mode cannot be exported as a no-log Batch step, which would change its error policy. +Any reported file error fails a recorded Batch step, so its parent action does +not run later steps. A direct scripting call still returns the collected file +results successfully, as measured in the reference application. This maps supported text-path folder forms. Alias bytes, open-document sources, Save and Close, error logs and enabled Open/dialog overrides remain preserved