trusty-mpm 1.7.3

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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! Unit tests for `standalone::trust_seed` — split out to keep the production
//! module under the 500-SLOC cap (mirrors the `native_mcp_tests.rs` /
//! `mcp_config_tests.rs` split pattern already used in this crate).
//! What: coverage for [`super::preseed_managed_trust`] — trust-dialog
//! marking, `enabledMcpjsonServers` seeding, idempotency, malformed-JSON
//! quarantine, the isolation invariant (never writes `$HOME/.claude*`), the
//! WI-8/#3918 builtin-name inclusion, and the issue #3934 regression: an
//! untrusted manifest disabling the `trusty-memory`/`trusty-search` injector
//! combined with a spoofed `.mcp.json` entry must not leave the name
//! pre-approved, while a legitimate operator toggle still launches cleanly.
//! Test: this file IS the test module.

use super::*;
use tempfile::TempDir;

/// Every timestamped quarantine sibling currently sitting in `cfg`.
///
/// Why (issue #4206): the quarantine name is no longer the fixed
/// `.claude.json.corrupt`, so tests must match the
/// `.claude.json.corrupt-<timestamp>` family by prefix. Centralising the match
/// keeps the "how many quarantine events happened" question answerable in one
/// place.
/// What: file names under `cfg` starting with `.claude.json.corrupt`, sorted
/// for deterministic assertion messages.
fn quarantine_files(cfg: &Path) -> Vec<String> {
    let mut found: Vec<String> = std::fs::read_dir(cfg)
        .expect("config dir must be readable")
        .flatten()
        .filter_map(|e| e.file_name().to_str().map(str::to_owned))
        .filter(|n| n.starts_with(".claude.json.corrupt"))
        .collect();
    found.sort();
    found
}

/// Build a `.claude.json` whose `projects` map is exactly `entries`.
///
/// Why: the prune tests (issue #4206) all need a pre-populated config with
/// hand-crafted project entries; inlining the JSON assembly in each obscured
/// what each test was actually varying.
/// What: writes `{"projects": {<entries>}}` to `<cfg>/.claude.json`.
fn write_config_with_projects(cfg: &Path, entries: serde_json::Value) {
    let config = serde_json::json!({ "projects": entries });
    std::fs::write(
        cfg.join(".claude.json"),
        serde_json::to_string_pretty(&config).unwrap(),
    )
    .unwrap();
}

/// Read back `projects` from `<cfg>/.claude.json`.
fn read_projects(cfg: &Path) -> serde_json::Map<String, serde_json::Value> {
    let text = std::fs::read_to_string(cfg.join(".claude.json")).unwrap();
    let val: serde_json::Value = serde_json::from_str(&text).unwrap();
    val["projects"].as_object().cloned().unwrap_or_default()
}

/// A `projects` entry carrying only the four keys the seeder itself writes —
/// i.e. what a leaked test-fixture entry looks like on disk.
fn seeder_only_entry() -> serde_json::Value {
    serde_json::json!({
        "hasTrustDialogAccepted": true,
        "hasCompletedProjectOnboarding": true,
        "projectOnboardingSeenCount": 1,
        "enabledMcpjsonServers": ["trusty-mpm"],
    })
}

// WI-3 TRUST-SEED: preseed_managed_trust must write trust keys for the workspace.
#[test]
fn test_preseed_managed_trust_marks_directory() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = tmp.path().join("projects").join("my-repo").join("repo");
    std::fs::create_dir_all(&workspace).unwrap();

    preseed_managed_trust(&cfg, &workspace).unwrap();

    let text = std::fs::read_to_string(cfg.join(".claude.json")).unwrap();
    let val: serde_json::Value = serde_json::from_str(&text).unwrap();

    let key = workspace.to_string_lossy().to_string();
    let proj = val["projects"][&key].as_object().unwrap();
    assert_eq!(
        proj.get("hasTrustDialogAccepted"),
        Some(&serde_json::Value::Bool(true)),
        "hasTrustDialogAccepted must be true"
    );
    assert_eq!(
        proj.get("hasCompletedProjectOnboarding"),
        Some(&serde_json::Value::Bool(true)),
        "hasCompletedProjectOnboarding must be true"
    );
    assert!(
        proj.get("projectOnboardingSeenCount")
            .and_then(|v| v.as_i64())
            .is_some_and(|n| n >= 1),
        "projectOnboardingSeenCount must be >= 1"
    );
}

