trusty-mpm 0.8.0

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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Unit tests for the trusty-mpm MCP dispatch layer (#1221 + earlier phases).
//!
//! Why: extracting the test module out of `mcp/mod.rs` keeps that production
//! file under the 500-SLOC cap while the tests (classified as a test file, 1500
//! cap) live alongside it. Included via `#[path = "tests.rs"] mod tests;`.
//! What: a `MockBackend` implementing every `OrchestratorBackend` method plus
//! dispatch tests for the handshake, the core tools, the bug-reporting tools,
//! and the six session-lifecycle tools.
//! Test: this IS the test module.

use super::*;

/// In-memory backend that records calls and returns canned values.
struct MockBackend;

#[async_trait]
impl OrchestratorBackend for MockBackend {
    async fn session_list(&self) -> Result<Value, String> {
        Ok(json!([{ "id": "s1", "status": "active" }]))
    }
    async fn session_status(&self, session_id: &str) -> Result<Value, String> {
        Ok(json!({ "id": session_id, "status": "active" }))
    }
    async fn agent_delegate(
        &self,
        _session_id: &str,
        agent: &str,
        _task: &str,
        _tier: Option<&str>,
    ) -> Result<Value, String> {
        Ok(json!({ "delegation_id": "d1", "agent": agent }))
    }
    async fn memory_protect(
        &self,
        _session_id: &str,
        used_tokens: u64,
        window_tokens: u64,
    ) -> Result<Value, String> {
        Ok(json!({ "fraction": used_tokens as f64 / window_tokens as f64 }))
    }
    async fn circuit_breaker_status(&self, _agent: Option<&str>) -> Result<Value, String> {
        Ok(json!({ "state": "closed" }))
    }
    async fn hook_event(
        &self,
        _session_id: &str,
        event: &str,
        _payload: Value,
    ) -> Result<Value, String> {
        Ok(json!({ "received": event }))
    }
    async fn list_recent_errors(&self, limit: u64) -> Result<Value, String> {
        Ok(json!({ "errors": [], "limit": limit, "total": 0 }))
    }
    async fn preview_bug_report(&self, fingerprint: &str) -> Result<Value, String> {
        Ok(json!({ "fingerprint": fingerprint, "title": "mock title", "body": "mock body" }))
    }
    async fn report_bug(&self, fingerprint: &str, confirm: bool) -> Result<Value, String> {
        if confirm {
            Ok(json!({ "filed": false, "note": "mock: no token in test" }))
        } else {
            Ok(
                json!({ "filed": false, "note": "confirm:false — preview only", "fingerprint": fingerprint }),
            )
        }
    }

    // ── #1221: session-lifecycle mock impls ──────────────────────────────
    async fn session_new(
        &self,
        repo_url: &str,
        git_ref: &str,
        _task: &str,
        _name_hint: Option<&str>,
        runtime: Option<&str>,
    ) -> Result<Value, String> {
        Ok(json!({
            "id": "m-1",
            "repo_url": repo_url,
            "branch": git_ref,
            "runtime": runtime.unwrap_or("claude-code"),
            "state": "active",
        }))
    }
    async fn session_stop(&self, session_id: &str) -> Result<Value, String> {
        Ok(json!({ "id": session_id, "state": "stopped" }))
    }
    async fn session_resume(&self, session_id: &str) -> Result<Value, String> {
        Ok(json!({ "id": session_id, "state": "active" }))
    }
    async fn session_decommission(&self, session_id: &str) -> Result<Value, String> {
        Ok(json!({ "id": session_id, "state": "decommissioned" }))
    }
    async fn session_activity(&self, session_id: &str, lines: u32) -> Result<Value, String> {
        Ok(json!({ "id": session_id, "lines": lines, "raw_pane": "" }))
    }
    async fn session_send(&self, session_id: &str, text: &str) -> Result<Value, String> {
        Ok(json!({ "id": session_id, "sent": true, "text": text }))
    }

    // ── #1222: console-facing mock impls ─────────────────────────────────────
    async fn console_metrics(&self) -> Result<Value, String> {
        Ok(json!({
            "service_id": "trusty-mpm",
            "display_name": "Trusty MPM",
            "version": "0.0.0",
            "status": "ok",
            "metrics": { "fleet": { "active": 0 }, "auto_resume": { "desired": false } },
            "metrics_schema_version": 1,
        }))
    }
    async fn supervisor_status(&self) -> Result<Value, String> {
        Ok(json!({
            "fleet": { "active": 0, "stopped": 0, "total": 0 },
            "auto_resume": { "desired": false, "env": false, "pending_restart": false },
        }))
    }
    async fn auto_resume_set(&self, enabled: bool) -> Result<Value, String> {
        Ok(json!({ "desired": enabled, "env": false, "pending_restart": enabled }))
    }

    // ── #1220: config-convention mock impls ──────────────────────────────────
    async fn config_read(&self) -> Result<Value, String> {
        Ok(json!({
            "workspace_root_template": null,
            "auto_resume": null,
            "default_model": null,
            "workspace_root": "/home/test/trusty-mpm-projects",
        }))
    }
    async fn config_write(
        &self,
        workspace_root_template: Option<&str>,
        auto_resume: Option<bool>,
        default_model: Option<&str>,
    ) -> Result<Value, String> {
        Ok(json!({
            "workspace_root_template": workspace_root_template,
            "auto_resume": auto_resume,
            "default_model": default_model,
            "workspace_root": workspace_root_template.unwrap_or("/home/test/trusty-mpm-projects"),
        }))
    }
}

fn call(name: &str, args: Value) -> Request {
    Request {
        jsonrpc: Some("2.0".into()),
        id: Some(json!(1)),
        method: "tools/call".into(),
        params: Some(json!({ "name": name, "arguments": args })),
    }
}

#[tokio::test]
async fn dispatch_initialize_returns_server_info() {
    let req = Request {
        jsonrpc: Some("2.0".into()),
        id: Some(json!(1)),
        method: "initialize".into(),
        params: None,
    };
    let resp = dispatch(&MockBackend, req).await;
    let result = resp.result.unwrap();
    assert_eq!(result["serverInfo"]["name"], SERVER_NAME);
}

#[tokio::test]
async fn dispatch_tools_list_returns_full_catalog() {
    // 9 pre-existing + 6 session-lifecycle + 5 console-facing tools = 20
    // (#1222 + the two #1220 config tools).
    let req = Request {
        jsonrpc: Some("2.0".into()),
        id: Some(json!(1)),
        method: "tools/list".into(),
        params: None,
    };
    let resp = dispatch(&MockBackend, req).await;
    let tools = resp.result.unwrap()["tools"].clone();
    assert_eq!(tools.as_array().unwrap().len(), 20);
}

/// Why: the console Config tab calls `config_read`; dispatch must route it and
/// return a non-error result carrying the resolved `workspace_root`.
/// Test: this test.
#[tokio::test]
async fn dispatch_config_read_tool() {
    let resp = dispatch(&MockBackend, call("config_read", json!({}))).await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("workspace_root")
    );
}

