ftui-runtime 0.4.0

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
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
#![forbid(unsafe_code)]

//! bd-382tk.1: E2E integration test for Recipe F Policy-as-Data controllers.
//!
//! Validates:
//! 1. Policy artifact creation and loading into the registry.
//! 2. Shadow-run: execute loaded vs baked-in policy decisions in parallel.
//! 3. Progressive delivery stages (shadow → canary → ramp → default).
//! 4. Fallback on invalid/corrupt policy (validation failure).
//! 5. Seamless recovery (fallback triggers within one decision cycle).
//! 6. JSONL structured logging for every policy decision.
//!
//! Run:
//!   cargo test -p ftui-runtime --test policy_e2e

use std::sync::Arc;

use ftui_runtime::policy_config::PolicyConfig;
use ftui_runtime::policy_registry::{PolicyRegistry, PolicyRegistryError, STANDARD_POLICY};

// ============================================================================
// JSONL Log Entry
// ============================================================================

#[derive(serde::Serialize)]
struct PolicyDecisionLog {
    event: &'static str,
    frame_id: u64,
    policy_source: String,
    delivery_stage: String,
    decision_point: String,
    loaded_action: String,
    baked_in_action: String,
    divergence: bool,
    divergence_reason: Option<String>,
    policy_version: String,
    signature_valid: bool,
    fallback_triggered: bool,
}

// ============================================================================
// Decision Simulation
// ============================================================================

/// Simulate a policy decision by comparing loaded vs baked-in config values.
/// Returns (loaded_action, baked_in_action, divergence).
fn simulate_decision(
    loaded: &PolicyConfig,
    baked_in: &PolicyConfig,
    decision_point: &str,
) -> (String, String, bool) {
    match decision_point {
        "diff_strategy" => {
            let loaded_alpha = loaded.conformal.alpha;
            let baked_in_alpha = baked_in.conformal.alpha;
            let loaded_action = if loaded_alpha < 0.03 {
                "aggressive_diff"
            } else {
                "conservative_diff"
            };
            let baked_in_action = if baked_in_alpha < 0.03 {
                "aggressive_diff"
            } else {
                "conservative_diff"
            };
            (
                loaded_action.to_string(),
                baked_in_action.to_string(),
                loaded_action != baked_in_action,
            )
        }
        "resize_coalesce" => {
            let loaded_kp = loaded.pid.kp;
            let baked_in_kp = baked_in.pid.kp;
            let loaded_action = if loaded_kp > 0.7 {
                "fast_coalesce"
            } else {
                "slow_coalesce"
            };
            let baked_in_action = if baked_in_kp > 0.7 {
                "fast_coalesce"
            } else {
                "slow_coalesce"
            };
            (
                loaded_action.to_string(),
                baked_in_action.to_string(),
                loaded_action != baked_in_action,
            )
        }
        "degradation" => {
            let loaded_threshold = loaded.cascade.recovery_threshold;
            let baked_in_threshold = baked_in.cascade.recovery_threshold;
            let loaded_action = if loaded_threshold > 15 {
                "strict_recovery"
            } else {
                "relaxed_recovery"
            };
            let baked_in_action = if baked_in_threshold > 15 {
                "strict_recovery"
            } else {
                "relaxed_recovery"
            };
            (
                loaded_action.to_string(),
                baked_in_action.to_string(),
                loaded_action != baked_in_action,
            )
        }
        _ => ("unknown".to_string(), "unknown".to_string(), false),
    }
}

/// Which policy's decision to use based on delivery stage and frame_id.
fn select_action(
    delivery_stage: &str,
    frame_id: u64,
    loaded_action: &str,
    baked_in_action: &str,
) -> String {
    match delivery_stage {
        "shadow" => baked_in_action.to_string(), // 100% baked-in
        "canary" => {
            // 10% loaded, 90% baked-in (deterministic based on frame_id)
            if frame_id.is_multiple_of(10) {
                loaded_action.to_string()
            } else {
                baked_in_action.to_string()
            }
        }
        "ramp" => {
            // 50/50
            if frame_id.is_multiple_of(2) {
                loaded_action.to_string()
            } else {
                baked_in_action.to_string()
            }
        }
        "default" => loaded_action.to_string(), // 100% loaded
        _ => baked_in_action.to_string(),
    }
}

// ============================================================================
// Test: Full E2E Progressive Delivery
// ============================================================================

