agentmux_srv/server/route_table.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Every route srv's main router serves, declared once, each with what is decided about it.
5//!
6//! A group of routes is one `declared_routes!` block, in the file of the domain it serves. The
7//! block makes the function that builds the group's router and a table of the same routes, so the
8//! router and the table cannot disagree. What is decided about a route is written on its line:
9//! today how a call that can change state is recorded ([`Policy`], read by `super::http_action`).
10//! A write with no policy does not compile, so a route cannot be added without deciding it, and
11//! `tests/http_action_policy.snapshot.txt` pins every decision.
12//!
13//! Who a route is for follows from its being here: a declared route is for every agent, native or
14//! container, unless `backend::route_audience` names it among its exceptions (a route that needs
15//! the host, or is the app's own). `tests/route_audience.snapshot.txt` pins who each route is for,
16//! so a new route shows there as `everyone` unless an exception is added with it.
17
18/// How a call to a route that can change state is recorded (`super::http_action`). A call the
19/// record meets with no decision is recorded.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub(super) enum Policy {
22 /// Recorded whoever calls: an agent by its token, anything else as `unproven`. For a route
23 /// that changes state, runs or stops a process, writes files, sends to an agent or changes
24 /// who may do what.
25 Always,
26 /// The window's and the host's own calls, which cannot be told from a process that holds the
27 /// key: recorded when an agent's token is on the request.
28 Plumbing,
29 /// The route records its successes itself, with more than the layer knows; the layer records
30 /// its refusals.
31 Handler,
32 /// Reads data or carries a stream, and changes nothing that is kept.
33 Never,
34}
35
36/// One declared route: its method, its path as the router takes it (`:param` and `*rest` kept),
37/// and how it is recorded when it can change state (`None` for a read, and for `ANY`).
38pub(super) struct Declared {
39 pub method: &'static str,
40 pub path: &'static str,
41 pub policy: Option<Policy>,
42}
43
44/// Every group declared with `declared_routes!`.
45pub(super) const GROUPS: &[&[Declared]] = &[
46 super::vault_routes::VAULT_ROUTES,
47 super::routes::REACTIVE_ROUTES,
48 super::routes::BUS_ROUTES,
49 super::routes::AGENT_API_ROUTES,
50 super::routes::MEMORY_AND_IDENTITY_ROUTES,
51 super::routes::EXTERNAL_HOST_ROUTES,
52 super::routes::BROWSER_CONTROL_ROUTES,
53 super::routes::AGENT_DEFINITION_ROUTES,
54 super::routes::GOOGLE_AGENT_ROUTES,
55 super::routes::OS_LINK_ROUTES,
56 super::routes::WIDGET_FILES_ROUTES,
57 super::routes::MUXSPECT_ROUTES,
58 super::routes::FORWARDABLE_MUXSPECT_ROUTES,
59 super::routes::UI_AND_HOST_ROUTES,
60 super::routes::FORWARD_MESSAGING_AND_WORK_ROUTES,
61 super::routes::AGENT_SELF_AND_FLEET_ROUTES,
62 super::routes::SERVICE_SHELL_AND_VAULT_ROUTES,
63 super::routes::AGENT_ROUTES,
64 super::routes::LAN_FORWARD_ROUTES,
65 super::routes::WHATSAPP_WEBHOOKS,
66 super::routes::STREAMED_BROWSER_ROUTES,
67 super::routes::ROOT_HEALTH_ROUTE,
68 super::routes::HEALTH_ROUTE,
69 super::routes::MODEL_GATEWAY_ROUTE,
70 super::routes::LOCAL_HOOK_ROUTE,
71];
72
73/// Every route of the main router, group after group.
74pub(super) fn declared() -> impl Iterator<Item = &'static Declared> {
75 GROUPS.iter().flat_map(|group| group.iter())
76}
77
78/// Whether the main router serves a route at `path`, a request's raw path: what makes a path
79/// everyone's unless `backend::route_audience` says otherwise.
80pub(super) fn serves(path: &str) -> bool {
81 use crate::backend::route_audience::Match;
82 declared().any(|route| Match::Route(route.path).matches(path))
83}
84
85/// How `route` (`"METHOD /template"`) is recorded; `None` for a route that is not declared, or is
86/// a read. A call the record meets with no decision is recorded.
87pub(super) fn policy(route: &str) -> Option<Policy> {
88 let (method, path) = route.split_once(' ')?;
89 declared().find(|d| d.method == method && d.path == path).and_then(|d| d.policy)
90}
91
92/// Declares a group of routes: the function that builds their router, and their table.
93///
94/// ```ignore
95/// declared_routes! {
96/// /// What the group is.
97/// pub(super) fn vault_routes, VAULT_ROUTES;
98/// GET "/api/v1/vault" => handlers::list;
99/// POST "/api/v1/vault/show", Never => handlers::show;
100/// }
101/// ```
102///
103/// A read (`GET`) takes no policy; a write (`POST`, `PUT`, `PATCH`, `DELETE`) takes one of
104/// `Always`, `Plumbing`, `Handler` and `Never`, and does not compile without it. `ANY` is for a
105/// route that passes every method on to something else (the model gateway); it takes no policy,
106/// because the record asks by the method of the call and so never finds an `ANY` line. Two lines may
107/// name the same path with different methods. The path is a literal or a constant. A line may end
108/// with `, layer(..)` or `, route_layer(..)` for a layer on that one route's handler. What a group
109/// shares (a layer over all of it, another group merged in) is written in a function that calls
110/// the declared one.
111macro_rules! declared_routes {
112 ($(#[$attr:meta])* $vis:vis fn $name:ident, $table:ident; $($method:ident $path:expr $(, $policy:ident)? => $handler:expr $(, layer($layer:expr))? $(, route_layer($route_layer:expr))?;)+) => {
113 $(#[$attr])*
114 $vis fn $name() -> axum::Router<$crate::server::AppState> {
115 axum::Router::new()
116 $(.route($path, $crate::server::route_table::declared_routes!(@on $method)($handler)$(.layer($layer))?$(.route_layer($route_layer))?))+
117 }
118
119 /// The routes of the function above, as declared.
120 pub(in $crate::server) const $table: &[$crate::server::route_table::Declared] = &[
121 $($crate::server::route_table::Declared {
122 method: stringify!($method),
123 path: $path,
124 policy: $crate::server::route_table::declared_routes!(@policy $method $($policy)?),
125 },)+
126 ];
127 };
128 (@on GET) => { axum::routing::get };
129 (@on POST) => { axum::routing::post };
130 (@on PUT) => { axum::routing::put };
131 (@on PATCH) => { axum::routing::patch };
132 (@on DELETE) => { axum::routing::delete };
133 (@on ANY) => { axum::routing::any };
134 (@policy GET) => { None };
135 (@policy ANY) => { None };
136 (@policy POST $policy:ident) => { Some($crate::server::route_table::Policy::$policy) };
137 (@policy PUT $policy:ident) => { Some($crate::server::route_table::Policy::$policy) };
138 (@policy PATCH $policy:ident) => { Some($crate::server::route_table::Policy::$policy) };
139 (@policy DELETE $policy:ident) => { Some($crate::server::route_table::Policy::$policy) };
140}
141pub(crate) use declared_routes;