Skip to main content

agentmux_mcp/tools/
stop_tool.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Stop: one tool for the actions that end work, with an `action`
5//! (docs/specs/SPEC_MUXAPI_TOOL_CONSOLIDATION_2026_10_15.md §4.1 rule 6 and
6//! §4.2, slice S4). Kept apart from the other tools so a harness can ask before
7//! any of them. It replaces QuitSelf, FleetBulkStop and ClosePane; their
8//! handlers (panes.rs, fleet.rs) run each action unchanged, and srv's own
9//! guards (the user's override window, the audit log) apply to all three.
10
11use super::family::{pick, relabeled, Act};
12use super::*;
13
14pub(super) const ACTS: &[Act] = &[
15    Act { action: "self", tool: "QuitSelf", rename: &[] },
16    Act { action: "agents", tool: "FleetBulkStop", rename: &[] },
17    Act { action: "pane", tool: "ClosePane", rename: &[] },
18];
19
20pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
21    match name {
22        "Stop" => {
23            let (act, args) = pick("Stop", ACTS, arguments)?;
24            // ClosePane with no block_id ended the caller's own session without
25            // QuitSelf's quote check; here ending your own session is `self`.
26            // (Closing a pane you are in, by id, waits for the user's window
27            // in srv like any other pane.)
28            if act.action == "pane" && arg_str(&args, "block_id").is_none() {
29                anyhow::bail!("Stop pane needs block_id (from Pane layout); to end your own session, use action \"self\"");
30            }
31            // `self` reaches only the caller's own block: a target is a
32            // mistake, refused rather than ignored.
33            if act.action == "self" && (args.get("block_id").is_some() || args.get("targets").is_some()) {
34                anyhow::bail!("Stop self ends only your own session and takes no block_id or targets");
35            }
36            let result = match act.tool {
37                "FleetBulkStop" => fleet::call(act.tool, &args, cx).await,
38                _ => panes::call(act.tool, &args, cx).await,
39            };
40            relabeled("Stop", ACTS, result)
41        }
42        _ => Err(not_in_family()),
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn every_action_is_in_the_schema() {
52        let schema: Value = serde_json::from_str(crate::tool_schemas::STOP_TOOL).unwrap();
53        let mut listed: Vec<&str> = schema["inputSchema"]["properties"]["action"]["enum"]
54            .as_array()
55            .unwrap()
56            .iter()
57            .filter_map(|v| v.as_str())
58            .collect();
59        let mut want = super::super::family::action_names(ACTS);
60        listed.sort();
61        want.sort();
62        assert_eq!(listed, want);
63    }
64
65    /// `agents` closes the agent's tab, and the tool says so; the two
66    /// parameters that only made sense for a stop are gone.
67    #[test]
68    fn the_schema_says_agents_closes_the_tab_and_has_no_signal_or_staged() {
69        let schema: Value = serde_json::from_str(crate::tool_schemas::STOP_TOOL).unwrap();
70        let props = &schema["inputSchema"]["properties"];
71        assert!(props.get("signal").is_none() && props.get("staged").is_none(), "{props}");
72        let d = schema["description"].as_str().unwrap();
73        for needle in [
74            "closes each target agent's tab",
75            "Only that agent's tab closes",
76            "My Agents",
77            "your own block (use self)",
78            "An agent's tab is refused with a pointer to agents",
79            "banner in the agent's pane and a notification, each with a countdown",
80            "and Keep running and Close now buttons",
81            "MAJOR WARNING",
82        ] {
83            assert!(d.contains(needle), "the description lacks {needle:?}");
84        }
85        assert!(!d.contains("stop many agent panes"), "the old stop wording is gone");
86    }
87
88    /// Closing a pane never stands in for ending your own session: without a
89    /// block_id it is refused before anything is sent.
90    #[tokio::test]
91    async fn pane_without_a_target_is_refused() {
92        let client = reqwest::Client::new();
93        let loops = LoopRegistry::default();
94        let counter = std::sync::atomic::AtomicU64::new(0);
95        let cx = ToolCtx { local_url: "", auth_key: "", block_id: "", client: &client, loops: &loops, loop_counter: &counter };
96        for args in [json!({ "action": "pane" }), json!({ "action": "pane", "block_id": " " })] {
97            let e = call("Stop", &args, &cx).await.unwrap_err().to_string();
98            assert!(e.starts_with("Stop pane needs block_id"), "{e}");
99        }
100        // And `self` can't be aimed at anyone else.
101        for args in [
102            json!({ "action": "self", "reason": "r", "user_instruction": "quit", "block_id": "b2" }),
103            json!({ "action": "self", "reason": "r", "user_instruction": "quit", "targets": ["b2"] }),
104        ] {
105            let e = call("Stop", &args, &cx).await.unwrap_err().to_string();
106            assert!(e.starts_with("Stop self ends only your own session"), "{e}");
107        }
108    }
109}