diff --git a/apps/vectorcraft-cli/src/main.rs b/apps/vectorcraft-cli/src/main.rs index b064958d8a..6f44609423 100644 --- a/apps/vectorcraft-cli/src/main.rs +++ b/apps/vectorcraft-cli/src/main.rs @@ -390,7 +390,11 @@ fn mcp(args: &[String], safe_mode: bool) -> Result<(), String> { } else { match prefs_dir::prefs_dir() { Some(dir) => Box::new(LiveBackend::new(live_config(dir, when_closed))), - // No settings folder (no home folder): no window can be found either. + // No settings folder (no home folder): no window can be found either, so `--when-closed error` + // refuses rather than answering from a session the agent didn't ask for. + None if when_closed == Some(WhenAppClosed::Error) => { + return Err("VectorCraft's settings folder can't be found (no home folder), so its window can't be either: open VectorCraft, or run `mcp` without `--when-closed error`".into()); + } None => Box::new(with_stores(Headless::with_document())), } }; diff --git a/crates/mcp/src/lib.rs b/crates/mcp/src/lib.rs index fcae5e35fc..819eb48f14 100644 --- a/crates/mcp/src/lib.rs +++ b/crates/mcp/src/lib.rs @@ -6,7 +6,10 @@ //! //! - [`agents::LiveBackend`] (plain `vectorcraft-cli mcp`) sends each call to the open window //! while the ArtCraft Launcher's switch lets agents use VectorCraft, through the connection file -//! the window publishes ([`agents::Window`]), and otherwise to a [`Headless`] session. +//! the window publishes ([`agents::Window`]). While agents may not use it, calls go to a +//! [`Headless`] session; while they may but VectorCraft is closed, the closed-app choice of +//! Preferences › AI Integrations (or `--when-closed`) starts the app, uses a headless session in +//! the agent folders, or answers with an error. //! - [`Remote`] talks to a running desktop app through its loopback JSON-lines control channel //! (`vectorcraft --control 7979`, `vectorcraft-cli mcp --connect`, deprecated): one //! `{"id","method","params"}` line in, one `{"id","ok","result"|"error"}` line out. diff --git a/crates/ui-egui/src/prefs_dialog/model.rs b/crates/ui-egui/src/prefs_dialog/model.rs index 94cda29863..48d1b86f06 100644 --- a/crates/ui-egui/src/prefs_dialog/model.rs +++ b/crates/ui-egui/src/prefs_dialog/model.rs @@ -517,15 +517,27 @@ impl PreferencesDraft for Live<'_> { edited.iter().filter(|(k, _)| !k.starts_with("__") && *k != AGENTS_KEY).map(|(k, v)| (k.clone(), v.clone())).collect(); let touch_ws = values.get("touchWorkspace").and_then(Value::as_bool); let document_units = self.w.reset_units.as_deref().is_none_or(|u| values.get("unitsGeneral").and_then(Value::as_str) != Some(u)); - // A refused folder, typed or picked, comes back here as the engine's message. - if !values.is_empty() { - self.app.run("prefs.set", json!({ "values": values, "documentUnits": document_units }))?; - } - // The agent settings are saved where `vectorcraft-cli mcp` reads them, not with the preferences. - if let Some(agents) = edited.get(AGENTS_KEY) { + // The agent settings are saved where `vectorcraft-cli mcp` reads them, not with the preferences: only the + // fields changed in the window, so one `prefs.agents` changed while it is open keeps its new value. First, + // so a refused agent setting leaves the preferences unapplied too. + if let Some(agents) = edited.get(AGENTS_KEY).and_then(Value::as_object) { + let read = self.w.source.get::>(AGENTS_KEY).unwrap_or_default(); let given: Map = - agents.as_object().into_iter().flatten().filter(|(k, _)| *k != "allowAgentControl").map(|(k, v)| (k.clone(), v.clone())).collect(); - self.app.run("prefs.agents", Value::Object(given))?; + agents.iter().filter(|(k, v)| *k != "allowAgentControl" && read.get(*k) != Some(*v)).map(|(k, v)| (k.clone(), v.clone())).collect(); + if !given.is_empty() { + self.app.run("prefs.agents", Value::Object(given))?; + } + } + // A refused folder, typed or picked, comes back here as the engine's message. + if !values.is_empty() + && let Err(e) = self.app.run("prefs.set", json!({ "values": values, "documentUnits": document_units })) + { + // The agent settings were saved: the window shows them as applied, and Apply doesn't send them again. + if let Some(agents) = super::agent_settings(self.app) { + self.w.source.set(AGENTS_KEY, &agents); + self.w.opened.set(AGENTS_KEY, &agents); + } + return Err(e); } let row = |key: &str| edited.get(key).and_then(Value::as_bool); // When OK enters the Touch workspace, the task bar is set before the switch, so it goes to the workspace being diff --git a/crates/ui-egui/src/prefs_dialog/tests.rs b/crates/ui-egui/src/prefs_dialog/tests.rs index d40c97be54..4e7de7049b 100644 --- a/crates/ui-egui/src/prefs_dialog/tests.rs +++ b/crates/ui-egui/src/prefs_dialog/tests.rs @@ -1615,3 +1615,28 @@ fn ai_integrations_page_saves_the_agent_settings_on_ok() { assert_eq!(saved.len(), 1); assert_eq!(saved[0].when_app_closed, craft::automation::agents::WhenAppClosed::Error); } + +/// OK sends only the agent settings changed in the window: one changed through `prefs.agents` while Preferences is +/// open keeps its new value. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn ok_keeps_an_agent_setting_changed_while_preferences_is_open() { + use craft::automation::agents::{AgentSettings, AgentSwitch}; + use std::sync::{Arc, Mutex}; + let saved: Arc>> = Arc::default(); + let into = saved.clone(); + let save: crate::agents::SaveFn = Box::new(move |s| { + into.lock().unwrap().push(s.clone()); + Ok(()) + }); + let agents = crate::agents::Agents::new(None, "VectorCraft test".into(), AgentSwitch::fixed(true, false), AgentSettings::default(), save) + .with_default_folder(None); + let mut a = app().with_agents(agents); + open(&mut a, Some(crate::agents::PAGE_TITLE)); + a.run("prefs.agents", json!({"readRoot": "/elsewhere"})).unwrap(); + a.prefs.as_mut().unwrap().draft.set("integrations.whenAppClosed", &"error"); + confirm(&mut a).unwrap(); + let last = saved.lock().unwrap().last().cloned().unwrap(); + assert_eq!(last.read_root, "/elsewhere", "the window did not send back the folder it opened with"); + assert_eq!(last.when_app_closed, craft::automation::agents::WhenAppClosed::Error); +} diff --git a/docs/mcp.md b/docs/mcp.md index d42c070b49..16942c9f96 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -12,8 +12,8 @@ It has three ways to run: | Mode | How | What works | |---|---|---| -| **Open window** | `vectorcraft-cli mcp` while the ArtCraft Launcher's *Settings › AI agents* switch lets agents use VectorCraft (see [Agents in the open window](#agents-in-the-open-window)) | Everything. Each tool call goes to the open VectorCraft window, so you watch the app change live. | -| **Headless** | `vectorcraft-cli mcp --headless`, or plain `mcp` while agents may not use the open window | An in-process engine session with a CPU renderer. Everything except the UI-only tools (`inspect_ui` or `ui_inspect`, `type_text`, `answer_dialog`, `open_panel`, `screenshot {window:true}` or `ui_screenshot`). | +| **Open window** | `vectorcraft-cli mcp` while the ArtCraft Launcher's *Settings › AI agents* switch lets agents use VectorCraft (see [Agents in the open window](#agents-in-the-open-window)); `--automation-read-root` or `--automation-write-root` make it headless instead | Everything. Each tool call goes to the open VectorCraft window, so you watch the app change live. | +| **Headless** | `vectorcraft-cli mcp --headless` or with automation roots, or plain `mcp` while agents may not use the open window | An in-process engine session with a CPU renderer. Everything except the UI-only tools (`inspect_ui` or `ui_inspect`, `type_text`, `answer_dialog`, `open_panel`, `screenshot {window:true}` or `ui_screenshot`). | | **Remote** (deprecated) | `vectorcraft-cli mcp --connect 127.0.0.1:7979` (the app must run with `vectorcraft --control 7979` and `VECTORCRAFT_CONTROL_TOKEN`) | Everything, over the [control protocol](control-protocol.md). `--connect` prints a deprecation line and goes away in a later release. | Logs go to stderr; stdout carries only protocol messages. @@ -120,8 +120,9 @@ claude mcp add vectorcraft -- vectorcraft-cli mcp ``` For a live session, turn on *Settings › AI agents* in the ArtCraft Launcher and open VectorCraft as usual (Dock, -Start menu, desktop launcher). The server finds the window whenever it opens; see [Agents in the open -window](#agents-in-the-open-window). +Start menu, desktop launcher). The server finds the window whenever it opens, unless the session already went headless +(a call made while VectorCraft was closed, with the headless closed-app choice); such a session needs a new +MCP session to use the window. See [Agents in the open window](#agents-in-the-open-window). ## Confining file access diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index b430d5c7d2..912ef49e23 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1183,7 +1183,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Parameter keys: the engine refuses a key that a command's params description doesn't name before the command runs, in every build and for every caller, including the app's own calls, the commands a command runs and the steps of `command.batch` and `action.batch`. An action recorded by an older version still plays from the Actions panel and the Batch dialog (`Session::replay`). `crates/engine/tests/it/param_keys.rs`. - Parameter key lookups: the engine reads the params description of each built-in command, with the descriptions its `…` entries name, once per process, and answers the key check and the `ids` and `id` questions of each later command from that reading. A plug-in command's description, and a description whose `…` entries name a plug-in command, are still read each time. The accepted keys, the answers and the error messages are the same. In a release build on a one-object document, `object.move` with `ids` and `dx` takes 48 µs of thread CPU time per command instead of 61 µs, and `select.set` with `ids` 2.3 µs instead of 3.2 µs (medians of 7 alternating runs of 20,000 commands in one process, measured while other builds were running; `tests::params_cache::bench_params_keys_once`). The time is saved per command, in scripts, batches and MCP or plug-in call loops, and does not grow with the size of the document. `crates/engine/src/tests/params_cache.rs` compares every built-in command's answers with those of a fresh parse. - Effect parameter types: `effect.apply`, `appearance.addEffect` and `effect.setParams` refuse an effect parameter of another type than the effect reads, for keys only the params text names too (the earlier 3D `perspective`, `lightAzimuth` and `lightElevation`, Scribble's `variation`, Outline Stroke's `width`, Gaussian Blur's `linear`), a Crop Marks `style` other than `roman` or `japanese`, and an installed plug-in effect's parameter of another type than its manifest gives it, in a `command.batch` step too, with the document and undo history unchanged; a replayed action still plays. `effect.setParams` checks the parameters against a Warp's new style, and refuses a `visible` that is not true or false, in a replayed action too. `crates/engine/src/tests/params.rs` (`an_effect_parameter_without_a_default_is_held_to_its_type`, `effect_commands_refuse_an_undefaulted_parameter_of_the_wrong_type`), `crates/engine/src/tests/plugins.rs` (`live_effect_plugins_reshape_editable_art_and_expand`), `crates/engine/src/cmd/effectcmd/tests.rs` (`set_params_refuses_a_visible_that_is_not_true_or_false`, `set_params_checks_the_effect_it_leaves`). - - MCP server with 34 tools (drawing, text, effects, Pathfinder, transforms, graphs, text wrap, export, screenshots, undo/redo, UI inspect/screenshot aliases, any command, agent folders), attached to the open window through the ArtCraft Launcher's AI agents switch or headless (#10621). Headless and remote `command_list` include desktop `UI_COMMANDS` (`ui: true`); a UI-only id needs the open window to run. Eight prompts (poster, icon-set, recolor, trace-and-style, export-set, check-spelling, variables-bind, path-tools). Effect ids are not command ids. Prefer a CLI built from the checkout when a release binary lags (#6460). `vectorcraft://commands/reference` serves the API reference. Tool descriptions and replies agree with the engine: format lists come from its format table, `save_file` says it keeps the document's own format, `inspect_document` reports the paint type's characters show, and selection exports report the encoder's warnings. `--automation-read-root` / `--automation-write-root` (on `vectorcraft-cli mcp` and on `vectorcraft --control`) confine the files commands read and write to chosen folders, links followed, with the flags PhotoCraft takes (#832); each flag may be given more than once, a path inside any of its folders being allowed (#3546, `file_access::tests::several_roots_of_a_kind_each_grant_their_access`, `cli::mcp_automation_roots_take_several_folders`). A long export can be cancelled (`notifications/cancelled` stops it at the next page or group and writes nothing), reports page progress to a client that sent a progress token, and does not make other requests wait: engine-free ones answer at once, the rest are refused as busy after two seconds (#2165, #2171, #2172). + - MCP server with 34 tools (drawing, text, effects, Pathfinder, transforms, graphs, text wrap, export, screenshots, undo/redo, UI inspect/screenshot aliases, any command, agent folders), attached to the open window through the ArtCraft Launcher's AI agents switch or headless (#10621). Headless and remote `command_list` include desktop `UI_COMMANDS` (`ui: true`); a UI-only id needs the open window to run. Eight prompts (poster, icon-set, recolor, trace-and-style, export-set, check-spelling, variables-bind, path-tools). Effect ids are not command ids. Prefer a CLI built from the checkout when a release binary lags (#6460). `vectorcraft://commands/reference` serves the API reference. Tool descriptions and replies agree with the engine: format lists come from its format table, `save_file` says it keeps the document's own format, `inspect_document` reports the paint type's characters show, and selection exports report the encoder's warnings. `--automation-read-root` / `--automation-write-root` (on `vectorcraft-cli mcp` and on `vectorcraft --control`) confine the files commands read and write to chosen folders, links followed, with the flags PhotoCraft takes (#832); each flag may be given more than once, a path inside any of its folders being allowed (#3546, `file_access::tests::several_roots_of_a_kind_each_grant_their_access`, `cli::mcp_automation_roots_take_several_folders`). A long export can be cancelled (headless, `notifications/cancelled` stops it at the next page or group and writes nothing; in the open window it only stops the call waiting, and the app may still finish and write the export), reports page progress to a client that sent a progress token, and does not make other requests wait: engine-free ones answer at once, the rest are refused as busy after two seconds (#2165, #2171, #2172). - MCP window captures from an app that does not send the image back go through a file in a new folder inside the system's temporary folder, which only the user can open on Unix, instead of a fixed name in the shared folder; the folder is removed after the read, also when the capture or the read fails (`tests::conventions::an_old_app_captures_the_window_in_a_new_private_folder`). - MCP protocol: five prompts (`prompts/list` / `prompts/get`: poster, icon set, recolour, trace-and-style, export set), `completion/complete` for prompt arguments and template variables read from the live catalogues (formats, effect ids, command ids, trace presets, swatch and object ids), four resource templates (`vectorcraft://object/{id}`, `//command/{id}`, `//effect/{id}`, `//swatch/{name}`) so an agent reads one thing instead of the whole document, and `logging/setLevel` with `notifications/message`. `window.panel` ids are case-insensitive (display labels work too), `vectorcraft://object/{id}` reads through `document.node {summary: true}` (the same reply bytes from one lookup instead of summarizing the whole document: a leaf read on a 2,000-object document drops from ~20 ms to ~0.06 ms), large containers slice with `document.node {depth?, childLimit?}` (truncated levels report `childCount`), `document.inspect` and the `inspect_document` tool slice the same way so a depth-0 read is the layer skeleton (~9.7 KB instead of ~2 MB on a 2,000-object document), `document.find` locates nodes by name/kind/text with ancestor paths, and the CLI sweeps the protocol surface over stdio against the compiled binary. `docs/mcp.md` has a Protocol section covering the capability table and what is deliberately absent (subscriptions, progress, cancellation, sampling, elicitation, Streamable HTTP — all of which need the transport to interleave reads). - Headless CLI (`vectorcraft-cli run`, `convert`, `info`, `bench`, `perf`, `mcp`). A `run` step, like a `command_batch` step over MCP, can use an earlier step's result (`"$1.id"`, #785); `run --commands FILE` (or `-` for standard input) reads the steps from a JSON array or JSON Lines, past the Windows command-line limit (#3147, `apps/vectorcraft-cli/tests/it/cli.rs` `run_reads_*`, `run_refuses_malformed_commands_naming_the_step`).