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
//! trusty-mpm's `~/.trusty-tools/trusty-mpm/config.yaml` settings + workspace-root
//! resolution (#1220).
//!
//! Why: #1220 introduces two related conventions for trusty-mpm. (1) the
//! cross-crate config convention `~/.trusty-tools/<crate>/config.yaml` — trusty-mpm
//! reads its slice (`~/.trusty-tools/trusty-mpm/config.yaml`) for the workspace-root
//! template, the auto-resume default, and the default model; (2) the managed-session
//! workspace-root default moves from `~/.trusty-mpm/workspaces/<project>/<id>/` to
//! `~/trusty-mpm-projects/<owner>/<repo>/<id>/`, deriving `<owner>/<repo>` from the
//! target repo's GitHub remote. Keeping the typed config AND the root resolver in one
//! module keeps the precedence rules (env > config > built-in default) in a single
//! tested place and well under the 500-SLOC cap.
//!
//! What: [`TrustyToolsConfig`] is the YAML-deserialised settings struct
//! (`workspace_root_template`, `auto_resume`, `default_model`), loaded via
//! [`TrustyToolsConfig::load`] (delegates to `trusty_common::crate_config`).
//! [`workspace_root`] resolves the absolute root for managed-session workspaces with
//! precedence **`TRUSTY_MPM_WORKSPACE_ROOT` env > config template > built-in default
//! (`~/trusty-mpm-projects`)**, expanding a leading `~`. [`workspace_subpath`] joins
//! the `<owner>/<repo>` identity onto that root.
//!
//! Test: `default_template_is_trusty_mpm_projects`, `env_overrides_config_and_default`,
//! `config_template_used_when_no_env`, `tilde_expansion`, `subpath_nests_owner_repo`.
use ;
use ;
use GithubPath;
use crateKeepList;
/// Untracked/secret file sync config shape + resolution (#2196), split into
/// its own module to keep this file under the 500-SLOC production cap. See
/// that module's doc for the full rationale.
pub use ;
/// The `agents:` group and its first occupant `agents.ticketing` (#6918),
/// split out for the same SLOC reason as `untracked_sync`. See that module's
/// doc for why two of its fields exist only to be refused.
pub use ;
/// Cloud log-drain config shape + resolution (#6535), split out for the same
/// SLOC reason as `untracked_sync`. See that module's doc for why a malformed
/// section is an error rather than a silent fall-back to defaults.
pub use ;
/// Crate name used as the `~/.trusty-tools/<crate>/` directory segment.
///
/// Why: the cross-crate convention keys each crate's config dir by its crate name;
/// a constant keeps trusty-mpm's segment from drifting.
/// What: `"trusty-mpm"`.
/// Test: `config_dir_is_trusty_mpm` indirectly via `crate_config` path tests.
pub const CRATE_NAME: &str = "trusty-mpm";
/// Sub-directory of `~/.trusty-tools/trusty-mpm/` that holds the shared,
/// tm-owned `CLAUDE_CONFIG_DIR` for every DAEMON-managed session (DOC-34).
///
/// Why: managed sessions must NOT read the target project's committed `.claude/`
/// (which ships a partial/broken agent roster and shadows the framework set,
/// #1996). Pointing the launched `claude` at a tm-owned config home under the
/// shared `~/.trusty-tools` base — the same base the #1220 crate-config
/// convention already uses — segregates the framework agents/skills/hooks/mcp
/// from whatever the project happens to commit. Naming the segment once keeps
/// the accessor and any future migration in agreement.
/// What: `"claude-config"`, joined onto `~/.trusty-tools/trusty-mpm/`.
/// Test: `managed_claude_config_dir_nests_under_trusty_tools`.
pub const MANAGED_CLAUDE_CONFIG_SUBDIR: &str = "claude-config";
// #5203: the layout constants live with the resolver in trusty-common so the
// crates that cannot depend on trusty-mpm read the same values. Re-exported
// here (rather than redeclared) to keep every existing `trusty_tools_config::`
// spelling working.
pub use ;
/// trusty-mpm's slice of the `~/.trusty-tools/<crate>/config.yaml` convention.
///
/// Why: gives operators a single declarative file for the settings #1220 calls out
/// — the workspace-root template, the auto-resume default, and the default model —
/// instead of relying solely on env vars or the legacy `~/.trusty-mpm/config.toml`
/// (which stays for its existing agent/model sections; this is the NEW, additive
/// YAML surface).
/// What: every field is optional so an absent file or absent key falls back to the
/// built-in default. `workspace_root_template` may contain a leading `~`.
/// Test: `config_template_used_when_no_env`, the `crate_config` round-trip tests.
// #5204: `#[non_exhaustive]` so adding a settings field stays non-breaking.
// Without it, every new `pub` field on this all-public struct is a
// `constructible_struct_adds_field` major under cargo-semver-checks — the
// #4088 class of break, which a workspace `cargo check` cannot catch because
// the root path override pairs local source with local dependency.
/// The `disk:` section of `~/.trusty-tools/trusty-mpm/config.yaml` (#6927).
///
/// Why: DOC-73 §16.2 makes the owner keep-list a first-class gate in
/// `worktree_reclaim::classify`, and a gate whose input is an operator
/// decision needs a declarative home. This is that home: one list, in the file
/// the console Config tab already reads and writes.
/// What: `keep_list` holds directories and globs. A directory entry keeps that
/// directory and everything under it, so naming a workspace keeps every
/// worktree inside it; an entry containing `*?[]{` compiles as a glob. See
/// [`crate::session_manager::worktree_keep_list::KeepList`] for the matching
/// rules and for what happens to a pattern that will not compile.
///
/// ```yaml
/// disk:
/// keep_list:
/// - ~/work/hotstats
/// - "**/scratch-*"
/// max_usage_pct: 90
/// ```
///
/// Test: `disk_config_yaml_round_trip`, `disk_keep_list_defaults_to_empty`,
/// `max_usage_pct_round_trips`.
/// The `daemon:` section of `~/.trusty-tools/trusty-mpm/config.yaml` (#1836).
///
/// Why: the ARIA incident showed that an MCP-triggered `session_new` call can
/// silently provision real infrastructure (clone + worktree + tmux + harness)
/// for any repo an LLM caller names, with no operator confirmation. The fix is
/// an explicit, declarative opt-in an operator sets once — rather than a
/// per-call flag a careless/compromised caller could always supply.
/// What: currently one toggle, `allow_mcp_spawn`; resolution precedence (env >
/// config > safe default) lives in
/// `crate::daemon::managed_routes::mcp_spawn_gate::mcp_spawn_enabled`, not
/// here — this is purely the on-disk shape.
/// Test: `daemon_config_yaml_round_trip`.
/// Built-in default tmux `history-limit`/`mouse` (#2398) and
/// `alternate-screen` (#5151), now the single shared source of truth in
/// `trusty_common::tmux` (#3004) so trusty-agents' independent tmux
/// implementation uses the identical defaults. Re-exported here so every
/// existing reference in this module keeps compiling unchanged.
/// Test: `tmux_options_default_when_no_config`.
pub use ;
/// Minimum tmux `history-limit` accepted from config (#2398 QA finding).
///
/// Why: `history-limit 0` does NOT mean "unlimited" in tmux — it means "keep
/// no scrollback beyond the visible screen", the exact opposite of what an
/// operator setting `tmux.history_limit: 0` almost certainly intended (this
/// is a common mental-model mismatch with tools where `0` conventionally
/// means unbounded). Rather than silently honouring a self-defeating value
/// that reintroduces the unscrollable-session problem #2398 exists to fix, a
/// configured value below this floor is clamped UP to it.
/// What: `1_000`. Deliberately below [`DEFAULT_TMUX_HISTORY_LIMIT`] so an
/// operator who intentionally wants a SMALL (but still usable) scrollback
/// can still dial it down — only the pathological `0` (or another
/// near-zero value) is corrected.
/// Test: `tmux_options_clamps_zero_history_limit`,
/// `tmux_options_clamps_below_minimum`.
pub const MIN_TMUX_HISTORY_LIMIT: u32 = 1_000;
/// The `tmux:` section of `~/.trusty-tools/trusty-mpm/config.yaml` (#2398,
/// extended by #5151).
///
/// Why: managed sessions inherited tmux's tiny default `history-limit`
/// (2000 lines), making long PM sessions unscrollable. Every field is
/// optional so an absent section (or a partly-filled one) falls back to the
/// built-in defaults — no regression for existing configs.
/// What: `history_limit` (scrollback lines retained per pane), `mouse`
/// (server-wide wheel-scroll/copy-mode toggle), and `alternate_screen`
/// (whether panes may use the terminal alternate screen buffer). Resolution
/// precedence (config > built-in default, clamped to
/// [`MIN_TMUX_HISTORY_LIMIT`]) lives in [`resolve_tmux_options`], not here —
/// this is purely the on-disk shape.
/// Test: `tmux_config_yaml_round_trip`.
///
/// `#[non_exhaustive]` (#5151): this section is designed to grow — it went from
/// two fields to three when `alternate_screen` landed, and adding a `pub` field
/// to an exhaustively-constructible struct is a SemVer break, because external
/// code can no longer use a struct literal. Marking it non-exhaustive makes
/// every future field addition non-breaking by construction. Downstream code
/// builds one via [`Default`] plus field assignment, or gets one from
/// [`TrustyToolsConfig::load`]; reading fields is unaffected. Same treatment
/// `trusty-common`'s `IndexOptions` got in #5065.
/// Resolved tmux scrollback + mouse ergonomics, after applying config
/// overrides on top of the built-in defaults (#2398).
///
/// Why: [`crate::daemon::tmux::TmuxDriver::apply_scrollback_options`] needs
/// concrete `u32`/`bool` values, not the optional on-disk shape; resolving
/// once here keeps the precedence logic in a single tested place.
/// What: `history_limit`, `mouse` and `alternate_screen`, all always
/// populated.
/// Test: `tmux_options_default_when_no_config`, `tmux_options_config_override`.
///
/// `#[non_exhaustive]` (#5151): tracks [`TmuxConfig`] field-for-field, so it
/// grows whenever that does — see its doc for why the attribute is here.
/// [`resolve_tmux_options`] is the only intended way to obtain one.
/// Resolve the effective tmux scrollback + mouse options for managed
/// sessions (#2398).
///
/// Why: gives every tmux-session-creating call site (normal spawn, resume/
/// restart, GUI spawn-mode, config-restart, and the CLI/TUI paths that create
/// sessions directly) a single answer, so a future config change cannot land
/// in one call site and be forgotten in another.
/// What: applies precedence **config override > built-in default**
/// ([`DEFAULT_TMUX_HISTORY_LIMIT`] / [`DEFAULT_TMUX_MOUSE`] /
/// [`DEFAULT_TMUX_ALTERNATE_SCREEN`]) for each field independently, then
/// clamps `history_limit` up to [`MIN_TMUX_HISTORY_LIMIT`] — a configured `0`
/// (or any other near-zero value) does NOT mean "unlimited" in tmux, so it is
/// corrected rather than honoured verbatim (#2398 QA finding).
/// `alternate_screen` is NOT clamped or corrected: both values are legitimate,
/// so a configured value is honoured verbatim. Its default is `false` (#5364),
/// which is deliberately NOT tmux's factory value — see
/// [`TmuxConfig::alternate_screen`] for the scrollback it buys and the
/// screen-restore behaviour it gives up server-wide.
/// Test: `tmux_options_default_when_no_config`, `tmux_options_config_override`,
/// `tmux_options_clamps_zero_history_limit`, `tmux_options_clamps_below_minimum`,
/// `tmux_options_alternate_screen_defaults_off`,
/// `tmux_options_alternate_screen_config_override`.
/// The `github:` section of `~/.trusty-tools/trusty-mpm/config.yaml` (#1265).
///
/// Why: a host may have several GitHub identities (a personal account, a work
/// account, a bot, a GitHub Enterprise host) and `tm`'s many `gh` calls must use
/// the RIGHT one for the active project rather than whichever identity happens to
/// be ambient. Binding the identity per-project — alongside the `watch:` section
/// that already keys defaults to the active board — lets an operator pin
/// "this project's gh actions use THIS account/token/host" declaratively. Every
/// field is optional so an absent section (or a partly-filled one) cleanly falls
/// back to the ambient gh identity, guaranteeing no regression for existing setups.
/// What: optional `config_dir` (a private gh config home), `token_env` (the NAME
/// of an env var or keychain reference to resolve a token from at call time —
/// never a plaintext token), `account` (a gh username), and `host` (the gh host,
/// default `github.com`, also used for clone-URL synthesis). The resolution
/// precedence and the env overrides each field maps to live in the
/// `gh_identity` module, not here — this is purely the on-disk shape.
/// Test: round-trips via the `crate_config` tests; resolution in `gh_identity`;
/// the no-plaintext-token guarantee is asserted by `github_config_stores_only_env_name`.
/// Built-in default GitHub host used for clone-URL synthesis and `GH_HOST`.
///
/// Why: when no `github.host` is configured, clone URLs and gh fall back to the
/// public host. Naming it once keeps the resolver and the URL synthesiser aligned.
/// What: `"github.com"`.
/// Test: `default_host_is_github_com` in `gh_identity`.
pub const DEFAULT_GITHUB_HOST: &str = "github.com";
/// The `watch:` section of `~/.trusty-tools/trusty-mpm/config.yaml`.
///
/// Why: `tm watch poll|listen` routes board issues to managed sessions; an
/// operator running it repeatedly (or from cron) wants to pin the board repo,
/// the routing label, and the poll interval once rather than passing them on
/// every invocation. Every field is optional so an absent section or key falls
/// back to the CLI flag's built-in default; CLI flags always override config.
/// What: optional `repo` (`owner/repo`), `label` (routing label), and
/// `interval_secs` (listen-mode poll cadence). Resolution precedence lives in
/// the `watch::args` module, not here — this is purely the on-disk shape.
/// Test: round-trips via the `crate_config` tests; precedence in `watch::args`.
/// A single project entry declared in the `projects:` YAML section (#1519 MR-1).
///
/// Why: operators who use a fixed set of repos want to declare them once in
/// `config.yaml` so the project registry is pre-populated on daemon startup
/// without requiring MCP `project_register` calls. Every field beyond `name`
/// and `repo_url` is optional so a minimal two-line entry is valid.
/// What: on-disk shape for one project declaration. The daemon reads these at
/// startup and upserts them into the `ProjectRegistry` via `seed_from_config`.
/// Test: `registry_seed_from_config` in the `project::registry` module.
/// Resolve the shared tm-owned `CLAUDE_CONFIG_DIR` for daemon-managed sessions.
///
/// Why: the daemon managed-spawn path must launch `claude` with a
/// `CLAUDE_CONFIG_DIR` that supplies the framework agent roster, skills, hooks,
/// and MCP servers from a tm-controlled location — NOT from the target
/// project's committed `.claude/` and NOT from the operator's `~/.claude`
/// (DOC-34 / #1996). This accessor is the single source of truth for that path
/// so the provisioner and the spawn-command builder can never disagree.
/// What: `~/.trusty-tools/trusty-mpm/claude-config/`, delegating to
/// [`trusty_common::crate_config::crate_config_dir`] for the `~/.trusty-tools/<crate>`
/// base and joining [`MANAGED_CLAUDE_CONFIG_SUBDIR`]. Returns `None` only when
/// the home directory cannot be resolved (a stripped CI environment).
/// Test: `managed_claude_config_dir_nests_under_trusty_tools`.
/// Resolve the managed `CLAUDE_CONFIG_DIR` under an explicit base (hermetic).
///
/// Why: the production accessor calls `dirs::home_dir()`, which unit tests must
/// not depend on. Pointing `base` at a `tempfile::TempDir` keeps the layout
/// assertion hermetic and parallel-safe.
/// What: `<base>/.trusty-tools/trusty-mpm/claude-config/`.
/// Test: `managed_claude_config_dir_nests_under_trusty_tools`.
/// Resolve the absolute workspace root for managed sessions.
///
/// Why: the spawn path needs ONE answer for "where do session workspaces live?"
/// with #1220's new default and an env/config override, in a tested place so the
/// HTTP route and the MCP tool cannot diverge.
/// What: #5203 moved the precedence chain itself into
/// [`trusty_common::workspace_layout::resolve_workspace_root`] so `trusty-code`
/// — which cannot depend on this crate — resolves the identical value. This
/// function is now the trusty-mpm-shaped façade over it: unwrap the template
/// from [`TrustyToolsConfig`] and delegate. The precedence is unchanged
/// (**`TRUSTY_MPM_WORKSPACE_ROOT` env > config `workspace_root_template` >
/// built-in `~/trusty-mpm-projects`**).
/// Test: `default_template_is_trusty_mpm_projects`, `env_overrides_config_and_default`,
/// `config_template_used_when_no_env`.
/// Resolve the session-worktree base directory name (#5204).
///
/// Why: the creation sites (`provisioner::workspace`,
/// `daemon::managed_routes::inproject`) need the single name new worktrees are
/// created under. Detection sites must use `worktree_dir_names` instead —
/// see that function for why the two differ.
/// What: delegates to [`trusty_common::workspace_layout::worktrees_dirname`],
/// which applies **`TRUSTY_MPM_WORKTREES_DIRNAME` env > config
/// `worktrees_dirname` > built-in `.worktrees`**.
/// Test: `worktrees_dirname_defaults_to_dot_worktrees`,
/// `worktrees_dirname_honours_config`.
/// The operator's keep-list patterns, or an empty list (#6927).
///
/// Why: named rather than read inline at each call site so the DEFAULT — no
/// `disk:` section means no kept worktrees — is stated once. The patterns stay
/// raw here; compiling them into a matcher is
/// [`KeepList::from_patterns`](crate::session_manager::worktree_keep_list::KeepList::from_patterns)'
/// job, which keeps this module free of the matching rules and free of any
/// dependency on the classifier.
/// What: `config.disk.keep_list`, or `[]`.
/// Test: `disk_keep_list_defaults_to_empty`, `disk_config_yaml_round_trip`.
/// Read the operator's keep-list, FAILING CLOSED on a config that will not
/// parse (#6927).
///
/// Why: [`TrustyToolsConfig::load`] collapses every YAML error to `Default`, by
/// design — a bad config must not stop the daemon starting. Applied to a
/// PROTECTIVE list that rule inverts its meaning: a typo in an unrelated
/// section left `disk` at `None`, produced an empty keep-list, and let
/// `tm session prune-worktrees --merged-prs` delete a worktree the operator had
/// vetoed, with only a `warn!` line to say so. This loader is why every
/// keep-list consumer now distinguishes "no `disk:` section" from "config
/// unreadable".
/// What: the fallible [`trusty_common::crate_config::load`], not
/// `load_or_default`. An absent file (or an unknown home) is an EMPTY list,
/// which is the documented default and permits. A read or parse failure builds
/// [`KeepList::unreadable`], which keeps every path — see that constructor.
/// Test: `an_unparseable_config_yields_an_unreadable_keep_list`,
/// `an_absent_config_yields_an_empty_keep_list`,
/// `a_valid_config_yields_the_operators_patterns`,
/// `a_malformed_config_refuses_to_reclaim_a_merged_clean_worktree`.
pub
/// [`load_disk_keep_list`] against an explicit home directory (hermetic).
///
/// Why: the production reader resolves `dirs::home_dir()`, which a test must
/// not depend on. Pointing `base` at a `tempfile::TempDir` is what lets the
/// fail-closed path be proved end to end — write a config there, run a reclaim
/// against a scratch worktree store, assert nothing was deleted.
/// What: `<base>/.trusty-tools/trusty-mpm/config.yaml`, with the same
/// absent/valid/unreadable three-way as [`load_disk_keep_list`].
/// Test: `an_unparseable_config_yields_an_unreadable_keep_list`,
/// `a_malformed_config_refuses_to_reclaim_a_merged_clean_worktree`.
pub
/// Join a project's `<owner>/<repo>` identity onto the workspace root.
///
/// Why: #1220 nests sessions under `<root>/<owner>/<repo>/` so multiple sessions
/// on the same repo group together and the path mirrors the GitHub identity.
/// What: returns `<root>/<owner>/<repo>` for the given [`GithubPath`]. The session
/// id is appended by the provisioner, not here, so this stays the "project home".
/// Test: `subpath_nests_owner_repo`.
/// Process-global lock serialising every test that mutates the workspace-root /
/// repos-root environment variables.
///
/// Why: `cargo test` runs the whole crate's tests in parallel threads sharing one
/// process env. Both this module's env tests AND
/// `crate::daemon::managed_routes::inproject`'s `TRUSTY_MPM_REPOS_ROOT` test mutate
/// `TRUSTY_MPM_WORKSPACE_ROOT` / `TRUSTY_MPM_REPOS_ROOT`; without a SHARED lock a
/// per-module mutex lets them race (one test's `remove_var` clobbers another's
/// `set_var`), causing flaky failures. Exposing ONE crate-wide lock guarantees
/// mutual exclusion across modules.
/// What: returns a guard over a single `static` mutex; poisoning is recovered so a
/// panicking test cannot deadlock the rest.
/// Test: used by the env-precedence tests here and in `inproject`.
pub
/// Pin `TRUSTY_MPM_WORKSPACE_ROOT` at an empty fixture dir for one test (#6551).
///
/// Why: `prune_worktrees_core` and `reconcile_worktrees_core` resolve the
/// worktree root through `TrustyToolsConfig::load()` on EVERY call, and with the
/// variable unset that resolves to `$HOME/trusty-mpm-projects` — the operator's
/// real fleet. Two consequences, both observed: the two transports enumerate
/// ~200 real worktrees instead of the `TempDir` fixture, and a sibling test that
/// sets or clears this same process-global variable between the HTTP call and
/// the RPC call splits the parity comparison. Pinning it removes both, because
/// the two calls then read one value that names an empty directory.
///
/// What: takes the crate-wide `env_test_lock` and sets the variable, restoring
/// the previous value (or removing it) on drop, unwinding panics included.
/// BOTH guards are needed and they cover different populations, exactly as
/// `prune::tests::prune_spares_a_stopped_records_workspace` records:
/// `env_test_lock` serialises the env-precedence tests, `#[serial_test::serial]`
/// serialises `connectors::tm_tests`, which mutates this variable under
/// `serial` alone. The lock is held across the awaited route calls on purpose —
/// releasing it earlier is the race.
///
/// `#[serial_test::file_serial]` is deliberately NOT used: the pinned resource
/// is a process environment variable, so under `cargo nextest`'s
/// process-per-test model (#4162) each test already has its own copy and there
/// is nothing shared across processes to serialise.
///
/// Test: `managed_prune_worktrees_enumerates_only_the_pinned_workspace_root`.
pub