adk-guardrail 2.1.0

Guardrails framework for ADK agents - input/output validation, content filtering, PII redaction
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! Guardrails for tool calls.
//!
//! [`Guardrail`](crate::Guardrail) validates [`Content`](adk_core::Content) — a user message or a
//! model response. It never sees a tool call, so it cannot express "this tool may run, but not
//! with these arguments." That left argument-level policy with nowhere to live:
//! [`ToolConfirmationPolicy`](adk_core::ToolConfirmationPolicy) decides per *tool name*, and a
//! plugin short-circuit is a general-purpose hook rather than a stated policy.
//!
//! [`ToolGuardrail`] closes that gap. It runs before a tool executes, sees the tool name and the
//! arguments, and can allow, deny, or revise.

use std::path::{Component, Path, PathBuf};
use std::sync::Arc;

use async_trait::async_trait;
use regex::Regex;
use serde_json::Value;

use crate::Severity;

/// Outcome of validating a tool call.
#[derive(Debug, Clone)]
pub enum ToolGuardrailResult {
    /// The call may proceed unchanged.
    Allow,
    /// The call is refused. The tool does not run.
    Deny {
        /// Why the call was refused. Surfaced to the model so it can adjust.
        reason: String,
        /// How serious the violation is.
        severity: Severity,
    },
    /// The call may proceed, with these arguments instead.
    ///
    /// Use for narrowing rather than broadening — clamping a limit, forcing a dry-run flag,
    /// dropping a field the caller should not set.
    ReviseArgs {
        /// Arguments the tool is invoked with.
        args: Value,
        /// Why the arguments were changed.
        reason: String,
    },
}

impl ToolGuardrailResult {
    /// Refuses the call.
    pub fn deny(reason: impl Into<String>, severity: Severity) -> Self {
        Self::Deny { reason: reason.into(), severity }
    }

    /// Allows the call with replaced arguments.
    pub fn revise(args: Value, reason: impl Into<String>) -> Self {
        Self::ReviseArgs { args, reason: reason.into() }
    }

    /// Whether the call may proceed.
    pub fn is_allowed(&self) -> bool {
        !matches!(self, Self::Deny { .. })
    }
}

/// Validates a tool call before it executes.
///
/// # Example
///
/// ```rust
/// use adk_guardrail::{Severity, ToolGuardrail, ToolGuardrailResult};
/// use async_trait::async_trait;
/// use serde_json::Value;
///
/// /// Refuses a recursive delete regardless of which tool is asked to perform it.
/// struct NoRecursiveDelete;
///
/// #[async_trait]
/// impl ToolGuardrail for NoRecursiveDelete {
///     fn name(&self) -> &str {
///         "no-recursive-delete"
///     }
///
///     async fn validate_call(&self, _tool: &str, args: &Value) -> ToolGuardrailResult {
///         if args.to_string().contains("-rf") {
///             return ToolGuardrailResult::deny("recursive delete is not permitted", Severity::Critical);
///         }
///         ToolGuardrailResult::Allow
///     }
/// }
/// ```
#[async_trait]
pub trait ToolGuardrail: Send + Sync {
    /// Unique name, used in denial messages and logs.
    fn name(&self) -> &str;

    /// Validates a call to `tool_name` with `args`.
    async fn validate_call(&self, tool_name: &str, args: &Value) -> ToolGuardrailResult;

    /// Whether this guardrail applies to `tool_name`. Defaults to every tool.
    ///
    /// Prefer this over an early `Allow` inside
    /// [`validate_call`](Self::validate_call) — a guardrail that declares its scope can be skipped
    /// without being run.
    fn applies_to(&self, _tool_name: &str) -> bool {
        true
    }
}

/// What a [`ToolGuardrailSet`] decided about a call.
#[derive(Debug, Clone)]
pub enum ToolCallDecision {
    /// The call may proceed with these arguments, which may have been revised.
    Allow {
        /// Arguments to invoke the tool with.
        args: Value,
    },
    /// The call is refused.
    Deny {
        /// Guardrail that refused it.
        guardrail: String,
        /// Why.
        reason: String,
        /// How serious.
        severity: Severity,
    },
}

impl ToolCallDecision {
    /// Whether the call may proceed.
    pub fn is_allowed(&self) -> bool {
        matches!(self, Self::Allow { .. })
    }
}