// WI-3 TRUST-SEED idempotency: calling preseed_managed_trust twice must not
// corrupt the file or duplicate entries.
#[test]
fn test_preseed_managed_trust_is_idempotent() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = tmp.path().join("my-repo");
    std::fs::create_dir_all(&workspace).unwrap();

    preseed_managed_trust(&cfg, &workspace).unwrap();
    let after_first = std::fs::read_to_string(cfg.join(".claude.json")).unwrap();

    preseed_managed_trust(&cfg, &workspace).unwrap();
    let after_second = std::fs::read_to_string(cfg.join(".claude.json")).unwrap();

    assert_eq!(
        after_first, after_second,
        "preseed_managed_trust must be idempotent: two calls must produce identical output"
    );
}

// WI-3 TRUST-SEED: existing keys must be preserved (trust seed must not
// clobber unrelated data already in the config file).
#[test]
fn test_preseed_managed_trust_preserves_other_keys() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = tmp.path().join("repo");
    std::fs::create_dir_all(&workspace).unwrap();

    // Pre-populate .claude.json with unrelated data.
    std::fs::write(
        cfg.join(".claude.json"),
        r#"{"someOAuthToken":"keep-me","otherField":99}"#,
    )
    .unwrap();

    preseed_managed_trust(&cfg, &workspace).unwrap();

    let text = std::fs::read_to_string(cfg.join(".claude.json")).unwrap();
    let val: serde_json::Value = serde_json::from_str(&text).unwrap();

    assert_eq!(
        val.get("someOAuthToken").and_then(|v| v.as_str()),
        Some("keep-me"),
        "someOAuthToken must be preserved after trust seed"
    );
    assert_eq!(
        val.get("otherField").and_then(|v| v.as_i64()),
        Some(99),
        "otherField must be preserved after trust seed"
    );
}

// WI-3 ISOLATION: preseed_managed_trust must NEVER write anything outside
// `<claude_config_dir>`.
//
// This test uses two complementary strategies:
//
// 1. Sentinel-root check: `cfg` is a subdirectory of `root`; we assert
//    nothing lands at the `root` level (outside `cfg`). This proves the
//    function only writes through the `claude_config_dir` argument.
//
// 2. Fake-HOME redirect (serial_test): we redirect `HOME` to a second
//    empty temp dir and assert it remains empty after the call. Because
//    the test is serialised with `#[serial]`, the env mutation is sound —
//    parallel Rust test threads cannot observe the changed `HOME`.
//
// Together the two checks cover both "writes through argument" and
// "never writes through $HOME" without unsound parallel env mutation.
#[serial_test::serial]
#[test]
fn test_preseed_managed_trust_no_home_write() {
    /// RAII guard that restores $HOME to its original value on drop (including panic).
    struct HomeGuard(Option<String>);
    impl Drop for HomeGuard {
        fn drop(&mut self) {
            match self.0 {
                Some(ref p) => unsafe { std::env::set_var("HOME", p) },
                None => unsafe { std::env::remove_var("HOME") },
            }
        }
    }

    // Strategy 1: sentinel-root guard.
    let root = TempDir::new().unwrap();
    let cfg = root.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = root.path().join("repo");
    std::fs::create_dir_all(&workspace).unwrap();

    // Strategy 2: redirect $HOME to an empty temp dir; assert it stays empty.
    let fake_home = TempDir::new().unwrap();
    // SAFETY: test is serial (#[serial_test::serial]), so no other test thread
    // reads HOME concurrently during this function body. The HomeGuard restores
    // HOME even if an assertion below panics.
    let _home_guard = {
        let prev = std::env::var("HOME").ok();
        unsafe { std::env::set_var("HOME", fake_home.path()) };
        HomeGuard(prev)
    };

    preseed_managed_trust(&cfg, &workspace).unwrap();

    // --- Strategy 1 assertions (sentinel-root) ---

    // The expected output file must exist inside cfg.
    assert!(
        cfg.join(".claude.json").exists(),
        "preseed_managed_trust must write .claude.json inside claude_config_dir"
    );

    // No .claude.json should exist directly under root (outside cfg).
    assert!(
        !root.path().join(".claude.json").exists(),
        "preseed_managed_trust must NOT write .claude.json outside claude_config_dir \
         (isolation invariant)"
    );

    // No .claude directory should exist directly under root (outside cfg).
    assert!(
        !root.path().join(".claude").exists(),
        "preseed_managed_trust must NOT create .claude/ outside claude_config_dir \
         (isolation invariant)"
    );

    // --- Strategy 2 assertions (fake-HOME stays empty) ---

    // The fake home directory must contain no .claude.json and no .claude/
    // sub-directory (the two locations Claude Code reads global config from).
    assert!(
        !fake_home.path().join(".claude.json").exists(),
        "preseed_managed_trust must NOT write .claude.json to $HOME \
         (isolation invariant)"
    );
    assert!(
        !fake_home.path().join(".claude").exists(),
        "preseed_managed_trust must NOT create $HOME/.claude/ \
         (isolation invariant)"
    );

    // _home_guard drops here and restores HOME.
}

