From 4604cfc4bb9ab26f4ffa6bc6d239ae215513f3e7 Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 23 Oct 2026 00:24:13 +0200 Subject: [PATCH] Address CodeRabbit review: doc.replaceSmartObject restores the active document (#8150) `index` targets one call (#7025): replace_smart_object now runs through on_doc like place and the data-set methods, so the previously active document is active again on success, on error and on the trusted-local path. The index test covers a successful and a failing indexed replace. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/automation/src/headless.rs | 32 +++++++++++++++---------------- crates/automation/src/rpc.rs | 5 ++++- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rs index 738b99843e..7348a8cbeb 100644 --- a/crates/automation/src/headless.rs +++ b/crates/automation/src/headless.rs @@ -156,24 +156,22 @@ impl Headless { /// `layer.smartObjects.replaceContents`, which takes an ambient `path` and is refused in /// automation (#8061). `params` is the command's own (`layer`). pub fn replace_smart_object(&mut self, index: Option, path: &str, params: &Value) -> Result { - let i = self.doc_index(index)?; - if self.session.active_index() != Some(i) && !self.session.set_active(i) { - return Err(AutomationError::BadRequest(format!("no document at index {i}"))); - } - let mut opts = params.as_object().cloned().unwrap_or_default(); - opts.insert("path".into(), json!(path)); - if matches!(&self.filesystem, Filesystem::TrustedLocal) { - return self.command_run("layer.smartObjects.replaceContents", Value::Object(opts)); - } - let bytes = match &self.filesystem { - Filesystem::Workspace(workspace) => workspace.read(path)?, - _ => { - return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into())); + self.on_doc(index, |this| { + let mut opts = params.as_object().cloned().unwrap_or_default(); + opts.insert("path".into(), json!(path)); + if matches!(&this.filesystem, Filesystem::TrustedLocal) { + return this.command_run("layer.smartObjects.replaceContents", Value::Object(opts)); } - }; - let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path); - self.sync_jobs(); - Ok(photocraft_engine::smart_cmds::replace_contents_from_bytes(&mut self.session, &Value::Object(opts), name, bytes)?) + let bytes = match &this.filesystem { + Filesystem::Workspace(workspace) => workspace.read(path)?, + _ => { + return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into())); + } + }; + let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path); + this.sync_jobs(); + Ok(photocraft_engine::smart_cmds::replace_contents_from_bytes(&mut this.session, &Value::Object(opts), name, bytes)?) + }) } /// Export each exported layer of a document as its own file beneath the write root: the diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs index 19de89e7c7..0b388ff76d 100644 --- a/crates/automation/src/rpc.rs +++ b/crates/automation/src/rpc.rs @@ -880,7 +880,7 @@ mod tests { } #[test] - fn indexed_place_and_data_set_methods_keep_the_active_document() { + fn indexed_place_replace_and_data_set_methods_keep_the_active_document() { let base = std::env::temp_dir().join(format!("pc-doc-index-active-{}", std::process::id())); let root = base.join("workspace"); std::fs::create_dir_all(&root).unwrap(); @@ -897,6 +897,9 @@ mod tests { let steps = [ ("doc.place", json!({"path": "logo.png", "index": 1}), true), + // Document 1's active layer is now the placed Smart Object. + ("doc.replaceSmartObject", json!({"path": "logo.png", "index": 1}), true), + ("doc.replaceSmartObject", json!({"path": "missing.png", "index": 1}), false), ("doc.importDataSets", json!({"path": "sets.csv", "index": 1}), true), ("doc.exportLayers", json!({"dir": "layers", "index": 1}), true), ("doc.exportDataSets", json!({"dir": "sets", "index": 1}), true),