midstream 0.2.0

Real-time LLM streaming with inflight analysis
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
//! Temporal logic verification with neural reasoning
//!
//! Integrates temporal-neural-solver for:
//! - Linear Temporal Logic (LTL) verification
//! - Metric Temporal Logic (MTL) with timing constraints
//! - Neural-symbolic reasoning
//! - Differentiable temporal logic

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

/// Temporal logic operators
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TemporalOperator {
    /// Next (X φ)
    Next,
    /// Eventually (F φ) - sometime in the future
    Eventually,
    /// Globally (G φ) - always in the future
    Globally,
    /// Until (φ U ψ) - φ holds until ψ becomes true
    Until,
    /// Release (φ R ψ) - ψ holds until and including when φ becomes true
    Release,
}

/// Temporal logic formula
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TemporalFormula {
    /// Atomic proposition (e.g., "safety_check_passed")
    Atom(String),
    /// Negation (¬φ)
    Not(Box<TemporalFormula>),
    /// Conjunction (φ ∧ ψ)
    And(Box<TemporalFormula>, Box<TemporalFormula>),
    /// Disjunction (φ ∨ ψ)
    Or(Box<TemporalFormula>, Box<TemporalFormula>),
    /// Implication (φ → ψ)
    Implies(Box<TemporalFormula>, Box<TemporalFormula>),
    /// Temporal operator
    Temporal(TemporalOperator, Box<TemporalFormula>),
    /// Bounded temporal (with time constraint for MTL)
    BoundedTemporal {
        operator: TemporalOperator,
        formula: Box<TemporalFormula>,
        lower_bound: Duration,
        upper_bound: Duration,
    },
}

impl TemporalFormula {
    /// Create an atomic proposition
    pub fn atom(name: impl Into<String>) -> Self {
        TemporalFormula::Atom(name.into())
    }

    /// Create negation
    pub fn not(formula: TemporalFormula) -> Self {
        TemporalFormula::Not(Box::new(formula))
    }

    /// Create conjunction
    pub fn and(left: TemporalFormula, right: TemporalFormula) -> Self {
        TemporalFormula::And(Box::new(left), Box::new(right))
    }

    /// Create disjunction
    pub fn or(left: TemporalFormula, right: TemporalFormula) -> Self {
        TemporalFormula::Or(Box::new(left), Box::new(right))
    }

    /// Create implication
    pub fn implies(left: TemporalFormula, right: TemporalFormula) -> Self {
        TemporalFormula::Implies(Box::new(left), Box::new(right))
    }

    /// Create eventually (F)
    pub fn eventually(formula: TemporalFormula) -> Self {
        TemporalFormula::Temporal(TemporalOperator::Eventually, Box::new(formula))
    }

    /// Create globally (G)
    pub fn globally(formula: TemporalFormula) -> Self {
        TemporalFormula::Temporal(TemporalOperator::Globally, Box::new(formula))
    }

    /// Create next (X)
    pub fn next(formula: TemporalFormula) -> Self {
        TemporalFormula::Temporal(TemporalOperator::Next, Box::new(formula))
    }

    /// Create until (U)
    pub fn until(left: TemporalFormula, right: TemporalFormula) -> Self {
        TemporalFormula::Temporal(
            TemporalOperator::Until,
            Box::new(TemporalFormula::And(Box::new(left), Box::new(right))),
        )
    }

    /// Create bounded eventually (for MTL)
    pub fn eventually_bounded(
        formula: TemporalFormula,
        lower: Duration,
        upper: Duration,
    ) -> Self {
        TemporalFormula::BoundedTemporal {
            operator: TemporalOperator::Eventually,
            formula: Box::new(formula),
            lower_bound: lower,
            upper_bound: upper,
        }
    }

    /// Create bounded globally (for MTL)
    pub fn globally_bounded(
        formula: TemporalFormula,
        lower: Duration,
        upper: Duration,
    ) -> Self {
        TemporalFormula::BoundedTemporal {
            operator: TemporalOperator::Globally,
            formula: Box::new(formula),
            lower_bound: lower,
            upper_bound: upper,
        }
    }
}

