trusty-mpm 1.3.5

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
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
//! Tests for `control::activity` — regex/NLP parse layer (§8.2, WI-3 #1594).
//!
//! Why: keeping tests in a sibling file lets activity.rs stay within the
//! 500-SLOC production cap while retaining full coverage.
//! What: covers every `ActivityKind` variant, false-positive guards for the
//! git-op regex, and pending-decision extraction.
//! Test: run with `cargo test -p trusty-mpm control::activity_tests`.

use super::*;

/// Table-driven: (input, expected_kind, summary_contains, pending_decision,
/// proposed_default)
struct Case {
    input: &'static str,
    expected_kind: ActivityKind,
    summary_prefix: &'static str,
    pending_decision: Option<&'static str>,
    proposed_default: Option<&'static str>,
}

#[test]
fn activity_parser_awaiting_input() {
    let result = ActivityParser::parse_output("> ");
    assert!(result.is_some(), "should match awaiting input");
    let r = result.unwrap();
    assert_eq!(r.kind, ActivityKind::AwaitingInput);
    assert!(r.summary.contains("awaiting"));
}

#[test]
fn activity_parser_tool_use() {
    let result = ActivityParser::parse_output("Tool use: Bash");
    assert!(result.is_some());
    let r = result.unwrap();
    assert_eq!(
        r.kind,
        ActivityKind::ToolUse {
            name: "Bash".into()
        }
    );
    assert!(r.summary.contains("Bash"));
}

#[test]
fn activity_parser_tool_use_using_tool() {
    let result = ActivityParser::parse_output("Using tool: Edit");
    assert!(result.is_some());
    let r = result.unwrap();
    assert_eq!(
        r.kind,
        ActivityKind::ToolUse {
            name: "Edit".into()
        }
    );
}

#[test]
fn activity_parser_error() {
    let result = ActivityParser::parse_output("Error: file not found");
    assert!(result.is_some());
    let r = result.unwrap();
    assert!(matches!(r.kind, ActivityKind::Error { .. }));
    assert!(r.summary.contains("error"));
}

#[test]
fn activity_parser_test_result_cargo() {
    let result = ActivityParser::parse_output("test result: ok. 42 passed; 0 failed; 0 ignored");
    assert!(result.is_some());
    let r = result.unwrap();
    assert_eq!(
        r.kind,
        ActivityKind::TestResult {
            passed: 42,
            failed: 0
        }
    );
    assert!(r.summary.contains("42 passed"));
}

#[test]
fn activity_parser_test_result_cargo_failed() {
    let result =
        ActivityParser::parse_output("test result: FAILED. 10 passed; 3 failed; 0 ignored");
    assert!(result.is_some());
    let r = result.unwrap();
    assert_eq!(
        r.kind,
        ActivityKind::TestResult {
            passed: 10,
            failed: 3
        }
    );
    assert!(r.summary.contains("3 failed"));
}

#[test]
fn activity_parser_test_result_pytest() {
    let result = ActivityParser::parse_output("5 passed, 2 failed");
    assert!(result.is_some());
    let r = result.unwrap();
    assert_eq!(
        r.kind,
        ActivityKind::TestResult {
            passed: 5,
            failed: 2
        }
    );
}

/// Verify git-op detection with word-boundary guards (review bug #4).
///
/// Why: the old regex over-matched prose phrases like "fetching dependencies".
/// What: asserts that real git output matches and prose phrases do NOT.
/// Test: this test plus `activity_parser_git_op_no_false_positives`.
#[test]
fn activity_parser_git_op() {
    let cases = [
        ("[main abc1234] Add feature", true),
        ("git push origin main", true),
        ("git pull --rebase", true),
        ("git merge feature-branch", true),
        ("git fetch upstream", true),
        // false-positive phrases must NOT match (review bug #4)
        ("fetching dependencies from npm", false),
        ("merging results from the database", false),
        ("pulling data from the API endpoint", false),
    ];
    for (input, should_match) in &cases {
        let result = ActivityParser::parse_output(input);
        let is_git_op = result
            .as_ref()
            .is_some_and(|r| matches!(r.kind, ActivityKind::GitOp { .. }));
        if *should_match {
            assert!(
                is_git_op,
                "expected GitOp match for: {input:?}; got: {result:?}"
            );
        } else {
            assert!(
                !is_git_op,
                "false positive: {input:?} must NOT classify as GitOp; \
                 got: {result:?}"
            );
        }
    }
}

