1use super::audience::{Audience, HostCap, RuntimeCaps};
23
24pub const HOST_COMMANDS: &str = "container agents can't run commands on the host. Run it in your own shell in the container, \
26 or ask a native agent or your leader.";
27pub const APP_ONLY: &str = "this route is for the AgentMux app itself, not for agents.";
29pub const DESKTOP: &str = "desktop control can launch programs on the host, so container agents can't use it. \
31 Ask a native agent or your leader.";
32pub const GATEWAY: &str = "this is a generic gateway to the host that container agents can't use. Use the specific tool for what you need.";
34pub const STREAMED_BROWSER: &str = "a streamed page runs with the server's own network reach, which a container agent does not have. Use your own Browser tool.";
36pub const HOST_FILES: &str = "this reads files on the host for the app's windows. A container agent reads its own files in /workspace directly; \
38 to show one in a pane, open it with the editor.";
39pub const UPGRADE: &str = "upgrading AgentMux replaces the app on the host, so container agents can't. Ask a native agent or your leader.";
41pub const GLOBAL_MEMORY: &str = "Global Memory is read by every native agent at launch, so a container agent can read it but not change it. \
43 Ask a native agent or your leader to write it.";
44pub const SRV_DOES_IT: &str = "srv does this for container agents itself.";
46pub const INSTANCE_ONLY: &str = "this route is for other AgentMux instances, not for agents.";
48pub const LOCAL_SCRIPTS: &str = "this is the hook a job offers to scripts on this computer, with its own secret; an agent starts a job with Run now.";
50pub const UNCLASSIFIED: &str = "this route has not been classified for container agents yet. Ask your leader to have its audience set.";
52
53#[derive(Debug, Clone, Copy)]
55pub enum Match {
56 Exact(&'static str),
57 Prefix(&'static str),
58 Route(&'static str),
60}
61
62impl Match {
63 pub fn matches(&self, path: &str) -> bool {
64 match *self {
65 Match::Exact(p) => path == p,
66 Match::Prefix(p) => path.starts_with(p),
67 Match::Route(pattern) => route_matches(pattern, path),
68 }
69 }
70}
71
72fn route_matches(pattern: &str, path: &str) -> bool {
73 let mut pat = pattern.split('/');
74 let mut got = path.split('/');
75 loop {
76 match (pat.next(), got.next()) {
77 (None, None) => return true,
78 (Some(p), Some(g)) if p.starts_with('*') => return !g.is_empty(),
79 (Some(p), Some(g)) if p.starts_with(':') => {
80 if g.is_empty() {
81 return false;
82 }
83 }
84 (Some(p), Some(g)) if p == g => {}
85 _ => return false,
86 }
87 }
88}
89
90pub const EXCEPTIONS: &[(Match, Audience)] = &[
93 (Match::Prefix("/api/v1/shell/"), Audience::NativeOnly(HostCap::Exec, HOST_COMMANDS)),
95 (Match::Prefix("/api/v1/ptyshell/"), Audience::NativeOnly(HostCap::Exec, HOST_COMMANDS)),
96 (Match::Prefix("/api/v1/conn/"), Audience::NativeOnly(HostCap::Exec, HOST_COMMANDS)),
97 (Match::Exact("/api/v1/askpass"), Audience::NativeOnly(HostCap::Exec, HOST_COMMANDS)),
98 (Match::Prefix("/api/v1/agent/desktop/"), Audience::NativeOnly(HostCap::Exec, DESKTOP)),
100 (Match::Prefix("/api/v1/host/"), Audience::AppOnly(APP_ONLY)),
102 (Match::Prefix("/agentmux/reactive/poller/"), Audience::AppOnly(APP_ONLY)),
104 (Match::Exact("/agentmux/reactive/unregister"), Audience::AppOnly(APP_ONLY)),
105 (Match::Exact("/agentmux/reactive/ensure-signing-key"), Audience::AppOnly(APP_ONLY)),
106 (Match::Exact("/agentmux/reactive/agents"), Audience::AppOnly(APP_ONLY)),
107 (Match::Exact("/agentmux/reactive/audit"), Audience::AppOnly(APP_ONLY)),
108 (Match::Exact("/agentmux/audit/own"), Audience::AppOnly(APP_ONLY)),
109 (Match::Exact("/agentmux/agent/holding"), Audience::AppOnly(APP_ONLY)),
110 (Match::Exact("/agentmux/diag/sagas"), Audience::AppOnly(APP_ONLY)),
111 (Match::Prefix("/api/messaging/"), Audience::AppOnly(APP_ONLY)),
112 (Match::Exact("/api/post-chat-message"), Audience::AppOnly(APP_ONLY)),
113 (Match::Prefix("/webhook/"), Audience::AppOnly(APP_ONLY)),
114 (Match::Exact("/agentmux/streamed-browser"), Audience::AppOnly(STREAMED_BROWSER)),
117 (Match::Exact("/agentmux/service"), Audience::NativeOnly(HostCap::Exec, GATEWAY)),
119 (Match::Exact("/ws"), Audience::NativeOnly(HostCap::Exec, GATEWAY)),
120 (Match::Exact("/agentmux/wps/publish"), Audience::NativeOnly(HostCap::Exec, GATEWAY)),
121 (Match::Exact("/agentmux/stream-local-file"), Audience::NativeOnly(HostCap::Read, HOST_FILES)),
123 (Match::Exact("/agentmux/file"), Audience::NativeOnly(HostCap::Read, HOST_FILES)),
124 (Match::Exact("/agentmux/file-icon"), Audience::NativeOnly(HostCap::Read, HOST_FILES)),
125 (Match::Exact("/api/v1/agent/globalmemory/write"), Audience::NativeOnly(HostCap::Write, GLOBAL_MEMORY)),
127 (Match::Exact("/api/v1/agent/globalmemory/remove"), Audience::NativeOnly(HostCap::Write, GLOBAL_MEMORY)),
128 (Match::Exact("/api/v1/agent/globalmemory/revert"), Audience::NativeOnly(HostCap::Write, GLOBAL_MEMORY)),
129 (Match::Exact("/api/v1/agent/globalmemory/reach"), Audience::NativeOnly(HostCap::Write, GLOBAL_MEMORY)),
130 (Match::Exact("/api/v1/agent/self/upgrade"), Audience::NativeOnly(HostCap::Write, UPGRADE)),
132 (Match::Exact("/agentmux/agents/self/keys"), Audience::AppOnly("srv signs for a container agent, which holds no key to fetch.")),
134 (Match::Exact("/api/v1/agent/self/mcp-beat"), Audience::AppOnly("health beats belong to persistent controllers; a container's turns are per-exec.")),
135 (Match::Exact("/agentmux/reactive/register"), Audience::AppOnly(SRV_DOES_IT)),
136 (Match::Exact("/agentmux/jobs/stop-run"), Audience::AppOnly(INSTANCE_ONLY)),
138 (Match::Prefix("/agentmux/viewer/"), Audience::AppOnly(INSTANCE_ONLY)),
139 (Match::Prefix("/.well-known/"), Audience::AppOnly(INSTANCE_ONLY)),
140 (Match::Route("/agentmux/hook/:job"), Audience::AppOnly(LOCAL_SCRIPTS)),
142];
143
144pub type Served = fn(&str) -> bool;
147
148pub fn audience_of(path: &str, served: Served) -> Option<Audience> {
151 match EXCEPTIONS.iter().find(|(m, _)| m.matches(path)) {
152 Some((_, audience)) => Some(*audience),
153 None if served(path) => Some(Audience::Everyone),
154 None => None,
155 }
156}
157
158const ROUTE_NAMESPACES: [&str; 2] = ["/api/", "/agentmux/"];
161
162pub fn refusal(path: &str, served: Served) -> Option<&'static str> {
166 match audience_of(path, served) {
167 Some(audience) => audience.refusal(RuntimeCaps::CONTAINER),
168 None if ROUTE_NAMESPACES.iter().any(|ns| path.starts_with(ns)) => Some(UNCLASSIFIED),
169 None => None,
170 }
171}
172
173#[cfg(test)]
174mod tests {
175 use super::*;
176
177 fn sample(route: &str) -> String {
179 route.split('/').map(|s| if s.starts_with(':') || s.starts_with('*') { "x" } else { s }).collect::<Vec<_>>().join("/")
180 }
181
182 fn nothing(_: &str) -> bool {
184 false
185 }
186
187 #[test]
189 fn no_entry_is_dead() {
190 for (i, (m, _)) in EXCEPTIONS.iter().enumerate() {
191 if let Match::Route(p) = m {
192 let s = sample(p);
193 let first = EXCEPTIONS.iter().position(|(e, _)| e.matches(&s)).unwrap();
194 assert_eq!(first, i, "{p} is claimed by an earlier entry ({:?})", EXCEPTIONS[first].0);
195 }
196 }
197 }
198
199 #[test]
201 fn a_served_route_is_for_everyone_unless_an_entry_names_it() {
202 let served: Served = |path| ["/api/v1/vault", "/api/v1/shell/create", "/agentmux/hook/x"].contains(&path);
203 assert_eq!(audience_of("/api/v1/vault", served), Some(Audience::Everyone));
204 assert_eq!(refusal("/api/v1/vault", served), None);
205 assert_eq!(refusal("/api/v1/shell/create", served), Some(HOST_COMMANDS));
206 assert_eq!(refusal("/agentmux/hook/x", served), Some(LOCAL_SCRIPTS));
207 assert_eq!(audience_of("/api/v1/vault", nothing), None);
208 }
209
210 #[test]
211 fn a_route_nobody_classified_is_refused_with_a_message_that_says_so() {
212 assert_eq!(refusal("/api/v1/not-a-route-yet", nothing), Some(UNCLASSIFIED));
213 assert_eq!(refusal("/agentmux/not-a-route-yet", nothing), Some(UNCLASSIFIED));
214 assert!(UNCLASSIFIED.contains("Ask your leader"));
215 }
216
217 #[test]
219 fn a_path_the_frontend_fallback_serves_is_not_refused() {
220 for path in ["/assets/index-abc.js", "/index.html", "/favicon.ico", "/nope"] {
221 assert_eq!(refusal(path, nothing), None, "{path}");
222 }
223 }
224
225 #[test]
226 fn a_param_matches_one_segment_and_a_rest_matches_one_or_more() {
227 assert!(route_matches("/agentmux/work/:id", "/agentmux/work/12"));
228 assert!(!route_matches("/agentmux/work/:id", "/agentmux/work/"));
229 assert!(!route_matches("/agentmux/work/:id", "/agentmux/work/12/accept"));
230 assert!(route_matches("/docsite/*path", "/docsite/a/b/c.html"));
231 assert!(!route_matches("/docsite/*path", "/docsite/"));
232 assert!(route_matches("/", "/"));
233 assert!(!route_matches("/", "/x"));
234 }
235
236 #[test]
239 fn the_boundary_in_one_table() {
240 let mut table = String::new();
241 for (m, a) in EXCEPTIONS {
242 table.push_str(&format!("{:<12} {:?}\n", a.cap_name(), m));
243 }
244 assert!(table.contains("host_exec") && table.contains("host_write") && table.contains("host_read"), "{table}");
245 }
246}