/// A collection of [`ToolGuardrail`]s evaluated together.
///
/// # Example
///
/// ```rust
/// use adk_guardrail::{PathAllowList, Severity, ToolGuardrailSet};
///
/// let guardrails = ToolGuardrailSet::new().with(
///     PathAllowList::new("plist-paths", ["path"], ["/Users/me/Library/LaunchAgents"])
///         .on_tools(["plist_write"]),
/// );
///
/// assert_eq!(guardrails.guardrails().len(), 1);
/// ```
#[derive(Default)]
pub struct ToolGuardrailSet {
    guardrails: Vec<Arc<dyn ToolGuardrail>>,
}

impl ToolGuardrailSet {
    /// Creates an empty set.
    pub fn new() -> Self {
        Self { guardrails: Vec::new() }
    }

    /// Adds a guardrail.
    pub fn with(mut self, guardrail: impl ToolGuardrail + 'static) -> Self {
        self.guardrails.push(Arc::new(guardrail));
        self
    }

    /// Adds a pre-wrapped guardrail.
    pub fn with_arc(mut self, guardrail: Arc<dyn ToolGuardrail>) -> Self {
        self.guardrails.push(guardrail);
        self
    }

    /// The registered guardrails.
    pub fn guardrails(&self) -> &[Arc<dyn ToolGuardrail>] {
        &self.guardrails
    }

    /// Whether no guardrails have been added.
    pub fn is_empty(&self) -> bool {
        self.guardrails.is_empty()
    }

    /// Evaluates a call against every applicable guardrail.
    ///
    /// Guardrails run in order and revisions compose: a later guardrail sees the arguments an
    /// earlier one produced. Evaluation is sequential rather than parallel because a revision has
    /// to be visible to whatever runs next — parallel evaluation would make the outcome depend on
    /// completion order. The first denial stops evaluation, so a denied call is never revised and
    /// never reaches a later guardrail.
    pub async fn evaluate(&self, tool_name: &str, args: &Value) -> ToolCallDecision {
        let mut current = args.clone();

        for guardrail in &self.guardrails {
            if !guardrail.applies_to(tool_name) {
                continue;
            }

            match guardrail.validate_call(tool_name, &current).await {
                ToolGuardrailResult::Allow => {}
                ToolGuardrailResult::Deny { reason, severity } => {
                    tracing::warn!(
                        guardrail = guardrail.name(),
                        tool = tool_name,
                        reason = %reason,
                        ?severity,
                        "tool call denied by guardrail"
                    );
                    return ToolCallDecision::Deny {
                        guardrail: guardrail.name().to_string(),
                        reason,
                        severity,
                    };
                }
                ToolGuardrailResult::ReviseArgs { args: revised, reason } => {
                    tracing::debug!(
                        guardrail = guardrail.name(),
                        tool = tool_name,
                        reason = %reason,
                        "tool call arguments revised by guardrail"
                    );
                    current = revised;
                }
            }
        }

        ToolCallDecision::Allow { args: current }
    }
}

/// Denies a call whose serialized arguments match a pattern.
///
/// The pattern is matched against the JSON encoding of the whole argument object, so it catches a
/// value wherever it appears rather than requiring the field to be named up front.
///
/// # Example
///
/// ```rust
/// use adk_guardrail::{DeniedArgumentPattern, Severity};
///
/// # fn main() -> Result<(), regex::Error> {
/// let guardrail = DeniedArgumentPattern::new("no-force-push", r"--force\b", Severity::High)?
///     .on_tools(["run_command"]);
/// # Ok(())
/// # }
/// ```
pub struct DeniedArgumentPattern {
    name: String,
    pattern: Regex,
    severity: Severity,
    tools: Option<Vec<String>>,
}

impl DeniedArgumentPattern {
    /// Creates a guardrail denying calls whose arguments match `pattern`.
    ///
    /// # Errors
    ///
    /// Returns an error if `pattern` is not a valid regular expression.
    pub fn new(
        name: impl Into<String>,
        pattern: &str,
        severity: Severity,
    ) -> std::result::Result<Self, regex::Error> {
        Ok(Self { name: name.into(), pattern: Regex::new(pattern)?, severity, tools: None })
    }

    /// Restricts this guardrail to the named tools. Without this it applies to every tool.
    pub fn on_tools<I, S>(mut self, tools: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.tools = Some(tools.into_iter().map(Into::into).collect());
        self
    }
}

#[async_trait]
impl ToolGuardrail for DeniedArgumentPattern {
    fn name(&self) -> &str {
        &self.name
    }

    fn applies_to(&self, tool_name: &str) -> bool {
        match &self.tools {
            Some(tools) => tools.iter().any(|t| t == tool_name),
            None => true,
        }
    }

