bitrouter-guardrails 0.33.0

BitRouter guardrails — local firewall for AI agent traffic at the proxy layer
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
use std::borrow::Cow;

use bitrouter_core::models::language::{
    call_options::LanguageModelCallOptions,
    content::LanguageModelContent,
    generate_result::LanguageModelGenerateResult,
    prompt::{
        LanguageModelAssistantContent, LanguageModelMessage, LanguageModelToolResult,
        LanguageModelToolResultOutput, LanguageModelToolResultOutputContent,
        LanguageModelUserContent,
    },
    stream_part::LanguageModelStreamPart,
};

use crate::{
    config::{GuardrailConfig, PatternDirection},
    pattern::{
        CompiledPattern, CustomCompiledPattern, builtin_patterns, compile_custom_patterns,
        downgoing_pattern_ids, upgoing_pattern_ids,
    },
    rule::{Action, InspectionResult, REDACTED_PLACEHOLDER, Violation, ViolationSource},
};

/// Traffic direction for guardrail inspection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Direction {
    /// Outbound: user/tool → LLM provider.
    Upgoing,
    /// Inbound: LLM provider → user/tool.
    Downgoing,
}

/// The guardrail engine.
///
/// Pre-compiles all built-in patterns on construction and evaluates
/// incoming/outgoing content against the configured rules.
#[derive(Debug, Clone)]
pub struct Guardrail {
    config: GuardrailConfig,
    patterns: Vec<CompiledPattern>,
    custom_patterns: Vec<CustomCompiledPattern>,
}

impl Guardrail {
    /// Create a new guardrail engine from the given configuration.
    pub fn new(config: GuardrailConfig) -> Self {
        let custom_patterns = compile_custom_patterns(&config.custom_patterns);
        Self {
            config,
            patterns: builtin_patterns(),
            custom_patterns,
        }
    }

    /// Returns `true` when the guardrail engine is disabled and will skip all
    /// checks.
    pub fn is_disabled(&self) -> bool {
        !self.config.enabled
    }

    /// Inspect a text string for upgoing (outbound) pattern matches.
    pub fn inspect_upgoing_text(&self, text: &str) -> InspectionResult {
        self.inspect_text(text, Direction::Upgoing)
    }

    /// Inspect a text string for downgoing (inbound) pattern matches.
    pub fn inspect_downgoing_text(&self, text: &str) -> InspectionResult {
        self.inspect_text(text, Direction::Downgoing)
    }

