ptuf 0.5.0

PreToolUseFilter: a generic guardrail layer for coding agents
Documentation
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
use serde::Serialize;

/// Append a per-adapter "Ask is unavailable" note to a deny reason.
/// Adapters that cannot surface an interactive prompt (Codex, Copilot,
/// Kiro) demote `Ask` to `Deny` and use this helper to produce a
/// human-readable explanation.
fn append_demote_note(reason: &str, note: &str) -> String {
    format!("{reason}\n\n{note}")
}

#[derive(Debug, Serialize)]
pub struct HookResponse {
    #[serde(rename = "hookSpecificOutput")]
    pub hook_specific_output: HookSpecificOutput,
}

#[derive(Debug, Serialize)]
pub struct HookSpecificOutput {
    #[serde(rename = "hookEventName")]
    pub hook_event_name: &'static str,
    #[serde(rename = "permissionDecision")]
    pub permission_decision: &'static str,
    #[serde(rename = "permissionDecisionReason")]
    pub permission_decision_reason: String,
}

pub mod claude_code {
    use super::{HookResponse, HookSpecificOutput};
    use crate::Decision;

    /// Build a Claude Code `hookSpecificOutput` response from a decision.
    /// Returns `None` for `Allow` and `Monitor`, since those produce no
    /// hook-protocol output.
    pub fn from_decision(decision: &Decision) -> Option<HookResponse> {
        let (verdict, reason) = match decision {
            Decision::Allow | Decision::Monitor { .. } => return None,
            Decision::Ask { reason, .. } => ("ask", reason.clone()),
            Decision::Deny { reason, .. } => ("deny", reason.clone()),
        };

        Some(HookResponse {
            hook_specific_output: HookSpecificOutput {
                hook_event_name: "PreToolUse",
                permission_decision: verdict,
                permission_decision_reason: reason,
            },
        })
    }
}

pub mod codex {
    use super::{HookResponse, HookSpecificOutput, append_demote_note};
    use crate::Decision;

    const ASK_UNAVAILABLE_NOTE: &str =
        "Codex PreToolUse cannot prompt interactively, so ptuf is blocking this request.";

    /// Build a Codex `hookSpecificOutput` response from a decision.
    /// `Ask` is mapped to a deny because Codex currently fails open on it.
    pub fn from_decision(decision: &Decision) -> Option<HookResponse> {
        let reason = match decision {
            Decision::Allow | Decision::Monitor { .. } => return None,
            Decision::Ask { reason, .. } => deny_reason_for_ask(reason),
            Decision::Deny { reason, .. } => reason.clone(),
        };

        Some(HookResponse {
            hook_specific_output: HookSpecificOutput {
                hook_event_name: "PreToolUse",
                permission_decision: "deny",
                permission_decision_reason: reason,
            },
        })
    }

    pub fn deny_reason_for_ask(reason: &str) -> String {
        append_demote_note(reason, ASK_UNAVAILABLE_NOTE)
    }
}

pub mod copilot {
    use serde::Serialize;

    use super::append_demote_note;
    use crate::Decision;

    /// Note appended to a deny reason whenever a Copilot `Ask` decision
    /// is demoted to `Deny`. Copilot hooks have no reliable interactive
    /// confirmation channel, so ptuf surfaces the demotion explicitly.
    const ASK_UNAVAILABLE_NOTE: &str = "GitHub Copilot hooks do not reliably process interactive ask decisions; ptuf is blocking this request instead.";

    /// Bare deny envelope expected by GitHub Copilot's `preToolUse` hook.
    /// Unlike Claude Code / Codex, Copilot does **not** wrap the body in
    /// `hookSpecificOutput` and treats non-zero exit codes as hook
    /// failures (skipping the response). The CLI therefore writes this
    /// JSON object directly and uses exit `0`, even for deny.
    #[derive(Debug, Serialize)]
    pub struct CopilotResponse {
        #[serde(rename = "permissionDecision")]
        pub permission_decision: &'static str,
        #[serde(rename = "permissionDecisionReason")]
        pub permission_decision_reason: String,
    }

