openbutler-protocol 0.0.2

The wire contract between the OpenButler daemon and its clients: frame encoding, action envelopes, and the shared state types.
Documentation
//! The action catalog: every name a client may place in `DaemonRequest.action`.
//!
//! The envelope carries the action as a string, so without this table the
//! contract would declare no surface of its own: a client would have to read
//! the host's source to learn what it can call, and the host would keep its
//! own list somewhere else. Here the surface is named once, in the crate both
//! sides depend on, and these questions are answered per entry:
//!
//! - [`Action::exposure`] — may a signed-in remote connection call it, or does
//!   it require the host machine's own identity (the pipe or socket)?
//! - [`Action::mutates`] — does it change stored state or the machine?
//! - [`Action::view_only`] — may a read-only attachment (a subagent view) call
//!   it?
//! - [`Action::streams`] — does its answer open a live stream of frames, so a
//!   transport that carries one request and one answer cannot serve it?
//! - [`Action::commands`] — when present, which exposure and mutation rules
//!   apply to each subcommand of this action?
//!
//! A name in this table is a promise: the host handles it. The host pins the
//! other direction with a test of its own — every routed name is catalogued
//! and every catalogued name is routed — so a name cannot be advertised
//! without an implementation, and an implementation cannot exist off the
//! books. Frame steps that precede the request (`login`, `auth`, `pair`) are
//! not actions: they are admission, decided before any action is read.

use serde_json::{Value, json};

/// Who may call an action.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Exposure {
    /// The host machine's own identity: the local pipe or Unix socket, where
    /// the operating system already proved who is asking. A remote
    /// connection never holds this, whatever its account.
    Local,
    /// Any connection that authenticated — a signed-in remote session as much
    /// as the local client the owner started.
    Remote,
}

impl Exposure {
    /// The wire spelling, used by the API reference and by clients that
    /// render the catalog.
    pub fn as_str(self) -> &'static str {
        match self {
            Exposure::Local => "local",
            Exposure::Remote => "remote",
        }
    }
}

/// The authority for one subcommand of an action.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CommandExposure {
    /// The subcommand name as it travels in the action's payload.
    pub name: &'static str,
    /// Who may call this subcommand.
    pub exposure: Exposure,
    /// Whether this subcommand changes stored state or the machine.
    pub mutates: bool,
}

impl CommandExposure {
    /// Builds a const subcommand entry.
    pub const fn new(name: &'static str, exposure: Exposure, mutates: bool) -> Self {
        Self {
            name,
            exposure,
            mutates,
        }
    }
}

/// One callable action.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Action {
    /// The name exactly as it travels in `DaemonRequest.action`.
    pub name: &'static str,
    /// Who may call it.
    pub exposure: Exposure,
    /// Whether it changes stored state or the machine.
    pub mutates: bool,
    /// Whether a read-only attachment may call it.
    pub view_only: bool,
    /// Whether its answer opens a live stream of frames. A transport that
    /// carries one request and one answer (the browser bridge) cannot serve
    /// such an action and says so instead of answering half of it.
    pub streams: bool,
    /// Per-subcommand authority, present only when it differs from the
    /// action-level summary.
    pub commands: Option<&'static [CommandExposure]>,
}

impl Action {
    /// Builds a catalog entry. Const so the table below stays a plain list.
    pub const fn new(
        name: &'static str,
        exposure: Exposure,
        mutates: bool,
        view_only: bool,
    ) -> Self {
        Self {
            name,
            exposure,
            mutates,
            view_only,
            streams: false,
            commands: None,
        }
    }

    /// Builds an entry whose answer opens a live stream of frames.
    pub const fn streaming(
        name: &'static str,
        exposure: Exposure,
        mutates: bool,
        view_only: bool,
    ) -> Self {
        Self {
            name,
            exposure,
            mutates,
            view_only,
            streams: true,
            commands: None,
        }
    }

    /// Adds per-subcommand authority to an entry.
    pub const fn with_commands(mut self, commands: &'static [CommandExposure]) -> Self {
        self.commands = Some(commands);
        self
    }

    /// The entry as a JSON object, for the generated API reference and for
    /// clients that show the surface without linking this crate.
    pub fn to_json(&self) -> Value {
        let mut value = json!({
            "name": self.name,
            "exposure": self.exposure.as_str(),
            "mutates": self.mutates,
            "viewOnly": self.view_only,
            "streams": self.streams,
        });
        if let Some(commands) = self.commands {
            value["commands"] = Value::Object(
                commands
                    .iter()
                    .map(|command| {
                        (
                            command.name.to_owned(),
                            json!({
                                "exposure": command.exposure.as_str(),
                                "mutates": command.mutates,
                            }),
                        )
                    })
                    .collect(),
            );
        }
        value
    }
}

use Exposure::{Local, Remote};

const MCP_COMMANDS: &[CommandExposure] = &[
    CommandExposure::new("list", Remote, false),
    CommandExposure::new("add", Local, true),
    CommandExposure::new("remove", Local, true),
];

