From cb0aab2fe50e8cbc2b2168c5ead50877cd351613 Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 4 Dec 2026 17:28:23 +0100 Subject: [PATCH] feat(automation): a project an agent opens in the window reads footage only in the agent folders Closes the window's one gap against headless agent sessions (#4387): while an agent's request or queued input runs, a project it opens wraps the session's footage source in the agent folders' ConfinedFootage. The next project the person opens reads footage as before. Co-Authored-By: Claude Opus 5.5 --- apps/effectcraft/src/main.rs | 2 ++ crates/engine/src/lib.rs | 12 +++++++++++ crates/engine/src/session/edit.rs | 9 ++++++++ crates/ui-egui/src/agents.rs | 2 +- crates/ui-egui/src/app/frame.rs | 15 +++++++++----- crates/ui-egui/src/control.rs | 2 ++ crates/ui-egui/src/lib.rs | 6 +++--- crates/ui-egui/src/lifecycle_tests.rs | 30 ++++++++++++++++++++++++++- docs/agents.md | 7 ++++--- 9 files changed, 72 insertions(+), 13 deletions(-) diff --git a/apps/effectcraft/src/main.rs b/apps/effectcraft/src/main.rs index cc436b59c..7e6f945e0 100644 --- a/apps/effectcraft/src/main.rs +++ b/apps/effectcraft/src/main.rs @@ -199,10 +199,12 @@ fn agent_scope(folders: &craft_control::agents::AgentFolders) -> Result, + /// Set with the [`Session::command_gate`] while an agent's request or input runs (it stays set + /// while the gate is taken for the command itself): a project opened then + /// reads its footage through this wrapper (the agent folders), as a headless agent session does + /// (#4387). The person's next project reads through [`Session::footage`] as before. + pub agent_footage: Option, + /// The footage source from before an agent's project wrapped it ([`Session::agent_footage`]). + pub unconfined_footage: Option>, } +/// Wraps the session's footage source for a project an agent opened ([`Session::agent_footage`]). +pub type FootageWrap = Arc) -> Arc + Send + Sync>; + /// A check run before a command (id, params), with the session to look things up in: `Err` refuses /// the command with that reason. While it runs, the session has no gate, so the commands it runs /// to look things up aren't checked again. @@ -440,6 +450,8 @@ impl Default for Session { script_calls: ScriptCalls::default(), output_guards: Default::default(), command_gate: None, + agent_footage: None, + unconfined_footage: None, } } } diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index 19001796d..6055c6033 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -232,6 +232,15 @@ impl Session { } self.drop_crypto_probe(); self.layer_cache.clear(); + // A project an agent opens reads its footage only inside the agent folders; the next one + // the person opens reads it as before. + if let Some(own) = self.unconfined_footage.take() { + self.footage = own; + } + if let Some(wrap) = self.agent_footage.clone() { + self.unconfined_footage = Some(self.footage.clone()); + self.footage = wrap(self.footage.clone()); + } self.project = Arc::new(p); // The set of footage effects read the channels of is sent again, even when it is empty. self.aux_footage = None; diff --git a/crates/ui-egui/src/agents.rs b/crates/ui-egui/src/agents.rs index 6bb77c8bb..d6043495b 100644 --- a/crates/ui-egui/src/agents.rs +++ b/crates/ui-egui/src/agents.rs @@ -171,7 +171,7 @@ mod tests { fn scope(_: &AgentFolders) -> Result { let gate: effectcraft_engine::CommandGate = Arc::new(|_: &mut effectcraft_engine::Session, _: &str, _: &Value| Ok(())); - Ok(AgentScope { gate, check_write: Box::new(|_| Ok(())), folders: json!({}) }) + Ok(AgentScope { gate, check_write: Box::new(|_| Ok(())), folders: json!({}), footage: Arc::new(|f| f) }) } fn agents(dir: &std::path::Path, switch: AgentSwitch) -> Agents { diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index d48de2dbd..e8562aed6 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -65,13 +65,17 @@ impl EffectcraftApp { // pass has applied it (`agent_input`). let queued = self.synthetic.len(); let gate = req.agent.as_ref().map(|scope| scope.gate.clone()); + let footage = req.agent.as_ref().map(|scope| scope.footage.clone()); // The person's own channel (`--control`) isn't confined, even while an agent's input waits. - let outer = std::mem::replace(&mut self.session.command_gate, gate.clone()); + let outer = (std::mem::replace(&mut self.session.command_gate, gate.clone()), std::mem::replace(&mut self.session.agent_footage, footage.clone())); let outcome = control::handle(self, ctx, &req, now); - self.session.command_gate = outer; - if gate.is_some() && self.synthetic.len() > queued { - self.agent_input = gate; - self.session.command_gate = self.agent_input.clone(); + (self.session.command_gate, self.session.agent_footage) = outer; + if let Some(gate) = gate + && self.synthetic.len() > queued + { + self.session.command_gate = Some(gate.clone()); + self.session.agent_footage = footage.clone(); + self.agent_input = Some((gate, footage)); } match outcome { control::Outcome::Done(v) => { @@ -499,6 +503,7 @@ impl EffectcraftApp { // The agent's input has all been applied: what runs next is the person's again. if self.synthetic.is_empty() && self.agent_input.take().is_some() { self.session.command_gate = None; + self.session.agent_footage = None; } // `AfterInput` replies wait for the final (non-discarded) pass that applied the last // synthetic event: a hidden request is answered only after its forced pass, and a pass diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index bec84a92c..61e7e54bf 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -71,6 +71,8 @@ pub struct AgentScope { pub check_write: Box Result<(), String> + Send + Sync>, /// The `agent_folders` answer: the folders the requests may use. pub folders: Value, + /// The footage a project they open reads: only inside the agent folders (#4387). + pub footage: effectcraft_engine::FootageWrap, } impl ControlRequest { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index c4a9297a2..6abfd9d67 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -390,9 +390,9 @@ pub struct EffectcraftApp { /// Agents in the open window through the ArtCraft Launcher's switch ([`agents::Agents`]). #[cfg(not(target_arch = "wasm32"))] pub(crate) agents: Option, - /// The gate of the agent whose queued input is still being applied: the commands that input - /// runs are the agent's, and stay inside its agent folders. - pub(crate) agent_input: Option, + /// The gate and footage wrapper of the agent whose queued input is still being applied: the + /// commands that input runs are the agent's, and stay inside its agent folders. + pub(crate) agent_input: Option<(effectcraft_engine::CommandGate, Option)>, pub(crate) deferred: Vec<(ControlRequest, f64)>, /// Requests a tick left to the next one when it ran out of [`EffectcraftApp::control_budget`], /// in the order they came, with their retry deadline once they ran (#3328). diff --git a/crates/ui-egui/src/lifecycle_tests.rs b/crates/ui-egui/src/lifecycle_tests.rs index 5645c0355..b8ebc5b11 100644 --- a/crates/ui-egui/src/lifecycle_tests.rs +++ b/crates/ui-egui/src/lifecycle_tests.rs @@ -718,7 +718,7 @@ fn agent_scope(root: &std::path::Path) -> Arc { let gate_inside = inside.clone(); let gate: effectcraft_engine::CommandGate = Arc::new(move |_: &mut Session, _: &str, params: &serde_json::Value| params.get("path").and_then(|p| p.as_str()).map_or(Ok(()), &gate_inside)); - Arc::new(control::AgentScope { gate, check_write: Box::new(inside), folders: json!({"write_folder": root}) }) + Arc::new(control::AgentScope { gate, check_write: Box::new(inside), folders: json!({"write_folder": root}), footage: Arc::new(|f| f) }) } /// Queues an agent's request with `scope`; its reply. @@ -735,6 +735,34 @@ fn send_agent( reply } +#[test] +fn a_project_an_agent_opens_reads_footage_through_its_folders_and_the_persons_next_does_not() { + let ctx = headless(); + let frame = eframe::Frame::_new_kittest(); + let mut app = app_with_comp(); + let own = app.session.footage.clone(); + let agent_dir = tempfile::tempdir().unwrap(); + let project = agent_dir.path().canonicalize().unwrap().join("agent.ecproj"); + app.session.execute("file.saveAs", json!({"path": project.to_string_lossy()})).unwrap(); + // The agent's footage source stands in for the desktop app's `ConfinedFootage`. + let confined: Arc = Arc::new(effectcraft_engine::render::NoFootage); + let wrapped = confined.clone(); + let mut scope = Arc::into_inner(agent_scope(agent_dir.path())).unwrap(); + scope.footage = Arc::new(move |_| wrapped.clone()); + let scope = Arc::new(scope); + let open = json!({"command": "file.open", "params": {"path": project.to_string_lossy()}}); + let reply = send_agent(&mut app, &scope, "engine.execute", open.clone()); + hidden_tick(&mut app, &ctx, &frame); + assert_eq!(reply.try_recv().unwrap()["ok"], true); + assert!(Arc::ptr_eq(&app.session.footage, &confined), "the agent's project reads footage outside its folders"); + assert!(app.session.agent_footage.is_none(), "the agent's footage wrapper outlived its request"); + // The person opens a project: its footage is read as before. + let reply = send(&mut app, "engine.execute", open); + hidden_tick(&mut app, &ctx, &frame); + assert_eq!(reply.try_recv().unwrap()["ok"], true); + assert!(Arc::ptr_eq(&app.session.footage, &own), "the person's project reads footage through the agent's folders"); +} + #[test] fn an_agents_request_stays_inside_its_agent_folders() { let ctx = headless(); diff --git a/docs/agents.md b/docs/agents.md index 81d066cae..c22a130d6 100644 --- a/docs/agents.md +++ b/docs/agents.md @@ -46,13 +46,14 @@ switch names EffectCraft (behind the suite's `AI-MCP` flag): never uses the window. So do `--autosave` and the project options (`--project`, `--demo`, `--empty`). - **Agent folders.** Agent requests open files only from the agent folders and save only into - them, in the window too (one exception: in the window, footage that a project opened from the agent - folder already names is read wherever it is; #4387): the Automation Read and Write Folders on the Agents page, or + them, in the window too: the Automation Read and Write Folders on the Agents page, or `Documents/ArtCraft/Agents/EffectCraft` (created when first used) when they are empty. The checks are the same as for `--automation-read-root` / `--automation-write-root` (see [MCP conventions](mcp.md#confining-file-access)). They cover each command an agent runs, and the commands its queued keys and clicks run until they have been applied. A screenshot or frame - without a file comes back inline. A folder that can't be used means no file access, never + without a file comes back inline. A project an agent opens reads its footage only inside the + agent folders, as a headless session does: footage outside them shows as missing until the person + opens a project themselves. A folder that can't be used means no file access, never unrestricted access, and the Agents page says why. The `agent_folders` tool names the folders the session really uses. Agents can't change the agent settings; only the person can. `--automation-read-root` / `--automation-write-root` on plain `mcp` bind calls to the window too: