mahbot 0.11.1

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Tool grants: the grammar of a custom role's `custom_roles.tools` column, and the kind vocabulary
//! of the dispatch tools.
//!
//! One entry is `"tool"` (the tool with its narrowest kind), `"tool:kind1,kind2"` (the named kinds),
//! or `"custom:<script>"` — one script from the admin's `shared` folder, which is also the only way
//! a role holds the `custom` dispatcher. [`kinds_of`] is the single source of each dispatch tool's
//! kind vocabulary — the same const arrays the tool enums expose ([`AnalyzeDepth::STRS`],
//! [`Addressee::STRS`]) — so a kind can never be advertised by a tool it does not exist on. The
//! catalog a role may be given is decided elsewhere ([`crate::Role::custom_role_tool_catalog`]);
//! this module owns the grammar and the vocabulary only.

use anyhow::Result;

use crate::tools::analyze::AnalyzeDepth;
use crate::tools::custom::is_tool_name;
use crate::tools::send::Addressee;

/// The prefix that makes an entry name one custom script rather than a tool.
const SCRIPT_PREFIX: &str = "custom:";

/// One stored entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Grant {
    /// `"tool"` or `"tool:kind1,kind2"`.
    Tool { tool: String, kinds: Vec<String> },
    /// `"custom:<script>"`.
    Script { name: String },
}

impl Grant {
    /// The tool this entry names; `None` for a script entry.
    #[must_use]
    pub(crate) fn tool(&self) -> Option<&str> {
        match self {
            Self::Tool { tool, .. } => Some(tool),
            Self::Script { .. } => None,
        }
    }

    /// The script this entry names; `None` for a tool entry.
    #[must_use]
    pub(crate) fn script(&self) -> Option<&str> {
        match self {
            Self::Script { name } => Some(name),
            Self::Tool { .. } => None,
        }
    }

    /// Whether the two entries name the same thing: a tool by its name (its kinds are its interface,
    /// not its identity) or a script by its name. A tool and a script never collide, whatever they
    /// are called.
    #[must_use]
    pub(crate) fn names_the_same(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Tool { tool: a, .. }, Self::Tool { tool: b, .. })
            | (Self::Script { name: a }, Self::Script { name: b }) => a == b,
            _ => false,
        }
    }
}

/// Whether a raw entry names a script rather than a tool.
#[must_use]
pub(crate) fn is_script_entry(entry: &str) -> bool {
    entry.starts_with(SCRIPT_PREFIX)
}

/// The script a raw entry names, or `None` when it names a tool.
#[must_use]
pub(crate) fn script_of(entry: &str) -> Option<&str> {
    entry.strip_prefix(SCRIPT_PREFIX)
}

/// The entry that names one script.
#[must_use]
pub(crate) fn script_entry(name: &str) -> String {
    format!("{SCRIPT_PREFIX}{name}")
}

/// The scripts a stored list names, in stored order, dropping entries the grammar refuses — the
/// same lenient read the tool set is built from.
#[must_use]
pub(crate) fn scripts_of(entries: &[String]) -> Vec<String> {
    parse_lossy(entries)
        .iter()
        .filter_map(|grant| grant.script().map(str::to_string))
        .collect()
}

#[must_use]
pub(crate) fn kinds_of(tool: &str) -> Option<&'static [&'static str]> {
    match tool {
        "analyze" => Some(AnalyzeDepth::STRS),
        "send" => Some(Addressee::STRS),
        _ => None,
    }
}

/// The tool name of a raw tool entry — everything before the first `:`. A script entry
/// ([`is_script_entry`]) names a script instead, whose name is [`script_of`]; every reader of a
/// stored entry checks that first.
#[must_use]
pub(crate) fn tool_name(entry: &str) -> &str {
    entry.split(':').next().unwrap_or(entry)
}

fn canonical_kinds(tool: &str, kinds: &mut [String]) {
    if let Some(vocab) = kinds_of(tool) {
        kinds.sort_by_key(|kind| vocab.iter().position(|v| v == kind).unwrap_or(usize::MAX));
    }
}

/// Parse the stored entries into grants: one entry per tool, in the stored order, with each tool's
/// kinds in the interface order. A tool named twice, or a script named twice, is refused — like an
/// unknown kind.
pub(crate) fn parse(entries: &[String]) -> Result<Vec<Grant>> {
    let mut out: Vec<Grant> = Vec::with_capacity(entries.len());
    for entry in entries {
        let grant = parse_entry(entry)?;
        anyhow::ensure!(
            !out.iter().any(|held| held.names_the_same(&grant)),
            "'{entry}' is named twice"
        );
        out.push(grant);
    }
    Ok(out)
}

