Skip to main content

agentmux_srv_core/backend/
settings_consent.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The settings that waive or widen what a person has agreed to: whether agents may start agents, which programs may
5//! run, who may be reached, and what is shared with other devices. An agent may change any other setting, recorded
6//! as that agent; these it never changes, because a change would be counted as the person's own choice. Only the
7//! AgentMux window, proven through its host, passes (`RpcContext::operator_only`).
8
9use serde_json::{Map, Value};
10
11use super::rpc_types::RpcContext;
12use super::supervisor::settings::KEYS as SUPERVISOR_KEYS;
13
14/// Every setting that is the person's alone to change, besides the Supervisor's own keys.
15pub const PERSON_ONLY: &[&str] = &[
16    "agent:maystartagents",
17    "desktop:enabled",
18    "desktop:allowlist",
19    "desktop:blocklist",
20    "desktop:grantminutes",
21    "network:lan_discovery",
22    "network:remote_ops",
23    "term:localshellpath",
24    "term:localshellopts",
25    "cmd:env",
26    "conn:helper",
27    "audit:sharewithpaired",
28    "tower:sharewithpaired",
29    "viewer:shareterminals",
30    "viewer:allowcomputers",
31    "swarm:sharesummaries",
32    "messaging:telegram:allowed_chats",
33    "messaging:discord:token",
34    "messaging:telegram:token",
35    "messaging:slack:bot_token",
36    "messaging:slack:app_token",
37    "messaging:whatsapp:access_token",
38    "messaging:whatsapp:webhook_verify_token",
39];
40
41/// Is `key` one only the person changes.
42pub fn is_person_only(key: &str) -> bool {
43    PERSON_ONLY.contains(&key) || SUPERVISOR_KEYS.contains(&key)
44}
45
46/// The keys of a settings write that only the person changes, in the order given.
47pub fn person_only_keys(keys: &Map<String, Value>) -> Vec<&str> {
48    keys.keys().map(String::as_str).filter(|k| is_person_only(k)).collect()
49}
50
51/// `Err` unless the connection is the person's window when the write sets a key only the person changes.
52pub fn refuse_unless_person(ctx: &RpcContext, keys: &Map<String, Value>) -> Result<(), String> {
53    let held = person_only_keys(keys);
54    if held.is_empty() {
55        return Ok(());
56    }
57    let mut why = format!("only the person changes {}: ask the person to change it in the AgentMux window", held.join(", "));
58    if held.contains(&ENV_KEY) {
59        why.push_str(". An agent sets its own environment variables with Agents env_set");
60    }
61    ctx.operator_only(&why)
62}
63
64/// The setting for the environment every command starts with; an agent's own variables go through `Agents env_set`.
65const ENV_KEY: &str = "cmd:env";
66
67/// The names of the keys a settings write sets (never their values), for the record of who changed what.
68pub fn key_names(keys: &Map<String, Value>) -> String {
69    keys.keys().map(String::as_str).collect::<Vec<_>>().join(",")
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75    use serde_json::json;
76
77    #[test]
78    fn every_listed_key_is_a_setting_the_schema_knows() {
79        let schema: Value = serde_json::from_str(include_str!("../../../../schema/settings.json")).unwrap();
80        let known = |key: &str| schema.to_string().contains(&format!("\"{key}\""));
81        for key in PERSON_ONLY.iter().chain(SUPERVISOR_KEYS.iter()) {
82            assert!(known(key), "{key} is not in schema/settings.json: a renamed setting would drop out of the list unnoticed");
83        }
84    }
85
86    #[test]
87    fn the_person_only_keys_of_a_write_are_picked_out_and_the_rest_are_left() {
88        let write = json!({"term:fontsize": 12, "agent:maystartagents": true, "supervisor:enabled": true, "app:startatlogin": false});
89        let write = write.as_object().unwrap();
90        let mut found = person_only_keys(write);
91        found.sort_unstable();
92        assert_eq!(found, vec!["agent:maystartagents", "supervisor:enabled"]);
93        assert!(person_only_keys(json!({"term:fontsize": 12}).as_object().unwrap()).is_empty());
94    }
95
96    #[test]
97    fn only_the_window_passes_a_write_that_sets_a_key_only_the_person_changes() {
98        let write = json!({"term:fontsize": 12, "agent:maystartagents": true});
99        let write = write.as_object().unwrap();
100        let agent = RpcContext { agent_id: "korp".into(), operator_proven: true, ..Default::default() };
101        let error = refuse_unless_person(&agent, write).unwrap_err();
102        assert!(error.starts_with("FORBIDDEN: only the person changes agent:maystartagents"), "{error}");
103        assert!(error.contains("ask the person to change it in the AgentMux window"), "{error}");
104        assert!(!error.contains("term:fontsize") && !error.contains("env_set"), "{error}");
105        assert!(refuse_unless_person(&RpcContext::default(), write).unwrap_err().contains("isn't the AgentMux window"));
106        assert!(refuse_unless_person(&RpcContext::operator_for_tests(), write).is_ok());
107    }
108
109    #[test]
110    fn an_agent_still_changes_any_other_setting_including_how_fast_agents_start() {
111        let agent = RpcContext { agent_id: "korp".into(), ..Default::default() };
112        let write = json!({"term:fontsize": 12, "app:startatlogin": false, "agents:start_per_minute": 30});
113        assert!(refuse_unless_person(&agent, write.as_object().unwrap()).is_ok());
114    }
115
116    #[test]
117    fn a_refused_environment_write_names_the_route_an_agent_has_for_its_own() {
118        let agent = RpcContext { agent_id: "korp".into(), ..Default::default() };
119        let error = refuse_unless_person(&agent, json!({"cmd:env": {"A": "b"}}).as_object().unwrap()).unwrap_err();
120        assert!(error.contains("Agents env_set"), "{error}");
121    }
122
123    #[test]
124    fn the_record_names_keys_and_never_values() {
125        let write = json!({"messaging:telegram:token": "s3cret-value", "term:fontsize": 12});
126        let names = key_names(write.as_object().unwrap());
127        assert!(names.contains("messaging:telegram:token") && names.contains("term:fontsize"));
128        assert!(!names.contains("s3cret-value") && !names.contains("12"));
129    }
130}