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
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
//! Unit tests for the NL-to-project resolver (WI-5, #1517).
//!
//! Why: extracted from the old monolithic `resolver.rs` into a sibling test
//! file so the production module stays under the 500-SLOC cap while tests
//! use the 1500-SLOC test-file cap.
//! What: covers exact-name, URL, keyword, multi-candidate disambiguation,
//! clamping, no-match, tie-breaking, and confidence-capping scenarios.
//! Test: this IS the test module; run with `cargo test -p trusty-mpm`.

use super::*;
use crate::session_manager::record::{ManagedSessionId, ManagedSessionState, SessionRecord};
use chrono::Utc;
use std::path::PathBuf;

// ---- helpers ----

fn proj(name: &str, repo_url: &str) -> Project {
    Project {
        name: name.to_string(),
        repo_url: repo_url.to_string(),
        default_branch: "main".to_string(),
        stack_hint: None,
        tags: vec![],
        description: None,
        gh_user: None,
        gh_account: None,
        github: None,
        commit_name: None,
        commit_email: None,
        worktree: None,
    }
}

fn proj_tagged(name: &str, repo_url: &str, tags: &[&str], desc: &str) -> Project {
    Project {
        name: name.to_string(),
        repo_url: repo_url.to_string(),
        default_branch: "main".to_string(),
        stack_hint: None,
        tags: tags.iter().map(|s| s.to_string()).collect(),
        description: Some(desc.to_string()),
        gh_user: None,
        gh_account: None,
        github: None,
        commit_name: None,
        commit_email: None,
        worktree: None,
    }
}

fn session_with_repo(repo_url: &str) -> SessionRecord {
    SessionRecord {
        id: ManagedSessionId::new(),
        tmux_name: "test".into(),
        cwd: PathBuf::from("/tmp"),
        task: "task".into(),
        state: ManagedSessionState::Active,
        created_at: Utc::now(),
        last_activity_at: None,
        workspace_path: None,
        repo_url: Some(repo_url.to_string()),
        branch: None,
        pending_decision: None,
        proposed_default: None,
        correlation: Default::default(),
        runtime: Default::default(),
        ephemeral: false,
        workspace_owned: false,
        source_id: None,
        claude_session_id: None,
        scrollback_path: None,
        last_cwd: None,
        deliverable_id: None,
        pane_id: None,
        tmux_server: None,
        injection_status: Default::default(),
        worktree_owner: None,
        terminal_at: None,
        stop_cause: None,
        kind: Default::default(),
    }
}

fn session_no_repo() -> SessionRecord {
    SessionRecord {
        id: ManagedSessionId::new(),
        tmux_name: "no-repo".into(),
        cwd: PathBuf::from("/tmp"),
        task: "no repo".into(),
        state: ManagedSessionState::Active,
        created_at: Utc::now(),
        last_activity_at: None,
        workspace_path: None,
        repo_url: None,
        branch: None,
        pending_decision: None,
        proposed_default: None,
        correlation: Default::default(),
        runtime: Default::default(),
        ephemeral: false,
        workspace_owned: false,
        source_id: None,
        claude_session_id: None,
        scrollback_path: None,
        last_cwd: None,
        deliverable_id: None,
        pane_id: None,
        tmux_server: None,
        injection_status: Default::default(),
        worktree_owner: None,
        terminal_at: None,
        stop_cause: None,
        kind: Default::default(),
    }
}

// ---- resolve_project: exact name ----

#[test]
fn resolver_exact_name() {
    let projects = vec![
        proj("trusty-tools", "https://github.com/org/trusty-tools"),
        proj("trusty-search", "https://github.com/org/trusty-search"),
    ];
    let res = resolve_project("trusty-tools", &projects).expect("should match");
    assert_eq!(
        res.primary.as_ref().expect("primary").project.name,
        "trusty-tools"
    );
    assert_eq!(
        res.primary.as_ref().expect("primary").reason,
        ResolutionReason::ExactNameMatch
    );
    assert!((res.primary.as_ref().expect("primary").confidence - 1.0).abs() < f32::EPSILON);
}

#[test]
fn resolver_exact_name_case_insensitive() {
    let projects = vec![proj("TrustyTools", "https://github.com/org/trusty-tools")];
    let res = resolve_project("trustytools", &projects).expect("case-insensitive match");
    assert_eq!(
        res.primary.as_ref().expect("primary").reason,
        ResolutionReason::ExactNameMatch
    );
}

#[test]
fn resolver_exact_name_does_not_partial_match_keyword() {
    // "trusty" should NOT produce an ExactNameMatch for "trusty-tools".
    let projects = vec![proj("trusty-tools", "https://github.com/org/trusty-tools")];
    let res = resolve_project("trusty", &projects).expect("keyword fallback");
    // Should fall through to keyword match, not exact.
    assert_ne!(
        res.primary.as_ref().expect("primary").reason,
        ResolutionReason::ExactNameMatch
    );
}

