rill-ml 1.2.0-rc.3

Lightweight, serializable online machine learning for Rust applications and streaming data.
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
//! Bounded, caller-clocked primitives for decisions with delayed feedback.
//!
//! The ledger intentionally knows nothing about product actions or reward
//! meaning. Callers provide identifiers, timestamps, generations, contexts,
//! and actions. Pending decisions are never evicted implicitly.

use std::collections::BTreeMap;

use thiserror::Error;

use crate::RillError;
#[cfg(feature = "serde")]
use crate::ValidateState;

/// Version of the serialized ledger state introduced with the Preview API.
pub const DECISION_LEDGER_STATE_VERSION: u32 = 1;
/// Default maximum serialized-equivalent context size per decision.
pub const DEFAULT_MAX_CONTEXT_BYTES: usize = 1024 * 1024;
/// Default maximum serialized-equivalent action size per decision.
pub const DEFAULT_MAX_ACTION_BYTES: usize = 64 * 1024;

/// Caller-extensible validation hook that makes per-decision memory bounded.
pub trait BoundedDecisionValue {
    /// Validate content and reject an estimated representation above `max_bytes`.
    fn validate_bounded(&self, max_bytes: usize) -> Result<(), DecisionLedgerError>;
}

impl BoundedDecisionValue for Vec<f64> {
    fn validate_bounded(&self, max_bytes: usize) -> Result<(), DecisionLedgerError> {
        if self
            .len()
            .checked_mul(std::mem::size_of::<f64>())
            .is_none_or(|size| size > max_bytes)
        {
            return Err(DecisionLedgerError::ValueTooLarge);
        }
        if self.iter().any(|value| !value.is_finite()) {
            return Err(DecisionLedgerError::InvalidValue);
        }
        Ok(())
    }
}

impl BoundedDecisionValue for Vec<u8> {
    fn validate_bounded(&self, max_bytes: usize) -> Result<(), DecisionLedgerError> {
        if self.len() > max_bytes {
            return Err(DecisionLedgerError::ValueTooLarge);
        }
        Ok(())
    }
}

impl BoundedDecisionValue for String {
    fn validate_bounded(&self, max_bytes: usize) -> Result<(), DecisionLedgerError> {
        if self.len() > max_bytes {
            return Err(DecisionLedgerError::ValueTooLarge);
        }
        Ok(())
    }
}

macro_rules! impl_fixed_decision_value {
    ($($type:ty),+ $(,)?) => {
        $(impl BoundedDecisionValue for $type {
            fn validate_bounded(&self, max_bytes: usize) -> Result<(), DecisionLedgerError> {
                if std::mem::size_of::<$type>() > max_bytes {
                    return Err(DecisionLedgerError::ValueTooLarge);
                }
                Ok(())
            }
        })+
    };
}

impl_fixed_decision_value!((), bool, usize, u64, u32, u16, u8, i64, i32, i16, i8);

/// Opaque, caller-assigned decision identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DecisionId(pub u128);

/// Capacity limits for a [`DecisionLedger`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct DecisionLedgerConfig {
    /// Maximum number of incomplete decisions. They are never auto-evicted.
    pub max_pending: usize,
    /// Maximum number of completed tombstones. They are never auto-evicted.
    pub max_completed: usize,
    /// Maximum bounded-size estimate for one context.
    pub max_context_bytes: usize,
    /// Maximum bounded-size estimate for one action.
    pub max_action_bytes: usize,
}

impl DecisionLedgerConfig {
    /// Build explicit pending/completed bounds.
    pub fn new(max_pending: usize, max_completed: usize) -> Result<Self, DecisionLedgerError> {
        if max_pending == 0 || max_completed == 0 {
            return Err(DecisionLedgerError::InvalidCapacity);
        }
        Ok(Self {
            max_pending,
            max_completed,
            max_context_bytes: DEFAULT_MAX_CONTEXT_BYTES,
            max_action_bytes: DEFAULT_MAX_ACTION_BYTES,
        })
    }

