marque-rules 0.2.1

Rule trait definitions for marque — the contract every rule crate implements
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
// SPDX-FileCopyrightText: 2026 Knitli Inc.
//
// SPDX-License-Identifier: LicenseRef-MarqueLicense-1.0

//! Confidence — Phase D audit-provenance payload.
//!
//! Every [`FixProposal`](crate::FixProposal) carries a `Confidence`
//! record describing how the engine arrived at the proposal. The
//! record stores two primary scalar confidence axes —
//! `recognition` and `rule` — plus optional auxiliary fields
//! (`region` and `runner_up_ratio`) and a list of named feature
//! contributions. Together they reconstruct the decoder's scoring
//! path so an auditor can verify *why* a given fix was promoted.
//!
//! The engine's current threshold-facing combined score is
//! `recognition * rule` as exposed by [`Confidence::combined`].
//! `region` is recorded as additional audit/context metadata when
//! available, but it does not currently participate in that
//! combined score. `runner_up_ratio` likewise provides decoder
//! provenance rather than a direct multiplicative/additive input to
//! `combined()`.
//!
//! ## Precision: `f32` throughout
//!
//! All scores are `f32`. The decoder scores in `f64` internally
//! (log-priors and posteriors accumulate across many features), but
//! the emitted `Confidence` downcasts once at the boundary so the
//! audit record stays compact and byte-stable. This matches the
//! foundational-plan invariant line 739-757.
//!
//! ## `features` is closed
//!
//! [`FeatureId`] is a non-`#[non_exhaustive]` closed enum. A new
//! feature means a new variant and a coordinated bump of the audit
//! schema version (`MARQUE_AUDIT_SCHEMA`) — silent additions would
//! break the auditability contract on already-emitted records.

/// Multi-axis confidence attached to every [`FixProposal`](crate::FixProposal).
///
/// Fields:
///
/// - `recognition` — posterior from the [`Recognizer`](marque_scheme::Recognizer)
///   that surfaced this candidate (0.0–1.0).
/// - `rule` — confidence the emitting rule has in its own fix
///   (0.0–1.0). Strict-path rules report 1.0 when the invariant is
///   unambiguous.
/// - `region` — optional region-level confidence (a page-context
///   prior, for example).
/// - `runner_up_ratio` — optional ratio of top candidate to runner-up
///   posterior. Decoder-sourced fixes carry this; strict-path fixes
///   leave it `None` because the strict grammar has no runner-up by
///   construction.
/// - `features` — the concrete evidence features that contributed to
///   `recognition`. Used by the corpus-accuracy harness to break down
///   where posterior mass came from.
///
/// Construction happens via [`Confidence::strict`] (for rules that
/// bypass the decoder) or the decoder's scoring path (Phase 4 / task
/// T061).
#[derive(Debug, Clone, PartialEq)]
pub struct Confidence {
    /// Recognizer posterior in `[0.0, 1.0]`.
    pub recognition: f32,
    /// Rule-level confidence in `[0.0, 1.0]`.
    pub rule: f32,
    /// Region / page-context confidence, when a rule computes one.
    pub region: Option<f32>,
    /// Posterior ratio between top candidate and runner-up
    /// (`None` for strict-path fixes; set by decoder-sourced fixes).
    pub runner_up_ratio: Option<f32>,
    /// Per-feature contributions to `recognition`.
    pub features: Vec<FeatureContribution>,
}

impl Confidence {
    /// Confidence record for a strict-path fix where recognition was
    /// unambiguous.
    ///
    /// `rule_confidence` is the rule's own confidence in its proposed
    /// fix (typically 1.0 for migrations, lower for heuristics). The
    /// recognition axis is pinned at 1.0 because the strict grammar
    /// has one unambiguous match by definition, and no feature
    /// contributions are recorded — strict-path fixes do not traverse
    /// the decoder's feature graph.
    #[inline]
    pub fn strict(rule_confidence: f32) -> Self {
        assert!(
            (0.0..=1.0).contains(&rule_confidence) && !rule_confidence.is_nan(),
            "Confidence::strict rule confidence must be in [0.0, 1.0] and not NaN, got {rule_confidence}"
        );
        Self {
            recognition: 1.0,
            rule: rule_confidence,
            region: None,
            runner_up_ratio: None,
            features: Vec::new(),
        }
    }

    /// Product of `recognition` and `rule`. The engine's
    /// confidence-threshold gate compares this combined score against
    /// the configured threshold (FR-016).
    #[inline]
    pub fn combined(&self) -> f32 {
        self.recognition * self.rule
    }

