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
//! Harness-manifest schema (HR-2 / DOC-17).
//!
//! Why: today `prepare_session` provisions a harness from compile-time-bundled
//! assets alone — there is no declarative description of *what* a harness gets.
//! HR-2 makes provisioning manifest-driven: a single TOML document describes the
//! agent set, skills, instruction layers, output style, MCP servers, and model
//! tiers a harness receives, so the runner can resolve it from a configurable
//! catalog instead of hard-coding the set in Rust. TOML is chosen to match the
//! existing `config.toml` convention (`crate::core::config`).
//! What: [`HarnessManifest`] is the top-level deserialization target. Every field
//! is optional so a higher-precedence layer can override only the keys it cares
//! about (see [`HarnessManifest::merge`]); the compiled-in default
//! ([`super::default::default_manifest`]) supplies the floor. A `version` field
//! makes the schema forward-compatible so HR-3 can hash/compare manifests.
//! Test: `manifest_roundtrip`, `manifest_partial_parse`,
//! `manifest_merge_overrides`, `selection_matches` in this module's tests.
use ;
/// The manifest schema version this build understands.
///
/// Why: HR-3 will hash and compare manifests to detect stale catalog content; a
/// version field lets a future build reject or migrate an incompatible manifest
/// instead of silently mis-parsing it.
/// What: the integer written into a default manifest's `version` field and
/// compared on load.
/// Test: `default_manifest_carries_current_version`.
pub const MANIFEST_VERSION: u32 = 1;
/// A declarative description of everything a provisioned harness receives.
///
/// Why: a single value the resolver materializes (project override beats user
/// config beats catalog manifest beats compiled-in default) and `prepare_session`
/// consumes, replacing the implicit "deploy whatever is bundled" behavior with an
/// explicit, overridable contract.
/// What: optional sections for the agent set, skills, instruction layers, the
/// output style id, MCP servers, and model tiers. Absent sections mean "inherit
/// from the lower-precedence layer"; the compiled-in default fills any that are
/// still absent after all layers merge. `Default` yields an all-`None` manifest
/// used as the merge identity.
/// Test: `manifest_roundtrip`, `manifest_partial_parse`, `manifest_merge_overrides`.
/// `[agents]` — the agent set a harness deploys.
///
/// Why: a harness may want only a subset of the bundled/catalog agents (e.g. a
/// Rust-only project drops the JS engineers). Include/exclude lists express that
/// without enumerating every agent.
/// What: `include` is a list of agent names or glob patterns (`*` wildcard); an
/// empty or absent `include` means "all available agents". `exclude` removes
/// names/patterns from the included set and always wins over `include` — this is
/// the DEPLOY-ROSTER filter: an excluded agent is never written to ANY deploy
/// destination `agent_selected` gates (session/worktree launches, `tm catalog
/// apply`, pruning). `ignore_staleness` (issue #2462) is a SEPARATE, narrower
/// concern: it only suppresses the `/health`/`tm catalog apply` drift warning for
/// an agent the operator has deliberately customized, WITHOUT removing it from
/// any deploy roster. Do not put a name in `exclude` merely to silence a
/// staleness warning — that also deletes it from every filtered deploy
/// destination (the exact drift #2462 diagnosed: `exclude = ["engineer"]`,
/// written only to quiet `/health`, silently dropped `engineer.md` from every
/// session-local `.claude/agents/`). `source` selects where the agent *source
/// files* come from (`bundled` or `catalog`).
/// Test: `selection_matches`, `agent_set_source_default_is_bundled`,
/// `plan_ignore_staleness_does_not_affect_agent_selected`.
/// `[agent_categories]` — the framework tier's catalog declaration (#4760).
///
/// Why: [`AgentSet`] carries exactly one flat, un-categorised selection list
/// (`include`). It can say "deploy these names", but it cannot say "deploy these
/// unconditionally, gate THESE on a detected language, gate THESE on a detected
/// framework, gate THESE on a detected platform, and never deploy THESE" —
/// four of the five buckets need a *gating rule* bound to the name, and no
/// existing field carries a rule dimension. This is the additive extension that
/// closes that gap. It is a separate section rather than extra keys on
/// `[agents]` for a second reason: `[agents]` merges by WHOLE-SECTION
/// REPLACEMENT ([`HarnessManifest::merge`]), so a project manifest that set
/// `[agents]` at all would silently erase the framework catalog declaration.
/// What: the five disjoint lists that partition the bundled agent catalog.
/// `universal` deploys everywhere; `language`/`framework`/`platform` deploy only
/// when the entry's own declared [`GatedAgent::markers`] are detected;
/// `deprecated` never deploys and exists so a retired agent stays accounted for
/// rather than becoming an unexplained omission. `core::manifest::framework`
/// validates that the five lists exactly partition the bundled catalog and
/// composes them into an [`AgentSet`].
/// Test: `agent_categories_roundtrip` here;
/// `core::manifest::framework_tests` for validation and composition.
/// One marker-gated agent: the stem AND the condition that deploys it (#4765).
///
/// Why: declaring the category alone left the *condition* — the actual gate —
/// in a Rust table (`project_lang.rs::LANGUAGE_ENGINEERS` / `PLATFORM_AGENTS`),
/// so a reader asking "why did this project get `react-engineer`?" had two
/// places to look and two places to drift. The owner ruling of 2026-08-04
/// ("authoritative agent/skill bundling should be in framework-manifest.toml or
/// project manifest.toml") makes the gate a manifest field: membership and
/// condition are one declaration.
/// What: `stem` is the agent filename without `.md`; `markers` are the paths
/// (relative to the project root, or to any declared workspace member) whose
/// presence selects it. The three marker forms — exact path, `*.<ext>` glob, and
/// `<path>::<needle>` content probe — are evaluated by
/// `core::manifest::project_lang::marker_present`, which owns the probing
/// mechanics; this struct owns the declaration.
/// Test: `agent_categories_roundtrip`,
/// `core::manifest::framework_tests::gated_entry_without_markers_is_an_error`.
/// `[skill_categories]` — the framework tier's SKILL catalog declaration (#4765).
///
/// Why: the same owner ruling that made the agent roster a declaration covers
/// skills. Before this section the bundled skill roster existed only as
/// `include_str!` rows in `bundle_all.rs`, and the human-facing list of it lived
/// as prose in `assets/skills/tm.md` — which drifted, exactly as the agent
/// roster did. Declaring skills here gives them the agent side's guarantee: a
/// bundled skill nobody declared is a hard error, not a silent default.
/// What: skills carry ONE deploy rule today — every bundled skill deploys to
/// every project — so `universal` is the only list. No gated list is declared
/// speculatively: when a skill first needs a marker gate, it gets a list of
/// [`GatedAgent`]-shaped entries, exactly as the agent side has.
/// Test: `skill_categories_roundtrip` here;
/// `core::manifest::framework_tests::bundled_skill_roster_is_valid`.
/// `[skills]` — the skill set a harness deploys.
///
/// Why: like agents, a harness may enable only some skills. Skills carry no
/// inheritance so the selection is a plain include/exclude filter.
/// What: `include`/`exclude` are name-or-glob lists with the same semantics as
/// [`AgentSet`]; `source` selects the source directory (`bundled` or `catalog`).
/// Test: `selection_matches`, `skill_set_roundtrip`.
/// Where a harness reads agent/skill *source* content from.
///
/// Why: HR-2 must let a manifest point provisioning at either the compile-time
/// bundled assets (the default, regression-safe path) or the synced catalog
/// (`~/.trusty-mpm/catalog/repo/.claude/…`). Encoding the choice in the manifest
/// keeps `prepare_session` from guessing.
/// What: `Bundled` (the framework's own source dirs) or `Catalog` (the
/// CatalogSync checkout). `Default` is `Bundled` so an absent value reproduces
/// today's behavior.
/// Test: `agent_set_source_default_is_bundled`, `content_source_roundtrip`.
/// `[instructions]` — the instruction layers folded into the launch prompt.
///
/// Why: DOC-17 names "instruction layers (system, contextual, domain-specific)"
/// as part of what a harness gets. Today the layers are fixed in code; the
/// manifest records which optional layers are enabled so the set is declarative
/// and forward-compatible (HR-3 hashes it).
/// What: boolean toggles for the optional layers. The non-overridable PM floor
/// (`BASE_PM`) is always applied regardless of these flags — they only gate the
/// *optional* contextual/domain layers. Absent flags inherit from the lower
/// layer; the default enables the standard set.
/// Test: `instruction_layers_roundtrip`, `manifest_merge_overrides`.
/// `[style]` — the active output-style selection.
///
/// Why: DOC-17 lists the output style (professional/teaching/research) as a
/// harness-level choice. A manifest can set the default style; an explicit
/// `--style` flag still overrides it at launch (HR-4 precedence is preserved).
/// What: an optional active style id matching a bundled style
/// (`crate::core::bundle::OUTPUT_STYLES`). Absent → inherit; the default sets the
/// professional id.
/// Test: `style_selection_roundtrip`, `manifest_merge_overrides`.
/// `[mcp]` — which MCP servers the harness injects.
///
/// Why: DOC-17 lists "MCP servers (static and dynamic)" as part of provisioning.
/// Today `prepare_session` unconditionally injects `trusty-memory` and
/// `trusty-search`; the manifest makes those toggleable so a harness can opt out.
///
/// #7894: this also carried `custom` — a `[mcp.custom.<name>]` project-scope
/// server map (issue #2739 follow-up). Its only reader was
/// `session_launch::custom_mcp`, which ADR-0042 deleted along with every other
/// workspace `.mcp.json` injector, so the map resolved through three merge
/// layers into a plan field nothing read. The supported route for a custom
/// server is the USER-scope registry (`tm mcp add`, the managed `.claude.json`
/// `mcpServers` map), which every session reads directly. A manifest still
/// carrying `[mcp.custom]` keeps parsing — serde ignores the unknown table —
/// and, as before, has no effect.
/// What: boolean toggles for the two built-in servers. Absent → inherit; the
/// default enables both, reproducing today's behavior.
/// Test: `mcp_servers_roundtrip`, `default_manifest_enables_both_mcp`,
/// `a_custom_mcp_table_parses_and_is_dropped`.
/// `[divert]` — bulk-read diversion to a cheap worker model (#6887).
///
/// Why: a session that `Read`s a 2000-line file pays for every one of those
/// lines in its own context, on its own (expensive) model. Diverting that read
/// to a cheap worker that answers the question instead measured -45% cost and
/// -54% output tokens in the #6882 POC. The feature changes what the agent is
/// allowed to do, so it is OPT-IN: absent means off, unlike the `[mcp]`
/// toggles whose absent state is on.
/// What: three independent `Option` fields merged FIELD-BY-FIELD (see
/// [`Self::merge`]), so a project that sets only `min_lines` keeps a lower
/// layer's explicit `enabled`. `worker_model` is a model name `claude --model`
/// accepts (empty = [`DEFAULT_DIVERT_WORKER_MODEL`](crate::core::manifest::DEFAULT_DIVERT_WORKER_MODEL)).
/// There is no provider field: owner ruling 2026-09-07 fixed the worker as
/// headless Claude Code under the developer's existing login, so there is
/// nothing to select between.
/// Test: `divert_config_merge_field_level`, `manifest_roundtrip`,
/// `default_manifest_divert_disabled_by_default`, `plan_divert_toggles`.
/// `[models]` — model-tier defaults for the harness.
///
/// Why: DOC-17 lists "model tiers/overrides" as part of a manifest. HR-1 already
/// derives a model from each agent's `resource_tier` at deploy time; this section
/// lets a manifest pin the canonical model id for each tier so a harness can, for
/// example, route `intensive` to a specific Opus build.
/// What: an optional model id (or alias) per tier. Absent → inherit; the default
/// leaves all `None` so HR-1's built-in tier mapping applies.
/// Test: `model_tiers_roundtrip`, `manifest_merge_overrides`.
/// Merge two `Option<T>` sections, folding field-by-field when both are present.
///
/// Why: the field-level sections (`[mcp]`, `[instructions]`) need "higher wins
/// per field, lower inherited" semantics, but only when BOTH layers set the
/// section; when only one is present that one wins wholesale. Factoring the
/// `None`-handling here keeps [`HarnessManifest::merge`] readable.
/// What: returns `higher.merge(lower)` when both are `Some`; otherwise the single
/// present value (or `None`). `f` performs the per-field fold as `f(lower, higher)`.
/// Test: covered via `manifest_merge_mcp_field_level` and
/// `instruction_layers_field_merge`.
/// Whether `name` is selected by an include/exclude filter.
///
/// Why: both [`AgentSet`] and [`SkillSet`] share the same include/exclude/glob
/// semantics; factoring the rule here keeps the two consumers consistent and
/// independently testable.
/// What: returns `true` when `name` matches the `include` set (an empty include
/// list means "match all") AND does not match the `exclude` set. Matching is by
/// exact name or a trailing/leading `*` glob via [`glob_matches`].
/// Test: `selection_matches`.
/// Whether `name` matches any pattern in `patterns` (exact name or `*`-glob).
///
/// Why: [`AgentSet::ignore_staleness`] needs a plain membership test against the
/// same glob vocabulary [`selection_matches`] uses for include/exclude, without
/// the include/exclude pairing — factoring it out keeps both callers on
/// identical match semantics.
/// What: `true` iff any entry in `patterns` matches `name` via [`glob_matches`].
/// Test: `selection_matches` (via `selection_matches`'s reuse of this helper),
/// `plan_ignore_staleness_does_not_affect_agent_selected`.
/// Match `name` against a simple glob `pattern` supporting any number of `*`.
///
/// Why: manifests express agent/skill sets compactly (`"*-engineer"`,
/// `"rust-*"`, `"*-engineer-*"`); a tiny matcher avoids pulling in a glob crate
/// for the one wildcard. The earlier `split_once('*')` implementation only split
/// on the FIRST `*`, so a multi-`*` pattern (`"*-engineer-*"`, `"a*b*c"`) was
/// mis-parsed — the leftover `*` was treated as a literal in the suffix, silently
/// producing wrong matches instead of the documented behavior. This version
/// handles an arbitrary number of `*`.
/// What: splits `pattern` on every `*` into literal segments and matches them in
/// order against `name`. A leading `*` (empty first segment) means "no prefix
/// anchor"; a trailing `*` (empty last segment) means "no suffix anchor". With no
/// `*` the pattern is an exact equality check; a bare `*` matches everything.
/// Interior segments must appear in order, left to right, without overlapping.
/// Test: `glob_matching`.