Skip to main content

logit_loom_core/
sampling.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Backend-neutral generation and native-sampler plans.
4
5use std::collections::HashSet;
6
7use serde::{Deserialize, Serialize};
8
9use crate::{CoreError, Digest, TokenId};
10
11/// Maximum exact sequence breakers accepted by one DRY sampler.
12pub const MAX_DRY_SEQUENCE_BREAKERS: usize = 64;
13/// Maximum UTF-8 bytes accepted in one DRY sequence breaker.
14pub const MAX_DRY_SEQUENCE_BREAKER_BYTES: usize = 40;
15/// Maximum additive token biases accepted by one generation plan.
16pub const MAX_LOGIT_BIASES: usize = 65_536;
17/// Maximum UTF-8 bytes accepted in an eager grammar source.
18pub const MAX_GRAMMAR_SOURCE_BYTES: usize = 1024 * 1024;
19/// Maximum UTF-8 bytes accepted in an eager grammar root name.
20pub const MAX_GRAMMAR_ROOT_BYTES: usize = 256;
21/// Maximum trigger patterns accepted by one lazy grammar.
22pub const MAX_LAZY_GRAMMAR_TRIGGER_PATTERNS: usize = 64;
23/// Maximum UTF-8 bytes accepted in one lazy-grammar trigger pattern.
24pub const MAX_LAZY_GRAMMAR_TRIGGER_PATTERN_BYTES: usize = 4_096;
25/// Maximum aggregate UTF-8 bytes accepted across lazy-grammar trigger patterns.
26pub const MAX_LAZY_GRAMMAR_TRIGGER_BYTES: usize = 64 * 1_024;
27/// Maximum trigger tokens accepted by one lazy grammar.
28pub const MAX_LAZY_GRAMMAR_TRIGGER_TOKENS: usize = 4_096;
29/// Maximum exact byte stop sequences accepted by one generation plan.
30pub const MAX_STOP_SEQUENCES: usize = 64;
31/// Maximum bytes accepted in one exact stop sequence.
32pub const MAX_STOP_SEQUENCE_BYTES: usize = 4_096;
33
34/// Stateful repetition, frequency, and presence penalties.
35#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
36pub struct RepetitionSampler {
37    /// Number of preceding tokens to retain; `-1` selects the context bound.
38    pub last_n: i32,
39    /// Multiplicative repeat penalty; `1.0` is neutral.
40    pub repeat_penalty: f32,
41    /// Additive frequency penalty per occurrence.
42    pub frequency_penalty: f32,
43    /// Additive penalty when a token is present.
44    pub presence_penalty: f32,
45}
46
47/// Don't Repeat Yourself sampler configuration.
48#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
49pub struct DrySampler {
50    /// Positive penalty multiplier.
51    pub multiplier: f32,
52    /// Exponential penalty base, at least `1.0`.
53    pub base: f32,
54    /// Prefix length allowed before penalties begin.
55    pub allowed_length: i32,
56    /// Number of preceding tokens to scan; `-1` selects the context bound.
57    pub penalty_last_n: i32,
58    /// Exact sequence breakers.
59    pub sequence_breakers: Vec<String>,
60}
61
62/// Native Mirostat algorithm version.
63#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
64#[serde(rename_all = "snake_case")]
65pub enum MirostatVersion {
66    /// Original Mirostat algorithm.
67    V1,
68    /// Mirostat 2.0.
69    V2,
70}
71
72/// Stateful Mirostat terminal sampler configuration.
73#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
74pub struct MirostatSampler {
75    /// Selected algorithm.
76    pub version: MirostatVersion,
77    /// Target surprisal.
78    pub tau: f32,
79    /// Adaptive learning rate.
80    pub eta: f32,
81    /// Positive candidate-estimation window for version one; zero for version
82    /// two.
83    pub m: i32,
84}
85
86/// Native sampler configuration.
87#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
88pub struct SamplingPlan {
89    /// Deterministic sampler seed.
90    pub seed: u32,
91    /// Temperature; zero selects greedy decoding.
92    pub temperature: f32,
93    /// Top-k candidate count; zero disables the filter.
94    pub top_k: i32,
95    /// Nucleus probability.
96    pub top_p: f32,
97    /// Minimum probability relative to the most likely token.
98    pub min_p: f32,
99    /// Locally typical probability mass; `1.0` disables the filter.
100    pub typical_p: f32,
101    /// Optional repetition/frequency/presence penalties.
102    #[serde(default, skip_serializing_if = "Option::is_none")]
103    pub repetition: Option<RepetitionSampler>,
104    /// Optional DRY sampler.
105    #[serde(default, skip_serializing_if = "Option::is_none")]
106    pub dry: Option<DrySampler>,
107    /// Optional Mirostat terminal sampler.
108    #[serde(default, skip_serializing_if = "Option::is_none")]
109    pub mirostat: Option<MirostatSampler>,
110}
111
112impl Default for SamplingPlan {
113    fn default() -> Self {
114        Self {
115            seed: 0,
116            temperature: 0.0,
117            top_k: 40,
118            top_p: 0.95,
119            min_p: 0.05,
120            typical_p: 1.0,
121            repetition: None,
122            dry: None,
123            mirostat: None,
124        }
125    }
126}
127
128impl SamplingPlan {
129    /// Validates finite values and native sampler bounds.
130    ///
131    /// # Errors
132    ///
133    /// Returns an error for unsupported numeric values.
134    pub fn validate(&self) -> Result<(), CoreError> {
135        for (field, value) in [
136            ("temperature", self.temperature),
137            ("top p", self.top_p),
138            ("min p", self.min_p),
139            ("typical p", self.typical_p),
140        ] {
141            if !value.is_finite() {
142                return Err(CoreError::NonFinite { field });
143            }
144        }
145        if self.temperature < 0.0
146            || self.top_k < 0
147            || !(0.0..=1.0).contains(&self.top_p)
148            || !(0.0..=1.0).contains(&self.min_p)
149            || !(0.0..=1.0).contains(&self.typical_p)
150        {
151            return Err(CoreError::invalid(
152                "sampling plan",
153                "temperature/top-k must be non-negative and probabilities must be in 0..=1",
154            ));
155        }
156        if let Some(repetition) = &self.repetition {
157            for (field, value) in [
158                ("repeat penalty", repetition.repeat_penalty),
159                ("frequency penalty", repetition.frequency_penalty),
160                ("presence penalty", repetition.presence_penalty),
161            ] {
162                if !value.is_finite() {
163                    return Err(CoreError::NonFinite { field });
164                }
165            }
166            if repetition.last_n < -1 || repetition.repeat_penalty <= 0.0 {
167                return Err(CoreError::invalid(
168                    "repetition sampler",
169                    "last_n must be at least -1 and repeat_penalty must be positive",
170                ));
171            }
172        }
173        if let Some(dry) = &self.dry {
174            for (field, value) in [("DRY multiplier", dry.multiplier), ("DRY base", dry.base)] {
175                if !value.is_finite() {
176                    return Err(CoreError::NonFinite { field });
177                }
178            }
179            if dry.multiplier <= 0.0
180                || dry.base < 1.0
181                || dry.allowed_length < 0
182                || dry.penalty_last_n < -1
183                || dry.penalty_last_n == 0
184            {
185                return Err(CoreError::invalid(
186                    "dry sampler",
187                    "multiplier must be positive, base at least one, allowed length non-negative, and lookback -1 or positive",
188                ));
189            }
190            if dry.sequence_breakers.len() > MAX_DRY_SEQUENCE_BREAKERS
191                || dry.sequence_breakers.iter().any(|value| {
192                    value.is_empty()
193                        || value.len() > MAX_DRY_SEQUENCE_BREAKER_BYTES
194                        || value.contains('\0')
195                })
196            {
197                return Err(CoreError::invalid(
198                    "dry sequence breakers",
199                    format!(
200                        "requires at most {MAX_DRY_SEQUENCE_BREAKERS} non-empty, NUL-free values of at most {MAX_DRY_SEQUENCE_BREAKER_BYTES} bytes"
201                    ),
202                ));
203            }
204        }
205        if let Some(mirostat) = &self.mirostat {
206            for (field, value) in [
207                ("Mirostat tau", mirostat.tau),
208                ("Mirostat eta", mirostat.eta),
209            ] {
210                if !value.is_finite() {
211                    return Err(CoreError::NonFinite { field });
212                }
213            }
214            let invalid_window = match mirostat.version {
215                MirostatVersion::V1 => mirostat.m <= 0,
216                MirostatVersion::V2 => mirostat.m != 0,
217            };
218            if self.temperature <= 0.0
219                || mirostat.tau <= 0.0
220                || mirostat.eta <= 0.0
221                || invalid_window
222            {
223                return Err(CoreError::invalid(
224                    "mirostat sampler",
225                    "temperature, tau, and eta must be positive; m must be positive for v1 and zero for v2",
226                ));
227            }
228        }
229        Ok(())
230    }
231}
232
233/// One additive token-logit bias.
234#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
235pub struct LogitBias {
236    /// Target token.
237    pub token: TokenId,
238    /// Finite additive bias.
239    pub bias: f32,
240}
241
242/// An eager llama.cpp GBNF grammar.
243#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
244pub struct Grammar {
245    /// Complete GBNF source.
246    pub source: String,
247    /// Root rule name.
248    pub root: String,
249}
250
251impl Grammar {
252    /// Validates bounded strings before they cross a native C-string boundary.
253    ///
254    /// # Errors
255    ///
256    /// Returns an error for empty, oversized, or NUL-containing values.
257    pub fn validate(&self) -> Result<(), CoreError> {
258        if self.source.is_empty() || self.root.is_empty() {
259            return Err(CoreError::Empty {
260                field: "grammar source/root",
261            });
262        }
263        if self.source.len() > MAX_GRAMMAR_SOURCE_BYTES || self.root.len() > MAX_GRAMMAR_ROOT_BYTES
264        {
265            return Err(CoreError::invalid(
266                "grammar",
267                format!(
268                    "source must be at most {MAX_GRAMMAR_SOURCE_BYTES} bytes and root at most {MAX_GRAMMAR_ROOT_BYTES} bytes"
269                ),
270            ));
271        }
272        if self.source.contains('\0') || self.root.contains('\0') {
273            return Err(CoreError::invalid(
274                "grammar",
275                "source and root must not contain NUL bytes",
276            ));
277        }
278        Ok(())
279    }
280}
281
282/// Activation policy for a version-two native grammar.
283#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
284#[serde(rename_all = "snake_case", tag = "kind")]
285pub enum GrammarActivationV2 {
286    /// Constrain the first generated token and every subsequent token.
287    Eager,
288    /// Begin unconstrained and activate when an exact pattern or token
289    /// trigger is observed by the native sampler.
290    Lazy {
291        /// Ordered native regular-expression trigger patterns.
292        trigger_patterns: Vec<String>,
293        /// Ordered exact tokenizer IDs that activate the grammar.
294        trigger_tokens: Vec<TokenId>,
295    },
296}
297
298/// Version-two eager or lazy llama.cpp GBNF grammar.
299///
300/// This is a separate serialized contract from [`Grammar`]. Existing
301/// generation-plan-v1 identities therefore remain unchanged.
302#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
303#[serde(deny_unknown_fields)]
304pub struct GrammarPlanV2 {
305    /// Complete bounded GBNF grammar and root rule.
306    pub grammar: Grammar,
307    /// Eager or trigger-driven activation.
308    pub activation: GrammarActivationV2,
309}
310
311impl GrammarPlanV2 {
312    /// Validates grammar and lazy-trigger bounds.
313    ///
314    /// # Errors
315    ///
316    /// Returns an error for malformed grammar text, empty lazy activation,
317    /// duplicate triggers, excessive trigger counts, or excessive pattern
318    /// bytes.
319    pub fn validate(&self) -> Result<(), CoreError> {
320        self.grammar.validate()?;
321        let GrammarActivationV2::Lazy {
322            trigger_patterns,
323            trigger_tokens,
324        } = &self.activation
325        else {
326            return Ok(());
327        };
328        if trigger_patterns.is_empty() && trigger_tokens.is_empty() {
329            return Err(CoreError::invalid(
330                "lazy grammar",
331                "at least one trigger pattern or token is required",
332            ));
333        }
334        let trigger_bytes = trigger_patterns
335            .iter()
336            .try_fold(0_usize, |total, pattern| {
337                total
338                    .checked_add(pattern.len())
339                    .ok_or_else(|| CoreError::invalid("lazy grammar", "trigger bytes overflowed"))
340            })?;
341        if trigger_patterns.len() > MAX_LAZY_GRAMMAR_TRIGGER_PATTERNS
342            || trigger_patterns.iter().any(|pattern| {
343                pattern.is_empty()
344                    || pattern.len() > MAX_LAZY_GRAMMAR_TRIGGER_PATTERN_BYTES
345                    || pattern.contains('\0')
346            })
347            || trigger_bytes > MAX_LAZY_GRAMMAR_TRIGGER_BYTES
348            || trigger_tokens.len() > MAX_LAZY_GRAMMAR_TRIGGER_TOKENS
349        {
350            return Err(CoreError::invalid(
351                "lazy grammar",
352                format!(
353                    "requires at most {MAX_LAZY_GRAMMAR_TRIGGER_PATTERNS} non-empty NUL-free patterns of at most {MAX_LAZY_GRAMMAR_TRIGGER_PATTERN_BYTES} bytes, {MAX_LAZY_GRAMMAR_TRIGGER_BYTES} aggregate pattern bytes, and {MAX_LAZY_GRAMMAR_TRIGGER_TOKENS} trigger tokens"
354                ),
355            ));
356        }
357        if trigger_patterns.iter().collect::<HashSet<_>>().len() != trigger_patterns.len()
358            || trigger_tokens.iter().collect::<HashSet<_>>().len() != trigger_tokens.len()
359        {
360            return Err(CoreError::invalid(
361                "lazy grammar",
362                "trigger patterns and token identifiers must each be unique",
363            ));
364        }
365        Ok(())
366    }
367
368    /// Returns the exact grammar identity.
369    ///
370    /// # Errors
371    ///
372    /// Returns a validation or serialization error.
373    pub fn digest(&self) -> Result<Digest, CoreError> {
374        self.validate()?;
375        Digest::of_serializable("grammar-plan-v2", self)
376    }
377}
378
379/// Complete bounded generation mechanics independent of prompt semantics.
380#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
381pub struct GenerationPlan {
382    /// Native sampler configuration.
383    pub sampling: SamplingPlan,
384    /// Maximum causally admitted output tokens.
385    pub max_tokens: u32,
386    /// Additive biases with unique token IDs, applied after Rust transforms and
387    /// before filters.
388    #[serde(default)]
389    pub biases: Vec<LogitBias>,
390    /// Optional eager grammar.
391    #[serde(default, skip_serializing_if = "Option::is_none")]
392    pub grammar: Option<Grammar>,
393    /// Exact byte suffixes that stop after causal admission.
394    ///
395    /// Matching bytes remain in output and causal state. When several suffixes
396    /// match the same output, the lowest declared index wins.
397    #[serde(default)]
398    pub stops: Vec<Vec<u8>>,
399}
400
401impl GenerationPlan {
402    /// Validates all bounded generation mechanics.
403    ///
404    /// # Errors
405    ///
406    /// Returns an error for invalid samplers, bounds, biases, grammar, or stops.
407    pub fn validate(&self) -> Result<(), CoreError> {
408        self.sampling.validate()?;
409        if self.max_tokens == 0 {
410            return Err(CoreError::invalid(
411                "maximum tokens",
412                "must be greater than zero",
413            ));
414        }
415        if self.biases.len() > MAX_LOGIT_BIASES
416            || self.biases.iter().any(|bias| !bias.bias.is_finite())
417        {
418            return Err(CoreError::invalid(
419                "logit biases",
420                "too many biases or a non-finite value",
421            ));
422        }
423        if self
424            .biases
425            .iter()
426            .map(|bias| bias.token)
427            .collect::<HashSet<_>>()
428            .len()
429            != self.biases.len()
430        {
431            return Err(CoreError::invalid(
432                "logit biases",
433                "token identifiers must be unique",
434            ));
435        }
436        if let Some(grammar) = &self.grammar {
437            grammar.validate()?;
438        }
439        if self.stops.len() > MAX_STOP_SEQUENCES
440            || self
441                .stops
442                .iter()
443                .any(|stop| stop.is_empty() || stop.len() > MAX_STOP_SEQUENCE_BYTES)
444        {
445            return Err(CoreError::invalid(
446                "stop sequences",
447                format!(
448                    "requires at most {MAX_STOP_SEQUENCES} non-empty sequences of at most {MAX_STOP_SEQUENCE_BYTES} bytes"
449                ),
450            ));
451        }
452        Ok(())
453    }
454
455    /// Returns a content identity for exact generation mechanics.
456    ///
457    /// # Errors
458    ///
459    /// Returns a validation or serialization error.
460    pub fn digest(&self) -> Result<Digest, CoreError> {
461        self.validate()?;
462        Digest::of_serializable("generation-plan-v1", self)
463    }
464}
465
466/// Version-two generation mechanics with explicit eager or lazy grammar.
467///
468/// The embedded v1 generation plan must not select its eager grammar. This
469/// prevents the new contract from reinterpreting `generation-plan-v1` while
470/// reusing all unchanged sampler, bias, stop, and bound mechanics.
471#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
472#[serde(deny_unknown_fields)]
473pub struct GenerationPlanV2 {
474    /// Complete unchanged v1 sampler, bias, stop, and token-bound mechanics.
475    pub generation: GenerationPlan,
476    /// Optional version-two eager or lazy grammar.
477    #[serde(default, skip_serializing_if = "Option::is_none")]
478    pub grammar: Option<GrammarPlanV2>,
479}
480
481impl GenerationPlanV2 {
482    /// Validates the complete version-two generation plan.
483    ///
484    /// # Errors
485    ///
486    /// Returns an error for invalid v1 mechanics, simultaneous v1 and v2
487    /// grammar selection, or invalid version-two grammar triggers.
488    pub fn validate(&self) -> Result<(), CoreError> {
489        self.generation.validate()?;
490        if self.generation.grammar.is_some() {
491            return Err(CoreError::invalid(
492                "generation plan v2",
493                "the embedded generation-plan-v1 grammar must be absent",
494            ));
495        }
496        if let Some(grammar) = &self.grammar {
497            grammar.validate()?;
498        }
499        Ok(())
500    }
501
502    /// Returns a content identity for exact version-two mechanics.
503    ///
504    /// # Errors
505    ///
506    /// Returns a validation or serialization error.
507    pub fn digest(&self) -> Result<Digest, CoreError> {
508        self.validate()?;
509        Digest::of_serializable("generation-plan-v2", self)
510    }
511}
512
513#[cfg(test)]
514mod tests {
515    use super::*;
516
517    fn plan() -> GenerationPlan {
518        GenerationPlan {
519            sampling: SamplingPlan::default(),
520            max_tokens: 8,
521            biases: Vec::new(),
522            grammar: None,
523            stops: vec![b"stop".to_vec()],
524        }
525    }
526
527    #[test]
528    fn generation_plan_roundtrip_preserves_identity() {
529        let original = plan();
530        let encoded = serde_json::to_vec(&original).unwrap();
531        let decoded: GenerationPlan = serde_json::from_slice(&encoded).unwrap();
532        assert_eq!(original.digest().unwrap(), decoded.digest().unwrap());
533    }
534
535    #[test]
536    fn generation_plan_rejects_unbounded_or_nonfinite_mechanics() {
537        let mut invalid = plan();
538        invalid.max_tokens = 0;
539        assert!(invalid.validate().is_err());
540        invalid.max_tokens = 1;
541        invalid.sampling.temperature = f32::NAN;
542        assert!(invalid.validate().is_err());
543        invalid.sampling.temperature = 0.0;
544        invalid.stops = vec![Vec::new()];
545        assert!(invalid.validate().is_err());
546    }
547
548    #[test]
549    fn stateful_sampler_bounds_are_explicit() {
550        let mut sampling = SamplingPlan {
551            temperature: 0.8,
552            dry: Some(DrySampler {
553                multiplier: 0.8,
554                base: 1.75,
555                allowed_length: 2,
556                penalty_last_n: -1,
557                sequence_breakers: vec!["\n".to_owned()],
558            }),
559            mirostat: Some(MirostatSampler {
560                version: MirostatVersion::V2,
561                tau: 5.0,
562                eta: 0.1,
563                m: 0,
564            }),
565            ..SamplingPlan::default()
566        };
567        assert!(sampling.validate().is_ok());
568
569        sampling.dry.as_mut().unwrap().sequence_breakers = vec![String::new()];
570        assert!(sampling.validate().is_err());
571        sampling.dry.as_mut().unwrap().sequence_breakers = vec!["\n".to_owned()];
572        sampling.dry.as_mut().unwrap().base = f32::NAN;
573        assert!(matches!(
574            sampling.validate(),
575            Err(CoreError::NonFinite { field: "DRY base" })
576        ));
577
578        sampling.dry = None;
579        sampling.temperature = 0.0;
580        assert!(sampling.validate().is_err());
581
582        sampling.temperature = 0.8;
583        sampling.mirostat.as_mut().unwrap().m = 1;
584        assert!(sampling.validate().is_err());
585        sampling.mirostat = Some(MirostatSampler {
586            version: MirostatVersion::V1,
587            tau: 5.0,
588            eta: 0.1,
589            m: 0,
590        });
591        assert!(sampling.validate().is_err());
592    }
593
594    #[test]
595    fn grammar_and_stop_inputs_have_explicit_byte_bounds() {
596        let oversized_source = Grammar {
597            source: "x".repeat(MAX_GRAMMAR_SOURCE_BYTES + 1),
598            root: "root".to_owned(),
599        };
600        assert!(oversized_source.validate().is_err());
601
602        let oversized_root = Grammar {
603            source: "root ::= \"x\"".to_owned(),
604            root: "x".repeat(MAX_GRAMMAR_ROOT_BYTES + 1),
605        };
606        assert!(oversized_root.validate().is_err());
607
608        let mut invalid = plan();
609        invalid.stops = vec![vec![b'x'; MAX_STOP_SEQUENCE_BYTES + 1]];
610        assert!(invalid.validate().is_err());
611        invalid.stops = vec![vec![b'x']; MAX_STOP_SEQUENCES + 1];
612        assert!(invalid.validate().is_err());
613    }
614
615    #[test]
616    fn every_logit_bias_must_be_finite() {
617        let mut invalid = plan();
618        invalid.biases.push(LogitBias {
619            token: TokenId::new(7).unwrap(),
620            bias: f32::INFINITY,
621        });
622        assert!(invalid.validate().is_err());
623    }
624
625    #[test]
626    fn generation_logit_bias_tokens_must_be_unique() {
627        let mut invalid = plan();
628        invalid.biases = vec![
629            LogitBias {
630                token: TokenId::new(7).unwrap(),
631                bias: 1.0,
632            },
633            LogitBias {
634                token: TokenId::new(7).unwrap(),
635                bias: 2.0,
636            },
637        ];
638        assert!(invalid.validate().is_err());
639    }
640
641    #[test]
642    fn version_two_generation_preserves_v1_and_binds_lazy_triggers() {
643        let grammar = GrammarPlanV2 {
644            grammar: Grammar {
645                source: "root ::= \"{}\"".to_owned(),
646                root: "root".to_owned(),
647            },
648            activation: GrammarActivationV2::Lazy {
649                trigger_patterns: vec!["<tool>.*".to_owned()],
650                trigger_tokens: vec![TokenId::new(7).unwrap()],
651            },
652        };
653        let version_two = GenerationPlanV2 {
654            generation: plan(),
655            grammar: Some(grammar.clone()),
656        };
657        assert!(version_two.validate().is_ok());
658        assert_ne!(
659            version_two.digest().unwrap(),
660            version_two.generation.digest().unwrap()
661        );
662
663        let mut duplicate = version_two.clone();
664        duplicate.grammar.as_mut().unwrap().activation = GrammarActivationV2::Lazy {
665            trigger_patterns: vec!["x".to_owned(), "x".to_owned()],
666            trigger_tokens: Vec::new(),
667        };
668        assert!(duplicate.validate().is_err());
669
670        let mut ambiguous = version_two;
671        ambiguous.generation.grammar = Some(grammar.grammar);
672        assert!(ambiguous.validate().is_err());
673    }
674}