    /// Validate every axis of this `Confidence` record.
    ///
    /// Returns `Err(message)` naming the first invalid axis. Checks:
    ///
    /// - `recognition` and `rule` in `[0.0, 1.0]` and not NaN.
    /// - `region`, when `Some`, in `[0.0, 1.0]` and not NaN.
    /// - `runner_up_ratio`, when `Some`, finite and not NaN. No range
    ///   constraint — a well-behaved decoder returns `≥ 1.0` (top /
    ///   runner-up) but infinity (runner-up posterior = 0) and values
    ///   `< 1.0` are legal for debugging / inspection code.
    /// - Every `features[i].delta` finite and not NaN. `delta` carries
    ///   signed log-posterior contributions so any finite value is
    ///   legal; `NaN` / infinity would poison downstream audit-sum
    ///   invariants silently.
    ///
    /// The zero-axis edge case (recognition = 0 or rule = 0) is valid
    /// — `combined() = 0.0` is a legitimate below-threshold result,
    /// not an invariant violation.
    pub fn validate(&self) -> Result<(), String> {
        let check_unit = |label: &str, v: f32| -> Result<(), String> {
            if v.is_nan() || !(0.0..=1.0).contains(&v) {
                Err(format!(
                    "Confidence.{label} must be in [0.0, 1.0] and not NaN, got {v}"
                ))
            } else {
                Ok(())
            }
        };
        let check_finite = |label: &str, v: f32| -> Result<(), String> {
            if v.is_nan() || !v.is_finite() {
                Err(format!(
                    "Confidence.{label} must be finite and not NaN, got {v}"
                ))
            } else {
                Ok(())
            }
        };

        check_unit("recognition", self.recognition)?;
        check_unit("rule", self.rule)?;
        if let Some(r) = self.region {
            check_unit("region", r)?;
        }
        if let Some(r) = self.runner_up_ratio {
            check_finite("runner_up_ratio", r)?;
        }
        for (i, feature) in self.features.iter().enumerate() {
            if feature.delta.is_nan() || !feature.delta.is_finite() {
                return Err(format!(
                    "Confidence.features[{i}].delta must be finite and not NaN, got {}",
                    feature.delta
                ));
            }
        }
        Ok(())
    }
}

/// One named contribution to [`Confidence::recognition`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FeatureContribution {
    /// Which feature.
    pub id: FeatureId,
    /// Signed delta added to the log-posterior by this feature.
    pub delta: f32,
}

/// Closed enumeration of features the decoder can record.
///
/// New variants MUST bump the audit schema version (see
/// `MARQUE_AUDIT_SCHEMA` in `crates/engine/build.rs`). Treat this
/// enum as part of the on-the-wire audit contract.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FeatureId {
    /// Observed form is edit-distance 1 from a canonical token.
    EditDistance1,
    /// Observed form is edit-distance 2 from a canonical token.
    EditDistance2,
    /// Observed form is a token-order permutation of a canonical
    /// banner/portion shape.
    TokenReorder,
    /// Observed form is a known CAPCO-2016-superseded token whose
    /// replacement is unambiguous (e.g., `COMINT → SI`).
    SupersededToken,
    /// The candidate's base rate in the target corpus dominates the
    /// posterior (common-marking prior).
    BaseRateCommonMarking,
    /// Strict-context classification floor (FR-011) applied — e.g.,
    /// banner at TOP SECRET forces a strict posterior for
    /// classification tokens at ≥ that level on the same page.
    StrictContextClassification,
    /// Corpus-override data (opt-in, non-WASM, non-server) shifted
    /// the posterior. Recorded so an auditor can identify fixes
    /// produced under organizational overrides vs. stock priors.
    CorpusOverrideInEffect,
}

impl FeatureId {
    /// Canonical on-the-wire string label for this feature.
    ///
    /// This is the **single source of truth** for `FeatureId →
    /// audit-record-string` projection. Audit emitters (CLI, WASM,
    /// server) and snapshot tests MUST call this method rather than
    /// re-implementing the match. A new `FeatureId` variant added
    /// without a matching `as_str` arm fails the exhaustiveness check
    /// here at compile time, so the on-the-wire contract cannot drift
    /// silently across emitters.
    ///
    /// Returns a `&'static str` so callers can embed the value in
    /// zero-copy serialization paths (`Serialize` derives,
    /// `serde_json::json!` etc.) without an allocation.
    #[inline]
    pub const fn as_str(self) -> &'static str {
        match self {
            FeatureId::EditDistance1 => "EditDistance1",
            FeatureId::EditDistance2 => "EditDistance2",
            FeatureId::TokenReorder => "TokenReorder",
            FeatureId::SupersededToken => "SupersededToken",
            FeatureId::BaseRateCommonMarking => "BaseRateCommonMarking",
            FeatureId::StrictContextClassification => "StrictContextClassification",
            FeatureId::CorpusOverrideInEffect => "CorpusOverrideInEffect",
        }
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;

    #[test]
    fn strict_pins_recognition_at_one() {
        let c = Confidence::strict(0.85);
        assert_eq!(c.recognition, 1.0);
        assert_eq!(c.rule, 0.85);
        assert!(c.region.is_none());
        assert!(c.runner_up_ratio.is_none());
        assert!(c.features.is_empty());
    }

    #[test]
    fn combined_is_product_of_axes() {
        let c = Confidence::strict(0.9);
        assert!((c.combined() - 0.9).abs() < 1e-6);

        let c2 = Confidence {
            recognition: 0.8,
            rule: 0.5,
            region: None,
            runner_up_ratio: None,
            features: Vec::new(),
        };
        assert!((c2.combined() - 0.4).abs() < 1e-6);
    }

