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
//! Skill deployment — writes skill directories into `~/.claude/skills/`.
//!
//! Why: Claude Code discovers skills from `~/.claude/skills/<name>/SKILL.md`
//! (directory per skill, entry-point file named `SKILL.md`). trusty-mpm must
//! keep that directory populated with up-to-date skills in that format, while
//! never destroying files the user owns or has hand-edited. Skills carry no
//! inheritance, so — unlike agents — deployment is a plain content copy, but
//! the manifest-based ownership tracking is identical. Moved to
//! trusty-agents-common (#2892, #2818) from `trusty-mpm::core::skill_deployer`
//! so a second harness (trusty-code) can reuse the same deploy pipeline;
//! `trusty-mpm` re-exports every item here from `core::skill_deployer` for
//! source compatibility. [`is_skill_file`] and [`skill_stem`] widen from
//! `pub(crate)` to `pub` (mirroring `agents::deployer::is_agent_file`, #2892)
//! so trusty-mpm's `skill_staleness` module can keep calling them cross-crate.
//! What: [`deploy_skills`] enumerates the source directory's skills through
//! [`crate::skills::source_scan::scan_skill_sources`] — which accepts BOTH a
//! flat `<stem>.md` and a directory-shaped `<stem>/SKILL.md` (#4949) — and
//! writes each one as `~/.claude/skills/<name>/SKILL.md`. It consults the
//! [`SkillManifest`] to classify each target file and writes only the files it
//! safely may. Every file a multi-file skill carries alongside its entry point
//! travels with it under the identical ownership rule, keyed as
//! `<stem>/<relative-path>` (issue #2903 introduced this for
//! `references/*.md`; #4949 widened it to `metadata.json`, `scripts/`, and any
//! other carried file). It returns a [`DeployStats`] summarising what
//! happened, including — since #4949 — the names it could not deploy.
//! Test: `cargo test -p trusty-agents-common skills::deployer` covers a new
//! deploy, a skipped user-modified file, an unchanged file, a user-owned
//! file, the reference-file mirroring cases, and the directory-shaped-skill
//! cases (`deploy_directory_shaped_skill` and siblings).
use Path;
use crate;
use crate;
use crate;
/// Summary of one [`deploy_skills`] run.
///
/// Why: callers print per-file status; they need the file lists split by
/// outcome to render that summary and to know whether any work was skipped.
/// What: filenames grouped into freshly written, skipped (user-owned or
/// user-modified), and unchanged (checksum already current).
/// Test: every `deploy_*` test asserts on these vectors.
/// Whether a source filename names a skill file to deploy.
///
/// Why: the source directory holds `.md` files; only those should be deployed,
/// and any manifest file or hidden file must be ignored. Exported (PR #2818
/// review, MEDIUM) so `skills::tiers::list_source_stems` enumerates
/// exactly the same file set this deployer will act on — a second,
/// independently-written filter here would risk drifting out of sync.
/// What: returns `true` for `*.md` files that do not start with `.`.
/// Test: covered indirectly by `deploy_new_skill`;
/// `crate::skills::tiers::tests::list_source_stems_reads_md_files` exercises
/// it via the shared helper.
/// Derive the skill name (manifest key and target directory name) from a
/// source filename.
///
/// Why: sources are flat `<name>.md` files but the deploy target is
/// `<dest>/<name>/SKILL.md`. Stripping `.md` gives the shared name. Exported
/// (PR #2818 review, MEDIUM) so `skills::tiers::list_source_stems` derives
/// stems with the IDENTICAL single-strip semantics this deployer uses — a
/// second `trim_end_matches(".md")`-based implementation would repeat-strip a
/// pathological `foo.md.md` filename to `foo`, while this deployer's own
/// `deploy_skills_filtered` (via [`is_skill_file`] + this function) treats it
/// as stem `foo.md`; the mismatch meant the tier planner would greenlight a
/// stem the deployer's `select` predicate then rejected, so the skill silently
/// never deployed.
/// What: returns the filename without its `.md` suffix (single strip).
/// Test: covered indirectly by every `deploy_*` test;
/// `crate::skills::tiers::tests::list_source_stems_reads_md_files` exercises
/// it via the shared helper.
/// Whether a skill's carried file deploys executable (`0o755`), by its path.
///
/// Why: #10211 — the content tarball pins every entry to mode `0644`
/// (`scripts/package_content.sh`), so a script's exec bit cannot travel through
/// the bundle; the path below the skill directory is the one contract every
/// writer (the deployer, trusty-mpm's source materializer and its drift repair)
/// can read. A reference file never earns the bit by this rule.
/// What: `true` when `rel` (the `/`-joined path below `<stem>/`, e.g.
/// `scripts/check.sh`) has `scripts` as its first segment.
/// Test: `scripts_are_executable_by_path`, `deploy_scripts_carry_the_exec_bit`.
/// Whether the source copy of a carried file is itself executable.
///
/// Why: #10211 — a dev checkout or a user's own skill source can carry the bit
/// outside `scripts/`; a deploy keeps it rather than quietly dropping it.
/// What: any exec bit in the source file's mode. Unreadable metadata, or a
/// non-Unix host, reads as `false`.
/// Test: `deploy_keeps_a_source_exec_bit_outside_scripts`.
/// Deploy all skills from `source` to `dest`.
///
/// Why: ensures `~/.claude/skills/` has up-to-date skill files without
/// clobbering user-owned or user-modified files.
///
/// Rules:
/// - Name (stem) contains "mcp" (case-insensitive) → collides with Claude
/// Code's built-in `/mcp` command → skip, never written (#2186)
/// - Not in manifest, file exists → user-owned → skip silently
/// - In manifest, checksum matches deployed copy → overwrite when stale
/// - In manifest, checksum differs → user-modified → skip
/// - New trusty-mpm skill → write + add to manifest
///
/// Test: `deploy_new_skill`, `deploy_skips_user_modified`,
/// `deploy_unchanged_no_write`, `deploy_user_owned_skipped`,
/// `deploy_skips_mcp_named_skill`.
/// Deploy skills from `source`, restricting to those `select` accepts.
///
/// Why: HR-2 manifests describe a skill *set* (include/exclude globs); the
/// session-launch path must deploy a subset of the source skills without copying
/// the whole directory. Factoring the selection into a predicate keeps the
/// manifest logic in `session_launch` while reusing the identical
/// ownership/atomic-write machinery here.
/// What: identical to [`deploy_skills`] except a source skill whose stem (the
/// `.md`-stripped name) `select` rejects is skipped before any write.
/// [`deploy_skills`] delegates here with an accept-all predicate, so existing
/// behavior is unchanged. Deselecting a skill does not remove a previously
/// deployed copy (an HR-3 concern).
/// Test: `deploy_filtered_respects_predicate`.
/// The body of [`deploy_skills_filtered`], run while holding the ledger lock.
///
/// Why (#4881): split out so the critical section is a single expression the
/// lock helper wraps, and so the lock's scope is impossible to misread — every
/// manifest load, file write, and manifest save in this function happens with
/// the lock held. Mirrors `agents::deployer::deploy_agents_locked`.
/// What: the classify/write/save pipeline documented on
/// [`deploy_skills_filtered`]. Never call it directly; it is unsafe against
/// concurrent writers by construction.
/// Test: covered by every `deploy_*` test through the public wrapper, plus
/// `deploy_blocks_while_the_skill_ledger_lock_is_held`.
/// Write every selected skill and record it, stopping at the first I/O failure.
///
/// Why (#4881): split out of [`deploy_skills_locked`] so its `?` sites cannot
/// bypass the manifest save. Files this function has already written are on disk
/// whether it returns `Ok` or `Err`, so the caller must save the ledger on BOTH
/// paths — a shape that is easy to get wrong while the loop and the save share
/// one function body, and was wrong here before this change.
/// What: for each skill, writes `<dest>/<stem>/SKILL.md` plus every file the
/// skill carries through [`deploy_one_file`], recording each in `manifest` and
/// `stats`. Skills whose stem contains `mcp` are rejected before any I/O
/// (#2186). Returns the first I/O error; `manifest` and `stats` still describe
/// everything written up to that point.
/// Test: `deploy_records_files_written_before_a_mid_loop_failure`,
/// `deploy_directory_shaped_skill`, plus every `deploy_*` test through the
/// public entry point.
/// Deploy one managed file (a skill's `SKILL.md` or one of its
/// `references/*.md` siblings) under the shared ownership rules.
///
/// Why: [`deploy_skills_filtered`] applies the IDENTICAL
/// managed/unmanaged/unchanged/stale classification to both the entry-point
/// `SKILL.md` (keyed by the bare stem) and each multi-file skill's
/// `references/*.md` siblings (keyed by `<stem>/references/<file>`, issue
/// #2903, ported from `trusty-mpm` PR #2915) — factoring it out here is what
/// keeps those two call sites from silently diverging on the ownership rule.
/// What: writes `content` to `target_path` (creating parent directories as
/// needed) via [`atomic_write`] — [`atomic_write_executable`] when
/// `executable` (#10211) — and records `key` in `manifest` UNLESS the
/// target exists and is either unmanaged (user-owned — skip) or managed with
/// a checksum mismatch (user-modified — skip), or is managed with a matching
/// checksum and `content` is unchanged (unchanged — no-op, except that a
/// managed script missing its exec bit gets the bit back). Every outcome is
/// pushed to the matching `stats` vector under `key`.
/// Test: `deploy_new_skill`, `deploy_skips_user_modified`,
/// `deploy_unchanged_no_write`, `deploy_user_owned_skipped`, plus the
/// reference-file tests in `deployer_tests.rs`
/// (`deploy_reference_files_land_alongside_skill`,
/// `deploy_reference_files_skip_user_modified`,
/// `deploy_reference_files_sync_even_when_entry_unchanged`,
/// `deploy_scripts_carry_the_exec_bit`,
/// `redeploy_restores_a_lost_exec_bit_on_an_unchanged_script`).