gam 0.3.119

Generalized penalized likelihood engine
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
//! Fingerprint keying for the warm-start store.
//!
//! A [`Fingerprint`] is a SHA-256 hash; two fits whose (data, spec) byte
//! representations agree under [`Fingerprinter`] absorption produce the same
//! key. Adversarial collisions don't matter — per-variant warm-start
//! validators are the correctness fail-safe; the fingerprint is just a fast
//! filter.

use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};
use std::fmt;

/// 256-bit warm-start key.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Fingerprint([u8; 32]);

impl Serialize for Fingerprint {
    /// Serialize as the canonical 64-char lowercase hex string so on-disk
    /// payloads carrying a `Fingerprint` (e.g. the cross-fit `FitArtifact`
    /// term identities) are stable and human-readable.
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.to_hex())
    }
}

impl<'de> Deserialize<'de> for Fingerprint {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct HexVisitor;
        impl Visitor<'_> for HexVisitor {
            type Value = Fingerprint;
            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("a 64-character hex-encoded SHA-256 fingerprint")
            }
            fn visit_str<E: de::Error>(self, v: &str) -> Result<Fingerprint, E> {
                Fingerprint::from_hex(v).ok_or_else(|| de::Error::custom("invalid hex fingerprint"))
            }
        }
        deserializer.deserialize_str(HexVisitor)
    }
}

impl Fingerprint {
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    pub fn to_hex(&self) -> String {
        let mut s = String::with_capacity(64);
        for b in &self.0 {
            use std::fmt::Write;
            write!(&mut s, "{:02x}", b).expect("writing to String is infallible");
        }
        s
    }

    pub fn from_hex(s: &str) -> Option<Self> {
        if s.len() != 64 {
            return None;
        }
        let bytes = s.as_bytes();
        let mut out = [0u8; 32];
        for i in 0..32 {
            let hi = from_hex_nibble(bytes[2 * i])?;
            let lo = from_hex_nibble(bytes[2 * i + 1])?;
            out[i] = (hi << 4) | lo;
        }
        Some(Fingerprint(out))
    }
}

const fn from_hex_nibble(c: u8) -> Option<u8> {
    match c {
        b'0'..=b'9' => Some(c - b'0'),
        b'a'..=b'f' => Some(c - b'a' + 10),
        b'A'..=b'F' => Some(c - b'A' + 10),
        _ => None,
    }
}

impl fmt::Debug for Fingerprint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Fingerprint({})", self.to_hex())
    }
}

impl fmt::Display for Fingerprint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_hex())
    }
}

/// Streaming hasher for building a [`Fingerprint`].
///
/// Each `absorb_*` writes a per-type discriminator byte, the caller's content
/// tag, and a length before the data, so `absorb_f64(b"x", 0.5)` cannot
/// collide with `absorb_bytes(b"x", <the 8 little-endian bytes of 0.5>)`, nor
/// with `absorb_u64(b"x", 0.5f64.to_bits())` — heterogeneous fields sharing a
/// tag can never alias.
pub struct Fingerprinter {
    h: Sha256,
}

/// Per-type frame discriminators for the `absorb_*` family. Written before
/// the content tag so values of different primitive types absorbed under the
/// same tag with coinciding payload bytes still produce distinct digests.
mod type_code {
    pub const TAG: u8 = 0;
    pub const BYTES: u8 = 1;
    pub const STR: u8 = 2;
    pub const U64: u8 = 3;
    pub const F64: u8 = 4;
    pub const F64_SLICE: u8 = 5;
    pub const F64_2D: u8 = 6;
}

impl Fingerprinter {
    pub fn new() -> Self {
        Self { h: Sha256::new() }
    }

    /// Write one frame header: type discriminator, then length-prefixed tag.
    fn frame(&mut self, code: u8, tag: &[u8]) {
        self.h.update([code]);
        self.h.update((tag.len() as u32).to_le_bytes());
        self.h.update(tag);
    }

    /// Absorb a tag with no payload. Useful for structural separators.
    pub fn absorb_tag(&mut self, tag: &[u8]) {
        self.frame(type_code::TAG, tag);
    }

    pub fn absorb_bytes(&mut self, tag: &[u8], data: &[u8]) {
        self.frame(type_code::BYTES, tag);
        self.h.update((data.len() as u64).to_le_bytes());
        self.h.update(data);
    }

    pub fn absorb_str(&mut self, tag: &[u8], s: &str) {
        self.frame(type_code::STR, tag);
        self.h.update((s.len() as u64).to_le_bytes());
        self.h.update(s.as_bytes());
    }

    pub fn absorb_u64(&mut self, tag: &[u8], v: u64) {
        self.frame(type_code::U64, tag);
        self.h.update(v.to_le_bytes());
    }

