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
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
//! Unit tests for [`super`] (project-tier trusty-owned hook combination,
//! issue #2003).
//!
//! Why: split out of `project_hooks.rs` to keep the production file focused,
//! mirroring the `hooks/{mod.rs,tests.rs}` split already used elsewhere in
//! this crate.
//! What: exercises `project_managed_hook_additions` (all three sources
//! combine, stable across repeated calls) and
//! `is_project_managed_hook_command` (recognises all three sources, rejects
//! foreign commands), plus end-to-end coverage of
//! `super::super::settings::write_project_hooks` proving the daemon
//! managed-launch path now writes the full lifecycle triad AND preserves a
//! pre-existing foreign hook.
//! Test: this module IS the test suite for `super`.

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

#[test]
fn project_managed_hook_additions_combines_all_three_sources() {
    let additions = project_managed_hook_additions(
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("a stable hook exe resolves in the test environment");
    let hooks = additions["hooks"]
        .as_object()
        .expect("hooks must be an object");

    // trusty-memory source.
    assert_eq!(
        hooks["UserPromptSubmit"][0]["hooks"][0]["command"],
        serde_json::json!("trusty-memory prompt-context")
    );

    // PreToolUse must carry BOTH the PM-guard group and the lifecycle-triad
    // group — neither source may clobber the other.
    let pre = hooks["PreToolUse"]
        .as_array()
        .expect("PreToolUse must be an array");
    assert_eq!(pre.len(), 2, "PreToolUse must carry guard + triad groups");
    let pre_commands: Vec<&str> = pre
        .iter()
        .map(|g| g["hooks"][0]["command"].as_str().unwrap())
        .collect();
    assert!(
        pre_commands.iter().any(|c| c.ends_with(" hook --pm-guard")),
        "PM-guard command must be present: {pre_commands:?}"
    );
    assert!(
        pre_commands.iter().any(|c| c.ends_with(" hook")),
        "lifecycle-triad command must be present: {pre_commands:?}"
    );

    // SessionStart must carry BOTH trusty-memory and the lifecycle triad.
    let session_start = hooks["SessionStart"]
        .as_array()
        .expect("SessionStart must be an array");
    assert_eq!(session_start.len(), 2);

    // Lifecycle-triad-only events (no trusty-memory / PM-guard overlap) must
    // still be present.
    for event in ["PostToolUse", "Stop", "SubagentStop", "SessionEnd"] {
        assert!(
            hooks
                .get(event)
                .and_then(serde_json::Value::as_array)
                .is_some_and(|a| !a.is_empty()),
            "{event} must carry the lifecycle-triad group"
        );
    }
}

#[test]
fn project_managed_hook_additions_is_stable_across_calls() {
    // Why: two independent calls must produce byte-identical output so the
    // caller's merge is idempotent across repeated `write_project_hooks` runs.
    let first = project_managed_hook_additions(
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("a stable hook exe resolves in the test environment");
    let second = project_managed_hook_additions(
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("a stable hook exe resolves in the test environment");
    assert_eq!(first, second);
}

/// Why (#5034): `[hooks] prompt_context = false` must drop the
/// `UserPromptSubmit` entry and NOTHING else. The regression this guards is a
/// toggle that also takes out `SessionStart` — the other `TRUSTY_MEMORY_HOOKS`
/// key, sharing the same constant and the same `trusty-memory ` command prefix.
/// What: builds the additions with the toggle off and asserts `UserPromptSubmit`
/// is absent while `SessionStart`, the PM guard, and all six lifecycle-triad
/// events are byte-identical to the enabled build.
#[test]
fn project_managed_hook_additions_omits_prompt_context_when_disabled() {
    let enabled = project_managed_hook_additions(
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("a stable hook exe resolves in the test environment");
    let disabled = project_managed_hook_additions(
        Some(std::path::Path::new("/usr/local/bin/tm")),
        false,
        false,
    )
    .expect("a stable hook exe resolves in the test environment");

    let off = disabled["hooks"].as_object().expect("hooks is an object");
    assert!(
        !off.contains_key("UserPromptSubmit"),
        "UserPromptSubmit must be absent when the toggle is off: {off:?}"
    );

    // Every other event must survive with its enabled-build value untouched.
    let on = enabled["hooks"].as_object().unwrap();
    for event in [
        "SessionStart",
        "PreToolUse",
        "PostToolUse",
        "Stop",
        "SubagentStop",
        "SessionEnd",
    ] {
        assert_eq!(
            off.get(event),
            on.get(event),
            "{event} must be unchanged by the prompt_context toggle"
        );
    }
    assert_eq!(
        off.len(),
        on.len() - 1,
        "exactly one event key may differ between the two builds"
    );
}

/// Why (#5034): the strip domain must not shrink with the toggle, or the
/// opt-out silently does nothing on any project already launched once.
/// What: asserts the owned-event list covers the key set of BOTH toggle
/// variants.
#[test]
fn project_managed_hook_events_is_a_superset_of_every_variant() {
    let owned = project_managed_hook_events();
    for enabled in [true, false] {
        for key in project_managed_hook_additions(
            Some(std::path::Path::new("/usr/local/bin/tm")),
            enabled,
            false,
        )
        .expect("a stable hook exe resolves in the test environment")["hooks"]
            .as_object()
            .unwrap()
            .keys()
        {
            assert!(
                owned.contains(key),
                "{key} (toggle={enabled}) must be in the owned strip domain: {owned:?}"
            );
        }
    }
    assert!(
        owned.iter().any(|e| e == "UserPromptSubmit"),
        "UserPromptSubmit must stay in the strip domain even though the \
         disabled build never writes it: {owned:?}"
    );
}

#[test]
fn is_project_managed_hook_command_recognises_all_three_sources() {
    assert!(is_project_managed_hook_command("trusty-mpm hook"));
    assert!(is_project_managed_hook_command("/opt/bin/tm hook"));
    assert!(is_project_managed_hook_command(
        "trusty-memory prompt-context"
    ));
    assert!(is_project_managed_hook_command("trusty-memory inbox-check"));
    assert!(is_project_managed_hook_command(
        "/opt/bin/tm hook --pm-guard"
    ));
    assert!(
        !is_project_managed_hook_command("claude-mpm hooks fire PreToolUse"),
        "a foreign command must never be recognised as project-managed"
    );
    assert!(
        !is_project_managed_hook_command("some-other-tool run"),
        "an unrelated command must never be recognised as project-managed"
    );
}

/// Why (issue #2003): this is the regression test for the bug report — a
/// managed session launched via `write_project_hooks` (the daemon path) must
/// end up with the lifecycle triad in the PROJECT-tier settings file, since
/// `--setting-sources project,local` never loads the user-tier file
/// `ensure_managed_hooks` provisions.
/// What: calls `write_project_hooks` and asserts all six triad events are
/// present with a `... hook` command.
#[test]
fn write_project_hooks_writes_lifecycle_triad() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("write succeeds");

    let value: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(project.join(".claude").join("settings.json")).unwrap(),
    )
    .unwrap();
    let hooks = value["hooks"].as_object().expect("hooks must be an object");
    for event in [
        "PreToolUse",
        "PostToolUse",
        "Stop",
        "SubagentStop",
        "SessionStart",
        "SessionEnd",
    ] {
        let groups = hooks
            .get(event)
            .and_then(serde_json::Value::as_array)
            .unwrap_or_else(|| panic!("{event} must be present"));
        assert!(
            groups.iter().any(|g| g["hooks"][0]["command"]
                .as_str()
                .is_some_and(|c| c.ends_with(" hook"))),
            "{event} must carry the lifecycle-triad `... hook` command"
        );
    }
}

/// Why (issue #2003, consistent with #2948's entry-level guarantee): a
/// project's own pre-existing, genuinely foreign hook must survive
/// `write_project_hooks` — the old REPLACE-the-whole-`hooks`-key behaviour
/// clobbered it.
/// What: seeds `settings.json` with a foreign `PreToolUse` group, calls
/// `write_project_hooks`, and asserts the foreign group is still present
/// alongside the newly-added PM-guard/triad groups.
#[test]
fn write_project_hooks_preserves_foreign_hooks() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let claude_dir = project.join(".claude");
    std::fs::create_dir_all(&claude_dir).unwrap();
    std::fs::write(
        claude_dir.join("settings.json"),
        serde_json::to_string_pretty(&serde_json::json!({
            "hooks": {
                "PreToolUse": [{
                    "matcher": "*",
                    "hooks": [{ "type": "command", "command": "claude-mpm hooks fire PreToolUse", "timeout": 5 }]
                }]
            }
        }))
        .unwrap(),
    )
    .unwrap();

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("write succeeds");

    let value: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(claude_dir.join("settings.json")).unwrap())
            .unwrap();
    let pre = value["hooks"]["PreToolUse"]
        .as_array()
        .expect("PreToolUse must be an array");
    let commands: Vec<&str> = pre
        .iter()
        .map(|g| g["hooks"][0]["command"].as_str().unwrap())
        .collect();
    assert!(
        commands.contains(&"claude-mpm hooks fire PreToolUse"),
        "the foreign entry must survive: {commands:?}"
    );
    assert!(
        commands.iter().any(|c| c.ends_with(" hook --pm-guard")),
        "the PM-guard entry must also be present: {commands:?}"
    );

    // Re-running must not duplicate the foreign entry or our own groups.
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("second write succeeds");
    let value2: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(claude_dir.join("settings.json")).unwrap())
            .unwrap();
    let pre2 = value2["hooks"]["PreToolUse"].as_array().unwrap();
    assert_eq!(
        pre2.len(),
        pre.len(),
        "re-running must not duplicate any group"
    );
}

