trusty-mpm 1.3.4

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Tests for the delegation-authority scanner, roster resolver, and renderer.
//!
//! Split out of `delegation_authority.rs` (#4589) so the production file stays
//! under the 500-SLOC cap enforced by `scripts/check_line_cap.sh` — the same
//! move `instruction_pipeline.rs` made in #4318. The module is included with
//! `#[path]`, so `use super::*` still reaches every item (including the private
//! `is_foundation_file`) exactly as it did inline; no assertion changed.

use super::*;
use std::fs;
use tempfile::TempDir;

/// Write `<name>.md` into `dir` with the given raw content.
fn write_agent(dir: &Path, name: &str, content: &str) {
    fs::write(dir.join(format!("{name}.md")), content).expect("write agent");
}

#[test]
fn scan_finds_agents() {
    // A directory with one deployable agent and one base agent must yield
    // exactly the deployable one, with its frontmatter parsed.
    let tmp = TempDir::new().unwrap();
    write_agent(
        tmp.path(),
        "engineer",
        "---\nname: engineer\nrole: engineer\nextends: base-engineer\n\
         description: Implements features and fixes bugs.\nmodel: sonnet\n---\n\n# Engineer\n",
    );
    write_agent(
        tmp.path(),
        "BASE-AGENT",
        "---\nname: base-agent\nrole: base\n---\n\n# Base\n",
    );

    let agents = scan_agents(tmp.path());
    assert_eq!(agents.len(), 1, "only the engineer is deployable");
    let engineer = &agents[0];
    assert_eq!(engineer.name, "engineer");
    assert_eq!(engineer.role, "engineer");
    assert_eq!(
        engineer.description.as_deref(),
        Some("Implements features and fixes bugs.")
    );
    assert_eq!(engineer.model.as_deref(), Some("sonnet"));
    assert_eq!(
        engineer.extends_chain,
        vec!["base-engineer".to_string(), "engineer".to_string()]
    );
}

#[test]
fn scan_excludes_base_agents() {
    // Foundation templates are excluded by the `BASE-*` FILE NAME
    // convention, case-insensitively, and never by frontmatter (#4589 —
    // see `agent_with_a_base_role_but_no_base_filename_stays_in_the_roster`
    // for why the `role:` half was removed).
    let tmp = TempDir::new().unwrap();
    write_agent(
        tmp.path(),
        "BASE-AGENT",
        "---\nname: base-agent\nrole: base\n---\n\nfoundation\n",
    );
    write_agent(
        tmp.path(),
        "base-engineer",
        "---\nname: base-engineer\nrole: base-engineer\n---\n\nfoundation\n",
    );
    write_agent(
        tmp.path(),
        "qa",
        "---\nname: qa\nrole: qa\ndescription: Tests things.\n---\n\n# QA\n",
    );

    let agents = scan_agents(tmp.path());
    assert_eq!(agents.len(), 1);
    assert_eq!(agents[0].name, "qa");
}

#[test]
fn agent_with_a_base_role_but_no_base_filename_stays_in_the_roster() {
    // #4589 REGRESSION. `memory-manager`, `mpm-agent-manager` and
    // `mpm-skills-manager` shipped with `role: base` and were therefore
    // dropped from every tier of the roster — deployed, dispatchable, and
    // invisible to the PM. The exclusion keys off the `BASE-*` FILE NAME
    // convention (tm's own, applied to the files tm ships) rather than a
    // frontmatter value tm does not control in the project and generic
    // `~/.claude/agents` tiers it also unions.
    let tmp = TempDir::new().unwrap();
    write_agent(
        tmp.path(),
        "memory-manager",
        "---\nname: memory-manager\nrole: base\ndescription: Manages memory.\n---\n\n# MM\n",
    );

    let agents = scan_agents(tmp.path());
    assert_eq!(
        agents.iter().map(|a| a.name.as_str()).collect::<Vec<_>>(),
        vec!["memory-manager"],
        "a non-`BASE-*` file must never be classified as a foundation \
         template by its `role:` value alone (#4589)"
    );
}

