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
//! Combine the project-tier hook sources [`super::settings::write_project_hooks`]
//! writes into one merge-ready `{"hooks": {...}}` value (issue #2003).
//!
//! Why: the daemon's managed-launch path spawns `claude --setting-sources
//! project,local` (see [`crate::core::model_inject::SETTING_SOURCES_FLAG`]),
//! which excludes the `user` tier where
//! [`crate::core::standalone::hooks::ensure_managed_hooks`] provisions the
//! trusty-mpm lifecycle triad (circuit breaker / audit log / dashboard
//! observability, via [`crate::core::standalone::hooks::mpm_hook_additions_with_exe`]).
//! Managed sessions therefore never fired those six events at all — only the
//! `trusty-memory` + PM-guard entries the OLD `write_project_hooks` wrote
//! (and it wrote them by REPLACING the entire `hooks` key, clobbering any
//! pre-existing foreign hooks too). This module folds the SAME triad
//! definition the user-tier writer uses into the project-tier write, so a
//! single source of truth backs both writers and they cannot drift apart
//! again, and exposes the matching identity predicate so the caller's
//! replace-by-identity strip (entry-level, per issue #2948) recognises every
//! source this module combines.
//! What: [`project_managed_hook_additions`] deep-merges the `trusty-memory`
//! block, the PM-enforcement guard, the lifecycle triad, and (#6887, when the
//! manifest opts in) the bulk-read diversion groups into one value;
//! [`is_project_managed_hook_command`] recognises a command from ANY of those
//! four sources.
//! Test: `project_hooks_tests.rs`.
use ;
// #7688: the prompt-feedback capture's groups and its identity predicate.
use ;
use ;
use crate;
/// Build the full set of trusty-mpm-owned hook additions for the project tier.
///
/// Why: see the module doc — this is the single place the three sources
/// combine, so [`super::settings::write_project_hooks`] stays a thin
/// read-strip-merge-write shell.
/// What: starts from [`TRUSTY_MEMORY_HOOKS`] (parsed; `UserPromptSubmit` +
/// `SessionStart`), inserts the [`pm_guard_hook_value`] `PreToolUse` block,
/// then appends every group from
/// [`mpm_hook_additions_with_exe`]`(None)`'s six-event lifecycle triad onto
/// the corresponding event array (deduping by deep equality, mirroring
/// [`trusty_common::claude_config::merge_hook_entries`]'s own semantics).
/// `PreToolUse` and `SessionStart` — sources for more than one of the three —
/// end up with multiple handler groups (PM-guard + tm-hook, or trusty-memory +
/// tm-hook) rather than one clobbering the other.
/// `inject_prompt_context` is the `[hooks] prompt_context` config key (#5034):
/// `true` (the default) keeps the `UserPromptSubmit` entry; `false` drops that
/// event key entirely and leaves every other source untouched.
/// `divert_enabled` is the `[divert] enabled` manifest key (#6887): `true`
/// APPENDS two more `PreToolUse` groups (matcher `Read`, matcher `Bash`) after
/// every existing group, so no other group's bytes change either way; `false`
/// (the default) writes none.
/// Test: `project_managed_hook_additions_combines_all_three_sources`,
/// `project_managed_hook_additions_is_stable_across_calls`,
/// `project_managed_hook_additions_omits_prompt_context_when_disabled`,
/// `project_managed_hook_additions_includes_divert_when_enabled`,
/// `project_managed_hook_additions_omits_divert_when_disabled`.
///
/// #7244: returns the lifecycle triad's resolution error rather than a block
/// built around an unvouched-for command, so the caller writes nothing.
/// `exe_override` pins the binary path (production passes `None` and lets
/// [`mpm_hook_additions_with_exe`] resolve); a test passes an installed-looking
/// path so its assertions do not depend on whether the host running them has
/// `tm` installed.
///
/// #7688: production now reaches
/// [`project_managed_hook_additions_with_prompt_feedback`] instead, so this
/// three-argument form survives as the pre-#7688 REFERENCE its own tests (and
/// `the_disabled_block_is_byte_identical_to_the_pre_7688_writer`) assert
/// against. Keeping it is what makes "the flag off changes nothing" a checkable
/// claim rather than a comment.
pub
/// [`project_managed_hook_additions`], with the #7688 capture toggled.
///
/// Why: an ADDITIVE seam rather than a fourth parameter on the function above,
/// whose existing call sites all assert shapes this flag does not change — see
/// the same reasoning on
/// [`super::settings::write_project_hooks`].
/// What: `prompt_feedback_enabled = true` APPENDS one empty-matcher group onto
/// `Stop` and one onto `SubagentStop`, after every existing group, so no other
/// group's bytes change either way; `false` writes none.
/// Test: `project_managed_hook_additions_includes_prompt_feedback_when_enabled`,
/// `project_managed_hook_additions_omits_prompt_feedback_when_disabled`.
pub
/// Every hook event key trusty-mpm owns at the project tier, regardless of
/// which ones the current config asks to write.
///
/// Why (#5034): [`super::settings::write_project_hooks`] strips its own prior
/// entries only for the events it is about to write. Deriving that strip list
/// from the toggled-down additions would make the opt-out a no-op on any
/// project launched at least once with the hook enabled — the stale
/// `UserPromptSubmit` entry would sit in `.claude/settings.json` forever,
/// still firing, because the strip no longer covered that key. The strip
/// domain must therefore be the full owned set, not the write set.
/// What: the event keys of [`project_managed_hook_additions`] with EVERY
/// toggle on — a superset of every toggled variant by construction, so it
/// cannot drift as toggles are added (#6887 adds the second one).
/// Test: `project_managed_hook_events_is_a_superset_of_every_variant`,
/// `write_project_hooks_strips_stale_prompt_context_when_disabled`,
/// `write_project_hooks_strips_stale_divert_when_disabled`.
pub
/// A stable-looking binary path used only to enumerate hook EVENT KEYS.
///
/// Why (#7244): see [`project_managed_hook_events`] — the key set must not
/// vary with the host's installed binaries. Absolute, outside every build
/// tree, and named `tm`, so it satisfies both halves of the
/// `resolve_stable_hook_exe` guard by construction.
/// What: never written anywhere; only the keys of the block built around it
/// are read.
/// Test: `project_managed_hook_events_is_a_superset_of_every_variant`.
const EVENT_KEY_PROBE_EXE: &str = "/usr/local/bin/tm";
/// Recognise a hook command belonging to ANY of the three project-tier
/// sources [`project_managed_hook_additions`] combines.
///
/// Why: [`super::settings::write_project_hooks`]'s replace-by-identity strip
/// (via [`crate::core::standalone::hooks::strip_hook_entries_matching_for_events`])
/// needs a predicate broader than
/// [`crate::core::standalone::hooks::is_mpm_hook_command`] alone — that
/// predicate only recognises the lifecycle-triad `<exe> hook` shape, not the
/// `trusty-memory` or PM-guard commands this module also writes. Without the
/// broader predicate, re-running `write_project_hooks` would duplicate the
/// `trusty-memory`/PM-guard groups on every launch instead of replacing them.
/// What: returns `true` for a lifecycle-triad command
/// ([`is_mpm_hook_command`]), a `trusty-memory ` command, a PM-guard command
/// (ends with [`PM_GUARD_SUFFIX`]), a diversion-check command (#6887,
/// [`is_divert_hook_command`]), or a prompt-feedback capture (#7688,
/// [`is_prompt_feedback_hook_command`]).
/// Test: `is_project_managed_hook_command_recognises_all_three_sources`,
/// `is_project_managed_hook_command_recognises_divert_check`,
/// `is_project_managed_hook_command_recognises_prompt_feedback`.
pub
/// Recognise a hook command only the PROJECT tier ever writes.
///
/// Why (#7849): the toggle-driven gap probe compares a settings file against
/// what THIS writer would produce, and `tm doctor` hands it every settings file
/// it can reach — including the USER-tier `~/.claude/settings.json`, which
/// [`crate::core::standalone::hooks::ensure_managed_hooks`] provisions with the
/// lifecycle triad alone. Gating on [`is_project_managed_hook_command`] would
/// therefore match that file too and report every project-tier group missing
/// from it. The `trusty-memory` block and the PM guard are written by this
/// module and by nothing else, so either one identifies the project tier.
/// What: `true` for a PM-guard command (ends with [`PM_GUARD_SUFFIX`]) or a
/// `trusty-memory ` command. Deliberately narrower than
/// [`is_project_managed_hook_command`], which is a STRIP domain rather than a
/// tier marker.
/// Test: `the_tier_marker_ignores_a_user_tier_lifecycle_command`,
/// `the_tier_marker_recognises_the_pm_guard`.
pub