const SCHEDULE_COMMANDS: &[CommandExposure] = &[
    CommandExposure::new("capabilities", Remote, false),
    CommandExposure::new("list", Remote, false),
    CommandExposure::new("log", Remote, false),
    CommandExposure::new("run", Remote, true),
    CommandExposure::new("add", Remote, true),
    CommandExposure::new("update", Remote, true),
    CommandExposure::new("remove", Remote, true),
    CommandExposure::new("enable", Remote, true),
    CommandExposure::new("disable", Remote, true),
];

/// Every action the host accepts, in domain order.
pub const ACTIONS: &[Action] = &[
    // Host state.
    Action::new("status", Remote, false, false),
    Action::new("stop", Local, true, false),
    Action::new("about.report", Remote, false, false),
    Action::new("config.report", Remote, false, true),
    Action::new("settings.reload", Remote, true, false),
    Action::new("settings.setup", Remote, true, false),
    Action::new("settings.setup.review", Remote, false, false),
    Action::new("settings.set-listen", Local, true, false),
    // Accounts, keys and permissions.
    Action::new("admin.create", Local, true, false),
    Action::new("admin.backup", Local, true, false),
    Action::new("keys.list", Remote, false, false),
    Action::new("keys.add", Remote, true, false),
    Action::new("keys.remove", Remote, true, false),
    Action::new("permissions.set-mode", Remote, true, false),
    Action::new("permissions.manage", Remote, true, false),
    // The model catalog.
    Action::new("catalog.listProviders", Remote, false, true),
    Action::new("catalog.listModels", Remote, false, true),
    Action::new("catalog.listToolModels", Remote, false, true),
    // Chats.
    Action::streaming("attach", Remote, false, false),
    Action::new("session.list", Remote, false, true),
    Action::new("session.search", Remote, false, true),
    Action::new("session.load", Remote, false, false),
    Action::new("session.entries-range", Remote, false, true),
    Action::new("session.find-attachment", Remote, false, true),
    Action::new("session.new", Remote, true, false),
    Action::new("session.select-project", Remote, true, false),
    Action::new("session.select-model", Remote, true, true),
    Action::new("session.flush", Remote, true, true),
    Action::new("session.add-entry", Remote, true, true),
    Action::new("session.send", Remote, true, false),
    Action::new("session.approve", Remote, true, true),
    Action::new("session.stop", Remote, true, true),
    Action::new("session.compact", Remote, true, false),
    Action::new("session.queue", Remote, true, true),
    Action::new("session.answer", Remote, true, true),
    Action::new("session.command", Remote, true, false),
    // Per-session runtime choices.
    Action::new("runtime.save-reasoning", Remote, true, false),
    Action::new("runtime.save-show-reasoning", Remote, true, false),
    Action::new("runtime.save-approval-mode", Remote, true, false),
    // Review and the ledgers.
    Action::new("auto_review.approve", Remote, true, false),
    Action::new("ledger.events", Remote, false, false),
    Action::new("ledger.actions", Remote, false, false),
    Action::new("ledger.prune", Remote, true, false),
    Action::new("events.retry", Remote, true, false),
    Action::new("usage.report", Remote, false, false),
    // Skills, MCP servers and the tool surface.
    Action::new("skills.list", Remote, false, false),
    Action::new("mcp.manage", Remote, true, false).with_commands(MCP_COMMANDS),
    // Schedules and team.
    Action::new("schedule.manage", Remote, true, false).with_commands(SCHEDULE_COMMANDS),
    // Telegram (deferred; the host answers with a refusal while the channel
    // is out of scope).
    Action::new("telegram.status", Remote, false, false),
    Action::new("telegram.pair", Remote, true, false),
    Action::new("telegram.test", Remote, true, false),
    // The host's own files and process.
    Action::new("logs.tail", Local, false, false),
    // The log system's query surface: the daemon serves its own log over
    // any authenticated connection (an admin reads logs from anywhere),
    // and the runtime level is a state change the daemon owns.
    Action::new("logs.query", Remote, false, true),
    Action::new("logs.set-level", Remote, true, false),
    // The audit chain's read surface: append-only evidence, served
    // record by record over an authenticated connection.
    Action::new("audit.query", Remote, false, true),
    Action::new("sessions.prune", Local, true, false),
    // Login entry: the one machine change that outlives the process. The
    // host writes it so the guard that refuses a foreign value, the unit
    // text and the Run command line all live beside the host.
    Action::new("daemon.install", Local, true, false),
    Action::new("daemon.uninstall", Local, true, false),
];

/// The catalog entry for `name`, or `None` when the name is not an action.
pub fn find(name: &str) -> Option<&'static Action> {
    ACTIONS.iter().find(|action| action.name == name)
}

/// Whether `name` is a catalogued action.
pub fn is_known(name: &str) -> bool {
    find(name).is_some()
}