Skip to main content

agentmux_srv_core/backend/
reserved_names.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The names no agent may have: ones that read as the person using AgentMux or as AgentMux itself.
5//!
6//! One list ([`RESERVED`]), one check ([`check`]). Every way an agent comes to exist or to be called
7//! something else (create, spawn, rename, define, import, fork) refuses a reserved id, slug or display
8//! name with [`check_all`]. An agent that already has such a name is left alone (see [`reserved_in`]).
9//!
10//! A name is compared case-insensitively, after trimming, with look-alike characters folded to the letter
11//! they imitate (fullwidth forms, the Cyrillic and Greek letters that copy Latin ones, and the digits and
12//! signs that stand in for letters), and with separators dropped: `Op3rator`, `оperator` (Cyrillic о),
13//! `O.P.E.R.A.T.O.R` and `operator-2` all read as `operator`.
14
15/// Words that read as the person or the system, and the names AgentMux uses for its own senders.
16pub const RESERVED: &[&str] = &[
17    // The person.
18    "operator", "user", "human", "person", "owner", "admin", "administrator", "root",
19    // The system.
20    "system", "agentmux", "unproven", "unknown", "window", "host", "srv",
21    // The names AgentMux already uses for its own senders and notes.
22    "githubconsumer", "muxreview", "reagent", "swarmforward", "swarm", "supervisor", "broadcast", "bulletin",
23    "jekt", "cron", "drone", "slack", "discord", "telegram", "whatsapp",
24    // The words the records use for the person and the system as actors.
25    "you", "vault", "failover", "scheduler", "watchdog", "loop", "armory",
26];
27
28/// A letter a look-alike character stands for, or the character itself lower-cased.
29fn fold(c: char) -> char {
30    let c = match c as u32 {
31        0xFF01..=0xFF5E => char::from_u32(c as u32 - 0xFEE0).unwrap_or(c),
32        _ => c,
33    };
34    match c.to_lowercase().next().unwrap_or(c) {
35        // Cyrillic and Greek letters that copy Latin ones.
36        'а' | 'α' => 'a',
37        'е' | 'є' | 'ε' => 'e',
38        'о' | 'ο' | 'σ' => 'o',
39        'р' | 'ρ' => 'p',
40        'с' | 'ϲ' => 'c',
41        'у' | 'γ' => 'y',
42        'х' | 'χ' => 'x',
43        'і' | 'ї' | 'ι' => 'i',
44        'ѕ' => 's',
45        'ј' => 'j',
46        'ԁ' => 'd',
47        'ո' | 'ν' => 'n',
48        'τ' => 't',
49        'μ' | 'м' => 'm',
50        'н' | 'η' => 'h',
51        'к' | 'κ' => 'k',
52        'т' => 't',
53        'в' | 'β' => 'b',
54        // Digits and signs that stand in for letters.
55        '0' => 'o',
56        '1' | '|' | '!' => 'i',
57        '3' => 'e',
58        '4' | '@' => 'a',
59        '5' | '$' => 's',
60        '7' => 't',
61        '8' => 'b',
62        other => other,
63    }
64}
65
66/// The letters of `name` as a reader takes them: look-alikes folded, everything that is not a letter dropped.
67fn letters(name: &str) -> String {
68    name.trim()
69        .chars()
70        .filter(|c| !matches!(*c, '\u{200B}'..='\u{200F}' | '\u{2060}'..='\u{2064}' | '\u{FEFF}' | '\u{00AD}' | '\u{202A}'..='\u{202E}'))
71        .map(fold)
72        .filter(|c| c.is_alphabetic())
73        .collect()
74}
75
76/// The reserved word `name` reads as, or `None`: the whole name, or that word with a counting number after
77/// it (`admin2`, `user-3`).
78pub fn check(name: &str) -> Option<&'static str> {
79    // The name as typed, and without a counting number or separators after it.
80    let counted = name.trim().trim_end_matches(|c: char| c.is_ascii_digit() || !c.is_alphanumeric());
81    [letters(name), letters(counted)]
82        .iter()
83        .filter(|read| !read.is_empty())
84        .find_map(|read| RESERVED.iter().copied().find(|word| read == word))
85}
86
87/// What an id, slug or display name is refused with: the name is reserved, and what to do instead.
88pub fn refusal(name: &str, word: &str) -> String {
89    format!("the name \"{name}\" is reserved (it reads as \"{word}\"): pick a nickname for the agent, a name that reads as a person's")
90}
91
92/// `Err` with the refusal for the first of `names` that is reserved. Empty names are skipped: a missing
93/// name is another check's business.
94pub fn check_all(names: &[&str]) -> Result<(), String> {
95    for name in names {
96        if let Some(word) = check(name) {
97            return Err(refusal(name, word));
98        }
99    }
100    Ok(())
101}
102
103/// The names among `names` that read as reserved, for listing the agents that already have one.
104pub fn reserved_in<'a>(names: impl IntoIterator<Item = &'a str>) -> Vec<&'a str> {
105    names.into_iter().filter(|n| check(n).is_some()).collect()
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn every_reserved_word_is_refused_in_any_case_and_after_trimming() {
114        for word in RESERVED {
115            assert_eq!(check(word), Some(*word), "{word}");
116            assert_eq!(check(&format!("  {}  ", word.to_uppercase())), Some(*word), "{word}");
117        }
118        // The hyphenated forms AgentMux itself uses.
119        assert_eq!(check("github-consumer"), Some("githubconsumer"));
120        assert_eq!(check("Swarm Forward"), Some("swarmforward"));
121        assert_eq!(check("AgentMux"), Some("agentmux"));
122    }
123
124    #[test]
125    fn look_alike_characters_and_separators_do_not_get_a_name_past() {
126        for name in ["operator-3", "admin2", "user_10", "Op3rator", "0perator", "оperator", "operator", "O.P.E.R.A.T.O.R", "oper\u{200B}ator", "sys_tem", "@dmin", "us3r", "hum@n", "agent-mux", "ΑgentMux"] {
127            assert!(check(name).is_some(), "{name:?} slipped through");
128        }
129    }
130
131    #[test]
132    fn a_nickname_is_accepted() {
133        for name in ["Wren", "Winny", "Pulp", "Clamk", "Loap-3", "Naki #2", "AgentY", "AgentX", "Opaz", "QTest", "Hostess", "Userland", "Administrate", "wren2", ""] {
134            assert_eq!(check(name), None, "{name:?} refused");
135        }
136    }
137
138    #[test]
139    fn the_refusal_says_the_name_is_reserved_and_to_pick_a_nickname() {
140        let e = check_all(&["fine", "Operator"]).unwrap_err();
141        assert!(e.contains("reserved") && e.contains("nickname") && e.contains("\"Operator\""), "{e}");
142        assert!(check_all(&["Wren", "wren"]).is_ok());
143    }
144
145    #[test]
146    fn the_agents_that_already_have_a_reserved_name_are_listed() {
147        assert_eq!(reserved_in(["wren", "Admin", "agentmux", "pulp"]), vec!["Admin", "agentmux"]);
148    }
149}