PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(automation): bridge doc_save without path writes back like headless (#946) (#1704)
MarekSpodBiedry1fd600d3adc1
diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rsindex e52343b69..61c439d05 100644--- a/crates/automation/src/server.rs+++ b/crates/automation/src/server.rs@@ -670,11 +670,9 @@ impl PhotocraftMcp {async fn save_impl(&self, p: SaveParams) -> Result<CallToolResult, McpError> {if let Some(b) = self.bridge_client() {- let Some(path) = p.path else {- return Ok(fail("bridge mode needs `path`"));- };- // The bridge forwards to the running app's `app.save`, which takes a path only:- // the extra options are headless-only. Saying so beats saving with defaults+ // The bridge forwards to the running app's `app.save`, which takes only an optional+ // path (without one it writes back to the document's own layered file, as headless+ // does), so the extra options are headless-only. Saying so beats saving with defaults// while the caller believes their quality or format was applied.let unsupported: Vec<&str> = [p.format.is_some().then_some("format"),@@ -688,7 +686,11 @@ impl PhotocraftMcp {if !unsupported.is_empty() {return Ok(fail(format!("bridge mode saves with the app's current settings; `{}` need headless mode", unsupported.join("`, `"))));}- return to_result(b.call("app.save", json!({"path": path})).await);+ let params = match p.path {+ Some(path) => json!({"path": path}),+ None => json!({}),+ };+ return to_result(b.call("app.save", params).await);}let Some(r) = self.headless_op(move |h| {diff --git a/crates/automation/tests/mcp.rs b/crates/automation/tests/mcp.rsindex f3effb035..b2ff392fb 100644--- a/crates/automation/tests/mcp.rs+++ b/crates/automation/tests/mcp.rs@@ -288,6 +288,20 @@ async fn bridge_doc_save_rejects_headless_only_options_instead_of_saving_with_de}}+/// #946: like headless mode, a bridged `doc_save` without `path` reaches the app's `app.save`,+/// which writes back to the document's own PSD, PSB or .pcraft file or refuses with its own error.+#[tokio::test(flavor = "multi_thread")]+async fn bridge_doc_save_without_path_forwards_app_save_for_write_back() {+ let (addr, app) = fake_app().await;+ let client = connect(PhotocraftMcp::bridge(&addr, CONTROL_TOKEN).unwrap()).await;+ let saved = json_of(&call(&client, "doc_save", json!({})).await);+ assert_eq!(saved, json!({"saved": {}}), "no `path` is sent, not even null");+ let saved = json_of(&call(&client, "doc_save", json!({"path": "out.psd"})).await);+ assert_eq!(saved, json!({"saved": {"path": "out.psd"}}));+ client.cancel().await.unwrap();+ app.abort();+}+#[tokio::test(flavor = "multi_thread")]async fn bridge_response_budget_drops_connection_without_retrying_the_operation() {use photocraft_automation::{BridgeClient, budgets::MAX_RESPONSE_BYTES};@@ -486,6 +500,7 @@ async fn fake_app_with_screenshot(screenshot_png: Option<Vec<u8>>) -> (String, t"engine.execute" => {json!({"id": id, "ok": true, "result": {"ran": req["params"]["command"], "params": req["params"]["params"], "wait": req["params"]["wait"]}})}+ "app.save" => json!({"id": id, "ok": true, "result": {"saved": req["params"]}}),"engine.commands" => {json!({"id": id, "ok": true, "result": [{"id": "file.new", "label": "New…", "enabled": true}]})}diff --git a/docs/control-protocol.md b/docs/control-protocol.mdindex 86110fcf4..5dde86f21 100644--- a/docs/control-protocol.md+++ b/docs/control-protocol.md@@ -228,7 +228,7 @@ How each MCP tool maps onto control methods in bridge mode:| `doc_new {…}` | `engine.execute {command: "file.new", params}` || `doc_inspect` | `engine.execute {command: "document.inspect"}` || `doc_open {path}` | `app.open {path}` |-| `doc_save {path}` / `doc_export {path}` | `app.save {path}` |+| `doc_save {path?}` / `doc_export {path}` | `app.save {path?}` || `doc_render_preview {max_side?}` | `ui.screenshot`, returned directly as PNG image content || `session_list`, `ui_inspect` | `ui.inspect` || `ui_screenshot {max_side?}` | `ui.screenshot`, returned as PNG image content |