/// Why (issue #2972): the final write must be a temp-file + rename, not a plain
/// `fs::write`, so a crash mid-write can never leave a torn/truncated
/// `settings.json`.
///
/// #7762 changed the observable proxy. The writer no longer calls
/// `write_json_atomic`, whose `<path>.bak` this test read as "the atomic path
/// ran" — that copy is now deliberately absent, because a `.bak` written on
/// every launch is untracked litter in the operator's project. The #7244
/// snapshot is the surviving record of the replaced file, written by the same
/// branch, so it proves the same thing.
/// What: writes twice, asserting no snapshot and no staging file after the first
/// write (the file did not exist yet), a snapshot byte-identical to the
/// pre-second-write content after the second, no `<path>.bak` and no leftover
/// staging file either time (the rename is what makes the write atomic), and
/// that the JSON payload itself is unchanged.
#[test]
fn write_project_hooks_writes_via_atomic_path() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let claude_dir = project.join(".claude");
    let settings_path = claude_dir.join("settings.json");
    let bak_path = claude_dir.join("settings.json.bak");

    // Every `.claude/` entry that is a snapshot of `settings.json`, by the same
    // rule the prune applies.
    let snapshots = || -> Vec<String> {
        std::fs::read_dir(&claude_dir)
            .expect("read .claude")
            .filter_map(Result::ok)
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|name| {
                crate::core::standalone::hooks::backup::is_snapshot_of("settings.json", name)
            })
            .collect()
    };
    // #7762: the publish stages under `<name>.<pid>.<seq>.tmp` and renames, so a
    // surviving `.tmp` means the rename did not happen.
    let staging = || -> Vec<String> {
        std::fs::read_dir(&claude_dir)
            .expect("read .claude")
            .filter_map(Result::ok)
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|name| name.ends_with(".tmp"))
            .collect()
    };

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("first write succeeds");
    assert!(settings_path.exists(), "settings.json must be created");
    assert!(
        snapshots().is_empty(),
        "no snapshot expected on first write (file did not previously exist): {:?}",
        snapshots()
    );
    assert!(
        staging().is_empty(),
        "the staging file must be renamed away, never left behind: {:?}",
        staging()
    );
    let first_content = std::fs::read_to_string(&settings_path).unwrap();

    // #7244 (round 3): the second call flips `divert_enabled` so the merged
    // value actually DIFFERS. Repeating the first call's arguments now takes
    // the no-op exit, which writes nothing — and a test asserting the atomic
    // path ran would then be asserting against a call that never reached it.
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        true,
    )
    .expect("second write succeeds");
    let taken = snapshots();
    assert_eq!(
        taken.len(),
        1,
        "the writer must snapshot the prior file before replacing it: {taken:?}"
    );
    assert!(
        staging().is_empty(),
        "the staging file must be renamed away, never left behind: {:?}",
        staging()
    );
    // #7762: a launch must not drop a `.bak` into the operator's project.
    assert!(!bak_path.exists(), "a launch must not drop a .bak");
    let backup_content = std::fs::read_to_string(claude_dir.join(&taken[0])).unwrap();
    assert_eq!(
        backup_content, first_content,
        "the snapshot must be a byte-identical copy of the pre-second-write content"
    );

    let value: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&settings_path).unwrap()).unwrap();
    assert!(value["hooks"]["SessionStart"].is_array());
}

