A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(desktop): refuse a daemon this app did not launch on Windows too
Marcelitob7060b99e3f7
diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.ymlindex d9b2446..01d039c 100644--- a/.github/workflows/windows.yml+++ b/.github/workflows/windows.yml@@ -25,7 +25,8 @@ jobs:steps:- uses: actions/checkout@v7- # The toolchain the Windows port is built and tested with.+ # The toolchain the Windows port is built and tested with. The repository has no+ # rust-toolchain.toml, so this pin is the one place that names it; later PRs keep it.- uses: dtolnay/[email protected]- uses: Swatinem/[email protected]diff --git a/apps/desktop/src-tauri/src/daemon_unavailable.rs b/apps/desktop/src-tauri/src/daemon_unavailable.rsindex 5d597c9..c5b9d6b 100644--- a/apps/desktop/src-tauri/src/daemon_unavailable.rs+++ b/apps/desktop/src-tauri/src/daemon_unavailable.rs@@ -1,12 +1,13 @@//! Temporary seam of the Windows port (gentle-dot#89). `daemon.rs` spawns the daemon with Unix//! primitives (fd 3, signals, file modes); its Windows side lands later in the chain and replaces-//! this file. Until then the shell builds and opens on Windows, and every start fails with-//! [`UNAVAILABLE`] instead of spawning anything.+//! this file. Until then the shell builds and opens on Windows, and every start fails instead of+//! spawning anything: with [`foreign_daemon_message`] when a daemon this app did not launch holds+//! the port (as on Unix, an app with a channel never attaches to one), else with [`UNAVAILABLE`].use crate::app_channel::{AppChannel, Handler};use crate::health::check_health;use std::path::PathBuf;-use std::sync::Arc;+use std::sync::{Arc, Mutex};use std::thread;use std::time::{Duration, Instant};@@ -44,14 +45,17 @@ pub enum RestartOutcome {/// The Unix `Daemon`'s interface, without a daemon behind it.pub struct Daemon {port: u16,+ wants_channel: bool,+ refused: Mutex<Option<String>>,}impl Daemon {pub fn new(port: u16, _data_dir: PathBuf, _runtime_dir: Option<PathBuf>) -> Self {- Self { port }+ Self { port, wants_channel: false, refused: Mutex::new(None) }}- pub fn with_channel(self, _handler: Arc<dyn Handler>) -> Self {+ pub fn with_channel(mut self, _handler: Arc<dyn Handler>) -> Self {+ self.wants_channel = true;self}@@ -67,13 +71,13 @@ impl Daemon {check_health(self.port, HEALTH_TIMEOUT)}- /// This app never launches a daemon here, so it would refuse any on the port.+ /// Why the last start refused the daemon on the port, if it did.pub fn refusal(&self) -> Option<String> {- None+ self.refused.lock().unwrap().clone()}pub fn wait_ready(&self, _timeout: Duration) -> Result<(), String> {- Err(UNAVAILABLE.into())+ Err(self.refusal().unwrap_or_else(|| UNAVAILABLE.into()))}pub fn wait_healthy(&self, timeout: Duration) -> bool {@@ -87,8 +91,11 @@ impl Daemon {false}+ /// This app never launches a daemon here, so with a channel it refuses any on the port.pub fn ensure_running(&self, _timeout: Duration) -> Result<(), String> {- Err(UNAVAILABLE.into())+ let refusal = (self.wants_channel && self.is_healthy()).then(|| foreign_daemon_message(self.port));+ *self.refused.lock().unwrap() = refusal.clone();+ Err(refusal.unwrap_or_else(|| UNAVAILABLE.into()))}pub fn owns_running(&self) -> bool {@@ -97,8 +104,12 @@ impl Daemon {pub fn stop(&self) {}- pub fn restart(&self, _timeout: Duration) -> RestartOutcome {- RestartOutcome::Failed(UNAVAILABLE.into())+ pub fn restart(&self, timeout: Duration) -> RestartOutcome {+ match self.ensure_running(timeout) {+ Ok(()) => RestartOutcome::Restarted,+ Err(_) if self.refusal().is_some() => RestartOutcome::External,+ Err(message) => RestartOutcome::Failed(message),+ }}}@@ -114,5 +125,38 @@ mod tests {assert_eq!(daemon.restart(Duration::ZERO), RestartOutcome::Failed(UNAVAILABLE.to_string()));assert!(daemon.channel().is_none());assert!(!daemon.owns_running());+ assert_eq!(daemon.refusal(), None);+ }++ /// A healthy `/health` answer on a free port, for as many probes as `answers`.+ fn foreign_daemon(answers: usize) -> u16 {+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ let port = listener.local_addr().unwrap().port();+ thread::spawn(move || {+ for mut stream in listener.incoming().take(answers).flatten() {+ use std::io::{Read, Write};+ let _ = stream.read(&mut [0u8; 1024]);+ let _ = stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");+ }+ });+ port+ }++ struct NoHandler;+ impl Handler for NoHandler {+ fn approve(&self, _request: &crate::approvals::ApprovalRequest) -> bool {+ false+ }+ }++ #[test]+ fn an_app_with_a_channel_refuses_a_daemon_it_did_not_launch() {+ let port = foreign_daemon(2);+ let daemon = Daemon::new(port, PathBuf::new(), None).with_channel(Arc::new(NoHandler));+ assert_eq!(daemon.ensure_running(Duration::ZERO), Err(foreign_daemon_message(port)));+ assert_eq!(daemon.refusal(), Some(foreign_daemon_message(port)));+ assert_eq!(daemon.wait_ready(Duration::ZERO), Err(foreign_daemon_message(port)));+ assert_eq!(daemon.restart(Duration::ZERO), RestartOutcome::External);+ assert!(daemon.channel().is_none());}}