    /// Build a Copilot deny envelope from a decision.
    /// Returns `None` for `Allow` and `Monitor` (Copilot, like the other
    /// adapters, emits no output for those). `Ask` is demoted to a deny
    /// because Copilot can't surface an interactive prompt reliably.
    pub fn from_decision(decision: &Decision) -> Option<CopilotResponse> {
        let reason = match decision {
            Decision::Allow | Decision::Monitor { .. } => return None,
            Decision::Ask { reason, .. } => deny_reason_for_ask(reason),
            Decision::Deny { reason, .. } => reason.clone(),
        };

        Some(CopilotResponse {
            permission_decision: "deny",
            permission_decision_reason: reason,
        })
    }

    pub fn deny_reason_for_ask(reason: &str) -> String {
        append_demote_note(reason, ASK_UNAVAILABLE_NOTE)
    }
}

pub mod kiro {
    use super::append_demote_note;

    /// Note appended to a deny reason whenever a Kiro `Ask` decision is
    /// demoted to `Deny`. Kiro's `preToolUse` hook protocol does not
    /// expose an interactive ask channel, so ptuf surfaces the demotion
    /// explicitly on stderr.
    const ASK_UNAVAILABLE_NOTE: &str = "Kiro CLI PreToolUse hooks do not define an interactive ask channel; ptuf is blocking this request instead.";

    pub fn deny_reason_for_ask(reason: &str) -> String {
        append_demote_note(reason, ASK_UNAVAILABLE_NOTE)
    }
}

pub mod cline {
    use serde::Serialize;

    use super::append_demote_note;
    use crate::Decision;

    /// Note appended to a deny reason whenever a Cline `Ask` decision is
    /// demoted to `Deny`. Cline `PreToolUse` file hooks have no reliable
    /// interactive review channel, so ptuf surfaces the demotion
    /// explicitly in the cancel JSON.
    const ASK_UNAVAILABLE_NOTE: &str = "Cline PreToolUse file hooks do not currently provide a uniformly reliable interactive review channel; ptuf is blocking this request instead.";

    /// Cline `PreToolUse` hook response. Allow / Monitor serialise to the
    /// bare `{}` object (every field skipped); deny / demoted-ask
    /// serialise to a `cancel: true` envelope. The renderer never emits
    /// `shouldContinue`, `review`, or `overrideInput`.
    #[derive(Debug, Serialize)]
    pub struct ClineResponse {
        #[serde(skip_serializing_if = "Option::is_none")]
        pub cancel: Option<bool>,

        #[serde(rename = "errorMessage", skip_serializing_if = "Option::is_none")]
        pub error_message: Option<String>,

        #[serde(skip_serializing_if = "Option::is_none")]
        pub context: Option<String>,

        #[serde(
            rename = "contextModification",
            skip_serializing_if = "Option::is_none"
        )]
        pub context_modification: Option<String>,
    }

    impl ClineResponse {
        fn empty() -> Self {
            Self {
                cancel: None,
                error_message: None,
                context: None,
                context_modification: None,
            }
        }

        fn cancel(reason: String) -> Self {
            Self {
                cancel: Some(true),
                error_message: Some(reason.clone()),
                context: Some(reason.clone()),
                context_modification: Some(reason),
            }
        }
    }

    /// Build a Cline hook response from a decision. `Allow` / `Monitor`
    /// produce the empty `{}` object; `Deny` produces a cancel envelope;
    /// `Ask` is demoted to a cancel envelope with the demotion note.
    pub fn from_decision(decision: &Decision) -> ClineResponse {
        match decision {
            Decision::Allow | Decision::Monitor { .. } => ClineResponse::empty(),
            Decision::Ask { reason, .. } => ClineResponse::cancel(deny_reason_for_ask(reason)),
            Decision::Deny { reason, .. } => ClineResponse::cancel(reason.clone()),
        }
    }

    pub fn deny_reason_for_ask(reason: &str) -> String {
        append_demote_note(reason, ASK_UNAVAILABLE_NOTE)
    }
}

