mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 16:24:38 +00:00
craft-control agents: second review round (#2141)
- The token is passed on as the app wrote it (trimmed): craft-control's server compares exactly and accepts any CONTROL_TOKEN of 32+ chars. The token file is checked as a regular file before it is opened (a FIFO would block the open). - Allow Agent Control off counts as not allowed on the CLI's side too. - A window route is dropped once no app accepts agents. - A headless session with the app's agent folders ends when the person stops letting agents in. - The launch timeout is capped and the deadline computed without overflow; the poll interval is kept between 1 ms and 1 s. - The connect error names the app's window; the guide says to remove the connection file on quit and that one process per settings folder accepts agents; the AppImage mount is compared after resolving links. - Tests for each, and a wider margin in the queued-start test.
This commit is contained in:
1 parent
aa0f72303c
commit
6b16ec2b41
4 files changed
+227
-53
No files matched your search
@@ -7,15 +7,20 @@
|
||||
//!
|
||||
//! - An app that accepts agents: the call goes to its open window ([`Route::Window`]). The same
|
||||
//! port, token and process id keep the window client the app made; any change makes a new one.
|
||||
//! - No app accepts agents, and the person doesn't let agents use it: a session without a window
|
||||
//! with only the folders given to the command ([`LiveConfig::read_roots`], [`LiveConfig::write_root`]),
|
||||
//! as `mcp` always worked. The app's own folders are for agents the person let in.
|
||||
//! - No app accepts agents, and the person doesn't let agents use it ([`Consent::allowed`] off, or
|
||||
//! [`AgentSettings::allow_agent_control`] off, under which the app doesn't listen either): a
|
||||
//! session without a window with only the folders given to the command
|
||||
//! ([`LiveConfig::read_roots`], [`LiveConfig::write_root`]), as `mcp` always worked. The app's own
|
||||
//! folders are for agents the person let in.
|
||||
//! - No app accepts agents, but agents are allowed: the closed-app choice decides
|
||||
//! ([`LiveConfig::when_closed`], otherwise [`AgentSettings::when_app_closed`]): start the app and
|
||||
//! wait until it accepts agents, a headless session with the app's agent folders (the command's
|
||||
//! folders win), or an error that says what to do.
|
||||
//!
|
||||
//! Once headless, the session stays headless: documents don't move under the agent. Lookups are
|
||||
//! Once headless, the session stays headless: documents don't move under the agent. Only a session
|
||||
//! made with the app's agent folders ends when the person stops letting agents in; the next call
|
||||
//! gets one with the command's folders. A window route is dropped as soon as no app accepts agents.
|
||||
//! Lookups are
|
||||
//! serialised, so concurrent calls start the app once: calls that queued while a start was under
|
||||
//! way and found no window get that start's error rather than starting the app again; a call made
|
||||
//! after it tries again.
|
||||
@@ -49,6 +54,10 @@ pub type HeadlessFn<H> = Box<dyn Fn(&HeadlessFolders) -> Result<H, String> + Sen
|
||||
pub const LAUNCH_TIMEOUT: Duration = Duration::from_secs(45);
|
||||
/// How often a starting app's connection file is looked for.
|
||||
pub const LAUNCH_POLL: Duration = Duration::from_millis(250);
|
||||
/// The longest [`LiveConfig::launch_timeout`] used; a longer one is cut to this.
|
||||
const MAX_LAUNCH_TIMEOUT: Duration = Duration::from_secs(600);
|
||||
/// The range [`LiveConfig::poll`] is kept in.
|
||||
const POLL_RANGE: (Duration, Duration) = (Duration::from_millis(1), Duration::from_secs(1));
|
||||
|
||||
/// The open window a call goes to: from the connection file, with the token from its token file.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
@@ -58,7 +67,7 @@ pub struct Window {
|
||||
pub port: u16,
|
||||
/// The app's process id.
|
||||
pub pid: u32,
|
||||
/// The control token (hex digits, lower case).
|
||||
/// The control token, as its token file holds it (trimmed).
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
@@ -80,8 +89,9 @@ impl fmt::Debug for Window {
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct Consent {
|
||||
/// Agents may use the app ([`super::agents_allowed`]: with the suite's `AI-MCP` flag, the
|
||||
/// ArtCraft Launcher's switch).
|
||||
/// Agents may use the app: `agents_allowed(mcp_flag(), launcher_allows("<id>"), legacy)`
|
||||
/// ([`super::agents_allowed`]), with the suite's `AI-MCP` flag the ArtCraft Launcher's switch.
|
||||
/// [`Live`] also requires [`AgentSettings::allow_agent_control`].
|
||||
pub allowed: bool,
|
||||
/// The app's agent settings (closed-app choice, folders).
|
||||
pub settings: AgentSettings,
|
||||
@@ -130,9 +140,9 @@ pub struct LiveConfig<W, H> {
|
||||
pub write_root: Option<PathBuf>,
|
||||
/// Starts the app; `None` when this install can't ([`launcher`] found nothing to start).
|
||||
pub launch: Option<LaunchFn>,
|
||||
/// How long a started app gets to accept agents ([`LAUNCH_TIMEOUT`]).
|
||||
/// How long a started app gets to accept agents ([`LAUNCH_TIMEOUT`]; at most 10 minutes).
|
||||
pub launch_timeout: Duration,
|
||||
/// How often a starting app is looked for ([`LAUNCH_POLL`]).
|
||||
/// How often a starting app is looked for ([`LAUNCH_POLL`]; kept between 1 ms and 1 s).
|
||||
pub poll: Duration,
|
||||
consent: ConsentFn,
|
||||
connect: ConnectFn<W>,
|
||||
@@ -168,8 +178,15 @@ impl<W, H> LiveConfig<W, H> {
|
||||
/// The port, token and process id a window client was made for: a restarted app may keep its
|
||||
/// port and token, but its old socket is gone.
|
||||
type WindowKey = (u16, String, u32);
|
||||
/// The current route, and for a window the key it was made for.
|
||||
type Current<W, H> = Option<(Route<W, H>, Option<WindowKey>)>;
|
||||
/// The route the last call took.
|
||||
struct Current<W, H> {
|
||||
route: Route<W, H>,
|
||||
/// For a window, the key its client was made for.
|
||||
key: Option<WindowKey>,
|
||||
/// A headless session that holds folders of the app's agent settings: it lasts only while
|
||||
/// the person lets agents in.
|
||||
app_folders: bool,
|
||||
}
|
||||
|
||||
/// Resolves each tool call of one MCP session; see the module docs.
|
||||
pub struct Live<W, H> {
|
||||
@@ -178,7 +195,7 @@ pub struct Live<W, H> {
|
||||
lookup: Mutex<Option<String>>,
|
||||
/// How many starts have finished, read before a call queues for `lookup`.
|
||||
finished: AtomicU64,
|
||||
current: Mutex<Current<W, H>>,
|
||||
current: Mutex<Option<Current<W, H>>>,
|
||||
}
|
||||
|
||||
impl<W: Clone, H: Clone> Live<W, H> {
|
||||
@@ -189,38 +206,67 @@ impl<W: Clone, H: Clone> Live<W, H> {
|
||||
|
||||
/// The route the last [`Self::resolve`] chose, if any.
|
||||
pub fn current(&self) -> Option<Route<W, H>> {
|
||||
self.current.lock().unwrap_or_else(PoisonError::into_inner).as_ref().map(|(r, _)| r.clone())
|
||||
self.current.lock().unwrap_or_else(PoisonError::into_inner).as_ref().map(|c| c.route.clone())
|
||||
}
|
||||
|
||||
fn set(&self, route: Route<W, H>, key: Option<WindowKey>) -> Route<W, H> {
|
||||
*self.current.lock().unwrap_or_else(PoisonError::into_inner) = Some((route.clone(), key));
|
||||
fn set(&self, route: Route<W, H>, key: Option<WindowKey>, app_folders: bool) -> Route<W, H> {
|
||||
*self.current.lock().unwrap_or_else(PoisonError::into_inner) = Some(Current { route: route.clone(), key, app_folders });
|
||||
route
|
||||
}
|
||||
|
||||
/// The headless session to keep using, if any: one made with the app's folders only while
|
||||
/// agents are still `allowed`. A route that may not be kept is dropped.
|
||||
fn sticky_headless(&self, allowed: bool) -> Option<Route<W, H>> {
|
||||
let mut current = self.current.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
match current.as_ref() {
|
||||
Some(c) if matches!(c.route, Route::Headless(_)) && (allowed || !c.app_folders) => Some(c.route.clone()),
|
||||
Some(c) if matches!(c.route, Route::Headless(_)) => {
|
||||
*current = None;
|
||||
None
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Forgets a window route: no app accepts agents any more, so its client is for a socket
|
||||
/// that is gone.
|
||||
fn drop_window(&self) {
|
||||
let mut current = self.current.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if current.as_ref().is_some_and(|c| matches!(c.route, Route::Window(_))) {
|
||||
*current = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the next tool call goes. Blocks; see the module docs. The error is the tool call's
|
||||
/// result.
|
||||
pub fn resolve(&self) -> Result<Route<W, H>, String> {
|
||||
let queued_at = self.finished.load(Ordering::Acquire);
|
||||
let mut last_failure = self.lookup.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if let Some(route @ Route::Headless(_)) = self.current() {
|
||||
let consent = (self.cfg.consent)(&self.cfg.config_dir);
|
||||
// Allow Agent Control off refuses agents, as the app does: it doesn't listen then.
|
||||
let allowed = consent.allowed && consent.settings.allow_agent_control;
|
||||
if let Some(route) = self.sticky_headless(allowed) {
|
||||
return Ok(route);
|
||||
}
|
||||
let consent = (self.cfg.consent)(&self.cfg.config_dir);
|
||||
if let Some(window) = self.find_app()? {
|
||||
return self.use_window(window);
|
||||
}
|
||||
if !consent.allowed {
|
||||
let folders = HeadlessFolders { read: self.cfg.read_roots.clone(), write: self.cfg.write_root.clone() };
|
||||
return self.headless(&folders, "the automation roots can't be used");
|
||||
}
|
||||
self.drop_window();
|
||||
let app = &self.cfg.app_name;
|
||||
if !allowed {
|
||||
let folders = HeadlessFolders { read: self.cfg.read_roots.clone(), write: self.cfg.write_root.clone() };
|
||||
return self.headless(&folders, false, &format!("{app} can't use the automation roots"));
|
||||
}
|
||||
match self.cfg.when_closed.unwrap_or(consent.settings.when_app_closed) {
|
||||
WhenAppClosed::Error => Err(format!("{app} isn't running: open it, then try again.")),
|
||||
WhenAppClosed::Headless => {
|
||||
let app_read = self.cfg.read_roots.is_empty() && consent.settings.read_folder().is_some();
|
||||
let app_write = self.cfg.write_root.is_none() && consent.settings.write_folder().is_some();
|
||||
let read =
|
||||
if self.cfg.read_roots.is_empty() { consent.settings.read_folder().into_iter().collect() } else { self.cfg.read_roots.clone() };
|
||||
let write = self.cfg.write_root.clone().or_else(|| consent.settings.write_folder());
|
||||
self.headless(&HeadlessFolders { read, write }, &format!("{app} isn't running and its agent folders can't be used"))
|
||||
let why = format!("{app} isn't running and its agent folders can't be used");
|
||||
self.headless(&HeadlessFolders { read, write }, app_read || app_write, &why)
|
||||
}
|
||||
WhenAppClosed::Launch => {
|
||||
// A start finished while this call waited and the app still isn't there: its answer
|
||||
@@ -238,44 +284,51 @@ impl<W: Clone, H: Clone> Live<W, H> {
|
||||
}
|
||||
}
|
||||
|
||||
fn headless(&self, folders: &HeadlessFolders, why: &str) -> Result<Route<W, H>, String> {
|
||||
/// A headless session with `folders` from now on; `app_folders` when some of them are the
|
||||
/// app's agent folders. `why` leads its error.
|
||||
fn headless(&self, folders: &HeadlessFolders, app_folders: bool, why: &str) -> Result<Route<W, H>, String> {
|
||||
let session = (self.cfg.headless)(folders).map_err(|e| format!("{why}: {e}"))?;
|
||||
Ok(self.set(Route::Headless(session), None))
|
||||
Ok(self.set(Route::Headless(session), None, app_folders))
|
||||
}
|
||||
|
||||
/// Starts the app and waits until it accepts agents, at most the launch timeout.
|
||||
fn launch_and_wait(&self) -> Result<Route<W, H>, String> {
|
||||
let app = &self.cfg.app_name;
|
||||
let Some(launch) = &self.cfg.launch else {
|
||||
return Err(format!("{app} isn't running, and this install can't start it from here: open {app}, then try again."));
|
||||
};
|
||||
launch().map_err(|e| format!("couldn't start {app}: {e}"))?;
|
||||
let deadline = Instant::now() + self.cfg.launch_timeout;
|
||||
let timeout = self.cfg.launch_timeout.min(MAX_LAUNCH_TIMEOUT);
|
||||
let started = Instant::now();
|
||||
let deadline = started.checked_add(timeout).unwrap_or(started);
|
||||
let poll = self.cfg.poll.clamp(POLL_RANGE.0, POLL_RANGE.1);
|
||||
loop {
|
||||
std::thread::sleep(self.cfg.poll);
|
||||
std::thread::sleep(poll);
|
||||
if let Some(window) = self.find_app()? {
|
||||
return self.use_window(window);
|
||||
}
|
||||
if Instant::now() >= deadline {
|
||||
return Err(format!(
|
||||
"{app} didn't accept agents within {} s of starting. If it's still opening, try again in a moment; otherwise check that AI agents are on in ArtCraft Launcher › Settings › AI agents.",
|
||||
self.cfg.launch_timeout.as_secs()
|
||||
timeout.as_secs()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The client for `window`: the one made for the same port, token and process, or a new one.
|
||||
fn use_window(&self, window: Window) -> Result<Route<W, H>, String> {
|
||||
let key: WindowKey = (window.port, window.token.clone(), window.pid);
|
||||
{
|
||||
let current = self.current.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if let Some((route, Some(k))) = current.as_ref()
|
||||
&& *k == key
|
||||
if let Some(c) = current.as_ref()
|
||||
&& c.key.as_ref() == Some(&key)
|
||||
{
|
||||
return Ok(route.clone());
|
||||
return Ok(c.route.clone());
|
||||
}
|
||||
}
|
||||
let client = (self.cfg.connect)(&window)?;
|
||||
Ok(self.set(Route::Window(client), Some(key)))
|
||||
let client = (self.cfg.connect)(&window).map_err(|e| format!("couldn't reach {}'s window: {e}", self.cfg.app_name))?;
|
||||
Ok(self.set(Route::Window(client), Some(key), false))
|
||||
}
|
||||
|
||||
/// The app accepting agents now: `None` when there is no connection file, or one left by an
|
||||
@@ -299,12 +352,17 @@ pub fn find_window(dir: &Path, app_name: &str) -> Result<Option<Window>, String>
|
||||
/// The largest token file read; a token is far shorter.
|
||||
const MAX_TOKEN_FILE: u64 = 4096;
|
||||
|
||||
/// The token in the token file at `path`, trimmed. The file must be a regular file of at most
|
||||
/// [`MAX_TOKEN_FILE`] bytes holding a non-empty token of hex digits, and on Unix readable by its
|
||||
/// owner only: a token others could read is refused rather than used.
|
||||
/// The token in the token file at `path`, trimmed and otherwise as written: the app's server
|
||||
/// compares it exactly ([`crate::auth::tokens_match`]). The file must be a regular file of at most
|
||||
/// [`MAX_TOKEN_FILE`] bytes holding one token (no spaces or control characters in it), and on Unix
|
||||
/// readable by its owner only: a token others could read is refused rather than used.
|
||||
fn read_token(path: &Path) -> Result<String, String> {
|
||||
use std::io::Read;
|
||||
let shown = |e: &dyn fmt::Display| format!("{}: {e}", path.display());
|
||||
// Before opening: opening a FIFO for reading blocks until something writes to it.
|
||||
if !std::fs::metadata(path).map_err(|e| shown(&e))?.is_file() {
|
||||
return Err(shown(&"not a regular file"));
|
||||
}
|
||||
let file = std::fs::File::open(path).map_err(|e| shown(&e))?;
|
||||
let meta = file.metadata().map_err(|e| shown(&e))?;
|
||||
if !meta.is_file() {
|
||||
@@ -323,10 +381,10 @@ fn read_token(path: &Path) -> Result<String, String> {
|
||||
return Err(shown(&format!("larger than {MAX_TOKEN_FILE} bytes")));
|
||||
}
|
||||
let token = text.trim();
|
||||
if token.is_empty() || !token.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||
if token.is_empty() || token.chars().any(|c| c.is_whitespace() || c.is_control()) {
|
||||
return Err(shown(&"not a control token"));
|
||||
}
|
||||
Ok(token.to_ascii_lowercase())
|
||||
Ok(token.to_owned())
|
||||
}
|
||||
|
||||
/// How to start the desktop app `exe` (its file name without `.exe`) that ships beside the CLI at
|
||||
@@ -336,14 +394,16 @@ fn read_token(path: &Path) -> Result<String, String> {
|
||||
pub fn launcher(cli: &Path, exe: &str) -> Option<LaunchFn> {
|
||||
use std::process::{Command, Stdio};
|
||||
let env = |name: &str| std::env::var_os(name).filter(|v| !v.is_empty());
|
||||
// Both sides resolved, so a mount reached through a symlink (`$TMPDIR`) still matches.
|
||||
let resolved = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf());
|
||||
let install = Install {
|
||||
platform: Platform::current(),
|
||||
flatpak: env("FLATPAK_ID").is_some(),
|
||||
appimage: env("APPIMAGE"),
|
||||
appdir: env("APPDIR"),
|
||||
appdir: env("APPDIR").map(|d| resolved(Path::new(&d)).into_os_string()),
|
||||
system_root: env("SystemRoot"),
|
||||
};
|
||||
let (program, args) = launch_command(cli, exe, &install, &|p| p.is_file())?;
|
||||
let (program, args) = launch_command(&resolved(cli), exe, &install, &|p| p.is_file())?;
|
||||
let mut command = Command::new(program);
|
||||
command.args(args);
|
||||
let command = Mutex::new(command);
|
||||
@@ -391,6 +451,7 @@ enum Platform {
|
||||
}
|
||||
|
||||
impl Platform {
|
||||
/// The platform this build runs on.
|
||||
fn current() -> Self {
|
||||
if cfg!(windows) {
|
||||
Self::Windows
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
//! reads that file before every tool call ([`Live`]), so an MCP client's configuration never holds
|
||||
//! a port or a token. The file holds no secret. A crashed app can leave it behind; nothing then
|
||||
//! listens on its port ([`probe`]), and the app's next launch removes it
|
||||
//! ([`remove_stale_connection`]).
|
||||
//! ([`remove_stale_connection`]). An app that quits removes its own ([`remove_connection`]).
|
||||
//!
|
||||
//! | Part | Names | Builds |
|
||||
//! |---|---|---|
|
||||
@@ -37,13 +37,16 @@
|
||||
//! launcher's switch wakes the app and when the person changes the setting. When it turns true,
|
||||
//! listen on loopback with a token, the token in an owner-only file
|
||||
//! ([`crate::auth::App::write_token_file`]), and [`write_connection`] with [`Connection::new`];
|
||||
//! when it turns false, stop listening and [`remove_connection`] with the app's process id. No
|
||||
//! restart, no agents switch of its own.
|
||||
//! when it turns false, and when the app quits, stop listening and [`remove_connection`] with
|
||||
//! the app's process id. No restart, no agents switch of its own. There is one connection file
|
||||
//! per settings folder: an app that runs more than one process on one folder lets only one of
|
||||
//! them accept agents.
|
||||
//!
|
||||
//! Its `gridcraft-cli mcp`:
|
||||
//!
|
||||
//! 1. A [`LiveConfig`] with the app's settings folder; `consent` reads its saved
|
||||
//! [`AgentSettings`] and asks [`launcher_allows`]; `connect` makes the app's client for a
|
||||
//! [`AgentSettings`] and answers `agents_allowed(mcp_flag(), launcher_allows("gridcraft"), legacy)`
|
||||
//! ([`agents_allowed`], [`mcp_flag`], [`launcher_allows`]); `connect` makes the app's client for a
|
||||
//! [`Window`] (its address and token); `headless` makes its session without a window for
|
||||
//! [`HeadlessFolders`]. [`launcher`] starts the app beside the CLI.
|
||||
//! 2. Before every tool call, [`Live::resolve`] (from a blocking thread in an async server), then
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
//! request to the window ([`server`]), the client that talks to it ([`client`]), the request the
|
||||
//! window answers ([`ControlRequest`]), the document watch subscribed connections wait on
|
||||
//! ([`DocumentWatch`]), what a request still running does when it finishes ([`Effect`]), and AI
|
||||
//! agents in the open window behind the ArtCraft Launcher's switch ([`agents`], feature `agents`). It
|
||||
//! agents in the open window behind the ArtCraft Launcher's switch ([`agents`]: its settings on
|
||||
//! every target, the switch, the connection file and the CLI's side with feature `agents`). It
|
||||
//! needs no UI toolkit; the window's own requests over egui (`ui.screenshot`, `ui.resize`) are
|
||||
//! `craft-control-egui`.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
@@ -246,7 +246,11 @@ fn an_open_app_takes_the_call_and_keeps_its_client_until_it_restarts() {
|
||||
// Even without consent: an app publishes its connection only while it accepts agents.
|
||||
let live = TestLive::new(config(dir.path(), Consent::default(), &made));
|
||||
let w = window(live.resolve());
|
||||
assert_eq!((w.port, w.pid, w.token.as_str(), w.addr()), (c.port, c.pid, TOKEN, c.addr()), "the token is trimmed and lower-cased");
|
||||
assert_eq!(
|
||||
(w.port, w.pid, w.token.as_str(), w.addr()),
|
||||
(c.port, c.pid, TOKEN.to_ascii_uppercase().as_str(), c.addr()),
|
||||
"the token is trimmed, otherwise as written"
|
||||
);
|
||||
window(live.resolve());
|
||||
assert_eq!(made.connects.load(Ordering::Relaxed), 1, "the same window keeps its client");
|
||||
assert!(matches!(live.current(), Some(Route::Window(_))));
|
||||
@@ -333,8 +337,8 @@ fn a_crashed_apps_connection_file_counts_as_closed() {
|
||||
assert_eq!(find_window(dir.path(), "Test").unwrap(), None);
|
||||
}
|
||||
|
||||
/// A token file that isn't a token, is too big or is missing fails the call and names the app;
|
||||
/// nothing is sent to the window.
|
||||
/// A token file that isn't a token, is too big, is missing, isn't a regular file or (on Unix)
|
||||
/// others can read fails the call and names the app; nothing is sent to the window.
|
||||
#[test]
|
||||
fn a_bad_token_file_fails_the_call() {
|
||||
let dir = temp_dir("token");
|
||||
@@ -345,6 +349,23 @@ fn a_bad_token_file_fails_the_call() {
|
||||
let err = TestLive::new(config(dir.path(), Consent::default(), &made)).resolve().err().unwrap();
|
||||
assert!(err.starts_with("Test's agent token: "), "{err}");
|
||||
}
|
||||
// Any token the app's server accepts is passed on: upper case, dashes.
|
||||
write_token(&dir.path().join(TOKEN_FILE), "AB12-cd34-EF56-0000-1111-2222-3333-4444");
|
||||
assert_eq!(find_window(dir.path(), "Test").unwrap().unwrap().token, "AB12-cd34-EF56-0000-1111-2222-3333-4444");
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let token = dir.path().join(TOKEN_FILE);
|
||||
write_token(&token, TOKEN);
|
||||
std::fs::set_permissions(&token, std::fs::Permissions::from_mode(0o644)).unwrap();
|
||||
let err = find_window(dir.path(), "Test").err().unwrap();
|
||||
assert!(err.contains("readable by group or others"), "{err}");
|
||||
// A FIFO would block a plain open until something writes to it.
|
||||
std::fs::remove_file(&token).unwrap();
|
||||
assert!(std::process::Command::new("mkfifo").arg(&token).status().unwrap().success());
|
||||
let err = find_window(dir.path(), "Test").err().unwrap();
|
||||
assert!(err.contains("not a regular file"), "{err}");
|
||||
}
|
||||
std::fs::remove_file(dir.path().join(TOKEN_FILE)).unwrap();
|
||||
assert!(find_window(dir.path(), "Test").is_err());
|
||||
assert_eq!(made.connects.load(Ordering::Relaxed), 0);
|
||||
@@ -362,7 +383,7 @@ fn the_apps_errors_reach_the_agent() {
|
||||
|w: &Window| Ok(w.clone()),
|
||||
|_: &HeadlessFolders| Err("no such folder".into()),
|
||||
);
|
||||
assert_eq!(TestLive::new(cfg).resolve().err().unwrap(), "the automation roots can't be used: no such folder");
|
||||
assert_eq!(TestLive::new(cfg).resolve().err().unwrap(), "Test can't use the automation roots: no such folder");
|
||||
|
||||
let (_listener, _c) = publish(dir.path());
|
||||
let cfg: LiveConfig<Window, HeadlessFolders> = LiveConfig::new(
|
||||
@@ -373,7 +394,7 @@ fn the_apps_errors_reach_the_agent() {
|
||||
|f: &HeadlessFolders| Ok(f.clone()),
|
||||
);
|
||||
let live = TestLive::new(cfg);
|
||||
assert_eq!(live.resolve().err().unwrap(), "refused");
|
||||
assert_eq!(live.resolve().err().unwrap(), "couldn't reach Test's window: refused");
|
||||
assert!(live.current().is_none(), "a failed connect leaves no route");
|
||||
}
|
||||
|
||||
@@ -438,18 +459,26 @@ fn calls_queued_behind_a_failed_start_share_its_error() {
|
||||
let made = Arc::new(Made::default());
|
||||
let launches = Arc::new(AtomicUsize::new(0));
|
||||
let mut cfg = config(dir.path(), allowed(WhenAppClosed::Launch), &made);
|
||||
let n = launches.clone();
|
||||
// The app is started but never accepts agents within the timeout.
|
||||
let entered = Arc::new(AtomicUsize::new(0));
|
||||
let (n, e) = (launches.clone(), entered.clone());
|
||||
// The app is started but never accepts agents within the timeout. The start waits until every
|
||||
// call is under way and then a while longer, so a call that hasn't reached its lookup by the
|
||||
// time the start ends is very unlikely (there is no hook between the two).
|
||||
cfg.launch = Some(Box::new(move || {
|
||||
n.fetch_add(1, Ordering::Relaxed);
|
||||
assert!(wait_for(|| e.load(Ordering::Relaxed) == 4), "every call starts");
|
||||
std::thread::sleep(Duration::from_millis(250));
|
||||
Ok(())
|
||||
}));
|
||||
cfg.launch_timeout = Duration::from_millis(150);
|
||||
let live = Arc::new(TestLive::new(cfg));
|
||||
let calls: Vec<_> = (0..4)
|
||||
.map(|_| {
|
||||
let live = live.clone();
|
||||
std::thread::spawn(move || live.resolve())
|
||||
let (live, entered) = (live.clone(), entered.clone());
|
||||
std::thread::spawn(move || {
|
||||
entered.fetch_add(1, Ordering::Relaxed);
|
||||
live.resolve()
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let answers: Vec<_> = calls.into_iter().map(|c| c.join().unwrap()).collect();
|
||||
@@ -461,3 +490,83 @@ fn calls_queued_behind_a_failed_start_share_its_error() {
|
||||
assert!(live.resolve().is_err());
|
||||
assert_eq!(launches.load(Ordering::Relaxed), 2, "a later call starts the app again");
|
||||
}
|
||||
|
||||
/// Allow Agent Control off refuses agents on the CLI's side too, as the app does by not
|
||||
/// listening: no start, and only the command's folders.
|
||||
#[test]
|
||||
fn allow_agent_control_off_counts_as_not_allowed() {
|
||||
let dir = temp_dir("aac-off");
|
||||
let made = Arc::new(Made::default());
|
||||
let mut s = settings(WhenAppClosed::Launch, "/app-in", "/app-out");
|
||||
s.allow_agent_control = false;
|
||||
let mut cfg = config(dir.path(), Consent::new(true, s), &made);
|
||||
cfg.launch = Some(Box::new(|| panic!("nothing may start the app")));
|
||||
cfg.read_roots = paths(&["/cli-in"]);
|
||||
let f = headless_folders(TestLive::new(cfg).resolve());
|
||||
assert_eq!((f.read, f.write), (paths(&["/cli-in"]), None));
|
||||
}
|
||||
|
||||
/// A window route is dropped once no app accepts agents, so a dead client isn't kept or handed
|
||||
/// back.
|
||||
#[test]
|
||||
fn a_window_that_went_away_is_forgotten() {
|
||||
let dir = temp_dir("gone");
|
||||
let made = Arc::new(Made::default());
|
||||
let live = TestLive::new(config(dir.path(), allowed(WhenAppClosed::Error), &made));
|
||||
let (listener, c) = publish(dir.path());
|
||||
window(live.resolve());
|
||||
drop(listener);
|
||||
remove_connection(dir.path(), c.pid);
|
||||
assert!(live.resolve().is_err(), "the app is closed and the choice is an error");
|
||||
assert!(live.current().is_none(), "the dead window is gone");
|
||||
}
|
||||
|
||||
/// A headless session with the app's agent folders ends when the person stops letting agents in;
|
||||
/// one with only the command's folders stays.
|
||||
#[test]
|
||||
fn a_headless_session_with_the_apps_folders_ends_with_consent() {
|
||||
let dir = temp_dir("revoke");
|
||||
let made = Arc::new(Made::default());
|
||||
let on = Arc::new(AtomicBool::new(true));
|
||||
let gate = on.clone();
|
||||
let (b, c) = (made.clone(), made.clone());
|
||||
let cfg: LiveConfig<Window, HeadlessFolders> = LiveConfig::new(
|
||||
"Test",
|
||||
dir.path().to_path_buf(),
|
||||
move |_: &Path| Consent::new(gate.load(Ordering::Relaxed), settings(WhenAppClosed::Headless, "/app-in", "/app-out")),
|
||||
move |w: &Window| {
|
||||
b.connects.fetch_add(1, Ordering::Relaxed);
|
||||
Ok(w.clone())
|
||||
},
|
||||
move |f: &HeadlessFolders| {
|
||||
c.sessions.fetch_add(1, Ordering::Relaxed);
|
||||
Ok(f.clone())
|
||||
},
|
||||
);
|
||||
let live = TestLive::new(cfg);
|
||||
assert_eq!(headless_folders(live.resolve()).read, paths(&["/app-in"]));
|
||||
on.store(false, Ordering::Relaxed);
|
||||
assert_eq!(headless_folders(live.resolve()), HeadlessFolders::default(), "the app's folders went with the consent");
|
||||
on.store(true, Ordering::Relaxed);
|
||||
assert_eq!(headless_folders(live.resolve()), HeadlessFolders::default(), "a session without the app's folders stays");
|
||||
assert_eq!(made.sessions.load(Ordering::Relaxed), 2);
|
||||
}
|
||||
|
||||
/// A huge launch timeout or poll is cut down rather than overflowing the deadline or blocking.
|
||||
#[test]
|
||||
fn a_huge_launch_timeout_or_poll_neither_panics_nor_hangs() {
|
||||
let dir = temp_dir("huge");
|
||||
let made = Arc::new(Made::default());
|
||||
let mut cfg = config(dir.path(), allowed(WhenAppClosed::Launch), &made);
|
||||
let (d, app) = (dir.path().to_path_buf(), Arc::new(Mutex::new(None)));
|
||||
let slot = app.clone();
|
||||
cfg.launch = Some(Box::new(move || {
|
||||
*slot.lock().unwrap() = Some(publish(&d).0);
|
||||
Ok(())
|
||||
}));
|
||||
cfg.launch_timeout = Duration::MAX;
|
||||
cfg.poll = Duration::MAX;
|
||||
let started = Instant::now();
|
||||
window(TestLive::new(cfg).resolve());
|
||||
assert!(started.elapsed() < Duration::from_secs(5), "{:?}", started.elapsed());
|
||||
}
|
||||
Reference in new issue
Block a user