ftui-runtime 0.3.1

Elm-style runtime loop and subscriptions for FrankenTUI.
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
#![forbid(unsafe_code)]

//! Evidence telemetry snapshots for runtime explainability overlays.
//!
//! These snapshots provide a low-overhead, in-memory view of the most recent
//! diff, resize, and budget decisions so demo screens can render cockpit
//! views without parsing JSONL logs.

use std::sync::{LazyLock, RwLock};

#[cfg(test)]
use std::sync::Mutex;

use ftui_render::budget::{BudgetDecision, DegradationLevel};
use ftui_render::diff_strategy::{DiffStrategy, StrategyEvidence};

use crate::bocpd::BocpdEvidence;
use crate::resize_coalescer::Regime;

/// Snapshot of the most recent diff-strategy decision.
#[derive(Debug, Clone)]
pub struct DiffDecisionSnapshot {
    pub event_idx: u64,
    pub screen_mode: String,
    pub cols: u16,
    pub rows: u16,
    pub evidence: StrategyEvidence,
    pub span_count: usize,
    pub span_coverage_pct: f64,
    pub max_span_len: usize,
    pub scan_cost_estimate: usize,
    pub fallback_reason: String,
    pub tile_used: bool,
    pub tile_fallback: String,
    pub strategy_used: DiffStrategy,
}

/// Snapshot of the most recent resize/coalescer decision.
#[derive(Debug, Clone)]
pub struct ResizeDecisionSnapshot {
    pub event_idx: u64,
    pub action: &'static str,
    pub dt_ms: f64,
    pub event_rate: f64,
    pub regime: Regime,
    pub pending_size: Option<(u16, u16)>,
    pub applied_size: Option<(u16, u16)>,
    pub time_since_render_ms: f64,
    pub bocpd: Option<BocpdEvidence>,
}

/// Conformal evidence snapshot for budget decisions.
#[derive(Debug, Clone)]
pub struct ConformalSnapshot {
    pub bucket_key: String,
    pub sample_count: usize,
    pub upper_us: f64,
    pub risk: bool,
}

/// Snapshot of the most recent budget decision.
#[derive(Debug, Clone)]
pub struct BudgetDecisionSnapshot {
    pub frame_idx: u64,
    pub decision: BudgetDecision,
    pub controller_decision: BudgetDecision,
    pub degradation_before: DegradationLevel,
    pub degradation_after: DegradationLevel,
    pub frame_time_us: f64,
    pub budget_us: f64,
    pub pid_output: f64,
    pub e_value: f64,
    pub frames_observed: u32,
    pub frames_since_change: u32,
    pub in_warmup: bool,
    pub conformal: Option<ConformalSnapshot>,
}

static DIFF_SNAPSHOT: LazyLock<RwLock<Option<DiffDecisionSnapshot>>> =
    LazyLock::new(|| RwLock::new(None));
static RESIZE_SNAPSHOT: LazyLock<RwLock<Option<ResizeDecisionSnapshot>>> =
    LazyLock::new(|| RwLock::new(None));
static BUDGET_SNAPSHOT: LazyLock<RwLock<Option<BudgetDecisionSnapshot>>> =
    LazyLock::new(|| RwLock::new(None));

// Global snapshot telemetry is shared state. In tests, we serialize snapshot
// access to avoid flakiness under parallel test execution.
#[cfg(test)]
static TEST_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

/// Store the latest diff decision snapshot.
pub fn set_diff_snapshot(snapshot: Option<DiffDecisionSnapshot>) {
    #[cfg(test)]
    let _lock = TEST_LOCK.lock().expect("test lock poisoned");

    if let Ok(mut guard) = DIFF_SNAPSHOT.write() {
        *guard = snapshot;
    }
}

/// Fetch the latest diff decision snapshot.
#[must_use]
pub fn diff_snapshot() -> Option<DiffDecisionSnapshot> {
    #[cfg(test)]
    let _lock = TEST_LOCK.lock().expect("test lock poisoned");

    DIFF_SNAPSHOT.read().ok().and_then(|guard| guard.clone())
}

/// Clear any stored diff snapshot.
pub fn clear_diff_snapshot() {
    set_diff_snapshot(None);
}

/// Store the latest resize decision snapshot.
pub fn set_resize_snapshot(snapshot: Option<ResizeDecisionSnapshot>) {
    #[cfg(test)]
    let _lock = TEST_LOCK.lock().expect("test lock poisoned");

    if let Ok(mut guard) = RESIZE_SNAPSHOT.write() {
        *guard = snapshot;
    }
}