    async fn validate_call(&self, tool_name: &str, args: &Value) -> ToolGuardrailResult {
        if self.pattern.is_match(&args.to_string()) {
            return ToolGuardrailResult::deny(
                format!(
                    "arguments to `{tool_name}` match the denied pattern `{}`",
                    self.pattern.as_str()
                ),
                self.severity,
            );
        }
        ToolGuardrailResult::Allow
    }
}

/// Denies a call whose path-valued arguments fall outside a set of allowed roots.
///
/// Checks the named arguments when present, and requires each to be an absolute path contained by
/// one of the allowed roots. Containment is compared by path component and after resolving the
/// allowed root and every existing candidate component, so string-prefix, dangling-symlink, and
/// resolved-symlink escapes are refused. Any path containing a `..` component is denied outright.
///
/// This is a preflight policy check, not a replacement for opening filesystem paths relative to a
/// trusted directory handle. A hostile process able to replace path components between validation
/// and tool execution can create a time-of-check/time-of-use race; filesystem tools operating
/// across such a trust boundary must still use platform secure-open primitives.
///
/// # Example
///
/// ```rust
/// use adk_guardrail::PathAllowList;
///
/// let guardrail = PathAllowList::new(
///     "launch-agents-only",
///     ["path"],
///     ["/Users/me/Library/LaunchAgents"],
/// );
/// ```
pub struct PathAllowList {
    name: String,
    arg_names: Vec<String>,
    allowed_roots: Vec<PathBuf>,
    severity: Severity,
    tools: Option<Vec<String>>,
}

impl PathAllowList {
    /// Creates a guardrail confining `arg_names` to `allowed_roots`.
    pub fn new<A, S, R, P>(name: impl Into<String>, arg_names: A, allowed_roots: R) -> Self
    where
        A: IntoIterator<Item = S>,
        S: Into<String>,
        R: IntoIterator<Item = P>,
        P: Into<PathBuf>,
    {
        Self {
            name: name.into(),
            arg_names: arg_names.into_iter().map(Into::into).collect(),
            allowed_roots: allowed_roots.into_iter().map(Into::into).collect(),
            severity: Severity::Critical,
            tools: None,
        }
    }

    /// Sets the severity reported on denial. Defaults to [`Severity::Critical`].
    pub fn with_severity(mut self, severity: Severity) -> Self {
        self.severity = severity;
        self
    }

    /// Restricts this guardrail to the named tools. Without this it applies to every tool.
    pub fn on_tools<I, S>(mut self, tools: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.tools = Some(tools.into_iter().map(Into::into).collect());
        self
    }

    /// Whether `candidate` is an absolute, traversal-free path inside an allowed root.
    fn is_permitted(&self, candidate: &str) -> bool {
        let path = Path::new(candidate);

        if !path.is_absolute() {
            return false;
        }

        // A `..` cannot be resolved for a path that may not exist, so it is refused rather than
        // normalized.
        if path.components().any(|c| matches!(c, Component::ParentDir)) {
            return false;
        }

        self.allowed_roots.iter().any(|root| {
            if !root.is_absolute()
                || root.components().any(|component| matches!(component, Component::ParentDir))
                || !path.starts_with(root)
            {
                return false;
            }

            let Ok(canonical_root) = std::fs::canonicalize(root) else {
                // An unresolved policy root cannot establish a trustworthy boundary.
                return false;
            };

            let Ok(relative) = path.strip_prefix(root) else {
                return false;
            };
            let mut current = root.clone();
            for component in relative.components() {
                current.push(component);
                match std::fs::symlink_metadata(&current) {
                    Ok(_) => {
                        let Ok(canonical) = std::fs::canonicalize(&current) else {
                            // Includes dangling symlinks, whose eventual target cannot be trusted.
                            return false;
                        };
                        if !canonical.starts_with(&canonical_root) {
                            return false;
                        }
                    }
                    Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                        // Once a component is absent, every remaining component is new and cannot
                        // currently hide a symlink. The tool may create this suffix.
                        break;
                    }
                    Err(_) => return false,
                }
            }

            true
        })
    }
}

#[async_trait]
impl ToolGuardrail for PathAllowList {
    fn name(&self) -> &str {
        &self.name
    }

    fn applies_to(&self, tool_name: &str) -> bool {
        match &self.tools {
            Some(tools) => tools.iter().any(|t| t == tool_name),
            None => true,
        }
    }

