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
//! Which agent SYSTEM authored this `.md` file? (issue #4448)
//!
//! Why: `.claude/agents/` is a shared convention, not a trusty-mpm namespace.
//! claude-mpm — a separate, still-live project — deploys into the same
//! directory reusing trusty-mpm's exact filenames: `engineer.md`, `ops.md`,
//! `qa.md`, `research.md`, `ticketing.md`, `memory-manager.md`,
//! `code-analyzer.md`, `code-critic.md`, `documentation.md`,
//! `web-ui-engineer.md`, `mpm-agent-manager.md`, `mpm-skills-manager.md`. A
//! sweep keyed on the FILENAME moves another project's files. So does one
//! keyed on the declared `name:` alone, because those collide too. The only
//! signal that separates the two systems is the SHAPE of the frontmatter, and
//! [`quarantine`](crate::agents::quarantine) — the one caller that MOVES
//! files — gates on it.
//!
//! What: [`agent_schema`] returns [`AgentSchema::TrustyMpm`] only when THREE
//! conditions hold together — no foreign key, the identity keys `name:`+`role:`,
//! and the composer's own base preamble ([`COMPOSED_BASE_MARKER`]) in the body.
//! Anything else is foreign or unknown, and the quarantine refuses it.
//!
//! The key set alone is NOT enough, and that was #4448 review's HIGH finding.
//! A whitelist is satisfied by OMISSION: a hand-authored agent declaring only
//! `name`/`role`/`description`/`model` declares nothing exotic, so it passed —
//! a real operator-written file on this machine classified as tm's. A key set
//! can rule a file OUT (a foreign key proves the composer did not write it);
//! only something present in the file can rule it IN. The body marker is that
//! something. See [`COMPOSED_BASE_MARKER`] for why a compose-time provenance
//! key would not work.
//!
//! The negative half is the key SET, deliberately not any single key:
//!
//! | Key | trusty-mpm | claude-mpm |
//! |---|---|---|
//! | `role:` | always | never (uses `agent_type:`) |
//! | `agent_type:` | never | always |
//! | `version:`, `effort:`, `author:`, `schema_version:`, `agent_id:` | never | one or more |
//! | `initialPrompt:` | **often** | often |
//!
//! That last row is the trap. `initialPrompt:` reads like a claude-mpm marker
//! and is not one: `merge_frontmatter` injects a role-derived prompt when the
//! source declares none (`builder.rs`, HR-1 Part B), so trusty-mpm emits it
//! for most agents. Treating it as foreign would exempt nearly the whole
//! bundled roster from the sweep.
//!
//! SIZE IS NOT A SIGNAL. It looks like one — trusty-mpm bodies run 3–4 KB and
//! claude-mpm's 8–80 KB — but the 2026-07-31 inventory found middle-generation
//! claude-mpm files at 23–81 KB overlapping and exceeding the bundled band.
//! Nothing here measures length.
//!
//! Test: `agent_schema_tests.rs`.
use crateparse_kv_line;
/// The agent system whose frontmatter shape this document matches.
///
/// Why: the quarantine's move gate is a POSITIVE identification — it acts only
/// on files it can prove trusty-mpm's own composer wrote. The two non-matching
/// cases stay distinct rather than collapsing into one "not ours", because the
/// receipt has to tell an operator WHICH foreign thing it declined to touch;
/// "skipped: claude-mpm schema" and "skipped: unrecognized schema" send an
/// operator to different places.
/// What: [`TrustyMpm`](Self::TrustyMpm) — every declared key is one this
/// crate's composer emits, `name:`/`role:` are both present, AND the body
/// carries [`COMPOSED_BASE_MARKER`]; [`ClaudeMpm`](Self::ClaudeMpm) — carries at
/// least one key unique to claude-mpm's schema;
/// [`Unrecognized`](Self::Unrecognized) — everything else, including a
/// hand-authored file (even a minimal one declaring nothing exotic), a
/// trusty-mpm SOURCE file (it still declares `extends:`, which the composer
/// strips, and its body is not yet the composed chain), and any document
/// without a well-formed frontmatter block.
/// Test: `schema_of_a_composed_trusty_mpm_agent`,
/// `schema_of_a_claude_mpm_agent`, `schema_of_a_hand_authored_file`,
/// `schema_of_a_minimal_hand_authored_file_with_no_exotic_key`.
/// Every frontmatter key `merge_frontmatter` can emit, lower-cased.
///
/// Why: this is the whitelist the positive identification is built on, so it
/// must track the emitter EXACTLY. It is derived from the `out.push_str`
/// sequence in `crate::agents::builder::merge_frontmatter`; adding an emitted
/// key there without adding it here would make every freshly deployed agent
/// classify [`AgentSchema::Unrecognized`] and silently disable the sweep.
/// `pinned_to_the_composer_emission` fails when the two drift.
const TRUSTY_MPM_KEYS: & = &;
/// Keys that appear ONLY in claude-mpm's agent schema.
///
/// Why: naming the foreign system explicitly is what lets the receipt say
/// "this belongs to claude-mpm, left alone" instead of a vague "skipped".
/// `initialPrompt` is deliberately ABSENT — see the module doc's trap note.
const CLAUDE_MPM_KEYS: & = &;
/// The composition root every tm-composed agent BODY carries.
///
/// Why: the frontmatter key set alone cannot answer "did tm write this?". It is
/// a whitelist, and a whitelist is satisfied by OMISSION — a hand-authored file
/// declaring nothing but `name`/`role`/`description`/`model` passes it, which is
/// how #4448 review found a real operator-written agent classifying as tm's. The
/// key set can only rule a file OUT (a foreign key proves it is not composer
/// output); something in the file has to rule it IN.
///
/// This is that something. `compose_agent` concatenates the whole `extends:`
/// chain base-first, and EVERY bundled agent's chain roots at `base-agent` —
/// all 42 in `content/agents/`, and `trusty-code`'s set
/// too. So every composed agent body opens with this line, and no file that tm
/// did not compose carries it unless someone pasted tm's own base preamble
/// verbatim, at which point the file IS a copy of a tm artifact.
///
/// What: byte-identical across EVERY revision of `BASE-AGENT.md` back to
/// 2026-05-19, including the pre-rename `trusty-mpm-core` path — which matters
/// because the files this sweep exists to move were written by OLD binaries.
/// That also rules out the alternative fix of stamping a fresh provenance key
/// at compose time: a new key marks only files written by NEW binaries, i.e.
/// exactly the ones that are not stale, making the sweep a no-op against its
/// own target.
///
/// A future edit to that heading does not cause a wrong move — it causes a
/// REFUSAL to move, which is the safe direction and the one this issue asks
/// for. Two tests guard it: `composed_marker_survives_composition` here, and
/// `the_bundled_base_agent_carries_the_composition_marker` in `trusty-mpm`,
/// which owns the asset and asserts its heading line is EXACTLY this string —
/// public for that reason, so the pin compares the real constant rather than a
/// second copy that could drift on its own.
/// Test: `composed_marker_survives_composition`.
pub const COMPOSED_BASE_MARKER: &str = "# BASE-AGENT — Foundation for all trusty-mpm agents";
/// Classify a document by its frontmatter key set.
///
/// Why: the movability predicate's fourth gate (#4448). A quarantine that
/// cannot tell trusty-mpm's artifacts from claude-mpm's moves a live project's
/// agents out from under it, and three of six independent inventory passes on
/// 2026-08-03 made exactly that error by keying on the filename.
/// What: reads the leading `---`-fenced frontmatter block's keys via
/// [`frontmatter_keys`] and returns [`AgentSchema::TrustyMpm`] only when the
/// key set is a SUBSET of [`TRUSTY_MPM_KEYS`] and contains both `name` and
/// `role`; otherwise [`AgentSchema::ClaudeMpm`] when any [`CLAUDE_MPM_KEYS`]
/// entry is present; otherwise [`AgentSchema::Unrecognized`]. Fail-closed at
/// every step: a missing block, an unterminated block, an unknown key, or a
/// missing `role:` all land outside `TrustyMpm`, and only `TrustyMpm` is
/// movable.
///
/// The body check is what makes this a POSITIVE identification rather than a
/// whitelist. Three conditions must ALL hold for [`AgentSchema::TrustyMpm`]:
/// no foreign key, the identity keys `name:`+`role:`, and
/// [`COMPOSED_BASE_MARKER`] in the body. Dropping the last one is the #4448
/// review's HIGH finding — a minimal hand-authored agent satisfies a key
/// whitelist by declaring nothing exotic.
///
/// Test: `schema_of_a_composed_trusty_mpm_agent`, `schema_of_a_claude_mpm_agent`,
/// `schema_of_a_hand_authored_file`,
/// `schema_of_a_minimal_hand_authored_file_with_no_exotic_key`,
/// `schema_of_a_source_file_with_extends`,
/// `schema_without_frontmatter_is_unrecognized`,
/// `schema_of_an_unterminated_block_is_unrecognized`,
/// `schema_requires_role`, `initial_prompt_alone_is_not_foreign`,
/// `composed_marker_survives_composition`,
/// `the_body_marker_is_what_separates_composed_from_hand_authored`.
/// The keys declared in a document's leading frontmatter block, lower-cased.
///
/// Why: [`crate::agents::metadata::agent_metadata_from_str`] projects only the
/// keys trusty-mpm models and DROPS the rest, so it cannot answer "does this
/// file declare anything foreign?" — the exact question the schema gate asks.
/// This reads the raw key list instead.
/// What: `None` unless the document's FIRST line is a `---` fence and a
/// closing `---` fence follows — an unterminated block is not a frontmatter
/// block. Otherwise the keys [`parse_kv_line`] finds, in declaration order,
/// lower-cased by that parser. Non-`key: value` lines (a `- item` block-list
/// element, a folded-scalar continuation, a comment) yield no key. A
/// continuation line that happens to contain a colon contributes a spurious
/// key; that only ever ADDS keys, which can push a document out of
/// [`AgentSchema::TrustyMpm`] but never into it — the fail-closed direction.
/// Test: `keys_are_declaration_ordered`, `keys_skip_block_list_items`,
/// `keys_require_a_leading_fence`, `keys_require_a_closing_fence`.
/// [`frontmatter_keys`] plus everything after the closing fence.
///
/// Why: [`agent_schema`]'s positive half reads the BODY, and re-scanning for the
/// fence a second time would let the two halves disagree about where it is.
/// What: `None` unless the document's first line is a `---` fence and a closing
/// `---` fence follows. Otherwise the declared keys and the remaining text.
/// Test: `keys_are_declaration_ordered`, `keys_require_a_closing_fence`,
/// `schema_of_a_composed_trusty_mpm_agent`.