/// Fetch the latest resize decision snapshot.
#[must_use]
pub fn resize_snapshot() -> Option<ResizeDecisionSnapshot> {
    #[cfg(test)]
    let _lock = TEST_LOCK.lock().expect("test lock poisoned");

    RESIZE_SNAPSHOT.read().ok().and_then(|guard| guard.clone())
}

/// Clear any stored resize snapshot.
pub fn clear_resize_snapshot() {
    set_resize_snapshot(None);
}

/// Store the latest budget decision snapshot.
pub fn set_budget_snapshot(snapshot: Option<BudgetDecisionSnapshot>) {
    #[cfg(test)]
    let _lock = TEST_LOCK.lock().expect("test lock poisoned");

    if let Ok(mut guard) = BUDGET_SNAPSHOT.write() {
        *guard = snapshot;
    }
}

/// Fetch the latest budget decision snapshot.
#[must_use]
pub fn budget_snapshot() -> Option<BudgetDecisionSnapshot> {
    #[cfg(test)]
    let _lock = TEST_LOCK.lock().expect("test lock poisoned");

    BUDGET_SNAPSHOT.read().ok().and_then(|guard| guard.clone())
}

/// Clear any stored budget snapshot.
pub fn clear_budget_snapshot() {
    set_budget_snapshot(None);
}

#[cfg(test)]
mod tests {
    use super::*;
    use ftui_render::budget::{BudgetDecision, DegradationLevel};
    use ftui_render::diff_strategy::{DiffStrategy, StrategyEvidence};

    use crate::bocpd::{BocpdEvidence, BocpdRegime};

    // ── helpers ──────────────────────────────────────────────────────

    fn make_diff_snapshot(event_idx: u64) -> DiffDecisionSnapshot {
        DiffDecisionSnapshot {
            event_idx,
            screen_mode: "alt".into(),
            cols: 80,
            rows: 24,
            evidence: StrategyEvidence {
                strategy: DiffStrategy::DirtyRows,
                cost_full: 1.0,
                cost_dirty: 0.5,
                cost_redraw: 2.0,
                posterior_mean: 0.05,
                posterior_variance: 0.001,
                alpha: 2.0,
                beta: 38.0,
                dirty_rows: 3,
                total_rows: 24,
                total_cells: 1920,
                guard_reason: "none",
                hysteresis_applied: false,
                hysteresis_ratio: 0.05,
            },
            span_count: 2,
            span_coverage_pct: 6.25,
            max_span_len: 12,
            scan_cost_estimate: 200,
            fallback_reason: "none".into(),
            tile_used: false,
            tile_fallback: String::new(),
            strategy_used: DiffStrategy::DirtyRows,
        }
    }

    fn make_resize_snapshot(event_idx: u64) -> ResizeDecisionSnapshot {
        ResizeDecisionSnapshot {
            event_idx,
            action: "apply",
            dt_ms: 150.0,
            event_rate: 5.0,
            regime: Regime::Steady,
            pending_size: None,
            applied_size: Some((120, 40)),
            time_since_render_ms: 100.0,
            bocpd: None,
        }
    }

    fn make_budget_snapshot(frame_idx: u64) -> BudgetDecisionSnapshot {
        BudgetDecisionSnapshot {
            frame_idx,
            decision: BudgetDecision::Hold,
            controller_decision: BudgetDecision::Hold,
            degradation_before: DegradationLevel::Full,
            degradation_after: DegradationLevel::Full,
            frame_time_us: 8000.0,
            budget_us: 16000.0,
            pid_output: 0.1,
            e_value: 0.5,
            frames_observed: 100,
            frames_since_change: 50,
            in_warmup: false,
            conformal: None,
        }
    }

    // ── diff snapshot tests ─────────────────────────────────────────

    #[test]
    fn diff_snapshot_initially_none() {
        let mut guard = DIFF_SNAPSHOT.write().expect("diff snapshot lock poisoned");
        *guard = None;
        assert!(guard.is_none());
    }

    #[test]
    fn diff_snapshot_store_and_retrieve() {
        let snap = make_diff_snapshot(42);
        let mut guard = DIFF_SNAPSHOT.write().expect("diff snapshot lock poisoned");
        *guard = Some(snap);
        let retrieved = guard.clone().expect("should be Some");
        assert_eq!(retrieved.event_idx, 42);
        assert_eq!(retrieved.cols, 80);
        assert_eq!(retrieved.rows, 24);
        *guard = None;
    }

