trusty-mpm 1.8.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Unit tests for the pure `tm issue audit` evaluation (#7097).
//!
//! Every case runs against fixture JSON shaped exactly like a live
//! `gh issue view --json` payload, so no test needs a network, a repository, or
//! an authenticated `gh`.

use super::*;
use crate::core::trusty_tools_config::{TrustyToolsConfig, resolve_ticketing};

/// The built-in standard: milestone and project both required.
fn standard() -> ResolvedTicketing {
    resolve_ticketing(&TrustyToolsConfig::default()).expect("the defaults resolve")
}

/// The accepted component labels this workspace's crates yield (#7123), named
/// literally so no test here reads a Cargo manifest — `ComponentLabels`'s own
/// tests cover the derivation.
fn components() -> ComponentLabels {
    ComponentLabels::from_names(
        ["trusty-mpm", "trusty-audit", "trusty-console", "tga"]
            .into_iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>(),
    )
}

/// A real `gh issue view 7093 --json <AUDIT_JSON_FIELDS>` payload, captured
/// live on 2026-09-08. A compliant issue: milestone, project, component label.
const COMPLIANT: &str = r#"{
  "number": 7093,
  "milestone": {"title": "Backlog · mpm/core"},
  "projectItems": [{"title": "trusty-mpm"}],
  "labels": [{"name": "enhancement"}, {"name": "trusty-mpm"}],
  "comments": [],
  "state": "OPEN",
  "createdAt": "2026-09-08T00:30:20Z",
  "parent": null,
  "blockedBy": {"nodes": [], "totalCount": 0},
  "subIssues": {"nodes": [], "totalCount": 0}
}"#;

fn parse(json: &str) -> IssueFacts {
    serde_json::from_str(json).expect("the fixture parses")
}

fn row<'a>(audit: &'a IssueAudit, requirement: &str) -> &'a AuditRow {
    audit
        .rows
        .iter()
        .find(|r| r.requirement == requirement)
        .unwrap_or_else(|| panic!("the audit carries a `{requirement}` row"))
}

#[test]
fn facts_parse_a_live_view_payload() {
    let facts = parse(COMPLIANT);
    assert_eq!(facts.number, 7093);
    assert_eq!(
        facts.milestone.as_ref().map(|m| m.title.as_str()),
        Some("Backlog · mpm/core")
    );
    assert_eq!(facts.project_items.len(), 1);
    assert_eq!(facts.created_at, "2026-09-08T00:30:20Z");
}

/// A real `gh issue view 7166 --json projectItems` payload, captured live on
/// 2026-09-08 (#7169). Unlike [`COMPLIANT`]'s simplified
/// `{"title": "trusty-mpm"}`, a live project item carries a sibling `status`
/// object — `gh`'s actual wire shape for a ProjectV2 item, not the minimal
/// shape prior fixtures exercised.
const LIVE_PROJECT_ITEM_WITH_STATUS: &str = r#"{"projectItems":[
    {"status":{"optionId":"f75ad846","name":"Todo"},"title":"trusty-mpm"}
]}"#;

#[test]
fn a_project_item_with_a_status_sibling_field_still_parses() {
    // #7169: `tm issue audit 7166` was reported to FAIL "no project" despite
    // `gh issue view 7166 --json projectItems` showing the item attached.
    // `TitleRef` has no `deny_unknown_fields`, so the extra `status` object
    // must be ignored rather than rejected — this fixture is the exact live
    // shape, captured directly from #7166, that a stricter deserializer would
    // break on.
    let facts: IssueFacts =
        serde_json::from_str(LIVE_PROJECT_ITEM_WITH_STATUS).expect("the live shape parses");
    assert_eq!(facts.project_items.len(), 1);
    assert_eq!(facts.project_items[0].title, "trusty-mpm");
}

#[test]
fn facts_parse_a_list_payload() {
    // #7097: `gh issue list --json` returns an ARRAY of the same objects, so
    // one shape must serve both fetches or a batch run would audit less.
    let facts: Vec<IssueFacts> =
        serde_json::from_str(&format!("[{COMPLIANT}]")).expect("the list fixture parses");
    assert_eq!(facts.len(), 1);
    assert_eq!(facts[0].number, 7093);
}