// WI-3 TRUST-SEED robustness: a pre-existing malformed .claude.json must be
// quarantined (renamed to .claude.json.corrupt) and seeding must proceed from
// a fresh `{}` — so the managed session never gets stuck in a permanent
// "trust dialog" loop because of a corrupt file.
#[test]
fn test_preseed_managed_trust_quarantines_malformed_json() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = tmp.path().join("repo");
    std::fs::create_dir_all(&workspace).unwrap();

    // Write deliberately malformed JSON into .claude.json.
    std::fs::write(cfg.join(".claude.json"), b"{ this is not valid json !!!").unwrap();

    // preseed_managed_trust must succeed (no error).
    preseed_managed_trust(&cfg, &workspace).unwrap();

    // The quarantine sibling must exist (corrupt file was renamed, not deleted).
    // Issue #4206: the name is now TIMESTAMPED (`.claude.json.corrupt-<stamp>`)
    // rather than the fixed `.claude.json.corrupt`, so match by prefix.
    let quarantined = quarantine_files(&cfg);
    assert_eq!(
        quarantined.len(),
        1,
        "exactly one timestamped quarantine file must exist after malformed-JSON \
         quarantine, found: {quarantined:?}"
    );

    // The new .claude.json must be valid JSON containing the seeded trust keys.
    let text = std::fs::read_to_string(cfg.join(".claude.json"))
        .expect(".claude.json must exist after quarantine + fresh seed");
    let val: serde_json::Value =
        serde_json::from_str(&text).expect(".claude.json must be valid JSON after fresh seed");

    let key = workspace.to_string_lossy().to_string();
    let proj = val["projects"][&key]
        .as_object()
        .expect("projects.<workspace> must be an object");
    assert_eq!(
        proj.get("hasTrustDialogAccepted"),
        Some(&serde_json::Value::Bool(true)),
        "hasTrustDialogAccepted must be true after quarantine + fresh seed"
    );
    assert_eq!(
        proj.get("hasCompletedProjectOnboarding"),
        Some(&serde_json::Value::Bool(true)),
        "hasCompletedProjectOnboarding must be true after quarantine + fresh seed"
    );
}

// ─── issue #4206: bounded growth (the prune) ──────────────────────────────

// Issue #4206 TEST 2 — a `projects` entry whose directory is DEFINITIVELY
// absent (a plain ENOENT: the path simply does not exist) and which carries
// nothing but the four keys this seeder writes is a leaked test-fixture
// dropping. It must be removed during the read-modify-write that is already
// happening, so the file can shrink instead of only ever growing. Before this
// fix `preseed_managed_trust` had no removal path at all and the reporting
// operator's config had reached 2,541 entries, 2,445 of them tempdir paths.
#[test]
fn prune_drops_entry_when_directory_definitively_absent() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = tmp.path().join("live-repo");
    std::fs::create_dir_all(&workspace).unwrap();

    // A path that has never existed and whose parent DOES exist, so the OS
    // reports a clean ENOENT rather than anything ambiguous.
    let vanished = tmp.path().join("vanished-fixture-dir");
    assert!(!vanished.exists(), "precondition: the path must not exist");

    write_config_with_projects(
        &cfg,
        serde_json::json!({
            vanished.to_string_lossy(): seeder_only_entry(),
            workspace.to_string_lossy(): seeder_only_entry(),
        }),
    );

    preseed_managed_trust(&cfg, &workspace).unwrap();

    let projects = read_projects(&cfg);
    assert!(
        !projects.contains_key(vanished.to_string_lossy().as_ref()),
        "a pure-seeder entry for a definitively-absent directory must be pruned: {:?}",
        projects.keys().collect::<Vec<_>>()
    );
    assert!(
        projects.contains_key(workspace.to_string_lossy().as_ref()),
        "the live workspace entry must survive the prune"
    );
}