/// Parse the stored entries, dropping only the entries the grammar refuses. The read side (building
/// a role's tool set) uses this so a single bad row cannot cost the role its whole tool set — the
/// validated write side never stores one, so this only ever narrows to what the record can serve.
pub(crate) fn parse_lossy(entries: &[String]) -> Vec<Grant> {
    let mut out: Vec<Grant> = Vec::with_capacity(entries.len());
    for entry in entries {
        let Ok(grant) = parse_entry(entry) else {
            continue;
        };
        // One entry per name, as `parse` holds: a repeat is dropped like any other entry the
        // grammar refuses.
        if !out.iter().any(|held| held.names_the_same(&grant)) {
            out.push(grant);
        }
    }
    out
}

/// Parse one entry: a script entry (`custom:<script>`, whose name is a plain file name) or a tool
/// entry (split on its first `:`, with the kinds split on `,` and refused on a tool that has no kind
/// vocabulary, or an unknown kind, or a kind named twice on one tool).
fn parse_entry(entry: &str) -> Result<Grant> {
    if let Some(name) = script_of(entry) {
        anyhow::ensure!(
            is_tool_name(name),
            "'{entry}' does not name a script — hint: a script's name is its file name without the \
             extension"
        );
        return Ok(Grant::Script {
            name: name.to_string(),
        });
    }
    let tool = tool_name(entry);
    let mut kinds: Vec<String> = match entry.split_once(':') {
        None => Vec::new(),
        Some((_, raw)) => {
            let vocab = kinds_of(tool).ok_or_else(|| {
                anyhow::anyhow!("tool '{tool}' takes no kinds, but '{entry}' names some")
            })?;
            let mut kinds: Vec<String> = Vec::new();
            for kind in raw.split(',') {
                let kind = kind.trim();
                anyhow::ensure!(
                    vocab.contains(&kind),
                    "unknown kind '{kind}' for tool '{tool}'"
                );
                anyhow::ensure!(
                    !kinds.iter().any(|k| k == kind),
                    "kind '{kind}' is named twice for tool '{tool}'"
                );
                kinds.push(kind.to_string());
            }
            kinds
        }
    };
    canonical_kinds(tool, &mut kinds);
    Ok(Grant::Tool {
        tool: tool.to_string(),
        kinds,
    })
}

#[must_use]
pub(crate) fn held_kinds(grant: &Grant) -> Vec<String> {
    let Grant::Tool { tool, kinds } = grant else {
        return Vec::new();
    };
    if !kinds.is_empty() {
        return kinds.clone();
    }
    match kinds_of(tool).and_then(|vocab| vocab.first()) {
        Some(narrowest) => vec![(*narrowest).to_string()],
        None => Vec::new(),
    }
}

/// The entries as a display shows them: a bare tool entry with the narrowest kind it implies filled
/// in (`analyze` → `analyze:quick`), a script entry as its own name.
#[must_use]
pub(crate) fn describe_all(entries: &[String]) -> Vec<String> {
    parse_lossy(entries).iter().map(describe).collect()
}

fn describe(grant: &Grant) -> String {
    match grant {
        Grant::Script { name } => script_entry(name),
        Grant::Tool { tool, .. } => encode(&Grant::Tool {
            tool: tool.clone(),
            kinds: held_kinds(grant),
        }),
    }
}

#[must_use]
pub(crate) fn toggle(entries: &[String], tool: &str, kind: &str) -> Vec<String> {
    // Nothing to flip on a tool that has no kinds, nor on a kind the tool's vocabulary does not
    // name.
    let Some(vocab) = kinds_of(tool) else {
        return entries.to_vec();
    };
    if !vocab.contains(&kind) {
        return entries.to_vec();
    }
    // An entry the grammar refuses is carried through untouched — rewriting it is the write side's
    // job, and it refuses such a record outright — so a record the picker renders checkmarks for is
    // one this can still edit. The read is the same lenient one the picker and the launch path use.
    let mut out: Vec<String> = Vec::with_capacity(entries.len() + 1);
    let mut found = false;
    for entry in entries {
        let Ok(grant) = parse_entry(entry) else {
            out.push(entry.clone());
            continue;
        };
        // A script entry (or another tool's) is carried through untouched.
        if grant.tool() != Some(tool) {
            out.push(entry.clone());
            continue;
        }
        found = true;
        let mut kinds = held_kinds(&grant);
        match kinds.iter().position(|k| k == kind) {
            Some(at) => {
                kinds.remove(at);
            }
            None => kinds.push(kind.to_string()),
        }
        canonical_kinds(tool, &mut kinds);
        if !kinds.is_empty() {
            out.push(encode(&Grant::Tool {
                tool: tool.to_string(),
                kinds,
            }));
        }
    }
    if !found {
        out.push(encode(&Grant::Tool {
            tool: tool.to_string(),
            kinds: vec![kind.to_string()],
        }));
    }
    out
}

