trusty-mpm 0.19.26

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
//! Unit tests for the harness-manifest schema (HR-2 / DOC-17), split out of
//! `schema.rs` (a 500-SLOC-capped production file) so coverage can grow under
//! the 1500-SLOC test cap — mirrors the `native_mcp_tests.rs` split.
//!
//! Test: this file IS the test module.

use super::*;

#[test]
fn manifest_roundtrip() {
    // A fully-populated manifest must survive a serialize → parse round-trip
    // unchanged, proving the schema is self-consistent.
    let manifest = HarnessManifest {
        version: Some(MANIFEST_VERSION),
        agents: Some(AgentSet {
            include: vec!["rust-engineer".into(), "qa".into()],
            exclude: vec!["*-ops".into()],
            ignore_staleness: vec!["engineer".into()],
            source: ContentSource::Catalog,
        }),
        skills: Some(SkillSet {
            include: vec!["example-skill".into()],
            exclude: vec![],
            source: ContentSource::Bundled,
        }),
        instructions: Some(InstructionLayers {
            system: Some(true),
            contextual: Some(true),
            domain: Some(false),
        }),
        style: Some(StyleSelection {
            active: Some("trusty-mpm-teacher".into()),
        }),
        mcp: Some(McpServers {
            trusty_memory: Some(true),
            trusty_search: Some(false),
            custom: BTreeMap::from([(
                "duetto-memory".to_string(),
                CustomMcpServer::Http {
                    url: "https://mcp.example/memory".to_string(),
                    headers: BTreeMap::new(),
                },
            )]),
        }),
        models: Some(ModelTiers {
            lightweight: Some("haiku".into()),
            standard: None,
            high: None,
            intensive: Some("opus".into()),
        }),
    };

    let toml = manifest.to_toml().expect("serialize");
    let parsed = HarnessManifest::from_toml(&toml).expect("parse");
    assert_eq!(manifest, parsed);
}

#[test]
fn manifest_partial_parse() {
    // A manifest that sets only one section must parse, with every other
    // section left `None` so the merge keeps the lower layer's values.
    let toml = r#"
version = 1

[style]
active = "trusty-mpm-research"
"#;
    let parsed = HarnessManifest::from_toml(toml).expect("partial parse");
    assert_eq!(parsed.version, Some(1));
    assert_eq!(
        parsed.style.as_ref().and_then(|s| s.active.as_deref()),
        Some("trusty-mpm-research")
    );
    assert!(parsed.agents.is_none());
    assert!(parsed.skills.is_none());
    assert!(parsed.mcp.is_none());
}

#[test]
fn manifest_merge_overrides() {
    // A higher-precedence layer must replace only the sections it sets and
    // leave the rest of the lower layer intact.
    let lower = HarnessManifest {
        version: Some(1),
        agents: Some(AgentSet {
            include: vec!["a".into()],
            ..AgentSet::default()
        }),
        style: Some(StyleSelection {
            active: Some("trusty-mpm".into()),
        }),
        mcp: Some(McpServers {
            trusty_memory: Some(true),
            trusty_search: Some(true),
            ..McpServers::default()
        }),
        ..HarnessManifest::default()
    };
    let higher = HarnessManifest {
        style: Some(StyleSelection {
            active: Some("trusty-mpm-teacher".into()),
        }),
        ..HarnessManifest::default()
    };

    let merged = lower.merge(higher);
    // style replaced by the higher layer
    assert_eq!(
        merged.style.as_ref().and_then(|s| s.active.as_deref()),
        Some("trusty-mpm-teacher")
    );
    // agents + mcp kept from the lower layer (higher left them None)
    assert_eq!(
        merged.agents.as_ref().map(|a| a.include.clone()),
        Some(vec!["a".to_string()])
    );
    assert_eq!(
        merged.mcp.as_ref().and_then(|m| m.trusty_search),
        Some(true)
    );
}

#[test]
fn manifest_merge_keeps_lower_when_absent() {
    // Merging an all-None higher layer is the identity on the lower layer.
    let lower = HarnessManifest {
        version: Some(1),
        style: Some(StyleSelection {
            active: Some("trusty-mpm".into()),
        }),
        ..HarnessManifest::default()
    };
    let merged = lower.clone().merge(HarnessManifest::default());
    assert_eq!(lower, merged);
}

