Skip to main content

agentmux_srv_core/backend/rpc/
actor.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Who made a websocket RPC call, for the record of what was done.
5//!
6//! The answer comes from the connection, never from the request: an agent is
7//! the one the connection registered as (`bus:register`, token-bound), the
8//! operator is a connection the CEF host proved is the AgentMux window
9//! (`server::operator_proof`), and a connection that is neither is
10//! [`Actor::Unproven`], not the operator. Nothing in a request's data names the
11//! actor.
12
13use crate::backend::rpc_types::RpcContext;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum Actor {
17    /// A connection registered as this agent.
18    Agent(String),
19    /// A connection the host proved is the AgentMux window.
20    Operator,
21    /// A connection a registered external host vouched for as this person's window (a headless server
22    /// reached through that host): the person's id, so the record names who acted.
23    Person(String),
24    /// Neither: a socket that holds the instance key and has proved nothing more.
25    Unproven,
26    /// AgentMux itself, doing its own housekeeping (no caller asked for it).
27    Srv,
28}
29
30impl Actor {
31    /// The person's window, however it was proven: the operator or a person an external host vouched for.
32    pub fn is_operator(&self) -> bool {
33        matches!(self, Actor::Operator | Actor::Person(_))
34    }
35
36    /// How the record names the actor. An agent is `agent:<id>`, so no agent id can
37    /// read as `operator` or `unproven`.
38    pub fn label(&self) -> String {
39        match self {
40            Actor::Agent(id) => format!("agent:{id}"),
41            Actor::Operator => "operator".to_string(),
42            Actor::Person(id) => format!("person:{id} (AgentMux Web)"),
43            Actor::Unproven => "unproven".to_string(),
44            Actor::Srv => "agentmux".to_string(),
45        }
46    }
47}
48
49/// The actor of a call made on a connection with `ctx`. A connection registered as an
50/// agent is that agent whatever else it carries; the operator only with the host's proof.
51pub fn actor_of(ctx: &RpcContext) -> Actor {
52    if !ctx.agent_id.is_empty() {
53        Actor::Agent(ctx.agent_id.clone())
54    } else if ctx.operator_proven_now() {
55        match &ctx.operator_person {
56            Some(person) => Actor::Person(person.clone()),
57            None => Actor::Operator,
58        }
59    } else {
60        Actor::Unproven
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn an_agents_connection_is_that_agent() {
70        let ctx = RpcContext { agent_id: "opaz".into(), ..Default::default() };
71        assert_eq!(actor_of(&ctx), Actor::Agent("opaz".into()));
72        assert_eq!(actor_of(&ctx).label(), "agent:opaz");
73    }
74
75    #[test]
76    fn the_operator_needs_the_hosts_proof() {
77        assert_eq!(actor_of(&RpcContext::operator_for_tests()), Actor::Operator);
78        // Holding the instance key, or being any other connection, is not the operator.
79        assert_eq!(actor_of(&RpcContext::default()), Actor::Unproven);
80        assert_eq!(actor_of(&RpcContext { blockid: "b".into(), tabid: "t".into(), conn: "c".into(), ..Default::default() }), Actor::Unproven);
81    }
82
83    #[test]
84    fn an_agent_is_never_the_operator_even_with_the_proof_flag_set() {
85        let ctx = RpcContext { agent_id: "opaz".into(), operator_proven: true, ..Default::default() };
86        assert_eq!(actor_of(&ctx), Actor::Agent("opaz".into()));
87    }
88
89    #[test]
90    fn no_agent_id_can_read_as_the_operator_or_unproven() {
91        assert_eq!(Actor::Agent("operator".into()).label(), "agent:operator");
92        assert_eq!(Actor::Agent("unproven".into()).label(), "agent:unproven");
93        assert_ne!(Actor::Agent("operator".into()).label(), Actor::Operator.label());
94    }
95}