/// Explicit false-positive guard for git-op regex (review bug #4).
///
/// Why: prose phrases containing git verbs must not be misclassified.
/// What: verifies word-boundary / `git ` prefix requirement prevents matches.
/// Test: this test.
#[test]
fn activity_parser_git_op_no_false_positives() {
    let false_positives = [
        "fetching dependencies from npm",
        "merging results from the database",
        "pulling data from the API",
        "pushing the limits of what's possible",
    ];
    for input in &false_positives {
        let result = ActivityParser::parse_output(input);
        let is_git_op = result
            .as_ref()
            .is_some_and(|r| matches!(r.kind, ActivityKind::GitOp { .. }));
        assert!(
            !is_git_op,
            "false positive: {input:?} must NOT classify as GitOp; got: {result:?}"
        );
    }
}

/// Table-driven test for the bracket-SHA false-positive fix.
///
/// Why: the old regex used `[0-9a-f]+` (1+ chars) so non-git bracket patterns
/// like `[session a1b2c3]` (6-char hex-like token) matched as GitOp. The fix
/// requires 7–40 hex chars (real git short-SHA range).
/// What: checks that short (≤6 char) bracket tokens do NOT match and that
/// real 7-char and 40-char SHAs DO match.
/// Test: this test.
#[test]
fn activity_parser_git_op_bracket_sha_length() {
    // --- False positives: short tokens that look like hex but are not SHAs ---
    let false_positive_cases = [
        // 6-char hex — one char short of minimum SHA length
        "[session a1b2c3] starting up",
        "[error 404abc] not found",
        "[job a1b2] done",
        // edge: exactly 6 chars
        "[main ab1234] message",
    ];
    for input in &false_positive_cases {
        let result = ActivityParser::parse_output(input);
        let is_git_op = result
            .as_ref()
            .is_some_and(|r| matches!(r.kind, ActivityKind::GitOp { .. }));
        assert!(
            !is_git_op,
            "bracket false positive: {input:?} must NOT classify as GitOp \
             (token is <7 hex chars); got: {result:?}"
        );
    }

    // --- True positives: real git commit bracket output ---
    let true_positive_cases = [
        // Standard 7-char short SHA
        "[main abc1234] Add feature",
        // 8-char SHA
        "[main 1a2b3c4d] Fix typo",
        // Full 40-char SHA
        "[main a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0] Large commit",
    ];
    for input in &true_positive_cases {
        let result = ActivityParser::parse_output(input);
        let is_git_op = result
            .as_ref()
            .is_some_and(|r| matches!(r.kind, ActivityKind::GitOp { .. }));
        assert!(
            is_git_op,
            "expected GitOp for bracket commit output: {input:?}; got: {result:?}"
        );
    }
}

#[test]
fn activity_parser_auth_prompt() {
    let cases = [
        "Please log in to claude.ai",
        "Run claude auth login",
        "Authentication required",
    ];
    for input in &cases {
        let result = ActivityParser::parse_output(input);
        assert!(result.is_some(), "expected match for: {input}");
        let r = result.unwrap();
        assert_eq!(r.kind, ActivityKind::AuthPrompt, "input={input}");
    }
}

#[test]
fn activity_parser_session_complete() {
    let result = ActivityParser::parse_output("Session complete");
    assert!(result.is_some());
    let r = result.unwrap();
    assert_eq!(r.kind, ActivityKind::SessionComplete);
}