    #[test]
    #[should_panic(expected = "Confidence::strict rule confidence")]
    fn strict_panics_on_nan() {
        let _ = Confidence::strict(f32::NAN);
    }

    #[test]
    #[should_panic(expected = "Confidence::strict rule confidence")]
    fn strict_panics_above_one() {
        let _ = Confidence::strict(1.01);
    }

    #[test]
    fn feature_id_as_str_matches_audit_contract() {
        // Pin the on-the-wire labels for `FeatureId`. These strings are
        // part of the audit-record contract (see
        // `contracts/audit-record-v2.md`); a future rename here MUST be
        // a deliberate audit-schema bump (`MARQUE_AUDIT_SCHEMA`), not an
        // accidental refactor. Kept as an explicit per-variant table
        // (rather than a round-trip) so a label drift is loud.
        let cases: &[(FeatureId, &str)] = &[
            (FeatureId::EditDistance1, "EditDistance1"),
            (FeatureId::EditDistance2, "EditDistance2"),
            (FeatureId::TokenReorder, "TokenReorder"),
            (FeatureId::SupersededToken, "SupersededToken"),
            (FeatureId::BaseRateCommonMarking, "BaseRateCommonMarking"),
            (
                FeatureId::StrictContextClassification,
                "StrictContextClassification",
            ),
            (FeatureId::CorpusOverrideInEffect, "CorpusOverrideInEffect"),
        ];
        for (id, expected) in cases {
            assert_eq!(id.as_str(), *expected, "label drift for {id:?}");
        }
    }

    #[test]
    fn feature_contribution_roundtrip() {
        let fc = FeatureContribution {
            id: FeatureId::EditDistance1,
            delta: -0.3,
        };
        assert_eq!(fc.id, FeatureId::EditDistance1);
        assert!((fc.delta - (-0.3)).abs() < 1e-6);
    }

    #[test]
    fn validate_accepts_well_formed_record() {
        assert!(Confidence::strict(0.85).validate().is_ok());
        assert!(
            Confidence {
                recognition: 0.9,
                rule: 0.8,
                region: Some(0.5),
                runner_up_ratio: Some(2.7),
                features: vec![FeatureContribution {
                    id: FeatureId::EditDistance1,
                    delta: -0.5,
                }],
            }
            .validate()
            .is_ok()
        );
    }

    #[test]
    fn validate_rejects_out_of_range_recognition() {
        let c = Confidence {
            recognition: 1.5,
            rule: 0.5,
            region: None,
            runner_up_ratio: None,
            features: Vec::new(),
        };
        let err = c.validate().unwrap_err();
        assert!(
            err.contains("recognition"),
            "error should name the offending axis, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_out_of_range_rule() {
        let c = Confidence {
            recognition: 0.5,
            rule: -0.1,
            region: None,
            runner_up_ratio: None,
            features: Vec::new(),
        };
        let err = c.validate().unwrap_err();
        assert!(err.contains("rule"), "got: {err}");
    }

    #[test]
    fn validate_rejects_out_of_range_region() {
        let c = Confidence {
            recognition: 0.5,
            rule: 0.5,
            region: Some(1.5),
            runner_up_ratio: None,
            features: Vec::new(),
        };
        let err = c.validate().unwrap_err();
        assert!(err.contains("region"), "got: {err}");
    }

    #[test]
    fn validate_rejects_non_finite_runner_up_ratio() {
        for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
            let c = Confidence {
                recognition: 0.5,
                rule: 0.5,
                region: None,
                runner_up_ratio: Some(bad),
                features: Vec::new(),
            };
            assert!(
                c.validate().is_err(),
                "runner_up_ratio = {bad:?} should fail validation"
            );
        }
    }

    #[test]
    fn validate_accepts_finite_runner_up_ratio_of_any_magnitude() {
        // No range constraint on the ratio — verify low values pass.
        let c = Confidence {
            recognition: 0.5,
            rule: 0.5,
            region: None,
            runner_up_ratio: Some(0.01),
            features: Vec::new(),
        };
        assert!(c.validate().is_ok());
    }

    #[test]
    fn validate_rejects_non_finite_feature_delta() {
        for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
            let c = Confidence {
                recognition: 0.5,
                rule: 0.5,
                region: None,
                runner_up_ratio: None,
                features: vec![FeatureContribution {
                    id: FeatureId::EditDistance1,
                    delta: bad,
                }],
            };
            assert!(
                c.validate().is_err(),
                "feature delta = {bad:?} should fail validation"
            );
        }
    }

    #[test]
    fn validate_accepts_zero_axes() {
        // Zero is a legal below-threshold value, not an invariant
        // violation — check that validate doesn't treat it specially.
        let c = Confidence {
            recognition: 0.0,
            rule: 0.0,
            region: Some(0.0),
            runner_up_ratio: None,
            features: Vec::new(),
        };
        assert!(c.validate().is_ok());
    }
}