Skip to main content

agentmux_srv/server/agent_handlers/
composer_origin.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Who typed into an agent's composer (`agentinput`), decided from the
5//! connection, never from the request
6//! (SPEC_MESSAGE_ORIGIN_AND_ONE_CONTAINER_2026_10_25.md ยง3.4).
7//!
8//! `TurnOrigin::User` is the human, the one thing nothing else may pass
9//! itself off as, so the `agentinput` route takes it from the caller:
10//!
11//! - a connection registered as an agent is refused: agents send with the
12//!   `Message` tool, which is delivered as a jekt;
13//! - the AgentMux window, proven by the CEF host (`RpcContext::operator_only`),
14//!   is the human (`System` for a hidden memory reinjection, or a message the pane sends on
15//!   AgentMux's behalf, such as its start-up document: the request says so with `system`);
16//! - any other connection is refused when srv runs with a CEF host (every
17//!   desktop build); a headless srv (the cloud web host) has no CEF host and logs
18//!   the call. The mode comes from how srv was started, never from anything
19//!   the connection says.
20
21use crate::backend::blockcontroller::health::TurnOrigin;
22use crate::backend::rpc_types::RpcContext;
23
24const WHY: &str = "only the AgentMux window types into an agent's composer; an agent sends a message with the Message tool";
25
26/// The origin of an `agentinput` turn from the caller, or why it is refused. `lowered` (a hidden
27/// reinjection, a message the pane sends on AgentMux's behalf) can only turn the window's `User` into `System`.
28pub(super) fn composer_origin(ctx: &RpcContext, lowered: bool) -> Result<TurnOrigin, String> {
29    composer_origin_in(ctx, lowered, crate::headless::active())
30}
31
32/// Whether the caller may put text into an agent's input as the person does (`agent.send`): the same decision as the
33/// composer's, for a call that has no turn origin of its own. Refused for a registered agent in every mode, and for any
34/// connection the CEF host did not prove when srv has a host.
35pub(crate) fn window_only(ctx: &RpcContext) -> Result<(), String> {
36    composer_origin(ctx, false).map(|_| ())
37}
38
39fn composer_origin_in(ctx: &RpcContext, lowered: bool, headless: bool) -> Result<TurnOrigin, String> {
40    // A registered agent is refused in every mode, whatever else it claims.
41    if !ctx.agent_id.is_empty() {
42        return Err(format!("FORBIDDEN: {WHY}"));
43    }
44    if let Err(refusal) = ctx.operator_only(WHY) {
45        if !headless {
46            return Err(refusal);
47        }
48        // A headless srv has no CEF host: the cloud web host's own turns.
49        tracing::warn!(
50            client_type = %ctx.client_type,
51            "agentinput from a connection no host proved, in a headless srv"
52        );
53    }
54    Ok(if lowered { TurnOrigin::System } else { TurnOrigin::User })
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    fn agent(proven: bool) -> RpcContext {
62        RpcContext { agent_id: "AgentX".into(), operator_proven: proven, ..Default::default() }
63    }
64
65    #[test]
66    fn the_proven_window_is_the_user_or_the_system_for_a_hidden_reinjection() {
67        let window = RpcContext::operator_for_tests();
68        assert_eq!(composer_origin_in(&window, false, false), Ok(TurnOrigin::User));
69        assert_eq!(composer_origin_in(&window, true, false), Ok(TurnOrigin::System));
70    }
71
72    #[test]
73    fn an_agent_connection_is_never_the_user_in_any_mode() {
74        for headless in [false, true] {
75            for proven in [false, true] {
76                for hidden in [false, true] {
77                    let err = composer_origin_in(&agent(proven), hidden, headless).unwrap_err();
78                    assert!(err.starts_with("FORBIDDEN: only the AgentMux window"), "{err}");
79                }
80            }
81        }
82    }
83
84    #[test]
85    fn an_unproven_connection_is_refused_when_srv_has_a_host() {
86        let err = composer_origin_in(&RpcContext::default(), false, false).unwrap_err();
87        assert!(err.contains("isn't the AgentMux window"), "{err}");
88    }
89
90    #[test]
91    fn a_headless_srv_keeps_the_old_behaviour_for_an_unproven_connection() {
92        let ctx = RpcContext::default();
93        assert_eq!(composer_origin_in(&ctx, false, true), Ok(TurnOrigin::User));
94        assert_eq!(composer_origin_in(&ctx, true, true), Ok(TurnOrigin::System));
95    }
96
97    #[test]
98    fn nothing_the_request_carries_can_raise_the_origin() {
99        // `hidden` only ever lowers User to System; it never turns a refusal into an origin.
100        assert!(composer_origin_in(&RpcContext::default(), true, false).is_err());
101        assert!(composer_origin_in(&agent(false), true, true).is_err());
102    }
103}