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
//! Explicit, human-initiated adoption of unreachable bundled skills (#4605).
//!
//! Why: [`crate::skills::unmanaged`] reports bundled-named skills a deploy
//! target does not manage; reporting alone leaves the operator with no way to
//! fix them short of hand-deleting files. This module is the repair half —
//! deliberately NOT automatic. Adoption overwrites a file tm cannot prove it
//! wrote, so it runs only when a human asks for it by name, and it backs up
//! every file it touches first.
//!
//! What: [`adopt_unmanaged_bundled_skills`] copies each unmanaged bundled
//! skill directory under a backup root, then records its files in the deploy
//! target's manifest with the checksum of the content ALREADY ON DISK. That
//! single act is what unsticks the pipeline: the stem stops being classified
//! project-custom by [`crate::skills::tiers::list_project_custom_stems`], so
//! the next [`crate::skills::tiers::deploy_all_skill_tiers`] plans it into
//! `bundled_deploy`, and `crate::skills::deployer::deploy_one_file` then
//! sees "managed, checksum matches, content differs" — its existing safe-
//! refresh branch. No new write path, no new ownership rule; the adoption is
//! the whole fix and the refresh is the machinery that already worked.
//!
//! CONTRACT — a stem matching nothing bundled is NEVER touched. The roster is
//! the only admission test, and it is applied by
//! [`crate::skills::unmanaged::unmanaged_bundled_skills`], not re-derived
//! here, so report and repair cannot drift on which files are in scope.
//! Content similarity is deliberately not a test: an untracked file identical
//! to a shipped version is indistinguishable from a customization that started
//! from it.
//!
//! Test: `reconcile_tests.rs`.
use BTreeSet;
use Write as _;
use ;
use crate;
use crate;
use crate;
/// Filename of the per-backup-root ledger of what was copied where.
///
/// Why: matches the `moved-paths.log` this repository's existing
/// `~/.trusty-mpm/backup-doctor-remediation-<date>/` backups already carry, so
/// an operator finds the same artifact in the same place rather than learning
/// a second convention.
/// What: `moved-paths.log`, appended one `BACKUP: <src> -> <dst>` line per file.
/// Test: `adopt_writes_the_backup_ledger`.
pub const BACKUP_LEDGER_FILE: &str = "moved-paths.log";
/// One skill adopted into a deploy target's manifest.
///
/// Why: the caller prints exactly what was touched, per file, per tier — so
/// the stem, where its pre-adoption copy went, and the manifest keys now
/// tracking it all travel together.
/// What: the stem, the deployed directory, the backup directory the originals
/// were copied to, and the manifest keys inserted (entry point first).
/// Test: `adopt_registers_the_skill_and_its_references`.
/// Adopt every unmanaged bundled-named skill at `dest` into its manifest,
/// backing each one up under `backup_root` first.
///
/// Why: see the module doc — this is the only path that makes an untracked
/// bundled skill reachable again, and it must never run without a human
/// asking. Backing up before recording ownership means the operator can always
/// recover a customization the roster match happened to catch.
/// What: finds the in-scope skills with
/// [`unmanaged_bundled_skills`] (the roster is the ONLY admission test),
/// copies each file to `backup_root/<mirrored absolute path>`, appends a
/// [`BACKUP_LEDGER_FILE`] line per copy, then inserts a
/// [`SkillManifestEntry`] per file whose checksum is that of the CONTENT
/// CURRENTLY ON DISK — never the source's — so the deployer's existing
/// managed-and-unmodified branch decides the refresh. Saves the manifest once
/// at the end, atomically. Returns the adopted skills sorted by stem; an empty
/// result means nothing was in scope and NOTHING was written (no backup root,
/// no manifest rewrite). Adoption does not itself rewrite any skill content —
/// run a deploy afterwards to pull the current bundled text in.
/// Test: `adopt_registers_the_skill_and_its_references`,
/// `adopt_backs_up_before_recording`, `adopt_leaves_an_operator_skill_alone`,
/// `adopt_then_deploy_refreshes_a_stale_skill`, `adopt_no_findings_writes_nothing`.
/// The body of [`adopt_unmanaged_bundled_skills`], run holding the ledger lock.
///
/// Why (#4881): adoption is a load-modify-save of the same ledger the deployer
/// writes; unlocked, it drops whatever a concurrent deploy recorded. Never call
/// it directly, and never from inside another [`with_skill_manifest_lock`] on
/// the same directory (`flock` on a second descriptor self-deadlocks).
/// What: the scan/back-up/record pipeline documented on the public wrapper.
/// Test: the `adopt_*` tests in `reconcile_tests.rs` exercise it through that
/// wrapper.
/// Preview what [`adopt_unmanaged_bundled_skills`] would touch at `dest`.
///
/// Why: the operator sees the file list before anything is written, and the
/// preview must be produced by the SAME scan the adoption runs — a separately
/// derived preview is a preview of a different command.
/// What: [`unmanaged_bundled_skills`] verbatim. Read-only.
/// Test: `preview_matches_what_adoption_touches`.
/// Re-stamp every bundled-named skill at `dest` the deployer would decline to
/// refresh, backing each file up first — the `tm reinstall --force` clobber.
///
/// Why: [`adopt_unmanaged_bundled_skills`] covers only skills the manifest has
/// lost track of. The other declined state is a MANAGED file hand-edited away
/// from its recorded checksum, which the deployer reads as a user edit and
/// skips forever. `tm reinstall --force` is the explicit, human-initiated
/// instruction to overwrite both, and this module's contract for that is
/// unchanged: nothing is touched without a backup, and the backup goes to the
/// same `backup_root` with the same [`BACKUP_LEDGER_FILE`] ledger.
/// What: scans [`bundled_skill_dirs`] — the same walk the unmanaged detector
/// uses, without its ownership filter — and for each file whose manifest entry
/// is absent or whose checksum no longer matches the bytes on disk, copies it
/// under `backup_root` and records a [`SkillManifestEntry`] carrying the CURRENT
/// on-disk checksum. That returns the file to the deployer's
/// managed-and-unmodified branch, so the deploy that runs next writes the
/// bundled text over it. Files already managed and current are left completely
/// alone — no backup, no manifest churn. Content itself is never rewritten here.
/// An empty result means nothing was in scope and NOTHING was written.
///
/// Scope limit: inside a skill the ledger ALREADY tracks, only files the ledger
/// also tracks are re-stamped. An operator's own `references/*.md` dropped next
/// to a managed skill is left untracked, so it stays in
/// `prune_guard::skill_verdict`'s spared set instead of becoming tm-managed and
/// prunable. When the skill's entry point is itself untracked the whole
/// directory is adopted, references included — that is the #4605 state, and it
/// is what [`adopt_unmanaged_bundled_skills`] already does.
/// Test: `force_adopt_restamps_a_frozen_managed_skill`,
/// `force_adopt_leaves_a_current_skill_alone`,
/// `force_adopt_leaves_an_operator_skill_alone`,
/// `force_adopt_leaves_an_operator_reference_stray_untracked`.
/// The body of [`force_adopt_bundled_skills`], run holding the ledger lock.
///
/// Why (#4881): re-stamping is a load-modify-save of the ledger a concurrent
/// deploy also writes; unlocked, the two drop each other's entries and the files
/// those entries described freeze. Never call it directly, and never from inside
/// another [`with_skill_manifest_lock`] on the same directory.
/// What: the scan/back-up/re-stamp pipeline documented on the public wrapper.
/// Test: exercised through that wrapper by the `force_adopt_*` tests.
/// Where `source`'s backup copy lives under `backup_root`.
///
/// Why: two deploy tiers can hold a same-named skill (`~/.claude/skills/tm-x`
/// and `$CLAUDE_CONFIG_DIR/skills/tm-x`), so a flat backup directory would
/// have the second silently clobber the first. Mirroring the source path makes
/// collisions impossible and the origin of every backup obvious.
/// What: `backup_root` joined with `source`'s components minus any root
/// prefix — `/Users/x/.claude/skills/tm-x` becomes
/// `<backup_root>/Users/x/.claude/skills/tm-x`. A relative `source` is joined
/// as-is.
/// Test: `backup_target_mirrors_the_absolute_path`.
/// Copy one file under `backup_root` and record it in the backup ledger.
///
/// Why: the copy is what makes adoption recoverable; the ledger line is what
/// makes it findable months later without diffing directory trees.
/// What: creates the mirrored parent directory, copies `source` to
/// [`backup_target`], then appends `BACKUP: <source> -> <target>` to
/// `<backup_root>/`[`BACKUP_LEDGER_FILE`]. Errors propagate — a backup that
/// cannot be written must abort the adoption, never proceed without it.
/// Test: `adopt_backs_up_before_recording`, `adopt_writes_the_backup_ledger`.