Skip to main content

agentmux_srv/server/
quit_question.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What the launcher asks before the tray's Quit: which agents a quit stops, and whether to ask.
5//!
6//! Quitting stops every agent. A window's close warns first when it stops agents in the middle of
7//! a turn (`frontend/app/window/window-close.ts`). The tray's Quit never passes a page, and it is
8//! most often used with no window open, when the agents running are the ones nobody is looking at.
9//! So the launcher asks here (`GET /agentmux/quit-question`), and puts the question in a dialog
10//! of its own (`crates/launcher/src/quit_ask.rs`).
11
12use axum::extract::State;
13use axum::response::Json;
14use serde_json::{json, Value};
15
16use super::AppState;
17
18/// The close warning's setting. Its "Don't ask again" turns this question off too.
19const CONFIRM_SETTING: &str = "window:confirmcloseworking";
20
21/// The answer, from each running agent's `(name, mid-turn)`: `ask` (an agent is mid-turn and the
22/// warning is on), `working` (their names, sorted) and `agents` (how many agents the quit stops).
23pub(crate) fn question(agents: &[(String, bool)], confirm: bool) -> Value {
24    let mut working: Vec<&str> = agents.iter().filter(|(_, working)| *working).map(|(name, _)| name.as_str()).collect();
25    working.sort_unstable();
26    json!({ "ask": confirm && !working.is_empty(), "working": working, "agents": agents.len() })
27}
28
29pub(super) async fn handle_quit_question(State(state): State<AppState>) -> Json<Value> {
30    let agents: Vec<(String, bool)> = crate::backend::blockcontroller::get_all_controllers()
31        .keys()
32        .filter_map(|block_id| super::service::session_closed::agent_of(&state.mstore, block_id))
33        .collect();
34    let confirm = state.config_watcher.get_full_config().settings.extra.get(CONFIRM_SETTING) != Some(&Value::Bool(false));
35    Json(question(&agents, confirm))
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    fn agents(list: &[(&str, bool)]) -> Vec<(String, bool)> {
43        list.iter().map(|(name, working)| (name.to_string(), *working)).collect()
44    }
45
46    #[test]
47    fn it_asks_only_when_an_agent_is_mid_turn_and_names_those() {
48        let q = question(&agents(&[("Loap", true), ("Korp", true), ("Camper", false)]), true);
49        assert_eq!(q, json!({ "ask": true, "working": ["Korp", "Loap"], "agents": 3 }));
50        let idle = question(&agents(&[("Camper", false)]), true);
51        assert_eq!(idle, json!({ "ask": false, "working": [], "agents": 1 }));
52        assert_eq!(question(&[], true), json!({ "ask": false, "working": [], "agents": 0 }));
53    }
54
55    #[test]
56    fn dont_ask_again_turns_the_question_off_and_still_says_who_is_working() {
57        let q = question(&agents(&[("Korp", true)]), false);
58        assert_eq!(q, json!({ "ask": false, "working": ["Korp"], "agents": 1 }));
59    }
60}