    pub fn absorb_f64(&mut self, tag: &[u8], v: f64) {
        self.frame(type_code::F64, tag);
        self.h.update(v.to_bits().to_le_bytes());
    }

    pub fn absorb_f64_slice(&mut self, tag: &[u8], xs: &[f64]) {
        self.frame(type_code::F64_SLICE, tag);
        self.h.update((xs.len() as u64).to_le_bytes());
        absorb_f64_bytes(&mut self.h, xs);
    }

    pub fn absorb_f64_2d(&mut self, tag: &[u8], rows: usize, cols: usize, xs: &[f64]) {
        self.frame(type_code::F64_2D, tag);
        self.h.update((rows as u64).to_le_bytes());
        self.h.update((cols as u64).to_le_bytes());
        absorb_f64_bytes(&mut self.h, xs);
    }

    pub fn finalize(self) -> Fingerprint {
        let out = self.h.finalize();
        let mut bytes = [0u8; 32];
        bytes.copy_from_slice(&out);
        Fingerprint(bytes)
    }

    // ------------------------------------------------------------------
    // Untagged write_* API — drop-in replacement for the formerly-separate
    // `StableHasher` (warm-start) and `CacheDigestBuilder` (latent_cache)
    // hashers. Callers that use this API are responsible for their own
    // type-disambiguation (typically by writing a leading namespace string
    // via `write_str`); the `absorb_*` family above prepends per-call tags
    // and is the safer choice for new code. Callers that use the untagged API
    // must preserve their own hand-framed input protocol.
    // ------------------------------------------------------------------

    pub fn write_bytes(&mut self, data: &[u8]) {
        self.h.update(data);
    }

    pub fn write_u8(&mut self, value: u8) {
        self.h.update([value]);
    }

    pub fn write_bool(&mut self, value: bool) {
        self.h.update([u8::from(value)]);
    }

    pub fn write_u64(&mut self, value: u64) {
        self.h.update(value.to_le_bytes());
    }

    pub fn write_usize(&mut self, value: usize) {
        self.h.update((value as u64).to_le_bytes());
    }

    pub fn write_f64(&mut self, value: f64) {
        // Normalize -0.0 to +0.0 so signed-zero comparison ambiguity does
        // not split warm-start key buckets — matches the prior `StableHasher`
        // contract that warm-start keys depended on.
        let normalized = if value == 0.0 { 0.0 } else { value };
        self.h.update(normalized.to_bits().to_le_bytes());
    }

    pub fn write_str(&mut self, value: &str) {
        self.write_usize(value.len());
        self.h.update(value.as_bytes());
    }

    /// Absorb a length-prefixed `f64` slice using the per-element
    /// [`Fingerprinter::write_f64`] contract (so `-0.0` is normalized to
    /// `+0.0`). Canonical home for the byte-identical `len`-then-each-`f64`
    /// hashing that previously lived as module-local `hash_f64_slice` /
    /// `hash_vector` copies in `solver/latent_cache`. Uses a bulk byte path
    /// only when it can emit exactly the same bytes as the element-wise
    /// normalizing protocol.
    pub fn write_f64_slice(&mut self, values: &[f64]) {
        self.write_usize(values.len());
        self.write_f64_slice_payload(values);
    }

    fn write_f64_slice_payload(&mut self, values: &[f64]) {
        #[cfg(target_endian = "little")]
        {
            let needs_normalization = values
                .iter()
                .any(|&value| value.is_nan() || (value == 0.0 && value.is_sign_negative()));
            if !needs_normalization {
                // SAFETY: values.as_ptr() is valid for values.len() contiguous
                // f64s, f64 has no padding, and reborrowing as bytes is confined
                // to this update call. Little-endian storage matches write_f64's
                // to_bits().to_le_bytes() byte stream for non-normalized values.
                let bytes = unsafe {
                    std::slice::from_raw_parts(
                        values.as_ptr() as *const u8,
                        std::mem::size_of_val(values),
                    )
                };
                self.h.update(bytes);
                return;
            }
        }
        self.write_f64_slice_payload_slow(values);
    }

    fn write_f64_slice_payload_slow(&mut self, values: &[f64]) {
        for &value in values {
            self.write_f64(value);
        }
    }

    /// Absorb a 1D `f64` array as `len` followed by every element via
    /// [`Fingerprinter::write_f64`]. Canonical home for the byte-identical
    /// `hash_vector` copy that previously lived in `solver/latent_cache`.
    pub fn write_f64_array1(&mut self, values: &ndarray::Array1<f64>) {
        self.write_usize(values.len());
        if let Some(slice) = values.as_slice() {
            self.write_f64_slice_payload(slice);
        } else {
            self.write_f64_slice_payload_slow_iter(values.iter().copied());
        }
    }