pub mod pi {
    use serde::Serialize;

    use crate::Decision;

    /// Bare decision envelope expected by the Pi Coding Agent extension.
    /// Unlike Claude Code / Codex, Pi reads `decision` / `rule_id` / `reason`
    /// directly (no `hookSpecificOutput` wrapper). `Ask` is preserved because
    /// the TS extension can surface an interactive confirm channel.
    #[derive(Debug, Serialize)]
    pub struct PiHookResponse {
        pub decision: &'static str,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub rule_id: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub reason: Option<String>,
    }

    /// Build a Pi hook response from a decision. Every variant serialises to
    /// JSON on stdout so the extension can interpret allow/monitor/notify/ask/
    /// deny without relying on exit code alone.
    pub fn from_decision(decision: &Decision) -> PiHookResponse {
        match decision {
            Decision::Allow => PiHookResponse {
                decision: "allow",
                rule_id: None,
                reason: None,
            },
            Decision::Monitor { rule_id } => PiHookResponse {
                decision: "monitor",
                rule_id: Some(rule_id.clone()),
                reason: None,
            },
            Decision::Ask { rule_id, reason } => PiHookResponse {
                decision: "ask",
                rule_id: Some(rule_id.clone()),
                reason: Some(reason.clone()),
            },
            Decision::Deny { rule_id, reason } => PiHookResponse {
                decision: "deny",
                rule_id: Some(rule_id.clone()),
                reason: Some(reason.clone()),
            },
        }
    }
}

pub mod opencode {
    use super::append_demote_note;
    use super::pi::{self, PiHookResponse};
    use crate::Decision;

    const ASK_UNAVAILABLE_NOTE: &str = "OpenCode has a permission.ask plugin hook, but it is \
         known to fail silently in some builds, and tool.execute.before cannot start an \
         interactive confirmation; ptuf is blocking this request instead.";

    pub type OpencodeHookResponse = PiHookResponse;

    pub fn from_decision(decision: &Decision) -> PiHookResponse {
        pi::from_decision(decision)
    }

    pub fn deny_reason_for_ask(reason: &str) -> String {
        append_demote_note(reason, ASK_UNAVAILABLE_NOTE)
    }
}

pub mod cursor {
    use serde::Serialize;

    use crate::Decision;

    /// Bare permission envelope expected by Cursor's `preToolUse` hook.
    /// Unlike Copilot / Codex / Kiro, Cursor exposes a genuine `ask`
    /// channel, so `Ask` is **not** demoted to `Deny`. The CLI writes this
    /// JSON object directly (no `hookSpecificOutput` wrapper); `deny`
    /// additionally exits 2 while `ask` exits 0.
    #[derive(Debug, Serialize)]
    pub struct CursorResponse {
        pub permission: &'static str,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub user_message: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub agent_message: Option<String>,
    }

    /// Build a Cursor permission envelope from a decision. Returns `None`
    /// for no decision. `Allow` / `Monitor` map to an explicit
    /// `permission: "allow"` because current Cursor treats empty stdout
    /// from a `failClosed` hook as invalid output. `Ask` maps to
    /// `permission: "ask"` and `Deny` to `permission: "deny"`; both carry
    /// the reason verbatim in `user_message` / `agent_message`.
    pub fn from_decision(decision: &Decision) -> CursorResponse {
        let (permission, reason) = match decision {
            Decision::Allow | Decision::Monitor { .. } => ("allow", None),
            Decision::Ask { reason, .. } => ("ask", Some(reason.clone())),
            Decision::Deny { reason, .. } => ("deny", Some(reason.clone())),
        };

        CursorResponse {
            permission,
            user_message: reason.clone(),
            agent_message: reason,
        }
    }
}

pub use claude_code::from_decision;

