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
//! Tests for [`super`] (#8534): the gitignored-output rule and its fail-safe.
//! Test: this file.

use std::path::Path;

use super::{
    ignored_entries, ignored_output_blocks_removal, ignored_output_refusal,
    inspect_dirt_with_ignored_output, is_harness_path, is_regenerable, kept_ignored_output,
};
use crate::session_manager::DirtyWorktreePolicy;
use crate::session_manager::worktree_git_fixture::GitWorktreeFixture;

/// Write `patterns` as the shared `info/exclude` of `fx`'s repository.
fn exclude(fx: &GitWorktreeFixture, patterns: &str) {
    std::fs::write(fx.repo.join(".git/info/exclude"), patterns).expect("write info/exclude");
}

/// Ignore `target/` and `results/` for every worktree of `fx`'s repository.
fn ignore_target_and_results(fx: &GitWorktreeFixture) {
    exclude(fx, "target/\nresults/\n");
}

/// Write `body` to `wt/rel`, creating its parent directories.
fn put(wt: &Path, rel: &str, body: &str) {
    let path = wt.join(rel);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).expect("mkdir");
    }
    std::fs::write(path, body).expect("write");
}

/// The name rule: only the entry's LAST component decides (critic round 2).
#[test]
fn rule_excuses_build_output_and_keeps_run_output() {
    for (entry, regenerable) in [
        ("target/", true),
        ("crates/x/target/", true),
        ("web/node_modules/", true),
        ("crates/a/ui/dist/", true),
        ("crates/a/ui/src-tauri/gen/", true),
        ("site/.nuxt/", true),
        (".parcel-cache/", true),
        ("infra/.terraform/", true),
        ("pkg/__pycache__/", true),
        ("mod.pyc", true),
        (".DS_Store", true),
        ("build/out.json", false),
        ("target/report.json", false),
        ("target-analysis/", false),
        ("target-worktree/", false),
        ("gen/", false),
        ("results/", false),
        ("run-output.json", false),
        (".env.local", false),
        ("targets/", false),
    ] {
        assert_eq!(is_regenerable(entry), regenerable, "{entry}");
    }
}

/// Critic round 3: framework and test caches, by last component only.
/// Fails at 26b7c15ca, which kept each of the `true` rows. The final round
/// dropped `.vercel`, `.output` and `.wrangler`: they hold kept files.
#[test]
fn tool_caches_added_in_round_3_are_regenerable() {
    for (entry, regenerable) in [
        (".vercel/", false),
        ("site/.astro/", true),
        (".output/", false),
        ("app/.angular/", true),
        (".expo/", true),
        ("docs/.docusaurus/", true),
        (".wrangler/", false),
        ("pkg/.dart_tool/", true),
        (".direnv/", true),
        (".hypothesis/", true),
        ("htmlcov/", true),
        (".eggs/", true),
        ("py/mypkg.egg-info/", true),
        ("web/.eslintcache", true),
        ("web/tsconfig.tsbuildinfo", true),
        (".coverage", true),
        ("htmlcov/index.html", false),
        (".vercel/output/notes.md", false),
        ("egg-info-notes/", false),
        (".coverage-report.json", false),
    ] {
        assert_eq!(is_regenerable(entry), regenerable, "{entry}");
    }
}

/// Harness bookkeeping is excused at the tree root only, never a look-alike.
/// Critic round 3: agents and skills are a ledger's call, not the name's —
/// only the ledgers themselves are harness files.
#[test]
fn harness_paths_are_excused_and_look_alikes_are_not() {
    for (rel, harness) in [
        (".trusty-mpm-worktree", true),
        (".trusty-mpm/", true),
        (".trusty-mpm/sessions/a.json", true),
        (".claude/settings.json", true),
        (".claude/settings.json.20260926T000000Z.bak", true),
        (".claude/settings.json.bak", true),
        (".claude/settings.json.lock", true),
        (".claude/settings.local.json", true),
        (".claude/agents/", false),
        (".claude/agents/engineer.md", false),
        (".claude/agents/.trusty-mpm-manifest.json", true),
        (".claude/agents/.trusty-mpm-manifest.json.lock", true),
        (".claude/skills/tm/SKILL.md", false),
        (".claude/skills/.trusty-mpm-skills-manifest.json", true),
        (".claude/skills/.trusty-mpm-project-tier-stamp", true),
        (".claude/skills/.trusty-mpm-mine/SKILL.md", false),
        (".claude/output-styles/trusty-mpm.md", true),
        (".claude/output-styles/x.tm-floor.md", true),
        (".claude/output-styles/mine.md", false),
        (".claude/commands/deploy.md", false),
        ("crates/a/.claude/settings.json", false),
        (".trusty-mpmx", false),
        ("results/out.json", false),
    ] {
        assert_eq!(is_harness_path(rel), harness, "{rel}");
    }
}