#[test]
fn near_miss_base_names_are_not_treated_as_foundation_files() {
    // #4589 REVIEW (HIGH). The first cut of this fix read
    // `starts_with("base")` with no separator while its docs and its test
    // name both said "`BASE-*` file name". Under that rule
    // `baseline-analyzer`, `base64-decoder`, `basecamp-sync` and
    // `based-reviewer` are all silently deleted from the roster — the same
    // "a rule tm controls eats an agent tm did not author, and no asset
    // edit can fix it" failure the frontmatter `role:` rule was removed
    // for. The failure would have MOVED rather than been removed.
    //
    // This pins both directions: the five bundled foundation files stay
    // out, and every near miss stays in.
    let tmp = TempDir::new().unwrap();
    let kept = [
        "baseline-analyzer",
        "base64-decoder",
        "basecamp-sync",
        "based-reviewer",
        "database-migrator",
        "engineer",
    ];
    let excluded = [
        "BASE-AGENT",
        "BASE-ENGINEER",
        "BASE-OPS",
        "BASE-QA",
        "BASE-RESEARCH",
        // Lower-case and mixed-case spellings are the same convention.
        "base-custom",
        "Base-Custom-Two",
    ];
    for name in kept.iter().chain(excluded.iter()) {
        write_agent(
            tmp.path(),
            name,
            &format!("---\nname: {name}\nrole: {name}\n---\n\n# {name}\n"),
        );
    }

    let mut scanned: Vec<String> = scan_agents(tmp.path())
        .into_iter()
        .map(|a| a.name)
        .collect();
    scanned.sort();
    let mut want: Vec<String> = kept.iter().map(|s| s.to_string()).collect();
    want.sort();

    assert_eq!(
        scanned, want,
        "only `base-`-prefixed files are foundation templates; a bare \
         `base` prefix silently deletes real agents (#4589 review)"
    );
}

#[test]
fn scan_empty_dir() {
    // An empty directory must yield an empty vec with no error.
    let tmp = TempDir::new().unwrap();
    let agents = scan_agents(tmp.path());
    assert!(agents.is_empty());
}

#[test]
fn scan_missing_dir_is_empty() {
    // A non-existent directory must also yield an empty vec, not panic.
    let agents = scan_agents(Path::new("/no/such/agents/dir/xyz"));
    assert!(agents.is_empty());
}

#[test]
fn scan_ignores_manifest_and_non_md() {
    // The deploy manifest and non-Markdown files must be skipped.
    let tmp = TempDir::new().unwrap();
    fs::write(tmp.path().join("manifest.json"), "{}").unwrap();
    fs::write(tmp.path().join("notes.txt"), "hello").unwrap();
    write_agent(
        tmp.path(),
        "writer",
        "---\nname: writer\nrole: writer\n---\n\n# Writer\n",
    );
    let agents = scan_agents(tmp.path());
    assert_eq!(agents.len(), 1);
    assert_eq!(agents[0].name, "writer");
}

#[test]
fn scan_handles_no_frontmatter() {
    // An agent file with no frontmatter falls back to the file stem.
    let tmp = TempDir::new().unwrap();
    write_agent(
        tmp.path(),
        "plain",
        "# Plain agent\n\nNo frontmatter here.\n",
    );
    let agents = scan_agents(tmp.path());
    assert_eq!(agents.len(), 1);
    assert_eq!(agents[0].name, "plain");
    assert_eq!(agents[0].role, "plain");
    assert_eq!(agents[0].extends_chain, vec!["plain".to_string()]);
}

#[test]
fn generate_authority_nonempty() {
    // A single agent renders under the heading with its name and details.
    let agents = vec![AgentSummary {
        name: "engineer".to_string(),
        role: "engineer".to_string(),
        description: Some("Implements features.".to_string()),
        model: Some("sonnet".to_string()),
        extends_chain: vec![
            "base-agent".to_string(),
            "base-engineer".to_string(),
            "engineer".to_string(),
        ],
    }];
    let md = generate_authority(&agents);
    assert!(md.contains("## Delegation Authority"));
    assert!(md.contains("### engineer"));
    assert!(md.contains("Implements features."));
    assert!(md.contains("base-agent → base-engineer → engineer"));
    assert!(md.contains("**Model:** sonnet"));
}