#[cfg(test)]
mod tests {

    use super::*;
    use crate::Decision;

    #[test]
    fn allow_produces_no_response() {
        assert!(claude_code::from_decision(&Decision::Allow).is_none());
        assert!(codex::from_decision(&Decision::Allow).is_none());
        assert!(copilot::from_decision(&Decision::Allow).is_none());
    }

    #[test]
    fn monitor_produces_no_response() {
        let d = Decision::Monitor {
            rule_id: "core.m".into(),
        };
        assert!(claude_code::from_decision(&d).is_none());
        assert!(codex::from_decision(&d).is_none());
        assert!(copilot::from_decision(&d).is_none());
    }

    #[test]
    fn copilot_deny_serialises_bare_envelope() {
        let d = Decision::Deny {
            rule_id: "core.x".into(),
            reason: "Blocked by ptuf rule core.x.\n".into(),
        };
        let resp = copilot::from_decision(&d).expect("response");
        let json = serde_json::to_string(&resp).expect("serialise");
        assert!(
            !json.contains("hookSpecificOutput"),
            "Copilot must not wrap response: {json}"
        );
        assert!(json.contains("\"permissionDecision\":\"deny\""));
        assert!(json.contains("\"permissionDecisionReason\":\"Blocked by ptuf rule core.x.\\n\""));
    }

    #[test]
    fn claude_deny_serialises_expected_shape() {
        let d = Decision::Deny {
            rule_id: "core.x".into(),
            reason: "Blocked by ptuf rule core.x.\n".into(),
        };
        let resp = claude_code::from_decision(&d).expect("response");
        let json = serde_json::to_string(&resp).expect("serialise");
        assert!(json.contains("\"hookSpecificOutput\""));
        assert!(json.contains("\"hookEventName\":\"PreToolUse\""));
        assert!(json.contains("\"permissionDecision\":\"deny\""));
        assert!(json.contains("\"permissionDecisionReason\":\"Blocked by ptuf rule core.x.\\n\""));
    }

    #[test]
    fn claude_ask_serialises_with_ask_verdict() {
        let d = Decision::Ask {
            rule_id: "core.a".into(),
            reason: "confirm please".into(),
        };
        let resp = claude_code::from_decision(&d).expect("response");
        let json = serde_json::to_string(&resp).expect("serialise");
        assert!(json.contains("\"permissionDecision\":\"ask\""));
        assert!(json.contains("\"permissionDecisionReason\":\"confirm please\""));
    }

    #[test]
    fn kiro_deny_reason_for_ask_appends_note() {
        let s = kiro::deny_reason_for_ask("please confirm");
        assert!(s.starts_with("please confirm"));
        assert!(s.contains("Kiro CLI PreToolUse hooks do not define an interactive ask channel"));
    }

    #[test]
    fn cline_allow_outputs_empty_object() {
        let resp = cline::from_decision(&Decision::Allow);
        let json = serde_json::to_string(&resp).expect("serialise");
        assert_eq!(json, "{}");
    }

    #[test]
    fn cline_monitor_outputs_empty_object() {
        let d = Decision::Monitor {
            rule_id: "core.m".into(),
        };
        let resp = cline::from_decision(&d);
        let json = serde_json::to_string(&resp).expect("serialise");
        assert_eq!(json, "{}");
    }

    #[test]
    fn cline_deny_outputs_cancel_json() {
        let d = Decision::Deny {
            rule_id: "core.x".into(),
            reason: "blocked".into(),
        };
        let resp = cline::from_decision(&d);
        let json = serde_json::to_value(resp).expect("serialise");
        assert_eq!(json["cancel"], true);
        assert_eq!(json["errorMessage"], "blocked");
        assert_eq!(json["context"], "blocked");
        assert_eq!(json["contextModification"], "blocked");
        assert!(json.get("shouldContinue").is_none());
        assert!(json.get("review").is_none());
    }