/// 🔴 #8540: a user file whose name merely starts with `settings.json` is run
/// output, not harness bookkeeping, so removal keeps it. Fails at a791b467ee,
/// whose `.claude/settings.json*` prefix excused all three.
#[test]
fn settings_json_look_alikes_are_not_harness() {
    for rel in [
        ".claude/settings.json-notes.md",
        ".claude/settings.jsonc",
        ".claude/settings.json.mine.bak",
    ] {
        assert!(!is_harness_path(rel), "{rel}");
    }
}

/// 🔴 #8540 follow-up: the `.claude/settings.local.json*` prefix excused a
/// user's look-alike, and the reap's `git worktree remove --force` deleted it.
/// Fails at e911f70771.
#[test]
fn settings_local_json_look_alikes_are_not_harness() {
    for rel in [
        ".claude/settings.local.json-notes.md",
        ".claude/settings.local.jsonc",
        ".claude/settings.local.json.mine",
    ] {
        assert!(!is_harness_path(rel), "{rel}");
    }
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("local-look-alike");
    exclude(&fx, ".claude/\n");
    put(&wt, ".claude/settings.local.json", "{}");
    put(&wt, ".claude/settings.local.json-notes.md", "keep me");
    let kept = kept_ignored_output(&wt)
        .expect("the check completes")
        .expect("the notes file is kept output");
    assert_eq!((kept.files, kept.first.as_str()), (1, ".claude/"));
}

/// 🔴 #8540 follow-up: a crashed atomic write's staging file is harness
/// output; a name that only resembles one is not. Fails at e911f70771, where
/// no `settings.json` staging name was excused.
#[test]
fn settings_staging_leftovers_are_harness() {
    for (rel, harness) in [
        (".claude/settings.json.tmp.4242.0", true),
        (".claude/settings.json.bak.4242.17", true),
        (".claude/settings.json.4242.3.tmp", true),
        (".claude/settings.local.json.tmp.4242.0", true),
        (".claude/settings.local.json.4242.3.tmp", true),
        (".claude/settings.local.json.bak", true),
        (".claude/settings.json.tmp", false),
        (".claude/settings.json.tmp.4242", false),
        (".claude/settings.json.tmp.notes.0", false),
        (".claude/settings.json.1.2.3.tmp", false),
        (".claude/settings.local.json.4242.x.tmp", false),
    ] {
        assert_eq!(is_harness_path(rel), harness, "{rel}");
    }
}

/// A gitignored results directory is kept output, counted file by file.
#[test]
fn kept_output_counts_files_in_an_ignored_results_dir() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("ignored-results");
    ignore_target_and_results(&fx);
    put(&wt, "results/run.json", "{}");
    put(&wt, "results/batch/part.json", "{}");

    let kept = kept_ignored_output(&wt)
        .expect("the check completes")
        .expect("results/ is kept output");

    assert_eq!(kept.files, 2);
    assert_eq!(kept.first, "results/");
}

/// Gitignored build output alone is nothing to keep.
#[test]
fn kept_output_is_none_for_build_output_only() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("ignored-target");
    ignore_target_and_results(&fx);
    put(&wt, "target/debug/bin", "elf");
    std::fs::create_dir_all(wt.join("results")).expect("an empty ignored dir");

    assert_eq!(kept_ignored_output(&wt), Ok(None));
}

/// 🔴 #8534 final round: `.vercel/` holds the pulled environment file, so the
/// gate keeps it. Fails at 3165ad29b, which excused `.vercel` by name.
#[test]
fn a_vercel_dir_holding_an_env_file_keeps_the_tree() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("vercel-env");
    exclude(&fx, ".vercel\n");
    put(&wt, ".vercel/project.json", "{}");
    put(&wt, ".vercel/.env.production.local", "API_KEY=x\n");

    let kept = kept_ignored_output(&wt)
        .expect("the check completes")
        .expect(".vercel/ is kept output");
    assert_eq!((kept.files, kept.first.as_str()), (2, ".vercel/"));
}

/// 🔴 critic round 2: `build/out.json` under a TRACKED `build/` is run output.
/// Fails at 199c2e447, which excused any path with a `build` component.
#[test]
fn an_ignored_file_under_a_tracked_build_dir_is_kept() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("tracked-build");
    put(&wt, "build/keep.txt", "tracked\n");
    GitWorktreeFixture::commit_all_and_push(&wt, "track build/");
    exclude(&fx, "build/*.json\n");
    put(&wt, "build/out.json", "{}");

    let kept = kept_ignored_output(&wt)
        .expect("the check completes")
        .expect("build/out.json is kept output");
    assert_eq!(kept.first, "build/out.json");
}

