diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json index ab54292..929ec72 100644 --- a/.claude-plugin/plugin.json +++ b/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "theseus", - "version": "0.6.4", + "version": "0.6.5", "description": "A private, never-sends personal assistant for Claude Code on macOS: one prioritized list, morning and evening briefings, texts you owe replies, drafts in your voice. Independent project, not affiliated with Anthropic.", "author": { "name": "Theseus contributors" }, "license": "MIT", diff --git a/CHANGELOG.md b/CHANGELOG.md index f56cec1..fbadd29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.6.5 +- **Theseus is in the Dock now,** like LeafJuice, with its menu bar icon as well. Right-click the Dock icon and choose Quit, or use Cmd-Q, Theseus > Quit, or the menu bar icon's Quit. Closing the window keeps Theseus running. +- **Every Quit really quits.** Before, Quit stopped the helper that gives Remote Control a terminal but not the `claude remote-control` it had started, so that kept running, and macOS called Theseus "in use" when you tried to replace it. Some Quits also didn't leave the note that tells the watchdog to leave Theseus closed, so it came back within a minute. Now every Quit leaves that note and stops everything Theseus started, politely first and by force after three seconds. If anything holds Quit up, Theseus exits anyway after eight seconds. Opening Theseus clears the note, so the watchdog looks after it again. +- Tested: new tests for the note, the Dock icon and stopping a child that ignores hangups and stop requests. In a sandbox with a stand-in for Remote Control, Cmd-Q and the Dock's Quit (sent as macOS sends it) each exited, left the note and left no process behind. A bundled build kept its Dock icon and menus with the window closed. Not tested: clicking the menu bar icon's Quit (it runs the same code as Cmd-Q), and a real Remote Control session. + ## 0.6.4 - **A normal Mac menu bar while the window is open.** Theseus lives in the menu bar, and until now its window had no menus, so the only way to quit was the menu bar icon. Opening the window now gives Theseus a Dock icon and the usual menus: Theseus (About, Settings…, Hide, Quit with Cmd-Q), Edit and Window. Settings… opens Setup. Closing the window puts Theseus back in the menu bar only. Quit from either place stops Remote Control and keeps Theseus closed until you open it again. Tested on a debug build with an empty workspace: the menus appear, Close Window returns it to the menu bar, and Quit exits and leaves the quit flag. Not tested: Settings… switching to Setup on screen, and Open Theseus from the menu bar icon (the same code path as the first-launch window, which was tested). diff --git a/apps/desktop/package.json b/apps/desktop/package.json index f77a8ee..536f6e0 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -1,7 +1,7 @@ { "name": "theseus-desktop", "private": true, - "version": "0.6.4", + "version": "0.6.5", "type": "module", "description": "Theseus for Mac: hosts the Theseus engine (Remote Control, checks, briefings) with one set of macOS permissions.", "scripts": { diff --git a/apps/desktop/src-tauri/Cargo.lock b/apps/desktop/src-tauri/Cargo.lock index 1cb14ee..197b9b5 100644 --- a/apps/desktop/src-tauri/Cargo.lock +++ b/apps/desktop/src-tauri/Cargo.lock @@ -3277,8 +3277,9 @@ dependencies = [ [[package]] name = "theseus" -version = "0.6.4" +version = "0.6.5" dependencies = [ + "libc", "serde", "serde_json", "tauri", diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index b2ffe2a..37d8592 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "theseus" -version = "0.6.4" +version = "0.6.5" description = "Theseus for Mac" authors = ["Theseus contributors"] license = "MIT" @@ -17,3 +17,4 @@ tauri-build = { version = "2", features = [] } tauri = { version = "2", features = ["tray-icon", "image-png"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } +libc = "0.2" diff --git a/apps/desktop/src-tauri/Info.plist b/apps/desktop/src-tauri/Info.plist index 95c1a42..e2e240e 100644 --- a/apps/desktop/src-tauri/Info.plist +++ b/apps/desktop/src-tauri/Info.plist @@ -16,7 +16,5 @@ Theseus reads your Notes inbox folder and updates your pinned Today and Master List notes. NSContactsUsageDescription Theseus shows names instead of phone numbers for people who are waiting on you. - LSUIElement - diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs index 83da756..136179e 100644 --- a/apps/desktop/src-tauri/src/lib.rs +++ b/apps/desktop/src-tauri/src/lib.rs @@ -196,12 +196,58 @@ fn set(s: &S, f: F) { fn stop_remote(s: &S) { if let Ok(mut r) = s.remote.lock() { if let Some(mut rem) = r.take() { - let _ = rem.child.kill(); - let _ = rem.child.wait(); + stop_tree(&mut rem.child); } } } +/// Every process below `root`, children first found, from one `ps` listing. +fn descendants(root: u32) -> Vec { + let Ok(out) = Command::new("/bin/ps").args(["-A", "-o", "pid=,ppid="]).output() else { return Vec::new() }; + let pairs: Vec<(i32, i32)> = String::from_utf8_lossy(&out.stdout) + .lines() + .filter_map(|l| { + let mut it = l.split_whitespace().map(|n| n.parse::()); + Some((it.next()?.ok()?, it.next()?.ok()?)) + }) + .collect(); + let mut found = vec![root as i32]; + let mut i = 0; + while i < found.len() { + let parent = found[i]; + found.extend(pairs.iter().filter(|(_, pp)| *pp == parent).map(|(p, _)| *p)); + i += 1; + } + found.remove(0); + found +} + +/// Stops a child and everything it started. `script` runs Remote Control in a session of its own, so killing +/// `script` alone left `claude remote-control` running after Quit (owner's report, 2026-11-15). Asks politely +/// first, then forces whatever is still there after three seconds. +fn stop_tree(child: &mut Child) { + let alive = |pid: i32| unsafe { libc::kill(pid, 0) == 0 }; + let mut pids = descendants(child.id()); + pids.push(child.id() as i32); + for &pid in &pids { + unsafe { libc::kill(pid, libc::SIGTERM) }; + } + let deadline = Instant::now() + Duration::from_secs(3); + while Instant::now() < deadline { + let _ = child.try_wait(); + if !pids.iter().any(|&p| alive(p)) { + break; + } + thread::sleep(Duration::from_millis(100)); + } + for &pid in &pids { + if alive(pid) { + unsafe { libc::kill(pid, libc::SIGKILL) }; + } + } + let _ = child.wait(); +} + fn remote_loop(s: S) { let mut wait_until = Instant::now(); loop { @@ -1015,26 +1061,53 @@ fn build_menu_bar(app: &AppHandle) -> tauri::Result> { Ok(menu) } -/// Theseus lives in the menu bar. While its window is open it's a regular app, with a Dock icon and the menu bar -/// above, so Quit and Cmd-Q are there like in any Mac app (D211). Closing the window puts it back in the menu bar. +/// Theseus is a regular Mac app with a Dock icon, a menu bar and its menu bar icon (owner, 2026-11-15: "adding it so +/// theseus shows as an app in the mac app bar at the bottom and allowing it to be quit from the context menu like LJ"). fn show_main(app: &AppHandle) { - #[cfg(target_os = "macos")] - let _ = app.set_activation_policy(tauri::ActivationPolicy::Regular); if let Some(w) = app.get_webview_window("main") { let _ = w.show(); let _ = w.set_focus(); } } -/// Quit on purpose: finish what the window holds, stop Remote Control, and leave the flag so the watchdog doesn't -/// start Theseus again. +/// Set once a Quit has begun, so the exit it ends with isn't taken for a new Quit. +static QUIT_STARTED: AtomicBool = AtomicBool::new(false); + +/// The first step of every Quit: leave the marker that tells the watchdog not to start Theseus again. Returns false +/// when a Quit is already under way. +fn begin_quit(marker: &Path) -> bool { + if QUIT_STARTED.swap(true, Ordering::SeqCst) { + return false; + } + let _ = fs::write(marker, ""); + true +} + +/// Opening Theseus undoes a Quit: without the marker, the watchdog looks after it again. +fn clear_quit_marker(marker: &Path) { + let _ = fs::remove_file(marker); +} + +/// Every way out goes through here: the Dock's Quit, Cmd-Q, Theseus > Quit and the menu bar icon's Quit. It finishes +/// what the window holds, stops Remote Control and everything it started, leaves the marker, and exits. The work runs +/// off the main thread so the window can save while it waits. fn quit_theseus(app: &AppHandle, s: &S) { - let _ = fs::write(quit_flag(), ""); - settle_window(app); - thread::sleep(Duration::from_millis(600)); - s.quitting.store(true, Ordering::SeqCst); - stop_remote(s); - app.exit(0); + if !begin_quit(&quit_flag()) { + return; + } + // If anything below holds Quit up, Theseus still goes: a Quit that doesn't quit is worse than an unclean one. + thread::spawn(|| { + thread::sleep(Duration::from_secs(8)); + std::process::exit(0); + }); + let (app, s) = (app.clone(), s.clone()); + thread::spawn(move || { + settle_window(&app); + thread::sleep(Duration::from_millis(600)); + s.quitting.store(true, Ordering::SeqCst); + stop_remote(&s); + app.exit(0); + }); } #[cfg_attr(mobile, tauri::mobile_entry_point)] @@ -1073,12 +1146,9 @@ pub fn run() { _ => {} }) .setup(move |app| { - #[cfg(target_os = "macos")] - app.set_activation_policy(tauri::ActivationPolicy::Accessory); - // Open on the very first launch, so a new user isn't left with only a menu bar icon. // Opening the app undoes "Quit": from here on, the watchdog looks after it again. - let _ = fs::remove_file(quit_flag()); + clear_quit_marker(&quit_flag()); if hosting(&config(&shared.ws)) { install_watchdog(&shared.ws); } @@ -1153,13 +1223,11 @@ pub fn run() { Ok(()) }) .on_window_event(|window, event| { - // Closing the window just hides it; Theseus keeps running in the menu bar. + // Closing the window just hides it; Theseus keeps running, in the Dock and the menu bar. if let WindowEvent::CloseRequested { api, .. } = event { api.prevent_close(); settle_window(window.app_handle()); let _ = window.hide(); - #[cfg(target_os = "macos")] - let _ = window.app_handle().set_activation_policy(tauri::ActivationPolicy::Accessory); } }) .build(tauri::generate_context!()) @@ -1167,8 +1235,16 @@ pub fn run() { .run(move |handle, event| match event { // Opening the app again (Finder, Spotlight) brings the window back. RunEvent::Reopen { .. } => show_main(handle), - // Any way out, including logging out: don't leave the server behind. + // The Dock's Quit, Cmd-Q, logging out and shutting down all ask to exit here. The first ask becomes a full + // Quit; the exit that Quit ends with goes through. + RunEvent::ExitRequested { api, .. } if !QUIT_STARTED.load(Ordering::SeqCst) => { + api.prevent_exit(); + quit_theseus(handle, &on_exit); + } + // Any way out, including logging out: don't leave the server behind. The Dock's Quit comes straight here + // without asking first, so this also leaves the marker that keeps the watchdog from reopening Theseus. RunEvent::Exit => { + begin_quit(&quit_flag()); on_exit.quitting.store(true, Ordering::SeqCst); stop_remote(&on_exit); } @@ -1204,6 +1280,55 @@ mod tests { } } + #[cfg(target_os = "macos")] + #[test] + fn stopping_remote_control_leaves_no_process_behind() { + // Like Remote Control: `script` starts a child in its own session, and that child ignores hangups. + let mut child = Command::new("/usr/bin/script") + .args(["-q", "/dev/null", "/bin/bash", "-c", "trap '' HUP TERM; while true; do sleep 1; done"]) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + let mut below = Vec::new(); + for _ in 0..50 { + below = descendants(child.id()); + if !below.is_empty() { + break; + } + thread::sleep(Duration::from_millis(100)); + } + assert!(!below.is_empty(), "script started its child"); + stop_tree(&mut child); + thread::sleep(Duration::from_millis(200)); + for pid in below { + assert!(unsafe { libc::kill(pid, 0) } != 0, "process {pid} outlived the stop"); + } + } + + #[test] + fn quit_leaves_the_marker_once_and_opening_clears_it() { + let marker = std::env::temp_dir().join(format!("theseus-quit-test-{}", std::process::id())); + let _ = fs::remove_file(&marker); + assert!(begin_quit(&marker), "the first Quit begins"); + assert!(marker.exists(), "Quit leaves the marker for the watchdog"); + assert!(!begin_quit(&marker), "a second Quit while one is under way does nothing"); + clear_quit_marker(&marker); + assert!(!marker.exists(), "opening Theseus clears the marker, so the watchdog guards it again"); + QUIT_STARTED.store(false, Ordering::SeqCst); + } + + #[test] + fn theseus_is_a_regular_app_with_a_dock_icon() { + // The owner wants Theseus in the Dock, quit from its menu like LeafJuice: no menu-bar-only mode. + let plist = include_str!("../Info.plist"); + assert!(!plist.contains("LSUIElement"), "Info.plist must not hide the Dock icon"); + let src = include_str!("lib.rs"); + let accessory = ["ActivationPolicy::", "Accessory"].concat(); + assert!(!src.contains(&accessory), "the app must not switch to the accessory policy"); + } + #[test] fn the_app_menu_ids_never_clash_with_the_tray_menu() { // The tray menu's own "quit" and "open" are handled by the tray. The app menu uses its own ids, so one click diff --git a/apps/desktop/src-tauri/tauri.conf.json b/apps/desktop/src-tauri/tauri.conf.json index ae47d36..a1d4924 100644 --- a/apps/desktop/src-tauri/tauri.conf.json +++ b/apps/desktop/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "Theseus", - "version": "0.6.4", + "version": "0.6.5", "identifier": "io.github.mgilardi.theseus", "build": { "beforeDevCommand": "pnpm dev", diff --git a/engine/VERSION b/engine/VERSION index d2b13eb..ef5e445 100644 --- a/engine/VERSION +++ b/engine/VERSION @@ -1 +1 @@ -0.6.4 +0.6.5