/// Why (#5034): the end-to-end opt-out — `[hooks] prompt_context = false` must
/// leave no `UserPromptSubmit` entry in the file the launched session reads,
/// while every other hook trusty-mpm writes is still there. The measured cost
/// this buys back is ~1,211 tokens per prompt (#4904).
/// What: writes with the toggle off into a fresh project and asserts
/// `UserPromptSubmit` is absent, `SessionStart` still carries the
/// `trusty-memory inbox-check` group, `PreToolUse` still carries the PM guard,
/// and all six lifecycle-triad events are present.
#[test]
fn write_project_hooks_omits_prompt_context_when_disabled() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        false,
        false,
    )
    .expect("write succeeds");

    let value: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(project.join(".claude").join("settings.json")).unwrap(),
    )
    .unwrap();
    let hooks = value["hooks"].as_object().expect("hooks must be an object");

    assert!(
        !hooks.contains_key("UserPromptSubmit"),
        "UserPromptSubmit must not be written when disabled: {hooks:?}"
    );
    let raw = serde_json::to_string(&value).unwrap();
    assert!(
        !raw.contains("prompt-context"),
        "no prompt-context command may survive anywhere in the file: {raw}"
    );

    // The SessionStart trusty-memory group is a separate hook and must stay.
    let session_start = hooks["SessionStart"].as_array().unwrap();
    let ss_commands: Vec<&str> = session_start
        .iter()
        .map(|g| g["hooks"][0]["command"].as_str().unwrap())
        .collect();
    assert!(
        ss_commands.contains(&"trusty-memory inbox-check"),
        "SessionStart must keep the inbox-check hook: {ss_commands:?}"
    );

    // The PM guard and the full lifecycle triad must be unaffected.
    let pre_commands: Vec<&str> = hooks["PreToolUse"]
        .as_array()
        .unwrap()
        .iter()
        .map(|g| g["hooks"][0]["command"].as_str().unwrap())
        .collect();
    assert!(
        pre_commands.iter().any(|c| c.ends_with(" hook --pm-guard")),
        "the PM guard must still be registered: {pre_commands:?}"
    );
    for event in [
        "PreToolUse",
        "PostToolUse",
        "Stop",
        "SubagentStop",
        "SessionStart",
        "SessionEnd",
    ] {
        assert!(
            hooks[event]
                .as_array()
                .unwrap()
                .iter()
                .any(|g| g["hooks"][0]["command"]
                    .as_str()
                    .is_some_and(|c| c.ends_with(" hook"))),
            "{event} must still carry the lifecycle-triad command"
        );
    }
}