#[test]
fn json_fields_cover_every_audited_requirement() {
    // A field dropped from the list is a requirement that silently stops being
    // checked, so the list is asserted rather than trusted.
    for field in [
        "number",
        "title",
        "milestone",
        "projectItems",
        "labels",
        "comments",
        "state",
        "createdAt",
        "parent",
        "blockedBy",
        "subIssues",
    ] {
        assert!(
            AUDIT_JSON_FIELDS.split(',').any(|f| f == field),
            "AUDIT_JSON_FIELDS must request `{field}`: {AUDIT_JSON_FIELDS}"
        );
    }
}

#[test]
fn a_compliant_issue_passes_every_requirement() {
    let audit = audit_issue(&parse(COMPLIANT), &standard(), &components());
    assert!(!audit.failed(), "{:?}", audit.rows);
    assert_eq!(row(&audit, "project").verdict, Verdict::Pass);
    assert_eq!(row(&audit, "milestone").verdict, Verdict::Pass);
    assert_eq!(row(&audit, "component label").verdict, Verdict::Pass);
    assert_eq!(row(&audit, "component label").detail, "trusty-mpm");
}

#[test]
fn a_missing_project_fails() {
    let json = COMPLIANT.replace(r#"[{"title": "trusty-mpm"}]"#, "[]");
    let audit = audit_issue(&parse(&json), &standard(), &components());
    assert!(audit.failed());
    assert_eq!(audit.failing_requirements(), vec!["project"]);
    assert!(
        row(&audit, "project")
            .detail
            .contains("gh project item-add"),
        "the failure names the fix: {}",
        row(&audit, "project").detail
    );
}

/// The project remediation names the attach that works, not the one that
/// silently no-ops (#7952).
///
/// Why: `gh issue edit <N> --add-project "<title>"` exits 0 and attaches
/// nothing when the title does not resolve in the scope gh derives from the
/// repository, so an agent that follows the old remediation reports done and
/// leaves the issue unattached — observed twice on #7945.
/// What: the FAIL detail carries the owner-scoped, number-keyed form and no
/// longer carries `--add-project` at all.
#[test]
fn a_missing_project_names_the_owner_scoped_attach() {
    let json = COMPLIANT.replace(r#"[{"title": "trusty-mpm"}]"#, "[]");
    let audit = audit_issue(&parse(&json), &standard(), &components());
    let detail = &row(&audit, "project").detail;
    assert!(detail.contains("gh project item-add"), "{detail}");
    assert!(detail.contains("--owner"), "{detail}");
    assert!(detail.contains("--url"), "{detail}");
    assert!(
        !detail.contains("--add-project"),
        "the silent form must not be offered: {detail}"
    );
}

#[test]
fn an_unset_milestone_without_a_comment_fails() {
    let json = COMPLIANT.replace(r#"{"title": "Backlog · mpm/core"}"#, "null");
    let audit = audit_issue(&parse(&json), &standard(), &components());
    assert_eq!(row(&audit, "milestone").verdict, Verdict::Fail);
    assert!(
        row(&audit, "milestone").detail.contains("no-milestone:"),
        "{}",
        row(&audit, "milestone").detail
    );
}

#[test]
fn an_unset_milestone_with_a_reason_comment_is_a_skip() {
    // #7097: SKIP, never PASS — a reader must be able to tell a waived
    // milestone from a set one, and the audit must still exit 0.
    let json = COMPLIANT
        .replace(r#"{"title": "Backlog · mpm/core"}"#, "null")
        .replace(
            r#""comments": []"#,
            r#""comments": [{"body": "no-milestone: spike, retired before the next release"}]"#,
        );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    let milestone = row(&audit, "milestone");
    assert_eq!(milestone.verdict, Verdict::Skip);
    assert!(
        milestone
            .detail
            .contains("spike, retired before the next release"),
        "the reason is quoted: {}",
        milestone.detail
    );
    assert!(!audit.failed(), "a recorded waiver must not exit 1");
}

#[test]
fn a_skip_does_not_fail_the_audit() {
    let audit = IssueAudit {
        number: 1,
        rows: vec![AuditRow {
            requirement: "milestone",
            verdict: Verdict::Skip,
            detail: String::new(),
        }],
    };
    assert!(!audit.failed());
}

#[test]
fn the_no_milestone_comment_is_matched_case_insensitively() {
    let json = COMPLIANT
        .replace(r#"{"title": "Backlog · mpm/core"}"#, "null")
        .replace(
            r#""comments": []"#,
            r#""comments": [{"body": "No-Milestone: tracked upstream"}]"#,
        );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    assert_eq!(row(&audit, "milestone").verdict, Verdict::Skip);
}

#[test]
fn a_no_milestone_note_inside_a_longer_comment_counts() {
    // An agent posting the note as one line of its filing report still
    // satisfies the standard; requiring a comment consisting ONLY of the note
    // would make the escape hatch depend on comment formatting.
    let json = COMPLIANT
        .replace(r#"{"title": "Backlog · mpm/core"}"#, "null")
        .replace(
            r#""comments": []"#,
            r#""comments": [{"body": "Filed per the standard.\nno-milestone: no release owns this yet\n"}]"#,
        );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    let milestone = row(&audit, "milestone");
    assert_eq!(milestone.verdict, Verdict::Skip);
    assert!(
        milestone.detail.contains("no release owns this yet"),
        "{}",
        milestone.detail
    );
}

#[test]
fn an_unrequired_milestone_is_informational() {
    // #7097: the audit reports the operator's rules, never invents one. With
    // `milestone_required: false` an unset milestone is INFO, not FAIL.
    // Built from YAML rather than a struct literal so the test exercises the
    // same parse an operator's `config.yaml` goes through.
    let config: TrustyToolsConfig = serde_yaml::from_str(
        "agents:\n  ticketing:\n    milestone_required: false\n    project_required: false\n",
    )
    .expect("the relaxed block parses");
    let relaxed = resolve_ticketing(&config).expect("the relaxed block resolves");

    let json = COMPLIANT
        .replace(r#"{"title": "Backlog · mpm/core"}"#, "null")
        .replace(r#"[{"title": "trusty-mpm"}]"#, "[]");
    let audit = audit_issue(&parse(&json), &relaxed, &components());
    assert_eq!(row(&audit, "milestone").verdict, Verdict::Info);
    assert_eq!(row(&audit, "project").verdict, Verdict::Info);
    assert!(!audit.failed());
}

#[test]
fn a_missing_component_label_fails() {
    // The observed #7103/#7104 shape: `enhancement` alone, no owning crate.
    let json = COMPLIANT.replace(
        r#"[{"name": "enhancement"}, {"name": "trusty-mpm"}]"#,
        r#"[{"name": "enhancement"}]"#,
    );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    let component = row(&audit, "component label");
    assert_eq!(component.verdict, Verdict::Fail);
    assert!(
        component.detail.contains("trusty-mpm") && component.detail.contains("enhancement"),
        "the failure names both the accepted set and what was found: {}",
        component.detail
    );
}

#[test]
fn a_missing_component_label_with_a_reason_comment_is_a_skip() {
    // #7198: the website/CI shape — no Cargo crate owns the changed path, so
    // applying no component label is policy-correct, not a violation.
    let json = COMPLIANT
        .replace(
            r#"[{"name": "enhancement"}, {"name": "trusty-mpm"}]"#,
            r#"[{"name": "bug"}, {"name": "ci"}]"#,
        )
        .replace(
            r#""comments": []"#,
            r#""comments": [{"body": "no-component-label: website/ tests, no crate owns the path"}]"#,
        );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    let component = row(&audit, "component label");
    assert_eq!(component.verdict, Verdict::Skip);
    assert!(
        component
            .detail
            .contains("website/ tests, no crate owns the path"),
        "the reason is quoted: {}",
        component.detail
    );
    assert!(!audit.failed(), "a recorded waiver must not exit 1");
}

#[test]
fn the_no_component_label_comment_is_matched_case_insensitively() {
    // #7198: mirrors `the_no_milestone_comment_is_matched_case_insensitively` —
    // an agent writing `No-Component-Label:` still records a waiver.
    let json = COMPLIANT
        .replace(
            r#"[{"name": "enhancement"}, {"name": "trusty-mpm"}]"#,
            r#"[{"name": "bug"}]"#,
        )
        .replace(
            r#""comments": []"#,
            r#""comments": [{"body": "context\nNo-Component-Label: CI workflow only"}]"#,
        );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    let component = row(&audit, "component label");
    assert_eq!(component.verdict, Verdict::Skip);
    assert!(
        component.detail.contains("CI workflow only"),
        "the reason is quoted: {}",
        component.detail
    );
}

#[test]
fn a_present_component_label_wins_over_a_waiver_comment() {
    // #7198: a waiver never downgrades a label that IS present — the row still
    // names the crate, so a reader cannot mistake a labelled issue for a waived
    // one.
    let json = COMPLIANT.replace(
        r#""comments": []"#,
        r#""comments": [{"body": "no-component-label: stale waiver"}]"#,
    );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    let component = row(&audit, "component label");
    assert_eq!(component.verdict, Verdict::Pass);
    assert_eq!(component.detail, "trusty-mpm");
}

/// A throwaway workspace whose crate labels are the ones #7139 carries.
///
/// Why: the two #7123 regressions below have to run the real derivation — a
/// hand-written label list would still pass if `ComponentLabels::resolve`
/// stopped reading the workspace, which is the exact defect they guard.
fn workspace_fixture() -> tempfile::TempDir {
    let tmp = tempfile::TempDir::new().unwrap();
    std::fs::write(
        tmp.path().join("Cargo.toml"),
        "[workspace]\nmembers = [\"crates/*\"]\n",
    )
    .unwrap();
    for (dir, package) in [
        ("trusty-mpm", "trusty-mpm"),
        ("trusty-audit", "trusty-audit"),
        ("trusty-git-analytics", "tga"),
    ] {
        let path = tmp.path().join("crates").join(dir);
        std::fs::create_dir_all(&path).unwrap();
        std::fs::write(
            path.join("Cargo.toml"),
            format!("[package]\nname = \"{package}\"\nversion = \"0.1.0\"\n"),
        )
        .unwrap();
    }
    tmp
}

#[test]
fn a_non_mpm_crate_component_label_passes() {
    // #7123: #7139's real labels — `tga` and `trusty-audit`, no `trusty-mpm`.
    // Before the fix the accepted set was the seed table alone, so this audited
    // `FAIL none of [trusty-mpm] present`.
    let tmp = workspace_fixture();
    let resolved = ComponentLabels::resolve(&standard(), Some(tmp.path()));
    let json = COMPLIANT.replace(
        r#"[{"name": "enhancement"}, {"name": "trusty-mpm"}]"#,
        r#"[{"name": "enhancement"}, {"name": "P2"}, {"name": "tga"}, {"name": "trusty-audit"}]"#,
    );
    let audit = audit_issue(&parse(&json), &standard(), &resolved);
    let component = row(&audit, "component label");
    assert_eq!(
        component.verdict,
        Verdict::Pass,
        "detail was: {}",
        component.detail
    );
    assert_eq!(component.detail, "tga, trusty-audit");
    assert!(!audit.failed());
}

#[test]
fn trusty_audit_alone_passes_the_component_label_check() {
    // #7182: the recurrence of #7123 — `a_non_mpm_crate_component_label_passes`
    // above carries `tga` AND `trusty-audit` together, so it passed via `tga`
    // and never isolated `trusty-audit` as the deciding label (the exact gap
    // the recurrence comment named). This fixture carries `trusty-audit` alone.
    let tmp = workspace_fixture();
    let resolved = ComponentLabels::resolve(&standard(), Some(tmp.path()));
    let json = COMPLIANT.replace(
        r#"[{"name": "enhancement"}, {"name": "trusty-mpm"}]"#,
        r#"[{"name": "enhancement"}, {"name": "trusty-audit"}]"#,
    );
    let audit = audit_issue(&parse(&json), &standard(), &resolved);
    let component = row(&audit, "component label");
    assert_eq!(
        component.verdict,
        Verdict::Pass,
        "detail was: {}",
        component.detail
    );
    assert_eq!(component.detail, "trusty-audit");
    assert!(!audit.failed());
}

#[test]
fn an_issue_with_no_crate_label_still_fails_against_the_widened_set() {
    // #7123 widened the accepted set; it did not weaken the rule to "any label
    // present". An issue carrying only a type and a priority still FAILs, and
    // the failure names the accepted set so the fix is a label to copy.
    let tmp = workspace_fixture();
    let resolved = ComponentLabels::resolve(&standard(), Some(tmp.path()));
    let json = COMPLIANT.replace(
        r#"[{"name": "enhancement"}, {"name": "trusty-mpm"}]"#,
        r#"[{"name": "enhancement"}, {"name": "P2"}]"#,
    );
    let audit = audit_issue(&parse(&json), &standard(), &resolved);
    let component = row(&audit, "component label");
    assert_eq!(component.verdict, Verdict::Fail);
    assert!(
        component.detail.contains("tga") && component.detail.contains("trusty-audit"),
        "the failure names the accepted set: {}",
        component.detail
    );
    assert!(audit.failed());
}

#[test]
fn a_workstream_label_never_satisfies_the_component_rule() {
    // #7097: `ws/<session>` is a workstream label. Accepting it here would let
    // a session name stand in for the owning crate.
    let json = COMPLIANT.replace(
        r#"[{"name": "enhancement"}, {"name": "trusty-mpm"}]"#,
        r#"[{"name": "ws/trusty-tools-ec"}]"#,
    );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    assert_eq!(row(&audit, "component label").verdict, Verdict::Fail);
}

#[test]
fn relationships_are_never_a_failure() {
    // A standalone issue legitimately has none, so their absence is reported
    // and never gates.
    let audit = audit_issue(&parse(COMPLIANT), &standard(), &components());
    let rel = row(&audit, "relationships");
    assert_eq!(rel.verdict, Verdict::Info);
    assert!(rel.detail.contains("no parent"), "{}", rel.detail);
    assert!(rel.detail.contains("blocked-by 0"), "{}", rel.detail);
    assert!(rel.detail.contains("sub-issues 0"), "{}", rel.detail);
}

#[test]
fn relationships_report_what_is_set() {
    let json = COMPLIANT
        .replace(r#""parent": null"#, r#""parent": {"number": 6918}"#)
        .replace(
            r#""blockedBy": {"nodes": [], "totalCount": 0}"#,
            r#""blockedBy": {"nodes": [{"number": 42}], "totalCount": 1}"#,
        )
        .replace(
            r#""subIssues": {"nodes": [], "totalCount": 0}"#,
            r#""subIssues": {"nodes": [], "totalCount": 3}"#,
        );
    let audit = audit_issue(&parse(&json), &standard(), &components());
    let rel = row(&audit, "relationships");
    assert_eq!(rel.verdict, Verdict::Info);
    assert!(rel.detail.contains("parent #6918"), "{}", rel.detail);
    assert!(rel.detail.contains("blocked-by 1"), "{}", rel.detail);
    // `totalCount` over `nodes.len()` — a truncated page must not read as 0.
    assert!(rel.detail.contains("sub-issues 3"), "{}", rel.detail);
}

#[test]
fn render_audit_prints_one_line_per_requirement() {
    let text = render_audit(&audit_issue(&parse(COMPLIANT), &standard(), &components()));
    assert!(text.starts_with("#7093\n"), "{text}");
    for requirement in ["project", "milestone", "component label", "relationships"] {
        assert!(
            text.lines()
                .any(|l| l.trim_start().starts_with(requirement)),
            "a `{requirement}` line renders: {text}"
        );
    }
    assert_eq!(text.lines().count(), 5, "{text}");
}

#[test]
fn render_summary_tabulates_every_issue() {
    let compliant = audit_issue(&parse(COMPLIANT), &standard(), &components());
    let broken = audit_issue(
        &parse(&COMPLIANT.replace(r#"[{"title": "trusty-mpm"}]"#, "[]")),
        &standard(),
        &components(),
    );
    let text = render_summary(&[compliant, broken]);
    assert!(text.contains("issue"), "{text}");
    assert_eq!(text.matches("#7093").count(), 2, "{text}");
    assert!(text.contains("2 issue(s) audited, 1 with a FAIL"), "{text}");
}

#[test]
fn render_summary_counts_only_failures() {
    let audits = vec![audit_issue(&parse(COMPLIANT), &standard(), &components())];
    let text = render_summary(&audits);
    assert!(text.contains("1 issue(s) audited, 0 with a FAIL"), "{text}");
}

#[test]
fn the_since_window_keeps_the_boundary_day() {
    let facts = parse(COMPLIANT);
    assert!(created_on_or_after(&facts, "2026-09-08"));
    assert!(created_on_or_after(&facts, "2026-09-01"));
    assert!(!created_on_or_after(&facts, "2026-09-09"));
}

#[test]
fn the_since_window_keeps_an_issue_with_no_timestamp() {
    // Dropping it would silently shrink the audited set, which is the failure
    // mode this whole command exists to catch.
    let facts = IssueFacts::default();
    assert!(created_on_or_after(&facts, "2026-09-08"));
}