#[test]
fn activity_parser_rate_limited() {
    let cases = ["rate limit exceeded", "429 Too Many Requests"];
    for input in &cases {
        let result = ActivityParser::parse_output(input);
        assert!(result.is_some(), "expected match for: {input}");
        let r = result.unwrap();
        assert_eq!(r.kind, ActivityKind::RateLimited, "input={input}");
    }
}

#[test]
fn activity_parser_pending_decision_yes_default() {
    let result =
        ActivityParser::parse_output("Do you want to proceed with the file deletion? [Y/n]:");
    assert!(result.is_some());
    let r = result.unwrap();
    assert_eq!(r.kind, ActivityKind::AwaitingInput);
    assert!(r.pending_decision.is_some());
    assert_eq!(r.proposed_default.as_deref(), Some("yes"));
}

#[test]
fn activity_parser_pending_decision_no_default() {
    let result = ActivityParser::parse_output("Continue with destructive operation? [y/N]:");
    assert!(result.is_some());
    let r = result.unwrap();
    assert_eq!(r.kind, ActivityKind::AwaitingInput);
    assert!(r.pending_decision.is_some());
    assert_eq!(r.proposed_default.as_deref(), Some("no"));
}

/// Verify that a bracket-SHA git commit line produces `commit (branch)` as the
/// verb, NOT the branch name alone.
///
/// Why: before this fix the verb was set to `caps.name("branch")` which gave
/// `git: main` for `[main abc1234] msg`. The summary should be
/// `git: commit (main)` so it's clear this is a commit, not a git push.
/// What: asserts that the `GitOp { verb }` carries "commit (main)" and that the
/// summary contains both "git:" and "commit".
/// Test: this test.
#[test]
fn activity_parser_git_op_bracket_verb_is_commit() {
    let result = ActivityParser::parse_output("[main abc1234] Add feature");
    assert!(result.is_some(), "expected GitOp match");
    let r = result.unwrap();
    match &r.kind {
        ActivityKind::GitOp { verb } => {
            assert!(
                verb.starts_with("commit"),
                "verb must start with 'commit' for bracket-SHA path, got: {verb:?}"
            );
            assert!(
                verb.contains("main"),
                "verb should include the branch name, got: {verb:?}"
            );
        }
        other => panic!("expected GitOp, got: {other:?}"),
    }
    assert!(
        r.summary.contains("commit"),
        "summary must contain 'commit' for bracket-SHA path, got: {:?}",
        r.summary
    );
}

/// Verify that a plain `git push` line produces `push` as the verb (not `commit`).
///
/// Why: companion to the bracket-verb test; confirms the verb branch is correct
/// for explicit git invocations.
/// Test: this test.
#[test]
fn activity_parser_git_op_explicit_verb() {
    for (input, expected_verb) in [
        ("git push origin main", "push"),
        ("git pull --rebase", "pull"),
        ("git merge feature-branch", "merge"),
    ] {
        let result = ActivityParser::parse_output(input);
        assert!(result.is_some(), "expected GitOp for: {input}");
        let r = result.unwrap();
        match &r.kind {
            ActivityKind::GitOp { verb } => {
                assert_eq!(
                    verb.as_str(),
                    expected_verb,
                    "verb mismatch for input={input:?}"
                );
            }
            other => panic!("expected GitOp for {input:?}, got: {other:?}"),
        }
    }
}