    async fn validate_call(&self, tool_name: &str, args: &Value) -> ToolGuardrailResult {
        for arg_name in &self.arg_names {
            let Some(value) = args.get(arg_name) else {
                continue;
            };

            let Some(candidate) = value.as_str() else {
                return ToolGuardrailResult::deny(
                    format!(
                        "argument `{arg_name}` of `{tool_name}` must be a path string, got \
                         {value}"
                    ),
                    self.severity,
                );
            };

            if !self.is_permitted(candidate) {
                let roots: Vec<_> =
                    self.allowed_roots.iter().map(|r| r.display().to_string()).collect();
                return ToolGuardrailResult::deny(
                    format!(
                        "argument `{arg_name}` of `{tool_name}` is {candidate:?}, which is not an \
                         absolute path inside an allowed root ({})",
                        roots.join(", ")
                    ),
                    self.severity,
                );
            }
        }

        ToolGuardrailResult::Allow
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    /// Revises rather than denies, so composition can be observed.
    struct ForceDryRun;

    #[async_trait]
    impl ToolGuardrail for ForceDryRun {
        fn name(&self) -> &str {
            "force-dry-run"
        }
        async fn validate_call(&self, _tool: &str, args: &Value) -> ToolGuardrailResult {
            let mut revised = args.clone();
            if let Some(object) = revised.as_object_mut() {
                object.insert("dry_run".to_string(), json!(true));
            }
            ToolGuardrailResult::revise(revised, "dry-run is mandatory here")
        }
    }

    struct DenyAll;

    #[async_trait]
    impl ToolGuardrail for DenyAll {
        fn name(&self) -> &str {
            "deny-all"
        }
        async fn validate_call(&self, _tool: &str, _args: &Value) -> ToolGuardrailResult {
            ToolGuardrailResult::deny("nothing is permitted", Severity::Critical)
        }
    }

    #[tokio::test]
    async fn an_empty_set_allows_the_call_unchanged() {
        let decision = ToolGuardrailSet::new().evaluate("any", &json!({ "a": 1 })).await;

        match decision {
            ToolCallDecision::Allow { args } => assert_eq!(args, json!({ "a": 1 })),
            other => panic!("expected Allow, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn a_revision_is_returned_to_the_caller() {
        let set = ToolGuardrailSet::new().with(ForceDryRun);

        match set.evaluate("delete", &json!({ "path": "/tmp/x" })).await {
            ToolCallDecision::Allow { args } => {
                assert_eq!(args, json!({ "path": "/tmp/x", "dry_run": true }));
            }
            other => panic!("expected Allow, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn a_denial_names_the_guardrail_that_refused() {
        let set = ToolGuardrailSet::new().with(DenyAll);

        match set.evaluate("delete", &json!({})).await {
            ToolCallDecision::Deny { guardrail, severity, .. } => {
                assert_eq!(guardrail, "deny-all");
                assert_eq!(severity, Severity::Critical);
            }
            other => panic!("expected Deny, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn a_denial_stops_evaluation_so_a_denied_call_is_never_revised() {
        let set = ToolGuardrailSet::new().with(DenyAll).with(ForceDryRun);

        assert!(!set.evaluate("delete", &json!({})).await.is_allowed());
    }

    #[tokio::test]
    async fn a_later_guardrail_sees_an_earlier_revision() {
        /// Denies unless a previous guardrail already set `dry_run`.
        struct RequireDryRun;

        #[async_trait]
        impl ToolGuardrail for RequireDryRun {
            fn name(&self) -> &str {
                "require-dry-run"
            }
            async fn validate_call(&self, _tool: &str, args: &Value) -> ToolGuardrailResult {
                if args.get("dry_run") == Some(&json!(true)) {
                    ToolGuardrailResult::Allow
                } else {
                    ToolGuardrailResult::deny("dry_run was not set", Severity::High)
                }
            }
        }

        let set = ToolGuardrailSet::new().with(ForceDryRun).with(RequireDryRun);
        assert!(
            set.evaluate("delete", &json!({})).await.is_allowed(),
            "revisions must compose in order"
        );

        let reversed = ToolGuardrailSet::new().with(RequireDryRun).with(ForceDryRun);
        assert!(
            !reversed.evaluate("delete", &json!({})).await.is_allowed(),
            "order is meaningful and must not be silently reordered"
        );
    }

    #[tokio::test]
    async fn applies_to_skips_an_unrelated_tool() {
        let set = ToolGuardrailSet::new().with(
            DeniedArgumentPattern::new("no-rf", r"-rf", Severity::Critical)
                .expect("valid pattern")
                .on_tools(["run_command"]),
        );

        assert!(set.evaluate("read_file", &json!({ "flags": "-rf" })).await.is_allowed());
        assert!(!set.evaluate("run_command", &json!({ "flags": "-rf" })).await.is_allowed());
    }

    #[tokio::test]
    async fn a_denied_pattern_matches_anywhere_in_the_arguments() {
        let guardrail = DeniedArgumentPattern::new("no-rf", r"-rf\b", Severity::Critical)
            .expect("valid pattern");

        for args in [
            json!({ "cmd": "rm -rf /" }),
            json!({ "nested": { "cmd": "rm -rf ." } }),
            json!({ "argv": ["rm", "-rf", "/tmp"] }),
        ] {
            assert!(
                !guardrail.validate_call("run_command", &args).await.is_allowed(),
                "should deny {args}"
            );
        }

        assert!(
            guardrail.validate_call("run_command", &json!({ "cmd": "ls -l" })).await.is_allowed()
        );
    }

    #[test]
    fn an_invalid_pattern_is_reported() {
        assert!(DeniedArgumentPattern::new("bad", "([unclosed", Severity::Low).is_err());
    }

    #[tokio::test]
    async fn a_path_inside_an_allowed_root_is_permitted() {
        let root = tempfile::tempdir().expect("allowed root");
        let guardrail = PathAllowList::new("agents", ["path"], [root.path()]);
        let candidate = root.path().join("x.plist");

        assert!(
            guardrail
                .validate_call("plist_write", &json!({ "path": candidate }))
                .await
                .is_allowed()
        );
    }

    #[tokio::test]
    async fn traversal_and_escape_attempts_are_denied() {
        let guardrail = PathAllowList::new("agents", ["path"], ["/Users/me/Library/LaunchAgents"]);

        for candidate in [
            "/Users/me/Library/LaunchAgents/../../../etc/passwd",
            "/etc/passwd",
            "relative/path.plist",
            "/Users/me/Library/LaunchAgentsEvil/x.plist",
        ] {
            assert!(
                !guardrail
                    .validate_call("plist_write", &json!({ "path": candidate }))
                    .await
                    .is_allowed(),
                "should deny {candidate:?}"
            );
        }
    }

    #[tokio::test]
    async fn a_sibling_root_is_not_admitted_by_string_prefix() {
        // `/etc/passwd-backup` shares a string prefix with `/etc/passwd` but is a different file.
        let guardrail = PathAllowList::new("etc", ["path"], ["/etc/passwd"]);

        assert!(
            !guardrail
                .validate_call("read", &json!({ "path": "/etc/passwd-backup" }))
                .await
                .is_allowed()
        );
    }

    #[tokio::test]
    async fn a_non_string_path_argument_is_denied() {
        let guardrail = PathAllowList::new("agents", ["path"], ["/tmp"]);

        assert!(
            !guardrail.validate_call("write", &json!({ "path": 42 })).await.is_allowed(),
            "a non-string path cannot be checked and must not be waved through"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn a_symlink_inside_the_root_cannot_escape_it() {
        let root = tempfile::tempdir().expect("allowed root");
        let outside = tempfile::tempdir().expect("outside root");
        std::os::unix::fs::symlink(outside.path(), root.path().join("escape"))
            .expect("create symlink");
        let guardrail = PathAllowList::new("root", ["path"], [root.path()]);
        let candidate = root.path().join("escape/secret.txt");

        assert!(
            !guardrail.validate_call("write", &json!({ "path": candidate })).await.is_allowed(),
            "a lexical child resolving outside the allowed root must be denied"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn a_dangling_symlink_inside_the_root_is_denied() {
        let root = tempfile::tempdir().expect("allowed root");
        let outside = tempfile::tempdir().expect("outside root");
        let missing_target = outside.path().join("not-created");
        std::os::unix::fs::symlink(&missing_target, root.path().join("escape"))
            .expect("create dangling symlink");
        let guardrail = PathAllowList::new("root", ["path"], [root.path()]);

        assert!(
            !guardrail
                .validate_call("write", &json!({ "path": root.path().join("escape/secret.txt") }))
                .await
                .is_allowed()
        );
    }

    #[tokio::test]
    async fn an_unresolvable_allowed_root_is_fail_closed() {
        let root = tempfile::tempdir().expect("root");
        let missing = root.path().join("not-created");
        let guardrail = PathAllowList::new("missing", ["path"], [&missing]);

        assert!(
            !guardrail
                .validate_call("write", &json!({ "path": missing.join("file.txt") }))
                .await
                .is_allowed()
        );
    }

    #[tokio::test]
    async fn an_absent_path_argument_is_not_checked() {
        let guardrail = PathAllowList::new("agents", ["path"], ["/tmp"]);

        assert!(
            guardrail.validate_call("write", &json!({ "other": 1 })).await.is_allowed(),
            "a guardrail on `path` says nothing about a call that has no `path`"
        );
    }
}