varpulis-pst 0.10.0

Prediction Suffix Tree (PST) for complex event forecasting
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//! Pattern Markov Chain (PMC) = PST + SASE NFA
//!
//! Combines the Prediction Suffix Tree with the SASE NFA to forecast
//! whether a partially-matched pattern will complete and estimate when.
//!
//! Includes Hawkes process intensity modulation for temporal density awareness
//! and conformal prediction intervals for calibrated uncertainty bounds.

use hashlink::LruCache;
use rustc_hash::FxHashMap;

use super::conformal::ConformalCalibrator;
use super::hawkes::HawkesIntensity;
use super::online::OnlinePSTLearner;
use super::tree::{PSTConfig, PredictionSuffixTree, SymbolId};

/// Configuration for the Pattern Markov Chain.
#[derive(Debug, Clone)]
pub struct PMCConfig {
    /// Minimum probability to emit a forecast.
    pub confidence_threshold: f64,
    /// Forecast horizon in nanoseconds.
    pub horizon_ns: u64,
    /// Maximum simulation steps for waiting time estimation.
    pub max_simulation_steps: usize,
    /// Minimum events before forecasting starts. With adaptive warmup,
    /// the actual warmup may extend beyond this until predictions stabilize.
    pub warmup_events: u64,
    /// Enable Hawkes intensity modulation (default true).
    pub hawkes_enabled: bool,
    /// Enable conformal prediction intervals (default true).
    pub conformal_enabled: bool,
    /// Enable adaptive warmup: extend warmup until predictions stabilize (default true).
    pub adaptive_warmup: bool,
    /// Maximum entries in the prediction stability cache (LRU eviction).
    pub max_prediction_cache: usize,
    /// Maximum entries in the pending forecasts cache (LRU eviction).
    pub max_pending_forecasts: usize,
}

impl Default for PMCConfig {
    fn default() -> Self {
        Self {
            confidence_threshold: 0.5,
            horizon_ns: 300_000_000_000, // 5 minutes
            max_simulation_steps: 1000,
            warmup_events: 100,
            hawkes_enabled: true,
            conformal_enabled: true,
            adaptive_warmup: true,
            max_prediction_cache: 10_000,
            max_pending_forecasts: 10_000,
        }
    }
}

/// Result of a forecast computation.
#[derive(Debug, Clone)]
pub struct ForecastResult {
    /// Probability that the pattern will complete.
    pub probability: f64,
    /// Estimated time to completion in nanoseconds.
    pub expected_time_ns: u64,
    /// Label describing current state (e.g., "state_2_of_4").
    pub state_label: String,
    /// Depth of PST context used for this prediction.
    pub context_depth: usize,
    /// Number of currently active partial match runs.
    pub active_runs: usize,
    /// Lower bound of the conformal prediction interval (0.0–1.0).
    pub forecast_lower: f64,
    /// Upper bound of the conformal prediction interval (0.0–1.0).
    pub forecast_upper: f64,
    /// Prediction stability (0.0–1.0). High values mean the prediction has
    /// converged and is reliable. Low values indicate the model is still learning.
    pub forecast_confidence: f64,
}

/// Snapshot of a partial match run, used by the PMC to track active runs.
///
/// This mirrors `varpulis_sase::RunSnapshot` to avoid coupling the PST crate
/// to the SASE engine.
#[derive(Debug, Clone)]
pub struct RunSnapshot {
    /// Current NFA state index.
    pub current_state: usize,
    /// When this run started (nanoseconds since epoch).
    pub started_at_ns: i64,
}

