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
//! What a hook installs on one target: `[hooks]` rendered, the native hook laid over it, and
//! the native hook's own contract checks.
use std::collections::BTreeMap;
use serde::Serialize;
use super::portable::{self, PortableHooks};
use super::render::{self, Skip};
use super::targets::{self, Target};
use super::{HookDef, Origin, Tokens};
use crate::ordered::OrderedValue;
/// What a hook installs on one target.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum HookOutput {
Merge(MergeOutput),
/// One entry of a keyed hook file: the hook's events, and `enabled` where it is set.
Keyed(MergeOutput),
File(FileOutput),
}
impl HookOutput {
/// What `[hooks]` leaves out on the target, minus what a native hook replaces.
pub fn skips(&self) -> &[Skip] {
match self {
Self::Merge(output) | Self::Keyed(output) => &output.skips,
Self::File(output) => &output.skips,
}
}
}
/// A file target's files for a hook: the authored native hook as written, and the file
/// generated from `[hooks]`.
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FileOutput {
pub files: Vec<OutputFile>,
/// What is wrong with the native hook, if anything; it is installed regardless.
pub native_hook_problem: Option<&'static str>,
pub skips: Vec<Skip>,
/// Rendered events whose portable answers do not all carry over to the target.
pub answer_limits: Vec<Skip>,
}
impl FileOutput {
/// Whether a native hook installs next to a file generated from `[hooks]`.
pub fn has_native_hook_beside_generated(&self) -> bool {
self.files.len() > 1
}
}
/// A file a hook installs on a file target.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct OutputFile {
/// Relative to the target root.
pub path: String,
pub content: String,
}
/// One merge target's events for a hook: `[hooks]` rendered, with the native hook's events
/// replacing the generated ones they name.
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MergeOutput {
/// Finished entries per native event. An event whose array is empty is turned off.
pub events: BTreeMap<String, Vec<OrderedValue>>,
/// Unknown events a dependency's native hook lists, left out of `events`.
pub skipped_events: Vec<String>,
/// Events the target does not list that `unchecked = true` let through.
pub unchecked_events: Vec<String>,
/// Events the native hook declares that `[hooks]` also declares: the native hook's replace
/// the generated entries.
pub overrides: Vec<String>,
/// What `[hooks]` leaves out here, minus the events a native hook replaces.
pub skips: Vec<Skip>,
/// A keyed target's `enabled` switch, as the native hook sets it.
pub enabled: Option<bool>,
/// Files a keyed target's entries run, relative to the target root (`[hooks]` translated:
/// the runner's data file).
pub files: Vec<OutputFile>,
/// Rendered events whose portable answers do not all carry over to the target.
pub answer_limits: Vec<Skip>,
}
impl MergeOutput {
/// The hook as a keyed target's file holds it: `enabled`, then each event with entries.
pub fn keyed_value(&self) -> serde_json::Value {
let mut value = serde_json::Map::new();
if let Some(enabled) = self.enabled {
value.insert("enabled".to_owned(), enabled.into());
}
for (event, entries) in &self.events {
if !entries.is_empty() {
value.insert(
event.clone(),
OrderedValue::Array(entries.clone()).to_json(),
);
}
}
value.into()
}
/// Hold every event Claude lists to Codex's handler rules, reporting what that leaves
/// out. Events outside Claude's table (`unchecked`) and events turned off stay as they
/// are. Idempotent, so entries `[hooks]` already rendered pass unchanged.
fn conform_to_codex(&mut self, profile: &targets::Profile) -> Result<(), NativeHookError> {
let checked: BTreeMap<_, _> = self
.events
.iter()
.filter(|(event, entries)| {
!entries.is_empty() && targets::claude_event(event).is_some()
})
.map(|(event, entries)| (event.clone(), OrderedValue::Array(entries.clone())))
.collect();
if checked.is_empty() {
return Ok(());
}
let (table, nulls) = PortableHooks::from_ordered(OrderedValue::Object(
checked
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
))
.map_err(|error| NativeHookError(format!("native hook is not a hook table: {error}")))?;
for path in nulls {
// `hooks.<Event>[..]…`: the second segment is the event.
let event = path.split(['.', '[']).nth(1).map(str::to_owned);
self.skips.push(render::Skip {
event,
reason: format!("`null` at `{path}` dropped: Codex cannot parse it"),
});
}
let rendered = table.render(profile, &Tokens::default());
self.skips.extend(rendered.skips.iter().cloned());
for event in checked.keys() {
self.events.remove(event);
}
self.events.extend(rendered.events().clone());
Ok(())
}
}
/// Why a native hook's text is refused. The caller adds the file it came from.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{0}")]
pub struct NativeHookError(String);
impl HookDef {
/// What this hook installs on `target`: `[hooks]` rendered (unless the target is in
/// `skip-targets`) and the native hook laid over it. `native_hook_text` is the native hook's file,
/// read by the caller when the hook declares one for the target.
///
/// On a merge target the native hook is an event-keyed JSON object, optionally wrapped as
/// `{"hooks": …, "version"?, "description"?}`, whose events the target lists (unless
/// its table is `unchecked`) and whose values are arrays. It replaces only the events it
/// declares, so one with an empty array turns that event off. On a file target it is
/// opaque text.
pub fn output(
&self,
target: &Target,
native_hook_text: Option<&str>,
origin: Origin,
tokens: &Tokens<'_>,
) -> Result<HookOutput, NativeHookError> {
if target.is_file() {
Ok(HookOutput::File(self.file_output(
target,
native_hook_text,
tokens,
)))
} else {
let output =
self.merge_output(target, &target.profile, native_hook_text, origin, tokens)?;
Ok(if target.keyed_path().is_some() {
HookOutput::Keyed(output)
} else {
HookOutput::Merge(output)
})
}
}
fn file_output(
&self,
target: &Target,
native_hook_text: Option<&str>,
tokens: &Tokens<'_>,
) -> FileOutput {
let tokens = &Tokens {
hook_name: &self.name,
..*tokens
};
let mut output = FileOutput::default();
if let (Some(text), Some(path)) = (native_hook_text, target.native_hook_path(&self.name)) {
let content = tokens.substitute(text);
output.native_hook_problem = target.native_hook_problem(&content);
output.files.push(OutputFile { path, content });
}
if let (Some(portable), true) = (&self.portable, self.generates_for(target.root)) {
let rendered = portable.render(&target.profile, tokens);
if let (
render::Output::File {
text: Some(content),
},
Some(path),
) = (rendered.output, target.generated_path(&self.name))
{
output.files.push(OutputFile { path, content });
}
output.skips = rendered.skips;
output.answer_limits = rendered.answer_limits;
}
output
}
/// What this hook puts in a plugin build's tree for `target`. A tree whose hook file
/// another harness also reads passes `shared` (the Claude tree: [`targets::PLUGIN_CLAUDE_TREE`]):
/// Codex rejects the whole `hooks/hooks.json` over one handler it cannot parse, so
/// everything that reaches the file, `[hooks]` and native hook alike, is rendered under
/// `shared` and conformed to Codex's rules while keeping its events. Any other
/// tree passes `None` and takes what [`Self::output`] gives.
pub fn plugin_output(
&self,
target: &Target,
shared: Option<&targets::Profile>,
native_hook_text: Option<&str>,
origin: Origin,
tokens: &Tokens<'_>,
) -> Result<HookOutput, NativeHookError> {
let Some(profile) = shared else {
return self.output(target, native_hook_text, origin, tokens);
};
let mut output = self.merge_output(target, profile, native_hook_text, origin, tokens)?;
output.conform_to_codex(profile)?;
Ok(HookOutput::Merge(output))
}
/// Whether the hook's native hook for `target` loads, as [`Self::output`] would lay it. A
/// file target's native hook is opaque, and `[hooks]` never fails to render.
pub fn check_native_hook(
&self,
target: &Target,
native_hook_text: Option<&str>,
origin: Origin,
) -> Result<(), NativeHookError> {
if target.is_file() {
return Ok(());
}
let tokens = Tokens::default();
self.lay_native_hook(
MergeOutput::default(),
target,
&target.profile,
native_hook_text,
origin,
&tokens,
)
.map(drop)
}
fn merge_output(
&self,
target: &Target,
profile: &targets::Profile,
native_hook_text: Option<&str>,
origin: Origin,
tokens: &Tokens<'_>,
) -> Result<MergeOutput, NativeHookError> {
let mut output = MergeOutput::default();
if let (Some(portable), true) = (&self.portable, self.generates_for(target.root)) {
let generated = target.generated_path(&self.name).unwrap_or_default();
let tokens = &Tokens {
hook_name: &self.name,
generated_file: &generated,
..*tokens
};
let rendered = portable.render(profile, tokens);
match rendered.output {
render::Output::Merge {
events,
unchecked_events,
} => {
output.events = events;
output.unchecked_events = unchecked_events;
}
render::Output::Keyed { events, data_file } => {
output.events = events;
output.files = data_file
.zip(target.generated_path(&self.name))
.map(|(content, path)| OutputFile { path, content })
.into_iter()
.collect();
}
render::Output::File { .. } => {}
}
output.skips = rendered.skips;
output.answer_limits = rendered.answer_limits;
}
self.lay_native_hook(output, target, profile, native_hook_text, origin, tokens)
}
/// Lay the hook's native hook over the events `[hooks]` rendered.
fn lay_native_hook(
&self,
mut output: MergeOutput,
target: &Target,
profile: &targets::Profile,
native_hook_text: Option<&str>,
origin: Origin,
tokens: &Tokens<'_>,
) -> Result<MergeOutput, NativeHookError> {
let (Some(text), Some(native_hook)) = (native_hook_text, self.native_hook(target.root))
else {
return Ok(output);
};
let err = |message: String| NativeHookError(message);
let mut value: OrderedValue = serde_json::from_str(text)
.map_err(|error| err(format!("native hook is not valid JSON: {error}")))?;
value = portable::unwrap_hooks_object(value);
let OrderedValue::Object(object) = value else {
return Err(err(
"native hook top level must be an event-keyed JSON object".into(),
));
};
// Sorted, so the first fault reported does not depend on authored order.
let sorted: BTreeMap<_, _> = object.into_iter().collect();
for (event, entries) in sorted {
if event == "enabled" && target.keyed_path().is_some() {
let OrderedValue::Bool(enabled) = entries else {
return Err(err("`enabled` must be true or false".into()));
};
output.enabled = Some(enabled);
continue;
}
if profile.event(&event).is_none() {
if !native_hook.is_unchecked() && origin == Origin::Dependency {
output.skipped_events.push(event);
continue;
}
if !native_hook.is_unchecked() {
let checked = profile
.verified_against
.map_or(String::new(), |v| format!(" (checked against {v})"));
return Err(err(format!(
"unknown event `{event}` for target `{}`; valid events: {}{checked}; use `unchecked = true` to pass a newer native event through verbatim",
target.root,
profile
.events
.iter()
.map(|e| e.name)
.collect::<Vec<_>>()
.join(", ")
)));
}
output.unchecked_events.push(event.clone());
}
let OrderedValue::Array(mut array) = entries else {
return Err(err(format!("event `{event}` value must be an array")));
};
array.iter_mut().for_each(|e| tokens.substitute_value(e));
// A translating target's event serves other names (`PreInvocation` is
// `UserPromptSubmit`): what the native hook replaces is what those events carried.
let served = profile.served(&event);
if let Some(declared) = served
.iter()
.find(|name| self.portable.as_ref().is_some_and(|p| p.declares(name)))
{
output.overrides.push((*declared).to_owned());
}
output.skips.retain(|skip| {
!served
.iter()
.any(|name| skip.event.as_deref() == Some(name))
});
output.answer_limits.retain(|limit| {
!served
.iter()
.any(|name| limit.event.as_deref() == Some(name))
});
output.events.insert(event, array);
}
Ok(output)
}
}