    /// Override per-entry context/action byte bounds.
    pub fn with_value_limits(
        mut self,
        max_context_bytes: usize,
        max_action_bytes: usize,
    ) -> Result<Self, DecisionLedgerError> {
        if max_context_bytes == 0 || max_action_bytes == 0 {
            return Err(DecisionLedgerError::InvalidCapacity);
        }
        self.max_context_bytes = max_context_bytes;
        self.max_action_bytes = max_action_bytes;
        Ok(self)
    }
}

impl Default for DecisionLedgerConfig {
    fn default() -> Self {
        Self {
            max_pending: 1_024,
            max_completed: 4_096,
            max_context_bytes: DEFAULT_MAX_CONTEXT_BYTES,
            max_action_bytes: DEFAULT_MAX_ACTION_BYTES,
        }
    }
}

/// Immutable facts recorded at decision time.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct PendingDecision<C, A> {
    /// Unique identifier.
    pub id: DecisionId,
    /// Context presented to the decision policy.
    pub context: C,
    /// Selected action.
    pub action: A,
    /// Caller-supplied decision timestamp.
    pub created_at: u64,
    /// Last timestamp at which feedback is accepted, inclusively.
    pub expires_at: u64,
    /// Model generation that produced the action.
    pub model_generation: u64,
}

impl<C, A> PendingDecision<C, A> {
    /// Construct immutable decision facts.
    pub const fn new(
        id: DecisionId,
        context: C,
        action: A,
        created_at: u64,
        expires_at: u64,
        model_generation: u64,
    ) -> Self {
        Self {
            id,
            context,
            action,
            created_at,
            expires_at,
            model_generation,
        }
    }
}

/// Delayed outcome submitted by a caller.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct DecisionOutcome<A> {
    /// Decision being completed.
    pub decision_id: DecisionId,
    /// Action the outcome belongs to.
    pub action: A,
    /// Caller-defined finite reward.
    pub reward: f64,
    /// Caller-supplied outcome timestamp.
    pub observed_at: u64,
    /// Generation expected by the outcome producer.
    pub model_generation: u64,
}

/// Completed decision retained as a bounded replay tombstone.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct CompletedDecision<C, A> {
    /// Original immutable decision facts.
    pub decision: PendingDecision<C, A>,
    /// Accepted reward.
    pub reward: f64,
    /// Accepted outcome timestamp.
    pub observed_at: u64,
}

/// Result of registering a decision.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecisionReceipt {
    /// Registered or replayed identifier.
    pub id: DecisionId,
    /// Whether this call inserted new pending state.
    pub status: RegistrationStatus,
}

/// Registration/idempotency status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegistrationStatus {
    /// Newly inserted pending decision.
    Registered,
    /// Exact replay of an existing pending decision.
    PendingReplay,
    /// Exact replay of a decision already represented by a tombstone.
    CompletedReplay,
}

/// Result of accepted feedback.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FeedbackStatus {
    /// Feedback was accepted and the decision moved to completed state.
    Applied,
}

/// Validation and capacity failures. Every failure leaves the ledger intact.
#[derive(Debug, Error, Clone, PartialEq)]
#[non_exhaustive]
pub enum DecisionLedgerError {
    /// A configured capacity was zero.
    #[error("decision ledger capacities must be greater than zero")]
    InvalidCapacity,
    /// Decision expiry preceded creation.
    #[error("decision expiry precedes its creation time")]
    InvalidTimeRange,
    /// A reward was NaN or infinite.
    #[error("decision reward must be finite")]
    NonFiniteReward,
    /// Pending storage is full; live decisions are not evicted implicitly.
    #[error("pending decision capacity is exhausted")]
    PendingCapacityExceeded,
    /// Tombstone storage is full; completed entries require explicit cleanup.
    #[error("completed decision capacity is exhausted")]
    CompletedCapacityExceeded,
    /// A context or action exceeds the configured byte estimate.
    #[error("decision context or action exceeds its configured byte limit")]
    ValueTooLarge,
    /// A context or action violates its type-specific semantic invariants.
    #[error("decision context or action is invalid")]
    InvalidValue,
    /// The identifier exists with different immutable facts.
    #[error("decision id already exists with different facts")]
    DecisionConflict,
    /// Feedback references no pending or completed identifier.
    #[error("feedback references an unknown decision")]
    UnknownDecision,
    /// Feedback was already accepted for this identifier.
    #[error("feedback was already applied")]
    DuplicateFeedback,
    /// Outcome timestamp precedes decision creation.
    #[error("feedback predates the decision")]
    FeedbackBeforeDecision,
    /// Outcome timestamp is after the inclusive expiry boundary.
    #[error("feedback arrived after decision expiry")]
    FeedbackExpired,
    /// Outcome generation differs from the recorded generation.
    #[error("feedback model generation does not match the decision")]
    GenerationMismatch,
    /// Outcome action differs from the selected action.
    #[error("feedback action does not match the decision")]
    ActionMismatch,
}