/// Why (#5034): this is the test that fails if the strip domain is derived from
/// the additions instead of the owned set. Every project in the wild has
/// already been launched with the hook enabled, so the entry is ALREADY in
/// `.claude/settings.json`; an opt-out that only stops re-writing it would
/// leave it firing forever and the config key would appear to do nothing.
/// What: writes once enabled (seeding the entry), then writes disabled, and
/// asserts the stale entry is gone while a foreign `UserPromptSubmit` entry
/// added by the operator survives.
#[test]
fn write_project_hooks_strips_stale_prompt_context_when_disabled() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let settings_path = project.join(".claude").join("settings.json");

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("enabled write succeeds");
    let seeded: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&settings_path).unwrap()).unwrap();
    assert_eq!(
        seeded["hooks"]["UserPromptSubmit"][0]["hooks"][0]["command"],
        serde_json::json!("trusty-memory prompt-context"),
        "precondition: the enabled write must have seeded the entry"
    );

    // The operator also has a hook of their own on the same event.
    let mut with_foreign = seeded.clone();
    with_foreign["hooks"]["UserPromptSubmit"]
        .as_array_mut()
        .unwrap()
        .push(serde_json::json!({
            "matcher": "",
            "hooks": [{ "type": "command", "command": "my-own-tool inject", "timeout": 5 }]
        }));
    std::fs::write(
        &settings_path,
        serde_json::to_string_pretty(&with_foreign).unwrap(),
    )
    .unwrap();

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        false,
        false,
    )
    .expect("disabled write succeeds");

    let value: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&settings_path).unwrap()).unwrap();
    let raw = serde_json::to_string(&value).unwrap();
    assert!(
        !raw.contains("trusty-memory prompt-context"),
        "the stale entry from the earlier enabled launch must be stripped: {raw}"
    );

    let remaining: Vec<&str> = value["hooks"]["UserPromptSubmit"]
        .as_array()
        .expect("the operator's own group keeps the event key alive")
        .iter()
        .map(|g| g["hooks"][0]["command"].as_str().unwrap())
        .collect();
    assert_eq!(
        remaining,
        vec!["my-own-tool inject"],
        "a foreign UserPromptSubmit entry must survive the strip"
    );
}

/// Why (#5034): the opt-out must be reversible — clearing the config key has to
/// restore the hook, not leave the project permanently stripped.
/// What: enabled → disabled → enabled, asserting the final file is
/// byte-identical to the first enabled write.
#[test]
fn write_project_hooks_re_enabling_restores_the_hook() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let settings_path = project.join(".claude").join("settings.json");

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("write 1");
    let first = std::fs::read_to_string(&settings_path).unwrap();
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        false,
        false,
    )
    .expect("write 2");
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("write 3");
    let third = std::fs::read_to_string(&settings_path).unwrap();

    assert_eq!(
        third, first,
        "re-enabling must reproduce the enabled write byte for byte"
    );
}

