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
//! Multi-tier skill DEPLOY-TIME source precedence — project-custom >
//! user-custom > bundled. NOT Claude Code's runtime resolution (#4958): this
//! decides which SOURCE is written into one `dest`, never which of several
//! on-disk copies loads.
//!
//! Why: skills are deployed per-project into `<project>/.claude/skills/`, and
//! historically only ONE source fed that deploy (the bundled framework skills,
//! see [`crate::skills::deployer`]). Two custom tiers were missing as
//! first-class, documented concepts (issue #2816). The `project-custom` tier is
//! a skill a user hand-places directly in `<project>/.claude/skills/<name>/`;
//! Claude Code already discovers it and the deployer's ownership manifest
//! already preserves it (an unmanaged file is never overwritten), but nothing
//! NAMED it a tier or let it deliberately shadow a same-named bundled skill. The
//! `user-custom` tier is a skill authored once in `~/.trusty-mpm/skills/` and
//! deployed into EVERY project, ranking above the shipped portfolio. This
//! mirrors the user-level agent source (`~/.trusty-mpm/agents/`) and the
//! agent-precedence work tracked in #387 so the two land consistently. Moved
//! to trusty-agents-common (#2892, #2818) from `trusty-mpm::core::skill_tiers`
//! so a second harness (trusty-code) can reuse the same precedence resolver;
//! `trusty-mpm` re-exports every item here from `core::skill_tiers` for source
//! compatibility.
//!
//! What: [`plan_skill_tiers`] is the PURE precedence resolver — given the skill
//! stems present in each tier it decides which stems each *deployable* tier
//! (user, bundled) should actually write and records the shadow relationships
//! for logging. [`deploy_all_skill_tiers`] is the thin IO orchestrator that
//! enumerates each tier's stems, runs the planner, and drives the existing
//! [`crate::skills::deployer::deploy_skills_filtered`] once per deployable
//! tier so the manifest-based ownership/atomic-write machinery is reused
//! unchanged.
//! Test: see `plan_disjoint_all_deploy` and the sibling `plan_*` unit tests for
//! the pure precedence table, and `deploy_all_user_overrides_bundled` and its
//! `deploy_all_*` siblings for the end-to-end tier merge over real directories.
use BTreeSet;
use Path;
use crateResult;
use crate;
use crateSkillManifest;
/// The three skill source tiers, ordered highest precedence first.
///
/// Why: shadow logging and the planner need a stable, comparable identity for
/// each tier so a collision can name its winner and loser.
/// What: `Project` (hand-placed in the project's `.claude/skills/`) outranks
/// `User` (`~/.trusty-mpm/skills/`), which outranks `Bundled` (shipped).
/// Test: `plan_project_shadows_user_and_bundled`.
/// One name-collision record: `winner` shadows `loser` for skill `stem`.
///
/// Why: callers log every collision so an operator understands why a
/// lower-precedence skill of a given name was not deployed.
/// What: the colliding stem plus the winning and losing tiers.
/// Test: `plan_user_shadows_bundled`, `plan_project_shadows_user_and_bundled`.
/// The deploy decision produced by [`plan_skill_tiers`].
///
/// Why: the orchestrator needs, per deployable tier, exactly which stems to
/// write (so no two tiers fight over the same target and each skill is written
/// at most once), plus the shadow records for logging.
/// What: the bundled and user stems that survive precedence, and every
/// collision as a [`Shadow`]. Project-custom stems are never "deployed" — they
/// already exist on disk and are preserved by the manifest ownership model — so
/// they appear only as shadow winners.
/// Test: every `plan_*` test asserts on these fields.
/// Resolve a single skill stem's tier via the 3-tier precedence, or `None` if
/// absent from every tier.
///
/// Why (DOC-42, issue #2889): agent-bundled skills need the SAME precedence
/// rule [`plan_skill_tiers`] encodes, but for one declared skill name at a
/// time — co-deployment logging, `tm doctor`'s dangling-reference check, and
/// `tm agent show`'s per-skill tier column all ask "which tier (if any) does
/// THIS ONE stem resolve to?" rather than planning a full multi-stem deploy.
/// What: `Project` if `project` contains `stem`, else `User` if `user`
/// contains it, else `Bundled` if `bundled` contains it, else `None` (the
/// skill does not exist in any tier — a dangling reference).
/// Test: `resolve_tier_project_wins`, `resolve_tier_user_wins_over_bundled`,
/// `resolve_tier_bundled_only`, `resolve_tier_absent_is_none`.
/// Resolve tier precedence for a set of skill stems.
///
/// Why: precedence must be decided in ONE pure, exhaustively-tested place so
/// the IO orchestrator stays trivial and the rule (`project > user > bundled`)
/// cannot drift between call sites.
/// What: for each user stem, it is suppressed iff the project tier also has it
/// (project shadows user); otherwise it deploys. For each bundled stem, it is
/// suppressed iff EITHER higher tier has it (the nearer tier — project, else
/// user — is recorded as the winner); otherwise it deploys. Collisions are
/// returned sorted by `(stem, winner-precedence)` for stable output.
/// Test: `plan_disjoint_all_deploy`, `plan_user_shadows_bundled`,
/// `plan_project_shadows_user_and_bundled`, `plan_project_shadows_user_only`.
/// List the deployable skill stems in a source directory.
///
/// Why: the planner works over stem sets; the orchestrator must translate a
/// source directory into that set using the EXACT SAME scan
/// `deploy_skills_filtered` uses. A second, independently-written rule here
/// previously used a repeated `trim_end_matches(".md")` strip against the
/// deployer's single `strip_suffix(".md")` (PR #2818 review, MEDIUM): for a
/// pathological `foo.md.md` source file the planner would derive stem `foo`
/// while the deployer expected `foo.md`, so the planner would greenlight a stem
/// the deployer then silently rejected and the skill never deployed. Calling
/// the deployer's own scan makes that drift structurally impossible — and #4949
/// proved the point again from the other direction, where the two copies were
/// identically blind to directory-shaped skills.
/// What: returns the stem of every skill
/// [`crate::skills::source_scan::scan_skill_sources`] finds in `dir` — the flat
/// `<stem>.md` form and, since #4949, the directory `<stem>/SKILL.md` form. A
/// missing or non-directory path yields an empty set (an absent tier is not an
/// error). Errors reading an existing directory propagate.
/// Test: `list_source_stems_reads_md_files`, `list_source_stems_missing_empty`,
/// `list_source_stems_reads_directory_skills`.
/// List the project-custom skill stems already present in the deploy target.
///
/// Why: a project-custom skill is one the user hand-placed in the project's
/// `.claude/skills/` — it is on disk but absent from the deploy manifest. The
/// planner needs those names to let them deliberately shadow same-named user or
/// bundled skills.
/// What: scans `<dest>/<stem>/SKILL.md` directories and returns each `<stem>`
/// that the deploy manifest does NOT manage. A missing target yields an empty
/// set. Errors reading an existing directory propagate.
/// Test: `project_custom_stems_finds_unmanaged`.
/// Result of a multi-tier deploy: the merged stats plus every shadow.
///
/// Why: callers want the same [`DeployStats`] shape they had with the single
/// source, plus the collision records to surface in logs / diagnostics.
/// What: the union of both deploy runs' stats and the planner's shadow list.
/// Test: `deploy_all_user_overrides_bundled`, `deploy_all_preserves_project`.
/// Deploy the bundled and user-custom skill tiers into `dest`, honoring
/// precedence project-custom > user-custom > bundled.
///
/// Why: this is the single entry point session provisioning calls in place of
/// the old single-source deploy — it adds the user tier and makes project
/// precedence explicit while reusing the existing per-file ownership machinery.
/// What: builds each tier's stem set (bundled filtered by `select`; user-custom
/// deployed in full — an explicit user opt-in is not subject to the harness
/// manifest's bundled include/exclude), plans precedence, then runs
/// [`deploy_skills_filtered`] for the bundled-then-user survivors. Project-custom
/// files already on disk are never rewritten (the deployer skips unmanaged
/// targets). Deploying the two tiers over DISJOINT stem sets means each skill is
/// written at most once and the winner is never clobbered by a loser.
/// Test: `deploy_all_user_overrides_bundled`, `deploy_all_preserves_project`,
/// `deploy_all_no_user_tier_matches_bundled_only`.
/// Fold one deploy pass's stats into the running total.
///
/// Why: two deploy passes each return their own [`DeployStats`]; callers want a
/// single merged view.
/// What: appends each vector. The passes deploy disjoint stem sets, so no
/// de-duplication is required.
/// Test: covered via `deploy_all_*` end-to-end assertions.