#[test]
fn e2e_progressive_delivery_with_shadow_run() {
    let registry = Arc::new(PolicyRegistry::new());
    let baked_in = PolicyConfig::default();

    // Step 1: Create a custom policy artifact with different tuning.
    let mut loaded_policy = PolicyConfig::default();
    loaded_policy.conformal.alpha = 0.01; // aggressive → diverges on diff_strategy
    loaded_policy.pid.kp = 0.8; // fast → diverges on resize_coalesce
    loaded_policy.cascade.recovery_threshold = 20; // strict → diverges on degradation

    registry.register("loaded", loaded_policy.clone()).unwrap();

    let decision_points = ["diff_strategy", "resize_coalesce", "degradation"];
    let stages = ["shadow", "canary", "ramp", "default"];
    let frames_per_stage = 20u64;

    let mut logs = Vec::new();
    let mut frame_id = 0u64;

    for &stage in &stages {
        // Switch to loaded policy for all stages except shadow
        // (in shadow mode we still use baked-in but log loaded decisions)
        match stage {
            "shadow" => {
                // Active stays "standard"
                assert_eq!(registry.active_name(), STANDARD_POLICY);
            }
            "canary" | "ramp" | "default" => {
                let _ = registry.set_active("loaded");
                assert_eq!(registry.active_name(), "loaded");
            }
            _ => unreachable!(),
        }

        for _ in 0..frames_per_stage {
            for &dp in &decision_points {
                let (loaded_action, baked_in_action, divergence) =
                    simulate_decision(&loaded_policy, &baked_in, dp);

                let selected = select_action(stage, frame_id, &loaded_action, &baked_in_action);

                let log = PolicyDecisionLog {
                    event: "recipe_f_policy",
                    frame_id,
                    policy_source: if selected == loaded_action {
                        "loaded".to_string()
                    } else {
                        "baked_in".to_string()
                    },
                    delivery_stage: stage.to_string(),
                    decision_point: dp.to_string(),
                    loaded_action: loaded_action.clone(),
                    baked_in_action: baked_in_action.clone(),
                    divergence,
                    divergence_reason: if divergence {
                        Some(format!(
                            "{dp}: loaded={loaded_action}, baked_in={baked_in_action}"
                        ))
                    } else {
                        None
                    },
                    policy_version: "loaded-v1".to_string(),
                    signature_valid: true,
                    fallback_triggered: false,
                };

                logs.push(log);
            }
            frame_id += 1;
        }
    }

    // Verify JSONL compliance.
    for log in &logs {
        let json = serde_json::to_string(log).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["event"], "recipe_f_policy");
        assert!(parsed["frame_id"].is_u64());
        assert!(parsed["divergence"].is_boolean());
    }

    // Verify shadow mode: all decisions use baked_in.
    let shadow_logs: Vec<_> = logs
        .iter()
        .filter(|l| l.delivery_stage == "shadow")
        .collect();
    assert_eq!(shadow_logs.len(), frames_per_stage as usize * 3);
    for log in &shadow_logs {
        assert_eq!(
            log.policy_source, "baked_in",
            "shadow mode must always use baked_in"
        );
    }

    // Verify shadow mode logs divergences.
    let shadow_divergences: Vec<_> = shadow_logs.iter().filter(|l| l.divergence).collect();
    assert!(
        !shadow_divergences.is_empty(),
        "shadow mode should detect divergences with different policy"
    );

    // Verify canary mode: 10% loaded decisions.
    let canary_logs: Vec<_> = logs
        .iter()
        .filter(|l| l.delivery_stage == "canary")
        .collect();
    let canary_loaded: Vec<_> = canary_logs
        .iter()
        .filter(|l| l.policy_source == "loaded")
        .collect();
    // Should be roughly 10% (frame_id.is_multiple_of(10))
    assert!(
        !canary_loaded.is_empty(),
        "canary mode should use some loaded decisions"
    );
    assert!(
        canary_loaded.len() < canary_logs.len(),
        "canary mode should not use loaded for all decisions"
    );

    // Verify default mode: all decisions use loaded.
    let default_logs: Vec<_> = logs
        .iter()
        .filter(|l| l.delivery_stage == "default")
        .collect();
    for log in &default_logs {
        assert_eq!(
            log.policy_source, "loaded",
            "default mode must always use loaded"
        );
    }

    // Verify progressive order.
    let stage_first_frame: Vec<(&str, u64)> = stages
        .iter()
        .map(|&s| {
            let first = logs
                .iter()
                .find(|l| l.delivery_stage == s)
                .unwrap()
                .frame_id;
            (s, first)
        })
        .collect();
    for w in stage_first_frame.windows(2) {
        assert!(
            w[0].1 < w[1].1,
            "stages must execute in order: {} (frame {}) before {} (frame {})",
            w[0].0,
            w[0].1,
            w[1].0,
            w[1].1,
        );
    }

    let total = logs.len();
    eprintln!("--- e2e_progressive_delivery summary ---");
    eprintln!("total_decisions: {total}");
    eprintln!("shadow_divergences: {}", shadow_divergences.len());
    eprintln!(
        "canary_loaded_pct: {:.1}%",
        canary_loaded.len() as f64 / canary_logs.len() as f64 * 100.0
    );
    eprintln!(
        "stages: {:?}",
        stage_first_frame
            .iter()
            .map(|(s, f)| format!("{s}@{f}"))
            .collect::<Vec<_>>()
    );
}

