agentmux_mcp/tools/
stop_tool.rs1use 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 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 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 #[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 #[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 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}