#[test]
fn manifest_merge_mcp_field_level() {
    // A partial `[mcp]` override (only trusty_search) must NOT discard the
    // lower layer's other toggle (trusty_memory) — field-level merge.
    let lower = HarnessManifest {
        mcp: Some(McpServers {
            trusty_memory: Some(true),
            trusty_search: Some(true),
            ..McpServers::default()
        }),
        ..HarnessManifest::default()
    };
    let higher = HarnessManifest {
        mcp: Some(McpServers {
            trusty_memory: None,
            trusty_search: Some(false),
            ..McpServers::default()
        }),
        ..HarnessManifest::default()
    };

    let merged = lower.merge(higher);
    let mcp = merged.mcp.expect("mcp section present");
    assert_eq!(
        mcp.trusty_memory,
        Some(true),
        "the unmentioned toggle must survive the partial override"
    );
    assert_eq!(
        mcp.trusty_search,
        Some(false),
        "the overridden toggle must take the higher layer's value"
    );
}

#[test]
fn instruction_layers_field_merge() {
    // A partial `[instructions]` override must merge field-by-field too.
    let lower = HarnessManifest {
        instructions: Some(InstructionLayers {
            system: Some(true),
            contextual: Some(true),
            domain: Some(true),
        }),
        ..HarnessManifest::default()
    };
    let higher = HarnessManifest {
        instructions: Some(InstructionLayers {
            system: None,
            contextual: None,
            domain: Some(false),
        }),
        ..HarnessManifest::default()
    };

    let layers = lower.merge(higher).instructions.expect("layers present");
    assert_eq!(layers.system, Some(true), "system inherited from lower");
    assert_eq!(
        layers.contextual,
        Some(true),
        "contextual inherited from lower"
    );
    assert_eq!(layers.domain, Some(false), "domain overridden by higher");
}

#[test]
fn selection_matches() {
    // Empty include → match all; exclude wins over include.
    assert!(super::selection_matches("anything", &[], &[]));
    // Exact include.
    let inc = vec!["rust-engineer".to_string()];
    assert!(super::selection_matches("rust-engineer", &inc, &[]));
    assert!(!super::selection_matches("qa", &inc, &[]));
    // Glob include.
    let glob_inc = vec!["*-engineer".to_string()];
    assert!(super::selection_matches("rust-engineer", &glob_inc, &[]));
    assert!(!super::selection_matches("rust-ops", &glob_inc, &[]));
    // Multi-`*` glob include (regression: was mis-matched by split_once).
    let multi_inc = vec!["*-engineer-*".to_string()];
    assert!(super::selection_matches(
        "rust-engineer-ops",
        &multi_inc,
        &[]
    ));
    assert!(!super::selection_matches("rust-engineer", &multi_inc, &[]));
    // Exclude wins.
    let exc = vec!["rust-engineer".to_string()];
    assert!(!super::selection_matches("rust-engineer", &[], &exc));
    let inc_and_exc = ["rust-engineer".to_string()];
    assert!(!super::selection_matches(
        "rust-engineer",
        &inc_and_exc,
        &exc
    ));
}

#[test]
fn glob_matching() {
    // Bare `*` matches everything.
    assert!(super::glob_matches("*", "anything"));
    assert!(super::glob_matches("*", ""));

    // Exact (no wildcard) is equality.
    assert!(super::glob_matches("rust-engineer", "rust-engineer"));
    assert!(!super::glob_matches("rust-engineer", "qa"));
    assert!(!super::glob_matches("rust-engineer", "rust-engineer-2"));

    // Trailing `*` = prefix; leading `*` = suffix.
    assert!(super::glob_matches("*-engineer", "rust-engineer"));
    assert!(!super::glob_matches("*-engineer", "rust-engineer-ops"));
    assert!(super::glob_matches("engineer-*", "engineer-rust"));
    assert!(!super::glob_matches("engineer-*", "rust-engineer"));
    assert!(super::glob_matches("rust-*", "rust-engineer"));

    // Single interior `*` = prefix + suffix.
    assert!(super::glob_matches("a*b", "axxb"));
    assert!(super::glob_matches("a*b", "ab"), "interior * matches empty");
    assert!(!super::glob_matches("a*b", "axx"));

    // Multi-`*` patterns: the previously-broken cases.
    // `*-engineer-*` = "contains -engineer-".
    assert!(super::glob_matches("*-engineer-*", "rust-engineer-ops"));
    assert!(super::glob_matches("*-engineer-*", "x-engineer-y"));
    assert!(
        !super::glob_matches("*-engineer-*", "rust-engineer"),
        "no trailing segment after -engineer- → no match"
    );
    // `a*b*c` = a-prefix, then b, then c-suffix in order.
    assert!(super::glob_matches("a*b*c", "aXbYc"));
    assert!(super::glob_matches("a*b*c", "abc"));
    assert!(
        !super::glob_matches("a*b*c", "acb"),
        "segments must appear in order"
    );
    assert!(
        !super::glob_matches("a*b*c", "aXbY"),
        "missing the trailing c suffix"
    );
}