    #[test]
    fn cline_ask_is_demoted_to_cancel() {
        let d = Decision::Ask {
            rule_id: "core.x".into(),
            reason: "confirm".into(),
        };
        let resp = cline::from_decision(&d);
        let json = serde_json::to_value(resp).expect("serialise");
        assert_eq!(json["cancel"], true);
        let msg = json["errorMessage"].as_str().expect("errorMessage string");
        assert!(msg.starts_with("confirm"));
        assert!(msg.contains("Cline PreToolUse file hooks"));
    }

    #[test]
    fn codex_ask_serialises_as_deny_with_note() {
        let d = Decision::Ask {
            rule_id: "core.a".into(),
            reason: "confirm please".into(),
        };
        let resp = codex::from_decision(&d).expect("response");
        let json = serde_json::to_string(&resp).expect("serialise");
        assert!(json.contains("\"permissionDecision\":\"deny\""));
        assert!(json.contains("Codex PreToolUse cannot prompt interactively"));
    }

    #[test]
    fn cursor_allow_and_monitor_emit_explicit_allow() {
        let allow =
            serde_json::to_value(cursor::from_decision(&Decision::Allow)).expect("serialise allow");
        assert_eq!(allow["permission"], "allow");
        assert!(allow.get("user_message").is_none());
        assert!(allow.get("agent_message").is_none());

        let monitor = serde_json::to_value(cursor::from_decision(&Decision::Monitor {
            rule_id: "core.m".into(),
        }))
        .expect("serialise monitor");
        assert_eq!(monitor["permission"], "allow");
        assert!(monitor.get("user_message").is_none());
        assert!(monitor.get("agent_message").is_none());
    }

    #[test]
    fn cursor_deny_serialises_bare_permission_envelope() {
        let d = Decision::Deny {
            rule_id: "core.x".into(),
            reason: "blocked".into(),
        };
        let resp = cursor::from_decision(&d);
        let json = serde_json::to_value(&resp).expect("serialise");
        assert!(
            json.get("hookSpecificOutput").is_none(),
            "Cursor must not wrap response: {json}"
        );
        assert_eq!(json["permission"], "deny");
        assert_eq!(json["user_message"], "blocked");
        assert_eq!(json["agent_message"], "blocked");
    }

    #[test]
    fn cursor_ask_is_preserved_not_demoted() {
        let d = Decision::Ask {
            rule_id: "core.a".into(),
            reason: "confirm please".into(),
        };
        let resp = cursor::from_decision(&d);
        let json = serde_json::to_value(&resp).expect("serialise");
        assert_eq!(
            json["permission"], "ask",
            "Cursor has an ask channel; Ask must not demote to deny"
        );
        assert_eq!(json["user_message"], "confirm please");
    }

    #[test]
    fn pi_allow_serialises_bare_decision_envelope() {
        let resp = pi::from_decision(&Decision::Allow);
        let json = serde_json::to_value(&resp).expect("serialise");
        assert_eq!(json["decision"], "allow");
        assert!(json.get("rule_id").is_none());
        assert!(json.get("reason").is_none());
        assert!(json.get("hookSpecificOutput").is_none());
    }

    #[test]
    fn pi_ask_is_preserved_not_demoted() {
        let d = Decision::Ask {
            rule_id: "core.a".into(),
            reason: "confirm please".into(),
        };
        let resp = pi::from_decision(&d);
        let json = serde_json::to_value(&resp).expect("serialise");
        assert_eq!(json["decision"], "ask");
        assert_eq!(json["rule_id"], "core.a");
        assert_eq!(json["reason"], "confirm please");
    }

    #[test]
    fn pi_deny_includes_rule_id_and_reason() {
        let d = Decision::Deny {
            rule_id: "core.x".into(),
            reason: "blocked".into(),
        };
        let resp = pi::from_decision(&d);
        let json = serde_json::to_value(&resp).expect("serialise");
        assert_eq!(json["decision"], "deny");
        assert_eq!(json["rule_id"], "core.x");
        assert_eq!(json["reason"], "blocked");
        assert!(json.get("hookSpecificOutput").is_none());
    }

