wallfacer-core 0.3.1

Runtime fuzzing and invariant-testing harness for MCP servers — catch crashes, hangs, schema drift, and state leaks before they ship.
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
//! YAML invariants DSL.
//!
//! Phase D introduces:
//!
//! * **Typed operands** — `equals: { lhs: { path: "$.x" }, rhs: { value: 42 } }`
//!   removes the legacy `starts_with('$')` heuristic. The legacy form
//!   (`lhs: "$.x"`) keeps working: a bare string starting with `$` is still
//!   resolved as a path, anything else is a literal.
//! * **Boolean combinators** — `all_of`, `any_of`, `not`.
//! * **`for_each`** — runs child assertions for every node matched by a
//!   wildcard JSONPath.
//! * **`matches_schema`** — validates the value at a path against an inline
//!   JSON Schema using `jsonschema 0.46`.
//! * **Versioning** — `version: 1` and `version: 2` are accepted by the
//!   same parser; v2 unlocks the new constructs above without changing how
//!   v1 files parse.
//!
//! Phase G adds:
//!
//! * **`version: 3`** with a `metadata` block: `name`, `description`,
//!   `authors`, `tags`, `parameters`, and `extends`.
//! * **Mustache-style templating** — every `{{var}}` in the file is
//!   resolved before YAML parsing using parameter defaults overridden by
//!   the caller. References to undeclared parameters error.
//! * **`extends`** — pack inheritance with cycle detection and a depth
//!   cap; resolution lives in `crate::run::pack` because it requires a
//!   loader closure.
//!
//! See `tests/fixtures/invariants/*.yaml` for working examples of each
//! construct.

use std::collections::BTreeMap;

use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;

/// Highest invariant file version this build understands.
pub const MAX_VERSION: u64 = 3;

/// Maximum depth of a chain of `metadata.extends` references. The
/// resolver returns an error past this depth so a malformed pack ring
/// cannot lock the loader into an unbounded walk.
pub const MAX_EXTENDS_DEPTH: usize = 4;

#[derive(Debug, Error)]
pub enum DslError {
    /// YAML deserialization failed.
    #[error("failed to parse invariants YAML: {0}")]
    Parse(#[from] serde_yaml::Error),
    /// `generate` and `fixed` were both set or both omitted on the same
    /// invariant.
    #[error("invariant `{0}` must define exactly one of `generate` or `fixed`")]
    InvalidInputMode(String),
    /// File declared a `version` greater than [`MAX_VERSION`].
    #[error("invariants file declares unsupported version `{0}`; expected ≤ {MAX_VERSION}")]
    UnsupportedVersion(u64),
    /// A `{{var}}` reference targets a parameter that the file does not
    /// declare and the caller did not override.
    #[error("undefined template parameter(s): {0:?}")]
    UndefinedParameters(Vec<String>),
    /// The caller passed an override for a parameter the pack does not
    /// declare. We reject these to surface typos rather than silently
    /// ignoring them.
    #[error("override key `{0}` is not declared in metadata.parameters")]
    UnknownParameterOverride(String),
    /// `for_each_tool[*].where.{name,description}_matches` regex failed
    /// to compile.
    #[error("invalid `where` regex: {0}")]
    InvalidWhereRegex(String),
}

pub type Result<T> = std::result::Result<T, DslError>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvariantFile {
    pub version: u64,
    /// Pack-style metadata (name, description, parameters, extends).
    /// Optional: a v1/v2 invariants file omits the block entirely.
    /// (Phase G — version 3 introduces this; older versions ignore it.)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<PackMetadata>,
    pub invariants: Vec<Invariant>,
    /// Phase I — schema-aware invariant templates. Each block is
    /// expanded against the live `client.list_tools()` result at run
    /// time, producing one concrete [`Invariant`] per matching tool.
    /// Default empty.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub for_each_tool: Vec<ForEachToolBlock>,
}

/// Phase I — `for_each_tool` directive: a tool-name-agnostic template
/// expanded at run time against the server's tool list.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForEachToolBlock {
    /// Invariant name template. May contain `{{tool_name}}`, replaced
    /// at expansion time with the matched tool's name.
    pub name: String,
    /// Filter that decides which tools the template applies to.
    #[serde(rename = "where")]
    pub matches: ToolMatch,
    /// Body of the generated invariant, minus `name` (provided by the
    /// block) and `tool` (auto-set to the matched tool's name).
    pub apply: ApplyTemplate,
}

