everruns-core 0.15.0

Core agent abstractions for Everruns - agent loop, events, tools, LLM providers
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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
// Declarative guardrail checks engine.
//
// Pure check-evaluation core behind the `guardrails` capability (see
// specs/guardrails.md): a typed, declarative config of deterministic checks
// that the capability compiles onto the existing interception seams
// (streaming output guardrails, pre/post tool hooks) and that the dry-run
// API evaluates against sample text without a session.
//
// Design constraints:
//  - Deterministic only. Checks here run in the streaming hot path and the
//    per-tool-call path, so every rule must evaluate in linear time with no
//    I/O. Model-based checks (classifiers, LLM judges) are a later phase and
//    will never share this code path.
//  - Compiled once, evaluated many times. `GuardrailsConfig::compile`
//    validates and pre-compiles all rules; evaluation never allocates
//    regexes or lowercases word lists.
//  - The `regex` crate guarantees linear-time matching (no backtracking),
//    and `MAX_*` limits bound compile cost, so user-authored patterns cannot
//    DoS the worker (TM-API input validation, TM-DOS resource exhaustion).

use serde::{Deserialize, Serialize};

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

/// Maximum number of checks in one guardrails config.
pub const MAX_CHECKS: usize = 64;
/// Maximum patterns/words/tool globs per check.
pub const MAX_ENTRIES_PER_CHECK: usize = 64;
/// Maximum length of a single pattern/word/tool glob.
pub const MAX_ENTRY_LEN: usize = 512;
/// Maximum length of a custom replacement message.
pub const MAX_REPLACEMENT_LEN: usize = 2_000;
/// Maximum length of a check id.
pub const MAX_CHECK_ID_LEN: usize = 64;
/// Maximum byte length of an `llm_judge` policy prompt.
pub const MAX_JUDGE_PROMPT_LEN: usize = 4_000;
/// Maximum byte length of an `mcp` check's server reference or tool name.
pub const MAX_MCP_REF_LEN: usize = 128;
/// Compiled regex size budget per pattern (bytes). Keeps pathological
/// patterns from ballooning compile time/memory.
const REGEX_SIZE_LIMIT: usize = 1 << 20;
/// Cap on the matched-snippet excerpt carried in a hit. Keeps audit
/// logs and dry-run responses bounded.
const MAX_MATCH_SNIPPET: usize = 200;

/// Default replacement when an output-stage block has no custom text.
pub const DEFAULT_OUTPUT_REPLACEMENT: &str = "[Response withheld by a guardrail.]";
/// Default notice replacing tool output suppressed by a block.
pub const DEFAULT_TOOL_OUTPUT_REPLACEMENT: &str = "[Tool output withheld by a guardrail.]";

/// Whether hits take effect or are only logged. Advisory mode is how a
/// guardrail is tuned against false positives before being made active:
/// checks run and report, but never block or replace anything.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum GuardrailMode {
    #[default]
    Active,
    Advisory,
}

/// Pipeline stage a check applies to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(example = "output"))]
#[serde(rename_all = "snake_case")]
pub enum GuardrailStage {
    /// The model's streamed assistant text (evaluated per delta against the
    /// accumulated output).
    Output,
    /// A tool call before execution (tool name and serialized arguments).
    ToolUse,
    /// A tool result after execution, before it enters model context.
    ToolOutput,
}

impl GuardrailStage {
    pub fn as_str(&self) -> &'static str {
        match self {
            GuardrailStage::Output => "output",
            GuardrailStage::ToolUse => "tool_use",
            GuardrailStage::ToolOutput => "tool_output",
        }
    }
}

/// Per-check failure action. Effective action is downgraded to `Log` when
/// the whole config is in advisory mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum GuardrailOnFail {
    #[default]
    Block,
    Log,
}

/// Deterministic rule variants. Tagged as `"type"` in JSON.
///
/// `LlmJudge` and `Mcp` are the async variants — they are excluded from the
/// sync `evaluate()` path and handled separately by capability hooks via
/// `CompiledGuardrails::judge_checks_for_stage()` and
/// `CompiledGuardrails::mcp_checks_for_stage()`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum GuardrailRule {
    /// Match any of the regex patterns against the stage text.
    Regex { patterns: Vec<String> },
    /// Match any of the words/phrases as substrings of the stage text.
    Blocklist {
        words: Vec<String>,
        #[serde(default)]
        case_sensitive: bool,
    },
    /// Match the tool name against `*`-wildcard patterns. Only valid for
    /// the `tool_use` stage.
    ToolPattern { tools: Vec<String> },
    /// Natural-language policy evaluated by the utility LLM.
    /// Valid only on `tool_use` and `tool_output` stages.
    /// Runs asynchronously in the hook path, not in `evaluate()`.
    /// Cost flows through utility-LLM accounting (not the session budget).
    /// Fails open on timeout or LLM error: the verdict defaults to `allow`
    /// so a judge outage never wedges a turn.
    LlmJudge { prompt: String },
    /// Delegate the guardrail decision to a third-party guardrail served as an
    /// external MCP endpoint, called over Everruns' existing scoped-MCP client.
    /// `server` is a scoped-MCP server reference (sanitized server name) and
    /// `tool` is the guardrail tool/method to call on it.
    /// Valid only on `tool_use` and `tool_output` stages.
    /// Runs asynchronously in the hook path, not in `evaluate()`.
    /// External/higher-risk: the stage payload is sent off-platform to the
    /// configured MCP endpoint (data egress). Tenant scoping is enforced by the
    /// host's per-session MCP connection resolver, which only resolves servers
    /// scoped to the current session/org.
    /// Fails open on timeout, connection error, parse failure, or
    /// server-not-configured: the verdict defaults to `allow` so a guardrail
    /// outage never wedges a turn.
    Mcp { server: String, tool: String },
}