/// Pattern Markov Chain — combines PST with SASE NFA for pattern forecasting.
///
/// Includes Hawkes process intensity modulation to boost predictions during
/// temporal bursts, and conformal calibration for prediction intervals.
#[derive(Debug)]
pub struct PatternMarkovChain {
    /// The Prediction Suffix Tree for learning event transitions.
    pst: PredictionSuffixTree,
    /// NFA transition structure: state -> { symbol -> next_state }
    nfa_transitions: Vec<FxHashMap<SymbolId, usize>>,
    /// Accept (final) states in the NFA.
    accept_states: Vec<usize>,
    /// Total number of NFA states.
    num_states: usize,
    /// Configuration.
    config: PMCConfig,
    /// Online learner for incremental PST updates.
    learner: OnlinePSTLearner,
    /// Total events processed.
    events_processed: u64,
    /// Running average inter-event time in nanoseconds.
    avg_inter_event_ns: f64,
    /// Last event timestamp for inter-event time tracking.
    last_event_ns: Option<i64>,
    /// Mapping from NFA event types to PST symbol IDs.
    event_type_to_symbol: FxHashMap<String, SymbolId>,
    /// Hawkes intensity trackers, one per event type symbol.
    hawkes_intensities: FxHashMap<SymbolId, HawkesIntensity>,
    /// Conformal calibrator for prediction intervals.
    conformal: ConformalCalibrator,
    /// Previous run states for outcome tracking: started_at_ns -> last_known_state.
    previous_run_states: FxHashMap<i64, usize>,
    /// Pending forecasts for outcome tracking: started_at_ns -> predicted_probability.
    pending_forecasts: LruCache<i64, f64>,
    /// Last prediction per (NFA state, context tail), for per-context stability tracking.
    last_prediction_per_key: LruCache<u64, f64>,
    /// Running count of consecutive "stable" predictions (delta < threshold).
    stable_prediction_count: usize,
    /// Whether adaptive warmup has declared the model stable.
    adaptive_warmup_complete: bool,
}

impl PatternMarkovChain {
    /// Create a new PMC from SASE NFA transition structure.
    ///
    /// `nfa_states` contains the NFA state information:
    /// each entry is (state_id, event_type, transitions_to).
    pub fn new(
        nfa_event_types: &[String],
        nfa_transitions: Vec<FxHashMap<SymbolId, usize>>,
        accept_states: Vec<usize>,
        num_states: usize,
        pst_config: PSTConfig,
        pmc_config: PMCConfig,
    ) -> Self {
        let max_depth = pst_config.max_depth;
        let mut pst = PredictionSuffixTree::new(pst_config);
        let mut event_type_to_symbol = FxHashMap::default();
        let mut hawkes_intensities = FxHashMap::default();

        // Register all event types from the NFA
        for et in nfa_event_types {
            let id = pst.register_symbol(et);
            event_type_to_symbol.insert(et.clone(), id);
            if pmc_config.hawkes_enabled {
                hawkes_intensities.insert(id, HawkesIntensity::new());
            }
        }

        let learner = OnlinePSTLearner::new(max_depth);
        let max_pending = pmc_config.max_pending_forecasts;
        let max_pred_cache = pmc_config.max_prediction_cache;

        Self {
            pst,
            nfa_transitions,
            accept_states,
            num_states,
            config: pmc_config,
            learner,
            events_processed: 0,
            avg_inter_event_ns: 0.0,
            last_event_ns: None,
            event_type_to_symbol,
            hawkes_intensities,
            conformal: ConformalCalibrator::with_defaults(),
            previous_run_states: FxHashMap::default(),
            pending_forecasts: LruCache::new(max_pending),
            last_prediction_per_key: LruCache::new(max_pred_cache),
            stable_prediction_count: 0,
            adaptive_warmup_complete: false,
        }
    }