/// Filter for [`ForEachToolBlock`]. Every set field is an AND condition.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ToolMatch {
    /// Match on `tool.annotations` fields. Each set field requires the
    /// tool's annotation to equal the given value.
    #[serde(default, skip_serializing_if = "ToolAnnotationMatch::is_empty")]
    pub annotations: ToolAnnotationMatch,
    /// Regex applied to `tool.name`. Tools whose name does not match
    /// are skipped.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name_matches: Option<String>,
    /// Regex applied to `tool.description`. Tools without a description
    /// or whose description does not match are skipped.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description_matches: Option<String>,
}

/// Subset of MCP `ToolAnnotations` fields the filter understands.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ToolAnnotationMatch {
    #[serde(
        default,
        rename = "readOnlyHint",
        skip_serializing_if = "Option::is_none"
    )]
    pub read_only_hint: Option<bool>,
    #[serde(
        default,
        rename = "destructiveHint",
        skip_serializing_if = "Option::is_none"
    )]
    pub destructive_hint: Option<bool>,
    #[serde(
        default,
        rename = "idempotentHint",
        skip_serializing_if = "Option::is_none"
    )]
    pub idempotent_hint: Option<bool>,
    #[serde(
        default,
        rename = "openWorldHint",
        skip_serializing_if = "Option::is_none"
    )]
    pub open_world_hint: Option<bool>,
}

impl ToolAnnotationMatch {
    /// `true` when no annotation constraint is set; used by the serde
    /// `skip_serializing_if`.
    pub fn is_empty(&self) -> bool {
        self.read_only_hint.is_none()
            && self.destructive_hint.is_none()
            && self.idempotent_hint.is_none()
            && self.open_world_hint.is_none()
    }
}

/// Body of a [`ForEachToolBlock`]. Mirrors [`Invariant`] minus `name`
/// and `tool` — those are supplied by the block at expansion time.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ApplyTemplate {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub generate: Option<BTreeMap<String, ValueSpec>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fixed: Option<BTreeMap<String, Value>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cases: Option<u32>,
    #[serde(rename = "assert")]
    pub assertions: Vec<Assertion>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub test_fixtures: Vec<TestFixture>,
}

/// `metadata` block of a v3 invariants file. Acts as the pack header
/// when the file is loaded as a rule pack.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PackMetadata {
    /// Canonical pack name. When the file is referenced via
    /// `wallfacer property --pack <name>`, `<name>` should match this.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// One-paragraph human-readable description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Author identities (free-form strings, e.g. `"wallfacer-core"`,
    /// `"alice@example.org"`).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub authors: Vec<String>,
    /// Tags for catalog grouping (e.g. `["security", "auth"]`).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
    /// Declared parameters. Every `{{name}}` referenced in the file
    /// must be declared here; the value of `default` is substituted
    /// unless the caller passes an override via `parse_with_overrides`.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub parameters: BTreeMap<String, Parameter>,
    /// Names of other packs whose invariants are imported when this
    /// pack is loaded. Cycles are rejected; depth is capped by
    /// [`MAX_EXTENDS_DEPTH`]. Resolution lives in
    /// `crate::run::pack::resolve_extends`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub extends: Vec<String>,
}

/// Declaration of a single template parameter inside `metadata.parameters`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Parameter {
    /// One-line operator-facing description, surfaced by
    /// `wallfacer pack params <name>`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Logical type (currently informational; the substituted value is
    /// always a string at the YAML source level).
    #[serde(default = "default_param_kind", rename = "type")]
    pub kind: ParamKind,
    /// Default value used when no override is supplied. Always
    /// stringified before substitution.
    pub default: Value,
}

fn default_param_kind() -> ParamKind {
    ParamKind::String
}

/// Logical type of a [`Parameter`]. Informational for now; the
/// substituted value is always inserted as a string at the YAML source
/// level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ParamKind {
    String,
    Integer,
    Number,
    Boolean,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Invariant {
    pub name: String,
    pub tool: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub generate: Option<BTreeMap<String, ValueSpec>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fixed: Option<BTreeMap<String, Value>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cases: Option<u32>,
    #[serde(rename = "assert")]
    pub assertions: Vec<Assertion>,
    /// Phase H — inline test fixtures for `wallfacer pack test`. Each
    /// fixture provides a synthetic response (and optionally an
    /// overriding input) along with whether evaluating the invariant
    /// against it should `pass` or `fail`. The runner compares the
    /// observed outcome to `expect` and surfaces mismatches as test
    /// failures, gating CI on pack quality without needing a live MCP
    /// server.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub test_fixtures: Vec<TestFixture>,
}