impl GuardrailRule {
    pub fn rule_type(&self) -> &'static str {
        match self {
            GuardrailRule::Regex { .. } => "regex",
            GuardrailRule::Blocklist { .. } => "blocklist",
            GuardrailRule::ToolPattern { .. } => "tool_pattern",
            GuardrailRule::LlmJudge { .. } => "llm_judge",
            GuardrailRule::Mcp { .. } => "mcp",
        }
    }
}

/// One declarative check: a rule bound to a stage with a failure action.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GuardrailCheck {
    /// Optional stable identifier surfaced in reason codes and audit logs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    pub stage: GuardrailStage,
    #[serde(default)]
    pub on_fail: GuardrailOnFail,
    /// Replacement text used when a block suppresses output (output and
    /// tool_output stages) or shown to the user for blocked tool calls.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub replacement: Option<String>,
    #[serde(flatten)]
    pub rule: GuardrailRule,
}

/// Declarative guardrails config — the `guardrails` capability's per-agent
/// config payload and the dry-run API's input.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct GuardrailsConfig {
    #[serde(default)]
    pub mode: GuardrailMode,
    #[serde(default)]
    pub checks: Vec<GuardrailCheck>,
}

impl GuardrailsConfig {
    /// Parse a config out of arbitrary JSON (the capability config value).
    pub fn from_value(value: &serde_json::Value) -> Result<Self, String> {
        if value.is_null() {
            return Ok(Self::default());
        }
        serde_json::from_value(value.clone()).map_err(|e| format!("invalid guardrails config: {e}"))
    }

    /// Validate and compile all checks. Errors are human-readable and refer
    /// to checks by index/id so config editors can surface them.
    pub fn compile(&self) -> Result<CompiledGuardrails, String> {
        if self.checks.len() > MAX_CHECKS {
            return Err(format!(
                "too many checks: {} (max {MAX_CHECKS})",
                self.checks.len()
            ));
        }
        let mut compiled = Vec::with_capacity(self.checks.len());
        let mut judge_checks = Vec::new();
        let mut mcp_checks = Vec::new();
        for (index, check) in self.checks.iter().enumerate() {
            match &check.rule {
                GuardrailRule::LlmJudge { prompt } => {
                    judge_checks.push(compile_judge_check(index, check, prompt)?);
                }
                GuardrailRule::Mcp { server, tool } => {
                    mcp_checks.push(compile_mcp_check(index, check, server, tool)?);
                }
                _ => compiled.push(compile_check(index, check)?),
            }
        }
        Ok(CompiledGuardrails {
            mode: self.mode,
            checks: compiled,
            judge_checks,
            mcp_checks,
        })
    }
}

/// Effective action of a hit after applying the config mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(example = "block"))]
#[serde(rename_all = "snake_case")]
pub enum GuardrailAction {
    Block,
    Log,
}

/// One triggered check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GuardrailHit {
    /// Index of the check in the config's `checks` array.
    pub check_index: usize,
    /// The check's `id`, or `"<type>#<index>"` when none was set.
    pub check_label: String,
    pub stage: GuardrailStage,
    pub rule_type: &'static str,
    /// Effective action (advisory mode downgrades Block to Log).
    pub action: GuardrailAction,
    /// Stable machine-readable code, `guardrail.<rule_type>`.
    pub reason_code: String,
    /// The check's custom replacement, if configured.
    pub replacement: Option<String>,
    /// Bounded excerpt of the matched content (pattern source for regex,
    /// matched word for blocklist, tool name for tool_pattern).
    pub matched: Option<String>,
}

/// A compiled `llm_judge` check, carried separately from the sync checks
/// because it must be evaluated asynchronously by the capability hooks.
#[derive(Debug)]
pub struct CompiledJudgeCheck {
    pub index: usize,
    pub label: String,
    pub stage: GuardrailStage,
    pub on_fail: GuardrailOnFail,
    pub replacement: Option<String>,
    /// The natural-language policy prompt.
    pub prompt: String,
}

/// A compiled `mcp` check, carried separately from the sync checks because it
/// must be evaluated asynchronously (network I/O to an external MCP endpoint)
/// by the capability hooks.
#[derive(Debug)]
pub struct CompiledMcpCheck {
    pub index: usize,
    pub label: String,
    pub stage: GuardrailStage,
    pub on_fail: GuardrailOnFail,
    pub replacement: Option<String>,
    /// Scoped-MCP server reference (sanitized server name).
    pub server: String,
    /// Guardrail tool/method to call on the server.
    pub tool: String,
}