    #[test]
    fn pi_monitor_serialises_bare_decision_envelope() {
        let resp = pi::from_decision(&Decision::Monitor {
            rule_id: "core.m".into(),
        });
        let json = serde_json::to_value(&resp).expect("serialise");
        assert_eq!(json["decision"], "monitor");
        assert_eq!(json["rule_id"], "core.m");
    }

    use crate::testing::proptest::{decision, reason_text, rule_id};
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn pbt_claude_allow_and_monitor_yield_none(rule_id in rule_id()) {
            let allow_response = claude_code::from_decision(&Decision::Allow);
            let monitor_response = claude_code::from_decision(&Decision::Monitor { rule_id });
            prop_assert!(allow_response.is_none());
            prop_assert!(monitor_response.is_none());
        }

        #[test]
        fn pbt_claude_ask_emits_ask_verdict(id in rule_id(), reason in reason_text()) {
            let d = Decision::Ask {
                rule_id: id,
                reason: reason.clone(),
            };
            let resp = claude_code::from_decision(&d).expect("ask emits a response");
            prop_assert_eq!(resp.hook_specific_output.permission_decision, "ask");
            prop_assert_eq!(resp.hook_specific_output.hook_event_name, "PreToolUse");
            prop_assert_eq!(resp.hook_specific_output.permission_decision_reason, reason);
        }

        #[test]
        fn pbt_codex_maps_ask_to_deny(id in rule_id(), reason in reason_text()) {
            let d = Decision::Ask {
                rule_id: id,
                reason: reason.clone(),
            };
            let resp = codex::from_decision(&d).expect("ask emits a response");
            prop_assert_eq!(resp.hook_specific_output.permission_decision, "deny");
            prop_assert!(resp.hook_specific_output.permission_decision_reason.contains(&reason));
        }

        #[test]
        fn pbt_deny_emits_deny_verdict_in_both_adapters(id in rule_id(), reason in reason_text()) {
            let d = Decision::Deny {
                rule_id: id,
                reason: reason.clone(),
            };
            let claude = claude_code::from_decision(&d).expect("deny emits a response");
            let codex = codex::from_decision(&d).expect("deny emits a response");
            prop_assert_eq!(claude.hook_specific_output.permission_decision, "deny");
            prop_assert_eq!(codex.hook_specific_output.permission_decision, "deny");
            prop_assert_eq!(claude.hook_specific_output.permission_decision_reason, reason.clone());
            prop_assert_eq!(codex.hook_specific_output.permission_decision_reason, reason);
        }

        #[test]
        fn pbt_response_presence_matches_variant(d in decision()) {
            let expects_response = matches!(d, Decision::Ask { .. } | Decision::Deny { .. });
            prop_assert_eq!(claude_code::from_decision(&d).is_some(), expects_response);
            prop_assert_eq!(codex::from_decision(&d).is_some(), expects_response);
            prop_assert_eq!(copilot::from_decision(&d).is_some(), expects_response);
        }

        #[test]
        fn pbt_copilot_maps_ask_to_deny(id in rule_id(), reason in reason_text()) {
            let d = Decision::Ask {
                rule_id: id,
                reason: reason.clone(),
            };
            let resp = copilot::from_decision(&d).expect("ask emits a response");
            prop_assert_eq!(resp.permission_decision, "deny");
            prop_assert!(resp.permission_decision_reason.contains(&reason));
        }

        #[test]
        fn pbt_copilot_deny_keeps_reason_verbatim(id in rule_id(), reason in reason_text()) {
            let d = Decision::Deny {
                rule_id: id,
                reason: reason.clone(),
            };
            let resp = copilot::from_decision(&d).expect("deny emits a response");
            prop_assert_eq!(resp.permission_decision, "deny");
            prop_assert_eq!(resp.permission_decision_reason, reason);
        }
    }
}