// Issue #4206 TEST 3 — THE ANTI-OVER-DELETION TEST, and the one that matters
// most. An entry whose path cannot be resolved for an AMBIGUOUS reason must be
// KEPT. "The directory did not stat" is NOT the same claim as "the directory
// does not exist": an unmounted volume, a down network mount, a permission
// error, or an I/O fault all fail to stat while the user's real project is
// perfectly intact behind them. Deleting on that signal would silently destroy
// live trust state. This project shipped two bugs in the opposite direction
// (an ambiguous observation treated as a definite negative) on the same day
// this fix was written, so the prune treats ONLY a clean `NotFound` as absent.
//
// The ambiguity here is produced by ENOTDIR — a regular FILE occupying what
// the entry key uses as a parent directory, so `symlink_metadata` on the child
// fails with a non-`NotFound` error. Chosen over a chmod-000 directory because
// it reproduces identically whether or not the test runs as root, so the
// assertion can never silently degrade into a vacuous pass in a container.
#[test]
fn prune_keeps_entry_when_path_error_is_ambiguous() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = tmp.path().join("live-repo");
    std::fs::create_dir_all(&workspace).unwrap();

    // A regular file standing where a directory would have to be.
    let blocker = tmp.path().join("not-a-directory");
    std::fs::write(&blocker, b"regular file").unwrap();
    let unreachable = blocker.join("project");

    // Assert the precondition this test depends on: the error really is
    // ambiguous, NOT NotFound. Without this the test could pass for the wrong
    // reason on a platform that reports ENOENT here.
    let err =
        std::fs::symlink_metadata(&unreachable).expect_err("stat through a regular file must fail");
    assert_ne!(
        err.kind(),
        std::io::ErrorKind::NotFound,
        "precondition: this path must fail AMBIGUOUSLY, not with NotFound — \
         otherwise this test is not exercising the over-deletion guard (got {err:?})"
    );

    write_config_with_projects(
        &cfg,
        serde_json::json!({
            unreachable.to_string_lossy(): seeder_only_entry(),
            workspace.to_string_lossy(): seeder_only_entry(),
        }),
    );

    preseed_managed_trust(&cfg, &workspace).unwrap();

    let projects = read_projects(&cfg);
    assert!(
        projects.contains_key(unreachable.to_string_lossy().as_ref()),
        "an entry whose path is unreachable for an AMBIGUOUS reason must be KEPT — \
         only a definitive NotFound may prune: {:?}",
        projects.keys().collect::<Vec<_>>()
    );
}

// Issue #4206 TEST 4 — an entry carrying Claude Code's OWN runtime state
// (`lastSessionId`, `lastCost`, `mcpServers`, `history`, …) represents real
// user work, not a seeder dropping, and must survive even when its directory
// is definitively gone. A user whose project moved or whose volume is detached
// must not silently lose their session history. Only 21 of the reporting
// operator's 2,541 entries carried such fields — they are precisely the ones
// worth protecting.
#[test]
fn prune_keeps_entry_with_runtime_fields() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = tmp.path().join("live-repo");
    std::fs::create_dir_all(&workspace).unwrap();

    let gone = tmp.path().join("moved-away-project");
    assert!(!gone.exists(), "precondition: the path must not exist");

    write_config_with_projects(
        &cfg,
        serde_json::json!({
            gone.to_string_lossy(): {
                "hasTrustDialogAccepted": true,
                "hasCompletedProjectOnboarding": true,
                "projectOnboardingSeenCount": 3,
                "enabledMcpjsonServers": ["trusty-mpm"],
                // The distinguishing signal: real Claude Code runtime state.
                "lastSessionId": "abc-123",
                "lastCost": 4.2,
                "mcpServers": { "custom": { "command": "x" } },
            },
            workspace.to_string_lossy(): seeder_only_entry(),
        }),
    );

    preseed_managed_trust(&cfg, &workspace).unwrap();

    let projects = read_projects(&cfg);
    let kept = projects
        .get(gone.to_string_lossy().as_ref())
        .expect("an entry with Claude Code runtime fields must be KEPT even when its path is gone");
    assert_eq!(
        kept.get("lastSessionId").and_then(|v| v.as_str()),
        Some("abc-123"),
        "the preserved entry must keep its runtime state intact, not just its key"
    );
    assert!(
        kept.get("mcpServers").is_some(),
        "mcpServers must survive the prune"
    );
}

