ptuf 0.2.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
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 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 copilot_ask_serialises_as_deny_with_demote_note() {
        let d = Decision::Ask {
            rule_id: "core.a".into(),
            reason: "confirm please".into(),
        };
        let resp = copilot::from_decision(&d).expect("response");
        let json = serde_json::to_string(&resp).expect("serialise");
        assert!(json.contains("\"permissionDecision\":\"deny\""));
        assert!(json.contains("GitHub Copilot hooks do not reliably process interactive ask"));
    }

    #[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"));
    }

    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);
        }
    }
}