Skip to main content

agentmux_srv_core/backend/rpc/
action_access.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Which websocket RPC commands a connection may call without being the AgentMux window.
5//!
6//! Holding the instance key opens a connection; it does not make the connection the window. A command is
7//! the window's alone unless it is listed here: a new command is refused to everything but the window until
8//! someone decides it is safe to open. `server::dispatch_guard` applies these lists at dispatch, and
9//! `scripts/gen-operator-commands.mjs` writes the frontend's list of commands that wait for the window's
10//! proof from them and from [`super::action_commands`].
11
12/// Calls any connection may make: they read what the window itself shows to anyone who can open it, they
13/// only subscribe this connection, or the handler decides per caller (`setconfig` per key, `notify.ack` by the
14/// launcher's proof). The window makes these before its own proof lands.
15pub const OPEN: &[&str] = &[
16    "app.update-check",
17    "app.update-status",
18    "eventreadhistory",
19    "eventsub",
20    "eventunsub",
21    "eventunsuball",
22    "getfullconfig",
23    "getwaveairatelimit",
24    "jobs.list",
25    "kanban.list",
26    "notify.ack",
27    "operator.challenge",
28    "presence.status",
29    "routeannounce",
30    "routeunannounce",
31    "setconfig",
32    "waveinfo",
33    "widgets.catalog",
34    "widgets.list",
35];
36
37/// The commands that carry an event name, which only the events in [`OPEN_EVENTS`] may be named in by a connection
38/// that is not the window: every event is delivered to whoever subscribed to it, and most carry another's words
39/// (a pane's output, a person's typing, an agent's turns).
40pub const EVENT_COMMANDS: &[&str] = &[
41    "eventreadhistory",
42    "eventsub",
43];
44
45/// The events any connection may subscribe to or read the history of: the launcher's notification events.
46pub const OPEN_EVENTS: &[&str] = &[
47    "notification",
48    "notification:retract",
49    "notification:state",
50];
51
52/// Calls a connection registered as an agent may make although they are the window's: the handler lets the
53/// agent act on its own pane only (`server::pane_caller`) and refuses the rest.
54pub const OWN_PANE: &[&str] = &[
55    "agentinput",
56    "agentstop",
57    "controllerresync",
58    "createsubblock",
59    "deletesubblock",
60    "setmeta",
61];
62
63/// Whether any connection may call `command` with this request: it is open, and when it names an event (`eventsub`,
64/// `eventreadhistory`) the event is one of [`OPEN_EVENTS`].
65pub fn is_open_for(command: &str, data: &serde_json::Value) -> bool {
66    if !OPEN.contains(&command) {
67        return false;
68    }
69    !EVENT_COMMANDS.contains(&command) || data.get("event").and_then(serde_json::Value::as_str).is_some_and(|e| OPEN_EVENTS.contains(&e))
70}
71
72/// Whether a connection registered as an agent reaches `command`'s handler although the command is not open: the
73/// handler checks the pane.
74pub fn is_own_pane(command: &str) -> bool {
75    OWN_PANE.contains(&command)
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use crate::backend::rpc::action_commands::{ALWAYS, LIVE_INPUT, READ_ONLY, UNLESS_OPERATOR};
82
83    #[test]
84    fn the_lists_name_commands_that_are_decided_and_do_not_overlap() {
85        let decided: std::collections::HashSet<&str> =
86            [ALWAYS, UNLESS_OPERATOR, LIVE_INPUT, READ_ONLY].iter().flat_map(|l| l.iter().copied()).collect();
87        for name in OPEN.iter().chain(OWN_PANE) {
88            // `operator.challenge` is in UNLESS_OPERATOR; every listed name must be a command that exists.
89            assert!(decided.contains(name), "{name} is in no list of action_commands.rs");
90        }
91        for name in OWN_PANE {
92            assert!(!OPEN.contains(name), "{name} is in OPEN and OWN_PANE");
93        }
94        for name in EVENT_COMMANDS {
95            assert!(OPEN.contains(name), "{name} names an event and is open only for OPEN_EVENTS");
96        }
97    }
98
99    /// One entry per line, in byte order: two changes to a list meet only when they add the same name.
100    #[test]
101    fn each_list_is_in_order() {
102        let lists: [(&str, &[&str]); 4] = [("OPEN", OPEN), ("EVENT_COMMANDS", EVENT_COMMANDS), ("OPEN_EVENTS", OPEN_EVENTS), ("OWN_PANE", OWN_PANE)];
103        for (list, names) in lists {
104            if let Some(pair) = names.windows(2).find(|pair| pair[0] >= pair[1]) {
105                panic!("{list} is not in order: {:?} comes before {:?}", pair[0], pair[1]);
106            }
107        }
108    }
109
110    #[test]
111    fn an_event_is_open_only_when_it_carries_nobody_elses_words() {
112        use serde_json::json;
113        for event in OPEN_EVENTS {
114            assert!(is_open_for("eventsub", &json!({"event": event, "allscopes": true})), "{event}");
115            assert!(is_open_for("eventreadhistory", &json!({"event": event, "scope": "", "maxitems": 5})), "{event}");
116        }
117        // Another pane's output, the person's typing, an agent's turns and the message flow.
118        for event in ["blockfile", "userinput", "agentturn", "message-flow", "controllerstatus", "a-new-event", ""] {
119            assert!(!is_open_for("eventsub", &json!({"event": event, "allscopes": true})), "{event}");
120            assert!(!is_open_for("eventreadhistory", &json!({"event": event, "scope": ""})), "{event}");
121        }
122        assert!(!is_open_for("eventsub", &json!({})) && !is_open_for("eventsub", &serde_json::Value::Null));
123        assert!(is_open_for("eventunsub", &json!("blockfile")), "taking a subscription away reveals nothing");
124        assert!(!is_open_for("agent.kill-tree", &json!({})));
125    }
126
127    #[test]
128    fn only_the_decided_reads_are_open() {
129        // What an unproven connection must not read or change: another agent's transcript, memory and
130        // history, the person's typed history, instructions, exports, and any object's meta.
131        for name in [
132            "agent.output", "agent:session:read", "agent:memory:read_file", "getmemory", "listrecentsessions", "session:export",
133            "agent.input-history", "getagentcontent", "bundle.export", "exportagents", "getmeta", "searchagenthistory",
134            // Reads of a path the caller names (also over a saved connection): the window's own.
135            "readeditorfile", "listeditordir", "fs.list", "fs.places", "fileprobe", "filesearchtext", "filesearchbytes",
136            "fileindexlines", "widgets.readfile",
137        ] {
138            assert!(!OPEN.contains(&name), "{name} must be the window's alone");
139        }
140    }
141}