// ─── issue #4206: legible quarantine ──────────────────────────────────────

// Issue #4206 TEST 5 — two quarantine events must produce TWO distinct files.
// Both writers used the fixed name `.claude.json.corrupt`, so a second
// corruption silently overwrote the first one's bytes — erasing the only
// record of the first failure in a file that also holds OAuth state. A
// post-mortem could tell that corruption had happened at least once, and
// nothing more.
#[test]
fn two_quarantine_events_produce_two_distinct_files() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().join("claude-config");
    std::fs::create_dir_all(&cfg).unwrap();
    let workspace = tmp.path().join("repo");
    std::fs::create_dir_all(&workspace).unwrap();

    // First corruption + quarantine.
    std::fs::write(cfg.join(".claude.json"), b"{ first corruption !!!").unwrap();
    preseed_managed_trust(&cfg, &workspace).unwrap();
    assert_eq!(
        quarantine_files(&cfg).len(),
        1,
        "first quarantine must produce exactly one file"
    );

    // Second, independent corruption + quarantine.
    std::fs::write(cfg.join(".claude.json"), b"{ second corruption ???").unwrap();
    preseed_managed_trust(&cfg, &workspace).unwrap();

    let files = quarantine_files(&cfg);
    assert_eq!(
        files.len(),
        2,
        "two quarantine events must leave TWO distinct records, not overwrite one: {files:?}"
    );

    // And both sets of original bytes must still be recoverable — the whole
    // point of quarantining rather than deleting.
    let bodies: Vec<String> = files
        .iter()
        .map(|f| std::fs::read_to_string(cfg.join(f)).unwrap())
        .collect();
    assert!(
        bodies.iter().any(|b| b.contains("first corruption")),
        "the FIRST failure's bytes must survive the second quarantine: {bodies:?}"
    );
    assert!(
        bodies.iter().any(|b| b.contains("second corruption")),
        "the second failure's bytes must be preserved too: {bodies:?}"
    );
}

/// // #4181 (ADR-0042): the seeder writes NO MCP approval.
///
/// Why: an approved name makes a workspace `.mcp.json` entry of that name win
/// over the operator's own user-scope declaration — the displacement the
/// #3918→#3950 chain kept re-defusing. Removing the approval removes it.
/// What: seeds a fresh config dir and asserts the trust keys are present and
/// `enabledMcpjsonServers` is absent.
/// Test: this is the test.
#[test]
fn test_preseed_managed_trust_writes_no_mcp_approval() {
    let tmp = tempfile::tempdir().unwrap();
    let cfg = tmp.path().join("cfg");
    let workspace = tmp.path().join("ws");
    std::fs::create_dir_all(&workspace).unwrap();

    preseed_managed_trust(&cfg, &workspace).unwrap();

    let projects = read_projects(&cfg);
    let entry = &projects[&workspace.to_string_lossy().to_string()];
    assert_eq!(entry["hasTrustDialogAccepted"], serde_json::json!(true));
    assert!(
        entry.get("enabledMcpjsonServers").is_none(),
        "no MCP name may be pre-approved: {entry}"
    );
}

/// // #4181: a stale approval a prior version wrote is REMOVED.
///
/// Why: ceasing to write leaves the key in place on every machine an older tm
/// launched, so the displacement stays live exactly where a repo could reach
/// it. The strip is what makes the invariant real rather than forward-only.
/// What: seeds over an entry that already carries a full approval list and a
/// higher onboarding count, and asserts the key is gone while Claude Code's own
/// runtime state in the same entry survives.
/// Test: this is the test.
#[test]
fn test_preseed_managed_trust_strips_a_stale_mcp_approval() {
    let tmp = tempfile::tempdir().unwrap();
    let cfg = tmp.path().join("cfg");
    let workspace = tmp.path().join("ws");
    std::fs::create_dir_all(&workspace).unwrap();
    std::fs::create_dir_all(&cfg).unwrap();
    write_config_with_projects(
        &cfg,
        serde_json::json!({
            workspace.to_string_lossy().to_string(): {
                "hasTrustDialogAccepted": true,
                "hasCompletedProjectOnboarding": true,
                "projectOnboardingSeenCount": 4,
                "enabledMcpjsonServers": ["trusty-mpm", "evil-server"],
                "lastSessionId": "abc-123"
            }
        }),
    );

    preseed_managed_trust(&cfg, &workspace).unwrap();

    let projects = read_projects(&cfg);
    let entry = &projects[&workspace.to_string_lossy().to_string()];
    assert!(
        entry.get("enabledMcpjsonServers").is_none(),
        "a stale approval must be stripped, not merely left unwritten: {entry}"
    );
    assert_eq!(entry["lastSessionId"], serde_json::json!("abc-123"));
    assert_eq!(entry["projectOnboardingSeenCount"], serde_json::json!(4));
}