    /// Unified text inspection for both directions.
    ///
    /// Matches built-in and custom patterns against the input text,
    /// applying the configured action (warn/redact/block) per match.
    /// Redaction uses offset-based replacement to avoid the pitfalls of
    /// `String::replace` (double-replacing overlapping or repeated matches).
    fn inspect_text(&self, text: &str, direction: Direction) -> InspectionResult {
        if self.is_disabled() {
            return InspectionResult {
                violations: vec![],
                blocked: false,
                content: text.to_owned(),
            };
        }

        let dir_label = match direction {
            Direction::Upgoing => "upgoing",
            Direction::Downgoing => "downgoing",
        };

        let pattern_ids = match direction {
            Direction::Upgoing => upgoing_pattern_ids(),
            Direction::Downgoing => downgoing_pattern_ids(),
        };

        let mut violations = Vec::new();
        let mut blocked = false;
        // Collect byte ranges to redact; applied in reverse order at the end.
        let mut redact_ranges: Vec<std::ops::Range<usize>> = Vec::new();

        // Built-in patterns
        for pat in &self.patterns {
            if !pattern_ids.contains(&pat.id) {
                continue;
            }
            if self.config.is_pattern_disabled(pat.id) {
                continue;
            }
            let action = match direction {
                Direction::Upgoing => self.config.upgoing_action(pat.id),
                Direction::Downgoing => self.config.downgoing_action(pat.id),
            };
            for m in pat.regex.find_iter(text) {
                let matched = m.as_str().to_owned();
                match action {
                    Action::Warn => {
                        tracing::warn!(
                            pattern = ?pat.id,
                            matched = %matched,
                            "guardrail: {dir_label} content matched pattern (warn)"
                        );
                    }
                    Action::Redact => {
                        redact_ranges.push(m.range());
                        tracing::info!(
                            pattern = ?pat.id,
                            "guardrail: {dir_label} content redacted"
                        );
                    }
                    Action::Block => {
                        blocked = true;
                        tracing::warn!(
                            pattern = ?pat.id,
                            "guardrail: {dir_label} content blocked"
                        );
                    }
                }
                violations.push(Violation {
                    source: ViolationSource::BuiltIn(pat.id),
                    description: Cow::Borrowed(pat.description),
                    action,
                    matched,
                });
            }
        }

        // Custom patterns
        let (custom_dir_a, custom_dir_b) = match direction {
            Direction::Upgoing => (PatternDirection::Upgoing, PatternDirection::Both),
            Direction::Downgoing => (PatternDirection::Downgoing, PatternDirection::Both),
        };

        for cpat in &self.custom_patterns {
            if cpat.direction != custom_dir_a && cpat.direction != custom_dir_b {
                continue;
            }
            let action = match direction {
                Direction::Upgoing => self.config.custom_upgoing_action(&cpat.name),
                Direction::Downgoing => self.config.custom_downgoing_action(&cpat.name),
            };
            for m in cpat.regex.find_iter(text) {
                let matched = m.as_str().to_owned();
                match action {
                    Action::Warn => {
                        tracing::warn!(
                            pattern = %cpat.name,
                            matched = %matched,
                            "guardrail: {dir_label} content matched custom pattern (warn)"
                        );
                    }
                    Action::Redact => {
                        redact_ranges.push(m.range());
                        tracing::info!(
                            pattern = %cpat.name,
                            "guardrail: {dir_label} custom pattern redacted"
                        );
                    }
                    Action::Block => {
                        blocked = true;
                        tracing::warn!(
                            pattern = %cpat.name,
                            "guardrail: {dir_label} content blocked by custom pattern"
                        );
                    }
                }
                violations.push(Violation {
                    source: ViolationSource::Custom(cpat.name.clone()),
                    description: Cow::Owned(cpat.description.clone()),
                    action,
                    matched,
                });
            }
        }

        // Merge overlapping ranges, then apply in reverse offset order so
        // earlier replacements don't shift the byte positions of later ones.
        redact_ranges.sort_by_key(|a| a.start);
        let merged: Vec<std::ops::Range<usize>> =
            redact_ranges
                .into_iter()
                .fold(Vec::new(), |mut acc, range| {
                    if let Some(last) = acc.last_mut()
                        && range.start <= last.end
                    {
                        last.end = last.end.max(range.end);
                        return acc;
                    }
                    acc.push(range);
                    acc
                });
        let mut content = text.to_owned();
        for range in merged.into_iter().rev() {
            content.replace_range(range, REDACTED_PLACEHOLDER);
        }

        InspectionResult {
            violations,
            blocked,
            content,
        }
    }

    // ── High-level call-options / result inspection ──────────────────