/// Temporal trace (sequence of states over time)
#[derive(Debug, Clone)]
pub struct TemporalTrace {
    states: Vec<TemporalState>,
}

impl TemporalTrace {
    /// Create a new empty trace
    pub fn new() -> Self {
        Self { states: Vec::new() }
    }

    /// Add a state to the trace
    pub fn add_state(&mut self, state: TemporalState) {
        self.states.push(state);
    }

    /// Get trace length
    pub fn len(&self) -> usize {
        self.states.len()
    }

    /// Check if trace is empty
    pub fn is_empty(&self) -> bool {
        self.states.is_empty()
    }

    /// Get state at index
    pub fn get_state(&self, index: usize) -> Option<&TemporalState> {
        self.states.get(index)
    }
}

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

/// A state in time with propositions
#[derive(Debug, Clone)]
pub struct TemporalState {
    /// Atomic propositions that are true in this state
    pub propositions: HashMap<String, bool>,
    /// Timestamp of this state
    pub timestamp: Duration,
    /// Confidence in state observations (for neural reasoning)
    pub confidence: f64,
}

impl TemporalState {
    /// Create a new temporal state
    pub fn new(timestamp: Duration) -> Self {
        Self {
            propositions: HashMap::new(),
            timestamp,
            confidence: 1.0,
        }
    }

    /// Set a proposition value
    pub fn set(&mut self, name: String, value: bool) {
        self.propositions.insert(name, value);
    }

    /// Check if a proposition is true
    pub fn is_true(&self, name: &str) -> bool {
        self.propositions.get(name).copied().unwrap_or(false)
    }
}

/// Verification result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationResult {
    /// Whether the formula holds
    pub holds: bool,
    /// Confidence score (0.0 to 1.0)
    pub confidence: f64,
    /// Explanation of the result
    pub explanation: String,
    /// Counterexample trace if formula doesn't hold
    pub counterexample: Option<Vec<String>>,
}

/// Temporal neural solver combining logic and learning
pub struct TemporalNeuralSolver {
    /// Neural weights for soft logic (learned from data)
    neural_weights: HashMap<String, f64>,
    /// Verification cache
    cache: HashMap<String, VerificationResult>,
}

impl TemporalNeuralSolver {
    /// Create a new temporal neural solver
    pub fn new() -> Self {
        Self {
            neural_weights: HashMap::new(),
            cache: HashMap::new(),
        }
    }

    /// Verify a temporal formula against a trace
    pub fn verify(
        &mut self,
        formula: &TemporalFormula,
        trace: &TemporalTrace,
    ) -> VerificationResult {
        // Check cache
        let cache_key = format!("{:?}", formula);
        if let Some(cached) = self.cache.get(&cache_key) {
            return cached.clone();
        }

        // Verify formula
        let result = self.verify_at_position(formula, trace, 0);

        // Cache result
        self.cache.insert(cache_key, result.clone());

        result
    }