/// Verify `(yes/no)` proposed_default derivation from capitalisation.
///
/// Why: §8.4 convention — uppercase option = default. The `(yes/no)` form
/// without capitalisation has no defined default (None), but `(Yes/no)` means
/// "yes" is default and `(yes/No)` means "no" is default.
/// What: table-driven check of all three forms.
/// Test: this test.
#[test]
fn activity_parser_yes_no_proposed_default() {
    let cases = [
        // (yes/no) — no capitalisation → None (no default defined per §8.4)
        ("Do you want to continue? (yes/no):", None),
        // (Yes/no) — Yes is capitalised → default "yes"
        ("Overwrite the file? (Yes/no):", Some("yes")),
        // (yes/No) — No is capitalised → default "no"
        ("Delete the directory? (yes/No):", Some("no")),
    ];
    for (input, expected_default) in &cases {
        let result = ActivityParser::parse_output(input);
        assert!(result.is_some(), "expected a match for {input:?}; got None");
        let r = result.unwrap();
        assert!(
            r.pending_decision.is_some(),
            "pending_decision must be set for {input:?}"
        );
        assert_eq!(
            r.proposed_default.as_deref(),
            *expected_default,
            "proposed_default mismatch for {input:?}: \
             expected {:?}, got {:?}",
            expected_default,
            r.proposed_default
        );
    }
}

#[test]
fn activity_parser_returns_none_for_unknown_output() {
    let result =
        ActivityParser::parse_output("Some totally normal progress message without any keywords");
    assert!(
        result.is_none(),
        "should not match unrecognised output: got {result:?}"
    );
}

/// Table-driven sweep of all ActivityKind variants.
#[test]
fn activity_parser_table_driven() {
    let cases: Vec<Case> = vec![
        Case {
            input: "> ",
            expected_kind: ActivityKind::AwaitingInput,
            summary_prefix: "awaiting",
            pending_decision: None,
            proposed_default: None,
        },
        Case {
            input: "Tool use: Bash",
            expected_kind: ActivityKind::ToolUse {
                name: "Bash".into(),
            },
            summary_prefix: "tool use",
            pending_decision: None,
            proposed_default: None,
        },
        Case {
            input: "Error: cannot read file",
            expected_kind: ActivityKind::Error {
                excerpt: "cannot read file".into(),
            },
            summary_prefix: "error",
            pending_decision: None,
            proposed_default: None,
        },
        Case {
            input: "test result: ok. 7 passed; 0 failed; 0 ignored",
            expected_kind: ActivityKind::TestResult {
                passed: 7,
                failed: 0,
            },
            summary_prefix: "tests:",
            pending_decision: None,
            proposed_default: None,
        },
        Case {
            input: "Session complete",
            expected_kind: ActivityKind::SessionComplete,
            summary_prefix: "session complete",
            pending_decision: None,
            proposed_default: None,
        },
        Case {
            // AuthPrompt must NOT set pending_decision (actor handles it via
            // a dedicated state transition, not via PendingDecision emission).
            input: "Please log in to use Claude",
            expected_kind: ActivityKind::AuthPrompt,
            summary_prefix: "authentication",
            pending_decision: None,
            proposed_default: None,
        },
        Case {
            input: "rate limit exceeded: try again later",
            expected_kind: ActivityKind::RateLimited,
            summary_prefix: "rate-limit",
            pending_decision: None,
            proposed_default: None,
        },
    ];

    for c in &cases {
        let result = ActivityParser::parse_output(c.input);
        assert!(result.is_some(), "expected Some for input={:?}", c.input);
        let r = result.unwrap();
        let kind_matches = match (&r.kind, &c.expected_kind) {
            (ActivityKind::ToolUse { name: a }, ActivityKind::ToolUse { name: b }) => a == b,
            (ActivityKind::Error { .. }, ActivityKind::Error { .. }) => true,
            (a, b) => a == b,
        };
        assert!(
            kind_matches,
            "kind mismatch for input={:?}: got {:?}, want {:?}",
            c.input, r.kind, c.expected_kind
        );
        assert!(
            r.summary.contains(c.summary_prefix),
            "summary {:?} missing prefix {:?} for input={:?}",
            r.summary,
            c.summary_prefix,
            c.input
        );
        assert_eq!(
            r.pending_decision.as_deref(),
            c.pending_decision,
            "pending_decision mismatch for input={:?}",
            c.input
        );
        assert_eq!(
            r.proposed_default.as_deref(),
            c.proposed_default,
            "proposed_default mismatch for input={:?}",
            c.input
        );
    }
}