diff --git a/crates/ui-egui/src/prefs_dialog.rs b/crates/ui-egui/src/prefs_dialog.rs index 193ea99ec9..eaacf7880e 100644 --- a/crates/ui-egui/src/prefs_dialog.rs +++ b/crates/ui-egui/src/prefs_dialog.rs @@ -64,6 +64,9 @@ pub struct PrefsWindow { /// General › Script File Access: the file access kept with Always, read when the window opens; `None` where the /// session keeps none (no settings folder, and the web). script_access: Option, + /// Automation drove the window: a pass that carried injected input showed it. OK then saves no agent setting + /// changed in it until the window closes ([`model::Live`]'s `apply`). + automated: bool, } impl PrefsWindow { @@ -248,6 +251,7 @@ pub fn open(app: &mut VectorcraftApp, category: Option<&str>) { reset_asked: None, strings: Default::default(), script_access, + automated: false, }; w.state.selected = selected.to_string(); w.sync_strings(); @@ -257,6 +261,7 @@ pub fn open(app: &mut VectorcraftApp, category: Option<&str>) { /// One pass of the window, while it is open. The window leaves `app` for the pass so its model can use the app. pub fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui) { let Some(mut w) = app.prefs.take() else { return }; + w.automated |= app.automation_is_acting(); // Rows drawn by `PathSetting` edit the draft directly. w.sync_strings(); let mut state = std::mem::take(&mut w.state); diff --git a/crates/ui-egui/src/prefs_dialog/model.rs b/crates/ui-egui/src/prefs_dialog/model.rs index 2cc8b0c387..59b5d9e15f 100644 --- a/crates/ui-egui/src/prefs_dialog/model.rs +++ b/crates/ui-egui/src/prefs_dialog/model.rs @@ -524,6 +524,19 @@ impl PreferencesDraft for Live<'_> { let read = self.w.source.get::>(AGENTS_KEY).unwrap_or_default(); let given: Map = agents.iter().filter(|(k, v)| *k != "allowAgentControl" && read.get(*k) != Some(*v)).map(|(k, v)| (k.clone(), v.clone())).collect(); + // Agents can't widen the folders they work in. Once automation drove the window, its agent settings are + // put back as they were read for as long as it stays open (a field may still hold injected text), and OK + // applies nothing. An OK automation sends is refused and leaves the draft as it is. + if !given.is_empty() && self.app.automation_is_acting() { + return Err(tl!("Agents can't change the agent settings: the person sets them in Preferences › AI Integrations.").into()); + } + if !given.is_empty() && self.w.automated { + self.w.draft.set(AGENTS_KEY, &read); + return Err(tl!( + "The agent settings were changed while automation used this window, so they were put back: only the person at the keyboard can change them. Close Preferences and open it again to change them." + ) + .into()); + } if !given.is_empty() { self.app.run("prefs.agents", Value::Object(given))?; } diff --git a/crates/ui-egui/src/prefs_dialog/tests.rs b/crates/ui-egui/src/prefs_dialog/tests.rs index 4e7de7049b..48691220a7 100644 --- a/crates/ui-egui/src/prefs_dialog/tests.rs +++ b/crates/ui-egui/src/prefs_dialog/tests.rs @@ -1616,6 +1616,83 @@ fn ai_integrations_page_saves_the_agent_settings_on_ok() { assert_eq!(saved[0].when_app_closed, craft::automation::agents::WhenAppClosed::Error); } +/// Once input injected through the control channel has reached the open window, OK does not save agent settings +/// changed in it while it stays open: an edit made there (such as a folder typed on AI Integrations that lands in a +/// later pass) is undone and OK says why. In a window the person opens again, OK saves what they enter. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn ok_does_not_save_agent_settings_edited_while_automation_drove_the_window() { + use craft::automation::ControlRequest; + 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 (tx, rx) = std::sync::mpsc::channel(); + let mut a = crate::testing::windowed(VectorcraftApp::new(Session::new(), Services::default()).with_control(rx)).with_agents(agents); + let ctx = crate::testing::ui_context(); + open(&mut a, Some(crate::agents::PAGE_TITLE)); + crate::testing::app_frame(&mut a, &ctx, egui::RawInput::default()); + let (req, _reply) = ControlRequest::new("ui.key", json!({"key": "A"})); + tx.send(req).unwrap(); + a.run_control(&ctx); + for _ in 0..3 { + crate::testing::app_frame(&mut a, &ctx, egui::RawInput::default()); + } + assert!(is_open(&a)); + let read_root = |a: &VectorcraftApp| a.prefs.as_ref().and_then(|w| w.draft.get::("integrations.readRoot")); + a.prefs.as_mut().unwrap().draft.set("integrations.readRoot", &"/everything"); + let refused = confirm(&mut a).expect_err("the agent folder edited while automation drove the window is not saved"); + assert!(refused.contains("person at the keyboard"), "{refused}"); + assert!(saved.lock().unwrap().is_empty(), "nothing saved"); + assert!(is_open(&a), "the window stays open"); + assert_ne!(read_root(&a).as_deref(), Some("/everything"), "the edit is undone in the window"); + a.prefs.as_mut().unwrap().draft.set("integrations.readRoot", &"/later"); + a.ui.answering_control = true; + assert!(confirm(&mut a).is_err(), "an OK automation sends"); + a.ui.answering_control = false; + assert_eq!(read_root(&a).as_deref(), Some("/later"), "an OK automation sends leaves the draft as it is"); + assert!(confirm(&mut a).is_err(), "the window stays guarded while it is open"); + assert!(saved.lock().unwrap().is_empty(), "still nothing saved"); + cancel(&mut a); + open(&mut a, Some(crate::agents::PAGE_TITLE)); + a.prefs.as_mut().unwrap().draft.set("integrations.readRoot", &"/chosen"); + confirm(&mut a).unwrap(); + assert_eq!(saved.lock().unwrap().last().map(|s| s.read_root.clone()), Some("/chosen".to_string()), "the person's own edit"); +} + +/// An OK a control request sends saves no agent setting the person changed, and leaves their edit in the window. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn ok_sent_by_automation_keeps_the_persons_agent_edit_unsaved_in_the_window() { + 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.prefs.as_mut().unwrap().draft.set("integrations.readRoot", &"/mine"); + a.ui.answering_control = true; + let r = confirm(&mut a); + a.ui.answering_control = false; + assert!(r.is_err(), "{r:?}"); + assert!(saved.lock().unwrap().is_empty()); + assert_eq!(a.prefs.as_ref().and_then(|w| w.draft.get::("integrations.readRoot")).as_deref(), Some("/mine")); + confirm(&mut a).unwrap(); + assert_eq!(saved.lock().unwrap().last().map(|s| s.read_root.clone()), Some("/mine".to_string())); +} + /// 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"))] diff --git a/docs/mcp.md b/docs/mcp.md index e96c805329..2ce9d4f326 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -47,7 +47,11 @@ only the folders given on its command line, as before. Preferences › AI Integrations shows the switch's state, the port agents connect on, Control Port (0: any free port), the two folders and the closed-app choice. Without the `AI-MCP` flag it only says that agents use VectorCraft without a window. The same settings are the UI command `prefs.agents {controlPort?, readRoot?, -writeRoot?, whenAppClosed?}`, which agents' own requests can read but not change. Started with `--control`, +writeRoot?, whenAppClosed?}`, which agents' own requests can read but not change. Once input injected through +the control channel has reached the open Preferences window, OK and Apply in that window put the agent +settings back as they were read and save none of them; the person changes them in a Preferences window +they open again. An OK or Apply sent by a control request is refused when it would change an agent setting, +and leaves the window's draft as it is. Started with `--control`, the window serves that channel for the whole run instead, and the page says so. ### Command catalogue (headless vs remote)