// ---- resolve_project: URL match ----

#[test]
fn resolver_url_match_https() {
    let projects = vec![
        proj("trusty-tools", "https://github.com/org/trusty-tools"),
        proj("other", "https://github.com/org/other"),
    ];
    let res =
        resolve_project("https://github.com/org/trusty-tools.git", &projects).expect("url match");
    assert_eq!(
        res.primary.as_ref().expect("primary").project.name,
        "trusty-tools"
    );
    assert_eq!(
        res.primary.as_ref().expect("primary").reason,
        ResolutionReason::UrlMatch
    );
    assert!((res.primary.as_ref().expect("primary").confidence - 0.95).abs() < f32::EPSILON);
}

#[test]
fn resolver_url_match_github_fragment() {
    let projects = vec![proj("my-repo", "https://github.com/owner/my-repo")];
    // A partial URL fragment without scheme prefix.
    let res = resolve_project("github.com/owner/my-repo", &projects).expect("fragment match");
    assert_eq!(
        res.primary.as_ref().expect("primary").project.name,
        "my-repo"
    );
    assert_eq!(
        res.primary.as_ref().expect("primary").reason,
        ResolutionReason::UrlMatch
    );
}

#[test]
fn resolver_url_no_match_falls_through_to_keyword() {
    let projects = vec![proj("trusty-tools", "https://github.com/org/trusty-tools")];
    // URL that does not match any repo_url.
    let res = resolve_project("https://github.com/other/unrelated.git", &projects);
    // Should fail with NoMatch (no keyword hits either).
    assert!(res.is_err());
}

// ---- resolve_project: keyword match ----

#[test]
fn resolver_keyword_match_name() {
    let projects = vec![
        proj("trusty-search", "https://github.com/org/trusty-search"),
        proj("trusty-memory", "https://github.com/org/trusty-memory"),
    ];
    let res = resolve_project("search", &projects).expect("keyword name match");
    assert_eq!(
        res.primary.as_ref().expect("primary").project.name,
        "trusty-search"
    );
    assert_eq!(
        res.primary.as_ref().expect("primary").reason,
        ResolutionReason::KeywordMatch
    );
}

#[test]
fn resolver_keyword_match_tag() {
    let projects = vec![
        proj_tagged(
            "trusty-search",
            "https://github.com/org/trusty-search",
            &["rust", "search-daemon"],
            "hybrid code search",
        ),
        proj_tagged(
            "trusty-memory",
            "https://github.com/org/trusty-memory",
            &["rust", "memory"],
            "memory palace MCP server",
        ),
    ];
    let res = resolve_project("memory", &projects).expect("tag match");
    assert_eq!(
        res.primary.as_ref().expect("primary").project.name,
        "trusty-memory"
    );
}

#[test]
fn resolver_keyword_match_description() {
    // A description hit alone scores 0.2, which is below KEYWORD_COLLECTION_FLOOR.
    // To score above the floor we need a name hit (0.4) combined with the
    // description hit to demonstrate multi-field scoring.
    let projects = vec![proj_tagged(
        "frontend-app",
        "https://github.com/org/frontend-app",
        &[],
        "the best frontend application ever",
    )];
    // "frontend" hits both the name (+0.4) and description (+0.2) → raw 0.6.
    let res = resolve_project("frontend", &projects).expect("description+name match");
    assert_eq!(
        res.primary.as_ref().expect("primary").project.name,
        "frontend-app"
    );
}

#[test]
fn resolver_keyword_threshold_filters_low_score() {
    // A project whose name does NOT contain "xyz" and has no tags or desc
    // must NOT appear in matches.
    let projects = vec![proj("alpha", "https://github.com/org/alpha")];
    let res = resolve_project("xyz unrelated words", &projects);
    assert!(
        res.is_err(),
        "should produce NoMatch for completely unrelated query"
    );
}

// ---- confidence clamping (#1532 item 1) ----

#[test]
fn resolver_confidence_clamped_to_one() {
    // Craft a query with many words all present in name, desc, and tags
    // so the raw score exceeds 1.0 before clamping.
    let project = proj_tagged(
        "search-daemon",
        "https://github.com/org/search-daemon",
        &["search", "daemon", "backend"],
        "search daemon backend service for search",
    );
    let projects = vec![project];
    // "search daemon backend" hits name(0.4*3=1.2) + tags(0.3 each) + desc(0.2 each)
    // raw score will be well above 1.0.
    let res = resolve_project("search daemon backend", &projects).expect("should match");
    let conf = res.primary.as_ref().expect("primary").confidence;
    assert!(conf <= 1.0, "confidence must be clamped to 1.0, got {conf}");
    assert!(conf > 0.0, "confidence must be positive");
}

