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
//! Unit tests for the per-account `gh` config directory bootstrap (#7166).
//!
//! Why: every function under test takes its paths as parameters, so these
//! run against real temp directories — no real `$HOME`, no real `gh`, no
//! network, and no process-global env mutation.
//! What: dir creation from an operator `hosts.yml` that names the login,
//! refusal (dir untouched) when it does not, reuse of an already-built dir,
//! `config.yml` copying (and tolerance of its absence), and the exact
//! `hosts.yml` shape written round-tripping through the real parser.
//! Test: this file IS the test module.

use super::*;

const TWO_ACCOUNT_HOSTS_YML: &str = "\
github.com:
    git_protocol: https
    users:
        bobmatnyc:
        bob-duetto:
    user: bob-duetto
";

/// `TWO_ACCOUNT_HOSTS_YML` with synthetic logins, for the #8510 tests.
const SYNTHETIC_HOSTS_YML: &str = "\
github.com:
    git_protocol: https
    users:
        octo-other:
        octo-pinned:
    user: octo-pinned
";

/// Build a fake operator gh config dir (`hosts.yml` + optional `config.yml`)
/// under `root`, returning its path.
fn fake_operator_gh_config_dir(
    root: &std::path::Path,
    hosts_yml: &str,
    config_yml: Option<&str>,
) -> std::path::PathBuf {
    let dir = root.join("operator-gh-config");
    std::fs::create_dir_all(&dir).expect("create fake operator gh config dir");
    std::fs::write(dir.join("hosts.yml"), hosts_yml).expect("write fake hosts.yml");
    if let Some(config) = config_yml {
        std::fs::write(dir.join("config.yml"), config).expect("write fake config.yml");
    }
    dir
}

#[test]
fn ensure_account_config_dir_places_it_under_gh_accounts() {
    let state_root = std::path::Path::new("/tmp/tm-state");
    // #8510: the path helper moved to core, where the account-dir borrow uses it.
    assert_eq!(
        crate::core::gh_account_dir::tm_account_dir(state_root, "octo-pinned"),
        Ok(std::path::PathBuf::from(
            "/tmp/tm-state/gh-accounts/octo-pinned"
        ))
    );
}

/// The headline case: a login the operator's `hosts.yml` names gets a fresh,
/// isolated config dir built for it.
#[test]
fn ensure_account_config_dir_builds_from_operator_hosts_yml() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let dir = ensure_account_config_dir(&state_root, &operator_dir, "bob-duetto")
        .expect("bob-duetto is a known account");

    assert_eq!(dir, state_root.join("gh-accounts").join("bob-duetto"));
    assert!(dir.join("hosts.yml").is_file());
    let written = std::fs::read_to_string(dir.join("hosts.yml")).unwrap();
    assert!(written.contains("bob-duetto"), "{written}");
    assert!(
        !written.contains("bobmatnyc"),
        "the built hosts.yml must name ONLY the selected account: {written}"
    );
    // No token was ever written anywhere this function touches.
    assert!(!written.to_lowercase().contains("token"), "{written}");
}

/// The login is matched case-insensitively, but the WRITTEN file uses `gh`'s
/// own canonical spelling — not whatever case the caller passed.
#[test]
fn ensure_account_config_dir_uses_the_canonical_spelling() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    // Note the deliberately wrong case on input.
    let dir = ensure_account_config_dir(&state_root, &operator_dir, "BOB-DUETTO")
        .expect("case-insensitive match must succeed");

    // The directory path itself is keyed on the CALLER's spelling (matches
    // register_args'/the CLI's own case handling of --account), but the
    // hosts.yml content inside it is gh's canonical spelling.
    assert_eq!(dir, state_root.join("gh-accounts").join("BOB-DUETTO"));
    let written = std::fs::read_to_string(dir.join("hosts.yml")).unwrap();
    assert!(written.contains("bob-duetto"), "{written}");
}

