Skip to main content

agentmux_srv_core/backend/container/
routes.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What a container agent is told when it calls a route it may not. Which routes those are is in
5//! `route_audience` (a route srv serves is for everyone unless an entry there says otherwise; a path
6//! that is neither is refused), so there is one list to read. This module is the container
7//! listener's view of it. `served` says whether srv serves a route at a path; srv answers it from
8//! its route table.
9
10use crate::backend::route_audience::{self, Served, UNCLASSIFIED};
11
12/// Why a container agent may not call `path`, or `None` when it may.
13pub fn refusal(path: &str, served: Served) -> Option<&'static str> {
14    route_audience::refusal(path, served)
15}
16
17/// What a refused container agent is told: the reason, in a sentence of its own. A path with no
18/// audience is logged, so it gets one.
19pub fn refusal_message(path: &str, served: Served) -> Option<String> {
20    let why = refusal(path, served)?;
21    if why == UNCLASSIFIED {
22        tracing::warn!(path, "a container agent called a route with no audience; declare it in srv's route table or name it in backend/route_audience.rs");
23    }
24    Some(format!("this route is not available to container agents: {why}"))
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    /// Srv serves nothing: the answers of `route_audience`'s entries alone. What is open is tested in srv, which
32    /// has the route table (`server/tests/route_audience_tests.rs`).
33    fn nothing(_: &str) -> bool {
34        false
35    }
36
37    #[test]
38    fn what_a_container_agent_cannot_do_says_why_and_what_to_do() {
39        let cases = [
40            ("/api/v1/shell/create", "can't run commands on the host"),
41            ("/api/v1/ptyshell/input", "can't run commands on the host"),
42            ("/api/v1/conn/list", "can't run commands on the host"),
43            ("/api/v1/agent/desktop/authorize", "launch programs"),
44            ("/api/v1/host/vault", "for the AgentMux app itself"),
45            ("/agentmux/reactive/poller/config", "for the AgentMux app itself"),
46            ("/api/messaging/discord/send", "for the AgentMux app itself"),
47            ("/agentmux/service", "generic gateway"),
48            ("/ws", "generic gateway"),
49            ("/agentmux/stream-local-file", "reads files on the host"),
50            ("/api/v1/agent/self/upgrade", "replaces the app"),
51            ("/api/v1/agent/globalmemory/write", "read it but not change it"),
52        ];
53        for (path, says) in cases {
54            let message = refusal_message(path, nothing).unwrap_or_else(|| panic!("{path} should be refused"));
55            assert!(message.starts_with("this route is not available to container agents: "), "{message}");
56            assert!(message.contains(says), "{path}: {message}");
57        }
58    }
59
60    #[test]
61    fn near_misses_are_not_taken_for_a_refused_prefix() {
62        // Not routes, so nothing claims them; they are told so, not that a host route is closed.
63        for path in ["/api/v1/shells", "/api/v1/hostile", "/api/v1/agent/desktops"] {
64            assert_eq!(refusal(path, nothing), Some(UNCLASSIFIED), "{path}");
65        }
66    }
67}