diff --git a/crates/engine/src/cmd/actions_cmds.rs b/crates/engine/src/cmd/actions_cmds.rs index 551fae270e..e5a1f36db2 100644 --- a/crates/engine/src/cmd/actions_cmds.rs +++ b/crates/engine/src/cmd/actions_cmds.rs @@ -253,34 +253,69 @@ pub fn playback_plan(s: &Session, p: &Value) -> Result<(Action, usize)> { Ok((action, from)) } -/// The actions that `steps` call through Play Action steps and the branches of Conditional steps, +/// The actions that `steps` call through Play Action, explicit/named Batch and Conditional steps, /// followed transitively, in list order, so a droplet can run without the Actions panel that made /// it (#2794, #6804). Index references in `steps` and in the copies become names, which resolve /// without the rest of the list. A target that doesn't resolve is left alone: it fails at /// playback, as it would have before. pub fn called_actions(list: &[Action], steps: &mut [(String, Value)]) -> Vec { + fn step_ref(step: &mut Value) -> Option<(String, &mut Value)> { + match step { + Value::Array(a) => { + let id = a.first()?.as_str()?.to_string(); + Some((id, a.get_mut(1)?)) + } + Value::Object(o) => { + let id = o.get("command").or_else(|| o.get("id"))?.as_str()?.to_string(); + Some((id, o.get_mut("params")?)) + } + // A parameter-less step cannot name another action. + _ => None, + } + } fn targets(list: &[Action], steps: &mut [(String, Value)], depth: usize, queue: &mut Vec<(usize, usize)>) { + let mut pending = Vec::new(); for (id, params) in steps.iter_mut() { - match id.as_str() { - "actions.play" | "file.automate.batch" => { - let resolved = - if id == "file.automate.batch" { crate::file_cmds::batch_action(list, params) } else { resolve(list, params, "actions.play") }; - let Ok(i) = resolved else { continue }; - if let (Some(action), Some(obj)) = (list.get(i), params.as_object_mut()) { - obj.insert("action".into(), json!(action.name)); + pending.push((id.clone(), params)); + while let Some((id, params)) = pending.pop() { + // Follow the same explicit-step precedence as Batch. Walk the JSON tree with a + // heap stack; containers do not consume action-call depth or recurse on the stack. + if id == "file.automate.batch" { + let explicit = if params.get("steps").is_some() { + Some("steps") + } else if params.get("action").is_some_and(Value::is_array) { + Some("action") + } else { + None + }; + if let Some(key) = explicit { + if let Some(children) = params.get_mut(key).and_then(Value::as_array_mut) { + pending.extend(children.iter_mut().rev().filter_map(step_ref)); + } + continue; } - queue.push((i, depth)); } - // Conditional branches only take names, so there is nothing to rewrite. - "actions.conditional" => { - for key in ["then", "else"] { - let Some(name) = params.get(key).and_then(Value::as_str) else { continue }; - if let Some(i) = list.iter().position(|a| a.name == name) { - queue.push((i, depth)); + match id.as_str() { + "actions.play" | "file.automate.batch" => { + let resolved = + if id == "file.automate.batch" { crate::file_cmds::batch_action(list, params) } else { resolve(list, params, "actions.play") }; + let Ok(i) = resolved else { continue }; + if let (Some(action), Some(obj)) = (list.get(i), params.as_object_mut()) { + obj.insert("action".into(), json!(action.name)); + } + queue.push((i, depth)); + } + // Conditional branches only take names, so there is nothing to rewrite. + "actions.conditional" => { + for key in ["then", "else"] { + let Some(name) = params.get(key).and_then(Value::as_str) else { continue }; + if let Some(i) = list.iter().position(|a| a.name == name) { + queue.push((i, depth)); + } } } + _ => {} } - _ => {} } } } diff --git a/crates/engine/tests/it/atn_review_regressions.rs b/crates/engine/tests/it/atn_review_regressions.rs index ff6c680600..9f6c9998d8 100644 --- a/crates/engine/tests/it/atn_review_regressions.rs +++ b/crates/engine/tests/it/atn_review_regressions.rs @@ -606,6 +606,76 @@ fn batch_save_close_preserves_additional_images_and_allows_single_images() { } } +/// Droplet dependencies retain explicit Batch calls and rewrite their indices before packaging. +#[test] +fn called_actions_packages_explicit_batch_children_and_replays_them() { + let dir = tempfile::tempdir().unwrap(); + let input = dir.path().join("input.png"); + std::fs::write(&input, small_image(photocraft_codecs::Format::Png)).unwrap(); + let list = vec![Action::new("Decoy", vec![]), Action::new("Child", vec![("image.imageSize".into(), json!({"width":3,"height":1,"resample":"nearest"}))])]; + for field in ["steps", "action"] { + for shape in 0..3 { + let step = match shape { + 0 => json!(["actions.play", {"action":1}]), + 1 => json!({"command":"actions.play","params":{"action":1},"kept":17}), + _ => json!({"id":"actions.play","params":{"action":1},"kept":17}), + }; + let mut params = json!({"input":[input],"output":dir.path().join(format!("{field}-{shape}"))}); + params[field] = json!([step]); + if field == "steps" { + params["action"] = json!(0); + } + let mut steps = vec![("file.automate.batch".into(), params)]; + let captured = photocraft_engine::actions_cmds::called_actions(&list, &mut steps); + assert_eq!(captured.iter().map(|a| a.name.as_str()).collect::>(), ["Child"]); + let rewritten = &steps[0].1[field][0]; + let params = if shape == 0 { &rewritten[1] } else { &rewritten["params"] }; + assert_eq!(params["action"], "Child"); + if shape != 0 { + assert_eq!(rewritten["kept"], 17); + } + let mut s = Session::new(); + s.actions.list = captured; + let result = s.execute(&steps[0].0, steps[0].1.clone()).unwrap(); + assert_eq!(result["files"].as_array().unwrap().len(), 1, "{result}"); + let doc = photocraft_io::import("saved.png", &std::fs::read(result["files"][0].as_str().unwrap()).unwrap()).unwrap().document; + assert_eq!(doc.size, photocraft_doc::Size::new(3, 1)); + } + } +} + +/// JSON containers do not consume action-call depth; the existing cycle and sixteen-call limits remain. +#[test] +fn called_actions_nested_batch_shapes_keep_cycle_and_action_depth_bounds() { + let cycle = vec![Action::new("Child", vec![("actions.play".into(), json!({"action":0}))])]; + let mut step = json!(["actions.play", {"action":0}]); + for level in 0..64 { + let key = if level % 2 == 0 { "steps" } else { "action" }; + let mut params = json!({"kept":level}); + params[key] = json!([step]); + step = json!({"id":"file.automate.batch","params":params}); + } + let mut steps = vec![("file.automate.batch".into(), json!({"steps":[step]}))]; + let captured = photocraft_engine::actions_cmds::called_actions(&cycle, &mut steps); + assert_eq!(captured.len(), 1); + assert_eq!(captured[0].steps[0].1["action"], "Child"); + let mut node = &steps[0].1["steps"][0]; + for level in (0..64).rev() { + let params = &node["params"]; + assert_eq!(params["kept"], level); + node = ¶ms[if level % 2 == 0 { "steps" } else { "action" }][0]; + } + assert_eq!(node[1]["action"], "Child"); + let chain: Vec<_> = + (0..17).map(|i| Action::new(format!("A{i}"), if i == 16 { vec![] } else { vec![("actions.play".into(), json!({"action":i+1}))] })).collect(); + let mut steps = vec![("file.automate.batch".into(), json!({"steps":[["actions.play",{"action":0}]]}))]; + assert_eq!(photocraft_engine::actions_cmds::called_actions(&chain, &mut steps).len(), 16); + let mut invalid = vec![("file.automate.batch".into(), json!({"steps":null,"action":0}))]; + let before = invalid.clone(); + assert!(photocraft_engine::actions_cmds::called_actions(&chain, &mut invalid).is_empty()); + assert_eq!(invalid, before, "malformed explicit steps must not fall back to a named action"); +} + /// Scripts nested through Batch retain the caller's depth and cumulative spending across failed inputs. #[test] fn batch_carries_script_limits_and_restores_balanced_depth() { diff --git a/docs/atn.md b/docs/atn.md index b0b79d3198..50e8954487 100644 --- a/docs/atn.md +++ b/docs/atn.md @@ -266,8 +266,10 @@ kernel cells, scale 1..9999 and offset -9999..9999. Fibers uses the correspondin part of [#10620](https://github.com/storytold/photocraft/pull/10620), revision `33ee38b43165610b295e91f8b99841c591cf97b8`, with the now-measured variance and strength limits 1..64. Its seed retains all 32 bits. Both native commands already -support these ranges. Unrepresented types, settings, omitted fields or values -that would clamp stay complete preserved steps; export refuses native settings +support these ranges. Custom requires all 25 kernel cells; omitted scale and offset +use 1 and 0. Fibers defaults to variance 16 and strength 4 and captures an omitted seed +once for later playback and save. Unrepresented types, duplicate fields, settings or +values that would clamp stay complete preserved steps; export refuses native settings that the recorded form cannot retain. ### Color Lookup with an embedded LUT