/// A login the operator's `hosts.yml` does NOT name is refused, with the
/// `gh auth login` remedy, and the directory is never created.
#[test]
fn ensure_account_config_dir_refuses_an_unknown_login() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let err = ensure_account_config_dir(&state_root, &operator_dir, "someone-else")
        .expect_err("an unlogged-in account must be refused");
    assert!(err.contains("someone-else"), "{err}");
    assert!(err.contains("gh auth login"), "{err}");
    assert!(
        !state_root.join("gh-accounts").join("someone-else").exists(),
        "a refused account must never get a directory"
    );
}

/// No operator `hosts.yml` at all (never ran `gh auth login`) is the same
/// refusal shape, not a panic or a different error class.
#[test]
fn ensure_account_config_dir_refuses_when_operator_has_no_hosts_yml() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = tmp.path().join("empty-operator-dir");
    std::fs::create_dir_all(&operator_dir).unwrap();

    let err = ensure_account_config_dir(&state_root, &operator_dir, "bob-duetto")
        .expect_err("no hosts.yml at all must refuse");
    assert!(err.contains("gh auth login"), "{err}");
}

/// An already-built dir (from a prior `--account` use) is reused UNTOUCHED —
/// no re-read of the operator's `hosts.yml`, no rewrite.
#[test]
fn ensure_account_config_dir_reuses_an_existing_dir_untouched() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let dir = ensure_account_config_dir(&state_root, &operator_dir, "bob-duetto")
        .expect("first build succeeds");
    let sentinel = "hand-edited-by-the-operator, must survive reuse";
    std::fs::write(dir.join("hosts.yml"), sentinel).unwrap();

    // Second call: even with an operator hosts.yml that no longer lists the
    // account at all, an already-built dir must still be reused as-is.
    let empty_operator_dir = tmp.path().join("now-empty-operator-dir");
    std::fs::create_dir_all(&empty_operator_dir).unwrap();
    let dir2 = ensure_account_config_dir(&state_root, &empty_operator_dir, "bob-duetto")
        .expect("an existing dir must be reused, not rebuilt or refused");

    assert_eq!(dir, dir2);
    assert_eq!(
        std::fs::read_to_string(dir.join("hosts.yml")).unwrap(),
        sentinel,
        "reuse must not overwrite the existing hosts.yml"
    );
}

/// The operator's `config.yml` is copied into the new account dir when present.
#[test]
fn ensure_account_config_dir_copies_config_yml_when_present() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(
        tmp.path(),
        TWO_ACCOUNT_HOSTS_YML,
        Some("version: \"1\"\ngit_protocol: ssh\n"),
    );

    let dir = ensure_account_config_dir(&state_root, &operator_dir, "bob-duetto").unwrap();
    assert_eq!(
        std::fs::read_to_string(dir.join("config.yml")).unwrap(),
        "version: \"1\"\ngit_protocol: ssh\n"
    );
}

/// A missing `config.yml` on the operator side is NOT an error — `gh`
/// tolerates a config dir with no `config.yml` of its own.
#[test]
fn ensure_account_config_dir_tolerates_a_missing_config_yml() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), SYNTHETIC_HOSTS_YML, None);

    ensure_account_config_dir(&state_root, &operator_dir, "octo-pinned")
        .expect("no config.yml must not be an error");
}

/// 🔴 #8510 HIGH: with no operator `config.yml`, the built dir still declares
/// `version: "1"`, so gh never migrates it.
#[test]
fn ensure_account_config_dir_writes_the_config_version() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), SYNTHETIC_HOSTS_YML, None);

    let dir = ensure_account_config_dir(&state_root, &operator_dir, "octo-pinned").unwrap();
    assert_eq!(
        std::fs::read_to_string(dir.join("config.yml")).unwrap(),
        "version: \"1\"\n"
    );
    crate::core::gh_account_dir::refuse_unmigrated_config(&dir)
        .expect("the built dir must pass the migration guard");
}