// ============================================================================
// Test: Fallback on Invalid Policy
// ============================================================================

#[test]
fn e2e_fallback_on_invalid_policy() {
    let registry = PolicyRegistry::new();
    let baked_in = PolicyConfig::default();

    // Register a valid loaded policy and activate it.
    let mut loaded_policy = PolicyConfig::default();
    loaded_policy.conformal.alpha = 0.01;
    registry.register("loaded", loaded_policy.clone()).unwrap();
    registry.set_active("loaded").unwrap();
    assert_eq!(registry.active_name(), "loaded");

    // Simulate normal operation for a few frames.
    let mut logs = Vec::new();
    for frame_id in 0..5u64 {
        let (loaded_action, baked_in_action, divergence) =
            simulate_decision(&loaded_policy, &baked_in, "diff_strategy");
        logs.push(PolicyDecisionLog {
            event: "recipe_f_policy",
            frame_id,
            policy_source: "loaded".to_string(),
            delivery_stage: "default".to_string(),
            decision_point: "diff_strategy".to_string(),
            loaded_action,
            baked_in_action,
            divergence,
            divergence_reason: None,
            policy_version: "loaded-v1".to_string(),
            signature_valid: true,
            fallback_triggered: false,
        });
    }

    // Step 7: Inject verification failure — try to register a corrupt policy.
    let mut corrupt = PolicyConfig::default();
    corrupt.conformal.alpha = 0.0; // Invalid: alpha must be in (0, 1)
    let err = registry.register("corrupt", corrupt);
    assert!(
        matches!(err, Err(PolicyRegistryError::ValidationFailed(_))),
        "corrupt policy must be rejected"
    );

    // Step 8: Verify immediate fallback — switch back to standard.
    registry.set_active(STANDARD_POLICY).unwrap();
    assert_eq!(registry.active_name(), STANDARD_POLICY);

    // Step 9: Verify fallback is seamless — next decision uses baked-in.
    let active = registry.active_config();
    let (loaded_action, baked_in_action, divergence) =
        simulate_decision(&active, &baked_in, "diff_strategy");
    assert!(
        !divergence,
        "after fallback, active policy must match baked-in"
    );

    logs.push(PolicyDecisionLog {
        event: "recipe_f_policy",
        frame_id: 5,
        policy_source: "baked_in".to_string(),
        delivery_stage: "fallback".to_string(),
        decision_point: "diff_strategy".to_string(),
        loaded_action,
        baked_in_action,
        divergence,
        divergence_reason: None,
        policy_version: "standard".to_string(),
        signature_valid: false,
        fallback_triggered: true,
    });

    // Verify JSONL.
    for log in &logs {
        let json = serde_json::to_string(log).unwrap();
        let _: serde_json::Value = serde_json::from_str(&json).unwrap();
    }

    // Verify fallback log entry.
    let fallback_log = logs.last().unwrap();
    assert!(fallback_log.fallback_triggered);
    assert!(!fallback_log.signature_valid);
    assert_eq!(fallback_log.delivery_stage, "fallback");

    eprintln!("--- e2e_fallback_on_invalid_policy ---");
    eprintln!("normal_frames: 5, fallback_frame: 1");
    eprintln!("corrupt_rejected: true, fallback_triggered: true");
}

// ============================================================================
// Test: Policy Switch Evidence Ledger
// ============================================================================