/// Why (#5034): the default path must not move. `#[hooks] prompt_context`
/// defaults to `true`, and the enabled write must be exactly what the
/// pre-toggle code produced — which, for the strip half of the change, means
/// the widened strip domain has to be a no-op when the hook is enabled.
/// What: asserts the config default is `true`, and that the enabled write is
/// unchanged whether the strip runs over the owned event set or over the
/// additions' own keys (the pre-#5034 derivation), on both a fresh project and
/// one already carrying a prior write.
#[test]
fn write_project_hooks_enabled_output_is_unchanged_by_the_toggle() {
    assert!(
        crate::core::config::MpmConfig::default()
            .hooks
            .prompt_context,
        "the shipped default must keep the hook enabled"
    );

    // The pre-#5034 strip domain was the additions' own key set. With the hook
    // enabled the two derivations must be identical, which is what makes the
    // default path byte-identical to before.
    let mut from_additions: Vec<String> = project_managed_hook_additions(
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("a stable hook exe resolves in the test environment")["hooks"]
        .as_object()
        .unwrap()
        .keys()
        .cloned()
        .collect();
    let mut owned = project_managed_hook_events();
    from_additions.sort();
    owned.sort();
    assert_eq!(
        owned, from_additions,
        "with the hook enabled the widened strip domain must be a no-op"
    );

    // And the write itself is stable across repeats, on a project that already
    // carries a prior launch's entries.
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let settings_path = project.join(".claude").join("settings.json");
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("write 1");
    let first = std::fs::read_to_string(&settings_path).unwrap();
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("write 2");
    assert_eq!(
        std::fs::read_to_string(&settings_path).unwrap(),
        first,
        "the enabled write must be idempotent"
    );
}

/// Why (#6887): the toggle must add EXACTLY the two `PreToolUse` groups it
/// promises and touch nothing else. A writer that `insert`ed `PreToolUse`
/// instead of appending would silently drop the PM guard and the lifecycle
/// triad — the failure mode this module's doc comment exists to prevent.
/// What: builds the additions with the toggle on and off, asserts `PreToolUse`
/// grows by exactly two groups (matcher `Read`, matcher `Bash`, both invoking
/// `hook --divert-check`), and asserts every OTHER event key is byte-identical
/// between the two builds.
#[test]
fn project_managed_hook_additions_includes_divert_when_enabled() {
    let off = project_managed_hook_additions(
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("a stable hook exe resolves in the test environment");
    let on =
        project_managed_hook_additions(Some(std::path::Path::new("/usr/local/bin/tm")), true, true)
            .expect("a stable hook exe resolves in the test environment");

    let off_pre = off["hooks"]["PreToolUse"].as_array().expect("array");
    let on_pre = on["hooks"]["PreToolUse"].as_array().expect("array");
    assert_eq!(
        on_pre.len(),
        off_pre.len() + 2,
        "the toggle must add exactly two PreToolUse groups"
    );
    assert_eq!(
        &on_pre[..off_pre.len()],
        &off_pre[..],
        "the pre-existing PreToolUse groups must be byte-identical and in place"
    );

    let added: Vec<&serde_json::Value> = on_pre[off_pre.len()..].iter().collect();
    let matchers: Vec<&str> = added
        .iter()
        .map(|g| g["matcher"].as_str().unwrap())
        .collect();
    assert_eq!(matchers, vec!["Read", "Bash"]);
    for group in &added {
        let cmd = group["hooks"][0]["command"].as_str().unwrap();
        assert!(
            cmd.ends_with(" hook --divert-check"),
            "the divert group must invoke the divert-check hook: {cmd}"
        );
        assert!(
            !cmd.contains("KEY") && !cmd.contains("TOKEN") && !cmd.contains("SECRET"),
            "no credential may appear in the hook command string: {cmd}"
        );
    }

    // Every other event key must be untouched by the toggle.
    let off_obj = off["hooks"].as_object().unwrap();
    let on_obj = on["hooks"].as_object().unwrap();
    assert_eq!(off_obj.len(), on_obj.len(), "no new event key may appear");
    for (event, value) in off_obj {
        if event == "PreToolUse" {
            continue;
        }
        assert_eq!(
            on_obj.get(event),
            Some(value),
            "{event} must be unchanged by the divert toggle"
        );
    }
}

/// Why (#6887): the feature is OPT-IN, so the default build must carry zero
/// diversion groups — a toggle that leaked one entry would divert every
/// project's reads without anyone asking.
/// What: with the toggle off, no hook command anywhere mentions
/// `--divert-check`.
#[test]
fn project_managed_hook_additions_omits_divert_when_disabled() {
    let off = project_managed_hook_additions(
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("a stable hook exe resolves in the test environment");
    let text = off.to_string();
    assert!(
        !text.contains("--divert-check"),
        "a disabled toggle must write no diversion hook: {text}"
    );
}

/// Why (#6887, the #2948 duplication lesson): the strip predicate must claim
/// the divert command, or `write_project_hooks` appends a second copy on every
/// relaunch and never removes one when the toggle flips back off. Note
/// `is_mpm_hook_command` does NOT cover it — that predicate requires the
/// command to end with exactly ` hook`.
/// What: the predicate accepts the divert command and still rejects a foreign
/// one.
#[test]
fn is_project_managed_hook_command_recognises_divert_check() {
    assert!(is_project_managed_hook_command(
        "/opt/bin/tm hook --divert-check"
    ));
    assert!(is_project_managed_hook_command("tm hook --divert-check"));
    assert!(
        !crate::core::standalone::hooks::is_mpm_hook_command("/opt/bin/tm hook --divert-check"),
        "the triad predicate must not claim it; the divert arm is what covers it"
    );
    assert!(!is_project_managed_hook_command(
        "claude-mpm hooks fire PreToolUse --divert"
    ));
}

/// Why (#6887): the end-to-end write, and the property N relaunches must hold —
/// exactly two divert groups, never 2N.
/// What: writes with the toggle on three times and asserts the file is stable
/// and carries exactly two `--divert-check` groups.
#[test]
fn write_project_hooks_writes_divert_groups_when_enabled() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let settings_path = project.join(".claude").join("settings.json");

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        true,
    )
    .expect("write 1");
    let first = std::fs::read_to_string(&settings_path).unwrap();
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        true,
    )
    .expect("write 2");
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        true,
    )
    .expect("write 3");
    assert_eq!(
        std::fs::read_to_string(&settings_path).unwrap(),
        first,
        "repeated launches must not duplicate the divert groups"
    );

    let value: serde_json::Value = serde_json::from_str(&first).unwrap();
    let pre = value["hooks"]["PreToolUse"].as_array().expect("array");
    let divert: Vec<&serde_json::Value> = pre
        .iter()
        .filter(|g| {
            g["hooks"][0]["command"]
                .as_str()
                .is_some_and(|c| c.ends_with(" hook --divert-check"))
        })
        .collect();
    assert_eq!(divert.len(), 2, "exactly two divert groups: {pre:?}");
    // The credential-free rule, asserted against what actually lands on disk.
    assert!(
        !first.contains("TRUSTY_DIVERT_WORKER_MODEL") && !first.contains("api_key"),
        "settings.json must carry no divert config and no credential: {first}"
    );
}