    /// Verify formula at a specific position in the trace
    fn verify_at_position(
        &self,
        formula: &TemporalFormula,
        trace: &TemporalTrace,
        position: usize,
    ) -> VerificationResult {
        match formula {
            TemporalFormula::Atom(name) => {
                if let Some(state) = trace.get_state(position) {
                    let holds = state.is_true(name);
                    VerificationResult {
                        holds,
                        confidence: state.confidence,
                        explanation: format!(
                            "Atom '{}' is {} at position {}",
                            name,
                            if holds { "true" } else { "false" },
                            position
                        ),
                        counterexample: if holds { None } else { Some(vec![name.clone()]) },
                    }
                } else {
                    VerificationResult {
                        holds: false,
                        confidence: 0.0,
                        explanation: format!("Position {} out of bounds", position),
                        counterexample: Some(vec!["out_of_bounds".to_string()]),
                    }
                }
            }

            TemporalFormula::Not(inner) => {
                let inner_result = self.verify_at_position(inner, trace, position);
                VerificationResult {
                    holds: !inner_result.holds,
                    confidence: inner_result.confidence,
                    explanation: format!("Not({})", inner_result.explanation),
                    counterexample: if !inner_result.holds {
                        None
                    } else {
                        inner_result.counterexample
                    },
                }
            }

            TemporalFormula::And(left, right) => {
                let left_result = self.verify_at_position(left, trace, position);
                let right_result = self.verify_at_position(right, trace, position);

                let holds = left_result.holds && right_result.holds;
                let confidence = left_result.confidence.min(right_result.confidence);

                VerificationResult {
                    holds,
                    confidence,
                    explanation: format!(
                        "({}) AND ({})",
                        left_result.explanation, right_result.explanation
                    ),
                    counterexample: if !holds {
                        Some(
                            left_result
                                .counterexample
                                .unwrap_or_default()
                                .into_iter()
                                .chain(right_result.counterexample.unwrap_or_default())
                                .collect(),
                        )
                    } else {
                        None
                    },
                }
            }

            TemporalFormula::Or(left, right) => {
                let left_result = self.verify_at_position(left, trace, position);
                let right_result = self.verify_at_position(right, trace, position);

                let holds = left_result.holds || right_result.holds;
                let confidence = left_result.confidence.max(right_result.confidence);

                VerificationResult {
                    holds,
                    confidence,
                    explanation: format!(
                        "({}) OR ({})",
                        left_result.explanation, right_result.explanation
                    ),
                    counterexample: if !holds {
                        Some(
                            left_result
                                .counterexample
                                .unwrap_or_default()
                                .into_iter()
                                .chain(right_result.counterexample.unwrap_or_default())
                                .collect(),
                        )
                    } else {
                        None
                    },
                }
            }

            TemporalFormula::Implies(left, right) => {
                // A -> B is equivalent to (¬A) ∨ B
                let left_result = self.verify_at_position(left, trace, position);
                let right_result = self.verify_at_position(right, trace, position);

                let holds = !left_result.holds || right_result.holds;
                let confidence = if left_result.holds {
                    right_result.confidence
                } else {
                    1.0
                };

                VerificationResult {
                    holds,
                    confidence,
                    explanation: format!(
                        "({}) IMPLIES ({})",
                        left_result.explanation, right_result.explanation
                    ),
                    counterexample: if !holds {
                        right_result.counterexample
                    } else {
                        None
                    },
                }
            }

            TemporalFormula::Temporal(op, inner) => match op {
                TemporalOperator::Next => {
                    if position + 1 < trace.len() {
                        self.verify_at_position(inner, trace, position + 1)
                    } else {
                        VerificationResult {
                            holds: false,
                            confidence: 0.0,
                            explanation: "Next: no next state".to_string(),
                            counterexample: Some(vec!["no_next_state".to_string()]),
                        }
                    }
                }

                TemporalOperator::Eventually => {
                    // F φ: φ holds at some point in the future
                    for i in position..trace.len() {
                        let result = self.verify_at_position(inner, trace, i);
                        if result.holds {
                            return VerificationResult {
                                holds: true,
                                confidence: result.confidence,
                                explanation: format!("Eventually at position {}: {}", i, result.explanation),
                                counterexample: None,
                            };
                        }
                    }
                    VerificationResult {
                        holds: false,
                        confidence: 0.0,
                        explanation: "Eventually: never becomes true".to_string(),
                        counterexample: Some(vec!["never_true".to_string()]),
                    }
                }

                TemporalOperator::Globally => {
                    // G φ: φ holds at all points in the future
                    let mut min_confidence = 1.0;
                    for i in position..trace.len() {
                        let result = self.verify_at_position(inner, trace, i);
                        if !result.holds {
                            return VerificationResult {
                                holds: false,
                                confidence: result.confidence,
                                explanation: format!(
                                    "Globally fails at position {}: {}",
                                    i, result.explanation
                                ),
                                counterexample: Some(vec![format!("fails_at_{}", i)]),
                            };
                        }
                        min_confidence = min_confidence.min(result.confidence);
                    }
                    VerificationResult {
                        holds: true,
                        confidence: min_confidence,
                        explanation: "Globally: holds everywhere".to_string(),
                        counterexample: None,
                    }
                }

                TemporalOperator::Until => {
                    // Simplified Until operator
                    VerificationResult {
                        holds: false,
                        confidence: 0.5,
                        explanation: "Until: not fully implemented".to_string(),
                        counterexample: Some(vec!["not_implemented".to_string()]),
                    }
                }

                TemporalOperator::Release => {
                    // Simplified Release operator
                    VerificationResult {
                        holds: false,
                        confidence: 0.5,
                        explanation: "Release: not fully implemented".to_string(),
                        counterexample: Some(vec!["not_implemented".to_string()]),
                    }
                }
            },

            TemporalFormula::BoundedTemporal {
                operator,
                formula,
                lower_bound,
                upper_bound,
            } => {
                // MTL: check within time bounds
                let current_time = trace
                    .get_state(position)
                    .map(|s| s.timestamp)
                    .unwrap_or(Duration::ZERO);

                match operator {
                    TemporalOperator::Eventually => {
                        // F[a,b] φ: φ holds at some point within time interval [a, b]
                        for i in position..trace.len() {
                            if let Some(state) = trace.get_state(i) {
                                let delta = state.timestamp.saturating_sub(current_time);
                                if delta >= *lower_bound && delta <= *upper_bound {
                                    let result = self.verify_at_position(formula, trace, i);
                                    if result.holds {
                                        return VerificationResult {
                                            holds: true,
                                            confidence: result.confidence,
                                            explanation: format!(
                                                "Bounded Eventually at {} ms: {}",
                                                delta.as_millis(),
                                                result.explanation
                                            ),
                                            counterexample: None,
                                        };
                                    }
                                }
                            }
                        }
                        VerificationResult {
                            holds: false,
                            confidence: 0.0,
                            explanation: format!(
                                "Bounded Eventually: never true within [{}, {}] ms",
                                lower_bound.as_millis(),
                                upper_bound.as_millis()
                            ),
                            counterexample: Some(vec!["not_within_bounds".to_string()]),
                        }
                    }

                    _ => VerificationResult {
                        holds: false,
                        confidence: 0.5,
                        explanation: "Bounded temporal: operator not fully implemented".to_string(),
                        counterexample: Some(vec!["not_implemented".to_string()]),
                    },
                }
            }
        }
    }