/// Why: the Config tab's save calls `config_write`; dispatch must parse the
/// optional fields and route them, echoing the persisted value.
/// Test: this test.
#[tokio::test]
async fn dispatch_config_write_tool() {
    let resp = dispatch(
        &MockBackend,
        call(
            "config_write",
            json!({ "workspace_root_template": "~/custom-projects" }),
        ),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("custom-projects")
    );
}

/// Why: the console poller calls `console_metrics`; dispatch must route it to the
/// backend and return a non-error result carrying the report.
/// Test: this test.
#[tokio::test]
async fn dispatch_console_metrics_tool() {
    let resp = dispatch(&MockBackend, call("console_metrics", json!({}))).await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("trusty-mpm")
    );
}

/// Why: the supervisor widget calls `supervisor_status`; dispatch must route it.
/// Test: this test.
#[tokio::test]
async fn dispatch_supervisor_status_tool() {
    let resp = dispatch(&MockBackend, call("supervisor_status", json!({}))).await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("fleet")
    );
}

/// Why: the auto-resume toggle calls `auto_resume_set` with `enabled`; dispatch
/// must parse the bool and route it.
/// Test: this test.
#[tokio::test]
async fn dispatch_auto_resume_set_tool() {
    let resp = dispatch(
        &MockBackend,
        call("auto_resume_set", json!({ "enabled": true })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("desired")
    );
}

/// Why: `auto_resume_set` must reject a call missing the required `enabled` bool.
/// Test: this test.
#[tokio::test]
async fn dispatch_auto_resume_set_requires_enabled() {
    let resp = dispatch(&MockBackend, call("auto_resume_set", json!({}))).await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], true);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("enabled")
    );
}