    /// Inspect **outbound** call options (prompt messages). Returns `Err`
    /// with a human-readable reason if any pattern triggered `Block`.
    /// When `Redact` is active, matched substrings in text messages are
    /// replaced in-place.
    pub fn inspect_call_options(
        &self,
        options: &mut LanguageModelCallOptions,
    ) -> Result<Vec<Violation>, String> {
        if self.is_disabled() {
            return Ok(vec![]);
        }

        let mut all_violations = Vec::new();

        for msg in &mut options.prompt {
            match msg {
                LanguageModelMessage::System { content, .. } => {
                    let result = self.inspect_upgoing_text(content);
                    if result.blocked {
                        return Err(self.config.format_block_message(
                            "upgoing system message",
                            &violation_descriptions(&result.violations),
                        ));
                    }
                    *content = result.content;
                    all_violations.extend(result.violations);
                }
                LanguageModelMessage::User { content, .. } => {
                    for item in content.iter_mut() {
                        if let LanguageModelUserContent::Text { text, .. } = item {
                            let result = self.inspect_upgoing_text(text);
                            if result.blocked {
                                return Err(self.config.format_block_message(
                                    "upgoing user message",
                                    &violation_descriptions(&result.violations),
                                ));
                            }
                            *text = result.content;
                            all_violations.extend(result.violations);
                        }
                    }
                }
                LanguageModelMessage::Assistant { content, .. } => {
                    for item in content.iter_mut() {
                        match item {
                            LanguageModelAssistantContent::Text { text, .. } => {
                                let result = self.inspect_upgoing_text(text);
                                if result.blocked {
                                    return Err(self.config.format_block_message(
                                        "upgoing assistant message",
                                        &violation_descriptions(&result.violations),
                                    ));
                                }
                                *text = result.content;
                                all_violations.extend(result.violations);
                            }
                            LanguageModelAssistantContent::Reasoning { text, .. } => {
                                let result = self.inspect_upgoing_text(text);
                                if result.blocked {
                                    return Err(self.config.format_block_message(
                                        "upgoing assistant reasoning",
                                        &violation_descriptions(&result.violations),
                                    ));
                                }
                                *text = result.content;
                                all_violations.extend(result.violations);
                            }
                            _ => {}
                        }
                    }
                }
                LanguageModelMessage::Tool { content, .. } => {
                    for item in content.iter_mut() {
                        if let LanguageModelToolResult::ToolResult { output, .. } = item {
                            self.inspect_tool_result_output_upgoing(output, &mut all_violations)?;
                        }
                    }
                }
            }
        }

        Ok(all_violations)
    }

