Skip to main content

openbutler_protocol/
action.rs

1//! The action catalog: every name a client may place in `DaemonRequest.action`.
2//!
3//! The envelope carries the action as a string, so without this table the
4//! contract would declare no surface of its own: a client would have to read
5//! the host's source to learn what it can call, and the host would keep its
6//! own list somewhere else. Here the surface is named once, in the crate both
7//! sides depend on, and these questions are answered per entry:
8//!
9//! - [`Action::exposure`] — may a signed-in remote connection call it, or does
10//!   it require the host machine's own identity (the pipe or socket)?
11//! - [`Action::mutates`] — does it change stored state or the machine?
12//! - [`Action::view_only`] — may a read-only attachment (a subagent view) call
13//!   it?
14//! - [`Action::streams`] — does its answer open a live stream of frames, so a
15//!   transport that carries one request and one answer cannot serve it?
16//! - [`Action::commands`] — when present, which exposure and mutation rules
17//!   apply to each subcommand of this action?
18//!
19//! A name in this table is a promise: the host handles it. The host pins the
20//! other direction with a test of its own — every routed name is catalogued
21//! and every catalogued name is routed — so a name cannot be advertised
22//! without an implementation, and an implementation cannot exist off the
23//! books. Frame steps that precede the request (`login`, `auth`, `pair`) are
24//! not actions: they are admission, decided before any action is read.
25
26use serde_json::{Value, json};
27
28/// Who may call an action.
29#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30pub enum Exposure {
31    /// The host machine's own identity: the local pipe or Unix socket, where
32    /// the operating system already proved who is asking. A remote
33    /// connection never holds this, whatever its account.
34    Local,
35    /// Any connection that authenticated — a signed-in remote session as much
36    /// as the local client the owner started.
37    Remote,
38}
39
40impl Exposure {
41    /// The wire spelling, used by the API reference and by clients that
42    /// render the catalog.
43    pub fn as_str(self) -> &'static str {
44        match self {
45            Exposure::Local => "local",
46            Exposure::Remote => "remote",
47        }
48    }
49}
50
51/// The authority for one subcommand of an action.
52#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53pub struct CommandExposure {
54    /// The subcommand name as it travels in the action's payload.
55    pub name: &'static str,
56    /// Who may call this subcommand.
57    pub exposure: Exposure,
58    /// Whether this subcommand changes stored state or the machine.
59    pub mutates: bool,
60}
61
62impl CommandExposure {
63    /// Builds a const subcommand entry.
64    pub const fn new(name: &'static str, exposure: Exposure, mutates: bool) -> Self {
65        Self {
66            name,
67            exposure,
68            mutates,
69        }
70    }
71}
72
73/// One callable action.
74#[derive(Clone, Copy, Debug, PartialEq, Eq)]
75pub struct Action {
76    /// The name exactly as it travels in `DaemonRequest.action`.
77    pub name: &'static str,
78    /// Who may call it.
79    pub exposure: Exposure,
80    /// Whether it changes stored state or the machine.
81    pub mutates: bool,
82    /// Whether a read-only attachment may call it.
83    pub view_only: bool,
84    /// Whether its answer opens a live stream of frames. A transport that
85    /// carries one request and one answer (the browser bridge) cannot serve
86    /// such an action and says so instead of answering half of it.
87    pub streams: bool,
88    /// Per-subcommand authority, present only when it differs from the
89    /// action-level summary.
90    pub commands: Option<&'static [CommandExposure]>,
91}
92
93impl Action {
94    /// Builds a catalog entry. Const so the table below stays a plain list.
95    pub const fn new(
96        name: &'static str,
97        exposure: Exposure,
98        mutates: bool,
99        view_only: bool,
100    ) -> Self {
101        Self {
102            name,
103            exposure,
104            mutates,
105            view_only,
106            streams: false,
107            commands: None,
108        }
109    }
110
111    /// Builds an entry whose answer opens a live stream of frames.
112    pub const fn streaming(
113        name: &'static str,
114        exposure: Exposure,
115        mutates: bool,
116        view_only: bool,
117    ) -> Self {
118        Self {
119            name,
120            exposure,
121            mutates,
122            view_only,
123            streams: true,
124            commands: None,
125        }
126    }
127
128    /// Adds per-subcommand authority to an entry.
129    pub const fn with_commands(mut self, commands: &'static [CommandExposure]) -> Self {
130        self.commands = Some(commands);
131        self
132    }
133
134    /// The entry as a JSON object, for the generated API reference and for
135    /// clients that show the surface without linking this crate.
136    pub fn to_json(&self) -> Value {
137        let mut value = json!({
138            "name": self.name,
139            "exposure": self.exposure.as_str(),
140            "mutates": self.mutates,
141            "viewOnly": self.view_only,
142            "streams": self.streams,
143        });
144        if let Some(commands) = self.commands {
145            value["commands"] = Value::Object(
146                commands
147                    .iter()
148                    .map(|command| {
149                        (
150                            command.name.to_owned(),
151                            json!({
152                                "exposure": command.exposure.as_str(),
153                                "mutates": command.mutates,
154                            }),
155                        )
156                    })
157                    .collect(),
158            );
159        }
160        value
161    }
162}
163
164use Exposure::{Local, Remote};
165
166const MCP_COMMANDS: &[CommandExposure] = &[
167    CommandExposure::new("list", Remote, false),
168    CommandExposure::new("add", Local, true),
169    CommandExposure::new("remove", Local, true),
170];
171
172const SCHEDULE_COMMANDS: &[CommandExposure] = &[
173    CommandExposure::new("capabilities", Remote, false),
174    CommandExposure::new("list", Remote, false),
175    CommandExposure::new("log", Remote, false),
176    CommandExposure::new("run", Remote, true),
177    CommandExposure::new("add", Remote, true),
178    CommandExposure::new("update", Remote, true),
179    CommandExposure::new("remove", Remote, true),
180    CommandExposure::new("enable", Remote, true),
181    CommandExposure::new("disable", Remote, true),
182];
183
184/// Every action the host accepts, in domain order.
185pub const ACTIONS: &[Action] = &[
186    // Host state.
187    Action::new("status", Remote, false, false),
188    Action::new("stop", Local, true, false),
189    Action::new("about.report", Remote, false, false),
190    Action::new("config.report", Remote, false, true),
191    Action::new("settings.reload", Remote, true, false),
192    Action::new("settings.setup", Remote, true, false),
193    Action::new("settings.setup.review", Remote, false, false),
194    Action::new("settings.set-listen", Local, true, false),
195    // Accounts, keys and permissions.
196    Action::new("admin.create", Local, true, false),
197    Action::new("admin.backup", Local, true, false),
198    Action::new("keys.list", Remote, false, false),
199    Action::new("keys.add", Remote, true, false),
200    Action::new("keys.remove", Remote, true, false),
201    Action::new("permissions.set-mode", Remote, true, false),
202    Action::new("permissions.manage", Remote, true, false),
203    // The model catalog.
204    Action::new("catalog.listProviders", Remote, false, true),
205    Action::new("catalog.listModels", Remote, false, true),
206    Action::new("catalog.listToolModels", Remote, false, true),
207    // Chats.
208    Action::streaming("attach", Remote, false, false),
209    Action::new("session.list", Remote, false, true),
210    Action::new("session.search", Remote, false, true),
211    Action::new("session.load", Remote, false, false),
212    Action::new("session.entries-range", Remote, false, true),
213    Action::new("session.find-attachment", Remote, false, true),
214    Action::new("session.new", Remote, true, false),
215    Action::new("session.select-project", Remote, true, false),
216    Action::new("session.select-model", Remote, true, true),
217    Action::new("session.flush", Remote, true, true),
218    Action::new("session.add-entry", Remote, true, true),
219    Action::new("session.send", Remote, true, false),
220    Action::new("session.approve", Remote, true, true),
221    Action::new("session.stop", Remote, true, true),
222    Action::new("session.compact", Remote, true, false),
223    Action::new("session.queue", Remote, true, true),
224    Action::new("session.answer", Remote, true, true),
225    Action::new("session.command", Remote, true, false),
226    // Per-session runtime choices.
227    Action::new("runtime.save-reasoning", Remote, true, false),
228    Action::new("runtime.save-show-reasoning", Remote, true, false),
229    Action::new("runtime.save-approval-mode", Remote, true, false),
230    // Review and the ledgers.
231    Action::new("auto_review.approve", Remote, true, false),
232    Action::new("ledger.events", Remote, false, false),
233    Action::new("ledger.actions", Remote, false, false),
234    Action::new("ledger.prune", Remote, true, false),
235    Action::new("events.retry", Remote, true, false),
236    Action::new("usage.report", Remote, false, false),
237    // Skills, MCP servers and the tool surface.
238    Action::new("skills.list", Remote, false, false),
239    Action::new("mcp.manage", Remote, true, false).with_commands(MCP_COMMANDS),
240    // Schedules and team.
241    Action::new("schedule.manage", Remote, true, false).with_commands(SCHEDULE_COMMANDS),
242    // Telegram (deferred; the host answers with a refusal while the channel
243    // is out of scope).
244    Action::new("telegram.status", Remote, false, false),
245    Action::new("telegram.pair", Remote, true, false),
246    Action::new("telegram.test", Remote, true, false),
247    // The host's own files and process.
248    Action::new("logs.tail", Local, false, false),
249    // The log system's query surface: the daemon serves its own log over
250    // any authenticated connection (an admin reads logs from anywhere),
251    // and the runtime level is a state change the daemon owns.
252    Action::new("logs.query", Remote, false, true),
253    Action::new("logs.set-level", Remote, true, false),
254    // The audit chain's read surface: append-only evidence, served
255    // record by record over an authenticated connection.
256    Action::new("audit.query", Remote, false, true),
257    Action::new("sessions.prune", Local, true, false),
258    // Login entry: the one machine change that outlives the process. The
259    // host writes it so the guard that refuses a foreign value, the unit
260    // text and the Run command line all live beside the host.
261    Action::new("daemon.install", Local, true, false),
262    Action::new("daemon.uninstall", Local, true, false),
263];
264
265/// The catalog entry for `name`, or `None` when the name is not an action.
266pub fn find(name: &str) -> Option<&'static Action> {
267    ACTIONS.iter().find(|action| action.name == name)
268}
269
270/// Whether `name` is a catalogued action.
271pub fn is_known(name: &str) -> bool {
272    find(name).is_some()
273}