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
//! Self-healing (against binary upgrade) materialization + auto-deploy of the
//! bundled agent *source* directory (`~/.trusty-mpm/framework/agents/`) —
//! issue #4840.
//!
//! Why: the agent roster (`content/agents/*.md` since #9011, resolved at
//! runtime by [`crate::core::content_source`]; compiled in before that), but
//! the ONLY code path that ever wrote it to disk was the
//! separate, manual `tm install`. Rebuilding the binary did not trigger it;
//! starting a session did not trigger it. So a merged, compiled-in instruction
//! change (e.g. a new `BASE-AGENT.md` rule) silently had no effect until
//! someone remembered to re-run `tm install` — measured on 2026-08-04 as a
//! deployed `BASE-AGENT.md` three days older than the binary containing the
//! fix, with no warning anywhere. Skills already solved exactly this
//! (`skill_source::ensure_skill_source_fresh`, issue #1917); agents were the
//! missing half.
//!
//! What: [`agent_bundle_stamp`] fingerprints the resolved agent roster via
//! sha256 — this is the cheap gate, so the common
//! "nothing changed" case costs one file read and one string compare.
//! [`materialize_agent_artifacts`] writes every bundled agent into the source
//! directory and prunes any `.md` file the current table no longer lists (a
//! renamed or removed agent). [`ensure_agent_source_fresh`] compares the stamp
//! against a marker file and re-materializes only on a mismatch.
//! [`autodeploy_agents`] is the entry point session provisioning calls: it
//! refreshes the source, deploys it, and returns a report — it NEVER returns
//! an error, because a broken deploy must not block a session (a stale file is
//! the strictly lesser failure). Anything it declined to overwrite, and
//! anything that failed to compose at all, is summarised in
//! [`AgentAutodeploy::warnings`], closing the other half of #4840: the
//! deployer's user-edited-file protection previously skipped silently.
//!
//! Overwrite policy (#4840, decided): a BUNDLED-origin file whose checksum
//! drifted IS overwritten — the deploy tracker (`.trusty-mpm-manifest.json`)
//! records `origin`, and `agents::deployer` already treats framework-owned
//! drift as corruption rather than user ownership (#4408). Auto-deploy
//! inherits that unchanged. Only an UNTRACKED file that differs, or a
//! user-owned (seed-once tier) entry that was edited, is preserved — and now
//! warned about by name.
//!
//! Test: `crates/trusty-mpm/src/core/agent_source_tests.rs`.
use HashSet;
use Path;
use crate;
use crate;
use crate;
use crateResult;
use crateFrameworkPaths;
/// Marker file recording the last-materialized bundle stamp, written directly
/// under the agent source directory (`<agents_dir>/.bundle-stamp`).
///
/// Mirrors `skill_source::STAMP_FILE_NAME`; hidden (leading `.`) so the prune
/// sweep never considers it a stale agent. `pub(crate)` so a test can pin a
/// hand-seeded agent source as already-current and stop the self-heal
/// materializing the real compiled-in bundle over it.
pub const STAMP_FILE_NAME: &str = ".bundle-stamp";
/// Outcome of one [`autodeploy_agents`] run.
///
/// Why: the caller needs to know whether anything changed (to log it) and,
/// critically, WHICH files were left stale — #4840's second half is that a
/// declined overwrite was silent.
/// What: `refreshed` is `true` when the source directory was re-materialized
/// from the compiled-in bundle this run; `deployed` lists the composed agent
/// files actually (re)written into the target; `warnings` carries a BOUNDED
/// summary — at most one line for the stale set, one for the failed set, plus
/// one line if the refresh or the deploy itself failed outright.
/// Test: every `autodeploy_*` case in `agent_source_tests.rs`.
/// Compute a stable sha256 fingerprint over every file of `roster`.
///
/// Why: this is the gate that makes auto-deploy cheap. It detects "this binary
/// embeds different agent content than what is on disk" without depending on
/// file mtimes, which a backup/restore (or a `cargo install` that preserves
/// timestamps) can shuffle.
/// What: concatenates `agents/<name>\0<contents>\n` for every roster file, in
/// roster order, and returns the sha256 hex digest.
/// Test: `agent_bundle_stamp_is_stable_across_calls`,
/// `agent_bundle_stamp_differs_from_skill_stamp`.
/// Write every `roster` file into `agents_dir`, pruning any `.md` file on disk
/// the roster no longer lists.
///
/// Why: `agents_dir` (`~/.trusty-mpm/framework/agents/`) is a framework-owned
/// artifact directory — every entry is written exclusively by trusty-mpm's own
/// installer/self-heal path, never hand-edited (the USER-level agent source is
/// the separate `~/.trusty-mpm/agents/`). So it is safe, and necessary for the
/// renamed-agent case, to prune rather than only add.
/// What: creates `agents_dir` if absent, atomically (over)writes each roster
/// file to `<agents_dir>/<name>`, then removes any remaining top-level `*.md`
/// file that is not one of the files just written. Hidden files (leading `.`, e.g. the stamp marker and the deploy
/// manifest) are never touched. Returns the basenames written.
/// Test: `materialize_agent_artifacts_writes_all_agents`,
/// `materialize_agent_artifacts_prunes_files_not_in_table`.
/// Ensure `agents_dir` reflects the resolved agent roster, re-materializing it
/// when missing or stale.
///
/// Why: the self-healing entry point behind [`autodeploy_agents`] — it removes
/// the dependency on a prior, separate `tm install` having refreshed the
/// source directory (#4840).
/// What: resolves the roster ([`content_source::agent_roster`]; a content
/// error is `Err`), then [`ensure_agent_source_fresh_with`]: compares
/// [`agent_bundle_stamp`] against `<agents_dir>/.bundle-stamp`;
/// when they differ (including "stamp file absent", which covers a missing or
/// never-materialized directory), calls [`materialize_agent_artifacts`] and
/// rewrites the stamp. Returns `true` when a refresh happened, `false` when
/// the source was already current.
/// Test: `ensure_agent_source_fresh_materializes_when_missing`,
/// `ensure_agent_source_fresh_is_noop_when_current`,
/// `ensure_agent_source_fresh_prunes_renamed_files`.
/// [`ensure_agent_source_fresh`] against a roster already resolved.
/// `count` + a short preview of `items`, for a one-line summary.
///
/// What: joins the first `limit` entries with `, `, appending `, …` when more
/// were elided. Pure.
///
/// #4873: shared with `managed_config::skill_skip_summary`, which needs the
/// identical count-plus-preview shape for the SKILL half of the same warning
/// policy — a second copy would drift from this one.
/// Test: exercised through `deploy_summary_lines_*` and
/// `skill_skip_summary_*`.
pub
/// A bounded summary of what a deploy left stale and what it failed to compose.
///
/// Why: #4840's second half is that `agents::deployer` preserves an untracked-
/// and-differing file, and a user-owned (seed-once tier) entry that was edited,
/// **silently** — which is precisely what let a three-day-stale `BASE-AGENT.md`
/// go unnoticed. But this runs on EVERY spawn and every resume, and the stale
/// set is never reconciled until someone runs `--reset-agents`, so one line per
/// file would be permanent, dozens-wide log spam. `agents::deployer` already
/// settled that tradeoff for its own warning (issue #2504: count + preview);
/// this is the same policy, not a second one. PR #4848 review (HIGH).
/// What: at most two lines — one for [`DeployResult::skipped`] (with the
/// actionable `tm install --reset-agents <name>` pointer when
/// [`DeployResult::untracked_modified`] is non-empty — `--reset-agents`
/// resolves the untracked-and-differing case, not an edited seed-once entry,
/// so the pointer never names a deliberate user customization), one for
/// [`DeployResult::failed`], whose agents did not land AT ALL and are therefore
/// worse than stale. Each is a count plus a short preview. Returns an empty
/// vector on a clean deploy. Pure — no I/O, no logging.
/// Test: `deploy_summary_lines_is_empty_on_a_clean_deploy`,
/// `deploy_summary_lines_summarises_skips_in_one_line`,
/// `deploy_summary_lines_omits_the_reset_pointer_for_user_owned_edits_only`,
/// `deploy_summary_lines_names_the_untracked_modified_file_in_the_pointer`,
/// `deploy_summary_lines_reports_failed_agents`.
/// Deploy `source_dir` into `target_dir`, folding the outcome into `out`.
///
/// What: on success records the deployed set and appends
/// [`deploy_summary_lines`]; on failure appends a single fail-open warning.
/// Test: exercised through every `autodeploy_agents*` case.
/// Whether `dir` holds at least one non-hidden `.md` file.
///
/// Why: an *empty* `agents/agents` directory — an uninitialized git submodule
/// on a source checkout — satisfies `agent_source_dir()`'s `is_dir()` test but
/// is not an authoritative source; deploying from it lands nothing, silently.
/// PR #4848 review. #4840 was originally measured on a source checkout, so this
/// is not hypothetical.
/// What: a shallow read of `dir`, ignoring hidden entries. A read error (absent
/// or unreadable) counts as empty. Test:
/// `autodeploy_agents_for_falls_back_when_the_submodule_is_empty`.
pub
/// Refresh the bundled agent source from the compiled-in bundle and deploy it,
/// reporting anything left stale. Never fails.
///
/// Why: this is what makes a merged `BASE-AGENT.md` change reach a running
/// agent without a manual `tm install` (#4840). It is called from session
/// provisioning, so it MUST fail open: if the refresh or the deploy cannot
/// run, the session still starts with whatever is already on disk. A stale
/// file is a strictly better failure than a session that will not launch.
/// What: (1) [`ensure_agent_source_fresh`] on `source_dir` — cheap when the
/// stamp matches; (2) [`deploy_agents`] from `source_dir` into `target_dir`,
/// which composes and writes only the files it safely may (bundled-origin
/// drift IS overwritten — see the module doc's overwrite policy); (3) collects
/// [`deploy_summary_lines`] for everything it declined or could not compose.
/// Every failure is converted into a warning line rather than an `Err`. When
/// nothing is installed (#9011) it adds no warning — the one ERROR per process
/// naming `tm content install` comes from
/// [`trusty_agents_common::agent_content::report_not_installed`]; any other
/// content error is a warning line. Either way it deploys nothing: whatever
/// `source_dir` holds came from an earlier roster and is stale.
/// Test: `autodeploy_agents_deploys_when_bundle_differs`,
/// `autodeploy_agents_deploys_nothing_without_content`,
/// `autodeploy_agents_is_a_noop_when_already_current`,
/// `autodeploy_agents_fails_open_when_target_is_unwritable`,
/// `autodeploy_agents_warns_when_it_skips_a_user_modified_file`.
/// [`autodeploy_agents`] against a roster resolution already made.
/// [`autodeploy_agents`] against a resolved framework layout, with the
/// git-submodule guard applied.
///
/// Why: [`FrameworkPaths::agent_source_dir`] prefers the `agents/agents` git
/// submodule when a source checkout has one. That directory is git-tracked and
/// authoritative on its own — materializing the compiled-in bundle over it
/// would be destructive and wrong. Callers holding a `FrameworkPaths` should
/// use this instead of [`autodeploy_agents`] so they cannot get that wrong.
/// (Mirrors `skill_source::ensure_skill_source_fresh`'s identical guard.)
/// What: when the resolved source is a POPULATED submodule, deploys from it
/// without refreshing the source; otherwise — the framework-owned
/// [`FrameworkPaths::agents`] directory, or an EMPTY submodule directory
/// (uninitialized on a source checkout, which would otherwise deploy nothing
/// at all, silently — PR #4848 review) — delegates to [`autodeploy_agents`]
/// against the framework source. Never fails, for the same fail-open reason.
/// Test: `autodeploy_agents_for_skips_source_refresh_on_submodule`,
/// `autodeploy_agents_for_falls_back_when_the_submodule_is_empty`.