/// 🔴 critic round 2: a `target-*` directory is excused by cargo's own marker,
/// never by its name. Fails at 199c2e447, which excused `target-analysis/`.
#[test]
fn tagged_cache_dirs_are_excused_by_content_not_name() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("tagged-caches");
    exclude(&fx, "target-*/\n");
    put(
        &wt,
        "target-8510/CACHEDIR.TAG",
        "Signature: 8a477f597d28d172789f06886806bc55\n# cargo\n",
    );
    put(&wt, "target-8510/debug/app", "elf");
    put(&wt, "crates/py/.pytest_cache/.gitignore", "*\n");
    put(
        &wt,
        "crates/py/.pytest_cache/CACHEDIR.TAG",
        "Signature: 8a477f597d28d172789f06886806bc55\n# pytest\n",
    );
    put(&wt, "crates/py/.pytest_cache/v/cache/lastfailed", "{}");
    assert_eq!(kept_ignored_output(&wt), Ok(None), "tagged caches only");

    put(&wt, "target-analysis/x", "a user's notes");
    let kept = kept_ignored_output(&wt)
        .expect("the check completes")
        .expect("target-analysis/ is kept output");
    assert_eq!(kept.first, "target-analysis/");
}

/// Critic round 3: a `.rustc_info.json` is a file anyone can leave; only the
/// tag excuses a directory. Fails at 26b7c15ca, which excused it.
#[test]
fn a_rustc_info_file_alone_does_not_excuse_a_directory() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("rustc-info-only");
    exclude(&fx, "target-*/\n");
    put(&wt, "target-copy/.rustc_info.json", "{}");
    put(&wt, "target-copy/report.json", "{}");

    let kept = kept_ignored_output(&wt)
        .expect("the check completes")
        .expect("an untagged directory is kept output");
    assert_eq!((kept.files, kept.first.as_str()), (2, "target-copy/"));
}

/// A `CACHEDIR.TAG` without the spec's signature does not excuse its directory.
#[test]
fn a_cachedir_tag_without_the_signature_is_not_a_cache() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("forged-tag");
    exclude(&fx, "runs/\n");
    put(&wt, "runs/CACHEDIR.TAG", "not a cache\n");
    put(&wt, "runs/out.json", "{}");

    let kept = kept_ignored_output(&wt).expect("the check completes");
    assert_eq!(kept.map(|k| k.files), Some(2));
}

/// A wholly ignored `.claude/` holding only harness files and a ledger-recorded
/// agent is nothing to keep; a note beside them is.
#[test]
fn a_wholly_ignored_claude_dir_counts_only_non_harness_files() {
    use crate::core::agent_manifest::{AgentManifest, ManifestEntry, Origin, checksum};
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("ignored-claude");
    exclude(&fx, ".claude/\n");
    put(&wt, ".claude/settings.json", "{}");
    put(&wt, ".claude/agents/engineer.md", "# agent");
    let mut ledger = AgentManifest::default();
    ledger.managed.insert(
        "engineer.md".to_string(),
        ManifestEntry {
            source_chain: vec!["engineer".to_string()],
            checksum: checksum("# agent"),
            deployed_at: "2026-09-26T00:00:00Z".to_string(),
            origin: Origin::Bundled,
        },
    );
    ledger
        .save(&wt.join(".claude/agents"))
        .expect("save ledger");
    assert_eq!(kept_ignored_output(&wt), Ok(None));

    put(&wt, ".claude/notes.md", "keep me");
    let kept = kept_ignored_output(&wt)
        .expect("the check completes")
        .expect(".claude/notes.md is kept output");
    assert_eq!((kept.files, kept.first.as_str()), (1, ".claude/"));
}

/// A directory git cannot answer for is an error, never "nothing kept".
#[test]
fn kept_output_errors_when_git_cannot_answer() {
    let dir = tempfile::tempdir().expect("tempdir");
    let err = kept_ignored_output(dir.path()).expect_err("git status fails outside a repo");
    assert!(err.contains("git"), "{err}");
}

/// Critic round 3: a name git would quote is classified like any other. The
/// pinned `core.quotePath=false` already spells non-ASCII raw, but git still
/// quotes a `"`, a backslash or a control character. A build directory under
/// such a name lets removal proceed; run output under one is kept. Fails at
/// 26b7c15ca, where every quoted name was an `Err`.
#[test]
fn non_ascii_names_are_classified_not_refused() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("non-ascii");
    exclude(&fx, "node_modules/\nrésultats/\n*.json\n");
    put(&wt, "café/node_modules/pkg/index.js", "x");
    put(&wt, "we\"ird/node_modules/pkg/index.js", "x");
    assert_eq!(kept_ignored_output(&wt), Ok(None));

    put(&wt, "résultats/sortie.txt", "x");
    put(&wt, "a\"b.json", "{}");
    put(&wt, "tab\tname.json", "{}");
    let kept = kept_ignored_output(&wt)
        .expect("the check completes")
        .expect("run output under a quoted name is kept");
    assert_eq!(kept.files, 3, "{kept:?}");
    assert!(
        ["résultats/", "a\"b.json", "tab\tname.json"].contains(&kept.first.as_str()),
        "{kept:?}"
    );
}

