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
//! Removal of deployed skills no source ships any more (issue #5224).
//!
//! Why: `skills::deployer::deploy_skills_filtered` only ever writes — its own
//! doc says "Deselecting a skill does not remove a previously deployed copy".
//! When a skill is RETIRED from the bundle the deployed directory outlives it,
//! and two things go wrong at once. Claude Code keeps loading text the binary
//! deliberately stopped shipping, and
//! [`crate::core::skill_drift::audit_deployed_skills`] classifies the orphaned
//! ledger key [`SkillDrift::Unverifiable`](crate::core::skill_drift::SkillDrift)
//! — which folds the whole `skill_staleness` doctor check to
//! [`CheckStatus::Unknown`](crate::core::doctor::CheckStatus). A check reporting
//! `Unknown` has stopped protecting anything, so one intentional retirement
//! silently disables the probe on every machine that had the skill.
//!
//! [`crate::core::stale_skills`] solved this shape once for the #1905
//! `mpm-*`→`tm-*` rename behind a frozen allowlist, deliberately scoped to that
//! one migration. This module generalises it: retirement is DERIVED from the
//! live sources rather than listed by hand, so a future retirement needs no
//! second edit here.
//!
//! What: a deployed skill is RETIRED at a target when its stem is tracked by
//! that target's ownership ledger and appears in NO live source —
//! [`live_skill_stems`] unions every source that can legitimately feed a deploy
//! target. [`retire_orphaned_skills`] sweeps every
//! [`skill_deploy_tiers`](crate::core::skill_deploy_tiers::skill_deploy_tiers)
//! entry; [`retire_orphans_in`] is the injectable core one target at a time.
//!
//! TIER SAFETY — the hard constraint, held by three independent gates:
//! 1. Candidates come only from the ownership LEDGER. A skill the operator
//! hand-placed in a project's or their own `.claude/skills/` has no ledger
//! entry and is therefore never a candidate at all.
//! 2. The live set unions the user-custom tier (`~/.trusty-mpm/skills/`) and
//! the project-custom stems already on disk, so a skill those tiers supply
//! is never retired even when the bundle drops the same name. This is the
//! tier oracle `update_check::apply::prune_skills` already uses for #391 —
//! the live user source, not a recorded origin field, because the ledger
//! records no tier.
//! 3. [`skill_removal_verdict`] refuses to delete a directory holding anything
//! the ledger does not claim, or any claimed file whose bytes changed after
//! deployment.
//!
//! DESELECTION IS NOT RETIREMENT. The live set is built from what the sources
//! CONTAIN, never from what a harness manifest's include/exclude selects, so an
//! excluded-but-still-shipped skill keeps its deployed copy exactly as before.
//! Deselection remains `tm catalog apply --prune`'s business.
//!
//! FAILING SAFE: every uncertainty widens the live set or aborts the sweep, so
//! the failure mode is leaving an orphan behind, never deleting something live.
//! An unreadable source aborts the sweep for that target entirely.
//!
//! Test: `skill_retire_tests.rs`.
use BTreeSet;
use Path;
use crateResult as ManifestResult;
use cratebundle;
use crateFrameworkPaths;
use crateskill_deploy_tiers;
use crate;
use crate;
use crate;
/// Whether a retired skill's deployed directory may be deleted.
///
/// Why: "the ledger says this skill is retired" and "nothing under its
/// directory belongs to the operator" are separate questions, and only the
/// second one authorises `remove_dir_all`. Keeping the reason with the refusal
/// is what makes a skipped removal visible instead of silent.
/// What: `Removable`, or `Kept` carrying an operator-facing reason.
/// Test: `verdict_keeps_a_hand_edited_skill`, `verdict_keeps_an_untracked_file`.
/// One skill this sweep stopped tracking, and what happened to its files.
///
/// Why: callers report retirement to the operator, and "deleted" versus "left
/// on disk because you edited it" are different facts they must be able to
/// print separately.
/// What: the deploy tier's label, the stem, whether the directory was deleted,
/// and — when it was not — why.
/// Test: `retire_removes_a_pristine_orphan`,
/// `retire_keeps_a_hand_edited_orphan_but_releases_the_ledger`.
/// Every skill stem this binary itself embeds.
///
/// Why: the compiled-in table is the one source that cannot lag the binary
/// running it — the same reasoning [`crate::core::skill_drift::skill_reference`]
/// gives for never trusting the `~/.trusty-mpm/framework/skills/` extraction
/// cache as an authority.
/// What: the first path segment of every `skills/…` entry in [`bundle::ALL`],
/// with `.md` stripped — so both `skills/<stem>.md` and
/// `skills/<stem>/references/<file>.md` yield `<stem>`.
/// Test: `bundled_stems_covers_a_known_skill`.
/// Union every source that could legitimately supply a skill to `dest`.
///
/// Why: a stem is only safe to retire when NO source has it, so this set must
/// err large. It deliberately includes sources a given deploy is not currently
/// reading from — the catalog checkout, the extraction cache — because a target
/// like `~/.claude/skills` is shared across projects and one project's
/// `source = "catalog"` manifest must not make another project's bundled skills
/// look retired.
/// What: the compiled-in [`bundled_skill_stems`], plus the resolved bundled
/// source (the `agents/skills` submodule when checked out, else the extraction
/// cache), the user-custom tier, the synced catalog's skills, and the
/// project-custom stems already sitting in `dest`. Returns `None` when any of
/// those directories exists but cannot be enumerated — an incomplete live set
/// would misread live skills as retired, so the caller must skip the sweep.
/// Test: `live_stems_include_the_user_tier`,
/// `live_stems_are_none_when_a_source_cannot_be_read`.
/// May a retired skill's whole directory be deleted?
///
/// Why (#391, extended by #5224): a skill is a DIRECTORY, so the question is
/// not "does one file match" but "is everything under it something trusty-mpm
/// deployed and the operator has not touched" — `remove_dir_all` takes the
/// untracked sibling with it. `update_check::apply::prune_guard::skill_verdict`
/// asked exactly this question first and now delegates here, so the deselection
/// prune and this retirement sweep cannot drift on what counts as safe.
/// What: `Removable` when the directory is absent, or when every file under it
/// is claimed by a ledger key belonging to `stem` AND still checksums to that
/// key's entry. `Kept` when the directory holds a file no ledger key claims,
/// when a claimed file's bytes differ, or when anything under it cannot be
/// read. A claimed file that is already gone is not disqualifying.
/// Test: `verdict_keeps_a_hand_edited_skill`, `verdict_keeps_an_untracked_file`,
/// `verdict_allows_a_pristine_skill`.
/// Collect every regular file under `dir`, recursively.
///
/// Why: [`skill_removal_verdict`] must see the whole subtree `remove_dir_all`
/// would take, not just the entry point — a reference file or a carried script
/// is operator content too.
/// What: appends each file path to `out`. Directory entries recurse; a symlink
/// is reported as a plain path (`DirEntry::file_type` does not follow it), which
/// makes it unclaimed and therefore disqualifying — the conservative answer.
/// Test: exercised through [`skill_removal_verdict`] by
/// `verdict_keeps_an_untracked_file`.
/// Retire every ledger-tracked stem at `dest` that `live` does not contain.
///
/// Why: this is the whole fix, and taking `live` as a parameter is what makes
/// it testable against a literal source set instead of the real embedded bundle
/// — the same split `crate::daemon::doctor_skill_drift` uses for its report.
/// What: under the ledger lock, for each retired stem —
/// - [`skill_removal_verdict`] `Removable` → delete `<dest>/<stem>/`;
/// - `Kept` → leave every file exactly where it is.
///
/// EITHER WAY the stem's ledger keys are dropped. A ledger entry is a claim
/// that tm deployed the file and can refresh it; once no source ships the
/// skill, that claim is false and keeping it is what pins `skill_staleness` to
/// `Unknown`. Releasing it reclassifies a kept file as operator-owned, which is
/// what a hand-edited copy has effectively become — and [`crate::core::
/// skill_drift`]'s #4605 unmanaged-skill detector already finds and adopts such
/// a file if the skill ever returns to the bundle.
///
/// CONCURRENCY (#4881): the whole load-modify-save runs under
/// [`with_skill_manifest_lock`], and as a SEPARATE locked section from the
/// deploy that precedes it — `flock` is not reentrant, so one span would have
/// to call an unlocked inner deploy entry point, a worse hazard than the
/// residual window. A skill deployed between the two is retired only if no
/// source has it, which cannot be true of one just deployed.
/// Test: `retire_removes_a_pristine_orphan`,
/// `retire_keeps_a_hand_edited_orphan_but_releases_the_ledger`,
/// `retire_spares_a_user_tier_skill`, `retire_spares_a_project_tier_skill`,
/// `retire_is_a_noop_when_nothing_is_orphaned`.
/// The body of [`retire_orphans_in`], run while holding the ledger lock.
///
/// Why/What: mirrors `deployer::deploy_skills_locked` — the critical section is
/// one expression so the lock's scope cannot be misread. Never call it directly.
/// Test: covered through [`retire_orphans_in`] by every `retire_*` test.
/// Sweep every skill deploy tier for skills no source ships any more.
///
/// Why: the orphan folds `skill_staleness` to `Unknown` independently at each
/// tier, so a sweep covering only the tier the caller happened to deploy into
/// leaves the others reporting `Unknown` forever — the same reasoning
/// [`skill_deploy_tiers`] exists for.
/// What: for each tier, builds [`live_skill_stems`] and calls
/// [`retire_orphans_in`]. A tier whose live set cannot be established, or whose
/// sweep fails, is logged and skipped — retirement is maintenance and must
/// never fail an install or a session launch.
/// Test: `retire_orphaned_skills_sweeps_every_tier`.