#[test]
fn e2e_switch_evidence_ledger() {
    let registry = PolicyRegistry::new();

    // Register multiple policies.
    let policies = [
        ("aggressive", 0.01f64),
        ("moderate", 0.03),
        ("relaxed", 0.08),
    ];
    for &(name, alpha) in &policies {
        let mut p = PolicyConfig::default();
        p.conformal.alpha = alpha;
        registry.register(name, p).unwrap();
    }

    // Progressive delivery: aggressive → moderate → relaxed → standard.
    let sequence = ["aggressive", "moderate", "relaxed", STANDARD_POLICY];
    let mut switch_events = Vec::new();

    for &target in &sequence {
        let event = registry.set_active(target).unwrap();
        let jsonl = event.to_jsonl();

        // Verify JSONL format.
        let parsed: serde_json::Value = serde_json::from_str(&jsonl).unwrap();
        assert_eq!(parsed["schema"], "policy-switch-v1");
        assert_eq!(parsed["new"], target);

        switch_events.push(event);
    }

    // Verify monotonic switch IDs.
    for w in switch_events.windows(2) {
        assert!(
            w[1].switch_id > w[0].switch_id,
            "switch IDs must be monotonically increasing"
        );
    }

    // Verify old/new chain.
    assert_eq!(switch_events[0].old_name, STANDARD_POLICY);
    assert_eq!(switch_events[0].new_name, "aggressive");
    assert_eq!(switch_events[1].old_name, "aggressive");
    assert_eq!(switch_events[1].new_name, "moderate");

    // Verify round-trip back to standard.
    assert_eq!(switch_events.last().unwrap().new_name, STANDARD_POLICY);
    assert_eq!(registry.active_name(), STANDARD_POLICY);

    // Switch count matches.
    assert_eq!(registry.switch_count(), sequence.len() as u64);

    eprintln!("--- e2e_switch_evidence_ledger ---");
    eprintln!("switches: {}", switch_events.len());
    for e in &switch_events {
        eprintln!("  #{}: {} -> {}", e.switch_id, e.old_name, e.new_name);
    }
}

// ============================================================================
// Test: Concurrent Shadow-Run Safety
// ============================================================================

#[test]
fn e2e_concurrent_shadow_run() {
    let registry = Arc::new(PolicyRegistry::new());
    let baked_in = PolicyConfig::default();

    // Register loaded policy.
    let mut loaded_policy = PolicyConfig::default();
    loaded_policy.conformal.alpha = 0.02;
    registry.register("loaded", loaded_policy.clone()).unwrap();

    // Run shadow comparison from multiple threads simultaneously.
    let divergence_count = std::sync::atomic::AtomicU64::new(0);

    std::thread::scope(|s| {
        // 4 reader threads simulating shadow-run decisions.
        for thread_id in 0..4u64 {
            let registry = Arc::clone(&registry);
            let baked_in = &baked_in;
            let loaded_policy = &loaded_policy;
            let divergence_count = &divergence_count;

            s.spawn(move || {
                for i in 0..100u64 {
                    let frame_id = thread_id * 100 + i;
                    let active = registry.active_config();

                    // Shadow: compare loaded vs baked_in.
                    let (loaded_action, baked_in_action, divergence) =
                        simulate_decision(loaded_policy, baked_in, "diff_strategy");

                    if divergence {
                        divergence_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                    }

                    // Verify active config is always readable (no panic).
                    assert!(active.conformal.alpha > 0.0);
                    let _ = (loaded_action, baked_in_action, frame_id);
                }
            });
        }

        // 1 writer thread cycling policies.
        {
            let registry = Arc::clone(&registry);
            s.spawn(move || {
                for i in 0..50 {
                    if i % 2 == 0 {
                        let _ = registry.set_active("loaded");
                    } else {
                        let _ = registry.set_active(STANDARD_POLICY);
                    }
                }
            });
        }
    });

    // Divergences were detected (loaded has different alpha).
    let total_divergences = divergence_count.load(std::sync::atomic::Ordering::Relaxed);
    assert!(
        total_divergences > 0,
        "shadow-run should detect divergences"
    );

    eprintln!("--- e2e_concurrent_shadow_run ---");
    eprintln!("total_divergences: {total_divergences} / {}", 4 * 100);
}

// ============================================================================
// Test: JSONL Schema Compliance
// ============================================================================