    /// Process an incoming event: update PST online and compute forecast.
    ///
    /// Returns `Some(ForecastResult)` if there are active runs and the warmup period
    /// has elapsed, `None` otherwise.
    pub fn process(
        &mut self,
        event_type: &str,
        event_timestamp_ns: i64,
        active_runs: &[RunSnapshot],
    ) -> Option<ForecastResult> {
        // --- Outcome tracking: detect completed/expired runs ---
        if self.config.conformal_enabled {
            self.track_outcomes(active_runs);
        }

        // Update inter-event timing
        if let Some(last_ns) = self.last_event_ns {
            let delta = (event_timestamp_ns - last_ns).max(0) as f64;
            // Exponential moving average
            if self.avg_inter_event_ns == 0.0 {
                self.avg_inter_event_ns = delta;
            } else {
                self.avg_inter_event_ns = 0.95f64.mul_add(self.avg_inter_event_ns, 0.05 * delta);
            }
        }
        self.last_event_ns = Some(event_timestamp_ns);

        // Update PST online and (optionally) Hawkes intensity for this event type
        if let Some(&symbol) = self.event_type_to_symbol.get(event_type) {
            self.learner.update(&mut self.pst, symbol);
            if self.config.hawkes_enabled {
                if let Some(hawkes) = self.hawkes_intensities.get_mut(&symbol) {
                    hawkes.update(event_timestamp_ns);
                }
            }
        }

        self.events_processed += 1;

        // Suppress during minimum warmup
        if self.events_processed < self.config.warmup_events {
            return None;
        }

        // No active runs -> no forecast needed
        if active_runs.is_empty() {
            return None;
        }

        // Compute forecast for the most advanced active run
        let best_run = active_runs.iter().max_by_key(|r| r.current_state)?;

        // Copy context to avoid borrow conflict with mutable methods below
        let context: Vec<SymbolId> = self.learner.current_context().to_vec();
        let context_depth = context.len();

        // Compute completion probability (Hawkes-modulated or plain PST)
        let probability = if self.config.hawkes_enabled {
            self.compute_completion_probability_hawkes(
                best_run.current_state,
                &context,
                event_timestamp_ns,
            )
        } else {
            self.compute_completion_probability_plain(best_run.current_state, &context)
        };

        // Track prediction stability for adaptive warmup and forecast_confidence
        let last_ctx_symbol = context.last().copied();
        let forecast_confidence =
            self.update_prediction_stability(probability, best_run.current_state, last_ctx_symbol);

        // Adaptive warmup: suppress until predictions stabilize
        if self.config.adaptive_warmup && !self.adaptive_warmup_complete {
            return None;
        }

        // Store pending forecast for future outcome tracking (conformal only)
        if self.config.conformal_enabled {
            self.pending_forecasts
                .insert(best_run.started_at_ns, probability);
        }

        // Compute conformal prediction interval or return full interval
        let (forecast_lower, forecast_upper) = if self.config.conformal_enabled {
            self.conformal.prediction_interval(probability)
        } else {
            (0.0, 1.0)
        };

        // Estimate waiting time
        let expected_time_ns = self.estimate_waiting_time(best_run.current_state, &context);

        // State label
        let state_label = format!("state_{}_of_{}", best_run.current_state, self.num_states);

        Some(ForecastResult {
            probability,
            expected_time_ns,
            state_label,
            context_depth,
            active_runs: active_runs.len(),
            forecast_lower,
            forecast_upper,
            forecast_confidence,
        })
    }

    /// Update prediction stability tracker. Returns the current forecast_confidence
    /// (0.0–1.0) based on how stable recent predictions are.
    ///
    /// Keys by (NFA state, last context symbol) to handle alternating event patterns
    /// correctly. For A→B at state 1, P(B|A)≈1.0 and P(B|B)≈0.0 are both stable —
    /// they just differ by context. Tracking per-(state, context) avoids false oscillation.
    fn update_prediction_stability(
        &mut self,
        probability: f64,
        current_state: usize,
        last_context_symbol: Option<SymbolId>,
    ) -> f64 {
        const DELTA_THRESHOLD: f64 = 0.05;
        const STABLE_COUNT_TARGET: usize = 10;

        // Composite key: upper 32 bits = state, lower 32 bits = last context symbol
        let ctx_part = last_context_symbol.unwrap_or(0) as u64;
        let key = ((current_state as u64) << 32) | ctx_part;

        let last = self.last_prediction_per_key.get(&key).copied();
        self.last_prediction_per_key.insert(key, probability);

        if let Some(prev) = last {
            let delta = (probability - prev).abs();
            if delta < DELTA_THRESHOLD {
                self.stable_prediction_count += 1;
            } else {
                self.stable_prediction_count = self.stable_prediction_count.saturating_sub(1);
            }
        }

        // Confidence: fraction of target stable predictions, capped at 1.0
        let confidence =
            (self.stable_prediction_count as f64 / STABLE_COUNT_TARGET as f64).min(1.0);

        if self.stable_prediction_count >= STABLE_COUNT_TARGET {
            self.adaptive_warmup_complete = true;
        }

        confidence
    }

