Files
effectcraft/crates/engine/tests/it/command_gate.rs
T
David MannandClaude Opus 5.5 308381360b feat(automation): agents in the open window through the ArtCraft Launcher's switch
While the launcher's Settings > AI agents switch names EffectCraft (behind
the suite's AI-MCP flag), the window serves agents through the shared
craft_automation::agents::AgentLink (token, connection file, switch
watching) and plain `effectcraft-cli mcp` finds it on every tool call
through the shared craft_control::agents::Live: the open window while
agents may use it, otherwise a session without a window, and the
closed-app choice (launch, headless, error) when EffectCraft is closed.

- The CLI signs in with the challenge handshake; the token never crosses
  the wire and a listener that can't prove it holds it gets nothing.
- Agent requests in the window stay inside the agent folders: a command
  gate on the session (Session::command_gate) runs the same Confinement
  checks as --automation-read-root/--automation-write-root, also for the
  input an agent queues until a UI pass applied it. Screenshots and
  frames without a file come back inline.
- New `agent_folders` tool; Settings > AI Agents is the shared Agents
  page (agents.settings saves agents.json for the CLI).
- `mcp --headless`, `--when-closed`; `mcp --bridge` stays one release as a
  deprecated alias. No client configuration is written.

Closes #4383.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-03 01:22:27 +01:00

51 lines
2.3 KiB
Rust

//! The command gate the desktop app sets while it runs an agent's request: it checks each command
//! the agent gives before it runs, not the commands that command runs in turn.
use std::sync::{Arc, Mutex};
use effectcraft_engine::{CommandGate, Session};
use serde_json::{Value, json};
/// A gate that refuses `layer.newSolid` and records every command it was asked about.
fn gate(seen: &Arc<Mutex<Vec<String>>>) -> CommandGate {
let seen = seen.clone();
Arc::new(move |_: &mut Session, id: &str, _: &Value| {
seen.lock().unwrap().push(id.to_string());
if id == "layer.newSolid" { Err("not for agents".into()) } else { Ok(()) }
})
}
fn layers(s: &Session) -> usize {
s.project.comps().map(|(_, c)| c.layers.len()).sum()
}
#[test]
fn a_refused_command_does_not_run_and_the_gate_stays() {
let seen = Arc::new(Mutex::new(vec![]));
let mut s = Session { command_gate: Some(gate(&seen)), ..Default::default() };
s.execute("comp.new", json!({"name": "C", "width": 32, "height": 32})).unwrap();
let before = layers(&s);
let refused = s.execute("layer.newSolid", json!({"name": "S"})).unwrap_err();
assert!(refused.to_string().contains("not for agents"), "{refused}");
assert_eq!(layers(&s), before, "the refused command changed the project");
// Still checked on the next call.
assert!(s.execute("layer.newSolid", json!({"name": "S"})).is_err());
assert!(s.command_gate.is_some());
// Without the gate the same command runs.
s.command_gate = None;
s.execute("layer.newSolid", json!({"name": "S"})).unwrap();
assert!(layers(&s) > before);
}
#[test]
fn the_commands_a_command_runs_are_not_checked_again() {
let seen = Arc::new(Mutex::new(vec![]));
let mut s = Session { command_gate: Some(gate(&seen)), ..Default::default() };
s.execute("engine.batch", json!({"steps": [{"command": "comp.new", "params": {"name": "C", "width": 32, "height": 32}}]})).unwrap();
assert_eq!(*seen.lock().unwrap(), ["engine.batch"]);
assert_eq!(s.project.comps().count(), 1);
// A UI id that never reaches `execute` is checked through `check_gate`.
assert!(s.check_gate("layer.newSolid", &json!({})).is_err());
assert!(s.without_gate(|s| s.execute("layer.newSolid", json!({"name": "S"}))).is_ok());
assert!(s.command_gate.is_some());
}