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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Materialize the embedded agent roster to `<project>/.trusty-code/agents/`
//! with a manifest and recorded provenance (#2074, epic #2892).
//!
//! Why: before this, `crate::assets::DEFAULT_AGENTS` existed only in memory.
//! An operator could not read the prompt an agent actually runs, diff it, or
//! edit it, and nothing on disk recorded which files Trusty Code wrote versus
//! which the project owns. #2074 requires source provenance for every shipped
//! role, and provenance needs a file and a ledger to attach to.
//!
//! What: [`ensure_roster_deployed`] stages the compiled-in agent sources into a
//! throwaway directory and hands that directory to
//! `trusty_agents_common::agents::deployer::deploy_agents_filtered` — the same
//! writer trusty-mpm's `~/.claude/agents/` deploy uses, unchanged. That
//! deployer already takes `source_dir`/`target_dir` as plain parameters, so
//! reusing it needed no shared-library change. It brings the strict-YAML
//! validation of every composed agent, the per-agent compose-failure isolation,
//! the `.trusty-mpm-manifest.json` ledger with a recorded checksum and
//! [`Origin`], and the refusal to proceed on a corrupt ledger.
//!
//! ## Three policies this module adds on top of the shared deployer
//!
//! **A compatibility root that currently wins is never shadowed.** `.claude/`
//! and `.open-mpm/` remain readable discovery INPUTS (#5426), and
//! `crate::paths::agents_dir` prefers `.trusty-code/agents` over both. Writing
//! the roster into `.trusty-code/agents` while a project's `.claude/agents/`
//! holds its real catalog would silently demote that catalog, so the deploy is
//! skipped in that case — [`SkipReason::CompatRootWins`].
//!
//! **A hand-edited deployed file is authoritative.** The shared deployer treats
//! a checksum mismatch on an [`Origin::Bundled`] entry as corruption and repairs
//! it (#4408) — correct for trusty-mpm's machine-global `~/.claude/agents/`,
//! wrong for a project-local directory whose whole contract is
//! `load_all_agents`' "disk wins, never merged". This module therefore selects
//! against the ledger before deploying: a tracked file whose on-disk content no
//! longer matches its recorded checksum is deselected, so the deployer never
//! composes or writes it. Pristine files still refresh, and an UNTRACKED file is
//! left to the deployer's own adopt-or-skip branch. No deployer change was
//! needed for either policy. Since #7779 the policy covers REAL files only: a
//! symlinked entry in the agents directory refuses the deploy instead of being
//! read through, because the pinned handle opens every entry `O_NOFOLLOW` (the
//! `write_dir` module header states why that is uniform rather than per-tree).
//!
//! **Every write reaches the project through a pinned directory handle.** The
//! shared deployer writes through plain paths — correct for trusty-mpm's
//! machine-global `~/.claude/agents/`, where a symlinked `~/.claude` is a
//! legitimate setup, and wrong for a project directory a racing writer can swap
//! (#7779). It therefore runs against a private scratch copy, and the ledger and
//! the files it produced are published back `openat`-relative to
//! [`crate::paths::write_dir::NativeWriteDir`]. The project's own ledger lock is
//! taken through that same handle, so cross-process serialisation is unchanged.
//!
//! Test: `agents::deploy::deploy_tests::*`,
//! `tests/roster_deploy_e2e.rs`.
//!
//! [`Origin`]: trusty_agents_common::agents::manifest::Origin
//! [`Origin::Bundled`]: trusty_agents_common::agents::manifest::Origin::Bundled
use HashSet;
use ;
use AgentBuildError;
use ;
use ;
use crateSKILL_REFS_DIRNAME;
use crate;
use crateNativeWriteDir;
use crate;
/// The `agents` subdirectory name, shared by every configuration root.
///
/// Why: named once so the write target cannot drift from the read target
/// `crate::paths::agents_dir` resolves.
/// What: `"agents"`.
/// Test: `roster_target_is_under_the_native_config_dir`.
pub const AGENTS_DIRNAME: &str = "agents";
/// Why a roster deploy could not run.
///
/// Why: each variant has a different fix, and collapsing them into one string
/// would hide which. A refused write target means the project layout is wrong; a
/// corrupt ledger means ownership cannot be established and must be repaired
/// before anything is written.
/// What: the four failure modes [`ensure_roster_deployed`] can return.
/// Test: `corrupt_manifest_is_reported_and_nothing_is_written`.
/// Why a deploy was skipped rather than attempted.
///
/// Why: "nothing was written" is a legitimate outcome the caller must be able to
/// log accurately, and it is not an error.
/// What: the single skip condition today — a compatibility root currently
/// resolves the agents directory.
/// Test: `deploy_is_skipped_when_claude_agents_dir_wins`.
/// What one [`ensure_roster_deployed`] call did.
///
/// Why: the caller logs a different line for each, and a test asserts on which
/// branch ran rather than inferring it from the filesystem.
/// What: either a skip with its reason, or the deployer's own
/// [`DeployResult`] alongside the directory it wrote into.
/// Test: `fresh_project_materializes_the_whole_roster`,
/// `deploy_is_skipped_when_claude_agents_dir_wins`.
/// What the deployed-agent ledger looks like right now.
///
/// Why: `tcode paths show` answers "which root wins"; after #2074 it must also
/// answer "is there a deployed roster, and is its ledger readable" — the
/// question an operator debugging a stale or hand-edited agent asks next.
/// What: absent (nothing deployed yet), present with a managed-file count, or
/// corrupt with the underlying detail.
/// Test: `manifest_status_reports_absent_then_present`.
/// The ledger path and its current state for a project.
///
/// Why: `tcode paths show` needs both, and deriving the path a second time at
/// the call site would be a second implementation of the layout rule.
/// What: `<project>/.trusty-code/agents/.trusty-mpm-manifest.json` and what
/// reading it yields. Absence is distinguished from an empty ledger by testing
/// the file itself — `AgentManifest::load_checked` reports both as `Ok`.
/// Test: `manifest_status_reports_absent_then_present`,
/// `manifest_status_reports_corrupt`.
/// `<project>/.trusty-code/agents` — the only directory this module writes.
///
/// Why: the write target is deliberately NOT `crate::paths::agents_dir`'s
/// result, which may legitimately resolve into `.claude/`. The read path and the
/// write path are different functions on purpose (#5426).
/// What: [`crate::paths::native_child`] for [`AGENTS_DIRNAME`].
/// Test: `roster_target_is_under_the_native_config_dir`.
/// Materialize the embedded roster into `<project>/.trusty-code/agents/`.
///
/// Why: #2074's provenance requirement — every shipped role needs a file, a
/// recorded checksum, and an [`Origin`] on disk, not just an in-memory
/// projection. `crate::agents::load_embedded_default_agents` stays the zero-disk
/// fallback for a projectless session, which has no project root to write to.
///
/// What, in order:
/// 1. Skip when `.claude/agents/` or `.open-mpm/agents/` currently wins
/// discovery — see this module's docs.
/// 2. Stage every source into a scratch directory, including the five `BASE-*`
/// templates so the deployer's own `extends:` composer resolves the chains
/// from disk exactly as it does for trusty-mpm. The shared texts come from
/// content (#9011); with none, this fails before anything below creates the
/// agents directory or its lock (D5).
/// 3. PIN the agents directory with [`crate::paths::write_dir::NativeWriteDir`],
/// which applies the unchanged ADR-0044 membership rule and then holds the
/// directory open on a descriptor. Every write below is `openat`-relative to
/// it, so a symlink swapped in afterwards cannot redirect one (#7779).
/// 4. Take the project's ledger lock through the pinned handle, so concurrent
/// `tcode` daemons still serialise on the same sidecar the shared deployer
/// would have used. It is a blocking `LOCK_EX` held across the whole
/// mirror/compose/publish sequence — wider than the shared deployer's,
/// which takes it around the compose only, and deliberately so: the mirror
/// that decides "hand-edited" and the publish that acts on that decision have
/// to see the same directory.
/// 5. Refuse to proceed on a corrupt ledger, naming the file. Never reset it.
/// 6. PIN the skill-refs tree — after step 5, so a refused deploy leaves no
/// empty directory behind — and materialize the roster's skill pointers.
/// 7. Deploy into a second scratch directory seeded from the pinned handle,
/// selecting the dispatchable roster only and deselecting any tracked file
/// the user has since edited, then publish the result back through the handle.
///
/// Test: `fresh_project_materializes_the_whole_roster`,
/// `hand_edited_agent_survives_a_second_deploy`,
/// `corrupt_manifest_is_reported_and_nothing_is_written`,
/// `symlinked_roster_file_is_refused_before_any_write`,
/// `deploy_is_skipped_when_claude_agents_dir_wins`,
/// `base_templates_are_never_deployed`,
/// `symlinked_skill_refs_dir_is_refused_before_any_write`,
/// `symlinked_skill_folder_is_refused_before_any_write`,
/// `symlinked_skill_ref_file_is_refused_before_any_write`,
/// `tests/roster_deploy_e2e.rs`,
/// `no_content_writes_no_agents_dir_and_no_lock` (`tests/no_content_e2e.rs`).
///
/// [`Origin`]: trusty_agents_common::agents::manifest::Origin
/// Copy the ledger and every roster file the deployer may read into `scratch`.
///
/// Why: the deployer decides adopt / refresh / preserve by reading the target
/// directory, so running it against an empty scratch tree would reclassify every
/// existing file as new and overwrite a hand-edited agent.
/// What: reads `<MANIFEST_FILE>` and `<name>.md` for every roster name through
/// the PINNED handle — the same descriptor the publish writes back through, so
/// the snapshot and the publish cannot disagree about which directory they mean.
/// Absent entries are simply not copied.
///
/// A SYMLINKED entry refuses the whole deploy rather than being followed, which
/// narrows this module's "a hand-edited deployed file is authoritative" policy
/// to real files (#7779 round 2; rationale in the `write_dir` module header).
/// Test: `agents::deploy::deploy_tests::hand_edited_agent_survives_a_second_deploy`,
/// `agents::deploy::deploy_tests::symlinked_roster_file_is_refused_before_any_write`.
/// Publish what the deploy produced back through the pinned handle.
///
/// Why: the only writes that reach the project directory. Doing them
/// `openat`-relative to the validated descriptor is what makes "the path
/// validated is the path written" true for the agents directory (#7779).
/// What: the ledger (always — an adoption changes it without rewriting a file)
/// plus every file the deployer reported as deployed. Files it left alone are
/// not republished, so a preserved hand-edit is not even rewritten with its own
/// bytes. A swap detected mid-publish aborts with
/// [`WriteTargetError::Unpinned`]; nothing was written outside the directory.
///
/// Every file is READ before any is written. #7779: a `continue` on an
/// unreadable scratch file dropped that file while the ledger published its
/// checksum anyway, so [`ensure_roster_deployed`] returned `Deployed` and
/// [`deploy_and_log`] logged a file that is not on disk — fail-open. Reading
/// first makes the run all-or-nothing up to the first `atomic_write`.
/// Test: `agents::deploy::deploy_tests::fresh_project_materializes_the_whole_roster`,
/// `agents::deploy::deploy_tests::swapped_agents_dir_never_reaches_the_victim`,
/// `agents::deploy::deploy_tests::unreadable_scratch_file_fails_the_publish`.
/// Run [`ensure_roster_deployed`] for a bound project and log what happened.
///
/// Why: two entry points materialize the roster — the daemon's router build and
/// the legacy in-process `run-task` — and both want the same behaviour: deploy
/// if we can, report loudly if we cannot, and never abort the run. A failed
/// deploy is recoverable, because `crate::agents::load_embedded_default_agents`
/// still answers every resolution from memory; aborting would turn a repairable
/// ledger into an unusable harness.
/// What: `None` when `project_root` is `None` (a projectless session writes
/// nothing and keeps the in-memory embed). Otherwise the outcome, with a
/// `tracing::error!` naming the failure when one occurred. #2074. No content
/// installed is reported once per process with the loader's line (#9011 D4).
/// Test: `deploy_and_log_is_a_no_op_without_a_project`,
/// `deploy_and_log_reports_a_corrupt_ledger_without_panicking`.
/// Whether a tracked deployed file has been edited since Trusty Code wrote it.
///
/// Why: the "hand-edited file is authoritative" policy from this module's docs.
/// The shared deployer would repair such a file (#4408); a project-local
/// directory must preserve it instead.
/// What: `true` only when the ledger tracks `<stem>.md` AND the file on disk no
/// longer matches its recorded checksum. An untracked file returns `false` so
/// the deployer's own adopt-or-skip branch decides; an unreadable or absent file
/// returns `false` so it is (re)deployed.
/// Test: `hand_edited_agent_survives_a_second_deploy`,
/// `untracked_file_is_left_to_the_deployer`.
/// Write every built-in agent source into a throwaway directory.
///
/// Why: the shared deployer composes from a source DIRECTORY, and tcode's roster
/// is read from content into memory. Staging it is what lets the deployer be
/// reused verbatim instead of growing a second, pre-composed entry point — a
/// shared library change this slice deliberately avoided.
/// What: resolves content once and reads every roster text
/// ([`crate::assets::content::AgentSources`], #9011/#9013) and the skill-ref
/// files ([`super::skill_refs::SkillRefs`]); a content error is
/// [`RosterDeployError::Content`] and nothing is staged. Then one `.md` per
/// catalog entry (keyed by its filename, so `extends: base-qa` resolves
/// against `BASE-QA.md`) plus one per [`EmbeddedAgent::Direct`] in
/// [`DEFAULT_AGENTS`]. The `TempDir` is returned so the caller keeps it alive
/// across the deploy; dropping it removes the scratch tree.
/// Test: `staged_sources_cover_every_roster_name_and_base_template`.