#[test]
fn e2e_jsonl_schema_compliance() {
    let baked_in = PolicyConfig::default();
    let mut loaded = PolicyConfig::default();
    loaded.conformal.alpha = 0.01;

    let (loaded_action, baked_in_action, divergence) =
        simulate_decision(&loaded, &baked_in, "diff_strategy");

    let log = PolicyDecisionLog {
        event: "recipe_f_policy",
        frame_id: 42,
        policy_source: "loaded".to_string(),
        delivery_stage: "canary".to_string(),
        decision_point: "diff_strategy".to_string(),
        loaded_action,
        baked_in_action,
        divergence,
        divergence_reason: Some("alpha divergence".to_string()),
        policy_version: "loaded-v1".to_string(),
        signature_valid: true,
        fallback_triggered: false,
    };

    let json = serde_json::to_string(&log).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

    // Verify all required fields exist and have correct types.
    assert!(parsed["event"].is_string());
    assert!(parsed["frame_id"].is_u64());
    assert!(parsed["policy_source"].is_string());
    assert!(parsed["delivery_stage"].is_string());
    assert!(parsed["decision_point"].is_string());
    assert!(parsed["loaded_action"].is_string());
    assert!(parsed["baked_in_action"].is_string());
    assert!(parsed["divergence"].is_boolean());
    assert!(parsed["policy_version"].is_string());
    assert!(parsed["signature_valid"].is_boolean());
    assert!(parsed["fallback_triggered"].is_boolean());

    // divergence_reason can be null or string.
    assert!(parsed["divergence_reason"].is_string() || parsed["divergence_reason"].is_null());

    // Verify specific values.
    assert_eq!(parsed["event"], "recipe_f_policy");
    assert_eq!(parsed["frame_id"], 42);
    assert_eq!(parsed["delivery_stage"], "canary");
    assert!(parsed["divergence"].as_bool().unwrap());

    // Also test the PolicyConfig JSONL format.
    let config_jsonl = baked_in.to_jsonl();
    let config_parsed: serde_json::Value = serde_json::from_str(&config_jsonl).unwrap();
    assert_eq!(config_parsed["schema"], "policy-config-v1");
}

// ============================================================================
// Test: No Dropped Frames During Fallback
// ============================================================================

#[test]
fn e2e_no_dropped_frames_during_fallback() {
    let registry = PolicyRegistry::new();
    let baked_in = PolicyConfig::default();

    // Register and activate loaded policy.
    let mut loaded = PolicyConfig::default();
    loaded.conformal.alpha = 0.01;
    registry.register("loaded", loaded.clone()).unwrap();
    registry.set_active("loaded").unwrap();

    // Simulate 10 frames with loaded policy, then fallback at frame 10.
    let mut frame_decisions = Vec::new();

    for frame_id in 0..15u64 {
        if frame_id == 10 {
            // Trigger fallback.
            registry.set_active(STANDARD_POLICY).unwrap();
        }

        let active = registry.active_config();
        let (loaded_action, baked_in_action, divergence) =
            simulate_decision(&active, &baked_in, "diff_strategy");

        let is_fallback = frame_id >= 10;
        frame_decisions.push((
            frame_id,
            loaded_action,
            baked_in_action,
            divergence,
            is_fallback,
        ));
    }

    // Verify continuous frame IDs (no gaps = no dropped frames).
    for (i, (frame_id, _, _, _, _)) in frame_decisions.iter().enumerate() {
        assert_eq!(
            *frame_id, i as u64,
            "frame IDs must be continuous (no dropped frames)"
        );
    }

    // Pre-fallback: decisions should diverge (loaded != baked_in).
    let pre_fallback: Vec<_> = frame_decisions.iter().filter(|d| !d.4).collect();
    assert!(
        pre_fallback.iter().all(|d| d.3),
        "pre-fallback decisions should diverge"
    );

    // Post-fallback: decisions should NOT diverge (active == baked_in).
    let post_fallback: Vec<_> = frame_decisions.iter().filter(|d| d.4).collect();
    assert!(
        post_fallback.iter().all(|d| !d.3),
        "post-fallback decisions must not diverge"
    );

    assert_eq!(frame_decisions.len(), 15);
    eprintln!("--- e2e_no_dropped_frames ---");
    eprintln!(
        "frames: {}, pre_fallback: {}, post_fallback: {}",
        frame_decisions.len(),
        pre_fallback.len(),
        post_fallback.len()
    );
}