/// 🔴 #8510 HIGH: a copied operator `config.yml` with no `version` gains one;
/// the rest of the operator's settings are kept.
#[test]
fn ensure_account_config_dir_adds_the_version_to_a_copied_config() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir =
        fake_operator_gh_config_dir(tmp.path(), SYNTHETIC_HOSTS_YML, Some("git_protocol: ssh\n"));

    let dir = ensure_account_config_dir(&state_root, &operator_dir, "octo-pinned").unwrap();
    assert_eq!(
        std::fs::read_to_string(dir.join("config.yml")).unwrap(),
        "version: \"1\"\ngit_protocol: ssh\n"
    );
    assert_eq!(
        std::fs::read_to_string(operator_dir.join("config.yml")).unwrap(),
        "git_protocol: ssh\n",
        "the operator's own config.yml is never touched"
    );
}

/// 🔴 #8510 HIGH: a dir built before the version was written gains it on
/// reuse; its `hosts.yml` stays untouched.
#[test]
fn ensure_account_config_dir_adds_the_version_to_a_reused_dir() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let dir = state_root.join("gh-accounts").join("octo-pinned");
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("hosts.yml"), "old-hosts").unwrap();
    std::fs::write(dir.join("config.yml"), "git_protocol: ssh\n").unwrap();
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), SYNTHETIC_HOSTS_YML, None);

    ensure_account_config_dir(&state_root, &operator_dir, "octo-pinned").unwrap();
    assert_eq!(
        std::fs::read_to_string(dir.join("config.yml")).unwrap(),
        "version: \"1\"\ngit_protocol: ssh\n"
    );
    assert_eq!(
        std::fs::read_to_string(dir.join("hosts.yml")).unwrap(),
        "old-hosts"
    );
}

/// The exact `hosts.yml` this module writes round-trips through the real
/// `gh_account` parser and names exactly one account.
#[test]
fn render_single_account_hosts_yml_round_trips_through_the_parser() {
    let text = render_single_account_hosts_yml("bob-duetto");
    let status =
        crate::core::gh_account::parse_gh_account_status_from_hosts_yml(&text).expect("must parse");
    assert_eq!(status.active.as_deref(), Some("bob-duetto"));
    assert_eq!(status.logged_in, vec!["bob-duetto".to_string()]);
    assert!(!status.is_ambiguous());
}

// -----------------------------------------------------------------------
// #7166 review follow-up MEDIUM: `login` must be rejected before it ever
// becomes a path segment — `is_name_segment` (the CLI-layer check) restricts
// only the character set, not the exact strings `.`/`..`.
// -----------------------------------------------------------------------

#[test]
fn ensure_account_config_dir_refuses_dot() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let err = ensure_account_config_dir(&state_root, &operator_dir, ".")
        .expect_err("'.' must be refused before any join");
    assert!(err.contains('.'), "{err}");
    assert!(
        !state_root.exists(),
        "a refused login must never touch the filesystem"
    );
}

#[test]
fn ensure_account_config_dir_refuses_dotdot() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let err = ensure_account_config_dir(&state_root, &operator_dir, "..").expect_err(
        "'..' must be refused before any join — otherwise it resolves to state_root itself",
    );
    assert!(err.contains(".."), "{err}");
}

#[test]
fn ensure_account_config_dir_refuses_empty() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let err = ensure_account_config_dir(&state_root, &operator_dir, "")
        .expect_err("an empty login must be refused");
    assert!(err.contains("not a valid account login"), "{err}");
}

#[test]
fn ensure_account_config_dir_refuses_a_forward_slash() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let err = ensure_account_config_dir(&state_root, &operator_dir, "bob/duetto")
        .expect_err("a path separator must be refused");
    assert!(err.contains("bob/duetto"), "{err}");
}

#[test]
fn ensure_account_config_dir_refuses_a_backslash() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let err = ensure_account_config_dir(&state_root, &operator_dir, "bob\\duetto")
        .expect_err("a path separator must be refused");
    assert!(err.contains("bob\\duetto"), "{err}");
}

