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
//! Delegation authority — scan deployed agents and render a routing section.
//!
//! Why: the orchestrating Claude Code instance needs to know which agents are
//! deployed and what each one handles so it can route work correctly; that
//! list is dynamic (it depends on what was deployed) and must be regenerated
//! at every session start.
//! What: [`scan_agents`] reads every `.md` file in an agents directory, parses
//! its frontmatter, and returns one [`AgentSummary`] per deployable (non-base)
//! agent; [`generate_authority`] folds those summaries into a Markdown section
//! injected into the session launch instructions;
//! [`resolve_roster`] is THE roster resolver every consumer shares — the PM
//! prompt's delegation section, the `tm session start` count, and `tm doctor`
//! (#4588); [`deployed_roster_section`] renders what it returns (#4069), minus
//! the agents Claude Code already publishes in its own agent-type listing
//! ([`harness_loaded_agent_dirs`], #4513).
//! Test: `delegation_authority_tests.rs` covers scanning, foundation-file
//! exclusion and its near misses, the `name:`-required admission rule (#4711),
//! the empty directory, and both render branches.
use BTreeMap;
use ;
use parse_kv_line;
/// A deployed agent as advertised to the orchestrating instance.
///
/// Why: the delegation authority section needs a small, render-ready view of
/// each agent — name, role, what it handles, its foundation chain, and a model
/// hint — without exposing the full composed agent body.
/// What: the display fields parsed from a composed agent's frontmatter plus the
/// resolved `extends` chain (base-first, ending in the agent itself).
/// Test: exercised by every `scan_*` and `generate_*` test.
/// File name of the deploy manifest, excluded from agent scans.
const MANIFEST_FILE: &str = "manifest.json";
/// The minimal frontmatter fields a composed agent advertises.
///
/// Why: scanning only needs a handful of YAML keys; a tiny struct avoids a
/// full YAML dependency and keeps parsing explicit.
/// What: the display fields plus `extends` (composed agents normally carry no
/// `extends`, but it is parsed so a hand-written source dir still resolves).
/// Test: exercised indirectly by every `scan_*` test.
/// Whether a parsed value is a YAML block-scalar header rather than a value.
///
/// Why: `description: >` means "the value is on the following indented lines",
/// not "the value is `>`". Five bundled writing agents (`copyeditor`,
/// `pangram-editor`, `proofreader`, `writer`, `writing-critic`) author their
/// descriptions that way, and taking the marker literally rendered each one into
/// the PM prompt as a bare `>`.
/// What: matches `>` or `|` optionally followed by a chomping indicator (`-`,
/// `+`) and/or an explicit indentation digit, in either order — the full set of
/// YAML block-scalar headers.
/// Test: `block_scalar_description_is_folded`,
/// `literal_block_scalar_description_keeps_its_line_breaks`.
/// Consume a block scalar's body from the frontmatter line stream.
///
/// Why: the body is whatever follows the header, indented, up to the first line
/// that is not — which is how the closing `---` (column 0) terminates it without
/// a special case.
/// What: takes indented and blank lines from `lines`, then joins them. A folded
/// header (`>`) joins the lines with a space and turns a blank line into a line
/// break, per YAML folding; a literal header (`|`) preserves every line break.
/// Test: `block_scalar_description_is_folded`,
/// `literal_block_scalar_description_keeps_its_line_breaks`.
/// Parse the leading `---` frontmatter block of a Markdown document.
///
/// Why: composed agents store their metadata in a YAML-ish frontmatter block;
/// the scanner reads just the keys it needs without a YAML library.
/// What: if the document opens with a `---` line, collects `key: value` pairs
/// until the closing `---`; quotes are stripped and keys lower-cased. A value
/// that is a block-scalar header (`>`, `|`, with any chomping indicator) takes
/// the following indented lines as its value via [`read_block_scalar`]. A
/// document with no frontmatter yields an all-`None` result.
/// Test: `scan_finds_agents` (frontmatter present), `scan_handles_no_frontmatter`,
/// `block_scalar_description_is_folded`.
/// Whether a file stem names a foundation (non-delegatable) template.
///
/// Why (#4589): this is the ONLY rule that removes an agent from the roster, so
/// it must be narrow and it must exist in exactly one place. The `-` is the
/// whole point: `starts_with("base")` silently deletes `baseline-analyzer`,
/// `base64-decoder`, `basecamp-sync` and `based-reviewer` — ordinary names in
/// the project and `~/.claude/agents` tiers tm does not author, which is
/// precisely the failure mode the frontmatter `role:` rule was removed for.
/// Sharing the predicate with the asset-level gate keeps the shipped files and
/// the scanner from drifting apart the way the roster and its count did.
/// What: case-insensitive `base-` prefix on the file stem, OR the bare stem
/// `base` — so every bundled `BASE-*.md` matches, a hand-placed `base.md`
/// matches, and nothing else plausibly does.
/// Test: `scan_excludes_base_agents`,
/// `near_miss_base_names_are_not_treated_as_foundation_files`,
/// `bare_base_md_is_excluded_from_the_roster`,
/// `base_prefixed_near_misses_survive_the_bare_base_rule`.
pub
/// Scan `agents_dir` and return summaries for all non-base agents.
///
/// Why: the orchestrating CC instance needs to know which agents exist
/// and what they handle so it can route work correctly.
///
/// What: reads all .md files in agents_dir, parses frontmatter (name,
/// role, description, model, extends), excludes BASE-* files, files with no
/// `name:` frontmatter, and the manifest file, returns one AgentSummary per
/// deployable agent.
///
/// Two rules remove a file from the roster, and only two:
/// 1. [`is_foundation_file`] — the `BASE-*` / bare `base` FILE NAME convention.
/// 2. No frontmatter `name:` (#4711) — Claude Code dispatches by `name:`, so a
/// file lacking one is a prompt fragment that could never be delegated to.
/// The live case was `~/.claude/agents/base.md`, a "Base QA Instructions…
/// Appended to all QA agents" fragment that the stem fallback turned into a
/// delegatable agent called `base`.
///
/// Foundation templates are identified by [`is_foundation_file`] — the `BASE-*`
/// FILE NAME convention alone (#4589). An earlier revision also dropped any
/// agent whose frontmatter
/// `role:` began with `base`, which silently deleted three real, dispatchable
/// agents — `memory-manager`, `mpm-agent-manager`, `mpm-skills-manager` — from
/// every roster. `role:` is the wrong signal for the job: the roster is a union
/// over three tiers (see [`deployed_agent_dirs`]) and tm authors the frontmatter
/// in exactly one of them, so no asset edit can stop the same rule from eating
/// an operator's own agent in `<project>/.claude/agents` or `~/.claude/agents`.
/// The file-name convention is tm's own, applies to the files tm actually
/// ships, and is checked before the file is even read.
///
/// Test: `scan_finds_agents`, `scan_excludes_base_agents`,
/// `agent_with_a_base_role_but_no_base_filename_stays_in_the_roster`,
/// `bare_base_md_is_excluded_from_the_roster`,
/// `file_without_name_frontmatter_is_excluded_from_the_roster`,
/// `scan_empty_dir`
/// A roster scan's full result: what was found AND what was lost finding it.
///
/// Why (#5544): every roster scan is fail-open — an unreadable tier and an
/// unreadable agent file both degrade to "this file contributes nothing", which
/// is byte-identical to "the tier legitimately holds no agent". A caller that
/// only receives `Vec<AgentSummary>` therefore cannot tell a complete roster
/// from a truncated one, and a PM handed the truncated one routes around agents
/// that exist. Carrying the losses alongside the finds is what makes the
/// difference expressible at all.
/// What: `agents` is the same list [`scan_agents`] has always returned;
/// `unreadable` names every path that was present but could not be read — an
/// individual agent file, or the tier directory itself when enumeration failed
/// for any reason other than "not found".
/// Test: `an_unreadable_agent_file_is_reported_not_silently_dropped`,
/// `an_unreadable_tier_directory_is_reported_not_silently_dropped`.
/// [`scan_agents`], additionally reporting what the scan could not read.
///
/// Why: see [`RosterScan`] — this is the variant that keeps a truncated roster
/// distinguishable from a short one.
/// What: identical scanning and admission rules; a non-`NotFound` `read_dir`
/// failure records the DIRECTORY, and a per-file read failure records the FILE,
/// each at `error!` rather than being dropped in silence.
/// Test: `an_unreadable_agent_file_is_reported_not_silently_dropped`,
/// `an_unreadable_tier_directory_is_reported_not_silently_dropped`.
/// Build a base-first inheritance chain from a single `extends` parent.
///
/// Why: composed agents flatten their inheritance into one file and normally
/// carry no `extends`; when one is present we still record the immediate
/// parent so the rendered "Foundation" line is informative.
/// What: returns `[parent, name]` when an `extends` parent exists, otherwise
/// just `[name]`.
/// Test: `scan_finds_agents` asserts the single-element chain.
/// Generate the delegation authority Markdown section from summaries.
///
/// Why: injected into session launch instructions so the orchestrating
/// instance knows its delegation options.
///
/// What: produces a Markdown section listing each agent with name,
/// description, extends chain, and model hint.
///
/// Zero-information lines are omitted (#4069 review): `Role` is skipped when it
/// merely repeats the `###` heading (it falls back to `name` when unset, true
/// for ~14 of 40 deployed agents) and `Foundation` is skipped for a
/// single-element chain, which renders as the agent citing itself as its own
/// foundation (true for every composed agent, since composition flattens
/// `extends`). This section is re-emitted into EVERY PM session, so ~2 KB of
/// per-session noise is worth deleting.
///
/// `Handles` is omitted for the same reason, one level up: its text is the
/// agent's frontmatter `description`, which is byte-identical to the description
/// the harness already publishes for that agent in its own Agent-type catalog.
/// Re-emitting it made the roster a second copy of a list the session already
/// has. What the harness does NOT publish is `Role` and `Model`, so those are
/// the roster's whole contribution and they stay. `description` is still parsed
/// into [`AgentSummary`] — it is public API and other consumers read it.
///
/// Test: `generate_authority_nonempty`, `generate_authority_empty`,
/// `generate_authority_omits_self_referential_lines`,
/// `generate_authority_omits_the_harness_supplied_description`
/// The banner [`generate_authority_reporting`] prepends when the scan lost
/// entries. Asserted on by name so the wording can change without the gate
/// silently stopping to check anything.
pub const ROSTER_INCOMPLETE_MARKER: &str = "ROSTER INCOMPLETE";
/// [`generate_authority`], rendering an explicit incompleteness banner when the
/// scan could not read one or more paths.
///
/// Why (#5544): the composed prompt is the artifact whose truncation is
/// invisible — a PM handed a short roster reads it as the complete set and
/// routes around agents that exist. A `tracing::error!` records the loss where
/// nobody reading the prompt will see it, so the loss has to appear IN the
/// prompt. This is deliberately still fail-open: a launch must not be blocked by
/// one bad directory, but it must never claim a roster it does not have.
/// What: prepends a banner naming every unreadable path above the agent list,
/// then renders exactly as [`generate_authority`]. With `unreadable` empty the
/// output is byte-identical to the previous behaviour.
/// Test: `an_unreadable_agent_file_is_reported_not_silently_dropped`,
/// `an_unreadable_tier_directory_is_reported_not_silently_dropped`,
/// `generate_authority_is_unchanged_when_nothing_was_lost`.
/// [`generate_authority_reporting`], stating how many entries the harness's own
/// agent-type listing already carries so they need not be repeated.
///
/// Why (#4513): Claude Code injects an "Available agent types for the Agent
/// tool" listing covering every agent it loads, and that listing — not this
/// section — is what the Agent tool actually accepts. Rendering the same agents
/// again put two copies of one list in every PM context. What the harness cannot
/// publish is an agent it never loads, so those are the only entries this
/// section still owes the PM.
/// What: `harness_injected` is the count filtered out upstream by
/// [`roster_section_from_tiers`]. At zero the output is byte-identical to the
/// previous behaviour. Above zero the header points at the harness listing as
/// the routing surface and renders only `agents`; when `agents` is then empty,
/// it says so rather than claiming no agent is deployed.
/// Test: `harness_injected_agents_are_not_repeated_in_the_roster`,
/// `a_fully_harness_injected_roster_points_at_the_harness_listing`,
/// `generate_authority_is_unchanged_when_nothing_was_deduped`.
/// Agent directories a launched session can load composed agents from,
/// highest precedence first.
///
/// Why (#4069): the roster the PM must route to is whatever Claude Code will
/// actually load, and that is a union of tiers rather than a single directory.
/// Since #4409 every BUNDLED agent deploys into exactly one of them — the
/// tm-owned `CLAUDE_CONFIG_DIR/agents` — while `<project>/.claude/agents` holds
/// only agents the operator hand-placed (and, in future, project-custom
/// trusty-built agents), and `~/.claude/agents` holds whatever the operator's
/// own generic Claude Code install carries, which tm never writes to. All three
/// are still scanned because a session can legitimately resolve an agent from
/// any of them, and the project tier still WINS on a name collision. Prompt
/// composition only ever receives a `project_dir`, so it resolves all three
/// tiers here rather than depending on a launch-mode value it cannot see.
/// What: returns `<project>/.claude/agents`, then the active managed
/// `CLAUDE_CONFIG_DIR/agents` (the `CLAUDE_CONFIG_DIR` env var when set,
/// otherwise [`managed_claude_config_dir`](crate::core::trusty_tools_config::managed_claude_config_dir)), then
/// `FrameworkPaths::default().claude_agents_dir()`. Paths are returned whether
/// or not they exist; [`scan_agents`] treats an unreadable directory as empty.
/// Test: `deployed_agent_dirs_puts_project_tier_first`.
/// The tier ORDER behind [`deployed_agent_dirs`], with every environment
/// lookup hoisted out.
///
/// Why (#4946): the compiled prompt used to state a precedence no code read
/// (`~/.trusty-mpm/agents/`), because the real order lived only here and was
/// restated by hand elsewhere. The `tm-capabilities` skill now renders that
/// order instead of restating it, and the renderer must be reproducible — it
/// cannot call [`deployed_agent_dirs`], which reads `CLAUDE_CONFIG_DIR` and
/// the caller's home directory and so differs per machine. Splitting the pure
/// ordering out means the generated documentation and the runtime resolution
/// are the same three lines of code: reorder the tiers and the committed skill
/// drifts, failing `scripts/check_capabilities.sh`.
/// What: `<project>/.claude/agents`, then `<managed_config_dir>/agents` when
/// one is supplied, then `framework_claude_agents` verbatim. No I/O, no env.
/// Test: `deployed_agent_dirs_from_is_pure_and_ordered`,
/// `deployed_agent_dirs_from_skips_absent_managed_tier`.
/// The subset of [`deployed_agent_dirs`] that Claude Code loads agents from
/// ITSELF, and therefore already publishes in its own agent-type listing.
///
/// Why (#4513): the harness injects "Available agent types for the Agent tool"
/// into every session, covering every agent it loads. tm scans a WIDER set of
/// tiers than the harness reads, so the two lists overlap but do not coincide,
/// and the roster's only non-redundant content is the difference. The rule is
/// derived, not enumerated: an agent is redundant exactly when its file sits in
/// a tier the harness reads.
/// What: the project tier — Claude Code always reads `<project>/.claude/agents` —
/// plus ONE user tier, which is `$CLAUDE_CONFIG_DIR/agents` when that variable is
/// set and `~/.claude/agents` when it is not. The asymmetry with
/// [`deployed_agent_dirs`] is the whole point: under tm, `CLAUDE_CONFIG_DIR`
/// points at the managed config directory, so `~/.claude/agents` is a tier tm
/// scans and the harness never reads. Reads the raw env var rather than
/// `managed_claude_config_dir`, because the env var is what Claude Code reads.
/// Test: `harness_loaded_dirs_follow_claude_config_dir`,
/// `harness_loaded_dirs_fall_back_to_the_home_tier`.
/// [`harness_loaded_agent_dirs`] with every environment lookup hoisted out.
///
/// Why: the shipped entry point reads `CLAUDE_CONFIG_DIR` and the caller's home
/// directory, so its rule can only be tested by mutating process-global state.
/// What: `<project>/.claude/agents`, then `<claude_config_dir>/agents` when one
/// is supplied, otherwise `framework_claude_agents` verbatim. No I/O, no env.
/// Test: `harness_loaded_dirs_follow_claude_config_dir`,
/// `harness_loaded_dirs_fall_back_to_the_home_tier`.
/// Merge the agents found across `dirs` into one roster, earlier dirs winning.
///
/// Why: the tiers returned by [`deployed_agent_dirs`] overlap — the same agent
/// is normally deployed into more than one of them — so a naive concatenation
/// would advertise duplicates. Claude Code resolves a same-named agent by tier
/// precedence, and the rendered roster must describe the copy that actually
/// wins.
/// What: scans each directory in order and keeps the FIRST summary seen per
/// agent name, returning them name-sorted for a stable prompt. The dedup key is
/// LOWER-CASED — agent names are case-insensitive to Claude Code's dispatcher,
/// so `QA` in one tier and `qa` in another are one agent, not two.
/// Test: `roster_from_dirs_dedupes_with_first_dir_winning`,
/// `roster_from_dirs_dedup_is_case_insensitive`.
/// [`roster_from_dirs`], additionally reporting what the union could not read.
///
/// Why: see [`RosterScan`]. The union is where a per-tier loss stops being
/// attributable — one tier failing shrinks the merged roster by however many
/// agents only that tier carried, and nothing downstream can tell.
/// What: same first-tier-wins, case-insensitive dedup; concatenates every
/// tier's `unreadable` paths.
/// Test: `an_unreadable_tier_directory_is_reported_not_silently_dropped`.
/// THE agent roster for `project_dir`. Every consumer calls this one function.
///
/// Why (#4588): a roster resolved twice is a roster that drifts. The PM prompt
/// took the three-tier union while `tm session start` printed a count taken
/// from a single directory, so the operator was told 34 when the PM had been
/// given 39 — the two numbers were never wrong in the same way at the same
/// time, because they were never the same computation. There is now exactly one
/// implementation, and `tm session start`, the delegation section injected into
/// the PM prompt, and `tm doctor` all read it.
/// What: unions [`deployed_agent_dirs`] via [`roster_from_dirs`], name-sorted
/// and deduplicated with the highest-precedence tier winning.
/// Test: `session_start_count_matches_the_delivered_delegation_roster`
/// (instruction_pipeline_tests) is the cross-consumer agreement gate; the
/// tier-union behaviour itself is covered by the `roster_from_dirs_*` tests.
/// This function consults machine-global tiers, so it has no hermetic direct
/// test of its own.
/// [`resolve_roster`], additionally reporting what the resolution could not read.
///
/// Why (#5544 review): `resolve_roster` returns a bare `Vec`, so the two
/// consumers that publish its LENGTH — `tm session start`'s
/// `"Instructions: N agents in delegation authority"` (via
/// [`crate::core::instruction_pipeline::PipelineOutput::agent_count`]) and
/// `tm doctor`'s `agents` check — had no way to know the number was short. That
/// made `tm doctor` report a truncated roster as authoritative, which is the
/// remedy the composed prompt's `ROSTER INCOMPLETE` banner points the operator
/// at. Every surface that publishes the count now resolves through here.
/// What: unions [`deployed_agent_dirs`] via [`roster_from_dirs_reporting`],
/// carrying each tier's unreadable paths alongside the merged roster.
/// Test: `session_start_roster_line_declares_an_incomplete_roster`
/// (`commands/session/start.rs`), `agents_check_is_not_ok_when_a_roster_read_failed`
/// (`doctor_fs_checks.rs`), `build_instructions_reports_an_unreadable_agent_file`
/// (`instruction_pipeline_tests.rs`).
/// Render the LIVE deployed roster for a project, or `None` when none is found.
///
/// Why (#4069): `build_instructions` already computed this section via
/// [`generate_authority`], but its output is merged into a `PipelineOutput`
/// string that the prompt composer never reads — `resolve_pm_prompt` fell back
/// to the static `AGENT_DELEGATION.md` asset, so a 42-agent deployment was
/// advertised to the PM as the asset's hand-maintained 8-row table and agents
/// such as `ticketing` and `memory-manager` were invisible. Regenerating here
/// (rather than threading the pipeline's string down) is the only shape that
/// works for every composer: `build_system_prompt_for*` and its callers —
/// `tm session instructions`, the tmux connect path, the daemon spawn — receive
/// a `project_dir` and nothing else, and two of them never run the pipeline at
/// all, so there is no `PipelineOutput` available to thread.
/// What: resolves the roster via [`resolve_roster`] — the one resolver every
/// consumer shares — and renders it with [`generate_authority`]. Returns `None` when no agent is deployed
/// anywhere, so an unprovisioned environment keeps exactly the previous
/// behaviour (the bundled asset alone) instead of being told it has no agents.
/// Test: `roster_from_dirs_ignores_missing_dirs` (the `None` branch's input) and
/// `instruction_overrides::tests::bundled_delegation_appends_deployed_roster`
/// (the rendered output reaching the delivered prompt). This function itself
/// consults machine-global tiers, so it has no hermetic direct test.
/// [`deployed_roster_section`] with the tiers supplied, so it can be tested.
///
/// Why (#5544): the shipped entry point resolves `$CLAUDE_CONFIG_DIR` and the
/// caller's home directory, which makes every one of its behaviours — including
/// the new incompleteness banner — untestable except by mutating process-global
/// state. Taking the tier list is the seam that removes that coupling; the
/// public function is the same three lines with the tiers resolved.
/// What: scans `dirs` via [`roster_from_dirs_reporting`] and renders through
/// [`generate_authority_reporting`]. Returns `None` ONLY when the scan both
/// found nothing and lost nothing — an all-empty roster that lost paths still
/// renders, because "nothing is deployed" and "nothing could be read" must not
/// collapse into the same silent answer.
/// Test: `an_unreadable_tier_directory_is_reported_not_silently_dropped`,
/// `a_roster_that_is_empty_because_reads_failed_still_renders_a_section`,
/// `roster_section_from_dirs_is_none_when_nothing_is_deployed`.
/// [`roster_section_from_dirs`], omitting every agent the harness itself
/// publishes.
///
/// Why (#4513): the composed prompt carried 43 agents while the harness's own
/// "Available agent types for the Agent tool" listing carried 38 of the same
/// names, so the PM read one list twice. The harness listing is authoritative —
/// it is what the Agent tool accepts — so the duplicate half is pure cost. This
/// is a RENDERING filter only: [`resolve_roster`] and every count taken from it
/// (`tm session start`, `tm doctor`) still see the full union, because a cut
/// agent is still deployed and still dispatchable.
/// What: scans `dirs` once, recording which names came from a tier in
/// `harness_dirs` (compared by path, and expected to be a subset of `dirs` — see
/// [`harness_loaded_agent_dirs`]). Renders through
/// [`generate_authority_deduped`] with the harness-carried names removed and
/// counted. An all-harness roster still renders a section pointing at the
/// harness listing; only a roster that is both empty and lost nothing is `None`,
/// exactly as before.
/// Test: `harness_injected_agents_are_not_repeated_in_the_roster`,
/// `a_fully_harness_injected_roster_points_at_the_harness_listing`,
/// `an_agent_in_both_tiers_counts_as_harness_injected`,
/// `roster_section_from_tiers_with_no_harness_tier_matches_the_undeduped_render`,
/// `a_cut_agent_stays_in_the_resolved_roster_and_so_stays_dispatchable`,
/// `roster_section_from_tiers_is_none_when_nothing_is_deployed`.