khive-fold 0.2.0

Cognitive primitives — Fold, Anchor, Objective, Selector
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
//! Deterministic ordering primitives for objective functions
//!
//! This module provides utilities for deterministic ordering of scored candidates,
//! ensuring reproducible results across processes, architectures, and executions.
//!
//! # Key Components
//!
//! - [`HasId`]: Trait for candidates with stable UUID identifiers
//! - [`canonical_f64`]/[`canonical_f32`]: Normalize floating-point values for comparison
//! - [`cmp_desc_score_then_id`]: Deterministic comparator (f64 + Uuid) with UUID tie-breaking
//! - [`ScoredEntry`]: Ord-implementing wrapper for heap operations, backed by [`DeterministicScore`]
//! - [`QuantKey`]: Re-exported from `khive-score` — 8-byte packed sort key (i32 score + u32 ID prefix)
//! - [`DeterministicScore`]: Re-exported from `khive-score` — i64 fixed-point score
//! - [`Ranked`]: Re-exported from `khive-score` — score + generic `Ord` ID pair for heaps

mod canonical;
mod compare;
mod has_id;
mod scored_entry;

pub use canonical::{canonical_f32, canonical_f64};
pub use compare::{cmp_asc_score_then_id, cmp_desc_score_then_id};
pub use has_id::HasId;
pub use scored_entry::ScoredEntry;

// Re-exports from khive-score
pub use khive_score::QuantKey;
pub use khive_score::{cmp_asc_then_id, cmp_desc_then_id, DeterministicScore, Ranked};

#[cfg(test)]
use canonical::{CANONICAL_NAN_F32, CANONICAL_NAN_F64};

#[cfg(test)]
mod tests {
    use super::*;
    use std::cmp::Ordering;
    use uuid::Uuid;

    // ------------------------------------------------------------------------
    // Canonical Function Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_canonical_f64_nan_variants() {
        let nans = [
            f64::NAN,
            -f64::NAN,
            f64::from_bits(0x7ff0_0000_0000_0001), // signaling NaN
            f64::from_bits(0x7ff8_0000_0000_0001), // quiet NaN with payload
            f64::from_bits(0xfff8_0000_0000_0001), // negative quiet NaN
        ];