/// A rename record's source path is not an entry, even one spelled `!! `.
#[test]
fn ignored_entries_skips_a_rename_source() {
    let status = "R  new.rs\0!! old.rs\0!! results/\0 M lib.rs\0!! é/\0";
    assert_eq!(ignored_entries(status), ["results/", "é/"]);
}

/// Fail-safe: an unreadable kept directory is an error, and the tree is kept.
#[test]
fn kept_output_errors_on_an_unreadable_directory() {
    use std::os::unix::fs::PermissionsExt;
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("unreadable-results");
    ignore_target_and_results(&fx);
    put(&wt, "results/sub/out.json", "{}");
    let sub = wt.join("results/sub");
    std::fs::set_permissions(&sub, std::fs::Permissions::from_mode(0o000)).expect("chmod");

    let result = kept_ignored_output(&wt);

    std::fs::set_permissions(&sub, std::fs::Permissions::from_mode(0o755)).expect("chmod back");
    let err = result.expect_err("an unreadable directory cannot be counted");
    assert!(err.contains("unreadable"), "{err}");
}

/// Fail-safe: a failed check keeps the tree and says why.
#[test]
fn refusal_keeps_the_tree_when_the_check_fails() {
    let dir = tempfile::tempdir().expect("tempdir");
    let reason = ignored_output_refusal(dir.path()).expect("a failed check must refuse");
    assert!(reason.contains("check failed"), "{reason}");
    assert!(
        reason.contains(&dir.path().display().to_string()),
        "{reason}"
    );
    assert!(ignored_output_blocks_removal(dir.path(), DirtyWorktreePolicy::Skip).is_some());
}

/// Only the explicit discard opt-in lets gitignored output go.
#[test]
fn force_discard_policy_lets_gitignored_output_go() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("discard-policy");
    ignore_target_and_results(&fx);
    put(&wt, "results/out.json", "{}");

    assert!(ignored_output_blocks_removal(&wt, DirtyWorktreePolicy::Skip).is_some());
    assert_eq!(
        ignored_output_blocks_removal(&wt, DirtyWorktreePolicy::ForceDiscard),
        None
    );
}

/// The `tm pr cleanup` probe: kept output is dirt, so an ahead-only reading —
/// which cleanup defers to a merge check — no longer passes. Fails at
/// 199c2e447, where the probe was `inspect_dirt` alone.
#[test]
fn probe_counts_ignored_output_as_dirt() {
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("probe-ahead");
    GitWorktreeFixture::commit_unpushed(&wt);
    ignore_target_and_results(&fx);
    put(&wt, "results/out.json", "{}");

    let dirt = inspect_dirt_with_ignored_output(&wt).expect("kept output is dirt");
    assert_eq!(dirt.dirty_files, 1, "{dirt:?}");
    assert!(dirt.unpushed_commits > 0, "{dirt:?}");
    assert!(dirt.reason.contains("results/"), "{dirt:?}");

    std::fs::remove_dir_all(wt.join("results")).expect("clear results");
    let plain = inspect_dirt_with_ignored_output(&wt).expect("still ahead");
    assert_eq!(plain.dirty_files, 0, "only the unpushed commit: {plain:?}");
}

/// The probe fails toward the "could not read" arm when its own check fails.
#[test]
fn probe_fails_toward_dirty_when_the_ignored_check_fails() {
    use std::os::unix::fs::PermissionsExt;
    let fx = GitWorktreeFixture::new();
    let wt = fx.add_worktree("probe-unreadable");
    ignore_target_and_results(&fx);
    put(&wt, "results/sub/out.json", "{}");
    let sub = wt.join("results/sub");
    std::fs::set_permissions(&sub, std::fs::Permissions::from_mode(0o000)).expect("chmod");

    let dirt = inspect_dirt_with_ignored_output(&wt);

    std::fs::set_permissions(&sub, std::fs::Permissions::from_mode(0o755)).expect("chmod back");
    let dirt = dirt.expect("a failed check is dirt");
    assert_eq!(
        (dirt.dirty_files, dirt.unpushed_commits),
        (0, 0),
        "{dirt:?}"
    );
    assert!(dirt.reason.contains("check failed"), "{dirt:?}");
}