    /// Absorb a 2D `f64` array as `(nrows, ncols)` followed by every element in
    /// iteration order, each via [`Fingerprinter::write_f64`]. Canonical home
    /// for the byte-identical heuristic that previously lived as module-local
    /// `write_array2_fingerprint` (`solver/arrow_schur`) and `hash_matrix`
    /// (`solver/latent_cache`) copies.
    pub fn write_f64_array2(&mut self, values: &ndarray::Array2<f64>) {
        self.write_usize(values.nrows());
        self.write_usize(values.ncols());
        if let Some(slice) = values.as_slice() {
            self.write_f64_slice_payload(slice);
        } else {
            self.write_f64_slice_payload_slow_iter(values.iter().copied());
        }
    }

    fn write_f64_slice_payload_slow_iter<I>(&mut self, values: I)
    where
        I: IntoIterator<Item = f64>,
    {
        for value in values {
            self.write_f64(value);
        }
    }

    /// Finalize and return the first 8 bytes of the SHA-256 digest as a
    /// little-endian `u64`. Used by callers that need a compact in-process
    /// identifier (manifold mode fingerprints, registry fingerprints, …)
    /// rather than the full 32-byte [`Fingerprint`].
    pub fn finish_u64(self) -> u64 {
        let out = self.h.finalize();
        let mut bytes = [0u8; 8];
        bytes.copy_from_slice(&out[..8]);
        u64::from_le_bytes(bytes)
    }

    /// Finalize and return a zero-padded 16-character hex representation
    /// of [`Fingerprinter::finish_u64`], suitable for embedding directly
    /// in cache-key strings.
    pub fn finish_hex(self) -> String {
        format!("{:016x}", self.finish_u64())
    }
}

/// Feed `xs` to the hasher in one bulk `update` instead of one 8-byte
/// `to_le_bytes` call per element. On little-endian hosts we reinterpret the
/// `&[f64]` storage directly as `&[u8]`; on big-endian hosts we fall back to
/// a per-element loop so the fingerprint stays endian-stable across machines.
#[inline]
fn absorb_f64_bytes(h: &mut Sha256, xs: &[f64]) {
    #[cfg(target_endian = "little")]
    {
        // SAFETY: xs.as_ptr() is non-null/aligned (slice invariant); f64
        // has no padding and any bit pattern is a valid u8; size_of_val
        // covers exactly xs's bytes and the borrow lives within this call.
        let bytes = unsafe {
            std::slice::from_raw_parts(xs.as_ptr() as *const u8, std::mem::size_of_val(xs))
        };
        h.update(bytes);
    }
    #[cfg(not(target_endian = "little"))]
    {
        for &x in xs {
            h.update(x.to_bits().to_le_bytes());
        }
    }
}

impl Default for Fingerprinter {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn hex_roundtrips() {
        let mut fp = Fingerprinter::new();
        fp.absorb_str(b"family", "standard");
        fp.absorb_f64_slice(b"y", &[1.0, 2.0, 3.0]);
        let key = fp.finalize();
        let hex = key.to_hex();
        assert_eq!(hex.len(), 64);
        let parsed = Fingerprint::from_hex(&hex).unwrap();
        assert_eq!(key, parsed);
    }

    #[test]
    fn tagged_absorptions_dont_collide() {
        let mut a = Fingerprinter::new();
        a.absorb_f64(b"x", 0.5);
        let ka = a.finalize();

        let mut b = Fingerprinter::new();
        // Same bytes, different tag: must produce a different key.
        b.absorb_bytes(b"y", &0.5f64.to_bits().to_le_bytes());
        let kb = b.finalize();

        assert_ne!(ka, kb);
    }

    #[test]
    fn same_tag_cross_type_absorptions_dont_collide() {
        // The documented contract: heterogeneous fields sharing a tag whose
        // payload bytes coincide must still produce distinct fingerprints.
        let v = 0.5f64;

        let mut f = Fingerprinter::new();
        f.absorb_f64(b"t", v);
        let kf = f.finalize();

        let mut u = Fingerprinter::new();
        u.absorb_u64(b"t", v.to_bits());
        let ku = u.finalize();

        let mut raw = Fingerprinter::new();
        raw.absorb_bytes(b"t", &v.to_bits().to_le_bytes());
        let kraw = raw.finalize();

        assert_ne!(kf, ku, "f64/u64 type confusion under a shared tag");
        assert_ne!(kf, kraw, "f64/bytes type confusion under a shared tag");
        assert_ne!(ku, kraw, "u64/bytes type confusion under a shared tag");

        let mut s = Fingerprinter::new();
        s.absorb_str(b"k", "AB");
        let ks = s.finalize();
        let mut sb = Fingerprinter::new();
        sb.absorb_bytes(b"k", b"AB");
        let ksb = sb.finalize();
        assert_ne!(ks, ksb, "str/bytes type confusion under a shared tag");

        // A bare structural tag must not alias an empty-payload absorption.
        let mut t = Fingerprinter::new();
        t.absorb_tag(b"sep");
        let kt = t.finalize();
        let mut e = Fingerprinter::new();
        e.absorb_bytes(b"sep", b"");
        let ke = e.finalize();
        assert_ne!(kt, ke, "tag/empty-bytes confusion under a shared tag");
    }