    /// Recursively inspect tool result output for upgoing patterns.
    fn inspect_tool_result_output_upgoing(
        &self,
        output: &mut LanguageModelToolResultOutput,
        violations: &mut Vec<Violation>,
    ) -> Result<(), String> {
        match output {
            LanguageModelToolResultOutput::Text { value, .. } => {
                let result = self.inspect_upgoing_text(value);
                if result.blocked {
                    return Err(self.config.format_block_message(
                        "upgoing tool result",
                        &violation_descriptions(&result.violations),
                    ));
                }
                *value = result.content;
                violations.extend(result.violations);
            }
            LanguageModelToolResultOutput::ErrorText { value, .. } => {
                let result = self.inspect_upgoing_text(value);
                if result.blocked {
                    return Err(self.config.format_block_message(
                        "upgoing tool error",
                        &violation_descriptions(&result.violations),
                    ));
                }
                *value = result.content;
                violations.extend(result.violations);
            }
            LanguageModelToolResultOutput::Content { value, .. } => {
                for content_item in value.iter_mut() {
                    if let LanguageModelToolResultOutputContent::Text { text, .. } = content_item {
                        let result = self.inspect_upgoing_text(text);
                        if result.blocked {
                            return Err(self.config.format_block_message(
                                "upgoing tool content",
                                &violation_descriptions(&result.violations),
                            ));
                        }
                        *text = result.content;
                        violations.extend(result.violations);
                    }
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// Inspect **inbound** generate result (response content). Returns `Err`
    /// with a reason if any pattern triggered `Block`. When `Redact` is
    /// active, matched text content is replaced in-place.
    pub fn inspect_generate_result(
        &self,
        result: &mut LanguageModelGenerateResult,
    ) -> Result<Vec<Violation>, String> {
        if self.is_disabled() {
            return Ok(vec![]);
        }

        let mut all_violations = Vec::new();

        for block in result.content.iter_mut() {
            match block {
                LanguageModelContent::Text { text, .. } => {
                    let inspection = self.inspect_downgoing_text(text);
                    if inspection.blocked {
                        return Err(self.config.format_block_message(
                            "downgoing text",
                            &violation_descriptions(&inspection.violations),
                        ));
                    }
                    *text = inspection.content;
                    all_violations.extend(inspection.violations);
                }
                LanguageModelContent::Reasoning { text, .. } => {
                    let inspection = self.inspect_downgoing_text(text);
                    if inspection.blocked {
                        return Err(self.config.format_block_message(
                            "downgoing reasoning",
                            &violation_descriptions(&inspection.violations),
                        ));
                    }
                    *text = inspection.content;
                    all_violations.extend(inspection.violations);
                }
                LanguageModelContent::ToolCall { tool_input, .. } => {
                    let inspection = self.inspect_downgoing_text(tool_input);
                    if inspection.blocked {
                        return Err(self.config.format_block_message(
                            "downgoing tool call",
                            &violation_descriptions(&inspection.violations),
                        ));
                    }
                    *tool_input = inspection.content;
                    all_violations.extend(inspection.violations);
                }
                _ => {}
            }
        }

        Ok(all_violations)
    }

    /// Inspect a single **inbound** stream part. Returns `Ok(violations)` on
    /// pass, `Err(reason)` if the part should be blocked. Text deltas are
    /// mutated in-place when `Redact` is active.
    pub fn inspect_stream_part(
        &self,
        part: &mut LanguageModelStreamPart,
    ) -> Result<Vec<Violation>, String> {
        if self.is_disabled() {
            return Ok(vec![]);
        }

        match part {
            LanguageModelStreamPart::TextDelta { delta, .. } => {
                let inspection = self.inspect_downgoing_text(delta);
                if inspection.blocked {
                    return Err(self.config.format_block_message(
                        "downgoing stream text",
                        &violation_descriptions(&inspection.violations),
                    ));
                }
                *delta = inspection.content;
                Ok(inspection.violations)
            }
            LanguageModelStreamPart::ReasoningDelta { delta, .. } => {
                let inspection = self.inspect_downgoing_text(delta);
                if inspection.blocked {
                    return Err(self.config.format_block_message(
                        "downgoing stream reasoning",
                        &violation_descriptions(&inspection.violations),
                    ));
                }
                *delta = inspection.content;
                Ok(inspection.violations)
            }
            LanguageModelStreamPart::ToolInputDelta { delta, .. } => {
                let inspection = self.inspect_downgoing_text(delta);
                if inspection.blocked {
                    return Err(self.config.format_block_message(
                        "downgoing stream tool input",
                        &violation_descriptions(&inspection.violations),
                    ));
                }
                *delta = inspection.content;
                Ok(inspection.violations)
            }
            LanguageModelStreamPart::ToolCall { tool_input, .. } => {
                let inspection = self.inspect_downgoing_text(tool_input);
                if inspection.blocked {
                    return Err(self.config.format_block_message(
                        "downgoing stream tool call",
                        &violation_descriptions(&inspection.violations),
                    ));
                }
                *tool_input = inspection.content;
                Ok(inspection.violations)
            }
            _ => Ok(vec![]),
        }
    }
}

/// Collect human-readable descriptions from block violations.
fn violation_descriptions(violations: &[Violation]) -> String {
    let mut result = String::new();
    for v in violations.iter().filter(|v| v.action == Action::Block) {
        if !result.is_empty() {
            result.push_str(", ");
        }
        result.push_str(&v.description);
    }
    result
}

// ── Tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pattern::PatternId;
    use bitrouter_core::models::language::{
        content::LanguageModelContent,
        finish_reason::LanguageModelFinishReason,
        generate_result::LanguageModelGenerateResult,
        prompt::{LanguageModelMessage, LanguageModelUserContent},
        stream_part::LanguageModelStreamPart,
        usage::{LanguageModelInputTokens, LanguageModelOutputTokens, LanguageModelUsage},
    };

    fn default_usage() -> LanguageModelUsage {
        LanguageModelUsage {
            input_tokens: LanguageModelInputTokens {
                total: None,
                no_cache: None,
                cache_read: None,
                cache_write: None,
            },
            output_tokens: LanguageModelOutputTokens {
                total: None,
                text: None,
                reasoning: None,
            },
            raw: None,
        }
    }

    // ── Disabled engine ──────────────────────────────────────────────

    #[test]
    fn disabled_guardrail_is_noop() {
        let config = GuardrailConfig {
            enabled: false,
            ..Default::default()
        };
        let g = Guardrail::new(config);
        assert!(g.is_disabled());

        let result = g.inspect_upgoing_text("sk-abc123def456ghi789jkl012");
        assert!(result.is_clean());
        assert_eq!(result.content, "sk-abc123def456ghi789jkl012");
    }

    // ── Warn (default) ──────────────────────────────────────────────

    #[test]
    fn default_config_warns_on_api_key() {
        let g = Guardrail::new(GuardrailConfig::default());
        let text = "my key is sk-abc123def456ghi789jkl012 ok";
        let result = g.inspect_upgoing_text(text);
        assert!(!result.blocked);
        // Content is unchanged under Warn
        assert_eq!(result.content, text);
        assert_eq!(result.violations.len(), 1);
        assert!(matches!(
            result.violations[0].source,
            ViolationSource::BuiltIn(PatternId::ApiKeys)
        ));
        assert_eq!(result.violations[0].action, Action::Warn);
    }

    // ── Redact ──────────────────────────────────────────────────────

    #[test]
    fn redact_replaces_api_key_with_placeholder() {
        let mut config = GuardrailConfig::default();
        config.upgoing.insert(PatternId::ApiKeys, Action::Redact);
        let g = Guardrail::new(config);

        let text = "key: sk-abc123def456ghi789jkl012 done";
        let result = g.inspect_upgoing_text(text);
        assert!(!result.blocked);
        assert!(result.content.contains(REDACTED_PLACEHOLDER));
        assert!(!result.content.contains("sk-abc123"));
        assert_eq!(result.violations.len(), 1);
    }

    #[test]
    fn redact_replaces_multiple_matches() {
        let mut config = GuardrailConfig::default();
        config.upgoing.insert(PatternId::PiiEmails, Action::Redact);
        let g = Guardrail::new(config);

        let text = "contact alice@example.com or bob@test.org";
        let result = g.inspect_upgoing_text(text);
        assert!(!result.blocked);
        assert!(!result.content.contains("alice@example.com"));
        assert!(!result.content.contains("bob@test.org"));
        assert_eq!(result.violations.len(), 2);
    }

    // ── Block ───────────────────────────────────────────────────────

    #[test]
    fn block_sets_blocked_flag() {
        let mut config = GuardrailConfig::default();
        config.upgoing.insert(PatternId::PrivateKeys, Action::Block);
        let g = Guardrail::new(config);

        let text = "here is -----BEGIN RSA PRIVATE KEY-----\nMIIE... end";
        let result = g.inspect_upgoing_text(text);
        assert!(result.blocked);
        assert_eq!(result.violations.len(), 1);
        assert_eq!(result.violations[0].action, Action::Block);
    }

    // ── Downgoing inspection ────────────────────────────────────────

    #[test]
    fn downgoing_warns_on_suspicious_command() {
        let g = Guardrail::new(GuardrailConfig::default());
        let text = "try running rm -rf / to clean up";
        let result = g.inspect_downgoing_text(text);
        assert!(!result.blocked);
        assert_eq!(result.violations.len(), 1);
        assert!(matches!(
            result.violations[0].source,
            ViolationSource::BuiltIn(PatternId::SuspiciousCommands)
        ));
    }

    #[test]
    fn downgoing_block_stops_suspicious_command() {
        let mut config = GuardrailConfig::default();
        config
            .downgoing
            .insert(PatternId::SuspiciousCommands, Action::Block);
        let g = Guardrail::new(config);

        let text = "run this: rm -rf / for cleanup";
        let result = g.inspect_downgoing_text(text);
        assert!(result.blocked);
    }

    // ── Call options inspection ──────────────────────────────────────

    #[test]
    fn inspect_call_options_redacts_user_text() {
        let mut config = GuardrailConfig::default();
        config.upgoing.insert(PatternId::ApiKeys, Action::Redact);
        let g = Guardrail::new(config);

        let mut options = LanguageModelCallOptions {
            prompt: vec![LanguageModelMessage::User {
                content: vec![LanguageModelUserContent::Text {
                    text: "my key sk-abc123def456ghi789jkl012 here".to_owned(),
                    provider_options: None,
                }],
                provider_options: None,
            }],
            ..default_call_options()
        };

        let violations = g.inspect_call_options(&mut options).unwrap();
        assert_eq!(violations.len(), 1);

        // The text in the prompt should have been redacted
        assert!(matches!(
            &options.prompt[0],
            LanguageModelMessage::User { .. }
        ));
        let LanguageModelMessage::User { content, .. } = &options.prompt[0] else {
            return;
        };
        assert!(matches!(&content[0], LanguageModelUserContent::Text { .. }));
        let LanguageModelUserContent::Text { text, .. } = &content[0] else {
            return;
        };
        assert!(text.contains(REDACTED_PLACEHOLDER));
        assert!(!text.contains("sk-abc123"));
    }

    #[test]
    fn inspect_call_options_blocks_private_key() {
        let mut config = GuardrailConfig::default();
        config.upgoing.insert(PatternId::PrivateKeys, Action::Block);
        let g = Guardrail::new(config);

        let mut options = LanguageModelCallOptions {
            prompt: vec![LanguageModelMessage::System {
                content: "-----BEGIN PRIVATE KEY-----\nMIIE...".to_owned(),
                provider_options: None,
            }],
            ..default_call_options()
        };

        let err = g.inspect_call_options(&mut options).unwrap_err();
        assert!(err.contains("blocked"));
        assert!(err.contains("PEM-encoded private keys"));
        assert!(err.contains("github.com/bitrouter/bitrouter"));
    }

    // ── Generate result inspection ──────────────────────────────────

    #[test]
    fn inspect_generate_result_blocks_suspicious_tool_call() {
        let mut config = GuardrailConfig::default();
        config
            .downgoing
            .insert(PatternId::SuspiciousCommands, Action::Block);
        let g = Guardrail::new(config);

        let mut gen_result = LanguageModelGenerateResult {
            content: vec![LanguageModelContent::ToolCall {
                tool_call_id: "tc1".to_owned(),
                tool_name: "bash".to_owned(),
                tool_input: "rm -rf /".to_owned(),
                provider_executed: None,
                dynamic: None,
                provider_metadata: None,
            }],
            finish_reason: LanguageModelFinishReason::Stop,
            usage: default_usage(),
            provider_metadata: None,
            request: None,
            response_metadata: None,
            warnings: None,
        };

        let err = g.inspect_generate_result(&mut gen_result).unwrap_err();
        assert!(err.contains("blocked"));
        assert!(err.contains("Dangerous shell commands"));
        assert!(err.contains("github.com/bitrouter/bitrouter"));
    }

    // ── Stream part inspection ──────────────────────────────────────

    #[test]
    fn inspect_stream_part_redacts_text_delta() {
        let mut config = GuardrailConfig::default();
        config
            .downgoing
            .insert(PatternId::SuspiciousCommands, Action::Redact);
        let g = Guardrail::new(config);

        let mut part = LanguageModelStreamPart::TextDelta {
            id: "d1".to_owned(),
            delta: "do: rm -rf / please".to_owned(),
            provider_metadata: None,
        };

        let violations = g.inspect_stream_part(&mut part).unwrap();
        assert_eq!(violations.len(), 1);

        if let LanguageModelStreamPart::TextDelta { delta, .. } = &part {
            assert!(delta.contains(REDACTED_PLACEHOLDER));
            assert!(!delta.contains("rm -rf /"));
        }
    }

    #[test]
    fn inspect_stream_part_noop_for_non_text_parts() {
        let g = Guardrail::new(GuardrailConfig::default());
        let mut part = LanguageModelStreamPart::StreamStart { warnings: vec![] };
        let violations = g.inspect_stream_part(&mut part).unwrap();
        assert!(violations.is_empty());
    }

    // ── Multiple patterns ───────────────────────────────────────────

    #[test]
    fn multiple_patterns_all_detected() {
        let mut config = GuardrailConfig::default();
        config.upgoing.insert(PatternId::ApiKeys, Action::Redact);
        config.upgoing.insert(PatternId::PiiEmails, Action::Redact);
        let g = Guardrail::new(config);

        let text = "key=sk-abc123def456ghi789jkl012 email=user@example.com";
        let result = g.inspect_upgoing_text(text);
        assert_eq!(result.violations.len(), 2);
        assert!(result.content.contains(REDACTED_PLACEHOLDER));
        assert!(!result.content.contains("sk-abc123"));
        assert!(!result.content.contains("user@example.com"));
    }

    // ── Disabled patterns ───────────────────────────────────────────

    #[test]
    fn disabled_builtin_pattern_is_skipped() {
        let mut config = GuardrailConfig::default();
        config.upgoing.insert(PatternId::ApiKeys, Action::Block);
        config.disabled_patterns.push(PatternId::ApiKeys);
        let g = Guardrail::new(config);

        let text = "my key sk-abc123def456ghi789jkl012 here";
        let result = g.inspect_upgoing_text(text);
        assert!(result.is_clean());
        assert!(!result.blocked);
    }

    #[test]
    fn disabled_downgoing_pattern_is_skipped() {
        let mut config = GuardrailConfig::default();
        config
            .downgoing
            .insert(PatternId::SuspiciousCommands, Action::Block);
        config.disabled_patterns.push(PatternId::SuspiciousCommands);
        let g = Guardrail::new(config);

        let text = "rm -rf / is fine";
        let result = g.inspect_downgoing_text(text);
        assert!(result.is_clean());
        assert!(!result.blocked);
    }

    // ── Custom patterns ─────────────────────────────────────────────

    #[test]
    fn custom_upgoing_pattern_detects_match() {
        let mut config = GuardrailConfig::default();
        config
            .custom_patterns
            .push(crate::config::CustomPatternDef {
                name: "my_token".to_owned(),
                regex: r"myapp_[A-Za-z0-9]{16}".to_owned(),
                direction: PatternDirection::Upgoing,
            });
        config
            .custom_upgoing
            .insert("my_token".to_owned(), Action::Redact);
        let g = Guardrail::new(config);

        let text = "token: myapp_AAAABBBBCCCCDDDD here";
        let result = g.inspect_upgoing_text(text);
        assert_eq!(result.violations.len(), 1);
        assert!(
            matches!(&result.violations[0].source, ViolationSource::Custom(name) if name == "my_token")
        );
        assert!(result.content.contains(REDACTED_PLACEHOLDER));
        assert!(!result.content.contains("myapp_AAAA"));
    }

    #[test]
    fn custom_downgoing_pattern_blocks() {
        let mut config = GuardrailConfig::default();
        config
            .custom_patterns
            .push(crate::config::CustomPatternDef {
                name: "evil_url".to_owned(),
                regex: r"https://evil\.com".to_owned(),
                direction: PatternDirection::Downgoing,
            });
        config
            .custom_downgoing
            .insert("evil_url".to_owned(), Action::Block);
        let g = Guardrail::new(config);

        let text = "visit https://evil.com for more info";
        let result = g.inspect_downgoing_text(text);
        assert!(result.blocked);
        assert_eq!(result.violations.len(), 1);
        assert!(
            matches!(&result.violations[0].source, ViolationSource::Custom(name) if name == "evil_url")
        );
    }

    #[test]
    fn custom_both_direction_pattern_applies_to_upgoing() {
        let mut config = GuardrailConfig::default();
        config
            .custom_patterns
            .push(crate::config::CustomPatternDef {
                name: "secret_val".to_owned(),
                regex: r"SECRET_VALUE_\d+".to_owned(),
                direction: PatternDirection::Both,
            });
        config
            .custom_upgoing
            .insert("secret_val".to_owned(), Action::Warn);
        let g = Guardrail::new(config);

        let text = "data: SECRET_VALUE_12345";
        let result = g.inspect_upgoing_text(text);
        assert_eq!(result.violations.len(), 1);
    }

    #[test]
    fn custom_both_direction_pattern_applies_to_downgoing() {
        let mut config = GuardrailConfig::default();
        config
            .custom_patterns
            .push(crate::config::CustomPatternDef {
                name: "secret_val".to_owned(),
                regex: r"SECRET_VALUE_\d+".to_owned(),
                direction: PatternDirection::Both,
            });
        config
            .custom_downgoing
            .insert("secret_val".to_owned(), Action::Redact);
        let g = Guardrail::new(config);

        let text = "data: SECRET_VALUE_99";
        let result = g.inspect_downgoing_text(text);
        assert_eq!(result.violations.len(), 1);
        assert!(result.content.contains(REDACTED_PLACEHOLDER));
    }

    #[test]
    fn custom_upgoing_pattern_not_checked_in_downgoing() {
        let mut config = GuardrailConfig::default();
        config
            .custom_patterns
            .push(crate::config::CustomPatternDef {
                name: "up_only".to_owned(),
                regex: r"UP_TOKEN".to_owned(),
                direction: PatternDirection::Upgoing,
            });
        config
            .custom_upgoing
            .insert("up_only".to_owned(), Action::Block);
        let g = Guardrail::new(config);

        let text = "UP_TOKEN should not trigger";
        let result = g.inspect_downgoing_text(text);
        // Custom upgoing-only pattern should not fire on downgoing
        assert!(result.is_clean());
    }

    // ── Block message formatting ────────────────────────────────────

    #[test]
    fn block_message_includes_details_and_link_by_default() {
        let mut config = GuardrailConfig::default();
        config
            .downgoing
            .insert(PatternId::SuspiciousCommands, Action::Block);
        let g = Guardrail::new(config);

        let text = "rm -rf / please";
        let result = g.inspect_downgoing_text(text);
        assert!(result.blocked);
        // Use format_block_message to check what the error would look like
        let msg = g.config.format_block_message(
            "downgoing text",
            &violation_descriptions(&result.violations),
        );
        assert!(msg.contains("Dangerous shell commands"));
        assert!(msg.contains("github.com/bitrouter/bitrouter"));
    }

    #[test]
    fn block_message_without_details() {
        let mut config = GuardrailConfig::default();
        config
            .downgoing
            .insert(PatternId::SuspiciousCommands, Action::Block);
        config.block_message.include_details = false;
        let g = Guardrail::new(config);

        let mut gen_result = LanguageModelGenerateResult {
            content: vec![LanguageModelContent::ToolCall {
                tool_call_id: "tc1".to_owned(),
                tool_name: "bash".to_owned(),
                tool_input: "rm -rf /".to_owned(),
                provider_executed: None,
                dynamic: None,
                provider_metadata: None,
            }],
            finish_reason: LanguageModelFinishReason::Stop,
            usage: default_usage(),
            provider_metadata: None,
            request: None,
            response_metadata: None,
            warnings: None,
        };

        let err = g.inspect_generate_result(&mut gen_result).unwrap_err();
        assert!(err.contains("blocked"));
        assert!(!err.contains("Dangerous shell commands"));
        assert!(err.contains("github.com/bitrouter/bitrouter"));
    }

    #[test]
    fn block_message_without_link() {
        let mut config = GuardrailConfig::default();
        config
            .downgoing
            .insert(PatternId::SuspiciousCommands, Action::Block);
        config.block_message.include_help_link = false;
        let g = Guardrail::new(config);

        let mut gen_result = LanguageModelGenerateResult {
            content: vec![LanguageModelContent::ToolCall {
                tool_call_id: "tc1".to_owned(),
                tool_name: "bash".to_owned(),
                tool_input: "rm -rf /".to_owned(),
                provider_executed: None,
                dynamic: None,
                provider_metadata: None,
            }],
            finish_reason: LanguageModelFinishReason::Stop,
            usage: default_usage(),
            provider_metadata: None,
            request: None,
            response_metadata: None,
            warnings: None,
        };

        let err = g.inspect_generate_result(&mut gen_result).unwrap_err();
        assert!(err.contains("blocked"));
        assert!(err.contains("Dangerous shell commands"));
        assert!(!err.contains("github.com/bitrouter/bitrouter"));
    }

    // ── Helpers ─────────────────────────────────────────────────────

    fn default_call_options() -> LanguageModelCallOptions {
        LanguageModelCallOptions {
            prompt: vec![],
            stream: None,
            max_output_tokens: None,
            temperature: None,
            top_p: None,
            top_k: None,
            stop_sequences: None,
            presence_penalty: None,
            frequency_penalty: None,
            response_format: None,
            seed: None,
            tools: None,
            tool_choice: None,
            include_raw_chunks: None,
            abort_signal: None,
            headers: None,
            reasoning_effort: None,
            provider_options: None,
        }
    }
}