    /// Track outcomes of previously observed runs by comparing current active_runs
    /// to the previous snapshot. Runs that disappeared either completed (were in
    /// accept state) or expired.
    fn track_outcomes(&mut self, active_runs: &[RunSnapshot]) {
        // Build a set of current run keys
        let current_keys: FxHashMap<i64, usize> = active_runs
            .iter()
            .map(|r| (r.started_at_ns, r.current_state))
            .collect();

        // Find runs that were in previous_run_states but are no longer in active_runs
        let disappeared: Vec<(i64, usize)> = self
            .previous_run_states
            .iter()
            .filter(|(key, _)| !current_keys.contains_key(key))
            .map(|(&key, &state)| (key, state))
            .collect();

        for (started_at, last_state) in disappeared {
            let completed = self.accept_states.contains(&last_state);
            if let Some(predicted) = self.pending_forecasts.remove(&started_at) {
                self.conformal.record_outcome(predicted, completed);
            }
        }

        // Update previous run states snapshot
        self.previous_run_states = current_keys;
    }

    /// Forward algorithm: compute probability using plain PST-predicted transitions (no Hawkes).
    fn compute_completion_probability_plain(
        &self,
        current_state: usize,
        context: &[SymbolId],
    ) -> f64 {
        if self.accept_states.contains(&current_state) {
            return 1.0;
        }

        let max_steps = self.config.max_simulation_steps.min(50);
        let mut prob = vec![0.0f64; self.num_states];
        let mut new_prob = vec![0.0f64; self.num_states];

        for &s in &self.accept_states {
            prob[s] = 1.0;
        }

        for _ in 0..max_steps {
            new_prob.copy_from_slice(&prob);
            for (state, new_p) in new_prob.iter_mut().enumerate().take(self.num_states) {
                if self.accept_states.contains(&state) {
                    continue;
                }
                if let Some(transitions) = self.nfa_transitions.get(state) {
                    let mut state_prob = 0.0;
                    for (&symbol, &next_state) in transitions {
                        let pst_prob = self.pst.predict_symbol(context, symbol);
                        state_prob += pst_prob * prob[next_state];
                    }
                    *new_p = state_prob;
                }
            }
            // Early exit if converged
            if prob
                .iter()
                .zip(new_prob.iter())
                .all(|(a, b)| (a - b).abs() < 1e-10)
            {
                break;
            }
            std::mem::swap(&mut prob, &mut new_prob);
        }

        new_prob.get(current_state).copied().unwrap_or(0.0).min(1.0)
    }

    /// Forward algorithm: compute probability using Hawkes-modulated PST-predicted transitions.
    fn compute_completion_probability_hawkes(
        &self,
        current_state: usize,
        context: &[SymbolId],
        now_ns: i64,
    ) -> f64 {
        if self.accept_states.contains(&current_state) {
            return 1.0;
        }

        let max_steps = self.config.max_simulation_steps.min(50);
        let mut prob = vec![0.0f64; self.num_states];
        let mut new_prob = vec![0.0f64; self.num_states];

        for &s in &self.accept_states {
            prob[s] = 1.0;
        }

        for _ in 0..max_steps {
            new_prob.copy_from_slice(&prob);
            for (state, new_p) in new_prob.iter_mut().enumerate().take(self.num_states) {
                if self.accept_states.contains(&state) {
                    continue;
                }
                if let Some(transitions) = self.nfa_transitions.get(state) {
                    // Compute Hawkes-modulated transition probabilities
                    let mut raw_probs: Vec<(SymbolId, usize, f64)> = transitions
                        .iter()
                        .map(|(&symbol, &next_state)| {
                            let pst_prob = self.pst.predict_symbol(context, symbol);
                            let boost = self
                                .hawkes_intensities
                                .get(&symbol)
                                .map_or(1.0, |h| h.boost_factor(now_ns));
                            (symbol, next_state, pst_prob * boost)
                        })
                        .collect();

                    // Renormalize so modulated transition probabilities sum correctly
                    let total: f64 = raw_probs.iter().map(|(_, _, p)| p).sum();
                    if total > 0.0 {
                        for entry in &mut raw_probs {
                            entry.2 /= total;
                        }
                    }

                    // Also get the original PST total for scaling
                    let pst_total: f64 = transitions
                        .keys()
                        .map(|&sym| self.pst.predict_symbol(context, sym))
                        .sum();

                    let mut state_prob = 0.0;
                    for &(_, next_state, modulated_prob) in &raw_probs {
                        // Scale by pst_total to preserve original probability magnitude
                        state_prob += modulated_prob * pst_total * prob[next_state];
                    }
                    *new_p = state_prob;
                }
            }
            // Early exit if converged
            if prob
                .iter()
                .zip(new_prob.iter())
                .all(|(a, b)| (a - b).abs() < 1e-10)
            {
                break;
            }
            std::mem::swap(&mut prob, &mut new_prob);
        }

        new_prob.get(current_state).copied().unwrap_or(0.0).min(1.0)
    }