#[must_use]
fn encode(grant: &Grant) -> String {
    match grant {
        Grant::Script { name } => script_entry(name),
        Grant::Tool { tool, kinds } if kinds.is_empty() => tool.clone(),
        Grant::Tool { tool, kinds } => format!("{tool}:{}", kinds.join(",")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn entries(items: &[&str]) -> Vec<String> {
        items.iter().map(|s| (*s).to_string()).collect()
    }

    #[test]
    fn held_kinds_and_describe_all_fill_in_the_narrowest_kind() {
        let parsed = parse(&entries(&["analyze", "send:worker,role", "read"])).unwrap();
        assert_eq!(held_kinds(&parsed[0]), vec!["quick".to_string()]);
        assert_eq!(
            held_kinds(&parsed[1]),
            vec!["worker".to_string(), "role".to_string()]
        );
        assert!(
            held_kinds(&parsed[2]).is_empty(),
            "a tool with no kind vocabulary holds none"
        );
        assert_eq!(
            describe_all(&entries(&["analyze", "send", "read", "send:manager"])),
            entries(&["analyze:quick", "send:worker", "read"])
        );
    }

    #[test]
    fn toggle_adds_removes_and_drops_an_entry() {
        assert_eq!(
            toggle(&entries(&["read"]), "send", "manager"),
            entries(&["read", "send:manager"])
        );
        assert_eq!(
            toggle(&entries(&["send:manager", "read"]), "send", "worker"),
            entries(&["send:worker,manager", "read"]),
            "the kind lands in interface order and the entry keeps its position"
        );
        assert_eq!(
            toggle(&entries(&["send:manager"]), "send", "manager"),
            Vec::<String>::new(),
            "the entry goes with its last kind"
        );
        assert_eq!(
            toggle(&entries(&["analyze"]), "analyze", "quick"),
            Vec::<String>::new(),
            "the implied narrowest kind is what a bare entry holds"
        );
        assert_eq!(
            toggle(&entries(&["analyze"]), "analyze", "deep"),
            entries(&["analyze:quick,deep"]),
            "the implied kind stays alongside the added one"
        );
        assert_eq!(
            toggle(&entries(&["read"]), "read", "quick"),
            entries(&["read"]),
            "a tool with no kind vocabulary is left untouched"
        );
    }

    /// The grant vocabulary is [`Addressee::STRS`], never the wider vocabulary [`Addressee::parse`]
    /// reads: an entry is validated here before it ever reaches the tool's parser, so neither
    /// owner-side address is grantable.
    #[test]
    fn a_grant_can_never_name_the_command_lines_own_assistant_or_the_guest_handover() {
        assert!(parse(&entries(&["send:assistant"])).is_err());
        assert!(parse(&entries(&["send:guest"])).is_err());
        assert!(
            parse(&entries(&["send:worker,assistant"])).is_err(),
            "a known kind beside an unknown one does not make the entry valid"
        );
    }

    /// A script entry is one script, held beside any number of others and beside tools of the same
    /// name: `custom:read` and `read` are two different things.
    #[test]
    fn script_entries_are_individual_and_never_collapse() {
        let stored = entries(&["custom:read", "custom:weather", "read"]);
        let parsed = parse(&stored).unwrap();
        assert_eq!(
            parsed,
            vec![
                Grant::Script {
                    name: "read".to_string()
                },
                Grant::Script {
                    name: "weather".to_string()
                },
                Grant::Tool {
                    tool: "read".to_string(),
                    kinds: Vec::new()
                },
            ]
        );
        assert_eq!(scripts_of(&stored), entries(&["read", "weather"]));
        assert_eq!(
            describe_all(&stored),
            stored,
            "a script entry is shown as itself"
        );
        assert_eq!(
            toggle(&stored, "read", "quick"),
            stored,
            "a tool with no kinds leaves every entry alone, scripts included"
        );
    }

    /// The grammar refuses a script entry that names no plain file name, and one script named
    /// twice; the lenient read drops exactly those entries.
    #[test]
    fn a_script_entry_is_a_plain_file_name_named_once() {
        for bad in ["custom:", "custom:.hidden", "custom:a/b", "custom:a\\b"] {
            assert!(parse(&entries(&[bad])).is_err(), "{bad:?} must be refused");
            assert!(
                parse_lossy(&entries(&[bad])).is_empty(),
                "{bad:?} must be dropped on the read side"
            );
        }
        assert!(
            parse(&entries(&["custom:weather", "custom:weather"])).is_err(),
            "one script named twice is refused"
        );
        assert_eq!(
            parse_lossy(&entries(&["custom:weather", "custom:weather"])).len(),
            1,
            "the repeat is dropped like any other refused entry"
        );
    }
}