/// Validated, pre-compiled guardrails ready for evaluation.
#[derive(Debug)]
pub struct CompiledGuardrails {
    mode: GuardrailMode,
    checks: Vec<CompiledCheck>,
    /// LLM-judge checks, separated from the sync deterministic checks.
    /// Evaluated asynchronously by capability hooks; never by `evaluate()`.
    judge_checks: Vec<CompiledJudgeCheck>,
    /// MCP-served checks, separated from the sync deterministic checks.
    /// Evaluated asynchronously by capability hooks; never by `evaluate()`.
    mcp_checks: Vec<CompiledMcpCheck>,
}

#[derive(Debug)]
struct CompiledCheck {
    index: usize,
    label: String,
    stage: GuardrailStage,
    on_fail: GuardrailOnFail,
    replacement: Option<String>,
    rule_type: &'static str,
    matcher: CompiledRule,
}

#[derive(Debug)]
enum CompiledRule {
    Regex(Vec<regex::Regex>),
    Blocklist {
        /// Lowercased ahead of time for case-insensitive matching.
        words: Vec<String>,
        case_sensitive: bool,
    },
    ToolPattern(Vec<String>),
}

impl CompiledGuardrails {
    pub fn mode(&self) -> GuardrailMode {
        self.mode
    }

    /// Whether any check (deterministic, llm_judge, or mcp) applies to `stage`.
    pub fn has_stage(&self, stage: GuardrailStage) -> bool {
        self.checks.iter().any(|c| c.stage == stage)
            || self.judge_checks.iter().any(|c| c.stage == stage)
            || self.mcp_checks.iter().any(|c| c.stage == stage)
    }

    /// LLM-judge checks that target `stage`. Empty when no `llm_judge` rule
    /// targets `stage`. Callers run these asynchronously via the utility LLM.
    pub fn judge_checks_for_stage(
        &self,
        stage: GuardrailStage,
    ) -> impl Iterator<Item = &CompiledJudgeCheck> {
        self.judge_checks.iter().filter(move |c| c.stage == stage)
    }

    /// MCP-served checks that target `stage`. Empty when no `mcp` rule targets
    /// `stage`. Callers run these asynchronously via the scoped-MCP client.
    pub fn mcp_checks_for_stage(
        &self,
        stage: GuardrailStage,
    ) -> impl Iterator<Item = &CompiledMcpCheck> {
        self.mcp_checks.iter().filter(move |c| c.stage == stage)
    }

    /// Effective action for an async check hit (llm_judge / mcp), applying
    /// advisory mode.
    pub fn async_action(&self, on_fail: GuardrailOnFail) -> GuardrailAction {
        match (self.mode, on_fail) {
            (GuardrailMode::Advisory, _) | (_, GuardrailOnFail::Log) => GuardrailAction::Log,
            (GuardrailMode::Active, GuardrailOnFail::Block) => GuardrailAction::Block,
        }
    }

    /// Effective action for a judge check hit, applying advisory mode.
    /// Retained as a name-stable alias of [`Self::async_action`].
    pub fn judge_action(&self, on_fail: GuardrailOnFail) -> GuardrailAction {
        self.async_action(on_fail)
    }

    /// Evaluate all checks for `stage` against `text`. For the `tool_use`
    /// stage, `tool_name` feeds `tool_pattern` rules and `text` is the
    /// serialized tool arguments; other stages ignore `tool_name`.
    /// `skip` suppresses checks by index (used by the streaming run to
    /// avoid re-reporting an already-logged advisory hit on every delta).
    pub fn evaluate(
        &self,
        stage: GuardrailStage,
        text: &str,
        tool_name: Option<&str>,
        skip: &dyn Fn(usize) -> bool,
    ) -> Vec<GuardrailHit> {
        let lowercased: std::cell::OnceCell<String> = std::cell::OnceCell::new();
        let mut hits = Vec::new();
        for check in self.checks.iter() {
            if check.stage != stage || skip(check.index) {
                continue;
            }
            let matched = match &check.matcher {
                CompiledRule::Regex(patterns) => patterns
                    .iter()
                    .find_map(|re| re.find(text).map(|m| snippet(m.as_str()))),
                CompiledRule::Blocklist {
                    words,
                    case_sensitive,
                } => {
                    let haystack: &str = if *case_sensitive {
                        text
                    } else {
                        lowercased.get_or_init(|| text.to_lowercase())
                    };
                    words
                        .iter()
                        .find(|w| haystack.contains(w.as_str()))
                        .map(|w| snippet(w))
                }
                CompiledRule::ToolPattern(patterns) => tool_name.and_then(|name| {
                    patterns
                        .iter()
                        .find(|p| wildcard_match(p, name))
                        .map(|_| snippet(name))
                }),
            };
            if matched.is_some() {
                let action = match (self.mode, check.on_fail) {
                    (GuardrailMode::Advisory, _) | (_, GuardrailOnFail::Log) => {
                        GuardrailAction::Log
                    }
                    (GuardrailMode::Active, GuardrailOnFail::Block) => GuardrailAction::Block,
                };
                hits.push(GuardrailHit {
                    check_index: check.index,
                    check_label: check.label.clone(),
                    stage: check.stage,
                    rule_type: check.rule_type,
                    action,
                    reason_code: format!("guardrail.{}", check.rule_type),
                    replacement: check.replacement.clone(),
                    matched,
                });
            }
        }
        hits
    }
}