#[test]
fn generate_authority_empty() {
    // With no agents the heading still renders, plus a "no agents" note.
    let md = generate_authority(&[]);
    assert!(md.contains("## Delegation Authority"));
    assert!(md.to_lowercase().contains("no delegatable agents"));
}

// ── colon-in-value regression tests (issue #389) ─────────────────────────

#[test]
fn scan_preserves_url_in_description() {
    // A `description:` value that is or contains a URL must not be
    // silently truncated at the first colon.
    let tmp = TempDir::new().unwrap();
    write_agent(
        tmp.path(),
        "docs-agent",
        "---\nname: docs-agent\nrole: docs\ndescription: See https://docs.example.com/guide\n---\n\n# Docs\n",
    );
    let agents = scan_agents(tmp.path());
    assert_eq!(agents.len(), 1);
    assert_eq!(
        agents[0].description.as_deref(),
        Some("See https://docs.example.com/guide"),
        "URL in description must not be truncated"
    );
}

#[test]
fn scan_preserves_bedrock_model_id() {
    // A `model:` value containing a bedrock model id (with `/` and `.`)
    // must survive the scan without truncation.
    let tmp = TempDir::new().unwrap();
    write_agent(
        tmp.path(),
        "ml-agent",
        "---\nname: ml-agent\nrole: ml\nmodel: bedrock/us.anthropic.claude-sonnet-4-6\n---\n\n# ML\n",
    );
    let agents = scan_agents(tmp.path());
    assert_eq!(agents.len(), 1);
    assert_eq!(
        agents[0].model.as_deref(),
        Some("bedrock/us.anthropic.claude-sonnet-4-6"),
        "bedrock model id must be preserved verbatim"
    );
}

#[test]
fn scan_preserves_timestamp_in_description() {
    // A description that embeds an ISO-8601 timestamp must keep the full
    // timestamp including the time component (colons after the first).
    let tmp = TempDir::new().unwrap();
    write_agent(
        tmp.path(),
        "timed-agent",
        "---\nname: timed-agent\nrole: timer\ndescription: Deployed at 2026-06-05T14:31:34\n---\n\n# Timed\n",
    );
    let agents = scan_agents(tmp.path());
    assert_eq!(agents.len(), 1);
    assert_eq!(
        agents[0].description.as_deref(),
        Some("Deployed at 2026-06-05T14:31:34"),
        "timestamp in description must not be truncated"
    );
}

#[test]
fn every_bundled_non_base_agent_reaches_the_roster() {
    // Issue #4069 / #4589 REGRESSION GATE. The scanner's only exclusion is
    // the `BASE-*` file-name convention, so this asserts the property that
    // matters end to end: every bundled `.md` that is not a `BASE-*`
    // foundation file survives `scan_agents` and can therefore be routed
    // to. It is the asset-level half of the same invariant
    // `agent_with_a_base_role_but_no_base_filename_stays_in_the_roster`
    // asserts at the scanner level — one guards the shipped files, the
    // other guards the rule, and #4589 needed both.
    let assets = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/assets/agents");

    let mut expected: Vec<String> = fs::read_dir(&assets)
        .expect("bundled agent assets dir")
        .flatten()
        .map(|e| e.path())
        .filter(|p| p.extension().and_then(|e| e.to_str()) == Some("md"))
        .filter_map(|p| {
            p.file_stem()
                .and_then(|s| s.to_str())
                // Shares the production predicate deliberately: a private
                // copy of the rule here would let the gate and the scanner
                // drift, which is the defect shape this PR removes.
                .filter(|stem| !is_foundation_file(stem))
                .map(str::to_string)
        })
        .collect();
    expected.sort();

    let mut scanned: Vec<String> = scan_agents(&assets).into_iter().map(|a| a.name).collect();
    scanned.sort();

    let dropped: Vec<&String> = expected.iter().filter(|n| !scanned.contains(n)).collect();
    assert!(
        dropped.is_empty(),
        "bundled agents silently dropped from every roster (check for a stray \
         `role: base` in their frontmatter): {dropped:?}"
    );
    assert_eq!(
        scanned.len(),
        expected.len(),
        "every non-BASE bundled agent must be delegatable"
    );
}