    #[test]
    fn diff_snapshot_overwrite() {
        let mut guard = DIFF_SNAPSHOT.write().expect("diff snapshot lock poisoned");
        *guard = Some(make_diff_snapshot(1));
        *guard = Some(make_diff_snapshot(2));
        let snap = guard.clone().expect("should be Some");
        assert_eq!(snap.event_idx, 2);
        *guard = None;
    }

    #[test]
    fn diff_snapshot_clear() {
        let mut guard = DIFF_SNAPSHOT.write().expect("diff snapshot lock poisoned");
        *guard = Some(make_diff_snapshot(10));
        *guard = None;
        assert!(guard.is_none());
    }

    #[test]
    fn diff_snapshot_preserves_evidence_fields() {
        let snap = make_diff_snapshot(7);
        let mut guard = DIFF_SNAPSHOT.write().expect("diff snapshot lock poisoned");
        *guard = Some(snap);
        let retrieved = guard.clone().unwrap();
        assert_eq!(retrieved.evidence.strategy, DiffStrategy::DirtyRows);
        assert!((retrieved.evidence.cost_full - 1.0).abs() < f64::EPSILON);
        assert!((retrieved.evidence.posterior_mean - 0.05).abs() < f64::EPSILON);
        assert_eq!(retrieved.span_count, 2);
        assert_eq!(retrieved.strategy_used, DiffStrategy::DirtyRows);
        *guard = None;
    }

    // ── resize snapshot tests ───────────────────────────────────────

    #[test]
    fn resize_snapshot_initially_none() {
        let mut guard = RESIZE_SNAPSHOT
            .write()
            .expect("resize snapshot lock poisoned");
        *guard = None;
        assert!(guard.is_none());
    }

    #[test]
    fn resize_snapshot_store_and_retrieve() {
        let snap = make_resize_snapshot(5);
        let mut guard = RESIZE_SNAPSHOT
            .write()
            .expect("resize snapshot lock poisoned");
        *guard = Some(snap);
        let retrieved = guard.clone().expect("should be Some");
        assert_eq!(retrieved.event_idx, 5);
        assert_eq!(retrieved.action, "apply");
        assert_eq!(retrieved.regime, Regime::Steady);
        assert_eq!(retrieved.applied_size, Some((120, 40)));
        *guard = None;
    }

    #[test]
    fn resize_snapshot_overwrite() {
        let mut guard = RESIZE_SNAPSHOT
            .write()
            .expect("resize snapshot lock poisoned");
        *guard = Some(make_resize_snapshot(1));
        *guard = Some(make_resize_snapshot(2));
        let snap = guard.clone().unwrap();
        assert_eq!(snap.event_idx, 2);
        *guard = None;
    }

    #[test]
    fn resize_snapshot_clear() {
        let mut guard = RESIZE_SNAPSHOT
            .write()
            .expect("resize snapshot lock poisoned");
        *guard = Some(make_resize_snapshot(10));
        *guard = None;
        assert!(guard.is_none());
    }

    #[test]
    fn resize_snapshot_with_bocpd_evidence() {
        let mut snap = make_resize_snapshot(3);
        snap.regime = Regime::Burst;
        snap.bocpd = Some(BocpdEvidence {
            p_burst: 0.85,
            log_bayes_factor: 1.5,
            observation_ms: 15.0,
            regime: BocpdRegime::Burst,
            likelihood_steady: 0.001,
            likelihood_burst: 0.05,
            expected_run_length: 3.0,
            run_length_variance: 2.0,
            run_length_mode: 2,
            run_length_p95: 8,
            run_length_tail_mass: 0.01,
            recommended_delay_ms: Some(20),
            hard_deadline_forced: None,
            observation_count: 50,
            timestamp: web_time::Instant::now(),
        });
        let mut guard = RESIZE_SNAPSHOT
            .write()
            .expect("resize snapshot lock poisoned");
        *guard = Some(snap);
        let retrieved = guard.clone().unwrap();
        assert_eq!(retrieved.regime, Regime::Burst);
        let bocpd = retrieved.bocpd.as_ref().unwrap();
        assert!((bocpd.p_burst - 0.85).abs() < f64::EPSILON);
        assert_eq!(bocpd.regime, BocpdRegime::Burst);
        *guard = None;
    }

    // ── budget snapshot tests ───────────────────────────────────────