fn compile_check(index: usize, check: &GuardrailCheck) -> Result<CompiledCheck, String> {
    let label = match &check.id {
        Some(id) => {
            if id.is_empty() || id.chars().count() > MAX_CHECK_ID_LEN {
                return Err(format!(
                    "check #{index}: id must be 1..={MAX_CHECK_ID_LEN} characters"
                ));
            }
            id.clone()
        }
        None => format!("{}#{}", check.rule.rule_type(), index),
    };
    if let Some(replacement) = &check.replacement
        && replacement.len() > MAX_REPLACEMENT_LEN
    {
        return Err(format!(
            "check '{label}': replacement exceeds {MAX_REPLACEMENT_LEN} bytes"
        ));
    }
    let matcher = match &check.rule {
        GuardrailRule::Regex { patterns } => {
            validate_entries(&label, "patterns", patterns)?;
            let mut compiled = Vec::with_capacity(patterns.len());
            for pattern in patterns {
                let re = regex::RegexBuilder::new(pattern)
                    .size_limit(REGEX_SIZE_LIMIT)
                    .build()
                    .map_err(|e| format!("check '{label}': invalid regex '{pattern}': {e}"))?;
                compiled.push(re);
            }
            CompiledRule::Regex(compiled)
        }
        GuardrailRule::Blocklist {
            words,
            case_sensitive,
        } => {
            validate_entries(&label, "words", words)?;
            let words = if *case_sensitive {
                words.clone()
            } else {
                words.iter().map(|w| w.to_lowercase()).collect()
            };
            CompiledRule::Blocklist {
                words,
                case_sensitive: *case_sensitive,
            }
        }
        GuardrailRule::ToolPattern { tools } => {
            if check.stage != GuardrailStage::ToolUse {
                return Err(format!(
                    "check '{label}': tool_pattern is only valid for the tool_use stage"
                ));
            }
            validate_entries(&label, "tools", tools)?;
            CompiledRule::ToolPattern(tools.clone())
        }
        GuardrailRule::LlmJudge { .. } => {
            unreachable!(
                "llm_judge checks are routed to compile_judge_check before compile_check is called"
            )
        }
        GuardrailRule::Mcp { .. } => {
            unreachable!(
                "mcp checks are routed to compile_mcp_check before compile_check is called"
            )
        }
    };
    Ok(CompiledCheck {
        index,
        label,
        stage: check.stage,
        on_fail: check.on_fail,
        replacement: check.replacement.clone(),
        rule_type: check.rule.rule_type(),
        matcher,
    })
}

fn compile_judge_check(
    index: usize,
    check: &GuardrailCheck,
    prompt: &str,
) -> Result<CompiledJudgeCheck, String> {
    let label = match &check.id {
        Some(id) => {
            if id.is_empty() || id.chars().count() > MAX_CHECK_ID_LEN {
                return Err(format!(
                    "check #{index}: id must be 1..={MAX_CHECK_ID_LEN} characters"
                ));
            }
            id.clone()
        }
        None => format!("llm_judge#{index}"),
    };
    if prompt.is_empty() {
        return Err(format!(
            "check '{label}': llm_judge prompt must not be empty"
        ));
    }
    if prompt.len() > MAX_JUDGE_PROMPT_LEN {
        return Err(format!(
            "check '{label}': llm_judge prompt exceeds {MAX_JUDGE_PROMPT_LEN} bytes"
        ));
    }
    match check.stage {
        GuardrailStage::ToolUse | GuardrailStage::ToolOutput => {}
        GuardrailStage::Output => {
            return Err(format!(
                "check '{label}': llm_judge is not supported on the 'output' stage in this phase; \
                 use 'tool_use' or 'tool_output'"
            ));
        }
    }
    if let Some(replacement) = &check.replacement
        && replacement.len() > MAX_REPLACEMENT_LEN
    {
        return Err(format!(
            "check '{label}': replacement exceeds {MAX_REPLACEMENT_LEN} bytes"
        ));
    }
    Ok(CompiledJudgeCheck {
        index,
        label,
        stage: check.stage,
        on_fail: check.on_fail,
        replacement: check.replacement.clone(),
        prompt: prompt.to_string(),
    })
}

