From 8db8df5791e0d6195889df409c0e8d08a79f1b24 Mon Sep 17 00:00:00 2001 From: Justin Parker Date: Wed, 28 Oct 2026 01:14:50 -0700 Subject: [PATCH] Make the quit check say whether the agent is waiting on you, with one icon AI Assisted --- .agents-docs/AGENTS-architecture.md | 1 + plan2code-app/src-tauri/Cargo.toml | 5 +- plan2code-app/src-tauri/src/lib.rs | 3 + plan2code-app/src-tauri/src/main.rs | 72 +++++++++++++-- plan2code-app/src-tauri/src/quit_prompt.rs | 102 +++++++++++++++++++++ plan2code-app/src-tauri/src/task_dialog.rs | 98 ++++++++++++++++++++ 6 files changed, 272 insertions(+), 9 deletions(-) create mode 100644 plan2code-app/src-tauri/src/quit_prompt.rs create mode 100644 plan2code-app/src-tauri/src/task_dialog.rs diff --git a/.agents-docs/AGENTS-architecture.md b/.agents-docs/AGENTS-architecture.md index 671ba38..5d3e431 100644 --- a/.agents-docs/AGENTS-architecture.md +++ b/.agents-docs/AGENTS-architecture.md @@ -97,6 +97,7 @@ plan2code/ | `src/launcher/plan2code.js` | Global `plan2code` command that opens the dashboard skill in Claude Code, Devin or Codex (installed by `I` and Advanced → `E`; also via Advanced → `D`). When the desktop app is installed (`~/.plan2code/app/plan2code-app[.exe]`, or the file `PLAN2CODE_APP` names) and a display exists (Windows and macOS always; Linux needs `DISPLAY` or `WAYLAND_DISPLAY`), it opens the app after the agent and model questions with `--project --agent [--model ]` and exits; `--classic` (never forwarded to the CLI) and a failed spawn keep the terminal flow. The app reads all three (`project::launch_choice`): the picker starts on `--agent`, and `--model` applies to that agent on every start and Restart (`AgentManager::set_model`); a model never applies to another agent | | `plan2code-app/` | Tauri desktop app (Rust host, xterm panel, the console's dashboard in a frame; its own README). `RELEASE-CHECKLIST.md` lists the by-hand checks before each release. `src-tauri/src/versions.rs` compares the app's version (`tauri.conf.json`) with the installed skills' (`~/.plan2code/bin/version.json`) for the bar's **Update skills** line, which runs `node ~/.plan2code/bin/plan2code.js update` only when pressed | | `plan2code-app/src-tauri/src/attention.rs` | The taskbar signal's rules, pure and knowing nothing about Tauri: `Signal` (`busy`, `ready`, `attention`, `plain`), `badge_for(signal, os, enabled)` (Windows: green dot on `ready`, red on `attention`; macOS: red `!` on `attention` only; Linux: count 1 on `attention`), `should_flash(prev, next, focused, enabled)` (once, only on a change into `ready` or `attention` while unfocused) and `dot_rgba()` (the dot's pixels, in the favicon's colors, which a test checks against `public/favicon.js`). `main.rs` applies the result through `set_attention`, `set_taskbar_signal` and `clear_attention_cmd`; the page asks with the `attention` and `taskbar` messages (`public/app-bridge.js`) and the switch is `taskbarSignal` in the app's own `~/.plan2code/app.json`. | +| `plan2code-app/src-tauri/src/quit_prompt.rs`, `task_dialog.rs` | The quit check shown while an agent runs. `quit_prompt(last_signal)` is pure: the page's last taskbar signal `ready` or `plain` is the waiting case ("Done for now?", Info icon), and `busy`, `attention` or none yet is the working case ("Leave while your agent is busy?", Warning icon). `task_dialog.rs` (Windows only) draws it with `TaskDialogIndirect` through `winapi`: a heading, Stay as the default, and the app's icon in the title bar, since the dialog plugin has no heading and Windows copies its icon into the title bar (the warning once showed twice). `confirm_quit` in `main.rs` runs it on its own thread; macOS and Linux use the plugin box with the same words, where Quit stays the default button (putting Stay first would make Esc quit). A page reload or an unanswered page clears the signal to none (`clear_attention`), so the box stays on the working words until the page reports again; `QUIT_OPEN` keeps a second box from stacking | | `plan2code-app/src-tauri/src/agents.rs` | Custom agents in the app: `CustomAgent`, the `Agent` handle over built-ins and custom entries, `load_custom_agents`, reading the Agents file below; the console side is `src/web-console/agents.mjs` | | Desktop app install (`install.js`) | `installDesktopApp()` downloads `plan2code-app---[.exe]` and `plan2code-app--SHA256SUMS.txt` from the GitHub release (`PLAN2CODE_RELEASE_BASE` replaces the URL; the tests serve a stand-in), checks the SHA-256 and places the file in `~/.plan2code/app/` (the old file is renamed aside first, since Windows cannot overwrite a running exe). `app` is an install piece (`INSTALL_PIECES`, after `launcher`), so `plan2code update` updates it with the rest. `install()` asks for it after the command (default yes; a no is kept as `skipped` in `install.json`); unattended it installs it unless it is already recorded or was declined. A failed app never fails the install. `.github/workflows/release-app.yml` builds one file per system (`tauri build --no-bundle`) and the checksum list on a published release, after `scripts/check-versions.mjs --tag` | diff --git a/plan2code-app/src-tauri/Cargo.toml b/plan2code-app/src-tauri/Cargo.toml index f22878b..529e40b 100644 --- a/plan2code-app/src-tauri/Cargo.toml +++ b/plan2code-app/src-tauri/Cargo.toml @@ -26,5 +26,6 @@ libc = "0.2" [target.'cfg(windows)'.dependencies] # Already pulled in by portable-pty itself; named directly here only for the # Job Object APIs it does not expose (ending a `cmd /c` npm shim's whole -# process tree, not just the shim). -winapi = { version = "0.3.9", features = ["handleapi", "jobapi2", "minwindef", "ntdef", "winnt"] } +# process tree, not just the shim), and for the quit check's own TaskDialog +# (task_dialog.rs). +winapi = { version = "0.3.9", features = ["commctrl", "handleapi", "jobapi2", "minwindef", "ntdef", "windef", "winerror", "winnt", "winuser"] } diff --git a/plan2code-app/src-tauri/src/lib.rs b/plan2code-app/src-tauri/src/lib.rs index c5e3bd8..67e6893 100644 --- a/plan2code-app/src-tauri/src/lib.rs +++ b/plan2code-app/src-tauri/src/lib.rs @@ -10,7 +10,10 @@ pub mod handoff; pub mod node_check; pub mod project; pub mod pty_host; +pub mod quit_prompt; pub mod restart_brief; pub mod session_reader; pub mod settings; +#[cfg(windows)] +pub mod task_dialog; pub mod versions; diff --git a/plan2code-app/src-tauri/src/main.rs b/plan2code-app/src-tauri/src/main.rs index 97b5485..b207006 100644 --- a/plan2code-app/src-tauri/src/main.rs +++ b/plan2code-app/src-tauri/src/main.rs @@ -1,6 +1,7 @@ // Hide the extra console window on Windows release builds. #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, OnceLock}; use plan2code_app_lib::agent_manager::{AgentEvent, AgentManager, AgentState, BeforeFresh, Sink}; @@ -12,8 +13,11 @@ use plan2code_app_lib::console_supervisor::{ }; use plan2code_app_lib::node_check; use plan2code_app_lib::project::{self, ProjectDir}; +use plan2code_app_lib::quit_prompt::{quit_prompt, QuitCase}; use plan2code_app_lib::restart_brief; use plan2code_app_lib::settings; +#[cfg(windows)] +use plan2code_app_lib::task_dialog; use plan2code_app_lib::versions; use tauri::ipc::{Channel, InvokeResponseBody}; use tauri::{Emitter, Manager, State}; @@ -405,17 +409,70 @@ fn agent_reattach( }) } -// Asks before quitting while an agent runs; "no" keeps the app open. +// Set while a quit box is open, so a second close or quit does not stack another. +static QUIT_OPEN: AtomicBool = AtomicBool::new(false); + +// Clears QUIT_OPEN when the box closes, however its handler ends. +struct QuitOpen; + +impl Drop for QuitOpen { + fn drop(&mut self) { + QUIT_OPEN.store(false, Ordering::SeqCst); + } +} + +// Asks before quitting while an agent runs; anything but Quit keeps the app +// open. The words follow whose turn the page last reported (quit_prompt.rs). fn confirm_quit(app: &tauri::AppHandle) { + if QUIT_OPEN.swap(true, Ordering::SeqCst) { + return; + } + let open = QuitOpen; + let last = app + .state::() + .attention + .lock() + .ok() + .and_then(|guard| guard.last); + let prompt = quit_prompt(last); + #[cfg(windows)] + { + if let Some(parent) = app + .get_webview_window("main") + .and_then(|window| window.hwnd().ok()) + { + // TaskDialog blocks until it closes, so it runs off the event loop. + let app = app.clone(); + let parent = parent.0 as isize; + std::thread::spawn(move || { + let _open = open; + if task_dialog::ask(parent as _, &prompt) { + quit_app(&app); + } + }); + return; + } + } + let kind = match prompt.case { + QuitCase::Waiting => MessageDialogKind::Info, + QuitCase::Working => MessageDialogKind::Warning, + }; + // The plugin makes its first button the default, so off Windows Enter + // means Quit. Stay first would make Esc quit instead, since the plugin + // reads Esc as "not the first button". let app = app.clone(); app.dialog() - .message("An agent is still running. Quit and stop it?") - .title("Quit Plan2Code") - .kind(MessageDialogKind::Warning) - .buttons(MessageDialogButtons::YesNo) + .message(prompt.message) + .title(prompt.heading) + .kind(kind) + .buttons(MessageDialogButtons::OkCancelCustom( + prompt.quit.to_string(), + prompt.stay.to_string(), + )) .show({ let app = app.clone(); move |quit| { + let _open = open; if quit { quit_app(&app); } @@ -506,10 +563,11 @@ fn apply_badge(window: &tauri::WebviewWindow, badge: Badge) { } // No dot and no flash: the agent ended, the page went away, or the switch was -// turned off. The next state the page tells starts from plain. +// turned off. Whose turn it is is unknown until the page tells again, so the +// quit check uses its working words meanwhile (the agent may be mid-step). fn clear_attention(app: &tauri::AppHandle) { if let Ok(mut guard) = app.state::().attention.lock() { - guard.last = Some(Signal::Plain); + guard.last = None; } let Some(window) = app.get_webview_window("main") else { return; diff --git a/plan2code-app/src-tauri/src/quit_prompt.rs b/plan2code-app/src-tauri/src/quit_prompt.rs new file mode 100644 index 0000000..e360e08 --- /dev/null +++ b/plan2code-app/src-tauri/src/quit_prompt.rs @@ -0,0 +1,102 @@ +//! The words the quit check shows while an agent runs. Most of the time the +//! agent is not mid-step at all but waiting on the person's answer in the web +//! console, so the check reads the page's last taskbar signal (attention.rs) +//! and says which case it is. Anything it cannot be sure of is the working +//! case, the safer warning. Knows nothing about Tauri; main.rs shows it. + +use crate::attention::Signal; + +/// Whether quitting would stop the agent partway through something. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum QuitCase { + /// The person's move, or nothing left to do: nothing is half done. + Waiting, + /// Busy, quiet for too long, gone, or not known yet. + Working, +} + +/// Everything the box says. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct QuitPrompt { + pub case: QuitCase, + pub title: &'static str, + pub heading: &'static str, + pub message: &'static str, + pub quit: &'static str, + pub stay: &'static str, +} + +/// The prompt for the page's last signal (`None` before the page has sent one). +pub fn quit_prompt(last: Option) -> QuitPrompt { + match last { + Some(Signal::Ready | Signal::Plain) => QuitPrompt { + case: QuitCase::Waiting, + title: "Plan2Code", + heading: "Done for now?", + message: "Your agent is just waiting on your next answer. Quit and this session ends.", + quit: "Quit", + stay: "Stay", + }, + Some(Signal::Busy | Signal::Attention) | None => QuitPrompt { + case: QuitCase::Working, + title: "Plan2Code", + heading: "Leave while your agent is busy?", + message: "If you quit now, the agent stops mid-step. Everything it has saved so far stays put.", + quit: "Quit and stop it", + stay: "Stay", + }, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_persons_move_and_nothing_left_are_the_waiting_case() { + assert_eq!(quit_prompt(Some(Signal::Ready)).case, QuitCase::Waiting); + assert_eq!(quit_prompt(Some(Signal::Plain)).case, QuitCase::Waiting); + } + + #[test] + fn busy_attention_and_unknown_are_the_working_case() { + assert_eq!(quit_prompt(Some(Signal::Busy)).case, QuitCase::Working); + assert_eq!( + quit_prompt(Some(Signal::Attention)).case, + QuitCase::Working, + "attention covers a quiet agent that may still be mid-step" + ); + assert_eq!(quit_prompt(None).case, QuitCase::Working); + } + + #[test] + fn each_case_has_its_own_words() { + let waiting = quit_prompt(Some(Signal::Ready)); + assert_eq!(waiting.heading, "Done for now?"); + assert_eq!(waiting.quit, "Quit"); + let working = quit_prompt(Some(Signal::Busy)); + assert_eq!(working.heading, "Leave while your agent is busy?"); + assert_eq!(working.quit, "Quit and stop it"); + assert!(working.message.contains("mid-step")); + } + + #[test] + fn stay_is_never_mistaken_for_quit() { + for last in [None, Some(Signal::Busy), Some(Signal::Ready)] { + let prompt = quit_prompt(last); + assert_eq!(prompt.stay, "Stay"); + assert_ne!(prompt.stay, prompt.quit); + assert_eq!(prompt.title, "Plan2Code"); + } + } + + #[test] + fn no_text_carries_an_em_dash() { + for last in [None, Some(Signal::Busy), Some(Signal::Ready)] { + let p = quit_prompt(last); + for text in [p.title, p.heading, p.message, p.quit, p.stay] { + assert!(!text.contains('\u{2014}'), "{text}"); + } + } + } +} diff --git a/plan2code-app/src-tauri/src/task_dialog.rs b/plan2code-app/src-tauri/src/task_dialog.rs new file mode 100644 index 0000000..7f8d8b7 --- /dev/null +++ b/plan2code-app/src-tauri/src/task_dialog.rs @@ -0,0 +1,98 @@ +//! The quit check's box on Windows, drawn with TaskDialog directly. The dialog +//! plugin has no heading, always makes its first button the default, and +//! leaves Windows to copy the body's icon into the title bar, so the same +//! warning showed twice. Here the heading is the question, Stay is the default +//! (Enter, Esc and the close button all keep the app open), and the title bar +//! wears the app's own icon. The words come from quit_prompt.rs. + +use crate::quit_prompt::{QuitCase, QuitPrompt}; +use std::ptr; +use winapi::shared::basetsd::LONG_PTR; +use winapi::shared::minwindef::{LPARAM, UINT, WPARAM}; +use winapi::shared::ntdef::HRESULT; +use winapi::shared::windef::HWND; +use winapi::shared::winerror::S_OK; +use winapi::um::commctrl::{ + TaskDialogIndirect, TASKDIALOGCONFIG, TASKDIALOG_BUTTON, TDF_ALLOW_DIALOG_CANCELLATION, + TDF_POSITION_RELATIVE_TO_WINDOW, TDN_CREATED, TD_INFORMATION_ICON, TD_WARNING_ICON, +}; +use winapi::um::winuser::{ + GetClassLongPtrW, SendMessageW, GCLP_HICONSM, ICON_BIG, ICON_SMALL, ICON_SMALL2, WM_GETICON, + WM_SETICON, +}; + +const ID_QUIT: i32 = 100; +const ID_STAY: i32 = 101; + +fn wide(text: &str) -> Vec { + text.encode_utf16().chain(Some(0)).collect() +} + +// Once the box exists, give its title bar the parent window's icon instead of +// the copy of the body's icon Windows puts there. +unsafe extern "system" fn on_event( + hwnd: HWND, + msg: UINT, + _wparam: WPARAM, + _lparam: LPARAM, + parent: LONG_PTR, +) -> HRESULT { + if msg == TDN_CREATED { + let parent = parent as HWND; + let mut icon = SendMessageW(parent, WM_GETICON, ICON_SMALL2 as WPARAM, 0); + if icon == 0 { + icon = GetClassLongPtrW(parent, GCLP_HICONSM) as isize; + } + SendMessageW(hwnd, WM_SETICON, ICON_SMALL as WPARAM, icon); + SendMessageW(hwnd, WM_SETICON, ICON_BIG as WPARAM, icon); + } + S_OK +} + +/// Shows the box over `parent` and blocks until it closes. True only when +/// Quit was pressed; anything else (Stay, Esc, the close button, a failure to +/// show the box) keeps the app open. +pub fn ask(parent: HWND, prompt: &QuitPrompt) -> bool { + let title = wide(prompt.title); + let heading = wide(prompt.heading); + let message = wide(prompt.message); + let quit = wide(prompt.quit); + let stay = wide(prompt.stay); + let buttons = [ + TASKDIALOG_BUTTON { + nButtonID: ID_QUIT, + pszButtonText: quit.as_ptr(), + }, + TASKDIALOG_BUTTON { + nButtonID: ID_STAY, + pszButtonText: stay.as_ptr(), + }, + ]; + let icon = match prompt.case { + QuitCase::Waiting => TD_INFORMATION_ICON, + QuitCase::Working => TD_WARNING_ICON, + }; + + // SAFETY: every pointer in the config points at a local above that + // outlives the blocking TaskDialogIndirect call, and an all-zero + // TASKDIALOGCONFIG is the documented "nothing set" for every other field. + unsafe { + let mut config: TASKDIALOGCONFIG = std::mem::zeroed(); + config.cbSize = std::mem::size_of::() as UINT; + config.hwndParent = parent; + config.dwFlags = TDF_ALLOW_DIALOG_CANCELLATION | TDF_POSITION_RELATIVE_TO_WINDOW; + config.pszWindowTitle = title.as_ptr(); + *config.u1.pszMainIcon_mut() = icon; + config.pszMainInstruction = heading.as_ptr(); + config.pszContent = message.as_ptr(); + config.cButtons = buttons.len() as UINT; + config.pButtons = buttons.as_ptr(); + config.nDefaultButton = ID_STAY; + config.pfCallback = Some(on_event); + config.lpCallbackData = parent as LONG_PTR; + + let mut pressed = 0; + let result = TaskDialogIndirect(&config, &mut pressed, ptr::null_mut(), ptr::null_mut()); + result == S_OK && pressed == ID_QUIT + } +}