#[test]
fn generate_authority_omits_self_referential_lines() {
    // Review MEDIUM-3: `Role` that merely repeats the heading and a
    // single-element `Foundation` chain are pure noise, re-emitted into
    // every PM session. A real (multi-element) chain still renders.
    let agents = vec![AgentSummary {
        name: "ticketing".to_string(),
        role: "ticketing".to_string(),
        description: Some("Files tickets.".to_string()),
        model: None,
        extends_chain: vec!["ticketing".to_string()],
    }];
    let md = generate_authority(&agents);

    assert!(md.contains("### ticketing"));
    assert!(md.contains("Files tickets."));
    assert!(
        !md.contains("**Role:**"),
        "role duplicating the heading must be omitted"
    );
    assert!(
        !md.contains("**Foundation:**"),
        "a self-referential single-element chain must be omitted"
    );
}

#[test]
fn roster_from_dirs_dedup_is_case_insensitive() {
    // Review LOW-1: agent names are case-insensitive to the dispatcher, so
    // `QA` and `qa` across two tiers are one agent, not two entries.
    let high = TempDir::new().unwrap();
    let low = TempDir::new().unwrap();
    write_agent(
        high.path(),
        "QA",
        "---\nname: QA\nrole: qa\ndescription: PROJECT TIER\n---\n\n# QA\n",
    );
    write_agent(
        low.path(),
        "qa",
        "---\nname: qa\nrole: qa\ndescription: USER TIER\n---\n\n# QA\n",
    );

    let roster = roster_from_dirs(&[high.path().to_path_buf(), low.path().to_path_buf()]);

    assert_eq!(roster.len(), 1, "QA and qa are the same agent");
    assert_eq!(roster[0].description.as_deref(), Some("PROJECT TIER"));
}

#[test]
fn deployed_agent_dirs_puts_project_tier_first() {
    // Issue #4069: the project tier is the destination the daemon
    // managed-spawn path deploys into AND the only tier readable under
    // `--setting-sources project,local`, so it must rank first.
    let dirs = deployed_agent_dirs(Path::new("/proj"));
    assert_eq!(dirs[0], PathBuf::from("/proj/.claude/agents"));
    assert!(
        dirs.len() >= 2,
        "the user-level tier(s) must also be consulted"
    );
}

#[test]
fn roster_from_dirs_dedupes_with_first_dir_winning() {
    // The same agent is normally deployed into more than one tier; the
    // higher-precedence copy must be the one described, exactly once.
    let high = TempDir::new().unwrap();
    let low = TempDir::new().unwrap();
    write_agent(
        high.path(),
        "qa",
        "---\nname: qa\nrole: qa\ndescription: PROJECT TIER\n---\n\n# QA\n",
    );
    write_agent(
        low.path(),
        "qa",
        "---\nname: qa\nrole: qa\ndescription: USER TIER\n---\n\n# QA\n",
    );
    write_agent(
        low.path(),
        "ops",
        "---\nname: ops\nrole: ops\ndescription: USER TIER\n---\n\n# Ops\n",
    );

    let roster = roster_from_dirs(&[high.path().to_path_buf(), low.path().to_path_buf()]);

    assert_eq!(roster.len(), 2, "qa must appear once, plus ops");
    assert_eq!(roster[0].name, "ops", "roster is name-sorted");
    assert_eq!(roster[1].name, "qa");
    assert_eq!(roster[1].description.as_deref(), Some("PROJECT TIER"));
}

#[test]
fn roster_from_dirs_ignores_missing_dirs() {
    // A tier that does not exist is an empty tier, never a failure — this is
    // the input that makes `deployed_roster_section` return `None`, leaving
    // an unprovisioned environment with the bundled asset alone.
    let roster = roster_from_dirs(&[PathBuf::from("/nonexistent/agents")]);
    assert!(roster.is_empty());
    assert!(roster_from_dirs(&[]).is_empty());
}