/// Bounded pending decisions plus bounded completed tombstones.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct DecisionLedger<C, A> {
    state_version: u32,
    config: DecisionLedgerConfig,
    pending: BTreeMap<DecisionId, PendingDecision<C, A>>,
    completed: BTreeMap<DecisionId, CompletedDecision<C, A>>,
}

impl<C, A> DecisionLedger<C, A>
where
    C: PartialEq + BoundedDecisionValue,
    A: PartialEq + BoundedDecisionValue,
{
    /// Create an empty ledger with explicit capacity bounds.
    pub fn new(config: DecisionLedgerConfig) -> Result<Self, DecisionLedgerError> {
        if config.max_pending == 0
            || config.max_completed == 0
            || config.max_context_bytes == 0
            || config.max_action_bytes == 0
        {
            return Err(DecisionLedgerError::InvalidCapacity);
        }
        Ok(Self {
            state_version: DECISION_LEDGER_STATE_VERSION,
            config,
            pending: BTreeMap::new(),
            completed: BTreeMap::new(),
        })
    }

    /// Register immutable decision facts, accepting exact idempotent replay.
    pub fn register(
        &mut self,
        decision: PendingDecision<C, A>,
    ) -> Result<DecisionReceipt, DecisionLedgerError> {
        if decision.expires_at < decision.created_at {
            return Err(DecisionLedgerError::InvalidTimeRange);
        }
        decision
            .context
            .validate_bounded(self.config.max_context_bytes)?;
        decision
            .action
            .validate_bounded(self.config.max_action_bytes)?;
        if let Some(existing) = self.pending.get(&decision.id) {
            return if existing == &decision {
                Ok(DecisionReceipt {
                    id: decision.id,
                    status: RegistrationStatus::PendingReplay,
                })
            } else {
                Err(DecisionLedgerError::DecisionConflict)
            };
        }
        if let Some(existing) = self.completed.get(&decision.id) {
            return if existing.decision == decision {
                Ok(DecisionReceipt {
                    id: decision.id,
                    status: RegistrationStatus::CompletedReplay,
                })
            } else {
                Err(DecisionLedgerError::DecisionConflict)
            };
        }
        if self.pending.len() >= self.config.max_pending {
            return Err(DecisionLedgerError::PendingCapacityExceeded);
        }
        let id = decision.id;
        self.pending.insert(id, decision);
        Ok(DecisionReceipt {
            id,
            status: RegistrationStatus::Registered,
        })
    }

    /// Validate and apply delayed feedback transactionally.
    pub fn apply_feedback(
        &mut self,
        outcome: DecisionOutcome<A>,
    ) -> Result<FeedbackStatus, DecisionLedgerError> {
        if !outcome.reward.is_finite() {
            return Err(DecisionLedgerError::NonFiniteReward);
        }
        outcome
            .action
            .validate_bounded(self.config.max_action_bytes)?;
        if self.completed.contains_key(&outcome.decision_id) {
            return Err(DecisionLedgerError::DuplicateFeedback);
        }
        let pending = self
            .pending
            .get(&outcome.decision_id)
            .ok_or(DecisionLedgerError::UnknownDecision)?;
        if outcome.observed_at < pending.created_at {
            return Err(DecisionLedgerError::FeedbackBeforeDecision);
        }
        if outcome.observed_at > pending.expires_at {
            return Err(DecisionLedgerError::FeedbackExpired);
        }
        if outcome.model_generation != pending.model_generation {
            return Err(DecisionLedgerError::GenerationMismatch);
        }
        if outcome.action != pending.action {
            return Err(DecisionLedgerError::ActionMismatch);
        }
        if self.completed.len() >= self.config.max_completed {
            return Err(DecisionLedgerError::CompletedCapacityExceeded);
        }

        // Every fallible check is above this point. Moving the pending entry is
        // therefore an all-or-nothing state transition.
        let decision = self
            .pending
            .remove(&outcome.decision_id)
            .ok_or(DecisionLedgerError::UnknownDecision)?;
        self.completed.insert(
            outcome.decision_id,
            CompletedDecision {
                decision,
                reward: outcome.reward,
                observed_at: outcome.observed_at,
            },
        );
        Ok(FeedbackStatus::Applied)
    }

    /// Return pending decision facts without updating state.
    pub fn pending(&self, id: DecisionId) -> Option<&PendingDecision<C, A>> {
        self.pending.get(&id)
    }

    /// Return a completed tombstone without updating state.
    pub fn completed(&self, id: DecisionId) -> Option<&CompletedDecision<C, A>> {
        self.completed.get(&id)
    }

    /// Number of incomplete decisions.
    pub fn pending_len(&self) -> usize {
        self.pending.len()
    }

    /// Number of retained completed tombstones.
    pub fn completed_len(&self) -> usize {
        self.completed.len()
    }

    /// Validate all feature-independent structural and bounded-value invariants.
    ///
    /// This is available even when the optional `serde` feature is disabled;
    /// [`ValidateState`] delegates to the same checks when serde persistence is
    /// enabled.
    pub fn validate(&self) -> Result<(), RillError> {
        self.validate_structure()
    }

    /// Explicitly clear decisions whose expiry is strictly before `now`.
    pub fn clear_expired(&mut self, now: u64) -> Vec<DecisionId> {
        let ids = self
            .pending
            .iter()
            .filter_map(|(&id, decision)| (decision.expires_at < now).then_some(id))
            .collect::<Vec<_>>();
        for id in &ids {
            self.pending.remove(id);
        }
        ids
    }

    /// Explicitly clear tombstones observed strictly before `before`.
    pub fn clear_completed_before(&mut self, before: u64) -> Vec<DecisionId> {
        let ids = self
            .completed
            .iter()
            .filter_map(|(&id, decision)| (decision.observed_at < before).then_some(id))
            .collect::<Vec<_>>();
        for id in &ids {
            self.completed.remove(id);
        }
        ids
    }

    fn validate_structure(&self) -> Result<(), RillError> {
        if self.state_version != DECISION_LEDGER_STATE_VERSION {
            return Err(RillError::IncompatibleStateVersion {
                expected: DECISION_LEDGER_STATE_VERSION,
                actual: self.state_version,
            });
        }
        if self.config.max_pending == 0
            || self.config.max_completed == 0
            || self.config.max_context_bytes == 0
            || self.config.max_action_bytes == 0
        {
            return Err(RillError::InvalidState(
                "decision ledger capacity must be non-zero".to_owned(),
            ));
        }
        if self.pending.len() > self.config.max_pending
            || self.completed.len() > self.config.max_completed
        {
            return Err(RillError::InvalidState(
                "decision ledger exceeds its configured capacity".to_owned(),
            ));
        }
        for (id, decision) in &self.pending {
            if id != &decision.id || decision.expires_at < decision.created_at {
                return Err(RillError::InvalidState(
                    "pending decision has inconsistent id or timestamps".to_owned(),
                ));
            }
            if self.completed.contains_key(id) {
                return Err(RillError::InvalidState(
                    "decision exists in pending and completed sets".to_owned(),
                ));
            }
            decision
                .context
                .validate_bounded(self.config.max_context_bytes)
                .map_err(|error| RillError::InvalidState(error.to_string()))?;
            decision
                .action
                .validate_bounded(self.config.max_action_bytes)
                .map_err(|error| RillError::InvalidState(error.to_string()))?;
        }
        for (id, completed) in &self.completed {
            let decision = &completed.decision;
            if id != &decision.id
                || decision.expires_at < decision.created_at
                || completed.observed_at < decision.created_at
                || completed.observed_at > decision.expires_at
                || !completed.reward.is_finite()
            {
                return Err(RillError::InvalidState(
                    "completed decision has inconsistent state".to_owned(),
                ));
            }
            decision
                .context
                .validate_bounded(self.config.max_context_bytes)
                .map_err(|error| RillError::InvalidState(error.to_string()))?;
            decision
                .action
                .validate_bounded(self.config.max_action_bytes)
                .map_err(|error| RillError::InvalidState(error.to_string()))?;
        }
        Ok(())
    }

    /// Validate structural invariants and caller-owned context/action state.
    pub fn validate_state_with<FC, FA>(
        &self,
        mut validate_context: FC,
        mut validate_action: FA,
    ) -> Result<(), RillError>
    where
        FC: FnMut(&C) -> Result<(), RillError>,
        FA: FnMut(&A) -> Result<(), RillError>,
    {
        self.validate_structure()?;
        for decision in self.pending.values() {
            validate_context(&decision.context)?;
            validate_action(&decision.action)?;
        }
        for completed in self.completed.values() {
            validate_context(&completed.decision.context)?;
            validate_action(&completed.decision.action)?;
        }
        Ok(())
    }
}