fn compile_mcp_check(
    index: usize,
    check: &GuardrailCheck,
    server: &str,
    tool: &str,
) -> Result<CompiledMcpCheck, String> {
    let label = match &check.id {
        Some(id) => {
            if id.is_empty() || id.chars().count() > MAX_CHECK_ID_LEN {
                return Err(format!(
                    "check #{index}: id must be 1..={MAX_CHECK_ID_LEN} characters"
                ));
            }
            id.clone()
        }
        None => format!("mcp#{index}"),
    };
    // The mcp check, like llm_judge, only makes sense at the tool seams in this
    // phase; the `output` stage depends on the end-of-message seam (EVE-573).
    match check.stage {
        GuardrailStage::ToolUse | GuardrailStage::ToolOutput => {}
        GuardrailStage::Output => {
            return Err(format!(
                "check '{label}': mcp is not supported on the 'output' stage in this phase; \
                 use 'tool_use' or 'tool_output'"
            ));
        }
    }
    for (field, value) in [("server", server), ("tool", tool)] {
        if value.is_empty() {
            return Err(format!("check '{label}': mcp {field} must not be empty"));
        }
        if value.len() > MAX_MCP_REF_LEN {
            return Err(format!(
                "check '{label}': mcp {field} exceeds {MAX_MCP_REF_LEN} bytes"
            ));
        }
    }
    if let Some(replacement) = &check.replacement
        && replacement.len() > MAX_REPLACEMENT_LEN
    {
        return Err(format!(
            "check '{label}': replacement exceeds {MAX_REPLACEMENT_LEN} bytes"
        ));
    }
    Ok(CompiledMcpCheck {
        index,
        label,
        stage: check.stage,
        on_fail: check.on_fail,
        replacement: check.replacement.clone(),
        server: server.to_string(),
        tool: tool.to_string(),
    })
}

fn validate_entries(label: &str, field: &str, entries: &[String]) -> Result<(), String> {
    if entries.is_empty() {
        return Err(format!("check '{label}': {field} must not be empty"));
    }
    if entries.len() > MAX_ENTRIES_PER_CHECK {
        return Err(format!(
            "check '{label}': too many {field}: {} (max {MAX_ENTRIES_PER_CHECK})",
            entries.len()
        ));
    }
    for entry in entries {
        if entry.is_empty() {
            return Err(format!(
                "check '{label}': {field} entries must not be empty"
            ));
        }
        if entry.len() > MAX_ENTRY_LEN {
            return Err(format!(
                "check '{label}': {field} entry exceeds {MAX_ENTRY_LEN} bytes"
            ));
        }
    }
    Ok(())
}

fn snippet(s: &str) -> String {
    let mut end = MAX_MATCH_SNIPPET.min(s.len());
    while end > 0 && !s.is_char_boundary(end) {
        end -= 1;
    }
    s[..end].to_string()
}

