Skip to main content

agentmux_srv/server/app_api/
supervisor.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `supervisor.set-mode`: the Swarm's Supervisor control, for one row or a
5//! fleet selection. WS-RPC only, like `fleet.broadcast`: it is the user's
6//! control in Swarm, and no MCP tool sets it, so an agent can't put itself (or
7//! another agent) in Drive. docs/specs/SPEC_SWARM_SUPERVISOR_2026_10_16.md ยง3.1.
8
9use std::sync::Arc;
10
11use crate::backend::rpc::engine::WshRpcEngine;
12use crate::backend::rpc_types::*;
13
14use super::AppState;
15
16pub fn register(engine: &Arc<WshRpcEngine>, _state: &AppState) {
17    engine.register_typed(COMMAND_SUPERVISOR_SET_MODE, move |cmd: CommandSupervisorSetModeData, ctx| async move {
18        // Whoever asks is recorded; the mode itself is the Swarm's to set, and
19        // refused for Steady/Drive while the Supervisor is switched off.
20        let caller = if ctx.agent_id.is_empty() { "the app".to_string() } else { format!("agent {}", ctx.agent_id) };
21        tracing::info!(
22            caller = %caller,
23            mode = cmd.mode.as_str(),
24            targets = cmd.targets.len(),
25            "supervisor.set-mode"
26        );
27        Ok(set_mode(cmd).await)
28    });
29}
30
31async fn set_mode(cmd: CommandSupervisorSetModeData) -> FleetActionResult {
32    let mut result = FleetActionResult::default();
33    let Some(supervisor) = crate::backend::supervisor::watcher::instance() else {
34        result.failed = cmd
35            .targets
36            .into_iter()
37            .map(|id| FleetActionFailure { id, error: "the Supervisor is not running".to_string() })
38            .collect();
39        return result;
40    };
41    for block_id in cmd.targets {
42        match supervisor.set_mode(&block_id, cmd.mode).await {
43            Ok(()) => result.succeeded.push(block_id),
44            Err(error) => result.failed.push(FleetActionFailure { id: block_id, error }),
45        }
46    }
47    result
48}