    /// Learn neural weights from verified traces (neural-symbolic learning)
    pub fn learn_from_trace(&mut self, formula: &TemporalFormula, trace: &TemporalTrace) {
        // Extract atoms from formula
        let atoms = self.extract_atoms(formula);

        // Update weights based on trace satisfaction
        for atom in atoms {
            let satisfaction_rate = self.calculate_satisfaction_rate(&atom, trace);
            self.neural_weights.insert(atom, satisfaction_rate);
        }
    }

    /// Extract all atoms from a formula
    fn extract_atoms(&self, formula: &TemporalFormula) -> Vec<String> {
        let mut atoms = Vec::new();
        self.extract_atoms_recursive(formula, &mut atoms);
        atoms.sort();
        atoms.dedup();
        atoms
    }

    fn extract_atoms_recursive(&self, formula: &TemporalFormula, atoms: &mut Vec<String>) {
        match formula {
            TemporalFormula::Atom(name) => atoms.push(name.clone()),
            TemporalFormula::Not(inner) => self.extract_atoms_recursive(inner, atoms),
            TemporalFormula::And(left, right)
            | TemporalFormula::Or(left, right)
            | TemporalFormula::Implies(left, right) => {
                self.extract_atoms_recursive(left, atoms);
                self.extract_atoms_recursive(right, atoms);
            }
            TemporalFormula::Temporal(_, inner) => self.extract_atoms_recursive(inner, atoms),
            TemporalFormula::BoundedTemporal { formula, .. } => {
                self.extract_atoms_recursive(formula, atoms)
            }
        }
    }