/// Writer threads the #4076 concurrency regression drives, and how many
/// distinct workspaces each one seeds.
///
/// Why: a lost update only manifests when two read → mutate → write cycles
/// actually interleave, and every seed must target a DIFFERENT workspace key —
/// re-seeding one key in a loop makes a clobber self-healing, because the next
/// round re-adds what the last one dropped and the final state converges.
/// Production writes each entry exactly once (one workspace, one session), so a
/// clobber there is permanent; distinct keys reproduce that. 8 × 40 = 320
/// one-shot cycles over one file failed on every observed pre-fix run while
/// keeping the test well under a second — the same shape and constants as
/// `session_launch::tests_claude_json_concurrency_4072`, which covers the two
/// seeders #4072 fixed.
const CONCURRENT_WRITERS: usize = 8;
const CONCURRENT_ROUNDS: usize = 40;

/// Why (issue #4076): `preseed_managed_trust` was the third `.claude.json`
/// seeder carrying the unguarded load → mutate → store cycle that #4072 fixed
/// for the other two. The daemon calls it once per session it provisions, from
/// independent tokio tasks, so two overlapping cycles silently drop one
/// session's whole `projects.<workspace>` entry — the operator then meets the
/// trust dialog this function exists to dismiss, with both writes reporting
/// success and nothing logged.
///
/// What: drives the REAL seeder against ONE managed `.claude.json` from
/// multiple threads, which is the only way to prove the fix — single-threaded
/// coverage of this function passes both before and after the guard exists.
#[test]
fn concurrent_managed_seeds_preserve_every_workspace_entry() {
    let tmp = TempDir::new().unwrap();
    let cfg = tmp.path().to_path_buf();

    // Each workspace is a REAL directory: `prune_stale_project_entries` drops
    // pure seeder entries whose path is definitively absent, so keys that never
    // existed on disk would be pruned by a later round and misread as a lost
    // update that no guard could have prevented.
    let workspaces: Vec<std::path::PathBuf> = (0..CONCURRENT_WRITERS)
        .flat_map(|writer| {
            let cfg = cfg.clone();
            (0..CONCURRENT_ROUNDS).map(move |round| cfg.join(format!("ws-w{writer}-s{round}")))
        })
        .collect();
    for ws in &workspaces {
        std::fs::create_dir_all(ws).expect("workspace dir");
    }

    std::thread::scope(|scope| {
        for writer in 0..CONCURRENT_WRITERS {
            let (cfg, workspaces) = (&cfg, &workspaces);
            scope.spawn(move || {
                for round in 0..CONCURRENT_ROUNDS {
                    let ws = &workspaces[writer * CONCURRENT_ROUNDS + round];
                    preseed_managed_trust(cfg, ws).expect("a managed trust seed must not fail");
                }
            });
        }
    });

    let projects = read_projects(&cfg);
    let lost: Vec<String> = workspaces
        .iter()
        .map(|ws| ws.to_string_lossy().to_string())
        .filter(|key| !projects.contains_key(key))
        .collect();
    assert!(
        lost.is_empty(),
        "concurrent managed trust seeds lost {} of {} workspace entries — a \
         read-modify-write cycle stored a snapshot taken before a sibling's store \
         (issue #4076). Lost: {lost:?}",
        lost.len(),
        workspaces.len()
    );

    // Every surviving entry must carry its full payload, not merely its key.
    for ws in &workspaces {
        let entry = &projects[&ws.to_string_lossy().to_string()];
        assert_eq!(
            entry["hasTrustDialogAccepted"],
            serde_json::json!(true),
            "{} lost its trust acceptance",
            ws.display()
        );
        assert_eq!(
            entry["hasCompletedProjectOnboarding"],
            serde_json::json!(true),
            "{} lost its onboarding acceptance",
            ws.display()
        );
    }
}