    /// Estimate expected waiting time (nanoseconds) to pattern completion
    /// using Monte Carlo simulation on the PMC.
    fn estimate_waiting_time(&self, current_state: usize, context: &[SymbolId]) -> u64 {
        if self.accept_states.contains(&current_state) {
            return 0;
        }

        if self.avg_inter_event_ns <= 0.0 {
            return 0;
        }

        // Simple estimate: remaining states * avg inter-event time
        // Count minimum transitions to accept state via BFS
        let remaining = self.min_transitions_to_accept(current_state);
        if remaining == 0 {
            return 0;
        }

        // Weight by probability of progressing (higher probability = faster)
        let context_prob = if let Some(transitions) = self.nfa_transitions.get(current_state) {
            transitions
                .keys()
                .map(|&sym| self.pst.predict_symbol(context, sym))
                .sum::<f64>()
                .max(0.01)
        } else {
            0.01
        };

        // Expected time = (remaining transitions / progress probability) * avg inter-event
        let expected_events = remaining as f64 / context_prob;
        (expected_events * self.avg_inter_event_ns) as u64
    }

    /// BFS to find minimum transitions from a state to any accept state.
    fn min_transitions_to_accept(&self, start: usize) -> usize {
        if self.accept_states.contains(&start) {
            return 0;
        }

        let mut visited = vec![false; self.num_states];
        let mut queue = std::collections::VecDeque::new();
        queue.push_back((start, 0usize));
        visited[start] = true;

        while let Some((state, depth)) = queue.pop_front() {
            if let Some(transitions) = self.nfa_transitions.get(state) {
                for &next in transitions.values() {
                    if self.accept_states.contains(&next) {
                        return depth + 1;
                    }
                    if next < self.num_states && !visited[next] {
                        visited[next] = true;
                        queue.push_back((next, depth + 1));
                    }
                }
            }
        }

        // Unreachable accept state
        self.num_states
    }