#[tokio::test]
async fn dispatch_session_list_tool() {
    let resp = dispatch(&MockBackend, call("session_list", json!({}))).await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("s1")
    );
}

#[tokio::test]
async fn dispatch_agent_delegate_tool() {
    let resp = dispatch(
        &MockBackend,
        call(
            "agent_delegate",
            json!({ "session_id": "s1", "agent": "research", "task": "find" }),
        ),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("research")
    );
}

#[tokio::test]
async fn dispatch_missing_argument_is_tool_error() {
    // `session_id` is required; omitting it yields an isError result.
    let resp = dispatch(&MockBackend, call("session_status", json!({}))).await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], true);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("session_id")
    );
}

#[tokio::test]
async fn dispatch_unknown_tool_is_error() {
    let resp = dispatch(&MockBackend, call("nope", json!({}))).await;
    assert_eq!(resp.result.unwrap()["isError"], true);
}

#[tokio::test]
async fn dispatch_unknown_method_returns_jsonrpc_error() {
    let req = Request {
        jsonrpc: Some("2.0".into()),
        id: Some(json!(1)),
        method: "frobnicate".into(),
        params: None,
    };
    let resp = dispatch(&MockBackend, req).await;
    assert_eq!(resp.error.unwrap().code, error_codes::METHOD_NOT_FOUND);
}

#[tokio::test]
async fn dispatch_notification_is_suppressed() {
    let req = Request {
        jsonrpc: Some("2.0".into()),
        id: None,
        method: "notifications/initialized".into(),
        params: None,
    };
    let resp = dispatch(&MockBackend, req).await;
    assert!(resp.suppress);
}

// ── Phase 3: bug-reporting dispatch tests ─────────────────────────────────

