Skip to main content

agentmux_srv_core/backend/
audience.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Who a thing is for: one answer to "does this reach a native agent, a container agent, or only
5//! the app?", used for srv's routes (`route_audience`), catalog MCP servers
6//! (`storage::mcp_servers`) and Operator Config entries (`operator_config_seed`).
7//!
8//! The operator's rule (2026-10-28): a container agent can do everything a native agent can, except
9//! write to, run on or read from the host directly. So [`Audience::Everyone`] is the normal answer.
10//! Anything narrower names the host capability that is the reason and says what the caller should do
11//! instead, so a container agent is never refused without a reason and a next step.
12
13/// A thing only a process on the host can do.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum HostCap {
16    /// Run a command or launch a program on the host.
17    Exec,
18    /// Change files or the app on the host.
19    Write,
20    /// Read files on the host outside the container's own mounts.
21    Read,
22}
23
24impl HostCap {
25    pub fn name(self) -> &'static str {
26        match self {
27            HostCap::Exec => "host_exec",
28            HostCap::Write => "host_write",
29            HostCap::Read => "host_read",
30        }
31    }
32}
33
34/// What a runtime may do on the host. A native agent has all three; a container agent has none.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct RuntimeCaps {
37    pub host_exec: bool,
38    pub host_write: bool,
39    pub host_read: bool,
40}
41
42impl RuntimeCaps {
43    pub const NATIVE: RuntimeCaps = RuntimeCaps { host_exec: true, host_write: true, host_read: true };
44    pub const CONTAINER: RuntimeCaps = RuntimeCaps { host_exec: false, host_write: false, host_read: false };
45
46    pub fn of(container: bool) -> RuntimeCaps {
47        if container { Self::CONTAINER } else { Self::NATIVE }
48    }
49
50    pub fn has(self, cap: HostCap) -> bool {
51        match cap {
52            HostCap::Exec => self.host_exec,
53            HostCap::Write => self.host_write,
54            HostCap::Read => self.host_read,
55        }
56    }
57}
58
59/// Who gets a thing. The `&'static str` of the narrower kinds is what the refused caller is told:
60/// the reason, and what to do instead.
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub enum Audience {
63    /// Every agent, native or container. The normal answer.
64    Everyone,
65    /// Only a runtime that has `cap` on the host.
66    NativeOnly(HostCap, &'static str),
67    /// The AgentMux app's and srv's own plumbing: not a container agent's to call. (Native agents
68    /// are not held back by this tag; the app's own calls carry the host key.)
69    AppOnly(&'static str),
70    /// Only container agents (nothing uses this yet; the Operator Config manifest can say it).
71    ContainerOnly,
72}
73
74impl Audience {
75    /// Whether an agent whose runtime has `caps` gets the thing.
76    pub fn permits(&self, caps: RuntimeCaps) -> bool {
77        match *self {
78            Audience::Everyone => true,
79            Audience::NativeOnly(cap, _) => caps.has(cap),
80            Audience::AppOnly(_) => caps == RuntimeCaps::NATIVE,
81            Audience::ContainerOnly => caps == RuntimeCaps::CONTAINER,
82        }
83    }
84
85    /// What a refused caller is told, `None` when `caps` is let in.
86    pub fn refusal(&self, caps: RuntimeCaps) -> Option<&'static str> {
87        if self.permits(caps) {
88            return None;
89        }
90        match *self {
91            Audience::NativeOnly(_, why) | Audience::AppOnly(why) => Some(why),
92            Audience::ContainerOnly => Some("this is for container agents."),
93            Audience::Everyone => None,
94        }
95    }
96
97    /// `host_exec`, `host_write` or `host_read` for a [`NativeOnly`](Audience::NativeOnly), else `-`:
98    /// the column a reviewer reads to see the boundary.
99    pub fn cap_name(&self) -> &'static str {
100        match *self {
101            Audience::NativeOnly(cap, _) => cap.name(),
102            _ => "-",
103        }
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn everyone_reaches_both_runtimes() {
113        assert!(Audience::Everyone.permits(RuntimeCaps::NATIVE));
114        assert!(Audience::Everyone.permits(RuntimeCaps::CONTAINER));
115        assert_eq!(Audience::Everyone.refusal(RuntimeCaps::CONTAINER), None);
116    }
117
118    #[test]
119    fn a_native_only_thing_needs_its_capability_and_a_container_has_none() {
120        for cap in [HostCap::Exec, HostCap::Write, HostCap::Read] {
121            let a = Audience::NativeOnly(cap, "why");
122            assert!(a.permits(RuntimeCaps::NATIVE));
123            assert!(!a.permits(RuntimeCaps::CONTAINER));
124            assert_eq!(a.refusal(RuntimeCaps::CONTAINER), Some("why"));
125        }
126        // A runtime with only some capabilities (a later WSL or remote one) gets what it can use.
127        let read_only = RuntimeCaps { host_exec: false, host_write: false, host_read: true };
128        assert!(Audience::NativeOnly(HostCap::Read, "r").permits(read_only));
129        assert!(!Audience::NativeOnly(HostCap::Exec, "e").permits(read_only));
130    }
131
132    #[test]
133    fn app_only_and_container_only_split_the_two_runtimes() {
134        assert!(!Audience::AppOnly("app").permits(RuntimeCaps::CONTAINER));
135        assert!(Audience::AppOnly("app").permits(RuntimeCaps::NATIVE));
136        assert!(Audience::ContainerOnly.permits(RuntimeCaps::CONTAINER));
137        assert!(!Audience::ContainerOnly.permits(RuntimeCaps::NATIVE));
138    }
139}