    /// Get the number of events processed.
    pub const fn events_processed(&self) -> u64 {
        self.events_processed
    }
}

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

    fn make_simple_pmc() -> PatternMarkovChain {
        // Simple 3-state NFA: Start --(A)--> S1 --(B)--> Accept
        let event_types = vec!["A".to_string(), "B".to_string()];
        let pst_config = PSTConfig {
            max_depth: 3,
            smoothing: 0.01,
            ..Default::default()
        };
        let pmc_config = PMCConfig {
            warmup_events: 5,
            confidence_threshold: 0.5,
            adaptive_warmup: false,
            ..Default::default()
        };

        let mut transitions = vec![FxHashMap::default(); 3];
        // State 0 (start) -> State 1 on symbol 0 (A)
        transitions[0].insert(0, 1);
        // State 1 -> State 2 (accept) on symbol 1 (B)
        transitions[1].insert(1, 2);

        PatternMarkovChain::new(
            &event_types,
            transitions,
            vec![2], // accept state
            3,
            pst_config,
            pmc_config,
        )
    }

    #[test]
    fn test_deterministic_pattern() {
        let mut pmc = make_simple_pmc();

        // Train with deterministic A -> B pattern
        let runs = vec![RunSnapshot {
            current_state: 1,
            started_at_ns: 0,
        }];

        // Warmup with alternating A, B
        for i in 0..10 {
            let event_type = if i % 2 == 0 { "A" } else { "B" };
            pmc.process(event_type, i * 1_000_000_000, &[]);
        }

        // After warmup, with an active run at state 1, should forecast
        let result = pmc.process("A", 10_000_000_000, &runs);
        assert!(result.is_some(), "Should produce forecast after warmup");
        let forecast = result.unwrap();
        assert!(
            forecast.probability > 0.0,
            "Probability should be > 0, got {}",
            forecast.probability
        );
        // Verify conformal interval fields are present
        assert!(
            forecast.forecast_lower <= forecast.forecast_upper,
            "lower ({}) should be <= upper ({})",
            forecast.forecast_lower,
            forecast.forecast_upper
        );
    }

    #[test]
    fn test_warmup_suppression() {
        let mut pmc = make_simple_pmc();
        let runs = vec![RunSnapshot {
            current_state: 1,
            started_at_ns: 0,
        }];

        // During warmup (< 5 events), should return None
        for i in 0..4 {
            let result = pmc.process("A", i * 1_000_000_000, &runs);
            assert!(
                result.is_none(),
                "Should suppress during warmup (event {i})"
            );
        }

        // After warmup (>= 5 events), should produce forecast
        let result = pmc.process("A", 4_000_000_000, &runs);
        assert!(result.is_some(), "Should produce forecast after warmup");
    }

    #[test]
    fn test_no_active_runs() {
        let mut pmc = make_simple_pmc();

        // Process events without active runs
        for i in 0..10 {
            let result = pmc.process("A", i * 1_000_000_000, &[]);
            assert!(result.is_none(), "No forecast without active runs");
        }
    }

    #[test]
    fn test_horizon_effect() {
        let mut pmc = make_simple_pmc();
        let runs = vec![RunSnapshot {
            current_state: 1,
            started_at_ns: 0,
        }];

        // Warmup
        for i in 0..10 {
            pmc.process("A", i * 1_000_000_000, &[]);
        }

        // Forecast with active run
        let result = pmc.process("A", 10_000_000_000, &runs).unwrap();
        assert!(
            result.expected_time_ns > 0 || result.probability > 0.9,
            "Should have positive expected time or near-certain probability"
        );
    }

    #[test]
    fn test_hawkes_integration_no_panic() {
        let mut pmc = make_simple_pmc();

        // Send a burst of events and verify Hawkes modulation doesn't panic
        let runs = vec![RunSnapshot {
            current_state: 1,
            started_at_ns: 0,
        }];

        // Rapid burst: events 1ms apart
        for i in 0..100 {
            let event_type = if i % 2 == 0 { "A" } else { "B" };
            pmc.process(event_type, i * 1_000_000, &runs);
        }

        // Should still produce valid forecasts after burst
        let result = pmc.process("A", 100_000_000, &runs);
        assert!(result.is_some(), "Should produce forecast after burst");
        let forecast = result.unwrap();
        assert!(
            forecast.probability.is_finite(),
            "Probability should be finite after burst"
        );
        assert!(
            forecast.forecast_lower.is_finite() && forecast.forecast_upper.is_finite(),
            "Conformal bounds should be finite after burst"
        );
    }

    #[test]
    fn test_conformal_fields_populated() {
        let mut pmc = make_simple_pmc();

        let runs = vec![RunSnapshot {
            current_state: 1,
            started_at_ns: 0,
        }];

        // Warmup
        for i in 0..10 {
            let event_type = if i % 2 == 0 { "A" } else { "B" };
            pmc.process(event_type, i * 1_000_000_000, &[]);
        }

        let result = pmc.process("A", 10_000_000_000, &runs).unwrap();

        // Before any outcome data, conformal interval should be (0.0, 1.0)
        assert!(
            result.forecast_lower >= 0.0,
            "forecast_lower should be >= 0.0"
        );
        assert!(
            result.forecast_upper <= 1.0,
            "forecast_upper should be <= 1.0"
        );
        assert!(
            result.forecast_lower <= result.forecast_upper,
            "lower ({}) <= upper ({})",
            result.forecast_lower,
            result.forecast_upper
        );
    }

    #[test]
    fn test_outcome_tracking() {
        let mut pmc = make_simple_pmc();

        // Warmup
        for i in 0..10 {
            let event_type = if i % 2 == 0 { "A" } else { "B" };
            pmc.process(event_type, i * 1_000_000_000, &[]);
        }

        // Process with an active run
        let runs = vec![RunSnapshot {
            current_state: 1,
            started_at_ns: 100,
        }];
        pmc.process("A", 10_000_000_000, &runs);

        // Run disappears (completed — was at accept state 2 equivalent scenario)
        // Simulate by passing empty runs — the run disappeared
        pmc.process("B", 11_000_000_000, &[]);

        // The conformal calibrator should have recorded an outcome
        assert!(
            pmc.conformal.sample_count() > 0 || pmc.pending_forecasts.is_empty(),
            "After run disappears, either outcome recorded or no pending forecast"
        );
    }

    #[test]
    fn test_hawkes_disabled_no_modulation() {
        // PMC with hawkes_enabled=false should produce the same probability regardless of burst
        let event_types = vec!["A".to_string(), "B".to_string()];
        let pst_config = PSTConfig {
            max_depth: 3,
            smoothing: 0.01,
            ..Default::default()
        };
        let pmc_config = PMCConfig {
            warmup_events: 5,
            confidence_threshold: 0.5,
            hawkes_enabled: false,
            adaptive_warmup: false,
            ..Default::default()
        };

        let mut transitions = vec![FxHashMap::default(); 3];
        transitions[0].insert(0, 1);
        transitions[1].insert(1, 2);

        let mut pmc = PatternMarkovChain::new(
            &event_types,
            transitions,
            vec![2],
            3,
            pst_config,
            pmc_config,
        );

        let runs = vec![RunSnapshot {
            current_state: 1,
            started_at_ns: 0,
        }];

        // Warmup with steady events 1s apart
        for i in 0..10 {
            let event_type = if i % 2 == 0 { "A" } else { "B" };
            pmc.process(event_type, i * 1_000_000_000, &[]);
        }

        // Get baseline probability
        let result_steady = pmc.process("A", 10_000_000_000, &runs).unwrap();

        // Now send a rapid burst (1ms apart) — with Hawkes disabled, probability should be similar
        for i in 0..50 {
            let event_type = if i % 2 == 0 { "A" } else { "B" };
            pmc.process(event_type, 11_000_000_000 + i * 1_000_000, &[]);
        }
        let result_burst = pmc.process("A", 11_050_000_000, &runs).unwrap();

        // Without Hawkes, probability should be stable (no burst boost)
        let diff = (result_burst.probability - result_steady.probability).abs();
        assert!(
            diff < 0.3,
            "Without Hawkes, burst should not dramatically change probability: steady={}, burst={}, diff={}",
            result_steady.probability, result_burst.probability, diff
        );

        // Verify hawkes_intensities map is empty when disabled
        assert!(
            pmc.hawkes_intensities.is_empty(),
            "Hawkes intensities should not be populated when disabled"
        );
    }

    #[test]
    fn test_conformal_disabled_full_interval() {
        let event_types = vec!["A".to_string(), "B".to_string()];
        let pst_config = PSTConfig {
            max_depth: 3,
            smoothing: 0.01,
            ..Default::default()
        };
        let pmc_config = PMCConfig {
            warmup_events: 5,
            confidence_threshold: 0.5,
            conformal_enabled: false,
            adaptive_warmup: false,
            ..Default::default()
        };

        let mut transitions = vec![FxHashMap::default(); 3];
        transitions[0].insert(0, 1);
        transitions[1].insert(1, 2);

        let mut pmc = PatternMarkovChain::new(
            &event_types,
            transitions,
            vec![2],
            3,
            pst_config,
            pmc_config,
        );

        let runs = vec![RunSnapshot {
            current_state: 1,
            started_at_ns: 0,
        }];

        // Warmup
        for i in 0..10 {
            let event_type = if i % 2 == 0 { "A" } else { "B" };
            pmc.process(event_type, i * 1_000_000_000, &[]);
        }

        let result = pmc.process("A", 10_000_000_000, &runs).unwrap();

        // With conformal disabled, interval should always be (0.0, 1.0)
        assert!(
            (result.forecast_lower - 0.0).abs() < 1e-10,
            "forecast_lower should be 0.0 when conformal disabled, got {}",
            result.forecast_lower
        );
        assert!(
            (result.forecast_upper - 1.0).abs() < 1e-10,
            "forecast_upper should be 1.0 when conformal disabled, got {}",
            result.forecast_upper
        );

        // pending_forecasts should remain empty
        assert!(
            pmc.pending_forecasts.is_empty(),
            "pending_forecasts should be empty when conformal disabled"
        );
    }
}