#[tokio::test]
async fn dispatch_list_recent_errors_tool() {
    let resp = dispatch(
        &MockBackend,
        call("list_recent_errors", json!({ "limit": 10 })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("errors")
    );
}

#[tokio::test]
async fn dispatch_list_recent_errors_default_limit() {
    // No limit specified — should default to 20 without error.
    let resp = dispatch(&MockBackend, call("list_recent_errors", json!({}))).await;
    assert_eq!(resp.result.unwrap()["isError"], false);
}

#[tokio::test]
async fn dispatch_preview_bug_report_tool() {
    let fp = "a".repeat(64);
    let resp = dispatch(
        &MockBackend,
        call("preview_bug_report", json!({ "fingerprint": fp })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(result["content"][0]["text"].as_str().unwrap().contains(&fp));
}

#[tokio::test]
async fn dispatch_preview_bug_report_requires_fingerprint() {
    let resp = dispatch(&MockBackend, call("preview_bug_report", json!({}))).await;
    assert_eq!(resp.result.unwrap()["isError"], true);
}

#[tokio::test]
async fn dispatch_report_bug_no_confirm_is_preview_only() {
    let fp = "b".repeat(64);
    let resp = dispatch(
        &MockBackend,
        call("report_bug", json!({ "fingerprint": fp, "confirm": false })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    let text = result["content"][0]["text"].as_str().unwrap();
    // The mock returns `filed: false` with the preview-only note.
    assert!(text.contains("false"), "expected filed:false: {text}");
}

#[tokio::test]
async fn dispatch_report_bug_confirm_true_calls_backend() {
    let fp = "c".repeat(64);
    let resp = dispatch(
        &MockBackend,
        call("report_bug", json!({ "fingerprint": fp, "confirm": true })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    // Mock returns filed:false + no-token note (still a valid response shape).
    let text = result["content"][0]["text"].as_str().unwrap();
    assert!(text.contains("filed"), "expected 'filed' key: {text}");
}

// ── #1221: session-lifecycle dispatch tests ───────────────────────────────

/// Why: `session_new` is the spawn tool; it must parse the three required
/// fields and forward them to the backend.
/// What: calls the tool with repo/ref/task and asserts a non-error result
/// echoing the repo url.
/// Test: this test.
#[tokio::test]
async fn dispatch_session_new_tool() {
    let resp = dispatch(
        &MockBackend,
        call(
            "session_new",
            json!({ "repo_url": "https://example.com/r.git", "ref": "main", "task": "do it" }),
        ),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("example.com")
    );
}

/// Why: `session_new` must reject a call missing the required `repo_url`.
/// Test: this test.
#[tokio::test]
async fn dispatch_session_new_requires_repo_url() {
    let resp = dispatch(
        &MockBackend,
        call("session_new", json!({ "ref": "main", "task": "x" })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], true);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("repo_url")
    );
}

/// Why: the four single-id session tools must each parse `session_id` and
/// forward it; one parameterised test covers stop/resume/decommission.
/// Test: this test.
#[tokio::test]
async fn dispatch_single_id_session_tools() {
    for tool in ["session_stop", "session_resume", "session_decommission"] {
        let resp = dispatch(&MockBackend, call(tool, json!({ "session_id": "s9" }))).await;
        let result = resp.result.unwrap();
        assert_eq!(result["isError"], false, "{tool} should succeed");
        assert!(
            result["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("s9"),
            "{tool} should echo the id"
        );
    }
}

/// Why: a single-id session tool must error when `session_id` is absent.
/// Test: this test.
#[tokio::test]
async fn dispatch_session_stop_requires_id() {
    let resp = dispatch(&MockBackend, call("session_stop", json!({}))).await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], true);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("session_id")
    );
}

/// Why: `session_activity` must honour an explicit `lines` value.
/// Test: this test.
#[tokio::test]
async fn dispatch_session_activity_tool() {
    let resp = dispatch(
        &MockBackend,
        call(
            "session_activity",
            json!({ "session_id": "s1", "lines": 25 }),
        ),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("25"),
        "should reflect the requested line count"
    );
}

/// Why: `session_activity` must default `lines` to 60 when omitted (parity
/// with the HTTP activity route).
/// Test: this test.
#[tokio::test]
async fn dispatch_session_activity_default_lines() {
    let resp = dispatch(
        &MockBackend,
        call("session_activity", json!({ "session_id": "s1" })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("60"),
        "should default to 60 lines"
    );
}

/// Why: `session_send` requires both `session_id` and `text`.
/// Test: this test.
#[tokio::test]
async fn dispatch_session_send_tool() {
    let resp = dispatch(
        &MockBackend,
        call("session_send", json!({ "session_id": "s1", "text": "yes" })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], false);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("yes")
    );
}

/// Why: `session_send` must error when `text` is missing.
/// Test: this test.
#[tokio::test]
async fn dispatch_session_send_requires_text() {
    let resp = dispatch(
        &MockBackend,
        call("session_send", json!({ "session_id": "s1" })),
    )
    .await;
    let result = resp.result.unwrap();
    assert_eq!(result["isError"], true);
    assert!(
        result["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("text")
    );
}