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
//! Audit deployed skills against the RUNNING BINARY'S OWN bundled asset
//! (issue #4604).
//!
//! Why: [`crate::core::skill_staleness::stale_skills`] compares
//! `FrameworkPaths::skill_source_dir()` — which for every binary-only install
//! resolves to `~/.trusty-mpm/framework/skills/`, an EXTRACTION CACHE — against
//! the checksum recorded in the deploy manifest. Both sides of that comparison
//! can be the same stale content. Measured 2026-08-01: PR #4583 rewrote the
//! bundled `tm-workflow` skill and shipped in `trusty-mpm 1.3.1`, but the cache
//! had not refreshed since before the merge, so its sha256 was byte-identical
//! to the checksum already in the deployed manifest. The check compared stale
//! against stale, correctly reported "no drift", and named only two OTHER
//! skills — which read as an all-clear for `tm-workflow` while all three
//! deployed copies still carried the removed text. A check that misses a
//! drifted skill while flagging others is worse than no check.
//!
//! Two structural changes close it:
//!
//! 1. **The reference is the binary's own embedded asset**, never the
//! extraction cache. The cache is derived state; the compiled-in
//! `bundle::ALL` table cannot lag the binary running it. The one exception is
//! a populated `agents/skills` git submodule, which is version-controlled and
//! authoritative on its own (see `core::skill_source`) — that source is used
//! when present and named explicitly in the report.
//! 2. **The comparison reads the DEPLOYED FILE**, not the manifest checksum.
//! The manifest records what tm last wrote; reading it instead of the file
//! cannot see a hand-edit, and it is the manifest's agreement with the file
//! that distinguishes "drifted, a redeploy fixes it" from "drifted and
//! FROZEN, the redeploy will deliberately skip it". Those are different
//! states with different remediation.
//!
//! UNVERIFIABLE REPORTS UNKNOWN: a managed skill with no embedded counterpart,
//! or a deployed file that cannot be read, is [`SkillDrift::Unverifiable`] and
//! never counted as fresh; a tier whose ownership ledger is absent-over-content
//! or unparseable yields the matching [`ManifestState`] and no findings at all,
//! because nothing there is attributable (#4622 review).
//!
//! Keys are the DEPLOY-MANIFEST keys, which come in two shapes — a bare
//! `<stem>` and a nested `<stem>/references/<file>.md`. Handling only the first
//! made 80 of this machine's 132 keys permanently unverifiable (#4622 review,
//! HIGH-1); [`deployed_path`] is the one resolver both the audit and the repair
//! use.
//!
//! Test: `skill_drift_tests.rs`.
use BTreeMap;
use Path;
use cratebundle;
use crate;
/// Entry-point filename Claude Code reads inside a deployed skill directory.
const SKILL_ENTRY_FILE: &str = "SKILL.md";
/// What the deployed copy of one skill is, relative to the running binary.
///
/// Why: `tm doctor` previously had one bit (stale / not stale) and could
/// therefore not express the two states the #4604 comment thread demanded be
/// separated — a drift a redeploy repairs versus a drift a redeploy will
/// deliberately skip — nor the unverifiable state that must never render as
/// healthy.
/// What: five states, ordered by how much they should worry an operator.
/// Test: `skill_drift_tests.rs`.
/// One skill's audit result at one deploy target.
///
/// Why: every finding must name the skill so remediation can be targeted, and
/// the state so the message can prescribe the right remedy.
/// What: the skill stem plus its [`SkillDrift`].
/// Test: `skill_drift_tests.rs`.
/// Where the authoritative skill text came from, for the report.
///
/// Why: "compared against the binary's embedded assets" and "compared against
/// the checked-out `agents/skills` submodule" are different claims, and an
/// operator reading a drift report needs to know which one was made — the whole
/// #4604 defect was a reference point nobody named.
/// What: a short label rendered into the doctor message.
/// Test: `reference_prefers_the_submodule`, `reference_falls_back_to_embedded`.
/// Build the skill reference from the RUNNING BINARY, never from the cache.
///
/// Why: this is the #4604 fix in one function. `skill_source_dir()` resolves to
/// the `~/.trusty-mpm/framework/skills/` extraction cache for every binary-only
/// install, and that cache is exactly what lagged the installed binary. The
/// compiled-in table cannot lag the binary that contains it.
/// What: when `submodule_source` is `Some` (a populated, git-tracked
/// `agents/skills` checkout — the one source that legitimately outranks the
/// embedded table, per `core::skill_source`) its top-level `*.md` files are the
/// reference. Otherwise every `skills/<stem>.md` entry of [`bundle::ALL`] is —
/// keyed EXACTLY as the deploy manifest keys them: a bare `<stem>` for an entry
/// point, and `<stem>/references/<file>.md` for each reference sibling
/// (#4622 review, HIGH-1 — dropping the nested entries made all 80 of this
/// machine's 132 manifest keys unverifiable, pinning `skill_staleness` to
/// Unknown on every real install). A submodule directory that cannot be read, or
/// yields nothing, falls back to the embedded table rather than producing an
/// empty reference, since an empty reference would make every skill
/// unverifiable at once.
/// Test: `reference_prefers_the_submodule`, `reference_falls_back_to_embedded`,
/// `reference_includes_nested_reference_keys`,
/// `a_pristine_bundled_deploy_is_entirely_fresh`.
/// Walk a checked-out `agents/skills` submodule into manifest-keyed assets.
///
/// Why: the submodule mirrors the bundled layout — flat `<stem>.md` entry points
/// beside `<stem>/references/<file>.md` subtrees — so it must produce the same
/// key shapes the embedded table does, or the submodule path would reintroduce
/// the #4622 HIGH-1 defect on dev checkouts only.
/// What: recurses from `root`, keying each `.md` file by its path relative to
/// `root` (with `.md` stripped for a top-level entry point). Hidden files and
/// unreadable entries are skipped, never guessed at.
/// Test: `reference_prefers_the_submodule`.
/// Whether a deploy tier's ownership manifest could be read at all.
///
/// Why (#4622 review, MEDIUM): [`SkillManifest::load`] returns an EMPTY manifest
/// for a file that is absent AND for one that fails to parse. Folding both into
/// "nothing managed here" made a tier with skills on disk but a corrupt ledger
/// report `Ok — every deployed skill matches`, which is the exact
/// unverifiable-rendered-as-healthy failure this PR exists to remove.
/// What: four states. Only [`Present`](Self::Present) and
/// [`AbsentAndEmpty`](Self::AbsentAndEmpty) are answers; the other two are
/// admissions that the tier could not be audited.
/// Test: `nothing_deployed_yields_no_findings`,
/// `absent_manifest_over_deployed_skills_is_unknown`,
/// `corrupt_manifest_is_unknown_not_empty`.
/// One deploy tier's audit: the ledger's own state plus the per-skill findings.
///
/// Why: the tier verdict depends on BOTH — a tier with zero drifted skills but
/// an unreadable ledger has not been shown to be clean.
/// What: [`manifest`](Self::manifest) is the ledger state,
/// [`findings`](Self::findings) the per-manifest-key results (empty whenever the
/// ledger could not be read).
/// Test: `skill_drift_tests.rs`.
/// Resolve the on-disk path a deploy-manifest key refers to.
///
/// Why (#4622 review, HIGH-1): manifest keys come in TWO shapes and they do not
/// share a layout. `skills::deployer::deploy_skills` writes a bare stem's entry
/// point to `<dest>/<stem>/SKILL.md`, but records each reference sibling under
/// `<stem>/references/<file>.md` and writes it to that same relative path. One
/// resolver keeps the audit, the repair, and the backup from each inventing a
/// different answer.
/// What: a key containing `/` mirrors the source layout verbatim; a bare key
/// resolves to `<dest>/<key>/SKILL.md`.
/// Test: `deployed_path_matches_the_deployer_layout`.
/// The skill stem a manifest key belongs to.
///
/// Why: severity rules (the conventions-bearing escalation) are stated per
/// SKILL, but a key may name one of that skill's reference files.
/// What: the first path segment.
/// Test: `drift_in_a_reference_file_is_caught` (which asserts on the full key)
/// and `staleness_escalates_when_a_reference_file_of_a_conventions_skill_drifts`.
/// Audit every tm-managed skill deployed under `dest_dir`.
///
/// Why: see the module doc. This reads the FILE, so it sees hand-edits the
/// manifest-only comparison structurally could not, and compares against
/// `reference`, so a stale extraction cache cannot make a drifted skill report
/// clean.
/// What: probes the ownership manifest first — an unreadable one, or an absent
/// one over a populated tier, yields the matching [`ManifestState`] and NO
/// findings, because nothing there is attributable. Otherwise, for each manifest
/// key —
/// - no reference asset for that key → [`SkillDrift::Unverifiable`];
/// - [`deployed_path`] absent → [`SkillDrift::Missing`];
/// - unreadable → [`SkillDrift::Unverifiable`];
/// - content equals the reference → [`SkillDrift::Fresh`];
/// - content still matches the manifest checksum → [`SkillDrift::Drifted`];
/// - otherwise → [`SkillDrift::DriftedFrozen`].
///
/// Findings are sorted by key for stable output.
/// Test: `skill_drift_tests.rs`.
/// Classify one manifest key. See [`audit_deployed_skills`] for the rules.