/// Why (#6887, the #5034 lesson applied to this toggle): flipping `[divert]
/// enabled` back to false must REMOVE what a prior launch wrote. Deriving the
/// strip domain from the toggled-down additions would leave the groups firing
/// forever.
/// What: writes with the toggle on, then off, and asserts no `--divert-check`
/// command survives while the PM guard and the lifecycle triad do.
#[test]
fn write_project_hooks_strips_stale_divert_when_disabled() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let settings_path = project.join(".claude").join("settings.json");

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        true,
    )
    .expect("enabled write");
    assert!(
        std::fs::read_to_string(&settings_path)
            .unwrap()
            .contains("--divert-check"),
        "precondition: the enabled write must land the groups"
    );

    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new("/usr/local/bin/tm")),
        true,
        false,
    )
    .expect("disabled write");
    let after = std::fs::read_to_string(&settings_path).unwrap();
    assert!(
        !after.contains("--divert-check"),
        "the disabled write must strip the stale divert groups: {after}"
    );

    // And it must strip ONLY those: the rest of the enabled-off build survives.
    let value: serde_json::Value = serde_json::from_str(&after).unwrap();
    let pre = value["hooks"]["PreToolUse"].as_array().expect("array");
    assert_eq!(
        pre.len(),
        2,
        "PM guard + lifecycle triad must remain: {pre:?}"
    );
}

// ── #7688: the prompt-feedback capture, end to end through the writer ───────

/// The enabled write must land one capture group on each of the two events, and
/// a relaunch must replace rather than append.
///
/// Why: `prompt_feedback_hooks_tests` asserts the ADDITIONS map; this asserts
/// what reaches `settings.json`, which is the artifact Claude Code reads. The
/// idempotence half is the #2948 failure mode — a strip that stops recognising
/// what the writer wrote grows a duplicate group per launch, and nothing in the
/// additions map can see that.
/// What: three identical enabled writes, then one `Stop` and one `SubagentStop`
/// capture group asserted on disk.
/// Test: this function IS the test.
#[test]
fn write_project_hooks_writes_prompt_feedback_when_enabled() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let settings_path = project.join(".claude").join("settings.json");

    for pass in 1..=3 {
        super::super::settings::write_project_hooks_with_prompt_feedback(
            project,
            Some(std::path::Path::new(TEST_EXE)),
            true,
            false,
            true,
        )
        .unwrap_or_else(|e| panic!("write {pass}: {e}"));
    }
    let written = std::fs::read_to_string(&settings_path).unwrap();

    let value: serde_json::Value = serde_json::from_str(&written).unwrap();
    for event in ["Stop", "SubagentStop"] {
        let groups = value["hooks"][event].as_array().expect("array");
        let captures: Vec<&serde_json::Value> = groups
            .iter()
            .filter(|g| {
                g["hooks"][0]["command"]
                    .as_str()
                    .is_some_and(|c| c.ends_with(" hook --prompt-feedback"))
            })
            .collect();
        assert_eq!(
            captures.len(),
            1,
            "exactly one {event} capture group after three launches: {groups:?}"
        );
    }
}

/// Turning the flag back off must REMOVE what a prior launch wrote.
///
/// Why (#5034, restated for this toggle): a strip domain derived from the
/// toggled-down additions leaves the groups firing forever, so a project that
/// disabled the feature keeps spawning a `tm` process at the end of every turn.
/// What: writes enabled, asserts the groups landed, writes disabled, and asserts
/// no capture command survives while the lifecycle triad's own `Stop` entry does.
/// Test: this function IS the test.
#[test]
fn write_project_hooks_strips_stale_prompt_feedback_when_disabled() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let settings_path = project.join(".claude").join("settings.json");

    super::super::settings::write_project_hooks_with_prompt_feedback(
        project,
        Some(std::path::Path::new(TEST_EXE)),
        true,
        false,
        true,
    )
    .expect("enabled write");
    assert!(
        std::fs::read_to_string(&settings_path)
            .unwrap()
            .contains("--prompt-feedback"),
        "precondition: the enabled write must land the groups"
    );

    super::super::settings::write_project_hooks_with_prompt_feedback(
        project,
        Some(std::path::Path::new(TEST_EXE)),
        true,
        false,
        false,
    )
    .expect("disabled write");
    let after = std::fs::read_to_string(&settings_path).unwrap();
    assert!(
        !after.contains("--prompt-feedback"),
        "the disabled write must strip the stale capture groups: {after}"
    );

    // And it must strip ONLY those: the lifecycle triad's `Stop` entry survives.
    let value: serde_json::Value = serde_json::from_str(&after).unwrap();
    let stop = value["hooks"]["Stop"].as_array().expect("array");
    assert_eq!(stop.len(), 1, "the lifecycle triad must remain: {stop:?}");
}

/// The pinned installed-looking binary every test in this module writes with.
const TEST_EXE: &str = "/usr/local/bin/tm";

/// Every timestamped snapshot of `settings.json` in `dir`, name-sorted.
///
/// Shares the prune's own inclusion rule rather than re-deriving it, so a
/// count here can never include a file the prune would not have touched — the
/// atomic writer's single-slot `settings.json.bak` in particular.
fn snapshot_names(dir: &std::path::Path) -> Vec<String> {
    let mut names: Vec<String> = std::fs::read_dir(dir)
        .expect("readable dir")
        .flatten()
        .map(|e| e.file_name().to_string_lossy().into_owned())
        .filter(|n| crate::core::standalone::hooks::backup::is_snapshot_of("settings.json", n))
        .collect();
    names.sort();
    names
}

/// Call the project-tier writer with the pinned test binary.
fn write(project: &std::path::Path, prompt_context: bool, divert: bool) {
    super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new(TEST_EXE)),
        prompt_context,
        divert,
    )
    .expect("write succeeds");
}