    #[test]
    fn budget_snapshot_clear_then_none() {
        let mut guard = BUDGET_SNAPSHOT
            .write()
            .expect("budget snapshot lock poisoned");
        *guard = None;
        assert!(guard.is_none());
    }

    #[test]
    fn budget_snapshot_store_and_retrieve() {
        let snap = make_budget_snapshot(100);
        let mut guard = BUDGET_SNAPSHOT
            .write()
            .expect("budget snapshot lock poisoned");
        *guard = Some(snap);
        let retrieved = guard.clone().expect("should be Some");
        assert_eq!(retrieved.frame_idx, 100);
        assert_eq!(retrieved.decision, BudgetDecision::Hold);
        assert_eq!(retrieved.degradation_before, DegradationLevel::Full);
        assert_eq!(retrieved.frames_observed, 100);
        *guard = None;
    }

    #[test]
    fn budget_snapshot_overwrite() {
        let mut guard = BUDGET_SNAPSHOT
            .write()
            .expect("budget snapshot lock poisoned");
        *guard = Some(make_budget_snapshot(1));
        *guard = Some(make_budget_snapshot(2));
        let snap = guard.clone().unwrap();
        assert_eq!(snap.frame_idx, 2);
        *guard = None;
    }

    #[test]
    fn budget_snapshot_clear() {
        let mut guard = BUDGET_SNAPSHOT
            .write()
            .expect("budget snapshot lock poisoned");
        *guard = Some(make_budget_snapshot(10));
        *guard = None;
        assert!(guard.is_none());
    }

    #[test]
    fn budget_snapshot_with_conformal() {
        let mut snap = make_budget_snapshot(50);
        snap.decision = BudgetDecision::Degrade;
        snap.conformal = Some(ConformalSnapshot {
            bucket_key: "alt:DirtyRows:medium".into(),
            sample_count: 30,
            upper_us: 20000.0,
            risk: true,
        });
        let mut guard = BUDGET_SNAPSHOT
            .write()
            .expect("budget snapshot lock poisoned");
        *guard = Some(snap);
        let retrieved = guard.clone().unwrap();
        assert_eq!(retrieved.decision, BudgetDecision::Degrade);
        let conformal = retrieved.conformal.as_ref().unwrap();
        assert_eq!(conformal.bucket_key, "alt:DirtyRows:medium");
        assert_eq!(conformal.sample_count, 30);
        assert!(conformal.risk);
        *guard = None;
    }

    #[test]
    fn budget_snapshot_degradation_levels() {
        let mut snap = make_budget_snapshot(1);
        snap.degradation_before = DegradationLevel::Full;
        snap.degradation_after = DegradationLevel::SimpleBorders;
        snap.decision = BudgetDecision::Degrade;
        let mut guard = BUDGET_SNAPSHOT
            .write()
            .expect("budget snapshot lock poisoned");
        *guard = Some(snap);
        let retrieved = guard.clone().unwrap();
        assert!(retrieved.degradation_after > retrieved.degradation_before);
        *guard = None;
    }

    #[test]
    fn budget_snapshot_warmup_flag() {
        let mut snap = make_budget_snapshot(1);
        snap.in_warmup = true;
        snap.frames_observed = 5;
        let mut guard = BUDGET_SNAPSHOT
            .write()
            .expect("budget snapshot lock poisoned");
        *guard = Some(snap);
        let retrieved = guard.clone().unwrap();
        assert!(retrieved.in_warmup);
        assert_eq!(retrieved.frames_observed, 5);
        *guard = None;
    }

    // ── set_*_snapshot(None) tests ──────────────────────────────────

    #[test]
    fn set_diff_none_clears() {
        let mut guard = DIFF_SNAPSHOT.write().expect("diff snapshot lock poisoned");
        *guard = Some(make_diff_snapshot(1));
        *guard = None;
        assert!(guard.is_none());
    }

    #[test]
    fn set_resize_none_clears() {
        let mut guard = RESIZE_SNAPSHOT
            .write()
            .expect("resize snapshot lock poisoned");
        *guard = Some(make_resize_snapshot(1));
        *guard = None;
        assert!(guard.is_none());
    }

    #[test]
    fn set_budget_none_clears() {
        let mut guard = BUDGET_SNAPSHOT
            .write()
            .expect("budget snapshot lock poisoned");
        *guard = Some(make_budget_snapshot(1));
        *guard = None;
        assert!(guard.is_none());
    }
}