/// `*`-wildcard match over tool names: `*` matches any (possibly empty)
/// run of characters; everything else is literal. Linear in input length.
pub fn wildcard_match(pattern: &str, name: &str) -> bool {
    let segments: Vec<&str> = pattern.split('*').collect();
    if segments.len() == 1 {
        return pattern == name;
    }
    let mut rest = name;
    for (i, seg) in segments.iter().enumerate() {
        if seg.is_empty() {
            continue;
        }
        if i == 0 {
            match rest.strip_prefix(seg) {
                Some(r) => rest = r,
                None => return false,
            }
        } else if i == segments.len() - 1 {
            return rest.ends_with(seg);
        } else {
            match rest.find(seg) {
                Some(pos) => rest = &rest[pos + seg.len()..],
                None => return false,
            }
        }
    }
    // Pattern ends with '*' (last segment empty) — any remainder matches.
    true
}

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

    fn no_skip() -> impl Fn(usize) -> bool {
        |_| false
    }

    fn compile(value: serde_json::Value) -> Result<CompiledGuardrails, String> {
        GuardrailsConfig::from_value(&value)?.compile()
    }

    #[test]
    fn parses_and_compiles_minimal_config() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "output", "type": "blocklist", "words": ["forbidden"]},
            ]
        }))
        .expect("compiles");
        assert_eq!(compiled.mode(), GuardrailMode::Active);
        assert!(compiled.has_stage(GuardrailStage::Output));
        assert!(!compiled.has_stage(GuardrailStage::ToolUse));
    }

    #[test]
    fn empty_or_null_config_compiles_to_no_checks() {
        let compiled = compile(json!({})).expect("compiles");
        assert!(!compiled.has_stage(GuardrailStage::Output));
        let compiled = GuardrailsConfig::from_value(&serde_json::Value::Null)
            .unwrap()
            .compile()
            .unwrap();
        assert!(!compiled.has_stage(GuardrailStage::Output));
    }

    #[test]
    fn blocklist_matches_case_insensitive_by_default() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "output", "type": "blocklist", "words": ["Secret Word"]},
            ]
        }))
        .unwrap();
        let hits = compiled.evaluate(
            GuardrailStage::Output,
            "this contains a SECRET word inside",
            None,
            &no_skip(),
        );
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].action, GuardrailAction::Block);
        assert_eq!(hits[0].reason_code, "guardrail.blocklist");
        assert_eq!(hits[0].matched.as_deref(), Some("secret word"));
    }

    #[test]
    fn blocklist_case_sensitive_only_matches_exact_case() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "output", "type": "blocklist", "words": ["Secret"], "case_sensitive": true},
            ]
        }))
        .unwrap();
        assert!(
            compiled
                .evaluate(GuardrailStage::Output, "a secret here", None, &no_skip())
                .is_empty()
        );
        assert_eq!(
            compiled
                .evaluate(GuardrailStage::Output, "a Secret here", None, &no_skip())
                .len(),
            1
        );
    }

    #[test]
    fn regex_matches_and_reports_pattern_source() {
        let compiled = compile(json!({
            "checks": [
                {"id": "ssn", "stage": "output", "type": "regex",
                 "patterns": ["\\b\\d{3}-\\d{2}-\\d{4}\\b"]},
            ]
        }))
        .unwrap();
        let hits = compiled.evaluate(
            GuardrailStage::Output,
            "my ssn is 123-45-6789 ok",
            None,
            &no_skip(),
        );
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].check_label, "ssn");
        assert_eq!(hits[0].rule_type, "regex");
    }

    #[test]
    fn invalid_regex_fails_compile_with_check_label() {
        let err = compile(json!({
            "checks": [
                {"id": "bad", "stage": "output", "type": "regex", "patterns": ["("]},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("check 'bad'"), "{err}");
    }

    #[test]
    fn tool_pattern_matches_wildcards_on_tool_use_stage() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "tool_pattern", "tools": ["mcp_*", "bash*"]},
            ]
        }))
        .unwrap();
        let hits = compiled.evaluate(
            GuardrailStage::ToolUse,
            "{\"cmd\":\"ls\"}",
            Some("mcp_github__create_issue"),
            &no_skip(),
        );
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].matched.as_deref(), Some("mcp_github__create_issue"));
        assert!(
            compiled
                .evaluate(GuardrailStage::ToolUse, "{}", Some("read_file"), &no_skip())
                .is_empty()
        );
    }

    #[test]
    fn tool_pattern_rejected_outside_tool_use_stage() {
        let err = compile(json!({
            "checks": [
                {"stage": "output", "type": "tool_pattern", "tools": ["bash*"]},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("only valid for the tool_use stage"), "{err}");
    }

    #[test]
    fn advisory_mode_downgrades_block_to_log() {
        let compiled = compile(json!({
            "mode": "advisory",
            "checks": [
                {"stage": "output", "type": "blocklist", "words": ["x"], "on_fail": "block"},
            ]
        }))
        .unwrap();
        let hits = compiled.evaluate(GuardrailStage::Output, "x", None, &no_skip());
        assert_eq!(hits[0].action, GuardrailAction::Log);
    }

    #[test]
    fn on_fail_log_yields_log_action_in_active_mode() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "output", "type": "blocklist", "words": ["x"], "on_fail": "log"},
            ]
        }))
        .unwrap();
        let hits = compiled.evaluate(GuardrailStage::Output, "x", None, &no_skip());
        assert_eq!(hits[0].action, GuardrailAction::Log);
    }

    #[test]
    fn skip_suppresses_checks_by_index() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "output", "type": "blocklist", "words": ["x"]},
                {"stage": "output", "type": "blocklist", "words": ["y"]},
            ]
        }))
        .unwrap();
        let hits = compiled.evaluate(GuardrailStage::Output, "x y", None, &|i| i == 0);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].check_index, 1);
    }

    #[test]
    fn enforces_limits() {
        let too_many_checks: Vec<_> = (0..=MAX_CHECKS)
            .map(|_| json!({"stage": "output", "type": "blocklist", "words": ["x"]}))
            .collect();
        assert!(
            compile(json!({"checks": too_many_checks}))
                .unwrap_err()
                .contains("too many checks")
        );

        let long_entry = "a".repeat(MAX_ENTRY_LEN + 1);
        assert!(
            compile(json!({
                "checks": [{"stage": "output", "type": "blocklist", "words": [long_entry]}]
            }))
            .unwrap_err()
            .contains("exceeds")
        );

        assert!(
            compile(json!({
                "checks": [{"stage": "output", "type": "blocklist", "words": []}]
            }))
            .unwrap_err()
            .contains("must not be empty")
        );
    }

    #[test]
    fn unknown_fields_are_rejected_gracefully_by_value_parse() {
        // serde keeps unknown fields by default for forward-compat; the
        // important part is that a wrong type errors with a clear message.
        let err = GuardrailsConfig::from_value(&json!({"checks": "nope"})).unwrap_err();
        assert!(err.contains("invalid guardrails config"), "{err}");
    }

    #[test]
    fn wildcard_match_covers_anchors_and_inner_stars() {
        assert!(wildcard_match("bash*", "bashkit_exec"));
        assert!(wildcard_match("*_file", "read_file"));
        assert!(wildcard_match("mcp_*__delete_*", "mcp_github__delete_repo"));
        assert!(wildcard_match("*", "anything"));
        assert!(wildcard_match("exact", "exact"));
        assert!(!wildcard_match("exact", "exact_no"));
        assert!(!wildcard_match("bash*", "zsh"));
        assert!(!wildcard_match(
            "mcp_*__delete_*",
            "mcp_github__create_repo"
        ));
    }

    #[test]
    fn config_roundtrips_serde() {
        let cfg = GuardrailsConfig {
            mode: GuardrailMode::Advisory,
            checks: vec![GuardrailCheck {
                id: Some("c1".into()),
                stage: GuardrailStage::ToolUse,
                on_fail: GuardrailOnFail::Log,
                replacement: None,
                rule: GuardrailRule::ToolPattern {
                    tools: vec!["bash*".into()],
                },
            }],
        };
        let value = serde_json::to_value(&cfg).unwrap();
        assert_eq!(value["checks"][0]["type"], "tool_pattern");
        assert_eq!(value["checks"][0]["stage"], "tool_use");
        let back = GuardrailsConfig::from_value(&value).unwrap();
        assert_eq!(back, cfg);
    }

    // --- llm_judge tests ---

    #[test]
    fn llm_judge_compiles_for_tool_stages() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "llm_judge", "prompt": "Block requests to delete data."},
                {"id": "tj2", "stage": "tool_output", "type": "llm_judge",
                 "prompt": "Block responses containing PII.", "on_fail": "log"},
            ]
        }))
        .expect("compiles");
        assert!(compiled.has_stage(GuardrailStage::ToolUse));
        assert!(compiled.has_stage(GuardrailStage::ToolOutput));
        // llm_judge checks are not in the sync path
        assert!(
            compiled
                .evaluate(
                    GuardrailStage::ToolUse,
                    "{}",
                    Some("delete_user"),
                    &no_skip()
                )
                .is_empty()
        );
        let use_checks: Vec<_> = compiled
            .judge_checks_for_stage(GuardrailStage::ToolUse)
            .collect();
        assert_eq!(use_checks.len(), 1);
        assert_eq!(use_checks[0].prompt, "Block requests to delete data.");
        assert_eq!(use_checks[0].on_fail, GuardrailOnFail::Block);

        let out_checks: Vec<_> = compiled
            .judge_checks_for_stage(GuardrailStage::ToolOutput)
            .collect();
        assert_eq!(out_checks.len(), 1);
        assert_eq!(out_checks[0].label, "tj2");
        assert_eq!(out_checks[0].on_fail, GuardrailOnFail::Log);
    }

    #[test]
    fn llm_judge_rejected_on_output_stage() {
        let err = compile(json!({
            "checks": [
                {"stage": "output", "type": "llm_judge", "prompt": "Block bad content."},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("not supported on the 'output' stage"), "{err}");
    }

    #[test]
    fn llm_judge_empty_prompt_rejected() {
        let err = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "llm_judge", "prompt": ""},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("prompt must not be empty"), "{err}");
    }

    #[test]
    fn llm_judge_prompt_too_long_rejected() {
        let long_prompt = "x".repeat(MAX_JUDGE_PROMPT_LEN + 1);
        let err = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "llm_judge", "prompt": long_prompt},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("exceeds"), "{err}");
    }

    #[test]
    fn llm_judge_not_in_sync_evaluate() {
        // A config with only an llm_judge check contributes nothing to sync
        // evaluate() — no hits, ever.
        let compiled = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "llm_judge", "prompt": "Block everything."},
            ]
        }))
        .unwrap();
        assert!(
            compiled
                .evaluate(
                    GuardrailStage::ToolUse,
                    "anything",
                    Some("tool"),
                    &no_skip()
                )
                .is_empty()
        );
    }

    #[test]
    fn llm_judge_advisory_downgrades_judge_action() {
        let compiled = compile(json!({
            "mode": "advisory",
            "checks": [
                {"stage": "tool_use", "type": "llm_judge", "prompt": "p", "on_fail": "block"},
            ]
        }))
        .unwrap();
        let check = compiled
            .judge_checks_for_stage(GuardrailStage::ToolUse)
            .next()
            .unwrap();
        // advisory mode downgrades block → log
        assert_eq!(compiled.judge_action(check.on_fail), GuardrailAction::Log);
    }

    #[test]
    fn llm_judge_active_block_yields_block_action() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "llm_judge", "prompt": "p", "on_fail": "block"},
            ]
        }))
        .unwrap();
        let check = compiled
            .judge_checks_for_stage(GuardrailStage::ToolUse)
            .next()
            .unwrap();
        assert_eq!(compiled.judge_action(check.on_fail), GuardrailAction::Block);
    }

    #[test]
    fn llm_judge_serde_roundtrip() {
        let cfg = GuardrailsConfig {
            mode: GuardrailMode::Active,
            checks: vec![GuardrailCheck {
                id: Some("pii-judge".into()),
                stage: GuardrailStage::ToolOutput,
                on_fail: GuardrailOnFail::Log,
                replacement: None,
                rule: GuardrailRule::LlmJudge {
                    prompt: "Block responses that contain PII.".into(),
                },
            }],
        };
        let value = serde_json::to_value(&cfg).unwrap();
        assert_eq!(value["checks"][0]["type"], "llm_judge");
        assert_eq!(value["checks"][0]["stage"], "tool_output");
        assert_eq!(
            value["checks"][0]["prompt"],
            "Block responses that contain PII."
        );
        let back = GuardrailsConfig::from_value(&value).unwrap();
        assert_eq!(back, cfg);
    }

    #[test]
    fn mixed_sync_and_judge_checks_compile_independently() {
        // A config that has both sync and judge checks: sync checks land in
        // evaluate(), judge checks in judge_checks_for_stage().
        let compiled = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "tool_pattern", "tools": ["bash*"]},
                {"stage": "tool_use", "type": "llm_judge", "prompt": "Block policy violations."},
            ]
        }))
        .unwrap();
        // Sync path catches bash tool
        let hits = compiled.evaluate(GuardrailStage::ToolUse, "{}", Some("bash_exec"), &no_skip());
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].rule_type, "tool_pattern");
        // Judge check is available for async evaluation
        let judges: Vec<_> = compiled
            .judge_checks_for_stage(GuardrailStage::ToolUse)
            .collect();
        assert_eq!(judges.len(), 1);
    }

    // --- mcp tests ---

    #[test]
    fn mcp_compiles_for_tool_stages() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "mcp", "server": "guard", "tool": "screen"},
                {"id": "mc2", "stage": "tool_output", "type": "mcp",
                 "server": "guard", "tool": "scan", "on_fail": "log"},
            ]
        }))
        .expect("compiles");
        assert!(compiled.has_stage(GuardrailStage::ToolUse));
        assert!(compiled.has_stage(GuardrailStage::ToolOutput));
        // mcp checks are not in the sync path
        assert!(
            compiled
                .evaluate(
                    GuardrailStage::ToolUse,
                    "{}",
                    Some("delete_user"),
                    &no_skip()
                )
                .is_empty()
        );
        let use_checks: Vec<_> = compiled
            .mcp_checks_for_stage(GuardrailStage::ToolUse)
            .collect();
        assert_eq!(use_checks.len(), 1);
        assert_eq!(use_checks[0].server, "guard");
        assert_eq!(use_checks[0].tool, "screen");
        assert_eq!(use_checks[0].on_fail, GuardrailOnFail::Block);

        let out_checks: Vec<_> = compiled
            .mcp_checks_for_stage(GuardrailStage::ToolOutput)
            .collect();
        assert_eq!(out_checks.len(), 1);
        assert_eq!(out_checks[0].label, "mc2");
        assert_eq!(out_checks[0].on_fail, GuardrailOnFail::Log);
    }

    #[test]
    fn mcp_rejected_on_output_stage() {
        let err = compile(json!({
            "checks": [
                {"stage": "output", "type": "mcp", "server": "guard", "tool": "scan"},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("not supported on the 'output' stage"), "{err}");
    }

    #[test]
    fn mcp_empty_server_or_tool_rejected() {
        let err = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "mcp", "server": "", "tool": "scan"},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("server must not be empty"), "{err}");
        let err = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "mcp", "server": "guard", "tool": ""},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("tool must not be empty"), "{err}");
    }

    #[test]
    fn mcp_ref_too_long_rejected() {
        let long = "x".repeat(MAX_MCP_REF_LEN + 1);
        let err = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "mcp", "server": long, "tool": "scan"},
            ]
        }))
        .unwrap_err();
        assert!(err.contains("exceeds"), "{err}");
    }

    #[test]
    fn mcp_not_in_sync_evaluate() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "mcp", "server": "guard", "tool": "scan"},
            ]
        }))
        .unwrap();
        assert!(
            compiled
                .evaluate(
                    GuardrailStage::ToolUse,
                    "anything",
                    Some("tool"),
                    &no_skip()
                )
                .is_empty()
        );
    }

    #[test]
    fn mcp_advisory_downgrades_action() {
        let compiled = compile(json!({
            "mode": "advisory",
            "checks": [
                {"stage": "tool_use", "type": "mcp", "server": "g", "tool": "t", "on_fail": "block"},
            ]
        }))
        .unwrap();
        let check = compiled
            .mcp_checks_for_stage(GuardrailStage::ToolUse)
            .next()
            .unwrap();
        assert_eq!(compiled.async_action(check.on_fail), GuardrailAction::Log);
    }

    #[test]
    fn mcp_active_block_yields_block_action() {
        let compiled = compile(json!({
            "checks": [
                {"stage": "tool_use", "type": "mcp", "server": "g", "tool": "t", "on_fail": "block"},
            ]
        }))
        .unwrap();
        let check = compiled
            .mcp_checks_for_stage(GuardrailStage::ToolUse)
            .next()
            .unwrap();
        assert_eq!(compiled.async_action(check.on_fail), GuardrailAction::Block);
    }

    #[test]
    fn mcp_serde_roundtrip() {
        let cfg = GuardrailsConfig {
            mode: GuardrailMode::Active,
            checks: vec![GuardrailCheck {
                id: Some("ext-guard".into()),
                stage: GuardrailStage::ToolOutput,
                on_fail: GuardrailOnFail::Log,
                replacement: None,
                rule: GuardrailRule::Mcp {
                    server: "guard".into(),
                    tool: "scan".into(),
                },
            }],
        };
        let value = serde_json::to_value(&cfg).unwrap();
        assert_eq!(value["checks"][0]["type"], "mcp");
        assert_eq!(value["checks"][0]["stage"], "tool_output");
        assert_eq!(value["checks"][0]["server"], "guard");
        assert_eq!(value["checks"][0]["tool"], "scan");
        let back = GuardrailsConfig::from_value(&value).unwrap();
        assert_eq!(back, cfg);
    }
}