/// Why (#7244, round 3): this is the writer that damaged a real project's
/// `.claude/settings.json`. Its prior state has to survive the rewrite that
/// replaces it, and the atomic writer's one `.bak` slot cannot carry that —
/// the next launch overwrites it.
/// What: creates the file, rewrites it with a different toggle, and asserts one
/// snapshot exists holding the pre-rewrite bytes.
#[test]
fn write_project_hooks_snapshots_the_file_it_replaces() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let claude_dir = project.join(".claude");
    let settings_path = claude_dir.join("settings.json");

    write(project, true, false);
    let before = std::fs::read_to_string(&settings_path).unwrap();
    assert!(
        snapshot_names(&claude_dir).is_empty(),
        "nothing existed before the first write, so nothing was snapshotted"
    );

    write(project, true, true);

    let snapshots = snapshot_names(&claude_dir);
    assert_eq!(
        snapshots.len(),
        1,
        "expected one snapshot, got {snapshots:?}"
    );
    assert_eq!(
        std::fs::read_to_string(claude_dir.join(&snapshots[0])).unwrap(),
        before,
        "the snapshot must hold the file as it was before the rewrite"
    );
}

/// Why (#7244): every managed launch that changes the file adds a snapshot, so
/// an unbounded set would turn a long-lived project's `.claude/` into an
/// archive. Three is the kept depth.
/// What: four rewrites that each change the file, then asserts exactly three
/// snapshots survive and the FIRST one taken is the one gone — pruning the
/// newest would bound the set while discarding the copy an operator wants.
#[test]
fn write_project_hooks_prunes_snapshots_to_three() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let claude_dir = project.join(".claude");

    // Creates the file; no snapshot (nothing existed).
    write(project, true, false);
    // Four rewrites, each differing from the one before it.
    write(project, true, true);
    let oldest = snapshot_names(&claude_dir);
    assert_eq!(oldest.len(), 1, "the first rewrite snapshots once");
    write(project, false, true);
    write(project, false, false);
    write(project, true, false);

    let snapshots = snapshot_names(&claude_dir);
    assert_eq!(
        snapshots.len(),
        3,
        "four rewrites must leave exactly three snapshots, got {snapshots:?}"
    );
    assert!(
        !snapshots.contains(&oldest[0]),
        "the oldest snapshot must be the one pruned, still present in {snapshots:?}"
    );
}

/// Why (#7244): `prepare_session` calls this on EVERY managed launch, and
/// almost every call reproduces the bytes already on disk. Snapshotting those
/// would fill all three kept slots with copies of the current file within
/// three launches, evicting the one prior state worth keeping.
/// What: writes twice with identical arguments and asserts no snapshot, no
/// change to the file, and — the assertion that separates "returned early" from
/// "rewrote the same bytes" — no `settings.json.bak`, which
/// `write_json_atomic` creates on any call that reaches it.
#[test]
fn write_project_hooks_takes_no_snapshot_when_nothing_changes() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let claude_dir = project.join(".claude");
    let settings_path = claude_dir.join("settings.json");

    write(project, true, false);
    let before = std::fs::read_to_string(&settings_path).unwrap();

    write(project, true, false);

    assert!(
        !claude_dir.join("settings.json.bak").exists(),
        "an identical rewrite must return before the atomic write, which would \
         have left its own backup"
    );
    assert!(
        snapshot_names(&claude_dir).is_empty(),
        "an identical rewrite must take no snapshot"
    );
    assert_eq!(
        std::fs::read_to_string(&settings_path).unwrap(),
        before,
        "an identical rewrite must leave the file alone"
    );
}

/// Why (#7244): a refusal writes nothing, so there is no prior state at risk.
/// Snapshotting anyway would let a machine that cannot resolve `tm` evict a
/// real prior state, three launches at a time, while changing nothing.
/// What: seeds a settings file, hands the writer a refusal through the
/// resolution seam (a host with `tm` installed would otherwise have the PATH
/// fallback rescue any refused `exe_override`), and asserts the refusal
/// surfaced, no snapshot appeared, and the file is unchanged.
#[test]
fn write_project_hooks_takes_no_snapshot_when_the_exe_is_refused() {
    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let claude_dir = project.join(".claude");

    write(project, true, false);
    let before = std::fs::read_to_string(claude_dir.join("settings.json")).unwrap();

    let refusal = Err(
        crate::core::standalone::hooks::StableHookExeError::Ephemeral(std::path::PathBuf::from(
            "/x/target-7244/debug/deps/some_test-cd3ba8f0",
        )),
    );
    super::super::settings::write_project_hooks_with(project, refusal)
        .expect_err("a refusal must reach the caller, not be written");

    assert!(
        snapshot_names(&claude_dir).is_empty(),
        "a refused write must take no snapshot"
    );
    assert_eq!(
        std::fs::read_to_string(claude_dir.join("settings.json")).unwrap(),
        before,
        "a refused write must leave the file alone"
    );
}