        for nan in nans {
            let canonical = canonical_f64(nan);
            assert!(canonical.is_nan(), "Should still be NaN");
            assert_eq!(
                canonical.to_bits(),
                CANONICAL_NAN_F64,
                "NaN variant {:016x} should canonicalize to {:016x}",
                nan.to_bits(),
                CANONICAL_NAN_F64
            );
        }
    }

    #[test]
    fn test_canonical_f64_zero() {
        assert!(canonical_f64(-0.0).is_sign_positive());
        assert_eq!(canonical_f64(-0.0), 0.0);
        assert_eq!(canonical_f64(-0.0).to_bits(), 0u64);
    }

    #[test]
    fn test_canonical_f64_preserves_normal() {
        let values = [1.0, -1.0, 0.5, f64::MAX, f64::MIN_POSITIVE, f64::EPSILON];
        for v in values {
            assert_eq!(canonical_f64(v), v);
            assert_eq!(canonical_f64(v).to_bits(), v.to_bits());
        }
    }

    #[test]
    fn test_canonical_f32_nan_variants() {
        let nans = [
            f32::NAN,
            -f32::NAN,
            f32::from_bits(0x7f80_0001), // signaling NaN
            f32::from_bits(0x7fc0_0001), // quiet NaN with payload
        ];

        for nan in nans {
            let canonical = canonical_f32(nan);
            assert!(canonical.is_nan());
            assert_eq!(canonical.to_bits(), CANONICAL_NAN_F32);
        }
    }

    #[test]
    fn test_canonical_f32_zero() {
        assert!(canonical_f32(-0.0_f32).is_sign_positive());
        assert_eq!(canonical_f32(-0.0_f32), 0.0_f32);
    }

    #[test]
    fn test_canonical_idempotent() {
        let values = [0.0, -0.0, 1.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY];
        for v in values {
            let once = canonical_f64(v);
            let twice = canonical_f64(once);
            assert_eq!(once.to_bits(), twice.to_bits());
        }
    }

    // ------------------------------------------------------------------------
    // Comparison Function Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_descending_score_ordering() {
        let id_a = Uuid::from_u128(1);
        let id_b = Uuid::from_u128(2);

        assert_eq!(
            cmp_desc_score_then_id(0.9, id_a, 0.5, id_b),
            Ordering::Less,
            "Higher score should come first"
        );
        assert_eq!(
            cmp_desc_score_then_id(0.5, id_a, 0.9, id_b),
            Ordering::Greater,
            "Lower score should come second"
        );
    }

    #[test]
    fn test_uuid_tie_breaking() {
        let id_a = Uuid::from_u128(1);
        let id_b = Uuid::from_u128(2);

        assert_eq!(
            cmp_desc_score_then_id(0.5, id_a, 0.5, id_b),
            Ordering::Less,
            "Lower UUID should come first on tie"
        );
        assert_eq!(
            cmp_desc_score_then_id(0.5, id_b, 0.5, id_a),
            Ordering::Greater,
            "Higher UUID should come second on tie"
        );
    }

    #[test]
    fn test_nan_handling() {
        let id_a = Uuid::from_u128(1);
        let id_b = Uuid::from_u128(2);

        assert_eq!(
            cmp_desc_score_then_id(f64::NAN, id_a, 0.5, id_b),
            Ordering::Greater,
            "NaN should sort after normal values in descending"
        );
        assert_eq!(
            cmp_desc_score_then_id(0.5, id_a, f64::NAN, id_b),
            Ordering::Less,
            "Normal value should sort before NaN in descending"
        );
        assert_eq!(
            cmp_desc_score_then_id(f64::NAN, id_a, f64::NAN, id_b),
            Ordering::Less,
            "Two NaNs should use UUID tie-breaking"
        );
    }

    #[test]
    fn test_sorting_stability() {
        let entries: Vec<(f64, Uuid)> = (0..100)
            .map(|i| (0.5, Uuid::from_u128(i as u128)))
            .collect();

        let mut sorted1 = entries.clone();
        let mut sorted2 = entries.clone();

        sorted1.sort_by(|a, b| cmp_desc_score_then_id(a.0, a.1, b.0, b.1));
        sorted2.sort_by(|a, b| cmp_desc_score_then_id(a.0, a.1, b.0, b.1));

        assert_eq!(sorted1, sorted2, "Multiple sorts should produce same order");

        for i in 0..99 {
            assert!(sorted1[i].1 < sorted1[i + 1].1);
        }
    }

    #[test]
    fn test_ascending_variant() {
        let id_a = Uuid::from_u128(1);
        let id_b = Uuid::from_u128(2);

        assert_eq!(
            cmp_asc_score_then_id(0.3, id_a, 0.5, id_b),
            Ordering::Less,
            "Lower score should come first in ascending"
        );
        assert_eq!(
            cmp_asc_score_then_id(0.5, id_a, 0.5, id_b),
            Ordering::Less,
            "Equal scores use UUID tie-breaking"
        );
    }

    // ------------------------------------------------------------------------
    // ScoredEntry Tests
    // ------------------------------------------------------------------------

    #[derive(Debug, Clone)]
    #[allow(dead_code)]
    struct TestCandidate {
        id: Uuid,
        value: i32,
    }

    impl HasId for TestCandidate {
        fn id(&self) -> Uuid {
            self.id
        }
    }

    #[test]
    fn test_scored_entry_ord() {
        let a = TestCandidate {
            id: Uuid::from_u128(1),
            value: 10,
        };
        let b = TestCandidate {
            id: Uuid::from_u128(2),
            value: 20,
        };

        let entry_a = ScoredEntry::new(&a, 0.9, 0);
        let entry_b = ScoredEntry::new(&b, 0.5, 1);

        assert!(entry_a > entry_b);
    }

    #[test]
    fn test_scored_entry_heap() {
        use std::collections::BinaryHeap;

        let candidates: Vec<TestCandidate> = (0..10i32)
            .map(|i| TestCandidate {
                id: Uuid::from_u128(i as u128),
                value: i * 10,
            })
            .collect();

        let mut heap: BinaryHeap<ScoredEntry<&TestCandidate>> = candidates
            .iter()
            .enumerate()
            .map(|(i, c)| ScoredEntry::new(c, 0.5, i))
            .collect();

        let mut last_id = Uuid::nil();
        while let Some(entry) = heap.pop() {
            if last_id != Uuid::nil() {
                assert!(entry.id() > last_id, "Should pop in UUID order");
            }
            last_id = entry.id();
        }
    }

    #[test]
    fn test_scored_entry_equality() {
        let a = TestCandidate {
            id: Uuid::from_u128(1),
            value: 10,
        };
        let b = TestCandidate {
            id: Uuid::from_u128(1),
            value: 20,
        };

        let entry_a = ScoredEntry::new(&a, 0.5, 0);
        let entry_b = ScoredEntry::new(&b, 0.5, 1);

        assert_eq!(entry_a, entry_b);
    }

    #[test]
    fn test_scored_entry_hash() {
        use std::collections::HashSet;

        let a = TestCandidate {
            id: Uuid::from_u128(1),
            value: 10,
        };

        let entry1 = ScoredEntry::new(&a, 0.5, 0);
        let entry2 = ScoredEntry::new(&a, 0.5, 1);

        let mut set = HashSet::new();
        set.insert(entry1);
        assert!(set.contains(&entry2));
    }

    // ------------------------------------------------------------------------
    // QuantKey Tests (score's QuantKey: i32+u32 packed, NaN→0)
    // ------------------------------------------------------------------------

    #[test]
    fn test_quant_key_precision() {
        let a = QuantKey::new(0.123456, 1);
        let b = QuantKey::new(0.123457, 2);
        assert_ne!(
            a.quantized_score(),
            b.quantized_score(),
            "1e-6 difference should be distinguishable"
        );
    }

    #[test]
    fn test_quant_key_rounding() {
        let a = QuantKey::new(0.12345642, 1);
        let b = QuantKey::new(0.12345647, 2);
        assert_eq!(
            a.quantized_score(),
            b.quantized_score(),
            "Sub-1e-6 differences should round same"
        );
    }

    #[test]
    fn test_quant_key_nan_maps_to_zero() {
        let nan = QuantKey::new(f32::NAN, 1);
        let zero = QuantKey::new(0.0, 1);
        assert_eq!(
            nan.quantized_score(),
            zero.quantized_score(),
            "NaN maps to 0 in score's QuantKey"
        );
    }

    #[test]
    fn test_quant_key_heap_order() {
        use std::collections::BinaryHeap;

        let mut heap: BinaryHeap<QuantKey> = BinaryHeap::new();
        heap.push(QuantKey::new(0.95, 3));
        heap.push(QuantKey::new(0.95, 1));
        heap.push(QuantKey::new(0.95, 2));
        heap.push(QuantKey::new(0.87, 4));

        assert_eq!(heap.pop().unwrap().id_prefix(), 1);
        assert_eq!(heap.pop().unwrap().id_prefix(), 2);
        assert_eq!(heap.pop().unwrap().id_prefix(), 3);
        assert_eq!(heap.pop().unwrap().id_prefix(), 4);
    }

    // ------------------------------------------------------------------------
    // DeterministicScore Integration Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_deterministic_score_roundtrip() {
        let s = DeterministicScore::from_f64(0.75);
        assert!((s.to_f64() - 0.75).abs() < 1e-9);
    }

    #[test]
    fn test_deterministic_score_nan_maps_to_zero() {
        let s = DeterministicScore::from_f64(f64::NAN);
        assert_eq!(s, DeterministicScore::ZERO);
    }

    #[test]
    fn test_ranked_heap_with_uuid_ids() {
        use std::collections::BinaryHeap;

        let mut heap: BinaryHeap<Ranked<Uuid>> = BinaryHeap::new();
        heap.push(Ranked::new(
            DeterministicScore::from_f64(0.9),
            Uuid::from_u128(3),
        ));
        heap.push(Ranked::new(
            DeterministicScore::from_f64(0.9),
            Uuid::from_u128(1),
        ));
        heap.push(Ranked::new(
            DeterministicScore::from_f64(0.9),
            Uuid::from_u128(2),
        ));
        heap.push(Ranked::new(
            DeterministicScore::from_f64(0.5),
            Uuid::from_u128(4),
        ));

        // All 0.9 scores — lower UUID pops first
        let first = heap.pop().unwrap();
        assert_eq!(*first.id(), Uuid::from_u128(1));
        let second = heap.pop().unwrap();
        assert_eq!(*second.id(), Uuid::from_u128(2));
        let third = heap.pop().unwrap();
        assert_eq!(*third.id(), Uuid::from_u128(3));
        // Then lower score
        let fourth = heap.pop().unwrap();
        assert_eq!(*fourth.id(), Uuid::from_u128(4));
    }
}