// -----------------------------------------------------------------------
// #7166 review follow-up LOW: a pre-planted symlink at the dir or at
// `hosts.yml` must be refused before any create/write follows it.
// -----------------------------------------------------------------------

#[test]
#[cfg(unix)]
fn ensure_account_config_dir_refuses_a_symlinked_dir() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let real_target = tmp.path().join("elsewhere");
    std::fs::create_dir_all(&real_target).unwrap();
    std::fs::create_dir_all(state_root.join("gh-accounts")).unwrap();
    std::os::unix::fs::symlink(
        &real_target,
        state_root.join("gh-accounts").join("bob-duetto"),
    )
    .expect("create symlink");

    let err = ensure_account_config_dir(&state_root, &operator_dir, "bob-duetto")
        .expect_err("a pre-planted symlinked dir must be refused");
    assert!(err.contains("symlink"), "{err}");
}

#[test]
#[cfg(unix)]
fn ensure_account_config_dir_refuses_a_symlinked_hosts_yml() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), TWO_ACCOUNT_HOSTS_YML, None);

    let dir = state_root.join("gh-accounts").join("bob-duetto");
    std::fs::create_dir_all(&dir).unwrap();
    let real_target = tmp.path().join("some-real-file");
    std::fs::write(&real_target, "not a hosts.yml").unwrap();
    std::os::unix::fs::symlink(&real_target, dir.join("hosts.yml")).expect("create symlink");

    let err = ensure_account_config_dir(&state_root, &operator_dir, "bob-duetto")
        .expect_err("a pre-planted symlinked hosts.yml must be refused");
    assert!(err.contains("symlink"), "{err}");
}

/// #8510: the version write must never follow a planted `config.yml` link.
#[test]
#[cfg(unix)]
fn ensure_account_config_dir_refuses_a_symlinked_config_yml() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(tmp.path(), SYNTHETIC_HOSTS_YML, None);

    let dir = state_root.join("gh-accounts").join("octo-pinned");
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("hosts.yml"), "built").unwrap();
    let real_target = tmp.path().join("operator-config.yml");
    std::fs::write(&real_target, "git_protocol: ssh\n").unwrap();
    std::os::unix::fs::symlink(&real_target, dir.join("config.yml")).expect("create symlink");

    let err = ensure_account_config_dir(&state_root, &operator_dir, "octo-pinned")
        .expect_err("a pre-planted symlinked config.yml must be refused");
    assert!(err.contains("symlink"), "{err}");
    assert_eq!(
        std::fs::read_to_string(&real_target).unwrap(),
        "git_protocol: ssh\n"
    );
}

// -----------------------------------------------------------------------
// #7166 review follow-up MEDIUM: the built directory and files must be
// owner-only (0700/0600), matching `gh`'s own `hosts.yml` convention.
// -----------------------------------------------------------------------

#[test]
#[cfg(unix)]
fn ensure_account_config_dir_sets_restrictive_permissions() {
    use std::os::unix::fs::PermissionsExt;

    let tmp = tempfile::TempDir::new().expect("tempdir");
    let state_root = tmp.path().join("state");
    let operator_dir = fake_operator_gh_config_dir(
        tmp.path(),
        TWO_ACCOUNT_HOSTS_YML,
        Some("git_protocol: ssh\n"),
    );

    let dir = ensure_account_config_dir(&state_root, &operator_dir, "bob-duetto").unwrap();

    let dir_mode = std::fs::metadata(&dir).unwrap().permissions().mode() & 0o777;
    assert_eq!(dir_mode, 0o700, "dir mode was {dir_mode:o}");

    let hosts_mode = std::fs::metadata(dir.join("hosts.yml"))
        .unwrap()
        .permissions()
        .mode()
        & 0o777;
    assert_eq!(hosts_mode, 0o600, "hosts.yml mode was {hosts_mode:o}");

    let config_mode = std::fs::metadata(dir.join("config.yml"))
        .unwrap()
        .permissions()
        .mode()
        & 0o777;
    assert_eq!(config_mode, 0o600, "config.yml mode was {config_mode:o}");
}