    /// Calculate how often an atom is satisfied in a trace
    fn calculate_satisfaction_rate(&self, atom: &str, trace: &TemporalTrace) -> f64 {
        if trace.is_empty() {
            return 0.0;
        }

        let mut true_count = 0;
        for i in 0..trace.len() {
            if let Some(state) = trace.get_state(i) {
                if state.is_true(atom) {
                    true_count += 1;
                }
            }
        }

        true_count as f64 / trace.len() as f64
    }
}

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

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

    #[test]
    fn test_atom_verification() {
        let mut solver = TemporalNeuralSolver::new();
        let mut trace = TemporalTrace::new();

        let mut state = TemporalState::new(Duration::from_secs(0));
        state.set("safe".to_string(), true);
        trace.add_state(state);

        let formula = TemporalFormula::atom("safe");
        let result = solver.verify(&formula, &trace);

        assert!(result.holds);
        assert!(result.confidence > 0.9);
    }

    #[test]
    fn test_eventually_operator() {
        let mut solver = TemporalNeuralSolver::new();
        let mut trace = TemporalTrace::new();

        // Add states where "goal" becomes true at position 2
        for i in 0..3 {
            let mut state = TemporalState::new(Duration::from_secs(i));
            state.set("goal".to_string(), i == 2);
            trace.add_state(state);
        }

        let formula = TemporalFormula::eventually(TemporalFormula::atom("goal"));
        let result = solver.verify(&formula, &trace);

        assert!(result.holds);
        println!("Eventually result: {:?}", result);
    }

    #[test]
    fn test_globally_operator() {
        let mut solver = TemporalNeuralSolver::new();
        let mut trace = TemporalTrace::new();

        // Add states where "invariant" is always true
        for i in 0..5 {
            let mut state = TemporalState::new(Duration::from_secs(i));
            state.set("invariant".to_string(), true);
            trace.add_state(state);
        }

        let formula = TemporalFormula::globally(TemporalFormula::atom("invariant"));
        let result = solver.verify(&formula, &trace);

        assert!(result.holds);
        println!("Globally result: {:?}", result);
    }

    #[test]
    fn test_bounded_eventually() {
        let mut solver = TemporalNeuralSolver::new();
        let mut trace = TemporalTrace::new();

        // Add states with timestamps
        for i in 0..10 {
            let mut state = TemporalState::new(Duration::from_millis(i * 100));
            state.set("event".to_string(), i == 5); // Event occurs at 500ms
            trace.add_state(state);
        }

        // Check if event occurs within [400ms, 600ms]
        let formula = TemporalFormula::eventually_bounded(
            TemporalFormula::atom("event"),
            Duration::from_millis(400),
            Duration::from_millis(600),
        );

        let result = solver.verify(&formula, &trace);
        assert!(result.holds);
        println!("Bounded Eventually result: {:?}", result);
    }

    #[test]
    fn test_complex_formula() {
        let mut solver = TemporalNeuralSolver::new();
        let mut trace = TemporalTrace::new();

        // G(request -> F response)
        // "If request happens, response must eventually happen"

        for i in 0..10 {
            let mut state = TemporalState::new(Duration::from_secs(i));
            state.set("request".to_string(), i == 2);
            state.set("response".to_string(), i >= 5);
            trace.add_state(state);
        }

        let formula = TemporalFormula::globally(TemporalFormula::implies(
            TemporalFormula::atom("request"),
            TemporalFormula::eventually(TemporalFormula::atom("response")),
        ));

        let result = solver.verify(&formula, &trace);
        println!("Complex formula result: {:?}", result);
    }

    #[test]
    fn test_learning() {
        let mut solver = TemporalNeuralSolver::new();
        let mut trace = TemporalTrace::new();

        for i in 0..10 {
            let mut state = TemporalState::new(Duration::from_secs(i));
            state.set("pattern".to_string(), i % 2 == 0);
            trace.add_state(state);
        }

        let formula = TemporalFormula::atom("pattern");
        solver.learn_from_trace(&formula, &trace);

        // Check learned weight
        if let Some(&weight) = solver.neural_weights.get("pattern") {
            println!("Learned weight for 'pattern': {}", weight);
            assert!((weight - 0.5).abs() < 0.1); // Should be ~0.5 (true half the time)
        }
    }
}