1use super::agy;
8use super::typescript::{self, Harness};
9use crate::harness::{self, HarnessId};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct Event {
14 pub name: &'static str,
15 pub matcher: Option<&'static str>,
18 pub default_timeout_ms: u32,
22}
23
24const DEFAULT_TIMEOUT_MS: u32 = 600_000;
27
28impl Event {
29 const fn timeout_ms(self, default_timeout_ms: u32) -> Self {
34 Self {
35 default_timeout_ms,
36 ..self
37 }
38 }
39
40 pub fn is_tool_event(&self) -> bool {
42 self.matcher == Some("tool_name")
43 }
44}
45
46const fn event(name: &'static str) -> Event {
47 Event {
48 name,
49 matcher: None,
50 default_timeout_ms: DEFAULT_TIMEOUT_MS,
51 }
52}
53
54const fn matched(name: &'static str, field: &'static str) -> Event {
55 Event {
56 name,
57 matcher: Some(field),
58 default_timeout_ms: DEFAULT_TIMEOUT_MS,
59 }
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub struct MergeFile {
65 pub file: &'static str,
67 pub top_level: &'static [(&'static str, i64)],
69 pub trust_gated: bool,
72}
73
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub enum Placement {
77 Merge(MergeFile),
79 File { dir: &'static str },
81 Keyed {
86 path: &'static str,
87 dir: &'static str,
88 },
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub enum Rules {
94 Claude,
96 Codex,
98 TypeScript(&'static Harness),
100 Translated(&'static agy::Translation),
103 Withheld { reason: &'static str },
105}
106
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
111pub struct Profile {
112 pub events: &'static [Event],
114 pub verified_against: Option<&'static str>,
117 pub rules: Rules,
118}
119
120impl Profile {
121 pub fn event(&self, name: &str) -> Option<&'static Event> {
123 self.events.iter().find(|e| e.name == name)
124 }
125
126 pub fn served<'a>(&self, native_event: &'a str) -> Vec<&'a str> {
129 match self.rules {
130 Rules::Translated(translation) => translation.serves(native_event).to_vec(),
131 _ => vec![native_event],
132 }
133 }
134
135 pub fn renders(&self) -> bool {
137 !matches!(self.rules, Rules::Withheld { .. })
138 }
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
143pub struct Target {
144 pub root: &'static str,
145 pub placement: Placement,
146 pub profile: Profile,
147 pub other_events: &'static [&'static str],
150}
151
152const GENERATED_SUFFIX: &str = super::RESERVED_NAME_SUFFIX;
155
156impl Target {
157 pub fn reads(&self, event: &str) -> bool {
159 self.profile.event(event).is_some() || self.other_events.contains(&event)
160 }
161
162 pub fn merge_file(&self) -> Option<&MergeFile> {
164 match &self.placement {
165 Placement::Merge(merge) => Some(merge),
166 Placement::File { .. } | Placement::Keyed { .. } => None,
167 }
168 }
169
170 pub fn keyed_path(&self) -> Option<&'static str> {
173 match self.placement {
174 Placement::Keyed { path, .. } => Some(path),
175 Placement::Merge(_) | Placement::File { .. } => None,
176 }
177 }
178
179 pub fn native_hook_problem(&self, content: &str) -> Option<&'static str> {
182 match self.profile.rules {
183 Rules::TypeScript(harness) => harness.native_hook_problem(content),
184 _ => None,
185 }
186 }
187
188 pub fn is_file(&self) -> bool {
190 matches!(self.placement, Placement::File { .. })
191 }
192
193 pub fn installs_files(&self) -> bool {
196 self.is_file() || self.keyed_path().is_some()
197 }
198
199 pub fn native_hook_path(&self, hook_name: &str) -> Option<String> {
202 let Placement::File { dir } = self.placement else {
203 return None;
204 };
205 Some(format!("{dir}/mars-{hook_name}.ts"))
206 }
207
208 pub fn generated_path(&self, hook_name: &str) -> Option<String> {
211 match self.placement {
212 Placement::File { dir } => Some(format!("{dir}/mars-{hook_name}{GENERATED_SUFFIX}.ts")),
213 Placement::Keyed { dir, .. } => {
214 Some(format!("{dir}/mars-{hook_name}{GENERATED_SUFFIX}.json"))
215 }
216 Placement::Merge(_) => None,
217 }
218 }
219}
220
221const CLAUDE_EVENTS: &[Event] = &[
222 matched("SessionStart", "source"),
223 event("Setup"),
224 event("UserPromptSubmit").timeout_ms(30_000),
225 event("UserPromptExpansion"),
226 matched("PreToolUse", "tool_name"),
227 matched("PermissionRequest", "tool_name"),
228 matched("PermissionDenied", "tool_name"),
229 matched("PostToolUse", "tool_name"),
230 matched("PostToolUseFailure", "tool_name"),
231 event("PostToolBatch"),
232 matched("SubagentStart", "agent_type"),
233 matched("SubagentStop", "agent_type"),
234 event("TaskCreated"),
235 event("TaskCompleted"),
236 event("Stop"),
237 event("StopFailure"),
238 event("TeammateIdle"),
239 matched("PreCompact", "trigger"),
240 matched("PostCompact", "trigger"),
241 event("Elicitation"),
242 event("ElicitationResult"),
243 matched("Notification", "notification_type"),
244 event("ConfigChange"),
245 event("InstructionsLoaded"),
246 event("CwdChanged"),
247 event("FileChanged"),
248 event("WorktreeCreate"),
249 event("WorktreeRemove"),
250 matched("SessionEnd", "reason").timeout_ms(1_500),
251];
252
253const CODEX_EVENTS: &[Event] = &[
255 matched("SessionStart", "source"),
256 event("UserPromptSubmit"),
257 matched("PreToolUse", "tool_name"),
258 matched("PermissionRequest", "tool_name"),
259 matched("PostToolUse", "tool_name"),
260 matched("PreCompact", "trigger"),
261 matched("PostCompact", "trigger"),
262 matched("SubagentStart", "agent_type"),
263 matched("SubagentStop", "agent_type"),
264 event("Stop"),
265 matched("SessionEnd", "reason"),
266];
267
268const CURSOR_EVENTS: &[Event] = &[
269 event("beforeShellExecution"),
270 event("beforeMCPExecution"),
271 event("afterShellExecution"),
272 event("afterMCPExecution"),
273 event("beforeReadFile"),
274 event("afterFileEdit"),
275 event("beforeTabFileRead"),
276 event("afterTabFileEdit"),
277 event("stop"),
278 event("beforeSubmitPrompt"),
279 event("afterAgentResponse"),
280 event("afterAgentThought"),
281 event("sessionStart"),
282 event("sessionEnd"),
283 event("preCompact"),
284 event("subagentStart"),
285 event("subagentStop"),
286 event("preToolUse"),
287 event("postToolUse"),
288 event("postToolUseFailure"),
289 event("workspaceOpen"),
290];
291
292const OPENCODE_EVENTS: &[Event] = &[
294 matched("PreToolUse", "tool_name"),
295 matched("PostToolUse", "tool_name"),
296 matched("PostToolUseFailure", "tool_name"),
297 event("UserPromptSubmit"),
298 event("PreCompact"),
299 matched("PermissionRequest", "tool_name"),
300];
301
302const PI_EVENTS: &[Event] = &[
303 matched("PreToolUse", "tool_name"),
304 matched("PostToolUse", "tool_name"),
305 matched("PostToolUseFailure", "tool_name"),
306 event("UserPromptSubmit"),
307 matched("SessionStart", "source"),
308 event("Stop"),
309 matched("PreCompact", "trigger"),
310 matched("PostCompact", "trigger"),
311 matched("SessionEnd", "reason"),
312];
313
314const AGY_EVENTS: &[Event] = &[
317 matched("SessionStart", "source"),
319 event("PreInvocation"),
320 event("PostInvocation"),
321 event("Stop"),
322 matched("PreToolUse", "tool_name"),
323 matched("PostToolUse", "tool_name"),
324];
325
326const CLAUDE_VERSION: &str = "Claude Code 2.1.312";
327
328pub const PLUGIN_CLAUDE_TREE: Profile = Profile {
331 events: CLAUDE_EVENTS,
332 verified_against: Some(CLAUDE_VERSION),
333 rules: Rules::Codex,
334};
335
336const CLAUDE: Target = Target {
337 root: ".claude",
338 placement: Placement::Merge(MergeFile {
341 file: "settings.local.json",
342 top_level: &[],
343 trust_gated: false,
344 }),
345 profile: Profile {
346 events: CLAUDE_EVENTS,
347 verified_against: Some(CLAUDE_VERSION),
348 rules: Rules::Claude,
349 },
350 other_events: &[],
351};
352
353const CODEX: Target = Target {
354 root: ".codex",
355 placement: Placement::Merge(MergeFile {
356 file: "hooks.json",
357 top_level: &[],
358 trust_gated: true,
359 }),
360 profile: Profile {
361 events: CODEX_EVENTS,
362 verified_against: Some("codex-cli 0.164.0"),
363 rules: Rules::Codex,
364 },
365 other_events: &["PreModelRequest", "PostModelRequest", "Interrupt"],
366};
367
368const CURSOR: Target = Target {
369 root: ".cursor",
370 placement: Placement::Merge(MergeFile {
372 file: "hooks.json",
373 top_level: &[("version", 1)],
374 trust_gated: false,
375 }),
376 profile: Profile {
377 events: CURSOR_EVENTS,
378 verified_against: Some("cursor-agent 2026.07.16"),
379 rules: Rules::Withheld {
380 reason: "mars renders no generated hooks for Cursor before 0.26",
381 },
382 },
383 other_events: &[],
384};
385
386const OPENCODE: Target = Target {
387 root: ".opencode",
388 placement: Placement::File { dir: "plugins" },
389 profile: Profile {
390 events: OPENCODE_EVENTS,
391 verified_against: Some("OpenCode 2.2.0"),
392 rules: Rules::TypeScript(&typescript::OPENCODE),
393 },
394 other_events: &[],
395};
396
397const PI: Target = Target {
398 root: ".pi",
399 placement: Placement::File { dir: "extensions" },
400 profile: Profile {
401 events: PI_EVENTS,
402 verified_against: Some("Pi 1.4.0"),
403 rules: Rules::TypeScript(&typescript::PI),
404 },
405 other_events: &[],
406};
407
408const AGY: Target = Target {
409 root: ".agy",
410 placement: Placement::Keyed {
412 path: ".agy/hooks.json",
413 dir: "hooks",
414 },
415 profile: Profile {
416 events: AGY_EVENTS,
417 verified_against: Some("agy 1.5.12"),
418 rules: Rules::Translated(&agy::AGY),
419 },
420 other_events: &[],
421};
422
423fn row(harness: HarnessId) -> &'static Target {
425 match harness {
426 HarnessId::Claude => &CLAUDE,
427 HarnessId::Codex => &CODEX,
428 HarnessId::OpenCode => &OPENCODE,
429 HarnessId::Cursor => &CURSOR,
430 HarnessId::Pi => &PI,
431 HarnessId::Agy => &AGY,
432 }
433}
434
435pub fn target(root: &str) -> Option<&'static Target> {
437 harness::ALL
438 .iter()
439 .find(|harness| harness.target_dir() == root)
440 .map(|&harness| row(harness))
441}
442
443pub fn all() -> impl Iterator<Item = &'static Target> {
445 harness::ALL.iter().map(|&harness| row(harness))
446}
447
448pub fn claude_event(name: &str) -> Option<&'static Event> {
450 CLAUDE_EVENTS.iter().find(|event| event.name == name)
451}
452
453pub fn claude_event_names() -> String {
455 CLAUDE_EVENTS
456 .iter()
457 .map(|event| event.name)
458 .collect::<Vec<_>>()
459 .join(", ")
460}