// ---- disambiguation (#1532 items 2 & 3) ----

#[test]
fn resolver_multi_candidate_disambiguation() {
    let projects = vec![
        proj_tagged(
            "trusty-search",
            "https://github.com/org/trusty-search",
            &["rust", "search"],
            "search daemon",
        ),
        proj_tagged(
            "trusty-tools",
            "https://github.com/org/trusty-tools",
            &["rust", "workspace"],
            "the trusty search toolset",
        ),
    ];
    // "trusty search" hits both projects well.
    let res = resolve_project("trusty search", &projects).expect("multiple matches");
    assert!(res.matches.len() > 1, "should return multiple candidates");
    assert!(
        res.needs_disambiguation(),
        "both candidates above disambiguation floor should trigger disambiguation"
    );
    // primary is highest confidence.
    let primary_conf = res.primary.as_ref().expect("primary").confidence;
    for m in &res.matches {
        assert!(
            m.confidence <= primary_conf + f32::EPSILON,
            "matches must be sorted descending"
        );
    }
}

#[test]
fn resolver_single_above_floor_no_disambiguation() {
    let projects = vec![
        proj_tagged(
            "trusty-search",
            "https://github.com/org/trusty-search",
            &["search"],
            "search daemon",
        ),
        proj("other-project", "https://github.com/org/other"),
    ];
    // Only "trusty-search" matches "search".
    let res = resolve_project("search", &projects).expect("one match");
    assert!(
        !res.needs_disambiguation(),
        "single candidate above floor must not require disambiguation"
    );
}

// ---- no-match path ----

#[test]
fn resolver_no_match_returns_error() {
    let projects = vec![proj("alpha", "https://github.com/org/alpha")];
    let err = resolve_project("completely-unrelated-xyz", &projects).unwrap_err();
    assert!(
        matches!(err, ResolverError::NoMatch { .. }),
        "expected NoMatch, got: {err:?}"
    );
}

#[test]
fn resolver_empty_registry() {
    let err = resolve_project("anything", &[]).unwrap_err();
    assert!(
        matches!(err, ResolverError::EmptyRegistry),
        "expected EmptyRegistry, got: {err:?}"
    );
}

// ---- ticket-ID reason ----

#[test]
fn resolver_ticket_keyword_reason() {
    let projects = vec![proj_tagged(
        "my-proj",
        "https://github.com/org/my-proj",
        &["proj"],
        "project",
    )];
    let res = resolve_project("PROJ-123", &projects).expect("ticket keyword");
    // PROJ-123 is a ticket ID but also contains the word "proj" (lowercased).
    assert_eq!(
        res.primary.as_ref().expect("primary").reason,
        ResolutionReason::TicketKeyword
    );
}

// ---- resolve_session_project ----

#[test]
fn resolver_session_project_binding() {
    let projects = vec![
        proj("trusty-tools", "https://github.com/org/trusty-tools"),
        proj("other", "https://github.com/org/other"),
    ];
    let session = session_with_repo("https://github.com/org/trusty-tools");
    let bound = resolve_session_project(&session, &projects).expect("should bind");
    assert_eq!(bound.name, "trusty-tools");
}

#[test]
fn resolver_by_repo_url_matches() {
    // #2379: the pre-spawn Deliverable-linkage validation path binds a bare
    // `repo_url` (no `SessionRecord` exists yet) the same way a live session
    // would via `resolve_session_project`.
    let projects = vec![
        proj("trusty-tools", "https://github.com/org/trusty-tools"),
        proj("other", "https://github.com/org/other"),
    ];
    let bound = resolve_project_by_repo_url("https://github.com/org/trusty-tools", &projects)
        .expect("should match");
    assert_eq!(bound.name, "trusty-tools");
}

#[test]
fn resolver_by_repo_url_no_match() {
    let projects = vec![proj("trusty-tools", "https://github.com/org/trusty-tools")];
    assert!(
        resolve_project_by_repo_url("https://github.com/org/unregistered", &projects).is_none()
    );
}

#[test]
fn resolver_session_binding_strips_git_suffix() {
    let projects = vec![proj("trusty-tools", "https://github.com/org/trusty-tools")];
    // Session URL has a .git suffix — must still match.
    let session = session_with_repo("https://github.com/org/trusty-tools.git");
    let bound = resolve_session_project(&session, &projects).expect("git-suffix match");
    assert_eq!(bound.name, "trusty-tools");
}

#[test]
fn resolver_session_no_repo_returns_none() {
    let projects = vec![proj("trusty-tools", "https://github.com/org/trusty-tools")];
    let session = session_no_repo();
    assert!(
        resolve_session_project(&session, &projects).is_none(),
        "session with no repo_url must return None"
    );
}