    #[test]
    fn different_data_yields_different_keys() {
        let mut a = Fingerprinter::new();
        a.absorb_f64_slice(b"y", &[1.0, 2.0]);
        let mut b = Fingerprinter::new();
        b.absorb_f64_slice(b"y", &[1.0, 3.0]);
        assert_ne!(a.finalize(), b.finalize());
    }

    #[test]
    fn same_input_yields_same_key() {
        let mut a = Fingerprinter::new();
        a.absorb_str(b"f", "binomial");
        a.absorb_f64_2d(b"x", 2, 3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let mut b = Fingerprinter::new();
        b.absorb_str(b"f", "binomial");
        b.absorb_f64_2d(b"x", 2, 3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        assert_eq!(a.finalize(), b.finalize());
    }

    #[test]
    fn write_f64_slice_bulk_matches_element_protocol() {
        fn pseudo_random_values(n: usize) -> Vec<f64> {
            let mut state = 0x4d59_5df4_d0f3_3173_u64;
            let mut values = Vec::with_capacity(n);
            for idx in 0..n {
                state = state
                    .wrapping_mul(6364136223846793005)
                    .wrapping_add(1442695040888963407);
                let mantissa = state >> 12;
                let unit = f64::from_bits(0x3ff0_0000_0000_0000 | mantissa) - 1.0;
                values.push((unit - 0.5) * ((idx % 17) as f64 + 1.0));
            }
            values
        }

        fn fast_key(values: &[f64]) -> Fingerprint {
            let mut fp = Fingerprinter::new();
            fp.write_str("write_f64_slice_bulk_matches_element_protocol");
            fp.write_f64_slice(values);
            fp.finalize()
        }

        fn slow_key(values: &[f64]) -> Fingerprint {
            let mut fp = Fingerprinter::new();
            fp.write_str("write_f64_slice_bulk_matches_element_protocol");
            fp.write_usize(values.len());
            fp.write_f64_slice_payload_slow(values);
            fp.finalize()
        }

        let clean = pseudo_random_values(257);
        assert_eq!(fast_key(&clean), slow_key(&clean));

        let mut normalized = clean.clone();
        normalized[7] = -0.0;
        normalized[113] = f64::from_bits(0x7ff8_0000_0000_0042);
        assert_eq!(fast_key(&normalized), slow_key(&normalized));
    }

    #[test]
    fn write_f64_arrays_match_element_protocol() {
        let values = ndarray::Array2::from_shape_vec(
            (3, 4),
            vec![
                1.25,
                -2.5,
                3.75,
                4.0,
                5.5,
                -0.0,
                7.25,
                8.5,
                9.75,
                10.0,
                f64::from_bits(0x7ff8_0000_0000_0100),
                12.25,
            ],
        )
        .expect("test array shape is valid");

        let mut fast = Fingerprinter::new();
        fast.write_str("write_f64_arrays_match_element_protocol");
        fast.write_f64_array2(&values);

        let mut slow = Fingerprinter::new();
        slow.write_str("write_f64_arrays_match_element_protocol");
        slow.write_usize(values.nrows());
        slow.write_usize(values.ncols());
        slow.write_f64_slice_payload_slow_iter(values.iter().copied());

        assert_eq!(fast.finalize(), slow.finalize());
    }

    #[test]
    fn fingerprint_serde_roundtrips_as_hex() {
        let mut fp = Fingerprinter::new();
        fp.absorb_str(b"k", "fingerprint-serde");
        let key = fp.finalize();
        let json = serde_json::to_string(&key).expect("serialize");
        // Serialized form is the canonical quoted hex string.
        assert_eq!(json, format!("\"{}\"", key.to_hex()));
        let back: Fingerprint = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(key, back);
        // A malformed hex payload is rejected, not silently aliased.
        assert!(serde_json::from_str::<Fingerprint>("\"not-hex\"").is_err());
    }

    #[test]
    fn invalid_hex_rejected() {
        assert!(Fingerprint::from_hex("not hex").is_none());
        assert!(Fingerprint::from_hex(&"a".repeat(63)).is_none());
        assert!(Fingerprint::from_hex(&"z".repeat(64)).is_none());
    }
}