/// One scripted test case for an invariant: a synthetic response (and
/// optional input override) plus the expected outcome. Phase H —
/// consumed by `wallfacer pack test`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestFixture {
    /// Free-form short label, surfaced in the `pack test` output table.
    pub name: String,
    /// Optional override for `$.input`. When omitted, the fixture
    /// uses the invariant's `fixed` block (or an empty object if both
    /// are absent).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input: Option<Value>,
    /// Synthetic response value used as `$.response` during evaluation.
    pub response: Value,
    /// Outcome the runner expects when evaluating the invariant against
    /// this fixture.
    pub expect: FixtureExpect,
}

/// Outcome enum for [`TestFixture::expect`]. `Pass` means the invariant
/// must succeed; `Fail` means the invariant must report an assertion
/// failure (a structural error like a bad path is treated as a third
/// category and surfaced as a test failure of its own).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FixtureExpect {
    /// Invariant evaluation must return `Ok(())`.
    Pass,
    /// Invariant evaluation must return `Err(RunnerError::Assertion(...))`.
    Fail,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValueSpec {
    #[serde(rename = "type")]
    pub kind: ValueKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min_length: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_length: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min: Option<i64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max: Option<i64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub items: Option<Box<ValueSpec>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min_items: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_items: Option<usize>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ValueKind {
    String,
    Integer,
    Number,
    Boolean,
    Array,
}

/// An operand of a comparison (`equals`, `not_equals`, `at_most`, ...).
///
/// Three forms are accepted, selected by structure:
///
/// 1. `{ path: "$..." }` — explicit JSONPath, resolved against the
///    `{input, response}` context.
/// 2. `{ value: <any> }` — explicit literal.
/// 3. Anything else — bare value. If it's a string starting with `$` we
///    resolve it as a path (legacy v1 behaviour); otherwise it is treated
///    as a literal.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Operand {
    /// `{ path: "$..." }`
    Path {
        /// JSONPath expression (RFC 9535 syntax).
        path: String,
    },
    /// `{ value: <any> }`
    Literal {
        /// Verbatim value.
        value: Value,
    },
    /// Anything else: number, boolean, plain object, or string. Strings
    /// starting with `$` are resolved as JSONPath at runtime to preserve
    /// the v1 contract; everything else is treated as a literal.
    Direct(Value),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Assertion {
    /// `lhs == rhs` after operand resolution.
    Equals { lhs: Operand, rhs: Operand },
    /// `lhs != rhs` after operand resolution.
    NotEquals { lhs: Operand, rhs: Operand },
    /// Numeric `path <= value`.
    AtMost { path: String, value: Operand },
    /// Numeric `path >= value`.
    AtLeast { path: String, value: Operand },
    /// `len(path) == value` (for arrays / strings).
    LengthEq { path: String, value: Operand },
    /// `len(path) <= value`.
    LengthAtMost { path: String, value: Operand },
    /// `len(path) >= value`.
    LengthAtLeast { path: String, value: Operand },
    /// Type check: the value at `path` has the expected JSON type.
    IsType {
        path: String,
        #[serde(rename = "type")]
        expected: JsonType,
    },
    /// String at `path` matches `pattern` (Rust regex).
    MatchesRegex { path: String, pattern: String },
    /// All child assertions must pass (D1).
    AllOf {
        #[serde(rename = "assert")]
        assertions: Vec<Assertion>,
    },
    /// At least one child assertion must pass (D1).
    AnyOf {
        #[serde(rename = "assert")]
        assertions: Vec<Assertion>,
    },
    /// The single child assertion must fail (D1).
    Not {
        /// The assertion that must NOT hold for this invariant to pass.
        assertion: Box<Assertion>,
    },
    /// For every node matched by the wildcard JSONPath, every child
    /// assertion must pass (D3). The current node is exposed as `$.item`
    /// inside the `assert` block; the original input/response remain
    /// accessible via `$.input` / `$.response`.
    ForEach {
        path: String,
        #[serde(rename = "assert")]
        assertions: Vec<Assertion>,
    },
    /// The value at `path` validates against the inline JSON Schema (D4).
    MatchesSchema {
        path: String,
        /// Inline JSON Schema. Compiled with `jsonschema::validator_for`
        /// at evaluation time.
        schema: Value,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum JsonType {
    String,
    Number,
    Integer,
    Boolean,
    Array,
    Object,
    Null,
}

/// Parses an invariants YAML document with no template overrides. v3
/// files use the `metadata.parameters` defaults verbatim; v1/v2 files
/// pass through unchanged.
pub fn parse(source: &str) -> Result<InvariantFile> {
    parse_with_overrides(source, &BTreeMap::new())
}

/// Parses an invariants YAML document, applying `{{var}}` substitution
/// before YAML parsing.
///
/// Resolution order for the substituted value:
///
/// 1. The caller's `overrides` map (typically built from `--param` CLI
///    flags or `[packs.<name>]` config tables).
/// 2. The `default` field of the matching entry under
///    `metadata.parameters`.
///
/// Every `{{var}}` reference must resolve to a declared parameter — a
/// missing declaration produces [`DslError::UndefinedParameters`]. An
/// override targeting an undeclared parameter likewise produces
/// [`DslError::UnknownParameterOverride`] so typos surface immediately.
pub fn parse_with_overrides(
    source: &str,
    overrides: &BTreeMap<String, String>,
) -> Result<InvariantFile> {
    // First-pass: tolerant parse to extract `metadata.parameters` (we
    // need them before substitution so we can check the override map).
    let raw: serde_yaml::Value = serde_yaml::from_str(source)?;
    let parameters = extract_parameters(&raw);

    // Reject overrides that target undeclared parameters.
    for key in overrides.keys() {
        if !parameters.contains_key(key) {
            return Err(DslError::UnknownParameterOverride(key.clone()));
        }
    }

    // Build the substitution map: defaults first, overrides on top.
    let mut subst: BTreeMap<String, String> = parameters
        .iter()
        .map(|(name, param)| (name.clone(), stringify_default(&param.default)))
        .collect();
    for (key, value) in overrides {
        subst.insert(key.clone(), value.clone());
    }

    // Phase I — when the file declares `for_each_tool` blocks, we
    // auto-inject `tool_name` as a no-op substitution: any
    // `{{tool_name}}` in the source is replaced by itself, so the
    // literal `{{tool_name}}` survives parse and is later rewritten by
    // [`expand_for_each_tool`] for each matching tool. Users that
    // declare `tool_name` themselves (e.g. for testing) keep their own
    // value.
    if has_for_each_tool(&raw) {
        subst
            .entry("tool_name".to_string())
            .or_insert_with(|| "{{tool_name}}".to_string());
    }

    // Apply mustache substitution on the raw text. Any `{{var}}` that
    // is not in `subst` is collected and reported as a single error.
    let substituted = render_template(source, &subst)?;

    // Final-pass: strict parse + structural validation.
    let file: InvariantFile = serde_yaml::from_str(&substituted)?;
    if file.version == 0 || file.version > MAX_VERSION {
        return Err(DslError::UnsupportedVersion(file.version));
    }
    for invariant in &file.invariants {
        if invariant.generate.is_some() == invariant.fixed.is_some() {
            return Err(DslError::InvalidInputMode(invariant.name.clone()));
        }
    }
    Ok(file)
}

/// Phase I — synthesises a concrete [`Invariant`] from a
/// [`ForEachToolBlock`] template using a placeholder tool name. Used
/// by `wallfacer pack test` to evaluate `apply.test_fixtures` offline,
/// without consulting a live MCP server. The `where` filter is
/// bypassed; the placeholder fills in `{{tool_name}}` everywhere.
pub fn synthesize_for_test(block: &ForEachToolBlock, placeholder: &str) -> Result<Invariant> {
    let yaml = serde_yaml::to_string(&block.apply)?;
    let substituted = yaml
        .replace("{{tool_name}}", placeholder)
        .replace("{{ tool_name }}", placeholder);
    let apply: ApplyTemplate = serde_yaml::from_str(&substituted)?;
    let name = block
        .name
        .replace("{{tool_name}}", placeholder)
        .replace("{{ tool_name }}", placeholder);
    Ok(Invariant {
        name,
        tool: placeholder.to_string(),
        generate: apply.generate,
        fixed: apply.fixed,
        cases: apply.cases,
        assertions: apply.assertions,
        test_fixtures: apply.test_fixtures,
    })
}

/// Phase I — expands every [`ForEachToolBlock`] against the supplied
/// tool list, returning one concrete [`Invariant`] per matching tool.
///
/// Each generated invariant has its `tool` field set to the matched
/// tool's name; `{{tool_name}}` in the block's `name` template (or any
/// string inside the apply body) is rewritten to that name.
///
/// Errors propagate from the regex pre-compile step or from the
/// internal serde round-trip used to substitute `{{tool_name}}` deep
/// in the apply tree.
pub fn expand_for_each_tool(
    blocks: &[ForEachToolBlock],
    tools: &[rmcp::model::Tool],
) -> Result<Vec<Invariant>> {
    let mut out = Vec::new();
    for block in blocks {
        let name_re = block
            .matches
            .name_matches
            .as_deref()
            .map(Regex::new)
            .transpose()
            .map_err(|err| DslError::InvalidWhereRegex(err.to_string()))?;
        let description_re = block
            .matches
            .description_matches
            .as_deref()
            .map(Regex::new)
            .transpose()
            .map_err(|err| DslError::InvalidWhereRegex(err.to_string()))?;

        for tool in tools {
            if !block
                .matches
                .matches(tool, name_re.as_ref(), description_re.as_ref())
            {
                continue;
            }
            let tool_name = tool.name.as_ref();
            // Substitute {{tool_name}} in the apply tree by serializing
            // the template, doing a string replace, and re-parsing. This
            // keeps the substitution shallow but catches references in
            // any nested string (assertion paths, fixtures, regex
            // patterns, ...).
            let yaml = serde_yaml::to_string(&block.apply)?;
            let substituted = yaml
                .replace("{{tool_name}}", tool_name)
                .replace("{{ tool_name }}", tool_name);
            let apply: ApplyTemplate = serde_yaml::from_str(&substituted)?;
            let name = block
                .name
                .replace("{{tool_name}}", tool_name)
                .replace("{{ tool_name }}", tool_name);
            out.push(Invariant {
                name,
                tool: tool_name.to_string(),
                generate: apply.generate,
                fixed: apply.fixed,
                cases: apply.cases,
                assertions: apply.assertions,
                test_fixtures: apply.test_fixtures,
            });
        }
    }
    Ok(out)
}

impl ToolMatch {
    /// Returns `true` when the tool satisfies every set field on the
    /// filter. Pre-compiled regexes are passed by the caller to avoid
    /// recompiling per tool.
    pub fn matches(
        &self,
        tool: &rmcp::model::Tool,
        name_re: Option<&Regex>,
        description_re: Option<&Regex>,
    ) -> bool {
        let annotations = tool.annotations.as_ref();
        let check_bool = |configured: Option<bool>, actual: Option<bool>| -> bool {
            match configured {
                Some(want) => actual == Some(want),
                None => true,
            }
        };
        if !check_bool(
            self.annotations.read_only_hint,
            annotations.and_then(|a| a.read_only_hint),
        ) {
            return false;
        }
        if !check_bool(
            self.annotations.destructive_hint,
            annotations.and_then(|a| a.destructive_hint),
        ) {
            return false;
        }
        if !check_bool(
            self.annotations.idempotent_hint,
            annotations.and_then(|a| a.idempotent_hint),
        ) {
            return false;
        }
        if !check_bool(
            self.annotations.open_world_hint,
            annotations.and_then(|a| a.open_world_hint),
        ) {
            return false;
        }
        if let Some(re) = name_re {
            if !re.is_match(tool.name.as_ref()) {
                return false;
            }
        }
        if let Some(re) = description_re {
            let description = tool.description.as_deref().unwrap_or("");
            if !re.is_match(description) {
                return false;
            }
        }
        true
    }
}

/// Returns `true` when the parsed YAML carries a non-empty
/// `for_each_tool` array at the document root (Phase I).
fn has_for_each_tool(value: &serde_yaml::Value) -> bool {
    let key = serde_yaml::Value::String("for_each_tool".to_string());
    value
        .as_mapping()
        .and_then(|m| m.get(&key))
        .and_then(|v| v.as_sequence())
        .is_some_and(|seq| !seq.is_empty())
}

/// Walks a parsed YAML value and pulls `metadata.parameters` out as a
/// strict typed map. Returns an empty map if the path is missing or
/// malformed; the caller's strict pass will flag genuinely broken docs.
fn extract_parameters(value: &serde_yaml::Value) -> BTreeMap<String, Parameter> {
    let metadata_key = serde_yaml::Value::String("metadata".to_string());
    let parameters_key = serde_yaml::Value::String("parameters".to_string());
    let Some(metadata) = value.as_mapping().and_then(|m| m.get(&metadata_key)) else {
        return BTreeMap::new();
    };
    let Some(parameters) = metadata.as_mapping().and_then(|m| m.get(&parameters_key)) else {
        return BTreeMap::new();
    };
    serde_yaml::from_value(parameters.clone()).unwrap_or_default()
}

fn stringify_default(value: &Value) -> String {
    match value {
        Value::String(s) => s.clone(),
        Value::Bool(b) => b.to_string(),
        Value::Number(n) => n.to_string(),
        Value::Null => String::new(),
        // Arrays and objects fall back to canonical JSON; users can use
        // them in templates but should typically pick scalar parameters.
        other => other.to_string(),
    }
}

/// Substitutes every `{{name}}` (with optional surrounding whitespace)
/// in `template` using `vars`. Identifier syntax matches Rust's loose
/// snake-case identifiers: `[A-Za-z_][A-Za-z0-9_]*`.
#[allow(
    clippy::expect_used,
    clippy::unwrap_in_result,
    reason = "static regex pattern is checked at compile-time review and cannot fail at runtime"
)]
fn render_template(template: &str, vars: &BTreeMap<String, String>) -> Result<String> {
    // Compile once per call; the regex is small and patterns this short
    // are fast enough that caching across calls is overkill.
    let re =
        Regex::new(r"\{\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}\}").expect("static regex must compile");
    let mut missing: Vec<String> = Vec::new();
    let result = re.replace_all(template, |captures: &regex::Captures<'_>| {
        let name = captures.get(1).map(|m| m.as_str()).unwrap_or("");
        match vars.get(name) {
            Some(value) => value.clone(),
            None => {
                if !missing.iter().any(|existing| existing == name) {
                    missing.push(name.to_string());
                }
                captures
                    .get(0)
                    .map(|m| m.as_str().to_string())
                    .unwrap_or_default()
            }
        }
    });
    if !missing.is_empty() {
        return Err(DslError::UndefinedParameters(missing));
    }
    Ok(result.into_owned())
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn v1_legacy_form_still_parses() {
        let source = r#"
version: 1
invariants:
  - name: demo
    tool: echo
    fixed: { text: hello }
    assert:
      - kind: equals
        lhs: "$.response.text"
        rhs: "$.input.text"
"#;
        let file = parse(source).unwrap();
        assert_eq!(file.version, 1);
        assert_eq!(file.invariants.len(), 1);
        match &file.invariants[0].assertions[0] {
            Assertion::Equals { lhs, rhs } => {
                // Heuristic form: a bare string starting with `$` deserialises
                // into Operand::Direct, and the runner resolves it as a path.
                assert!(matches!(lhs, Operand::Direct(Value::String(s)) if s == "$.response.text"));
                assert!(matches!(rhs, Operand::Direct(Value::String(s)) if s == "$.input.text"));
            }
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn v2_explicit_operands_parse() {
        let source = r#"
version: 2
invariants:
  - name: demo
    tool: echo
    fixed: { text: hello }
    assert:
      - kind: equals
        lhs: { path: "$.response.text" }
        rhs: { value: hello }
"#;
        let file = parse(source).unwrap();
        match &file.invariants[0].assertions[0] {
            Assertion::Equals { lhs, rhs } => {
                assert!(matches!(lhs, Operand::Path { path } if path == "$.response.text"));
                assert!(
                    matches!(rhs, Operand::Literal { value } if value == &Value::String("hello".into()))
                );
            }
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn combinators_round_trip() {
        let source = r#"
version: 2
invariants:
  - name: combinators
    tool: t
    fixed: {}
    assert:
      - kind: all_of
        assert:
          - kind: equals
            lhs: { path: "$.response.a" }
            rhs: { value: 1 }
          - kind: any_of
            assert:
              - kind: at_least
                path: "$.response.b"
                value: { value: 0 }
              - kind: not
                assertion:
                  kind: equals
                  lhs: { path: "$.response.b" }
                  rhs: { value: -1 }
"#;
        let file = parse(source).unwrap();
        let serialized = serde_yaml::to_string(&file).unwrap();
        let reparsed = parse(&serialized).unwrap();
        assert_eq!(reparsed.invariants.len(), 1);
        // Walking down the tree confirms the structure round-tripped.
        let Assertion::AllOf { assertions } = &reparsed.invariants[0].assertions[0] else {
            panic!("expected all_of");
        };
        assert_eq!(assertions.len(), 2);
        assert!(matches!(assertions[1], Assertion::AnyOf { .. }));
    }

    #[test]
    fn for_each_parses() {
        let source = r#"
version: 2
invariants:
  - name: items
    tool: list
    fixed: {}
    assert:
      - kind: for_each
        path: "$.response.items[*]"
        assert:
          - kind: is_type
            path: "$.item.id"
            type: integer
"#;
        let file = parse(source).unwrap();
        let Assertion::ForEach { path, assertions } = &file.invariants[0].assertions[0] else {
            panic!("expected for_each");
        };
        assert_eq!(path, "$.response.items[*]");
        assert_eq!(assertions.len(), 1);
    }

    #[test]
    fn matches_schema_carries_inline_schema() {
        let source = r#"
version: 2
invariants:
  - name: shape
    tool: t
    fixed: {}
    assert:
      - kind: matches_schema
        path: "$.response.user"
        schema:
          type: object
          required: [name]
          properties:
            name: { type: string }
"#;
        let file = parse(source).unwrap();
        let Assertion::MatchesSchema { path, schema } = &file.invariants[0].assertions[0] else {
            panic!("expected matches_schema");
        };
        assert_eq!(path, "$.response.user");
        assert_eq!(schema["type"], Value::String("object".into()));
        let required = schema["required"].as_array().unwrap();
        assert_eq!(required[0], Value::String("name".into()));
    }

    #[test]
    fn unsupported_version_is_rejected() {
        let source = r#"
version: 99
invariants: []
"#;
        let err = parse(source).unwrap_err();
        assert!(matches!(err, DslError::UnsupportedVersion(99)));
    }

    #[test]
    fn generate_xor_fixed_is_enforced() {
        let source = r#"
version: 2
invariants:
  - name: bad
    tool: t
    generate: { x: { type: integer, min: 0, max: 1 } }
    fixed: { x: 0 }
    assert: []
"#;
        let err = parse(source).unwrap_err();
        assert!(matches!(err, DslError::InvalidInputMode(_)));
    }

    // ---------- Phase G — v3 metadata + templating ----------

    #[test]
    fn v3_minimal_pack_parses() {
        let source = r#"
version: 3
metadata:
  name: demo
  description: "demo pack"
  authors: ["wallfacer-core"]
  tags: [security]
invariants:
  - name: t
    tool: echo
    fixed: {}
    assert:
      - kind: equals
        lhs: { value: 1 }
        rhs: { value: 1 }
"#;
        let file = parse(source).unwrap();
        assert_eq!(file.version, 3);
        let meta = file.metadata.as_ref().expect("metadata");
        assert_eq!(meta.name.as_deref(), Some("demo"));
        assert_eq!(meta.tags, vec!["security".to_string()]);
    }

    #[test]
    fn templating_substitutes_defaults() {
        let source = r#"
version: 3
metadata:
  name: demo
  parameters:
    whoami_tool:
      description: tool returning the current user
      type: string
      default: whoami
invariants:
  - name: t
    tool: "{{whoami_tool}}"
    fixed: {}
    assert: []
"#;
        let file = parse(source).unwrap();
        assert_eq!(file.invariants[0].tool, "whoami");
    }

    #[test]
    fn templating_overrides_take_precedence() {
        let source = r#"
version: 3
metadata:
  name: demo
  parameters:
    whoami_tool:
      type: string
      default: whoami
invariants:
  - name: t
    tool: "{{whoami_tool}}"
    fixed: {}
    assert: []
"#;
        let mut overrides = BTreeMap::new();
        overrides.insert("whoami_tool".to_string(), "getCurrentUser".to_string());
        let file = parse_with_overrides(source, &overrides).unwrap();
        assert_eq!(file.invariants[0].tool, "getCurrentUser");
    }

    #[test]
    fn templating_undeclared_reference_errors() {
        let source = r#"
version: 3
metadata:
  name: demo
invariants:
  - name: t
    tool: "{{whoami_tool}}"
    fixed: {}
    assert: []
"#;
        let err = parse(source).unwrap_err();
        match err {
            DslError::UndefinedParameters(names) => {
                assert_eq!(names, vec!["whoami_tool".to_string()]);
            }
            other => panic!("expected UndefinedParameters, got {other:?}"),
        }
    }

    #[test]
    fn templating_unknown_override_errors() {
        let source = r#"
version: 3
metadata:
  name: demo
invariants:
  - name: t
    tool: echo
    fixed: {}
    assert: []
"#;
        let mut overrides = BTreeMap::new();
        overrides.insert("typoed".to_string(), "x".to_string());
        let err = parse_with_overrides(source, &overrides).unwrap_err();
        assert!(matches!(err, DslError::UnknownParameterOverride(name) if name == "typoed"));
    }

    #[test]
    fn templating_handles_repeated_references() {
        let source = r#"
version: 3
metadata:
  name: demo
  parameters:
    user_tool:
      type: string
      default: whoami
invariants:
  - name: same
    tool: "{{user_tool}}"
    fixed: {}
    assert:
      - kind: equals
        lhs: { path: "$.input" }
        rhs: { value: "{{ user_tool }}" }
"#;
        let file = parse(source).unwrap();
        assert_eq!(file.invariants[0].tool, "whoami");
    }

    #[test]
    fn v2_packs_remain_valid_under_v3_parser() {
        // No `metadata`, no `{{...}}`. Phase G must not break this.
        let source = r#"
version: 2
invariants:
  - name: legacy
    tool: echo
    fixed: { x: 1 }
    assert:
      - kind: equals
        lhs: { path: "$.input.x" }
        rhs: { value: 1 }
"#;
        let file = parse(source).unwrap();
        assert_eq!(file.version, 2);
        assert!(file.metadata.is_none());
    }

    #[test]
    fn v3_round_trip_serde_preserves_metadata_and_invariants() {
        let source = r#"
version: 3
metadata:
  name: roundtrip
  description: probe for serde drift
  authors: [w]
  tags: [t]
  parameters:
    a: { type: string, default: foo }
  extends: [parent]
invariants:
  - name: i1
    tool: "{{a}}"
    fixed: {}
    assert: []
"#;
        let parsed = parse(source).unwrap();
        let yaml = serde_yaml::to_string(&parsed).unwrap();
        let reparsed = parse(&yaml).unwrap();
        assert_eq!(parsed.invariants.len(), reparsed.invariants.len());
        let m1 = parsed.metadata.unwrap();
        let m2 = reparsed.metadata.unwrap();
        assert_eq!(m1.name, m2.name);
        assert_eq!(m1.tags, m2.tags);
        assert_eq!(m1.extends, m2.extends);
        assert_eq!(m1.parameters.len(), m2.parameters.len());
    }

    // ---------- Phase I — for_each_tool ----------

    fn make_tool(name: &str, read_only: Option<bool>) -> rmcp::model::Tool {
        let mut tool = rmcp::model::Tool::new(
            name.to_string(),
            "test tool".to_string(),
            std::sync::Arc::new(serde_json::Map::new()),
        );
        if let Some(read_only) = read_only {
            let mut annotations = rmcp::model::ToolAnnotations::default();
            annotations.read_only_hint = Some(read_only);
            tool = tool.annotate(annotations);
        }
        tool
    }

    #[test]
    fn for_each_tool_parses_with_auto_injected_tool_name() {
        let source = r#"
version: 3
metadata:
  name: tool-annotations
for_each_tool:
  - name: "tool-annotations.read_only_does_not_mutate.{{tool_name}}"
    where:
      annotations:
        readOnlyHint: true
    apply:
      fixed: {}
      assert:
        - kind: matches_schema
          path: "$.response.structuredContent"
          schema: { type: object }
invariants: []
"#;
        let file = parse(source).expect("parse");
        assert_eq!(file.for_each_tool.len(), 1);
        let block = &file.for_each_tool[0];
        // The literal `{{tool_name}}` survives parsing because we
        // auto-inject it as a no-op substitution.
        assert!(block.name.contains("{{tool_name}}"));
        assert_eq!(
            block.matches.annotations.read_only_hint,
            Some(true),
            "where clause didn't deserialise"
        );
    }

    #[test]
    fn for_each_tool_expands_per_matching_tool() {
        let source = r#"
version: 3
metadata:
  name: tool-annotations
for_each_tool:
  - name: "rule.{{tool_name}}"
    where:
      annotations:
        readOnlyHint: true
    apply:
      fixed: {}
      assert:
        - kind: equals
          lhs: { value: 1 }
          rhs: { value: 1 }
invariants: []
"#;
        let file = parse(source).unwrap();
        let tools = vec![
            make_tool("read_user", Some(true)),
            make_tool("delete_user", Some(false)),
            make_tool("get_status", Some(true)),
            make_tool("no_annotations", None),
        ];
        let expanded = expand_for_each_tool(&file.for_each_tool, &tools).unwrap();
        let names: Vec<String> = expanded.iter().map(|i| i.name.clone()).collect();
        assert_eq!(
            names,
            vec!["rule.read_user".to_string(), "rule.get_status".to_string()]
        );
        assert_eq!(expanded[0].tool, "read_user");
    }

    #[test]
    fn for_each_tool_filter_by_name_regex() {
        let source = r#"
version: 3
for_each_tool:
  - name: "rule.{{tool_name}}"
    where:
      name_matches: "^read_"
    apply:
      fixed: {}
      assert: []
invariants: []
"#;
        let file = parse(source).unwrap();
        let tools = vec![
            make_tool("read_user", None),
            make_tool("write_user", None),
            make_tool("read_post", None),
        ];
        let expanded = expand_for_each_tool(&file.for_each_tool, &tools).unwrap();
        let names: Vec<String> = expanded.iter().map(|i| i.name.clone()).collect();
        assert_eq!(
            names,
            vec!["rule.read_user".to_string(), "rule.read_post".to_string()]
        );
    }

    #[test]
    fn for_each_tool_substitutes_in_apply_body() {
        let source = r#"
version: 3
for_each_tool:
  - name: "{{tool_name}}.contract"
    where: {}
    apply:
      fixed:
        echo_back: "{{tool_name}}"
      assert:
        - kind: equals
          lhs: { path: "$.input.echo_back" }
          rhs: { value: "{{tool_name}}" }
invariants: []
"#;
        let file = parse(source).unwrap();
        let tools = vec![make_tool("only_one", None)];
        let expanded = expand_for_each_tool(&file.for_each_tool, &tools).unwrap();
        assert_eq!(expanded.len(), 1);
        assert_eq!(expanded[0].name, "only_one.contract");
        let fixed = expanded[0].fixed.as_ref().unwrap();
        assert_eq!(fixed["echo_back"], serde_json::json!("only_one"));
    }
}