#[test]
fn resolver_session_unregistered_url_returns_none() {
    let projects = vec![proj("trusty-tools", "https://github.com/org/trusty-tools")];
    let session = session_with_repo("https://github.com/other/unrelated");
    assert!(
        resolve_session_project(&session, &projects).is_none(),
        "unregistered repo URL must return None"
    );
}

/// Per #1532 item 5: when the same repo_url appears under two project names,
/// the first project in the slice wins (tie-break by ordering).
#[test]
fn resolver_session_url_tie_break_first_wins() {
    let projects = vec![
        proj("alpha", "https://github.com/org/shared-repo"),
        proj("beta", "https://github.com/org/shared-repo"),
    ];
    let session = session_with_repo("https://github.com/org/shared-repo");
    let bound = resolve_session_project(&session, &projects).expect("tie-break");
    assert_eq!(
        bound.name, "alpha",
        "tie-break: first project in registry order wins"
    );
}

// ---- fleet_by_project ----

#[test]
fn resolver_fleet_by_project_basic() {
    let projects = vec![
        proj("trusty-tools", "https://github.com/org/trusty-tools"),
        proj("trusty-search", "https://github.com/org/trusty-search"),
    ];
    let sessions = vec![
        session_with_repo("https://github.com/org/trusty-tools"),
        session_with_repo("https://github.com/org/trusty-tools"),
        session_with_repo("https://github.com/org/trusty-search"),
    ];
    let fleet = fleet_by_project(&sessions, &projects);
    assert_eq!(fleet.len(), 2, "one fleet entry per project");
    let tools_fleet = fleet
        .iter()
        .find(|f| f.project.name == "trusty-tools")
        .expect("tools");
    assert_eq!(tools_fleet.sessions.len(), 2);
    let search_fleet = fleet
        .iter()
        .find(|f| f.project.name == "trusty-search")
        .expect("search");
    assert_eq!(search_fleet.sessions.len(), 1);
}

#[test]
fn resolver_fleet_empty_sessions() {
    let projects = vec![proj("trusty-tools", "https://github.com/org/trusty-tools")];
    let fleet = fleet_by_project(&[], &projects);
    assert_eq!(fleet.len(), 1, "project with no sessions still appears");
    assert!(fleet[0].sessions.is_empty());
}

#[test]
fn resolver_fleet_no_repo_sessions_omitted() {
    let projects = vec![proj("trusty-tools", "https://github.com/org/trusty-tools")];
    let sessions = vec![
        session_no_repo(),
        session_with_repo("https://github.com/other/unregistered"),
    ];
    let fleet = fleet_by_project(&sessions, &projects);
    assert_eq!(
        fleet[0].sessions.len(),
        0,
        "unregistered/no-repo sessions omitted"
    );
}

#[test]
fn resolver_fleet_empty_projects() {
    let sessions = vec![session_with_repo("https://github.com/org/x")];
    let fleet = fleet_by_project(&sessions, &[]);
    assert!(fleet.is_empty(), "empty projects yields empty fleet");
}

// ---- reason label ----

#[test]
fn resolver_reason_label() {
    assert!(!ResolutionReason::ExactNameMatch.label().is_empty());
    assert!(!ResolutionReason::UrlMatch.label().is_empty());
    assert!(!ResolutionReason::KeywordMatch.label().is_empty());
    assert!(!ResolutionReason::TicketKeyword.label().is_empty());
}

// ---- local checkout (#7887) ----

/// A row holding only a GitHub URL resolves to its managed clone directory.
///
/// #7887's core claim: `bob-duetto/cto` IS `<repos_root>/bob-duetto/cto`, and
/// never the URL the registry stores.
#[test]
fn local_checkout_for_derives_the_managed_clone_path() {
    let project = proj("cto", "https://github.com/bob-duetto/cto");
    assert_eq!(
        local_checkout_for(&project),
        Some(
            crate::daemon::managed_routes::inproject::repos_root()
                .join("bob-duetto")
                .join("cto")
        )
    );
    // An `ssh` remote names the same checkout as its `https` spelling.
    assert_eq!(
        local_checkout_for(&proj("cto", "git@github.com:bob-duetto/cto.git")),
        local_checkout_for(&project)
    );
}

#[test]
fn local_checkout_for_keeps_an_absolute_repo_url() {
    assert_eq!(
        local_checkout_for(&proj("tools", "/Users/me/work/trusty-tools")),
        Some(PathBuf::from("/Users/me/work/trusty-tools"))
    );
}

#[test]
fn local_checkout_for_refuses_what_names_no_project() {
    assert_eq!(local_checkout_for(&proj("empty", "")), None);
    assert_eq!(
        local_checkout_for(&proj("host-only", "https://github.com/")),
        None
    );
    // One segment: the owner would have to be invented, so there is no answer.
    assert_eq!(local_checkout_for(&proj("no-owner", "cto")), None);
}