/// Why (#7244, fail-closed): a rewrite whose prior state cannot be preserved
/// must not run. Writing anyway and warning about the snapshot repeats the
/// original defect — a writer proceeding past a step it could not complete.
/// What: makes `.claude/` unwritable so the snapshot's exclusive create fails,
/// then asserts `PrepError::HookSnapshot` came back naming the file, and the
/// file is byte-identical. Unix-only: the read-only directory bit is the
/// portable way to deny file creation.
#[cfg(unix)]
#[test]
fn write_project_hooks_aborts_when_the_snapshot_fails() {
    use std::os::unix::fs::PermissionsExt;

    let tmp = TempDir::new().unwrap();
    let project = tmp.path();
    let claude_dir = project.join(".claude");
    let settings_path = claude_dir.join("settings.json");

    write(project, true, false);
    let before = std::fs::read_to_string(&settings_path).unwrap();

    std::fs::set_permissions(&claude_dir, std::fs::Permissions::from_mode(0o500)).unwrap();
    let result = super::super::settings::write_project_hooks(
        project,
        Some(std::path::Path::new(TEST_EXE)),
        true,
        true,
    );
    // Restore before asserting, so a failed assertion still leaves a removable
    // temp dir behind.
    std::fs::set_permissions(&claude_dir, std::fs::Permissions::from_mode(0o700)).unwrap();

    let err = result.expect_err("an unsnapshottable rewrite must fail");
    assert!(
        matches!(
            err,
            crate::core::session_launch::PrepError::HookSnapshot { .. }
        ),
        "expected HookSnapshot, got {err:?}"
    );
    assert!(
        err.to_string().contains("settings.json"),
        "the error must name the file whose rewrite was abandoned, got: {err}"
    );
    assert_eq!(
        std::fs::read_to_string(&settings_path).unwrap(),
        before,
        "the settings file must be untouched when its snapshot could not be taken"
    );
}

/// A build-tree executable path the #7262 classifier must always reject.
const EPHEMERAL_EXE: &str = "/repo/target-7247/debug/deps/test_session_lifecycle-cd3ba8f03938239b";

/// The argv shapes this crate's writers produce must stay inside the list
/// [`crate::core::standalone::hooks::build_tree`] classifies (#7262).
///
/// Why: the classifier keeps its own copy of the argv vocabulary, and a writer
/// that grows a new sub-flag without adding it there goes back to being
/// invisible to `tm doctor` and unstrippable by the writer — the exact #7244
/// failure, one flag later. This test derives each shape from the writer's OWN
/// output rather than restating the literal, so the two cannot drift.
/// What: strips the resolved binary off each writer-produced command, re-attaches
/// the same argv to a build-tree binary, and asserts the classifier claims it.
#[test]
fn pm_guard_and_divert_commands_end_in_a_known_argv_tail() {
    use crate::core::standalone::hooks::{
        is_build_tree_hook_command, is_build_tree_statusline_command,
    };

    let bin = super::super::settings::resolve_statusline_binary();
    let mut produced: Vec<String> = vec![
        super::super::settings::pm_guard_hook_value()[0]["hooks"][0]["command"]
            .as_str()
            .expect("the PM-guard command is a string")
            .to_string(),
    ];
    for group in super::super::divert_hooks::divert_hook_groups() {
        produced.push(
            group["hooks"][0]["command"]
                .as_str()
                .expect("a divert command is a string")
                .to_string(),
        );
    }

    for cmd in &produced {
        let argv = cmd
            .strip_prefix(&bin)
            .unwrap_or_else(|| panic!("{cmd} must start with the resolved binary {bin}"));
        assert!(
            is_build_tree_hook_command(&format!("{EPHEMERAL_EXE}{argv}")),
            "argv {argv:?} is not in the #7262 classifier's vocabulary"
        );
    }

    let statusline = super::super::settings::resolve_statusline_command();
    let argv = statusline
        .strip_prefix(&bin)
        .unwrap_or_else(|| panic!("{statusline} must start with the resolved binary {bin}"));
    assert!(
        is_build_tree_statusline_command(&format!("{EPHEMERAL_EXE}{argv}")),
        "statusLine argv {argv:?} is not in the #7262 classifier's vocabulary"
    );
}

/// The tier marker recognises what only this writer writes.
///
/// Why (#7849): the marker decides whether a settings file is one tm's PROJECT
/// tier provisioned, which is what keeps the toggle-driven gap probe quiet on a
/// foreign project.
/// What: the PM guard and the `trusty-memory` block both answer yes.
/// Test: itself.
#[test]
fn the_tier_marker_recognises_the_pm_guard() {
    assert!(super::is_project_tier_marker_command(&format!(
        "/usr/local/bin/tm{PM_GUARD_SUFFIX}"
    )));
    assert!(super::is_project_tier_marker_command(
        "trusty-memory inbox-check"
    ));
}

/// The tier marker is NOT satisfied by a user-tier lifecycle command.
///
/// Why (#7849): `~/.claude/settings.json` carries the lifecycle triad and
/// nothing else. Treating it as project-tier would make `tm doctor` report the
/// whole project-tier hook set missing from it, on every machine.
/// What: a bare `<exe> hook` answers no, and so does a foreign command.
/// Test: itself.
#[test]
fn the_tier_marker_ignores_a_user_tier_lifecycle_command() {
    assert!(!super::is_project_tier_marker_command(
        "/usr/local/bin/tm hook"
    ));
    assert!(!super::is_project_tier_marker_command(
        "/opt/other-harness/run --stop"
    ));
}