#[cfg(feature = "serde")]
impl<C, A> ValidateState for DecisionLedger<C, A>
where
    C: PartialEq + BoundedDecisionValue,
    A: PartialEq + BoundedDecisionValue,
{
    fn validate_state(&self) -> Result<(), RillError> {
        self.validate()
    }
}

/// LinUCB-oriented decision facts; the generic ledger remains algorithm-free.
#[cfg(feature = "bandit")]
pub type DelayedContextualDecision = PendingDecision<Vec<f64>, usize>;

/// Atomically apply a validated outcome to both a ledger and LinUCB model.
///
/// The helper clones both Preview ledger state and the model, so either both
/// transitions succeed or neither changes. Callers requiring a lower-copy
/// path can validate with [`DecisionLedger::pending`] and manage their own
/// transaction boundary.
#[cfg(feature = "bandit")]
pub fn apply_contextual_outcome(
    ledger: &mut DecisionLedger<Vec<f64>, usize>,
    model: &mut crate::bandit::LinUcb,
    outcome: DecisionOutcome<usize>,
) -> Result<FeedbackStatus, RillError> {
    use crate::bandit::ContextualBandit;

    let pending = ledger
        .pending(outcome.decision_id)
        .ok_or_else(|| RillError::InvalidState("unknown delayed decision".to_owned()))?;
    if pending.context.len() != model.feature_count() {
        return Err(RillError::DimensionMismatch {
            expected: model.feature_count(),
            actual: pending.context.len(),
        });
    }
    for &value in &pending.context {
        if !value.is_finite() {
            return Err(RillError::NonFiniteValue {
                field: "decision context",
                value,
            });
        }
    }
    let context = pending.context.clone();
    let action = outcome.action;
    let reward = outcome.reward;
    let mut next_ledger = ledger.clone();
    let mut next_model = model.clone();
    let status = next_ledger
        .apply_feedback(outcome)
        .map_err(|error| RillError::InvalidState(error.to_string()))?;
    next_model.update(action, &context, reward)?;
    *ledger = next_ledger;
    *model = next_model;
    Ok(status)
}

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

    fn decision(id: u128) -> PendingDecision<Vec<f64>, usize> {
        PendingDecision::new(DecisionId(id), vec![1.0, 2.0], 1, 10, 20, 7)
    }

    fn outcome(id: u128) -> DecisionOutcome<usize> {
        DecisionOutcome {
            decision_id: DecisionId(id),
            action: 1,
            reward: 0.5,
            observed_at: 15,
            model_generation: 7,
        }
    }

    #[test]
    fn registration_is_idempotent_but_conflicts_fail() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(2, 2).unwrap()).unwrap();
        assert_eq!(
            ledger.register(decision(1)).unwrap().status,
            RegistrationStatus::Registered
        );
        assert_eq!(
            ledger.register(decision(1)).unwrap().status,
            RegistrationStatus::PendingReplay
        );
        let mut conflict = decision(1);
        conflict.action = 0;
        assert_eq!(
            ledger.register(conflict),
            Err(DecisionLedgerError::DecisionConflict)
        );
        assert_eq!(ledger.pending_len(), 1);
    }

    #[test]
    fn feedback_checks_are_atomic_and_tombstoned() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(1, 1).unwrap()).unwrap();
        ledger.register(decision(1)).unwrap();
        let before = ledger.clone();
        let mut bad = outcome(1);
        bad.action = 0;
        assert_eq!(
            ledger.apply_feedback(bad),
            Err(DecisionLedgerError::ActionMismatch)
        );
        assert_eq!(ledger, before);
        assert_eq!(
            ledger.apply_feedback(outcome(1)).unwrap(),
            FeedbackStatus::Applied
        );
        assert_eq!(ledger.pending_len(), 0);
        assert_eq!(ledger.completed_len(), 1);
        assert_eq!(
            ledger.apply_feedback(outcome(1)),
            Err(DecisionLedgerError::DuplicateFeedback)
        );
    }

    #[test]
    fn expiry_boundary_is_inclusive_and_cleanup_is_explicit() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(2, 2).unwrap()).unwrap();
        ledger.register(decision(1)).unwrap();
        let mut at_boundary = outcome(1);
        at_boundary.observed_at = 20;
        assert!(ledger.apply_feedback(at_boundary).is_ok());

        ledger.register(decision(2)).unwrap();
        assert!(ledger.clear_expired(20).is_empty());
        assert_eq!(ledger.clear_expired(21), vec![DecisionId(2)]);
    }

    #[test]
    fn capacity_never_evicts_live_or_completed_state() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(1, 1).unwrap()).unwrap();
        ledger.register(decision(1)).unwrap();
        assert_eq!(
            ledger.register(decision(2)),
            Err(DecisionLedgerError::PendingCapacityExceeded)
        );
        ledger.apply_feedback(outcome(1)).unwrap();
        ledger.register(decision(2)).unwrap();
        assert_eq!(
            ledger.apply_feedback(outcome(2)),
            Err(DecisionLedgerError::CompletedCapacityExceeded)
        );
        assert!(ledger.pending(DecisionId(2)).is_some());
    }

    #[test]
    fn per_decision_context_and_action_memory_is_bounded() {
        let config = DecisionLedgerConfig::new(2, 2)
            .unwrap()
            .with_value_limits(8, std::mem::size_of::<usize>())
            .unwrap();
        let mut ledger = DecisionLedger::new(config).unwrap();
        assert_eq!(
            ledger.register(decision(1)),
            Err(DecisionLedgerError::ValueTooLarge)
        );
        assert_eq!(ledger.pending_len(), 0);
    }

    #[test]
    fn every_feedback_fact_is_checked_without_mutation() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(2, 2).unwrap()).unwrap();
        ledger.register(decision(1)).unwrap();
        let cases = [
            (
                DecisionOutcome {
                    decision_id: DecisionId(9),
                    ..outcome(1)
                },
                DecisionLedgerError::UnknownDecision,
            ),
            (
                DecisionOutcome {
                    observed_at: 9,
                    ..outcome(1)
                },
                DecisionLedgerError::FeedbackBeforeDecision,
            ),
            (
                DecisionOutcome {
                    observed_at: 21,
                    ..outcome(1)
                },
                DecisionLedgerError::FeedbackExpired,
            ),
            (
                DecisionOutcome {
                    model_generation: 8,
                    ..outcome(1)
                },
                DecisionLedgerError::GenerationMismatch,
            ),
            (
                DecisionOutcome {
                    action: 0,
                    ..outcome(1)
                },
                DecisionLedgerError::ActionMismatch,
            ),
        ];
        for (bad, expected) in cases {
            let before = ledger.clone();
            assert_eq!(ledger.apply_feedback(bad), Err(expected));
            assert_eq!(ledger, before);
        }
    }

    #[test]
    fn completed_replay_and_explicit_tombstone_cleanup() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(2, 2).unwrap()).unwrap();
        ledger.register(decision(1)).unwrap();
        ledger.apply_feedback(outcome(1)).unwrap();
        assert_eq!(
            ledger.register(decision(1)).unwrap().status,
            RegistrationStatus::CompletedReplay
        );
        assert!(ledger.clear_completed_before(15).is_empty());
        assert_eq!(ledger.clear_completed_before(16), vec![DecisionId(1)]);
        assert_eq!(ledger.completed_len(), 0);
    }

    #[test]
    fn caller_validator_can_enforce_domain_specific_context_rules() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(2, 2).unwrap()).unwrap();
        let mut invalid = decision(1);
        invalid.context[0] = -1.0;
        ledger.register(invalid).unwrap();
        assert!(
            ledger
                .validate_state_with(
                    |context| {
                        for &value in context {
                            if value < 0.0 {
                                return Err(RillError::InvalidState(
                                    "context values must be non-negative".to_owned(),
                                ));
                            }
                        }
                        Ok(())
                    },
                    |_| Ok(()),
                )
                .is_err()
        );
    }

    #[cfg(feature = "bandit")]
    #[test]
    fn contextual_helper_updates_model_and_ledger_together() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(2, 2).unwrap()).unwrap();
        ledger
            .register(PendingDecision::new(DecisionId(1), vec![1.0], 0, 10, 20, 7))
            .unwrap();
        let config = crate::bandit::LinUcbConfig {
            arm_count: 1,
            feature_count: 1,
            alpha: 1.0,
        };
        let mut model = crate::bandit::LinUcb::new(config).unwrap();
        apply_contextual_outcome(
            &mut ledger,
            &mut model,
            DecisionOutcome {
                decision_id: DecisionId(1),
                action: 0,
                reward: 1.0,
                observed_at: 12,
                model_generation: 7,
            },
        )
        .unwrap();
        assert_eq!(ledger.pending_len(), 0);
        assert_eq!(ledger.completed_len(), 1);
        assert_eq!(crate::bandit::ContextualBandit::samples_seen(&model), 1);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn serde_roundtrip_and_corruption_validation() {
        let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(2, 2).unwrap()).unwrap();
        ledger.register(decision(1)).unwrap();
        let json = serde_json::to_string(&ledger).unwrap();
        let restored: DecisionLedger<Vec<f64>, usize> = serde_json::from_str(&json).unwrap();
        assert_eq!(restored, ledger);
        restored.validate_state().unwrap();

        let corrupt = json.replace("\"state_version\":1", "\"state_version\":9");
        let restored: DecisionLedger<Vec<f64>, usize> = serde_json::from_str(&corrupt).unwrap();
        assert!(restored.validate_state().is_err());

        let corrupt = json.replace("\"max_pending\":2", "\"max_pending\":0");
        let restored: DecisionLedger<Vec<f64>, usize> = serde_json::from_str(&corrupt).unwrap();
        assert!(restored.validate_state().is_err());
    }

    proptest! {
        #[test]
        fn arbitrary_invalid_feedback_never_removes_pending(
            reward in prop_oneof![Just(f64::NAN), Just(f64::INFINITY)],
            observed_at in any::<u64>(),
        ) {
            let mut ledger = DecisionLedger::new(DecisionLedgerConfig::new(1, 1).unwrap()).unwrap();
            ledger.register(decision(1)).unwrap();
            let before = ledger.clone();
            let result = ledger.apply_feedback(DecisionOutcome {
                decision_id: DecisionId(1), action: 1, reward, observed_at, model_generation: 7,
            });
            prop_assert!(result.is_err());
            prop_assert_eq!(ledger, before);
        }
    }
}