#[test]
fn content_source_roundtrip() {
    // ContentSource serializes lowercase and round-trips.
    let serialized = toml::to_string(&AgentSet {
        source: ContentSource::Catalog,
        ..AgentSet::default()
    })
    .unwrap();
    assert!(serialized.contains("catalog"));
}

#[test]
fn agent_set_source_default_is_bundled() {
    // An [agents] section that omits `source` must default to Bundled, so
    // the default manifest reproduces today's bundled-asset behavior.
    let toml = "[agents]\ninclude = []\n";
    let parsed = HarnessManifest::from_toml(toml).unwrap();
    assert_eq!(parsed.agents.unwrap().source, ContentSource::Bundled);
}

#[test]
fn custom_mcp_server_stdio_roundtrip() {
    // A project-scope `[mcp.custom.<name>]` stdio table must round-trip and
    // parse from the exact TOML shape an operator would hand-write.
    let toml = r#"
[mcp.custom.my-local-tool]
type = "stdio"
command = "my-tool"
args = ["serve"]

[mcp.custom.my-local-tool.env]
API_KEY = "secret"
"#;
    let parsed = HarnessManifest::from_toml(toml).expect("parse");
    let custom = &parsed.mcp.expect("mcp section present").custom;
    let entry = custom.get("my-local-tool").expect("entry present");
    match entry {
        CustomMcpServer::Stdio { command, args, env } => {
            assert_eq!(command, "my-tool");
            assert_eq!(args, &vec!["serve".to_string()]);
            assert_eq!(env.get("API_KEY").map(String::as_str), Some("secret"));
        }
        other => panic!("expected Stdio, got {other:?}"),
    }
}

#[test]
fn custom_mcp_server_remote_roundtrip() {
    // The http/sse shapes must round-trip too, matching the acceptance
    // example (a clean URL, no headers).
    let toml = r#"
[mcp.custom.duetto-memory]
type = "http"
url = "https://mcp-services.dev.duettosystems.com/memory/mcp"
"#;
    let parsed = HarnessManifest::from_toml(toml).expect("parse");
    let custom = parsed.mcp.expect("mcp section present").custom;
    match custom.get("duetto-memory").expect("entry present") {
        CustomMcpServer::Http { url, headers } => {
            assert_eq!(url, "https://mcp-services.dev.duettosystems.com/memory/mcp");
            assert!(headers.is_empty());
        }
        other => panic!("expected Http, got {other:?}"),
    }

    // Full struct round-trip (serialize → parse) for both remote variants.
    let manifest = HarnessManifest {
        mcp: Some(McpServers {
            custom: BTreeMap::from([
                (
                    "a".to_string(),
                    CustomMcpServer::Http {
                        url: "https://a.example/mcp".to_string(),
                        headers: BTreeMap::new(),
                    },
                ),
                (
                    "b".to_string(),
                    CustomMcpServer::Sse {
                        url: "https://b.example/sse".to_string(),
                        headers: BTreeMap::new(),
                    },
                ),
            ]),
            ..McpServers::default()
        }),
        ..HarnessManifest::default()
    };
    let toml = manifest.to_toml().expect("serialize");
    let parsed = HarnessManifest::from_toml(&toml).expect("parse");
    assert_eq!(manifest, parsed);
}

#[test]
fn mcp_servers_merge_unions_custom() {
    // Two layers each declaring a DIFFERENT custom server must both survive
    // the merge (union by name); a SHARED name must take the higher layer's
    // definition (project-overrides-user precedent extended to multi-layer
    // manifest merging too).
    let lower = McpServers {
        custom: BTreeMap::from([
            (
                "shared".to_string(),
                CustomMcpServer::Stdio {
                    command: "lower-cmd".to_string(),
                    args: vec![],
                    env: BTreeMap::new(),
                },
            ),
            (
                "lower-only".to_string(),
                CustomMcpServer::Stdio {
                    command: "lower-only-cmd".to_string(),
                    args: vec![],
                    env: BTreeMap::new(),
                },
            ),
        ]),
        ..McpServers::default()
    };
    let higher = McpServers {
        custom: BTreeMap::from([(
            "shared".to_string(),
            CustomMcpServer::Stdio {
                command: "higher-cmd".to_string(),
                args: vec![],
                env: BTreeMap::new(),
            },
        )]),
        ..McpServers::default()
    };

    let merged = lower.merge(higher);
    assert_eq!(merged.custom.len(), 2, "both names survive the union");
    match &merged.custom["shared"] {
        CustomMcpServer::Stdio { command, .. } => {
            assert_eq!(command, "higher-cmd", "higher layer wins the shared name");
        }
        other => panic!("expected Stdio, got {other:?}"),
    }
    assert!(merged.custom.contains_key("lower-only"));
}