mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Agents: Preferences saves no agent settings once automation drove the window
When input injected through the control channel reached the open Preferences window, an edit on AI Integrations (such as a read or write folder) could reach the draft and be saved later by a person's OK or Apply, outside the automation roots. That let an agent driving the UI widen the folders it may open from and save to. The window now records that automation drove it. While it stays open, OK and Apply put the agent settings back as they were read and refuse them with a message; the person changes them in a Preferences window they open again. An OK or Apply that a control request sends is refused without touching the draft.
This commit is contained in:
1 parent
a9e223e425
commit
a010cd9ab4
3 files changed
+95
No files matched your search
@@ -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<script_access::Access>,
|
||||
/// 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);
|
||||
|
||||
@@ -524,6 +524,19 @@ impl PreferencesDraft for Live<'_> {
|
||||
let read = self.w.source.get::<Map<String, Value>>(AGENTS_KEY).unwrap_or_default();
|
||||
let given: Map<String, Value> =
|
||||
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))?;
|
||||
}
|
||||
|
||||
@@ -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<Mutex<Vec<AgentSettings>>> = 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::<String>("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<Mutex<Vec<AgentSettings>>> = 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::<String>("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"))]
|
||||
|
||||
Reference in new issue
Block a user