Skip to main content

agentmux_mcp/tools/
tasks_tool.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Tasks: one tool for the shared task queue (Muxqueue), with an `action`
5//! (docs/specs/SPEC_MUXAPI_TOOL_CONSOLIDATION_2026_10_15.md §4.2, slice S3). It
6//! replaces the six Work* tools, renamed so the queue can't be confused with
7//! Presence's claims; their handlers (work.rs) run each action unchanged.
8
9use super::family::{pick, relabeled, Act};
10use super::*;
11
12pub(super) const ACTS: &[Act] = &[
13    Act { action: "enqueue", tool: "WorkEnqueue", rename: &[] },
14    Act { action: "claim", tool: "WorkClaim", rename: &[] },
15    Act { action: "heartbeat", tool: "WorkHeartbeat", rename: &[] },
16    Act { action: "complete", tool: "WorkComplete", rename: &[] },
17    Act { action: "release", tool: "WorkRelease", rename: &[] },
18    Act { action: "list", tool: "WorkList", rename: &[] },
19    Act { action: "get", tool: "WorkGet", rename: &[] },
20    // The token tally of one task (SPEC_TOKEN_ANALYSIS_PER_KANBAN_TASK_2026_11_22.md §4.9).
21    Act { action: "tokens", tool: "WorkTokens", rename: &[] },
22    // Kanban (SPEC_WARDEN_KANBAN_2026_10_20.md §6.11).
23    Act { action: "update", tool: "WorkUpdate", rename: &[] },
24    Act { action: "ask", tool: "WorkAsk", rename: &[] },
25    Act { action: "submit", tool: "WorkSubmit", rename: &[] },
26    // SPEC_KANBAN_AGENTS_HOLD_AND_UNHOLD_2026_10_22.md §3.1.
27    Act { action: "hold", tool: "WorkHold", rename: &[] },
28    Act { action: "unhold", tool: "WorkUnhold", rename: &[] },
29    // Any agent decides a task in Review, and makes the Kanban's other moves; each is recorded as the agent.
30    Act { action: "accept", tool: "WorkAccept", rename: &[] },
31    Act { action: "send_back", tool: "WorkSendBack", rename: &[] },
32    Act { action: "edit", tool: "WorkEdit", rename: &[] },
33    Act { action: "move", tool: "WorkMove", rename: &[] },
34    Act { action: "start", tool: "WorkStart", rename: &[] },
35    Act { action: "answer", tool: "WorkAnswer", rename: &[] },
36    Act { action: "cancel", tool: "WorkCancel", rename: &[] },
37    Act { action: "reopen", tool: "WorkReopen", rename: &[] },
38    Act { action: "retry", tool: "WorkRetry", rename: &[] },
39    Act { action: "take_back", tool: "WorkTakeBack", rename: &[] },
40    Act { action: "reassign", tool: "WorkReassign", rename: &[] },
41    // Several boards (SPEC_SHARED_KANBAN_ACROSS_MACHINES_2026_11_01.md §9).
42    Act { action: "boards", tool: "WorkBoards", rename: &[] },
43    Act { action: "board_create", tool: "WorkBoardCreate", rename: &[] },
44    Act { action: "board_edit", tool: "WorkBoardEdit", rename: &[] },
45    Act { action: "board_archive", tool: "WorkBoardArchive", rename: &[] },
46    Act { action: "board_unarchive", tool: "WorkBoardUnarchive", rename: &[] },
47    Act { action: "board_default", tool: "WorkBoardDefault", rename: &[] },
48    Act { action: "board_delete", tool: "WorkBoardDelete", rename: &[] },
49    Act { action: "board_cancel_delete", tool: "WorkBoardCancelDelete", rename: &[] },
50];
51
52pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
53    match name {
54        "Tasks" => {
55            if let Some(host) = remote_op::host_arg(arguments) {
56                return remote(&host, arguments, cx).await;
57            }
58            let (act, args) = pick("Tasks", ACTS, arguments)?;
59            relabeled("Tasks", ACTS, work::call(act.tool, &args, cx).await)
60        }
61        _ => Err(not_in_family()),
62    }
63}
64
65/// Fields of `enqueue` that travel to another machine's board. Not `target_group` (a group is local),
66/// not `for: "me"` (this agent has no backlog there), and never a filer: srv names the agent itself.
67const ENQUEUE_FIELDS: &[&str] = &["title", "payload", "kind", "target_agent", "priority", "max_attempts", "not_before", "repo", "hold", "review", "after", "first", "ui_change", "board"];
68
69/// `enqueue` and `list` with a `host`: this agent's request to that machine's board.
70async fn remote(host: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
71    let (op, params, reason) = match str_arg(arguments, "action").as_deref() {
72        Some("enqueue") => {
73            let params = enqueue_params(arguments)?;
74            let title = params["title"].as_str().unwrap_or("").chars().take(60).collect::<String>();
75            ("tasks.enqueue", params, format!("Tasks enqueue: {title}"))
76        }
77        Some("list") => ("tasks.list", list_params(arguments), "Tasks list".to_string()),
78        other => anyhow::bail!(
79            "Tasks: with host only enqueue and list run on another machine (you asked for {other:?}); leave host out to act on this machine's board"
80        ),
81    };
82    let answer = remote_op::remote_op_call(cx, host, op, params, &reason).await?;
83    match remote_op::explain_not_done(host, op, &answer) {
84        Some(problem) => anyhow::bail!("{problem}"),
85        None => Ok(remote_op::pretty(host, &answer)),
86    }
87}
88
89/// The params of `tasks.enqueue` from the tool's arguments; what can't be asked of another machine is an error.
90fn enqueue_params(arguments: &Value) -> Result<Value> {
91    if arguments.get("for").and_then(Value::as_str).is_some() {
92        anyhow::bail!("Tasks enqueue: for \"me\" files into your own backlog and can't go to another machine; name target_agent there, or leave host out");
93    }
94    if str_arg(arguments, "target_group").is_some() {
95        anyhow::bail!("Tasks enqueue: target_group is a group on this machine and can't be used with host; name target_agent there");
96    }
97    if str_arg(arguments, "title").is_none() || str_arg(arguments, "payload").is_none() {
98        anyhow::bail!("Tasks enqueue: title and payload are required");
99    }
100    let mut params = serde_json::Map::new();
101    for key in ENQUEUE_FIELDS {
102        if let Some(v) = arguments.get(*key).filter(|v| !v.is_null()) {
103            params.insert((*key).to_string(), v.clone());
104        }
105    }
106    Ok(Value::Object(params))
107}
108
109fn list_params(arguments: &Value) -> Value {
110    let mut params = serde_json::Map::new();
111    for key in ["state", "limit", "board"] {
112        if let Some(v) = arguments.get(key).filter(|v| !v.is_null()) {
113            params.insert(key.to_string(), v.clone());
114        }
115    }
116    Value::Object(params)
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122
123    #[test]
124    fn an_enqueue_for_another_machine_carries_only_what_that_board_takes() {
125        let args = json!({ "action": "enqueue", "host": "starpower", "title": "Verify", "payload": "run it", "target_agent": "clare", "first": "spec",
126            "reason": "x", "created_by": "forged", "files": ["a.png"] });
127        let p = enqueue_params(&args).unwrap();
128        assert_eq!(p, json!({ "title": "Verify", "payload": "run it", "target_agent": "clare", "first": "spec" }));
129    }
130
131    #[test]
132    fn what_only_makes_sense_here_is_refused_with_the_way_out() {
133        let base = |extra: Value| {
134            let mut a = json!({ "action": "enqueue", "title": "t", "payload": "p" });
135            a.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
136            a
137        };
138        assert!(enqueue_params(&base(json!({ "for": "me" }))).unwrap_err().to_string().contains("own backlog"));
139        assert!(enqueue_params(&base(json!({ "target_group": "g" }))).unwrap_err().to_string().contains("target_agent"));
140        assert!(enqueue_params(&json!({ "action": "enqueue", "title": "t" })).unwrap_err().to_string().contains("required"));
141    }
142
143    #[test]
144    fn a_list_asks_only_for_a_state_and_a_limit() {
145        assert_eq!(list_params(&json!({ "action": "list", "host": "h", "state": "open", "limit": 5, "id": "9" })), json!({ "state": "open", "limit": 5 }));
146        assert_eq!(list_params(&json!({ "action": "list" })), json!({}));
147    }
148
149    #[test]
150    fn every_action_is_in_the_schema() {
151        let schema: Value = serde_json::from_str(crate::tool_schemas::TASKS_TOOL).unwrap();
152        let mut listed: Vec<&str> = schema["inputSchema"]["properties"]["action"]["enum"]
153            .as_array()
154            .unwrap()
155            .iter()
156            .filter_map(|v| v.as_str())
157            .collect();
158        let mut want = super::super::family::action_names(ACTS);
159        listed.sort();
160        want.sort();
161        assert_eq!(listed, want);
162    }
163}